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 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 48 | m_context.parseModuleMem(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 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 58 | m_context.parseModulePath(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 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 140 | std::set<enum_> enumValues(const libyang::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 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 148 | std::set<identityRef_> validIdentities(const libyang::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 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 170 | std::string leafrefPath(const libyang::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(); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 181 | std::function<yang::TypeInfo(const libyang::Type&)> resolveType; |
| 182 | resolveType = [&resolveType, leaf, leafUnits](const libyang::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 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 249 | default: |
| 250 | using namespace std::string_literals; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 251 | throw UnsupportedYangTypeException("the type of "s + |
| 252 | std::string{leaf->name()} + |
| 253 | " is not supported: " + |
| 254 | 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] | 255 | } |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame] | 256 | std::optional<std::string_view> typeDesc; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 257 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame] | 258 | try { |
| 259 | typeDesc = type.description(); |
| 260 | } catch (libyang::ParsedInfoUnavailable&) { |
| 261 | // libyang context doesn't have the parsed info. |
| 262 | } |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 263 | |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame] | 264 | return yang::TypeInfo(resType, std::optional<std::string>{leafUnits}, std::optional<std::string>{typeDesc}); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 265 | }; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 266 | return resolveType(leaf->valueType()); |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 269 | 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] | 270 | { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 271 | auto lyNode = getSchemaNode(location, node); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 272 | switch (lyNode->nodeType()) { |
| 273 | case libyang::NodeType::Leaf: |
| 274 | return impl_leafType(lyNode->asLeaf()); |
| 275 | case libyang::NodeType::Leaflist: |
| 276 | return impl_leafType(lyNode->asLeafList()); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 277 | default: |
| 278 | throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist"); |
| 279 | } |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 282 | yang::TypeInfo YangSchema::leafType(const std::string& path) const |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 283 | { |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 284 | auto lyNode = getSchemaNode(path); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 285 | switch (lyNode->nodeType()) { |
| 286 | case libyang::NodeType::Leaf: |
| 287 | return impl_leafType(lyNode->asLeaf()); |
| 288 | case libyang::NodeType::Leaflist: |
| 289 | return impl_leafType(lyNode->asLeafList()); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 290 | default: |
| 291 | throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist"); |
| 292 | } |
Václav Kubernát | 9bf3685 | 2020-02-18 17:47:56 +0100 | [diff] [blame] | 293 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 294 | |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame] | 295 | std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const |
| 296 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 297 | auto leaf = getSchemaNode(path)->asLeaf(); |
Václav Kubernát | ed4e378 | 2022-03-02 23:57:33 +0100 | [diff] [blame] | 298 | try { |
| 299 | return std::string{leaf.valueType().name()}; |
| 300 | } catch (libyang::ParsedInfoUnavailable&) { |
| 301 | return std::nullopt; |
| 302 | } |
Václav Kubernát | 6fcd028 | 2020-02-21 16:33:08 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 305 | std::string YangSchema::leafrefPath(const std::string& leafrefPath) const |
| 306 | { |
| 307 | using namespace std::string_literals; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 308 | return ::leafrefPath(getSchemaNode(leafrefPath)->asLeaf().valueType()); |
Václav Kubernát | bd5e3c2 | 2020-02-19 15:22:00 +0100 | [diff] [blame] | 309 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 310 | |
| 311 | std::set<std::string> YangSchema::modules() const |
| 312 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 313 | const auto& modules = m_context.modules(); |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 314 | |
| 315 | std::set<std::string> res; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 316 | 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] | 317 | return res; |
| 318 | } |
| 319 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 320 | 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] | 321 | { |
| 322 | using namespace std::string_view_literals; |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 323 | std::set<ModuleNodePair> res; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 324 | std::vector<libyang::ChildInstanstiables> nodeCollections; |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 325 | std::string topLevelModule; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 326 | |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 327 | if (path.type() == typeid(module_)) { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 328 | 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] | 329 | } else { |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 330 | auto schemaPath = anyPathToSchemaPath(path); |
| 331 | if (schemaPath.m_nodes.empty()) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 332 | for (const auto& module : m_context.modules()) { |
| 333 | if (module.implemented()) { |
| 334 | nodeCollections.emplace_back(module.childInstantiables()); |
| 335 | } |
| 336 | } |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 337 | } else { |
| 338 | const auto pathString = pathToSchemaString(schemaPath, Prefixes::Always); |
| 339 | const auto node = getSchemaNode(pathString); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 340 | nodeCollections.emplace_back(node->childInstantiables()); |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 341 | topLevelModule = schemaPath.m_nodes.begin()->m_prefix->m_name; |
| 342 | } |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 343 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 344 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 345 | for (const auto& coll : nodeCollections) { |
| 346 | for (const auto& node : coll) { |
| 347 | if (node.module().name() == "ietf-yang-library"sv) { |
| 348 | continue; |
| 349 | } |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 350 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 351 | if (node.module().name() == "ietf-yang-schema-mount"sv) { |
| 352 | continue; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 353 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 354 | |
| 355 | if (recursion == Recursion::Recursive) { |
| 356 | for (auto it : node.childrenDfs()) { |
| 357 | res.insert(ModuleNodePair(boost::none, it.path())); |
| 358 | } |
| 359 | } else { |
| 360 | ModuleNodePair toInsert; |
| 361 | if (topLevelModule.empty() || topLevelModule != node.module().name()) { |
| 362 | toInsert.first = std::string{node.module().name()}; |
| 363 | } |
| 364 | toInsert.second = node.name(); |
| 365 | res.insert(toInsert); |
Václav Kubernát | 4f77a25 | 2019-02-19 16:51:30 +0100 | [diff] [blame] | 366 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Václav Kubernát | 0d4db44 | 2018-07-18 17:18:43 +0200 | [diff] [blame] | 370 | return res; |
| 371 | } |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 372 | |
| 373 | void YangSchema::loadModule(const std::string& moduleName) |
| 374 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 375 | m_context.loadModule(moduleName); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 378 | 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] | 379 | { |
Václav Kubernát | 7aaf6dc | 2020-06-26 18:03:57 +0200 | [diff] [blame] | 380 | using namespace std::string_literals; |
Václav Kubernát | f44bdda | 2020-06-22 15:58:41 +0200 | [diff] [blame] | 381 | auto module = getYangModule(moduleName); |
| 382 | if (!module) { |
Václav Kubernát | f44bdda | 2020-06-22 15:58:41 +0200 | [diff] [blame] | 383 | throw std::runtime_error("Module \""s + moduleName + "\" doesn't exist."); |
| 384 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 385 | try { |
| 386 | module->setImplemented(features); |
| 387 | } catch (libyang::ErrorWithCode&) { |
| 388 | 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] | 389 | } |
Václav Kubernát | bf9c611 | 2019-11-04 16:03:35 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 392 | 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] | 393 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 394 | 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] | 395 | (void)submod_revision; |
Václav Kubernát | b52dc25 | 2019-12-04 13:03:39 +0100 | [diff] [blame] | 396 | 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] | 397 | if (moduleSource.empty()) { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 398 | return std::nullopt; |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 399 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 400 | return libyang::ModuleInfo { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 401 | .data = moduleSource, |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 402 | .format = libyang::SchemaFormat::YANG |
| 403 | |
| 404 | }; |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 405 | }; |
| 406 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 407 | m_context.registerModuleCallback(lambda); |
Václav Kubernát | a6c5fff | 2018-09-07 15:16:25 +0200 | [diff] [blame] | 408 | } |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 409 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 410 | 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] | 411 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 412 | auto options = [this, &path, &value] { |
| 413 | // If we're creating a node without a value and it's not the "empty" type, then we also need the Opaque flag. |
| 414 | auto schema = getSchemaNode(path); |
| 415 | if (schema->nodeType() == libyang::NodeType::Leaf && |
| 416 | schema->asLeaf().valueType().base() != libyang::LeafBaseType::Empty && |
| 417 | !value) { |
| 418 | return std::optional<libyang::CreationOptions>{libyang::CreationOptions::Opaque}; |
| 419 | } |
| 420 | |
| 421 | return std::optional<libyang::CreationOptions>{}; |
| 422 | }(); |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 423 | return m_context.newPath2(path, value, options); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 426 | std::optional<libyang::Module> YangSchema::getYangModule(const std::string& name) |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 427 | { |
Jan Kundrát | f59b83c | 2022-03-18 18:12:08 +0100 | [diff] [blame] | 428 | return m_context.getModuleImplemented(name); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 429 | } |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 430 | |
| 431 | namespace { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 432 | yang::NodeTypes impl_nodeType(const libyang::SchemaNode& node) |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 433 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 434 | switch (node.nodeType()) { |
| 435 | case libyang::NodeType::Container: |
| 436 | return node.asContainer().isPresence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container; |
| 437 | case libyang::NodeType::Leaf: |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 438 | return yang::NodeTypes::Leaf; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 439 | case libyang::NodeType::List: |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 440 | return yang::NodeTypes::List; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 441 | case libyang::NodeType::RPC: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 442 | return yang::NodeTypes::Rpc; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 443 | case libyang::NodeType::Action: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 444 | return yang::NodeTypes::Action; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 445 | case libyang::NodeType::Notification: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 446 | return yang::NodeTypes::Notification; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 447 | case libyang::NodeType::AnyXML: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 448 | return yang::NodeTypes::AnyXml; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 449 | case libyang::NodeType::Leaflist: |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 450 | return yang::NodeTypes::LeafList; |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 451 | default: |
Václav Kubernát | 2a14139 | 2020-02-18 17:12:32 +0100 | [diff] [blame] | 452 | throw InvalidNodeException(); // FIXME: Implement all types. |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const |
| 458 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 459 | return impl_nodeType(*getSchemaNode(location, node)); |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | yang::NodeTypes YangSchema::nodeType(const std::string& path) const |
| 463 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 464 | return impl_nodeType(*getSchemaNode(path)); |
Václav Kubernát | 34ee85a | 2020-02-18 17:12:12 +0100 | [diff] [blame] | 465 | } |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 466 | |
| 467 | std::optional<std::string> YangSchema::description(const std::string& path) const |
| 468 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 469 | auto desc = getSchemaNode(path.c_str())->description(); |
| 470 | return desc ? std::optional<std::string>{desc} : std::nullopt; |
| 471 | |
Václav Kubernát | 1e09bd6 | 2020-02-17 15:13:38 +0100 | [diff] [blame] | 472 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 473 | |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 474 | yang::Status YangSchema::status(const std::string& location) const |
| 475 | { |
| 476 | auto node = getSchemaNode(location.c_str()); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 477 | switch (node->status()) { |
| 478 | case libyang::Status::Deprecated: |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 479 | return yang::Status::Deprecated; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 480 | case libyang::Status::Obsolete: |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 481 | return yang::Status::Obsolete; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 482 | case libyang::Status::Current: |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 483 | return yang::Status::Current; |
| 484 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 485 | |
| 486 | __builtin_unreachable(); |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 489 | bool YangSchema::hasInputNodes(const std::string& path) const |
| 490 | { |
| 491 | auto node = getSchemaNode(path.c_str()); |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 492 | 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] | 493 | throw std::logic_error("StaticSchema::hasInputNodes called with non-RPC/action path"); |
| 494 | } |
| 495 | |
| 496 | // 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] | 497 | return node->child()->child().has_value(); |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 500 | bool YangSchema::isConfig(const std::string& path) const |
| 501 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 502 | auto node = getSchemaNode(path.c_str()); |
Václav Kubernát | c8e5ed0 | 2022-03-23 12:46:54 +0100 | [diff] [blame] | 503 | if (node->isInput()) { |
| 504 | return true; |
| 505 | } |
| 506 | |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 507 | try { |
| 508 | if (node->config() == libyang::Config::True) { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 509 | return true; |
| 510 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 511 | } catch (libyang::Error&) { |
| 512 | // For non-data nodes (like `rpc`), the config value can't be retrieved. In this case, we'll just default to |
| 513 | // "false". |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return false; |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 517 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 518 | |
| 519 | std::optional<std::string> YangSchema::defaultValue(const std::string& leafPath) const |
| 520 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 521 | return std::optional<std::string>{getSchemaNode(leafPath)->asLeaf().defaultValueStr()}; |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 522 | } |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 523 | |
| 524 | std::string YangSchema::dataPathToSchemaPath(const std::string& path) |
| 525 | { |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 526 | return std::string{getSchemaNode(path)->path()}; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 527 | } |