blob: b6818935c5ab0f3121efa9aa735232f138dffcc8 [file] [log] [blame]
Václav Kubernát80aacc02018-08-22 17:41:54 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
Václav Kubernátab612e92019-11-26 19:51:31 +01009#include <libyang/Tree_Data.hpp>
10#include <libyang/Tree_Schema.hpp>
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020011#include <sysrepo-cpp/Session.hpp>
Václav Kubernátab612e92019-11-26 19:51:31 +010012#include "libyang_utils.hpp"
Václav Kubernát80aacc02018-08-22 17:41:54 +020013#include "sysrepo_access.hpp"
Jan Kundrát6ee84792020-01-24 01:43:36 +010014#include "utils.hpp"
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020015#include "yang_schema.hpp"
Václav Kubernát80aacc02018-08-22 17:41:54 +020016
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020017leaf_data_ leafValueFromVal(const sysrepo::S_Val& value)
Václav Kubernátc89736b2018-08-30 16:14:05 +020018{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020019 using namespace std::string_literals;
Václav Kubernátc89736b2018-08-30 16:14:05 +020020 switch (value->type()) {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020021 case SR_INT8_T:
22 return value->data()->get_int8();
23 case SR_UINT8_T:
24 return value->data()->get_uint8();
25 case SR_INT16_T:
26 return value->data()->get_int16();
27 case SR_UINT16_T:
28 return value->data()->get_uint16();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020029 case SR_INT32_T:
30 return value->data()->get_int32();
31 case SR_UINT32_T:
32 return value->data()->get_uint32();
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020033 case SR_INT64_T:
34 return value->data()->get_int64();
35 case SR_UINT64_T:
36 return value->data()->get_uint64();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020037 case SR_BOOL_T:
38 return value->data()->get_bool();
39 case SR_STRING_T:
40 return std::string(value->data()->get_string());
41 case SR_ENUM_T:
Václav Kubernát152ce222019-12-19 12:23:32 +010042 return enum_{std::string(value->data()->get_enum())};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020043 case SR_DECIMAL64_T:
44 return value->data()->get_decimal64();
45 case SR_CONTAINER_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010046 return special_{SpecialValue::Container};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020047 case SR_CONTAINER_PRESENCE_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010048 return special_{SpecialValue::PresenceContainer};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020049 case SR_LIST_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010050 return special_{SpecialValue::List};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020051 default: // TODO: implement all types
52 return value->val_to_string();
Václav Kubernátc89736b2018-08-30 16:14:05 +020053 }
54}
Václav Kubernát80aacc02018-08-22 17:41:54 +020055
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020056struct valFromValue : boost::static_visitor<sysrepo::S_Val> {
57 sysrepo::S_Val operator()(const enum_& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020058 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020059 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T);
Václav Kubernát80aacc02018-08-22 17:41:54 +020060 }
61
Václav Kubernátab538992019-03-06 15:30:50 +010062 sysrepo::S_Val operator()(const binary_& value) const
63 {
64 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T);
65 }
66
Václav Kubernáteeb38842019-03-20 19:46:05 +010067 sysrepo::S_Val operator()(const identityRef_& value) const
68 {
69 auto res = value.m_prefix.value().m_name + ":" + value.m_value;
70 return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T);
71 }
72
Václav Kubernát144729d2020-01-08 15:20:35 +010073 sysrepo::S_Val operator()(const special_& value) const
74 {
75 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
76 }
77
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020078 sysrepo::S_Val operator()(const std::string& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020079 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020080 return std::make_shared<sysrepo::Val>(value.c_str());
Václav Kubernát80aacc02018-08-22 17:41:54 +020081 }
82
Jan Kundrátbd178362019-02-05 19:00:04 +010083 template <typename T>
84 sysrepo::S_Val operator()(const T& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020085 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020086 return std::make_shared<sysrepo::Val>(value);
Václav Kubernát80aacc02018-08-22 17:41:54 +020087 }
88};
89
Jan Kundrát6ee84792020-01-24 01:43:36 +010090struct updateSrValFromValue : boost::static_visitor<void> {
91 std::string xpath;
92 sysrepo::S_Val v;
93 updateSrValFromValue(const std::string& xpath, sysrepo::S_Val v)
94 : xpath(xpath)
95 , v(v)
96 {
97 }
98
99 void operator()(const enum_& value) const
100 {
101 v->set(xpath.c_str(), value.m_value.c_str(), SR_ENUM_T);
102 }
103
104 void operator()(const binary_& value) const
105 {
106 v->set(xpath.c_str(), value.m_value.c_str(), SR_BINARY_T);
107 }
108
109 void operator()(const identityRef_& value) const
110 {
111 v->set(xpath.c_str(), (value.m_prefix.value().m_name + ":" + value.m_value).c_str(), SR_IDENTITYREF_T);
112 }
113
114 void operator()(const special_& value) const
115 {
116 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
117 }
118
119 void operator()(const std::string& value) const
120 {
121 v->set(xpath.c_str(), value.c_str(), SR_STRING_T);
122 }
123
124 template <typename T>
125 void operator()(const T value) const
126 {
127 v->set(xpath.c_str(), value);
128 }
129};
130
Václav Kubernát812ee282018-08-30 17:10:03 +0200131SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200132
133SysrepoAccess::SysrepoAccess(const std::string& appname)
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200134 : m_connection(new sysrepo::Connection(appname.c_str()))
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200135 , m_schema(new YangSchema())
Václav Kubernát80aacc02018-08-22 17:41:54 +0200136{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200137 try {
138 m_session = std::make_shared<sysrepo::Session>(m_connection);
139 } catch (sysrepo::sysrepo_exception& ex) {
140 reportErrors();
141 }
Václav Kubernátb52dc252019-12-04 13:03:39 +0100142
143 // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module.
144 // That's why submoduleRevision is ignored.
145 m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule, [[maybe_unused]] const char* submoduleRevision) {
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200146 return fetchSchema(moduleName, revision, submodule);
147 });
148
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100149 for (const auto& it : listSchemas()) {
150 if (it->implemented()) {
151 m_schema->loadModule(it->module_name());
152 for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) {
153 m_schema->enableFeature(it->module_name(), it->enabled_features(i));
154 }
155 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200156 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200157}
158
Jan Kundrátb331b552020-01-23 15:25:29 +0100159DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path)
Václav Kubernát80aacc02018-08-22 17:41:54 +0200160{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200161 using namespace std::string_literals;
Jan Kundrátb331b552020-01-23 15:25:29 +0100162 Tree res;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200163
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200164 auto fillMap = [&res](auto items) {
165 if (!items)
166 return;
167 for (unsigned int i = 0; i < items->val_cnt(); i++) {
168 res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i)));
169 }
170 };
Václav Kubernátc89736b2018-08-30 16:14:05 +0200171
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200172 try {
173 if (path == "/") {
174 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
175 auto schemas = m_session->list_schemas();
176 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
177 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
178 }
179 } else {
180 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200181 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200182 } catch (sysrepo::sysrepo_exception& ex) {
183 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200184 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200185 return res;
186}
187
188void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
189{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200190 try {
191 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
192 } catch (sysrepo::sysrepo_exception& ex) {
193 reportErrors();
194 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200195}
196
197void SysrepoAccess::createPresenceContainer(const std::string& path)
198{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200199 try {
200 m_session->set_item(path.c_str());
201 } catch (sysrepo::sysrepo_exception& ex) {
202 reportErrors();
203 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200204}
205
206void SysrepoAccess::deletePresenceContainer(const std::string& path)
207{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200208 try {
209 m_session->delete_item(path.c_str());
210 } catch (sysrepo::sysrepo_exception& ex) {
211 reportErrors();
212 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200213}
Václav Kubernát812ee282018-08-30 17:10:03 +0200214
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100215void SysrepoAccess::createListInstance(const std::string& path)
216{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200217 try {
218 m_session->set_item(path.c_str());
219 } catch (sysrepo::sysrepo_exception& ex) {
220 reportErrors();
221 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100222}
223
224void SysrepoAccess::deleteListInstance(const std::string& path)
225{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200226 try {
227 m_session->delete_item(path.c_str());
228 } catch (sysrepo::sysrepo_exception& ex) {
229 reportErrors();
230 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100231}
232
Václav Kubernát812ee282018-08-30 17:10:03 +0200233void SysrepoAccess::commitChanges()
234{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200235 try {
236 m_session->commit();
237 } catch (sysrepo::sysrepo_exception& ex) {
238 reportErrors();
239 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200240}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200241
Václav Kubernát6d791432018-10-25 16:00:35 +0200242void SysrepoAccess::discardChanges()
243{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200244 try {
245 m_session->discard_changes();
246 } catch (sysrepo::sysrepo_exception& ex) {
247 reportErrors();
248 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200249}
250
Jan Kundrát6ee84792020-01-24 01:43:36 +0100251DatastoreAccess::Tree SysrepoAccess::executeRpc(const std::string &path, const Tree &input)
252{
253 auto srInput = std::make_shared<sysrepo::Vals>(input.size());
254 {
255 size_t i = 0;
256 for (const auto& [k, v] : input) {
257 boost::apply_visitor(updateSrValFromValue(joinPaths(path, k), srInput->val(i)), v);
258 ++i;
259 }
260 }
261 auto output = m_session->rpc_send(path.c_str(), srInput);
262 Tree res;
263 for (size_t i = 0; i < output->val_cnt(); ++i) {
264 const auto& v = output->val(i);
265 res.emplace(std::string(v->xpath()).substr(joinPaths(path, "/").size()), leafValueFromVal(v));
266 }
267 return res;
268}
269
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200270std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule)
271{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200272 std::string schema;
273 try {
Václav Kubernátb52dc252019-12-04 13:03:39 +0100274 schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200275 } catch (sysrepo::sysrepo_exception& ex) {
276 reportErrors();
277 }
278
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200279 if (schema.empty())
280 throw std::runtime_error(std::string("Module ") + module + " not available");
281
282 return schema;
283}
284
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100285std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200286{
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100287 std::vector<sysrepo::S_Yang_Schema> res;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200288 std::shared_ptr<sysrepo::Yang_Schemas> schemas;
289 try {
290 schemas = m_session->list_schemas();
291 } catch (sysrepo::sysrepo_exception& ex) {
292 reportErrors();
293 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200294 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
295 auto schema = schemas->schema(i);
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100296 res.push_back(schema);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200297 }
298 return res;
299}
300
301std::shared_ptr<Schema> SysrepoAccess::schema()
302{
303 return m_schema;
304}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200305
306[[noreturn]] void SysrepoAccess::reportErrors()
307{
308 // I only use get_last_errors to get error info, since the error code from
309 // sysrepo_exception doesn't really give any meaningful information. For
310 // example an "invalid argument" error could mean a node isn't enabled, or
311 // it could mean something totally different and there is no documentation
312 // for that, so it's better to just use the message sysrepo gives me.
313 auto srErrors = m_session->get_last_errors();
314 std::vector<DatastoreError> res;
315
316 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
317 auto error = srErrors->error(i);
318 res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt);
319 }
320
321 throw DatastoreException(res);
322}
Václav Kubernátab612e92019-11-26 19:51:31 +0100323
324std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path)
325{
326 std::vector<ListInstance> res;
327 auto lists = getItems(path);
328
329 decltype(lists) instances;
330 auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin());
331 std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck=wantedTree->schema()->path()](const auto& item) {
332 // This filters out non-instances.
333 if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) {
334 return false;
335 }
336
337 // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance...
338 auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin());
339 // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested.
340 return instanceTree->schema()->path() == pathToCheck;
341 });
342
343 // If there are no instances, then just return
344 if (instances.empty()) {
345 return res;
346 }
347
348 // I need to find out which keys does the list have. To do that, I create a
349 // tree from the first instance. This is gives me some top level node,
350 // which will be our list in case out list is a top-level node. In case it
351 // isn't, we have call find_path on the top level node. After that, I just
352 // retrieve the keys.
353 auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first);
354 auto list = *(topLevelTree->find_path(path.c_str())->data().begin());
355 auto keys = libyang::Schema_Node_List{list->schema()}.keys();
356
357 // Creating a full tree at the same time from the values sysrepo gives me
358 // would be a pain (and after sysrepo switches to libyang meaningless), so
359 // I just use this algorithm to create data nodes one by one and get the
360 // key values from them.
361 for (const auto& instance : instances) {
362 auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin());
363 ListInstance instanceRes;
364 for (const auto& key : keys) {
365 auto vec = wantedList->find_path(key->name())->data();
366 auto leaf = libyang::Data_Node_Leaf_List{*(vec.begin())};
367 instanceRes.emplace(key->name(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
368 }
369 res.push_back(instanceRes);
370 }
371
372 return res;
373}