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