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> |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 8 | #include "alarms.h" |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 9 | #include "utils/libyang.h" |
Tomáš Pecka | 190c724 | 2024-01-23 15:50:51 +0100 | [diff] [blame] | 10 | #include "utils/sysrepo.h" |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 11 | |
| 12 | using namespace std::string_literals; |
| 13 | |
| 14 | namespace { |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 15 | const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s; |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 16 | const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm"; |
| 17 | } |
| 18 | |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 19 | namespace velia::alarms { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 20 | 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] | 21 | { |
| 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áš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 26 | inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, ""); |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 27 | inputNode.newPath(alarmRpc + "/severity"s, severity); |
| 28 | inputNode.newPath(alarmRpc + "/alarm-text"s, text); |
| 29 | |
| 30 | session.sendRPC(inputNode); |
| 31 | } |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 32 | |
Tomáš Pecka | 9354bce | 2024-01-30 11:33:40 +0100 | [diff] [blame^] | 33 | void 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áš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 34 | { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 35 | const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='']"; |
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 | 14f912b | 2024-01-30 09:39:20 +0100 | [diff] [blame] | 39 | session.setItem(prefix + "/will-clear", willClear == WillClear::Yes ? "true" : "false"); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 40 | session.setItem(prefix + "/description", description); |
| 41 | |
| 42 | for (const auto& severity : severities) { |
| 43 | session.setItem(prefix + "/severity-level", severity); |
| 44 | } |
| 45 | |
Tomáš Pecka | ca85a6d | 2024-01-24 16:55:44 +0100 | [diff] [blame] | 46 | for (const auto& resource : resources) { |
| 47 | session.setItem(prefix + "/resource", resource); |
| 48 | } |
| 49 | |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 50 | session.applyChanges(); |
| 51 | } |
| 52 | |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 53 | void addResourceToInventory(sysrepo::Session session, const std::string& alarmId, const std::string& resource) |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 54 | { |
Tomáš Pecka | d694bc5 | 2024-01-30 09:53:06 +0100 | [diff] [blame] | 55 | const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='']"; |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 56 | |
Tomáš Pecka | c1aee5c | 2024-01-30 09:31:37 +0100 | [diff] [blame] | 57 | utils::ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational); |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 58 | |
| 59 | session.setItem(prefix + "/resource", resource); |
| 60 | session.applyChanges(); |
| 61 | } |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 62 | } |