blob: 67779eb5ac2a1bdb0f3106291177b5f90c96fbeb [file] [log] [blame]
Tomáš Peckaa18702d2021-01-25 18:07:23 +01001/*
2 * Copyright (C) 2016-2018 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Jan Kundrát <jan.kundrat@cesnet.cz>
5 *
6*/
7
8#pragma once
9
10#include <chrono>
11#include <mutex>
12#include <sysrepo-cpp/Session.hpp>
13#include "test_log_setup.h"
14
Václav Kubernát7efd6d52021-11-09 01:31:11 +010015/** @short YANG notifications */
Tomáš Peckaa18702d2021-01-25 18:07:23 +010016class EventWatcher {
17public:
Tomáš Peckaa18702d2021-01-25 18:07:23 +010018 struct Event {
19 std::string xPath;
20 std::map<std::string, std::string> data;
21 std::chrono::time_point<std::chrono::steady_clock> received;
22 };
23
Tomáš Pecka76fa2ff2021-03-15 16:28:06 +010024 explicit EventWatcher(std::function<void(Event)> callback);
25 ~EventWatcher();
Václav Kubernát7efd6d52021-11-09 01:31:11 +010026 void operator()(sysrepo::Session session, uint32_t subscriptionId, const sysrepo::NotificationType type, const std::optional<libyang::DataNode> notificationTree, const sysrepo::NotificationTimeStamp timestamp);
Tomáš Pecka76fa2ff2021-03-15 16:28:06 +010027
Tomáš Peckaa18702d2021-01-25 18:07:23 +010028 Event peek(std::vector<Event>::size_type index) const;
29 std::vector<Event>::size_type count() const;
30
31private:
Tomáš Pecka76fa2ff2021-03-15 16:28:06 +010032 std::function<void(Event)> notifRecvCb;
Tomáš Peckaa18702d2021-01-25 18:07:23 +010033 mutable std::shared_ptr<std::mutex> mutex = std::make_shared<std::mutex>();
34 std::shared_ptr<std::vector<Event>> events = std::make_shared<std::vector<Event>>();
35};