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 | 02a7115 | 2020-01-21 14:52:51 +0100 | [diff] [blame] | 8 | #include "libyang_utils.hpp" |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "netconf-client.hpp" |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 10 | #include "netconf_access.hpp" |
| 11 | #include "utils.hpp" |
| 12 | #include "yang_schema.hpp" |
| 13 | |
Jan Kundrát | 8f63fb7 | 2020-01-24 01:40:01 +0100 | [diff] [blame] | 14 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 15 | NetconfAccess::~NetconfAccess() = default; |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 16 | namespace { |
| 17 | auto targetToDs_get(const DatastoreTarget target) |
| 18 | { |
| 19 | switch (target) { |
| 20 | case DatastoreTarget::Operational: |
| 21 | return libnetconf::NmdaDatastore::Operational; |
| 22 | case DatastoreTarget::Running: |
| 23 | return libnetconf::NmdaDatastore::Running; |
| 24 | case DatastoreTarget::Startup: |
| 25 | return libnetconf::NmdaDatastore::Startup; |
| 26 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 27 | |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 28 | __builtin_unreachable(); |
| 29 | } |
| 30 | |
| 31 | auto targetToDs_set(const DatastoreTarget target) |
| 32 | { |
| 33 | switch (target) { |
| 34 | case DatastoreTarget::Operational: |
| 35 | case DatastoreTarget::Running: |
| 36 | return libnetconf::NmdaDatastore::Candidate; |
| 37 | case DatastoreTarget::Startup: |
| 38 | return libnetconf::NmdaDatastore::Startup; |
| 39 | } |
| 40 | |
| 41 | __builtin_unreachable(); |
| 42 | } |
| 43 | } |
Václav Kubernát | d628291 | 2020-06-23 14:49:34 +0200 | [diff] [blame] | 44 | DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path) const |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 45 | { |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 46 | Tree res; |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 47 | auto config = [this, &path] { |
| 48 | if (m_serverHasNMDA) { |
| 49 | return m_session->getData(targetToDs_get(m_target), (path != "/") ? std::optional{path} : std::nullopt); |
| 50 | } |
| 51 | |
| 52 | return m_session->get((path != "/") ? std::optional{path} : std::nullopt); |
| 53 | }(); |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 54 | |
| 55 | if (config) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 56 | lyNodesToTree(res, config->siblings()); |
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 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 61 | 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] | 62 | : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey)) |
| 63 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 64 | { |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 65 | checkNMDA(); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Václav Kubernát | 7a9463f | 2020-10-30 08:57:59 +0100 | [diff] [blame] | 68 | NetconfAccess::NetconfAccess(const int source, const int sink) |
| 69 | : m_session(libnetconf::client::Session::connectFd(source, sink)) |
| 70 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
| 71 | { |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 72 | checkNMDA(); |
Václav Kubernát | 7a9463f | 2020-10-30 08:57:59 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 75 | NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session) |
| 76 | : m_session(std::move(session)) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 77 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 78 | { |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 79 | checkNMDA(); |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 82 | NetconfAccess::NetconfAccess(const std::string& socketPath) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 83 | : m_session(libnetconf::client::Session::connectSocket(socketPath)) |
| 84 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 85 | { |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 86 | checkNMDA(); |
| 87 | } |
| 88 | |
| 89 | void NetconfAccess::checkNMDA() |
| 90 | { |
| 91 | auto nmdaMod = m_schema->getYangModule("ietf-netconf-nmda"); |
| 92 | m_serverHasNMDA = nmdaMod && nmdaMod->implemented(); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 95 | void NetconfAccess::setNcLogLevel(NC_VERB_LEVEL level) |
| 96 | { |
| 97 | libnetconf::client::setLogLevel(level); |
| 98 | } |
| 99 | |
| 100 | void NetconfAccess::setNcLogCallback(const LogCb& callback) |
| 101 | { |
| 102 | libnetconf::client::setLogCallback(callback); |
| 103 | } |
| 104 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 105 | void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 106 | { |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 107 | auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value)); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 108 | auto nodes = m_schema->dataNodeFromPath(path, lyValue); |
| 109 | doEditFromDataNode(*nodes.createdParent); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 112 | void NetconfAccess::createItem(const std::string& path) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 113 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 114 | auto nodes = m_schema->dataNodeFromPath(path); |
| 115 | doEditFromDataNode(*nodes.createdParent); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 118 | void NetconfAccess::deleteItem(const std::string& path) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 119 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 120 | auto nodes = m_schema->dataNodeFromPath(path); |
| 121 | |
| 122 | // When deleting leafs, `nodes.newNode` is opaque, because the leaf does not have a value. We need to use |
| 123 | // newAttrOpaqueJSON for opaque leafs. |
| 124 | if (nodes.createdNode->isOpaque()) { |
| 125 | nodes.createdNode->newAttrOpaqueJSON("ietf-netconf", "ietf-netconf:operation", "delete"); |
| 126 | } else { |
| 127 | nodes.createdNode->newMeta(*m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
| 128 | } |
| 129 | doEditFromDataNode(*nodes.createdParent); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 132 | struct impl_toYangInsert { |
| 133 | std::string operator()(yang::move::Absolute& absolute) |
| 134 | { |
| 135 | return absolute == yang::move::Absolute::Begin ? "first" : "last"; |
| 136 | } |
| 137 | std::string operator()(yang::move::Relative& relative) |
| 138 | { |
| 139 | return relative.m_position == yang::move::Relative::Position::After ? "after" : "before"; |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | std::string toYangInsert(std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 144 | { |
| 145 | return std::visit(impl_toYangInsert{}, move); |
| 146 | } |
| 147 | |
| 148 | void NetconfAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 149 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 150 | auto nodes = m_schema->dataNodeFromPath(source); |
| 151 | auto sourceNode = *(nodes.createdNode->findPath(source.c_str())); |
| 152 | auto yangModule = *m_schema->getYangModule("yang"); |
| 153 | sourceNode.newMeta(yangModule, "insert", toYangInsert(move).c_str()); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 154 | |
| 155 | if (std::holds_alternative<yang::move::Relative>(move)) { |
| 156 | auto relative = std::get<yang::move::Relative>(move); |
| 157 | if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 158 | sourceNode.newMeta(yangModule, "value", leafDataToString(relative.m_path.at(".")).c_str()); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 159 | } else { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 160 | sourceNode.newMeta(yangModule, "key", instanceToString(relative.m_path, std::string{nodes.createdNode->schema().module().name()}).c_str()); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | doEditFromDataNode(sourceNode); |
| 164 | } |
| 165 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 166 | void NetconfAccess::doEditFromDataNode(libyang::DataNode dataNode) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 167 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 168 | auto data = dataNode.printStr(libyang::DataFormat::XML, libyang::PrintFlags::WithSiblings); |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 169 | if (m_serverHasNMDA) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 170 | m_session->editData(targetToDs_set(m_target), std::string{*data}); |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 171 | } else { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 172 | m_session->editConfig(NC_DATASTORE_CANDIDATE, NC_RPC_EDIT_DFLTOP_MERGE, NC_RPC_EDIT_TESTOPT_TESTSET, NC_RPC_EDIT_ERROPT_STOP, std::string{*data}); |
Václav Kubernát | 06b0f38 | 2021-10-04 11:20:47 +0200 | [diff] [blame] | 173 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void NetconfAccess::commitChanges() |
| 177 | { |
| 178 | m_session->commit(); |
| 179 | } |
| 180 | |
| 181 | void NetconfAccess::discardChanges() |
| 182 | { |
| 183 | m_session->discard(); |
| 184 | } |
| 185 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 186 | DatastoreAccess::Tree NetconfAccess::execute(const std::string& path, const Tree& input) |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 187 | { |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 188 | auto inputNode = treeToRpcInput(m_session->libyangContext(), path, input); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 189 | auto data = inputNode.printStr(libyang::DataFormat::XML, libyang::PrintFlags::WithSiblings); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 190 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 191 | auto output = m_session->rpc_or_action(std::string{*data}); |
| 192 | if (!output) { |
| 193 | return {}; |
| 194 | } |
| 195 | return rpcOutputToTree(*output); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 198 | NC_DATASTORE toNcDatastore(Datastore datastore) |
| 199 | { |
| 200 | switch (datastore) { |
| 201 | case Datastore::Running: |
| 202 | return NC_DATASTORE_RUNNING; |
| 203 | case Datastore::Startup: |
| 204 | return NC_DATASTORE_STARTUP; |
| 205 | } |
| 206 | __builtin_unreachable(); |
| 207 | } |
| 208 | |
| 209 | void NetconfAccess::copyConfig(const Datastore source, const Datastore destination) |
| 210 | { |
| 211 | m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination)); |
| 212 | } |
| 213 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 214 | std::shared_ptr<Schema> NetconfAccess::schema() |
| 215 | { |
| 216 | return m_schema; |
| 217 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 218 | |
| 219 | std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path) |
| 220 | { |
| 221 | std::vector<ListInstance> res; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 222 | auto keys = m_session->libyangContext().findXPath(path.c_str()).front().asList().keys(); |
| 223 | auto nodes = m_session->libyangContext().newPath2(path.c_str(), nullptr, libyang::CreationOptions::Opaque); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 224 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 225 | // Here we create a tree with "selection leafs" for all they keys of our wanted list. These leafs tell NETCONF, that |
| 226 | // we only want the list's keys and not any other data. |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 227 | for (const auto& keyLeaf : keys) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 228 | // Selection leafs need to be inserted directly to the list using relative paths, that's why `newNode` is used |
| 229 | // here. |
| 230 | nodes.createdNode->newPath(keyLeaf.name().data(), nullptr, libyang::CreationOptions::Opaque); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 233 | // Have to use `newParent` in case our wanted list is a nested list. With `newNode` I would only send the inner |
| 234 | // nested list and not the whole tree. |
| 235 | auto instances = m_session->get(std::string{*nodes.createdParent->printStr(libyang::DataFormat::XML, libyang::PrintFlags::WithSiblings)}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 236 | |
Václav Kubernát | 45e5546 | 2020-02-04 11:19:32 +0100 | [diff] [blame] | 237 | if (!instances) { |
| 238 | return res; |
| 239 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 240 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 241 | for (const auto& instance : instances->findXPath(path.c_str())) { |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 242 | ListInstance instanceRes; |
| 243 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 244 | for (const auto& keyLeaf : instance.child()->siblings()) { |
Václav Kubernát | ba84ede | 2021-02-04 17:21:11 +0100 | [diff] [blame] | 245 | // FIXME: even though we specified we only want the key leafs, Netopeer disregards that and sends more data, |
| 246 | // even lists and other stuff. We only want keys, so filter out non-leafs and non-keys |
| 247 | // https://github.com/CESNET/netopeer2/issues/825 |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 248 | if (keyLeaf.schema().nodeType() != libyang::NodeType::Leaf) { |
Václav Kubernát | ba84ede | 2021-02-04 17:21:11 +0100 | [diff] [blame] | 249 | continue; |
| 250 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 251 | if (!keyLeaf.schema().asLeaf().isKey()) { |
Václav Kubernát | ba84ede | 2021-02-04 17:21:11 +0100 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 255 | auto leafData = keyLeaf.asTerm(); |
| 256 | instanceRes.insert({std::string{leafData.schema().name()}, leafValueFromNode(leafData)}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 257 | } |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 258 | res.emplace_back(instanceRes); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return res; |
| 262 | } |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 263 | |
| 264 | std::string NetconfAccess::dump(const DataFormat format) const |
| 265 | { |
| 266 | auto config = m_session->get(); |
| 267 | if (!config) { |
| 268 | return ""; |
| 269 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 270 | auto str = config->printStr(format == DataFormat::Xml ? libyang::DataFormat::XML : libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings); |
| 271 | if (!str) { |
| 272 | return ""; |
| 273 | } |
| 274 | |
| 275 | return std::string{*str}; |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 276 | } |