blob: c50e19e68738c888e825fa5cc291962ea334905a [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
Václav Kubernátab612e92019-11-26 19:51:31 +010048SysrepoSubscription::SysrepoSubscription(const std::string& moduleName, Recorder* rec)
Václav Kubernát73109382018-09-14 19:52:03 +020049 : 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);
Václav Kubernátab612e92019-11-26 19:51:31 +010053 if (rec) {
54 m_callback = std::make_shared<MyCallback>(moduleName, rec);
55 } else {
56 m_callback = std::make_shared<sysrepo::Callback>();
57 }
Václav Kubernát73109382018-09-14 19:52:03 +020058
Václav Kubernátab612e92019-11-26 19:51:31 +010059 m_subscription->module_change_subscribe(moduleName.c_str(), m_callback);
Václav Kubernát73109382018-09-14 19:52:03 +020060}