Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016-2019 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Jan Kundrát <jan.kundrat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "trompeloeil_doctest.h" |
| 9 | #include <boost/algorithm/string/predicate.hpp> |
| 10 | #include <map> |
| 11 | #include <sysrepo-cpp/Session.hpp> |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 12 | #include "test_log_setup.h" |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 13 | #include "utils/sysrepo.h" |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 14 | |
| 15 | /** @short Return a subtree from sysrepo, compacting the XPath */ |
| 16 | auto dataFromSysrepo(const std::shared_ptr<sysrepo::Session>& session, const std::string& xpath) |
| 17 | { |
| 18 | spdlog::get("main")->error("dataFrom {}", xpath); |
| 19 | std::map<std::string, std::string> res; |
| 20 | auto vals = session->get_items((xpath + "//*").c_str()); |
| 21 | REQUIRE(!!vals); |
| 22 | for (size_t i = 0; i < vals->val_cnt(); ++i) { |
| 23 | const auto& v = vals->val(i); |
| 24 | const auto briefXPath = std::string(v->xpath()).substr(boost::algorithm::ends_with(xpath, ":*") ? xpath.size() - 1 : xpath.size()); |
| 25 | res.emplace(briefXPath, v->val_to_string()); |
| 26 | } |
| 27 | return res; |
| 28 | } |
| 29 | |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 30 | /** @short Execute an RPC or action, return result, compacting the XPath. The rpcPath and input gets concatenated. */ |
| 31 | auto rpcFromSysrepo(const std::shared_ptr<sysrepo::Session>& session, const std::string& rpcPath, std::map<std::string, std::string> input) |
| 32 | { |
| 33 | spdlog::get("main")->info("rpcFromSysrepo {}", rpcPath); |
| 34 | auto inputNode = std::make_shared<libyang::Data_Node>(session->get_context(), rpcPath.c_str(), nullptr, LYD_ANYDATA_CONSTSTRING, 0); |
| 35 | for (const auto& [k, v] : input) { |
| 36 | inputNode->new_path(session->get_context(), (rpcPath + "/" + k).c_str(), v.c_str(), LYD_ANYDATA_CONSTSTRING, 0); |
| 37 | } |
| 38 | |
| 39 | auto output = session->rpc_send(inputNode); |
| 40 | REQUIRE(!!output); |
| 41 | |
| 42 | // Sysrepo returns a top-level node. I need the node that contains the action and iterate over that instead of the |
| 43 | // top-level node. |
| 44 | auto actualOutput = output->find_path(rpcPath.c_str())->data().front(); |
| 45 | |
| 46 | std::map<std::string, std::string> res; |
| 47 | for (const auto& node : actualOutput->tree_dfs()) { |
| 48 | if (node->schema()->nodetype() == LYS_LEAF) { |
| 49 | auto leaf = std::make_shared<libyang::Data_Node_Leaf_List>(node); |
| 50 | auto path = node->path(); |
| 51 | const auto briefXPath = path.substr(rpcPath.size()); |
| 52 | res.emplace(briefXPath, leaf->value_str()); |
| 53 | } |
| 54 | } |
| 55 | return res; |
| 56 | } |
| 57 | |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 58 | /** @short Return a subtree from specified sysrepo's datastore, compacting the XPath*/ |
| 59 | auto dataFromSysrepo(const std::shared_ptr<sysrepo::Session>& session, const std::string& xpath, sr_datastore_t datastore) |
| 60 | { |
| 61 | sr_datastore_t oldDatastore = session->session_get_ds(); |
| 62 | session->session_switch_ds(datastore); |
| 63 | |
| 64 | auto res = dataFromSysrepo(session, xpath); |
| 65 | |
| 66 | session->session_switch_ds(oldDatastore); |
| 67 | return res; |
| 68 | } |
| 69 | |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 70 | #define TEST_SYSREPO_INIT \ |
| 71 | auto srConn = std::make_shared<sysrepo::Connection>(); \ |
| 72 | auto srSess = std::make_shared<sysrepo::Session>(srConn); \ |
| 73 | auto srSubs = std::make_shared<sysrepo::Subscribe>(srSess); |
Tomáš Pecka | 749af2e | 2021-01-14 09:52:09 +0100 | [diff] [blame] | 74 | |
Tomáš Pecka | 9a6e15b | 2021-01-25 18:57:48 +0100 | [diff] [blame] | 75 | #define TEST_SYSREPO_INIT_CLIENT \ |
| 76 | auto clientConn = std::make_shared<sysrepo::Connection>(); \ |
| 77 | auto client = std::make_shared<sysrepo::Session>(clientConn); \ |
| 78 | auto subscription = std::make_shared<sysrepo::Subscribe>(client); |