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 | } |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 48 | case SR_BINARY_T: |
| 49 | return binary_{value->data()->get_binary()}; |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 50 | case SR_LEAF_EMPTY_T: |
| 51 | return empty_{}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 52 | case SR_DECIMAL64_T: |
| 53 | return value->data()->get_decimal64(); |
| 54 | case SR_CONTAINER_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 55 | return special_{SpecialValue::Container}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 56 | case SR_CONTAINER_PRESENCE_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 57 | return special_{SpecialValue::PresenceContainer}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 58 | case SR_LIST_T: |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 59 | return special_{SpecialValue::List}; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 60 | default: // TODO: implement all types |
| 61 | return value->val_to_string(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 62 | } |
| 63 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 64 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 65 | struct valFromValue : boost::static_visitor<sysrepo::S_Val> { |
| 66 | sysrepo::S_Val operator()(const enum_& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 67 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 68 | 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] | 69 | } |
| 70 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 71 | sysrepo::S_Val operator()(const binary_& value) const |
| 72 | { |
| 73 | return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T); |
| 74 | } |
| 75 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 76 | sysrepo::S_Val operator()(const empty_) const |
| 77 | { |
| 78 | return std::make_shared<sysrepo::Val>(nullptr, SR_LEAF_EMPTY_T); |
| 79 | } |
| 80 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 81 | sysrepo::S_Val operator()(const identityRef_& value) const |
| 82 | { |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 83 | 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] | 84 | return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T); |
| 85 | } |
| 86 | |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 87 | sysrepo::S_Val operator()(const special_& value) const |
| 88 | { |
| 89 | throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value)); |
| 90 | } |
| 91 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 92 | sysrepo::S_Val operator()(const std::string& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 93 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 94 | return std::make_shared<sysrepo::Val>(value.c_str()); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Jan Kundrát | bd17836 | 2019-02-05 19:00:04 +0100 | [diff] [blame] | 97 | template <typename T> |
| 98 | sysrepo::S_Val operator()(const T& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 99 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 100 | return std::make_shared<sysrepo::Val>(value); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 104 | struct updateSrValFromValue : boost::static_visitor<void> { |
| 105 | std::string xpath; |
| 106 | sysrepo::S_Val v; |
| 107 | updateSrValFromValue(const std::string& xpath, sysrepo::S_Val v) |
| 108 | : xpath(xpath) |
| 109 | , v(v) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | void operator()(const enum_& value) const |
| 114 | { |
| 115 | v->set(xpath.c_str(), value.m_value.c_str(), SR_ENUM_T); |
| 116 | } |
| 117 | |
| 118 | void operator()(const binary_& value) const |
| 119 | { |
| 120 | v->set(xpath.c_str(), value.m_value.c_str(), SR_BINARY_T); |
| 121 | } |
| 122 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 123 | void operator()(const empty_) const |
| 124 | { |
| 125 | v->set(xpath.c_str(), nullptr, SR_LEAF_EMPTY_T); |
| 126 | } |
| 127 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 128 | void operator()(const identityRef_& value) const |
| 129 | { |
| 130 | v->set(xpath.c_str(), (value.m_prefix.value().m_name + ":" + value.m_value).c_str(), SR_IDENTITYREF_T); |
| 131 | } |
| 132 | |
| 133 | void operator()(const special_& value) const |
| 134 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 135 | switch (value.m_value) { |
| 136 | case SpecialValue::PresenceContainer: |
| 137 | v->set(xpath.c_str(), nullptr, SR_CONTAINER_PRESENCE_T); |
| 138 | break; |
| 139 | case SpecialValue::List: |
| 140 | v->set(xpath.c_str(), nullptr, SR_LIST_T); |
| 141 | break; |
| 142 | default: |
| 143 | throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value)); |
| 144 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void operator()(const std::string& value) const |
| 148 | { |
| 149 | v->set(xpath.c_str(), value.c_str(), SR_STRING_T); |
| 150 | } |
| 151 | |
| 152 | template <typename T> |
| 153 | void operator()(const T value) const |
| 154 | { |
| 155 | v->set(xpath.c_str(), value); |
| 156 | } |
| 157 | }; |
| 158 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 159 | SysrepoAccess::~SysrepoAccess() = default; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 160 | |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 161 | sr_datastore_t toSrDatastore(Datastore datastore) |
| 162 | { |
| 163 | switch (datastore) { |
| 164 | case Datastore::Running: |
| 165 | return SR_DS_RUNNING; |
| 166 | case Datastore::Startup: |
| 167 | return SR_DS_STARTUP; |
| 168 | } |
| 169 | __builtin_unreachable(); |
| 170 | } |
| 171 | |
| 172 | SysrepoAccess::SysrepoAccess(const std::string& appname, const Datastore datastore) |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 173 | : m_connection(new sysrepo::Connection(appname.c_str())) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 174 | , m_schema(new YangSchema()) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 175 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 176 | try { |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 177 | 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] | 178 | } catch (sysrepo::sysrepo_exception& ex) { |
| 179 | reportErrors(); |
| 180 | } |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 181 | |
| 182 | // If fetching a submodule, sysrepo::Session::get_schema will determine the revision from the main module. |
| 183 | // That's why submoduleRevision is ignored. |
| 184 | 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] | 185 | return fetchSchema(moduleName, revision, submodule); |
| 186 | }); |
| 187 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 188 | for (const auto& it : listSchemas()) { |
| 189 | if (it->implemented()) { |
| 190 | m_schema->loadModule(it->module_name()); |
| 191 | for (unsigned int i = 0; i < it->enabled_feature_cnt(); i++) { |
| 192 | m_schema->enableFeature(it->module_name(), it->enabled_features(i)); |
| 193 | } |
| 194 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 195 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Václav Kubernát | d628291 | 2020-06-23 14:49:34 +0200 | [diff] [blame] | 198 | DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 199 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 200 | using namespace std::string_literals; |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 201 | Tree res; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 202 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 203 | auto fillMap = [this, &res](auto items) { |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 204 | if (!items) { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 205 | return; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 206 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 207 | for (unsigned int i = 0; i < items->val_cnt(); i++) { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 208 | auto value = leafValueFromVal(items->val(i)); |
Václav Kubernát | e811bfa | 2020-05-29 02:25:20 +0200 | [diff] [blame] | 209 | if (m_schema->nodeType(items->val(i)->xpath()) == yang::NodeTypes::LeafList) { |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 210 | res.emplace_back(items->val(i)->xpath(), special_{SpecialValue::LeafList}); |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 211 | std::string leafListPath = items->val(i)->xpath(); |
| 212 | while (i < items->val_cnt() && leafListPath == items->val(i)->xpath()) { |
| 213 | auto leafListValue = leafDataToString(leafValueFromVal(items->val(i))); |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 214 | res.emplace_back(items->val(i)->xpath() + "[.="s + escapeListKeyString(leafListValue) + "]", leafListValue); |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 215 | i++; |
| 216 | } |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 217 | } else { |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 218 | res.emplace_back(items->val(i)->xpath(), value); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 219 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 220 | } |
| 221 | }; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 222 | |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 223 | try { |
| 224 | if (path == "/") { |
| 225 | // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas |
| 226 | auto schemas = m_session->list_schemas(); |
| 227 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 228 | fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str())); |
| 229 | } |
| 230 | } else { |
| 231 | fillMap(m_session->get_items((path + "//.").c_str())); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 232 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 233 | } catch (sysrepo::sysrepo_exception& ex) { |
| 234 | reportErrors(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 235 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 236 | return res; |
| 237 | } |
| 238 | |
| 239 | void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 240 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 241 | try { |
| 242 | m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value)); |
| 243 | } catch (sysrepo::sysrepo_exception& ex) { |
| 244 | reportErrors(); |
| 245 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 248 | void SysrepoAccess::createItem(const std::string& path) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 249 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 250 | try { |
| 251 | m_session->set_item(path.c_str()); |
| 252 | } catch (sysrepo::sysrepo_exception& ex) { |
| 253 | reportErrors(); |
| 254 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 257 | void SysrepoAccess::deleteItem(const std::string& path) |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 258 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 259 | try { |
| 260 | m_session->delete_item(path.c_str()); |
| 261 | } catch (sysrepo::sysrepo_exception& ex) { |
| 262 | reportErrors(); |
| 263 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 266 | struct impl_toSrMoveOp { |
| 267 | sr_move_position_t operator()(yang::move::Absolute& absolute) |
| 268 | { |
| 269 | return absolute == yang::move::Absolute::Begin ? SR_MOVE_FIRST : SR_MOVE_LAST; |
| 270 | } |
| 271 | sr_move_position_t operator()(yang::move::Relative& relative) |
| 272 | { |
| 273 | return relative.m_position == yang::move::Relative::Position::After ? SR_MOVE_AFTER : SR_MOVE_BEFORE; |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | sr_move_position_t toSrMoveOp(std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 278 | { |
| 279 | return std::visit(impl_toSrMoveOp{}, move); |
| 280 | } |
| 281 | |
| 282 | void SysrepoAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 283 | { |
| 284 | std::string destPathStr; |
| 285 | if (std::holds_alternative<yang::move::Relative>(move)) { |
| 286 | auto relative = std::get<yang::move::Relative>(move); |
| 287 | if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) { |
| 288 | destPathStr = stripLeafListValueFromPath(source) + "[.='" + leafDataToString(relative.m_path.at(".")) + "']"; |
| 289 | } else { |
Václav Kubernát | 2c4778b | 2020-06-18 11:55:20 +0200 | [diff] [blame] | 290 | destPathStr = stripLastListInstanceFromPath(source) + instanceToString(relative.m_path, m_schema->dataNodeFromPath(source)->node_module()->name()); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | m_session->move_item(source.c_str(), toSrMoveOp(move), destPathStr.c_str()); |
| 294 | } |
| 295 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 296 | void SysrepoAccess::commitChanges() |
| 297 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 298 | try { |
| 299 | m_session->commit(); |
| 300 | } catch (sysrepo::sysrepo_exception& ex) { |
| 301 | reportErrors(); |
| 302 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 303 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 304 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 305 | void SysrepoAccess::discardChanges() |
| 306 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 307 | try { |
| 308 | m_session->discard_changes(); |
| 309 | } catch (sysrepo::sysrepo_exception& ex) { |
| 310 | reportErrors(); |
| 311 | } |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame^] | 314 | namespace { |
| 315 | std::shared_ptr<sysrepo::Vals> toSrVals(const std::string& path, const DatastoreAccess::Tree& input) |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 316 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame^] | 317 | auto res = std::make_shared<sysrepo::Vals>(input.size()); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 318 | { |
| 319 | size_t i = 0; |
| 320 | for (const auto& [k, v] : input) { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame^] | 321 | boost::apply_visitor(updateSrValFromValue(joinPaths(path, k), res->val(i)), v); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 322 | ++i; |
| 323 | } |
| 324 | } |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame^] | 325 | return res; |
| 326 | } |
| 327 | |
| 328 | DatastoreAccess::Tree toTree(const std::string& path, const std::shared_ptr<sysrepo::Vals>& output) |
| 329 | { |
| 330 | DatastoreAccess::Tree res; |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 331 | for (size_t i = 0; i < output->val_cnt(); ++i) { |
| 332 | const auto& v = output->val(i); |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 333 | res.emplace_back(std::string(v->xpath()).substr(joinPaths(path, "/").size()), leafValueFromVal(v)); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 334 | } |
| 335 | return res; |
| 336 | } |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame^] | 337 | } |
| 338 | |
| 339 | DatastoreAccess::Tree SysrepoAccess::executeRpc(const std::string &path, const Tree &input) |
| 340 | { |
| 341 | auto srInput = toSrVals(path, input); |
| 342 | auto output = m_session->rpc_send(path.c_str(), srInput); |
| 343 | return toTree(path, output); |
| 344 | } |
| 345 | |
| 346 | DatastoreAccess::Tree SysrepoAccess::executeAction(const std::string& path, const Tree& input) |
| 347 | { |
| 348 | auto srInput = toSrVals(path, input); |
| 349 | auto output = m_session->action_send(path.c_str(), srInput); |
| 350 | return toTree(path, output); |
| 351 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 352 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 353 | void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination) |
| 354 | { |
| 355 | m_session->copy_config(nullptr, toSrDatastore(source), toSrDatastore(destination)); |
| 356 | if (destination == Datastore::Running) { |
| 357 | m_session->refresh(); |
| 358 | } |
| 359 | } |
| 360 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 361 | std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule) |
| 362 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 363 | std::string schema; |
| 364 | try { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 365 | 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] | 366 | } catch (sysrepo::sysrepo_exception& ex) { |
| 367 | reportErrors(); |
| 368 | } |
| 369 | |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 370 | if (schema.empty()) { |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 371 | throw std::runtime_error(std::string("Module ") + module + " not available"); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 372 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 373 | |
| 374 | return schema; |
| 375 | } |
| 376 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 377 | std::vector<std::shared_ptr<sysrepo::Yang_Schema>> SysrepoAccess::listSchemas() |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 378 | { |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 379 | std::vector<sysrepo::S_Yang_Schema> res; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 380 | std::shared_ptr<sysrepo::Yang_Schemas> schemas; |
| 381 | try { |
| 382 | schemas = m_session->list_schemas(); |
| 383 | } catch (sysrepo::sysrepo_exception& ex) { |
| 384 | reportErrors(); |
| 385 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 386 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 387 | auto schema = schemas->schema(i); |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 388 | res.emplace_back(schema); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 389 | } |
| 390 | return res; |
| 391 | } |
| 392 | |
| 393 | std::shared_ptr<Schema> SysrepoAccess::schema() |
| 394 | { |
| 395 | return m_schema; |
| 396 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 397 | |
Václav Kubernát | d628291 | 2020-06-23 14:49:34 +0200 | [diff] [blame] | 398 | [[noreturn]] void SysrepoAccess::reportErrors() const |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 399 | { |
| 400 | // I only use get_last_errors to get error info, since the error code from |
| 401 | // sysrepo_exception doesn't really give any meaningful information. For |
| 402 | // example an "invalid argument" error could mean a node isn't enabled, or |
| 403 | // it could mean something totally different and there is no documentation |
| 404 | // for that, so it's better to just use the message sysrepo gives me. |
| 405 | auto srErrors = m_session->get_last_errors(); |
| 406 | std::vector<DatastoreError> res; |
| 407 | |
| 408 | for (size_t i = 0; i < srErrors->error_cnt(); i++) { |
| 409 | auto error = srErrors->error(i); |
| 410 | res.emplace_back(error->message(), error->xpath() ? std::optional<std::string>{error->xpath()} : std::nullopt); |
| 411 | } |
| 412 | |
| 413 | throw DatastoreException(res); |
| 414 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 415 | |
| 416 | std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path) |
| 417 | { |
| 418 | std::vector<ListInstance> res; |
| 419 | auto lists = getItems(path); |
| 420 | |
| 421 | decltype(lists) instances; |
| 422 | auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin()); |
| 423 | std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck=wantedTree->schema()->path()](const auto& item) { |
| 424 | // This filters out non-instances. |
| 425 | if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) { |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance... |
| 430 | auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin()); |
| 431 | // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested. |
| 432 | return instanceTree->schema()->path() == pathToCheck; |
| 433 | }); |
| 434 | |
| 435 | // If there are no instances, then just return |
| 436 | if (instances.empty()) { |
| 437 | return res; |
| 438 | } |
| 439 | |
| 440 | // I need to find out which keys does the list have. To do that, I create a |
| 441 | // tree from the first instance. This is gives me some top level node, |
| 442 | // which will be our list in case out list is a top-level node. In case it |
| 443 | // isn't, we have call find_path on the top level node. After that, I just |
| 444 | // retrieve the keys. |
| 445 | auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first); |
| 446 | auto list = *(topLevelTree->find_path(path.c_str())->data().begin()); |
| 447 | auto keys = libyang::Schema_Node_List{list->schema()}.keys(); |
| 448 | |
| 449 | // Creating a full tree at the same time from the values sysrepo gives me |
| 450 | // would be a pain (and after sysrepo switches to libyang meaningless), so |
| 451 | // I just use this algorithm to create data nodes one by one and get the |
| 452 | // key values from them. |
| 453 | for (const auto& instance : instances) { |
| 454 | auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin()); |
| 455 | ListInstance instanceRes; |
| 456 | for (const auto& key : keys) { |
| 457 | auto vec = wantedList->find_path(key->name())->data(); |
| 458 | auto leaf = libyang::Data_Node_Leaf_List{*(vec.begin())}; |
| 459 | instanceRes.emplace(key->name(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base())); |
| 460 | } |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 461 | res.emplace_back(instanceRes); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return res; |
| 465 | } |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 466 | |
| 467 | std::string SysrepoAccess::dump(const DataFormat format) const |
| 468 | { |
| 469 | std::shared_ptr<libyang::Data_Node> root; |
| 470 | auto input = getItems("/"); |
| 471 | if (input.empty()) { |
| 472 | return ""; |
| 473 | } |
| 474 | for (const auto& [k, v] : input) { |
| 475 | if (v.type() == typeid(special_) && boost::get<special_>(v).m_value != SpecialValue::PresenceContainer) { |
| 476 | continue; |
| 477 | } |
| 478 | if (!root) { |
| 479 | root = m_schema->dataNodeFromPath(k, leafDataToString(v)); |
| 480 | } else { |
| 481 | // Using UPDATE here, because in multi-key list, all of the keys get created with the first key (because they are encoded in the path) |
| 482 | // and libyang complains if the node already exists. |
| 483 | root->new_path(nullptr, k.c_str(), leafDataToString(v).c_str(), LYD_ANYDATA_CONSTSTRING, LYD_PATH_OPT_UPDATE); |
| 484 | } |
| 485 | } |
| 486 | return root->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT); |
| 487 | } |