Implement the NetconfAccess class

Change-Id: Idd63318364edd424e9c477f26db3c6d032721e8a
diff --git a/src/netconf-client.cpp b/src/netconf-client.cpp
new file mode 100644
index 0000000..d10dd2f
--- /dev/null
+++ b/src/netconf-client.cpp
@@ -0,0 +1,344 @@
+/*
+ * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Václav Kubernát <kubernat@cesnet.cz>
+ * Written by Jan Kundrát <jan.kundrat@cesnet.cz>
+ *
+*/
+
+#include <libyang/Tree_Data.hpp>
+#include <mutex>
+extern "C" {
+#include <nc_client.h>
+}
+#include <sstream>
+#include <tuple>
+#include "netconf-client.h"
+
+namespace libnetconf {
+
+namespace impl {
+
+/** @short Initialization of the libnetconf2 library client
+
+Just a safe wrapper over nc_client_init and nc_client_destroy, really.
+*/
+class ClientInit {
+    ClientInit()
+    {
+        nc_client_init();
+        nc_verbosity(NC_VERB_DEBUG);
+    }
+
+    ~ClientInit()
+    {
+        nc_client_destroy();
+    }
+
+public:
+    static ClientInit& instance()
+    {
+        static ClientInit lib;
+        return lib;
+    }
+
+    ClientInit(const ClientInit&) = delete;
+    ClientInit(ClientInit&&) = delete;
+    ClientInit& operator=(const ClientInit&) = delete;
+    ClientInit& operator=(ClientInit&&) = delete;
+};
+
+static std::mutex clientOptions;
+
+inline void custom_free_nc_reply_data(nc_reply_data* reply)
+{
+    nc_reply_free(reinterpret_cast<nc_reply*>(reply));
+}
+inline void custom_free_nc_reply_error(nc_reply_error* reply)
+{
+    nc_reply_free(reinterpret_cast<nc_reply*>(reply));
+}
+
+template <auto fn>
+using deleter_from_fn = std::integral_constant<decltype(fn), fn>;
+
+template <typename T>
+struct deleter_type_for {
+    using func_type = void (*)(T*);
+};
+
+template <typename T>
+struct deleter_for {
+};
+
+template <>
+struct deleter_for<struct nc_rpc> {
+    static constexpr void (*free)(struct nc_rpc*) = deleter_from_fn<nc_rpc_free>();
+};
+template <>
+struct deleter_for<struct nc_reply> {
+    static constexpr void (*free)(struct nc_reply*) = deleter_from_fn<nc_reply_free>();
+};
+template <>
+struct deleter_for<struct nc_reply_data> {
+    static constexpr void (*free)(struct nc_reply_data*) = deleter_from_fn<custom_free_nc_reply_data>();
+};
+template <>
+struct deleter_for<struct nc_reply_error> {
+    static constexpr void (*free)(struct nc_reply_error*) = deleter_from_fn<custom_free_nc_reply_error>();
+};
+
+template <typename T>
+using unique_ptr_for = std::unique_ptr<T, decltype(deleter_for<T>::free)>;
+
+template <typename T>
+auto guarded(T* ptr)
+{
+    return unique_ptr_for<T>(ptr, deleter_for<T>::free);
+}
+
+unique_ptr_for<struct nc_reply> do_rpc(client::Session* session, unique_ptr_for<struct nc_rpc>&& rpc)
+{
+    uint64_t msgid;
+    NC_MSG_TYPE msgtype;
+
+    msgtype = nc_send_rpc(session->session_internal(), rpc.get(), 1000, &msgid);
+    if (msgtype == NC_MSG_ERROR) {
+        throw std::runtime_error{"Failed to send RPC"};
+    }
+    if (msgtype == NC_MSG_WOULDBLOCK) {
+        throw std::runtime_error{"Timeout sending an RPC"};
+    }
+
+    struct nc_reply* raw_reply;
+    while (true) {
+        msgtype = nc_recv_reply(session->session_internal(), rpc.get(), msgid, 20000, LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS, &raw_reply);
+        auto reply = guarded(raw_reply);
+        raw_reply = nullptr;
+
+        switch (msgtype) {
+        case NC_MSG_ERROR:
+            throw std::runtime_error{"Failed to receive an RPC reply"};
+        case NC_MSG_WOULDBLOCK:
+            throw std::runtime_error{"Timed out waiting for RPC reply"};
+        case NC_MSG_REPLY_ERR_MSGID:
+            throw std::runtime_error{"Received a wrong reply -- msgid mismatch"};
+        case NC_MSG_NOTIF:
+            continue;
+        default:
+            return reply;
+        }
+    }
+    __builtin_unreachable();
+}
+
+client::ReportedError make_error(unique_ptr_for<struct nc_reply>&& reply)
+{
+    if (reply->type != NC_RPL_ERROR) {
+        throw std::logic_error{"Cannot extract an error from a non-error server reply"};
+    }
+
+    auto errorReply = guarded(reinterpret_cast<struct nc_reply_error*>(reply.release()));
+
+    // TODO: capture the error details, not just that human-readable string
+    std::ostringstream ss;
+    ss << "Server error:" << std::endl;
+    for (uint32_t i = 0; i < errorReply->count; ++i) {
+        const auto e = errorReply->err[i];
+        ss << " #" << i << ": " << e.message;
+        if (e.path) {
+            ss << " (XPath " << e.path << ")";
+        }
+        ss << std::endl;
+    }
+    return client::ReportedError{ss.str()};
+}
+
+unique_ptr_for<struct nc_reply_data> do_rpc_data(client::Session* session, unique_ptr_for<struct nc_rpc>&& rpc)
+{
+    auto x = do_rpc(session, std::move(rpc));
+
+    switch (x->type) {
+    case NC_RPL_DATA:
+        return guarded(reinterpret_cast<struct nc_reply_data*>(x.release()));
+    case NC_RPL_OK:
+        throw std::runtime_error{"Received OK instead of a data reply"};
+    case NC_RPL_ERROR:
+        throw make_error(std::move(x));
+    default:
+        throw std::runtime_error{"Unhandled reply type"};
+    }
+}
+
+void do_rpc_ok(client::Session* session, unique_ptr_for<struct nc_rpc>&& rpc)
+{
+    auto x = do_rpc(session, std::move(rpc));
+
+    switch (x->type) {
+    case NC_RPL_DATA:
+        throw std::runtime_error{"Unexpected DATA reply"};
+    case NC_RPL_OK:
+        return;
+    case NC_RPL_ERROR:
+        throw make_error(std::move(x));
+    default:
+        throw std::runtime_error{"Unhandled reply type"};
+    }
+}
+}
+
+namespace client {
+
+struct nc_session* Session::session_internal()
+{
+    return m_session;
+}
+
+Session::Session(struct nc_session* session)
+    : m_session(session)
+{
+    impl::ClientInit::instance();
+}
+
+Session::~Session()
+{
+    ::nc_session_free(m_session, nullptr);
+}
+
+std::unique_ptr<Session> Session::connectPubkey(const std::string& host, const uint16_t port, const std::string& user, const std::string& pubPath, const std::string& privPath)
+{
+    impl::ClientInit::instance();
+
+    {
+        // FIXME: this is still horribly not enough. libnetconf *must* provide us with something better.
+        std::lock_guard lk(impl::clientOptions);
+        nc_client_ssh_set_username(user.c_str());
+        nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 5);
+        nc_client_ssh_add_keypair(pubPath.c_str(), privPath.c_str());
+    }
+    auto session = std::make_unique<Session>(nc_connect_ssh(host.c_str(), port, nullptr));
+    if (!session->m_session) {
+        throw std::runtime_error{"nc_connect_ssh failed"};
+    }
+    return session;
+}
+
+std::unique_ptr<Session> Session::connectSocket(const std::string& path)
+{
+    impl::ClientInit::instance();
+
+    auto session = std::make_unique<Session>(nc_connect_unix(path.c_str(), nullptr));
+    if (!session->m_session) {
+        throw std::runtime_error{"nc_connect_unix failed"};
+    }
+    return session;
+}
+
+std::vector<std::string_view> Session::capabilities() const
+{
+    std::vector<std::string_view> res;
+    auto caps = nc_session_get_cpblts(m_session);
+    while (*caps) {
+        res.emplace_back(*caps);
+        ++caps;
+    }
+    return res;
+}
+
+std::shared_ptr<libyang::Data_Node> Session::get(const std::string& filter)
+{
+    auto rpc = impl::guarded(nc_rpc_get(filter.c_str(), NC_WD_ALL, NC_PARAMTYPE_CONST));
+    if (!rpc) {
+        throw std::runtime_error("Cannot create get RPC");
+    }
+    auto reply = impl::do_rpc_data(this, std::move(rpc));
+    // TODO: can we do without copying?
+    // If we just default-construct a new node (or use the create_new_Data_Node) and then set reply->data to nullptr,
+    // there are mem leaks and even libnetconf2 complains loudly.
+    return libyang::create_new_Data_Node(reply->data)->dup_withsiblings(1);
+}
+
+std::string Session::getSchema(const std::string_view identifier, const std::optional<std::string_view> version)
+{
+    auto rpc = impl::guarded(nc_rpc_getschema(identifier.data(), version ? version.value().data() : nullptr, nullptr, NC_PARAMTYPE_CONST));
+    if (!rpc) {
+        throw std::runtime_error("Cannot create get-schema RPC");
+    }
+    auto reply = impl::do_rpc_data(this, std::move(rpc));
+
+    auto node = libyang::create_new_Data_Node(reply->data)->dup_withsiblings(1);
+    auto set = node->find_path("data");
+    for (auto node : set->data()) {
+        if (node->schema()->nodetype() == LYS_ANYXML) {
+            libyang::Data_Node_Anydata anydata(node);
+            return anydata.value().str;
+        }
+    }
+    throw std::runtime_error("Got a reply to get-schema, but it didn't contain the required schema");
+}
+
+std::shared_ptr<libyang::Data_Node> Session::getConfig(const NC_DATASTORE datastore, const std::optional<const std::string> filter)
+{
+    auto rpc = impl::guarded(nc_rpc_getconfig(datastore, filter ? filter->c_str() : nullptr, NC_WD_ALL, NC_PARAMTYPE_CONST));
+    if (!rpc) {
+        throw std::runtime_error("Cannot create get-config RPC");
+    }
+    auto reply = impl::do_rpc_data(this, std::move(rpc));
+    // TODO: can we do without copying?
+    // If we just default-construct a new node (or use the create_new_Data_Node) and then set reply->data to nullptr,
+    // there are mem leaks and even libnetconf2 complains loudly.
+    auto dataNode = libyang::create_new_Data_Node(reply->data);
+    if (!dataNode)
+        return nullptr;
+    else
+        return dataNode->dup_withsiblings(1);
+}
+
+void Session::editConfig(const NC_DATASTORE datastore,
+                         const NC_RPC_EDIT_DFLTOP defaultOperation,
+                         const NC_RPC_EDIT_TESTOPT testOption,
+                         const NC_RPC_EDIT_ERROPT errorOption,
+                         const std::string& data)
+{
+    auto rpc = impl::guarded(nc_rpc_edit(datastore, defaultOperation, testOption, errorOption, data.c_str(), NC_PARAMTYPE_CONST));
+    if (!rpc) {
+        throw std::runtime_error("Cannot create edit-config RPC");
+    }
+    impl::do_rpc_ok(this, std::move(rpc));
+}
+
+void Session::copyConfigFromString(const NC_DATASTORE target, const std::string& data)
+{
+    auto rpc = impl::guarded(nc_rpc_copy(target, nullptr, target /* yeah, cannot be 0... */, data.c_str(), NC_WD_UNKNOWN, NC_PARAMTYPE_CONST));
+    if (!rpc) {
+        throw std::runtime_error("Cannot create copy-config RPC");
+    }
+    impl::do_rpc_ok(this, std::move(rpc));
+}
+
+void Session::commit()
+{
+    auto rpc = impl::guarded(nc_rpc_commit(1, /* "Optional confirm timeout" how do you optional an uint32_t? */ 0, nullptr, nullptr, NC_PARAMTYPE_CONST));
+    if (!rpc) {
+        throw std::runtime_error("Cannot create commit RPC");
+    }
+    impl::do_rpc_ok(this, std::move(rpc));
+}
+
+void Session::discard()
+{
+    auto rpc = impl::guarded(nc_rpc_discard());
+    if (!rpc) {
+        throw std::runtime_error("Cannot create discard RPC");
+    }
+    impl::do_rpc_ok(this, std::move(rpc));
+}
+
+ReportedError::ReportedError(const std::string& what)
+    : std::runtime_error(what)
+{
+}
+
+ReportedError::~ReportedError() = default;
+}
+}
diff --git a/src/netconf-client.h b/src/netconf-client.h
new file mode 100644
index 0000000..8162b64
--- /dev/null
+++ b/src/netconf-client.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <libnetconf2/messages_client.h>
+#include <memory>
+#include <optional>
+#include <string>
+#include <string_view>
+#include <vector>
+
+struct nc_session;
+
+namespace libyang {
+class Data_Node;
+}
+
+namespace libnetconf {
+namespace client {
+
+class ReportedError : public std::runtime_error {
+public:
+    ReportedError(const std::string& what);
+    ~ReportedError() override;
+};
+
+class Session {
+public:
+    Session(struct nc_session* session);
+    ~Session();
+    static std::unique_ptr<Session> viaSSH(const std::string& host, const uint16_t port, const std::string& user);
+    static std::unique_ptr<Session> connectPubkey(const std::string& host, const uint16_t port, const std::string& user, const std::string& pubPath, const std::string& privPath);
+    static std::unique_ptr<Session> connectSocket(const std::string& path);
+    std::vector<std::string_view> capabilities() const;
+    std::shared_ptr<libyang::Data_Node> getConfig(const NC_DATASTORE datastore,
+                                                  const std::optional<const std::string> filter = std::nullopt); // TODO: arguments...
+    std::shared_ptr<libyang::Data_Node> get(const std::string& filter);
+    std::string getSchema(const std::string_view identifier, const std::optional<std::string_view> version);
+    void editConfig(const NC_DATASTORE datastore,
+                    const NC_RPC_EDIT_DFLTOP defaultOperation,
+                    const NC_RPC_EDIT_TESTOPT testOption,
+                    const NC_RPC_EDIT_ERROPT errorOption,
+                    const std::string& data);
+    void copyConfigFromString(const NC_DATASTORE target, const std::string& data);
+    void commit();
+    void discard();
+    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
new file mode 100644
index 0000000..9974fca
--- /dev/null
+++ b/src/netconf_access.cpp
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Václav Kubernát <kubernat@cesnet.cz>
+ *
+*/
+
+#include <libyang/Libyang.hpp>
+#include <libyang/Tree_Data.hpp>
+#include "netconf-client.h"
+#include "netconf_access.hpp"
+#include "utils.hpp"
+#include "yang_schema.hpp"
+
+leaf_data_ leafValueFromValue(const libyang::S_Value& value, LY_DATA_TYPE type)
+{
+    using namespace std::string_literals;
+    switch (type) {
+    case LY_TYPE_INT8:
+        return value->int8();
+    case LY_TYPE_INT16:
+        return value->int16();
+    case LY_TYPE_INT32:
+        return value->int32();
+    case LY_TYPE_INT64:
+        return value->int64();
+    case LY_TYPE_UINT8:
+        return value->uint8();
+    case LY_TYPE_UINT16:
+        return value->uint16();
+    case LY_TYPE_UINT32:
+        return value->uintu32();
+    case LY_TYPE_UINT64:
+        return value->uint64();
+    case LY_TYPE_BOOL:
+        return value->bln();
+    case LY_TYPE_STRING:
+        return std::string(value->string());
+    case LY_TYPE_ENUM:
+        return std::string(value->enm()->name());
+    default: // TODO: implement all types
+        return "(can't print)"s;
+    }
+}
+
+NetconfAccess::~NetconfAccess() = default;
+
+std::map<std::string, leaf_data_> NetconfAccess::getItems(const std::string& path)
+{
+    using namespace std::string_literals;
+    std::map<std::string, leaf_data_> res;
+
+    // This is very similar to the fillMap lambda in SysrepoAccess, however,
+    // Sysrepo returns a weird array-like structure, while libnetconf
+    // returns libyang::Data_Node
+    auto fillMap = [&res](auto items) {
+        for (const auto& it : items) {
+            if (!it)
+                continue;
+            if (it->schema()->nodetype() == LYS_LEAF) {
+                libyang::Data_Node_Leaf_List leaf(it);
+                res.emplace(leaf.path(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
+            }
+        }
+    };
+
+    if (path == "/") {
+        for (auto it : m_session->getConfig(NC_DATASTORE_RUNNING)->tree_for()) {
+            fillMap(it->tree_dfs());
+        }
+    } else {
+        auto data = m_session->getConfig(NC_DATASTORE_RUNNING, path);
+        fillMap(data->tree_dfs());
+    }
+
+    return res;
+}
+
+void NetconfAccess::datastoreInit()
+{
+    m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule) {
+        return fetchSchema(moduleName,
+                           revision ? std::optional{revision} : std::nullopt,
+                           submodule ? std::optional{submodule} : 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);
+    datastoreInit();
+}
+
+NetconfAccess::NetconfAccess(const std::string& socketPath)
+    : m_schema(new YangSchema())
+{
+    m_session = libnetconf::client::Session::connectSocket(socketPath);
+    datastoreInit();
+}
+
+void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
+{
+    auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
+    doEditFromDataNode(node);
+}
+
+void NetconfAccess::createPresenceContainer(const std::string& path)
+{
+    auto node = m_schema->dataNodeFromPath(path);
+    doEditFromDataNode(node);
+}
+
+void NetconfAccess::deletePresenceContainer(const std::string& path)
+{
+    auto node = m_schema->dataNodeFromPath(path);
+    node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
+    doEditFromDataNode(node);
+}
+
+void NetconfAccess::createListInstance(const std::string& path)
+{
+    auto node = m_schema->dataNodeFromPath(path);
+    doEditFromDataNode(node);
+}
+
+void NetconfAccess::deleteListInstance(const std::string& path)
+{
+    auto node = m_schema->dataNodeFromPath(path);
+    node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
+    doEditFromDataNode(node);
+}
+
+void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
+{
+    auto data = dataNode->print_mem(LYD_XML, 0);
+    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()
+{
+    m_session->commit();
+}
+
+void NetconfAccess::discardChanges()
+{
+    m_session->discard();
+}
+
+std::string NetconfAccess::fetchSchema(const std::string_view module, const std::optional<std::string_view> revision, const std::optional<std::string_view>)
+{
+    return m_session->getSchema(module, revision);
+}
+
+std::vector<std::string> NetconfAccess::listImplementedSchemas()
+{
+    auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
+    auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
+
+    std::vector<std::string> res;
+    for (auto it : set->data()) {
+        if (it->schema()->nodetype() == LYS_LEAF) {
+            libyang::Data_Node_Leaf_List leaf(it);
+            res.push_back(leaf.value_str());
+        }
+    }
+    return res;
+}
+
+std::shared_ptr<Schema> NetconfAccess::schema()
+{
+    return m_schema;
+}
diff --git a/src/netconf_access.hpp b/src/netconf_access.hpp
new file mode 100644
index 0000000..1c9fb04
--- /dev/null
+++ b/src/netconf_access.hpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Václav Kubernát <kubernat@cesnet.cz>
+ *
+*/
+
+#pragma once
+
+#include <string>
+#include "ast_commands.hpp"
+#include "datastore_access.hpp"
+
+/*! \class NetconfAccess
+ *     \brief Implementation of DatastoreAccess for accessing a NETCONF server
+ */
+
+namespace libnetconf {
+namespace client {
+class Session;
+}
+}
+
+namespace libyang {
+class Data_Node;
+}
+
+class Schema;
+class YangSchema;
+
+class NetconfAccess : public DatastoreAccess {
+public:
+    NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey);
+    NetconfAccess(const std::string& socketPath);
+    ~NetconfAccess() override;
+    std::map<std::string, leaf_data_> getItems(const std::string& path) override;
+    void setLeaf(const std::string& path, leaf_data_ value) override;
+    void createPresenceContainer(const std::string& path) override;
+    void deletePresenceContainer(const std::string& path) override;
+    void createListInstance(const std::string& path) override;
+    void deleteListInstance(const std::string& path) override;
+    void commitChanges() override;
+    void discardChanges() override;
+
+    std::shared_ptr<Schema> schema() override;
+
+private:
+    std::string fetchSchema(const std::string_view module, const std::optional<std::string_view> revision, const std::optional<std::string_view>);
+    std::vector<std::string> listImplementedSchemas();
+    void datastoreInit();
+    void doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode);
+
+    std::unique_ptr<libnetconf::client::Session> m_session;
+    std::shared_ptr<YangSchema> m_schema;
+};
diff --git a/src/utils.cpp b/src/utils.cpp
index b6df7e8..cd1cf4b 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -103,7 +103,7 @@
 
     std::string operator()(const identityRef_& data) const
     {
-        return data.m_value;
+        return data.m_prefix.value().m_name + ":" + data.m_value;
     }
 
     std::string operator()(const std::string& data) const
diff --git a/src/utils.hpp b/src/utils.hpp
index a9fde88..bd959e3 100644
--- a/src/utils.hpp
+++ b/src/utils.hpp
@@ -10,6 +10,7 @@
 */
 #include <string>
 #include "ast_path.hpp"
+#include "ast_values.hpp"
 #include "schema.hpp"
 
 std::string joinPaths(const std::string& prefix, const std::string& suffix);
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index 6cd7024..5f8a8cf 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -7,6 +7,7 @@
 */
 
 #include <libyang/Libyang.hpp>
+#include <libyang/Tree_Data.hpp>
 #include <libyang/Tree_Schema.hpp>
 #include <string_view>
 #include "UniqueResource.h"
@@ -341,3 +342,17 @@
     };
     m_context->add_missing_module_callback(lambda, deleter);
 }
+
+std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
+{
+    return std::make_shared<libyang::Data_Node>(m_context,
+                                                path.c_str(),
+                                                value ? value.value().c_str() : nullptr,
+                                                LYD_ANYDATA_CONSTSTRING,
+                                                0);
+}
+
+std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
+{
+    return m_context->get_module(name.c_str(), nullptr, 0);
+}
diff --git a/src/yang_schema.hpp b/src/yang_schema.hpp
index baeb466..4565150 100644
--- a/src/yang_schema.hpp
+++ b/src/yang_schema.hpp
@@ -9,6 +9,7 @@
 #pragma once
 
 #include <functional>
+#include <optional>
 #include <set>
 #include <stdexcept>
 #include <unordered_map>
@@ -19,6 +20,8 @@
 class Context;
 class Set;
 class Schema_Node;
+class Data_Node;
+class Module;
 }
 
 /*! \class YangSchema
@@ -57,6 +60,10 @@
     /** @short Adds a new directory for schema lookup. */
     void addSchemaDirectory(const char* directoryName);
 
+    /** @short Creates a new data node from a path (to be used with NETCONF edit-config) */
+    std::shared_ptr<libyang::Data_Node> dataNodeFromPath(const std::string& path, const std::optional<const std::string> value = std::nullopt) const;
+    std::shared_ptr<libyang::Module> getYangModule(const std::string& name);
+
 private:
     std::set<std::string> modules() const;