blob: 06749ba12e81492b7b1ac93e3aed37ec87e6be5a [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át90de9502019-11-20 17:19:44 +0100284yang::LeafDataTypes YangSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200285{
286 using namespace std::string_literals;
287 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
288 try {
289 return lyTypeToLeafDataTypes(leaf.type()->info()->lref()->target()->type()->base());
Václav Kubernát90de9502019-11-20 17:19:44 +0100290 } catch (std::logic_error& ex) {
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200291 throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what());
Václav Kubernát0d4db442018-07-18 17:18:43 +0200292 }
293}
294
295std::set<std::string> YangSchema::modules() const
296{
Jan Kundrát4030b772018-08-23 15:54:56 +0200297 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200298
299 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100300 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 +0200301 return res;
302}
303
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200304std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200305{
306 using namespace std::string_view_literals;
307 std::set<std::string> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200308 std::vector<libyang::S_Schema_Node> nodes;
309
Václav Kubernát0d4db442018-07-18 17:18:43 +0200310 if (path.m_nodes.empty()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200311 nodes = m_context->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200312 } else {
Václav Kubernátefcac932020-01-10 15:26:32 +0100313 const auto pathString = pathToSchemaString(path, Prefixes::Always);
314 const auto node = getSchemaNode(pathString);
Václav Kubernát47a3f672019-11-08 15:42:43 +0100315 nodes = node->child_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200316 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200317
318 for (const auto node : nodes) {
319 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200320 continue;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200321 if (recursion == Recursion::Recursive) {
322 for (auto it : node->tree_dfs()) {
323 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
324 }
325 } else {
Václav Kubernát4f77a252019-02-19 16:51:30 +0100326 std::string toInsert;
327 if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) {
328 toInsert += node->module()->name();
329 toInsert += ":";
330 }
331 toInsert += node->name();
332 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200333 }
334 }
335
Václav Kubernát0d4db442018-07-18 17:18:43 +0200336 return res;
337}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200338
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200339std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const
340{
341 std::set<std::string> res;
342 const auto yangModule = m_context->get_module(module.m_name.c_str());
343
344 std::vector<libyang::S_Schema_Node> nodes;
345
346 for (const auto node : yangModule->data_instantiables(0)) {
347 if (recursion == Recursion::Recursive) {
348 for (const auto it : node->tree_dfs()) {
349 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
350 }
351 } else {
352 res.insert(module.m_name + ":" + node->name());
353 }
354 }
355
356 return res;
357}
358
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200359void YangSchema::loadModule(const std::string& moduleName)
360{
361 m_context->load_module(moduleName.c_str());
362}
363
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100364void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
365{
366 m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
367}
368
Václav Kubernátb52dc252019-12-04 13:03:39 +0100369void 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 +0200370{
371 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
372 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100373 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200374 if (moduleSource.empty()) {
375 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
376 }
377 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
378 };
379
380 auto deleter = [](void* data) {
381 free(data);
382 };
383 m_context->add_missing_module_callback(lambda, deleter);
384}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100385
386std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
387{
388 return std::make_shared<libyang::Data_Node>(m_context,
389 path.c_str(),
390 value ? value.value().c_str() : nullptr,
391 LYD_ANYDATA_CONSTSTRING,
Václav Kubernátab612e92019-11-26 19:51:31 +0100392 LYD_PATH_OPT_EDIT);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100393}
394
395std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
396{
397 return m_context->get_module(name.c_str(), nullptr, 0);
398}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100399
400namespace {
401yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node)
402{
403 if (!node) {
404 throw InvalidNodeException();
405 }
406 switch (node->nodetype()) {
407 case LYS_CONTAINER:
408 return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container;
409 case LYS_LEAF:
410 return yang::NodeTypes::Leaf;
411 case LYS_LIST:
412 return yang::NodeTypes::List;
413 default:
Václav Kubernát2a141392020-02-18 17:12:32 +0100414 throw InvalidNodeException(); // FIXME: Implement all types.
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100415 }
416}
417}
418
419yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
420{
421 return impl_nodeType(getSchemaNode(location, node));
422}
423
424yang::NodeTypes YangSchema::nodeType(const std::string& path) const
425{
426 return impl_nodeType(getSchemaNode(path));
427}
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100428
429std::optional<std::string> YangSchema::description(const std::string& path) const
430{
431 auto node = getSchemaNode(path.c_str());
432 return node->dsc() ? std::optional{node->dsc()} : std::nullopt;
433}
434
435std::optional<std::string> YangSchema::units(const std::string& path) const
436{
437 auto node = getSchemaNode(path.c_str());
438 if (node->nodetype() != LYS_LEAF) {
439 return std::nullopt;
440 }
441 libyang::Schema_Node_Leaf leaf{node};
442 auto units = leaf.units();
443
444 // A leaf can specify units as part of its definition.
445 if (units) {
446 return units;
447 }
448
449 // A typedef (or its parent typedefs) can specify units too. We'll use the first `units` we find.
450 for (auto parentTypedef = leaf.type()->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) {
451 units = parentTypedef->units();
452 if (units) {
453 return units;
454 }
455 }
456
457 return std::nullopt;
458}