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" |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 11 | #include "yang_schema.hpp" |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 12 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 13 | leaf_data_ leafValueFromVal(const sysrepo::S_Val& value) |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 14 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 15 | using namespace std::string_literals; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 16 | switch (value->type()) { |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 17 | 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át | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 25 | case SR_INT32_T: |
| 26 | return value->data()->get_int32(); |
| 27 | case SR_UINT32_T: |
| 28 | return value->data()->get_uint32(); |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 29 | case SR_INT64_T: |
| 30 | return value->data()->get_int64(); |
| 31 | case SR_UINT64_T: |
| 32 | return value->data()->get_uint64(); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 33 | 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át | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 38 | return enum_{std::string(value->data()->get_enum())}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 39 | case SR_DECIMAL64_T: |
| 40 | return value->data()->get_decimal64(); |
| 41 | case SR_CONTAINER_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 42 | return special_{SpecialValue::Container}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 43 | case SR_CONTAINER_PRESENCE_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 44 | return special_{SpecialValue::PresenceContainer}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 45 | case SR_LIST_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 46 | return special_{SpecialValue::List}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 47 | default: // TODO: implement all types |
| 48 | return value->val_to_string(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 49 | } |
| 50 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 51 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 52 | struct valFromValue : boost::static_visitor<sysrepo::S_Val> { |
| 53 | sysrepo::S_Val operator()(const enum_& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 54 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 55 | 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] | 56 | } |
| 57 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 58 | 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át | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 63 | 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 | |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 69 | sysrepo::S_Val operator()(const special_& value) const |
| 70 | { |
| 71 | throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value)); |
| 72 | } |
| 73 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 74 | sysrepo::S_Val operator()(const std::string& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 75 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 76 | return std::make_shared<sysrepo::Val>(value.c_str()); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Jan Kundrát | bd17836 | 2019-02-05 19:00:04 +0100 | [diff] [blame] | 79 | template <typename T> |
| 80 | sysrepo::S_Val operator()(const T& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 81 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 82 | return std::make_shared<sysrepo::Val>(value); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 83 | } |
| 84 | }; |
| 85 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 86 | SysrepoAccess::~SysrepoAccess() = default; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 87 | |
| 88 | SysrepoAccess::SysrepoAccess(const std::string& appname) |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 89 | : m_connection(new sysrepo::Connection(appname.c_str())) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 90 | , m_schema(new YangSchema()) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 91 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 92 | try { |
| 93 | m_session = std::make_shared<sysrepo::Session>(m_connection); |
| 94 | } catch (sysrepo::sysrepo_exception& ex) { |
| 95 | reportErrors(); |
| 96 | } |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 97 | |
| 98 | // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module. |
| 99 | // That's why submoduleRevision is ignored. |
| 100 | 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] | 101 | return fetchSchema(moduleName, revision, submodule); |
| 102 | }); |
| 103 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 104 | for (const auto& it : listSchemas()) { |
| 105 | if (it->implemented()) { |
| 106 | m_schema->loadModule(it->module_name()); |
| 107 | for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) { |
| 108 | m_schema->enableFeature(it->module_name(), it->enabled_features(i)); |
| 109 | } |
| 110 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 111 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path) |
| 115 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 116 | using namespace std::string_literals; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 117 | std::map<std::string, leaf_data_> res; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 118 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 119 | auto fillMap = [&res](auto items) { |
| 120 | if (!items) |
| 121 | return; |
| 122 | for (unsigned int i = 0; i < items->val_cnt(); i++) { |
| 123 | res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i))); |
| 124 | } |
| 125 | }; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 126 | |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 127 | try { |
| 128 | if (path == "/") { |
| 129 | // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas |
| 130 | auto schemas = m_session->list_schemas(); |
| 131 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 132 | fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str())); |
| 133 | } |
| 134 | } else { |
| 135 | fillMap(m_session->get_items((path + "//.").c_str())); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 136 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 137 | } catch (sysrepo::sysrepo_exception& ex) { |
| 138 | reportErrors(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 139 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 140 | return res; |
| 141 | } |
| 142 | |
| 143 | void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 144 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 145 | try { |
| 146 | m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value)); |
| 147 | } catch (sysrepo::sysrepo_exception& ex) { |
| 148 | reportErrors(); |
| 149 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void SysrepoAccess::createPresenceContainer(const std::string& path) |
| 153 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 154 | try { |
| 155 | m_session->set_item(path.c_str()); |
| 156 | } catch (sysrepo::sysrepo_exception& ex) { |
| 157 | reportErrors(); |
| 158 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void SysrepoAccess::deletePresenceContainer(const std::string& path) |
| 162 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 163 | try { |
| 164 | m_session->delete_item(path.c_str()); |
| 165 | } catch (sysrepo::sysrepo_exception& ex) { |
| 166 | reportErrors(); |
| 167 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 168 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 169 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 170 | void SysrepoAccess::createListInstance(const std::string& path) |
| 171 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 172 | try { |
| 173 | m_session->set_item(path.c_str()); |
| 174 | } catch (sysrepo::sysrepo_exception& ex) { |
| 175 | reportErrors(); |
| 176 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void SysrepoAccess::deleteListInstance(const std::string& path) |
| 180 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 181 | try { |
| 182 | m_session->delete_item(path.c_str()); |
| 183 | } catch (sysrepo::sysrepo_exception& ex) { |
| 184 | reportErrors(); |
| 185 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 188 | void SysrepoAccess::commitChanges() |
| 189 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 190 | try { |
| 191 | m_session->commit(); |
| 192 | } catch (sysrepo::sysrepo_exception& ex) { |
| 193 | reportErrors(); |
| 194 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 195 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 196 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 197 | void SysrepoAccess::discardChanges() |
| 198 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 199 | try { |
| 200 | m_session->discard_changes(); |
| 201 | } catch (sysrepo::sysrepo_exception& ex) { |
| 202 | reportErrors(); |
| 203 | } |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 206 | std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule) |
| 207 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 208 | std::string schema; |
| 209 | try { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 210 | 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] | 211 | } catch (sysrepo::sysrepo_exception& ex) { |
| 212 | reportErrors(); |
| 213 | } |
| 214 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 215 | if (schema.empty()) |
| 216 | throw std::runtime_error(std::string("Module ") + module + " not available"); |
| 217 | |
| 218 | return schema; |
| 219 | } |
| 220 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 221 | std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas() |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 222 | { |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 223 | std::vector<sysrepo::S_Yang_Schema> res; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 224 | std::shared_ptr<sysrepo::Yang_Schemas> schemas; |
| 225 | try { |
| 226 | schemas = m_session->list_schemas(); |
| 227 | } catch (sysrepo::sysrepo_exception& ex) { |
| 228 | reportErrors(); |
| 229 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 230 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 231 | auto schema = schemas->schema(i); |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 232 | res.push_back(schema); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 233 | } |
| 234 | return res; |
| 235 | } |
| 236 | |
| 237 | std::shared_ptr<Schema> SysrepoAccess::schema() |
| 238 | { |
| 239 | return m_schema; |
| 240 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 241 | |
| 242 | [[noreturn]] void SysrepoAccess::reportErrors() |
| 243 | { |
| 244 | // I only use get_last_errors to get error info, since the error code from |
| 245 | // sysrepo_exception doesn't really give any meaningful information. For |
| 246 | // example an "invalid argument" error could mean a node isn't enabled, or |
| 247 | // it could mean something totally different and there is no documentation |
| 248 | // for that, so it's better to just use the message sysrepo gives me. |
| 249 | auto srErrors = m_session->get_last_errors(); |
| 250 | std::vector<DatastoreError> res; |
| 251 | |
| 252 | for (size_t i = 0; i < srErrors->error_cnt(); i++) { |
| 253 | auto error = srErrors->error(i); |
| 254 | res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt); |
| 255 | } |
| 256 | |
| 257 | throw DatastoreException(res); |
| 258 | } |