blob: 522e58a0fb532fe84b2dfc3d01d0a9fdf52ef11c [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>
12#include "utils.hpp"
13#include "yang_schema.hpp"
14
15class YangLoadError : public std::runtime_error {
16public:
17 using std::runtime_error::runtime_error;
18 ~YangLoadError() override = default;
19};
20
21class UnsupportedYangTypeException : public std::runtime_error {
22public:
23 using std::runtime_error::runtime_error;
24 ~UnsupportedYangTypeException() override = default;
25};
26
27class InvalidSchemaQueryException : public std::runtime_error {
28public:
29 using std::runtime_error::runtime_error;
30 ~InvalidSchemaQueryException() override = default;
31};
32
Václav Kubernát2eaceb82018-10-08 19:56:30 +020033template <typename T>
34std::string pathToYangAbsSchemPath(const T& path)
Václav Kubernát0d4db442018-07-18 17:18:43 +020035{
36 std::string res = "/";
37 std::string currentModule;
38
39 for (const auto& it : path.m_nodes) {
40 const auto name = nodeToSchemaString(it);
41
42 if (it.m_suffix.type() == typeid(module_)) {
43 currentModule = name;
44 continue;
45 } else {
46 res += currentModule + ":";
47 res += name + "/";
48 }
49 }
50
51 return res;
52}
53
54YangSchema::YangSchema()
Václav Kubernáta6c5fff2018-09-07 15:16:25 +020055 : 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 +020056{
Václav Kubernát0d4db442018-07-18 17:18:43 +020057}
58
59YangSchema::~YangSchema() = default;
60
61void YangSchema::addSchemaString(const char* schema)
62{
63 if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) {
64 throw YangLoadError("Couldn't load schema");
65 }
66}
67
68void YangSchema::addSchemaDirectory(const char* directoryName)
69{
70 if (m_context->set_searchdir(directoryName)) {
71 throw YangLoadError("Couldn't add schema search directory");
72 }
73}
74
75void YangSchema::addSchemaFile(const char* filename)
76{
77 if (!m_context->parse_module_path(filename, LYS_IN_YANG)) {
78 throw YangLoadError("Couldn't load schema");
79 }
80}
81
Václav Kubernát2eaceb82018-10-08 19:56:30 +020082bool YangSchema::isModule(const schemaPath_&, const std::string& name) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020083{
84 const auto set = modules();
85 return set.find(name) != set.end();
86}
87
Václav Kubernát2eaceb82018-10-08 19:56:30 +020088bool YangSchema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020089{
90 const auto schemaNode = getSchemaNode(location, node);
91 return schemaNode && schemaNode->nodetype() == LYS_CONTAINER;
92}
93
Václav Kubernát2eaceb82018-10-08 19:56:30 +020094bool YangSchema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020095{
96 const auto schemaNode = getSchemaNode(location, node);
97 return schemaNode && schemaNode->nodetype() == LYS_LEAF;
98}
99
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200100bool YangSchema::isList(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200101{
102 const auto schemaNode = getSchemaNode(location, node);
103 return schemaNode && schemaNode->nodetype() == LYS_LIST;
104}
105
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200106bool YangSchema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200107{
108 if (!isContainer(location, node))
109 return false;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200110 return libyang::Schema_Node_Container(getSchemaNode(location, node)).presence();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200111}
112
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200113bool YangSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200114{
115 if (!isLeaf(location, node) || leafType(location, node) != yang::LeafDataTypes::Enum)
116 return false;
117
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200118 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
Václav Kubernát6a713d62018-10-03 18:47:34 +0200119 auto type = leaf.type();
120 auto enm = type->info()->enums()->enm();
121 // The enum can be a derived type and enm() only returns values,
122 // if that specific typedef changed the possible values. So we go
123 // up the hierarchy until we find a typedef that defined these values.
124 while (enm.empty()) {
125 type = type->der()->type();
126 enm = type->info()->enums()->enm();
127 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200128
Jan Kundrát4030b772018-08-23 15:54:56 +0200129 return std::any_of(enm.begin(), enm.end(), [=](const auto& x) { return x->name() == value; });
Václav Kubernát0d4db442018-07-18 17:18:43 +0200130}
131
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200132bool YangSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200133{
134 if (!isList(location, node))
135 return false;
136 const auto keys = listKeys(location, node);
137 return keys.find(key) != keys.end();
138}
139
140bool YangSchema::nodeExists(const std::string& location, const std::string& node) const
141{
142 const auto absPath = location + "/" + node;
143 const auto set = m_context->find_path(absPath.c_str());
144 return set->number() == 1;
145}
146
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200147libyang::S_Set YangSchema::getNodeSet(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200148{
149 std::string absPath = location.m_nodes.empty() ? "" : "/";
150 absPath += pathToAbsoluteSchemaString(location) + "/" + fullNodeName(location, node);
151 return m_context->find_path(absPath.c_str());
152}
153
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200154libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200155{
156 const auto set = getNodeSet(location, node);
157 if (!set)
158 return nullptr;
Jan Kundrát4030b772018-08-23 15:54:56 +0200159 const auto& schemaSet = set->schema();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200160 if (set->number() != 1)
161 return nullptr;
Jan Kundrát4030b772018-08-23 15:54:56 +0200162 return *schemaSet.begin();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200163}
164
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200165const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200166{
167 std::set<std::string> keys;
168 if (!isList(location, node))
169 return keys;
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200170 libyang::Schema_Node_List list(getSchemaNode(location, node));
Jan Kundrát4030b772018-08-23 15:54:56 +0200171 const auto& keysVec = list.keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200172
Jan Kundrát4030b772018-08-23 15:54:56 +0200173 std::transform(keysVec.begin(), keysVec.end(), std::inserter(keys, keys.begin()),
Václav Kubernát0d4db442018-07-18 17:18:43 +0200174 [] (const auto& it) {return it->name();});
175 return keys;
176}
177
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200178yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200179{
180 using namespace std::string_literals;
181 if (!isLeaf(location, node))
182 throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf");
183
Jan Kundrát4ed0e9f2018-08-23 16:56:58 +0200184 libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200185 switch (leaf.type()->base()) {
186 case LY_TYPE_STRING:
187 return yang::LeafDataTypes::String;
188 case LY_TYPE_DEC64:
189 return yang::LeafDataTypes::Decimal;
190 case LY_TYPE_BOOL:
191 return yang::LeafDataTypes::Bool;
192 case LY_TYPE_INT32:
193 return yang::LeafDataTypes::Int;
194 case LY_TYPE_UINT32:
195 return yang::LeafDataTypes::Uint;
196 case LY_TYPE_ENUM:
197 return yang::LeafDataTypes::Enum;
198 default:
199 throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported");
200 }
201}
202
203std::set<std::string> YangSchema::modules() const
204{
Jan Kundrát4030b772018-08-23 15:54:56 +0200205 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200206
207 std::set<std::string> res;
Jan Kundrát4030b772018-08-23 15:54:56 +0200208 std::transform(modules.begin(), modules.end(),
Václav Kubernát0d4db442018-07-18 17:18:43 +0200209 std::inserter(res, res.end()),
210 [] (const auto module) { return module->name(); });
211 return res;
212}
213
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200214std::set<std::string> YangSchema::childNodes(const schemaPath_& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200215{
216 using namespace std::string_view_literals;
217 std::set<std::string> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200218 std::vector<libyang::S_Schema_Node> nodes;
219
Václav Kubernát0d4db442018-07-18 17:18:43 +0200220 if (path.m_nodes.empty()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200221 nodes = m_context->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200222 } else {
223 const auto absolutePath = "/" + pathToAbsoluteSchemaString(path);
224 const auto set = m_context->find_path(absolutePath.c_str());
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200225 const auto schemaSet = set->schema();
Jan Kundrát4030b772018-08-23 15:54:56 +0200226 for (auto it = (*schemaSet.begin())->child(); it; it = it->next()) {
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200227 nodes.push_back(it);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200228 }
229 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200230
231 for (const auto node : nodes) {
232 if (node->module()->name() == "ietf-yang-library"sv)
Václav Kubernát89728d82018-09-13 16:28:28 +0200233 continue;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200234 if (recursion == Recursion::Recursive) {
235 for (auto it : node->tree_dfs()) {
236 res.insert(it->path(LYS_PATH_FIRST_PREFIX));
237 }
238 } else {
239 res.insert(std::string(node->module()->name()) + ":" + node->name());
240 }
241 }
242
Václav Kubernát0d4db442018-07-18 17:18:43 +0200243 return res;
244}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200245
246void YangSchema::loadModule(const std::string& moduleName)
247{
248 m_context->load_module(moduleName.c_str());
249}
250
251void YangSchema::registerModuleCallback(const std::function<std::string(const char*, const char*, const char*)>& clb)
252{
253 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
254 (void)submod_revision;
255 auto moduleSource = clb(mod_name, mod_revision, submod_name);
256 if (moduleSource.empty()) {
257 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
258 }
259 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
260 };
261
262 auto deleter = [](void* data) {
263 free(data);
264 };
265 m_context->add_missing_module_callback(lambda, deleter);
266}