Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 1 | /* |
| 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át | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 12 | leaf_data_ leafValueFromVal(const S_Val& value) |
| 13 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame^] | 14 | using namespace std::string_literals; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 15 | switch (value->type()) { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame^] | 16 | 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át | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 36 | } |
| 37 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 38 | |
| 39 | struct 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át | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 71 | SysrepoAccess::~SysrepoAccess() = default; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 72 | |
| 73 | SysrepoAccess::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 | |
| 79 | std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path) |
| 80 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame^] | 81 | using namespace std::string_literals; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 82 | std::map<std::string, leaf_data_> res; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 83 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame^] | 84 | 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át | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 91 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame^] | 92 | 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át | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 100 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 101 | |
| 102 | return res; |
| 103 | } |
| 104 | |
| 105 | void 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 | |
| 110 | void SysrepoAccess::createPresenceContainer(const std::string& path) |
| 111 | { |
| 112 | m_session->set_item(path.c_str()); |
| 113 | } |
| 114 | |
| 115 | void SysrepoAccess::deletePresenceContainer(const std::string& path) |
| 116 | { |
| 117 | m_session->delete_item(path.c_str()); |
| 118 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 119 | |
| 120 | void SysrepoAccess::commitChanges() |
| 121 | { |
| 122 | m_session->commit(); |
| 123 | } |