blob: 1e28bd2365bbac36b241dcfb4bef15c57b1c4f54 [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;
26 if (it->schema()->nodetype() == LYS_LIST) {
27 res.emplace(it->path(), special_{SpecialValue::List});
28 }
29 if (it->schema()->nodetype() == LYS_LEAF) {
30 libyang::Data_Node_Leaf_List leaf(it);
31 res.emplace(leaf.path(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
32 }
33 }
34}
35}
36
37
Václav Kubernátc31bd602019-03-07 11:44:48 +010038NetconfAccess::~NetconfAccess() = default;
39
Jan Kundrátb331b552020-01-23 15:25:29 +010040DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path)
Václav Kubernátc31bd602019-03-07 11:44:48 +010041{
Jan Kundrátb331b552020-01-23 15:25:29 +010042 Tree res;
Václav Kubernát9456b5c2019-10-02 21:14:52 +020043 auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt);
44
45 if (config) {
46 for (auto it : config->tree_for()) {
Jan Kundrát8f63fb72020-01-24 01:40:01 +010047 fillMap(res, it->tree_dfs());
Václav Kubernátc31bd602019-03-07 11:44:48 +010048 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010049 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010050 return res;
51}
52
53void NetconfAccess::datastoreInit()
54{
Václav Kubernátb52dc252019-12-04 13:03:39 +010055 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 +010056 return fetchSchema(moduleName,
57 revision ? std::optional{revision} : std::nullopt,
Václav Kubernátb52dc252019-12-04 13:03:39 +010058 submodule ? std::optional{submodule} : std::nullopt,
59 submoduleRevision ? std::optional{submoduleRevision} : std::nullopt);
Václav Kubernátc31bd602019-03-07 11:44:48 +010060 });
61
62 for (const auto& it : listImplementedSchemas()) {
63 m_schema->loadModule(it);
64 }
65}
66
67NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
68 : m_schema(new YangSchema())
69{
70 m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey);
71 datastoreInit();
72}
73
Jan Kundrátdab815e2020-01-22 19:44:11 +010074NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
75 : m_session(std::move(session))
76 , m_schema(new YangSchema())
77{
78 datastoreInit();
79}
80
Václav Kubernátc31bd602019-03-07 11:44:48 +010081NetconfAccess::NetconfAccess(const std::string& socketPath)
82 : m_schema(new YangSchema())
83{
84 m_session = libnetconf::client::Session::connectSocket(socketPath);
85 datastoreInit();
86}
87
88void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
89{
90 auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
91 doEditFromDataNode(node);
92}
93
94void NetconfAccess::createPresenceContainer(const std::string& path)
95{
96 auto node = m_schema->dataNodeFromPath(path);
97 doEditFromDataNode(node);
98}
99
100void NetconfAccess::deletePresenceContainer(const std::string& path)
101{
102 auto node = m_schema->dataNodeFromPath(path);
103 node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
104 doEditFromDataNode(node);
105}
106
107void NetconfAccess::createListInstance(const std::string& path)
108{
109 auto node = m_schema->dataNodeFromPath(path);
110 doEditFromDataNode(node);
111}
112
113void NetconfAccess::deleteListInstance(const std::string& path)
114{
115 auto node = m_schema->dataNodeFromPath(path);
116 node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
117 doEditFromDataNode(node);
118}
119
120void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
121{
122 auto data = dataNode->print_mem(LYD_XML, 0);
123 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
124}
125
126void NetconfAccess::commitChanges()
127{
128 m_session->commit();
129}
130
131void NetconfAccess::discardChanges()
132{
133 m_session->discard();
134}
135
Václav Kubernátb52dc252019-12-04 13:03:39 +0100136std::string NetconfAccess::fetchSchema(const std::string_view module, const
137 std::optional<std::string_view> revision, const
138 std::optional<std::string_view> submodule, const
139 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100140{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100141 if (submodule) {
142 return m_session->getSchema(*submodule, submoduleRevision);
143 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100144 return m_session->getSchema(module, revision);
145}
146
147std::vector<std::string> NetconfAccess::listImplementedSchemas()
148{
149 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
150 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
151
152 std::vector<std::string> res;
153 for (auto it : set->data()) {
154 if (it->schema()->nodetype() == LYS_LEAF) {
155 libyang::Data_Node_Leaf_List leaf(it);
156 res.push_back(leaf.value_str());
157 }
158 }
159 return res;
160}
161
162std::shared_ptr<Schema> NetconfAccess::schema()
163{
164 return m_schema;
165}