Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +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 <libyang/Libyang.hpp> |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 10 | #include <libyang/Tree_Data.hpp> |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 11 | #include <libyang/Tree_Schema.hpp> |
| 12 | #include <string_view> |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame^] | 13 | #include "UniqueResource.hpp" |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 14 | #include "utils.hpp" |
| 15 | #include "yang_schema.hpp" |
| 16 | |
| 17 | class YangLoadError : public std::runtime_error { |
| 18 | public: |
| 19 | using std::runtime_error::runtime_error; |
| 20 | ~YangLoadError() override = default; |
| 21 | }; |
| 22 | |
| 23 | class UnsupportedYangTypeException : public std::runtime_error { |
| 24 | public: |
| 25 | using std::runtime_error::runtime_error; |
| 26 | ~UnsupportedYangTypeException() override = default; |
| 27 | }; |
| 28 | |
| 29 | class InvalidSchemaQueryException : public std::runtime_error { |
| 30 | public: |
| 31 | using std::runtime_error::runtime_error; |
| 32 | ~InvalidSchemaQueryException() override = default; |
| 33 | }; |
| 34 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 35 | template <typename T> |
| 36 | std::string pathToYangAbsSchemPath(const T& path) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 37 | { |
| 38 | std::string res = "/"; |
| 39 | std::string currentModule; |
| 40 | |
| 41 | for (const auto& it : path.m_nodes) { |
| 42 | const auto name = nodeToSchemaString(it); |
| 43 | |
| 44 | if (it.m_suffix.type() == typeid(module_)) { |
| 45 | currentModule = name; |
| 46 | continue; |
| 47 | } else { |
| 48 | res += currentModule + ":"; |
| 49 | res += name + "/"; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return res; |
| 54 | } |
| 55 | |
| 56 | YangSchema::YangSchema() |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 57 | : m_context(std::make_shared<libyang::Context>(nullptr, LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_DISABLE_SEARCHDIR_CWD)) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 58 | { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 61 | YangSchema::YangSchema(std::shared_ptr<libyang::Context> lyCtx) |
| 62 | : m_context(lyCtx) |
| 63 | { |
| 64 | |
| 65 | } |
| 66 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 67 | YangSchema::~YangSchema() = default; |
| 68 | |
| 69 | void YangSchema::addSchemaString(const char* schema) |
| 70 | { |
| 71 | if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) { |
| 72 | throw YangLoadError("Couldn't load schema"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void YangSchema::addSchemaDirectory(const char* directoryName) |
| 77 | { |
| 78 | if (m_context->set_searchdir(directoryName)) { |
| 79 | throw YangLoadError("Couldn't add schema search directory"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void YangSchema::addSchemaFile(const char* filename) |
| 84 | { |
| 85 | if (!m_context->parse_module_path(filename, LYS_IN_YANG)) { |
| 86 | throw YangLoadError("Couldn't load schema"); |
| 87 | } |
| 88 | } |
| 89 | |
Václav Kubernát | 75877de | 2019-11-20 17:43:02 +0100 | [diff] [blame] | 90 | bool YangSchema::isModule(const std::string& name) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 91 | { |
| 92 | const auto set = modules(); |
| 93 | return set.find(name) != set.end(); |
| 94 | } |
| 95 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 96 | bool YangSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 97 | { |
| 98 | const auto schemaNode = getSchemaNode(location, node); |
| 99 | return schemaNode && schemaNode->nodetype() == LYS_CONTAINER; |
| 100 | } |
| 101 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 102 | bool YangSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 103 | { |
| 104 | const auto schemaNode = getSchemaNode(location, node); |
| 105 | return schemaNode && schemaNode->nodetype() == LYS_LEAF; |
| 106 | } |
| 107 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 108 | bool YangSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 109 | { |
| 110 | const auto schemaNode = getSchemaNode(location, node); |
| 111 | return schemaNode && schemaNode->nodetype() == LYS_LIST; |
| 112 | } |
| 113 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 114 | bool YangSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 115 | { |
| 116 | if (!isContainer(location, node)) |
| 117 | return false; |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 118 | return libyang::Schema_Node_Container(getSchemaNode(location, node)).presence(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 121 | bool YangSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 122 | { |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 123 | auto enums = enumValues(location, node); |
| 124 | |
| 125 | return std::any_of(enums.begin(), enums.end(), [=](const auto& x) { return x == value; }); |
| 126 | } |
| 127 | |
| 128 | const std::set<std::string> YangSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const |
| 129 | { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 130 | if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::Enum) |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 131 | return {}; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 132 | |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 133 | libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
Václav Kubernát | 6a713d6 | 2018-10-03 18:47:34 +0200 | [diff] [blame] | 134 | auto type = leaf.type(); |
| 135 | auto enm = type->info()->enums()->enm(); |
| 136 | // The enum can be a derived type and enm() only returns values, |
| 137 | // if that specific typedef changed the possible values. So we go |
| 138 | // up the hierarchy until we find a typedef that defined these values. |
| 139 | while (enm.empty()) { |
| 140 | type = type->der()->type(); |
| 141 | enm = type->info()->enums()->enm(); |
| 142 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 143 | |
Václav Kubernát | a38d417 | 2019-11-04 12:36:39 +0100 | [diff] [blame] | 144 | std::vector<libyang::S_Type_Enum> enabled; |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 145 | std::copy_if(enm.begin(), enm.end(), std::back_inserter(enabled), [](const libyang::S_Type_Enum& it) { |
Václav Kubernát | a38d417 | 2019-11-04 12:36:39 +0100 | [diff] [blame] | 146 | auto iffeatures = it->iffeature(); |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 147 | return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); }); |
Václav Kubernát | a38d417 | 2019-11-04 12:36:39 +0100 | [diff] [blame] | 148 | }); |
| 149 | |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 150 | std::set<std::string> enumSet; |
Václav Kubernát | a38d417 | 2019-11-04 12:36:39 +0100 | [diff] [blame] | 151 | std::transform(enabled.begin(), enabled.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return it->name(); }); |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 152 | return enumSet; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 155 | const std::set<std::string> YangSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const |
| 156 | { |
| 157 | if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::IdentityRef) |
| 158 | return {}; |
| 159 | |
| 160 | std::set<std::string> identSet; |
| 161 | |
| 162 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 163 | auto insertToSet = [&identSet, prefixes, topLevelModule](auto module, auto name) { |
| 164 | std::string stringIdent; |
| 165 | if (prefixes == Prefixes::Always || topLevelModule != module) { |
| 166 | stringIdent += module; |
| 167 | stringIdent += ":"; |
| 168 | } |
| 169 | stringIdent += name; |
| 170 | identSet.emplace(stringIdent); |
| 171 | }; |
| 172 | |
| 173 | auto leaf = std::make_shared<libyang::Schema_Node_Leaf>(getSchemaNode(location, node)); |
| 174 | auto info = leaf->type()->info(); |
| 175 | for (auto base : info->ident()->ref()) { // Iterate over all bases |
| 176 | insertToSet(base->module()->name(), base->name()); |
| 177 | // Iterate over derived identities (this is recursive!) |
| 178 | for (auto derived : base->der()->schema()) { |
| 179 | insertToSet(derived->module()->name(), derived->name()); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return identSet; |
| 184 | } |
| 185 | |
| 186 | bool YangSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const |
| 187 | { |
| 188 | auto identities = validIdentities(location, node, Prefixes::Always); |
| 189 | |
| 190 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 191 | auto identModule = value.first ? value.first.value() : topLevelModule; |
Václav Kubernát | 1bf704e | 2019-04-12 13:30:50 +0200 | [diff] [blame] | 192 | return std::any_of(identities.begin(), identities.end(), [toFind = identModule + ":" + value.second](const auto& x) { return x == toFind; }); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 195 | bool YangSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 196 | { |
| 197 | if (!isList(location, node)) |
| 198 | return false; |
| 199 | const auto keys = listKeys(location, node); |
| 200 | return keys.find(key) != keys.end(); |
| 201 | } |
| 202 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 203 | libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 204 | { |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 205 | // If no node is found find_path prints an error message, so we have to |
| 206 | // disable logging |
| 207 | // https://github.com/CESNET/libyang/issues/753 |
| 208 | { |
| 209 | int oldOptions; |
| 210 | auto logBlocker = make_unique_resource( |
| 211 | [&oldOptions]() { |
| 212 | oldOptions = libyang::set_log_options(0); |
| 213 | }, |
| 214 | [&oldOptions]() { |
| 215 | libyang::set_log_options(oldOptions); |
| 216 | }); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 217 | return m_context->get_node(nullptr, node.c_str()); |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 218 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 221 | |
| 222 | libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const |
| 223 | { |
| 224 | return impl_getSchemaNode(node); |
| 225 | } |
| 226 | |
| 227 | libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const |
| 228 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 229 | std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node)); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 230 | |
| 231 | return impl_getSchemaNode(absPath); |
| 232 | } |
| 233 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 234 | const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 235 | { |
| 236 | std::set<std::string> keys; |
| 237 | if (!isList(location, node)) |
| 238 | return keys; |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 239 | libyang::Schema_Node_List list(getSchemaNode(location, node)); |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 240 | const auto& keysVec = list.keys(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 241 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 242 | std::transform(keysVec.begin(), keysVec.end(), std::inserter(keys, keys.begin()), [](const auto& it) { return it->name(); }); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 243 | return keys; |
| 244 | } |
| 245 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 246 | yang::LeafDataTypes lyTypeToLeafDataTypes(LY_DATA_TYPE type) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 247 | { |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 248 | switch (type) { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 249 | case LY_TYPE_STRING: |
| 250 | return yang::LeafDataTypes::String; |
| 251 | case LY_TYPE_DEC64: |
| 252 | return yang::LeafDataTypes::Decimal; |
| 253 | case LY_TYPE_BOOL: |
| 254 | return yang::LeafDataTypes::Bool; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 255 | case LY_TYPE_INT8: |
| 256 | return yang::LeafDataTypes::Int8; |
| 257 | case LY_TYPE_INT16: |
| 258 | return yang::LeafDataTypes::Int16; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 259 | case LY_TYPE_INT32: |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 260 | return yang::LeafDataTypes::Int32; |
| 261 | case LY_TYPE_INT64: |
| 262 | return yang::LeafDataTypes::Int64; |
| 263 | case LY_TYPE_UINT8: |
| 264 | return yang::LeafDataTypes::Uint8; |
| 265 | case LY_TYPE_UINT16: |
| 266 | return yang::LeafDataTypes::Uint16; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 267 | case LY_TYPE_UINT32: |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 268 | return yang::LeafDataTypes::Uint32; |
| 269 | case LY_TYPE_UINT64: |
| 270 | return yang::LeafDataTypes::Uint64; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 271 | case LY_TYPE_ENUM: |
| 272 | return yang::LeafDataTypes::Enum; |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 273 | case LY_TYPE_BINARY: |
| 274 | return yang::LeafDataTypes::Binary; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 275 | case LY_TYPE_IDENT: |
| 276 | return yang::LeafDataTypes::IdentityRef; |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 277 | case LY_TYPE_LEAFREF: |
| 278 | return yang::LeafDataTypes::LeafRef; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 279 | default: |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 280 | using namespace std::string_literals; |
| 281 | throw std::logic_error{"internal error: unsupported libyang data type "s + std::to_string(type)}; |
| 282 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const |
| 286 | { |
| 287 | using namespace std::string_literals; |
| 288 | if (!isLeaf(location, node)) |
| 289 | throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf"); |
| 290 | |
| 291 | libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
| 292 | auto baseType{leaf.type()->base()}; |
| 293 | try { |
| 294 | return lyTypeToLeafDataTypes(baseType); |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 295 | } catch (std::logic_error& ex) { |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 296 | throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what()); |
| 297 | } |
| 298 | } |
| 299 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 300 | yang::LeafDataTypes YangSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 301 | { |
| 302 | using namespace std::string_literals; |
| 303 | libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
| 304 | try { |
| 305 | return lyTypeToLeafDataTypes(leaf.type()->info()->lref()->target()->type()->base()); |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 306 | } catch (std::logic_error& ex) { |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 307 | throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what()); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | std::set<std::string> YangSchema::modules() const |
| 312 | { |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 313 | const auto& modules = m_context->get_module_iter(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 314 | |
| 315 | std::set<std::string> res; |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 316 | std::transform(modules.begin(), modules.end(), std::inserter(res, res.end()), [](const auto module) { return module->name(); }); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 317 | return res; |
| 318 | } |
| 319 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 320 | std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 321 | { |
| 322 | using namespace std::string_view_literals; |
| 323 | std::set<std::string> res; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 324 | std::vector<libyang::S_Schema_Node> nodes; |
| 325 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 326 | if (path.m_nodes.empty()) { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 327 | nodes = m_context->data_instantiables(0); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 328 | } else { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 329 | const auto pathString = pathToSchemaString(path, Prefixes::Always); |
| 330 | const auto node = getSchemaNode(pathString); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 331 | nodes = node->child_instantiables(0); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 332 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 333 | |
| 334 | for (const auto node : nodes) { |
| 335 | if (node->module()->name() == "ietf-yang-library"sv) |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 336 | continue; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 337 | if (recursion == Recursion::Recursive) { |
| 338 | for (auto it : node->tree_dfs()) { |
| 339 | res.insert(it->path(LYS_PATH_FIRST_PREFIX)); |
| 340 | } |
| 341 | } else { |
Václav Kubernát | 4f77a25 | 2019-02-19 16:51:30 +0100 | [diff] [blame] | 342 | std::string toInsert; |
| 343 | if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) { |
| 344 | toInsert += node->module()->name(); |
| 345 | toInsert += ":"; |
| 346 | } |
| 347 | toInsert += node->name(); |
| 348 | res.insert(toInsert); |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 352 | return res; |
| 353 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 354 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 355 | std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const |
| 356 | { |
| 357 | std::set<std::string> res; |
| 358 | const auto yangModule = m_context->get_module(module.m_name.c_str()); |
| 359 | |
| 360 | std::vector<libyang::S_Schema_Node> nodes; |
| 361 | |
| 362 | for (const auto node : yangModule->data_instantiables(0)) { |
| 363 | if (recursion == Recursion::Recursive) { |
| 364 | for (const auto it : node->tree_dfs()) { |
| 365 | res.insert(it->path(LYS_PATH_FIRST_PREFIX)); |
| 366 | } |
| 367 | } else { |
| 368 | res.insert(module.m_name + ":" + node->name()); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return res; |
| 373 | } |
| 374 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 375 | void YangSchema::loadModule(const std::string& moduleName) |
| 376 | { |
| 377 | m_context->load_module(moduleName.c_str()); |
| 378 | } |
| 379 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 380 | void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName) |
| 381 | { |
| 382 | m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str()); |
| 383 | } |
| 384 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 385 | void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*, const char*)>& clb) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 386 | { |
| 387 | auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) { |
| 388 | (void)submod_revision; |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 389 | auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 390 | if (moduleSource.empty()) { |
| 391 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr}; |
| 392 | } |
| 393 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())}; |
| 394 | }; |
| 395 | |
| 396 | auto deleter = [](void* data) { |
| 397 | free(data); |
| 398 | }; |
| 399 | m_context->add_missing_module_callback(lambda, deleter); |
| 400 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 401 | |
| 402 | std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const |
| 403 | { |
| 404 | return std::make_shared<libyang::Data_Node>(m_context, |
| 405 | path.c_str(), |
| 406 | value ? value.value().c_str() : nullptr, |
| 407 | LYD_ANYDATA_CONSTSTRING, |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 408 | LYD_PATH_OPT_EDIT); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name) |
| 412 | { |
| 413 | return m_context->get_module(name.c_str(), nullptr, 0); |
| 414 | } |