blob: 4b151d36263cac4e0b98942283dab720797e9f68 [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 +010016
Václav Kubernátc31bd602019-03-07 11:44:48 +010017NetconfAccess::~NetconfAccess() = default;
18
Václav Kubernátd6282912020-06-23 14:49:34 +020019DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path) const
Václav Kubernátc31bd602019-03-07 11:44:48 +010020{
Jan Kundrátb331b552020-01-23 15:25:29 +010021 Tree res;
Jan Kundrátb091dd42020-01-30 09:28:28 +010022 auto config = m_session->get((path != "/") ? std::optional{path} : std::nullopt);
Václav Kubernát9456b5c2019-10-02 21:14:52 +020023
24 if (config) {
Václav Kubernátdaf40312020-06-19 11:34:55 +020025 lyNodesToTree(res, config->tree_for());
Václav Kubernátc31bd602019-03-07 11:44:48 +010026 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010027 return res;
28}
29
Václav Kubernátc31bd602019-03-07 11:44:48 +010030NetconfAccess::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 +010031 : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey))
32 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010033{
Václav Kubernátc31bd602019-03-07 11:44:48 +010034}
35
Jan Kundrátdab815e2020-01-22 19:44:11 +010036NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
37 : m_session(std::move(session))
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010038 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Jan Kundrátdab815e2020-01-22 19:44:11 +010039{
Jan Kundrátdab815e2020-01-22 19:44:11 +010040}
41
Václav Kubernátc31bd602019-03-07 11:44:48 +010042NetconfAccess::NetconfAccess(const std::string& socketPath)
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010043 : m_session(libnetconf::client::Session::connectSocket(socketPath))
44 , m_schema(std::make_shared<YangSchema>(m_session->libyangContext()))
Václav Kubernátc31bd602019-03-07 11:44:48 +010045{
Václav Kubernátc31bd602019-03-07 11:44:48 +010046}
47
48void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
49{
Jan Kundrát379bb572020-05-07 03:23:13 +020050 auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value));
51 auto node = m_schema->dataNodeFromPath(path, lyValue);
Václav Kubernátc31bd602019-03-07 11:44:48 +010052 doEditFromDataNode(node);
53}
54
Jan Kundrátcbf288b2020-06-18 20:44:39 +020055void NetconfAccess::createItem(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010056{
57 auto node = m_schema->dataNodeFromPath(path);
58 doEditFromDataNode(node);
59}
60
Jan Kundrátcbf288b2020-06-18 20:44:39 +020061void NetconfAccess::deleteItem(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010062{
63 auto node = m_schema->dataNodeFromPath(path);
Václav Kubernátda8e4b92020-02-04 11:56:30 +010064 auto container = *(node->find_path(path.c_str())->data().begin());
65 container->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
Václav Kubernátc31bd602019-03-07 11:44:48 +010066 doEditFromDataNode(node);
67}
68
Václav Kubernátbf65dd72020-05-28 02:32:31 +020069struct impl_toYangInsert {
70 std::string operator()(yang::move::Absolute& absolute)
71 {
72 return absolute == yang::move::Absolute::Begin ? "first" : "last";
73 }
74 std::string operator()(yang::move::Relative& relative)
75 {
76 return relative.m_position == yang::move::Relative::Position::After ? "after" : "before";
77 }
78};
79
80std::string toYangInsert(std::variant<yang::move::Absolute, yang::move::Relative> move)
81{
82 return std::visit(impl_toYangInsert{}, move);
83}
84
85void NetconfAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
86{
87 auto node = m_schema->dataNodeFromPath(source);
88 auto sourceNode = *(node->find_path(source.c_str())->data().begin());
89 auto yangModule = m_schema->getYangModule("yang");
90 sourceNode->insert_attr(yangModule, "insert", toYangInsert(move).c_str());
91
92 if (std::holds_alternative<yang::move::Relative>(move)) {
93 auto relative = std::get<yang::move::Relative>(move);
94 if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
95 sourceNode->insert_attr(yangModule, "value", leafDataToString(relative.m_path.at(".")).c_str());
96 } else {
Václav Kubernát2c4778b2020-06-18 11:55:20 +020097 sourceNode->insert_attr(yangModule, "key", instanceToString(relative.m_path, node->node_module()->name()).c_str());
Václav Kubernátbf65dd72020-05-28 02:32:31 +020098 }
99 }
100 doEditFromDataNode(sourceNode);
101}
102
Václav Kubernátc31bd602019-03-07 11:44:48 +0100103void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
104{
105 auto data = dataNode->print_mem(LYD_XML, 0);
106 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
107}
108
109void NetconfAccess::commitChanges()
110{
111 m_session->commit();
112}
113
114void NetconfAccess::discardChanges()
115{
116 m_session->discard();
117}
118
Václav Kubernáta8789602020-07-20 15:18:19 +0200119DatastoreAccess::Tree NetconfAccess::impl_execute(const std::string& path, const Tree& input)
Jan Kundrát6ee84792020-01-24 01:43:36 +0100120{
121 auto root = m_schema->dataNodeFromPath(path);
122 for (const auto& [k, v] : input) {
123 auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v));
124 root->merge(node, 0);
125 }
126 auto data = root->print_mem(LYD_XML, 0);
127
128 Tree res;
Václav Kubernáta8789602020-07-20 15:18:19 +0200129 auto output = m_session->rpc_or_action(data);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100130 if (output) {
Václav Kubernátdaf40312020-06-19 11:34:55 +0200131 lyNodesToTree(res, output->tree_for(), joinPaths(path, "/"));
Jan Kundrát6ee84792020-01-24 01:43:36 +0100132 }
133 return res;
134}
135
Václav Kubernáta8789602020-07-20 15:18:19 +0200136DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input)
137{
138 return impl_execute(path, input);
139}
140
141DatastoreAccess::Tree NetconfAccess::executeAction(const std::string& path, const Tree& input)
142{
143 return impl_execute(path, input);
144}
145
Václav Kubernát7160a132020-04-03 02:11:01 +0200146NC_DATASTORE toNcDatastore(Datastore datastore)
147{
148 switch (datastore) {
149 case Datastore::Running:
150 return NC_DATASTORE_RUNNING;
151 case Datastore::Startup:
152 return NC_DATASTORE_STARTUP;
153 }
154 __builtin_unreachable();
155}
156
157void NetconfAccess::copyConfig(const Datastore source, const Datastore destination)
158{
159 m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination));
160}
161
Václav Kubernátb52dc252019-12-04 13:03:39 +0100162std::string NetconfAccess::fetchSchema(const std::string_view module, const
163 std::optional<std::string_view> revision, const
164 std::optional<std::string_view> submodule, const
165 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100166{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100167 if (submodule) {
168 return m_session->getSchema(*submodule, submoduleRevision);
169 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100170 return m_session->getSchema(module, revision);
171}
172
173std::vector<std::string> NetconfAccess::listImplementedSchemas()
174{
175 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
176 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
177
178 std::vector<std::string> res;
179 for (auto it : set->data()) {
180 if (it->schema()->nodetype() == LYS_LEAF) {
181 libyang::Data_Node_Leaf_List leaf(it);
Václav Kubernátfaacd022020-07-08 16:44:38 +0200182 res.emplace_back(leaf.value_str());
Václav Kubernátc31bd602019-03-07 11:44:48 +0100183 }
184 }
185 return res;
186}
187
188std::shared_ptr<Schema> NetconfAccess::schema()
189{
190 return m_schema;
191}
Václav Kubernátab612e92019-11-26 19:51:31 +0100192
193std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path)
194{
195 std::vector<ListInstance> res;
196 auto list = m_schema->dataNodeFromPath(path);
197
198 // This inserts selection nodes - I only want keys not other data
199 // 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)
200 auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys();
201 for (const auto& keyLeaf : keys) {
202 // Have to call find_path here - otherwise I'll have the list, not the leaf inside it
203 auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin());
204 auto actualList = *(list->find_path(path.c_str())->data().begin());
205 actualList->insert(selectionLeaf);
206 }
207
Jan Kundrátbb525b42020-02-04 11:56:59 +0100208 auto instances = m_session->get(list->print_mem(LYD_XML, 0));
Václav Kubernátab612e92019-11-26 19:51:31 +0100209
Václav Kubernát45e55462020-02-04 11:19:32 +0100210 if (!instances) {
211 return res;
212 }
Václav Kubernátab612e92019-11-26 19:51:31 +0100213
214 for (const auto& instance : instances->find_path(path.c_str())->data()) {
215 ListInstance instanceRes;
216
217 // I take the first child here, because the first element (the parent of the child()) will be the list
218 for (const auto& keyLeaf : instance->child()->tree_for()) {
219 auto leafData = libyang::Data_Node_Leaf_List{keyLeaf};
220 auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()};
221 instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())});
222 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200223 res.emplace_back(instanceRes);
Václav Kubernátab612e92019-11-26 19:51:31 +0100224 }
225
226 return res;
227}
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200228
229std::string NetconfAccess::dump(const DataFormat format) const
230{
231 auto config = m_session->get();
232 if (!config) {
233 return "";
234 }
235 return config->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT);
236}