Handle servers that don't support NMDA

Change-Id: I4880f3bbed7485df18d0b27097b6dfc5fef6ef98
Bug: https://tree.taiga.io/project/jktjkt-netconf-cli/issue/222
diff --git a/src/netconf_access.cpp b/src/netconf_access.cpp
index 3405fe3..a6dbb7c 100644
--- a/src/netconf_access.cpp
+++ b/src/netconf_access.cpp
@@ -46,7 +46,13 @@
 DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path) const
 {
     Tree res;
-    auto config = m_session->getData(targetToDs_get(m_target), (path != "/") ? std::optional{path} : std::nullopt);
+    auto config = [this, &path] {
+        if (m_serverHasNMDA) {
+            return m_session->getData(targetToDs_get(m_target), (path != "/") ? std::optional{path} : std::nullopt);
+        }
+
+        return m_session->get((path != "/") ? std::optional{path} : std::nullopt);
+    }();
 
     if (config) {
         lyNodesToTree(res, config->tree_for());
@@ -58,24 +64,34 @@
     : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey))
     , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
 {
+    checkNMDA();
 }
 
 NetconfAccess::NetconfAccess(const int source, const int sink)
     : m_session(libnetconf::client::Session::connectFd(source, sink))
     , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
 {
+    checkNMDA();
 }
 
 NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
     : m_session(std::move(session))
     , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
 {
+    checkNMDA();
 }
 
 NetconfAccess::NetconfAccess(const std::string& socketPath)
     : m_session(libnetconf::client::Session::connectSocket(socketPath))
     , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
 {
+    checkNMDA();
+}
+
+void NetconfAccess::checkNMDA()
+{
+    auto nmdaMod = m_schema->getYangModule("ietf-netconf-nmda");
+    m_serverHasNMDA = nmdaMod && nmdaMod->implemented();
 }
 
 void NetconfAccess::setNcLogLevel(NC_VERB_LEVEL level)
@@ -146,7 +162,11 @@
 void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
 {
     auto data = dataNode->print_mem(LYD_XML, 0);
-    m_session->editData(targetToDs_set(m_target), data);
+    if (m_serverHasNMDA) {
+        m_session->editData(targetToDs_set(m_target), data);
+    } else {
+        m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
+    }
 }
 
 void NetconfAccess::commitChanges()
diff --git a/src/netconf_access.hpp b/src/netconf_access.hpp
index 734e5ae..43ca1e8 100644
--- a/src/netconf_access.hpp
+++ b/src/netconf_access.hpp
@@ -59,6 +59,10 @@
 
     void doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode);
 
+    void checkNMDA();
+
+    bool m_serverHasNMDA;
+
     std::unique_ptr<libnetconf::client::Session> m_session;
     std::shared_ptr<YangSchema> m_schema;
 };