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; |
Jan Kundrát | b091dd4 | 2020-01-30 09:28:28 +0100 | [diff] [blame] | 55 | auto config = m_session->get((path != "/") ? std::optional{path} : std::nullopt); |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 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 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 65 | NetconfAccess::NetconfAccess(const std::string& hostname, uint16_t port, const std::string& user, const std::string& pubKey, const std::string& privKey) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 66 | : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey)) |
| 67 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 68 | { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 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)) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 73 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 74 | { |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 77 | NetconfAccess::NetconfAccess(const std::string& socketPath) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 78 | : m_session(libnetconf::client::Session::connectSocket(socketPath)) |
| 79 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 80 | { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 84 | { |
| 85 | auto node = m_schema->dataNodeFromPath(path, leafDataToString(value)); |
| 86 | doEditFromDataNode(node); |
| 87 | } |
| 88 | |
| 89 | void NetconfAccess::createPresenceContainer(const std::string& path) |
| 90 | { |
| 91 | auto node = m_schema->dataNodeFromPath(path); |
| 92 | doEditFromDataNode(node); |
| 93 | } |
| 94 | |
| 95 | void NetconfAccess::deletePresenceContainer(const std::string& path) |
| 96 | { |
| 97 | auto node = m_schema->dataNodeFromPath(path); |
| 98 | node->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 99 | doEditFromDataNode(node); |
| 100 | } |
| 101 | |
| 102 | void NetconfAccess::createListInstance(const std::string& path) |
| 103 | { |
| 104 | auto node = m_schema->dataNodeFromPath(path); |
| 105 | doEditFromDataNode(node); |
| 106 | } |
| 107 | |
| 108 | void NetconfAccess::deleteListInstance(const std::string& path) |
| 109 | { |
| 110 | auto node = m_schema->dataNodeFromPath(path); |
| 111 | node->child()->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 112 | doEditFromDataNode(node); |
| 113 | } |
| 114 | |
| 115 | void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode) |
| 116 | { |
| 117 | auto data = dataNode->print_mem(LYD_XML, 0); |
| 118 | m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data); |
| 119 | } |
| 120 | |
| 121 | void NetconfAccess::commitChanges() |
| 122 | { |
| 123 | m_session->commit(); |
| 124 | } |
| 125 | |
| 126 | void NetconfAccess::discardChanges() |
| 127 | { |
| 128 | m_session->discard(); |
| 129 | } |
| 130 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 131 | DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input) |
| 132 | { |
| 133 | auto root = m_schema->dataNodeFromPath(path); |
| 134 | for (const auto& [k, v] : input) { |
| 135 | auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v)); |
| 136 | root->merge(node, 0); |
| 137 | } |
| 138 | auto data = root->print_mem(LYD_XML, 0); |
| 139 | |
| 140 | Tree res; |
| 141 | auto output = m_session->rpc(data); |
| 142 | if (output) { |
| 143 | for (auto it : output->tree_for()) { |
| 144 | fillMap(res, it->tree_dfs(), joinPaths(path, "/")); |
| 145 | } |
| 146 | } |
| 147 | return res; |
| 148 | } |
| 149 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 150 | std::string NetconfAccess::fetchSchema(const std::string_view module, const |
| 151 | std::optional<std::string_view> revision, const |
| 152 | std::optional<std::string_view> submodule, const |
| 153 | std::optional<std::string_view> submoduleRevision) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 154 | { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 155 | if (submodule) { |
| 156 | return m_session->getSchema(*submodule, submoduleRevision); |
| 157 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 158 | return m_session->getSchema(module, revision); |
| 159 | } |
| 160 | |
| 161 | std::vector<std::string> NetconfAccess::listImplementedSchemas() |
| 162 | { |
| 163 | auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas"); |
| 164 | auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier"); |
| 165 | |
| 166 | std::vector<std::string> res; |
| 167 | for (auto it : set->data()) { |
| 168 | if (it->schema()->nodetype() == LYS_LEAF) { |
| 169 | libyang::Data_Node_Leaf_List leaf(it); |
| 170 | res.push_back(leaf.value_str()); |
| 171 | } |
| 172 | } |
| 173 | return res; |
| 174 | } |
| 175 | |
| 176 | std::shared_ptr<Schema> NetconfAccess::schema() |
| 177 | { |
| 178 | return m_schema; |
| 179 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 180 | |
| 181 | std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path) |
| 182 | { |
| 183 | std::vector<ListInstance> res; |
| 184 | auto list = m_schema->dataNodeFromPath(path); |
| 185 | |
| 186 | // This inserts selection nodes - I only want keys not other data |
| 187 | // To get the keys, I have to call find_path here - otherwise I would get keys of a top-level node (which might not even be a list) |
| 188 | auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys(); |
| 189 | for (const auto& keyLeaf : keys) { |
| 190 | // Have to call find_path here - otherwise I'll have the list, not the leaf inside it |
| 191 | auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin()); |
| 192 | auto actualList = *(list->find_path(path.c_str())->data().begin()); |
| 193 | actualList->insert(selectionLeaf); |
| 194 | } |
| 195 | |
| 196 | auto instances = m_session->getConfig(NC_DATASTORE_RUNNING, list->print_mem(LYD_XML, 0)); |
| 197 | |
| 198 | |
| 199 | for (const auto& instance : instances->find_path(path.c_str())->data()) { |
| 200 | ListInstance instanceRes; |
| 201 | |
| 202 | // I take the first child here, because the first element (the parent of the child()) will be the list |
| 203 | for (const auto& keyLeaf : instance->child()->tree_for()) { |
| 204 | auto leafData = libyang::Data_Node_Leaf_List{keyLeaf}; |
| 205 | auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()}; |
| 206 | instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())}); |
| 207 | } |
| 208 | res.push_back(instanceRes); |
| 209 | } |
| 210 | |
| 211 | return res; |
| 212 | } |