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 | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 11 | #include "netconf-client.hpp" |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 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 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 17 | NetconfAccess::~NetconfAccess() = default; |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 18 | namespace { |
| 19 | auto targetToDs_get(const DatastoreTarget target) |
| 20 | { |
| 21 | switch (target) { |
| 22 | case DatastoreTarget::Operational: |
| 23 | return libnetconf::NmdaDatastore::Operational; |
| 24 | case DatastoreTarget::Running: |
| 25 | return libnetconf::NmdaDatastore::Running; |
| 26 | case DatastoreTarget::Startup: |
| 27 | return libnetconf::NmdaDatastore::Startup; |
| 28 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 29 | |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 30 | __builtin_unreachable(); |
| 31 | } |
| 32 | |
| 33 | auto targetToDs_set(const DatastoreTarget target) |
| 34 | { |
| 35 | switch (target) { |
| 36 | case DatastoreTarget::Operational: |
| 37 | case DatastoreTarget::Running: |
| 38 | return libnetconf::NmdaDatastore::Candidate; |
| 39 | case DatastoreTarget::Startup: |
| 40 | return libnetconf::NmdaDatastore::Startup; |
| 41 | } |
| 42 | |
| 43 | __builtin_unreachable(); |
| 44 | } |
| 45 | } |
Václav Kubernát | d628291 | 2020-06-23 14:49:34 +0200 | [diff] [blame] | 46 | DatastoreAccess::Tree NetconfAccess::getItems(const std::string& path) const |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 47 | { |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 48 | Tree res; |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 49 | auto config = m_session->getData(targetToDs_get(m_target), (path != "/") ? std::optional{path} : std::nullopt); |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 50 | |
| 51 | if (config) { |
Václav Kubernát | daf4031 | 2020-06-19 11:34:55 +0200 | [diff] [blame] | 52 | lyNodesToTree(res, config->tree_for()); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 53 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 54 | return res; |
| 55 | } |
| 56 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 57 | 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] | 58 | : m_session(libnetconf::client::Session::connectPubkey(hostname, port, user, pubKey, privKey)) |
| 59 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
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 | } |
| 62 | |
Václav Kubernát | 7a9463f | 2020-10-30 08:57:59 +0100 | [diff] [blame] | 63 | NetconfAccess::NetconfAccess(const int source, const int sink) |
| 64 | : m_session(libnetconf::client::Session::connectFd(source, sink)) |
| 65 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
| 66 | { |
| 67 | } |
| 68 | |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 69 | NetconfAccess::NetconfAccess(std::unique_ptr<libnetconf::client::Session>&& session) |
| 70 | : m_session(std::move(session)) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 71 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 72 | { |
Jan Kundrát | dab815e | 2020-01-22 19:44:11 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 75 | NetconfAccess::NetconfAccess(const std::string& socketPath) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 76 | : m_session(libnetconf::client::Session::connectSocket(socketPath)) |
| 77 | , m_schema(std::make_shared<YangSchema>(m_session->libyangContext())) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 78 | { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 81 | void NetconfAccess::setNcLogLevel(NC_VERB_LEVEL level) |
| 82 | { |
| 83 | libnetconf::client::setLogLevel(level); |
| 84 | } |
| 85 | |
| 86 | void NetconfAccess::setNcLogCallback(const LogCb& callback) |
| 87 | { |
| 88 | libnetconf::client::setLogCallback(callback); |
| 89 | } |
| 90 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 91 | void NetconfAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 92 | { |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 93 | auto lyValue = value.type() == typeid(empty_) ? std::nullopt : std::optional(leafDataToString(value)); |
| 94 | auto node = m_schema->dataNodeFromPath(path, lyValue); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 95 | doEditFromDataNode(node); |
| 96 | } |
| 97 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 98 | void NetconfAccess::createItem(const std::string& path) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 99 | { |
| 100 | auto node = m_schema->dataNodeFromPath(path); |
| 101 | doEditFromDataNode(node); |
| 102 | } |
| 103 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 104 | void NetconfAccess::deleteItem(const std::string& path) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 105 | { |
| 106 | auto node = m_schema->dataNodeFromPath(path); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 107 | auto container = *(node->find_path(path.c_str())->data().begin()); |
| 108 | container->insert_attr(m_schema->getYangModule("ietf-netconf"), "operation", "delete"); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 109 | doEditFromDataNode(node); |
| 110 | } |
| 111 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 112 | struct impl_toYangInsert { |
| 113 | std::string operator()(yang::move::Absolute& absolute) |
| 114 | { |
| 115 | return absolute == yang::move::Absolute::Begin ? "first" : "last"; |
| 116 | } |
| 117 | std::string operator()(yang::move::Relative& relative) |
| 118 | { |
| 119 | return relative.m_position == yang::move::Relative::Position::After ? "after" : "before"; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | std::string toYangInsert(std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 124 | { |
| 125 | return std::visit(impl_toYangInsert{}, move); |
| 126 | } |
| 127 | |
| 128 | void NetconfAccess::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 129 | { |
| 130 | auto node = m_schema->dataNodeFromPath(source); |
| 131 | auto sourceNode = *(node->find_path(source.c_str())->data().begin()); |
| 132 | auto yangModule = m_schema->getYangModule("yang"); |
| 133 | sourceNode->insert_attr(yangModule, "insert", toYangInsert(move).c_str()); |
| 134 | |
| 135 | if (std::holds_alternative<yang::move::Relative>(move)) { |
| 136 | auto relative = std::get<yang::move::Relative>(move); |
| 137 | if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) { |
| 138 | sourceNode->insert_attr(yangModule, "value", leafDataToString(relative.m_path.at(".")).c_str()); |
| 139 | } else { |
Václav Kubernát | 2c4778b | 2020-06-18 11:55:20 +0200 | [diff] [blame] | 140 | sourceNode->insert_attr(yangModule, "key", instanceToString(relative.m_path, node->node_module()->name()).c_str()); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | doEditFromDataNode(sourceNode); |
| 144 | } |
| 145 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 146 | void NetconfAccess::doEditFromDataNode(std::shared_ptr<libyang::Data_Node> dataNode) |
| 147 | { |
| 148 | auto data = dataNode->print_mem(LYD_XML, 0); |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 149 | m_session->editData(targetToDs_set(m_target), data); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void NetconfAccess::commitChanges() |
| 153 | { |
| 154 | m_session->commit(); |
| 155 | } |
| 156 | |
| 157 | void NetconfAccess::discardChanges() |
| 158 | { |
| 159 | m_session->discard(); |
| 160 | } |
| 161 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 162 | DatastoreAccess::Tree NetconfAccess::execute(const std::string& path, const Tree& input) |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 163 | { |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 164 | auto inputNode = treeToRpcInput(m_session->libyangContext(), path, input); |
| 165 | auto data = inputNode->print_mem(LYD_XML, 0); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 166 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 167 | auto output = m_session->rpc_or_action(data); |
Václav Kubernát | fbab2d4 | 2021-02-05 16:12:34 +0100 | [diff] [blame] | 168 | return rpcOutputToTree(path, output); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 171 | NC_DATASTORE toNcDatastore(Datastore datastore) |
| 172 | { |
| 173 | switch (datastore) { |
| 174 | case Datastore::Running: |
| 175 | return NC_DATASTORE_RUNNING; |
| 176 | case Datastore::Startup: |
| 177 | return NC_DATASTORE_STARTUP; |
| 178 | } |
| 179 | __builtin_unreachable(); |
| 180 | } |
| 181 | |
| 182 | void NetconfAccess::copyConfig(const Datastore source, const Datastore destination) |
| 183 | { |
| 184 | m_session->copyConfig(toNcDatastore(source), toNcDatastore(destination)); |
| 185 | } |
| 186 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 187 | std::shared_ptr<Schema> NetconfAccess::schema() |
| 188 | { |
| 189 | return m_schema; |
| 190 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 191 | |
| 192 | std::vector<ListInstance> NetconfAccess::listInstances(const std::string& path) |
| 193 | { |
| 194 | std::vector<ListInstance> res; |
| 195 | auto list = m_schema->dataNodeFromPath(path); |
| 196 | |
| 197 | // This inserts selection nodes - I only want keys not other data |
| 198 | // 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) |
| 199 | auto keys = libyang::Schema_Node_List{(*(list->find_path(path.c_str())->data().begin()))->schema()}.keys(); |
| 200 | for (const auto& keyLeaf : keys) { |
| 201 | // Have to call find_path here - otherwise I'll have the list, not the leaf inside it |
| 202 | auto selectionLeaf = *(m_schema->dataNodeFromPath(keyLeaf->path())->find_path(keyLeaf->path().c_str())->data().begin()); |
| 203 | auto actualList = *(list->find_path(path.c_str())->data().begin()); |
| 204 | actualList->insert(selectionLeaf); |
| 205 | } |
| 206 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 207 | 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] | 208 | |
Václav Kubernát | 45e5546 | 2020-02-04 11:19:32 +0100 | [diff] [blame] | 209 | if (!instances) { |
| 210 | return res; |
| 211 | } |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 212 | |
| 213 | for (const auto& instance : instances->find_path(path.c_str())->data()) { |
| 214 | ListInstance instanceRes; |
| 215 | |
| 216 | // I take the first child here, because the first element (the parent of the child()) will be the list |
| 217 | for (const auto& keyLeaf : instance->child()->tree_for()) { |
Václav Kubernát | ba84ede | 2021-02-04 17:21:11 +0100 | [diff] [blame] | 218 | // FIXME: even though we specified we only want the key leafs, Netopeer disregards that and sends more data, |
| 219 | // even lists and other stuff. We only want keys, so filter out non-leafs and non-keys |
| 220 | // https://github.com/CESNET/netopeer2/issues/825 |
| 221 | if (keyLeaf->schema()->nodetype() != LYS_LEAF) { |
| 222 | continue; |
| 223 | } |
| 224 | if (!std::make_shared<libyang::Schema_Node_Leaf>(keyLeaf->schema())->is_key()) { |
| 225 | continue; |
| 226 | } |
| 227 | |
Václav Kubernát | 2e4cafe | 2020-11-05 01:53:21 +0100 | [diff] [blame] | 228 | auto leafData = std::make_shared<libyang::Data_Node_Leaf_List>(keyLeaf); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 229 | instanceRes.insert({leafData->schema()->name(), leafValueFromNode(leafData)}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 230 | } |
Václav Kubernát | faacd02 | 2020-07-08 16:44:38 +0200 | [diff] [blame] | 231 | res.emplace_back(instanceRes); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return res; |
| 235 | } |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 236 | |
| 237 | std::string NetconfAccess::dump(const DataFormat format) const |
| 238 | { |
| 239 | auto config = m_session->get(); |
| 240 | if (!config) { |
| 241 | return ""; |
| 242 | } |
| 243 | return config->print_mem(format == DataFormat::Xml ? LYD_XML : LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT); |
| 244 | } |