blob: 06259b949bdfcd1dafc5b435075508277fa0f738 [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átf2b91e02019-04-11 15:36:48 +020013#include "UniqueResource.h"
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
61YangSchema::~YangSchema() = default;
62
63void YangSchema::addSchemaString(const char* schema)
64{
65 if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) {
66 throw YangLoadError("Couldn't load schema");
67 }
68}
69
70void YangSchema::addSchemaDirectory(const char* directoryName)
71{
72 if (m_context->set_searchdir(directoryName)) {
73 throw YangLoadError("Couldn't add schema search directory");
74 }
75}
76
77void YangSchema::addSchemaFile(const char* filename)
78{
79 if (!m_context->parse_module_path(filename, LYS_IN_YANG)) {
80 throw YangLoadError("Couldn't load schema");
81 }
82}
83
Václav Kubernát75877de2019-11-20 17:43:02 +010084bool YangSchema::isModule(const std::string& name) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020085{
86 const auto set = modules();
87 return set.find(name) != set.end();
88}
89
Václav Kubernát2eaceb82018-10-08 19:56:30 +020090bool YangSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020091{
92 const auto schemaNode = getSchemaNode(location, node);
93 return schemaNode && schemaNode->nodetype() == LYS_CONTAINER;
94}
95
Václav Kubernát2eaceb82018-10-08 19:56:30 +020096bool YangSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020097{
98 const auto schemaNode = getSchemaNode(location, node);
99 return schemaNode && schemaNode->nodetype() == LYS_LEAF;
100}
101
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200102bool YangSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200103{
104 const auto schemaNode = getSchemaNode(location, node);
105 return schemaNode && schemaNode->nodetype() == LYS_LIST;
106}
107
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200108bool YangSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200109{
110 if (!isContainer(location, node))
111 return false;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200112 return libyang::Schema_Node_Container(getSchemaNode(location, node)).presence();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200113}
114
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200115bool YangSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200116{
Václav Kubernát989b5de2019-02-20 16:28:35 +0100117 auto enums = enumValues(location, node);
118
119 return std::any_of(enums.begin(), enums.end(), [=](const auto& x) { return x == value; });
120}
121
122const std::set<std::string> YangSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
123{
Václav Kubernát0d4db442018-07-18 17:18:43 +0200124 if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::Enum)
Václav Kubernát989b5de2019-02-20 16:28:35 +0100125 return {};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200126
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200127 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
Václav Kubernát6a713d62018-10-03 18:47:34 +0200128 auto type = leaf.type();
129 auto enm = type->info()->enums()->enm();
130 // The enum can be a derived type and enm() only returns values,
131 // if that specific typedef changed the possible values. So we go
132 // up the hierarchy until we find a typedef that defined these values.
133 while (enm.empty()) {
134 type = type->der()->type();
135 enm = type->info()->enums()->enm();
136 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200137
Václav Kubernáta38d4172019-11-04 12:36:39 +0100138 std::vector<libyang::S_Type_Enum> enabled;
Václav Kubernát90de9502019-11-20 17:19:44 +0100139 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 +0100140 auto iffeatures = it->iffeature();
Václav Kubernát90de9502019-11-20 17:19:44 +0100141 return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); });
Václav Kubernáta38d4172019-11-04 12:36:39 +0100142 });
143
Václav Kubernát989b5de2019-02-20 16:28:35 +0100144 std::set<std::string> enumSet;
Václav Kubernáta38d4172019-11-04 12:36:39 +0100145 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 +0100146 return enumSet;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200147}
148
Václav Kubernáteeb38842019-03-20 19:46:05 +0100149const std::set<std::string> YangSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const
150{
151 if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::IdentityRef)
152 return {};
153
154 std::set<std::string> identSet;
155
156 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
157 auto insertToSet = [&identSet, prefixes, topLevelModule](auto module, auto name) {
158 std::string stringIdent;
159 if (prefixes == Prefixes::Always || topLevelModule != module) {
160 stringIdent += module;
161 stringIdent += ":";
162 }
163 stringIdent += name;
164 identSet.emplace(stringIdent);
165 };
166
167 auto leaf = std::make_shared<libyang::Schema_Node_Leaf>(getSchemaNode(location, node));
168 auto info = leaf->type()->info();
169 for (auto base : info->ident()->ref()) { // Iterate over all bases
170 insertToSet(base->module()->name(), base->name());
171 // Iterate over derived identities (this is recursive!)
172 for (auto derived : base->der()->schema()) {
173 insertToSet(derived->module()->name(), derived->name());
174 }
175 }
176
177 return identSet;
178}
179
180bool YangSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const
181{
182 auto identities = validIdentities(location, node, Prefixes::Always);
183
184 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
185 auto identModule = value.first ? value.first.value() : topLevelModule;
Václav Kubernát1bf704e2019-04-12 13:30:50 +0200186 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 +0100187}
188
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200189bool YangSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200190{
191 if (!isList(location, node))
192 return false;
193 const auto keys = listKeys(location, node);
194 return keys.find(key) != keys.end();
195}
196
Václav Kubernát47a3f672019-11-08 15:42:43 +0100197libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200198{
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200199 // If no node is found find_path prints an error message, so we have to
200 // disable logging
201 // https://github.com/CESNET/libyang/issues/753
202 {
203 int oldOptions;
204 auto logBlocker = make_unique_resource(
205 [&oldOptions]() {
206 oldOptions = libyang::set_log_options(0);
207 },
208 [&oldOptions]() {
209 libyang::set_log_options(oldOptions);
210 });
Václav Kubernát47a3f672019-11-08 15:42:43 +0100211 return m_context->get_node(nullptr, node.c_str());
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200212 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200213}
214
Václav Kubernát47a3f672019-11-08 15:42:43 +0100215
216libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const
217{
218 return impl_getSchemaNode(node);
219}
220
221libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
222{
Václav Kubernátefcac932020-01-10 15:26:32 +0100223 std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node));
Václav Kubernát47a3f672019-11-08 15:42:43 +0100224
225 return impl_getSchemaNode(absPath);
226}
227
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200228const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200229{
230 std::set<std::string> keys;
231 if (!isList(location, node))
232 return keys;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200233 libyang::Schema_Node_List list(getSchemaNode(location, node));
Jan Kundrát4030b772018-08-23 15:54:56 +0200234 const auto& keysVec = list.keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200235
Václav Kubernát90de9502019-11-20 17:19:44 +0100236 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 +0200237 return keys;
238}
239
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200240yang::LeafDataTypes lyTypeToLeafDataTypes(LY_DATA_TYPE type)
Václav Kubernát0d4db442018-07-18 17:18:43 +0200241{
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200242 switch (type) {
Václav Kubernát0d4db442018-07-18 17:18:43 +0200243 case LY_TYPE_STRING:
244 return yang::LeafDataTypes::String;
245 case LY_TYPE_DEC64:
246 return yang::LeafDataTypes::Decimal;
247 case LY_TYPE_BOOL:
248 return yang::LeafDataTypes::Bool;
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200249 case LY_TYPE_INT8:
250 return yang::LeafDataTypes::Int8;
251 case LY_TYPE_INT16:
252 return yang::LeafDataTypes::Int16;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200253 case LY_TYPE_INT32:
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200254 return yang::LeafDataTypes::Int32;
255 case LY_TYPE_INT64:
256 return yang::LeafDataTypes::Int64;
257 case LY_TYPE_UINT8:
258 return yang::LeafDataTypes::Uint8;
259 case LY_TYPE_UINT16:
260 return yang::LeafDataTypes::Uint16;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200261 case LY_TYPE_UINT32:
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200262 return yang::LeafDataTypes::Uint32;
263 case LY_TYPE_UINT64:
264 return yang::LeafDataTypes::Uint64;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200265 case LY_TYPE_ENUM:
266 return yang::LeafDataTypes::Enum;
Václav Kubernátab538992019-03-06 15:30:50 +0100267 case LY_TYPE_BINARY:
268 return yang::LeafDataTypes::Binary;
Václav Kubernáteeb38842019-03-20 19:46:05 +0100269 case LY_TYPE_IDENT:
270 return yang::LeafDataTypes::IdentityRef;
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200271 case LY_TYPE_LEAFREF:
272 return yang::LeafDataTypes::LeafRef;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200273 default:
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200274 using namespace std::string_literals;
275 throw std::logic_error{"internal error: unsupported libyang data type "s + std::to_string(type)};
276 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200277}
278
279yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
280{
281 using namespace std::string_literals;
282 if (!isLeaf(location, node))
283 throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf");
284
285 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
286 auto baseType{leaf.type()->base()};
287 try {
288 return lyTypeToLeafDataTypes(baseType);
Václav Kubernát90de9502019-11-20 17:19:44 +0100289 } catch (std::logic_error& ex) {
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200290 throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what());
291 }
292}
293
Václav Kubernát90de9502019-11-20 17:19:44 +0100294yang::LeafDataTypes YangSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200295{
296 using namespace std::string_literals;
297 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
298 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át6a8d1d92019-04-24 20:30:36 +0200301 throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what());
Václav Kubernát0d4db442018-07-18 17:18:43 +0200302 }
303}
304
305std::set<std::string> YangSchema::modules() const
306{
Jan Kundrát4030b772018-08-23 15:54:56 +0200307 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200308
309 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100310 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 +0200311 return res;
312}
313
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200314std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200315{
316 using namespace std::string_view_literals;
317 std::set<std::string> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200318 std::vector<libyang::S_Schema_Node> nodes;
319
Václav Kubernát0d4db442018-07-18 17:18:43 +0200320 if (path.m_nodes.empty()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200321 nodes = m_context->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200322 } else {
Václav Kubernátefcac932020-01-10 15:26:32 +0100323 const auto pathString = pathToSchemaString(path, Prefixes::Always);
324 const auto node = getSchemaNode(pathString);
Václav Kubernát47a3f672019-11-08 15:42:43 +0100325 nodes = node->child_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200326 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200327
328 for (const auto node : nodes) {
329 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200330 continue;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200331 if (recursion == Recursion::Recursive) {
332 for (auto it : node->tree_dfs()) {
333 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
334 }
335 } else {
Václav Kubernát4f77a252019-02-19 16:51:30 +0100336 std::string toInsert;
337 if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) {
338 toInsert += node->module()->name();
339 toInsert += ":";
340 }
341 toInsert += node->name();
342 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200343 }
344 }
345
Václav Kubernát0d4db442018-07-18 17:18:43 +0200346 return res;
347}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200348
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200349std::set<std::string> YangSchema::moduleNodes(const module_& module, const Recursion recursion) const
350{
351 std::set<std::string> res;
352 const auto yangModule = m_context->get_module(module.m_name.c_str());
353
354 std::vector<libyang::S_Schema_Node> nodes;
355
356 for (const auto node : yangModule->data_instantiables(0)) {
357 if (recursion == Recursion::Recursive) {
358 for (const auto it : node->tree_dfs()) {
359 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
360 }
361 } else {
362 res.insert(module.m_name + ":" + node->name());
363 }
364 }
365
366 return res;
367}
368
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200369void YangSchema::loadModule(const std::string& moduleName)
370{
371 m_context->load_module(moduleName.c_str());
372}
373
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100374void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
375{
376 m_context->get_module(moduleName.c_str())->feature_enable(featureName.c_str());
377}
378
Václav Kubernátb52dc252019-12-04 13:03:39 +0100379void 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 +0200380{
381 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
382 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100383 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200384 if (moduleSource.empty()) {
385 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
386 }
387 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
388 };
389
390 auto deleter = [](void* data) {
391 free(data);
392 };
393 m_context->add_missing_module_callback(lambda, deleter);
394}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100395
396std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
397{
398 return std::make_shared<libyang::Data_Node>(m_context,
399 path.c_str(),
400 value ? value.value().c_str() : nullptr,
401 LYD_ANYDATA_CONSTSTRING,
402 0);
403}
404
405std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
406{
407 return m_context->get_module(name.c_str(), nullptr, 0);
408}