blob: c1bd5dcf229371e18e803642385ddf593b76c5b1 [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>
10#include "netconf-client.h"
11#include "netconf_access.hpp"
12#include "utils.hpp"
13#include "yang_schema.hpp"
14
15leaf_data_ leafValueFromValue(const libyang::S_Value& value, LY_DATA_TYPE type)
16{
17 using namespace std::string_literals;
18 switch (type) {
19 case LY_TYPE_INT8:
20 return value->int8();
21 case LY_TYPE_INT16:
22 return value->int16();
23 case LY_TYPE_INT32:
24 return value->int32();
25 case LY_TYPE_INT64:
26 return value->int64();
27 case LY_TYPE_UINT8:
28 return value->uint8();
29 case LY_TYPE_UINT16:
30 return value->uint16();
31 case LY_TYPE_UINT32:
32 return value->uintu32();
33 case LY_TYPE_UINT64:
34 return value->uint64();
35 case LY_TYPE_BOOL:
Václav Kubernát8e121ff2019-10-15 15:47:45 +020036 return bool(value->bln());
Václav Kubernátc31bd602019-03-07 11:44:48 +010037 case LY_TYPE_STRING:
38 return std::string(value->string());
39 case LY_TYPE_ENUM:
40 return std::string(value->enm()->name());
41 default: // TODO: implement all types
42 return "(can't print)"s;
43 }
44}
45
46NetconfAccess::~NetconfAccess() = default;
47
48std::map<std::string, leaf_data_> NetconfAccess::getItems(const std::string& path)
49{
50 using namespace std::string_literals;
51 std::map<std::string, leaf_data_> res;
52
53 // This is very similar to the fillMap lambda in SysrepoAccess, however,
54 // Sysrepo returns a weird array-like structure, while libnetconf
55 // returns libyang::Data_Node
56 auto fillMap = [&res](auto items) {
57 for (const auto& it : items) {
58 if (!it)
59 continue;
60 if (it->schema()->nodetype() == LYS_LEAF) {
61 libyang::Data_Node_Leaf_List leaf(it);
62 res.emplace(leaf.path(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
63 }
64 }
65 };
66
Václav Kubernát9456b5c2019-10-02 21:14:52 +020067 auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt);
68
69 if (config) {
70 for (auto it : config->tree_for()) {
Václav Kubernátc31bd602019-03-07 11:44:48 +010071 fillMap(it->tree_dfs());
72 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010073 }
Václav Kubernátc31bd602019-03-07 11:44:48 +010074 return res;
75}
76
77void NetconfAccess::datastoreInit()
78{
79 m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule) {
80 return fetchSchema(moduleName,
81 revision ? std::optional{revision} : std::nullopt,
82 submodule ? std::optional{submodule} : std::nullopt);
83 });
84
85 for (const auto& it : listImplementedSchemas()) {
86 m_schema->loadModule(it);
87 }
88}
89
90NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey)
91 : m_schema(new YangSchema())
92{
93 m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey);
94 datastoreInit();
95}
96
97NetconfAccess::NetconfAccess(const std::string& socketPath)
98 : m_schema(new YangSchema())
99{
100 m_session = libnetconf::client::Session::connectSocket(socketPath);
101 datastoreInit();
102}
103
104void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value)
105{
106 auto node = m_schema->dataNodeFromPath(path, leafDataToString(value));
107 doEditFromDataNode(node);
108}
109
110void NetconfAccess::createPresenceContainer(const std::string& path)
111{
112 auto node = m_schema->dataNodeFromPath(path);
113 doEditFromDataNode(node);
114}
115
116void NetconfAccess::deletePresenceContainer(const std::string& path)
117{
118 auto node = m_schema->dataNodeFromPath(path);
119 node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
120 doEditFromDataNode(node);
121}
122
123void NetconfAccess::createListInstance(const std::string& path)
124{
125 auto node = m_schema->dataNodeFromPath(path);
126 doEditFromDataNode(node);
127}
128
129void NetconfAccess::deleteListInstance(const std::string& path)
130{
131 auto node = m_schema->dataNodeFromPath(path);
132 node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete");
133 doEditFromDataNode(node);
134}
135
136void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode)
137{
138 auto data = dataNode->print_mem(LYD_XML, 0);
139 m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data);
140}
141
142void NetconfAccess::commitChanges()
143{
144 m_session->commit();
145}
146
147void NetconfAccess::discardChanges()
148{
149 m_session->discard();
150}
151
152std::string NetconfAccess::fetchSchema(const std::string_view module, const std::optional<std::string_view> revision, const std::optional<std::string_view>)
153{
154 return m_session->getSchema(module, revision);
155}
156
157std::vector<std::string> NetconfAccess::listImplementedSchemas()
158{
159 auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas");
160 auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier");
161
162 std::vector<std::string> res;
163 for (auto it : set->data()) {
164 if (it->schema()->nodetype() == LYS_LEAF) {
165 libyang::Data_Node_Leaf_List leaf(it);
166 res.push_back(leaf.value_str());
167 }
168 }
169 return res;
170}
171
172std::shared_ptr<Schema> NetconfAccess::schema()
173{
174 return m_schema;
175}