blob: bbfce7c2ad4ed772d60092a9aabe100dfe046534 [file] [log] [blame]
Václav Kubernát0d4db442018-07-18 17:18:43 +02001/*
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átc31bd602019-03-07 11:44:48 +010010#include <libyang/Tree_Data.hpp>
Václav Kubernát0d4db442018-07-18 17:18:43 +020011#include <libyang/Tree_Schema.hpp>
12#include <string_view>
Václav Kubernát26b56082020-02-03 18:28:56 +010013#include "UniqueResource.hpp"
Václav Kubernát0d4db442018-07-18 17:18:43 +020014#include "utils.hpp"
15#include "yang_schema.hpp"
16
17class YangLoadError : public std::runtime_error {
18public:
19 using std::runtime_error::runtime_error;
20 ~YangLoadError() override = default;
21};
22
23class UnsupportedYangTypeException : public std::runtime_error {
24public:
25 using std::runtime_error::runtime_error;
26 ~UnsupportedYangTypeException() override = default;
27};
28
29class InvalidSchemaQueryException : public std::runtime_error {
30public:
31 using std::runtime_error::runtime_error;
32 ~InvalidSchemaQueryException() override = default;
33};
34
Václav Kubernát0d4db442018-07-18 17:18:43 +020035YangSchema::YangSchema()
Václav Kubernát51d08012020-06-08 15:20:12 +020036 : m_context(std::make_shared<libyang::Context>(nullptr, LY_CTX_DISABLE_SEARCHDIR_CWD))
Václav Kubernát0d4db442018-07-18 17:18:43 +020037{
Václav Kubernát0d4db442018-07-18 17:18:43 +020038}
39
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010040YangSchema::YangSchema(std::shared_ptr<libyang::Context> lyCtx)
41 : m_context(lyCtx)
42{
43
44}
45
Václav Kubernát0d4db442018-07-18 17:18:43 +020046YangSchema::~YangSchema() = default;
47
48void YangSchema::addSchemaString(const char* schema)
49{
50 if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) {
51 throw YangLoadError("Couldn't load schema");
52 }
53}
54
55void YangSchema::addSchemaDirectory(const char* directoryName)
56{
57 if (m_context->set_searchdir(directoryName)) {
58 throw YangLoadError("Couldn't add schema search directory");
59 }
60}
61
62void YangSchema::addSchemaFile(const char* filename)
63{
64 if (!m_context->parse_module_path(filename, LYS_IN_YANG)) {
65 throw YangLoadError("Couldn't load schema");
66 }
67}
68
Václav Kubernát75877de2019-11-20 17:43:02 +010069bool YangSchema::isModule(const std::string& name) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020070{
71 const auto set = modules();
72 return set.find(name) != set.end();
73}
74
Václav Kubernát2db124c2020-05-28 21:58:36 +020075bool YangSchema::listHasKey(const schemaPath_& listPath, const std::string& key) const
76{
77 const auto keys = listKeys(listPath);
78 return keys.find(key) != keys.end();
79}
80
Václav Kubernátc3866792020-02-20 14:12:56 +010081bool YangSchema::leafIsKey(const std::string& leafPath) const
82{
83 auto node = getSchemaNode(leafPath);
84 if (!node || node->nodetype() != LYS_LEAF)
85 return false;
86
87 return libyang::Schema_Node_Leaf{node}.is_key().get();
88}
89
Václav Kubernát47a3f672019-11-08 15:42:43 +010090libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020091{
Václav Kubernátf2b91e02019-04-11 15:36:48 +020092 // 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át47a3f672019-11-08 15:42:43 +0100104 return m_context->get_node(nullptr, node.c_str());
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200105 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200106}
107
Václav Kubernát47a3f672019-11-08 15:42:43 +0100108
109libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const
110{
111 return impl_getSchemaNode(node);
112}
113
114libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
115{
Václav Kubernátefcac932020-01-10 15:26:32 +0100116 std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node));
Václav Kubernát47a3f672019-11-08 15:42:43 +0100117
118 return impl_getSchemaNode(absPath);
119}
120
Václav Kubernát2db124c2020-05-28 21:58:36 +0200121libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& listPath) const
122{
123 std::string absPath = pathToSchemaString(listPath, Prefixes::Always);
124 return impl_getSchemaNode(absPath);
125}
126
Václav Kubernát912b9492020-05-29 02:03:40 +0200127const std::set<std::string> YangSchema::listKeys(const schemaPath_& listPath) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200128{
Václav Kubernát912b9492020-05-29 02:03:40 +0200129 auto node = getSchemaNode(listPath);
130 if (node->nodetype() != LYS_LIST) {
131 return {};
132 }
133
134 auto list = std::make_shared<libyang::Schema_Node_List>(node);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200135 std::set<std::string> keys;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200136 const auto& keysVec = list->keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200137
Václav Kubernát90de9502019-11-20 17:19:44 +0100138 std::transform(keysVec.begin(), keysVec.end(), std::inserter(keys, keys.begin()), [](const auto& it) { return it->name(); });
Václav Kubernát0d4db442018-07-18 17:18:43 +0200139 return keys;
140}
141
Václav Kubernát3a99f002020-03-31 02:27:41 +0200142namespace {
Václav Kubernát2984f442020-02-20 17:43:35 +0100143std::set<enum_> enumValues(const libyang::S_Type& typeArg)
Václav Kubernát0d4db442018-07-18 17:18:43 +0200144{
Václav Kubernát2984f442020-02-20 17:43:35 +0100145 auto type = typeArg;
Václav Kubernát3a99f002020-03-31 02:27:41 +0200146 auto enm = type->info()->enums()->enm();
147 // The enum can be a derived type and enm() only returns values,
148 // if that specific typedef changed the possible values. So we go
149 // up the hierarchy until we find a typedef that defined these values.
150 while (enm.empty()) {
151 type = type->der()->type();
152 enm = type->info()->enums()->enm();
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200153 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200154
155 std::vector<libyang::S_Type_Enum> enabled;
156 std::copy_if(enm.begin(), enm.end(), std::back_inserter(enabled), [](const libyang::S_Type_Enum& it) {
157 auto iffeatures = it->iffeature();
158 return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); });
159 });
160
161 std::set<enum_> enumSet;
162 std::transform(enabled.begin(), enabled.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return enum_{it->name()}; });
163 return enumSet;
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200164}
165
Václav Kubernát2984f442020-02-20 17:43:35 +0100166std::set<identityRef_> validIdentities(const libyang::S_Type& type)
Václav Kubernát3a99f002020-03-31 02:27:41 +0200167{
168 std::set<identityRef_> identSet;
169
170 // auto topLevelModule = leaf->module();
171
Václav Kubernát2984f442020-02-20 17:43:35 +0100172 auto info = type->info();
Václav Kubernát3a99f002020-03-31 02:27:41 +0200173 for (auto base : info->ident()->ref()) { // Iterate over all bases
174 identSet.emplace(base->module()->name(), base->name());
175 // Iterate over derived identities (this is recursive!)
176 for (auto derived : base->der()->schema()) {
177 identSet.emplace(derived->module()->name(), derived->name());
178 }
179 }
180
181 return identSet;
182}
183
Václav Kubernát2984f442020-02-20 17:43:35 +0100184std::string leafrefPath(const libyang::S_Type& type)
Václav Kubernát3a99f002020-03-31 02:27:41 +0200185{
Václav Kubernát2984f442020-02-20 17:43:35 +0100186 return type->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200187}
188}
189
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200190template <typename NodeType>
Václav Kubernát13b23d72020-04-16 21:49:51 +0200191yang::TypeInfo YangSchema::impl_leafType(const libyang::S_Schema_Node& node) const
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200192{
193 using namespace std::string_literals;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200194 auto leaf = std::make_shared<NodeType>(node);
Václav Kubernát13b23d72020-04-16 21:49:51 +0200195 auto leafUnits = leaf->units();
196 std::function<yang::TypeInfo(std::shared_ptr<libyang::Type>)> resolveType;
197 resolveType = [this, &resolveType, leaf, leafUnits] (std::shared_ptr<libyang::Type> type) -> yang::TypeInfo {
198 yang::LeafDataType resType;
Václav Kubernát2984f442020-02-20 17:43:35 +0100199 switch (type->base()) {
200 case LY_TYPE_STRING:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200201 resType.emplace<yang::String>();
202 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100203 case LY_TYPE_DEC64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200204 resType.emplace<yang::Decimal>();
205 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100206 case LY_TYPE_BOOL:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200207 resType.emplace<yang::Bool>();
208 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100209 case LY_TYPE_INT8:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200210 resType.emplace<yang::Int8>();
211 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100212 case LY_TYPE_INT16:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200213 resType.emplace<yang::Int16>();
214 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100215 case LY_TYPE_INT32:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200216 resType.emplace<yang::Int32>();
217 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100218 case LY_TYPE_INT64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200219 resType.emplace<yang::Int64>();
220 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100221 case LY_TYPE_UINT8:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200222 resType.emplace<yang::Uint8>();
223 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100224 case LY_TYPE_UINT16:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200225 resType.emplace<yang::Uint16>();
226 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100227 case LY_TYPE_UINT32:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200228 resType.emplace<yang::Uint32>();
229 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100230 case LY_TYPE_UINT64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200231 resType.emplace<yang::Uint64>();
232 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100233 case LY_TYPE_BINARY:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200234 resType.emplace<yang::Binary>();
235 break;
Jan Kundrát379bb572020-05-07 03:23:13 +0200236 case LY_TYPE_EMPTY:
237 resType.emplace<yang::Empty>();
238 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100239 case LY_TYPE_ENUM:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200240 resType.emplace<yang::Enum>(enumValues(type));
241 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100242 case LY_TYPE_IDENT:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200243 resType.emplace<yang::IdentityRef>(validIdentities(type));
244 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100245 case LY_TYPE_LEAFREF:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200246 resType.emplace<yang::LeafRef>(::leafrefPath(type), std::make_unique<yang::TypeInfo>(leafType(::leafrefPath(type))));
247 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100248 case LY_TYPE_UNION:
249 {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200250 auto resUnion = yang::Union{};
Václav Kubernát2984f442020-02-20 17:43:35 +0100251 for (auto unionType : type->info()->uni()->types()) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200252 resUnion.m_unionTypes.push_back(resolveType(unionType));
Václav Kubernát2984f442020-02-20 17:43:35 +0100253 }
Václav Kubernát13b23d72020-04-16 21:49:51 +0200254 resType.emplace<yang::Union>(std::move(resUnion));
255 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100256 }
257 default:
258 using namespace std::string_literals;
259 throw UnsupportedYangTypeException("the type of "s + leaf->name() + " is not supported: " + std::to_string(leaf->type()->base()));
260 }
Václav Kubernát13b23d72020-04-16 21:49:51 +0200261
262 std::optional<std::string> resUnits;
263
264 if (leafUnits) {
265 resUnits = leafUnits;
266 } else {
267 for (auto parentTypedef = type->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) {
268 auto units = parentTypedef->units();
269 if (units) {
270 resUnits = units;
271 break;
272 }
273 }
274 }
275
276 return yang::TypeInfo(resType, resUnits);
277 };
Václav Kubernát2984f442020-02-20 17:43:35 +0100278 return resolveType(leaf->type());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200279}
280
Václav Kubernát13b23d72020-04-16 21:49:51 +0200281yang::TypeInfo YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100282{
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200283 auto lyNode = getSchemaNode(location, node);
284 switch (lyNode->nodetype()) {
285 case LYS_LEAF:
286 return impl_leafType<libyang::Schema_Node_Leaf>(lyNode);
287 case LYS_LEAFLIST:
288 return impl_leafType<libyang::Schema_Node_Leaflist>(lyNode);
289 default:
290 throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist");
291 }
Václav Kubernát9bf36852020-02-18 17:47:56 +0100292}
293
Václav Kubernát13b23d72020-04-16 21:49:51 +0200294yang::TypeInfo YangSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100295{
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200296 auto lyNode = getSchemaNode(path);
297 switch (lyNode->nodetype()) {
298 case LYS_LEAF:
299 return impl_leafType<libyang::Schema_Node_Leaf>(lyNode);
300 case LYS_LEAFLIST:
301 return impl_leafType<libyang::Schema_Node_Leaflist>(lyNode);
302 default:
303 throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist");
304 }
Václav Kubernát9bf36852020-02-18 17:47:56 +0100305}
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200306
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100307std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const
308{
309 libyang::Schema_Node_Leaf leaf(getSchemaNode(path));
Václav Kubernát76ba4ec2020-05-18 13:26:56 +0200310 return leaf.type()->der().get() && leaf.type()->der()->type()->der().get() ? std::optional{leaf.type()->der()->name()} : std::nullopt;
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100311}
312
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100313std::string YangSchema::leafrefPath(const std::string& leafrefPath) const
314{
315 using namespace std::string_literals;
316 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath));
317 return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
318}
Václav Kubernát0d4db442018-07-18 17:18:43 +0200319
320std::set<std::string> YangSchema::modules() const
321{
Jan Kundrát4030b772018-08-23 15:54:56 +0200322 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200323
324 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100325 std::transform(modules.begin(), modules.end(), std::inserter(res, res.end()), [](const auto module) { return module->name(); });
Václav Kubernát0d4db442018-07-18 17:18:43 +0200326 return res;
327}
328
Václav Kubernát95b08872020-04-28 01:04:17 +0200329std::set<ModuleNodePair> YangSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200330{
331 using namespace std::string_view_literals;
Václav Kubernát95b08872020-04-28 01:04:17 +0200332 std::set<ModuleNodePair> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200333 std::vector<libyang::S_Schema_Node> nodes;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200334 std::string topLevelModule;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200335
Václav Kubernát3a823f42020-04-29 23:40:21 +0200336 if (path.type() == typeid(module_)) {
337 nodes = m_context->get_module(boost::get<module_>(path).m_name.c_str())->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200338 } else {
Václav Kubernát3a823f42020-04-29 23:40:21 +0200339 auto schemaPath = anyPathToSchemaPath(path);
340 if (schemaPath.m_nodes.empty()) {
341 nodes = m_context->data_instantiables(0);
342 } else {
343 const auto pathString = pathToSchemaString(schemaPath, Prefixes::Always);
344 const auto node = getSchemaNode(pathString);
345 nodes = node->child_instantiables(0);
346 topLevelModule = schemaPath.m_nodes.begin()->m_prefix->m_name;
347 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200348 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200349
Václav Kubernátef085742020-04-21 09:28:44 +0200350 for (const auto& node : nodes) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200351 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200352 continue;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200353
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200354 if (recursion == Recursion::Recursive) {
355 for (auto it : node->tree_dfs()) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200356 res.insert(ModuleNodePair(boost::none, it->path(LYS_PATH_FIRST_PREFIX)));
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200357 }
358 } else {
Václav Kubernát95b08872020-04-28 01:04:17 +0200359 ModuleNodePair toInsert;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200360 if (topLevelModule.empty() || topLevelModule != node->module()->name()) {
Václav Kubernát82d74632020-05-11 15:59:53 +0200361 toInsert.first = node->module()->type() == 0 ? node->module()->name() : libyang::Submodule(node->module()).belongsto()->name();
Václav Kubernát4f77a252019-02-19 16:51:30 +0100362 }
Václav Kubernát95b08872020-04-28 01:04:17 +0200363 toInsert.second = node->name();
Václav Kubernát4f77a252019-02-19 16:51:30 +0100364 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200365 }
366 }
367
Václav Kubernát0d4db442018-07-18 17:18:43 +0200368 return res;
369}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200370
371void YangSchema::loadModule(const std::string& moduleName)
372{
373 m_context->load_module(moduleName.c_str());
374}
375
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100376void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
377{
378 m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
379}
380
Václav Kubernátb52dc252019-12-04 13:03:39 +0100381void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*, const char*)>& clb)
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200382{
383 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
384 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100385 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200386 if (moduleSource.empty()) {
387 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
388 }
389 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
390 };
391
392 auto deleter = [](void* data) {
393 free(data);
394 };
395 m_context->add_missing_module_callback(lambda, deleter);
396}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100397
398std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
399{
400 return std::make_shared<libyang::Data_Node>(m_context,
401 path.c_str(),
402 value ? value.value().c_str() : nullptr,
403 LYD_ANYDATA_CONSTSTRING,
Václav Kubernátab612e92019-11-26 19:51:31 +0100404 LYD_PATH_OPT_EDIT);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100405}
406
407std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
408{
409 return m_context->get_module(name.c_str(), nullptr, 0);
410}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100411
412namespace {
413yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node)
414{
415 if (!node) {
416 throw InvalidNodeException();
417 }
418 switch (node->nodetype()) {
419 case LYS_CONTAINER:
420 return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container;
421 case LYS_LEAF:
422 return yang::NodeTypes::Leaf;
423 case LYS_LIST:
424 return yang::NodeTypes::List;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200425 case LYS_RPC:
426 return yang::NodeTypes::Rpc;
427 case LYS_ACTION:
428 return yang::NodeTypes::Action;
429 case LYS_NOTIF:
430 return yang::NodeTypes::Notification;
431 case LYS_ANYXML:
432 return yang::NodeTypes::AnyXml;
433 case LYS_LEAFLIST:
434 return yang::NodeTypes::LeafList;
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100435 default:
Václav Kubernát2a141392020-02-18 17:12:32 +0100436 throw InvalidNodeException(); // FIXME: Implement all types.
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100437 }
438}
439}
440
441yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
442{
443 return impl_nodeType(getSchemaNode(location, node));
444}
445
446yang::NodeTypes YangSchema::nodeType(const std::string& path) const
447{
448 return impl_nodeType(getSchemaNode(path));
449}
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100450
451std::optional<std::string> YangSchema::description(const std::string& path) const
452{
453 auto node = getSchemaNode(path.c_str());
454 return node->dsc() ? std::optional{node->dsc()} : std::nullopt;
455}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200456
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200457yang::Status YangSchema::status(const std::string& location) const
458{
459 auto node = getSchemaNode(location.c_str());
460 if (node->flags() & LYS_STATUS_DEPRC) {
461 return yang::Status::Deprecated;
462 } else if (node->flags() & LYS_STATUS_OBSLT) {
463 return yang::Status::Obsolete;
464 } else {
465 return yang::Status::Current;
466 }
467}
468
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200469bool YangSchema::isConfig(const std::string& path) const
470{
471 return getSchemaNode(path.c_str())->flags() & LYS_CONFIG_W;
472}
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200473
474std::optional<std::string> YangSchema::defaultValue(const std::string& leafPath) const
475{
476 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafPath));
477
478 if (auto leafDefault = leaf.dflt()) {
479 return leafDefault;
480 }
481
482 for (auto type = leaf.type()->der(); type != nullptr; type = type->type()->der()) {
483 if (auto defaultValue = type->dflt()) {
484 return defaultValue;
485 }
486 }
487
488 return std::nullopt;
489}