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 | |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // This is very similar to the fillMap lambda in SysrepoAccess, however, |
| 19 | // Sysrepo returns a weird array-like structure, while libnetconf |
| 20 | // returns libyang::Data_Node |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 21 | void fillMap(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix = std::nullopt) |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 22 | { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 23 | auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) { |
| 24 | return ignoredXPathPrefix ? path.substr(ignoredXPathPrefix->size()) : path; |
| 25 | }; |
| 26 | |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 27 | for (const auto& it : items) { |
| 28 | if (!it) |
| 29 | continue; |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 30 | if (it->schema()->nodetype() == LYS_CONTAINER) { |
| 31 | if (libyang::Schema_Node_Container{it->schema()}.presence()) { |
| 32 | // The fact that the container is included in the data tree |
| 33 | // means that it is present and I don't need to check any |
| 34 | // value. |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 35 | res.emplace(stripXPathPrefix(it->path()), special_{SpecialValue::PresenceContainer}); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 36 | } |
| 37 | } |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 38 | if (it->schema()->nodetype() == LYS_LIST) { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 39 | res.emplace(stripXPathPrefix(it->path()), special_{SpecialValue::List}); |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 40 | } |
| 41 | if (it->schema()->nodetype() == LYS_LEAF) { |
| 42 | libyang::Data_Node_Leaf_List leaf(it); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 43 | res.emplace(stripXPathPrefix(it->path()), leafValueFromValue(leaf.value(), leaf.leaf_type()->base())); |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 50 | NetconfAccess::~NetconfAccess() = default; |
| 51 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 52 | DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 53 | { |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 54 | Tree res; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 55 | auto config = m_session->getConfig(NC_DATASTORE_RUNNING, (path != "/") ? std::optional{path} : std::nullopt); |
| 56 | |
| 57 | if (config) { |
| 58 | for (auto it : config->tree_for()) { |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 59 | fillMap(res, it->tree_dfs()); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 60 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 61 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 62 | return res; |
| 63 | } |
| 64 | |
| 65 | void NetconfAccess::datastoreInit() |
| 66 | { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 67 | 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] | 68 | return fetchSchema(moduleName, |
| 69 | revision ? std::optional{revision} : std::nullopt, |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 70 | submodule ? std::optional{submodule} : std::nullopt, |
| 71 | submoduleRevision ? std::optional{submoduleRevision} : std::nullopt); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 72 | }); |
| 73 | |
| 74 | for (const auto& it : listImplementedSchemas()) { |
| 75 | m_schema->loadModule(it); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey) |
| 80 | : m_schema(new YangSchema()) |
| 81 | { |
| 82 | m_session = libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey); |
| 83 | datastoreInit(); |
| 84 | } |
| 85 | |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 86 | NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session) |
| 87 | : m_session(std::move(session)) |
| 88 | , m_schema(new YangSchema()) |
| 89 | { |
| 90 | datastoreInit(); |
| 91 | } |
| 92 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 93 | NetconfAccess::NetconfAccess(const std::string& socketPath) |
| 94 | : m_schema(new YangSchema()) |
| 95 | { |
| 96 | m_session = libnetconf::client::Session::connectSocket(socketPath); |
| 97 | datastoreInit(); |
| 98 | } |
| 99 | |
| 100 | void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 101 | { |
| 102 | auto node = m_schema->dataNodeFromPath(path, leafDataToString(value)); |
| 103 | doEditFromDataNode(node); |
| 104 | } |
| 105 | |
| 106 | void NetconfAccess::createPresenceContainer(const std::string& path) |
| 107 | { |
| 108 | auto node = m_schema->dataNodeFromPath(path); |
| 109 | doEditFromDataNode(node); |
| 110 | } |
| 111 | |
| 112 | void NetconfAccess::deletePresenceContainer(const std::string& path) |
| 113 | { |
| 114 | auto node = m_schema->dataNodeFromPath(path); |
| 115 | node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 116 | doEditFromDataNode(node); |
| 117 | } |
| 118 | |
| 119 | void NetconfAccess::createListInstance(const std::string& path) |
| 120 | { |
| 121 | auto node = m_schema->dataNodeFromPath(path); |
| 122 | doEditFromDataNode(node); |
| 123 | } |
| 124 | |
| 125 | void NetconfAccess::deleteListInstance(const std::string& path) |
| 126 | { |
| 127 | auto node = m_schema->dataNodeFromPath(path); |
| 128 | node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 129 | doEditFromDataNode(node); |
| 130 | } |
| 131 | |
| 132 | void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode) |
| 133 | { |
| 134 | auto data = dataNode->print_mem(LYD_XML, 0); |
| 135 | m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data); |
| 136 | } |
| 137 | |
| 138 | void NetconfAccess::commitChanges() |
| 139 | { |
| 140 | m_session->commit(); |
| 141 | } |
| 142 | |
| 143 | void NetconfAccess::discardChanges() |
| 144 | { |
| 145 | m_session->discard(); |
| 146 | } |
| 147 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 148 | DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input) |
| 149 | { |
| 150 | auto root = m_schema->dataNodeFromPath(path); |
| 151 | for (const auto& [k, v] : input) { |
| 152 | auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v)); |
| 153 | root->merge(node, 0); |
| 154 | } |
| 155 | auto data = root->print_mem(LYD_XML, 0); |
| 156 | |
| 157 | Tree res; |
| 158 | auto output = m_session->rpc(data); |
| 159 | if (output) { |
| 160 | for (auto it : output->tree_for()) { |
| 161 | fillMap(res, it->tree_dfs(), joinPaths(path, "/")); |
| 162 | } |
| 163 | } |
| 164 | return res; |
| 165 | } |
| 166 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 167 | std::string NetconfAccess::fetchSchema(const std::string_view module, const |
| 168 | std::optional<std::string_view> revision, const |
| 169 | std::optional<std::string_view> submodule, const |
| 170 | std::optional<std::string_view> submoduleRevision) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 171 | { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 172 | if (submodule) { |
| 173 | return m_session->getSchema(*submodule, submoduleRevision); |
| 174 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 175 | return m_session->getSchema(module, revision); |
| 176 | } |
| 177 | |
| 178 | std::vector<std::string> NetconfAccess::listImplementedSchemas() |
| 179 | { |
| 180 | auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas"); |
| 181 | auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier"); |
| 182 | |
| 183 | std::vector<std::string> res; |
| 184 | for (auto it : set->data()) { |
| 185 | if (it->schema()->nodetype() == LYS_LEAF) { |
| 186 | libyang::Data_Node_Leaf_List leaf(it); |
| 187 | res.push_back(leaf.value_str()); |
| 188 | } |
| 189 | } |
| 190 | return res; |
| 191 | } |
| 192 | |
| 193 | std::shared_ptr<Schema> NetconfAccess::schema() |
| 194 | { |
| 195 | return m_schema; |
| 196 | } |