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 | * |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame^] | 6 | */ |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 7 | |
| 8 | #include "trompeloeil_doctest.h" |
| 9 | #include <boost/algorithm/string/predicate.hpp> |
| 10 | #include <map> |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 11 | #include <sysrepo-cpp/Connection.hpp> |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame^] | 12 | #include "common.h" |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 13 | #include "test_log_setup.h" |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 14 | #include "utils/sysrepo.h" |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 15 | |
| 16 | /** @short Return a subtree from sysrepo, compacting the XPath */ |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame^] | 17 | Values dataFromSysrepo(const sysrepo::Session& session, const std::string& xpath) |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 18 | { |
| 19 | spdlog::get("main")->error("dataFrom {}", xpath); |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame^] | 20 | Values res; |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 21 | auto data = session.getData(xpath + "/*"); |
Tomáš Pecka | cf35500 | 2023-05-17 11:40:30 +0200 | [diff] [blame] | 22 | REQUIRE(data.has_value()); |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 23 | for (const auto& sibling : data->findXPath(xpath)) { // Use findXPath here in case the xpath is list without keys. |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 24 | 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áš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 26 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 27 | // 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át | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | return res; |
| 35 | } |
| 36 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 37 | /** @short Execute an RPC or action, return result, compacting the XPath. The rpcPath and input gets concatenated. */ |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame^] | 38 | Values rpcFromSysrepo(sysrepo::Session session, const std::string& rpcPath, Values input) |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 39 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 40 | spdlog::get("main")->info("rpcFromSysrepo {}", rpcPath); |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 41 | auto inputNode = session.getContext().newPath(rpcPath, std::nullopt); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 42 | for (const auto& [k, v] : input) { |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 43 | inputNode.newPath(rpcPath + "/" + k, v); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 44 | } |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 45 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 46 | auto output = session.sendRPC(inputNode); |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 47 | |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame^] | 48 | Values res; |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 49 | 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áš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 58 | return res; |
| 59 | } |
| 60 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 61 | /** @short Return a subtree from specified sysrepo's datastore, compacting the XPath*/ |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame^] | 62 | Values dataFromSysrepo(sysrepo::Session session, const std::string& xpath, sysrepo::Datastore datastore) |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 63 | { |
Tomáš Pecka | 190c724 | 2024-01-23 15:50:51 +0100 | [diff] [blame] | 64 | velia::utils::ScopedDatastoreSwitch s(session, datastore); |
| 65 | return dataFromSysrepo(session, xpath); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 66 | } |