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 | |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 9 | #include <libyang/Tree_Data.hpp> |
| 10 | #include <libyang/Tree_Schema.hpp> |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 11 | #include <sysrepo-cpp/Session.hpp> |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 12 | #include "libyang_utils.hpp" |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 13 | #include "sysrepo_access.hpp" |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 14 | #include "utils.hpp" |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 15 | #include "yang_schema.hpp" |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 16 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 17 | leaf_data_ leafValueFromVal(const sysrepo::S_Val& value) |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 18 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 19 | using namespace std::string_literals; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 20 | switch (value->type()) { |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 21 | case SR_INT8_T: |
| 22 | return value->data()->get_int8(); |
| 23 | case SR_UINT8_T: |
| 24 | return value->data()->get_uint8(); |
| 25 | case SR_INT16_T: |
| 26 | return value->data()->get_int16(); |
| 27 | case SR_UINT16_T: |
| 28 | return value->data()->get_uint16(); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 29 | case SR_INT32_T: |
| 30 | return value->data()->get_int32(); |
| 31 | case SR_UINT32_T: |
| 32 | return value->data()->get_uint32(); |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 33 | case SR_INT64_T: |
| 34 | return value->data()->get_int64(); |
| 35 | case SR_UINT64_T: |
| 36 | return value->data()->get_uint64(); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 37 | case SR_BOOL_T: |
| 38 | return value->data()->get_bool(); |
| 39 | case SR_STRING_T: |
| 40 | return std::string(value->data()->get_string()); |
| 41 | case SR_ENUM_T: |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 42 | return enum_{std::string(value->data()->get_enum())}; |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame^] | 43 | case SR_IDENTITYREF_T: |
| 44 | { |
| 45 | auto pair = splitModuleNode(value->data()->get_identityref()); |
| 46 | return identityRef_{*pair.first, pair.second}; |
| 47 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 48 | case SR_DECIMAL64_T: |
| 49 | return value->data()->get_decimal64(); |
| 50 | case SR_CONTAINER_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 51 | return special_{SpecialValue::Container}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 52 | case SR_CONTAINER_PRESENCE_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 53 | return special_{SpecialValue::PresenceContainer}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 54 | case SR_LIST_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 55 | return special_{SpecialValue::List}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 56 | default: // TODO: implement all types |
| 57 | return value->val_to_string(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 58 | } |
| 59 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 60 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 61 | struct valFromValue : boost::static_visitor<sysrepo::S_Val> { |
| 62 | sysrepo::S_Val operator()(const enum_& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 63 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 64 | 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] | 65 | } |
| 66 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 67 | sysrepo::S_Val operator()(const binary_& value) const |
| 68 | { |
| 69 | return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T); |
| 70 | } |
| 71 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 72 | sysrepo::S_Val operator()(const identityRef_& value) const |
| 73 | { |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame^] | 74 | auto res = value.m_prefix ? (value.m_prefix.value().m_name + ":" + value.m_value) : value.m_value; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 75 | return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T); |
| 76 | } |
| 77 | |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 78 | sysrepo::S_Val operator()(const special_& value) const |
| 79 | { |
| 80 | throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value)); |
| 81 | } |
| 82 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 83 | sysrepo::S_Val operator()(const std::string& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 84 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 85 | return std::make_shared<sysrepo::Val>(value.c_str()); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Jan Kundrát | bd17836 | 2019-02-05 19:00:04 +0100 | [diff] [blame] | 88 | template <typename T> |
| 89 | sysrepo::S_Val operator()(const T& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 90 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 91 | return std::make_shared<sysrepo::Val>(value); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 95 | struct updateSrValFromValue : boost::static_visitor<void> { |
| 96 | std::string xpath; |
| 97 | sysrepo::S_Val v; |
| 98 | updateSrValFromValue(const std::string& xpath, sysrepo::S_Val v) |
| 99 | : xpath(xpath) |
| 100 | , v(v) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | void operator()(const enum_& value) const |
| 105 | { |
| 106 | v->set(xpath.c_str(), value.m_value.c_str(), SR_ENUM_T); |
| 107 | } |
| 108 | |
| 109 | void operator()(const binary_& value) const |
| 110 | { |
| 111 | v->set(xpath.c_str(), value.m_value.c_str(), SR_BINARY_T); |
| 112 | } |
| 113 | |
| 114 | void operator()(const identityRef_& value) const |
| 115 | { |
| 116 | v->set(xpath.c_str(), (value.m_prefix.value().m_name + ":" + value.m_value).c_str(), SR_IDENTITYREF_T); |
| 117 | } |
| 118 | |
| 119 | void operator()(const special_& value) const |
| 120 | { |
| 121 | throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value)); |
| 122 | } |
| 123 | |
| 124 | void operator()(const std::string& value) const |
| 125 | { |
| 126 | v->set(xpath.c_str(), value.c_str(), SR_STRING_T); |
| 127 | } |
| 128 | |
| 129 | template <typename T> |
| 130 | void operator()(const T value) const |
| 131 | { |
| 132 | v->set(xpath.c_str(), value); |
| 133 | } |
| 134 | }; |
| 135 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 136 | SysrepoAccess::~SysrepoAccess() = default; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 137 | |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 138 | sr_datastore_t toSrDatastore(Datastore datastore) |
| 139 | { |
| 140 | switch (datastore) { |
| 141 | case Datastore::Running: |
| 142 | return SR_DS_RUNNING; |
| 143 | case Datastore::Startup: |
| 144 | return SR_DS_STARTUP; |
| 145 | } |
| 146 | __builtin_unreachable(); |
| 147 | } |
| 148 | |
| 149 | SysrepoAccess::SysrepoAccess(const std::string& appname, const Datastore datastore) |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 150 | : m_connection(new sysrepo::Connection(appname.c_str())) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 151 | , m_schema(new YangSchema()) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 152 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 153 | try { |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 154 | m_session = std::make_shared<sysrepo::Session>(m_connection, toSrDatastore(datastore)); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 155 | } catch (sysrepo::sysrepo_exception& ex) { |
| 156 | reportErrors(); |
| 157 | } |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 158 | |
| 159 | // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module. |
| 160 | // That's why submoduleRevision is ignored. |
| 161 | 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] | 162 | return fetchSchema(moduleName, revision, submodule); |
| 163 | }); |
| 164 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 165 | for (const auto& it : listSchemas()) { |
| 166 | if (it->implemented()) { |
| 167 | m_schema->loadModule(it->module_name()); |
| 168 | for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) { |
| 169 | m_schema->enableFeature(it->module_name(), it->enabled_features(i)); |
| 170 | } |
| 171 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 172 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 175 | DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 176 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 177 | using namespace std::string_literals; |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 178 | Tree res; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 179 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 180 | auto fillMap = [&res](auto items) { |
| 181 | if (!items) |
| 182 | return; |
| 183 | for (unsigned int i = 0; i < items->val_cnt(); i++) { |
| 184 | res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i))); |
| 185 | } |
| 186 | }; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 187 | |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 188 | try { |
| 189 | if (path == "/") { |
| 190 | // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas |
| 191 | auto schemas = m_session->list_schemas(); |
| 192 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 193 | fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str())); |
| 194 | } |
| 195 | } else { |
| 196 | fillMap(m_session->get_items((path + "//.").c_str())); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 197 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 198 | } catch (sysrepo::sysrepo_exception& ex) { |
| 199 | reportErrors(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 200 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 201 | return res; |
| 202 | } |
| 203 | |
| 204 | void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 205 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 206 | try { |
| 207 | m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value)); |
| 208 | } catch (sysrepo::sysrepo_exception& ex) { |
| 209 | reportErrors(); |
| 210 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void SysrepoAccess::createPresenceContainer(const std::string& path) |
| 214 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 215 | try { |
| 216 | m_session->set_item(path.c_str()); |
| 217 | } catch (sysrepo::sysrepo_exception& ex) { |
| 218 | reportErrors(); |
| 219 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void SysrepoAccess::deletePresenceContainer(const std::string& path) |
| 223 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 224 | try { |
| 225 | m_session->delete_item(path.c_str()); |
| 226 | } catch (sysrepo::sysrepo_exception& ex) { |
| 227 | reportErrors(); |
| 228 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 229 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 230 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 231 | void SysrepoAccess::createListInstance(const std::string& path) |
| 232 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 233 | try { |
| 234 | m_session->set_item(path.c_str()); |
| 235 | } catch (sysrepo::sysrepo_exception& ex) { |
| 236 | reportErrors(); |
| 237 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void SysrepoAccess::deleteListInstance(const std::string& path) |
| 241 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 242 | try { |
| 243 | m_session->delete_item(path.c_str()); |
| 244 | } catch (sysrepo::sysrepo_exception& ex) { |
| 245 | reportErrors(); |
| 246 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 249 | void SysrepoAccess::commitChanges() |
| 250 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 251 | try { |
| 252 | m_session->commit(); |
| 253 | } catch (sysrepo::sysrepo_exception& ex) { |
| 254 | reportErrors(); |
| 255 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 256 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 257 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 258 | void SysrepoAccess::discardChanges() |
| 259 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 260 | try { |
| 261 | m_session->discard_changes(); |
| 262 | } catch (sysrepo::sysrepo_exception& ex) { |
| 263 | reportErrors(); |
| 264 | } |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 267 | DatastoreAccess::Tree SysrepoAccess::executeRpc(const std::string &path, const Tree &input) |
| 268 | { |
| 269 | auto srInput = std::make_shared<sysrepo::Vals>(input.size()); |
| 270 | { |
| 271 | size_t i = 0; |
| 272 | for (const auto& [k, v] : input) { |
| 273 | boost::apply_visitor(updateSrValFromValue(joinPaths(path, k), srInput->val(i)), v); |
| 274 | ++i; |
| 275 | } |
| 276 | } |
| 277 | auto output = m_session->rpc_send(path.c_str(), srInput); |
| 278 | Tree res; |
| 279 | for (size_t i = 0; i < output->val_cnt(); ++i) { |
| 280 | const auto& v = output->val(i); |
| 281 | res.emplace(std::string(v->xpath()).substr(joinPaths(path, "/").size()), leafValueFromVal(v)); |
| 282 | } |
| 283 | return res; |
| 284 | } |
| 285 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 286 | void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination) |
| 287 | { |
| 288 | m_session->copy_config(nullptr, toSrDatastore(source), toSrDatastore(destination)); |
| 289 | if (destination == Datastore::Running) { |
| 290 | m_session->refresh(); |
| 291 | } |
| 292 | } |
| 293 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 294 | std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule) |
| 295 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 296 | std::string schema; |
| 297 | try { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 298 | 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] | 299 | } catch (sysrepo::sysrepo_exception& ex) { |
| 300 | reportErrors(); |
| 301 | } |
| 302 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 303 | if (schema.empty()) |
| 304 | throw std::runtime_error(std::string("Module ") + module + " not available"); |
| 305 | |
| 306 | return schema; |
| 307 | } |
| 308 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 309 | std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas() |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 310 | { |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 311 | std::vector<sysrepo::S_Yang_Schema> res; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 312 | std::shared_ptr<sysrepo::Yang_Schemas> schemas; |
| 313 | try { |
| 314 | schemas = m_session->list_schemas(); |
| 315 | } catch (sysrepo::sysrepo_exception& ex) { |
| 316 | reportErrors(); |
| 317 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 318 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 319 | auto schema = schemas->schema(i); |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 320 | res.push_back(schema); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 321 | } |
| 322 | return res; |
| 323 | } |
| 324 | |
| 325 | std::shared_ptr<Schema> SysrepoAccess::schema() |
| 326 | { |
| 327 | return m_schema; |
| 328 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 329 | |
| 330 | [[noreturn]] void SysrepoAccess::reportErrors() |
| 331 | { |
| 332 | // I only use get_last_errors to get error info, since the error code from |
| 333 | // sysrepo_exception doesn't really give any meaningful information. For |
| 334 | // example an "invalid argument" error could mean a node isn't enabled, or |
| 335 | // it could mean something totally different and there is no documentation |
| 336 | // for that, so it's better to just use the message sysrepo gives me. |
| 337 | auto srErrors = m_session->get_last_errors(); |
| 338 | std::vector<DatastoreError> res; |
| 339 | |
| 340 | for (size_t i = 0; i < srErrors->error_cnt(); i++) { |
| 341 | auto error = srErrors->error(i); |
| 342 | res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt); |
| 343 | } |
| 344 | |
| 345 | throw DatastoreException(res); |
| 346 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 347 | |
| 348 | std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path) |
| 349 | { |
| 350 | std::vector<ListInstance> res; |
| 351 | auto lists = getItems(path); |
| 352 | |
| 353 | decltype(lists) instances; |
| 354 | auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin()); |
| 355 | std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck=wantedTree->schema()->path()](const auto& item) { |
| 356 | // This filters out non-instances. |
| 357 | if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance... |
| 362 | auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin()); |
| 363 | // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested. |
| 364 | return instanceTree->schema()->path() == pathToCheck; |
| 365 | }); |
| 366 | |
| 367 | // If there are no instances, then just return |
| 368 | if (instances.empty()) { |
| 369 | return res; |
| 370 | } |
| 371 | |
| 372 | // I need to find out which keys does the list have. To do that, I create a |
| 373 | // tree from the first instance. This is gives me some top level node, |
| 374 | // which will be our list in case out list is a top-level node. In case it |
| 375 | // isn't, we have call find_path on the top level node. After that, I just |
| 376 | // retrieve the keys. |
| 377 | auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first); |
| 378 | auto list = *(topLevelTree->find_path(path.c_str())->data().begin()); |
| 379 | auto keys = libyang::Schema_Node_List{list->schema()}.keys(); |
| 380 | |
| 381 | // Creating a full tree at the same time from the values sysrepo gives me |
| 382 | // would be a pain (and after sysrepo switches to libyang meaningless), so |
| 383 | // I just use this algorithm to create data nodes one by one and get the |
| 384 | // key values from them. |
| 385 | for (const auto& instance : instances) { |
| 386 | auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin()); |
| 387 | ListInstance instanceRes; |
| 388 | for (const auto& key : keys) { |
| 389 | auto vec = wantedList->find_path(key->name())->data(); |
| 390 | auto leaf = libyang::Data_Node_Leaf_List{*(vec.begin())}; |
| 391 | instanceRes.emplace(key->name(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base())); |
| 392 | } |
| 393 | res.push_back(instanceRes); |
| 394 | } |
| 395 | |
| 396 | return res; |
| 397 | } |