Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 10 | namespace { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 11 | std::string sr_ev_notif_type_to_string(const sysrepo::NotificationType notif_type) |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 12 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 13 | std::ostringstream oss; |
| 14 | oss << notif_type; |
| 15 | return oss.str(); |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 16 | } |
| 17 | } |
| 18 | |
Tomáš Pecka | 76fa2ff | 2021-03-15 16:28:06 +0100 | [diff] [blame] | 19 | EventWatcher::EventWatcher(std::function<void(Event)> callback) |
| 20 | : notifRecvCb(std::move(callback)) |
| 21 | { |
| 22 | } |
| 23 | |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 24 | EventWatcher::~EventWatcher() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | void EventWatcher::operator()( |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 29 | sysrepo::Session, |
| 30 | uint32_t , |
| 31 | const sysrepo::NotificationType type, |
| 32 | const std::optional<libyang::DataNode> notificationTree, |
| 33 | const sysrepo::NotificationTimeStamp timestamp) |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 34 | { |
| 35 | Event e; |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 36 | e.xPath = std::string{notificationTree ? std::string{notificationTree->path()} : "<no-xpath>"}; |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 37 | e.received = std::chrono::steady_clock::now(); |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 38 | auto log = spdlog::get("main"); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 39 | log->info("SR event {} {} {}", sr_ev_notif_type_to_string(type), timestamp.time_since_epoch().count(), e.xPath); |
Tomáš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 40 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 41 | 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áš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Tomáš Pecka | 76fa2ff | 2021-03-15 16:28:06 +0100 | [diff] [blame] | 56 | { |
| 57 | std::lock_guard<std::mutex> lock(*mutex); |
| 58 | events->push_back(e); |
| 59 | } |
| 60 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 61 | 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áš Pecka | a18702d | 2021-01-25 18:07:23 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | std::vector<EventWatcher::Event>::size_type EventWatcher::count() const |
| 77 | { |
| 78 | std::lock_guard<std::mutex> lock(*mutex); |
| 79 | return events->size(); |
| 80 | } |
| 81 | |
| 82 | EventWatcher::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 | } |