blob: 8478f17890d7491e2ce03f607eda3f5e87e70757 [file] [log] [blame]
Tomáš Pecka98ad18d2020-11-13 15:39:55 +01001/*
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áš Pecka98ad18d2020-11-13 15:39:55 +010012#include "test_log_setup.h"
Tomáš Peckaba2dc312021-01-23 22:29:11 +010013#include "utils/sysrepo.h"
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010014
15/** @short Return a subtree from sysrepo, compacting the XPath */
16auto 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átbabbab92021-01-27 09:25:05 +010030/** @short Execute an RPC or action, return result, compacting the XPath. The rpcPath and input gets concatenated. */
31auto 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áš Pecka9971dcf2021-01-14 09:41:20 +010058/** @short Return a subtree from specified sysrepo's datastore, compacting the XPath*/
59auto 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áš Pecka55f64f82020-12-10 19:03:30 +010070#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áš Pecka749af2e2021-01-14 09:52:09 +010074
Tomáš Pecka9a6e15b2021-01-25 18:57:48 +010075#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);