blob: 7dbf147d9e991d2cae325f1f191e7074f65dc89a [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>
Tomáš Pecka6a2334b2022-07-12 13:57:54 +02008#include "alarms.h"
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +02009#include "utils/UniqueResource.h"
10#include "utils/libyang.h"
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020011
12using namespace std::string_literals;
13
14namespace {
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020015const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s;
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020016const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm";
17}
18
19namespace velia::health {
20void 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áš Pecka8e5a2d32022-08-22 17:48:58 +020032
33void 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áš Pecka18df5d02023-04-14 11:23:05 +020038 auto restoreDatastore = utils::make_unique_resource(
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020039 [&]() {
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
57void 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áš Pecka18df5d02023-04-14 11:23:05 +020062 auto restoreDatastore = utils::make_unique_resource(
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020063 [&]() {
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áš Pecka6a2334b2022-07-12 13:57:54 +020074}