blob: 54fa1fd407572f5a73526cdce22d4e11f36c765a [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{
14 switch (value->type()) {
15 case SR_INT32_T:
16 return value->data()->get_int32();
17 case SR_UINT32_T:
18 return value->data()->get_uint32();
19 case SR_BOOL_T:
20 return value->data()->get_bool();
21 case SR_STRING_T:
22 return std::string(value->data()->get_string());
23 case SR_ENUM_T:
24 return std::string(value->data()->get_enum());
25 case SR_DECIMAL64_T:
26 return value->data()->get_decimal64();
27 default: // TODO: implement all types
28 throw std::runtime_error("This type is not yet implemented");
29 }
30}
Václav Kubernát80aacc02018-08-22 17:41:54 +020031
32struct valFromValue : boost::static_visitor<S_Val> {
33 S_Val operator()(const enum_& value) const
34 {
35 return std::make_shared<Val>(value.m_value.c_str(), SR_ENUM_T);
36 }
37
38 S_Val operator()(const std::string& value) const
39 {
40 return std::make_shared<Val>(value.c_str());
41 }
42
43 S_Val operator()(const uint32_t& value) const
44 {
45 return std::make_shared<Val>(value, SR_UINT32_T);
46 }
47
48 S_Val operator()(const int32_t& value) const
49 {
50 return std::make_shared<Val>(value, SR_INT32_T);
51 }
52
53 S_Val operator()(const bool& value) const
54 {
55 return std::make_shared<Val>(value, SR_BOOL_T);
56 }
57
58 S_Val operator()(const double& value) const
59 {
60 return std::make_shared<Val>(value);
61 }
62};
63
Václav Kubernát812ee282018-08-30 17:10:03 +020064SysrepoAccess::~SysrepoAccess() = default;
Václav Kubernát80aacc02018-08-22 17:41:54 +020065
66SysrepoAccess::SysrepoAccess(const std::string& appname)
67 : m_connection(new Connection(appname.c_str()))
68{
69 m_session = std::make_shared<Session>(m_connection);
70}
71
72std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path)
73{
74 std::map<std::string, leaf_data_> res;
75 auto iterator = m_session->get_items_iter(path.c_str());
76
Václav Kubernátc89736b2018-08-30 16:14:05 +020077 if (!iterator)
78 return res;
79
80 while (auto value = m_session->get_item_next(iterator)) {
81 res.emplace(value->xpath(), leafValueFromVal(value));
82 }
Václav Kubernát80aacc02018-08-22 17:41:54 +020083
84 return res;
85}
86
87void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
88{
89 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
90}
91
92void SysrepoAccess::createPresenceContainer(const std::string& path)
93{
94 m_session->set_item(path.c_str());
95}
96
97void SysrepoAccess::deletePresenceContainer(const std::string& path)
98{
99 m_session->delete_item(path.c_str());
100}
Václav Kubernát812ee282018-08-30 17:10:03 +0200101
102void SysrepoAccess::commitChanges()
103{
104 m_session->commit();
105}