Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016-2021 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Jan Kundrát <jan.kundrat@cesnet.cz> |
| 5 | * Written by Tomáš Pecka <tomas.pecka@cesnet.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "sysrepo.h" |
| 10 | #include "utils/log.h" |
| 11 | |
| 12 | extern "C" { |
| 13 | #include <sysrepo.h> |
| 14 | } |
| 15 | |
| 16 | extern "C" { |
| 17 | /** @short Propagate sysrepo events to spdlog */ |
| 18 | static void spdlog_sr_log_cb(sr_log_level_t level, const char* message) |
| 19 | { |
| 20 | // Thread safety note: this is, as far as I know, thread safe: |
| 21 | // - the static initialization itself is OK |
| 22 | // - all loggers which we instantiate are thread-safe |
| 23 | // - std::shared_ptr::operator-> is const, and all const members of that class are documented to be thread-safe |
| 24 | static auto log = spdlog::get("sysrepo"); |
| 25 | assert(log); |
| 26 | switch (level) { |
| 27 | case SR_LL_NONE: |
| 28 | case SR_LL_ERR: |
| 29 | log->error(message); |
| 30 | break; |
| 31 | case SR_LL_WRN: |
| 32 | log->warn(message); |
| 33 | break; |
| 34 | case SR_LL_INF: |
| 35 | log->info(message); |
| 36 | break; |
| 37 | case SR_LL_DBG: |
| 38 | log->debug(message); |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 44 | namespace { |
| 45 | std::vector<velia::utils::YANGPair> mapToVector(const std::map<std::string, std::string>& values) |
| 46 | { |
| 47 | std::vector<velia::utils::YANGPair> res; |
| 48 | for (const auto& [xpath, value] : values) { |
| 49 | res.emplace_back(xpath, value); |
| 50 | } |
| 51 | |
| 52 | return res; |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 57 | namespace velia::utils { |
| 58 | |
| 59 | /** @short Setup sysrepo log forwarding |
| 60 | You must call cla::utils::initLogs prior to this function. |
| 61 | */ |
| 62 | void initLogsSysrepo() |
| 63 | { |
| 64 | sr_log_set_cb(spdlog_sr_log_cb); |
| 65 | } |
| 66 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 67 | void valuesToYang(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, ::sysrepo::Session session, std::optional<libyang::DataNode>& parent) |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 68 | { |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 69 | valuesToYang(mapToVector(values), removePaths, std::move(session), parent); |
| 70 | } |
| 71 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 72 | void valuesToYang(const std::vector<YANGPair>& values, const std::vector<std::string>& removePaths, ::sysrepo::Session session, std::optional<libyang::DataNode>& parent) |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 73 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 74 | auto netconf = session.getContext().getModuleImplemented("ietf-netconf"); |
Tomáš Pecka | 5b293f4 | 2021-03-02 17:47:03 +0100 | [diff] [blame] | 75 | auto log = spdlog::get("main"); |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 76 | |
| 77 | for (const auto& propertyName : removePaths) { |
Tomáš Pecka | 5b293f4 | 2021-03-02 17:47:03 +0100 | [diff] [blame] | 78 | log->trace("Processing node deletion {}", propertyName); |
| 79 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 80 | if (!parent) { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 81 | parent = session.getContext().newPath(propertyName.c_str(), nullptr, libyang::CreationOptions::Opaque); |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 82 | } else { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 83 | parent->newPath(propertyName.c_str(), nullptr, libyang::CreationOptions::Opaque); |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 86 | auto deletion = parent->findPath(propertyName.c_str()); |
| 87 | if (!deletion) { |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 88 | throw std::logic_error {"Cannot find XPath " + propertyName + " for deletion in libyang's new_path() output"}; |
| 89 | } |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 90 | deletion->newMeta(*netconf, "operation", "remove"); |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 93 | for (const auto& [propertyName, value] : values) { |
Tomáš Pecka | 5b293f4 | 2021-03-02 17:47:03 +0100 | [diff] [blame] | 94 | log->trace("Processing node update {} -> {}", propertyName, value); |
| 95 | |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 96 | if (!parent) { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 97 | parent = session.getContext().newPath(propertyName.c_str(), value.c_str(), libyang::CreationOptions::Output); |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 98 | } else { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 99 | parent->newPath(propertyName.c_str(), value.c_str(), libyang::CreationOptions::Output); |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 104 | /** @brief Set or remove values in Sysrepo's specified datastore. It changes the datastore and after the data are applied, the original datastore is restored. */ |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 105 | void valuesPush(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, ::sysrepo::Session session, sysrepo::Datastore datastore) |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 106 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 107 | auto oldDatastore = session.activeDatastore(); |
| 108 | session.switchDatastore(datastore); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 109 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 110 | valuesPush(values, removePaths, session); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 111 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 112 | session.switchDatastore(oldDatastore); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 115 | /** @brief Set or remove paths in Sysrepo's current datastore. */ |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 116 | void valuesPush(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, ::sysrepo::Session session) |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 117 | { |
Tomáš Pecka | daedba5 | 2021-03-08 12:52:20 +0100 | [diff] [blame] | 118 | if (values.empty() && removePaths.empty()) return; |
| 119 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 120 | std::optional<libyang::DataNode> edit; |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 121 | valuesToYang(values, removePaths, session, edit); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 122 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 123 | if (edit) { |
| 124 | session.editBatch(*edit, sysrepo::DefaultOperation::Merge); |
| 125 | session.applyChanges(); |
| 126 | } |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 129 | /** @brief Set or remove values in Sysrepo's specified datastore. It changes the datastore and after the data are applied, the original datastore is restored. */ |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 130 | void valuesPush(const std::vector<YANGPair>& values, const std::vector<std::string>& removePaths, sysrepo::Session session, sysrepo::Datastore datastore) |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 131 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 132 | auto oldDatastore = session.activeDatastore(); |
| 133 | session.switchDatastore(datastore); |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 134 | |
| 135 | valuesPush(values, removePaths, session); |
| 136 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 137 | session.switchDatastore(oldDatastore); |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** @brief Set or remove paths in Sysrepo's current datastore. */ |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 141 | void valuesPush(const std::vector<YANGPair>& values, const std::vector<std::string>& removePaths, sysrepo::Session session) |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 142 | { |
| 143 | if (values.empty() && removePaths.empty()) |
| 144 | return; |
| 145 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 146 | std::optional<libyang::DataNode> edit; |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 147 | valuesToYang(values, removePaths, session, edit); |
| 148 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 149 | if (edit) { |
| 150 | session.editBatch(*edit, sysrepo::DefaultOperation::Merge); |
| 151 | session.applyChanges(); |
| 152 | } |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Václav Kubernát | 6fa7f34 | 2021-01-26 17:00:01 +0100 | [diff] [blame] | 155 | /** @brief Checks whether a module is implemented in Sysrepo and throws if not. */ |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 156 | void ensureModuleImplemented(::sysrepo::Session session, const std::string& module, const std::string& revision) |
Václav Kubernát | 6fa7f34 | 2021-01-26 17:00:01 +0100 | [diff] [blame] | 157 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 158 | if (auto mod = session.getContext().getModule(module.c_str(), revision.c_str()); !mod || !mod->implemented()) { |
Václav Kubernát | 6fa7f34 | 2021-01-26 17:00:01 +0100 | [diff] [blame] | 159 | throw std::runtime_error(module + "@" + revision + " is not implemented in sysrepo."); |
| 160 | } |
| 161 | } |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 162 | YANGPair::YANGPair(std::string xpath, std::string value) |
| 163 | : m_xpath(std::move(xpath)) |
| 164 | , m_value(std::move(value)) |
| 165 | { |
| 166 | } |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 167 | |
| 168 | void setErrors(::sysrepo::Session session, const std::string& msg) |
| 169 | { |
| 170 | session.setNetconfError(sysrepo::NetconfErrorInfo{ |
| 171 | .type = "application", |
| 172 | .tag = "operation-failed", |
| 173 | .appTag = {}, |
| 174 | .path = {}, |
| 175 | .message = msg.c_str(), |
| 176 | .infoElements = {}, |
| 177 | }); |
| 178 | session.setErrorMessage(msg.c_str()); |
| 179 | } |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 180 | } |