blob: adc688b7b32f1f80da17173aafb7cb654762c6f7 [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>
Jan Kundrát58855c42024-09-25 19:31:56 +02008#include <spdlog/spdlog.h>
Tomáš Pecka6a2334b2022-07-12 13:57:54 +02009#include "alarms.h"
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020010#include "utils/libyang.h"
Tomáš Pecka190c7242024-01-23 15:50:51 +010011#include "utils/sysrepo.h"
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020012
13using namespace std::string_literals;
14
15namespace {
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020016const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s;
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020017const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm";
18}
19
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010020namespace velia::alarms {
Tomáš Peckad694bc52024-01-30 09:53:06 +010021void 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 +020022{
23 auto inputNode = session.getContext().newPath(alarmRpc, std::nullopt);
24
25 inputNode.newPath(alarmRpc + "/resource"s, resource);
26 inputNode.newPath(alarmRpc + "/alarm-type-id"s, alarmId);
Tomáš Peckad694bc52024-01-30 09:53:06 +010027 inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, "");
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020028 inputNode.newPath(alarmRpc + "/severity"s, severity);
29 inputNode.newPath(alarmRpc + "/alarm-text"s, text);
30
Jan Kundrát58855c42024-09-25 19:31:56 +020031 spdlog::get("main")->trace("alarms::push");
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020032 session.sendRPC(inputNode);
33}
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020034
Tomáš Pecka9bd61272024-01-31 15:08:13 +010035void pushInventory(sysrepo::Session session, const std::vector<AlarmInventoryEntry>& entries)
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áš Pecka9bd61272024-01-31 15:08:13 +010039 for (const auto& entry: entries) {
40 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + entry.alarmType + "'][alarm-type-qualifier='']";
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020041
Tomáš Pecka9bd61272024-01-31 15:08:13 +010042 session.setItem(prefix + "/will-clear", entry.willClear == WillClear::Yes ? "true" : "false");
43 session.setItem(prefix + "/description", entry.description);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020044
Tomáš Pecka9bd61272024-01-31 15:08:13 +010045 for (const auto& severity : entry.severities) {
46 session.setItem(prefix + "/severity-level", severity);
47 }
48
49 for (const auto& resource : entry.resources) {
50 session.setItem(prefix + "/resource", resource);
51 }
Tomáš Peckaca85a6d2024-01-24 16:55:44 +010052 }
53
Jan Kundrát58855c42024-09-25 19:31:56 +020054 spdlog::get("main")->trace("alarms::pushInventory");
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020055 session.applyChanges();
56}
57
Tomáš Peckabbfc1c32024-01-31 13:58:11 +010058void addResourcesToInventory(sysrepo::Session session, const std::map<std::string, std::vector<std::string>>& resourcesPerAlarm)
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020059{
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010060 utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020061
Tomáš Peckabbfc1c32024-01-31 13:58:11 +010062 for (const auto& [alarmId, resources] : resourcesPerAlarm) {
63 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='']";
64
65 for (const auto& resource : resources) {
66 session.setItem(prefix + "/resource", resource);
67 }
Tomáš Peckaea553052024-01-30 11:49:59 +010068 }
Jan Kundrát58855c42024-09-25 19:31:56 +020069 spdlog::get("main")->trace("alarms::addResourcesToInventory");
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020070 session.applyChanges();
71}
Tomáš Pecka9bd61272024-01-31 15:08:13 +010072
73AlarmInventoryEntry::AlarmInventoryEntry(const std::string& alarmType, const std::string& description, const std::vector<std::string>& resources, const std::vector<std::string>& severities, WillClear willClear)
74 : alarmType(alarmType)
75 , description(description)
76 , resources(resources)
77 , severities(severities)
78 , willClear(willClear)
79{
80}
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020081}