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/UniqueResource.h" |
| 10 | #include "utils/libyang.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 | |
| 19 | namespace velia::health { |
| 20 | void createOrUpdateAlarm(sysrepo::Session session, const std::string& alarmId, const std::optional<std::string>& alarmTypeQualifier, const std::string& resource, const std::string& severity, const std::string& text) |
| 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); |
| 26 | inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, alarmTypeQualifier.value_or("")); |
| 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 | |
| 33 | void createOrUpdateAlarmInventoryEntry(sysrepo::Session session, const std::string& alarmId, const std::optional<std::string>& alarmTypeQualifier, const std::vector<std::string>& severities, bool willClear, const std::string& description) |
| 34 | { |
| 35 | const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='" + alarmTypeQualifier.value_or("") + "']"; |
| 36 | |
| 37 | sysrepo::Datastore originalDS; |
Tomáš Pecka | 18df5d0 | 2023-04-14 11:23:05 +0200 | [diff] [blame^] | 38 | auto restoreDatastore = utils::make_unique_resource( |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 39 | [&]() { |
| 40 | originalDS = session.activeDatastore(); |
| 41 | session.switchDatastore(sysrepo::Datastore::Operational); |
| 42 | }, |
| 43 | [&]() { |
| 44 | session.switchDatastore(originalDS); |
| 45 | }); |
| 46 | |
| 47 | session.setItem(prefix + "/will-clear", willClear ? "true" : "false"); |
| 48 | session.setItem(prefix + "/description", description); |
| 49 | |
| 50 | for (const auto& severity : severities) { |
| 51 | session.setItem(prefix + "/severity-level", severity); |
| 52 | } |
| 53 | |
| 54 | session.applyChanges(); |
| 55 | } |
| 56 | |
| 57 | void addResourceToAlarmInventoryEntry(sysrepo::Session session, const std::string& alarmId, const std::optional<std::string>& alarmTypeQualifier, const std::string& resource) |
| 58 | { |
| 59 | const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='" + alarmTypeQualifier.value_or("") + "']"; |
| 60 | |
| 61 | sysrepo::Datastore originalDS; |
Tomáš Pecka | 18df5d0 | 2023-04-14 11:23:05 +0200 | [diff] [blame^] | 62 | auto restoreDatastore = utils::make_unique_resource( |
Tomáš Pecka | 8e5a2d3 | 2022-08-22 17:48:58 +0200 | [diff] [blame] | 63 | [&]() { |
| 64 | originalDS = session.activeDatastore(); |
| 65 | session.switchDatastore(sysrepo::Datastore::Operational); |
| 66 | }, |
| 67 | [&]() { |
| 68 | session.switchDatastore(originalDS); |
| 69 | }); |
| 70 | |
| 71 | session.setItem(prefix + "/resource", resource); |
| 72 | session.applyChanges(); |
| 73 | } |
Tomáš Pecka | 6a2334b | 2022-07-12 13:57:54 +0200 | [diff] [blame] | 74 | } |