Merge changes I494813fa,Iff7eb70e
* changes:
Fix YangSchema::enableFeature segfault on invalid modules
Refactor NetconfAccess::getItems algorithm
diff --git a/src/libyang_utils.cpp b/src/libyang_utils.cpp
index b4d584f..aa2b3f0 100644
--- a/src/libyang_utils.cpp
+++ b/src/libyang_utils.cpp
@@ -1,3 +1,4 @@
+#include <boost/algorithm/string/predicate.hpp>
#include <cmath>
#include "datastore_access.hpp"
#include "libyang_utils.hpp"
@@ -45,10 +46,8 @@
}
}
-// This is very similar to the fillMap lambda in SysrepoAccess, however,
-// Sysrepo returns a weird array-like structure, while libnetconf
-// returns libyang::Data_Node
-void lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
+namespace {
+void impl_lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
{
auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) {
return ignoredXPathPrefix ? path.substr(ignoredXPathPrefix->size()) : path;
@@ -73,3 +72,23 @@
}
}
}
+}
+
+// This is very similar to the fillMap lambda in SysrepoAccess, however,
+// Sysrepo returns a weird array-like structure, while libnetconf
+// returns libyang::Data_Node
+void lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
+{
+ for (auto it = items.begin(); it < items.end(); it++) {
+ if ((*it)->schema()->nodetype() == LYS_LEAFLIST) {
+ auto leafListPath = stripLeafListValueFromPath((*it)->path());
+ res.emplace_back(leafListPath, special_{SpecialValue::LeafList});
+ while (it != items.end() && boost::starts_with((*it)->path(), leafListPath)) {
+ impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix);
+ it++;
+ }
+ } else {
+ impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix);
+ }
+ }
+}
diff --git a/src/netconf_access.cpp b/src/netconf_access.cpp
index a7cd6b4..bcf6fb4 100644
--- a/src/netconf_access.cpp
+++ b/src/netconf_access.cpp
@@ -5,7 +5,6 @@
*
*/
-#include <boost/algorithm/string/predicate.hpp>
#include <libyang/Libyang.hpp>
#include <libyang/Tree_Data.hpp>
#include "libyang_utils.hpp"
@@ -23,19 +22,7 @@
auto config = m_session->get((path != "/") ? std::optional{path} : std::nullopt);
if (config) {
- auto siblings = config->tree_for();
- for (auto it = siblings.begin(); it < siblings.end(); it++) {
- if ((*it)->schema()->nodetype() == LYS_LEAFLIST) {
- auto leafListPath = stripLeafListValueFromPath((*it)->path());
- res.emplace_back(leafListPath, special_{SpecialValue::LeafList});
- while (it != siblings.end() && boost::starts_with((*it)->path(), leafListPath)) {
- lyNodesToTree(res, (*it)->tree_dfs());
- it++;
- }
- } else {
- lyNodesToTree(res, (*it)->tree_dfs());
- }
- }
+ lyNodesToTree(res, config->tree_for());
}
return res;
}
@@ -141,9 +128,7 @@
Tree res;
auto output = m_session->rpc(data);
if (output) {
- for (auto it : output->tree_for()) {
- lyNodesToTree(res, it->tree_dfs(), joinPaths(path, "/"));
- }
+ lyNodesToTree(res, output->tree_for(), joinPaths(path, "/"));
}
return res;
}
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index bbfce7c..2b69dc7 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -375,7 +375,12 @@
void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
{
- m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
+ auto module = getYangModule(moduleName);
+ if (!module) {
+ using namespace std::string_literals;
+ throw std::runtime_error("Module \""s + moduleName + "\" doesn't exist.");
+ }
+ module->feature_enable(featureName.c_str());
}
void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*, const char*)>& clb)
@@ -406,7 +411,7 @@
std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
{
- return m_context->get_module(name.c_str(), nullptr, 0);
+ return m_context->get_module(name.c_str());
}
namespace {
diff --git a/tests/yang.cpp b/tests/yang.cpp
index f50d76e..833ef70 100644
--- a/tests/yang.cpp
+++ b/tests/yang.cpp
@@ -1183,5 +1183,10 @@
REQUIRE_THROWS(ys.nodeType(path, node));
}
+
+ SECTION("enableFeature - non existing module")
+ {
+ REQUIRE_THROWS_AS(ys.enableFeature("non-existing", "just-no"), std::runtime_error);
+ }
}
}