blob: 5fca2003ab7d76dfe8a19117d8ad33f796595f4f [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 }
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;
Jan Kundrátb091dd42020-01-30 09:28:28 +010055 auto config = m_session->get((path != "/") ? std::optional{path} : std::nullopt);
Václav Kubernát9456b5c2019-10-02 21:14:52 +020056
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
Václav Kubernátc31bd602019-03-07 11:44:48 +010065NetconfAccess::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 +010066 : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey))
67 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010068{
Václav Kubernátc31bd602019-03-07 11:44:48 +010069}
70
Jan Kundrátdab815e2020-01-22 19:44:11 +010071NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
72 : m_session(std::move(session))
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010073 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Jan Kundrátdab815e2020-01-22 19:44:11 +010074{
Jan Kundrátdab815e2020-01-22 19:44:11 +010075}
76
Václav Kubernátc31bd602019-03-07 11:44:48 +010077NetconfAccess::NetconfAccess(const std::string& socketPath)
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010078 : m_session(libnetconf::client::Session::connectSocket(socketPath))
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
83void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
84{
85 auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
86 doEditFromDataNode(node);
87}
88
89void NetconfAccess::createPresenceContainer(const std::string& path)
90{
91 auto node = m_schema->dataNodeFromPath(path);
92 doEditFromDataNode(node);
93}
94
95void NetconfAccess::deletePresenceContainer(const std::string& path)
96{
97 auto node = m_schema->dataNodeFromPath(path);
Václav Kubernátda8e4b92020-02-04 11:56:30 +010098 auto container = *(node->find_path(path.c_str())->data().begin());
99 container->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +0100100 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);
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100112 auto list = *(node->find_path(path.c_str())->data().begin());
113 list->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +0100114 doEditFromDataNode(node);
115}
116
117void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
118{
119 auto data = dataNode->print_mem(LYD_XML, 0);
120 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
121}
122
123void NetconfAccess::commitChanges()
124{
125 m_session->commit();
126}
127
128void NetconfAccess::discardChanges()
129{
130 m_session->discard();
131}
132
Jan Kundrát6ee84792020-01-24 01:43:36 +0100133DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input)
134{
135 auto root = m_schema->dataNodeFromPath(path);
136 for (const auto& [k, v] : input) {
137 auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v));
138 root->merge(node, 0);
139 }
140 auto data = root->print_mem(LYD_XML, 0);
141
142 Tree res;
143 auto output = m_session->rpc(data);
144 if (output) {
145 for (auto it : output->tree_for()) {
146 fillMap(res, it->tree_dfs(), joinPaths(path, "/"));
147 }
148 }
149 return res;
150}
151
Václav Kubernát7160a132020-04-03 02:11:01 +0200152NC_DATASTORE toNcDatastore(Datastore datastore)
153{
154 switch (datastore) {
155 case Datastore::Running:
156 return NC_DATASTORE_RUNNING;
157 case Datastore::Startup:
158 return NC_DATASTORE_STARTUP;
159 }
160 __builtin_unreachable();
161}
162
163void NetconfAccess::copyConfig(const Datastore source, const Datastore destination)
164{
165 m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination));
166}
167
Václav Kubernátb52dc252019-12-04 13:03:39 +0100168std::string NetconfAccess::fetchSchema(const std::string_view module, const
169 std::optional<std::string_view> revision, const
170 std::optional<std::string_view> submodule, const
171 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100172{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100173 if (submodule) {
174 return m_session->getSchema(*submodule, submoduleRevision);
175 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100176 return m_session->getSchema(module, revision);
177}
178
179std::vector<std::string> NetconfAccess::listImplementedSchemas()
180{
181 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
182 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
183
184 std::vector<std::string> res;
185 for (auto it : set->data()) {
186 if (it->schema()->nodetype() == LYS_LEAF) {
187 libyang::Data_Node_Leaf_List leaf(it);
188 res.push_back(leaf.value_str());
189 }
190 }
191 return res;
192}
193
194std::shared_ptr<Schema> NetconfAccess::schema()
195{
196 return m_schema;
197}
Václav Kubernátab612e92019-11-26 19:51:31 +0100198
199std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path)
200{
201 std::vector<ListInstance> res;
202 auto list = m_schema->dataNodeFromPath(path);
203
204 // This inserts selection nodes - I only want keys not other data
205 // 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)
206 auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys();
207 for (const auto& keyLeaf : keys) {
208 // Have to call find_path here - otherwise I'll have the list, not the leaf inside it
209 auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin());
210 auto actualList = *(list->find_path(path.c_str())->data().begin());
211 actualList->insert(selectionLeaf);
212 }
213
Jan Kundrátbb525b42020-02-04 11:56:59 +0100214 auto instances = m_session->get(list->print_mem(LYD_XML, 0));
Václav Kubernátab612e92019-11-26 19:51:31 +0100215
Václav Kubernát45e55462020-02-04 11:19:32 +0100216 if (!instances) {
217 return res;
218 }
Václav Kubernátab612e92019-11-26 19:51:31 +0100219
220 for (const auto& instance : instances->find_path(path.c_str())->data()) {
221 ListInstance instanceRes;
222
223 // I take the first child here, because the first element (the parent of the child()) will be the list
224 for (const auto& keyLeaf : instance->child()->tree_for()) {
225 auto leafData = libyang::Data_Node_Leaf_List{keyLeaf};
226 auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()};
227 instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())});
228 }
229 res.push_back(instanceRes);
230 }
231
232 return res;
233}