blob: 16dfab56412bc99e0ee1cb62d3fda1fc2de4eb86 [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()) {
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020017 case SR_INT32_T:
18 return value->data()->get_int32();
19 case SR_UINT32_T:
20 return value->data()->get_uint32();
21 case SR_BOOL_T:
22 return value->data()->get_bool();
23 case SR_STRING_T:
24 return std::string(value->data()->get_string());
25 case SR_ENUM_T:
26 return std::string(value->data()->get_enum());
27 case SR_DECIMAL64_T:
28 return value->data()->get_decimal64();
29 case SR_CONTAINER_T:
30 return "(container)"s;
31 case SR_CONTAINER_PRESENCE_T:
32 return "(presence container)"s;
33 case SR_LIST_T:
34 return "(list)"s;
35 default: // TODO: implement all types
36 return value->val_to_string();
Václav Kubernátc89736b2018-08-30 16:14:05 +020037 }
38}
Václav Kubernát80aacc02018-08-22 17:41:54 +020039
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020040struct valFromValue : boost::static_visitor<sysrepo::S_Val> {
41 sysrepo::S_Val operator()(const enum_& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020042 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020043 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T);
Václav Kubernát80aacc02018-08-22 17:41:54 +020044 }
45
Václav Kubernátab538992019-03-06 15:30:50 +010046 sysrepo::S_Val operator()(const binary_& value) const
47 {
48 return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T);
49 }
50
Václav Kubernáteeb38842019-03-20 19:46:05 +010051 sysrepo::S_Val operator()(const identityRef_& value) const
52 {
53 auto res = value.m_prefix.value().m_name + ":" + value.m_value;
54 return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T);
55 }
56
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020057 sysrepo::S_Val operator()(const std::string& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020058 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020059 return std::make_shared<sysrepo::Val>(value.c_str());
Václav Kubernát80aacc02018-08-22 17:41:54 +020060 }
61
Jan Kundrátbd178362019-02-05 19:00:04 +010062 template <typename T>
63 sysrepo::S_Val operator()(const T& value) const
Václav Kubernát80aacc02018-08-22 17:41:54 +020064 {
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020065 return std::make_shared<sysrepo::Val>(value);
Václav Kubernát80aacc02018-08-22 17:41:54 +020066 }
67};
68
Václav Kubernát812ee282018-08-30 17:10:03 +020069SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +020070
71SysrepoAccess::SysrepoAccess(const std::string& appname)
Jan Kundrát68d4a2c2018-10-01 17:17:09 +020072 : m_connection(new sysrepo::Connection(appname.c_str()))
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020073 , m_schema(new YangSchema())
Václav Kubernát80aacc02018-08-22 17:41:54 +020074{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +020075 try {
76 m_session = std::make_shared<sysrepo::Session>(m_connection);
77 } catch (sysrepo::sysrepo_exception& ex) {
78 reportErrors();
79 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020080 m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule) {
81 return fetchSchema(moduleName, revision, submodule);
82 });
83
84 for (const auto& it : listImplementedSchemas()) {
85 m_schema->loadModule(it);
86 }
Václav Kubernát80aacc02018-08-22 17:41:54 +020087}
88
89std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path)
90{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020091 using namespace std::string_literals;
Václav Kubernát80aacc02018-08-22 17:41:54 +020092 std::map<std::string, leaf_data_> res;
Václav Kubernát80aacc02018-08-22 17:41:54 +020093
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020094 auto fillMap = [&res](auto items) {
95 if (!items)
96 return;
97 for (unsigned int i = 0; i < items->val_cnt(); i++) {
98 res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i)));
99 }
100 };
Václav Kubernátc89736b2018-08-30 16:14:05 +0200101
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200102 try {
103 if (path == "/") {
104 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
105 auto schemas = m_session->list_schemas();
106 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
107 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
108 }
109 } else {
110 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200111 }
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200112 } catch (sysrepo::sysrepo_exception& ex) {
113 reportErrors();
Václav Kubernátc89736b2018-08-30 16:14:05 +0200114 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200115 return res;
116}
117
118void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
119{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200120 try {
121 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
122 } catch (sysrepo::sysrepo_exception& ex) {
123 reportErrors();
124 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200125}
126
127void SysrepoAccess::createPresenceContainer(const std::string& path)
128{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200129 try {
130 m_session->set_item(path.c_str());
131 } catch (sysrepo::sysrepo_exception& ex) {
132 reportErrors();
133 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200134}
135
136void SysrepoAccess::deletePresenceContainer(const std::string& path)
137{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200138 try {
139 m_session->delete_item(path.c_str());
140 } catch (sysrepo::sysrepo_exception& ex) {
141 reportErrors();
142 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200143}
Václav Kubernát812ee282018-08-30 17:10:03 +0200144
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100145void SysrepoAccess::createListInstance(const std::string& path)
146{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200147 try {
148 m_session->set_item(path.c_str());
149 } catch (sysrepo::sysrepo_exception& ex) {
150 reportErrors();
151 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100152}
153
154void SysrepoAccess::deleteListInstance(const std::string& path)
155{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200156 try {
157 m_session->delete_item(path.c_str());
158 } catch (sysrepo::sysrepo_exception& ex) {
159 reportErrors();
160 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100161}
162
Václav Kubernát812ee282018-08-30 17:10:03 +0200163void SysrepoAccess::commitChanges()
164{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200165 try {
166 m_session->commit();
167 } catch (sysrepo::sysrepo_exception& ex) {
168 reportErrors();
169 }
Václav Kubernát812ee282018-08-30 17:10:03 +0200170}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200171
Václav Kubernát6d791432018-10-25 16:00:35 +0200172void SysrepoAccess::discardChanges()
173{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200174 try {
175 m_session->discard_changes();
176 } catch (sysrepo::sysrepo_exception& ex) {
177 reportErrors();
178 }
Václav Kubernát6d791432018-10-25 16:00:35 +0200179}
180
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200181std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule)
182{
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200183 std::string schema;
184 try {
185 schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG); // FIXME: maybe we should use get_submodule_schema for submodules?
186 } catch (sysrepo::sysrepo_exception& ex) {
187 reportErrors();
188 }
189
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200190 if (schema.empty())
191 throw std::runtime_error(std::string("Module ") + module + " not available");
192
193 return schema;
194}
195
196std::vector<std::string> SysrepoAccess::listImplementedSchemas()
197{
198 std::vector<std::string> res;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200199 std::shared_ptr<sysrepo::Yang_Schemas> schemas;
200 try {
201 schemas = m_session->list_schemas();
202 } catch (sysrepo::sysrepo_exception& ex) {
203 reportErrors();
204 }
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200205 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
206 auto schema = schemas->schema(i);
207 if (schema->implemented())
208 res.push_back(schema->module_name());
209 }
210 return res;
211}
212
213std::shared_ptr<Schema> SysrepoAccess::schema()
214{
215 return m_schema;
216}
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200217
218[[noreturn]] void SysrepoAccess::reportErrors()
219{
220 // I only use get_last_errors to get error info, since the error code from
221 // sysrepo_exception doesn't really give any meaningful information. For
222 // example an "invalid argument" error could mean a node isn't enabled, or
223 // it could mean something totally different and there is no documentation
224 // for that, so it's better to just use the message sysrepo gives me.
225 auto srErrors = m_session->get_last_errors();
226 std::vector<DatastoreError> res;
227
228 for (size_t i = 0; i < srErrors->error_cnt(); i++) {
229 auto error = srErrors->error(i);
230 res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt);
231 }
232
233 throw DatastoreException(res);
234}