blob: 30a3e2573f6833ad0fa1162f9945650aa67787eb [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>
Václav Kubernát19097f32020-10-05 10:08:29 +020012#include <sstream>
Václav Kubernátb4e5b182020-11-16 19:55:09 +010013#include <sysrepo-cpp/Session.hpp>
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
Václav Kubernát654303f2020-07-31 13:16:54 +020019const auto OPERATION_TIMEOUT_MS = 1000;
20
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020021struct valFromValue : boost::static_visitor<sysrepo::S_Val> {
22 sysrepo::S_Val operator()(const enum_& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020023 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020024 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T);
Václav Kubernát80aacc02018-08-22 17:41:54 +020025 }
26
Václav Kubernátab538992019-03-06 15:30:50 +010027 sysrepo::S_Val operator()(const binary_& value) const
28 {
29 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T);
30 }
31
Jan Kundrát379bb572020-05-07 03:23:13 +020032 sysrepo::S_Val operator()(const empty_) const
33 {
34 return std::make_shared<sysrepo::Val>(nullptr, SR_LEAF_EMPTY_T);
35 }
36
Václav Kubernáteeb38842019-03-20 19:46:05 +010037 sysrepo::S_Val operator()(const identityRef_& value) const
38 {
Jan Kundrát0d8abd12020-05-07 02:00:14 +020039 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 +010040 return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T);
41 }
42
Václav Kubernát144729d2020-01-08 15:20:35 +010043 sysrepo::S_Val operator()(const special_& value) const
44 {
45 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
46 }
47
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020048 sysrepo::S_Val operator()(const std::string& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020049 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020050 return std::make_shared<sysrepo::Val>(value.c_str());
Václav Kubernát80aacc02018-08-22 17:41:54 +020051 }
52
Václav Kubernát19097f32020-10-05 10:08:29 +020053 sysrepo::S_Val operator()(const bits_& value) const
54 {
55 std::stringstream ss;
56 std::copy(value.m_bits.begin(), value.m_bits.end(), std::experimental::make_ostream_joiner(ss, " "));
57 return std::make_shared<sysrepo::Val>(ss.str().c_str(), SR_BITS_T);
58 }
59
Jan Kundrátbd178362019-02-05 19:00:04 +010060 template <typename T>
61 sysrepo::S_Val operator()(const T& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020062 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020063 return std::make_shared<sysrepo::Val>(value);
Václav Kubernát80aacc02018-08-22 17:41:54 +020064 }
65};
66
Václav Kubernát812ee282018-08-30 17:10:03 +020067SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +020068
Václav Kubernát715c85c2020-04-14 01:46:08 +020069sr_datastore_t toSrDatastore(Datastore datastore)
70{
71 switch (datastore) {
72 case Datastore::Running:
73 return SR_DS_RUNNING;
74 case Datastore::Startup:
75 return SR_DS_STARTUP;
76 }
77 __builtin_unreachable();
78}
79
Václav Kubernátf5d75152020-12-03 03:52:34 +010080SysrepoAccess::SysrepoAccess()
Václav Kubernát654303f2020-07-31 13:16:54 +020081 : m_connection(std::make_shared<sysrepo::Connection>())
82 , m_session(std::make_shared<sysrepo::Session>(m_connection))
83 , m_schema(std::make_shared<YangSchema>(m_session->get_context()))
Václav Kubernát80aacc02018-08-22 17:41:54 +020084{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +020085 try {
Václav Kubernátf5d75152020-12-03 03:52:34 +010086 m_session = std::make_shared<sysrepo::Session>(m_connection);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +020087 } catch (sysrepo::sysrepo_exception& ex) {
88 reportErrors();
89 }
Václav Kubernát80aacc02018-08-22 17:41:54 +020090}
91
Václav Kubernátf5d75152020-12-03 03:52:34 +010092namespace {
93auto targetToDs_get(const DatastoreTarget target)
94{
95 switch (target) {
96 case DatastoreTarget::Operational:
97 return SR_DS_OPERATIONAL;
98 case DatastoreTarget::Running:
99 return SR_DS_RUNNING;
100 case DatastoreTarget::Startup:
101 return SR_DS_STARTUP;
102 }
103
104 __builtin_unreachable();
105}
106
107auto targetToDs_set(const DatastoreTarget target)
108{
109 switch (target) {
110 case DatastoreTarget::Operational:
111 case DatastoreTarget::Running:
112 // TODO: Doing candidate here doesn't work, why?
113 return SR_DS_RUNNING;
114 case DatastoreTarget::Startup:
115 return SR_DS_STARTUP;
116 }
117
118 __builtin_unreachable();
119}
120}
121
Václav Kubernátd6282912020-06-23 14:49:34 +0200122DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) const
Václav Kubernát80aacc02018-08-22 17:41:54 +0200123{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200124 using namespace std::string_literals;
Jan Kundrátb331b552020-01-23 15:25:29 +0100125 Tree res;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200126
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200127 try {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100128 m_session->session_switch_ds(targetToDs_get(m_target));
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100129 auto config = m_session->get_data(((path == "/") ? "/*" : path).c_str());
Václav Kubernát654303f2020-07-31 13:16:54 +0200130 if (config) {
131 lyNodesToTree(res, config->tree_for());
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200132 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200133 } catch (sysrepo::sysrepo_exception& ex) {
134 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200135 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200136 return res;
137}
138
139void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
140{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200141 try {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100142 m_session->session_switch_ds(targetToDs_set(m_target));
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200143 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
144 } catch (sysrepo::sysrepo_exception& ex) {
145 reportErrors();
146 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200147}
148
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200149void SysrepoAccess::createItem(const std::string& path)
Václav Kubernát80aacc02018-08-22 17:41:54 +0200150{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200151 try {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100152 m_session->session_switch_ds(targetToDs_set(m_target));
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200153 m_session->set_item(path.c_str());
154 } catch (sysrepo::sysrepo_exception& ex) {
155 reportErrors();
156 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200157}
158
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200159void SysrepoAccess::deleteItem(const std::string& path)
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100160{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200161 try {
Václav Kubernát654303f2020-07-31 13:16:54 +0200162 // Have to use SR_EDIT_ISOLATE, because deleting something that's been set without committing is not supported
163 // https://github.com/sysrepo/sysrepo/issues/1967#issuecomment-625085090
Václav Kubernátf5d75152020-12-03 03:52:34 +0100164 m_session->session_switch_ds(targetToDs_set(m_target));
Václav Kubernát654303f2020-07-31 13:16:54 +0200165 m_session->delete_item(path.c_str(), SR_EDIT_ISOLATE);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200166 } catch (sysrepo::sysrepo_exception& ex) {
167 reportErrors();
168 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100169}
170
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200171struct impl_toSrMoveOp {
172 sr_move_position_t operator()(yang::move::Absolute& absolute)
173 {
174 return absolute == yang::move::Absolute::Begin ? SR_MOVE_FIRST : SR_MOVE_LAST;
175 }
176 sr_move_position_t operator()(yang::move::Relative& relative)
177 {
178 return relative.m_position == yang::move::Relative::Position::After ? SR_MOVE_AFTER : SR_MOVE_BEFORE;
179 }
180};
181
182sr_move_position_t toSrMoveOp(std::variant<yang::move::Absolute, yang::move::Relative> move)
183{
184 return std::visit(impl_toSrMoveOp{}, move);
185}
186
187void SysrepoAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
188{
Václav Kubernát654303f2020-07-31 13:16:54 +0200189 std::string destination;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200190 if (std::holds_alternative<yang::move::Relative>(move)) {
191 auto relative = std::get<yang::move::Relative>(move);
192 if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
Václav Kubernát654303f2020-07-31 13:16:54 +0200193 destination = leafDataToString(relative.m_path.at("."));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200194 } else {
Václav Kubernát654303f2020-07-31 13:16:54 +0200195 destination = instanceToString(relative.m_path);
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200196 }
197 }
Václav Kubernátf5d75152020-12-03 03:52:34 +0100198 m_session->session_switch_ds(targetToDs_set(m_target));
Václav Kubernát654303f2020-07-31 13:16:54 +0200199 m_session->move_item(source.c_str(), toSrMoveOp(move), destination.c_str(), destination.c_str());
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200200}
201
Václav Kubernát812ee282018-08-30 17:10:03 +0200202void SysrepoAccess::commitChanges()
203{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200204 try {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100205 m_session->session_switch_ds(targetToDs_set(m_target));
Václav Kubernát654303f2020-07-31 13:16:54 +0200206 m_session->apply_changes(OPERATION_TIMEOUT_MS, 1);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200207 } catch (sysrepo::sysrepo_exception& ex) {
208 reportErrors();
209 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200210}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200211
Václav Kubernát6d791432018-10-25 16:00:35 +0200212void SysrepoAccess::discardChanges()
213{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200214 try {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100215 m_session->session_switch_ds(targetToDs_set(m_target));
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200216 m_session->discard_changes();
217 } catch (sysrepo::sysrepo_exception& ex) {
218 reportErrors();
219 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200220}
221
Václav Kubernátb3960f82020-12-01 03:21:48 +0100222DatastoreAccess::Tree SysrepoAccess::execute(const std::string& path, const Tree& input)
Václav Kubernáta8789602020-07-20 15:18:19 +0200223{
Václav Kubernátfbab2d42021-02-05 16:12:34 +0100224 auto inputNode = treeToRpcInput(m_session->get_context(), path, input);
Václav Kubernátf5d75152020-12-03 03:52:34 +0100225 m_session->session_switch_ds(targetToDs_set(m_target));
Václav Kubernát40776132021-02-03 08:47:33 +0100226 auto output = m_session->rpc_send(inputNode);
Václav Kubernátfbab2d42021-02-05 16:12:34 +0100227 return rpcOutputToTree(path, output);
Václav Kubernáta8789602020-07-20 15:18:19 +0200228}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100229
Václav Kubernát7160a132020-04-03 02:11:01 +0200230void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination)
231{
Václav Kubernát654303f2020-07-31 13:16:54 +0200232 m_session->session_switch_ds(toSrDatastore(destination));
233 m_session->copy_config(toSrDatastore(source), nullptr, OPERATION_TIMEOUT_MS, 1);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200234}
235
236std::shared_ptr<Schema> SysrepoAccess::schema()
237{
238 return m_schema;
239}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200240
Václav Kubernátd6282912020-06-23 14:49:34 +0200241[[noreturn]] void SysrepoAccess::reportErrors() const
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200242{
Václav Kubernát654303f2020-07-31 13:16:54 +0200243 // I only use get_error to get error info, since the error code from
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200244 // sysrepo_exception doesn't really give any meaningful information. For
245 // example an "invalid argument" error could mean a node isn't enabled, or
246 // it could mean something totally different and there is no documentation
247 // for that, so it's better to just use the message sysrepo gives me.
Václav Kubernát654303f2020-07-31 13:16:54 +0200248 auto srErrors = m_session->get_error();
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200249 std::vector<DatastoreError> res;
250
251 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
Václav Kubernát654303f2020-07-31 13:16:54 +0200252 res.emplace_back(srErrors->message(i), srErrors->xpath(i) ? std::optional<std::string>{srErrors->xpath(i)} : std::nullopt);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200253 }
254
255 throw DatastoreException(res);
256}
Václav Kubernátab612e92019-11-26 19:51:31 +0100257
258std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path)
259{
260 std::vector<ListInstance> res;
261 auto lists = getItems(path);
262
263 decltype(lists) instances;
264 auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin());
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100265 std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck = wantedTree->schema()->path()](const auto& item) {
Václav Kubernátab612e92019-11-26 19:51:31 +0100266 // This filters out non-instances.
267 if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) {
268 return false;
269 }
270
271 // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance...
272 auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin());
273 // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested.
274 return instanceTree->schema()->path() == pathToCheck;
275 });
276
277 // If there are no instances, then just return
278 if (instances.empty()) {
279 return res;
280 }
281
282 // I need to find out which keys does the list have. To do that, I create a
283 // tree from the first instance. This is gives me some top level node,
284 // which will be our list in case out list is a top-level node. In case it
285 // isn't, we have call find_path on the top level node. After that, I just
286 // retrieve the keys.
287 auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first);
288 auto list = *(topLevelTree->find_path(path.c_str())->data().begin());
289 auto keys = libyang::Schema_Node_List{list->schema()}.keys();
290
291 // Creating a full tree at the same time from the values sysrepo gives me
292 // would be a pain (and after sysrepo switches to libyang meaningless), so
293 // I just use this algorithm to create data nodes one by one and get the
294 // key values from them.
295 for (const auto& instance : instances) {
296 auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin());
297 ListInstance instanceRes;
298 for (const auto& key : keys) {
299 auto vec = wantedList->find_path(key->name())->data();
Václav Kubernát2e4cafe2020-11-05 01:53:21 +0100300 auto leaf = std::make_shared<libyang::Data_Node_Leaf_List>(*(vec.begin()));
301 instanceRes.emplace(key->name(), leafValueFromNode(leaf));
Václav Kubernátab612e92019-11-26 19:51:31 +0100302 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200303 res.emplace_back(instanceRes);
Václav Kubernátab612e92019-11-26 19:51:31 +0100304 }
305
306 return res;
307}
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200308
309std::string SysrepoAccess::dump(const DataFormat format) const
310{
Václav Kubernát654303f2020-07-31 13:16:54 +0200311 auto root = m_session->get_data("/*");
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200312 return root->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT);
313}