blob: 254f8a969ad7d4b83fb4315ecfd776020fff28c4 [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át47a3f672019-11-08 15:42:43 +0100178libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200179{
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200180 // If no node is found find_path prints an error message, so we have to
181 // disable logging
182 // https://github.com/CESNET/libyang/issues/753
183 {
184 int oldOptions;
185 auto logBlocker = make_unique_resource(
186 [&oldOptions]() {
187 oldOptions = libyang::set_log_options(0);
188 },
189 [&oldOptions]() {
190 libyang::set_log_options(oldOptions);
191 });
Václav Kubernát47a3f672019-11-08 15:42:43 +0100192 return m_context->get_node(nullptr, node.c_str());
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200193 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200194}
195
Václav Kubernát47a3f672019-11-08 15:42:43 +0100196
197libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const
198{
199 return impl_getSchemaNode(node);
200}
201
202libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
203{
Václav Kubernátefcac932020-01-10 15:26:32 +0100204 std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node));
Václav Kubernát47a3f672019-11-08 15:42:43 +0100205
206 return impl_getSchemaNode(absPath);
207}
208
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200209const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200210{
211 std::set<std::string> keys;
212 if (!isList(location, node))
213 return keys;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200214 libyang::Schema_Node_List list(getSchemaNode(location, node));
Jan Kundrát4030b772018-08-23 15:54:56 +0200215 const auto& keysVec = list.keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200216
Václav Kubernát90de9502019-11-20 17:19:44 +0100217 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 +0200218 return keys;
219}
220
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200221yang::LeafDataTypes lyTypeToLeafDataTypes(LY_DATA_TYPE type)
Václav Kubernát0d4db442018-07-18 17:18:43 +0200222{
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200223 switch (type) {
Václav Kubernát0d4db442018-07-18 17:18:43 +0200224 case LY_TYPE_STRING:
225 return yang::LeafDataTypes::String;
226 case LY_TYPE_DEC64:
227 return yang::LeafDataTypes::Decimal;
228 case LY_TYPE_BOOL:
229 return yang::LeafDataTypes::Bool;
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200230 case LY_TYPE_INT8:
231 return yang::LeafDataTypes::Int8;
232 case LY_TYPE_INT16:
233 return yang::LeafDataTypes::Int16;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200234 case LY_TYPE_INT32:
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200235 return yang::LeafDataTypes::Int32;
236 case LY_TYPE_INT64:
237 return yang::LeafDataTypes::Int64;
238 case LY_TYPE_UINT8:
239 return yang::LeafDataTypes::Uint8;
240 case LY_TYPE_UINT16:
241 return yang::LeafDataTypes::Uint16;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200242 case LY_TYPE_UINT32:
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200243 return yang::LeafDataTypes::Uint32;
244 case LY_TYPE_UINT64:
245 return yang::LeafDataTypes::Uint64;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200246 case LY_TYPE_ENUM:
247 return yang::LeafDataTypes::Enum;
Václav Kubernátab538992019-03-06 15:30:50 +0100248 case LY_TYPE_BINARY:
249 return yang::LeafDataTypes::Binary;
Václav Kubernáteeb38842019-03-20 19:46:05 +0100250 case LY_TYPE_IDENT:
251 return yang::LeafDataTypes::IdentityRef;
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200252 case LY_TYPE_LEAFREF:
253 return yang::LeafDataTypes::LeafRef;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200254 default:
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200255 using namespace std::string_literals;
256 throw std::logic_error{"internal error: unsupported libyang data type "s + std::to_string(type)};
257 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200258}
259
Václav Kubernát9bf36852020-02-18 17:47:56 +0100260namespace {
261yang::LeafDataTypes impl_leafType(const libyang::S_Schema_Node& node)
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200262{
263 using namespace std::string_literals;
Václav Kubernát9bf36852020-02-18 17:47:56 +0100264 libyang::Schema_Node_Leaf leaf(node);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200265 auto baseType{leaf.type()->base()};
266 try {
267 return lyTypeToLeafDataTypes(baseType);
Václav Kubernát90de9502019-11-20 17:19:44 +0100268 } catch (std::logic_error& ex) {
Václav Kubernát9bf36852020-02-18 17:47:56 +0100269 throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + ex.what());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200270 }
271}
Václav Kubernát9bf36852020-02-18 17:47:56 +0100272}
273
274yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
275{
276 return impl_leafType(getSchemaNode(location, node));
277}
278
279yang::LeafDataTypes YangSchema::leafType(const std::string& path) const
280{
281 return impl_leafType(getSchemaNode(path));
282}
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200283
Václav Kubernátad87ece2020-02-19 15:20:56 +0100284namespace {
285yang::LeafDataTypes impl_leafrefBaseType(const libyang::S_Schema_Node& node)
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200286{
287 using namespace std::string_literals;
Václav Kubernátad87ece2020-02-19 15:20:56 +0100288 libyang::Schema_Node_Leaf leaf(node);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200289 try {
290 return lyTypeToLeafDataTypes(leaf.type()->info()->lref()->target()->type()->base());
Václav Kubernát90de9502019-11-20 17:19:44 +0100291 } catch (std::logic_error& ex) {
Václav Kubernátad87ece2020-02-19 15:20:56 +0100292 throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + ex.what());
Václav Kubernát0d4db442018-07-18 17:18:43 +0200293 }
294}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100295}
296
297yang::LeafDataTypes YangSchema::leafrefBaseType(const schemaPath_& location, const ModuleNodePair& node) const
298{
299 return impl_leafrefBaseType(getSchemaNode(location, node));
300}
301
302yang::LeafDataTypes YangSchema::leafrefBaseType(const std::string& path) const
303{
304 return impl_leafrefBaseType(getSchemaNode(path));
305}
Václav Kubernát0d4db442018-07-18 17:18:43 +0200306
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100307std::string YangSchema::leafrefPath(const std::string& leafrefPath) const
308{
309 using namespace std::string_literals;
310 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath));
311 return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
312}
313
Václav Kubernát0d4db442018-07-18 17:18:43 +0200314std::set<std::string> YangSchema::modules() const
315{
Jan Kundrát4030b772018-08-23 15:54:56 +0200316 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200317
318 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100319 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 +0200320 return res;
321}
322
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200323std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200324{
325 using namespace std::string_view_literals;
326 std::set<std::string> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200327 std::vector<libyang::S_Schema_Node> nodes;
328
Václav Kubernát0d4db442018-07-18 17:18:43 +0200329 if (path.m_nodes.empty()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200330 nodes = m_context->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200331 } else {
Václav Kubernátefcac932020-01-10 15:26:32 +0100332 const auto pathString = pathToSchemaString(path, Prefixes::Always);
333 const auto node = getSchemaNode(pathString);
Václav Kubernát47a3f672019-11-08 15:42:43 +0100334 nodes = node->child_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200335 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200336
337 for (const auto node : nodes) {
338 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200339 continue;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200340 if (recursion == Recursion::Recursive) {
341 for (auto it : node->tree_dfs()) {
342 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
343 }
344 } else {
Václav Kubernát4f77a252019-02-19 16:51:30 +0100345 std::string toInsert;
346 if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) {
347 toInsert += node->module()->name();
348 toInsert += ":";
349 }
350 toInsert += node->name();
351 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200352 }
353 }
354
Václav Kubernát0d4db442018-07-18 17:18:43 +0200355 return res;
356}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200357
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200358std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const
359{
360 std::set<std::string> res;
361 const auto yangModule = m_context->get_module(module.m_name.c_str());
362
363 std::vector<libyang::S_Schema_Node> nodes;
364
365 for (const auto node : yangModule->data_instantiables(0)) {
366 if (recursion == Recursion::Recursive) {
367 for (const auto it : node->tree_dfs()) {
368 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
369 }
370 } else {
371 res.insert(module.m_name + ":" + node->name());
372 }
373 }
374
375 return res;
376}
377
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200378void YangSchema::loadModule(const std::string& moduleName)
379{
380 m_context->load_module(moduleName.c_str());
381}
382
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100383void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
384{
385 m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
386}
387
Václav Kubernátb52dc252019-12-04 13:03:39 +0100388void 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 +0200389{
390 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
391 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100392 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200393 if (moduleSource.empty()) {
394 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
395 }
396 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
397 };
398
399 auto deleter = [](void* data) {
400 free(data);
401 };
402 m_context->add_missing_module_callback(lambda, deleter);
403}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100404
405std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
406{
407 return std::make_shared<libyang::Data_Node>(m_context,
408 path.c_str(),
409 value ? value.value().c_str() : nullptr,
410 LYD_ANYDATA_CONSTSTRING,
Václav Kubernátab612e92019-11-26 19:51:31 +0100411 LYD_PATH_OPT_EDIT);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100412}
413
414std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
415{
416 return m_context->get_module(name.c_str(), nullptr, 0);
417}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100418
419namespace {
420yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node)
421{
422 if (!node) {
423 throw InvalidNodeException();
424 }
425 switch (node->nodetype()) {
426 case LYS_CONTAINER:
427 return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container;
428 case LYS_LEAF:
429 return yang::NodeTypes::Leaf;
430 case LYS_LIST:
431 return yang::NodeTypes::List;
432 default:
Václav Kubernát2a141392020-02-18 17:12:32 +0100433 throw InvalidNodeException(); // FIXME: Implement all types.
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100434 }
435}
436}
437
438yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
439{
440 return impl_nodeType(getSchemaNode(location, node));
441}
442
443yang::NodeTypes YangSchema::nodeType(const std::string& path) const
444{
445 return impl_nodeType(getSchemaNode(path));
446}
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100447
448std::optional<std::string> YangSchema::description(const std::string& path) const
449{
450 auto node = getSchemaNode(path.c_str());
451 return node->dsc() ? std::optional{node->dsc()} : std::nullopt;
452}
453
454std::optional<std::string> YangSchema::units(const std::string& path) const
455{
456 auto node = getSchemaNode(path.c_str());
457 if (node->nodetype() != LYS_LEAF) {
458 return std::nullopt;
459 }
460 libyang::Schema_Node_Leaf leaf{node};
461 auto units = leaf.units();
462
463 // A leaf can specify units as part of its definition.
464 if (units) {
465 return units;
466 }
467
468 // A typedef (or its parent typedefs) can specify units too. We'll use the first `units` we find.
469 for (auto parentTypedef = leaf.type()->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) {
470 units = parentTypedef->units();
471 if (units) {
472 return units;
473 }
474 }
475
476 return std::nullopt;
477}