blob: 719a21d3c10fab0907d653682512258b0ceab286 [file] [log] [blame]
Václav Kubernátc31bd602019-03-07 11:44:48 +01001#pragma once
2
Jan Kundráte0154282020-01-22 19:43:30 +01003#include <functional>
Václav Kubernáte2e15ee2020-02-05 17:38:13 +01004#include <libnetconf2/log.h>
Václav Kubernátc31bd602019-03-07 11:44:48 +01005#include <libnetconf2/messages_client.h>
Václav Kubernáted4e3782022-03-02 23:57:33 +01006#include <libyang-cpp/Context.hpp>
Václav Kubernátc31bd602019-03-07 11:44:48 +01007#include <memory>
8#include <optional>
9#include <string>
10#include <string_view>
11#include <vector>
12
13struct nc_session;
Václav Kubernáted4e3782022-03-02 23:57:33 +010014struct ly_ctx;
Václav Kubernátc31bd602019-03-07 11:44:48 +010015
16namespace libyang {
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010017class Context;
Václav Kubernátcfdb9222021-07-07 22:36:24 +020018class DataNode;
Václav Kubernátc31bd602019-03-07 11:44:48 +010019}
20
21namespace libnetconf {
Václav Kubernátd860d982021-01-08 13:46:02 +010022enum class NmdaDatastore {
23 Startup,
24 Running,
Václav Kubernát1fcfa9c2021-02-22 03:22:08 +010025 Candidate,
Václav Kubernátd860d982021-01-08 13:46:02 +010026 Operational
27};
Václav Kubernátc31bd602019-03-07 11:44:48 +010028namespace client {
29
30class ReportedError : public std::runtime_error {
31public:
32 ReportedError(const std::string& what);
33 ~ReportedError() override;
34};
35
Jan Kundráte0154282020-01-22 19:43:30 +010036using KbdInteractiveCb = std::function<std::string(const std::string&, const std::string&, const std::string&, bool)>;
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010037using LogCb = std::function<void(NC_VERB_LEVEL, const char*)>;
38
39void setLogLevel(NC_VERB_LEVEL level);
40void setLogCallback(const LogCb& callback);
Jan Kundráte0154282020-01-22 19:43:30 +010041
Václav Kubernátc31bd602019-03-07 11:44:48 +010042class Session {
43public:
44 Session(struct nc_session* session);
45 ~Session();
Václav Kubernáted4e3782022-03-02 23:57:33 +010046 static std::unique_ptr<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 = std::nullopt);
47 static std::unique_ptr<Session> connectKbdInteractive(const std::string& host, const uint16_t port, const std::string& user, const KbdInteractiveCb& callback, std::optional<libyang::Context> ctx = std::nullopt);
48 static std::unique_ptr<Session> connectSocket(const std::string& path, std::optional<libyang::Context> ctx = std::nullopt);
49 static std::unique_ptr<Session> connectFd(const int source, const int sink, std::optional<libyang::Context> ctx = std::nullopt);
Václav Kubernát59e4ee42020-07-08 17:32:45 +020050 [[nodiscard]] std::vector<std::string_view> capabilities() const;
Václav Kubernátcfdb9222021-07-07 22:36:24 +020051 std::optional<libyang::DataNode> get(const std::optional<std::string>& filter = std::nullopt);
52 std::optional<libyang::DataNode> getData(const NmdaDatastore datastore, const std::optional<std::string>& filter = std::nullopt);
Václav Kubernátc31bd602019-03-07 11:44:48 +010053 void editConfig(const NC_DATASTORE datastore,
54 const NC_RPC_EDIT_DFLTOP defaultOperation,
55 const NC_RPC_EDIT_TESTOPT testOption,
56 const NC_RPC_EDIT_ERROPT errorOption,
57 const std::string& data);
Václav Kubernát1fcfa9c2021-02-22 03:22:08 +010058 void editData(const NmdaDatastore datastore, const std::string& data);
Václav Kubernátc31bd602019-03-07 11:44:48 +010059 void copyConfigFromString(const NC_DATASTORE target, const std::string& data);
Václav Kubernátcfdb9222021-07-07 22:36:24 +020060 std::optional<libyang::DataNode> rpc_or_action(const std::string& xmlData);
Václav Kubernát7160a132020-04-03 02:11:01 +020061 void copyConfig(const NC_DATASTORE source, const NC_DATASTORE destination);
Václav Kubernátc31bd602019-03-07 11:44:48 +010062 void commit();
63 void discard();
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010064
Václav Kubernátcfdb9222021-07-07 22:36:24 +020065 libyang::Context libyangContext();
Václav Kubernátc31bd602019-03-07 11:44:48 +010066 struct nc_session* session_internal(); // FIXME: remove me
67protected:
68 struct nc_session* m_session;
69};
70}
71}