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" |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 10 | #include "utils/libyang.h" |
Tomáš Pecka | 190c724 | 2024-01-23 15:50:51 +0100 | [diff] [blame] | 11 | #include "utils/sysrepo.h" |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 12 | |
| 13 | using namespace std::string_literals; |
| 14 | |
| 15 | namespace { |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 16 | const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s; |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 17 | const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm"; |
| 18 | } |
| 19 | |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 20 | namespace velia::alarms { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 21 | 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] | 22 | { |
| 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áš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 27 | inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, ""); |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 28 | inputNode.newPath(alarmRpc + "/severity"s, severity); |
| 29 | inputNode.newPath(alarmRpc + "/alarm-text"s, text); |
| 30 | |
Jan Kundrát | 58855c4 | 2024-09-25 19:31:56 +0200 | [diff] [blame] | 31 | spdlog::get("main")->trace("alarms::push"); |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 32 | session.sendRPC(inputNode); |
| 33 | } |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 34 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 35 | void pushInventory(sysrepo::Session session, const std::vector<AlarmInventoryEntry>& entries) |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 36 | { |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 37 | utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 38 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 39 | for (const auto& entry: entries) { |
| 40 | 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] | 41 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 42 | session.setItem(prefix + "/will-clear", entry.willClear == WillClear::Yes ? "true" : "false"); |
| 43 | session.setItem(prefix + "/description", entry.description); |
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 | 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áš Pecka | ca85a6d | 2024-01-24 16:55:44 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Jan Kundrát | 42c4d7e | 2024-10-17 14:41:31 +0200 | [diff] [blame^] | 54 | spdlog::get("main")->trace("alarms::pushInventory: {}", *session.getPendingChanges()->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings)); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 55 | session.applyChanges(); |
| 56 | } |
| 57 | |
Tomáš Pecka | bbfc1c3 | 2024-01-31 13:58:11 +0100 | [diff] [blame] | 58 | 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] | 59 | { |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 60 | utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 61 | |
Tomáš Pecka | bbfc1c3 | 2024-01-31 13:58:11 +0100 | [diff] [blame] | 62 | 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áš Pecka | ea55305 | 2024-01-30 11:49:59 +0100 | [diff] [blame] | 68 | } |
Jan Kundrát | 42c4d7e | 2024-10-17 14:41:31 +0200 | [diff] [blame^] | 69 | spdlog::get("main")->trace("alarms::addResourcesToInventory: {}", *session.getPendingChanges()->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings)); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 70 | session.applyChanges(); |
| 71 | } |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame] | 72 | |
| 73 | AlarmInventoryEntry::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áš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 81 | } |