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