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