blob: 27e54e2aa04c17323d248ad8bb220e6d29e23d6d [file] [log] [blame]
Václav Kubernátc31bd602019-03-07 11:44:48 +01001/*
2 * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubernat@cesnet.cz>
5 *
6*/
7
8#pragma once
9
Václav Kubernáted4e3782022-03-02 23:57:33 +010010#include <libyang-cpp/Context.hpp>
Václav Kubernátbde37ba2022-03-25 15:18:12 +010011#include <libnetconf2-cpp/Enum.hpp>
Václav Kubernátc31bd602019-03-07 11:44:48 +010012#include <string>
Václav Kubernátc31bd602019-03-07 11:44:48 +010013#include "datastore_access.hpp"
14
15/*! \class NetconfAccess
16 * \brief Implementation of DatastoreAccess for accessing a NETCONF server
17 */
18
Václav Kubernátf5f6d242022-04-07 01:06:19 +020019namespace libnetconf::client {
Václav Kubernátc31bd602019-03-07 11:44:48 +010020class Session;
21}
Václav Kubernátc31bd602019-03-07 11:44:48 +010022
Václav Kubernátc31bd602019-03-07 11:44:48 +010023class Schema;
24class YangSchema;
25
Václav Kubernátbde37ba2022-03-25 15:18:12 +010026using LogCb = std::function<void(libnetconf::LogLevel, const char*)>;
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010027
Václav Kubernátc31bd602019-03-07 11:44:48 +010028class NetconfAccess : public DatastoreAccess {
29public:
30 NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey);
31 NetconfAccess(const std::string& socketPath);
Václav Kubernát7a9463f2020-10-30 08:57:59 +010032 NetconfAccess(const int source, const int sink);
Jan Kundrátdab815e2020-01-22 19:44:11 +010033 NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session);
Václav Kubernátc31bd602019-03-07 11:44:48 +010034 ~NetconfAccess() override;
Václav Kubernát59e4ee42020-07-08 17:32:45 +020035 [[nodiscard]] Tree getItems(const std::string& path) const override;
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010036
Václav Kubernátbde37ba2022-03-25 15:18:12 +010037 static void setNcLogLevel(libnetconf::LogLevel level);
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010038 static void setNcLogCallback(const LogCb& callback);
Václav Kubernátc31bd602019-03-07 11:44:48 +010039 void setLeaf(const std::string& path, leaf_data_ value) override;
Jan Kundrátcbf288b2020-06-18 20:44:39 +020040 void createItem(const std::string& path) override;
41 void deleteItem(const std::string& path) override;
Václav Kubernátbf65dd72020-05-28 02:32:31 +020042 void moveItem(const std::string& path, std::variant<yang::move::Absolute, yang::move::Relative> move) override;
Václav Kubernátc31bd602019-03-07 11:44:48 +010043 void commitChanges() override;
44 void discardChanges() override;
Václav Kubernátb3960f82020-12-01 03:21:48 +010045 Tree execute(const std::string& path, const Tree& input) override;
Václav Kubernát7160a132020-04-03 02:11:01 +020046 void copyConfig(const Datastore source, const Datastore destination) override;
Václav Kubernátc31bd602019-03-07 11:44:48 +010047
48 std::shared_ptr<Schema> schema() override;
49
Václav Kubernát59e4ee42020-07-08 17:32:45 +020050 [[nodiscard]] std::string dump(const DataFormat format) const override;
Václav Kubernát70d7f7a2020-06-23 14:40:40 +020051
Václav Kubernátc31bd602019-03-07 11:44:48 +010052private:
Václav Kubernátc15fe822020-06-04 11:28:39 +020053 std::vector<ListInstance> listInstances(const std::string& path) override;
Václav Kubernátab612e92019-11-26 19:51:31 +010054
Václav Kubernátcfdb9222021-07-07 22:36:24 +020055 void doEditFromDataNode(libyang::DataNode dataNode);
Václav Kubernátc31bd602019-03-07 11:44:48 +010056
Václav Kubernát06b0f382021-10-04 11:20:47 +020057 void checkNMDA();
58
59 bool m_serverHasNMDA;
60
Václav Kubernáted4e3782022-03-02 23:57:33 +010061 libyang::Context m_context;
Václav Kubernátc31bd602019-03-07 11:44:48 +010062 std::unique_ptr<libnetconf::client::Session> m_session;
63 std::shared_ptr<YangSchema> m_schema;
64};