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 | |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 8 | #include <boost/algorithm/string/predicate.hpp> |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 9 | #include <libyang/Libyang.hpp> |
| 10 | #include <libyang/Tree_Data.hpp> |
Václav Kubernát | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 11 | #include "libyang_utils.hpp" |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 12 | #include "netconf-client.hpp" |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 13 | #include "netconf_access.hpp" |
| 14 | #include "utils.hpp" |
| 15 | #include "yang_schema.hpp" |
| 16 | |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | // This is very similar to the fillMap lambda in SysrepoAccess, however, |
| 20 | // Sysrepo returns a weird array-like structure, while libnetconf |
| 21 | // returns libyang::Data_Node |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 22 | 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] | 23 | { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 24 | auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) { |
| 25 | return ignoredXPathPrefix ? path.substr(ignoredXPathPrefix->size()) : path; |
| 26 | }; |
| 27 | |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 28 | for (const auto& it : items) { |
| 29 | if (!it) |
| 30 | continue; |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 31 | if (it->schema()->nodetype() == LYS_CONTAINER) { |
| 32 | if (libyang::Schema_Node_Container{it->schema()}.presence()) { |
| 33 | // The fact that the container is included in the data tree |
| 34 | // means that it is present and I don't need to check any |
| 35 | // value. |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 36 | res.emplace_back(stripXPathPrefix(it->path()), special_{SpecialValue::PresenceContainer}); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 37 | } |
| 38 | } |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 39 | if (it->schema()->nodetype() == LYS_LIST) { |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 40 | res.push_back({stripXPathPrefix(it->path()), special_{SpecialValue::List}}); |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 41 | } |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 42 | if (it->schema()->nodetype() == LYS_LEAF || it->schema()->nodetype() == LYS_LEAFLIST) { |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 43 | libyang::Data_Node_Leaf_List leaf(it); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 44 | auto value = leafValueFromValue(leaf.value(), leaf.leaf_type()->base()); |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 45 | res.emplace_back(stripXPathPrefix(it->path()), value); |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 52 | NetconfAccess::~NetconfAccess() = default; |
| 53 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 54 | DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 55 | { |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 56 | Tree res; |
Jan Kundrát | b091dd4 | 2020-01-30 09:28:28 +0100 | [diff] [blame] | 57 | 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] | 58 | |
| 59 | if (config) { |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 60 | auto siblings = config->tree_for(); |
| 61 | for (auto it = siblings.begin(); it < siblings.end(); it++) { |
| 62 | if ((*it)->schema()->nodetype() == LYS_LEAFLIST) { |
| 63 | auto leafListPath = stripLeafListValueFromPath((*it)->path()); |
| 64 | res.emplace_back(leafListPath, special_{SpecialValue::LeafList}); |
| 65 | while (it != siblings.end() && boost::starts_with((*it)->path(), leafListPath)) { |
| 66 | fillMap(res, (*it)->tree_dfs()); |
| 67 | it++; |
| 68 | } |
| 69 | } else { |
| 70 | fillMap(res, (*it)->tree_dfs()); |
| 71 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 72 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 73 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 74 | return res; |
| 75 | } |
| 76 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 77 | 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] | 78 | : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey)) |
| 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 | |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 83 | NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session) |
| 84 | : m_session(std::move(session)) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 85 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 86 | { |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 89 | NetconfAccess::NetconfAccess(const std::string& socketPath) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 90 | : m_session(libnetconf::client::Session::connectSocket(socketPath)) |
| 91 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 92 | { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 96 | { |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 97 | auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value)); |
| 98 | auto node = m_schema->dataNodeFromPath(path, lyValue); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 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); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 111 | auto container = *(node->find_path(path.c_str())->data().begin()); |
| 112 | container->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 113 | doEditFromDataNode(node); |
| 114 | } |
| 115 | |
| 116 | void NetconfAccess::createListInstance(const std::string& path) |
| 117 | { |
| 118 | auto node = m_schema->dataNodeFromPath(path); |
| 119 | doEditFromDataNode(node); |
| 120 | } |
| 121 | |
| 122 | void NetconfAccess::deleteListInstance(const std::string& path) |
| 123 | { |
| 124 | auto node = m_schema->dataNodeFromPath(path); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 125 | auto list = *(node->find_path(path.c_str())->data().begin()); |
| 126 | list->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 127 | doEditFromDataNode(node); |
| 128 | } |
| 129 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 130 | void NetconfAccess::createLeafListInstance(const std::string& path) |
| 131 | { |
| 132 | auto node = m_schema->dataNodeFromPath(path); |
| 133 | doEditFromDataNode(node); |
| 134 | } |
| 135 | void NetconfAccess::deleteLeafListInstance(const std::string& path) |
| 136 | { |
| 137 | auto node = m_schema->dataNodeFromPath(path); |
| 138 | auto list = *(node->find_path(path.c_str())->data().begin()); |
| 139 | list->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 140 | doEditFromDataNode(node); |
| 141 | } |
| 142 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 143 | void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode) |
| 144 | { |
| 145 | auto data = dataNode->print_mem(LYD_XML, 0); |
| 146 | m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, data); |
| 147 | } |
| 148 | |
| 149 | void NetconfAccess::commitChanges() |
| 150 | { |
| 151 | m_session->commit(); |
| 152 | } |
| 153 | |
| 154 | void NetconfAccess::discardChanges() |
| 155 | { |
| 156 | m_session->discard(); |
| 157 | } |
| 158 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 159 | DatastoreAccess::Tree NetconfAccess::executeRpc(const std::string& path, const Tree& input) |
| 160 | { |
| 161 | auto root = m_schema->dataNodeFromPath(path); |
| 162 | for (const auto& [k, v] : input) { |
| 163 | auto node = m_schema->dataNodeFromPath(joinPaths(path, k), leafDataToString(v)); |
| 164 | root->merge(node, 0); |
| 165 | } |
| 166 | auto data = root->print_mem(LYD_XML, 0); |
| 167 | |
| 168 | Tree res; |
| 169 | auto output = m_session->rpc(data); |
| 170 | if (output) { |
| 171 | for (auto it : output->tree_for()) { |
| 172 | fillMap(res, it->tree_dfs(), joinPaths(path, "/")); |
| 173 | } |
| 174 | } |
| 175 | return res; |
| 176 | } |
| 177 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 178 | NC_DATASTORE toNcDatastore(Datastore datastore) |
| 179 | { |
| 180 | switch (datastore) { |
| 181 | case Datastore::Running: |
| 182 | return NC_DATASTORE_RUNNING; |
| 183 | case Datastore::Startup: |
| 184 | return NC_DATASTORE_STARTUP; |
| 185 | } |
| 186 | __builtin_unreachable(); |
| 187 | } |
| 188 | |
| 189 | void NetconfAccess::copyConfig(const Datastore source, const Datastore destination) |
| 190 | { |
| 191 | m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination)); |
| 192 | } |
| 193 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 194 | std::string NetconfAccess::fetchSchema(const std::string_view module, const |
| 195 | std::optional<std::string_view> revision, const |
| 196 | std::optional<std::string_view> submodule, const |
| 197 | std::optional<std::string_view> submoduleRevision) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 198 | { |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 199 | if (submodule) { |
| 200 | return m_session->getSchema(*submodule, submoduleRevision); |
| 201 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 202 | return m_session->getSchema(module, revision); |
| 203 | } |
| 204 | |
| 205 | std::vector<std::string> NetconfAccess::listImplementedSchemas() |
| 206 | { |
| 207 | auto data = m_session->get("/ietf-netconf-monitoring:netconf-state/schemas"); |
| 208 | auto set = data->find_path("/ietf-netconf-monitoring:netconf-state/schemas/schema/identifier"); |
| 209 | |
| 210 | std::vector<std::string> res; |
| 211 | for (auto it : set->data()) { |
| 212 | if (it->schema()->nodetype() == LYS_LEAF) { |
| 213 | libyang::Data_Node_Leaf_List leaf(it); |
| 214 | res.push_back(leaf.value_str()); |
| 215 | } |
| 216 | } |
| 217 | return res; |
| 218 | } |
| 219 | |
| 220 | std::shared_ptr<Schema> NetconfAccess::schema() |
| 221 | { |
| 222 | return m_schema; |
| 223 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 224 | |
| 225 | std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path) |
| 226 | { |
| 227 | std::vector<ListInstance> res; |
| 228 | auto list = m_schema->dataNodeFromPath(path); |
| 229 | |
| 230 | // This inserts selection nodes - I only want keys not other data |
| 231 | // 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) |
| 232 | auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys(); |
| 233 | for (const auto& keyLeaf : keys) { |
| 234 | // Have to call find_path here - otherwise I'll have the list, not the leaf inside it |
| 235 | auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin()); |
| 236 | auto actualList = *(list->find_path(path.c_str())->data().begin()); |
| 237 | actualList->insert(selectionLeaf); |
| 238 | } |
| 239 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 240 | auto instances = m_session->get(list->print_mem(LYD_XML, 0)); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 241 | |
Václav Kubernát | 45e5546 | 2020-02-04 11:19:32 +0100 | [diff] [blame] | 242 | if (!instances) { |
| 243 | return res; |
| 244 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 245 | |
| 246 | for (const auto& instance : instances->find_path(path.c_str())->data()) { |
| 247 | ListInstance instanceRes; |
| 248 | |
| 249 | // I take the first child here, because the first element (the parent of the child()) will be the list |
| 250 | for (const auto& keyLeaf : instance->child()->tree_for()) { |
| 251 | auto leafData = libyang::Data_Node_Leaf_List{keyLeaf}; |
| 252 | auto leafSchema = libyang::Schema_Node_Leaf{leafData.schema()}; |
| 253 | instanceRes.insert({ leafSchema.name(), leafValueFromValue(leafData.value(), leafSchema.type()->base())}); |
| 254 | } |
| 255 | res.push_back(instanceRes); |
| 256 | } |
| 257 | |
| 258 | return res; |
| 259 | } |