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::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] | 97 | { |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 98 | auto enums = enumValues(location, node); |
| 99 | |
| 100 | return std::any_of(enums.begin(), enums.end(), [=](const auto& x) { return x == value; }); |
| 101 | } |
| 102 | |
| 103 | const std::set<std::string> YangSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const |
| 104 | { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 105 | if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::Enum) |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 106 | return {}; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 107 | |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 108 | libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node)); |
Václav Kubernát | 6a713d6 | 2018-10-03 18:47:34 +0200 | [diff] [blame] | 109 | auto type = leaf.type(); |
| 110 | auto enm = type->info()->enums()->enm(); |
| 111 | // The enum can be a derived type and enm() only returns values, |
| 112 | // if that specific typedef changed the possible values. So we go |
| 113 | // up the hierarchy until we find a typedef that defined these values. |
| 114 | while (enm.empty()) { |
| 115 | type = type->der()->type(); |
| 116 | enm = type->info()->enums()->enm(); |
| 117 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 118 | |
Václav Kubernát | a38d417 | 2019-11-04 12:36:39 +0100 | [diff] [blame] | 119 | std::vector<libyang::S_Type_Enum> enabled; |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 120 | 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] | 121 | auto iffeatures = it->iffeature(); |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 122 | 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] | 123 | }); |
| 124 | |
Václav Kubernát | 989b5de | 2019-02-20 16:28:35 +0100 | [diff] [blame] | 125 | std::set<std::string> enumSet; |
Václav Kubernát | a38d417 | 2019-11-04 12:36:39 +0100 | [diff] [blame] | 126 | 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] | 127 | return enumSet; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 130 | const std::set<std::string> YangSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const |
| 131 | { |
| 132 | if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::IdentityRef) |
| 133 | return {}; |
| 134 | |
| 135 | std::set<std::string> identSet; |
| 136 | |
| 137 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 138 | auto insertToSet = [&identSet, prefixes, topLevelModule](auto module, auto name) { |
| 139 | std::string stringIdent; |
| 140 | if (prefixes == Prefixes::Always || topLevelModule != module) { |
| 141 | stringIdent += module; |
| 142 | stringIdent += ":"; |
| 143 | } |
| 144 | stringIdent += name; |
| 145 | identSet.emplace(stringIdent); |
| 146 | }; |
| 147 | |
| 148 | auto leaf = std::make_shared<libyang::Schema_Node_Leaf>(getSchemaNode(location, node)); |
| 149 | auto info = leaf->type()->info(); |
| 150 | for (auto base : info->ident()->ref()) { // Iterate over all bases |
| 151 | insertToSet(base->module()->name(), base->name()); |
| 152 | // Iterate over derived identities (this is recursive!) |
| 153 | for (auto derived : base->der()->schema()) { |
| 154 | insertToSet(derived->module()->name(), derived->name()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return identSet; |
| 159 | } |
| 160 | |
| 161 | bool YangSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const |
| 162 | { |
| 163 | auto identities = validIdentities(location, node, Prefixes::Always); |
| 164 | |
| 165 | auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name; |
| 166 | auto identModule = value.first ? value.first.value() : topLevelModule; |
Václav Kubernát | 1bf704e | 2019-04-12 13:30:50 +0200 | [diff] [blame] | 167 | 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] | 168 | } |
| 169 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 170 | 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] | 171 | { |
| 172 | if (!isList(location, node)) |
| 173 | return false; |
| 174 | const auto keys = listKeys(location, node); |
| 175 | return keys.find(key) != keys.end(); |
| 176 | } |
| 177 | |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 178 | bool YangSchema::leafIsKey(const std::string& leafPath) const |
| 179 | { |
| 180 | auto node = getSchemaNode(leafPath); |
| 181 | if (!node || node->nodetype() != LYS_LEAF) |
| 182 | return false; |
| 183 | |
| 184 | return libyang::Schema_Node_Leaf{node}.is_key().get(); |
| 185 | } |
| 186 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 187 | 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] | 188 | { |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 189 | // If no node is found find_path prints an error message, so we have to |
| 190 | // disable logging |
| 191 | // https://github.com/CESNET/libyang/issues/753 |
| 192 | { |
| 193 | int oldOptions; |
| 194 | auto logBlocker = make_unique_resource( |
| 195 | [&oldOptions]() { |
| 196 | oldOptions = libyang::set_log_options(0); |
| 197 | }, |
| 198 | [&oldOptions]() { |
| 199 | libyang::set_log_options(oldOptions); |
| 200 | }); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 201 | return m_context->get_node(nullptr, node.c_str()); |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 202 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 205 | |
| 206 | libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const |
| 207 | { |
| 208 | return impl_getSchemaNode(node); |
| 209 | } |
| 210 | |
| 211 | libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const |
| 212 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 213 | 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] | 214 | |
| 215 | return impl_getSchemaNode(absPath); |
| 216 | } |
| 217 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 218 | 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] | 219 | { |
| 220 | std::set<std::string> keys; |
| 221 | if (!isList(location, node)) |
| 222 | return keys; |
Jan Kundrát | 4ed0e9f | 2018-08-23 16:56:58 +0200 | [diff] [blame] | 223 | libyang::Schema_Node_List list(getSchemaNode(location, node)); |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 224 | const auto& keysVec = list.keys(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 225 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 226 | 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] | 227 | return keys; |
| 228 | } |
| 229 | |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 230 | yang::LeafDataTypes lyTypeToLeafDataTypes(LY_DATA_TYPE type) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 231 | { |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 232 | switch (type) { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 233 | case LY_TYPE_STRING: |
| 234 | return yang::LeafDataTypes::String; |
| 235 | case LY_TYPE_DEC64: |
| 236 | return yang::LeafDataTypes::Decimal; |
| 237 | case LY_TYPE_BOOL: |
| 238 | return yang::LeafDataTypes::Bool; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 239 | case LY_TYPE_INT8: |
| 240 | return yang::LeafDataTypes::Int8; |
| 241 | case LY_TYPE_INT16: |
| 242 | return yang::LeafDataTypes::Int16; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 243 | case LY_TYPE_INT32: |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 244 | return yang::LeafDataTypes::Int32; |
| 245 | case LY_TYPE_INT64: |
| 246 | return yang::LeafDataTypes::Int64; |
| 247 | case LY_TYPE_UINT8: |
| 248 | return yang::LeafDataTypes::Uint8; |
| 249 | case LY_TYPE_UINT16: |
| 250 | return yang::LeafDataTypes::Uint16; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 251 | case LY_TYPE_UINT32: |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 252 | return yang::LeafDataTypes::Uint32; |
| 253 | case LY_TYPE_UINT64: |
| 254 | return yang::LeafDataTypes::Uint64; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 255 | case LY_TYPE_ENUM: |
| 256 | return yang::LeafDataTypes::Enum; |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 257 | case LY_TYPE_BINARY: |
| 258 | return yang::LeafDataTypes::Binary; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 259 | case LY_TYPE_IDENT: |
| 260 | return yang::LeafDataTypes::IdentityRef; |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 261 | case LY_TYPE_LEAFREF: |
| 262 | return yang::LeafDataTypes::LeafRef; |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 263 | default: |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 264 | using namespace std::string_literals; |
| 265 | throw std::logic_error{"internal error: unsupported libyang data type "s + std::to_string(type)}; |
| 266 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 269 | namespace { |
| 270 | yang::LeafDataTypes impl_leafType(const libyang::S_Schema_Node& node) |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 271 | { |
| 272 | using namespace std::string_literals; |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 273 | libyang::Schema_Node_Leaf leaf(node); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 274 | auto baseType{leaf.type()->base()}; |
| 275 | try { |
| 276 | return lyTypeToLeafDataTypes(baseType); |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 277 | } catch (std::logic_error& ex) { |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 278 | throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + ex.what()); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 279 | } |
| 280 | } |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const |
| 284 | { |
| 285 | return impl_leafType(getSchemaNode(location, node)); |
| 286 | } |
| 287 | |
| 288 | yang::LeafDataTypes YangSchema::leafType(const std::string& path) const |
| 289 | { |
| 290 | return impl_leafType(getSchemaNode(path)); |
| 291 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 292 | |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame^] | 293 | std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const |
| 294 | { |
| 295 | libyang::Schema_Node_Leaf leaf(getSchemaNode(path)); |
| 296 | return leaf.type()->der().get() ? std::optional{leaf.type()->der()->name()} : std::nullopt; |
| 297 | } |
| 298 | |
Václav Kubernát | ad87ece | 2020-02-19 15:20:56 +0100 | [diff] [blame] | 299 | namespace { |
| 300 | yang::LeafDataTypes impl_leafrefBaseType(const libyang::S_Schema_Node& node) |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 301 | { |
| 302 | using namespace std::string_literals; |
Václav Kubernát | ad87ece | 2020-02-19 15:20:56 +0100 | [diff] [blame] | 303 | libyang::Schema_Node_Leaf leaf(node); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 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 | ad87ece | 2020-02-19 15:20:56 +0100 | [diff] [blame] | 307 | throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + ex.what()); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 308 | } |
| 309 | } |
Václav Kubernát | ad87ece | 2020-02-19 15:20:56 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | yang::LeafDataTypes YangSchema::leafrefBaseType(const schemaPath_& location, const ModuleNodePair& node) const |
| 313 | { |
| 314 | return impl_leafrefBaseType(getSchemaNode(location, node)); |
| 315 | } |
| 316 | |
| 317 | yang::LeafDataTypes YangSchema::leafrefBaseType(const std::string& path) const |
| 318 | { |
| 319 | return impl_leafrefBaseType(getSchemaNode(path)); |
| 320 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 321 | |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 322 | std::string YangSchema::leafrefPath(const std::string& leafrefPath) const |
| 323 | { |
| 324 | using namespace std::string_literals; |
| 325 | libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath)); |
| 326 | return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX); |
| 327 | } |
| 328 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 329 | std::set<std::string> YangSchema::modules() const |
| 330 | { |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 331 | const auto& modules = m_context->get_module_iter(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 332 | |
| 333 | std::set<std::string> res; |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 334 | 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] | 335 | return res; |
| 336 | } |
| 337 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 338 | 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] | 339 | { |
| 340 | using namespace std::string_view_literals; |
| 341 | std::set<std::string> res; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 342 | std::vector<libyang::S_Schema_Node> nodes; |
| 343 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 344 | if (path.m_nodes.empty()) { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 345 | nodes = m_context->data_instantiables(0); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 346 | } else { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 347 | const auto pathString = pathToSchemaString(path, Prefixes::Always); |
| 348 | const auto node = getSchemaNode(pathString); |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 349 | nodes = node->child_instantiables(0); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 350 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 351 | |
| 352 | for (const auto node : nodes) { |
| 353 | if (node->module()->name() == "ietf-yang-library"sv) |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 354 | continue; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 355 | if (recursion == Recursion::Recursive) { |
| 356 | for (auto it : node->tree_dfs()) { |
| 357 | res.insert(it->path(LYS_PATH_FIRST_PREFIX)); |
| 358 | } |
| 359 | } else { |
Václav Kubernát | 4f77a25 | 2019-02-19 16:51:30 +0100 | [diff] [blame] | 360 | std::string toInsert; |
| 361 | if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) { |
| 362 | toInsert += node->module()->name(); |
| 363 | toInsert += ":"; |
| 364 | } |
| 365 | toInsert += node->name(); |
| 366 | res.insert(toInsert); |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 370 | return res; |
| 371 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 372 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 373 | std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const |
| 374 | { |
| 375 | std::set<std::string> res; |
| 376 | const auto yangModule = m_context->get_module(module.m_name.c_str()); |
| 377 | |
| 378 | std::vector<libyang::S_Schema_Node> nodes; |
| 379 | |
| 380 | for (const auto node : yangModule->data_instantiables(0)) { |
| 381 | if (recursion == Recursion::Recursive) { |
| 382 | for (const auto it : node->tree_dfs()) { |
| 383 | res.insert(it->path(LYS_PATH_FIRST_PREFIX)); |
| 384 | } |
| 385 | } else { |
| 386 | res.insert(module.m_name + ":" + node->name()); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return res; |
| 391 | } |
| 392 | |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 393 | void YangSchema::loadModule(const std::string& moduleName) |
| 394 | { |
| 395 | m_context->load_module(moduleName.c_str()); |
| 396 | } |
| 397 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 398 | void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName) |
| 399 | { |
| 400 | m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str()); |
| 401 | } |
| 402 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 403 | 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] | 404 | { |
| 405 | auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) { |
| 406 | (void)submod_revision; |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 407 | 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] | 408 | if (moduleSource.empty()) { |
| 409 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr}; |
| 410 | } |
| 411 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())}; |
| 412 | }; |
| 413 | |
| 414 | auto deleter = [](void* data) { |
| 415 | free(data); |
| 416 | }; |
| 417 | m_context->add_missing_module_callback(lambda, deleter); |
| 418 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 419 | |
| 420 | std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const |
| 421 | { |
| 422 | return std::make_shared<libyang::Data_Node>(m_context, |
| 423 | path.c_str(), |
| 424 | value ? value.value().c_str() : nullptr, |
| 425 | LYD_ANYDATA_CONSTSTRING, |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 426 | LYD_PATH_OPT_EDIT); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name) |
| 430 | { |
| 431 | return m_context->get_module(name.c_str(), nullptr, 0); |
| 432 | } |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 433 | |
| 434 | namespace { |
| 435 | yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node) |
| 436 | { |
| 437 | if (!node) { |
| 438 | throw InvalidNodeException(); |
| 439 | } |
| 440 | switch (node->nodetype()) { |
| 441 | case LYS_CONTAINER: |
| 442 | return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container; |
| 443 | case LYS_LEAF: |
| 444 | return yang::NodeTypes::Leaf; |
| 445 | case LYS_LIST: |
| 446 | return yang::NodeTypes::List; |
| 447 | default: |
Václav Kubernát | 2a14139 | 2020-02-18 17:12:32 +0100 | [diff] [blame] | 448 | throw InvalidNodeException(); // FIXME: Implement all types. |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const |
| 454 | { |
| 455 | return impl_nodeType(getSchemaNode(location, node)); |
| 456 | } |
| 457 | |
| 458 | yang::NodeTypes YangSchema::nodeType(const std::string& path) const |
| 459 | { |
| 460 | return impl_nodeType(getSchemaNode(path)); |
| 461 | } |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 462 | |
| 463 | std::optional<std::string> YangSchema::description(const std::string& path) const |
| 464 | { |
| 465 | auto node = getSchemaNode(path.c_str()); |
| 466 | return node->dsc() ? std::optional{node->dsc()} : std::nullopt; |
| 467 | } |
| 468 | |
| 469 | std::optional<std::string> YangSchema::units(const std::string& path) const |
| 470 | { |
| 471 | auto node = getSchemaNode(path.c_str()); |
| 472 | if (node->nodetype() != LYS_LEAF) { |
| 473 | return std::nullopt; |
| 474 | } |
| 475 | libyang::Schema_Node_Leaf leaf{node}; |
| 476 | auto units = leaf.units(); |
| 477 | |
| 478 | // A leaf can specify units as part of its definition. |
| 479 | if (units) { |
| 480 | return units; |
| 481 | } |
| 482 | |
| 483 | // A typedef (or its parent typedefs) can specify units too. We'll use the first `units` we find. |
| 484 | for (auto parentTypedef = leaf.type()->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) { |
| 485 | units = parentTypedef->units(); |
| 486 | if (units) { |
| 487 | return units; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return std::nullopt; |
| 492 | } |