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); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 83 | if (!node || node->nodetype() != LYS_LEAF) { |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 84 | return false; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 85 | } |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 86 | |
| 87 | return libyang::Schema_Node_Leaf{node}.is_key().get(); |
| 88 | } |
| 89 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 90 | 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] | 91 | { |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 92 | // If no node is found find_path prints an error message, so we have to |
| 93 | // disable logging |
| 94 | // https://github.com/CESNET/libyang/issues/753 |
| 95 | { |
| 96 | int oldOptions; |
| 97 | auto logBlocker = make_unique_resource( |
| 98 | [&oldOptions]() { |
| 99 | oldOptions = libyang::set_log_options(0); |
| 100 | }, |
| 101 | [&oldOptions]() { |
| 102 | libyang::set_log_options(oldOptions); |
| 103 | }); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 104 | auto res = m_context->get_node(nullptr, node.c_str()); |
| 105 | if (!res) { // If no node is found, try output rpc nodes too. |
| 106 | res = m_context->get_node(nullptr, node.c_str(), 1); |
| 107 | } |
| 108 | return res; |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 109 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 112 | |
| 113 | libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const |
| 114 | { |
| 115 | return impl_getSchemaNode(node); |
| 116 | } |
| 117 | |
| 118 | libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const |
| 119 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 120 | 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] | 121 | |
| 122 | return impl_getSchemaNode(absPath); |
| 123 | } |
| 124 | |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 125 | libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& listPath) const |
| 126 | { |
| 127 | std::string absPath = pathToSchemaString(listPath, Prefixes::Always); |
| 128 | return impl_getSchemaNode(absPath); |
| 129 | } |
| 130 | |
Václav Kubernát | 912b949 | 2020-05-29 02:03:40 +0200 | [diff] [blame] | 131 | 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] | 132 | { |
Václav Kubernát | 912b949 | 2020-05-29 02:03:40 +0200 | [diff] [blame] | 133 | auto node = getSchemaNode(listPath); |
| 134 | if (node->nodetype() != LYS_LIST) { |
| 135 | return {}; |
| 136 | } |
| 137 | |
| 138 | auto list = std::make_shared<libyang::Schema_Node_List>(node); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 139 | std::set<std::string> keys; |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 140 | const auto& keysVec = list->keys(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 141 | |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 142 | 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] | 143 | return keys; |
| 144 | } |
| 145 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 146 | namespace { |
Václav Kubernát | fa36c82 | 2021-01-25 07:51:02 +0100 | [diff] [blame] | 147 | enum class ResolveMode { |
| 148 | Enum, |
| 149 | Identity |
| 150 | }; |
| 151 | /** @brief Resolves a typedef to a type which defines values. |
| 152 | * When we need allowed values of a type and that type is a typedef, we need to recurse into the typedef until we find a |
| 153 | * type which defines values. These values are the allowed values. |
| 154 | * Example: |
| 155 | * |
| 156 | * typedef MyOtherEnum { |
| 157 | * type enumeration { |
| 158 | * enum "A"; |
| 159 | * enum "B"; |
| 160 | * } |
| 161 | * } |
| 162 | * |
| 163 | * typedef MyEnum { |
| 164 | * type MyOtherEnum; |
| 165 | * } |
| 166 | * |
| 167 | * If `toResolve` points to MyEnum, then just doing ->enums()->enm() returns nothing and that means that this particular |
| 168 | * typedef (MyEnum) did not say which values are allowed. So, we need to dive into the parent enum (MyOtherEnum) with |
| 169 | * ->der()->type(). This typedef (MyOtherEnum) DID specify allowed values and enums()->enm() WILL contain them. These |
| 170 | * values are the only relevant values and we don't care about other parent typedefs. We return these values to the |
| 171 | * caller. |
| 172 | * |
| 173 | * For enums, this function simply returns all allowed enums. |
| 174 | * For identities, this function returns which bases `toResolve` has. |
| 175 | */ |
| 176 | template <ResolveMode TYPE> |
| 177 | auto resolveTypedef(const libyang::S_Type& toResolve) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 178 | { |
Václav Kubernát | fa36c82 | 2021-01-25 07:51:02 +0100 | [diff] [blame] | 179 | auto type = toResolve; |
| 180 | auto getValuesFromType = [] (const libyang::S_Type& type) { |
| 181 | if constexpr (TYPE == ResolveMode::Identity) { |
| 182 | return type->info()->ident()->ref(); |
| 183 | } else { |
| 184 | return type->info()->enums()->enm(); |
| 185 | } |
| 186 | }; |
| 187 | auto values = getValuesFromType(type); |
| 188 | while (values.empty()) { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 189 | type = type->der()->type(); |
Václav Kubernát | fa36c82 | 2021-01-25 07:51:02 +0100 | [diff] [blame] | 190 | values = getValuesFromType(type); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 191 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 192 | |
Václav Kubernát | fa36c82 | 2021-01-25 07:51:02 +0100 | [diff] [blame] | 193 | return values; |
| 194 | } |
| 195 | |
| 196 | std::set<enum_> enumValues(const libyang::S_Type& type) |
| 197 | { |
| 198 | auto values = resolveTypedef<ResolveMode::Enum>(type); |
| 199 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 200 | std::vector<libyang::S_Type_Enum> enabled; |
Václav Kubernát | fa36c82 | 2021-01-25 07:51:02 +0100 | [diff] [blame] | 201 | std::copy_if(values.begin(), values.end(), std::back_inserter(enabled), [](const libyang::S_Type_Enum& it) { |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 202 | auto iffeatures = it->iffeature(); |
| 203 | return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); }); |
| 204 | }); |
| 205 | |
| 206 | std::set<enum_> enumSet; |
| 207 | std::transform(enabled.begin(), enabled.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return enum_{it->name()}; }); |
| 208 | return enumSet; |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 211 | std::set<identityRef_> validIdentities(const libyang::S_Type& type) |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 212 | { |
| 213 | std::set<identityRef_> identSet; |
| 214 | |
Václav Kubernát | fa36c82 | 2021-01-25 07:51:02 +0100 | [diff] [blame] | 215 | for (auto base : resolveTypedef<ResolveMode::Identity>(type)) { // Iterate over all bases |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 216 | // Iterate over derived identities (this is recursive!) |
Václav Kubernát | 97f6b60 | 2022-01-17 23:55:51 +0100 | [diff] [blame^] | 217 | if (auto der = base->der()) { |
| 218 | for (auto derived : der->schema()) { |
| 219 | identSet.emplace(derived->module()->name(), derived->name()); |
| 220 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | return identSet; |
| 225 | } |
| 226 | |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 227 | std::string leafrefPath(const libyang::S_Type& type) |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 228 | { |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 229 | return type->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 233 | template <typename NodeType> |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 234 | 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] | 235 | { |
| 236 | using namespace std::string_literals; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 237 | auto leaf = std::make_shared<NodeType>(node); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 238 | auto leafUnits = leaf->units(); |
| 239 | std::function<yang::TypeInfo(std::shared_ptr<libyang::Type>)> resolveType; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 240 | resolveType = [this, &resolveType, leaf, leafUnits](std::shared_ptr<libyang::Type> type) -> yang::TypeInfo { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 241 | yang::LeafDataType resType; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 242 | switch (type->base()) { |
| 243 | case LY_TYPE_STRING: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 244 | resType.emplace<yang::String>(); |
| 245 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 246 | case LY_TYPE_DEC64: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 247 | resType.emplace<yang::Decimal>(); |
| 248 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 249 | case LY_TYPE_BOOL: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 250 | resType.emplace<yang::Bool>(); |
| 251 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 252 | case LY_TYPE_INT8: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 253 | resType.emplace<yang::Int8>(); |
| 254 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 255 | case LY_TYPE_INT16: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 256 | resType.emplace<yang::Int16>(); |
| 257 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 258 | case LY_TYPE_INT32: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 259 | resType.emplace<yang::Int32>(); |
| 260 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 261 | case LY_TYPE_INT64: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 262 | resType.emplace<yang::Int64>(); |
| 263 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 264 | case LY_TYPE_UINT8: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 265 | resType.emplace<yang::Uint8>(); |
| 266 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 267 | case LY_TYPE_UINT16: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 268 | resType.emplace<yang::Uint16>(); |
| 269 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 270 | case LY_TYPE_UINT32: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 271 | resType.emplace<yang::Uint32>(); |
| 272 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 273 | case LY_TYPE_UINT64: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 274 | resType.emplace<yang::Uint64>(); |
| 275 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 276 | case LY_TYPE_BINARY: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 277 | resType.emplace<yang::Binary>(); |
| 278 | break; |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 279 | case LY_TYPE_EMPTY: |
| 280 | resType.emplace<yang::Empty>(); |
| 281 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 282 | case LY_TYPE_ENUM: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 283 | resType.emplace<yang::Enum>(enumValues(type)); |
| 284 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 285 | case LY_TYPE_IDENT: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 286 | resType.emplace<yang::IdentityRef>(validIdentities(type)); |
| 287 | break; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 288 | case LY_TYPE_LEAFREF: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 289 | resType.emplace<yang::LeafRef>(::leafrefPath(type), std::make_unique<yang::TypeInfo>(leafType(::leafrefPath(type)))); |
| 290 | break; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 291 | case LY_TYPE_BITS: { |
| 292 | auto resBits = yang::Bits{}; |
| 293 | for (const auto& bit : type->info()->bits()->bit()) { |
| 294 | resBits.m_allowedValues.emplace(bit->name()); |
Václav Kubernát | dab73ca | 2020-10-26 23:44:43 +0100 | [diff] [blame] | 295 | } |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 296 | resType.emplace<yang::Bits>(std::move(resBits)); |
| 297 | break; |
| 298 | } |
| 299 | case LY_TYPE_UNION: { |
| 300 | auto resUnion = yang::Union{}; |
| 301 | for (auto unionType : type->info()->uni()->types()) { |
| 302 | resUnion.m_unionTypes.emplace_back(resolveType(unionType)); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 303 | } |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 304 | resType.emplace<yang::Union>(std::move(resUnion)); |
| 305 | break; |
| 306 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 307 | default: |
| 308 | using namespace std::string_literals; |
| 309 | throw UnsupportedYangTypeException("the type of "s + leaf->name() + " is not supported: " + std::to_string(leaf->type()->base())); |
| 310 | } |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 311 | |
| 312 | std::optional<std::string> resUnits; |
| 313 | |
| 314 | if (leafUnits) { |
| 315 | resUnits = leafUnits; |
| 316 | } else { |
| 317 | for (auto parentTypedef = type->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) { |
| 318 | auto units = parentTypedef->units(); |
| 319 | if (units) { |
| 320 | resUnits = units; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 326 | std::optional<std::string> resDescription; |
| 327 | |
| 328 | // checking for parentTypedef->type()->der() means I'm going to enter inside base types like "string". These |
| 329 | // also have a description, but it isn't too helpful ("human-readable string") |
| 330 | for (auto parentTypedef = type->der(); parentTypedef && parentTypedef->type()->der(); parentTypedef = parentTypedef->type()->der()) { |
| 331 | auto dsc = parentTypedef->dsc(); |
| 332 | if (dsc) { |
| 333 | resDescription = dsc; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return yang::TypeInfo(resType, resUnits, resDescription); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 339 | }; |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 340 | return resolveType(leaf->type()); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 343 | 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] | 344 | { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 345 | auto lyNode = getSchemaNode(location, node); |
| 346 | switch (lyNode->nodetype()) { |
| 347 | case LYS_LEAF: |
| 348 | return impl_leafType<libyang::Schema_Node_Leaf>(lyNode); |
| 349 | case LYS_LEAFLIST: |
| 350 | return impl_leafType<libyang::Schema_Node_Leaflist>(lyNode); |
| 351 | default: |
| 352 | throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist"); |
| 353 | } |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 356 | yang::TypeInfo YangSchema::leafType(const std::string& path) const |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 357 | { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 358 | auto lyNode = getSchemaNode(path); |
| 359 | switch (lyNode->nodetype()) { |
| 360 | case LYS_LEAF: |
| 361 | return impl_leafType<libyang::Schema_Node_Leaf>(lyNode); |
| 362 | case LYS_LEAFLIST: |
| 363 | return impl_leafType<libyang::Schema_Node_Leaflist>(lyNode); |
| 364 | default: |
| 365 | throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist"); |
| 366 | } |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 367 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 368 | |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame] | 369 | std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const |
| 370 | { |
| 371 | libyang::Schema_Node_Leaf leaf(getSchemaNode(path)); |
Václav Kubernát | 76ba4ec | 2020-05-18 13:26:56 +0200 | [diff] [blame] | 372 | 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] | 373 | } |
| 374 | |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 375 | std::string YangSchema::leafrefPath(const std::string& leafrefPath) const |
| 376 | { |
| 377 | using namespace std::string_literals; |
| 378 | libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath)); |
| 379 | return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX); |
| 380 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 381 | |
| 382 | std::set<std::string> YangSchema::modules() const |
| 383 | { |
Jan Kundrát | 4030b77 | 2018-08-23 15:54:56 +0200 | [diff] [blame] | 384 | const auto& modules = m_context->get_module_iter(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 385 | |
| 386 | std::set<std::string> res; |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 387 | 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] | 388 | return res; |
| 389 | } |
| 390 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 391 | 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] | 392 | { |
| 393 | using namespace std::string_view_literals; |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 394 | std::set<ModuleNodePair> res; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 395 | std::vector<libyang::S_Schema_Node> nodes; |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 396 | std::string topLevelModule; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 397 | |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 398 | if (path.type() == typeid(module_)) { |
| 399 | 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] | 400 | } else { |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 401 | auto schemaPath = anyPathToSchemaPath(path); |
| 402 | if (schemaPath.m_nodes.empty()) { |
| 403 | nodes = m_context->data_instantiables(0); |
| 404 | } else { |
| 405 | const auto pathString = pathToSchemaString(schemaPath, Prefixes::Always); |
| 406 | const auto node = getSchemaNode(pathString); |
| 407 | nodes = node->child_instantiables(0); |
| 408 | topLevelModule = schemaPath.m_nodes.begin()->m_prefix->m_name; |
| 409 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 410 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 411 | |
Václav Kubernát | ef08574 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 412 | for (const auto& node : nodes) { |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 413 | if (node->module()->name() == "ietf-yang-library"sv) { |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 414 | continue; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 415 | } |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 416 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 417 | if (recursion == Recursion::Recursive) { |
| 418 | for (auto it : node->tree_dfs()) { |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 419 | 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] | 420 | } |
| 421 | } else { |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 422 | ModuleNodePair toInsert; |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 423 | if (topLevelModule.empty() || topLevelModule != node->module()->name()) { |
Václav Kubernát | 82d7463 | 2020-05-11 15:59:53 +0200 | [diff] [blame] | 424 | 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] | 425 | } |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 426 | toInsert.second = node->name(); |
Václav Kubernát | 4f77a25 | 2019-02-19 16:51:30 +0100 | [diff] [blame] | 427 | res.insert(toInsert); |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 431 | return res; |
| 432 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 433 | |
| 434 | void YangSchema::loadModule(const std::string& moduleName) |
| 435 | { |
| 436 | m_context->load_module(moduleName.c_str()); |
| 437 | } |
| 438 | |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 439 | void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName) |
| 440 | { |
Václav Kubernát | 7aaf6dc | 2020-06-26 18:03:57 +0200 | [diff] [blame] | 441 | using namespace std::string_literals; |
Václav Kubernát | f44bdda | 2020-06-22 15:58:41 +0200 | [diff] [blame] | 442 | auto module = getYangModule(moduleName); |
| 443 | if (!module) { |
Václav Kubernát | f44bdda | 2020-06-22 15:58:41 +0200 | [diff] [blame] | 444 | throw std::runtime_error("Module \""s + moduleName + "\" doesn't exist."); |
| 445 | } |
Václav Kubernát | 7aaf6dc | 2020-06-26 18:03:57 +0200 | [diff] [blame] | 446 | if (module->feature_enable(featureName.c_str())) { |
| 447 | throw std::runtime_error("Can't enable feature \""s + featureName + "\" for module \"" + moduleName + "\"."); |
| 448 | } |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 451 | 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] | 452 | { |
| 453 | auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) { |
| 454 | (void)submod_revision; |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 455 | 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] | 456 | if (moduleSource.empty()) { |
| 457 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr}; |
| 458 | } |
| 459 | return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())}; |
| 460 | }; |
| 461 | |
Václav Kubernát | b37356d | 2021-02-19 09:58:47 +0100 | [diff] [blame] | 462 | m_context->add_missing_module_callback(lambda, free); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 463 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 464 | |
| 465 | std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const |
| 466 | { |
| 467 | return std::make_shared<libyang::Data_Node>(m_context, |
| 468 | path.c_str(), |
| 469 | value ? value.value().c_str() : nullptr, |
| 470 | LYD_ANYDATA_CONSTSTRING, |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 471 | LYD_PATH_OPT_EDIT); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name) |
| 475 | { |
Václav Kubernát | f44bdda | 2020-06-22 15:58:41 +0200 | [diff] [blame] | 476 | return m_context->get_module(name.c_str()); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 477 | } |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 478 | |
| 479 | namespace { |
| 480 | yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node) |
| 481 | { |
| 482 | if (!node) { |
| 483 | throw InvalidNodeException(); |
| 484 | } |
| 485 | switch (node->nodetype()) { |
| 486 | case LYS_CONTAINER: |
| 487 | return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container; |
| 488 | case LYS_LEAF: |
| 489 | return yang::NodeTypes::Leaf; |
| 490 | case LYS_LIST: |
| 491 | return yang::NodeTypes::List; |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 492 | case LYS_RPC: |
| 493 | return yang::NodeTypes::Rpc; |
| 494 | case LYS_ACTION: |
| 495 | return yang::NodeTypes::Action; |
| 496 | case LYS_NOTIF: |
| 497 | return yang::NodeTypes::Notification; |
| 498 | case LYS_ANYXML: |
| 499 | return yang::NodeTypes::AnyXml; |
| 500 | case LYS_LEAFLIST: |
| 501 | return yang::NodeTypes::LeafList; |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 502 | default: |
Václav Kubernát | 2a14139 | 2020-02-18 17:12:32 +0100 | [diff] [blame] | 503 | throw InvalidNodeException(); // FIXME: Implement all types. |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const |
| 509 | { |
| 510 | return impl_nodeType(getSchemaNode(location, node)); |
| 511 | } |
| 512 | |
| 513 | yang::NodeTypes YangSchema::nodeType(const std::string& path) const |
| 514 | { |
| 515 | return impl_nodeType(getSchemaNode(path)); |
| 516 | } |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 517 | |
| 518 | std::optional<std::string> YangSchema::description(const std::string& path) const |
| 519 | { |
| 520 | auto node = getSchemaNode(path.c_str()); |
| 521 | return node->dsc() ? std::optional{node->dsc()} : std::nullopt; |
| 522 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 523 | |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 524 | yang::Status YangSchema::status(const std::string& location) const |
| 525 | { |
| 526 | auto node = getSchemaNode(location.c_str()); |
| 527 | if (node->flags() & LYS_STATUS_DEPRC) { |
| 528 | return yang::Status::Deprecated; |
| 529 | } else if (node->flags() & LYS_STATUS_OBSLT) { |
| 530 | return yang::Status::Obsolete; |
| 531 | } else { |
| 532 | return yang::Status::Current; |
| 533 | } |
| 534 | } |
| 535 | |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 536 | bool YangSchema::hasInputNodes(const std::string& path) const |
| 537 | { |
| 538 | auto node = getSchemaNode(path.c_str()); |
| 539 | if (auto type = node->nodetype(); type != LYS_ACTION && type != LYS_RPC) { |
| 540 | throw std::logic_error("StaticSchema::hasInputNodes called with non-RPC/action path"); |
| 541 | } |
| 542 | |
| 543 | // The first child gives the /input node and then I check whether it has a child. |
| 544 | return node->child()->child().get(); |
| 545 | } |
| 546 | |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 547 | bool YangSchema::isConfig(const std::string& path) const |
| 548 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 549 | auto node = getSchemaNode(path.c_str()); |
| 550 | if (node->flags() & LYS_CONFIG_W) { |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | // Node can still be an input node. |
| 555 | while (node->parent()) { |
| 556 | node = node->parent(); |
| 557 | if (node->nodetype() == LYS_INPUT) { |
| 558 | return true; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return false; |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 563 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 564 | |
| 565 | std::optional<std::string> YangSchema::defaultValue(const std::string& leafPath) const |
| 566 | { |
| 567 | libyang::Schema_Node_Leaf leaf(getSchemaNode(leafPath)); |
| 568 | |
| 569 | if (auto leafDefault = leaf.dflt()) { |
| 570 | return leafDefault; |
| 571 | } |
| 572 | |
| 573 | for (auto type = leaf.type()->der(); type != nullptr; type = type->type()->der()) { |
| 574 | if (auto defaultValue = type->dflt()) { |
| 575 | return defaultValue; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return std::nullopt; |
| 580 | } |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 581 | |
| 582 | std::string YangSchema::dataPathToSchemaPath(const std::string& path) |
| 583 | { |
| 584 | return getSchemaNode(path)->path(LYS_PATH_FIRST_PREFIX); |
| 585 | } |