blob: f079f8d917a6f90aaa5bd287d5e734dcbdcbe5a9 [file] [log] [blame]
Tomáš Pecka6a2334b2022-07-12 13:57:54 +02001/*
2 * Copyright (C) 2022 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
5 *
6 */
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +02007#include <sysrepo-cpp/Enum.hpp>
Tomáš Pecka6a2334b2022-07-12 13:57:54 +02008#include "alarms.h"
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +02009#include "utils/libyang.h"
Tomáš Pecka190c7242024-01-23 15:50:51 +010010#include "utils/sysrepo.h"
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020011
12using namespace std::string_literals;
13
14namespace {
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020015const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s;
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020016const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm";
17}
18
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010019namespace velia::alarms {
Tomáš Peckad694bc52024-01-30 09:53:06 +010020void push(sysrepo::Session session, const std::string& alarmId, const std::string& resource, const std::string& severity, const std::string& text)
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020021{
22 auto inputNode = session.getContext().newPath(alarmRpc, std::nullopt);
23
24 inputNode.newPath(alarmRpc + "/resource"s, resource);
25 inputNode.newPath(alarmRpc + "/alarm-type-id"s, alarmId);
Tomáš Peckad694bc52024-01-30 09:53:06 +010026 inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, "");
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020027 inputNode.newPath(alarmRpc + "/severity"s, severity);
28 inputNode.newPath(alarmRpc + "/alarm-text"s, text);
29
30 session.sendRPC(inputNode);
31}
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020032
Tomáš Pecka9354bce2024-01-30 11:33:40 +010033void pushInventory(sysrepo::Session session, const std::string& alarmId, const std::string& description, const std::vector<std::string>& resources, const std::vector<std::string>& severities, WillClear willClear)
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020034{
Tomáš Peckad694bc52024-01-30 09:53:06 +010035 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='']";
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020036
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010037 utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020038
Tomáš Pecka14f912b2024-01-30 09:39:20 +010039 session.setItem(prefix + "/will-clear", willClear == WillClear::Yes ? "true" : "false");
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020040 session.setItem(prefix + "/description", description);
41
42 for (const auto& severity : severities) {
43 session.setItem(prefix + "/severity-level", severity);
44 }
45
Tomáš Peckaca85a6d2024-01-24 16:55:44 +010046 for (const auto& resource : resources) {
47 session.setItem(prefix + "/resource", resource);
48 }
49
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020050 session.applyChanges();
51}
52
Tomáš Peckabbfc1c32024-01-31 13:58:11 +010053void addResourcesToInventory(sysrepo::Session session, const std::map<std::string, std::vector<std::string>>& resourcesPerAlarm)
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020054{
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010055 utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020056
Tomáš Peckabbfc1c32024-01-31 13:58:11 +010057 for (const auto& [alarmId, resources] : resourcesPerAlarm) {
58 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='']";
59
60 for (const auto& resource : resources) {
61 session.setItem(prefix + "/resource", resource);
62 }
Tomáš Peckaea553052024-01-30 11:49:59 +010063 }
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020064 session.applyChanges();
65}
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020066}