blob: e350f2f615586baa3ec737be0cdb9891425939e3 [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>
Václav Kubernát7efd6d52021-11-09 01:31:11 +010011#include <sysrepo-cpp/Connection.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 */
Václav Kubernát7efd6d52021-11-09 01:31:11 +010016auto dataFromSysrepo(const sysrepo::Session& session, const std::string& xpath)
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010017{
18 spdlog::get("main")->error("dataFrom {}", xpath);
19 std::map<std::string, std::string> res;
Jan Kundrátb3e99982022-03-18 17:38:20 +010020 auto data = session.getData(xpath + "/*");
Tomáš Peckacf355002023-05-17 11:40:30 +020021 REQUIRE(data.has_value());
Jan Kundrátb3e99982022-03-18 17:38:20 +010022 for (const auto& sibling : data->findXPath(xpath)) { // Use findXPath here in case the xpath is list without keys.
Václav Kubernát7efd6d52021-11-09 01:31:11 +010023 for (const auto& node : sibling.childrenDfs()) {
24 const auto briefXPath = std::string(node.path()).substr(boost::algorithm::ends_with(xpath, ":*") ? xpath.size() - 1 : xpath.size());
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010025
Václav Kubernát7efd6d52021-11-09 01:31:11 +010026 // We ignore the thing that's exactly the xpath we're retrieving to avoid having {"": ""} entries.
27 if (briefXPath.empty()) {
28 continue;
29 }
30 res.emplace(briefXPath, node.isTerm() ? node.asTerm().valueStr() : "");
Václav Kubernátbabbab92021-01-27 09:25:05 +010031 }
32 }
33 return res;
34}
35
Václav Kubernát7efd6d52021-11-09 01:31:11 +010036/** @short Execute an RPC or action, return result, compacting the XPath. The rpcPath and input gets concatenated. */
37auto rpcFromSysrepo(sysrepo::Session session, const std::string& rpcPath, std::map<std::string, std::string> input)
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010038{
Václav Kubernát7efd6d52021-11-09 01:31:11 +010039 spdlog::get("main")->info("rpcFromSysrepo {}", rpcPath);
Jan Kundrátb3e99982022-03-18 17:38:20 +010040 auto inputNode = session.getContext().newPath(rpcPath, std::nullopt);
Václav Kubernát7efd6d52021-11-09 01:31:11 +010041 for (const auto& [k, v] : input) {
Jan Kundrátb3e99982022-03-18 17:38:20 +010042 inputNode.newPath(rpcPath + "/" + k, v);
Václav Kubernát7efd6d52021-11-09 01:31:11 +010043 }
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010044
Václav Kubernát7efd6d52021-11-09 01:31:11 +010045 auto output = session.sendRPC(inputNode);
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010046
Václav Kubernát7efd6d52021-11-09 01:31:11 +010047 std::map<std::string, std::string> res;
48 for (const auto& node : output.childrenDfs()) {
49 const auto briefXPath = std::string{node.path()}.substr(rpcPath.size());
50
51 // We ignore the thing that's exactly the xpath we're retrieving to avoid having {"": ""} entries.
52 if (briefXPath.empty()) {
53 continue;
54 }
55 res.emplace(briefXPath, node.isTerm() ? node.asTerm().valueStr() : "");
56 }
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010057 return res;
58}
59
Václav Kubernát7efd6d52021-11-09 01:31:11 +010060/** @short Return a subtree from specified sysrepo's datastore, compacting the XPath*/
61auto dataFromSysrepo(sysrepo::Session session, const std::string& xpath, sysrepo::Datastore datastore)
62{
Tomáš Pecka190c7242024-01-23 15:50:51 +010063 velia::utils::ScopedDatastoreSwitch s(session, datastore);
64 return dataFromSysrepo(session, xpath);
Václav Kubernát7efd6d52021-11-09 01:31:11 +010065}
66
67#define TEST_SYSREPO_INIT \
68 auto srConn = sysrepo::Connection{}; \
69 auto srSess = srConn.sessionStart();
70
71#define TEST_SYSREPO_INIT_CLIENT \
72 auto clientConn = sysrepo::Connection{}; \
73 auto client = clientConn.sessionStart();