NetconfAccess: reuse netconf libyang context

Change-Id: Ib8d4b35ddd0ecc24e9ff4066e10eaf71eb175bee
diff --git a/src/netconf-client.cpp b/src/netconf-client.cpp
index 3ced537..9f6908b 100644
--- a/src/netconf-client.cpp
+++ b/src/netconf-client.cpp
@@ -204,6 +204,11 @@
     return m_session;
 }
 
+libyang::S_Context Session::libyangContext()
+{
+    return std::make_shared<libyang::Context>(nc_session_get_ctx(m_session), nullptr);
+}
+
 Session::Session(struct nc_session* session)
     : m_session(session)
 {
diff --git a/src/netconf-client.h b/src/netconf-client.h
index 6d57e01..143f200 100644
--- a/src/netconf-client.h
+++ b/src/netconf-client.h
@@ -12,6 +12,7 @@
 
 namespace libyang {
 class Data_Node;
+class Context;
 }
 
 namespace libnetconf {
@@ -46,6 +47,8 @@
     std::shared_ptr<libyang::Data_Node> rpc(const std::string& xmlData);
     void commit();
     void discard();
+
+    std::shared_ptr<libyang::Context> libyangContext();
     struct nc_session* session_internal(); // FIXME: remove me
 protected:
     struct nc_session* m_session;
diff --git a/src/netconf_access.cpp b/src/netconf_access.cpp
index 91b1874..9fb456d 100644
--- a/src/netconf_access.cpp
+++ b/src/netconf_access.cpp
@@ -62,39 +62,22 @@
     return res;
 }
 
-void NetconfAccess::datastoreInit()
-{
-    m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule, const char* submoduleRevision) {
-        return fetchSchema(moduleName,
-                           revision ? std::optional{revision} : std::nullopt,
-                           submodule ? std::optional{submodule} : std::nullopt,
-                           submoduleRevision ? std::optional{submoduleRevision} : std::nullopt);
-    });
-
-    for (const auto& it : listImplementedSchemas()) {
-        m_schema->loadModule(it);
-    }
-}
-
 NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
-    : m_schema(new YangSchema())
+    : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey))
+    , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
 {
-    m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey);
-    datastoreInit();
 }
 
 NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
     : m_session(std::move(session))
-    , m_schema(new YangSchema())
+    , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
 {
-    datastoreInit();
 }
 
 NetconfAccess::NetconfAccess(const std::string& socketPath)
-    : m_schema(new YangSchema())
+    : m_session(libnetconf::client::Session::connectSocket(socketPath))
+    , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
 {
-    m_session = libnetconf::client::Session::connectSocket(socketPath);
-    datastoreInit();
 }
 
 void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index 999f33a..ddd6f37 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -58,6 +58,12 @@
 {
 }
 
+YangSchema::YangSchema(std::shared_ptr<libyang::Context> lyCtx)
+    : m_context(lyCtx)
+{
+
+}
+
 YangSchema::~YangSchema() = default;
 
 void YangSchema::addSchemaString(const char* schema)
diff --git a/src/yang_schema.hpp b/src/yang_schema.hpp
index 7fcd58c..1ff5144 100644
--- a/src/yang_schema.hpp
+++ b/src/yang_schema.hpp
@@ -27,6 +27,7 @@
 class YangSchema : public Schema {
 public:
     YangSchema();
+    YangSchema(std::shared_ptr<libyang::Context> lyCtx);
     ~YangSchema() override;
 
     bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const override;