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 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 9 | #include <libyang-cpp/Enum.hpp> |
| 10 | #include <libyang-cpp/Utils.hpp> |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 11 | #include <string_view> |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 12 | #include "UniqueResource.hpp" |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 13 | #include "utils.hpp" |
| 14 | #include "yang_schema.hpp" |
| 15 | |
| 16 | class YangLoadError : public std::runtime_error { |
| 17 | public: |
| 18 | using std::runtime_error::runtime_error; |
| 19 | ~YangLoadError() override = default; |
| 20 | }; |
| 21 | |
| 22 | class UnsupportedYangTypeException : public std::runtime_error { |
| 23 | public: |
| 24 | using std::runtime_error::runtime_error; |
| 25 | ~UnsupportedYangTypeException() override = default; |
| 26 | }; |
| 27 | |
| 28 | class InvalidSchemaQueryException : public std::runtime_error { |
| 29 | public: |
| 30 | using std::runtime_error::runtime_error; |
| 31 | ~InvalidSchemaQueryException() override = default; |
| 32 | }; |
| 33 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 34 | YangSchema::YangSchema() |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 35 | : m_context(std::nullopt, libyang::ContextOptions::DisableSearchDirs | libyang::ContextOptions::SetPrivParsed) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 36 | { |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 39 | YangSchema::YangSchema(libyang::Context lyCtx) |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 40 | : m_context(lyCtx) |
| 41 | { |
Václav Kubernát | 1d50a5b | 2020-02-03 16:44:22 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 44 | YangSchema::~YangSchema() = default; |
| 45 | |
| 46 | void YangSchema::addSchemaString(const char* schema) |
| 47 | { |
Jan Kundrát | 95c5582 | 2023-05-18 17:12:20 +0200 | [diff] [blame] | 48 | m_context.parseModule(std::string{schema}, libyang::SchemaFormat::YANG); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void YangSchema::addSchemaDirectory(const char* directoryName) |
| 52 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 53 | m_context.setSearchDir(directoryName); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void YangSchema::addSchemaFile(const char* filename) |
| 57 | { |
Jan Kundrát | 95c5582 | 2023-05-18 17:12:20 +0200 | [diff] [blame] | 58 | m_context.parseModule(std::filesystem::path{filename}, libyang::SchemaFormat::YANG); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Václav Kubernát | 75877de | 2019-11-20 17:43:02 +0100 | [diff] [blame] | 61 | bool YangSchema::isModule(const std::string& name) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 62 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 63 | return m_context.getModuleImplemented(name).has_value(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 66 | bool YangSchema::listHasKey(const schemaPath_& listPath, const std::string& key) const |
| 67 | { |
| 68 | const auto keys = listKeys(listPath); |
| 69 | return keys.find(key) != keys.end(); |
| 70 | } |
| 71 | |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 72 | bool YangSchema::leafIsKey(const std::string& leafPath) const |
| 73 | { |
| 74 | auto node = getSchemaNode(leafPath); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 75 | if (!node || node->nodeType() != libyang::NodeType::Leaf) { |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 76 | return false; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 77 | } |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 78 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 79 | return node->asLeaf().isKey(); |
Václav Kubernát | c386679 | 2020-02-20 14:12:56 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 82 | std::optional<libyang::SchemaNode> YangSchema::impl_getSchemaNode(const std::string& node) const |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 83 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 84 | // libyang::Context::findPath throws an exception, when no matching schema node is found. This exception has the |
| 85 | // ValidationFailure error code. We will catch that exception (and rethrow if it's not the correct error code. |
| 86 | // |
| 87 | // Also, we need to use findPath twice if we're trying to find output nodes. |
| 88 | try { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 89 | return m_context.findPath(node); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 90 | } catch (libyang::ErrorWithCode& err) { |
| 91 | if (err.code() != libyang::ErrorCode::ValidationFailure) { |
| 92 | throw; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 93 | } |
Václav Kubernát | f2b91e0 | 2019-04-11 15:36:48 +0200 | [diff] [blame] | 94 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 95 | try { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 96 | return m_context.findPath(node, libyang::OutputNodes::Yes); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 97 | } catch (libyang::ErrorWithCode& err) { |
| 98 | if (err.code() != libyang::ErrorCode::ValidationFailure) { |
| 99 | throw; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // We didn't find a matching node. |
| 104 | return std::nullopt; |
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 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 108 | std::optional<libyang::SchemaNode> YangSchema::getSchemaNode(const std::string& node) const |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 109 | { |
| 110 | return impl_getSchemaNode(node); |
| 111 | } |
| 112 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 113 | std::optional<libyang::SchemaNode> YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const |
Václav Kubernát | 47a3f67 | 2019-11-08 15:42:43 +0100 | [diff] [blame] | 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 | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 120 | std::optional<libyang::SchemaNode> YangSchema::getSchemaNode(const schemaPath_& listPath) const |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 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); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 129 | if (node->nodeType() != libyang::NodeType::List) { |
Václav Kubernát | 912b949 | 2020-05-29 02:03:40 +0200 | [diff] [blame] | 130 | return {}; |
| 131 | } |
| 132 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 133 | std::set<std::string> keys; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 134 | auto keysVec = node->asList().keys(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 135 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 136 | std::transform(keysVec.begin(), keysVec.end(), std::inserter(keys, keys.begin()), [](const auto& it) { return std::string{it.name()}; }); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 137 | return keys; |
| 138 | } |
| 139 | |
Petr Gotthard | 0aae768 | 2023-05-16 17:53:45 +0200 | [diff] [blame] | 140 | std::set<enum_> enumValues(const libyang::types::Type& type) |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 141 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 142 | auto enums = type.asEnum().items(); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 143 | std::set<enum_> enumSet; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 144 | std::transform(enums.begin(), enums.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return enum_{std::string{it.name}}; }); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 145 | return enumSet; |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Petr Gotthard | 0aae768 | 2023-05-16 17:53:45 +0200 | [diff] [blame] | 148 | std::set<identityRef_> validIdentities(const libyang::types::Type& type) |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 149 | { |
| 150 | std::set<identityRef_> identSet; |
| 151 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 152 | std::function<void(const std::vector<libyang::Identity>&)> impl = [&identSet, &impl] (const std::vector<libyang::Identity>& idents) { |
| 153 | if (idents.empty()) { |
| 154 | return; |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 155 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 156 | |
| 157 | for (const auto& ident : idents) { |
| 158 | identSet.emplace(std::string{ident.module().name()}, std::string{ident.name()}); |
| 159 | impl(ident.derived()); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | for (const auto& base : type.asIdentityRef().bases()) { |
| 164 | impl(base.derived()); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return identSet; |
| 168 | } |
| 169 | |
Petr Gotthard | 0aae768 | 2023-05-16 17:53:45 +0200 | [diff] [blame] | 170 | std::string leafrefPath(const libyang::types::Type& type) |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 171 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 172 | return std::string{type.asLeafRef().path()}; |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 175 | template <typename NodeType> |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 176 | yang::TypeInfo YangSchema::impl_leafType(const NodeType& node) const |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 177 | { |
| 178 | using namespace std::string_literals; |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 179 | auto leaf = std::make_shared<NodeType>(node); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 180 | auto leafUnits = leaf->units(); |
Petr Gotthard | 0aae768 | 2023-05-16 17:53:45 +0200 | [diff] [blame] | 181 | std::function<yang::TypeInfo(const libyang::types::Type&)> resolveType; |
| 182 | resolveType = [&resolveType, leaf, leafUnits](const libyang::types::Type& type) -> yang::TypeInfo { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 183 | yang::LeafDataType resType; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 184 | switch (type.base()) { |
| 185 | case libyang::LeafBaseType::String: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 186 | resType.emplace<yang::String>(); |
| 187 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 188 | case libyang::LeafBaseType::Dec64: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 189 | resType.emplace<yang::Decimal>(); |
| 190 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 191 | case libyang::LeafBaseType::Bool: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 192 | resType.emplace<yang::Bool>(); |
| 193 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 194 | case libyang::LeafBaseType::Int8: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 195 | resType.emplace<yang::Int8>(); |
| 196 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 197 | case libyang::LeafBaseType::Int16: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 198 | resType.emplace<yang::Int16>(); |
| 199 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 200 | case libyang::LeafBaseType::Int32: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 201 | resType.emplace<yang::Int32>(); |
| 202 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 203 | case libyang::LeafBaseType::Int64: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 204 | resType.emplace<yang::Int64>(); |
| 205 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 206 | case libyang::LeafBaseType::Uint8: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 207 | resType.emplace<yang::Uint8>(); |
| 208 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 209 | case libyang::LeafBaseType::Uint16: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 210 | resType.emplace<yang::Uint16>(); |
| 211 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 212 | case libyang::LeafBaseType::Uint32: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 213 | resType.emplace<yang::Uint32>(); |
| 214 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 215 | case libyang::LeafBaseType::Uint64: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 216 | resType.emplace<yang::Uint64>(); |
| 217 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 218 | case libyang::LeafBaseType::Binary: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 219 | resType.emplace<yang::Binary>(); |
| 220 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 221 | case libyang::LeafBaseType::Empty: |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 222 | resType.emplace<yang::Empty>(); |
| 223 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 224 | case libyang::LeafBaseType::Enum: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 225 | resType.emplace<yang::Enum>(enumValues(type)); |
| 226 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 227 | case libyang::LeafBaseType::IdentityRef: |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 228 | resType.emplace<yang::IdentityRef>(validIdentities(type)); |
| 229 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 230 | case libyang::LeafBaseType::Leafref: |
| 231 | resType.emplace<yang::LeafRef>(::leafrefPath(type), std::make_unique<yang::TypeInfo>(resolveType(type.asLeafRef().resolvedType()))); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 232 | break; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 233 | case libyang::LeafBaseType::Bits: { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 234 | auto resBits = yang::Bits{}; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 235 | for (const auto& bit : type.asBits().items()) { |
| 236 | resBits.m_allowedValues.emplace(std::string{bit.name}); |
Václav Kubernát | dab73ca | 2020-10-26 23:44:43 +0100 | [diff] [blame] | 237 | } |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 238 | resType.emplace<yang::Bits>(std::move(resBits)); |
| 239 | break; |
| 240 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 241 | case libyang::LeafBaseType::Union: { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 242 | auto resUnion = yang::Union{}; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 243 | for (auto unionType : type.asUnion().types()) { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 244 | resUnion.m_unionTypes.emplace_back(resolveType(unionType)); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 245 | } |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 246 | resType.emplace<yang::Union>(std::move(resUnion)); |
| 247 | break; |
| 248 | } |
Jan Kundrát | bb7aa85 | 2023-08-30 11:51:43 +0200 | [diff] [blame] | 249 | case libyang::LeafBaseType::InstanceIdentifier: { |
| 250 | resType.emplace<yang::InstanceIdentifier>(); |
| 251 | break; |
| 252 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 253 | default: |
| 254 | using namespace std::string_literals; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 255 | throw UnsupportedYangTypeException("the type of "s + |
| 256 | std::string{leaf->name()} + |
| 257 | " is not supported: " + |
| 258 | std::to_string(std::underlying_type_t<libyang::LeafBaseType>(leaf->valueType().base()))); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 259 | } |
Tomáš Pecka | c6b7ba5 | 2023-04-07 10:01:29 +0200 | [diff] [blame] | 260 | // note https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109434 |
| 261 | std::optional<std::string> typeDesc; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 262 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame] | 263 | try { |
| 264 | typeDesc = type.description(); |
| 265 | } catch (libyang::ParsedInfoUnavailable&) { |
| 266 | // libyang context doesn't have the parsed info. |
| 267 | } |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 268 | |
Tomáš Pecka | c6b7ba5 | 2023-04-07 10:01:29 +0200 | [diff] [blame] | 269 | return yang::TypeInfo(resType, std::optional<std::string>{leafUnits}, typeDesc); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 270 | }; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 271 | return resolveType(leaf->valueType()); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 274 | 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] | 275 | { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 276 | auto lyNode = getSchemaNode(location, node); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 277 | switch (lyNode->nodeType()) { |
| 278 | case libyang::NodeType::Leaf: |
| 279 | return impl_leafType(lyNode->asLeaf()); |
| 280 | case libyang::NodeType::Leaflist: |
| 281 | return impl_leafType(lyNode->asLeafList()); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 282 | default: |
| 283 | throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist"); |
| 284 | } |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 287 | yang::TypeInfo YangSchema::leafType(const std::string& path) const |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 288 | { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 289 | auto lyNode = getSchemaNode(path); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 290 | switch (lyNode->nodeType()) { |
| 291 | case libyang::NodeType::Leaf: |
| 292 | return impl_leafType(lyNode->asLeaf()); |
| 293 | case libyang::NodeType::Leaflist: |
| 294 | return impl_leafType(lyNode->asLeafList()); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 295 | default: |
| 296 | throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist"); |
| 297 | } |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 298 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 299 | |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame] | 300 | std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const |
| 301 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 302 | auto leaf = getSchemaNode(path)->asLeaf(); |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame] | 303 | try { |
| 304 | return std::string{leaf.valueType().name()}; |
| 305 | } catch (libyang::ParsedInfoUnavailable&) { |
| 306 | return std::nullopt; |
| 307 | } |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 310 | std::string YangSchema::leafrefPath(const std::string& leafrefPath) const |
| 311 | { |
| 312 | using namespace std::string_literals; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 313 | return ::leafrefPath(getSchemaNode(leafrefPath)->asLeaf().valueType()); |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 314 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 315 | |
| 316 | std::set<std::string> YangSchema::modules() const |
| 317 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 318 | const auto& modules = m_context.modules(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 319 | |
| 320 | std::set<std::string> res; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 321 | std::transform(modules.begin(), modules.end(), std::inserter(res, res.end()), [](const auto module) { return std::string{module.name()}; }); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 322 | return res; |
| 323 | } |
| 324 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 325 | 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] | 326 | { |
| 327 | using namespace std::string_view_literals; |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 328 | std::set<ModuleNodePair> res; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 329 | std::vector<libyang::ChildInstanstiables> nodeCollections; |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 330 | std::string topLevelModule; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 331 | |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 332 | if (path.type() == typeid(module_)) { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 333 | nodeCollections.emplace_back(m_context.getModule(boost::get<module_>(path).m_name)->childInstantiables()); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 334 | } else { |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 335 | auto schemaPath = anyPathToSchemaPath(path); |
| 336 | if (schemaPath.m_nodes.empty()) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 337 | for (const auto& module : m_context.modules()) { |
| 338 | if (module.implemented()) { |
| 339 | nodeCollections.emplace_back(module.childInstantiables()); |
| 340 | } |
| 341 | } |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 342 | } else { |
| 343 | const auto pathString = pathToSchemaString(schemaPath, Prefixes::Always); |
| 344 | const auto node = getSchemaNode(pathString); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 345 | nodeCollections.emplace_back(node->childInstantiables()); |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 346 | topLevelModule = schemaPath.m_nodes.begin()->m_prefix->m_name; |
| 347 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 348 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 349 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 350 | for (const auto& coll : nodeCollections) { |
| 351 | for (const auto& node : coll) { |
| 352 | if (node.module().name() == "ietf-yang-library"sv) { |
| 353 | continue; |
| 354 | } |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 355 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 356 | if (node.module().name() == "ietf-yang-schema-mount"sv) { |
| 357 | continue; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 358 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 359 | |
| 360 | if (recursion == Recursion::Recursive) { |
| 361 | for (auto it : node.childrenDfs()) { |
| 362 | res.insert(ModuleNodePair(boost::none, it.path())); |
| 363 | } |
| 364 | } else { |
| 365 | ModuleNodePair toInsert; |
| 366 | if (topLevelModule.empty() || topLevelModule != node.module().name()) { |
| 367 | toInsert.first = std::string{node.module().name()}; |
| 368 | } |
| 369 | toInsert.second = node.name(); |
| 370 | res.insert(toInsert); |
Václav Kubernát | 4f77a25 | 2019-02-19 16:51:30 +0100 | [diff] [blame] | 371 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 375 | return res; |
| 376 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 377 | |
| 378 | void YangSchema::loadModule(const std::string& moduleName) |
| 379 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 380 | m_context.loadModule(moduleName); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 383 | void YangSchema::setEnabledFeatures(const std::string& moduleName, const std::vector<std::string>& features) |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 384 | { |
Václav Kubernát | 7aaf6dc | 2020-06-26 18:03:57 +0200 | [diff] [blame] | 385 | using namespace std::string_literals; |
Václav Kubernát | f44bdda | 2020-06-22 15:58:41 +0200 | [diff] [blame] | 386 | auto module = getYangModule(moduleName); |
| 387 | if (!module) { |
Václav Kubernát | f44bdda | 2020-06-22 15:58:41 +0200 | [diff] [blame] | 388 | throw std::runtime_error("Module \""s + moduleName + "\" doesn't exist."); |
| 389 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 390 | try { |
| 391 | module->setImplemented(features); |
| 392 | } catch (libyang::ErrorWithCode&) { |
| 393 | throw std::runtime_error("Can't enable features for module \"" + moduleName + "\"."); |
Václav Kubernát | 7aaf6dc | 2020-06-26 18:03:57 +0200 | [diff] [blame] | 394 | } |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 397 | void YangSchema::registerModuleCallback(const std::function<std::string(const std::string_view, const std::optional<std::string_view>, const std::optional<std::string_view>, const std::optional<std::string_view>)>& clb) |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 398 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 399 | auto lambda = [clb](const auto mod_name, const auto mod_revision, const auto submod_name, const auto submod_revision) -> std::optional<libyang::ModuleInfo> { |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 400 | (void)submod_revision; |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 401 | 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] | 402 | if (moduleSource.empty()) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 403 | return std::nullopt; |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 404 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 405 | return libyang::ModuleInfo { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 406 | .data = moduleSource, |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 407 | .format = libyang::SchemaFormat::YANG |
| 408 | |
| 409 | }; |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 410 | }; |
| 411 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 412 | m_context.registerModuleCallback(lambda); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 413 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 414 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 415 | libyang::CreatedNodes YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 416 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 417 | auto options = [this, &path, &value] { |
| 418 | // If we're creating a node without a value and it's not the "empty" type, then we also need the Opaque flag. |
| 419 | auto schema = getSchemaNode(path); |
| 420 | if (schema->nodeType() == libyang::NodeType::Leaf && |
| 421 | schema->asLeaf().valueType().base() != libyang::LeafBaseType::Empty && |
| 422 | !value) { |
| 423 | return std::optional<libyang::CreationOptions>{libyang::CreationOptions::Opaque}; |
| 424 | } |
| 425 | |
| 426 | return std::optional<libyang::CreationOptions>{}; |
| 427 | }(); |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 428 | return m_context.newPath2(path, value, options); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 431 | std::optional<libyang::Module> YangSchema::getYangModule(const std::string& name) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 432 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 433 | return m_context.getModuleImplemented(name); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 434 | } |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 435 | |
| 436 | namespace { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 437 | yang::NodeTypes impl_nodeType(const libyang::SchemaNode& node) |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 438 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 439 | switch (node.nodeType()) { |
| 440 | case libyang::NodeType::Container: |
| 441 | return node.asContainer().isPresence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container; |
| 442 | case libyang::NodeType::Leaf: |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 443 | return yang::NodeTypes::Leaf; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 444 | case libyang::NodeType::List: |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 445 | return yang::NodeTypes::List; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 446 | case libyang::NodeType::RPC: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 447 | return yang::NodeTypes::Rpc; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 448 | case libyang::NodeType::Action: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 449 | return yang::NodeTypes::Action; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 450 | case libyang::NodeType::Notification: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 451 | return yang::NodeTypes::Notification; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 452 | case libyang::NodeType::AnyXML: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 453 | return yang::NodeTypes::AnyXml; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 454 | case libyang::NodeType::Leaflist: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 455 | return yang::NodeTypes::LeafList; |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 456 | default: |
Václav Kubernát | 2a14139 | 2020-02-18 17:12:32 +0100 | [diff] [blame] | 457 | throw InvalidNodeException(); // FIXME: Implement all types. |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const |
| 463 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 464 | return impl_nodeType(*getSchemaNode(location, node)); |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | yang::NodeTypes YangSchema::nodeType(const std::string& path) const |
| 468 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 469 | return impl_nodeType(*getSchemaNode(path)); |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 470 | } |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 471 | |
| 472 | std::optional<std::string> YangSchema::description(const std::string& path) const |
| 473 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 474 | auto desc = getSchemaNode(path.c_str())->description(); |
| 475 | return desc ? std::optional<std::string>{desc} : std::nullopt; |
| 476 | |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 477 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 478 | |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 479 | yang::Status YangSchema::status(const std::string& location) const |
| 480 | { |
| 481 | auto node = getSchemaNode(location.c_str()); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 482 | switch (node->status()) { |
| 483 | case libyang::Status::Deprecated: |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 484 | return yang::Status::Deprecated; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 485 | case libyang::Status::Obsolete: |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 486 | return yang::Status::Obsolete; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 487 | case libyang::Status::Current: |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 488 | return yang::Status::Current; |
| 489 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 490 | |
| 491 | __builtin_unreachable(); |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 494 | bool YangSchema::hasInputNodes(const std::string& path) const |
| 495 | { |
| 496 | auto node = getSchemaNode(path.c_str()); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 497 | if (auto type = node->nodeType(); type != libyang::NodeType::Action && type != libyang::NodeType::RPC) { |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 498 | throw std::logic_error("StaticSchema::hasInputNodes called with non-RPC/action path"); |
| 499 | } |
| 500 | |
| 501 | // The first child gives the /input node and then I check whether it has a child. |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 502 | return node->child()->child().has_value(); |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 505 | bool YangSchema::isConfig(const std::string& path) const |
| 506 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 507 | auto node = getSchemaNode(path.c_str()); |
Václav Kubernát | c8e5ed0 | 2022-03-23 12:46:54 +0100 | [diff] [blame] | 508 | if (node->isInput()) { |
| 509 | return true; |
| 510 | } |
| 511 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 512 | try { |
| 513 | if (node->config() == libyang::Config::True) { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 514 | return true; |
| 515 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 516 | } catch (libyang::Error&) { |
| 517 | // For non-data nodes (like `rpc`), the config value can't be retrieved. In this case, we'll just default to |
| 518 | // "false". |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | return false; |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 522 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 523 | |
| 524 | std::optional<std::string> YangSchema::defaultValue(const std::string& leafPath) const |
| 525 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 526 | return std::optional<std::string>{getSchemaNode(leafPath)->asLeaf().defaultValueStr()}; |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 527 | } |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 528 | |
| 529 | std::string YangSchema::dataPathToSchemaPath(const std::string& path) |
| 530 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 531 | return std::string{getSchemaNode(path)->path()}; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 532 | } |