blob: 5e2e52ad06b74b87ab296a9f568699441d93e02e [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
Václav Kubernátcf9224f2020-06-02 09:55:29 +02008#include <boost/algorithm/string/predicate.hpp>
Václav Kubernátc31bd602019-03-07 11:44:48 +01009#include <libyang/Libyang.hpp>
10#include <libyang/Tree_Data.hpp>
Václav Kubernát02a71152020-01-21 14:52:51 +010011#include "libyang_utils.hpp"
Václav Kubernát26b56082020-02-03 18:28:56 +010012#include "netconf-client.hpp"
Václav Kubernátc31bd602019-03-07 11:44:48 +010013#include "netconf_access.hpp"
14#include "utils.hpp"
15#include "yang_schema.hpp"
16
Jan Kundrát8f63fb72020-01-24 01:40:01 +010017namespace {
18
19// This is very similar to the fillMap lambda in SysrepoAccess, however,
20// Sysrepo returns a weird array-like structure, while libnetconf
21// returns libyang::Data_Node
Jan Kundrát6ee84792020-01-24 01:43:36 +010022void 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 +010023{
Jan Kundrát6ee84792020-01-24 01:43:36 +010024 auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) {
25 return ignoredXPathPrefix ? path.substr(ignoredXPathPrefix->size()) : path;
26 };
27
Jan Kundrát8f63fb72020-01-24 01:40:01 +010028 for (const auto& it : items) {
29 if (!it)
30 continue;
Václav Kubernát69aabe92020-01-24 16:53:12 +010031 if (it->schema()->nodetype() == LYS_CONTAINER) {
32 if (libyang::Schema_Node_Container{it->schema()}.presence()) {
33 // The fact that the container is included in the data tree
34 // means that it is present and I don't need to check any
35 // value.
Václav Kubernátcf9224f2020-06-02 09:55:29 +020036 res.emplace_back(stripXPathPrefix(it->path()), special_{SpecialValue::PresenceContainer});
Václav Kubernát69aabe92020-01-24 16:53:12 +010037 }
38 }
Jan Kundrát8f63fb72020-01-24 01:40:01 +010039 if (it->schema()->nodetype() == LYS_LIST) {
Václav Kubernátcf9224f2020-06-02 09:55:29 +020040 res.push_back({stripXPathPrefix(it->path()), special_{SpecialValue::List}});
Jan Kundrát8f63fb72020-01-24 01:40:01 +010041 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020042 if (it->schema()->nodetype() == LYS_LEAF || it->schema()->nodetype() == LYS_LEAFLIST) {
Jan Kundrát8f63fb72020-01-24 01:40:01 +010043 libyang::Data_Node_Leaf_List leaf(it);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020044 auto value = leafValueFromValue(leaf.value(), leaf.leaf_type()->base());
Václav Kubernátcf9224f2020-06-02 09:55:29 +020045 res.emplace_back(stripXPathPrefix(it->path()), value);
Jan Kundrát8f63fb72020-01-24 01:40:01 +010046 }
47 }
48}
49}
50
51
Václav Kubernátc31bd602019-03-07 11:44:48 +010052NetconfAccess::~NetconfAccess() = default;
53
Jan Kundrátb331b552020-01-23 15:25:29 +010054DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010055{
Jan Kundrátb331b552020-01-23 15:25:29 +010056 Tree res;
Jan Kundrátb091dd42020-01-30 09:28:28 +010057 auto config = m_session->get((path != "/") ? std::optional{path} : std::nullopt);
Václav Kubernát9456b5c2019-10-02 21:14:52 +020058
59 if (config) {
Václav Kubernátcf9224f2020-06-02 09:55:29 +020060 auto siblings = config->tree_for();
61 for (auto it = siblings.begin(); it < siblings.end(); it++) {
62 if ((*it)->schema()->nodetype() == LYS_LEAFLIST) {
63 auto leafListPath = stripLeafListValueFromPath((*it)->path());
64 res.emplace_back(leafListPath, special_{SpecialValue::LeafList});
65 while (it != siblings.end() && boost::starts_with((*it)->path(), leafListPath)) {
66 fillMap(res, (*it)->tree_dfs());
67 it++;
68 }
69 } else {
70 fillMap(res, (*it)->tree_dfs());
71 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010072 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010073 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010074 return res;
75}
76
Václav Kubernátc31bd602019-03-07 11:44:48 +010077NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010078 : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey))
79 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010080{
Václav Kubernátc31bd602019-03-07 11:44:48 +010081}
82
Jan Kundrátdab815e2020-01-22 19:44:11 +010083NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
84 : m_session(std::move(session))
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010085 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Jan Kundrátdab815e2020-01-22 19:44:11 +010086{
Jan Kundrátdab815e2020-01-22 19:44:11 +010087}
88
Václav Kubernátc31bd602019-03-07 11:44:48 +010089NetconfAccess::NetconfAccess(const std::string& socketPath)
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010090 : m_session(libnetconf::client::Session::connectSocket(socketPath))
91 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010092{
Václav Kubernátc31bd602019-03-07 11:44:48 +010093}
94
95void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
96{
Jan Kundrát379bb572020-05-07 03:23:13 +020097 auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value));
98 auto node = m_schema->dataNodeFromPath(path, lyValue);
Václav Kubernátc31bd602019-03-07 11:44:48 +010099 doEditFromDataNode(node);
100}
101
102void NetconfAccess::createPresenceContainer(const std::string& path)
103{
104 auto node = m_schema->dataNodeFromPath(path);
105 doEditFromDataNode(node);
106}
107
108void NetconfAccess::deletePresenceContainer(const std::string& path)
109{
110 auto node = m_schema->dataNodeFromPath(path);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100111 auto container = *(node->find_path(path.c_str())->data().begin());
112 container->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +0100113 doEditFromDataNode(node);
114}
115
116void NetconfAccess::createListInstance(const std::string& path)
117{
118 auto node = m_schema->dataNodeFromPath(path);
119 doEditFromDataNode(node);
120}
121
122void NetconfAccess::deleteListInstance(const std::string& path)
123{
124 auto node = m_schema->dataNodeFromPath(path);
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100125 auto list = *(node->find_path(path.c_str())->data().begin());
126 list->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +0100127 doEditFromDataNode(node);
128}
129
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200130void NetconfAccess::createLeafListInstance(const std::string& path)
131{
132 auto node = m_schema->dataNodeFromPath(path);
133 doEditFromDataNode(node);
134}
135void NetconfAccess::deleteLeafListInstance(const std::string& path)
136{
137 auto node = m_schema->dataNodeFromPath(path);
138 auto list = *(node->find_path(path.c_str())->data().begin());
139 list->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
140 doEditFromDataNode(node);
141}
142
Václav Kubernátc31bd602019-03-07 11:44:48 +0100143void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
144{
145 auto data = dataNode->print_mem(LYD_XML, 0);
146 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
147}
148
149void NetconfAccess::commitChanges()
150{
151 m_session->commit();
152}
153
154void NetconfAccess::discardChanges()
155{
156 m_session->discard();
157}
158
Jan Kundrát6ee84792020-01-24 01:43:36 +0100159DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input)
160{
161 auto root = m_schema->dataNodeFromPath(path);
162 for (const auto& [k, v] : input) {
163 auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v));
164 root->merge(node, 0);
165 }
166 auto data = root->print_mem(LYD_XML, 0);
167
168 Tree res;
169 auto output = m_session->rpc(data);
170 if (output) {
171 for (auto it : output->tree_for()) {
172 fillMap(res, it->tree_dfs(), joinPaths(path, "/"));
173 }
174 }
175 return res;
176}
177
Václav Kubernát7160a132020-04-03 02:11:01 +0200178NC_DATASTORE toNcDatastore(Datastore datastore)
179{
180 switch (datastore) {
181 case Datastore::Running:
182 return NC_DATASTORE_RUNNING;
183 case Datastore::Startup:
184 return NC_DATASTORE_STARTUP;
185 }
186 __builtin_unreachable();
187}
188
189void NetconfAccess::copyConfig(const Datastore source, const Datastore destination)
190{
191 m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination));
192}
193
Václav Kubernátb52dc252019-12-04 13:03:39 +0100194std::string NetconfAccess::fetchSchema(const std::string_view module, const
195 std::optional<std::string_view> revision, const
196 std::optional<std::string_view> submodule, const
197 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100198{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100199 if (submodule) {
200 return m_session->getSchema(*submodule, submoduleRevision);
201 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100202 return m_session->getSchema(module, revision);
203}
204
205std::vector<std::string> NetconfAccess::listImplementedSchemas()
206{
207 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
208 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
209
210 std::vector<std::string> res;
211 for (auto it : set->data()) {
212 if (it->schema()->nodetype() == LYS_LEAF) {
213 libyang::Data_Node_Leaf_List leaf(it);
214 res.push_back(leaf.value_str());
215 }
216 }
217 return res;
218}
219
220std::shared_ptr<Schema> NetconfAccess::schema()
221{
222 return m_schema;
223}
Václav Kubernátab612e92019-11-26 19:51:31 +0100224
225std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path)
226{
227 std::vector<ListInstance> res;
228 auto list = m_schema->dataNodeFromPath(path);
229
230 // This inserts selection nodes - I only want keys not other data
231 // To get the keys, I have to call find_path here - otherwise I would get keys of a top-level node (which might not even be a list)
232 auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys();
233 for (const auto& keyLeaf : keys) {
234 // Have to call find_path here - otherwise I'll have the list, not the leaf inside it
235 auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin());
236 auto actualList = *(list->find_path(path.c_str())->data().begin());
237 actualList->insert(selectionLeaf);
238 }
239
Jan Kundrátbb525b42020-02-04 11:56:59 +0100240 auto instances = m_session->get(list->print_mem(LYD_XML, 0));
Václav Kubernátab612e92019-11-26 19:51:31 +0100241
Václav Kubernát45e55462020-02-04 11:19:32 +0100242 if (!instances) {
243 return res;
244 }
Václav Kubernátab612e92019-11-26 19:51:31 +0100245
246 for (const auto& instance : instances->find_path(path.c_str())->data()) {
247 ListInstance instanceRes;
248
249 // I take the first child here, because the first element (the parent of the child()) will be the list
250 for (const auto& keyLeaf : instance->child()->tree_for()) {
251 auto leafData = libyang::Data_Node_Leaf_List{keyLeaf};
252 auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()};
253 instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())});
254 }
255 res.push_back(instanceRes);
256 }
257
258 return res;
259}