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/ast_values.cpp b/src/ast_values.cpp
index 8d9c40c..ae5aa6c 100644
--- a/src/ast_values.cpp
+++ b/src/ast_values.cpp
@@ -33,26 +33,51 @@
 {
 }
 
+bool module_::operator<(const module_& b) const
+{
+    return this->m_name < b.m_name;
+}
+
 bool identityRef_::operator==(const identityRef_& b) const
 {
     return this->m_prefix == b.m_prefix && this->m_value == b.m_value;
 }
 
+bool identityRef_::operator<(const identityRef_& b) const
+{
+    return std::tie(this->m_prefix, this->m_value) < std::tie(b.m_prefix, b.m_value);
+}
+
 bool binary_::operator==(const binary_& b) const
 {
     return this->m_value == b.m_value;
 }
 
+bool binary_::operator<(const binary_& b) const
+{
+    return this->m_value < b.m_value;
+}
+
 bool enum_::operator==(const enum_& b) const
 {
     return this->m_value == b.m_value;
 }
 
+bool enum_::operator<(const enum_& b) const
+{
+    return this->m_value < b.m_value;
+}
+
 bool special_::operator==(const special_& b) const
 {
     return this->m_value == b.m_value;
 }
 
+bool special_::operator<(const special_& b) const
+{
+    return this->m_value < b.m_value;
+}
+
 std::string specialValueToString(const special_& value)
 {
     switch (value.m_value) {
diff --git a/src/ast_values.hpp b/src/ast_values.hpp
index b2f2c3a..b79ac84 100644
--- a/src/ast_values.hpp
+++ b/src/ast_values.hpp
@@ -14,6 +14,7 @@
     enum_();
     enum_(const std::string& value);
     bool operator==(const enum_& b) const;
+    bool operator<(const enum_& b) const;
     std::string m_value;
 };
 
@@ -21,11 +22,13 @@
     binary_();
     binary_(const std::string& value);
     bool operator==(const binary_& b) const;
+    bool operator<(const binary_& b) const;
     std::string m_value;
 };
 
 struct module_ {
     bool operator==(const module_& b) const;
+    bool operator<(const module_& b) const;
     std::string m_name;
 };
 
@@ -34,6 +37,7 @@
     identityRef_(const std::string& module, const std::string& value);
     identityRef_(const std::string& value);
     bool operator==(const identityRef_& b) const;
+    bool operator<(const identityRef_& b) const;
     boost::optional<module_> m_prefix;
     std::string m_value;
 };
@@ -46,6 +50,7 @@
 
 struct special_ {
     bool operator==(const special_& b) const;
+    bool operator<(const special_& b) const;
     SpecialValue m_value;
 };
 
diff --git a/src/data_query.cpp b/src/data_query.cpp
new file mode 100644
index 0000000..be979ff
--- /dev/null
+++ b/src/data_query.cpp
@@ -0,0 +1,18 @@
+#include "data_query.hpp"
+#include "datastore_access.hpp"
+#include "schema.hpp"
+#include "utils.hpp"
+
+
+DataQuery::DataQuery(DatastoreAccess& datastore)
+    : m_datastore(datastore)
+{
+    m_schema = m_datastore.schema();
+}
+
+std::vector<ListInstance> DataQuery::listKeys(const dataPath_& location, const ModuleNodePair& node) const
+{
+    auto listPath = joinPaths(pathToDataString(location, Prefixes::Always), fullNodeName(location, node));
+
+    return m_datastore.listInstances(listPath);
+}
diff --git a/src/data_query.hpp b/src/data_query.hpp
new file mode 100644
index 0000000..b2f4a33
--- /dev/null
+++ b/src/data_query.hpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Václav Kubernát <kubernat@cesnet.cz>
+ *
+*/
+
+#pragma once
+
+#include <boost/optional.hpp>
+#include <map>
+#include <memory>
+#include <set>
+#include "ast_values.hpp"
+#include "datastore_access.hpp"
+#include "schema.hpp"
+
+
+/*! \class DataQuery
+ *  \brief A class used for querying data from a datastore for usage in tab-completion.
+ *
+*/
+class DataQuery {
+public:
+    DataQuery(DatastoreAccess& datastore);
+    /*! \brief Lists all possible instances of key/value pairs for a list specified by the arguments.
+     *  \param location Location of the list.
+     *  \param node Name (and optional module name) of the list.
+     *  \return A vector of maps, which represent the instances. The map is keyed by the name of the list key. The values in the map, are values of the list keys.
+     */
+    std::vector<ListInstance> listKeys(const dataPath_& location, const ModuleNodePair& node) const;
+
+private:
+    DatastoreAccess& m_datastore;
+    std::shared_ptr<Schema> m_schema;
+};
diff --git a/src/datastore_access.hpp b/src/datastore_access.hpp
index ae0d12a..6686b92 100644
--- a/src/datastore_access.hpp
+++ b/src/datastore_access.hpp
@@ -36,6 +36,8 @@
 
 class Schema;
 
