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" |
| 17 | |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 18 | enum class EntryAction { |
| 19 | Create, |
| 20 | Update, |
| 21 | }; |
| 22 | |
| 23 | inline ValueChanges constructAlarmInventoryChange(EntryAction action, |
| 24 | const std::string& alarmType, |
Tomáš Pecka | e66a193 | 2024-01-25 13:43:51 +0100 | [diff] [blame] | 25 | const std::set<std::string>& resources, |
| 26 | const std::set<std::string>& severities, |
| 27 | const std::optional<bool>& willClear, |
| 28 | const std::optional<std::string>& description) |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 29 | { |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 30 | const std::string prefix = "/ietf-alarms:alarms/alarm-inventory/alarm-type[alarm-type-id='" + alarmType + "'][alarm-type-qualifier='']"; |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 31 | |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 32 | ValueChanges ret; |
| 33 | |
| 34 | if (action == EntryAction::Create) { |
| 35 | ret.emplace(prefix + "/alarm-type-id", alarmType); |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 36 | ret.emplace(prefix + "/alarm-type-qualifier", ""); |
Tomáš Pecka | 8784429 | 2024-01-30 15:20:21 +0100 | [diff] [blame] | 37 | } |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 38 | |
Tomáš Pecka | e66a193 | 2024-01-25 13:43:51 +0100 | [diff] [blame] | 39 | if (willClear) { |
| 40 | ret.emplace(prefix + "/will-clear", *willClear ? "true" : "false"); |
| 41 | } |
| 42 | |
| 43 | if (description) { |
| 44 | ret.emplace(prefix + "/description", *description); |
| 45 | } |
| 46 | |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 47 | size_t i = 1; /* YANG uses 1-based indexing */ |
| 48 | for (const auto& severity : severities) { |
| 49 | ret.emplace(prefix + "/severity-level[" + std::to_string(i++) + "]", severity); |
| 50 | } |
| 51 | |
| 52 | i = 1; |
| 53 | for (const auto& resource : resources) { |
| 54 | ret.emplace(prefix + "/resource[" + std::to_string(i++) + "]", resource); |
| 55 | } |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 60 | /** @brief A simple mock implementation of the alarm server */ |
| 61 | struct AlarmWatcher { |
| 62 | /** @brief Poor man's /ietf-alarms:alarms/alarm-inventory implementation in C++. */ |
| 63 | struct AlarmInventory { |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 64 | using AlarmType = std::string; |
| 65 | |
Tomáš Pecka | 1211fd9 | 2024-01-24 15:24:07 +0100 | [diff] [blame] | 66 | struct AllowedResourcesAndSeverities { |
| 67 | std::set<std::string> resources; |
| 68 | std::set<std::string> severities; |
| 69 | }; |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 70 | |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 71 | std::map<AlarmType, AllowedResourcesAndSeverities> inventory; |
| 72 | |
| 73 | void add(const std::string& alarmTypeId, const std::set<std::string>& resources, const std::set<std::string>& severities); |
| 74 | 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] | 75 | }; |
| 76 | |
| 77 | AlarmInventory alarmInventory; |
| 78 | DatastoreWatcher datastoreWatcher; |
| 79 | RPCWatcher rpcWatcher; |
| 80 | |
| 81 | AlarmWatcher(sysrepo::Session& session); |
| 82 | }; |
| 83 | |
| 84 | // checks if the alarm is contained in AlarmInventory |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 85 | #define WITH_ALARM_IN_INVENTORY(INV, ALARM_TYPE, RESOURCE, SEVERITY) \ |
| 86 | LR_WITH(INV.contains(ALARM_TYPE, RESOURCE, SEVERITY)) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 87 | |
| 88 | // inserts the alarm in AlarmInventory as a side effect |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 89 | #define INSERT_INTO_INVENTORY(INV, ALARM_TYPE, RESOURCE, SEVERITIES) \ |
| 90 | LR_SIDE_EFFECT(INV.add(ALARM_TYPE, RESOURCE, SEVERITIES)) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 91 | |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 92 | #define REQUIRE_NEW_ALARM_INVENTORY_ENTRY(WATCHER, ALARM_TYPE, RESOURCES, SEVERITIES, WILL_CLEAR, DESCRIPTION) \ |
| 93 | REQUIRE_DATASTORE_CHANGE(WATCHER.datastoreWatcher, constructAlarmInventoryChange(EntryAction::Create, ALARM_TYPE, RESOURCES, SEVERITIES, WILL_CLEAR, DESCRIPTION)) \ |
| 94 | .INSERT_INTO_INVENTORY(WATCHER.alarmInventory, ALARM_TYPE, RESOURCES, SEVERITIES) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 95 | |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 96 | #define REQUIRE_NEW_ALARM_INVENTORY_RESOURCE(WATCHER, ALARM_TYPE, RESOURCES) \ |
| 97 | REQUIRE_DATASTORE_CHANGE(WATCHER.datastoreWatcher, constructAlarmInventoryChange(EntryAction::Update, ALARM_TYPE, RESOURCES, {}, std::nullopt, std::nullopt)) \ |
| 98 | .INSERT_INTO_INVENTORY(WATCHER.alarmInventory, ALARM_TYPE, RESOURCES, (std::set<std::string>{})) |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 99 | |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 100 | #define REQUIRE_NEW_ALARM(WATCHER, ALARM_TYPE, RESOURCE, SEVERITY, TEXT) \ |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 101 | REQUIRE_RPC_CALL(WATCHER.rpcWatcher, (Values{ \ |
| 102 | {"/sysrepo-ietf-alarms:create-or-update-alarm", "(unprintable)"}, \ |
| 103 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-text", TEXT}, \ |
| 104 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-type-id", ALARM_TYPE}, \ |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 105 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-type-qualifier", ""}, \ |
Tomáš Pecka | baf289c | 2024-01-24 15:12:44 +0100 | [diff] [blame] | 106 | {"/sysrepo-ietf-alarms:create-or-update-alarm/resource", RESOURCE}, \ |
| 107 | {"/sysrepo-ietf-alarms:create-or-update-alarm/severity", SEVERITY}, \ |
| 108 | })) \ |
Tomáš Pecka | 4163c0c | 2024-01-31 13:21:03 +0100 | [diff] [blame^] | 109 | .WITH_ALARM_IN_INVENTORY(WATCHER.alarmInventory, ALARM_TYPE, RESOURCE, SEVERITY) |