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