blob: 280004ba8cb23206af24e1962890e553f3333dcd [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"
Jan Kundrátb98b4f92024-10-17 17:39:24 +020010#include "utils/benchmark.h"
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020011#include "utils/libyang.h"
Tomáš Pecka190c7242024-01-23 15:50:51 +010012#include "utils/sysrepo.h"
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020013
14using namespace std::string_literals;
15
16namespace {
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020017const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s;
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020018const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm";
19}
20
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010021namespace velia::alarms {
Tomáš Peckad694bc52024-01-30 09:53:06 +010022void 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 +020023{
Jan Kundrátb98b4f92024-10-17 17:39:24 +020024 WITH_TIME_MEASUREMENT{};
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020025 auto inputNode = session.getContext().newPath(alarmRpc, std::nullopt);
26
27 inputNode.newPath(alarmRpc + "/resource"s, resource);
28 inputNode.newPath(alarmRpc + "/alarm-type-id"s, alarmId);
Tomáš Peckad694bc52024-01-30 09:53:06 +010029 inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, "");
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020030 inputNode.newPath(alarmRpc + "/severity"s, severity);
31 inputNode.newPath(alarmRpc + "/alarm-text"s, text);
32
Jan Kundrát58855c42024-09-25 19:31:56 +020033 spdlog::get("main")->trace("alarms::push");
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020034 session.sendRPC(inputNode);
35}
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020036
Tomáš Pecka9bd61272024-01-31 15:08:13 +010037void pushInventory(sysrepo::Session session, const std::vector<AlarmInventoryEntry>& entries)
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020038{
Jan Kundrátb98b4f92024-10-17 17:39:24 +020039 WITH_TIME_MEASUREMENT{};
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010040 utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020041
Tomáš Pecka9bd61272024-01-31 15:08:13 +010042 for (const auto& entry: entries) {
43 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + entry.alarmType + "'][alarm-type-qualifier='']";
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020044
Tomáš Pecka9bd61272024-01-31 15:08:13 +010045 session.setItem(prefix + "/will-clear", entry.willClear == WillClear::Yes ? "true" : "false");
46 session.setItem(prefix + "/description", entry.description);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020047
Tomáš Pecka9bd61272024-01-31 15:08:13 +010048 for (const auto& severity : entry.severities) {
49 session.setItem(prefix + "/severity-level", severity);
50 }
51
52 for (const auto& resource : entry.resources) {
53 session.setItem(prefix + "/resource", resource);
54 }
Tomáš Peckaca85a6d2024-01-24 16:55:44 +010055 }
56
Jan Kundrát42c4d7e2024-10-17 14:41:31 +020057 spdlog::get("main")->trace("alarms::pushInventory: {}", *session.getPendingChanges()->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings));
Jan Kundrátb98b4f92024-10-17 17:39:24 +020058 WITH_TIME_MEASUREMENT{"pushInventory/applyChanges"};
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020059 session.applyChanges();
60}
61
Tomáš Peckabbfc1c32024-01-31 13:58:11 +010062void addResourcesToInventory(sysrepo::Session session, const std::map<std::string, std::vector<std::string>>& resourcesPerAlarm)
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020063{
Jan Kundrátb98b4f92024-10-17 17:39:24 +020064 WITH_TIME_MEASUREMENT{};
Tomáš Peckac1aee5c2024-01-30 09:31:37 +010065 utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020066
Tomáš Peckabbfc1c32024-01-31 13:58:11 +010067 for (const auto& [alarmId, resources] : resourcesPerAlarm) {
68 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='']";
69
70 for (const auto& resource : resources) {
71 session.setItem(prefix + "/resource", resource);
72 }
Tomáš Peckaea553052024-01-30 11:49:59 +010073 }
Jan Kundrát42c4d7e2024-10-17 14:41:31 +020074 spdlog::get("main")->trace("alarms::addResourcesToInventory: {}", *session.getPendingChanges()->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings));
Jan Kundrátb98b4f92024-10-17 17:39:24 +020075 WITH_TIME_MEASUREMENT{"addResourcesToInventory/applyChanges"};
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020076 session.applyChanges();
77}
Tomáš Pecka9bd61272024-01-31 15:08:13 +010078
79AlarmInventoryEntry::AlarmInventoryEntry(const std::string& alarmType, const std::string& description, const std::vector<std::string>& resources, const std::vector<std::string>& severities, WillClear willClear)
80 : alarmType(alarmType)
81 , description(description)
82 , resources(resources)
83 , severities(severities)
84 , willClear(willClear)
85{
86}
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020087}