blob: 76d79ab0c9607bcc285cb1659d3471a1c253a88b [file] [log] [blame]
Tomáš Peckaba2dc312021-01-23 22:29:11 +01001/*
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
12extern "C" {
13#include <sysrepo.h>
14}
15
16extern "C" {
17/** @short Propagate sysrepo events to spdlog */
18static 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
44namespace velia::utils {
45
46/** @short Setup sysrepo log forwarding
47You must call cla::utils::initLogs prior to this function.
48*/
49void initLogsSysrepo()
50{
51 sr_log_set_cb(spdlog_sr_log_cb);
52}
53
Tomáš Pecka498e91c2021-03-02 17:46:47 +010054void 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áš Peckaba2dc312021-01-23 22:29:11 +010055{
Tomáš Pecka498e91c2021-03-02 17:46:47 +010056 auto netconf = session->get_context()->get_module("ietf-netconf");
Tomáš Pecka5b293f42021-03-02 17:47:03 +010057 auto log = spdlog::get("main");
Tomáš Pecka498e91c2021-03-02 17:46:47 +010058
59 for (const auto& propertyName : removePaths) {
Tomáš Pecka5b293f42021-03-02 17:47:03 +010060 log->trace("Processing node deletion {}", propertyName);
61
Tomáš Pecka498e91c2021-03-02 17:46:47 +010062 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áš Peckaba2dc312021-01-23 22:29:11 +010085 for (const auto& [propertyName, value] : values) {
Tomáš Pecka5b293f42021-03-02 17:47:03 +010086 log->trace("Processing node update {} -> {}", propertyName, value);
87
Tomáš Peckaba2dc312021-01-23 22:29:11 +010088 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áš Pecka498e91c2021-03-02 17:46:47 +0100106/** @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. */
107void 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áš Pecka272abaf2021-01-24 12:28:43 +0100108{
109 sr_datastore_t oldDatastore = session->session_get_ds();
110 session->session_switch_ds(datastore);
111
Tomáš Pecka498e91c2021-03-02 17:46:47 +0100112 valuesPush(values, removePaths, session);
Tomáš Pecka272abaf2021-01-24 12:28:43 +0100113
114 session->apply_changes();
115 session->session_switch_ds(oldDatastore);
116}
117
Tomáš Pecka498e91c2021-03-02 17:46:47 +0100118/** @brief Set or remove paths in Sysrepo's current datastore. */
119void valuesPush(const std::map<std::string, std::string>& values, const std::vector<std::string>& removePaths, std::shared_ptr<::sysrepo::Session> session)
Tomáš Pecka272abaf2021-01-24 12:28:43 +0100120{
Tomáš Peckadaedba52021-03-08 12:52:20 +0100121 if (values.empty() && removePaths.empty()) return;
122
Tomáš Pecka272abaf2021-01-24 12:28:43 +0100123 libyang::S_Data_Node edit;
Tomáš Pecka498e91c2021-03-02 17:46:47 +0100124 valuesToYang(values, removePaths, session, edit);
Tomáš Pecka272abaf2021-01-24 12:28:43 +0100125
126 session->edit_batch(edit, "merge");
127 session->apply_changes();
128}
129
Václav Kubernát6fa7f342021-01-26 17:00:01 +0100130/** @brief Checks whether a module is implemented in Sysrepo and throws if not. */
131void 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áš Peckaba2dc312021-01-23 22:29:11 +0100137}