Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 1 | /* |
| 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át | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 9 | #include <sysrepo-cpp/Session.hpp> |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 10 | #include "sysrepo_access.hpp" |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 11 | #include "utils.hpp" |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 12 | #include "yang_schema.hpp" |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 13 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 14 | leaf_data_ leafValueFromVal(const sysrepo::S_Val& value) |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 15 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 16 | using namespace std::string_literals; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 17 | switch (value->type()) { |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 18 | 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át | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 26 | case SR_INT32_T: |
| 27 | return value->data()->get_int32(); |
| 28 | case SR_UINT32_T: |
| 29 | return value->data()->get_uint32(); |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 30 | case SR_INT64_T: |
| 31 | return value->data()->get_int64(); |
| 32 | case SR_UINT64_T: |
| 33 | return value->data()->get_uint64(); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 34 | 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át | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 39 | return enum_{std::string(value->data()->get_enum())}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 40 | case SR_DECIMAL64_T: |
| 41 | return value->data()->get_decimal64(); |
| 42 | case SR_CONTAINER_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 43 | return special_{SpecialValue::Container}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 44 | case SR_CONTAINER_PRESENCE_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 45 | return special_{SpecialValue::PresenceContainer}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 46 | case SR_LIST_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 47 | return special_{SpecialValue::List}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 48 | default: // TODO: implement all types |
| 49 | return value->val_to_string(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 50 | } |
| 51 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 52 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 53 | struct valFromValue : boost::static_visitor<sysrepo::S_Val> { |
| 54 | sysrepo::S_Val operator()(const enum_& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 55 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 56 | return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 59 | 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át | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 64 | 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át | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 70 | 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át | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 75 | sysrepo::S_Val operator()(const std::string& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 76 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 77 | return std::make_shared<sysrepo::Val>(value.c_str()); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Jan Kundrát | bd17836 | 2019-02-05 19:00:04 +0100 | [diff] [blame] | 80 | template <typename T> |
| 81 | sysrepo::S_Val operator()(const T& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 82 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 83 | return std::make_shared<sysrepo::Val>(value); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 87 | struct 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át | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 128 | SysrepoAccess::~SysrepoAccess() = default; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 129 | |
| 130 | SysrepoAccess::SysrepoAccess(const std::string& appname) |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 131 | : m_connection(new sysrepo::Connection(appname.c_str())) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 132 | , m_schema(new YangSchema()) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 133 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 134 | try { |
| 135 | m_session = std::make_shared<sysrepo::Session>(m_connection); |
| 136 | } catch (sysrepo::sysrepo_exception& ex) { |
| 137 | reportErrors(); |
| 138 | } |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 139 | |
| 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át | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 143 | return fetchSchema(moduleName, revision, submodule); |
| 144 | }); |
| 145 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 146 | 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át | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 153 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 156 | DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 157 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 158 | using namespace std::string_literals; |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 159 | Tree res; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 160 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 161 | 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át | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 168 | |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 169 | 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át | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 178 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 179 | } catch (sysrepo::sysrepo_exception& ex) { |
| 180 | reportErrors(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 181 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 182 | return res; |
| 183 | } |
| 184 | |
| 185 | void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 186 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 187 | 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át | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void SysrepoAccess::createPresenceContainer(const std::string& path) |
| 195 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 196 | try { |
| 197 | m_session->set_item(path.c_str()); |
| 198 | } catch (sysrepo::sysrepo_exception& ex) { |
| 199 | reportErrors(); |
| 200 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void SysrepoAccess::deletePresenceContainer(const std::string& path) |
| 204 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 205 | try { |
| 206 | m_session->delete_item(path.c_str()); |
| 207 | } catch (sysrepo::sysrepo_exception& ex) { |
| 208 | reportErrors(); |
| 209 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 210 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 211 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 212 | void SysrepoAccess::createListInstance(const std::string& path) |
| 213 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 214 | try { |
| 215 | m_session->set_item(path.c_str()); |
| 216 | } catch (sysrepo::sysrepo_exception& ex) { |
| 217 | reportErrors(); |
| 218 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void SysrepoAccess::deleteListInstance(const std::string& path) |
| 222 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 223 | try { |
| 224 | m_session->delete_item(path.c_str()); |
| 225 | } catch (sysrepo::sysrepo_exception& ex) { |
| 226 | reportErrors(); |
| 227 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 230 | void SysrepoAccess::commitChanges() |
| 231 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 232 | try { |
| 233 | m_session->commit(); |
| 234 | } catch (sysrepo::sysrepo_exception& ex) { |
| 235 | reportErrors(); |
| 236 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 237 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 238 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 239 | void SysrepoAccess::discardChanges() |
| 240 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 241 | try { |
| 242 | m_session->discard_changes(); |
| 243 | } catch (sysrepo::sysrepo_exception& ex) { |
| 244 | reportErrors(); |
| 245 | } |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 248 | DatastoreAccess::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át | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 267 | std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule) |
| 268 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 269 | std::string schema; |
| 270 | try { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 271 | schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 272 | } catch (sysrepo::sysrepo_exception& ex) { |
| 273 | reportErrors(); |
| 274 | } |
| 275 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 276 | if (schema.empty()) |
| 277 | throw std::runtime_error(std::string("Module ") + module + " not available"); |
| 278 | |
| 279 | return schema; |
| 280 | } |
| 281 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 282 | std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas() |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 283 | { |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 284 | std::vector<sysrepo::S_Yang_Schema> res; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 285 | 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át | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 291 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 292 | auto schema = schemas->schema(i); |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 293 | res.push_back(schema); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 294 | } |
| 295 | return res; |
| 296 | } |
| 297 | |
| 298 | std::shared_ptr<Schema> SysrepoAccess::schema() |
| 299 | { |
| 300 | return m_schema; |
| 301 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 302 | |
| 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 | } |