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