blob: 24d9d756cfcdf4f4afddc43980023528319d8e8f [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/libyang.h"
Tomáš Pecka190c7242024-01-23 15:50:51 +010010#include "utils/sysrepo.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
Tomáš Peckaad3247f2023-05-11 15:56:26 +020019namespace velia::utils {
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020020void 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
Tomáš Peckaca85a6d2024-01-24 16:55:44 +010033void 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, const std::vector<std::string>& resources)
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020034{
35 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='" + alarmTypeQualifier.value_or("") + "']";
36
Tomáš Pecka190c7242024-01-23 15:50:51 +010037 ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020038
39 session.setItem(prefix + "/will-clear", willClear ? "true" : "false");
40 session.setItem(prefix + "/description", description);
41
42 for (const auto& severity : severities) {
43 session.setItem(prefix + "/severity-level", severity);
44 }
45
Tomáš Peckaca85a6d2024-01-24 16:55:44 +010046 for (const auto& resource : resources) {
47 session.setItem(prefix + "/resource", resource);
48 }
49
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020050 session.applyChanges();
51}
52
53void addResourceToAlarmInventoryEntry(sysrepo::Session session, const std::string& alarmId, const std::optional<std::string>& alarmTypeQualifier, const std::string& resource)
54{
55 const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='" + alarmTypeQualifier.value_or("") + "']";
56
Tomáš Pecka190c7242024-01-23 15:50:51 +010057 ScopedDatastoreSwitch s(session, sysrepo::Datastore::Operational);
Tomáš Pecka8e5a2d32022-08-22 17:48:58 +020058
59 session.setItem(prefix + "/resource", resource);
60 session.applyChanges();
61}
Tomáš Pecka6a2334b2022-07-12 13:57:54 +020062}