blob: 5a16cf2493b7804218024df3c88409eb8685325c [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 {
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010015class Context;
Václav Kubernátcfdb9222021-07-07 22:36:24 +020016class DataNode;
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,
Václav Kubernát1fcfa9c2021-02-22 03:22:08 +010023 Candidate,
Václav Kubernátd860d982021-01-08 13:46:02 +010024 Operational
25};
Václav Kubernátc31bd602019-03-07 11:44:48 +010026namespace client {
27
28class ReportedError : public std::runtime_error {
29public:
30 ReportedError(const std::string& what);
31 ~ReportedError() override;
32};
33
Jan Kundráte0154282020-01-22 19:43:30 +010034using KbdInteractiveCb = std::function<std::string(const std::string&, const std::string&, const std::string&, bool)>;
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010035using LogCb = std::function<void(NC_VERB_LEVEL, const char*)>;
36
37void setLogLevel(NC_VERB_LEVEL level);
38void setLogCallback(const LogCb& callback);
Jan Kundráte0154282020-01-22 19:43:30 +010039
Václav Kubernátc31bd602019-03-07 11:44:48 +010040class Session {
41public:
42 Session(struct nc_session* session);
43 ~Session();
Václav Kubernátc31bd602019-03-07 11:44:48 +010044 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 +010045 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 +010046 static std::unique_ptr<Session> connectSocket(const std::string& path);
Václav Kubernát7a9463f2020-10-30 08:57:59 +010047 static std::unique_ptr<Session> connectFd(const int source, const int sink);
Václav Kubernát59e4ee42020-07-08 17:32:45 +020048 [[nodiscard]] std::vector<std::string_view> capabilities() const;
Václav Kubernátcfdb9222021-07-07 22:36:24 +020049 std::optional<libyang::DataNode> get(const std::optional<std::string>& filter = std::nullopt);
50 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 +010051 void editConfig(const NC_DATASTORE datastore,
52 const NC_RPC_EDIT_DFLTOP defaultOperation,
53 const NC_RPC_EDIT_TESTOPT testOption,
54 const NC_RPC_EDIT_ERROPT errorOption,
55 const std::string& data);
Václav Kubernát1fcfa9c2021-02-22 03:22:08 +010056 void editData(const NmdaDatastore datastore, const std::string& data);
Václav Kubernátc31bd602019-03-07 11:44:48 +010057 void copyConfigFromString(const NC_DATASTORE target, const std::string& data);
Václav Kubernátcfdb9222021-07-07 22:36:24 +020058 std::optional<libyang::DataNode> rpc_or_action(const std::string& xmlData);
Václav Kubernát7160a132020-04-03 02:11:01 +020059 void copyConfig(const NC_DATASTORE source, const NC_DATASTORE destination);
Václav Kubernátc31bd602019-03-07 11:44:48 +010060 void commit();
61 void discard();
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010062
Václav Kubernátcfdb9222021-07-07 22:36:24 +020063 libyang::Context libyangContext();
Václav Kubernátc31bd602019-03-07 11:44:48 +010064 struct nc_session* session_internal(); // FIXME: remove me
65protected:
66 struct nc_session* m_session;
67};
68}
69}