blob: 0795b2b4ff9b66ee0bff962421b7ad6ed76bc7cf [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át2eaceb82018-10-08 19:56:30 +020035template <typename T>
36std::string pathToYangAbsSchemPath(const T& path)
Václav Kubernát0d4db442018-07-18 17:18:43 +020037{
38 std::string res = "/";
39 std::string currentModule;
40
41 for (const auto& it : path.m_nodes) {
42 const auto name = nodeToSchemaString(it);
43
44 if (it.m_suffix.type() == typeid(module_)) {
45 currentModule = name;
46 continue;
47 } else {
48 res += currentModule + ":";
49 res += name + "/";
50 }
51 }
52
53 return res;
54}
55
56YangSchema::YangSchema()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020057 : m_context(std::make_shared<libyang::Context>(nullptr, LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_DISABLE_SEARCHDIR_CWD))
Václav Kubernát0d4db442018-07-18 17:18:43 +020058{
Václav Kubernát0d4db442018-07-18 17:18:43 +020059}
60
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010061YangSchema::YangSchema(std::shared_ptr<libyang::Context> lyCtx)
62 : m_context(lyCtx)
63{
64
65}
66
Václav Kubernát0d4db442018-07-18 17:18:43 +020067YangSchema::~YangSchema() = default;
68
69void YangSchema::addSchemaString(const char* schema)
70{
71 if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) {
72 throw YangLoadError("Couldn't load schema");
73 }
74}
75
76void YangSchema::addSchemaDirectory(const char* directoryName)
77{
78 if (m_context->set_searchdir(directoryName)) {
79 throw YangLoadError("Couldn't add schema search directory");
80 }
81}
82
83void YangSchema::addSchemaFile(const char* filename)
84{
85 if (!m_context->parse_module_path(filename, LYS_IN_YANG)) {
86 throw YangLoadError("Couldn't load schema");
87 }
88}
89
Václav Kubernát75877de2019-11-20 17:43:02 +010090bool YangSchema::isModule(const std::string& name) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020091{
92 const auto set = modules();
93 return set.find(name) != set.end();
94}
95
Václav Kubernát2eaceb82018-10-08 19:56:30 +020096bool YangSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020097{
Václav Kubernát989b5de2019-02-20 16:28:35 +010098 auto enums = enumValues(location, node);
99
100 return std::any_of(enums.begin(), enums.end(), [=](const auto& x) { return x == value; });
101}
102
103const std::set<std::string> YangSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
104{
Václav Kubernát0d4db442018-07-18 17:18:43 +0200105 if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::Enum)
Václav Kubernát989b5de2019-02-20 16:28:35 +0100106 return {};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200107
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200108 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
Václav Kubernát6a713d62018-10-03 18:47:34 +0200109 auto type = leaf.type();
110 auto enm = type->info()->enums()->enm();
111 // The enum can be a derived type and enm() only returns values,
112 // if that specific typedef changed the possible values. So we go
113 // up the hierarchy until we find a typedef that defined these values.
114 while (enm.empty()) {
115 type = type->der()->type();
116 enm = type->info()->enums()->enm();
117 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200118
Václav Kubernáta38d4172019-11-04 12:36:39 +0100119 std::vector<libyang::S_Type_Enum> enabled;
Václav Kubernát90de9502019-11-20 17:19:44 +0100120 std::copy_if(enm.begin(), enm.end(), std::back_inserter(enabled), [](const libyang::S_Type_Enum& it) {
Václav Kubernáta38d4172019-11-04 12:36:39 +0100121 auto iffeatures = it->iffeature();
Václav Kubernát90de9502019-11-20 17:19:44 +0100122 return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); });
Václav Kubernáta38d4172019-11-04 12:36:39 +0100123 });
124
Václav Kubernát989b5de2019-02-20 16:28:35 +0100125 std::set<std::string> enumSet;
Václav Kubernáta38d4172019-11-04 12:36:39 +0100126 std::transform(enabled.begin(), enabled.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return it->name(); });
Václav Kubernát989b5de2019-02-20 16:28:35 +0100127 return enumSet;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200128}
129
Václav Kubernáteeb38842019-03-20 19:46:05 +0100130const std::set<std::string> YangSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const
131{
132 if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::IdentityRef)
133 return {};
134
135 std::set<std::string> identSet;
136
137 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
138 auto insertToSet = [&identSet, prefixes, topLevelModule](auto module, auto name) {
139 std::string stringIdent;
140 if (prefixes == Prefixes::Always || topLevelModule != module) {
141 stringIdent += module;
142 stringIdent += ":";
143 }
144 stringIdent += name;
145 identSet.emplace(stringIdent);
146 };
147
148 auto leaf = std::make_shared<libyang::Schema_Node_Leaf>(getSchemaNode(location, node));
149 auto info = leaf->type()->info();
150 for (auto base : info->ident()->ref()) { // Iterate over all bases
151 insertToSet(base->module()->name(), base->name());
152 // Iterate over derived identities (this is recursive!)
153 for (auto derived : base->der()->schema()) {
154 insertToSet(derived->module()->name(), derived->name());
155 }
156 }
157
158 return identSet;
159}
160
161bool YangSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const
162{
163 auto identities = validIdentities(location, node, Prefixes::Always);
164
165 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
166 auto identModule = value.first ? value.first.value() : topLevelModule;
Václav Kubernát1bf704e2019-04-12 13:30:50 +0200167 return std::any_of(identities.begin(), identities.end(), [toFind = identModule + ":" + value.second](const auto& x) { return x == toFind; });
Václav Kubernáteeb38842019-03-20 19:46:05 +0100168}
169
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200170bool YangSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200171{
172 if (!isList(location, node))
173 return false;
174 const auto keys = listKeys(location, node);
175 return keys.find(key) != keys.end();
176}
177
Václav Kubernátc3866792020-02-20 14:12:56 +0100178bool YangSchema::leafIsKey(const std::string& leafPath) const
179{
180 auto node = getSchemaNode(leafPath);
181 if (!node || node->nodetype() != LYS_LEAF)
182 return false;
183
184 return libyang::Schema_Node_Leaf{node}.is_key().get();
185}
186
Václav Kubernát47a3f672019-11-08 15:42:43 +0100187libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200188{
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200189 // If no node is found find_path prints an error message, so we have to
190 // disable logging
191 // https://github.com/CESNET/libyang/issues/753
192 {
193 int oldOptions;
194 auto logBlocker = make_unique_resource(
195 [&oldOptions]() {
196 oldOptions = libyang::set_log_options(0);
197 },
198 [&oldOptions]() {
199 libyang::set_log_options(oldOptions);
200 });
Václav Kubernát47a3f672019-11-08 15:42:43 +0100201 return m_context->get_node(nullptr, node.c_str());
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200202 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200203}
204
Václav Kubernát47a3f672019-11-08 15:42:43 +0100205
206libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const
207{
208 return impl_getSchemaNode(node);
209}
210
211libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
212{
Václav Kubernátefcac932020-01-10 15:26:32 +0100213 std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node));
Václav Kubernát47a3f672019-11-08 15:42:43 +0100214
215 return impl_getSchemaNode(absPath);
216}
217
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200218const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200219{
220 std::set<std::string> keys;
221 if (!isList(location, node))
222 return keys;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200223 libyang::Schema_Node_List list(getSchemaNode(location, node));
Jan Kundrát4030b772018-08-23 15:54:56 +0200224 const auto& keysVec = list.keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200225
Václav Kubernát90de9502019-11-20 17:19:44 +0100226 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 +0200227 return keys;
228}
229
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200230yang::LeafDataTypes lyTypeToLeafDataTypes(LY_DATA_TYPE type)
Václav Kubernát0d4db442018-07-18 17:18:43 +0200231{
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200232 switch (type) {
Václav Kubernát0d4db442018-07-18 17:18:43 +0200233 case LY_TYPE_STRING:
234 return yang::LeafDataTypes::String;
235 case LY_TYPE_DEC64:
236 return yang::LeafDataTypes::Decimal;
237 case LY_TYPE_BOOL:
238 return yang::LeafDataTypes::Bool;
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200239 case LY_TYPE_INT8:
240 return yang::LeafDataTypes::Int8;
241 case LY_TYPE_INT16:
242 return yang::LeafDataTypes::Int16;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200243 case LY_TYPE_INT32:
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200244 return yang::LeafDataTypes::Int32;
245 case LY_TYPE_INT64:
246 return yang::LeafDataTypes::Int64;
247 case LY_TYPE_UINT8:
248 return yang::LeafDataTypes::Uint8;
249 case LY_TYPE_UINT16:
250 return yang::LeafDataTypes::Uint16;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200251 case LY_TYPE_UINT32:
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200252 return yang::LeafDataTypes::Uint32;
253 case LY_TYPE_UINT64:
254 return yang::LeafDataTypes::Uint64;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200255 case LY_TYPE_ENUM:
256 return yang::LeafDataTypes::Enum;
Václav Kubernátab538992019-03-06 15:30:50 +0100257 case LY_TYPE_BINARY:
258 return yang::LeafDataTypes::Binary;
Václav Kubernáteeb38842019-03-20 19:46:05 +0100259 case LY_TYPE_IDENT:
260 return yang::LeafDataTypes::IdentityRef;
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200261 case LY_TYPE_LEAFREF:
262 return yang::LeafDataTypes::LeafRef;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200263 default:
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200264 using namespace std::string_literals;
265 throw std::logic_error{"internal error: unsupported libyang data type "s + std::to_string(type)};
266 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200267}
268
Václav Kubernát9bf36852020-02-18 17:47:56 +0100269namespace {
270yang::LeafDataTypes impl_leafType(const libyang::S_Schema_Node& node)
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200271{
272 using namespace std::string_literals;
Václav Kubernát9bf36852020-02-18 17:47:56 +0100273 libyang::Schema_Node_Leaf leaf(node);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200274 auto baseType{leaf.type()->base()};
275 try {
276 return lyTypeToLeafDataTypes(baseType);
Václav Kubernát90de9502019-11-20 17:19:44 +0100277 } catch (std::logic_error& ex) {
Václav Kubernát9bf36852020-02-18 17:47:56 +0100278 throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + ex.what());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200279 }
280}
Václav Kubernát9bf36852020-02-18 17:47:56 +0100281}
282
283yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
284{
285 return impl_leafType(getSchemaNode(location, node));
286}
287
288yang::LeafDataTypes YangSchema::leafType(const std::string& path) const
289{
290 return impl_leafType(getSchemaNode(path));
291}
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200292
Václav Kubernátad87ece2020-02-19 15:20:56 +0100293namespace {
294yang::LeafDataTypes impl_leafrefBaseType(const libyang::S_Schema_Node& node)
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200295{
296 using namespace std::string_literals;
Václav Kubernátad87ece2020-02-19 15:20:56 +0100297 libyang::Schema_Node_Leaf leaf(node);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200298 try {
299 return lyTypeToLeafDataTypes(leaf.type()->info()->lref()->target()->type()->base());
Václav Kubernát90de9502019-11-20 17:19:44 +0100300 } catch (std::logic_error& ex) {
Václav Kubernátad87ece2020-02-19 15:20:56 +0100301 throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + ex.what());
Václav Kubernát0d4db442018-07-18 17:18:43 +0200302 }
303}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100304}
305
306yang::LeafDataTypes YangSchema::leafrefBaseType(const schemaPath_& location, const ModuleNodePair& node) const
307{
308 return impl_leafrefBaseType(getSchemaNode(location, node));
309}
310
311yang::LeafDataTypes YangSchema::leafrefBaseType(const std::string& path) const
312{
313 return impl_leafrefBaseType(getSchemaNode(path));
314}
Václav Kubernát0d4db442018-07-18 17:18:43 +0200315
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100316std::string YangSchema::leafrefPath(const std::string& leafrefPath) const
317{
318 using namespace std::string_literals;
319 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath));
320 return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
321}
322
Václav Kubernát0d4db442018-07-18 17:18:43 +0200323std::set<std::string> YangSchema::modules() const
324{
Jan Kundrát4030b772018-08-23 15:54:56 +0200325 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200326
327 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100328 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 +0200329 return res;
330}
331
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200332std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200333{
334 using namespace std::string_view_literals;
335 std::set<std::string> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200336 std::vector<libyang::S_Schema_Node> nodes;
337
Václav Kubernát0d4db442018-07-18 17:18:43 +0200338 if (path.m_nodes.empty()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200339 nodes = m_context->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200340 } else {
Václav Kubernátefcac932020-01-10 15:26:32 +0100341 const auto pathString = pathToSchemaString(path, Prefixes::Always);
342 const auto node = getSchemaNode(pathString);
Václav Kubernát47a3f672019-11-08 15:42:43 +0100343 nodes = node->child_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200344 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200345
346 for (const auto node : nodes) {
347 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200348 continue;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200349 if (recursion == Recursion::Recursive) {
350 for (auto it : node->tree_dfs()) {
351 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
352 }
353 } else {
Václav Kubernát4f77a252019-02-19 16:51:30 +0100354 std::string toInsert;
355 if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) {
356 toInsert += node->module()->name();
357 toInsert += ":";
358 }
359 toInsert += node->name();
360 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200361 }
362 }
363
Václav Kubernát0d4db442018-07-18 17:18:43 +0200364 return res;
365}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200366
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200367std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const
368{
369 std::set<std::string> res;
370 const auto yangModule = m_context->get_module(module.m_name.c_str());
371
372 std::vector<libyang::S_Schema_Node> nodes;
373
374 for (const auto node : yangModule->data_instantiables(0)) {
375 if (recursion == Recursion::Recursive) {
376 for (const auto it : node->tree_dfs()) {
377 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
378 }
379 } else {
380 res.insert(module.m_name + ":" + node->name());
381 }
382 }
383
384 return res;
385}
386
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200387void YangSchema::loadModule(const std::string& moduleName)
388{
389 m_context->load_module(moduleName.c_str());
390}
391
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100392void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
393{
394 m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
395}
396
Václav Kubernátb52dc252019-12-04 13:03:39 +0100397void 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 +0200398{
399 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
400 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100401 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200402 if (moduleSource.empty()) {
403 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
404 }
405 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
406 };
407
408 auto deleter = [](void* data) {
409 free(data);
410 };
411 m_context->add_missing_module_callback(lambda, deleter);
412}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100413
414std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
415{
416 return std::make_shared<libyang::Data_Node>(m_context,
417 path.c_str(),
418 value ? value.value().c_str() : nullptr,
419 LYD_ANYDATA_CONSTSTRING,
Václav Kubernátab612e92019-11-26 19:51:31 +0100420 LYD_PATH_OPT_EDIT);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100421}
422
423std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
424{
425 return m_context->get_module(name.c_str(), nullptr, 0);
426}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100427
428namespace {
429yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node)
430{
431 if (!node) {
432 throw InvalidNodeException();
433 }
434 switch (node->nodetype()) {
435 case LYS_CONTAINER:
436 return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container;
437 case LYS_LEAF:
438 return yang::NodeTypes::Leaf;
439 case LYS_LIST:
440 return yang::NodeTypes::List;
441 default:
Václav Kubernát2a141392020-02-18 17:12:32 +0100442 throw InvalidNodeException(); // FIXME: Implement all types.
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100443 }
444}
445}
446
447yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
448{
449 return impl_nodeType(getSchemaNode(location, node));
450}
451
452yang::NodeTypes YangSchema::nodeType(const std::string& path) const
453{
454 return impl_nodeType(getSchemaNode(path));
455}
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100456
457std::optional<std::string> YangSchema::description(const std::string& path) const
458{
459 auto node = getSchemaNode(path.c_str());
460 return node->dsc() ? std::optional{node->dsc()} : std::nullopt;
461}
462
463std::optional<std::string> YangSchema::units(const std::string& path) const
464{
465 auto node = getSchemaNode(path.c_str());
466 if (node->nodetype() != LYS_LEAF) {
467 return std::nullopt;
468 }
469 libyang::Schema_Node_Leaf leaf{node};
470 auto units = leaf.units();
471
472 // A leaf can specify units as part of its definition.
473 if (units) {
474 return units;
475 }
476
477 // A typedef (or its parent typedefs) can specify units too. We'll use the first `units` we find.
478 for (auto parentTypedef = leaf.type()->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) {
479 units = parentTypedef->units();
480 if (units) {
481 return units;
482 }
483 }
484
485 return std::nullopt;
486}