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 | |
| 44 | namespace velia::utils { |
| 45 | |
| 46 | /** @short Setup sysrepo log forwarding |
| 47 | You must call cla::utils::initLogs prior to this function. |
| 48 | */ |
| 49 | void initLogsSysrepo() |
| 50 | { |
| 51 | sr_log_set_cb(spdlog_sr_log_cb); |
| 52 | } |
| 53 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 54 | void valuesToYang(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, std::shared_ptr<::sysrepo::Session> session, std::shared_ptr<libyang::Data_Node>& parent) |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 55 | { |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 56 | auto netconf = session->get_context()->get_module("ietf-netconf"); |
Tomáš Pecka | 5b293f4 | 2021-03-02 17:47:03 +0100 | [diff] [blame] | 57 | auto log = spdlog::get("main"); |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 58 | |
| 59 | for (const auto& propertyName : removePaths) { |
Tomáš Pecka | 5b293f4 | 2021-03-02 17:47:03 +0100 | [diff] [blame] | 60 | log->trace("Processing node deletion {}", propertyName); |
| 61 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 62 | if (!parent) { |
| 63 | parent = std::make_shared<libyang::Data_Node>( |
| 64 | session->get_context(), |
| 65 | propertyName.c_str(), |
| 66 | nullptr, |
| 67 | LYD_ANYDATA_CONSTSTRING, |
| 68 | LYD_PATH_OPT_EDIT); |
| 69 | } else { |
| 70 | parent->new_path( |
| 71 | session->get_context(), |
| 72 | propertyName.c_str(), |
| 73 | nullptr, |
| 74 | LYD_ANYDATA_CONSTSTRING, |
| 75 | LYD_PATH_OPT_EDIT); |
| 76 | } |
| 77 | |
| 78 | auto deletion = parent->find_path(propertyName.c_str()); |
| 79 | if (deletion->number() != 1) { |
| 80 | throw std::logic_error {"Cannot find XPath " + propertyName + " for deletion in libyang's new_path() output"}; |
| 81 | } |
| 82 | deletion->data()[0]->insert_attr(netconf, "operation", "remove"); |
| 83 | } |
| 84 | |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 85 | for (const auto& [propertyName, value] : values) { |
Tomáš Pecka | 5b293f4 | 2021-03-02 17:47:03 +0100 | [diff] [blame] | 86 | log->trace("Processing node update {} -> {}", propertyName, value); |
| 87 | |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 88 | if (!parent) { |
| 89 | parent = std::make_shared<libyang::Data_Node>( |
| 90 | session->get_context(), |
| 91 | propertyName.c_str(), |
| 92 | value.c_str(), |
| 93 | LYD_ANYDATA_CONSTSTRING, |
| 94 | LYD_PATH_OPT_OUTPUT); |
| 95 | } else { |
| 96 | parent->new_path( |
| 97 | session->get_context(), |
| 98 | propertyName.c_str(), |
| 99 | value.c_str(), |
| 100 | LYD_ANYDATA_CONSTSTRING, |
| 101 | LYD_PATH_OPT_OUTPUT); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 106 | /** @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. */ |
| 107 | void valuesPush(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, std::shared_ptr<::sysrepo::Session> session, sr_datastore_t datastore) |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 108 | { |
| 109 | sr_datastore_t oldDatastore = session->session_get_ds(); |
| 110 | session->session_switch_ds(datastore); |
| 111 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 112 | valuesPush(values, removePaths, session); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 113 | |
| 114 | session->apply_changes(); |
| 115 | session->session_switch_ds(oldDatastore); |
| 116 | } |
| 117 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 118 | /** @brief Set or remove paths in Sysrepo's current datastore. */ |
| 119 | void valuesPush(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, std::shared_ptr<::sysrepo::Session> session) |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 120 | { |
Tomáš Pecka | daedba5 | 2021-03-08 12:52:20 +0100 | [diff] [blame] | 121 | if (values.empty() && removePaths.empty()) return; |
| 122 | |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 123 | libyang::S_Data_Node edit; |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 124 | valuesToYang(values, removePaths, session, edit); |
Tomáš Pecka | 272abaf | 2021-01-24 12:28:43 +0100 | [diff] [blame] | 125 | |
| 126 | session->edit_batch(edit, "merge"); |
| 127 | session->apply_changes(); |
| 128 | } |
| 129 | |
Václav Kubernát | 6fa7f34 | 2021-01-26 17:00:01 +0100 | [diff] [blame] | 130 | /** @brief Checks whether a module is implemented in Sysrepo and throws if not. */ |
| 131 | void ensureModuleImplemented(std::shared_ptr<::sysrepo::Session> session, const std::string& module, const std::string& revision) |
| 132 | { |
| 133 | if (auto mod = session->get_context()->get_module(module.c_str(), revision.c_str()); !mod || !mod->implemented()) { |
| 134 | throw std::runtime_error(module + "@" + revision + " is not implemented in sysrepo."); |
| 135 | } |
| 136 | } |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 137 | } |