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 | |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 9 | #include <experimental/iterator> |
| 10 | #include <sstream> |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 11 | #include <sysrepo-cpp/Session.hpp> |
| 12 | #include "sysrepo_subscription.hpp" |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 13 | #include "utils.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 14 | |
| 15 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 16 | class MyCallback { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 17 | public: |
| 18 | MyCallback(const std::string& moduleName, Recorder* rec) |
| 19 | : m_moduleName(moduleName) |
| 20 | , m_recorder(rec) |
| 21 | { |
| 22 | } |
| 23 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 24 | sysrepo::ErrorCode operator()( |
| 25 | sysrepo::Session sess, |
| 26 | uint32_t /* sub_id */, |
| 27 | std::string_view module_name, |
| 28 | std::optional<std::string_view> /* sub_xpath */, |
| 29 | sysrepo::Event event, |
| 30 | uint32_t /* request_id */) |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 31 | { |
| 32 | using namespace std::string_literals; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 33 | if (event == sysrepo::Event::Change) { |
| 34 | return sysrepo::ErrorCode::Ok; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 35 | } |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 36 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 37 | for (const auto& it : sess.getChanges(("/"s + module_name.data() + ":*//.").c_str())) { |
| 38 | auto xpath = it.node.path(); |
| 39 | std::optional<std::string> oldValue; |
| 40 | std::optional<std::string> newValue; |
| 41 | if (it.operation == sysrepo::ChangeOperation::Deleted) { |
| 42 | oldValue = it.node.schema().nodeType() == libyang::NodeType::Leaf || it.node.schema().nodeType() == libyang::NodeType::Leaflist ? |
| 43 | std::optional<std::string>{it.node.asTerm().valueStr()} : |
| 44 | std::nullopt; |
| 45 | } else { |
| 46 | oldValue = std::optional<std::string>{it.previousValue}; |
| 47 | newValue = it.node.schema().nodeType() == libyang::NodeType::Leaf || it.node.schema().nodeType() == libyang::NodeType::Leaflist ? |
| 48 | std::optional<std::string>{it.node.asTerm().valueStr()} : |
| 49 | std::nullopt; |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 50 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 51 | } |
| 52 | std::optional<std::string> previousList; |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 53 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 54 | if (it.previousList) { |
| 55 | previousList = std::string{*it.previousList}; |
| 56 | } |
| 57 | |
| 58 | m_recorder->write(it.operation, std::string{xpath}, oldValue, newValue, previousList); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 61 | return sysrepo::ErrorCode::Ok; |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | private: |
| 65 | std::string m_moduleName; |
| 66 | Recorder* m_recorder; |
| 67 | }; |
| 68 | |
| 69 | Recorder::~Recorder() = default; |
| 70 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 71 | DataSupplier::~DataSupplier() = default; |
| 72 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 73 | SysrepoSubscription::SysrepoSubscription(const std::string& moduleName, Recorder* rec, sysrepo::Datastore ds) |
| 74 | : m_subscription([&moduleName, &rec, ds] { // This is an immediately invoked lambda. |
| 75 | return sysrepo::Connection{}.sessionStart(ds).onModuleChange(moduleName.c_str(), |
| 76 | rec ? sysrepo::ModuleChangeCb{MyCallback{moduleName, rec}} |
| 77 | : sysrepo::ModuleChangeCb{[](auto, auto, auto, auto, auto, auto) { return sysrepo::ErrorCode::Ok; }}); |
| 78 | }()) |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 79 | { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 80 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 81 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 82 | class OperationalDataCallback { |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 83 | public: |
| 84 | OperationalDataCallback(const DataSupplier& dataSupplier) |
| 85 | : m_dataSupplier(dataSupplier) |
| 86 | { |
| 87 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 88 | sysrepo::ErrorCode operator()( |
| 89 | sysrepo::Session session, |
| 90 | [[maybe_unused]] uint32_t subscriptionId, |
| 91 | [[maybe_unused]] std::string_view moduleName, |
| 92 | std::optional<std::string_view> subXPath, |
| 93 | [[maybe_unused]] std::optional<std::string_view> requestXPath, |
| 94 | [[maybe_unused]] uint32_t requestId, |
| 95 | std::optional<libyang::DataNode>& output) |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 96 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 97 | auto data = m_dataSupplier.get_data(subXPath->data()); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 98 | for (const auto& [p, v] : data) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 99 | if (!output) { |
| 100 | output = session.getContext().newPath(p.c_str(), v.type() == typeid(empty_) ? nullptr : leafDataToString(v).c_str()); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 101 | } else { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 102 | output->newPath(p.c_str(), v.type() == typeid(empty_) ? nullptr : leafDataToString(v).c_str()); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 103 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 104 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 105 | return sysrepo::ErrorCode::Ok; |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 106 | } |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 107 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 108 | private: |
| 109 | const DataSupplier& m_dataSupplier; |
| 110 | }; |
| 111 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 112 | OperationalDataSubscription::OperationalDataSubscription(const std::string& moduleName, const std::string& path, const DataSupplier& dataSupplier) |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 113 | : m_subscription(sysrepo::Connection{}.sessionStart().onOperGet(moduleName.c_str(), OperationalDataCallback{dataSupplier}, path.c_str())) |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 114 | { |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 115 | } |