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> |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 12 | #include <sstream> |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 13 | #include <sysrepo-cpp/Session.hpp> |
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 | struct valFromValue : boost::static_visitor<sysrepo::S_Val> { |
| 22 | sysrepo::S_Val operator()(const enum_& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 23 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 24 | 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] | 25 | } |
| 26 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 27 | sysrepo::S_Val operator()(const binary_& value) const |
| 28 | { |
| 29 | return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T); |
| 30 | } |
| 31 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 32 | sysrepo::S_Val operator()(const empty_) const |
| 33 | { |
| 34 | return std::make_shared<sysrepo::Val>(nullptr, SR_LEAF_EMPTY_T); |
| 35 | } |
| 36 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 37 | sysrepo::S_Val operator()(const identityRef_& value) const |
| 38 | { |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 39 | 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] | 40 | return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T); |
| 41 | } |
| 42 | |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 43 | sysrepo::S_Val operator()(const special_& value) const |
| 44 | { |
| 45 | throw std::runtime_error("Tried constructing S_Val from a " + specialValueToString(value)); |
| 46 | } |
| 47 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 48 | sysrepo::S_Val operator()(const std::string& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 49 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 50 | return std::make_shared<sysrepo::Val>(value.c_str()); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 53 | sysrepo::S_Val operator()(const bits_& value) const |
| 54 | { |
| 55 | std::stringstream ss; |
| 56 | std::copy(value.m_bits.begin(), value.m_bits.end(), std::experimental::make_ostream_joiner(ss, " ")); |
| 57 | return std::make_shared<sysrepo::Val>(ss.str().c_str(), SR_BITS_T); |
| 58 | } |
| 59 | |
Jan Kundrát | bd17836 | 2019-02-05 19:00:04 +0100 | [diff] [blame] | 60 | template <typename T> |
| 61 | sysrepo::S_Val operator()(const T& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 62 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 63 | return std::make_shared<sysrepo::Val>(value); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 64 | } |
| 65 | }; |
| 66 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 67 | SysrepoAccess::~SysrepoAccess() = default; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 68 | |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 69 | sr_datastore_t toSrDatastore(Datastore datastore) |
| 70 | { |
| 71 | switch (datastore) { |
| 72 | case Datastore::Running: |
| 73 | return SR_DS_RUNNING; |
| 74 | case Datastore::Startup: |
| 75 | return SR_DS_STARTUP; |
| 76 | } |
| 77 | __builtin_unreachable(); |
| 78 | } |
| 79 | |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 80 | SysrepoAccess::SysrepoAccess() |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 81 | : m_connection(std::make_shared<sysrepo::Connection>()) |
| 82 | , m_session(std::make_shared<sysrepo::Session>(m_connection)) |
| 83 | , m_schema(std::make_shared<YangSchema>(m_session->get_context())) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 84 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 85 | try { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 86 | m_session = std::make_shared<sysrepo::Session>(m_connection); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 87 | } catch (sysrepo::sysrepo_exception& ex) { |
| 88 | reportErrors(); |
| 89 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 92 | namespace { |
| 93 | auto targetToDs_get(const DatastoreTarget target) |
| 94 | { |
| 95 | switch (target) { |
| 96 | case DatastoreTarget::Operational: |
| 97 | return SR_DS_OPERATIONAL; |
| 98 | case DatastoreTarget::Running: |
| 99 | return SR_DS_RUNNING; |
| 100 | case DatastoreTarget::Startup: |
| 101 | return SR_DS_STARTUP; |
| 102 | } |
| 103 | |
| 104 | __builtin_unreachable(); |
| 105 | } |
| 106 | |
| 107 | auto targetToDs_set(const DatastoreTarget target) |
| 108 | { |
| 109 | switch (target) { |
| 110 | case DatastoreTarget::Operational: |
| 111 | case DatastoreTarget::Running: |
| 112 | // TODO: Doing candidate here doesn't work, why? |
| 113 | return SR_DS_RUNNING; |
| 114 | case DatastoreTarget::Startup: |
| 115 | return SR_DS_STARTUP; |
| 116 | } |
| 117 | |
| 118 | __builtin_unreachable(); |
| 119 | } |
| 120 | } |
| 121 | |
Václav Kubernát | d628291 | 2020-06-23 14:49:34 +0200 | [diff] [blame] | 122 | DatastoreAccess::Tree SysrepoAccess::getItems(const std::string& path) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 123 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 124 | using namespace std::string_literals; |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 125 | Tree res; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 126 | |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 127 | try { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 128 | m_session->session_switch_ds(targetToDs_get(m_target)); |
Václav Kubernát | f90a0b5 | 2020-11-06 05:53:03 +0100 | [diff] [blame] | 129 | auto config = m_session->get_data(((path == "/") ? "/*" : path).c_str()); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 130 | if (config) { |
| 131 | lyNodesToTree(res, config->tree_for()); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 132 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 133 | } catch (sysrepo::sysrepo_exception& ex) { |
| 134 | reportErrors(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 135 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 136 | return res; |
| 137 | } |
| 138 | |
| 139 | void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 140 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 141 | try { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 142 | m_session->session_switch_ds(targetToDs_set(m_target)); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 143 | m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value)); |
| 144 | } catch (sysrepo::sysrepo_exception& ex) { |
| 145 | reportErrors(); |
| 146 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 149 | void SysrepoAccess::createItem(const std::string& path) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 150 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 151 | try { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 152 | m_session->session_switch_ds(targetToDs_set(m_target)); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 153 | m_session->set_item(path.c_str()); |
| 154 | } catch (sysrepo::sysrepo_exception& ex) { |
| 155 | reportErrors(); |
| 156 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 159 | void SysrepoAccess::deleteItem(const std::string& path) |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 160 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 161 | try { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 162 | // Have to use SR_EDIT_ISOLATE, because deleting something that's been set without committing is not supported |
| 163 | // https://github.com/sysrepo/sysrepo/issues/1967#issuecomment-625085090 |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 164 | m_session->session_switch_ds(targetToDs_set(m_target)); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 165 | m_session->delete_item(path.c_str(), SR_EDIT_ISOLATE); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 166 | } catch (sysrepo::sysrepo_exception& ex) { |
| 167 | reportErrors(); |
| 168 | } |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 171 | struct impl_toSrMoveOp { |
| 172 | sr_move_position_t operator()(yang::move::Absolute& absolute) |
| 173 | { |
| 174 | return absolute == yang::move::Absolute::Begin ? SR_MOVE_FIRST : SR_MOVE_LAST; |
| 175 | } |
| 176 | sr_move_position_t operator()(yang::move::Relative& relative) |
| 177 | { |
| 178 | return relative.m_position == yang::move::Relative::Position::After ? SR_MOVE_AFTER : SR_MOVE_BEFORE; |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | sr_move_position_t toSrMoveOp(std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 183 | { |
| 184 | return std::visit(impl_toSrMoveOp{}, move); |
| 185 | } |
| 186 | |
| 187 | void SysrepoAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 188 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 189 | std::string destination; |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 190 | if (std::holds_alternative<yang::move::Relative>(move)) { |
| 191 | auto relative = std::get<yang::move::Relative>(move); |
| 192 | if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 193 | destination = leafDataToString(relative.m_path.at(".")); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 194 | } else { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 195 | destination = instanceToString(relative.m_path); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 196 | } |
| 197 | } |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 198 | m_session->session_switch_ds(targetToDs_set(m_target)); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 199 | 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] | 200 | } |
| 201 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 202 | void SysrepoAccess::commitChanges() |
| 203 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 204 | try { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 205 | m_session->session_switch_ds(targetToDs_set(m_target)); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 206 | m_session->apply_changes(OPERATION_TIMEOUT_MS, 1); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 207 | } catch (sysrepo::sysrepo_exception& ex) { |
| 208 | reportErrors(); |
| 209 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 210 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 211 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 212 | void SysrepoAccess::discardChanges() |
| 213 | { |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 214 | try { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 215 | m_session->session_switch_ds(targetToDs_set(m_target)); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 216 | m_session->discard_changes(); |
| 217 | } catch (sysrepo::sysrepo_exception& ex) { |
| 218 | reportErrors(); |
| 219 | } |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 222 | DatastoreAccess::Tree SysrepoAccess::execute(const std::string& path, const Tree& input) |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 223 | { |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 224 | auto inputNode = treeToRpcInput(m_session->get_context(), path, input); |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 225 | m_session->session_switch_ds(targetToDs_set(m_target)); |
Václav Kubernát | 4077613 | 2021-02-03 08:47:33 +0100 | [diff] [blame] | 226 | auto output = m_session->rpc_send(inputNode); |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 227 | return rpcOutputToTree(path, output); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 228 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 229 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 230 | void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination) |
| 231 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 232 | m_session->session_switch_ds(toSrDatastore(destination)); |
| 233 | m_session->copy_config(toSrDatastore(source), nullptr, OPERATION_TIMEOUT_MS, 1); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | std::shared_ptr<Schema> SysrepoAccess::schema() |
| 237 | { |
| 238 | return m_schema; |
| 239 | } |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 240 | |
Václav Kubernát | d628291 | 2020-06-23 14:49:34 +0200 | [diff] [blame] | 241 | [[noreturn]] void SysrepoAccess::reportErrors() const |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 242 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 243 | // 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] | 244 | // sysrepo_exception doesn't really give any meaningful information. For |
| 245 | // example an "invalid argument" error could mean a node isn't enabled, or |
| 246 | // it could mean something totally different and there is no documentation |
| 247 | // 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] | 248 | auto srErrors = m_session->get_error(); |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 249 | std::vector<DatastoreError> res; |
| 250 | |
| 251 | for (size_t i = 0; i < srErrors->error_cnt(); i++) { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 252 | 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] | 253 | } |
| 254 | |
| 255 | throw DatastoreException(res); |
| 256 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 257 | |
| 258 | std::vector<ListInstance> SysrepoAccess::listInstances(const std::string& path) |
| 259 | { |
| 260 | std::vector<ListInstance> res; |
| 261 | auto lists = getItems(path); |
| 262 | |
| 263 | decltype(lists) instances; |
| 264 | auto wantedTree = *(m_schema->dataNodeFromPath(path)->find_path(path.c_str())->data().begin()); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 265 | std::copy_if(lists.begin(), lists.end(), std::inserter(instances, instances.end()), [this, pathToCheck = wantedTree->schema()->path()](const auto& item) { |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 266 | // This filters out non-instances. |
| 267 | if (item.second.type() != typeid(special_) || boost::get<special_>(item.second).m_value != SpecialValue::List) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | // Now, getItems is recursive: it gives everything including nested lists. So I try create a tree from the instance... |
| 272 | auto instanceTree = *(m_schema->dataNodeFromPath(item.first)->find_path(item.first.c_str())->data().begin()); |
| 273 | // And then check if its schema path matches the list we actually want. This filters out lists which are not the ones I requested. |
| 274 | return instanceTree->schema()->path() == pathToCheck; |
| 275 | }); |
| 276 | |
| 277 | // If there are no instances, then just return |
| 278 | if (instances.empty()) { |
| 279 | return res; |
| 280 | } |
| 281 | |
| 282 | // I need to find out which keys does the list have. To do that, I create a |
| 283 | // tree from the first instance. This is gives me some top level node, |
| 284 | // which will be our list in case out list is a top-level node. In case it |
| 285 | // isn't, we have call find_path on the top level node. After that, I just |
| 286 | // retrieve the keys. |
| 287 | auto topLevelTree = m_schema->dataNodeFromPath(instances.begin()->first); |
| 288 | auto list = *(topLevelTree->find_path(path.c_str())->data().begin()); |
| 289 | auto keys = libyang::Schema_Node_List{list->schema()}.keys(); |
| 290 | |
| 291 | // Creating a full tree at the same time from the values sysrepo gives me |
| 292 | // would be a pain (and after sysrepo switches to libyang meaningless), so |
| 293 | // I just use this algorithm to create data nodes one by one and get the |
| 294 | // key values from them. |
| 295 | for (const auto& instance : instances) { |
| 296 | auto wantedList = *(m_schema->dataNodeFromPath(instance.first)->find_path(path.c_str())->data().begin()); |
| 297 | ListInstance instanceRes; |
| 298 | for (const auto& key : keys) { |
| 299 | auto vec = wantedList->find_path(key->name())->data(); |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 300 | auto leaf = std::make_shared<libyang::Data_Node_Leaf_List>(*(vec.begin())); |
| 301 | instanceRes.emplace(key->name(), leafValueFromNode(leaf)); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 302 | } |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 303 | res.emplace_back(instanceRes); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return res; |
| 307 | } |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 308 | |
| 309 | std::string SysrepoAccess::dump(const DataFormat format) const |
| 310 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 311 | auto root = m_session->get_data("/*"); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 312 | return root->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT); |
| 313 | } |