Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <libyang/Libyang.hpp> |
| 9 | #include <libyang/Tree_Data.hpp> |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 10 | #include "libyang_utils.hpp" |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 11 | #include "netconf-client.h" |
| 12 | #include "netconf_access.hpp" |
| 13 | #include "utils.hpp" |
| 14 | #include "yang_schema.hpp" |
| 15 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 16 | NetconfAccess::~NetconfAccess() = default; |
| 17 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame^] | 18 | DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 19 | { |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame^] | 20 | Tree res; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 21 | |
| 22 | // This is very similar to the fillMap lambda in SysrepoAccess, however, |
| 23 | // Sysrepo returns a weird array-like structure, while libnetconf |
| 24 | // returns libyang::Data_Node |
| 25 | auto fillMap = [&res](auto items) { |
| 26 | for (const auto& it : items) { |
| 27 | if (!it) |
| 28 | continue; |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 29 | if (it->schema()->nodetype() == LYS_LIST) { |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 30 | res.emplace(it->path(), special_{SpecialValue::List}); |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 31 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 32 | if (it->schema()->nodetype() == LYS_LEAF) { |
| 33 | libyang::Data_Node_Leaf_List leaf(it); |
| 34 | res.emplace(leaf.path(), leafValueFromValue(leaf.value(), leaf.leaf_type()->base())); |
| 35 | } |
| 36 | } |
| 37 | }; |
| 38 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 39 | auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt); |
| 40 | |
| 41 | if (config) { |
| 42 | for (auto it : config->tree_for()) { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 43 | fillMap(it->tree_dfs()); |
| 44 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 45 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 46 | return res; |
| 47 | } |
| 48 | |
| 49 | void NetconfAccess::datastoreInit() |
| 50 | { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 51 | m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule, const char* submoduleRevision) { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 52 | return fetchSchema(moduleName, |
| 53 | revision ? std::optional{revision} : std::nullopt, |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 54 | submodule ? std::optional{submodule} : std::nullopt, |
| 55 | submoduleRevision ? std::optional{submoduleRevision} : std::nullopt); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 56 | }); |
| 57 | |
| 58 | for (const auto& it : listImplementedSchemas()) { |
| 59 | m_schema->loadModule(it); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey) |
| 64 | : m_schema(new YangSchema()) |
| 65 | { |
| 66 | m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey); |
| 67 | datastoreInit(); |
| 68 | } |
| 69 | |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 70 | NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session) |
| 71 | : m_session(std::move(session)) |
| 72 | , m_schema(new YangSchema()) |
| 73 | { |
| 74 | datastoreInit(); |
| 75 | } |
| 76 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 77 | NetconfAccess::NetconfAccess(const std::string& socketPath) |
| 78 | : m_schema(new YangSchema()) |
| 79 | { |
| 80 | m_session = libnetconf::client::Session::connectSocket(socketPath); |
| 81 | datastoreInit(); |
| 82 | } |
| 83 | |
| 84 | void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 85 | { |
| 86 | auto node = m_schema->dataNodeFromPath(path, leafDataToString(value)); |
| 87 | doEditFromDataNode(node); |
| 88 | } |
| 89 | |
| 90 | void NetconfAccess::createPresenceContainer(const std::string& path) |
| 91 | { |
| 92 | auto node = m_schema->dataNodeFromPath(path); |
| 93 | doEditFromDataNode(node); |
| 94 | } |
| 95 | |
| 96 | void NetconfAccess::deletePresenceContainer(const std::string& path) |
| 97 | { |
| 98 | auto node = m_schema->dataNodeFromPath(path); |
| 99 | node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 100 | doEditFromDataNode(node); |
| 101 | } |
| 102 | |
| 103 | void NetconfAccess::createListInstance(const std::string& path) |
| 104 | { |
| 105 | auto node = m_schema->dataNodeFromPath(path); |
| 106 | doEditFromDataNode(node); |
| 107 | } |
| 108 | |
| 109 | void NetconfAccess::deleteListInstance(const std::string& path) |
| 110 | { |
| 111 | auto node = m_schema->dataNodeFromPath(path); |
| 112 | node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 113 | doEditFromDataNode(node); |
| 114 | } |
| 115 | |
| 116 | void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode) |
| 117 | { |
| 118 | auto data = dataNode->print_mem(LYD_XML, 0); |
| 119 | m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data); |
| 120 | } |
| 121 | |
| 122 | void NetconfAccess::commitChanges() |
| 123 | { |
| 124 | m_session->commit(); |
| 125 | } |
| 126 | |
| 127 | void NetconfAccess::discardChanges() |
| 128 | { |
| 129 | m_session->discard(); |
| 130 | } |
| 131 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 132 | std::string NetconfAccess::fetchSchema(const std::string_view module, const |
| 133 | std::optional<std::string_view> revision, const |
| 134 | std::optional<std::string_view> submodule, const |
| 135 | std::optional<std::string_view> submoduleRevision) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 136 | { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 137 | if (submodule) { |
| 138 | return m_session->getSchema(*submodule, submoduleRevision); |
| 139 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 140 | return m_session->getSchema(module, revision); |
| 141 | } |
| 142 | |
| 143 | std::vector<std::string> NetconfAccess::listImplementedSchemas() |
| 144 | { |
| 145 | auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas"); |
| 146 | auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier"); |
| 147 | |
| 148 | std::vector<std::string> res; |
| 149 | for (auto it : set->data()) { |
| 150 | if (it->schema()->nodetype() == LYS_LEAF) { |
| 151 | libyang::Data_Node_Leaf_List leaf(it); |
| 152 | res.push_back(leaf.value_str()); |
| 153 | } |
| 154 | } |
| 155 | return res; |
| 156 | } |
| 157 | |
| 158 | std::shared_ptr<Schema> NetconfAccess::schema() |
| 159 | { |
| 160 | return m_schema; |
| 161 | } |