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" |
| 11 | #include <sysrepo-cpp/Subscription.hpp> |
| 12 | #include "sysrepo-helpers/common.h" |
| 13 | #include "sysrepo-helpers/datastore.h" |
| 14 | #include "sysrepo-helpers/rpc.h" |
| 15 | #include "test_log_setup.h" |
| 16 | |
| 17 | /** @brief A simple mock implementation of the alarm server */ |
| 18 | struct AlarmWatcher { |
| 19 | /** @brief Poor man's /ietf-alarms:alarms/alarm-inventory implementation in C++. */ |
| 20 | struct AlarmInventory { |
| 21 | std::map<std::pair<std::string, std::string>, std::set<std::string>> inventory; |
| 22 | |
| 23 | void add(const std::string& alarmTypeId, const std::string& alarmTypeQualifier, const std::string& resource); |
| 24 | bool contains(const std::string& alarmTypeId, const std::string& alarmTypeQualifier, const std::string& resource) const; |
| 25 | }; |
| 26 | |
| 27 | AlarmInventory alarmInventory; |
| 28 | DatastoreWatcher datastoreWatcher; |
| 29 | RPCWatcher rpcWatcher; |
| 30 | |
| 31 | AlarmWatcher(sysrepo::Session& session); |
| 32 | }; |
| 33 | |
| 34 | // checks if the alarm is contained in AlarmInventory |
| 35 | #define WITH_ALARM_IN_INVENTORY(INV, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE) LR_WITH(INV.contains(ALARM_TYPE, ALARM_QUALIFIER, RESOURCE)) |
| 36 | |
| 37 | // inserts the alarm in AlarmInventory as a side effect |
| 38 | #define INSERT_INTO_INVENTORY(INV, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE) LR_SIDE_EFFECT(INV.add(ALARM_TYPE, ALARM_QUALIFIER, RESOURCE)) |
| 39 | |
| 40 | #define REQUIRE_NEW_ALARM_INVENTORY_ENTRY(WATCHER, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE) \ |
| 41 | REQUIRE_DATASTORE_CHANGE(WATCHER.datastoreWatcher, \ |
| 42 | (ValueChanges{ \ |
| 43 | {"/ietf-alarms:alarms/alarm-inventory/alarm-type[alarm-type-id='" ALARM_TYPE "'][alarm-type-qualifier='" ALARM_QUALIFIER "']/alarm-type-id", ALARM_TYPE}, \ |
| 44 | {"/ietf-alarms:alarms/alarm-inventory/alarm-type[alarm-type-id='" ALARM_TYPE "'][alarm-type-qualifier='" ALARM_QUALIFIER "']/alarm-type-qualifier", ALARM_QUALIFIER}, \ |
| 45 | {"/ietf-alarms:alarms/alarm-inventory/alarm-type[alarm-type-id='" ALARM_TYPE "'][alarm-type-qualifier='" ALARM_QUALIFIER "']/resource[1]", RESOURCE}, \ |
| 46 | })) \ |
| 47 | .INSERT_INTO_INVENTORY(WATCHER.alarmInventory, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE) |
| 48 | |
| 49 | #define REQUIRE_NEW_ALARM_INVENTORY_RESOURCE(WATCHER, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE) \ |
| 50 | REQUIRE_DATASTORE_CHANGE(WATCHER.datastoreWatcher, \ |
| 51 | (ValueChanges{ \ |
| 52 | {"/ietf-alarms:alarms/alarm-inventory/alarm-type[alarm-type-id='" ALARM_TYPE "'][alarm-type-qualifier='" ALARM_QUALIFIER "']/resource[1]", RESOURCE}, \ |
| 53 | })) \ |
| 54 | .INSERT_INTO_INVENTORY(WATCHER.alarmInventory, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE) |
| 55 | |
| 56 | #define REQUIRE_NEW_ALARM(WATCHER, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE, SEVERITY, TEXT) \ |
| 57 | REQUIRE_RPC_CALL(WATCHER.rpcWatcher, (Values{ \ |
| 58 | {"/sysrepo-ietf-alarms:create-or-update-alarm", "(unprintable)"}, \ |
| 59 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-text", TEXT}, \ |
| 60 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-type-id", ALARM_TYPE}, \ |
| 61 | {"/sysrepo-ietf-alarms:create-or-update-alarm/alarm-type-qualifier", ALARM_QUALIFIER}, \ |
| 62 | {"/sysrepo-ietf-alarms:create-or-update-alarm/resource", RESOURCE}, \ |
| 63 | {"/sysrepo-ietf-alarms:create-or-update-alarm/severity", SEVERITY}, \ |
| 64 | })) \ |
| 65 | .WITH_ALARM_IN_INVENTORY(WATCHER.alarmInventory, ALARM_TYPE, ALARM_QUALIFIER, RESOURCE) |