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