blob: 7e5bb9fdb6088743433c1a9b254394db8b0687ab [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át0d4db442018-07-18 17:18:43 +020035YangSchema::YangSchema()
Václav Kubernát51d08012020-06-08 15:20:12 +020036 : m_context(std::make_shared<libyang::Context>(nullptr, LY_CTX_DISABLE_SEARCHDIR_CWD))
Václav Kubernát0d4db442018-07-18 17:18:43 +020037{
Václav Kubernát0d4db442018-07-18 17:18:43 +020038}
39
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010040YangSchema::YangSchema(std::shared_ptr<libyang::Context> lyCtx)
41 : m_context(lyCtx)
42{
Václav Kubernát1d50a5b2020-02-03 16:44:22 +010043}
44
Václav Kubernát0d4db442018-07-18 17:18:43 +020045YangSchema::~YangSchema() = default;
46
47void YangSchema::addSchemaString(const char* schema)
48{
49 if (!m_context->parse_module_mem(schema, LYS_IN_YANG)) {
50 throw YangLoadError("Couldn't load schema");
51 }
52}
53
54void YangSchema::addSchemaDirectory(const char* directoryName)
55{
56 if (m_context->set_searchdir(directoryName)) {
57 throw YangLoadError("Couldn't add schema search directory");
58 }
59}
60
61void YangSchema::addSchemaFile(const char* filename)
62{
63 if (!m_context->parse_module_path(filename, LYS_IN_YANG)) {
64 throw YangLoadError("Couldn't load schema");
65 }
66}
67
Václav Kubernát75877de2019-11-20 17:43:02 +010068bool YangSchema::isModule(const std::string& name) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020069{
70 const auto set = modules();
71 return set.find(name) != set.end();
72}
73
Václav Kubernát2db124c2020-05-28 21:58:36 +020074bool YangSchema::listHasKey(const schemaPath_& listPath, const std::string& key) const
75{
76 const auto keys = listKeys(listPath);
77 return keys.find(key) != keys.end();
78}
79
Václav Kubernátc3866792020-02-20 14:12:56 +010080bool YangSchema::leafIsKey(const std::string& leafPath) const
81{
82 auto node = getSchemaNode(leafPath);
Václav Kubernát3a433232020-07-08 17:52:50 +020083 if (!node || node->nodetype() != LYS_LEAF) {
Václav Kubernátc3866792020-02-20 14:12:56 +010084 return false;
Václav Kubernát3a433232020-07-08 17:52:50 +020085 }
Václav Kubernátc3866792020-02-20 14:12:56 +010086
87 return libyang::Schema_Node_Leaf{node}.is_key().get();
88}
89
Václav Kubernát47a3f672019-11-08 15:42:43 +010090libyang::S_Schema_Node YangSchema::impl_getSchemaNode(const std::string& node) const
Václav Kubernát0d4db442018-07-18 17:18:43 +020091{
Václav Kubernátf2b91e02019-04-11 15:36:48 +020092 // If no node is found find_path prints an error message, so we have to
93 // disable logging
94 // https://github.com/CESNET/libyang/issues/753
95 {
96 int oldOptions;
97 auto logBlocker = make_unique_resource(
98 [&oldOptions]() {
99 oldOptions = libyang::set_log_options(0);
100 },
101 [&oldOptions]() {
102 libyang::set_log_options(oldOptions);
103 });
Václav Kubernáte7248b22020-06-26 15:38:59 +0200104 auto res = m_context->get_node(nullptr, node.c_str());
105 if (!res) { // If no node is found, try output rpc nodes too.
106 res = m_context->get_node(nullptr, node.c_str(), 1);
107 }
108 return res;
Václav Kubernátf2b91e02019-04-11 15:36:48 +0200109 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200110}
111
Václav Kubernát47a3f672019-11-08 15:42:43 +0100112
113libyang::S_Schema_Node YangSchema::getSchemaNode(const std::string& node) const
114{
115 return impl_getSchemaNode(node);
116}
117
118libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
119{
Václav Kubernátefcac932020-01-10 15:26:32 +0100120 std::string absPath = joinPaths(pathToSchemaString(location, Prefixes::Always), fullNodeName(location, node));
Václav Kubernát47a3f672019-11-08 15:42:43 +0100121
122 return impl_getSchemaNode(absPath);
123}
124
Václav Kubernát2db124c2020-05-28 21:58:36 +0200125libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& listPath) const
126{
127 std::string absPath = pathToSchemaString(listPath, Prefixes::Always);
128 return impl_getSchemaNode(absPath);
129}
130
Václav Kubernát912b9492020-05-29 02:03:40 +0200131const std::set<std::string> YangSchema::listKeys(const schemaPath_& listPath) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200132{
Václav Kubernát912b9492020-05-29 02:03:40 +0200133 auto node = getSchemaNode(listPath);
134 if (node->nodetype() != LYS_LIST) {
135 return {};
136 }
137
138 auto list = std::make_shared<libyang::Schema_Node_List>(node);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200139 std::set<std::string> keys;
Václav Kubernát2db124c2020-05-28 21:58:36 +0200140 const auto& keysVec = list->keys();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200141
Václav Kubernát90de9502019-11-20 17:19:44 +0100142 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 +0200143 return keys;
144}
145
Václav Kubernát3a99f002020-03-31 02:27:41 +0200146namespace {
Václav Kubernát2984f442020-02-20 17:43:35 +0100147std::set<enum_> enumValues(const libyang::S_Type& typeArg)
Václav Kubernát0d4db442018-07-18 17:18:43 +0200148{
Václav Kubernát2984f442020-02-20 17:43:35 +0100149 auto type = typeArg;
Václav Kubernát3a99f002020-03-31 02:27:41 +0200150 auto enm = type->info()->enums()->enm();
151 // The enum can be a derived type and enm() only returns values,
152 // if that specific typedef changed the possible values. So we go
153 // up the hierarchy until we find a typedef that defined these values.
154 while (enm.empty()) {
155 type = type->der()->type();
156 enm = type->info()->enums()->enm();
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200157 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200158
159 std::vector<libyang::S_Type_Enum> enabled;
160 std::copy_if(enm.begin(), enm.end(), std::back_inserter(enabled), [](const libyang::S_Type_Enum& it) {
161 auto iffeatures = it->iffeature();
162 return std::all_of(iffeatures.begin(), iffeatures.end(), [](auto it) { return it->value(); });
163 });
164
165 std::set<enum_> enumSet;
166 std::transform(enabled.begin(), enabled.end(), std::inserter(enumSet, enumSet.end()), [](auto it) { return enum_{it->name()}; });
167 return enumSet;
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200168}
169
Václav Kubernát2984f442020-02-20 17:43:35 +0100170std::set<identityRef_> validIdentities(const libyang::S_Type& type)
Václav Kubernát3a99f002020-03-31 02:27:41 +0200171{
172 std::set<identityRef_> identSet;
173
174 // auto topLevelModule = leaf->module();
175
Václav Kubernát2984f442020-02-20 17:43:35 +0100176 auto info = type->info();
Václav Kubernát3a99f002020-03-31 02:27:41 +0200177 for (auto base : info->ident()->ref()) { // Iterate over all bases
178 identSet.emplace(base->module()->name(), base->name());
179 // Iterate over derived identities (this is recursive!)
180 for (auto derived : base->der()->schema()) {
181 identSet.emplace(derived->module()->name(), derived->name());
182 }
183 }
184
185 return identSet;
186}
187
Václav Kubernát2984f442020-02-20 17:43:35 +0100188std::string leafrefPath(const libyang::S_Type& type)
Václav Kubernát3a99f002020-03-31 02:27:41 +0200189{
Václav Kubernát2984f442020-02-20 17:43:35 +0100190 return type->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
Václav Kubernát3a99f002020-03-31 02:27:41 +0200191}
192}
193
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200194template <typename NodeType>
Václav Kubernát13b23d72020-04-16 21:49:51 +0200195yang::TypeInfo YangSchema::impl_leafType(const libyang::S_Schema_Node& node) const
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200196{
197 using namespace std::string_literals;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200198 auto leaf = std::make_shared<NodeType>(node);
Václav Kubernát13b23d72020-04-16 21:49:51 +0200199 auto leafUnits = leaf->units();
200 std::function<yang::TypeInfo(std::shared_ptr<libyang::Type>)> resolveType;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100201 resolveType = [this, &resolveType, leaf, leafUnits](std::shared_ptr<libyang::Type> type) -> yang::TypeInfo {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200202 yang::LeafDataType resType;
Václav Kubernát2984f442020-02-20 17:43:35 +0100203 switch (type->base()) {
204 case LY_TYPE_STRING:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200205 resType.emplace<yang::String>();
206 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100207 case LY_TYPE_DEC64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200208 resType.emplace<yang::Decimal>();
209 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100210 case LY_TYPE_BOOL:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200211 resType.emplace<yang::Bool>();
212 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100213 case LY_TYPE_INT8:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200214 resType.emplace<yang::Int8>();
215 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100216 case LY_TYPE_INT16:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200217 resType.emplace<yang::Int16>();
218 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100219 case LY_TYPE_INT32:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200220 resType.emplace<yang::Int32>();
221 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100222 case LY_TYPE_INT64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200223 resType.emplace<yang::Int64>();
224 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100225 case LY_TYPE_UINT8:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200226 resType.emplace<yang::Uint8>();
227 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100228 case LY_TYPE_UINT16:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200229 resType.emplace<yang::Uint16>();
230 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100231 case LY_TYPE_UINT32:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200232 resType.emplace<yang::Uint32>();
233 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100234 case LY_TYPE_UINT64:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200235 resType.emplace<yang::Uint64>();
236 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100237 case LY_TYPE_BINARY:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200238 resType.emplace<yang::Binary>();
239 break;
Jan Kundrát379bb572020-05-07 03:23:13 +0200240 case LY_TYPE_EMPTY:
241 resType.emplace<yang::Empty>();
242 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100243 case LY_TYPE_ENUM:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200244 resType.emplace<yang::Enum>(enumValues(type));
245 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100246 case LY_TYPE_IDENT:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200247 resType.emplace<yang::IdentityRef>(validIdentities(type));
248 break;
Václav Kubernát2984f442020-02-20 17:43:35 +0100249 case LY_TYPE_LEAFREF:
Václav Kubernát13b23d72020-04-16 21:49:51 +0200250 resType.emplace<yang::LeafRef>(::leafrefPath(type), std::make_unique<yang::TypeInfo>(leafType(::leafrefPath(type))));
251 break;
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100252 case LY_TYPE_BITS: {
253 auto resBits = yang::Bits{};
254 for (const auto& bit : type->info()->bits()->bit()) {
255 resBits.m_allowedValues.emplace(bit->name());
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100256 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100257 resType.emplace<yang::Bits>(std::move(resBits));
258 break;
259 }
260 case LY_TYPE_UNION: {
261 auto resUnion = yang::Union{};
262 for (auto unionType : type->info()->uni()->types()) {
263 resUnion.m_unionTypes.emplace_back(resolveType(unionType));
Václav Kubernát2984f442020-02-20 17:43:35 +0100264 }
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100265 resType.emplace<yang::Union>(std::move(resUnion));
266 break;
267 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100268 default:
269 using namespace std::string_literals;
270 throw UnsupportedYangTypeException("the type of "s + leaf->name() + " is not supported: " + std::to_string(leaf->type()->base()));
271 }
Václav Kubernát13b23d72020-04-16 21:49:51 +0200272
273 std::optional<std::string> resUnits;
274
275 if (leafUnits) {
276 resUnits = leafUnits;
277 } else {
278 for (auto parentTypedef = type->der(); parentTypedef; parentTypedef = parentTypedef->type()->der()) {
279 auto units = parentTypedef->units();
280 if (units) {
281 resUnits = units;
282 break;
283 }
284 }
285 }
286
Václav Kubernát1ae24f42020-12-01 02:32:04 +0100287 std::optional<std::string> resDescription;
288
289 // checking for parentTypedef->type()->der() means I'm going to enter inside base types like "string". These
290 // also have a description, but it isn't too helpful ("human-readable string")
291 for (auto parentTypedef = type->der(); parentTypedef && parentTypedef->type()->der(); parentTypedef = parentTypedef->type()->der()) {
292 auto dsc = parentTypedef->dsc();
293 if (dsc) {
294 resDescription = dsc;
295 break;
296 }
297 }
298
299 return yang::TypeInfo(resType, resUnits, resDescription);
Václav Kubernát13b23d72020-04-16 21:49:51 +0200300 };
Václav Kubernát2984f442020-02-20 17:43:35 +0100301 return resolveType(leaf->type());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200302}
303
Václav Kubernát13b23d72020-04-16 21:49:51 +0200304yang::TypeInfo YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100305{
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200306 auto lyNode = getSchemaNode(location, node);
307 switch (lyNode->nodetype()) {
308 case LYS_LEAF:
309 return impl_leafType<libyang::Schema_Node_Leaf>(lyNode);
310 case LYS_LEAFLIST:
311 return impl_leafType<libyang::Schema_Node_Leaflist>(lyNode);
312 default:
313 throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist");
314 }
Václav Kubernát9bf36852020-02-18 17:47:56 +0100315}
316
Václav Kubernát13b23d72020-04-16 21:49:51 +0200317yang::TypeInfo YangSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100318{
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200319 auto lyNode = getSchemaNode(path);
320 switch (lyNode->nodetype()) {
321 case LYS_LEAF:
322 return impl_leafType<libyang::Schema_Node_Leaf>(lyNode);
323 case LYS_LEAFLIST:
324 return impl_leafType<libyang::Schema_Node_Leaflist>(lyNode);
325 default:
326 throw std::logic_error("YangSchema::leafType: type must be leaf or leaflist");
327 }
Václav Kubernát9bf36852020-02-18 17:47:56 +0100328}
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200329
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100330std::optional<std::string> YangSchema::leafTypeName(const std::string& path) const
331{
332 libyang::Schema_Node_Leaf leaf(getSchemaNode(path));
Václav Kubernát76ba4ec2020-05-18 13:26:56 +0200333 return leaf.type()->der().get() && leaf.type()->der()->type()->der().get() ? std::optional{leaf.type()->der()->name()} : std::nullopt;
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100334}
335
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100336std::string YangSchema::leafrefPath(const std::string& leafrefPath) const
337{
338 using namespace std::string_literals;
339 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafrefPath));
340 return leaf.type()->info()->lref()->target()->path(LYS_PATH_FIRST_PREFIX);
341}
Václav Kubernát0d4db442018-07-18 17:18:43 +0200342
343std::set<std::string> YangSchema::modules() const
344{
Jan Kundrát4030b772018-08-23 15:54:56 +0200345 const auto& modules = m_context->get_module_iter();
Václav Kubernát0d4db442018-07-18 17:18:43 +0200346
347 std::set<std::string> res;
Václav Kubernát90de9502019-11-20 17:19:44 +0100348 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 +0200349 return res;
350}
351
Václav Kubernát95b08872020-04-28 01:04:17 +0200352std::set<ModuleNodePair> YangSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát0d4db442018-07-18 17:18:43 +0200353{
354 using namespace std::string_view_literals;
Václav Kubernát95b08872020-04-28 01:04:17 +0200355 std::set<ModuleNodePair> res;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200356 std::vector<libyang::S_Schema_Node> nodes;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200357 std::string topLevelModule;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200358
Václav Kubernát3a823f42020-04-29 23:40:21 +0200359 if (path.type() == typeid(module_)) {
360 nodes = m_context->get_module(boost::get<module_>(path).m_name.c_str())->data_instantiables(0);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200361 } else {
Václav Kubernát3a823f42020-04-29 23:40:21 +0200362 auto schemaPath = anyPathToSchemaPath(path);
363 if (schemaPath.m_nodes.empty()) {
364 nodes = m_context->data_instantiables(0);
365 } else {
366 const auto pathString = pathToSchemaString(schemaPath, Prefixes::Always);
367 const auto node = getSchemaNode(pathString);
368 nodes = node->child_instantiables(0);
369 topLevelModule = schemaPath.m_nodes.begin()->m_prefix->m_name;
370 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200371 }
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200372
Václav Kubernátef085742020-04-21 09:28:44 +0200373 for (const auto& node : nodes) {
Václav Kubernát3a433232020-07-08 17:52:50 +0200374 if (node->module()->name() == "ietf-yang-library"sv) {
Václav Kubernát89728d82018-09-13 16:28:28 +0200375 continue;
Václav Kubernát3a433232020-07-08 17:52:50 +0200376 }
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200377
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200378 if (recursion == Recursion::Recursive) {
379 for (auto it : node->tree_dfs()) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200380 res.insert(ModuleNodePair(boost::none, it->path(LYS_PATH_FIRST_PREFIX)));
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200381 }
382 } else {
Václav Kubernát95b08872020-04-28 01:04:17 +0200383 ModuleNodePair toInsert;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200384 if (topLevelModule.empty() || topLevelModule != node->module()->name()) {
Václav Kubernát82d74632020-05-11 15:59:53 +0200385 toInsert.first = node->module()->type() == 0 ? node->module()->name() : libyang::Submodule(node->module()).belongsto()->name();
Václav Kubernát4f77a252019-02-19 16:51:30 +0100386 }
Václav Kubernát95b08872020-04-28 01:04:17 +0200387 toInsert.second = node->name();
Václav Kubernát4f77a252019-02-19 16:51:30 +0100388 res.insert(toInsert);
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200389 }
390 }
391
Václav Kubernát0d4db442018-07-18 17:18:43 +0200392 return res;
393}
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200394
395void YangSchema::loadModule(const std::string& moduleName)
396{
397 m_context->load_module(moduleName.c_str());
398}
399
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100400void YangSchema::enableFeature(const std::string& moduleName, const std::string& featureName)
401{
Václav Kubernát7aaf6dc2020-06-26 18:03:57 +0200402 using namespace std::string_literals;
Václav Kubernátf44bdda2020-06-22 15:58:41 +0200403 auto module = getYangModule(moduleName);
404 if (!module) {
Václav Kubernátf44bdda2020-06-22 15:58:41 +0200405 throw std::runtime_error("Module \""s + moduleName + "\" doesn't exist.");
406 }
Václav Kubernát7aaf6dc2020-06-26 18:03:57 +0200407 if (module->feature_enable(featureName.c_str())) {
408 throw std::runtime_error("Can't enable feature \""s + featureName + "\" for module \"" + moduleName + "\".");
409 }
Václav Kubernátbf9c6112019-11-04 16:03:35 +0100410}
411
Václav Kubernátb52dc252019-12-04 13:03:39 +0100412void 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 +0200413{
414 auto lambda = [clb](const char* mod_name, const char* mod_revision, const char* submod_name, const char* submod_revision) {
415 (void)submod_revision;
Václav Kubernátb52dc252019-12-04 13:03:39 +0100416 auto moduleSource = clb(mod_name, mod_revision, submod_name, submod_revision);
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200417 if (moduleSource.empty()) {
418 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, nullptr};
419 }
420 return libyang::Context::mod_missing_cb_return{LYS_IN_YANG, strdup(moduleSource.c_str())};
421 };
422
423 auto deleter = [](void* data) {
Václav Kubernáte3d282a2020-07-09 10:32:12 +0200424 free(data); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
Václav Kubernáta6c5fff2018-09-07 15:16:25 +0200425 };
426 m_context->add_missing_module_callback(lambda, deleter);
427}
Václav Kubernátc31bd602019-03-07 11:44:48 +0100428
429std::shared_ptr<libyang::Data_Node> YangSchema::dataNodeFromPath(const std::string& path, const std::optional<const std::string> value) const
430{
431 return std::make_shared<libyang::Data_Node>(m_context,
432 path.c_str(),
433 value ? value.value().c_str() : nullptr,
434 LYD_ANYDATA_CONSTSTRING,
Václav Kubernátab612e92019-11-26 19:51:31 +0100435 LYD_PATH_OPT_EDIT);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100436}
437
438std::shared_ptr<libyang::Module> YangSchema::getYangModule(const std::string& name)
439{
Václav Kubernátf44bdda2020-06-22 15:58:41 +0200440 return m_context->get_module(name.c_str());
Václav Kubernátc31bd602019-03-07 11:44:48 +0100441}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100442
443namespace {
444yang::NodeTypes impl_nodeType(const libyang::S_Schema_Node& node)
445{
446 if (!node) {
447 throw InvalidNodeException();
448 }
449 switch (node->nodetype()) {
450 case LYS_CONTAINER:
451 return libyang::Schema_Node_Container{node}.presence() ? yang::NodeTypes::PresenceContainer : yang::NodeTypes::Container;
452 case LYS_LEAF:
453 return yang::NodeTypes::Leaf;
454 case LYS_LIST:
455 return yang::NodeTypes::List;
Václav Kubernátaaafeae2020-05-05 15:41:45 +0200456 case LYS_RPC:
457 return yang::NodeTypes::Rpc;
458 case LYS_ACTION:
459 return yang::NodeTypes::Action;
460 case LYS_NOTIF:
461 return yang::NodeTypes::Notification;
462 case LYS_ANYXML:
463 return yang::NodeTypes::AnyXml;
464 case LYS_LEAFLIST:
465 return yang::NodeTypes::LeafList;
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100466 default:
Václav Kubernát2a141392020-02-18 17:12:32 +0100467 throw InvalidNodeException(); // FIXME: Implement all types.
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100468 }
469}
470}
471
472yang::NodeTypes YangSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
473{
474 return impl_nodeType(getSchemaNode(location, node));
475}
476
477yang::NodeTypes YangSchema::nodeType(const std::string& path) const
478{
479 return impl_nodeType(getSchemaNode(path));
480}
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100481
482std::optional<std::string> YangSchema::description(const std::string& path) const
483{
484 auto node = getSchemaNode(path.c_str());
485 return node->dsc() ? std::optional{node->dsc()} : std::nullopt;
486}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200487
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200488yang::Status YangSchema::status(const std::string& location) const
489{
490 auto node = getSchemaNode(location.c_str());
491 if (node->flags() & LYS_STATUS_DEPRC) {
492 return yang::Status::Deprecated;
493 } else if (node->flags() & LYS_STATUS_OBSLT) {
494 return yang::Status::Obsolete;
495 } else {
496 return yang::Status::Current;
497 }
498}
499
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200500bool YangSchema::isConfig(const std::string& path) const
501{
Václav Kubernáte7248b22020-06-26 15:38:59 +0200502 auto node = getSchemaNode(path.c_str());
503 if (node->flags() & LYS_CONFIG_W) {
504 return true;
505 }
506
507 // Node can still be an input node.
508 while (node->parent()) {
509 node = node->parent();
510 if (node->nodetype() == LYS_INPUT) {
511 return true;
512 }
513 }
514
515 return false;
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200516}
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200517
518std::optional<std::string> YangSchema::defaultValue(const std::string& leafPath) const
519{
520 libyang::Schema_Node_Leaf leaf(getSchemaNode(leafPath));
521
522 if (auto leafDefault = leaf.dflt()) {
523 return leafDefault;
524 }
525
526 for (auto type = leaf.type()->der(); type != nullptr; type = type->type()->der()) {
527 if (auto defaultValue = type->dflt()) {
528 return defaultValue;
529 }
530 }
531
532 return std::nullopt;
533}
Václav Kubernáta8789602020-07-20 15:18:19 +0200534
535std::string YangSchema::dataPathToSchemaPath(const std::string& path)
536{
537 return getSchemaNode(path)->path(LYS_PATH_FIRST_PREFIX);
538}