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 | |
Tomáš Pecka | 7eb6459 | 2024-01-24 14:10:47 +0100 | [diff] [blame] | 16 | bool operator==(const Deleted&, const Deleted&) { return true; } |
| 17 | |
| 18 | std::string nodeAsString(const libyang::DataNode& node) |
| 19 | { |
| 20 | switch (node.schema().nodeType()) { |
| 21 | case libyang::NodeType::Container: |
| 22 | return "(container)"; |
| 23 | case libyang::NodeType::List: |
| 24 | return "(list instance)"; |
| 25 | case libyang::NodeType::Leaf: |
| 26 | case libyang::NodeType::Leaflist: |
| 27 | return std::string(node.asTerm().valueStr()); |
| 28 | default: |
| 29 | return "(unprintable)"; |
| 30 | } |
| 31 | } |
| 32 | |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 33 | /** @short Return a subtree from sysrepo, compacting the XPath */ |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame] | 34 | Values dataFromSysrepo(const sysrepo::Session& session, const std::string& xpath) |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 35 | { |
| 36 | spdlog::get("main")->error("dataFrom {}", xpath); |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame] | 37 | Values res; |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 38 | auto data = session.getData(xpath + "/*"); |
Tomáš Pecka | cf35500 | 2023-05-17 11:40:30 +0200 | [diff] [blame] | 39 | REQUIRE(data.has_value()); |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 40 | 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] | 41 | for (const auto& node : sibling.childrenDfs()) { |
| 42 | 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] | 43 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 44 | // We ignore the thing that's exactly the xpath we're retrieving to avoid having {"": ""} entries. |
| 45 | if (briefXPath.empty()) { |
| 46 | continue; |
| 47 | } |
| 48 | res.emplace(briefXPath, node.isTerm() ? node.asTerm().valueStr() : ""); |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | return res; |
| 52 | } |
| 53 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 54 | /** @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] | 55 | Values rpcFromSysrepo(sysrepo::Session session, const std::string& rpcPath, Values input) |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 56 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 57 | spdlog::get("main")->info("rpcFromSysrepo {}", rpcPath); |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 58 | auto inputNode = session.getContext().newPath(rpcPath, std::nullopt); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 59 | for (const auto& [k, v] : input) { |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 60 | inputNode.newPath(rpcPath + "/" + k, v); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 61 | } |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 62 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 63 | auto output = session.sendRPC(inputNode); |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 64 | |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame] | 65 | Values res; |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 66 | for (const auto& node : output.childrenDfs()) { |
| 67 | const auto briefXPath = std::string{node.path()}.substr(rpcPath.size()); |
| 68 | |
| 69 | // We ignore the thing that's exactly the xpath we're retrieving to avoid having {"": ""} entries. |
| 70 | if (briefXPath.empty()) { |
| 71 | continue; |
| 72 | } |
| 73 | res.emplace(briefXPath, node.isTerm() ? node.asTerm().valueStr() : ""); |
| 74 | } |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 75 | return res; |
| 76 | } |
| 77 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 78 | /** @short Return a subtree from specified sysrepo's datastore, compacting the XPath*/ |
Tomáš Pecka | c164ca6 | 2024-01-24 13:38:03 +0100 | [diff] [blame] | 79 | 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] | 80 | { |
Tomáš Pecka | 190c724 | 2024-01-23 15:50:51 +0100 | [diff] [blame] | 81 | velia::utils::ScopedDatastoreSwitch s(session, datastore); |
| 82 | return dataFromSysrepo(session, xpath); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 83 | } |
Tomáš Pecka | de06290 | 2024-01-24 13:41:16 +0100 | [diff] [blame] | 84 | |
| 85 | std::string moduleFromXpath(const std::string& xpath) |
| 86 | { |
| 87 | auto pos = xpath.find(":"); |
| 88 | if (pos == 0 || pos == std::string::npos || xpath[0] != '/') { |
| 89 | throw std::logic_error{"module_from_xpath: malformed XPath " + xpath}; |
| 90 | } |
| 91 | return xpath.substr(1, pos - 1); |
| 92 | } |