blob: 456fa622cf47f78129f24ec367e6b4e9b836dd5a [file] [log] [blame]
Václav Kubernátdaf40312020-06-19 11:34:55 +02001#include <boost/algorithm/string/predicate.hpp>
Jan Kundrátbd3169c2020-02-03 19:31:34 +01002#include <cmath>
Václav Kubernát3c8fe022020-06-04 01:35:03 +02003#include "datastore_access.hpp"
4#include "libyang_utils.hpp"
5#include "utils.hpp"
Václav Kubernát02a71152020-01-21 14:52:51 +01006
7leaf_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átcc2538f2020-02-03 11:33:42 +010024 return value->uint32();
Václav Kubernát02a71152020-01-21 14:52:51 +010025 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át0d8abd12020-05-07 02:00:14 +020033 case LY_TYPE_IDENT:
34 return identityRef_{value->ident()->module()->name(), value->ident()->name()};
Jan Kundrát4225b3f2020-01-24 12:58:38 +010035 case LY_TYPE_BINARY:
Jan Kundrát68985442020-05-07 02:15:34 +020036 return binary_{value->binary()};
Jan Kundrát379bb572020-05-07 03:23:13 +020037 case LY_TYPE_EMPTY:
38 return empty_{};
Jan Kundrátbd3169c2020-02-03 19:31:34 +010039 case LY_TYPE_DEC64:
40 {
41 auto v = value->dec64();
42 return v.value * std::pow(10, -v.digits);
43 }
Václav Kubernát7b191ce2020-06-30 16:22:53 +020044 case LY_TYPE_LEAFREF:
45 {
46 libyang::Data_Node_Leaf_List toPrint{value->leafref()};
47 return leafValueFromValue(toPrint.value(), toPrint.value_type());
48 }
Václav Kubernát02a71152020-01-21 14:52:51 +010049 default: // TODO: implement all types
50 return "(can't print)"s;
51 }
52}
Václav Kubernát3c8fe022020-06-04 01:35:03 +020053
Václav Kubernátdaf40312020-06-19 11:34:55 +020054namespace {
55void impl_lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
Václav Kubernát3c8fe022020-06-04 01:35:03 +020056{
57 auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) {
Václav Kubernáta8789602020-07-20 15:18:19 +020058 return ignoredXPathPrefix && path.find(*ignoredXPathPrefix) != std::string::npos ? path.substr(ignoredXPathPrefix->size()) : path;
Václav Kubernát3c8fe022020-06-04 01:35:03 +020059 };
60
61 for (const auto& it : items) {
Václav Kubernát3c8fe022020-06-04 01:35:03 +020062 if (it->schema()->nodetype() == LYS_CONTAINER) {
63 if (libyang::Schema_Node_Container{it->schema()}.presence()) {
64 // The fact that the container is included in the data tree
65 // means that it is present and I don't need to check any
66 // value.
67 res.emplace_back(stripXPathPrefix(it->path()), special_{SpecialValue::PresenceContainer});
68 }
69 }
70 if (it->schema()->nodetype() == LYS_LIST) {
Václav Kubernátfaacd022020-07-08 16:44:38 +020071 res.emplace_back(stripXPathPrefix(it->path()), special_{SpecialValue::List});
Václav Kubernát3c8fe022020-06-04 01:35:03 +020072 }
73 if (it->schema()->nodetype() == LYS_LEAF || it->schema()->nodetype() == LYS_LEAFLIST) {
74 libyang::Data_Node_Leaf_List leaf(it);
Václav Kubernát7b191ce2020-06-30 16:22:53 +020075 auto value = leafValueFromValue(leaf.value(), leaf.value_type());
Václav Kubernát3c8fe022020-06-04 01:35:03 +020076 res.emplace_back(stripXPathPrefix(it->path()), value);
77 }
78 }
79}
Václav Kubernátdaf40312020-06-19 11:34:55 +020080}
81
82// This is very similar to the fillMap lambda in SysrepoAccess, however,
83// Sysrepo returns a weird array-like structure, while libnetconf
84// returns libyang::Data_Node
85void lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
86{
87 for (auto it = items.begin(); it < items.end(); it++) {
88 if ((*it)->schema()->nodetype() == LYS_LEAFLIST) {
89 auto leafListPath = stripLeafListValueFromPath((*it)->path());
90 res.emplace_back(leafListPath, special_{SpecialValue::LeafList});
91 while (it != items.end() && boost::starts_with((*it)->path(), leafListPath)) {
92 impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix);
93 it++;
94 }
95 } else {
96 impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix);
97 }
98 }
99}