blob: 132c25ff76e51a2a4f26d2a200a42bae65eb9065 [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::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020097{
98 if (!isList(location, node))
99 return false;
100 const auto keys = listKeys(location, node);
101 return keys.find(key) != keys.end();
102}
103
Václav Kubernátc3866792020-02-20 14:12:56 +0100104bool YangSchema::leafIsKey(const std::string& leafPath) const
105{
106 auto node = getSchemaNode(leafPath);
107 if (!node || node->nodetype() != LYS_LEAF)
108 return false;
109
110 return libyang::Schema_Node_Leaf{node}.is_key().get();
111}
112
Václav Kubernát47a3f672019-11-08 15:42:43 +0100113libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200114{
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200115 // If no node is found find_path prints an error message, so we have to
116 // disable logging
117 // https://github.com/CESNET/libyang/issues/753
118 {
119 int oldOptions;
120 auto logBlocker = make_unique_resource(
121 [&oldOptions]() {
122 oldOptions = libyang::set_log_options(0);
123 },
124 [&oldOptions]() {
125 libyang::set_log_options(oldOptions);
126 });
Václav Kubernát47a3f672019-11-08 15:42:43 +0100127 return m_context->get_node(nullptr, node.c_str());
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200128 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200129}
130
Václav Kubernát47a3f672019-11-08 15:42:43 +0100131
132libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const
133{
134 return impl_getSchemaNode(node);
135}
136
137libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
138{
Václav Kubernátefcac932020-01-10 15:26:32 +0100139 std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node));
Václav Kubernát47a3f672019-11-08 15:42:43 +0100140
141 return impl_getSchemaNode(absPath);
142}
143
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200144const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200145{
146 std::set<std::string> keys;
147 if (!isList(location, node))
148 return keys;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200149 libyang::Schema_Node_List list(getSchemaNode(location, node));
Jan Kundrát4030b772018-08-23 15:54:56 +0200150 const auto& keysVec = list.keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200151
Václav Kubernát90de9502019-11-20 17:19:44 +0100152 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 +0200153 return keys;
154}
155
Václav Kubernát3a99f002020-03-31 02:27:41 +0200156namespace {
Václav Kubernát2984f442020-02-20 17:43:35 +0100157std::set<enum_> enumValues(const libyang::S_Type& typeArg)
Václav Kubernát0d4db442018-07-18 17:18:43 +0200158{
Václav Kubernát2984f442020-02-20 17:43:35 +0100159 auto type = typeArg;
Václav Kubernát3a99f002020-03-31 02:27:41 +0200160 auto enm = type->info()->enums()->enm();
161 // The enum can be a derived type and enm() only returns values,
162 // if that specific typedef changed the possible values. So we go
163 // up the hierarchy until we find a typedef that defined these values.
164 while (enm.empty()) {
165 type = type->der()->type();
166 enm = type->info()->enums()->enm();
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200167 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200168
169 std::vector<libyang::S_Type_Enum> enabled;
170 std::copy_if(enm.begin(), enm.end(), std::back_inserter(enabled), [](const libyang::S_Type_Enum& it) {
171 auto iffeatures = it->iffeature();
172 return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); });
173 });
174
175 std::set<enum_> enumSet;
176 std::transform(enabled.begin(), enabled.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return enum_{it->name()}; });
177 return enumSet;
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200178}
179
Václav Kubernát2984f442020-02-20 17:43:35 +0100180std::set<identityRef_> validIdentities(const libyang::S_Type& type)
Václav Kubernát3a99f002020-03-31 02:27:41 +0200181{
182 std::set<identityRef_> identSet;
183
184 // auto topLevelModule = leaf->module();
185
Václav Kubernát2984f442020-02-20 17:43:35 +0100186 auto info = type->info();
Václav Kubernát3a99f002020-03-31 02:27:41 +0200187 for (auto base : info->ident()->ref()) { // Iterate over all bases
188 identSet.emplace(base->module()->name(), base->name());
189 // Iterate over derived identities (this is recursive!)
190 for (auto derived : base->der()->schema()) {
191 identSet.emplace(derived->module()->name(), derived->name());
192 }
193 }
194
195 return identSet;
196}
197
Václav Kubernát2984f442020-02-20 17:43:35 +0100198std::string leafrefPath(const libyang::S_Type& type)
Václav Kubernát3a99f002020-03-31 02:27:41 +0200199{
Václav Kubernát2984f442020-02-20 17:43:35 +0100200 return type->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200201}
202}
203
Václav Kubernát13b23d72020-04-16 21:49:51 +0200204yang::TypeInfo YangSchema::impl_leafType(const libyang::S_Schema_Node& node) const
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200205{
206 using namespace std::string_literals;
Václav Kubernát3a99f002020-03-31 02:27:41 +0200207 auto leaf = std::make_shared<libyang::Schema_Node_Leaf>(node);
Václav Kubernát13b23d72020-04-16 21:49:51 +0200208 auto leafUnits = leaf->units();
209 std::function<yang::TypeInfo(std::shared_ptr<libyang::Type>)> resolveType;
210 resolveType = [this, &resolveType, leaf, leafUnits] (std::shared_ptr<libyang::Type> type) -> yang::TypeInfo {
211 yang::LeafDataType resType;
Václav Kubernát2984f442020-02-20 17:43:35 +0100212 switch (type->base()) {
213 case LY_TYPE_STRING:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200214 resType.emplace<yang::String>();
215 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100216 case LY_TYPE_DEC64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200217 resType.emplace<yang::Decimal>();
218 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100219 case LY_TYPE_BOOL:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200220 resType.emplace<yang::Bool>();
221 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100222 case LY_TYPE_INT8:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200223 resType.emplace<yang::Int8>();
224 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100225 case LY_TYPE_INT16:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200226 resType.emplace<yang::Int16>();
227 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100228 case LY_TYPE_INT32:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200229 resType.emplace<yang::Int32>();
230 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100231 case LY_TYPE_INT64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200232 resType.emplace<yang::Int64>();
233 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100234 case LY_TYPE_UINT8:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200235 resType.emplace<yang::Uint8>();
236 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100237 case LY_TYPE_UINT16:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200238 resType.emplace<yang::Uint16>();
239 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100240 case LY_TYPE_UINT32:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200241 resType.emplace<yang::Uint32>();
242 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100243 case LY_TYPE_UINT64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200244 resType.emplace<yang::Uint64>();
245 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100246 case LY_TYPE_BINARY:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200247 resType.emplace<yang::Binary>();
248 break;
Jan Kundrát379bb572020-05-07 03:23:13 +0200249 case LY_TYPE_EMPTY:
250 resType.emplace<yang::Empty>();
251 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100252 case LY_TYPE_ENUM:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200253 resType.emplace<yang::Enum>(enumValues(type));
254 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100255 case LY_TYPE_IDENT:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200256 resType.emplace<yang::IdentityRef>(validIdentities(type));
257 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100258 case LY_TYPE_LEAFREF:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200259 resType.emplace<yang::LeafRef>(::leafrefPath(type), std::make_unique<yang::TypeInfo>(leafType(::leafrefPath(type))));
260 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100261 case LY_TYPE_UNION:
262 {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200263 auto resUnion = yang::Union{};
Václav Kubernát2984f442020-02-20 17:43:35 +0100264 for (auto unionType : type->info()->uni()->types()) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200265 resUnion.m_unionTypes.push_back(resolveType(unionType));
Václav Kubernát2984f442020-02-20 17:43:35 +0100266 }
Václav Kubernát13b23d72020-04-16 21:49:51 +0200267 resType.emplace<yang::Union>(std::move(resUnion));
268 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100269 }
270 default:
271 using namespace std::string_literals;
272 throw UnsupportedYangTypeException("the type of "s + leaf->name() + " is not supported: " + std::to_string(leaf->type()->base()));
273 }
Václav Kubernát13b23d72020-04-16 21:49:51 +0200274
275 std::optional<std::string> resUnits;
276
277 if (leafUnits) {
278 resUnits = leafUnits;
279 } else {
280 for (auto parentTypedef = type->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) {
281 auto units = parentTypedef->units();
282 if (units) {
283 resUnits = units;
284 break;
285 }
286 }
287 }
288
289 return yang::TypeInfo(resType, resUnits);
290 };
Václav Kubernát2984f442020-02-20 17:43:35 +0100291 return resolveType(leaf->type());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200292}
293
Václav Kubernát13b23d72020-04-16 21:49:51 +0200294yang::TypeInfo YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100295{
296 return impl_leafType(getSchemaNode(location, node));
297}
298
Václav Kubernát13b23d72020-04-16 21:49:51 +0200299yang::TypeInfo YangSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100300{
301 return impl_leafType(getSchemaNode(path));
302}
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200303
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100304std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const
305{
306 libyang::Schema_Node_Leaf leaf(getSchemaNode(path));
Václav Kubernát76ba4ec2020-05-18 13:26:56 +0200307 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 +0100308}
309
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100310std::string YangSchema::leafrefPath(const std::string& leafrefPath) const
311{
312 using namespace std::string_literals;
313 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath));
314 return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
315}
Václav Kubernát0d4db442018-07-18 17:18:43 +0200316
317std::set<std::string> YangSchema::modules() const
318{
Jan Kundrát4030b772018-08-23 15:54:56 +0200319 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200320
321 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100322 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 +0200323 return res;
324}
325
Václav Kubernát95b08872020-04-28 01:04:17 +0200326std::set<ModuleNodePair> YangSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200327{
328 using namespace std::string_view_literals;
Václav Kubernát95b08872020-04-28 01:04:17 +0200329 std::set<ModuleNodePair> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200330 std::vector<libyang::S_Schema_Node> nodes;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200331 std::string topLevelModule;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200332
Václav Kubernát3a823f42020-04-29 23:40:21 +0200333 if (path.type() == typeid(module_)) {
334 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 +0200335 } else {
Václav Kubernát3a823f42020-04-29 23:40:21 +0200336 auto schemaPath = anyPathToSchemaPath(path);
337 if (schemaPath.m_nodes.empty()) {
338 nodes = m_context->data_instantiables(0);
339 } else {
340 const auto pathString = pathToSchemaString(schemaPath, Prefixes::Always);
341 const auto node = getSchemaNode(pathString);
342 nodes = node->child_instantiables(0);
343 topLevelModule = schemaPath.m_nodes.begin()->m_prefix->m_name;
344 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200345 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200346
Václav Kubernátef085742020-04-21 09:28:44 +0200347 for (const auto& node : nodes) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200348 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200349 continue;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200350
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200351 if (recursion == Recursion::Recursive) {
352 for (auto it : node->tree_dfs()) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200353 res.insert(ModuleNodePair(boost::none, it->path(LYS_PATH_FIRST_PREFIX)));
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200354 }
355 } else {
Václav Kubernát95b08872020-04-28 01:04:17 +0200356 ModuleNodePair toInsert;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200357 if (topLevelModule.empty() || topLevelModule != node->module()->name()) {
Václav Kubernát82d74632020-05-11 15:59:53 +0200358 toInsert.first = node->module()->type() == 0 ? node->module()->name() : libyang::Submodule(node->module()).belongsto()->name();
Václav Kubernát4f77a252019-02-19 16:51:30 +0100359 }
Václav Kubernát95b08872020-04-28 01:04:17 +0200360 toInsert.second = node->name();
Václav Kubernát4f77a252019-02-19 16:51:30 +0100361 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200362 }
363 }
364
Václav Kubernát0d4db442018-07-18 17:18:43 +0200365 return res;
366}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200367
368void YangSchema::loadModule(const std::string& moduleName)
369{
370 m_context->load_module(moduleName.c_str());
371}
372
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100373void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
374{
375 m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
376}
377
Václav Kubernátb52dc252019-12-04 13:03:39 +0100378void 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 +0200379{
380 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
381 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100382 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200383 if (moduleSource.empty()) {
384 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
385 }
386 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
387 };
388
389 auto deleter = [](void* data) {
390 free(data);
391 };
392 m_context->add_missing_module_callback(lambda, deleter);
393}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100394
395std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
396{
397 return std::make_shared<libyang::Data_Node>(m_context,
398 path.c_str(),
399 value ? value.value().c_str() : nullptr,
400 LYD_ANYDATA_CONSTSTRING,
Václav Kubernátab612e92019-11-26 19:51:31 +0100401 LYD_PATH_OPT_EDIT);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100402}
403
404std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
405{
406 return m_context->get_module(name.c_str(), nullptr, 0);
407}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100408
409namespace {
410yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node)
411{
412 if (!node) {
413 throw InvalidNodeException();
414 }
415 switch (node->nodetype()) {
416 case LYS_CONTAINER:
417 return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container;
418 case LYS_LEAF:
419 return yang::NodeTypes::Leaf;
420 case LYS_LIST:
421 return yang::NodeTypes::List;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200422 case LYS_RPC:
423 return yang::NodeTypes::Rpc;
424 case LYS_ACTION:
425 return yang::NodeTypes::Action;
426 case LYS_NOTIF:
427 return yang::NodeTypes::Notification;
428 case LYS_ANYXML:
429 return yang::NodeTypes::AnyXml;
430 case LYS_LEAFLIST:
431 return yang::NodeTypes::LeafList;
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100432 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}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200453
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200454yang::Status YangSchema::status(const std::string& location) const
455{
456 auto node = getSchemaNode(location.c_str());
457 if (node->flags() & LYS_STATUS_DEPRC) {
458 return yang::Status::Deprecated;
459 } else if (node->flags() & LYS_STATUS_OBSLT) {
460 return yang::Status::Obsolete;
461 } else {
462 return yang::Status::Current;
463 }
464}
465
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200466bool YangSchema::isConfig(const std::string& path) const
467{
468 return getSchemaNode(path.c_str())->flags() & LYS_CONFIG_W;
469}
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200470
471std::optional<std::string> YangSchema::defaultValue(const std::string& leafPath) const
472{
473 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafPath));
474
475 if (auto leafDefault = leaf.dflt()) {
476 return leafDefault;
477 }
478
479 for (auto type = leaf.type()->der(); type != nullptr; type = type->type()->der()) {
480 if (auto defaultValue = type->dflt()) {
481 return defaultValue;
482 }
483 }
484
485 return std::nullopt;
486}