blob: bc654f5f0e2b7b4b2219b909c67f460071b2263b [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#include "events.h"
9
10namespace {
Václav Kubernát7efd6d52021-11-09 01:31:11 +010011std::string sr_ev_notif_type_to_string(const sysrepo::NotificationType notif_type)
Tomáš Peckaa18702d2021-01-25 18:07:23 +010012{
Václav Kubernát7efd6d52021-11-09 01:31:11 +010013 std::ostringstream oss;
14 oss << notif_type;
15 return oss.str();
Tomáš Peckaa18702d2021-01-25 18:07:23 +010016}
17}
18
Tomáš Pecka76fa2ff2021-03-15 16:28:06 +010019EventWatcher::EventWatcher(std::function<void(Event)> callback)
20 : notifRecvCb(std::move(callback))
21{
22}
23
Tomáš Peckaa18702d2021-01-25 18:07:23 +010024EventWatcher::~EventWatcher()
25{
26}
27
28void EventWatcher::operator()(
Václav Kubernát7efd6d52021-11-09 01:31:11 +010029 sysrepo::Session,
30 uint32_t ,
31 const sysrepo::NotificationType type,
32 const std::optional<libyang::DataNode> notificationTree,
33 const sysrepo::NotificationTimeStamp timestamp)
Tomáš Peckaa18702d2021-01-25 18:07:23 +010034{
35 Event e;
Václav Kubernát7efd6d52021-11-09 01:31:11 +010036 e.xPath = std::string{notificationTree ? std::string{notificationTree->path()} : "<no-xpath>"};
Tomáš Peckaa18702d2021-01-25 18:07:23 +010037 e.received = std::chrono::steady_clock::now();
Tomáš Peckaa18702d2021-01-25 18:07:23 +010038 auto log = spdlog::get("main");
Václav Kubernát7efd6d52021-11-09 01:31:11 +010039 log->info("SR event {} {} {}", sr_ev_notif_type_to_string(type), timestamp.time_since_epoch().count(), e.xPath);
Tomáš Peckaa18702d2021-01-25 18:07:23 +010040
Václav Kubernát7efd6d52021-11-09 01:31:11 +010041 if (notificationTree) {
42 for (const auto& node : notificationTree->childrenDfs()) {
43 auto path = std::string{node.path()};
44 auto val = [&] {
45 if (node.schema().nodeType() == libyang::NodeType::Leaf) {
46 return std::string{node.asTerm().valueStr()};
47 }
48
49 return std::string{""};
50 }();
51 e.data[path] = val;
52 log->debug(" {}: {}", path, val);
53 }
Tomáš Peckaa18702d2021-01-25 18:07:23 +010054 }
55
Tomáš Pecka76fa2ff2021-03-15 16:28:06 +010056 {
57 std::lock_guard<std::mutex> lock(*mutex);
58 events->push_back(e);
59 }
60
Václav Kubernát7efd6d52021-11-09 01:31:11 +010061 switch (type) {
62 case sysrepo::NotificationType::Realtime:
63 case sysrepo::NotificationType::Replay:
64 notifRecvCb(e);
65 break;
66 case sysrepo::NotificationType::ReplayComplete:
67 case sysrepo::NotificationType::Terminated:
68 case sysrepo::NotificationType::Modified:
69 case sysrepo::NotificationType::Suspended:
70 case sysrepo::NotificationType::Resumed:
71 break;
72 }
73
Tomáš Peckaa18702d2021-01-25 18:07:23 +010074}
75
76std::vector<EventWatcher::Event>::size_type EventWatcher::count() const
77{
78 std::lock_guard<std::mutex> lock(*mutex);
79 return events->size();
80}
81
82EventWatcher::Event EventWatcher::peek(const std::vector<EventWatcher::Event>::size_type index) const
83{
84 std::lock_guard<std::mutex> lock(*mutex);
85 return (*events)[index];
86}