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