blob: 5b1ad7805445d25fc7396fe0a05e5f1c30b7a101 [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
Václav Kubernátc31bd602019-03-07 11:44:48 +010016NetconfAccess::~NetconfAccess() = default;
17
18std::map<std::string, leaf_data_> NetconfAccess::getItems(const std::string& path)
19{
20 using namespace std::string_literals;
21 std::map<std::string, leaf_data_> res;
22
23 // This is very similar to the fillMap lambda in SysrepoAccess, however,
24 // Sysrepo returns a weird array-like structure, while libnetconf
25 // returns libyang::Data_Node
26 auto fillMap = [&res](auto items) {
27 for (const auto& it : items) {
28 if (!it)
29 continue;
Václav Kubernátd812cfb2020-01-07 17:30:20 +010030 if (it->schema()->nodetype() == LYS_LIST) {
Václav Kubernát144729d2020-01-08 15:20:35 +010031 res.emplace(it->path(), special_{SpecialValue::List});
Václav Kubernátd812cfb2020-01-07 17:30:20 +010032 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010033 if (it->schema()->nodetype() == LYS_LEAF) {
34 libyang::Data_Node_Leaf_List leaf(it);
35 res.emplace(leaf.path(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
36 }
37 }
38 };
39
Václav Kubernát9456b5c2019-10-02 21:14:52 +020040 auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt);
41
42 if (config) {
43 for (auto it : config->tree_for()) {
Václav Kubernátc31bd602019-03-07 11:44:48 +010044 fillMap(it->tree_dfs());
45 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010046 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010047 return res;
48}
49
50void NetconfAccess::datastoreInit()
51{
Václav Kubernátb52dc252019-12-04 13:03:39 +010052 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 +010053 return fetchSchema(moduleName,
54 revision ? std::optional{revision} : std::nullopt,
Václav Kubernátb52dc252019-12-04 13:03:39 +010055 submodule ? std::optional{submodule} : std::nullopt,
56 submoduleRevision ? std::optional{submoduleRevision} : std::nullopt);
Václav Kubernátc31bd602019-03-07 11:44:48 +010057 });
58
59 for (const auto& it : listImplementedSchemas()) {
60 m_schema->loadModule(it);
61 }
62}
63
64NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
65 : m_schema(new YangSchema())
66{
67 m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey);
68 datastoreInit();
69}
70
Jan Kundrátdab815e2020-01-22 19:44:11 +010071NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session)
72 : m_session(std::move(session))
73 , m_schema(new YangSchema())
74{
75 datastoreInit();
76}
77
Václav Kubernátc31bd602019-03-07 11:44:48 +010078NetconfAccess::NetconfAccess(const std::string& socketPath)
79 : m_schema(new YangSchema())
80{
81 m_session = libnetconf::client::Session::connectSocket(socketPath);
82 datastoreInit();
83}
84
85void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
86{
87 auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
88 doEditFromDataNode(node);
89}
90
91void NetconfAccess::createPresenceContainer(const std::string& path)
92{
93 auto node = m_schema->dataNodeFromPath(path);
94 doEditFromDataNode(node);
95}
96
97void NetconfAccess::deletePresenceContainer(const std::string& path)
98{
99 auto node = m_schema->dataNodeFromPath(path);
100 node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
101 doEditFromDataNode(node);
102}
103
104void NetconfAccess::createListInstance(const std::string& path)
105{
106 auto node = m_schema->dataNodeFromPath(path);
107 doEditFromDataNode(node);
108}
109
110void NetconfAccess::deleteListInstance(const std::string& path)
111{
112 auto node = m_schema->dataNodeFromPath(path);
113 node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
114 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
Václav Kubernátb52dc252019-12-04 13:03:39 +0100133std::string NetconfAccess::fetchSchema(const std::string_view module, const
134 std::optional<std::string_view> revision, const
135 std::optional<std::string_view> submodule, const
136 std::optional<std::string_view> submoduleRevision)
Václav Kubernátc31bd602019-03-07 11:44:48 +0100137{
Václav Kubernátb52dc252019-12-04 13:03:39 +0100138 if (submodule) {
139 return m_session->getSchema(*submodule, submoduleRevision);
140 }
Václav Kubernátc31bd602019-03-07 11:44:48 +0100141 return m_session->getSchema(module, revision);
142}
143
144std::vector<std::string> NetconfAccess::listImplementedSchemas()
145{
146 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
147 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
148
149 std::vector<std::string> res;
150 for (auto it : set->data()) {
151 if (it->schema()->nodetype() == LYS_LEAF) {
152 libyang::Data_Node_Leaf_List leaf(it);
153 res.push_back(leaf.value_str());
154 }
155 }
156 return res;
157}
158
159std::shared_ptr<Schema> NetconfAccess::schema()
160{
161 return m_schema;
162}