Refactor NetconfAccess::getItems algorithm

I'll be using the same algorithm in YangAccess.

Change-Id: Iff7eb70e64525111dab71fee2fe4e5e08c80dfc0
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;
 }