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