blob: af553d9d6e166b3e2eb9a944d156a62133686a0d [file] [log] [blame]
Václav Kubernát80aacc02018-08-22 17:41:54 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#include <sysrepo-cpp/Session.h>
10#include "sysrepo_access.hpp"
11
Václav Kubernátc89736b2018-08-30 16:14:05 +020012leaf_data_ leafValueFromVal(const S_Val& value)
13{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020014 using namespace std::string_literals;
Václav Kubernátc89736b2018-08-30 16:14:05 +020015 switch (value->type()) {
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020016 case SR_INT32_T:
17 return value->data()->get_int32();
18 case SR_UINT32_T:
19 return value->data()->get_uint32();
20 case SR_BOOL_T:
21 return value->data()->get_bool();
22 case SR_STRING_T:
23 return std::string(value->data()->get_string());
24 case SR_ENUM_T:
25 return std::string(value->data()->get_enum());
26 case SR_DECIMAL64_T:
27 return value->data()->get_decimal64();
28 case SR_CONTAINER_T:
29 return "(container)"s;
30 case SR_CONTAINER_PRESENCE_T:
31 return "(presence container)"s;
32 case SR_LIST_T:
33 return "(list)"s;
34 default: // TODO: implement all types
35 return value->val_to_string();
Václav Kubernátc89736b2018-08-30 16:14:05 +020036 }
37}
Václav Kubernát80aacc02018-08-22 17:41:54 +020038
39struct valFromValue : boost::static_visitor<S_Val> {
40 S_Val operator()(const enum_& value) const
41 {
42 return std::make_shared<Val>(value.m_value.c_str(), SR_ENUM_T);
43 }
44
45 S_Val operator()(const std::string& value) const
46 {
47 return std::make_shared<Val>(value.c_str());
48 }
49
50 S_Val operator()(const uint32_t& value) const
51 {
52 return std::make_shared<Val>(value, SR_UINT32_T);
53 }
54
55 S_Val operator()(const int32_t& value) const
56 {
57 return std::make_shared<Val>(value, SR_INT32_T);
58 }
59
60 S_Val operator()(const bool& value) const
61 {
62 return std::make_shared<Val>(value, SR_BOOL_T);
63 }
64
65 S_Val operator()(const double& value) const
66 {
67 return std::make_shared<Val>(value);
68 }
69};
70
Václav Kubernát812ee282018-08-30 17:10:03 +020071SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +020072
73SysrepoAccess::SysrepoAccess(const std::string& appname)
74 : m_connection(new Connection(appname.c_str()))
75{
76 m_session = std::make_shared<Session>(m_connection);
77}
78
79std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path)
80{
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020081 using namespace std::string_literals;
Václav Kubernát80aacc02018-08-22 17:41:54 +020082 std::map<std::string, leaf_data_> res;
Václav Kubernát80aacc02018-08-22 17:41:54 +020083
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020084 auto fillMap = [&res](auto items) {
85 if (!items)
86 return;
87 for (unsigned int i = 0; i < items->val_cnt(); i++) {
88 res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i)));
89 }
90 };
Václav Kubernátc89736b2018-08-30 16:14:05 +020091
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020092 if (path == "/") {
93 // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas
94 auto schemas = m_session->list_schemas();
95 for (unsigned int i = 0; i < schemas->schema_cnt(); i++) {
96 fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str()));
97 }
98 } else {
99 fillMap(m_session->get_items((path + "//.").c_str()));
Václav Kubernátc89736b2018-08-30 16:14:05 +0200100 }
Václav Kubernát80aacc02018-08-22 17:41:54 +0200101
102 return res;
103}
104
105void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
106{
107 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
108}
109
110void SysrepoAccess::createPresenceContainer(const std::string& path)
111{
112 m_session->set_item(path.c_str());
113}
114
115void SysrepoAccess::deletePresenceContainer(const std::string& path)
116{
117 m_session->delete_item(path.c_str());
118}
Václav Kubernát812ee282018-08-30 17:10:03 +0200119
120void SysrepoAccess::commitChanges()
121{
122 m_session->commit();
123}