blob: b01b484f50e96e79d06c7ac448f8822ce99f144b [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"
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020011#include "yang_schema.hpp"
Václav Kubernát80aacc02018-08-22 17:41:54 +020012
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020013leaf_data_ leafValueFromVal(const sysrepo::S_Val& value)
Václav Kubernátc89736b2018-08-30 16:14:05 +020014{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020015 using namespace std::string_literals;
Václav Kubernátc89736b2018-08-30 16:14:05 +020016 switch (value->type()) {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020017 case SR_INT8_T:
18 return value->data()->get_int8();
19 case SR_UINT8_T:
20 return value->data()->get_uint8();
21 case SR_INT16_T:
22 return value->data()->get_int16();
23 case SR_UINT16_T:
24 return value->data()->get_uint16();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020025 case SR_INT32_T:
26 return value->data()->get_int32();
27 case SR_UINT32_T:
28 return value->data()->get_uint32();
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020029 case SR_INT64_T:
30 return value->data()->get_int64();
31 case SR_UINT64_T:
32 return value->data()->get_uint64();
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020033 case SR_BOOL_T:
34 return value->data()->get_bool();
35 case SR_STRING_T:
36 return std::string(value->data()->get_string());
37 case SR_ENUM_T:
Václav Kubernát152ce222019-12-19 12:23:32 +010038 return enum_{std::string(value->data()->get_enum())};
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020039 case SR_DECIMAL64_T:
40 return value->data()->get_decimal64();
41 case SR_CONTAINER_T:
42 return "(container)"s;
43 case SR_CONTAINER_PRESENCE_T:
44 return "(presence container)"s;
45 case SR_LIST_T:
46 return "(list)"s;
47 default: // TODO: implement all types
48 return value->val_to_string();
Václav Kubernátc89736b2018-08-30 16:14:05 +020049 }
50}
Václav Kubernát80aacc02018-08-22 17:41:54 +020051
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020052struct valFromValue : boost::static_visitor<sysrepo::S_Val> {
53 sysrepo::S_Val operator()(const enum_& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020054 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020055 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T);
Václav Kubernát80aacc02018-08-22 17:41:54 +020056 }
57
Václav Kubernátab538992019-03-06 15:30:50 +010058 sysrepo::S_Val operator()(const binary_& value) const
59 {
60 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T);
61 }
62
Václav Kubernáteeb38842019-03-20 19:46:05 +010063 sysrepo::S_Val operator()(const identityRef_& value) const
64 {
65 auto res = value.m_prefix.value().m_name + ":" + value.m_value;
66 return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T);
67 }
68
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020069 sysrepo::S_Val operator()(const std::string& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020070 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020071 return std::make_shared<sysrepo::Val>(value.c_str());
Václav Kubernát80aacc02018-08-22 17:41:54 +020072 }
73
Jan Kundrátbd178362019-02-05 19:00:04 +010074 template <typename T>
75 sysrepo::S_Val operator()(const T& 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);
Václav Kubernát80aacc02018-08-22 17:41:54 +020078 }
79};
80
Václav Kubernát812ee282018-08-30 17:10:03 +020081SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +020082
83SysrepoAccess::SysrepoAccess(const std::string& appname)
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020084 : m_connection(new sysrepo::Connection(appname.c_str()))
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020085 , m_schema(new YangSchema())
Václav Kubernát80aacc02018-08-22 17:41:54 +020086{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +020087 try {
88 m_session = std::make_shared<sysrepo::Session>(m_connection);
89 } catch (sysrepo::sysrepo_exception& ex) {
90 reportErrors();
91 }
Václav Kubernátb52dc252019-12-04 13:03:39 +010092
93 // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module.
94 // That's why submoduleRevision is ignored.
95 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 +020096 return fetchSchema(moduleName, revision, submodule);
97 });
98
Václav Kubernátbf9c6112019-11-04 16:03:35 +010099 for (const auto& it : listSchemas()) {
100 if (it->implemented()) {
101 m_schema->loadModule(it->module_name());
102 for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) {
103 m_schema->enableFeature(it->module_name(), it->enabled_features(i));
104 }
105 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200106 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200107}
108
109std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path)
110{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200111 using namespace std::string_literals;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200112 std::map<std::string, leaf_data_> res;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200113
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200114 auto fillMap = [&res](auto items) {
115 if (!items)
116 return;
117 for (unsigned int i = 0; i < items->val_cnt(); i++) {
118 res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i)));
119 }
120 };
Václav Kubernátc89736b2018-08-30 16:14:05 +0200121
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200122 try {
123 if (path == "/") {
124 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
125 auto schemas = m_session->list_schemas();
126 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
127 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
128 }
129 } else {
130 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200131 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200132 } catch (sysrepo::sysrepo_exception& ex) {
133 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200134 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200135 return res;
136}
137
138void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
139{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200140 try {
141 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
142 } catch (sysrepo::sysrepo_exception& ex) {
143 reportErrors();
144 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200145}
146
147void SysrepoAccess::createPresenceContainer(const std::string& path)
148{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200149 try {
150 m_session->set_item(path.c_str());
151 } catch (sysrepo::sysrepo_exception& ex) {
152 reportErrors();
153 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200154}
155
156void SysrepoAccess::deletePresenceContainer(const std::string& path)
157{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200158 try {
159 m_session->delete_item(path.c_str());
160 } catch (sysrepo::sysrepo_exception& ex) {
161 reportErrors();
162 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200163}
Václav Kubernát812ee282018-08-30 17:10:03 +0200164
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100165void SysrepoAccess::createListInstance(const std::string& path)
166{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200167 try {
168 m_session->set_item(path.c_str());
169 } catch (sysrepo::sysrepo_exception& ex) {
170 reportErrors();
171 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100172}
173
174void SysrepoAccess::deleteListInstance(const std::string& path)
175{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200176 try {
177 m_session->delete_item(path.c_str());
178 } catch (sysrepo::sysrepo_exception& ex) {
179 reportErrors();
180 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100181}
182
Václav Kubernát812ee282018-08-30 17:10:03 +0200183void SysrepoAccess::commitChanges()
184{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200185 try {
186 m_session->commit();
187 } catch (sysrepo::sysrepo_exception& ex) {
188 reportErrors();
189 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200190}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200191
Václav Kubernát6d791432018-10-25 16:00:35 +0200192void SysrepoAccess::discardChanges()
193{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200194 try {
195 m_session->discard_changes();
196 } catch (sysrepo::sysrepo_exception& ex) {
197 reportErrors();
198 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200199}
200
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200201std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule)
202{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200203 std::string schema;
204 try {
Václav Kubernátb52dc252019-12-04 13:03:39 +0100205 schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG);
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200206 } catch (sysrepo::sysrepo_exception& ex) {
207 reportErrors();
208 }
209
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200210 if (schema.empty())
211 throw std::runtime_error(std::string("Module ") + module + " not available");
212
213 return schema;
214}
215
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100216std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200217{
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100218 std::vector<sysrepo::S_Yang_Schema> res;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200219 std::shared_ptr<sysrepo::Yang_Schemas> schemas;
220 try {
221 schemas = m_session->list_schemas();
222 } catch (sysrepo::sysrepo_exception& ex) {
223 reportErrors();
224 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200225 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
226 auto schema = schemas->schema(i);
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100227 res.push_back(schema);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200228 }
229 return res;
230}
231
232std::shared_ptr<Schema> SysrepoAccess::schema()
233{
234 return m_schema;
235}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200236
237[[noreturn]] void SysrepoAccess::reportErrors()
238{
239 // I only use get_last_errors to get error info, since the error code from
240 // sysrepo_exception doesn't really give any meaningful information. For
241 // example an "invalid argument" error could mean a node isn't enabled, or
242 // it could mean something totally different and there is no documentation
243 // for that, so it's better to just use the message sysrepo gives me.
244 auto srErrors = m_session->get_last_errors();
245 std::vector<DatastoreError> res;
246
247 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
248 auto error = srErrors->error(i);
249 res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt);
250 }
251
252 throw DatastoreException(res);
253}