blob: 73872311836fe09bff77dd208a3532e20fcd55e8 [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
Jan Kundrát8f63fb72020-01-24 01:40:01 +010016namespace {
17
18// This is very similar to the fillMap lambda in SysrepoAccess, however,
19// Sysrepo returns a weird array-like structure, while libnetconf
20// returns libyang::Data_Node
Jan Kundrát6ee84792020-01-24 01:43:36 +010021void fillMap(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix = std::nullopt)
Jan Kundrát8f63fb72020-01-24 01:40:01 +010022{
Jan Kundrát6ee84792020-01-24 01:43:36 +010023 auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) {
24 return ignoredXPathPrefix ? path.substr(ignoredXPathPrefix->size()) : path;
25 };
26
Jan Kundrát8f63fb72020-01-24 01:40:01 +010027 for (const auto& it : items) {
28 if (!it)
29 continue;
Václav Kubernát69aabe92020-01-24 16:53:12 +010030 if (it->schema()->nodetype() == LYS_CONTAINER) {
31 if (libyang::Schema_Node_Container{it->schema()}.presence()) {
32 // The fact that the container is included in the data tree
33 // means that it is present and I don't need to check any
34 // value.
Jan Kundrát6ee84792020-01-24 01:43:36 +010035 res.emplace(stripXPathPrefix(it->path()), special_{SpecialValue::PresenceContainer});
Václav Kubernát69aabe92020-01-24 16:53:12 +010036 }
37 }
Jan Kundrát8f63fb72020-01-24 01:40:01 +010038 if (it->schema()->nodetype() == LYS_LIST) {
Jan Kundrát6ee84792020-01-24 01:43:36 +010039 res.emplace(stripXPathPrefix(it->path()), special_{SpecialValue::List});
Jan Kundrát8f63fb72020-01-24 01:40:01 +010040 }
41 if (it->schema()->nodetype() == LYS_LEAF) {
42 libyang::Data_Node_Leaf_List leaf(it);
Jan Kundrát6ee84792020-01-24 01:43:36 +010043 res.emplace(stripXPathPrefix(it->path()), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
Jan Kundrát8f63fb72020-01-24 01:40:01 +010044 }
45 }
46}
47}
48
49
Václav Kubernátc31bd602019-03-07 11:44:48 +010050NetconfAccess::~NetconfAccess() = default;
51
Jan Kundrátb331b552020-01-23 15:25:29 +010052DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010053{
Jan Kundrátb331b552020-01-23 15:25:29 +010054 Tree res;
Václav Kubernát9456b5c2019-10-02 21:14:52 +020055 auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt);
56
57 if (config) {
58 for (auto it : config->tree_for()) {
Jan Kundrát8f63fb72020-01-24 01:40:01 +010059 fillMap(res, it->tree_dfs());
Václav Kubernátc31bd602019-03-07 11:44:48 +010060 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010061 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010062 return res;
63}
64
65void NetconfAccess::datastoreInit()
66{
Václav Kubernátb52dc252019-12-04 13:03:39 +010067 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 +010068 return fetchSchema(moduleName,
69 revision ? std::optional{revision} : std::nullopt,
Václav Kubernátb52dc252019-12-04 13:03:39 +010070 submodule ? std::optional{submodule} : std::nullopt,
71 submoduleRevision ? std::optional{submoduleRevision} : std::nullopt);
Václav Kubernátc31bd602019-03-07 11:44:48 +010072 });
73
74 for (const auto& it : listImplementedSchemas()) {
75 m_schema->loadModule(it);
76 }
77}
78
79NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
80 : m_schema(new YangSchema())
81{
82 m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey);
83 datastoreInit();
84}
85
Jan Kundrátdab815e2020-01-22 19:44:11 +010086NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
87 : m_session(std::move(session))
88 , m_schema(new YangSchema())
89{
90 datastoreInit();
91}
92
Václav Kubernátc31bd602019-03-07 11:44:48 +010093NetconfAccess::NetconfAccess(const std::string& socketPath)
94 : m_schema(new YangSchema())
95{
96 m_session = libnetconf::client::Session::connectSocket(socketPath);
97 datastoreInit();
98}
99
100void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
101{
102 auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
103 doEditFromDataNode(node);
104}
105
106void NetconfAccess::createPresenceContainer(const std::string& path)
107{
108 auto node = m_schema->dataNodeFromPath(path);
109 doEditFromDataNode(node);
110}
111
112void NetconfAccess::deletePresenceContainer(const std::string& path)
113{
114 auto node = m_schema->dataNodeFromPath(path);
115 node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
116 doEditFromDataNode(node);
117}
118
119void NetconfAccess::createListInstance(const std::string& path)
120{
121 auto node = m_schema->dataNodeFromPath(path);
122 doEditFromDataNode(node);
123}
124
125void NetconfAccess::deleteListInstance(const std::string& path)
126{
127 auto node = m_schema->dataNodeFromPath(path);
128 node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
129 doEditFromDataNode(node);
130}
131
132void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
133{
134 auto data = dataNode->print_mem(LYD_XML, 0);
135 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
136}
137
138void NetconfAccess::commitChanges()
139{
140 m_session->commit();
141}
142
143void NetconfAccess::discardChanges()
144{
145 m_session->discard();
146}
147
Jan Kundrát6ee84792020-01-24 01:43:36 +0100148DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input)
149{
150 auto root = m_schema->dataNodeFromPath(path);
151 for (const auto& [k, v] : input) {
152 auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v));
153 root->merge(node, 0);
154 }
155 auto data = root->print_mem(LYD_XML, 0);
156
157 Tree res;
158 auto output = m_session->rpc(data);
159 if (output) {
160 for (auto it : output->tree_for()) {
161 fillMap(res, it->tree_dfs(), joinPaths(path, "/"));
162 }
163 }
164 return res;
165}
166
Václav Kubernátb52dc252019-12-04 13:03:39 +0100167std::string NetconfAccess::fetchSchema(const std::string_view module, const
168 std::optional<std::string_view> revision, const
169 std::optional<std::string_view> submodule, const
170 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100171{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100172 if (submodule) {
173 return m_session->getSchema(*submodule, submoduleRevision);
174 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100175 return m_session->getSchema(module, revision);
176}
177
178std::vector<std::string> NetconfAccess::listImplementedSchemas()
179{
180 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
181 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
182
183 std::vector<std::string> res;
184 for (auto it : set->data()) {
185 if (it->schema()->nodetype() == LYS_LEAF) {
186 libyang::Data_Node_Leaf_List leaf(it);
187 res.push_back(leaf.value_str());
188 }
189 }
190 return res;
191}
192
193std::shared_ptr<Schema> NetconfAccess::schema()
194{
195 return m_schema;
196}