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)) { |
| 31 | m_recorder->write(change->new_val()->xpath(), |
| 32 | change->old_val() ? change->old_val()->val_to_string() : "", |
| 33 | change->new_val()->val_to_string()); |
| 34 | } |
| 35 | |
| 36 | return SR_ERR_OK; |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | std::string m_moduleName; |
| 41 | Recorder* m_recorder; |
| 42 | }; |
| 43 | |
| 44 | Recorder::~Recorder() = default; |
| 45 | |
| 46 | SysrepoSubscription::SysrepoSubscription(Recorder* rec) |
| 47 | : m_connection(new sysrepo::Connection("netconf-cli-test-subscription")) |
| 48 | { |
| 49 | m_session = std::make_shared<sysrepo::Session>(m_connection); |
| 50 | m_subscription = std::make_shared<sysrepo::Subscribe>(m_session); |
| 51 | const char* modName = "example-schema"; |
| 52 | m_callback = std::make_shared<MyCallback>(modName, rec); |
| 53 | |
| 54 | m_subscription->module_change_subscribe(modName, m_callback); |
| 55 | } |