blob: 2c80c2da7d2ab150fa8c66c9a72fb05dff74fcc3 [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:
38 return std::string(value->data()->get_enum());
39 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áta6c5fff2018-09-07 15:16:25 +020092 m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule) {
93 return fetchSchema(moduleName, revision, submodule);
94 });
95
96 for (const auto& it : listImplementedSchemas()) {
97 m_schema->loadModule(it);
98 }
Václav Kubernát80aacc02018-08-22 17:41:54 +020099}
100
101std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path)
102{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200103 using namespace std::string_literals;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200104 std::map<std::string, leaf_data_> res;
Václav Kubernát80aacc02018-08-22 17:41:54 +0200105
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200106 auto fillMap = [&res](auto items) {
107 if (!items)
108 return;
109 for (unsigned int i = 0; i < items->val_cnt(); i++) {
110 res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i)));
111 }
112 };
Václav Kubernátc89736b2018-08-30 16:14:05 +0200113
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200114 try {
115 if (path == "/") {
116 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
117 auto schemas = m_session->list_schemas();
118 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
119 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
120 }
121 } else {
122 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200123 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200124 } catch (sysrepo::sysrepo_exception& ex) {
125 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200126 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200127 return res;
128}
129
130void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
131{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200132 try {
133 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
134 } catch (sysrepo::sysrepo_exception& ex) {
135 reportErrors();
136 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200137}
138
139void SysrepoAccess::createPresenceContainer(const std::string& path)
140{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200141 try {
142 m_session->set_item(path.c_str());
143 } catch (sysrepo::sysrepo_exception& ex) {
144 reportErrors();
145 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200146}
147
148void SysrepoAccess::deletePresenceContainer(const std::string& path)
149{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200150 try {
151 m_session->delete_item(path.c_str());
152 } catch (sysrepo::sysrepo_exception& ex) {
153 reportErrors();
154 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200155}
Václav Kubernát812ee282018-08-30 17:10:03 +0200156
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100157void SysrepoAccess::createListInstance(const std::string& path)
158{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200159 try {
160 m_session->set_item(path.c_str());
161 } catch (sysrepo::sysrepo_exception& ex) {
162 reportErrors();
163 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100164}
165
166void SysrepoAccess::deleteListInstance(const std::string& path)
167{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200168 try {
169 m_session->delete_item(path.c_str());
170 } catch (sysrepo::sysrepo_exception& ex) {
171 reportErrors();
172 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100173}
174
Václav Kubernát812ee282018-08-30 17:10:03 +0200175void SysrepoAccess::commitChanges()
176{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200177 try {
178 m_session->commit();
179 } catch (sysrepo::sysrepo_exception& ex) {
180 reportErrors();
181 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200182}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200183
Václav Kubernát6d791432018-10-25 16:00:35 +0200184void SysrepoAccess::discardChanges()
185{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200186 try {
187 m_session->discard_changes();
188 } catch (sysrepo::sysrepo_exception& ex) {
189 reportErrors();
190 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200191}
192
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200193std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule)
194{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200195 std::string schema;
196 try {
197 schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG); // FIXME: maybe we should use get_submodule_schema for submodules?
198 } catch (sysrepo::sysrepo_exception& ex) {
199 reportErrors();
200 }
201
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200202 if (schema.empty())
203 throw std::runtime_error(std::string("Module ") + module + " not available");
204
205 return schema;
206}
207
208std::vector<std::string> SysrepoAccess::listImplementedSchemas()
209{
210 std::vector<std::string> res;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200211 std::shared_ptr<sysrepo::Yang_Schemas> schemas;
212 try {
213 schemas = m_session->list_schemas();
214 } catch (sysrepo::sysrepo_exception& ex) {
215 reportErrors();
216 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200217 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
218 auto schema = schemas->schema(i);
219 if (schema->implemented())
220 res.push_back(schema->module_name());
221 }
222 return res;
223}
224
225std::shared_ptr<Schema> SysrepoAccess::schema()
226{
227 return m_schema;
228}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200229
230[[noreturn]] void SysrepoAccess::reportErrors()
231{
232 // I only use get_last_errors to get error info, since the error code from
233 // sysrepo_exception doesn't really give any meaningful information. For
234 // example an "invalid argument" error could mean a node isn't enabled, or
235 // it could mean something totally different and there is no documentation
236 // for that, so it's better to just use the message sysrepo gives me.
237 auto srErrors = m_session->get_last_errors();
238 std::vector<DatastoreError> res;
239
240 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
241 auto error = srErrors->error(i);
242 res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt);
243 }
244
245 throw DatastoreException(res);
246}