blob: d915eef29b89e70e80d5ccc1fc65f2501d40bf45 [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
260yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
261{
262 using namespace std::string_literals;
263 if (!isLeaf(location, node))
264 throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf");
265
266 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
267 auto baseType{leaf.type()->base()};
268 try {
269 return lyTypeToLeafDataTypes(baseType);
Václav Kubernát90de9502019-11-20 17:19:44 +0100270 } catch (std::logic_error& ex) {
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200271 throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what());
272 }
273}
274
Václav Kubernát90de9502019-11-20 17:19:44 +0100275yang::LeafDataTypes YangSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200276{
277 using namespace std::string_literals;
278 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
279 try {
280 return lyTypeToLeafDataTypes(leaf.type()->info()->lref()->target()->type()->base());
Václav Kubernát90de9502019-11-20 17:19:44 +0100281 } catch (std::logic_error& ex) {
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200282 throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what());
Václav Kubernát0d4db442018-07-18 17:18:43 +0200283 }
284}
285
286std::set<std::string> YangSchema::modules() const
287{
Jan Kundrát4030b772018-08-23 15:54:56 +0200288 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200289
290 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100291 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 +0200292 return res;
293}
294
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200295std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200296{
297 using namespace std::string_view_literals;
298 std::set<std::string> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200299 std::vector<libyang::S_Schema_Node> nodes;
300
Václav Kubernát0d4db442018-07-18 17:18:43 +0200301 if (path.m_nodes.empty()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200302 nodes = m_context->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200303 } else {
Václav Kubernátefcac932020-01-10 15:26:32 +0100304 const auto pathString = pathToSchemaString(path, Prefixes::Always);
305 const auto node = getSchemaNode(pathString);
Václav Kubernát47a3f672019-11-08 15:42:43 +0100306 nodes = node->child_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200307 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200308
309 for (const auto node : nodes) {
310 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200311 continue;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200312 if (recursion == Recursion::Recursive) {
313 for (auto it : node->tree_dfs()) {
314 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
315 }
316 } else {
Václav Kubernát4f77a252019-02-19 16:51:30 +0100317 std::string toInsert;
318 if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) {
319 toInsert += node->module()->name();
320 toInsert += ":";
321 }
322 toInsert += node->name();
323 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200324 }
325 }
326
Václav Kubernát0d4db442018-07-18 17:18:43 +0200327 return res;
328}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200329
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200330std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const
331{
332 std::set<std::string> res;
333 const auto yangModule = m_context->get_module(module.m_name.c_str());
334
335 std::vector<libyang::S_Schema_Node> nodes;
336
337 for (const auto node : yangModule->data_instantiables(0)) {
338 if (recursion == Recursion::Recursive) {
339 for (const auto it : node->tree_dfs()) {
340 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
341 }
342 } else {
343 res.insert(module.m_name + ":" + node->name());
344 }
345 }
346
347 return res;
348}
349
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200350void YangSchema::loadModule(const std::string& moduleName)
351{
352 m_context->load_module(moduleName.c_str());
353}
354
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100355void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
356{
357 m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
358}
359
Václav Kubernátb52dc252019-12-04 13:03:39 +0100360void 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 +0200361{
362 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
363 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100364 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200365 if (moduleSource.empty()) {
366 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
367 }
368 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
369 };
370
371 auto deleter = [](void* data) {
372 free(data);
373 };
374 m_context->add_missing_module_callback(lambda, deleter);
375}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100376
377std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
378{
379 return std::make_shared<libyang::Data_Node>(m_context,
380 path.c_str(),
381 value ? value.value().c_str() : nullptr,
382 LYD_ANYDATA_CONSTSTRING,
Václav Kubernátab612e92019-11-26 19:51:31 +0100383 LYD_PATH_OPT_EDIT);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100384}
385
386std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
387{
388 return m_context->get_module(name.c_str(), nullptr, 0);
389}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100390
391namespace {
392yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node)
393{
394 if (!node) {
395 throw InvalidNodeException();
396 }
397 switch (node->nodetype()) {
398 case LYS_CONTAINER:
399 return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container;
400 case LYS_LEAF:
401 return yang::NodeTypes::Leaf;
402 case LYS_LIST:
403 return yang::NodeTypes::List;
404 default:
Václav Kubernát2a141392020-02-18 17:12:32 +0100405 throw InvalidNodeException(); // FIXME: Implement all types.
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100406 }
407}
408}
409
410yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
411{
412 return impl_nodeType(getSchemaNode(location, node));
413}
414
415yang::NodeTypes YangSchema::nodeType(const std::string& path) const
416{
417 return impl_nodeType(getSchemaNode(path));
418}
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100419
420std::optional<std::string> YangSchema::description(const std::string& path) const
421{
422 auto node = getSchemaNode(path.c_str());
423 return node->dsc() ? std::optional{node->dsc()} : std::nullopt;
424}
425
426std::optional<std::string> YangSchema::units(const std::string& path) const
427{
428 auto node = getSchemaNode(path.c_str());
429 if (node->nodetype() != LYS_LEAF) {
430 return std::nullopt;
431 }
432 libyang::Schema_Node_Leaf leaf{node};
433 auto units = leaf.units();
434
435 // A leaf can specify units as part of its definition.
436 if (units) {
437 return units;
438 }
439
440 // A typedef (or its parent typedefs) can specify units too. We'll use the first `units` we find.
441 for (auto parentTypedef = leaf.type()->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) {
442 units = parentTypedef->units();
443 if (units) {
444 return units;
445 }
446 }
447
448 return std::nullopt;
449}