blob: 0fac1d8aed2b114ba6685ed384d362082178ad2c [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())};
Jan Kundrát0d8abd12020-05-07 02:00:14 +020043 case SR_IDENTITYREF_T:
44 {
45 auto pair = splitModuleNode(value->data()->get_identityref());
46 return identityRef_{*pair.first, pair.second};
47 }
Jan Kundrát68985442020-05-07 02:15:34 +020048 case SR_BINARY_T:
49 return binary_{value->data()->get_binary()};
Jan Kundrát379bb572020-05-07 03:23:13 +020050 case SR_LEAF_EMPTY_T:
51 return empty_{};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020052 case SR_DECIMAL64_T:
53 return value->data()->get_decimal64();
54 case SR_CONTAINER_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010055 return special_{SpecialValue::Container};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020056 case SR_CONTAINER_PRESENCE_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010057 return special_{SpecialValue::PresenceContainer};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020058 case SR_LIST_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010059 return special_{SpecialValue::List};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020060 default: // TODO: implement all types
61 return value->val_to_string();
Václav Kubernátc89736b2018-08-30 16:14:05 +020062 }
63}
Václav Kubernát80aacc02018-08-22 17:41:54 +020064
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020065struct valFromValue : boost::static_visitor<sysrepo::S_Val> {
66 sysrepo::S_Val operator()(const enum_& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020067 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020068 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T);
Václav Kubernát80aacc02018-08-22 17:41:54 +020069 }
70
Václav Kubernátab538992019-03-06 15:30:50 +010071 sysrepo::S_Val operator()(const binary_& value) const
72 {
73 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T);
74 }
75
Jan Kundrát379bb572020-05-07 03:23:13 +020076 sysrepo::S_Val operator()(const empty_) const
77 {
78 return std::make_shared<sysrepo::Val>(nullptr, SR_LEAF_EMPTY_T);
79 }
80
Václav Kubernáteeb38842019-03-20 19:46:05 +010081 sysrepo::S_Val operator()(const identityRef_& value) const
82 {
Jan Kundrát0d8abd12020-05-07 02:00:14 +020083 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 +010084 return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T);
85 }
86
Václav Kubernát144729d2020-01-08 15:20:35 +010087 sysrepo::S_Val operator()(const special_& value) const
88 {
89 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
90 }
91
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020092 sysrepo::S_Val operator()(const std::string& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020093 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020094 return std::make_shared<sysrepo::Val>(value.c_str());
Václav Kubernát80aacc02018-08-22 17:41:54 +020095 }
96
Jan Kundrátbd178362019-02-05 19:00:04 +010097 template <typename T>
98 sysrepo::S_Val operator()(const T& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020099 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200100 return std::make_shared<sysrepo::Val>(value);
Václav Kubernát80aacc02018-08-22 17:41:54 +0200101 }
102};
103
Jan Kundrát6ee84792020-01-24 01:43:36 +0100104struct updateSrValFromValue : boost::static_visitor<void> {
105 std::string xpath;
106 sysrepo::S_Val v;
107 updateSrValFromValue(const std::string& xpath, sysrepo::S_Val v)
108 : xpath(xpath)
109 , v(v)
110 {
111 }
112
113 void operator()(const enum_& value) const
114 {
115 v->set(xpath.c_str(), value.m_value.c_str(), SR_ENUM_T);
116 }
117
118 void operator()(const binary_& value) const
119 {
120 v->set(xpath.c_str(), value.m_value.c_str(), SR_BINARY_T);
121 }
122
Jan Kundrát379bb572020-05-07 03:23:13 +0200123 void operator()(const empty_) const
124 {
125 v->set(xpath.c_str(), nullptr, SR_LEAF_EMPTY_T);
126 }
127
Jan Kundrát6ee84792020-01-24 01:43:36 +0100128 void operator()(const identityRef_& value) const
129 {
130 v->set(xpath.c_str(), (value.m_prefix.value().m_name + ":" + value.m_value).c_str(), SR_IDENTITYREF_T);
131 }
132
133 void operator()(const special_& value) const
134 {
135 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
136 }
137
138 void operator()(const std::string& value) const
139 {
140 v->set(xpath.c_str(), value.c_str(), SR_STRING_T);
141 }
142
143 template <typename T>
144 void operator()(const T value) const
145 {
146 v->set(xpath.c_str(), value);
147 }
148};
149
Václav Kubernát812ee282018-08-30 17:10:03 +0200150SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200151
Václav Kubernát715c85c2020-04-14 01:46:08 +0200152sr_datastore_t toSrDatastore(Datastore datastore)
153{
154 switch (datastore) {
155 case Datastore::Running:
156 return SR_DS_RUNNING;
157 case Datastore::Startup:
158 return SR_DS_STARTUP;
159 }
160 __builtin_unreachable();
161}
162
163SysrepoAccess::SysrepoAccess(const std::string& appname, const Datastore datastore)
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200164 : m_connection(new sysrepo::Connection(appname.c_str()))
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200165 , m_schema(new YangSchema())
Václav Kubernát80aacc02018-08-22 17:41:54 +0200166{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200167 try {
Václav Kubernát715c85c2020-04-14 01:46:08 +0200168 m_session = std::make_shared<sysrepo::Session>(m_connection, toSrDatastore(datastore));
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200169 } catch (sysrepo::sysrepo_exception& ex) {
170 reportErrors();
171 }
Václav Kubernátb52dc252019-12-04 13:03:39 +0100172
173 // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module.
174 // That's why submoduleRevision is ignored.
175 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 +0200176 return fetchSchema(moduleName, revision, submodule);
177 });
178
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100179 for (const auto& it : listSchemas()) {
180 if (it->implemented()) {
181 m_schema->loadModule(it->module_name());
182 for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) {
183 m_schema->enableFeature(it->module_name(), it->enabled_features(i));
184 }
185 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200186 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200187}
188
Václav Kubernátd6282912020-06-23 14:49:34 +0200189DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) const
Václav Kubernát80aacc02018-08-22 17:41:54 +0200190{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200191 using namespace std::string_literals;
Jan Kundrátb331b552020-01-23 15:25:29 +0100192 Tree res;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200193
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200194 auto fillMap = [this, &res](auto items) {
Václav Kubernát3a433232020-07-08 17:52:50 +0200195 if (!items) {
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200196 return;
Václav Kubernát3a433232020-07-08 17:52:50 +0200197 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200198 for (unsigned int i = 0; i < items->val_cnt(); i++) {
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200199 auto value = leafValueFromVal(items->val(i));
Václav Kubernáte811bfa2020-05-29 02:25:20 +0200200 if (m_schema->nodeType(items->val(i)->xpath()) == yang::NodeTypes::LeafList) {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200201 res.emplace_back(items->val(i)->xpath(), special_{SpecialValue::LeafList});
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200202 std::string leafListPath = items->val(i)->xpath();
203 while (i < items->val_cnt() && leafListPath == items->val(i)->xpath()) {
204 auto leafListValue = leafDataToString(leafValueFromVal(items->val(i)));
Václav Kubernátfaacd022020-07-08 16:44:38 +0200205 res.emplace_back(items->val(i)->xpath() + "[.="s + escapeListKeyString(leafListValue) + "]", leafListValue);
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200206 i++;
207 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200208 } else {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200209 res.emplace_back(items->val(i)->xpath(), value);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200210 }
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200211 }
212 };
Václav Kubernátc89736b2018-08-30 16:14:05 +0200213
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200214 try {
215 if (path == "/") {
216 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
217 auto schemas = m_session->list_schemas();
218 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
219 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
220 }
221 } else {
222 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200223 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200224 } catch (sysrepo::sysrepo_exception& ex) {
225 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200226 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200227 return res;
228}
229
230void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
231{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200232 try {
233 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
234 } catch (sysrepo::sysrepo_exception& ex) {
235 reportErrors();
236 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200237}
238
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200239void SysrepoAccess::createItem(const std::string& path)
Václav Kubernát80aacc02018-08-22 17:41:54 +0200240{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200241 try {
242 m_session->set_item(path.c_str());
243 } catch (sysrepo::sysrepo_exception& ex) {
244 reportErrors();
245 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200246}
247
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200248void SysrepoAccess::deleteItem(const std::string& path)
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100249{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200250 try {
251 m_session->delete_item(path.c_str());
252 } catch (sysrepo::sysrepo_exception& ex) {
253 reportErrors();
254 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100255}
256
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200257struct impl_toSrMoveOp {
258 sr_move_position_t operator()(yang::move::Absolute& absolute)
259 {
260 return absolute == yang::move::Absolute::Begin ? SR_MOVE_FIRST : SR_MOVE_LAST;
261 }
262 sr_move_position_t operator()(yang::move::Relative& relative)
263 {
264 return relative.m_position == yang::move::Relative::Position::After ? SR_MOVE_AFTER : SR_MOVE_BEFORE;
265 }
266};
267
268sr_move_position_t toSrMoveOp(std::variant<yang::move::Absolute, yang::move::Relative> move)
269{
270 return std::visit(impl_toSrMoveOp{}, move);
271}
272
273void SysrepoAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
274{
275 std::string destPathStr;
276 if (std::holds_alternative<yang::move::Relative>(move)) {
277 auto relative = std::get<yang::move::Relative>(move);
278 if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
279 destPathStr = stripLeafListValueFromPath(source) + "[.='" + leafDataToString(relative.m_path.at(".")) + "']";
280 } else {
Václav Kubernát2c4778b2020-06-18 11:55:20 +0200281 destPathStr = stripLastListInstanceFromPath(source) + instanceToString(relative.m_path, m_schema->dataNodeFromPath(source)->node_module()->name());
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200282 }
283 }
284 m_session->move_item(source.c_str(), toSrMoveOp(move), destPathStr.c_str());
285}
286
Václav Kubernát812ee282018-08-30 17:10:03 +0200287void SysrepoAccess::commitChanges()
288{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200289 try {
290 m_session->commit();
291 } catch (sysrepo::sysrepo_exception& ex) {
292 reportErrors();
293 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200294}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200295
Václav Kubernát6d791432018-10-25 16:00:35 +0200296void SysrepoAccess::discardChanges()
297{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200298 try {
299 m_session->discard_changes();
300 } catch (sysrepo::sysrepo_exception& ex) {
301 reportErrors();
302 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200303}
304
Jan Kundrát6ee84792020-01-24 01:43:36 +0100305DatastoreAccess::Tree SysrepoAccess::executeRpc(const std::string &path, const Tree &input)
306{
307 auto srInput = std::make_shared<sysrepo::Vals>(input.size());
308 {
309 size_t i = 0;
310 for (const auto& [k, v] : input) {
311 boost::apply_visitor(updateSrValFromValue(joinPaths(path, k), srInput->val(i)), v);
312 ++i;
313 }
314 }
315 auto output = m_session->rpc_send(path.c_str(), srInput);
316 Tree res;
317 for (size_t i = 0; i < output->val_cnt(); ++i) {
318 const auto& v = output->val(i);
Václav Kubernátfaacd022020-07-08 16:44:38 +0200319 res.emplace_back(std::string(v->xpath()).substr(joinPaths(path, "/").size()), leafValueFromVal(v));
Jan Kundrát6ee84792020-01-24 01:43:36 +0100320 }
321 return res;
322}
323
Václav Kubernát7160a132020-04-03 02:11:01 +0200324void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination)
325{
326 m_session->copy_config(nullptr, toSrDatastore(source), toSrDatastore(destination));
327 if (destination == Datastore::Running) {
328 m_session->refresh();
329 }
330}
331
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200332std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule)
333{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200334 std::string schema;
335 try {
Václav Kubernátb52dc252019-12-04 13:03:39 +0100336 schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200337 } catch (sysrepo::sysrepo_exception& ex) {
338 reportErrors();
339 }
340
Václav Kubernát3a433232020-07-08 17:52:50 +0200341 if (schema.empty()) {
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200342 throw std::runtime_error(std::string("Module ") + module + " not available");
Václav Kubernát3a433232020-07-08 17:52:50 +0200343 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200344
345 return schema;
346}
347
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100348std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200349{
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100350 std::vector<sysrepo::S_Yang_Schema> res;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200351 std::shared_ptr<sysrepo::Yang_Schemas> schemas;
352 try {
353 schemas = m_session->list_schemas();
354 } catch (sysrepo::sysrepo_exception& ex) {
355 reportErrors();
356 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200357 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
358 auto schema = schemas->schema(i);
Václav Kubernátfaacd022020-07-08 16:44:38 +0200359 res.emplace_back(schema);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200360 }
361 return res;
362}
363
364std::shared_ptr<Schema> SysrepoAccess::schema()
365{
366 return m_schema;
367}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200368
Václav Kubernátd6282912020-06-23 14:49:34 +0200369[[noreturn]] void SysrepoAccess::reportErrors() const
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200370{
371 // I only use get_last_errors to get error info, since the error code from
372 // sysrepo_exception doesn't really give any meaningful information. For
373 // example an "invalid argument" error could mean a node isn't enabled, or
374 // it could mean something totally different and there is no documentation
375 // for that, so it's better to just use the message sysrepo gives me.
376 auto srErrors = m_session->get_last_errors();
377 std::vector<DatastoreError> res;
378
379 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
380 auto error = srErrors->error(i);
381 res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt);
382 }
383
384 throw DatastoreException(res);
385}
Václav Kubernátab612e92019-11-26 19:51:31 +0100386
387std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path)
388{
389 std::vector<ListInstance> res;
390 auto lists = getItems(path);
391
392 decltype(lists) instances;
393 auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin());
394 std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck=wantedTree->schema()->path()](const auto& item) {
395 // This filters out non-instances.
396 if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) {
397 return false;
398 }
399
400 // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance...
401 auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin());
402 // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested.
403 return instanceTree->schema()->path() == pathToCheck;
404 });
405
406 // If there are no instances, then just return
407 if (instances.empty()) {
408 return res;
409 }
410
411 // I need to find out which keys does the list have. To do that, I create a
412 // tree from the first instance. This is gives me some top level node,
413 // which will be our list in case out list is a top-level node. In case it
414 // isn't, we have call find_path on the top level node. After that, I just
415 // retrieve the keys.
416 auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first);
417 auto list = *(topLevelTree->find_path(path.c_str())->data().begin());
418 auto keys = libyang::Schema_Node_List{list->schema()}.keys();
419
420 // Creating a full tree at the same time from the values sysrepo gives me
421 // would be a pain (and after sysrepo switches to libyang meaningless), so
422 // I just use this algorithm to create data nodes one by one and get the
423 // key values from them.
424 for (const auto& instance : instances) {
425 auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin());
426 ListInstance instanceRes;
427 for (const auto& key : keys) {
428 auto vec = wantedList->find_path(key->name())->data();
429 auto leaf = libyang::Data_Node_Leaf_List{*(vec.begin())};
430 instanceRes.emplace(key->name(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base()));
431 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200432 res.emplace_back(instanceRes);
Václav Kubernátab612e92019-11-26 19:51:31 +0100433 }
434
435 return res;
436}
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200437
438std::string SysrepoAccess::dump(const DataFormat format) const
439{
440 std::shared_ptr<libyang::Data_Node> root;
441 auto input = getItems("/");
442 if (input.empty()) {
443 return "";
444 }
445 for (const auto& [k, v] : input) {
446 if (v.type() == typeid(special_) && boost::get<special_>(v).m_value != SpecialValue::PresenceContainer) {
447 continue;
448 }
449 if (!root) {
450 root = m_schema->dataNodeFromPath(k, leafDataToString(v));
451 } else {
452 // 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)
453 // and libyang complains if the node already exists.
454 root->new_path(nullptr, k.c_str(), leafDataToString(v).c_str(), LYD_ANYDATA_CONSTSTRING, LYD_PATH_OPT_UPDATE);
455 }
456 }
457 return root->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT);
458}