Migrate to libyang2
Explanation of some of the changes:
1) New libyang produces different schema paths, that don't include
choice/case nodes. This can be seen in Firewall.cpp.
2) New sysrepo does not use <map>, so it has to be included at multiple
places.
3) getUniqueSubtree is now just one line of code. Another commit can get
rid of it.
4) dataFromSysrepo sometimes gives less and sometimes more data. This is
because it now uses libyang instead of sr_val_t
- When it gives more data it's usually just lists or empty containers,
sr_val_t didn't give those.
- When it gives less data it's also just empty containers. This can
be seen with "sensor-data" in hardware_ietf-hardware.cpp.
Depends-on: https://gerrit.cesnet.cz/c/CzechLight/dependencies/+/5171
Change-Id: I388536269e790b8b74ea7791c79b180adc5d80a6
Co-authored-by: Jan Kundrát <jan.kundrat@cesnet.cz>
diff --git a/src/utils/libyang.cpp b/src/utils/libyang.cpp
index a4b7479..d58822c 100644
--- a/src/utils/libyang.cpp
+++ b/src/utils/libyang.cpp
@@ -1,29 +1,30 @@
#include <fmt/format.h>
-#include <libyang/Tree_Data.hpp>
+#include <libyang-cpp/DataNode.hpp>
+#include <libyang-cpp/Set.hpp>
#include "utils/libyang.h"
namespace velia::utils {
-const char* getValueAsString(const libyang::S_Data_Node& node)
+const char* getValueAsString(const libyang::DataNode& node)
{
- if (!node || node->schema()->nodetype() != LYS_LEAF) {
+ if (node.schema().nodeType() != libyang::NodeType::Leaf) {
throw std::logic_error("retrieveString: invalid node");
}
- return libyang::Data_Node_Leaf_List(node).value_str();
+ return node.asTerm().valueStr().data();
}
-std::optional<libyang::S_Data_Node> getUniqueSubtree(const libyang::S_Data_Node& start, const char* path)
+std::optional<libyang::DataNode> getUniqueSubtree(const libyang::DataNode& start, const char* path)
{
- auto set = start->find_path(path);
+ auto set = start.findXPath(path);
- switch(set->number()) {
+ switch(set.size()) {
case 0:
return std::nullopt;
case 1:
- return set->data().front();
+ return set.front();
default:
- throw std::runtime_error(fmt::format("getUniqueSubtree({}, {}): more than one match (got {})", start->path(), path, set->number()));
+ throw std::runtime_error(fmt::format("getUniqueSubtree({}, {}): more than one match (got {})", start.path().get().get(), path, set.size()));
}
}
}