Implement DataQuery

For now, the DataQuery class includes the listKeys method, which will be
used in the parser for tab-completion of values of keys.

The listKeys method uses similar arguments as many methods from the
Schema interface do. This is due to how parsing works: when I parse
lists I won't get the full list instance until all keys are typed.
That's why I can't get the full path to the list: it can't be a full
data path until all keys are known.

I tried many different return types of the method. The first iteration
used just a string to string map. That didn't work well, because
discarding type info meant that I needed to quote strings in THIS
method. And that didn't go well when trying to integrate this to the
parser, because I couldn't compare quoted strings with unquoted strings.

The next return type I tried - a set of string to leaf_data_ maps - was
better. It doesn't discard type info and that makes it possible to
compare in the parser. However, I didn't really make use of an ordered
container, so I changed the type to a vector.

The various implementations of this method were pretty straightforward.
They usually just retrieved all the instances from the datastore with
DatastoreAccess::getItems and then used some sort of an algorithm to
transform the instances into the desired return type. However, there was
a problem with this approach: it is difficult to get ONLY the instances,
and no other data. This means that it is unsuitable to use getItems for this, because:
a) it always works recursively
b) even if it didn't Netconf would still give me more data than I
wanted.

It is possible to create an XML filter, so that no data has to be
discarded, but it's only possible in Netconf and not sysrepo. This
difference is going to be (hopefully) removed with sysrepo2. I decided
to implement a datastore-specific method, where both sysrepo and netconf
use their own best implementation.

Change-Id: I2c978762d3a78286b4f7fb97e16118772ddeb7bc
diff --git a/src/sysrepo_access.cpp b/src/sysrepo_access.cpp
index cf19407..b681893 100644
--- a/src/sysrepo_access.cpp
+++ b/src/sysrepo_access.cpp
@@ -6,7 +6,10 @@
  *
 */
 
+#include <libyang/Tree_Data.hpp>
+#include <libyang/Tree_Schema.hpp>
 #include <sysrepo-cpp/Session.hpp>
+#include "libyang_utils.hpp"
 #include "sysrepo_access.hpp"
 #include "utils.hpp"
 #include "yang_schema.hpp"
@@ -317,3 +320,54 @@
 
     throw DatastoreException(res);
 }
+
+std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path)
+{
+    std::vector<ListInstance> res;
+    auto lists = getItems(path);
+
+    decltype(lists) instances;
+    auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin());
+    std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck=wantedTree->schema()->path()](const auto& item) {
+        // This filters out non-instances.
+        if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) {
+            return false;
+        }
+
+        // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance...
+        auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin());
+        // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested.
+        return instanceTree->schema()->path() == pathToCheck;
+    });
+
+    // If there are no instances, then just return
+    if (instances.empty()) {
+        return res;
+    }
+
+    // I need to find out which keys does the list have. To do that, I create a
+    // tree from the first instance. This is gives me some top level node,
+    // which will be our list in case out list is a top-level node. In case it
+    // isn't, we have call find_path on the top level node. After that, I just
+    // retrieve the keys.
+    auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first);
+    auto list = *(topLevelTree->find_path(path.c_str())->data().begin());
+    auto keys = libyang::Schema_Node_List{list->schema()}.keys();
+
+    // Creating a full tree at the same time from the values sysrepo gives me
+    // would be a pain (and after sysrepo switches to libyang meaningless), so
+    // I just use this algorithm to create data nodes one by one and get the
+    // key values from them.
+    for (const auto& instance : instances) {
+        auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin());
+        ListInstance instanceRes;
+        for (const auto& key : keys) {
+            auto vec = wantedList->find_path(key->name())->data();
+            auto leaf = libyang::Data_Node_Leaf_List{*(vec.begin())};
+            instanceRes.emplace(key->name(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
+        }
+        res.push_back(instanceRes);
+    }
+
+    return res;
+}