blob: 6bcb6f070def2afe25552e01fbca7fa9041801be [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át654303f2020-07-31 13:16:54 +020080SysrepoAccess::SysrepoAccess(const Datastore datastore)
81 : 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át715c85c2020-04-14 01:46:08 +020086 m_session = std::make_shared<sysrepo::Session>(m_connection, toSrDatastore(datastore));
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átd6282912020-06-23 14:49:34 +020092DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020093{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020094 using namespace std::string_literals;
Jan Kundrátb331b552020-01-23 15:25:29 +010095 Tree res;
Václav Kubernát80aacc02018-08-22 17:41:54 +020096
Václav Kubernátc58e4aa2019-04-03 18:37:32 +020097 try {
Václav Kubernát654303f2020-07-31 13:16:54 +020098 auto oldDs = m_session->session_get_ds();
99 m_session->session_switch_ds(SR_DS_OPERATIONAL);
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100100 auto config = m_session->get_data(((path == "/") ? "/*" : path).c_str());
Václav Kubernát654303f2020-07-31 13:16:54 +0200101 m_session->session_switch_ds(oldDs);
102 if (config) {
103 lyNodesToTree(res, config->tree_for());
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200104 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200105 } catch (sysrepo::sysrepo_exception& ex) {
106 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200107 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200108 return res;
109}
110
111void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
112{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200113 try {
114 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
115 } catch (sysrepo::sysrepo_exception& ex) {
116 reportErrors();
117 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200118}
119
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200120void SysrepoAccess::createItem(const std::string& path)
Václav Kubernát80aacc02018-08-22 17:41:54 +0200121{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200122 try {
123 m_session->set_item(path.c_str());
124 } catch (sysrepo::sysrepo_exception& ex) {
125 reportErrors();
126 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200127}
128
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200129void SysrepoAccess::deleteItem(const std::string& path)
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100130{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200131 try {
Václav Kubernát654303f2020-07-31 13:16:54 +0200132 // Have to use SR_EDIT_ISOLATE, because deleting something that's been set without committing is not supported
133 // https://github.com/sysrepo/sysrepo/issues/1967#issuecomment-625085090
134 m_session->delete_item(path.c_str(), SR_EDIT_ISOLATE);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200135 } catch (sysrepo::sysrepo_exception& ex) {
136 reportErrors();
137 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100138}
139
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200140struct impl_toSrMoveOp {
141 sr_move_position_t operator()(yang::move::Absolute& absolute)
142 {
143 return absolute == yang::move::Absolute::Begin ? SR_MOVE_FIRST : SR_MOVE_LAST;
144 }
145 sr_move_position_t operator()(yang::move::Relative& relative)
146 {
147 return relative.m_position == yang::move::Relative::Position::After ? SR_MOVE_AFTER : SR_MOVE_BEFORE;
148 }
149};
150
151sr_move_position_t toSrMoveOp(std::variant<yang::move::Absolute, yang::move::Relative> move)
152{
153 return std::visit(impl_toSrMoveOp{}, move);
154}
155
156void SysrepoAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
157{
Václav Kubernát654303f2020-07-31 13:16:54 +0200158 std::string destination;
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200159 if (std::holds_alternative<yang::move::Relative>(move)) {
160 auto relative = std::get<yang::move::Relative>(move);
161 if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
Václav Kubernát654303f2020-07-31 13:16:54 +0200162 destination = leafDataToString(relative.m_path.at("."));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200163 } else {
Václav Kubernát654303f2020-07-31 13:16:54 +0200164 destination = instanceToString(relative.m_path);
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200165 }
166 }
Václav Kubernát654303f2020-07-31 13:16:54 +0200167 m_session->move_item(source.c_str(), toSrMoveOp(move), destination.c_str(), destination.c_str());
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200168}
169
Václav Kubernát812ee282018-08-30 17:10:03 +0200170void SysrepoAccess::commitChanges()
171{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200172 try {
Václav Kubernát654303f2020-07-31 13:16:54 +0200173 m_session->apply_changes(OPERATION_TIMEOUT_MS, 1);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200174 } catch (sysrepo::sysrepo_exception& ex) {
175 reportErrors();
176 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200177}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200178
Václav Kubernát6d791432018-10-25 16:00:35 +0200179void SysrepoAccess::discardChanges()
180{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200181 try {
182 m_session->discard_changes();
183 } catch (sysrepo::sysrepo_exception& ex) {
184 reportErrors();
185 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200186}
187
Václav Kubernátb3960f82020-12-01 03:21:48 +0100188DatastoreAccess::Tree SysrepoAccess::execute(const std::string& path, const Tree& input)
Václav Kubernáta8789602020-07-20 15:18:19 +0200189{
Václav Kubernátfbab2d42021-02-05 16:12:34 +0100190 auto inputNode = treeToRpcInput(m_session->get_context(), path, input);
Václav Kubernát40776132021-02-03 08:47:33 +0100191 auto output = m_session->rpc_send(inputNode);
Václav Kubernátfbab2d42021-02-05 16:12:34 +0100192 return rpcOutputToTree(path, output);
Václav Kubernáta8789602020-07-20 15:18:19 +0200193}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100194
Václav Kubernát7160a132020-04-03 02:11:01 +0200195void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination)
196{
Václav Kubernát654303f2020-07-31 13:16:54 +0200197 auto oldDs = m_session->session_get_ds();
198 m_session->session_switch_ds(toSrDatastore(destination));
199 m_session->copy_config(toSrDatastore(source), nullptr, OPERATION_TIMEOUT_MS, 1);
200 m_session->session_switch_ds(oldDs);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200201}
202
203std::shared_ptr<Schema> SysrepoAccess::schema()
204{
205 return m_schema;
206}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200207
Václav Kubernátd6282912020-06-23 14:49:34 +0200208[[noreturn]] void SysrepoAccess::reportErrors() const
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200209{
Václav Kubernát654303f2020-07-31 13:16:54 +0200210 // I only use get_error to get error info, since the error code from
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200211 // sysrepo_exception doesn't really give any meaningful information. For
212 // example an "invalid argument" error could mean a node isn't enabled, or
213 // it could mean something totally different and there is no documentation
214 // for that, so it's better to just use the message sysrepo gives me.
Václav Kubernát654303f2020-07-31 13:16:54 +0200215 auto srErrors = m_session->get_error();
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200216 std::vector<DatastoreError> res;
217
218 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
Václav Kubernát654303f2020-07-31 13:16:54 +0200219 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 +0200220 }
221
222 throw DatastoreException(res);
223}
Václav Kubernátab612e92019-11-26 19:51:31 +0100224
225std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path)
226{
227 std::vector<ListInstance> res;
228 auto lists = getItems(path);
229
230 decltype(lists) instances;
231 auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin());
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100232 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 +0100233 // This filters out non-instances.
234 if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) {
235 return false;
236 }
237
238 // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance...
239 auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin());
240 // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested.
241 return instanceTree->schema()->path() == pathToCheck;
242 });
243
244 // If there are no instances, then just return
245 if (instances.empty()) {
246 return res;
247 }
248
249 // I need to find out which keys does the list have. To do that, I create a
250 // tree from the first instance. This is gives me some top level node,
251 // which will be our list in case out list is a top-level node. In case it
252 // isn't, we have call find_path on the top level node. After that, I just
253 // retrieve the keys.
254 auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first);
255 auto list = *(topLevelTree->find_path(path.c_str())->data().begin());
256 auto keys = libyang::Schema_Node_List{list->schema()}.keys();
257
258 // Creating a full tree at the same time from the values sysrepo gives me
259 // would be a pain (and after sysrepo switches to libyang meaningless), so
260 // I just use this algorithm to create data nodes one by one and get the
261 // key values from them.
262 for (const auto& instance : instances) {
263 auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin());
264 ListInstance instanceRes;
265 for (const auto& key : keys) {
266 auto vec = wantedList->find_path(key->name())->data();
Václav Kubernát2e4cafe2020-11-05 01:53:21 +0100267 auto leaf = std::make_shared<libyang::Data_Node_Leaf_List>(*(vec.begin()));
268 instanceRes.emplace(key->name(), leafValueFromNode(leaf));
Václav Kubernátab612e92019-11-26 19:51:31 +0100269 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200270 res.emplace_back(instanceRes);
Václav Kubernátab612e92019-11-26 19:51:31 +0100271 }
272
273 return res;
274}
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200275
276std::string SysrepoAccess::dump(const DataFormat format) const
277{
Václav Kubernát654303f2020-07-31 13:16:54 +0200278 auto root = m_session->get_data("/*");
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200279 return root->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT);
280}