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 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 9 | #include <sysrepo-cpp/Session.hpp> |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 10 | #include "sysrepo_access.hpp" |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 11 | #include "yang_schema.hpp" |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 12 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 13 | leaf_data_ leafValueFromVal(const sysrepo::S_Val& value) |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 14 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 15 | using namespace std::string_literals; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 16 | switch (value->type()) { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 17 | case SR_INT32_T: |
| 18 | return value->data()->get_int32(); |
| 19 | case SR_UINT32_T: |
| 20 | return value->data()->get_uint32(); |
| 21 | case SR_BOOL_T: |
| 22 | return value->data()->get_bool(); |
| 23 | case SR_STRING_T: |
| 24 | return std::string(value->data()->get_string()); |
| 25 | case SR_ENUM_T: |
| 26 | return std::string(value->data()->get_enum()); |
| 27 | case SR_DECIMAL64_T: |
| 28 | return value->data()->get_decimal64(); |
| 29 | case SR_CONTAINER_T: |
| 30 | return "(container)"s; |
| 31 | case SR_CONTAINER_PRESENCE_T: |
| 32 | return "(presence container)"s; |
| 33 | case SR_LIST_T: |
| 34 | return "(list)"s; |
| 35 | default: // TODO: implement all types |
| 36 | return value->val_to_string(); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 37 | } |
| 38 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 39 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 40 | struct valFromValue : boost::static_visitor<sysrepo::S_Val> { |
| 41 | sysrepo::S_Val operator()(const enum_& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 42 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 43 | return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_ENUM_T); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 46 | sysrepo::S_Val operator()(const binary_& value) const |
| 47 | { |
| 48 | return std::make_shared<sysrepo::Val>(value.m_value.c_str(), SR_BINARY_T); |
| 49 | } |
| 50 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 51 | sysrepo::S_Val operator()(const identityRef_& value) const |
| 52 | { |
| 53 | auto res = value.m_prefix.value().m_name + ":" + value.m_value; |
| 54 | return std::make_shared<sysrepo::Val>(res.c_str(), SR_IDENTITYREF_T); |
| 55 | } |
| 56 | |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 57 | sysrepo::S_Val operator()(const std::string& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 58 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 59 | return std::make_shared<sysrepo::Val>(value.c_str()); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Jan Kundrát | bd17836 | 2019-02-05 19:00:04 +0100 | [diff] [blame] | 62 | template <typename T> |
| 63 | sysrepo::S_Val operator()(const T& value) const |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 64 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 65 | return std::make_shared<sysrepo::Val>(value); |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 66 | } |
| 67 | }; |
| 68 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 69 | SysrepoAccess::~SysrepoAccess() = default; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 70 | |
| 71 | SysrepoAccess::SysrepoAccess(const std::string& appname) |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 72 | : m_connection(new sysrepo::Connection(appname.c_str())) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 73 | , m_schema(new YangSchema()) |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 74 | { |
Jan Kundrát | 68d4a2c | 2018-10-01 17:17:09 +0200 | [diff] [blame] | 75 | m_session = std::make_shared<sysrepo::Session>(m_connection); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 76 | m_schema->registerModuleCallback([this](const char* moduleName, const char* revision, const char* submodule) { |
| 77 | return fetchSchema(moduleName, revision, submodule); |
| 78 | }); |
| 79 | |
| 80 | for (const auto& it : listImplementedSchemas()) { |
| 81 | m_schema->loadModule(it); |
| 82 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | std::map<std::string, leaf_data_> SysrepoAccess::getItems(const std::string& path) |
| 86 | { |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 87 | using namespace std::string_literals; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 88 | std::map<std::string, leaf_data_> res; |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 89 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 90 | auto fillMap = [&res](auto items) { |
| 91 | if (!items) |
| 92 | return; |
| 93 | for (unsigned int i = 0; i < items->val_cnt(); i++) { |
| 94 | res.emplace(items->val(i)->xpath(), leafValueFromVal(items->val(i))); |
| 95 | } |
| 96 | }; |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 97 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 98 | if (path == "/") { |
| 99 | // Sysrepo doesn't have a root node ("/"), so we take all top-level nodes from all schemas |
| 100 | auto schemas = m_session->list_schemas(); |
| 101 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 102 | fillMap(m_session->get_items(("/"s + schemas->schema(i)->module_name() + ":*//.").c_str())); |
| 103 | } |
| 104 | } else { |
| 105 | fillMap(m_session->get_items((path + "//.").c_str())); |
Václav Kubernát | c89736b | 2018-08-30 16:14:05 +0200 | [diff] [blame] | 106 | } |
Václav Kubernát | 80aacc0 | 2018-08-22 17:41:54 +0200 | [diff] [blame] | 107 | |
| 108 | return res; |
| 109 | } |
| 110 | |
| 111 | void SysrepoAccess::setLeaf(const std::string& path, leaf_data_ value) |
| 112 | { |
| 113 | m_session->set_item(path.c_str(), boost::apply_visitor(valFromValue(), value)); |
| 114 | } |
| 115 | |
| 116 | void SysrepoAccess::createPresenceContainer(const std::string& path) |
| 117 | { |
| 118 | m_session->set_item(path.c_str()); |
| 119 | } |
| 120 | |
| 121 | void SysrepoAccess::deletePresenceContainer(const std::string& path) |
| 122 | { |
| 123 | m_session->delete_item(path.c_str()); |
| 124 | } |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 125 | |
Václav Kubernát | f5f64f0 | 2019-03-19 17:15:47 +0100 | [diff] [blame] | 126 | void SysrepoAccess::createListInstance(const std::string& path) |
| 127 | { |
| 128 | m_session->set_item(path.c_str()); |
| 129 | } |
| 130 | |
| 131 | void SysrepoAccess::deleteListInstance(const std::string& path) |
| 132 | { |
| 133 | m_session->delete_item(path.c_str()); |
| 134 | } |
| 135 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 136 | void SysrepoAccess::commitChanges() |
| 137 | { |
| 138 | m_session->commit(); |
| 139 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 140 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 141 | void SysrepoAccess::discardChanges() |
| 142 | { |
| 143 | m_session->discard_changes(); |
| 144 | } |
| 145 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 146 | std::string SysrepoAccess::fetchSchema(const char* module, const char* revision, const char* submodule) |
| 147 | { |
| 148 | auto schema = m_session->get_schema(module, revision, submodule, SR_SCHEMA_YANG); // FIXME: maybe we should use get_submodule_schema for submodules? |
| 149 | if (schema.empty()) |
| 150 | throw std::runtime_error(std::string("Module ") + module + " not available"); |
| 151 | |
| 152 | return schema; |
| 153 | } |
| 154 | |
| 155 | std::vector<std::string> SysrepoAccess::listImplementedSchemas() |
| 156 | { |
| 157 | std::vector<std::string> res; |
| 158 | auto schemas = m_session->list_schemas(); |
| 159 | for (unsigned int i = 0; i < schemas->schema_cnt(); i++) { |
| 160 | auto schema = schemas->schema(i); |
| 161 | if (schema->implemented()) |
| 162 | res.push_back(schema->module_name()); |
| 163 | } |
| 164 | return res; |
| 165 | } |
| 166 | |
| 167 | std::shared_ptr<Schema> SysrepoAccess::schema() |
| 168 | { |
| 169 | return m_schema; |
| 170 | } |