blob: 09eb35dc2dec7db35c9502c5f6be6d5e11541e18 [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
64SysrepoAccess::~SysrepoAccess()
65{
66 m_session->commit();
67}
68
69SysrepoAccess::SysrepoAccess(const std::string& appname)
70 : m_connection(new Connection(appname.c_str()))
71{
72 m_session = std::make_shared<Session>(m_connection);
73}
74
75std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path)
76{
77 std::map<std::string, leaf_data_> res;
78 auto iterator = m_session->get_items_iter(path.c_str());
79
Václav Kubernátc89736b2018-08-30 16:14:05 +020080 if (!iterator)
81 return res;
82
83 while (auto value = m_session->get_item_next(iterator)) {
84 res.emplace(value->xpath(), leafValueFromVal(value));
85 }
Václav Kubernát80aacc02018-08-22 17:41:54 +020086
87 return res;
88}
89
90void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value)
91{
92 m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value));
93}
94
95void SysrepoAccess::createPresenceContainer(const std::string& path)
96{
97 m_session->set_item(path.c_str());
98}
99
100void SysrepoAccess::deletePresenceContainer(const std::string& path)
101{
102 m_session->delete_item(path.c_str());
103}