blob: fc35be637b97826a079db2a953060cf9adc7ad95 [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átc31bd602019-03-07 11:44:48 +010011#include "netconf-client.h"
12#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
21void fillMap(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items)
22{
23 for (const auto& it : items) {
24 if (!it)
25 continue;
Václav Kubernát69aabe92020-01-24 16:53:12 +010026 if (it->schema()->nodetype() == LYS_CONTAINER) {
27 if (libyang::Schema_Node_Container{it->schema()}.presence()) {
28 // The fact that the container is included in the data tree
29 // means that it is present and I don't need to check any
30 // value.
31 res.emplace(it->path(), special_{SpecialValue::PresenceContainer});
32 }
33 }
Jan Kundrát8f63fb72020-01-24 01:40:01 +010034 if (it->schema()->nodetype() == LYS_LIST) {
35 res.emplace(it->path(), special_{SpecialValue::List});
36 }
37 if (it->schema()->nodetype() == LYS_LEAF) {
38 libyang::Data_Node_Leaf_List leaf(it);
39 res.emplace(leaf.path(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
40 }
41 }
42}
43}
44
45
Václav Kubernátc31bd602019-03-07 11:44:48 +010046NetconfAccess::~NetconfAccess() = default;
47
Jan Kundrátb331b552020-01-23 15:25:29 +010048DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010049{
Jan Kundrátb331b552020-01-23 15:25:29 +010050 Tree res;
Václav Kubernát9456b5c2019-10-02 21:14:52 +020051 auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt);
52
53 if (config) {
54 for (auto it : config->tree_for()) {
Jan Kundrát8f63fb72020-01-24 01:40:01 +010055 fillMap(res, it->tree_dfs());
Václav Kubernátc31bd602019-03-07 11:44:48 +010056 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010057 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010058 return res;
59}
60
61void NetconfAccess::datastoreInit()
62{
Václav Kubernátb52dc252019-12-04 13:03:39 +010063 m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule, const char* submoduleRevision) {
Václav Kubernátc31bd602019-03-07 11:44:48 +010064 return fetchSchema(moduleName,
65 revision ? std::optional{revision} : std::nullopt,
Václav Kubernátb52dc252019-12-04 13:03:39 +010066 submodule ? std::optional{submodule} : std::nullopt,
67 submoduleRevision ? std::optional{submoduleRevision} : std::nullopt);
Václav Kubernátc31bd602019-03-07 11:44:48 +010068 });
69
70 for (const auto& it : listImplementedSchemas()) {
71 m_schema->loadModule(it);
72 }
73}
74
75NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
76 : m_schema(new YangSchema())
77{
78 m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey);
79 datastoreInit();
80}
81
Jan Kundrátdab815e2020-01-22 19:44:11 +010082NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
83 : m_session(std::move(session))
84 , m_schema(new YangSchema())
85{
86 datastoreInit();
87}
88
Václav Kubernátc31bd602019-03-07 11:44:48 +010089NetconfAccess::NetconfAccess(const std::string& socketPath)
90 : m_schema(new YangSchema())
91{
92 m_session = libnetconf::client::Session::connectSocket(socketPath);
93 datastoreInit();
94}
95
96void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
97{
98 auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
99 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);
111 node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
112 doEditFromDataNode(node);
113}
114
115void NetconfAccess::createListInstance(const std::string& path)
116{
117 auto node = m_schema->dataNodeFromPath(path);
118 doEditFromDataNode(node);
119}
120
121void NetconfAccess::deleteListInstance(const std::string& path)
122{
123 auto node = m_schema->dataNodeFromPath(path);
124 node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
125 doEditFromDataNode(node);
126}
127
128void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
129{
130 auto data = dataNode->print_mem(LYD_XML, 0);
131 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
132}
133
134void NetconfAccess::commitChanges()
135{
136 m_session->commit();
137}
138
139void NetconfAccess::discardChanges()
140{
141 m_session->discard();
142}
143
Václav Kubernátb52dc252019-12-04 13:03:39 +0100144std::string NetconfAccess::fetchSchema(const std::string_view module, const
145 std::optional<std::string_view> revision, const
146 std::optional<std::string_view> submodule, const
147 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100148{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100149 if (submodule) {
150 return m_session->getSchema(*submodule, submoduleRevision);
151 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100152 return m_session->getSchema(module, revision);
153}
154
155std::vector<std::string> NetconfAccess::listImplementedSchemas()
156{
157 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
158 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
159
160 std::vector<std::string> res;
161 for (auto it : set->data()) {
162 if (it->schema()->nodetype() == LYS_LEAF) {
163 libyang::Data_Node_Leaf_List leaf(it);
164 res.push_back(leaf.value_str());
165 }
166 }
167 return res;
168}
169
170std::shared_ptr<Schema> NetconfAccess::schema()
171{
172 return m_schema;
173}