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