Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 1 | #include <boost/algorithm/string/predicate.hpp> |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 2 | #include <cmath> |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 3 | #include <libyang-cpp/Context.hpp> |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 4 | #include "datastore_access.hpp" |
| 5 | #include "libyang_utils.hpp" |
| 6 | #include "utils.hpp" |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 7 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 8 | struct impl_leafValueFromNode { |
| 9 | leaf_data_ operator()(const libyang::Empty) const |
| 10 | { |
| 11 | return empty_{}; |
| 12 | } |
| 13 | |
| 14 | leaf_data_ operator()(const libyang::Binary& bin) const |
| 15 | { |
| 16 | return binary_{std::string{bin.base64}}; |
| 17 | } |
| 18 | |
| 19 | leaf_data_ operator()(const std::vector<libyang::Bit>& bits) const |
| 20 | { |
| 21 | bits_ res; |
| 22 | std::transform(bits.begin(), bits.end(), std::back_inserter(res.m_bits), [] (const libyang::Bit& bit) { |
| 23 | return bit.name; |
| 24 | }); |
| 25 | return res; |
| 26 | } |
| 27 | |
| 28 | leaf_data_ operator()(const libyang::Enum& enumVal) const |
| 29 | { |
| 30 | return enum_{enumVal.name}; |
| 31 | } |
| 32 | |
| 33 | leaf_data_ operator()(const libyang::IdentityRef& identRef) const |
| 34 | { |
| 35 | return identityRef_{identRef.module, identRef.name}; |
| 36 | } |
| 37 | |
| 38 | leaf_data_ operator()(const libyang::Decimal64& dec) const |
| 39 | { |
| 40 | return dec.number * std::pow(10, -dec.digits); |
| 41 | } |
| 42 | |
Jan Kundrát | bb7aa85 | 2023-08-30 11:51:43 +0200 | [diff] [blame] | 43 | leaf_data_ operator()(const libyang::InstanceIdentifier& node) const |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 44 | { |
Jan Kundrát | bb7aa85 | 2023-08-30 11:51:43 +0200 | [diff] [blame] | 45 | return instanceIdentifier_{node.path}; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | template <typename Type> |
| 49 | leaf_data_ operator()(const Type& val) const |
| 50 | { |
| 51 | return val; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | leaf_data_ leafValueFromNode(libyang::DataNodeTerm node) |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 56 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 57 | return std::visit(impl_leafValueFromNode{},node.value()); |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 58 | } |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 59 | |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 60 | namespace { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 61 | template <typename CollectionType> |
| 62 | void impl_lyNodesToTree(DatastoreAccess::Tree& res, CollectionType items, std::optional<std::string> ignoredXPathPrefix) |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 63 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 64 | auto stripXPathPrefix = [&ignoredXPathPrefix](auto path) { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 65 | return ignoredXPathPrefix && path.find(*ignoredXPathPrefix) != std::string::npos ? path.substr(ignoredXPathPrefix->size()) : path; |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | for (const auto& it : items) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 69 | if (it.schema().nodeType() == libyang::NodeType::Container) { |
| 70 | if (it.schema().asContainer().isPresence()) { |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 71 | // The fact that the container is included in the data tree |
| 72 | // means that it is present and I don't need to check any |
| 73 | // value. |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 74 | res.emplace_back(stripXPathPrefix(std::string{it.path()}), special_{SpecialValue::PresenceContainer}); |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 75 | } |
| 76 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 77 | if (it.schema().nodeType() == libyang::NodeType::List) { |
| 78 | res.emplace_back(stripXPathPrefix(std::string{it.path()}), special_{SpecialValue::List}); |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 79 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 80 | if (it.schema().nodeType() == libyang::NodeType::Leaf || it.schema().nodeType() == libyang::NodeType::Leaflist) { |
| 81 | auto term = it.asTerm(); |
| 82 | auto value = leafValueFromNode(term); |
| 83 | res.emplace_back(stripXPathPrefix(std::string{it.path()}), value); |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 89 | template <typename CollectionType> |
| 90 | void lyNodesToTree(DatastoreAccess::Tree& res, CollectionType items, std::optional<std::string> ignoredXPathPrefix) |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 91 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 92 | for (auto it = items.begin(); it != items.end(); /* nothing */) { |
| 93 | if ((*it).schema().nodeType() == libyang::NodeType::Leaflist) { |
| 94 | auto leafListPath = stripLeafListValueFromPath(std::string{(*it).path()}); |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 95 | res.emplace_back(leafListPath, special_{SpecialValue::LeafList}); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 96 | while (it != items.end() && boost::starts_with(std::string{(*it).path()}, leafListPath)) { |
| 97 | impl_lyNodesToTree(res, it->childrenDfs(), ignoredXPathPrefix); |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 98 | it++; |
| 99 | } |
| 100 | } else { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 101 | impl_lyNodesToTree(res, it->childrenDfs(), ignoredXPathPrefix); |
| 102 | it++; |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 106 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 107 | using SiblingColl = libyang::Collection<libyang::DataNode, libyang::IterationType::Sibling>; |
| 108 | using DfsColl = libyang::Collection<libyang::DataNode, libyang::IterationType::Dfs>; |
| 109 | |
| 110 | template |
| 111 | void lyNodesToTree<SiblingColl>(DatastoreAccess::Tree& res, SiblingColl items, std::optional<std::string> ignoredXPathPrefix); |
| 112 | template |
| 113 | void lyNodesToTree<DfsColl>(DatastoreAccess::Tree& res, DfsColl items, std::optional<std::string> ignoredXPathPrefix); |
| 114 | template |
| 115 | void lyNodesToTree<libyang::Set<libyang::DataNode>>(DatastoreAccess::Tree& res, libyang::Set<libyang::DataNode> items, std::optional<std::string> ignoredXPathPrefix); |
| 116 | |
| 117 | DatastoreAccess::Tree rpcOutputToTree(libyang::DataNode output) |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 118 | { |
| 119 | DatastoreAccess::Tree res; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 120 | lyNodesToTree(res, output.siblings(), joinPaths(std::string{output.path()}, "/")); |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 121 | return res; |
| 122 | } |
| 123 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 124 | libyang::DataNode treeToRpcInput(libyang::Context ctx, const std::string& path, DatastoreAccess::Tree in) |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 125 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 126 | auto root = ctx.newPath(path, std::nullopt, libyang::CreationOptions::Update); |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 127 | for (const auto& [k, v] : in) { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 128 | root.newPath(k, leafDataToString(v), libyang::CreationOptions::Update); |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return root; |
| 132 | } |