blob: 643150040435e946a7c99d7ee395598da17ca062 [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>
10#include <libyang/Tree_Schema.hpp>
11#include <string_view>
Václav Kubernátf2b91e02019-04-11 15:36:48 +020012#include "UniqueResource.h"
Václav Kubernát0d4db442018-07-18 17:18:43 +020013#include "utils.hpp"
14#include "yang_schema.hpp"
15
16class YangLoadError : public std::runtime_error {
17public:
18 using std::runtime_error::runtime_error;
19 ~YangLoadError() override = default;
20};
21
22class UnsupportedYangTypeException : public std::runtime_error {
23public:
24 using std::runtime_error::runtime_error;
25 ~UnsupportedYangTypeException() override = default;
26};
27
28class InvalidSchemaQueryException : public std::runtime_error {
29public:
30 using std::runtime_error::runtime_error;
31 ~InvalidSchemaQueryException() override = default;
32};
33
Václav Kubernát2eaceb82018-10-08 19:56:30 +020034template <typename T>
35std::string pathToYangAbsSchemPath(const T& path)
Václav Kubernát0d4db442018-07-18 17:18:43 +020036{
37 std::string res = "/";
38 std::string currentModule;
39
40 for (const auto& it : path.m_nodes) {
41 const auto name = nodeToSchemaString(it);
42
43 if (it.m_suffix.type() == typeid(module_)) {
44 currentModule = name;
45 continue;
46 } else {
47 res += currentModule + ":";
48 res += name + "/";
49 }
50 }
51
52 return res;
53}
54
55YangSchema::YangSchema()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020056 : 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 +020057{
Václav Kubernát0d4db442018-07-18 17:18:43 +020058}
59
60YangSchema::~YangSchema() = default;
61
62void YangSchema::addSchemaString(const char* schema)
63{
64 if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) {
65 throw YangLoadError("Couldn't load schema");
66 }
67}
68
69void YangSchema::addSchemaDirectory(const char* directoryName)
70{
71 if (m_context->set_searchdir(directoryName)) {
72 throw YangLoadError("Couldn't add schema search directory");
73 }
74}
75
76void YangSchema::addSchemaFile(const char* filename)
77{
78 if (!m_context->parse_module_path(filename, LYS_IN_YANG)) {
79 throw YangLoadError("Couldn't load schema");
80 }
81}
82
Václav Kubernát2eaceb82018-10-08 19:56:30 +020083bool YangSchema::isModule(const schemaPath_&, const std::string& name) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020084{
85 const auto set = modules();
86 return set.find(name) != set.end();
87}
88
Václav Kubernát2eaceb82018-10-08 19:56:30 +020089bool YangSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020090{
91 const auto schemaNode = getSchemaNode(location, node);
92 return schemaNode && schemaNode->nodetype() == LYS_CONTAINER;
93}
94
Václav Kubernát2eaceb82018-10-08 19:56:30 +020095bool YangSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020096{
97 const auto schemaNode = getSchemaNode(location, node);
98 return schemaNode && schemaNode->nodetype() == LYS_LEAF;
99}
100
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200101bool YangSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200102{
103 const auto schemaNode = getSchemaNode(location, node);
104 return schemaNode && schemaNode->nodetype() == LYS_LIST;
105}
106
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200107bool YangSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200108{
109 if (!isContainer(location, node))
110 return false;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200111 return libyang::Schema_Node_Container(getSchemaNode(location, node)).presence();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200112}
113
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200114bool YangSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200115{
Václav Kubernát989b5de2019-02-20 16:28:35 +0100116 auto enums = enumValues(location, node);
117
118 return std::any_of(enums.begin(), enums.end(), [=](const auto& x) { return x == value; });
119}
120
121const std::set<std::string> YangSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
122{
Václav Kubernát0d4db442018-07-18 17:18:43 +0200123 if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::Enum)
Václav Kubernát989b5de2019-02-20 16:28:35 +0100124 return {};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200125
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200126 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
Václav Kubernát6a713d62018-10-03 18:47:34 +0200127 auto type = leaf.type();
128 auto enm = type->info()->enums()->enm();
129 // The enum can be a derived type and enm() only returns values,
130 // if that specific typedef changed the possible values. So we go
131 // up the hierarchy until we find a typedef that defined these values.
132 while (enm.empty()) {
133 type = type->der()->type();
134 enm = type->info()->enums()->enm();
135 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200136
Václav Kubernát989b5de2019-02-20 16:28:35 +0100137 std::set<std::string> enumSet;
138 std::transform(enm.begin(), enm.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return it->name(); });
139 return enumSet;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200140}
141
Václav Kubernáteeb38842019-03-20 19:46:05 +0100142const std::set<std::string> YangSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const
143{
144 if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::IdentityRef)
145 return {};
146
147 std::set<std::string> identSet;
148
149 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
150 auto insertToSet = [&identSet, prefixes, topLevelModule](auto module, auto name) {
151 std::string stringIdent;
152 if (prefixes == Prefixes::Always || topLevelModule != module) {
153 stringIdent += module;
154 stringIdent += ":";
155 }
156 stringIdent += name;
157 identSet.emplace(stringIdent);
158 };
159
160 auto leaf = std::make_shared<libyang::Schema_Node_Leaf>(getSchemaNode(location, node));
161 auto info = leaf->type()->info();
162 for (auto base : info->ident()->ref()) { // Iterate over all bases
163 insertToSet(base->module()->name(), base->name());
164 // Iterate over derived identities (this is recursive!)
165 for (auto derived : base->der()->schema()) {
166 insertToSet(derived->module()->name(), derived->name());
167 }
168 }
169
170 return identSet;
171}
172
173bool YangSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const
174{
175 auto identities = validIdentities(location, node, Prefixes::Always);
176
177 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
178 auto identModule = value.first ? value.first.value() : topLevelModule;
179 return std::any_of(identities.begin(), identities.end(), [identModule, value](const auto& x) { return x == identModule + ":" + value.second; });
180}
181
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200182bool YangSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200183{
184 if (!isList(location, node))
185 return false;
186 const auto keys = listKeys(location, node);
187 return keys.find(key) != keys.end();
188}
189
190bool YangSchema::nodeExists(const std::string& location, const std::string& node) const
191{
192 const auto absPath = location + "/" + node;
193 const auto set = m_context->find_path(absPath.c_str());
194 return set->number() == 1;
195}
196
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200197libyang::S_Set YangSchema::getNodeSet(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200198{
199 std::string absPath = location.m_nodes.empty() ? "" : "/";
200 absPath += pathToAbsoluteSchemaString(location) + "/" + fullNodeName(location, node);
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200201
202 // If no node is found find_path prints an error message, so we have to
203 // disable logging
204 // https://github.com/CESNET/libyang/issues/753
205 {
206 int oldOptions;
207 auto logBlocker = make_unique_resource(
208 [&oldOptions]() {
209 oldOptions = libyang::set_log_options(0);
210 },
211 [&oldOptions]() {
212 libyang::set_log_options(oldOptions);
213 });
214 return m_context->find_path(absPath.c_str());
215 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200216}
217
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200218libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200219{
220 const auto set = getNodeSet(location, node);
221 if (!set)
222 return nullptr;
Jan Kundrát4030b772018-08-23 15:54:56 +0200223 const auto& schemaSet = set->schema();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200224 if (set->number() != 1)
225 return nullptr;
Jan Kundrát4030b772018-08-23 15:54:56 +0200226 return *schemaSet.begin();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200227}
228
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200229const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200230{
231 std::set<std::string> keys;
232 if (!isList(location, node))
233 return keys;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200234 libyang::Schema_Node_List list(getSchemaNode(location, node));
Jan Kundrát4030b772018-08-23 15:54:56 +0200235 const auto& keysVec = list.keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200236
Jan Kundrát4030b772018-08-23 15:54:56 +0200237 std::transform(keysVec.begin(), keysVec.end(), std::inserter(keys, keys.begin()),
Václav Kubernát0d4db442018-07-18 17:18:43 +0200238 [] (const auto& it) {return it->name();});
239 return keys;
240}
241
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200242yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200243{
244 using namespace std::string_literals;
245 if (!isLeaf(location, node))
246 throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf");
247
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200248 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200249 switch (leaf.type()->base()) {
250 case LY_TYPE_STRING:
251 return yang::LeafDataTypes::String;
252 case LY_TYPE_DEC64:
253 return yang::LeafDataTypes::Decimal;
254 case LY_TYPE_BOOL:
255 return yang::LeafDataTypes::Bool;
256 case LY_TYPE_INT32:
257 return yang::LeafDataTypes::Int;
258 case LY_TYPE_UINT32:
259 return yang::LeafDataTypes::Uint;
260 case LY_TYPE_ENUM:
261 return yang::LeafDataTypes::Enum;
Václav Kubernátab538992019-03-06 15:30:50 +0100262 case LY_TYPE_BINARY:
263 return yang::LeafDataTypes::Binary;
Václav Kubernáteeb38842019-03-20 19:46:05 +0100264 case LY_TYPE_IDENT:
265 return yang::LeafDataTypes::IdentityRef;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200266 default:
267 throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported");
268 }
269}
270
271std::set<std::string> YangSchema::modules() const
272{
Jan Kundrát4030b772018-08-23 15:54:56 +0200273 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200274
275 std::set<std::string> res;
Jan Kundrát4030b772018-08-23 15:54:56 +0200276 std::transform(modules.begin(), modules.end(),
Václav Kubernát0d4db442018-07-18 17:18:43 +0200277 std::inserter(res, res.end()),
278 [] (const auto module) { return module->name(); });
279 return res;
280}
281
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200282std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200283{
284 using namespace std::string_view_literals;
285 std::set<std::string> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200286 std::vector<libyang::S_Schema_Node> nodes;
287
Václav Kubernát0d4db442018-07-18 17:18:43 +0200288 if (path.m_nodes.empty()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200289 nodes = m_context->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200290 } else {
291 const auto absolutePath = "/" + pathToAbsoluteSchemaString(path);
292 const auto set = m_context->find_path(absolutePath.c_str());
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200293 const auto schemaSet = set->schema();
Jan Kundrát4030b772018-08-23 15:54:56 +0200294 for (auto it = (*schemaSet.begin())->child(); it; it = it->next()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200295 nodes.push_back(it);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200296 }
297 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200298
299 for (const auto node : nodes) {
300 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200301 continue;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200302 if (recursion == Recursion::Recursive) {
303 for (auto it : node->tree_dfs()) {
304 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
305 }
306 } else {
Václav Kubernát4f77a252019-02-19 16:51:30 +0100307 std::string toInsert;
308 if (path.m_nodes.empty() || path.m_nodes.front().m_prefix.get().m_name != node->module()->name()) {
309 toInsert += node->module()->name();
310 toInsert += ":";
311 }
312 toInsert += node->name();
313 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200314 }
315 }
316
Václav Kubernát0d4db442018-07-18 17:18:43 +0200317 return res;
318}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200319
320void YangSchema::loadModule(const std::string& moduleName)
321{
322 m_context->load_module(moduleName.c_str());
323}
324
325void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*)>& clb)
326{
327 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
328 (void)submod_revision;
329 auto moduleSource = clb(mod_name, mod_revision, submod_name);
330 if (moduleSource.empty()) {
331 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
332 }
333 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
334 };
335
336 auto deleter = [](void* data) {
337 free(data);
338 };
339 m_context->add_missing_module_callback(lambda, deleter);
340}