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