Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | class MyCallback : public sysrepo::Callback { |
| 14 | public: |
| 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át | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 31 | 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át | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | return SR_ERR_OK; |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | std::string m_moduleName; |
| 43 | Recorder* m_recorder; |
| 44 | }; |
| 45 | |
| 46 | Recorder::~Recorder() = default; |
| 47 | |
| 48 | SysrepoSubscription::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 | } |