blob: e648a3395ab15fd261cee0fc10239235e26b265d [file] [log] [blame]
Václav Kubernátc31bd602019-03-07 11:44:48 +01001/*
2 * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubernat@cesnet.cz>
5 *
6*/
7
8#include <libyang/Libyang.hpp>
9#include <libyang/Tree_Data.hpp>
Václav Kubernát02a71152020-01-21 14:52:51 +010010#include "libyang_utils.hpp"
Václav Kubernátc31bd602019-03-07 11:44:48 +010011#include "netconf-client.h"
12#include "netconf_access.hpp"
13#include "utils.hpp"
14#include "yang_schema.hpp"
15
Václav Kubernátc31bd602019-03-07 11:44:48 +010016NetconfAccess::~NetconfAccess() = default;
17
Jan Kundrátb331b552020-01-23 15:25:29 +010018DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010019{
Jan Kundrátb331b552020-01-23 15:25:29 +010020 Tree res;
Václav Kubernátc31bd602019-03-07 11:44:48 +010021
22 // This is very similar to the fillMap lambda in SysrepoAccess, however,
23 // Sysrepo returns a weird array-like structure, while libnetconf
24 // returns libyang::Data_Node
25 auto fillMap = [&res](auto items) {
26 for (const auto& it : items) {
27 if (!it)
28 continue;
Václav Kubernátd812cfb2020-01-07 17:30:20 +010029 if (it->schema()->nodetype() == LYS_LIST) {
Václav Kubernát144729d2020-01-08 15:20:35 +010030 res.emplace(it->path(), special_{SpecialValue::List});
Václav Kubernátd812cfb2020-01-07 17:30:20 +010031 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010032 if (it->schema()->nodetype() == LYS_LEAF) {
33 libyang::Data_Node_Leaf_List leaf(it);
34 res.emplace(leaf.path(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
35 }
36 }
37 };
38
Václav Kubernát9456b5c2019-10-02 21:14:52 +020039 auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt);
40
41 if (config) {
42 for (auto it : config->tree_for()) {
Václav Kubernátc31bd602019-03-07 11:44:48 +010043 fillMap(it->tree_dfs());
44 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010045 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010046 return res;
47}
48
49void NetconfAccess::datastoreInit()
50{
Václav Kubernátb52dc252019-12-04 13:03:39 +010051 m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule, const char* submoduleRevision) {
Václav Kubernátc31bd602019-03-07 11:44:48 +010052 return fetchSchema(moduleName,
53 revision ? std::optional{revision} : std::nullopt,
Václav Kubernátb52dc252019-12-04 13:03:39 +010054 submodule ? std::optional{submodule} : std::nullopt,
55 submoduleRevision ? std::optional{submoduleRevision} : std::nullopt);
Václav Kubernátc31bd602019-03-07 11:44:48 +010056 });
57
58 for (const auto& it : listImplementedSchemas()) {
59 m_schema->loadModule(it);
60 }
61}
62
63NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
64 : m_schema(new YangSchema())
65{
66 m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey);
67 datastoreInit();
68}
69
Jan Kundrátdab815e2020-01-22 19:44:11 +010070NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
71 : m_session(std::move(session))
72 , m_schema(new YangSchema())
73{
74 datastoreInit();
75}
76
Václav Kubernátc31bd602019-03-07 11:44:48 +010077NetconfAccess::NetconfAccess(const std::string& socketPath)
78 : m_schema(new YangSchema())
79{
80 m_session = libnetconf::client::Session::connectSocket(socketPath);
81 datastoreInit();
82}
83
84void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
85{
86 auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
87 doEditFromDataNode(node);
88}
89
90void NetconfAccess::createPresenceContainer(const std::string& path)
91{
92 auto node = m_schema->dataNodeFromPath(path);
93 doEditFromDataNode(node);
94}
95
96void NetconfAccess::deletePresenceContainer(const std::string& path)
97{
98 auto node = m_schema->dataNodeFromPath(path);
99 node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
100 doEditFromDataNode(node);
101}
102
103void NetconfAccess::createListInstance(const std::string& path)
104{
105 auto node = m_schema->dataNodeFromPath(path);
106 doEditFromDataNode(node);
107}
108
109void NetconfAccess::deleteListInstance(const std::string& path)
110{
111 auto node = m_schema->dataNodeFromPath(path);
112 node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
113 doEditFromDataNode(node);
114}
115
116void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
117{
118 auto data = dataNode->print_mem(LYD_XML, 0);
119 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
120}
121
122void NetconfAccess::commitChanges()
123{
124 m_session->commit();
125}
126
127void NetconfAccess::discardChanges()
128{
129 m_session->discard();
130}
131
Václav Kubernátb52dc252019-12-04 13:03:39 +0100132std::string NetconfAccess::fetchSchema(const std::string_view module, const
133 std::optional<std::string_view> revision, const
134 std::optional<std::string_view> submodule, const
135 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100136{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100137 if (submodule) {
138 return m_session->getSchema(*submodule, submoduleRevision);
139 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100140 return m_session->getSchema(module, revision);
141}
142
143std::vector<std::string> NetconfAccess::listImplementedSchemas()
144{
145 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
146 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
147
148 std::vector<std::string> res;
149 for (auto it : set->data()) {
150 if (it->schema()->nodetype() == LYS_LEAF) {
151 libyang::Data_Node_Leaf_List leaf(it);
152 res.push_back(leaf.value_str());
153 }
154 }
155 return res;
156}
157
158std::shared_ptr<Schema> NetconfAccess::schema()
159{
160 return m_schema;
161}