Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 7 | #include <sysrepo-cpp/Enum.hpp> |
Jan Kundrát | 58855c4 | 2024-09-25 19:31:56 +0200 | [diff] [blame] | 8 | #include <spdlog/spdlog.h> |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 9 | #include "alarms.h" |
Jan Kundrát | b98b4f9 | 2024-10-17 17:39:24 +0200 | [diff] [blame] | 10 | #include "utils/benchmark.h" |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 11 | #include "utils/libyang.h" |
Tomáš Pecka | 190c724 | 2024-01-23 15:50:51 +0100 | [diff] [blame] | 12 | #include "utils/sysrepo.h" |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 13 | |
| 14 | using namespace std::string_literals; |
| 15 | |
| 16 | namespace { |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 17 | const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s; |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 18 | const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm"; |
| 19 | } |
| 20 | |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 21 | namespace velia::alarms { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 22 | void push(sysrepo::Session session, const std::string& alarmId, const std::string& resource, const std::string& severity, const std::string& text) |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 23 | { |
Jan Kundrát | b98b4f9 | 2024-10-17 17:39:24 +0200 | [diff] [blame] | 24 | WITH_TIME_MEASUREMENT{}; |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 25 | 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áš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 29 | inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, ""); |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 30 | inputNode.newPath(alarmRpc + "/severity"s, severity); |
| 31 | inputNode.newPath(alarmRpc + "/alarm-text"s, text); |
| 32 | |
Jan Kundrát | 58855c4 | 2024-09-25 19:31:56 +0200 | [diff] [blame] | 33 | spdlog::get("main")->trace("alarms::push"); |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 34 | session.sendRPC(inputNode); |
| 35 | } |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 36 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 37 | void pushInventory(sysrepo::Session session, const std::vector<AlarmInventoryEntry>& entries) |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 38 | { |
Jan Kundrát | b98b4f9 | 2024-10-17 17:39:24 +0200 | [diff] [blame] | 39 | WITH_TIME_MEASUREMENT{}; |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 40 | utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 41 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 42 | for (const auto& entry: entries) { |
| 43 | const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + entry.alarmType + "'][alarm-type-qualifier='']"; |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 44 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 45 | session.setItem(prefix + "/will-clear", entry.willClear == WillClear::Yes ? "true" : "false"); |
| 46 | session.setItem(prefix + "/description", entry.description); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 47 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 48 | 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áš Pecka | ca85a6d | 2024-01-24 16:55:44 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Jan Kundrát | 42c4d7e | 2024-10-17 14:41:31 +0200 | [diff] [blame] | 57 | spdlog::get("main")->trace("alarms::pushInventory: {}", *session.getPendingChanges()->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings)); |
Jan Kundrát | b98b4f9 | 2024-10-17 17:39:24 +0200 | [diff] [blame] | 58 | WITH_TIME_MEASUREMENT{"pushInventory/applyChanges"}; |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 59 | session.applyChanges(); |
| 60 | } |
| 61 | |
Tomáš Pecka | bbfc1c3 | 2024-01-31 13:58:11 +0100 | [diff] [blame] | 62 | void addResourcesToInventory(sysrepo::Session session, const std::map<std::string, std::vector<std::string>>& resourcesPerAlarm) |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 63 | { |
Jan Kundrát | b98b4f9 | 2024-10-17 17:39:24 +0200 | [diff] [blame] | 64 | WITH_TIME_MEASUREMENT{}; |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 65 | utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 66 | |
Tomáš Pecka | bbfc1c3 | 2024-01-31 13:58:11 +0100 | [diff] [blame] | 67 | 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áš Pecka | ea55305 | 2024-01-30 11:49:59 +0100 | [diff] [blame] | 73 | } |
Jan Kundrát | 42c4d7e | 2024-10-17 14:41:31 +0200 | [diff] [blame] | 74 | spdlog::get("main")->trace("alarms::addResourcesToInventory: {}", *session.getPendingChanges()->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings)); |
Jan Kundrát | b98b4f9 | 2024-10-17 17:39:24 +0200 | [diff] [blame] | 75 | WITH_TIME_MEASUREMENT{"addResourcesToInventory/applyChanges"}; |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 76 | session.applyChanges(); |
| 77 | } |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 78 | |
| 79 | AlarmInventoryEntry::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áš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 87 | } |