blob: 6fd3f69189aecb1a5554af11fda9b48d2a8f9104 [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át26b56082020-02-03 18:28:56 +010011#include "netconf-client.hpp"
Václav Kubernátc31bd602019-03-07 11:44:48 +010012#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 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020041 if (it->schema()->nodetype() == LYS_LEAF || it->schema()->nodetype() == LYS_LEAFLIST) {
42 using namespace std::string_literals;
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());
45 if (it->schema()->nodetype() == LYS_LEAFLIST) {
46 std::string strippedLeafListValue = stripLeafListValueFromPath(it->path());
47 res.emplace(stripXPathPrefix(strippedLeafListValue), special_{SpecialValue::LeafList});
48 }
49 res.emplace(stripXPathPrefix(it->path()), value);
Jan Kundrát8f63fb72020-01-24 01:40:01 +010050 }
51 }
52}
53}
54
55
Václav Kubernátc31bd602019-03-07 11:44:48 +010056NetconfAccess::~NetconfAccess() = default;
57
Jan Kundrátb331b552020-01-23 15:25:29 +010058DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010059{
Jan Kundrátb331b552020-01-23 15:25:29 +010060 Tree res;
Jan Kundrátb091dd42020-01-30 09:28:28 +010061 auto config = m_session->get((path != "/") ? std::optional{path} : std::nullopt);
Václav Kubernát9456b5c2019-10-02 21:14:52 +020062
63 if (config) {
64 for (auto it : config->tree_for()) {
Jan Kundrát8f63fb72020-01-24 01:40:01 +010065 fillMap(res, it->tree_dfs());
Václav Kubernátc31bd602019-03-07 11:44:48 +010066 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010067 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010068 return res;
69}
70
Václav Kubernátc31bd602019-03-07 11:44:48 +010071NetconfAccess::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 +010072 : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey))
73 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010074{
Václav Kubernátc31bd602019-03-07 11:44:48 +010075}
76
Jan Kundrátdab815e2020-01-22 19:44:11 +010077NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
78 : m_session(std::move(session))
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010079 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Jan Kundrátdab815e2020-01-22 19:44:11 +010080{
Jan Kundrátdab815e2020-01-22 19:44:11 +010081}
82
Václav Kubernátc31bd602019-03-07 11:44:48 +010083NetconfAccess::NetconfAccess(const std::string& socketPath)
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010084 : m_session(libnetconf::client::Session::connectSocket(socketPath))
85 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010086{
Václav Kubernátc31bd602019-03-07 11:44:48 +010087}
88
89void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
90{
Jan Kundrát379bb572020-05-07 03:23:13 +020091 auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value));
92 auto node = m_schema->dataNodeFromPath(path, lyValue);
Václav Kubernátc31bd602019-03-07 11:44:48 +010093 doEditFromDataNode(node);
94}
95
96void NetconfAccess::createPresenceContainer(const std::string& path)
97{
98 auto node = m_schema->dataNodeFromPath(path);
99 doEditFromDataNode(node);
100}
101
102void NetconfAccess::deletePresenceContainer(const std::string& path)
103{
104 auto node = m_schema->dataNodeFromPath(path);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100105 auto container = *(node->find_path(path.c_str())->data().begin());
106 container->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +0100107 doEditFromDataNode(node);
108}
109
110void NetconfAccess::createListInstance(const std::string& path)
111{
112 auto node = m_schema->dataNodeFromPath(path);
113 doEditFromDataNode(node);
114}
115
116void NetconfAccess::deleteListInstance(const std::string& path)
117{
118 auto node = m_schema->dataNodeFromPath(path);
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100119 auto list = *(node->find_path(path.c_str())->data().begin());
120 list->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +0100121 doEditFromDataNode(node);
122}
123
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200124void NetconfAccess::createLeafListInstance(const std::string& path)
125{
126 auto node = m_schema->dataNodeFromPath(path);
127 doEditFromDataNode(node);
128}
129void NetconfAccess::deleteLeafListInstance(const std::string& path)
130{
131 auto node = m_schema->dataNodeFromPath(path);
132 auto list = *(node->find_path(path.c_str())->data().begin());
133 list->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
134 doEditFromDataNode(node);
135}
136
Václav Kubernátc31bd602019-03-07 11:44:48 +0100137void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
138{
139 auto data = dataNode->print_mem(LYD_XML, 0);
140 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
141}
142
143void NetconfAccess::commitChanges()
144{
145 m_session->commit();
146}
147
148void NetconfAccess::discardChanges()
149{
150 m_session->discard();
151}
152
Jan Kundrát6ee84792020-01-24 01:43:36 +0100153DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input)
154{
155 auto root = m_schema->dataNodeFromPath(path);
156 for (const auto& [k, v] : input) {
157 auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v));
158 root->merge(node, 0);
159 }
160 auto data = root->print_mem(LYD_XML, 0);
161
162 Tree res;
163 auto output = m_session->rpc(data);
164 if (output) {
165 for (auto it : output->tree_for()) {
166 fillMap(res, it->tree_dfs(), joinPaths(path, "/"));
167 }
168 }
169 return res;
170}
171
Václav Kubernát7160a132020-04-03 02:11:01 +0200172NC_DATASTORE toNcDatastore(Datastore datastore)
173{
174 switch (datastore) {
175 case Datastore::Running:
176 return NC_DATASTORE_RUNNING;
177 case Datastore::Startup:
178 return NC_DATASTORE_STARTUP;
179 }
180 __builtin_unreachable();
181}
182
183void NetconfAccess::copyConfig(const Datastore source, const Datastore destination)
184{
185 m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination));
186}
187
Václav Kubernátb52dc252019-12-04 13:03:39 +0100188std::string NetconfAccess::fetchSchema(const std::string_view module, const
189 std::optional<std::string_view> revision, const
190 std::optional<std::string_view> submodule, const
191 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100192{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100193 if (submodule) {
194 return m_session->getSchema(*submodule, submoduleRevision);
195 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100196 return m_session->getSchema(module, revision);
197}
198
199std::vector<std::string> NetconfAccess::listImplementedSchemas()
200{
201 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
202 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
203
204 std::vector<std::string> res;
205 for (auto it : set->data()) {
206 if (it->schema()->nodetype() == LYS_LEAF) {
207 libyang::Data_Node_Leaf_List leaf(it);
208 res.push_back(leaf.value_str());
209 }
210 }
211 return res;
212}
213
214std::shared_ptr<Schema> NetconfAccess::schema()
215{
216 return m_schema;
217}
Václav Kubernátab612e92019-11-26 19:51:31 +0100218
219std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path)
220{
221 std::vector<ListInstance> res;
222 auto list = m_schema->dataNodeFromPath(path);
223
224 // This inserts selection nodes - I only want keys not other data
225 // 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)
226 auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys();
227 for (const auto& keyLeaf : keys) {
228 // Have to call find_path here - otherwise I'll have the list, not the leaf inside it
229 auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin());
230 auto actualList = *(list->find_path(path.c_str())->data().begin());
231 actualList->insert(selectionLeaf);
232 }
233
Jan Kundrátbb525b42020-02-04 11:56:59 +0100234 auto instances = m_session->get(list->print_mem(LYD_XML, 0));
Václav Kubernátab612e92019-11-26 19:51:31 +0100235
Václav Kubernát45e55462020-02-04 11:19:32 +0100236 if (!instances) {
237 return res;
238 }
Václav Kubernátab612e92019-11-26 19:51:31 +0100239
240 for (const auto& instance : instances->find_path(path.c_str())->data()) {
241 ListInstance instanceRes;
242
243 // I take the first child here, because the first element (the parent of the child()) will be the list
244 for (const auto& keyLeaf : instance->child()->tree_for()) {
245 auto leafData = libyang::Data_Node_Leaf_List{keyLeaf};
246 auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()};
247 instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())});
248 }
249 res.push_back(instanceRes);
250 }
251
252 return res;
253}