blob: a7cd6b43699de7c7fbe8b8193cdb3d459ae36c15 [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 +010017
Václav Kubernátc31bd602019-03-07 11:44:48 +010018NetconfAccess::~NetconfAccess() = default;
19
Jan Kundrátb331b552020-01-23 15:25:29 +010020DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010021{
Jan Kundrátb331b552020-01-23 15:25:29 +010022 Tree res;
Jan Kundrátb091dd42020-01-30 09:28:28 +010023 auto config = m_session->get((path != "/") ? std::optional{path} : std::nullopt);
Václav Kubernát9456b5c2019-10-02 21:14:52 +020024
25 if (config) {
Václav Kubernátcf9224f2020-06-02 09:55:29 +020026 auto siblings = config->tree_for();
27 for (auto it = siblings.begin(); it < siblings.end(); it++) {
28 if ((*it)->schema()->nodetype() == LYS_LEAFLIST) {
29 auto leafListPath = stripLeafListValueFromPath((*it)->path());
30 res.emplace_back(leafListPath, special_{SpecialValue::LeafList});
31 while (it != siblings.end() && boost::starts_with((*it)->path(), leafListPath)) {
Václav Kubernát3c8fe022020-06-04 01:35:03 +020032 lyNodesToTree(res, (*it)->tree_dfs());
Václav Kubernátcf9224f2020-06-02 09:55:29 +020033 it++;
34 }
35 } else {
Václav Kubernát3c8fe022020-06-04 01:35:03 +020036 lyNodesToTree(res, (*it)->tree_dfs());
Václav Kubernátcf9224f2020-06-02 09:55:29 +020037 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010038 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010039 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010040 return res;
41}
42
Václav Kubernátc31bd602019-03-07 11:44:48 +010043NetconfAccess::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 +010044 : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey))
45 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010046{
Václav Kubernátc31bd602019-03-07 11:44:48 +010047}
48
Jan Kundrátdab815e2020-01-22 19:44:11 +010049NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
50 : m_session(std::move(session))
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010051 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Jan Kundrátdab815e2020-01-22 19:44:11 +010052{
Jan Kundrátdab815e2020-01-22 19:44:11 +010053}
54
Václav Kubernátc31bd602019-03-07 11:44:48 +010055NetconfAccess::NetconfAccess(const std::string& socketPath)
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010056 : m_session(libnetconf::client::Session::connectSocket(socketPath))
57 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010058{
Václav Kubernátc31bd602019-03-07 11:44:48 +010059}
60
61void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
62{
Jan Kundrát379bb572020-05-07 03:23:13 +020063 auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value));
64 auto node = m_schema->dataNodeFromPath(path, lyValue);
Václav Kubernátc31bd602019-03-07 11:44:48 +010065 doEditFromDataNode(node);
66}
67
Jan Kundrátcbf288b2020-06-18 20:44:39 +020068void NetconfAccess::createItem(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010069{
70 auto node = m_schema->dataNodeFromPath(path);
71 doEditFromDataNode(node);
72}
73
Jan Kundrátcbf288b2020-06-18 20:44:39 +020074void NetconfAccess::deleteItem(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010075{
76 auto node = m_schema->dataNodeFromPath(path);
Václav Kubernátda8e4b92020-02-04 11:56:30 +010077 auto container = *(node->find_path(path.c_str())->data().begin());
78 container->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +010079 doEditFromDataNode(node);
80}
81
Václav Kubernátbf65dd72020-05-28 02:32:31 +020082struct impl_toYangInsert {
83 std::string operator()(yang::move::Absolute& absolute)
84 {
85 return absolute == yang::move::Absolute::Begin ? "first" : "last";
86 }
87 std::string operator()(yang::move::Relative& relative)
88 {
89 return relative.m_position == yang::move::Relative::Position::After ? "after" : "before";
90 }
91};
92
93std::string toYangInsert(std::variant<yang::move::Absolute, yang::move::Relative> move)
94{
95 return std::visit(impl_toYangInsert{}, move);
96}
97
98void NetconfAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
99{
100 auto node = m_schema->dataNodeFromPath(source);
101 auto sourceNode = *(node->find_path(source.c_str())->data().begin());
102 auto yangModule = m_schema->getYangModule("yang");
103 sourceNode->insert_attr(yangModule, "insert", toYangInsert(move).c_str());
104
105 if (std::holds_alternative<yang::move::Relative>(move)) {
106 auto relative = std::get<yang::move::Relative>(move);
107 if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
108 sourceNode->insert_attr(yangModule, "value", leafDataToString(relative.m_path.at(".")).c_str());
109 } else {
Václav Kubernát2c4778b2020-06-18 11:55:20 +0200110 sourceNode->insert_attr(yangModule, "key", instanceToString(relative.m_path, node->node_module()->name()).c_str());
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200111 }
112 }
113 doEditFromDataNode(sourceNode);
114}
115
Václav Kubernátc31bd602019-03-07 11:44:48 +0100116void 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
Jan Kundrát6ee84792020-01-24 01:43:36 +0100132DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input)
133{
134 auto root = m_schema->dataNodeFromPath(path);
135 for (const auto& [k, v] : input) {
136 auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v));
137 root->merge(node, 0);
138 }
139 auto data = root->print_mem(LYD_XML, 0);
140
141 Tree res;
142 auto output = m_session->rpc(data);
143 if (output) {
144 for (auto it : output->tree_for()) {
Václav Kubernát3c8fe022020-06-04 01:35:03 +0200145 lyNodesToTree(res, it->tree_dfs(), joinPaths(path, "/"));
Jan Kundrát6ee84792020-01-24 01:43:36 +0100146 }
147 }
148 return res;
149}
150
Václav Kubernát7160a132020-04-03 02:11:01 +0200151NC_DATASTORE toNcDatastore(Datastore datastore)
152{
153 switch (datastore) {
154 case Datastore::Running:
155 return NC_DATASTORE_RUNNING;
156 case Datastore::Startup:
157 return NC_DATASTORE_STARTUP;
158 }
159 __builtin_unreachable();
160}
161
162void NetconfAccess::copyConfig(const Datastore source, const Datastore destination)
163{
164 m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination));
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}
Václav Kubernátab612e92019-11-26 19:51:31 +0100197
198std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path)
199{
200 std::vector<ListInstance> res;
201 auto list = m_schema->dataNodeFromPath(path);
202
203 // This inserts selection nodes - I only want keys not other data
204 // 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)
205 auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys();
206 for (const auto& keyLeaf : keys) {
207 // Have to call find_path here - otherwise I'll have the list, not the leaf inside it
208 auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin());
209 auto actualList = *(list->find_path(path.c_str())->data().begin());
210 actualList->insert(selectionLeaf);
211 }
212
Jan Kundrátbb525b42020-02-04 11:56:59 +0100213 auto instances = m_session->get(list->print_mem(LYD_XML, 0));
Václav Kubernátab612e92019-11-26 19:51:31 +0100214
Václav Kubernát45e55462020-02-04 11:19:32 +0100215 if (!instances) {
216 return res;
217 }
Václav Kubernátab612e92019-11-26 19:51:31 +0100218
219 for (const auto& instance : instances->find_path(path.c_str())->data()) {
220 ListInstance instanceRes;
221
222 // I take the first child here, because the first element (the parent of the child()) will be the list
223 for (const auto& keyLeaf : instance->child()->tree_for()) {
224 auto leafData = libyang::Data_Node_Leaf_List{keyLeaf};
225 auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()};
226 instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())});
227 }
228 res.push_back(instanceRes);
229 }
230
231 return res;
232}