Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include "trompeloeil_doctest.h" |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 11 | #include <optional> |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 12 | #include <sysrepo-cpp/Subscription.hpp> |
| 13 | #include "sysrepo-helpers/common.h" |
| 14 | #include "sysrepo-helpers/datastore.h" |
| 15 | #include "sysrepo-helpers/rpc.h" |
| 16 | #include "test_log_setup.h" |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 17 | #include "utils/alarms.h" |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 18 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 19 | inline ValueChanges constructAlarmInventoryChange(const std::vector<velia::alarms::AlarmInventoryEntry>& entries) |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 20 | { |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 21 | ValueChanges ret; |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 22 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 23 | for (const auto& entry : entries) { |
| 24 | const std::string prefix = "/ietf-alarms:alarms/alarm-inventory/alarm-type[alarm-type-id='" + entry.alarmType + "'][alarm-type-qualifier='']"; |
Tomáš Pecka | e66a193 | 2024-01-25 13:43:51 +0100 | [diff] [blame] | 25 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 26 | ret.emplace(prefix + "/alarm-type-id", entry.alarmType); |
| 27 | ret.emplace(prefix + "/alarm-type-qualifier", ""); |
Tomáš Pecka | e66a193 | 2024-01-25 13:43:51 +0100 | [diff] [blame] | 28 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 29 | ret.emplace(prefix + "/will-clear", entry.willClear ? "true" : "false"); |
| 30 | ret.emplace(prefix + "/description", entry.description); |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 31 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 32 | size_t i = 1; /* YANG uses 1-based indexing */ |
| 33 | for (const auto& severity : entry.severities) { |
| 34 | ret.emplace(prefix + "/severity-level[" + std::to_string(i++) + "]", severity); |
| 35 | } |
| 36 | |
| 37 | i = 1; |
| 38 | for (const auto& resource : entry.resources) { |
| 39 | ret.emplace(prefix + "/resource[" + std::to_string(i++) + "]", resource); |
| 40 | } |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
Tomáš Pecka | 4a0c6ea | 2024-01-31 15:14:55 +0100 | [diff] [blame] | 46 | inline ValueChanges constructAlarmInventoryResourceChange(const std::vector<std::string>& alarmTypeIds, const std::vector<std::string>& resources) { |
Tomáš Pecka | 52d5046 | 2024-01-31 13:37:29 +0100 | [diff] [blame] | 47 | ValueChanges ret; |
| 48 | |
| 49 | for (const auto& alarmType : alarmTypeIds) { |
| 50 | const std::string prefix = "/ietf-alarms:alarms/alarm-inventory/alarm-type[alarm-type-id='" + alarmType + "'][alarm-type-qualifier='']"; |
| 51 | |
| 52 | size_t i = 1; |
| 53 | for (const auto& resource : resources) { |
| 54 | ret.emplace(prefix + "/resource[" + std::to_string(i++) + "]", resource); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return ret; |
| 59 | } |
| 60 | |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 61 | /** @brief A simple mock implementation of the alarm server */ |
| 62 | struct AlarmWatcher { |
| 63 | /** @brief Poor man's /ietf-alarms:alarms/alarm-inventory implementation in C++. */ |
| 64 | struct AlarmInventory { |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame] | 65 | using AlarmType = std::string; |
| 66 | |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 67 | struct AllowedResourcesAndSeverities { |
| 68 | std::set<std::string> resources; |
| 69 | std::set<std::string> severities; |
| 70 | }; |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 71 | |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame] | 72 | std::map<AlarmType, AllowedResourcesAndSeverities> inventory; |
| 73 | |
Tomáš Pecka | 4a0c6ea | 2024-01-31 15:14:55 +0100 | [diff] [blame] | 74 | void add(const std::vector<std::string>& alarmTypeIds, const std::vector<std::string>& resources, const std::vector<std::string>& severities); |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 75 | void add(const std::vector<velia::alarms::AlarmInventoryEntry>& entries); |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame] | 76 | bool contains(const std::string& alarmTypeId, const std::optional<std::string>& resource, const std::optional<std::string>& severity) const; |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | AlarmInventory alarmInventory; |
| 80 | DatastoreWatcher datastoreWatcher; |
| 81 | RPCWatcher rpcWatcher; |
| 82 | |
| 83 | AlarmWatcher(sysrepo::Session& session); |
| 84 | }; |
| 85 | |
| 86 | // checks if the alarm is contained in AlarmInventory |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame] | 87 | #define WITH_ALARM_IN_INVENTORY(INV, ALARM_TYPE, RESOURCE, SEVERITY) \ |
| 88 | LR_WITH(INV.contains(ALARM_TYPE, RESOURCE, SEVERITY)) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 89 | |
| 90 | // inserts the alarm in AlarmInventory as a side effect |
Tomáš Pecka | 52d5046 | 2024-01-31 13:37:29 +0100 | [diff] [blame] | 91 | #define INSERT_INTO_INVENTORY(INV, ALARM_TYPES, RESOURCES, SEVERITIES) \ |
| 92 | LR_SIDE_EFFECT(INV.add(ALARM_TYPES, RESOURCES, SEVERITIES)) |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 93 | #define INSERT_INTO_INVENTORY_MANY(INV, ALARMS) \ |
| 94 | LR_SIDE_EFFECT(INV.add(ALARMS)) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 95 | |
Tomáš Pecka | 9bd6127 | 2024-01-31 15:08:13 +0100 | [diff] [blame^] | 96 | #define REQUIRE_NEW_ALARM_INVENTORY_ENTRIES(WATCHER, ALARMS) \ |
| 97 | REQUIRE_DATASTORE_CHANGE(WATCHER.datastoreWatcher, constructAlarmInventoryChange(ALARMS)) \ |
| 98 | .INSERT_INTO_INVENTORY_MANY(WATCHER.alarmInventory, ALARMS) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 99 | |
Tomáš Pecka | 52d5046 | 2024-01-31 13:37:29 +0100 | [diff] [blame] | 100 | #define REQUIRE_NEW_ALARM_INVENTORY_RESOURCES(WATCHER, ALARM_TYPES, RESOURCES) \ |
| 101 | REQUIRE_DATASTORE_CHANGE(WATCHER.datastoreWatcher, constructAlarmInventoryResourceChange(ALARM_TYPES, RESOURCES)) \ |
Tomáš Pecka | 4a0c6ea | 2024-01-31 15:14:55 +0100 | [diff] [blame] | 102 | .INSERT_INTO_INVENTORY(WATCHER.alarmInventory, ALARM_TYPES, RESOURCES, (std::vector<std::string>{})) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 103 | |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame] | 104 | #define REQUIRE_NEW_ALARM(WATCHER, ALARM_TYPE, RESOURCE, SEVERITY, TEXT) \ |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 105 | REQUIRE_RPC_CALL(WATCHER.rpcWatcher, (Values{ \ |
| 106 | {"/sysrepo-ietf-alarms:create-or-update-alarm", "(unprintable)"}, \ |
| 107 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-text", TEXT}, \ |
| 108 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-type-id", ALARM_TYPE}, \ |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame] | 109 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-type-qualifier", ""}, \ |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 110 | {"/sysrepo-ietf-alarms:create-or-update-alarm/resource", RESOURCE}, \ |
| 111 | {"/sysrepo-ietf-alarms:create-or-update-alarm/severity", SEVERITY}, \ |
| 112 | })) \ |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame] | 113 | .WITH_ALARM_IN_INVENTORY(WATCHER.alarmInventory, ALARM_TYPE, RESOURCE, SEVERITY) |