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 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 67 | void valuesToYang(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, const std::vector<std::string>& discardPaths, ::sysrepo::Session session, std::optional<libyang::DataNode>& parent) |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 68 | { |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 69 | valuesToYang(mapToVector(values), removePaths, discardPaths, std::move(session), parent); |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 72 | void valuesToYang(const std::vector<YANGPair>& values, const std::vector<std::string>& removePaths, const std::vector<std::string>& discardPaths, ::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) { |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 81 | parent = session.getContext().newPath(propertyName, std::nullopt, libyang::CreationOptions::Opaque); |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 82 | } else { |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 83 | parent->newPath(propertyName, std::nullopt, libyang::CreationOptions::Opaque); |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 86 | auto deletion = parent->findPath(propertyName); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 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) { |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 97 | parent = session.getContext().newPath(propertyName, value, libyang::CreationOptions::Output); |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 98 | } else { |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 99 | parent->newPath(propertyName, value, libyang::CreationOptions::Output); |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 102 | |
| 103 | for (const auto& propertyName : discardPaths) { |
| 104 | log->trace("Processing node discard {}", propertyName); |
| 105 | |
| 106 | auto discard = session.getContext().newOpaqueJSON("sysrepo", "discard-items", libyang::JSON{propertyName}); |
| 107 | |
| 108 | if (!parent) { |
| 109 | parent = discard; |
| 110 | } else { |
| 111 | parent->insertSibling(*discard); |
| 112 | parent->newPath(propertyName, std::nullopt, libyang::CreationOptions::Opaque); |
| 113 | } |
| 114 | } |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 117 | /** @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. */ |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 118 | void valuesPush(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, const std::vector<std::string>& discardPaths, ::sysrepo::Session session, sysrepo::Datastore datastore) |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 119 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 120 | auto oldDatastore = session.activeDatastore(); |
| 121 | session.switchDatastore(datastore); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 122 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 123 | valuesPush(values, removePaths, discardPaths, session); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 124 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 125 | session.switchDatastore(oldDatastore); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 128 | /** @brief Set or remove paths in Sysrepo's current datastore. */ |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 129 | void valuesPush(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, const std::vector<std::string>& discardPaths, ::sysrepo::Session session) |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 130 | { |
Tomáš Pecka | daedba5 | 2021-03-08 12:52:20 +0100 | [diff] [blame] | 131 | if (values.empty() && removePaths.empty()) return; |
| 132 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 133 | std::optional<libyang::DataNode> edit; |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 134 | valuesToYang(values, removePaths, discardPaths, session, edit); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 135 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 136 | if (edit) { |
| 137 | session.editBatch(*edit, sysrepo::DefaultOperation::Merge); |
| 138 | session.applyChanges(); |
| 139 | } |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 142 | /** @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. */ |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 143 | void valuesPush(const std::vector<YANGPair>& values, const std::vector<std::string>& removePaths, const std::vector<std::string>& discardPaths, sysrepo::Session session, sysrepo::Datastore datastore) |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 144 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 145 | auto oldDatastore = session.activeDatastore(); |
| 146 | session.switchDatastore(datastore); |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 147 | |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 148 | valuesPush(values, removePaths, discardPaths, session); |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 149 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 150 | session.switchDatastore(oldDatastore); |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** @brief Set or remove paths in Sysrepo's current datastore. */ |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 154 | void valuesPush(const std::vector<YANGPair>& values, const std::vector<std::string>& removePaths, const std::vector<std::string>& discardPaths, sysrepo::Session session) |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 155 | { |
| 156 | if (values.empty() && removePaths.empty()) |
| 157 | return; |
| 158 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 159 | std::optional<libyang::DataNode> edit; |
Jan Kundrát | 498c3f8 | 2023-05-24 19:25:48 +0200 | [diff] [blame^] | 160 | valuesToYang(values, removePaths, discardPaths, session, edit); |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 161 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 162 | if (edit) { |
Jan Kundrát | 7c16208 | 2023-01-13 12:18:39 +0100 | [diff] [blame] | 163 | session.editBatch(*edit, sysrepo::DefaultOperation::Merge); |
| 164 | session.applyChanges(); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 165 | } |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Václav Kubernát | 6fa7f34 | 2021-01-26 17:00:01 +0100 | [diff] [blame] | 168 | /** @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] | 169 | 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] | 170 | { |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 171 | if (auto mod = session.getContext().getModule(module, revision); !mod || !mod->implemented()) { |
Václav Kubernát | 6fa7f34 | 2021-01-26 17:00:01 +0100 | [diff] [blame] | 172 | throw std::runtime_error(module + "@" + revision + " is not implemented in sysrepo."); |
| 173 | } |
| 174 | } |
Tomáš Pecka | 53f08ee | 2021-04-28 12:38:11 +0200 | [diff] [blame] | 175 | YANGPair::YANGPair(std::string xpath, std::string value) |
| 176 | : m_xpath(std::move(xpath)) |
| 177 | , m_value(std::move(value)) |
| 178 | { |
| 179 | } |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 180 | |
| 181 | void setErrors(::sysrepo::Session session, const std::string& msg) |
| 182 | { |
| 183 | session.setNetconfError(sysrepo::NetconfErrorInfo{ |
| 184 | .type = "application", |
| 185 | .tag = "operation-failed", |
| 186 | .appTag = {}, |
| 187 | .path = {}, |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 188 | .message = msg, |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 189 | .infoElements = {}, |
| 190 | }); |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 191 | session.setErrorMessage(msg); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 192 | } |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 193 | } |