blob: cf1940706fb506d8cbcada6c0589fea793ed3ab9 [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
Jan Kundrát68d4a2c2018-10-01 17:17:09 +02009#include <sysrepo-cpp/Session.hpp>
Václav Kubernát80aacc02018-08-22 17:41:54 +020010#include "sysrepo_access.hpp"
Jan Kundrát6ee84792020-01-24 01:43:36 +010011#include "utils.hpp"
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020012#include "yang_schema.hpp"
Václav Kubernát80aacc02018-08-22 17:41:54 +020013
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020014leaf_data_ leafValueFromVal(const sysrepo::S_Val& value)
Václav Kubernátc89736b2018-08-30 16:14:05 +020015{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020016 using namespace std::string_literals;
Václav Kubernátc89736b2018-08-30 16:14:05 +020017 switch (value->type()) {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020018 case SR_INT8_T:
19 return value->data()->get_int8();
20 case SR_UINT8_T:
21 return value->data()->get_uint8();
22 case SR_INT16_T:
23 return value->data()->get_int16();
24 case SR_UINT16_T:
25 return value->data()->get_uint16();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020026 case SR_INT32_T:
27 return value->data()->get_int32();
28 case SR_UINT32_T:
29 return value->data()->get_uint32();
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020030 case SR_INT64_T:
31 return value->data()->get_int64();
32 case SR_UINT64_T:
33 return value->data()->get_uint64();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020034 case SR_BOOL_T:
35 return value->data()->get_bool();
36 case SR_STRING_T:
37 return std::string(value->data()->get_string());
38 case SR_ENUM_T:
Václav Kubernát152ce222019-12-19 12:23:32 +010039 return enum_{std::string(value->data()->get_enum())};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020040 case SR_DECIMAL64_T:
41 return value->data()->get_decimal64();
42 case SR_CONTAINER_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010043 return special_{SpecialValue::Container};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020044 case SR_CONTAINER_PRESENCE_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010045 return special_{SpecialValue::PresenceContainer};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020046 case SR_LIST_T:
Václav Kubernát144729d2020-01-08 15:20:35 +010047 return special_{SpecialValue::List};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020048 default: // TODO: implement all types
49 return value->val_to_string();
Václav Kubernátc89736b2018-08-30 16:14:05 +020050 }
51}
Václav Kubernát80aacc02018-08-22 17:41:54 +020052
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020053struct valFromValue : boost::static_visitor<sysrepo::S_Val> {
54 sysrepo::S_Val operator()(const enum_& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020055 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020056 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T);
Václav Kubernát80aacc02018-08-22 17:41:54 +020057 }
58
Václav Kubernátab538992019-03-06 15:30:50 +010059 sysrepo::S_Val operator()(const binary_& value) const
60 {
61 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T);
62 }
63
Václav Kubernáteeb38842019-03-20 19:46:05 +010064 sysrepo::S_Val operator()(const identityRef_& value) const
65 {
66 auto res = value.m_prefix.value().m_name + ":" + value.m_value;
67 return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T);
68 }
69
Václav Kubernát144729d2020-01-08 15:20:35 +010070 sysrepo::S_Val operator()(const special_& value) const
71 {
72 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
73 }
74
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020075 sysrepo::S_Val operator()(const std::string& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020076 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020077 return std::make_shared<sysrepo::Val>(value.c_str());
Václav Kubernát80aacc02018-08-22 17:41:54 +020078 }
79
Jan Kundrátbd178362019-02-05 19:00:04 +010080 template <typename T>
81 sysrepo::S_Val operator()(const T& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020082 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020083 return std::make_shared<sysrepo::Val>(value);
Václav Kubernát80aacc02018-08-22 17:41:54 +020084 }
85};
86
Jan Kundrát6ee84792020-01-24 01:43:36 +010087struct updateSrValFromValue : boost::static_visitor<void> {
88 std::string xpath;
89 sysrepo::S_Val v;
90 updateSrValFromValue(const std::string& xpath, sysrepo::S_Val v)
91 : xpath(xpath)
92 , v(v)
93 {
94 }
95
96 void operator()(const enum_& value) const
97 {
98 v->set(xpath.c_str(), value.m_value.c_str(), SR_ENUM_T);
99 }
100
101 void operator()(const binary_& value) const
102 {
103 v->set(xpath.c_str(), value.m_value.c_str(), SR_BINARY_T);
104 }
105
106 void operator()(const identityRef_& value) const
107 {
108 v->set(xpath.c_str(), (value.m_prefix.value().m_name + ":" + value.m_value).c_str(), SR_IDENTITYREF_T);
109 }
110
111 void operator()(const special_& value) const
112 {
113 throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value));
114 }
115
116 void operator()(const std::string& value) const
117 {
118 v->set(xpath.c_str(), value.c_str(), SR_STRING_T);
119 }
120
121 template <typename T>
122 void operator()(const T value) const
123 {
124 v->set(xpath.c_str(), value);
125 }
126};
127
Václav Kubernát812ee282018-08-30 17:10:03 +0200128SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200129
130SysrepoAccess::SysrepoAccess(const std::string& appname)
Jan Kundrát68d4a2c2018-10-01 17:17:09 +0200131 : m_connection(new sysrepo::Connection(appname.c_str()))
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200132 , m_schema(new YangSchema())
Václav Kubernát80aacc02018-08-22 17:41:54 +0200133{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200134 try {
135 m_session = std::make_shared<sysrepo::Session>(m_connection);
136 } catch (sysrepo::sysrepo_exception& ex) {
137 reportErrors();
138 }
Václav Kubernátb52dc252019-12-04 13:03:39 +0100139
140 // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module.
141 // That's why submoduleRevision is ignored.
142 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 +0200143 return fetchSchema(moduleName, revision, submodule);
144 });
145
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100146 for (const auto& it : listSchemas()) {
147 if (it->implemented()) {
148 m_schema->loadModule(it->module_name());
149 for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) {
150 m_schema->enableFeature(it->module_name(), it->enabled_features(i));
151 }
152 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200153 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200154}
155
Jan Kundrátb331b552020-01-23 15:25:29 +0100156DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path)
Václav Kubernát80aacc02018-08-22 17:41:54 +0200157{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200158 using namespace std::string_literals;
Jan Kundrátb331b552020-01-23 15:25:29 +0100159 Tree res;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200160
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200161 auto fillMap = [&res](auto items) {
162 if (!items)
163 return;
164 for (unsigned int i = 0; i < items->val_cnt(); i++) {
165 res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i)));
166 }
167 };
Václav Kubernátc89736b2018-08-30 16:14:05 +0200168
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200169 try {
170 if (path == "/") {
171 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
172 auto schemas = m_session->list_schemas();
173 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
174 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
175 }
176 } else {
177 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200178 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200179 } catch (sysrepo::sysrepo_exception& ex) {
180 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200181 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200182 return res;
183}
184
185void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
186{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200187 try {
188 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
189 } catch (sysrepo::sysrepo_exception& ex) {
190 reportErrors();
191 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200192}
193
194void SysrepoAccess::createPresenceContainer(const std::string& path)
195{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200196 try {
197 m_session->set_item(path.c_str());
198 } catch (sysrepo::sysrepo_exception& ex) {
199 reportErrors();
200 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200201}
202
203void SysrepoAccess::deletePresenceContainer(const std::string& path)
204{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200205 try {
206 m_session->delete_item(path.c_str());
207 } catch (sysrepo::sysrepo_exception& ex) {
208 reportErrors();
209 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200210}
Václav Kubernát812ee282018-08-30 17:10:03 +0200211
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100212void SysrepoAccess::createListInstance(const std::string& path)
213{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200214 try {
215 m_session->set_item(path.c_str());
216 } catch (sysrepo::sysrepo_exception& ex) {
217 reportErrors();
218 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100219}
220
221void SysrepoAccess::deleteListInstance(const std::string& path)
222{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200223 try {
224 m_session->delete_item(path.c_str());
225 } catch (sysrepo::sysrepo_exception& ex) {
226 reportErrors();
227 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100228}
229
Václav Kubernát812ee282018-08-30 17:10:03 +0200230void SysrepoAccess::commitChanges()
231{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200232 try {
233 m_session->commit();
234 } catch (sysrepo::sysrepo_exception& ex) {
235 reportErrors();
236 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200237}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200238
Václav Kubernát6d791432018-10-25 16:00:35 +0200239void SysrepoAccess::discardChanges()
240{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200241 try {
242 m_session->discard_changes();
243 } catch (sysrepo::sysrepo_exception& ex) {
244 reportErrors();
245 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200246}
247
Jan Kundrát6ee84792020-01-24 01:43:36 +0100248DatastoreAccess::Tree SysrepoAccess::executeRpc(const std::string &path, const Tree &input)
249{
250 auto srInput = std::make_shared<sysrepo::Vals>(input.size());
251 {
252 size_t i = 0;
253 for (const auto& [k, v] : input) {
254 boost::apply_visitor(updateSrValFromValue(joinPaths(path, k), srInput->val(i)), v);
255 ++i;
256 }
257 }
258 auto output = m_session->rpc_send(path.c_str(), srInput);
259 Tree res;
260 for (size_t i = 0; i < output->val_cnt(); ++i) {
261 const auto& v = output->val(i);
262 res.emplace(std::string(v->xpath()).substr(joinPaths(path, "/").size()), leafValueFromVal(v));
263 }
264 return res;
265}
266
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200267std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule)
268{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200269 std::string schema;
270 try {
Václav Kubernátb52dc252019-12-04 13:03:39 +0100271 schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200272 } catch (sysrepo::sysrepo_exception& ex) {
273 reportErrors();
274 }
275
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200276 if (schema.empty())
277 throw std::runtime_error(std::string("Module ") + module + " not available");
278
279 return schema;
280}
281
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100282std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200283{
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100284 std::vector<sysrepo::S_Yang_Schema> res;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200285 std::shared_ptr<sysrepo::Yang_Schemas> schemas;
286 try {
287 schemas = m_session->list_schemas();
288 } catch (sysrepo::sysrepo_exception& ex) {
289 reportErrors();
290 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200291 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
292 auto schema = schemas->schema(i);
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100293 res.push_back(schema);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200294 }
295 return res;
296}
297
298std::shared_ptr<Schema> SysrepoAccess::schema()
299{
300 return m_schema;
301}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200302
303[[noreturn]] void SysrepoAccess::reportErrors()
304{
305 // I only use get_last_errors to get error info, since the error code from
306 // sysrepo_exception doesn't really give any meaningful information. For
307 // example an "invalid argument" error could mean a node isn't enabled, or
308 // it could mean something totally different and there is no documentation
309 // for that, so it's better to just use the message sysrepo gives me.
310 auto srErrors = m_session->get_last_errors();
311 std::vector<DatastoreError> res;
312
313 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
314 auto error = srErrors->error(i);
315 res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt);
316 }
317
318 throw DatastoreException(res);
319}