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 | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 3 | #include "datastore_access.hpp" |
| 4 | #include "libyang_utils.hpp" |
| 5 | #include "utils.hpp" |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 6 | |
| 7 | leaf_data_ leafValueFromValue(const libyang::S_Value& value, LY_DATA_TYPE type) |
| 8 | { |
| 9 | using namespace std::string_literals; |
| 10 | switch (type) { |
| 11 | case LY_TYPE_INT8: |
| 12 | return value->int8(); |
| 13 | case LY_TYPE_INT16: |
| 14 | return value->int16(); |
| 15 | case LY_TYPE_INT32: |
| 16 | return value->int32(); |
| 17 | case LY_TYPE_INT64: |
| 18 | return value->int64(); |
| 19 | case LY_TYPE_UINT8: |
| 20 | return value->uint8(); |
| 21 | case LY_TYPE_UINT16: |
| 22 | return value->uint16(); |
| 23 | case LY_TYPE_UINT32: |
Jan Kundrát | cc2538f | 2020-02-03 11:33:42 +0100 | [diff] [blame] | 24 | return value->uint32(); |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 25 | case LY_TYPE_UINT64: |
| 26 | return value->uint64(); |
| 27 | case LY_TYPE_BOOL: |
| 28 | return bool(value->bln()); |
| 29 | case LY_TYPE_STRING: |
| 30 | return std::string(value->string()); |
| 31 | case LY_TYPE_ENUM: |
| 32 | return enum_{std::string(value->enm()->name())}; |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 33 | case LY_TYPE_IDENT: |
| 34 | return identityRef_{value->ident()->module()->name(), value->ident()->name()}; |
Jan Kundrát | 4225b3f | 2020-01-24 12:58:38 +0100 | [diff] [blame] | 35 | case LY_TYPE_BINARY: |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 36 | return binary_{value->binary()}; |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 37 | case LY_TYPE_EMPTY: |
| 38 | return empty_{}; |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 39 | case LY_TYPE_BITS: |
| 40 | { |
| 41 | auto bits = value->bit(); |
| 42 | std::vector<libyang::S_Type_Bit> filterNull; |
| 43 | std::copy_if(bits.begin(), bits.end(), std::back_inserter(filterNull), [] (auto bit) { return bit; }); |
| 44 | bits_ res; |
| 45 | std::transform(filterNull.begin(), filterNull.end(), std::inserter(res.m_bits, res.m_bits.end()), [] (const auto& bit) { return bit->name(); }); |
| 46 | return bits_{res}; |
| 47 | } |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 48 | case LY_TYPE_DEC64: |
| 49 | { |
| 50 | auto v = value->dec64(); |
| 51 | return v.value * std::pow(10, -v.digits); |
| 52 | } |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 53 | case LY_TYPE_LEAFREF: |
| 54 | { |
| 55 | libyang::Data_Node_Leaf_List toPrint{value->leafref()}; |
| 56 | return leafValueFromValue(toPrint.value(), toPrint.value_type()); |
| 57 | } |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 58 | default: // TODO: implement all types |
| 59 | return "(can't print)"s; |
| 60 | } |
| 61 | } |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 62 | |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 63 | namespace { |
| 64 | void impl_lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix) |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 65 | { |
| 66 | auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 67 | 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] | 68 | }; |
| 69 | |
| 70 | for (const auto& it : items) { |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 71 | if (it->schema()->nodetype() == LYS_CONTAINER) { |
| 72 | if (libyang::Schema_Node_Container{it->schema()}.presence()) { |
| 73 | // The fact that the container is included in the data tree |
| 74 | // means that it is present and I don't need to check any |
| 75 | // value. |
| 76 | res.emplace_back(stripXPathPrefix(it->path()), special_{SpecialValue::PresenceContainer}); |
| 77 | } |
| 78 | } |
| 79 | if (it->schema()->nodetype() == LYS_LIST) { |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 80 | res.emplace_back(stripXPathPrefix(it->path()), special_{SpecialValue::List}); |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 81 | } |
| 82 | if (it->schema()->nodetype() == LYS_LEAF || it->schema()->nodetype() == LYS_LEAFLIST) { |
| 83 | libyang::Data_Node_Leaf_List leaf(it); |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 84 | auto value = leafValueFromValue(leaf.value(), leaf.value_type()); |
Václav Kubernát | 3c8fe02 | 2020-06-04 01:35:03 +0200 | [diff] [blame] | 85 | res.emplace_back(stripXPathPrefix(it->path()), value); |
| 86 | } |
| 87 | } |
| 88 | } |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // This is very similar to the fillMap lambda in SysrepoAccess, however, |
| 92 | // Sysrepo returns a weird array-like structure, while libnetconf |
| 93 | // returns libyang::Data_Node |
| 94 | void lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix) |
| 95 | { |
| 96 | for (auto it = items.begin(); it < items.end(); it++) { |
| 97 | if ((*it)->schema()->nodetype() == LYS_LEAFLIST) { |
| 98 | auto leafListPath = stripLeafListValueFromPath((*it)->path()); |
| 99 | res.emplace_back(leafListPath, special_{SpecialValue::LeafList}); |
| 100 | while (it != items.end() && boost::starts_with((*it)->path(), leafListPath)) { |
| 101 | impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix); |
| 102 | it++; |
| 103 | } |
| 104 | } else { |
| 105 | impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix); |
| 106 | } |
| 107 | } |
| 108 | } |