+using ListInstance = std::map<std::string, leaf_data_>;
+
 class DatastoreAccess {
 public:
     using Tree = std::map<std::string, leaf_data_>;
@@ -52,4 +54,8 @@
 
     virtual void commitChanges() = 0;
     virtual void discardChanges() = 0;
+
+private:
+    friend class DataQuery;
+    virtual std::vector<ListInstance> listInstances(const std::string& path) = 0;
 };
diff --git a/src/netconf_access.cpp b/src/netconf_access.cpp
index 7387231..91b1874 100644
--- a/src/netconf_access.cpp
+++ b/src/netconf_access.cpp
@@ -194,3 +194,36 @@
 {
     return m_schema;
 }
+
+std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path)
+{
+    std::vector<ListInstance> res;
+    auto list = m_schema->dataNodeFromPath(path);
+
+    // This inserts selection nodes - I only want keys not other data
+    // To get the keys, I have to call find_path here - otherwise I would get keys of a top-level node (which might not even be a list)
+    auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys();
+    for (const auto& keyLeaf : keys) {
+        // Have to call find_path here - otherwise I'll have the list, not the leaf inside it
+        auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin());
+        auto actualList = *(list->find_path(path.c_str())->data().begin());
+        actualList->insert(selectionLeaf);
+    }
+
+    auto instances = m_session->getConfig(NC_DATASTORE_RUNNING, list->print_mem(LYD_XML, 0));
+
+
+    for (const auto& instance : instances->find_path(path.c_str())->data()) {
+        ListInstance instanceRes;
+
+        // I take the first child here, because the first element (the parent of the child()) will be the list
+        for (const auto& keyLeaf : instance->child()->tree_for()) {
+            auto leafData = libyang::Data_Node_Leaf_List{keyLeaf};
+            auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()};
+            instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())});
+        }
+        res.push_back(instanceRes);
+    }
+
+    return res;
+}
diff --git a/src/netconf_access.hpp b/src/netconf_access.hpp
index f43976a..aa13312 100644
--- a/src/netconf_access.hpp
+++ b/src/netconf_access.hpp
@@ -46,6 +46,8 @@
     std::shared_ptr<Schema> schema() override;
 
 private:
+    std::vector<std::map<std::string, leaf_data_>> listInstances(const std::string& path) override;
+
     std::string fetchSchema(const std::string_view module, const
             std::optional<std::string_view> revision, const
             std::optional<std::string_view> submodule, const
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;
+}
diff --git a/src/sysrepo_access.hpp b/src/sysrepo_access.hpp
index c2ce909..cebb564 100644
--- a/src/sysrepo_access.hpp
+++ b/src/sysrepo_access.hpp
@@ -42,6 +42,7 @@
     void discardChanges() override;
 
 private:
+    std::vector<std::map<std::string, leaf_data_>> listInstances(const std::string& path) override;
     [[noreturn]] void reportErrors();
 
     std::string fetchSchema(const char* module, const char* revision, const char* submodule);
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index 06259b9..999f33a 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -399,7 +399,7 @@
                                                 path.c_str(),
                                                 value ? value.value().c_str() : nullptr,
                                                 LYD_ANYDATA_CONSTSTRING,
-                                                0);
+                                                LYD_PATH_OPT_EDIT);
 }
 
 std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)