Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * Written by Jan Kundrát <jan.kundrat@cesnet.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
Jan Kundrát | e015428 | 2020-01-22 19:43:30 +0100 | [diff] [blame] | 9 | #include <cstring> |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 10 | #include <libyang-cpp/Context.hpp> |
| 11 | #include <libyang-cpp/DataNode.hpp> |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 12 | #include <mutex> |
| 13 | extern "C" { |
| 14 | #include <nc_client.h> |
| 15 | } |
| 16 | #include <sstream> |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 17 | #include "UniqueResource.hpp" |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 18 | #include "netconf-client.hpp" |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 19 | |
| 20 | namespace libnetconf { |
| 21 | |
| 22 | namespace impl { |
| 23 | |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 24 | static client::LogCb logCallback; |
| 25 | |
| 26 | static void logViaCallback(NC_VERB_LEVEL level, const char* message) |
| 27 | { |
| 28 | logCallback(level, message); |
| 29 | } |
| 30 | |
| 31 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 32 | /** @short Initialization of the libnetconf2 library client |
| 33 | |
| 34 | Just a safe wrapper over nc_client_init and nc_client_destroy, really. |
| 35 | */ |
| 36 | class ClientInit { |
| 37 | ClientInit() |
| 38 | { |
| 39 | nc_client_init(); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ~ClientInit() |
| 43 | { |
| 44 | nc_client_destroy(); |
| 45 | } |
| 46 | |
| 47 | public: |
| 48 | static ClientInit& instance() |
| 49 | { |
| 50 | static ClientInit lib; |
| 51 | return lib; |
| 52 | } |
| 53 | |
| 54 | ClientInit(const ClientInit&) = delete; |
| 55 | ClientInit(ClientInit&&) = delete; |
| 56 | ClientInit& operator=(const ClientInit&) = delete; |
| 57 | ClientInit& operator=(ClientInit&&) = delete; |
| 58 | }; |
| 59 | |
| 60 | static std::mutex clientOptions; |
| 61 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 62 | char* ssh_auth_interactive_cb(const char* auth_name, const char* instruction, const char* prompt, int echo, void* priv) |
Jan Kundrát | e015428 | 2020-01-22 19:43:30 +0100 | [diff] [blame] | 63 | { |
| 64 | const auto cb = static_cast<const client::KbdInteractiveCb*>(priv); |
| 65 | auto res = (*cb)(auth_name, instruction, prompt, echo); |
| 66 | return ::strdup(res.c_str()); |
| 67 | } |
| 68 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 69 | auto guarded(nc_rpc* ptr) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 70 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 71 | return std::unique_ptr<nc_rpc, decltype(&nc_rpc_free)>(ptr, nc_rpc_free); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 74 | namespace { |
| 75 | const auto getData_path = "/ietf-netconf-nmda:get-data/data"; |
| 76 | const auto get_path = "/ietf-netconf:get/data"; |
| 77 | } |
| 78 | |
| 79 | using managed_rpc = std::invoke_result_t<decltype(guarded), nc_rpc*>; |
| 80 | |
| 81 | std::optional<libyang::DataNode> do_rpc(client::Session* session, managed_rpc&& rpc, const char* dataIdentifier) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 82 | { |
| 83 | uint64_t msgid; |
| 84 | NC_MSG_TYPE msgtype; |
| 85 | |
| 86 | msgtype = nc_send_rpc(session->session_internal(), rpc.get(), 1000, &msgid); |
| 87 | if (msgtype == NC_MSG_ERROR) { |
| 88 | throw std::runtime_error{"Failed to send RPC"}; |
| 89 | } |
| 90 | if (msgtype == NC_MSG_WOULDBLOCK) { |
| 91 | throw std::runtime_error{"Timeout sending an RPC"}; |
| 92 | } |
| 93 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 94 | lyd_node* raw_reply; |
| 95 | lyd_node* envp; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 96 | while (true) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 97 | msgtype = nc_recv_reply(session->session_internal(), rpc.get(), msgid, 20000, &envp, &raw_reply); |
| 98 | auto replyInfo = libyang::wrapRawNode(envp); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 99 | |
| 100 | switch (msgtype) { |
| 101 | case NC_MSG_ERROR: |
| 102 | throw std::runtime_error{"Failed to receive an RPC reply"}; |
| 103 | case NC_MSG_WOULDBLOCK: |
| 104 | throw std::runtime_error{"Timed out waiting for RPC reply"}; |
| 105 | case NC_MSG_REPLY_ERR_MSGID: |
| 106 | throw std::runtime_error{"Received a wrong reply -- msgid mismatch"}; |
| 107 | case NC_MSG_NOTIF: |
| 108 | continue; |
| 109 | default: |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 110 | if (!raw_reply) { // <ok> reply, or empty data node, or error |
| 111 | std::string msg; |
| 112 | for (const auto& child : replyInfo.child()->siblings()) { |
| 113 | if (child.asOpaque().name().name == "rpc-error") { |
| 114 | for (const auto& error : child.childrenDfs()) { |
| 115 | if (error.asOpaque().name().name == "error-message") { |
| 116 | msg += "Error: "; |
| 117 | msg += error.asOpaque().value(); |
| 118 | } |
| 119 | |
| 120 | if (error.asOpaque().name().name == "error-path") { |
| 121 | msg += "Path: "; |
| 122 | msg += error.asOpaque().value(); |
| 123 | } |
| 124 | |
| 125 | if (error.asOpaque().name().name == "error-type") { |
| 126 | msg += "Type: "; |
| 127 | msg += error.asOpaque().value(); |
| 128 | } |
| 129 | |
| 130 | if (error.asOpaque().name().name == "error-tag") { |
| 131 | msg += "Tag: "; |
| 132 | msg += error.asOpaque().value(); |
| 133 | } |
| 134 | |
| 135 | if (error.asOpaque().name().name == "error-app-tag") { |
| 136 | msg += "App-tag: "; |
| 137 | msg += error.asOpaque().value(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | msg += "\n"; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (!msg.empty()) { |
| 146 | throw client::ReportedError{msg}; |
| 147 | } |
| 148 | |
| 149 | return std::nullopt; |
| 150 | } |
| 151 | auto wrapped = libyang::wrapRawNode(raw_reply); |
| 152 | |
| 153 | // If we have a dataIdentifier, then we'll need to look for it. |
| 154 | // Some operations don't have that, and then the result data are just the wrapped node. |
| 155 | if (!dataIdentifier) { |
| 156 | return wrapped; |
| 157 | } |
| 158 | |
| 159 | auto anydataValue = wrapped.findPath(dataIdentifier, libyang::OutputNodes::Yes)->asAny().releaseValue(); |
| 160 | |
| 161 | // If there's no anydata value, then that means we get empty (but valid) data. |
| 162 | if (!anydataValue) { |
| 163 | return std::nullopt; |
| 164 | } |
| 165 | |
| 166 | return std::get<libyang::DataNode>(*anydataValue); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | __builtin_unreachable(); |
| 170 | } |
| 171 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 172 | void do_rpc_ok(client::Session* session, managed_rpc&& rpc) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 173 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 174 | auto x = do_rpc(session, std::move(rpc), nullptr); |
Jan Kundrát | a2a4414 | 2020-01-24 01:31:54 +0100 | [diff] [blame] | 175 | if (x) { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 176 | throw std::runtime_error{"Unexpected DATA reply"}; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | namespace client { |
| 182 | |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 183 | void setLogLevel(NC_VERB_LEVEL level) |
| 184 | { |
| 185 | nc_verbosity(level); |
| 186 | } |
| 187 | |
| 188 | void setLogCallback(const client::LogCb& callback) |
| 189 | { |
| 190 | impl::logCallback = callback; |
| 191 | nc_set_print_clb(impl::logViaCallback); |
| 192 | } |
| 193 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 194 | struct nc_session* Session::session_internal() |
| 195 | { |
| 196 | return m_session; |
| 197 | } |
| 198 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 199 | libyang::Context Session::libyangContext() |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 200 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 201 | return libyang::createUnmanagedContext(const_cast<ly_ctx*>(nc_session_get_ctx(m_session)), nullptr); |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 204 | Session::Session(struct nc_session* session) |
| 205 | : m_session(session) |
| 206 | { |
| 207 | impl::ClientInit::instance(); |
| 208 | } |
| 209 | |
| 210 | Session::~Session() |
| 211 | { |
| 212 | ::nc_session_free(m_session, nullptr); |
| 213 | } |
| 214 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 215 | std::unique_ptr<Session> Session::connectPubkey(const std::string& host, const uint16_t port, const std::string& user, const std::string& pubPath, const std::string& privPath, std::optional<libyang::Context> ctx) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 216 | { |
| 217 | impl::ClientInit::instance(); |
| 218 | |
| 219 | { |
| 220 | // FIXME: this is still horribly not enough. libnetconf *must* provide us with something better. |
| 221 | std::lock_guard lk(impl::clientOptions); |
| 222 | nc_client_ssh_set_username(user.c_str()); |
| 223 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 5); |
| 224 | nc_client_ssh_add_keypair(pubPath.c_str(), privPath.c_str()); |
| 225 | } |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 226 | auto session = std::make_unique<Session>(nc_connect_ssh(host.c_str(), port, ctx ? libyang::retrieveContext(*ctx) : nullptr)); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 227 | if (!session->m_session) { |
| 228 | throw std::runtime_error{"nc_connect_ssh failed"}; |
| 229 | } |
| 230 | return session; |
| 231 | } |
| 232 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 233 | std::unique_ptr<Session> Session::connectKbdInteractive(const std::string& host, const uint16_t port, const std::string& user, const KbdInteractiveCb& callback, std::optional<libyang::Context> ctx) |
Jan Kundrát | e015428 | 2020-01-22 19:43:30 +0100 | [diff] [blame] | 234 | { |
| 235 | impl::ClientInit::instance(); |
| 236 | |
| 237 | std::lock_guard lk(impl::clientOptions); |
| 238 | auto cb_guard = make_unique_resource([user, &callback]() { |
| 239 | nc_client_ssh_set_username(user.c_str()); |
| 240 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, 5); |
| 241 | nc_client_ssh_set_auth_interactive_clb(impl::ssh_auth_interactive_cb, static_cast<void *>(&const_cast<KbdInteractiveCb&>(callback))); |
| 242 | }, []() { |
| 243 | nc_client_ssh_set_auth_interactive_clb(nullptr, nullptr); |
| 244 | nc_client_ssh_set_username(nullptr); |
| 245 | }); |
| 246 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 247 | auto session = std::make_unique<Session>(nc_connect_ssh(host.c_str(), port, ctx ? libyang::retrieveContext(*ctx) : nullptr)); |
Jan Kundrát | e015428 | 2020-01-22 19:43:30 +0100 | [diff] [blame] | 248 | if (!session->m_session) { |
| 249 | throw std::runtime_error{"nc_connect_ssh failed"}; |
| 250 | } |
| 251 | return session; |
| 252 | } |
| 253 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 254 | std::unique_ptr<Session> Session::connectFd(const int source, const int sink, std::optional<libyang::Context> ctx) |
Václav Kubernát | 7a9463f | 2020-10-30 08:57:59 +0100 | [diff] [blame] | 255 | { |
| 256 | impl::ClientInit::instance(); |
| 257 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 258 | auto session = std::make_unique<Session>(nc_connect_inout(source, sink, ctx ? libyang::retrieveContext(*ctx) : nullptr)); |
Václav Kubernát | 7a9463f | 2020-10-30 08:57:59 +0100 | [diff] [blame] | 259 | if (!session->m_session) { |
| 260 | throw std::runtime_error{"nc_connect_inout failed"}; |
| 261 | } |
| 262 | return session; |
| 263 | } |
| 264 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 265 | std::unique_ptr<Session> Session::connectSocket(const std::string& path, std::optional<libyang::Context> ctx) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 266 | { |
| 267 | impl::ClientInit::instance(); |
| 268 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame^] | 269 | auto session = std::make_unique<Session>(nc_connect_unix(path.c_str(), ctx ? libyang::retrieveContext(*ctx) : nullptr)); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 270 | if (!session->m_session) { |
| 271 | throw std::runtime_error{"nc_connect_unix failed"}; |
| 272 | } |
| 273 | return session; |
| 274 | } |
| 275 | |
| 276 | std::vector<std::string_view> Session::capabilities() const |
| 277 | { |
| 278 | std::vector<std::string_view> res; |
| 279 | auto caps = nc_session_get_cpblts(m_session); |
| 280 | while (*caps) { |
| 281 | res.emplace_back(*caps); |
| 282 | ++caps; |
| 283 | } |
| 284 | return res; |
| 285 | } |
| 286 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 287 | std::optional<libyang::DataNode> Session::get(const std::optional<std::string>& filter) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 288 | { |
Jan Kundrát | 9138b16 | 2020-01-30 09:27:51 +0100 | [diff] [blame] | 289 | auto rpc = impl::guarded(nc_rpc_get(filter ? filter->c_str() : nullptr, NC_WD_ALL, NC_PARAMTYPE_CONST)); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 290 | if (!rpc) { |
| 291 | throw std::runtime_error("Cannot create get RPC"); |
| 292 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 293 | return impl::do_rpc(this, std::move(rpc), impl::get_path); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Václav Kubernát | d860d98 | 2021-01-08 13:46:02 +0100 | [diff] [blame] | 296 | const char* datastoreToString(NmdaDatastore datastore) |
| 297 | { |
| 298 | switch (datastore) { |
| 299 | case NmdaDatastore::Startup: |
| 300 | return "ietf-datastores:startup"; |
| 301 | case NmdaDatastore::Running: |
| 302 | return "ietf-datastores:running"; |
Václav Kubernát | 1fcfa9c | 2021-02-22 03:22:08 +0100 | [diff] [blame] | 303 | case NmdaDatastore::Candidate: |
| 304 | return "ietf-datastores:candidate"; |
Václav Kubernát | d860d98 | 2021-01-08 13:46:02 +0100 | [diff] [blame] | 305 | case NmdaDatastore::Operational: |
| 306 | return "ietf-datastores:operational"; |
| 307 | } |
| 308 | __builtin_unreachable(); |
| 309 | } |
| 310 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 311 | std::optional<libyang::DataNode> Session::getData(const NmdaDatastore datastore, const std::optional<std::string>& filter) |
Václav Kubernát | d860d98 | 2021-01-08 13:46:02 +0100 | [diff] [blame] | 312 | { |
| 313 | auto rpc = impl::guarded(nc_rpc_getdata(datastoreToString(datastore), filter ? filter->c_str() : nullptr, nullptr, nullptr, 0, 0, 0, 0, NC_WD_ALL, NC_PARAMTYPE_CONST)); |
| 314 | if (!rpc) { |
| 315 | throw std::runtime_error("Cannot create get RPC"); |
| 316 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 317 | return impl::do_rpc(this, std::move(rpc), impl::getData_path); |
Václav Kubernát | d860d98 | 2021-01-08 13:46:02 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Václav Kubernát | 1fcfa9c | 2021-02-22 03:22:08 +0100 | [diff] [blame] | 320 | void Session::editData(const NmdaDatastore datastore, const std::string& data) |
| 321 | { |
| 322 | auto rpc = impl::guarded(nc_rpc_editdata(datastoreToString(datastore), NC_RPC_EDIT_DFLTOP_MERGE, data.c_str(), NC_PARAMTYPE_CONST)); |
| 323 | if (!rpc) { |
| 324 | throw std::runtime_error("Cannot create get RPC"); |
| 325 | } |
| 326 | return impl::do_rpc_ok(this, std::move(rpc)); |
| 327 | } |
| 328 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 329 | void Session::editConfig(const NC_DATASTORE datastore, |
| 330 | const NC_RPC_EDIT_DFLTOP defaultOperation, |
| 331 | const NC_RPC_EDIT_TESTOPT testOption, |
| 332 | const NC_RPC_EDIT_ERROPT errorOption, |
| 333 | const std::string& data) |
| 334 | { |
| 335 | auto rpc = impl::guarded(nc_rpc_edit(datastore, defaultOperation, testOption, errorOption, data.c_str(), NC_PARAMTYPE_CONST)); |
| 336 | if (!rpc) { |
| 337 | throw std::runtime_error("Cannot create edit-config RPC"); |
| 338 | } |
| 339 | impl::do_rpc_ok(this, std::move(rpc)); |
| 340 | } |
| 341 | |
| 342 | void Session::copyConfigFromString(const NC_DATASTORE target, const std::string& data) |
| 343 | { |
| 344 | auto rpc = impl::guarded(nc_rpc_copy(target, nullptr, target /* yeah, cannot be 0... */, data.c_str(), NC_WD_UNKNOWN, NC_PARAMTYPE_CONST)); |
| 345 | if (!rpc) { |
| 346 | throw std::runtime_error("Cannot create copy-config RPC"); |
| 347 | } |
| 348 | impl::do_rpc_ok(this, std::move(rpc)); |
| 349 | } |
| 350 | |
| 351 | void Session::commit() |
| 352 | { |
Jan Kundrát | 2400fff | 2021-01-19 09:44:59 +0100 | [diff] [blame] | 353 | auto rpc = impl::guarded(nc_rpc_commit(0, /* "Optional confirm timeout" how do you optional an uint32_t? */ 0, nullptr, nullptr, NC_PARAMTYPE_CONST)); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 354 | if (!rpc) { |
| 355 | throw std::runtime_error("Cannot create commit RPC"); |
| 356 | } |
| 357 | impl::do_rpc_ok(this, std::move(rpc)); |
| 358 | } |
| 359 | |
| 360 | void Session::discard() |
| 361 | { |
| 362 | auto rpc = impl::guarded(nc_rpc_discard()); |
| 363 | if (!rpc) { |
| 364 | throw std::runtime_error("Cannot create discard RPC"); |
| 365 | } |
| 366 | impl::do_rpc_ok(this, std::move(rpc)); |
| 367 | } |
| 368 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 369 | std::optional<libyang::DataNode> Session::rpc_or_action(const std::string& xmlData) |
Jan Kundrát | 59acc2f | 2020-01-23 15:12:59 +0100 | [diff] [blame] | 370 | { |
| 371 | auto rpc = impl::guarded(nc_rpc_act_generic_xml(xmlData.c_str(), NC_PARAMTYPE_CONST)); |
| 372 | if (!rpc) { |
| 373 | throw std::runtime_error("Cannot create generic RPC"); |
| 374 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 375 | |
| 376 | return impl::do_rpc(this, std::move(rpc), nullptr); |
Jan Kundrát | 59acc2f | 2020-01-23 15:12:59 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 379 | void Session::copyConfig(const NC_DATASTORE source, const NC_DATASTORE destination) |
| 380 | { |
| 381 | auto rpc = impl::guarded(nc_rpc_copy(destination, nullptr, source, nullptr, NC_WD_UNKNOWN, NC_PARAMTYPE_CONST)); |
| 382 | if (!rpc) { |
| 383 | throw std::runtime_error("Cannot create copy-config RPC"); |
| 384 | } |
| 385 | impl::do_rpc_ok(this, std::move(rpc)); |
| 386 | } |
| 387 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 388 | ReportedError::ReportedError(const std::string& what) |
| 389 | : std::runtime_error(what) |
| 390 | { |
| 391 | } |
| 392 | |
| 393 | ReportedError::~ReportedError() = default; |
| 394 | } |
| 395 | } |