blob: 7cd5cf3fc9d4e4f2373d9e7730380669c1b8e1a4 [file] [log] [blame]
Václav Kubernát73109382018-09-14 19:52:03 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#include <sysrepo-cpp/Session.hpp>
10#include "sysrepo_subscription.hpp"
11
12
13class MyCallback : public sysrepo::Callback {
14public:
15 MyCallback(const std::string& moduleName, Recorder* rec)
16 : m_moduleName(moduleName)
17 , m_recorder(rec)
18 {
19 }
20
21 int module_change(sysrepo::S_Session sess, const char* module_name, sr_notif_event_t event, void*) override
22 {
23 using namespace std::string_literals;
24 auto xpath = "/"s + module_name + ":*";
25 auto it = sess->get_changes_iter(xpath.c_str());
26
27 if (event == SR_EV_APPLY)
28 return SR_ERR_OK;
29
30 while (auto change = sess->get_change_next(it)) {
Václav Kubernát69aabe92020-01-24 16:53:12 +010031 auto xpath = (change->new_val() ? change->new_val() : change->old_val())->xpath();
32
33 auto oldValue = change->old_val() ? std::optional{change->old_val()->val_to_string()} : std::nullopt;
34 auto newValue = change->new_val() ? std::optional{change->new_val()->val_to_string()} : std::nullopt;
35 m_recorder->write(xpath, oldValue, newValue);
Václav Kubernát73109382018-09-14 19:52:03 +020036 }
37
38 return SR_ERR_OK;
39 }
40
41private:
42 std::string m_moduleName;
43 Recorder* m_recorder;
44};
45
46Recorder::~Recorder() = default;
47
48SysrepoSubscription::SysrepoSubscription(Recorder* rec)
49 : m_connection(new sysrepo::Connection("netconf-cli-test-subscription"))
50{
51 m_session = std::make_shared<sysrepo::Session>(m_connection);
52 m_subscription = std::make_shared<sysrepo::Subscribe>(m_session);
53 const char* modName = "example-schema";
54 m_callback = std::make_shared<MyCallback>(modName, rec);
55
56 m_subscription->module_change_subscribe(modName, m_callback);
57}