blob: 7dbf147d9e991d2cae325f1f191e7074f65dc89a [file] [log] [blame]
/*
* Copyright (C) 2022 CESNET, https://photonics.cesnet.cz/
*
* Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
*
*/
#include <sysrepo-cpp/Enum.hpp>
#include "alarms.h"
#include "utils/UniqueResource.h"
#include "utils/libyang.h"
using namespace std::string_literals;
namespace {
const auto alarmInventory = "/ietf-alarms:alarms/alarm-inventory"s;
const auto alarmRpc = "/sysrepo-ietf-alarms:create-or-update-alarm";
}
namespace velia::health {
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)
{
auto inputNode = session.getContext().newPath(alarmRpc, std::nullopt);
inputNode.newPath(alarmRpc + "/resource"s, resource);
inputNode.newPath(alarmRpc + "/alarm-type-id"s, alarmId);
inputNode.newPath(alarmRpc + "/alarm-type-qualifier"s, alarmTypeQualifier.value_or(""));
inputNode.newPath(alarmRpc + "/severity"s, severity);
inputNode.newPath(alarmRpc + "/alarm-text"s, text);
session.sendRPC(inputNode);
}
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)
{
const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='" + alarmTypeQualifier.value_or("") + "']";
sysrepo::Datastore originalDS;
auto restoreDatastore = utils::make_unique_resource(
[&]() {
originalDS = session.activeDatastore();
session.switchDatastore(sysrepo::Datastore::Operational);
},
[&]() {
session.switchDatastore(originalDS);
});
session.setItem(prefix + "/will-clear", willClear ? "true" : "false");
session.setItem(prefix + "/description", description);
for (const auto& severity : severities) {
session.setItem(prefix + "/severity-level", severity);
}
session.applyChanges();
}
void addResourceToAlarmInventoryEntry(sysrepo::Session session, const std::string& alarmId, const std::optional<std::string>& alarmTypeQualifier, const std::string& resource)
{
const auto prefix = alarmInventory + "/alarm-type[alarm-type-id='" + alarmId + "'][alarm-type-qualifier='" + alarmTypeQualifier.value_or("") + "']";
sysrepo::Datastore originalDS;
auto restoreDatastore = utils::make_unique_resource(
[&]() {
originalDS = session.activeDatastore();
session.switchDatastore(sysrepo::Datastore::Operational);
},
[&]() {
session.switchDatastore(originalDS);
});
session.setItem(prefix + "/resource", resource);
session.applyChanges();
}
}