blob: bd1b50693ec4ea4b2833ebff03bbe998302e2f92 [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
54void valuesToYang(const std::map<std::string, std::string>& values, std::shared_ptr<::sysrepo::Session> session, std::shared_ptr<libyang::Data_Node>& parent)
55{
56 for (const auto& [propertyName, value] : values) {
Tomáš Peckaba2dc312021-01-23 22:29:11 +010057 if (!parent) {
58 parent = std::make_shared<libyang::Data_Node>(
59 session->get_context(),
60 propertyName.c_str(),
61 value.c_str(),
62 LYD_ANYDATA_CONSTSTRING,
63 LYD_PATH_OPT_OUTPUT);
64 } else {
65 parent->new_path(
66 session->get_context(),
67 propertyName.c_str(),
68 value.c_str(),
69 LYD_ANYDATA_CONSTSTRING,
70 LYD_PATH_OPT_OUTPUT);
71 }
72 }
73}
74
Tomáš Pecka272abaf2021-01-24 12:28:43 +010075/** @brief Set values into Sysrepo's specified datastore. It changes the datastore and after the data are applied, the original datastore is restored. */
76void valuesPush(const std::map<std::string, std::string>& values, std::shared_ptr<::sysrepo::Session> session, sr_datastore_t datastore)
77{
78 sr_datastore_t oldDatastore = session->session_get_ds();
79 session->session_switch_ds(datastore);
80
81 valuesPush(values, session);
82
83 session->apply_changes();
84 session->session_switch_ds(oldDatastore);
85}
86
87/** @brief Set values into Sysrepo's current datastore. */
88void valuesPush(const std::map<std::string, std::string>& values, std::shared_ptr<::sysrepo::Session> session)
89{
90 libyang::S_Data_Node edit;
91 valuesToYang(values, session, edit);
92
93 session->edit_batch(edit, "merge");
94 session->apply_changes();
95}
96
Václav Kubernát6fa7f342021-01-26 17:00:01 +010097/** @brief Checks whether a module is implemented in Sysrepo and throws if not. */
98void ensureModuleImplemented(std::shared_ptr<::sysrepo::Session> session, const std::string& module, const std::string& revision)
99{
100 if (auto mod = session->get_context()->get_module(module.c_str(), revision.c_str()); !mod || !mod->implemented()) {
101 throw std::runtime_error(module + "@" + revision + " is not implemented in sysrepo.");
102 }
103}
Tomáš Peckaba2dc312021-01-23 22:29:11 +0100104}