blob: 205db3a0e949a9f5fb0ab0599bc8d7b1df7ca7d2 [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át19097f32020-10-05 10:08:29 +02009#include <experimental/iterator>
Václav Kubernátab612e92019-11-26 19:51:31 +010010#include <libyang/Tree_Data.hpp>
11#include <libyang/Tree_Schema.hpp>
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020012#include <sysrepo-cpp/Session.hpp>
Václav Kubernát19097f32020-10-05 10:08:29 +020013#include <sstream>
Václav Kubernátab612e92019-11-26 19:51:31 +010014#include "libyang_utils.hpp"
Václav Kubernát80aacc02018-08-22 17:41:54 +020015#include "sysrepo_access.hpp"
Jan Kundrát6ee84792020-01-24 01:43:36 +010016#include "utils.hpp"
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020017#include "yang_schema.hpp"
Václav Kubernát80aacc02018-08-22 17:41:54 +020018
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020019leaf_data_ leafValueFromVal(const sysrepo::S_Val& value)
Václav Kubernátc89736b2018-08-30 16:14:05 +020020{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020021 using namespace std::string_literals;
Václav Kubernátc89736b2018-08-30 16:14:05 +020022 switch (value->type()) {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020023 case SR_INT8_T:
24 return value->data()->get_int8();
25 case SR_UINT8_T:
26 return value->data()->get_uint8();
27 case SR_INT16_T:
28 return value->data()->get_int16();
29 case SR_UINT16_T:
30 return value->data()->get_uint16();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020031 case SR_INT32_T:
32 return value->data()->get_int32();
33 case SR_UINT32_T:
34 return value->data()->get_uint32();
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020035 case SR_INT64_T:
36 return value->data()->get_int64();
37 case SR_UINT64_T:
38 return value->data()->get_uint64();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020039 case SR_BOOL_T:
40 return value->data()->get_bool();
41 case SR_STRING_T:
42 return std::string(value->data()->get_string());
43 case SR_ENUM_T:
Václav Kubernát152ce222019-12-19 12:23:32 +010044 return enum_{std::string(value->data()->get_enum())};
Jan Kundrát0d8abd12020-05-07 02:00:14 +020045 case SR_IDENTITYREF_T:
46 {
47 auto pair = splitModuleNode(value->data()->get_identityref());
48 return identityRef_{*pair.first, pair.second};
49 }
Jan Kundrát68985442020-05-07 02:15:34 +020050 case SR_BINARY_T:
51 return binary_{value->data()->get_binary()};
Jan Kundrát379bb572020-05-07 03:23:13 +020052 case SR_LEAF_EMPTY_T:
53 return empty_{};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020054 case SR_DECIMAL64_T:
55 return value->data()->get_decimal64();
56 case SR_CONTAINER_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010057 return special_{SpecialValue::Container};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020058 case SR_CONTAINER_PRESENCE_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010059 return special_{SpecialValue::PresenceContainer};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020060 case SR_LIST_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010061 return special_{SpecialValue::List};
Václav Kubernát19097f32020-10-05 10:08:29 +020062 case SR_BITS_T:
63 {
64 bits_ res;
65 std::istringstream ss(value->data()->get_bits());
66 while (!ss.eof()) {
67 std::string bit;
68 ss >> bit;
69 res.m_bits.insert(bit);
70 }
71 return res;
72
73 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020074 default: // TODO: implement all types
75 return value->val_to_string();
Václav Kubernátc89736b2018-08-30 16:14:05 +020076 }
77}
Václav Kubernát80aacc02018-08-22 17:41:54 +020078
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020079struct valFromValue : boost::static_visitor<sysrepo::S_Val> {
80 sysrepo::S_Val operator()(const enum_& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020081 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020082 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T);
Václav Kubernát80aacc02018-08-22 17:41:54 +020083 }
84
Václav Kubernátab538992019-03-06 15:30:50 +010085 sysrepo::S_Val operator()(const binary_& value) const
86 {
87 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T);
88 }
89
Jan Kundrát379bb572020-05-07 03:23:13 +020090 sysrepo::S_Val operator()(const empty_) const
91 {
92 return std::make_shared<sysrepo::Val>(nullptr, SR_LEAF_EMPTY_T);
93 }
94
Václav Kubernáteeb38842019-03-20 19:46:05 +010095 sysrepo::S_Val operator()(const identityRef_& value) const
96 {
Jan Kundrát0d8abd12020-05-07 02:00:14 +020097 auto res = value.m_prefix ? (value.m_prefix.value().m_name + ":" + value.m_value) : value.m_value;
Václav Kubernáteeb38842019-03-20 19:46:05 +010098 return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T);
99 }
100
Václav Kubernát144729d2020-01-08 15:20:35 +0100101 sysrepo::S_Val operator()(const special_& value) const
102 {
103 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
104 }
105
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200106 sysrepo::S_Val operator()(const std::string& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +0200107 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200108 return std::make_shared<sysrepo::Val>(value.c_str());
Václav Kubernát80aacc02018-08-22 17:41:54 +0200109 }
110
Václav Kubernát19097f32020-10-05 10:08:29 +0200111 sysrepo::S_Val operator()(const bits_& value) const
112 {
113 std::stringstream ss;
114 std::copy(value.m_bits.begin(), value.m_bits.end(), std::experimental::make_ostream_joiner(ss, " "));
115 return std::make_shared<sysrepo::Val>(ss.str().c_str(), SR_BITS_T);
116 }
117
Jan Kundrátbd178362019-02-05 19:00:04 +0100118 template <typename T>
119 sysrepo::S_Val operator()(const T& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +0200120 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200121 return std::make_shared<sysrepo::Val>(value);
Václav Kubernát80aacc02018-08-22 17:41:54 +0200122 }
123};
124
Jan Kundrát6ee84792020-01-24 01:43:36 +0100125struct updateSrValFromValue : boost::static_visitor<void> {
126 std::string xpath;
127 sysrepo::S_Val v;
128 updateSrValFromValue(const std::string& xpath, sysrepo::S_Val v)
129 : xpath(xpath)
130 , v(v)
131 {
132 }
133
134 void operator()(const enum_& value) const
135 {
136 v->set(xpath.c_str(), value.m_value.c_str(), SR_ENUM_T);
137 }
138
139 void operator()(const binary_& value) const
140 {
141 v->set(xpath.c_str(), value.m_value.c_str(), SR_BINARY_T);
142 }
143
Jan Kundrát379bb572020-05-07 03:23:13 +0200144 void operator()(const empty_) const
145 {
146 v->set(xpath.c_str(), nullptr, SR_LEAF_EMPTY_T);
147 }
148
Jan Kundrát6ee84792020-01-24 01:43:36 +0100149 void operator()(const identityRef_& value) const
150 {
151 v->set(xpath.c_str(), (value.m_prefix.value().m_name + ":" + value.m_value).c_str(), SR_IDENTITYREF_T);
152 }
153
154 void operator()(const special_& value) const
155 {
Václav Kubernáte7248b22020-06-26 15:38:59 +0200156 switch (value.m_value) {
157 case SpecialValue::PresenceContainer:
158 v->set(xpath.c_str(), nullptr, SR_CONTAINER_PRESENCE_T);
159 break;
160 case SpecialValue::List:
161 v->set(xpath.c_str(), nullptr, SR_LIST_T);
162 break;
163 default:
164 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
165 }
Jan Kundrát6ee84792020-01-24 01:43:36 +0100166 }
167
Václav Kubernát19097f32020-10-05 10:08:29 +0200168 auto operator()(const bits_& value) const
169 {
170 std::stringstream ss;
171 std::copy(value.m_bits.begin(), value.m_bits.end(), std::experimental::make_ostream_joiner(ss, " "));
172 v->set(xpath.c_str(), ss.str().c_str(), SR_BITS_T);
173 }
174
Jan Kundrát6ee84792020-01-24 01:43:36 +0100175 void operator()(const std::string& value) const
176 {
177 v->set(xpath.c_str(), value.c_str(), SR_STRING_T);
178 }
179
180 template <typename T>
181 void operator()(const T value) const
182 {
183 v->set(xpath.c_str(), value);
184 }
185};
186
Václav Kubernát812ee282018-08-30 17:10:03 +0200187SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200188
Václav Kubernát715c85c2020-04-14 01:46:08 +0200189sr_datastore_t toSrDatastore(Datastore datastore)
190{
191 switch (datastore) {
192 case Datastore::Running:
193 return SR_DS_RUNNING;
194 case Datastore::Startup:
195 return SR_DS_STARTUP;
196 }
197 __builtin_unreachable();
198}
199
200SysrepoAccess::SysrepoAccess(const std::string& appname, const Datastore datastore)
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200201 : m_connection(new sysrepo::Connection(appname.c_str()))
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200202 , m_schema(new YangSchema())
Václav Kubernát80aacc02018-08-22 17:41:54 +0200203{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200204 try {
Václav Kubernát715c85c2020-04-14 01:46:08 +0200205 m_session = std::make_shared<sysrepo::Session>(m_connection, toSrDatastore(datastore));
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200206 } catch (sysrepo::sysrepo_exception& ex) {
207 reportErrors();
208 }
Václav Kubernátb52dc252019-12-04 13:03:39 +0100209
210 // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module.
211 // That's why submoduleRevision is ignored.
212 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 +0200213 return fetchSchema(moduleName, revision, submodule);
214 });
215
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100216 for (const auto& it : listSchemas()) {
217 if (it->implemented()) {
218 m_schema->loadModule(it->module_name());
219 for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) {
220 m_schema->enableFeature(it->module_name(), it->enabled_features(i));
221 }
222 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200223 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200224}
225
Václav Kubernátd6282912020-06-23 14:49:34 +0200226DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) const
Václav Kubernát80aacc02018-08-22 17:41:54 +0200227{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200228 using namespace std::string_literals;
Jan Kundrátb331b552020-01-23 15:25:29 +0100229 Tree res;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200230
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200231 auto fillMap = [this, &res](auto items) {
Václav Kubernát3a433232020-07-08 17:52:50 +0200232 if (!items) {
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200233 return;
Václav Kubernát3a433232020-07-08 17:52:50 +0200234 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200235 for (unsigned int i = 0; i < items->val_cnt(); i++) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200236 auto value = leafValueFromVal(items->val(i));
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200237 if (m_schema->nodeType(items->val(i)->xpath()) == yang::NodeTypes::LeafList) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200238 res.emplace_back(items->val(i)->xpath(), special_{SpecialValue::LeafList});
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200239 std::string leafListPath = items->val(i)->xpath();
240 while (i < items->val_cnt() && leafListPath == items->val(i)->xpath()) {
241 auto leafListValue = leafDataToString(leafValueFromVal(items->val(i)));
Václav Kubernátfaacd022020-07-08 16:44:38 +0200242 res.emplace_back(items->val(i)->xpath() + "[.="s + escapeListKeyString(leafListValue) + "]", leafListValue);
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200243 i++;
244 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200245 } else {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200246 res.emplace_back(items->val(i)->xpath(), value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200247 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200248 }
249 };
Václav Kubernátc89736b2018-08-30 16:14:05 +0200250
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200251 try {
252 if (path == "/") {
253 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
254 auto schemas = m_session->list_schemas();
255 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
256 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
257 }
258 } else {
259 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200260 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200261 } catch (sysrepo::sysrepo_exception& ex) {
262 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200263 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200264 return res;
265}
266
267void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
268{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200269 try {
270 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
271 } catch (sysrepo::sysrepo_exception& ex) {
272 reportErrors();
273 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200274}
275
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200276void SysrepoAccess::createItem(const std::string& path)
Václav Kubernát80aacc02018-08-22 17:41:54 +0200277{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200278 try {
279 m_session->set_item(path.c_str());
280 } catch (sysrepo::sysrepo_exception& ex) {
281 reportErrors();
282 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200283}
284
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200285void SysrepoAccess::deleteItem(const std::string& path)
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100286{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200287 try {
288 m_session->delete_item(path.c_str());
289 } catch (sysrepo::sysrepo_exception& ex) {
290 reportErrors();
291 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100292}
293
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200294struct impl_toSrMoveOp {
295 sr_move_position_t operator()(yang::move::Absolute& absolute)
296 {
297 return absolute == yang::move::Absolute::Begin ? SR_MOVE_FIRST : SR_MOVE_LAST;
298 }
299 sr_move_position_t operator()(yang::move::Relative& relative)
300 {
301 return relative.m_position == yang::move::Relative::Position::After ? SR_MOVE_AFTER : SR_MOVE_BEFORE;
302 }
303};
304
305sr_move_position_t toSrMoveOp(std::variant<yang::move::Absolute, yang::move::Relative> move)
306{
307 return std::visit(impl_toSrMoveOp{}, move);
308}
309
310void SysrepoAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
311{
312 std::string destPathStr;
313 if (std::holds_alternative<yang::move::Relative>(move)) {
314 auto relative = std::get<yang::move::Relative>(move);
315 if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
316 destPathStr = stripLeafListValueFromPath(source) + "[.='" + leafDataToString(relative.m_path.at(".")) + "']";
317 } else {
Václav Kubernát2c4778b2020-06-18 11:55:20 +0200318 destPathStr = stripLastListInstanceFromPath(source) + instanceToString(relative.m_path, m_schema->dataNodeFromPath(source)->node_module()->name());
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200319 }
320 }
321 m_session->move_item(source.c_str(), toSrMoveOp(move), destPathStr.c_str());
322}
323
Václav Kubernát812ee282018-08-30 17:10:03 +0200324void SysrepoAccess::commitChanges()
325{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200326 try {
327 m_session->commit();
328 } catch (sysrepo::sysrepo_exception& ex) {
329 reportErrors();
330 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200331}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200332
Václav Kubernát6d791432018-10-25 16:00:35 +0200333void SysrepoAccess::discardChanges()
334{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200335 try {
336 m_session->discard_changes();
337 } catch (sysrepo::sysrepo_exception& ex) {
338 reportErrors();
339 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200340}
341
Václav Kubernáta8789602020-07-20 15:18:19 +0200342namespace {
343std::shared_ptr<sysrepo::Vals> toSrVals(const std::string& path, const DatastoreAccess::Tree& input)
Jan Kundrát6ee84792020-01-24 01:43:36 +0100344{
Václav Kubernáta8789602020-07-20 15:18:19 +0200345 auto res = std::make_shared<sysrepo::Vals>(input.size());
Jan Kundrát6ee84792020-01-24 01:43:36 +0100346 {
347 size_t i = 0;
348 for (const auto& [k, v] : input) {
Václav Kubernáta8789602020-07-20 15:18:19 +0200349 boost::apply_visitor(updateSrValFromValue(joinPaths(path, k), res->val(i)), v);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100350 ++i;
351 }
352 }
Václav Kubernáta8789602020-07-20 15:18:19 +0200353 return res;
354}
355
356DatastoreAccess::Tree toTree(const std::string& path, const std::shared_ptr<sysrepo::Vals>& output)
357{
358 DatastoreAccess::Tree res;
Jan Kundrát6ee84792020-01-24 01:43:36 +0100359 for (size_t i = 0; i < output->val_cnt(); ++i) {
360 const auto& v = output->val(i);
Václav Kubernátfaacd022020-07-08 16:44:38 +0200361 res.emplace_back(std::string(v->xpath()).substr(joinPaths(path, "/").size()), leafValueFromVal(v));
Jan Kundrát6ee84792020-01-24 01:43:36 +0100362 }
363 return res;
364}
Václav Kubernáta8789602020-07-20 15:18:19 +0200365}
366
367DatastoreAccess::Tree SysrepoAccess::executeRpc(const std::string &path, const Tree &input)
368{
369 auto srInput = toSrVals(path, input);
370 auto output = m_session->rpc_send(path.c_str(), srInput);
371 return toTree(path, output);
372}
373
374DatastoreAccess::Tree SysrepoAccess::executeAction(const std::string& path, const Tree& input)
375{
376 auto srInput = toSrVals(path, input);
377 auto output = m_session->action_send(path.c_str(), srInput);
378 return toTree(path, output);
379}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100380
Václav Kubernát7160a132020-04-03 02:11:01 +0200381void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination)
382{
383 m_session->copy_config(nullptr, toSrDatastore(source), toSrDatastore(destination));
384 if (destination == Datastore::Running) {
385 m_session->refresh();
386 }
387}
388
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200389std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule)
390{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200391 std::string schema;
392 try {
Václav Kubernátb52dc252019-12-04 13:03:39 +0100393 schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200394 } catch (sysrepo::sysrepo_exception& ex) {
395 reportErrors();
396 }
397
Václav Kubernát3a433232020-07-08 17:52:50 +0200398 if (schema.empty()) {
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200399 throw std::runtime_error(std::string("Module ") + module + " not available");
Václav Kubernát3a433232020-07-08 17:52:50 +0200400 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200401
402 return schema;
403}
404
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100405std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200406{
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100407 std::vector<sysrepo::S_Yang_Schema> res;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200408 std::shared_ptr<sysrepo::Yang_Schemas> schemas;
409 try {
410 schemas = m_session->list_schemas();
411 } catch (sysrepo::sysrepo_exception& ex) {
412 reportErrors();
413 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200414 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
415 auto schema = schemas->schema(i);
Václav Kubernátfaacd022020-07-08 16:44:38 +0200416 res.emplace_back(schema);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200417 }
418 return res;
419}
420
421std::shared_ptr<Schema> SysrepoAccess::schema()
422{
423 return m_schema;
424}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200425
Václav Kubernátd6282912020-06-23 14:49:34 +0200426[[noreturn]] void SysrepoAccess::reportErrors() const
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200427{
428 // I only use get_last_errors to get error info, since the error code from
429 // sysrepo_exception doesn't really give any meaningful information. For
430 // example an "invalid argument" error could mean a node isn't enabled, or
431 // it could mean something totally different and there is no documentation
432 // for that, so it's better to just use the message sysrepo gives me.
433 auto srErrors = m_session->get_last_errors();
434 std::vector<DatastoreError> res;
435
436 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
437 auto error = srErrors->error(i);
438 res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt);
439 }
440
441 throw DatastoreException(res);
442}
Václav Kubernátab612e92019-11-26 19:51:31 +0100443
444std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path)
445{
446 std::vector<ListInstance> res;
447 auto lists = getItems(path);
448
449 decltype(lists) instances;
450 auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin());
451 std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck=wantedTree->schema()->path()](const auto& item) {
452 // This filters out non-instances.
453 if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) {
454 return false;
455 }
456
457 // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance...
458 auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin());
459 // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested.
460 return instanceTree->schema()->path() == pathToCheck;
461 });
462
463 // If there are no instances, then just return
464 if (instances.empty()) {
465 return res;
466 }
467
468 // I need to find out which keys does the list have. To do that, I create a
469 // tree from the first instance. This is gives me some top level node,
470 // which will be our list in case out list is a top-level node. In case it
471 // isn't, we have call find_path on the top level node. After that, I just
472 // retrieve the keys.
473 auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first);
474 auto list = *(topLevelTree->find_path(path.c_str())->data().begin());
475 auto keys = libyang::Schema_Node_List{list->schema()}.keys();
476
477 // Creating a full tree at the same time from the values sysrepo gives me
478 // would be a pain (and after sysrepo switches to libyang meaningless), so
479 // I just use this algorithm to create data nodes one by one and get the
480 // key values from them.
481 for (const auto& instance : instances) {
482 auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin());
483 ListInstance instanceRes;
484 for (const auto& key : keys) {
485 auto vec = wantedList->find_path(key->name())->data();
486 auto leaf = libyang::Data_Node_Leaf_List{*(vec.begin())};
487 instanceRes.emplace(key->name(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
488 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200489 res.emplace_back(instanceRes);
Václav Kubernátab612e92019-11-26 19:51:31 +0100490 }
491
492 return res;
493}
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200494
495std::string SysrepoAccess::dump(const DataFormat format) const
496{
497 std::shared_ptr<libyang::Data_Node> root;
498 auto input = getItems("/");
499 if (input.empty()) {
500 return "";
501 }
502 for (const auto& [k, v] : input) {
503 if (v.type() == typeid(special_) && boost::get<special_>(v).m_value != SpecialValue::PresenceContainer) {
504 continue;
505 }
506 if (!root) {
507 root = m_schema->dataNodeFromPath(k, leafDataToString(v));
508 } else {
509 // Using UPDATE here, because in multi-key list, all of the keys get created with the first key (because they are encoded in the path)
510 // and libyang complains if the node already exists.
511 root->new_path(nullptr, k.c_str(), leafDataToString(v).c_str(), LYD_ANYDATA_CONSTSTRING, LYD_PATH_OPT_UPDATE);
512 }
513 }
514 return root->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT);
515}