blob: 07949584e2caf81836b366ca77f5dbb0c9ace39e [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>
6#include <memory>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <vector>
11
12struct nc_session;
13
14namespace libyang {
15class Data_Node;
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010016class Context;
Václav Kubernátc31bd602019-03-07 11:44:48 +010017}
18
19namespace libnetconf {
Václav Kubernátd860d982021-01-08 13:46:02 +010020enum class NmdaDatastore {
21 Startup,
22 Running,
23 Operational
24};
Václav Kubernátc31bd602019-03-07 11:44:48 +010025namespace client {
26
27class ReportedError : public std::runtime_error {
28public:
29 ReportedError(const std::string& what);
30 ~ReportedError() override;
31};
32
Jan Kundráte0154282020-01-22 19:43:30 +010033using KbdInteractiveCb = std::function<std::string(const std::string&, const std::string&, const std::string&, bool)>;
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010034using LogCb = std::function<void(NC_VERB_LEVEL, const char*)>;
35
36void setLogLevel(NC_VERB_LEVEL level);
37void setLogCallback(const LogCb& callback);
Jan Kundráte0154282020-01-22 19:43:30 +010038
Václav Kubernátc31bd602019-03-07 11:44:48 +010039class Session {
40public:
41 Session(struct nc_session* session);
42 ~Session();
Václav Kubernátc31bd602019-03-07 11:44:48 +010043 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);
Jan Kundráte0154282020-01-22 19:43:30 +010044 static std::unique_ptr<Session> connectKbdInteractive(const std::string& host, const uint16_t port, const std::string& user, const KbdInteractiveCb& callback);
Václav Kubernátc31bd602019-03-07 11:44:48 +010045 static std::unique_ptr<Session> connectSocket(const std::string& path);
Václav Kubernát7a9463f2020-10-30 08:57:59 +010046 static std::unique_ptr<Session> connectFd(const int source, const int sink);
Václav Kubernát59e4ee42020-07-08 17:32:45 +020047 [[nodiscard]] std::vector<std::string_view> capabilities() const;
Václav Kubernátc31bd602019-03-07 11:44:48 +010048 std::shared_ptr<libyang::Data_Node> getConfig(const NC_DATASTORE datastore,
49 const std::optional<const std::string> filter = std::nullopt); // TODO: arguments...
Jan Kundrát9138b162020-01-30 09:27:51 +010050 std::shared_ptr<libyang::Data_Node> get(const std::optional<std::string>& filter = std::nullopt);
Václav Kubernátd860d982021-01-08 13:46:02 +010051 std::shared_ptr<libyang::Data_Node> getData(const NmdaDatastore datastore, const std::optional<std::string>& filter = std::nullopt);
Václav Kubernátc31bd602019-03-07 11:44:48 +010052 std::string getSchema(const std::string_view identifier, const std::optional<std::string_view> version);
53 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);
58 void copyConfigFromString(const NC_DATASTORE target, const std::string& data);
Václav Kubernáta8789602020-07-20 15:18:19 +020059 std::shared_ptr<libyang::Data_Node> rpc_or_action(const std::string& xmlData);
Václav Kubernát7160a132020-04-03 02:11:01 +020060 void copyConfig(const NC_DATASTORE source, const NC_DATASTORE destination);
Václav Kubernátc31bd602019-03-07 11:44:48 +010061 void commit();
62 void discard();
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010063
64 std::shared_ptr<libyang::Context> libyangContext();
Václav Kubernátc31bd602019-03-07 11:44:48 +010065 struct nc_session* session_internal(); // FIXME: remove me
66protected:
67 struct nc_session* m_session;
68};
69}
70}