blob: fd6b4d7c19d05f3dfd83d1383ba04700f1757bdb [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 *
Tomáš Peckac164ca62024-01-24 13:38:03 +01006 */
Tomáš Pecka98ad18d2020-11-13 15:39:55 +01007
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áš Peckac164ca62024-01-24 13:38:03 +010012#include "common.h"
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010013#include "test_log_setup.h"
Tomáš Peckaba2dc312021-01-23 22:29:11 +010014#include "utils/sysrepo.h"
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010015
16/** @short Return a subtree from sysrepo, compacting the XPath */
Tomáš Peckac164ca62024-01-24 13:38:03 +010017Values dataFromSysrepo(const sysrepo::Session& session, const std::string& xpath)
Tomáš Pecka98ad18d2020-11-13 15:39:55 +010018{
19 spdlog::get("main")->error("dataFrom {}", xpath);
Tomáš Peckac164ca62024-01-24 13:38:03 +010020 Values res;
Jan Kundrátb3e99982022-03-18 17:38:20 +010021 auto data = session.getData(xpath + "/*");
Tomáš Peckacf355002023-05-17 11:40:30 +020022 REQUIRE(data.has_value());
Jan Kundrátb3e99982022-03-18 17:38:20 +010023 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 +010024 for (const auto& node : sibling.childrenDfs()) {
25 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 +010026
Václav Kubernát7efd6d52021-11-09 01:31:11 +010027 // We ignore the thing that's exactly the xpath we're retrieving to avoid having {"": ""} entries.
28 if (briefXPath.empty()) {
29 continue;
30 }
31 res.emplace(briefXPath, node.isTerm() ? node.asTerm().valueStr() : "");
Václav Kubernátbabbab92021-01-27 09:25:05 +010032 }
33 }
34 return res;
35}
36
Václav Kubernát7efd6d52021-11-09 01:31:11 +010037/** @short Execute an RPC or action, return result, compacting the XPath. The rpcPath and input gets concatenated. */
Tomáš Peckac164ca62024-01-24 13:38:03 +010038Values rpcFromSysrepo(sysrepo::Session session, const std::string& rpcPath, Values input)
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010039{
Václav Kubernát7efd6d52021-11-09 01:31:11 +010040 spdlog::get("main")->info("rpcFromSysrepo {}", rpcPath);
Jan Kundrátb3e99982022-03-18 17:38:20 +010041 auto inputNode = session.getContext().newPath(rpcPath, std::nullopt);
Václav Kubernát7efd6d52021-11-09 01:31:11 +010042 for (const auto& [k, v] : input) {
Jan Kundrátb3e99982022-03-18 17:38:20 +010043 inputNode.newPath(rpcPath + "/" + k, v);
Václav Kubernát7efd6d52021-11-09 01:31:11 +010044 }
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010045
Václav Kubernát7efd6d52021-11-09 01:31:11 +010046 auto output = session.sendRPC(inputNode);
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010047
Tomáš Peckac164ca62024-01-24 13:38:03 +010048 Values res;
Václav Kubernát7efd6d52021-11-09 01:31:11 +010049 for (const auto& node : output.childrenDfs()) {
50 const auto briefXPath = std::string{node.path()}.substr(rpcPath.size());
51
52 // We ignore the thing that's exactly the xpath we're retrieving to avoid having {"": ""} entries.
53 if (briefXPath.empty()) {
54 continue;
55 }
56 res.emplace(briefXPath, node.isTerm() ? node.asTerm().valueStr() : "");
57 }
Tomáš Pecka9971dcf2021-01-14 09:41:20 +010058 return res;
59}
60
Václav Kubernát7efd6d52021-11-09 01:31:11 +010061/** @short Return a subtree from specified sysrepo's datastore, compacting the XPath*/
Tomáš Peckac164ca62024-01-24 13:38:03 +010062Values dataFromSysrepo(sysrepo::Session session, const std::string& xpath, sysrepo::Datastore datastore)
Václav Kubernát7efd6d52021-11-09 01:31:11 +010063{
Tomáš Pecka190c7242024-01-23 15:50:51 +010064 velia::utils::ScopedDatastoreSwitch s(session, datastore);
65 return dataFromSysrepo(session, xpath);
Václav Kubernát7efd6d52021-11-09 01:31:11 +010066}