blob: 839c52826cfda61ba0ec0c77dbc8651d35cf3001 [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,
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átc31bd602019-03-07 11:44:48 +010049 std::shared_ptr<libyang::Data_Node> getConfig(const NC_DATASTORE datastore,
50 const std::optional<const std::string> filter = std::nullopt); // TODO: arguments...
Jan Kundrát9138b162020-01-30 09:27:51 +010051 std::shared_ptr<libyang::Data_Node> get(const std::optional<std::string>& filter = std::nullopt);
Václav Kubernátd860d982021-01-08 13:46:02 +010052 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 +010053 std::string getSchema(const std::string_view identifier, const std::optional<std::string_view> version);
54 void editConfig(const NC_DATASTORE datastore,
55 const NC_RPC_EDIT_DFLTOP defaultOperation,
56 const NC_RPC_EDIT_TESTOPT testOption,
57 const NC_RPC_EDIT_ERROPT errorOption,
58 const std::string& data);
Václav Kubernát1fcfa9c2021-02-22 03:22:08 +010059 void editData(const NmdaDatastore datastore, const std::string& data);
Václav Kubernátc31bd602019-03-07 11:44:48 +010060 void copyConfigFromString(const NC_DATASTORE target, const std::string& data);
Václav Kubernáta8789602020-07-20 15:18:19 +020061 std::shared_ptr<libyang::Data_Node> rpc_or_action(const std::string& xmlData);
Václav Kubernát7160a132020-04-03 02:11:01 +020062 void copyConfig(const NC_DATASTORE source, const NC_DATASTORE destination);
Václav Kubernátc31bd602019-03-07 11:44:48 +010063 void commit();
64 void discard();
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010065
66 std::shared_ptr<libyang::Context> libyangContext();
Václav Kubernátc31bd602019-03-07 11:44:48 +010067 struct nc_session* session_internal(); // FIXME: remove me
68protected:
69 struct nc_session* m_session;
70};
71}
72}