blob: ea6c7ceef302ffc20472537e024ec550b3cb7646 [file] [log] [blame]
Radek Krejcida04f4a2015-05-21 12:54:09 +02001/**
Michal Vasko2d162e12015-09-24 14:33:29 +02002 * @file tree_schema.c
Radek Krejcida04f4a2015-05-21 12:54:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko2d162e12015-09-24 14:33:29 +02004 * @brief Manipulation with libyang schema data structures
Radek Krejcida04f4a2015-05-21 12:54:09 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
Radek Krejci54f6fb32016-02-24 12:56:39 +010014
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020015#define _GNU_SOURCE
Radek Krejcida04f4a2015-05-21 12:54:09 +020016
Radek Krejci812b10a2015-05-28 16:48:25 +020017#include <assert.h>
Radek Krejci5a988152015-07-15 11:16:26 +020018#include <ctype.h>
Radek Krejcib051f722016-02-25 15:12:21 +010019#include <limits.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020020#include <stdlib.h>
21#include <sys/mman.h>
Michal Vasko662610a2015-12-07 11:25:45 +010022#include <sys/types.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020023#include <sys/stat.h>
Michal Vasko662610a2015-12-07 11:25:45 +010024#include <fcntl.h>
Radek Krejci8bc9ca02015-06-04 15:52:46 +020025#include <string.h>
Michal Vasko662610a2015-12-07 11:25:45 +010026#include <unistd.h>
27#include <errno.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020028
29#include "common.h"
30#include "context.h"
Radek Krejci3045cf32015-05-28 10:58:52 +020031#include "parser.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020032#include "resolve.h"
Michal Vasko88c29542015-11-27 14:57:53 +010033#include "xml.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020034#include "xml_internal.h"
Radek Krejci8bc9ca02015-06-04 15:52:46 +020035#include "tree_internal.h"
Radek Krejcieab784a2015-08-27 09:56:53 +020036#include "validation.h"
Pavol Vicanf7cc2852016-03-22 23:27:35 +010037#include "parser_yang.h"
Radek Krejciefaeba32015-05-27 14:30:57 +020038
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020039static int
40lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
41 struct unres_schema *unres);
42
Michal Vasko1e62a092015-12-01 12:27:20 +010043API const struct lys_feature *
44lys_is_disabled(const struct lys_node *node, int recursive)
Radek Krejci48061fb2015-08-05 15:41:07 +020045{
46 int i;
47
48check:
49 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
50 /* input/output does not have if-feature, so skip them */
51
52 /* check local if-features */
53 for (i = 0; i < node->features_size; i++) {
54 if (!(node->features[i]->flags & LYS_FENABLED)) {
55 return node->features[i];
56 }
57 }
58 }
59
60 if (!recursive) {
61 return NULL;
62 }
63
64 /* go through parents */
65 if (node->nodetype == LYS_AUGMENT) {
66 /* go to parent actually means go to the target node */
67 node = ((struct lys_node_augment *)node)->target;
Radek Krejci48061fb2015-08-05 15:41:07 +020068 } else if (node->parent) {
69 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +020070 } else {
71 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +020072 }
73
Radek Krejci074bf852015-08-19 14:22:16 +020074 if (recursive == 2) {
75 /* continue only if the node cannot have a data instance */
76 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
77 return NULL;
78 }
79 }
80 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +020081}
82
Michal Vasko1dca6882015-10-22 14:29:42 +020083int
Michal Vasko36cbaa42015-12-14 13:15:48 +010084lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name,
85 int nam_len, LYS_NODE type, const struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +020086{
Radek Krejcic071c542016-01-27 14:57:51 +010087 const struct lys_node *node, *parent = NULL;
88 const struct lys_module *mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +010089 const char *node_mod_name;
Michal Vasko1dca6882015-10-22 14:29:42 +020090
Michal Vasko36cbaa42015-12-14 13:15:48 +010091 assert(siblings && mod_name && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +020092 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +020093
Michal Vasko36cbaa42015-12-14 13:15:48 +010094 /* fill the lengths in case the caller is so indifferent */
95 if (!mod_name_len) {
96 mod_name_len = strlen(mod_name);
97 }
Michal Vasko1dca6882015-10-22 14:29:42 +020098 if (!nam_len) {
99 nam_len = strlen(name);
100 }
101
Michal Vasko36cbaa42015-12-14 13:15:48 +0100102 /* set mod correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100103 parent = lys_parent(siblings);
104 if (!parent) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100105 mod = lys_node_module(siblings);
Michal Vasko1dca6882015-10-22 14:29:42 +0200106 }
107
Radek Krejcic071c542016-01-27 14:57:51 +0100108 /* try to find the node */
109 node = NULL;
110 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
111 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100112 /* module name comparison */
113 node_mod_name = lys_node_module(node)->name;
114 if ((node_mod_name != mod_name) && (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100115 continue;
116 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200117
Radek Krejcic071c542016-01-27 14:57:51 +0100118 /* direct name check */
119 if ((node->name == name) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
120 if (ret) {
121 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200122 }
Radek Krejcic071c542016-01-27 14:57:51 +0100123 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200124 }
125 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200126 }
127
128 return EXIT_FAILURE;
129}
130
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200131int
Michal Vasko1e62a092015-12-01 12:27:20 +0100132lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type,
133 const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200134{
Michal Vasko1e62a092015-12-01 12:27:20 +0100135 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200136
137 assert(siblings && name);
138 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
139
140 /* find the beginning */
141 while (siblings->prev->next) {
142 siblings = siblings->prev;
143 }
144
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200145 if (!mod) {
146 mod = siblings->module;
147 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200148
Michal Vasko4f0dad02016-02-15 14:08:23 +0100149 /* try to find the node */
150 node = NULL;
151 while ((node = lys_getnext(node, siblings->parent, mod, 0))) {
152 if (!type || (node->nodetype & type)) {
153 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200154 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100155 continue;
156 }
157
Michal Vasko4f0dad02016-02-15 14:08:23 +0100158 /* direct name check */
Radek Krejci749190d2016-02-18 16:26:25 +0100159 if (ly_strequal(node->name, name, 0)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100160 if (ret) {
161 *ret = node;
162 }
163 return EXIT_SUCCESS;
164 }
Radek Krejcic071c542016-01-27 14:57:51 +0100165 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200166 }
167
168 return EXIT_FAILURE;
169}
170
Michal Vasko1e62a092015-12-01 12:27:20 +0100171API const struct lys_node *
172lys_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module, int options)
Radek Krejci7f40ce32015-08-12 20:38:46 +0200173{
Michal Vasko1e62a092015-12-01 12:27:20 +0100174 const struct lys_node *next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200175
Radek Krejci8bc87f62015-09-02 16:19:05 +0200176 if (!last) {
177 /* first call */
178
179 /* get know where to start */
180 if (parent) {
181 /* schema subtree */
182 next = last = parent->child;
183 } else {
184 /* top level data */
185 assert(module);
186 next = last = module->data;
187 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200188 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200189 /* continue after the last returned value */
190 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200191 }
192
193repeat:
Michal Vasko7c386e72015-10-07 15:13:33 +0200194 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200195 if (options & LYS_GETNEXT_WITHGROUPING) {
196 return next;
197 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200198 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200199 }
200
Radek Krejcic4ccbd92016-01-07 13:13:33 +0100201 if (!next) {
Radek Krejci63318e82016-02-12 09:43:12 +0100202 if (!last || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200203 /* no next element */
204 return NULL;
205 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200206 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200207 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200208 goto repeat;
209 }
210
211 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200212 case LYS_INPUT:
213 case LYS_OUTPUT:
214 if (options & LYS_GETNEXT_WITHINOUT) {
215 return next;
216 } else {
217 next = next->child;
218 goto repeat;
219 }
220 break;
221
Michal Vaskoa5835e92015-10-20 15:07:39 +0200222 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200223 if (options & LYS_GETNEXT_WITHCASE) {
224 return next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200225 } else {
226 next = next->child;
227 goto repeat;
Michal Vasko1dca6882015-10-22 14:29:42 +0200228 }
Michal Vaskob6eedf02015-10-22 16:07:03 +0200229 break;
230
Michal Vasko1dca6882015-10-22 14:29:42 +0200231 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200232 /* go into */
233 next = next->child;
234 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200235
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200236 case LYS_RPC:
237 case LYS_NOTIF:
Radek Krejci8bc87f62015-09-02 16:19:05 +0200238 case LYS_CONTAINER:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200239 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200240 case LYS_ANYXML:
Radek Krejci14a11a62015-08-17 17:27:38 +0200241 case LYS_LIST:
242 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200243 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200244
245 case LYS_CHOICE:
246 if (options & LYS_GETNEXT_WITHCHOICE) {
247 return next;
248 } else {
249 /* go into */
250 next = next->child;
251 goto repeat;
252 }
253 break;
254
Radek Krejci7f40ce32015-08-12 20:38:46 +0200255 default:
256 /* we should not be here */
257 return NULL;
258 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200259
260
261}
262
Michal Vasko1e62a092015-12-01 12:27:20 +0100263static const struct lys_node *
Radek Krejci2342cf62016-01-29 16:48:23 +0100264check_mand_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module)
Radek Krejci8bc87f62015-09-02 16:19:05 +0200265{
Michal Vasko1e62a092015-12-01 12:27:20 +0100266 const struct lys_node *next;
267
Radek Krejci2342cf62016-01-29 16:48:23 +0100268 next = lys_getnext(last, parent, module, LYS_GETNEXT_WITHCHOICE);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200269
Radek Krejci4b6c2112015-10-06 12:48:34 +0200270repeat:
Radek Krejci8bc87f62015-09-02 16:19:05 +0200271 if (next && next->nodetype == LYS_CONTAINER) {
272 if (((struct lys_node_container *)next)->presence) {
273 /* mandatory elements under the non-existing presence
274 * container are not mandatory - 7.6.5, rule 1 */
275 next = next->next;
276 } else {
277 /* go into */
278 next = next->child;
279 }
280 goto repeat;
281 }
282
283 return next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200284}
285
Michal Vasko84f821a2016-04-11 11:03:42 +0200286static int
Michal Vasko1e62a092015-12-01 12:27:20 +0100287check_mand_check(const struct lys_node *node, const struct lys_node *stop, const struct lyd_node *data)
Radek Krejci7f40ce32015-08-12 20:38:46 +0200288{
Radek Krejci7531aa22016-04-12 13:52:19 +0200289 const struct lys_node *siter = NULL, *missing_parent = NULL;
Radek Krejci96e20852016-04-11 17:51:26 +0200290 struct lys_node *parent = NULL;
Radek Krejci7531aa22016-04-12 13:52:19 +0200291 const struct lyd_node *diter = NULL;
Radek Krejcidc154432016-01-21 11:10:59 +0100292 struct ly_set *set = NULL;
Michal Vaskof610fd42016-04-19 10:38:20 +0200293 unsigned int i, toplevel = (stop && stop->nodetype != LYS_OUTPUT) ? 0 : 1;
Michal Vasko84f821a2016-04-11 11:03:42 +0200294 uint32_t minmax, min, max;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200295
Radek Krejci7531aa22016-04-12 13:52:19 +0200296 if (data) {
297 /* go to the correct data level */
298 for (parent = lys_parent(node); parent && parent != stop; parent = lys_parent(parent)) {
299 /* 7.6.5, rule 1 (presence container), checking presence
300 * is not needed since it is done in check_mand_getnext()
301 */
302
303 if (parent->nodetype != LYS_CONTAINER) {
304 /* not interested in LYS_USES, LYS_CASE or LYS_CHOICE,
305 * because they are not instantiated in data tree */
306 continue;
307 }
308 /* add the parent to the list for searching in data tree */
309 if (!set) {
310 set = ly_set_new();
311 }
312 /* ignore return - memory error is logged and we will
313 * check at least the rest of nodes we have */
314 (void)ly_set_add(set, parent);
315 }
316 if (set) {
317 for (i = set->number; i > 0; ) {
318 i--;
319 LY_TREE_FOR(toplevel ? data : data->child, diter) {
320 if (diter->schema == set->set.s[i]) {
321 break;
322 }
323 }
324 if (!diter) {
325 /* instance not found */
326 missing_parent = set->set.s[i];
Radek Krejci29ac4f92016-04-12 15:05:53 +0200327 if (toplevel) {
328 data = NULL;
329 }
Radek Krejci7531aa22016-04-12 13:52:19 +0200330 break;
331 }
332 data = diter;
333 toplevel = 0;
334 if (data->validity == LYD_VAL_OK) {
335 /* already checked */
336 ly_set_free(set);
337 return EXIT_SUCCESS;
338 }
339 }
340 ly_set_free(set);
341 }
342 } else {
343 missing_parent = node;
344 }
345
Radek Krejci7f40ce32015-08-12 20:38:46 +0200346 if (node->flags & LYS_MAND_TRUE) {
Radek Krejci7531aa22016-04-12 13:52:19 +0200347 if (missing_parent) {
Radek Krejci29ac4f92016-04-12 15:05:53 +0200348 LOGVAL(LYE_MISSELEM, LY_VLOG_LYD, data, node->name,
349 (lys_parent(node) ? lys_parent(node)->name : lys_node_module(node)->name));
Michal Vasko84f821a2016-04-11 11:03:42 +0200350 return EXIT_FAILURE;
Radek Krejci2342cf62016-01-29 16:48:23 +0100351 }
Radek Krejci7531aa22016-04-12 13:52:19 +0200352
Radek Krejci2342cf62016-01-29 16:48:23 +0100353 switch (node->nodetype) {
354 case LYS_LEAF:
355 case LYS_ANYXML:
356 case LYS_CHOICE:
Michal Vasko84f821a2016-04-11 11:03:42 +0200357 if (lys_parent(node) && lys_parent(node)->nodetype == LYS_CASE) {
Radek Krejci2342cf62016-01-29 16:48:23 +0100358 /* 7.6.5, rule 2 */
359 /* 7.9.4, rule 1 */
Radek Krejci7531aa22016-04-12 13:52:19 +0200360
361 /* try to find the node's siblings in data */
362 LY_TREE_FOR(toplevel ? data : data->child, diter) {
363 LY_TREE_FOR(lys_parent(node)->child, siter) {
364 if (siter == diter->schema) {
365 /* some sibling exists, rule applies */
Radek Krejci2342cf62016-01-29 16:48:23 +0100366 break;
367 }
368 }
Radek Krejci7531aa22016-04-12 13:52:19 +0200369 if (siter) {
370 break;
371 }
Radek Krejci2342cf62016-01-29 16:48:23 +0100372 }
373 if (!siter) {
374 /* no sibling exists */
Michal Vasko84f821a2016-04-11 11:03:42 +0200375 return EXIT_SUCCESS;
Radek Krejci2342cf62016-01-29 16:48:23 +0100376 }
Radek Krejci2342cf62016-01-29 16:48:23 +0100377 }
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200378
Radek Krejci96e20852016-04-11 17:51:26 +0200379 if (node->nodetype == LYS_CHOICE) {
380 siter = NULL;
Radek Krejci7531aa22016-04-12 13:52:19 +0200381 LY_TREE_FOR(toplevel ? data : data->child, diter) {
Radek Krejci96e20852016-04-11 17:51:26 +0200382 while ((siter = lys_getnext(siter, node, NULL, 0))) {
383 if (diter->schema == siter) {
384 return EXIT_SUCCESS;
385 }
386 }
387 }
388 } else {
Radek Krejci7531aa22016-04-12 13:52:19 +0200389 LY_TREE_FOR(toplevel ? data : data->child, diter) {
Radek Krejci96e20852016-04-11 17:51:26 +0200390 if (diter->schema == node) {
391 return EXIT_SUCCESS;
392 }
Radek Krejci2342cf62016-01-29 16:48:23 +0100393 }
394 }
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200395
Radek Krejci2342cf62016-01-29 16:48:23 +0100396 /* instance not found */
397 /* 7.6.5, rule 3 (or 2) */
398 /* 7.9.4, rule 2 */
Michal Vasko84f821a2016-04-11 11:03:42 +0200399 if (node->nodetype == LYS_CHOICE) {
Radek Krejci29ac4f92016-04-12 15:05:53 +0200400 LOGVAL(LYE_NOMANDCHOICE, LY_VLOG_LYD, toplevel ? NULL : data, node->name);
Michal Vasko84f821a2016-04-11 11:03:42 +0200401 } else {
Radek Krejci29ac4f92016-04-12 15:05:53 +0200402 LOGVAL(LYE_MISSELEM, LY_VLOG_LYD, toplevel ? NULL : data, node->name,
Michal Vasko84f821a2016-04-11 11:03:42 +0200403 (lys_parent(node) ? lys_parent(node)->name : lys_node_module(node)->name));
404 }
Radek Krejci7531aa22016-04-12 13:52:19 +0200405 break;
Radek Krejci2342cf62016-01-29 16:48:23 +0100406 default:
407 /* error */
Radek Krejci7531aa22016-04-12 13:52:19 +0200408 LOGINT;
Radek Krejci2342cf62016-01-29 16:48:23 +0100409 break;
410 }
Radek Krejci7531aa22016-04-12 13:52:19 +0200411 return EXIT_FAILURE;
412
Radek Krejci14a11a62015-08-17 17:27:38 +0200413 } else if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
414 /* search for number of instances */
415 minmax = 0;
Radek Krejci7531aa22016-04-12 13:52:19 +0200416 if (!missing_parent) {
417 LY_TREE_FOR(toplevel ? data : data->child, diter) {
Radek Krejci2342cf62016-01-29 16:48:23 +0100418 if (diter->schema == node) {
419 minmax++;
Michal Vasko05ed8422016-04-12 11:38:24 +0200420 /* remember the last instance, we will use it in the log message */
421 data = diter;
Radek Krejci2342cf62016-01-29 16:48:23 +0100422 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200423 }
424 }
425
426 /* check the specified constraints */
427 if (node->nodetype == LYS_LIST) {
Michal Vasko84f821a2016-04-11 11:03:42 +0200428 min = ((struct lys_node_list *)node)->min;
429 max = ((struct lys_node_list *)node)->max;
430 } else {
431 min = ((struct lys_node_leaflist *)node)->min;
432 max = ((struct lys_node_leaflist *)node)->max;
433 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200434
Michal Vasko84f821a2016-04-11 11:03:42 +0200435 if (min && (minmax < min)) {
Radek Krejci29ac4f92016-04-12 15:05:53 +0200436 LOGVAL(LYE_NOMIN, LY_VLOG_LYD, toplevel ? NULL : data, node->name);
Michal Vasko84f821a2016-04-11 11:03:42 +0200437 return EXIT_FAILURE;
438 }
439 if (max && (minmax > max)) {
Radek Krejci29ac4f92016-04-12 15:05:53 +0200440 LOGVAL(LYE_NOMAX, LY_VLOG_LYD, toplevel ? NULL : data, node->name);
Michal Vasko84f821a2016-04-11 11:03:42 +0200441 return EXIT_FAILURE;
Radek Krejci14a11a62015-08-17 17:27:38 +0200442 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200443 }
444
Michal Vasko84f821a2016-04-11 11:03:42 +0200445 return EXIT_SUCCESS;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200446}
447
Michal Vasko84f821a2016-04-11 11:03:42 +0200448int
Radek Krejci763122e2016-04-05 16:35:33 +0200449ly_check_mandatory(const struct lyd_node *data, const struct lys_node *schema, int status)
Radek Krejci7f40ce32015-08-12 20:38:46 +0200450{
Michal Vasko84f821a2016-04-11 11:03:42 +0200451 const struct lys_node *siter, *saux, *saux2, *parent = NULL, *parent2;
Michal Vasko1e62a092015-12-01 12:27:20 +0100452 const struct lyd_node *diter;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200453 int found;
454
Radek Krejci2342cf62016-01-29 16:48:23 +0100455 assert(data || schema);
456
Radek Krejci7531aa22016-04-12 13:52:19 +0200457 if (schema) {
458 /* schema is preferred regardless the data */
Radek Krejci2342cf62016-01-29 16:48:23 +0100459 siter = schema;
Radek Krejci7531aa22016-04-12 13:52:19 +0200460 } else {
461 /* !schema && data */
Radek Krejcie2f12212016-02-12 13:50:22 +0100462 schema = data->schema;
463 siter = data->schema->child;
Radek Krejci2342cf62016-01-29 16:48:23 +0100464 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200465
466repeat:
467 while (siter) {
Radek Krejci763122e2016-04-05 16:35:33 +0200468 if (lys_is_disabled(siter, 2) || (!status && (siter->flags & LYS_CONFIG_R))) {
Radek Krejci074bf852015-08-19 14:22:16 +0200469 siter = siter->next;
470 continue;
471 }
472
Radek Krejci7f40ce32015-08-12 20:38:46 +0200473 switch (siter->nodetype) {
474 case LYS_CONTAINER:
475 case LYS_LEAF:
476 case LYS_ANYXML:
Radek Krejci14a11a62015-08-17 17:27:38 +0200477 case LYS_LIST:
478 case LYS_LEAFLIST:
479 /* check if there is some mandatory node; first test the siter itself ... */
Michal Vasko84f821a2016-04-11 11:03:42 +0200480 if (check_mand_check(siter, lys_parent(siter), data)) {
481 return EXIT_FAILURE;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200482 }
483 /* ... and then the subtree */
Radek Krejcicf82e932016-04-11 17:52:31 +0200484 if (siter->nodetype == LYS_CONTAINER && !((struct lys_node_container *)siter)->presence) {
Radek Krejci14a11a62015-08-17 17:27:38 +0200485 saux = NULL;
Radek Krejci2342cf62016-01-29 16:48:23 +0100486 while ((saux = check_mand_getnext(saux, siter, NULL))) {
Radek Krejcie8da2e02016-04-11 17:53:34 +0200487 if ((status || (saux->flags & LYS_CONFIG_W)) && check_mand_check(saux, lys_parent(siter), data)) {
Michal Vasko84f821a2016-04-11 11:03:42 +0200488 return EXIT_FAILURE;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200489 }
490 }
491 }
492 siter = siter->next;
493 break;
494 case LYS_CHOICE:
Radek Krejci14a11a62015-08-17 17:27:38 +0200495 /* search for instance */
496 saux = siter;
497 siter = siter->child;
498 found = 0;
499 parent2 = NULL;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200500repeat_choice:
Radek Krejci2342cf62016-01-29 16:48:23 +0100501 while (siter && data) {
Radek Krejci763122e2016-04-05 16:35:33 +0200502 if (lys_is_disabled(siter, 2) || (!status && (siter->flags & LYS_CONFIG_R))) {
Radek Krejci074bf852015-08-19 14:22:16 +0200503 siter = siter->next;
504 continue;
505 }
506
Radek Krejci14a11a62015-08-17 17:27:38 +0200507 switch (siter->nodetype) {
508 case LYS_CONTAINER:
509 case LYS_LEAF:
510 case LYS_LEAFLIST:
511 case LYS_LIST:
512 case LYS_ANYXML:
513 LY_TREE_FOR(data->child, diter) {
514 if (diter->schema == siter) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200515 break;
516 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200517 }
518 if (diter) {
519 /* got instance */
520 /* check presence of mandatory siblings */
Radek Krejci65d0a652015-08-27 13:09:42 +0200521 if (parent2 && parent2->nodetype == LYS_CASE) {
Radek Krejci14a11a62015-08-17 17:27:38 +0200522 saux2 = NULL;
Radek Krejci2342cf62016-01-29 16:48:23 +0100523 while ((saux2 = check_mand_getnext(saux2, parent2, NULL))) {
Radek Krejci7531aa22016-04-12 13:52:19 +0200524 if (check_mand_check(saux2, lys_parent(saux), data)) {
Michal Vasko84f821a2016-04-11 11:03:42 +0200525 return EXIT_FAILURE;
Radek Krejci14a11a62015-08-17 17:27:38 +0200526 }
527 }
528 }
529 siter = parent2 = NULL;
530 found = 1;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200531 break;
532 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200533 siter = siter->next;
534 break;
535 case LYS_CASE:
536 case LYS_CHOICE:
537 case LYS_USES:
538 /* go into */
Radek Krejci37bda002015-08-27 11:23:56 +0200539 if (!parent2) {
540 parent2 = siter;
541 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200542 siter = siter->child;
543 break;
544 case LYS_AUGMENT:
545 case LYS_GROUPING:
546 /* skip */
547 siter = siter->next;
548 break;
549 default:
550 /* error */
551 break;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200552 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200553 }
554
Radek Krejci14a11a62015-08-17 17:27:38 +0200555 if (parent2) {
556 siter = parent2->next;
Michal Vasko84f821a2016-04-11 11:03:42 +0200557 if (lys_parent(parent2) == saux) {
Radek Krejci14a11a62015-08-17 17:27:38 +0200558 parent2 = NULL;
559 } else {
Michal Vasko84f821a2016-04-11 11:03:42 +0200560 parent2 = lys_parent(parent2);
Radek Krejci14a11a62015-08-17 17:27:38 +0200561 }
562 goto repeat_choice;
563 }
564
Radek Krejci074bf852015-08-19 14:22:16 +0200565 if (!found && (saux->flags & LYS_MAND_TRUE)) {
Radek Krejci7531aa22016-04-12 13:52:19 +0200566 LOGVAL(LYE_MISSELEM, LY_VLOG_LYD, data, saux->name,
567 (lys_parent(saux) ? lys_parent(saux)->name : lys_node_module(saux)->name));
Michal Vasko84f821a2016-04-11 11:03:42 +0200568 return EXIT_FAILURE;
Radek Krejci14a11a62015-08-17 17:27:38 +0200569 }
570
571 /* go to next */
572 siter = saux->next;
573
Radek Krejci7f40ce32015-08-12 20:38:46 +0200574 break;
575 case LYS_USES:
576 case LYS_CASE:
Michal Vaskof610fd42016-04-19 10:38:20 +0200577 case LYS_INPUT:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200578 /* go into */
579 parent = siter;
580 siter = siter->child;
581 break;
582 default:
583 /* can ignore, go to next */
584 siter = siter->next;
585 break;
586 }
587 }
588
589 if (parent) {
590 siter = parent->next;
Michal Vasko9eb6dd02016-05-02 14:52:40 +0200591 if (lys_parent(parent) == schema) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200592 parent = NULL;
593 } else {
Michal Vasko84f821a2016-04-11 11:03:42 +0200594 parent = lys_parent(parent);
Radek Krejci7f40ce32015-08-12 20:38:46 +0200595 }
596 goto repeat;
597 }
598
Michal Vasko84f821a2016-04-11 11:03:42 +0200599 return EXIT_SUCCESS;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200600}
601
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200602void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200603lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200604{
Radek Krejci76512572015-08-04 09:47:08 +0200605 struct lys_node *parent, *first;
Radek Krejcic071c542016-01-27 14:57:51 +0100606 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200607
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200608 if (!node) {
609 return;
610 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200611
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200612 /* unlink from data model if necessary */
613 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100614 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100615 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100616 if (main_module->data == node) {
617 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200618 }
619 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200620
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200621 /* store pointers to important nodes */
622 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200623 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200624 /* handle augments - first, unlink it from the augment parent ... */
625 if (parent->child == node) {
626 parent->child = node->next;
627 }
628 /* and then continue with the target parent */
Radek Krejci76512572015-08-04 09:47:08 +0200629 parent = ((struct lys_node_augment *)parent)->target;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200630 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200631
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200632 /* unlink from parent */
633 if (parent) {
634 if (parent->child == node) {
635 parent->child = node->next;
636 }
637 node->parent = NULL;
638 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200639
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200640 /* unlink from siblings */
641 if (node->prev == node) {
642 /* there are no more siblings */
643 return;
644 }
645 if (node->next) {
646 node->next->prev = node->prev;
647 } else {
648 /* unlinking the last element */
649 if (parent) {
650 first = parent->child;
651 } else {
652 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200653 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200654 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200655 }
656 }
657 first->prev = node->prev;
658 }
659 if (node->prev->next) {
660 node->prev->next = node->next;
661 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200662
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200663 /* clean up the unlinked element */
664 node->next = NULL;
665 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200666}
667
Michal Vasko563ef092015-09-04 13:17:23 +0200668struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100669lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200670{
671 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200672
673 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200674 /* top-level augment, look into module (uses augment is handled correctly below) */
675 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
676 par_iter = par_iter->parent->module->data;
677 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200678 break;
679 }
680 }
681
Michal Vasko6f929da2015-10-02 16:23:25 +0200682 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200683 continue;
684 }
685
686 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
687 if (!stop) {
688 stop = par_iter;
689 } else if (iter == stop) {
690 break;
691 }
692 if (iter->nodetype != LYS_GROUPING) {
693 continue;
694 }
695
Radek Krejcif8426a72015-10-31 23:14:03 +0100696 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200697 return (struct lys_node_grp *)iter;
698 }
699 }
700 }
701
Michal Vasko563ef092015-09-04 13:17:23 +0200702 return NULL;
703}
704
Radek Krejci10c760e2015-08-14 14:45:43 +0200705/*
706 * get next grouping in the root's subtree, in the
707 * first call, tha last is NULL
708 */
709static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200710lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200711{
Radek Krejci10c760e2015-08-14 14:45:43 +0200712 struct lys_node *last = (struct lys_node *)lastgrp;
713 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200714
Radek Krejci10c760e2015-08-14 14:45:43 +0200715 assert(root);
716
717 if (!last) {
718 last = root;
719 }
720
721 while (1) {
722 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
723 next = last->child;
724 } else {
725 next = NULL;
726 }
727 if (!next) {
728 if (last == root) {
729 /* we are done */
730 return NULL;
731 }
732
733 /* no children, go to siblings */
734 next = last->next;
735 }
736 while (!next) {
737 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100738 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200739 /* we are done */
740 return NULL;
741 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200742 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100743 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200744 }
745
746 if (next->nodetype == LYS_GROUPING) {
747 return (struct lys_node_grp *)next;
748 }
749
750 last = next;
751 }
752}
753
Michal Vasko0d343d12015-08-24 14:57:36 +0200754/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200755int
Radek Krejci07911992015-08-14 15:13:31 +0200756lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
757{
Michal Vasko563ef092015-09-04 13:17:23 +0200758 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200759 struct lys_node_grp *grp;
760 int down;
761
762 assert(node);
763
764 if (!parent) {
765 assert(module);
766 } else {
767 module = parent->module;
768 }
769
770 switch (node->nodetype) {
771 case LYS_GROUPING:
772 /* 6.2.1, rule 6 */
773 if (parent) {
774 if (parent->child) {
775 down = 1;
776 start = parent->child;
777 } else {
778 down = 0;
779 start = parent;
780 }
781 } else {
782 down = 1;
783 start = module->data;
784 }
785 /* go up */
Radek Krejcic071c542016-01-27 14:57:51 +0100786 if (lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100787 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200788 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200789 }
790 /* go down, because grouping can be defined after e.g. container in which is collision */
791 if (down) {
792 for (iter = start, stop = NULL; iter; iter = iter->prev) {
793 if (!stop) {
794 stop = start;
795 } else if (iter == stop) {
796 break;
797 }
798 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
799 continue;
800 }
801
802 grp = NULL;
803 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100804 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100805 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200806 return EXIT_FAILURE;
807 }
808 }
809 }
810 }
811 break;
812 case LYS_LEAF:
813 case LYS_LEAFLIST:
814 case LYS_LIST:
815 case LYS_CONTAINER:
816 case LYS_CHOICE:
817 case LYS_ANYXML:
818 /* 6.2.1, rule 7 */
819 if (parent) {
820 iter = parent;
821 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE))) {
822 iter = iter->parent;
823 }
824 if (!iter) {
825 stop = NULL;
826 iter = module->data;
827 } else {
828 stop = iter;
829 iter = iter->child;
830 }
831 } else {
832 stop = NULL;
833 iter = module->data;
834 }
835 while (iter) {
836 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
837 iter = iter->child;
838 continue;
839 }
840
841 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYXML)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100842 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100843 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200844 return EXIT_FAILURE;
845 }
846 }
847
848 /* special case for choice - we must check the choice's name as
849 * well as the names of nodes under the choice
850 */
851 if (iter->nodetype == LYS_CHOICE) {
852 iter = iter->child;
853 continue;
854 }
855
856 /* go to siblings */
857 if (!iter->next) {
858 /* no sibling, go to parent's sibling */
859 do {
860 iter = iter->parent;
861 if (iter && iter->next) {
862 break;
863 }
864 } while (iter != stop);
865
866 if (iter == stop) {
867 break;
868 }
869 }
870 iter = iter->next;
871 }
872 break;
873 case LYS_CASE:
874 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100875 if (parent) {
876 start = parent->child;
877 } else {
878 start = module->data;
879 }
880
881 LY_TREE_FOR(start, iter) {
Radek Krejci07911992015-08-14 15:13:31 +0200882 if (!(iter->nodetype & (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
883 continue;
884 }
885
Radek Krejci749190d2016-02-18 16:26:25 +0100886 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100887 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200888 return EXIT_FAILURE;
889 }
890 }
891 break;
892 default:
893 /* no check needed */
894 break;
895 }
896
897 return EXIT_SUCCESS;
898}
899
Michal Vasko0d343d12015-08-24 14:57:36 +0200900/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200901int
Radek Krejci10c760e2015-08-14 14:45:43 +0200902lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
903{
Radek Krejci92720552015-10-05 15:28:27 +0200904 struct lys_node *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200905 int type;
Radek Krejci10c760e2015-08-14 14:45:43 +0200906
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200907 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200908
Radek Krejci10c760e2015-08-14 14:45:43 +0200909 if (parent) {
910 type = parent->nodetype;
911 module = parent->module;
912 } else {
913 assert(module);
914 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200915 }
916
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200917 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200918 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200919 case LYS_CONTAINER:
920 case LYS_LIST:
921 case LYS_GROUPING:
922 case LYS_USES:
923 case LYS_INPUT:
924 case LYS_OUTPUT:
925 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200926 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +0200927 (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
928 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100929 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200930 return EXIT_FAILURE;
931 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200932
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200933 break;
Radek Krejci76512572015-08-04 09:47:08 +0200934 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200935 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +0200936 (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100937 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200938 return EXIT_FAILURE;
939 }
940 break;
Radek Krejci76512572015-08-04 09:47:08 +0200941 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200942 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +0200943 (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100944 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200945 return EXIT_FAILURE;
946 }
947 break;
Radek Krejci76512572015-08-04 09:47:08 +0200948 case LYS_RPC:
949 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100950 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200951 return EXIT_FAILURE;
952 }
953 break;
Radek Krejci76512572015-08-04 09:47:08 +0200954 case LYS_LEAF:
955 case LYS_LEAFLIST:
956 case LYS_ANYXML:
Radek Krejci48464ed2016-03-17 15:44:09 +0100957 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
958 LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100959 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200960 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200961 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200962 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +0200963 (LYS_ANYXML | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
964 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100965 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200966 return EXIT_FAILURE;
967 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200968 break;
969 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200970 /* top level */
971 if (!(child->nodetype &
972 (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
973 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100974 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200975 return EXIT_FAILURE;
976 }
977
Radek Krejcic071c542016-01-27 14:57:51 +0100978 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200979 }
980
981 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200982 if (lys_check_id(child, parent, module)) {
983 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200984 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200985
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200986 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200987 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200988 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200989
Radek Krejci10c760e2015-08-14 14:45:43 +0200990 if (!parent) {
Radek Krejci92720552015-10-05 15:28:27 +0200991 if (module->data) {
992 module->data->prev->next = child;
993 child->prev = module->data->prev;
994 module->data->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200995 } else {
Radek Krejci92720552015-10-05 15:28:27 +0200996 module->data = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200997 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200998 } else {
Radek Krejci10c760e2015-08-14 14:45:43 +0200999 if (!parent->child) {
1000 /* the only/first child of the parent */
1001 parent->child = child;
1002 child->parent = parent;
1003 iter = child;
1004 } else {
1005 /* add a new child at the end of parent's child list */
1006 iter = parent->child->prev;
1007 iter->next = child;
1008 child->prev = iter;
1009 }
1010 while (iter->next) {
1011 iter = iter->next;
1012 iter->parent = parent;
1013 }
1014 parent->child->prev = iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001015 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001016
Radek Krejci41771502016-04-14 17:52:32 +02001017 /* propagate information about status data presence */
1018 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML)) &&
1019 (child->flags & LYS_INCL_STATUS)) {
1020 for(iter = parent; iter; iter = iter->parent) {
1021 /* store it only into container or list - the only data inner nodes */
1022 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
1023 if (iter->flags & LYS_INCL_STATUS) {
1024 /* done, someone else set it already from here */
1025 break;
1026 }
1027 /* set flag about including status data */
1028 iter->flags |= LYS_INCL_STATUS;
1029 }
1030 }
1031 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001032 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001033}
1034
Radek Krejcia1df1682016-04-11 14:56:59 +02001035static const struct lys_module *
1036lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001037{
Radek Krejcia1df1682016-04-11 14:56:59 +02001038 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +02001039 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +02001040 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001041
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001042 if (!ctx || !data) {
1043 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1044 return NULL;
1045 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001046
Radek Krejcia1df1682016-04-11 14:56:59 +02001047 if (!internal && format == LYS_IN_YANG) {
1048 /* enlarge data by 2 bytes for flex */
1049 len = strlen(data);
1050 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
1051 if (!enlarged_data) {
1052 LOGMEM;
1053 return NULL;
1054 }
1055 memcpy(enlarged_data, data, len);
1056 enlarged_data[len] = enlarged_data[len + 1] = '\0';
1057 data = enlarged_data;
1058 }
1059
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001060 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001061 case LYS_IN_YIN:
Radek Krejciff4874d2016-03-07 12:30:50 +01001062 mod = yin_read_module(ctx, data, NULL, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001063 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001064 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001065 mod = yang_read_module(ctx, data, 0, NULL, 1);
1066 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001067 default:
Radek Krejcia1df1682016-04-11 14:56:59 +02001068 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001069 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001070 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001071
Radek Krejcia1df1682016-04-11 14:56:59 +02001072 free(enlarged_data);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001073 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001074}
1075
Radek Krejcia1df1682016-04-11 14:56:59 +02001076API const struct lys_module *
1077lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
1078{
1079 return lys_parse_mem_(ctx, data, format, 0);
1080}
1081
Michal Vasko5a721fd2016-02-16 12:16:48 +01001082struct lys_submodule *
1083lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001084{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001085 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001086
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001087 assert(module);
1088 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +02001089
Radek Krejcic071c542016-01-27 14:57:51 +01001090 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +02001091 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001092
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001093 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001094 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +01001095 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001096 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001097 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001098 submod = yang_read_submodule(module, data, 0, unres);
1099 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001100 default:
Radek Krejci90a550a2016-04-13 16:00:58 +02001101 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001102 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001103 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001104
Michal Vasko5a721fd2016-02-16 12:16:48 +01001105 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +02001106}
1107
Michal Vasko1e62a092015-12-01 12:27:20 +01001108API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +01001109lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1110{
1111 int fd;
1112 const struct lys_module *ret;
1113
1114 if (!ctx || !path) {
1115 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1116 return NULL;
1117 }
1118
1119 fd = open(path, O_RDONLY);
1120 if (fd == -1) {
1121 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
1122 return NULL;
1123 }
1124
1125 ret = lys_parse_fd(ctx, fd, format);
1126 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +01001127
Radek Krejcia77904e2016-02-25 16:23:45 +01001128 if (ret && !ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +01001129 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +01001130 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +01001131 }
1132
Michal Vasko662610a2015-12-07 11:25:45 +01001133 return ret;
1134}
1135
1136API const struct lys_module *
1137lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001138{
Michal Vasko1e62a092015-12-01 12:27:20 +01001139 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +02001140 struct stat sb;
1141 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +01001142 char buf[PATH_MAX];
1143 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +02001144
1145 if (!ctx || fd < 0) {
1146 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1147 return NULL;
1148 }
1149
Radek Krejci10a833c2015-12-16 15:28:37 +01001150 if (fstat(fd, &sb) == -1) {
1151 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
1152 return NULL;
1153 }
Radek Krejcib051f722016-02-25 15:12:21 +01001154 if (!S_ISREG(sb.st_mode)) {
1155 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
1156 return NULL;
1157 }
1158
Michal Vasko164d9012016-04-01 10:16:59 +02001159 if (!sb.st_size) {
1160 LOGERR(LY_EINVAL, "File empty.");
1161 return NULL;
1162 }
1163
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001164 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001165 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +01001166 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001167 return NULL;
1168 }
Radek Krejcia1df1682016-04-11 14:56:59 +02001169 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001170 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001171
Radek Krejcia77904e2016-02-25 16:23:45 +01001172 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +01001173 /* get URI if there is /proc */
1174 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +01001175 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
1176 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
1177 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
1178 }
1179 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +01001180 }
Radek Krejcib051f722016-02-25 15:12:21 +01001181 }
1182
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001183 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001184}
1185
Michal Vasko5a721fd2016-02-16 12:16:48 +01001186struct lys_submodule *
1187lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001188{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001189 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001190 struct stat sb;
1191 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001192
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001193 assert(module);
1194 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001195
Radek Krejci10a833c2015-12-16 15:28:37 +01001196 if (fstat(fd, &sb) == -1) {
1197 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +01001198 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +01001199 }
Michal Vasko164d9012016-04-01 10:16:59 +02001200
1201 if (!sb.st_size) {
1202 LOGERR(LY_EINVAL, "File empty.");
1203 return NULL;
1204 }
1205
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001206 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001207 if (addr == MAP_FAILED) {
1208 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001209 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001210 }
Michal Vasko5a721fd2016-02-16 12:16:48 +01001211 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001212 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +02001213
Michal Vasko5a721fd2016-02-16 12:16:48 +01001214 return submodule;
1215
Radek Krejciefaeba32015-05-27 14:30:57 +02001216}
1217
Radek Krejci1d82ef62015-08-07 14:44:40 +02001218static struct lys_restr *
1219lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001220{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001221 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001222 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001223
Radek Krejci3733a802015-06-19 13:43:21 +02001224 if (!size) {
1225 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001226 }
Radek Krejci3733a802015-06-19 13:43:21 +02001227
1228 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001229 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +01001230 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +01001231 return NULL;
1232 }
Radek Krejci3733a802015-06-19 13:43:21 +02001233 for (i = 0; i < size; i++) {
1234 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
1235 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
1236 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
1237 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
1238 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
1239 }
1240
1241 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001242}
1243
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001244void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001245lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +02001246{
1247 assert(ctx);
1248 if (!restr) {
1249 return;
1250 }
1251
1252 lydict_remove(ctx, restr->expr);
1253 lydict_remove(ctx, restr->dsc);
1254 lydict_remove(ctx, restr->ref);
1255 lydict_remove(ctx, restr->eapptag);
1256 lydict_remove(ctx, restr->emsg);
1257}
1258
Michal Vaskob84f88a2015-09-24 13:16:10 +02001259static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001260type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
1261 LY_DATA_TYPE base, struct unres_schema *unres)
1262{
1263 int i;
1264
1265 switch (base) {
1266 case LY_TYPE_BINARY:
1267 if (old->info.binary.length) {
1268 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1269 }
1270 break;
1271
1272 case LY_TYPE_BITS:
1273 new->info.bits.count = old->info.bits.count;
1274 if (new->info.bits.count) {
1275 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1276 if (!new->info.bits.bit) {
1277 LOGMEM;
1278 return -1;
1279 }
1280 for (i = 0; i < new->info.bits.count; i++) {
1281 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1282 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1283 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1284 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1285 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1286 }
1287 }
1288 break;
1289
1290 case LY_TYPE_DEC64:
1291 new->info.dec64.dig = old->info.dec64.dig;
1292 if (old->info.dec64.range) {
1293 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1294 }
1295 break;
1296
1297 case LY_TYPE_ENUM:
1298 new->info.enums.count = old->info.enums.count;
1299 if (new->info.enums.count) {
1300 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1301 if (!new->info.enums.enm) {
1302 LOGMEM;
1303 return -1;
1304 }
1305 for (i = 0; i < new->info.enums.count; i++) {
1306 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1307 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1308 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1309 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1310 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1311 }
1312 }
1313 break;
1314
1315 case LY_TYPE_IDENT:
1316 if (old->info.ident.ref) {
1317 new->info.ident.ref = old->info.ident.ref;
1318 } else {
1319 i = unres_schema_find(unres, old, UNRES_TYPE_IDENTREF);
1320 if (i > -1 && unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i])) {
1321 return -1;
1322 }
1323 }
1324 break;
1325
1326 case LY_TYPE_INST:
1327 new->info.inst.req = old->info.inst.req;
1328 break;
1329
1330 case LY_TYPE_INT8:
1331 case LY_TYPE_INT16:
1332 case LY_TYPE_INT32:
1333 case LY_TYPE_INT64:
1334 case LY_TYPE_UINT8:
1335 case LY_TYPE_UINT16:
1336 case LY_TYPE_UINT32:
1337 case LY_TYPE_UINT64:
1338 if (old->info.num.range) {
1339 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1340 }
1341 break;
1342
1343 case LY_TYPE_LEAFREF:
1344 if (old->info.lref.path) {
1345 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
1346 if (unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent)) {
1347 return -1;
1348 }
1349 }
1350 break;
1351
1352 case LY_TYPE_STRING:
1353 if (old->info.str.length) {
1354 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1355 }
1356 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1357 new->info.str.pat_count = old->info.str.pat_count;
1358 break;
1359
1360 case LY_TYPE_UNION:
1361 new->info.uni.count = old->info.uni.count;
1362 if (new->info.uni.count) {
1363 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1364 if (!new->info.uni.types) {
1365 LOGMEM;
1366 return -1;
1367 }
1368 for (i = 0; i < new->info.uni.count; i++) {
1369 if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), unres)) {
1370 return -1;
1371 }
1372 }
1373 }
1374 break;
1375
1376 default:
1377 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1378 break;
1379 }
1380 return EXIT_SUCCESS;
1381}
1382
1383struct yang_type *
1384lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type, struct unres_schema *unres)
1385{
1386 struct yang_type *new;
1387
1388 new = calloc(1, sizeof *new);
1389 if (!new) {
1390 LOGMEM;
1391 return NULL;
1392 }
1393 new->flags = old->flags;
1394 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001395 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001396 new->type = type;
1397 if (!new->name) {
1398 LOGMEM;
1399 goto error;
1400 }
1401 if (type_dup(module, parent, type, old->type, new->base, unres)) {
1402 new->type->base = new->base;
1403 lys_type_free(module->ctx, new->type);
1404 memset(&new->type->info, 0, sizeof new->type->info);
1405 goto error;
1406 }
1407 return new;
1408
1409 error:
1410 free(new);
1411 return NULL;
1412}
1413
1414static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001415lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Michal Vaskof02e3742015-08-05 16:27:02 +02001416 struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001417{
1418 int i;
1419
Michal Vasko1dca6882015-10-22 14:29:42 +02001420 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001421 new->base = old->base;
1422 new->der = old->der;
1423
Michal Vasko0bd29d12015-08-19 11:45:49 +02001424 i = unres_schema_find(unres, old, UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001425 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001426 /* HACK (serious one) for unres */
1427 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001428 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
1429 new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, unres);
1430 } else {
1431 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1432 }
Radek Krejcic071c542016-01-27 14:57:51 +01001433 new->parent = (struct lys_tpdf *)parent;
Michal Vaskob84f88a2015-09-24 13:16:10 +02001434 /* all these unres additions can fail even though they did not before */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001435 if (!new->der || unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001436 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001437 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001438 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001439 }
1440
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001441 return type_dup(mod, parent, new, old, new->base, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001442}
1443
1444void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001445lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001446{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001447 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001448
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001449 assert(ctx);
1450 if (!type) {
1451 return;
1452 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001453
Michal Vasko1dca6882015-10-22 14:29:42 +02001454 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001455
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001456 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001457 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001458 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001459 free(type->info.binary.length);
1460 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001461 case LY_TYPE_BITS:
1462 for (i = 0; i < type->info.bits.count; i++) {
1463 lydict_remove(ctx, type->info.bits.bit[i].name);
1464 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1465 lydict_remove(ctx, type->info.bits.bit[i].ref);
1466 }
1467 free(type->info.bits.bit);
1468 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001469
1470 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001471 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001472 free(type->info.dec64.range);
1473 break;
1474
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001475 case LY_TYPE_ENUM:
1476 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001477 lydict_remove(ctx, type->info.enums.enm[i].name);
1478 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1479 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001480 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001481 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001482 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001483
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001484 case LY_TYPE_INT8:
1485 case LY_TYPE_INT16:
1486 case LY_TYPE_INT32:
1487 case LY_TYPE_INT64:
1488 case LY_TYPE_UINT8:
1489 case LY_TYPE_UINT16:
1490 case LY_TYPE_UINT32:
1491 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001492 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001493 free(type->info.num.range);
1494 break;
1495
Radek Krejcidc4c1412015-06-19 15:39:54 +02001496 case LY_TYPE_LEAFREF:
1497 lydict_remove(ctx, type->info.lref.path);
1498 break;
1499
Radek Krejci3733a802015-06-19 13:43:21 +02001500 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001501 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001502 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001503 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001504 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001505 }
1506 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001507 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001508
Radek Krejcie4c366b2015-07-02 10:11:31 +02001509 case LY_TYPE_UNION:
1510 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001511 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001512 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001513 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001514 break;
1515
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001516 default:
Radek Krejcic7c85532015-07-02 10:16:54 +02001517 /* nothing to do for LY_TYPE_IDENT, LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001518 break;
1519 }
Radek Krejci5a065542015-05-22 15:02:07 +02001520}
1521
Radek Krejci1d82ef62015-08-07 14:44:40 +02001522static void
1523lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001524{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001525 assert(ctx);
1526 if (!tpdf) {
1527 return;
1528 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001529
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001530 lydict_remove(ctx, tpdf->name);
1531 lydict_remove(ctx, tpdf->dsc);
1532 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001533
Radek Krejci1d82ef62015-08-07 14:44:40 +02001534 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001535
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001536 lydict_remove(ctx, tpdf->units);
1537 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001538}
1539
Michal Vaskob84f88a2015-09-24 13:16:10 +02001540static struct lys_tpdf *
1541lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1542{
1543 struct lys_tpdf *result;
1544 int i, j;
1545
1546 if (!size) {
1547 return NULL;
1548 }
1549
1550 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001551 if (!result) {
1552 LOGMEM;
1553 return NULL;
1554 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001555 for (i = 0; i < size; i++) {
1556 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1557 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1558 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1559 result[i].flags = old[i].flags;
1560 result[i].module = old[i].module;
1561
1562 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), unres)) {
1563 for (j = 0; j <= i; ++j) {
1564 lys_tpdf_free(mod->ctx, &result[j]);
1565 }
1566 free(result);
1567 return NULL;
1568 }
1569
1570 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1571 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1572 }
1573
1574 return result;
1575}
1576
Radek Krejci1d82ef62015-08-07 14:44:40 +02001577static struct lys_when *
1578lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001579{
Radek Krejci76512572015-08-04 09:47:08 +02001580 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001581
1582 if (!old) {
1583 return NULL;
1584 }
1585
1586 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001587 if (!new) {
1588 LOGMEM;
1589 return NULL;
1590 }
Radek Krejci00768f42015-06-18 17:04:04 +02001591 new->cond = lydict_insert(ctx, old->cond, 0);
1592 new->dsc = lydict_insert(ctx, old->dsc, 0);
1593 new->ref = lydict_insert(ctx, old->ref, 0);
1594
1595 return new;
1596}
1597
Michal Vasko0308dd62015-10-07 09:14:40 +02001598void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001599lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001600{
1601 if (!w) {
1602 return;
1603 }
1604
1605 lydict_remove(ctx, w->cond);
1606 lydict_remove(ctx, w->dsc);
1607 lydict_remove(ctx, w->ref);
1608
1609 free(w);
1610}
1611
Radek Krejcib7f5e412015-08-13 10:15:51 +02001612static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001613lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib7f5e412015-08-13 10:15:51 +02001614{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001615 struct lys_node *next, *sub;
1616
Radek Krejcic071c542016-01-27 14:57:51 +01001617 /* children from a resolved augment are freed under the target node */
Michal Vaskod2fbade2016-05-02 17:14:00 +02001618 if (!aug->target) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001619 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001620 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001621 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001622 }
1623
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001624 lydict_remove(ctx, aug->target_name);
1625 lydict_remove(ctx, aug->dsc);
1626 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001627
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001628 free(aug->features);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001629
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001630 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001631}
1632
Radek Krejci76512572015-08-04 09:47:08 +02001633static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001634lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001635{
Radek Krejci76512572015-08-04 09:47:08 +02001636 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001637 struct lys_node *old_child, *new_child;
1638 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001639
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001640 if (!size) {
1641 return NULL;
1642 }
Radek Krejci106efc02015-06-10 14:36:27 +02001643
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001644 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001645 if (!new) {
1646 LOGMEM;
1647 return NULL;
1648 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001649 for (i = 0; i < size; i++) {
1650 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1651 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1652 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1653 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001654 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001655 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001656
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001657 /* this must succeed, it was already resolved once */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001658 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL,
1659 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001660 LOGINT;
1661 free(new);
1662 return NULL;
1663 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001664 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001665
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001666 /* Correct the augment nodes.
1667 * This function can only be called from lys_node_dup() with uses
1668 * being the node duplicated, so we must have a case of grouping
1669 * with a uses with augments. The augmented nodes have already been
1670 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001671 * (it was set to their actual data parent, not an augment), and
1672 * the new augment does not have child pointer to its augment nodes,
1673 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001674 */
1675 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001676 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001677 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001678 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001679 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001680 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001681 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001682 LY_TREE_FOR(old[i].child, old_child) {
1683 /* all augment nodes were connected as siblings, there can be no more after this */
1684 if (old_child->parent != (struct lys_node *)&old[i]) {
1685 break;
1686 }
1687
Radek Krejci749190d2016-02-18 16:26:25 +01001688 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001689
1690 new_child->parent = (struct lys_node *)&new[i];
1691 new_child = new_child->next;
1692 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001693 }
Radek Krejci106efc02015-06-10 14:36:27 +02001694
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001695 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001696}
1697
Radek Krejci76512572015-08-04 09:47:08 +02001698static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001699lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001700{
Radek Krejci76512572015-08-04 09:47:08 +02001701 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001702 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001703
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001704 if (!size) {
1705 return NULL;
1706 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001707
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001708 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001709 if (!result) {
1710 LOGMEM;
1711 return NULL;
1712 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001713 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001714 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001715 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1716 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001717 result[i].flags = old[i].flags;
1718 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001719
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001720 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001721 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001722
Radek Krejci76512572015-08-04 09:47:08 +02001723 if (result[i].target_type & (LYS_LEAF | LYS_CHOICE)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001724 result[i].mod.dflt = lydict_insert(mod->ctx, old[i].mod.dflt, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001725 } else if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001726 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001727 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001728 result[i].mod.list = old[i].mod.list;
1729 }
1730 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001731
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001732 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001733}
1734
Radek Krejci1d82ef62015-08-07 14:44:40 +02001735static void
1736lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001737{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001738 assert(ctx);
1739 if (!ident) {
1740 return;
1741 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001742
Radek Krejci1b61d0e2016-04-15 13:55:44 +02001743 free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001744 lydict_remove(ctx, ident->name);
1745 lydict_remove(ctx, ident->dsc);
1746 lydict_remove(ctx, ident->ref);
Radek Krejci6793db02015-05-22 17:49:54 +02001747
1748}
1749
Radek Krejci1d82ef62015-08-07 14:44:40 +02001750static void
1751lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001752{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001753 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001754
Radek Krejcid12f57b2015-08-06 10:43:39 +02001755 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001756 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001757 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001758 }
1759 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001760}
1761
Radek Krejci1d82ef62015-08-07 14:44:40 +02001762static void
Radek Krejcid12f57b2015-08-06 10:43:39 +02001763lys_rpc_inout_free(struct ly_ctx *ctx, struct lys_node_rpc_inout *io)
1764{
1765 int i;
1766
1767 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1768 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001769 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001770 }
1771 free(io->tpdf);
1772}
1773
Radek Krejci1d82ef62015-08-07 14:44:40 +02001774static void
1775lys_anyxml_free(struct ly_ctx *ctx, struct lys_node_anyxml *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001776{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001777 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001778
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001779 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001780 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001781 }
1782 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001783
Radek Krejci1d82ef62015-08-07 14:44:40 +02001784 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001785}
1786
Radek Krejci1d82ef62015-08-07 14:44:40 +02001787static void
1788lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001789{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001790 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001791
Radek Krejci46c4cd72016-01-21 15:13:52 +01001792 if (leaf->child) {
1793 /* leafref backlinks */
1794 ly_set_free((struct ly_set *)leaf->child);
1795 }
1796
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001797 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001798 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001799 }
1800 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001801
Radek Krejci1d82ef62015-08-07 14:44:40 +02001802 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001803
Radek Krejci1d82ef62015-08-07 14:44:40 +02001804 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001805 lydict_remove(ctx, leaf->units);
1806 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001807}
1808
Radek Krejci1d82ef62015-08-07 14:44:40 +02001809static void
1810lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001811{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001812 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001813
Radek Krejci46c4cd72016-01-21 15:13:52 +01001814 if (llist->child) {
1815 /* leafref backlinks */
1816 ly_set_free((struct ly_set *)llist->child);
1817 }
1818
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001819 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001820 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001821 }
1822 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001823
Radek Krejci1d82ef62015-08-07 14:44:40 +02001824 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001825
Radek Krejci1d82ef62015-08-07 14:44:40 +02001826 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001827 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001828}
1829
Radek Krejci1d82ef62015-08-07 14:44:40 +02001830static void
1831lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001832{
Radek Krejci581ce772015-11-10 17:22:40 +01001833 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001834
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001835 /* handle only specific parts for LY_NODE_LIST */
1836 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001837 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001838 }
1839 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001840
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001841 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001842 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001843 }
1844 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001845
Radek Krejci1d82ef62015-08-07 14:44:40 +02001846 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001847
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001848 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001849 for (j = 0; j > list->unique[i].expr_size; j++) {
1850 lydict_remove(ctx, list->unique[i].expr[j]);
1851 }
1852 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001853 }
1854 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001855
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001856 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001857}
1858
Radek Krejci1d82ef62015-08-07 14:44:40 +02001859static void
1860lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001861{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001862 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001863
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001864 /* handle only specific parts for LY_NODE_CONTAINER */
1865 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001866
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001867 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001868 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001869 }
1870 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001871
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001872 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001873 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001874 }
1875 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001876
Radek Krejci1d82ef62015-08-07 14:44:40 +02001877 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001878}
1879
Radek Krejci1d82ef62015-08-07 14:44:40 +02001880static void
1881lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001882{
1883 lydict_remove(ctx, f->name);
1884 lydict_remove(ctx, f->dsc);
1885 lydict_remove(ctx, f->ref);
1886 free(f->features);
1887}
1888
Radek Krejci1d82ef62015-08-07 14:44:40 +02001889static void
Michal Vaskoff006c12016-02-17 11:15:19 +01001890lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001891{
Radek Krejci581ce772015-11-10 17:22:40 +01001892 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01001893 struct ly_ctx *ctx;
1894 struct lys_node *next, *elem;
1895
1896 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02001897
1898 lydict_remove(ctx, dev->target_name);
1899 lydict_remove(ctx, dev->dsc);
1900 lydict_remove(ctx, dev->ref);
1901
Michal Vaskoff006c12016-02-17 11:15:19 +01001902 /* the module was freed, but we only need the context from orig_node, use ours */
1903 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
1904 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
1905 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
1906 elem->module = module;
1907
1908 LY_TREE_DFS_END(dev->orig_node, next, elem);
1909 }
1910 lys_node_free(dev->orig_node, NULL, 0);
1911 } else {
1912 /* it's just a shallow copy, freeing one node */
1913 dev->orig_node->module = module;
1914 lys_node_free(dev->orig_node, NULL, 1);
1915 }
1916
Radek Krejcieb00f512015-07-01 16:44:58 +02001917 for (i = 0; i < dev->deviate_size; i++) {
1918 lydict_remove(ctx, dev->deviate[i].dflt);
1919 lydict_remove(ctx, dev->deviate[i].units);
1920
1921 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1922 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001923 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001924 }
1925 free(dev->deviate[i].must);
1926
1927 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001928 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1929 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1930 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01001931 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001932 }
1933 free(dev->deviate[i].unique);
1934 }
1935 }
1936 free(dev->deviate);
1937}
1938
Radek Krejci1d82ef62015-08-07 14:44:40 +02001939static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001940lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie1fa8582015-06-08 09:46:45 +02001941{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001942 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001943
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001944 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001945 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001946 lydict_remove(ctx, uses->refine[i].dsc);
1947 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001948
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001949 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001950 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001951 }
1952 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001953
Radek Krejci76512572015-08-04 09:47:08 +02001954 if (uses->refine[i].target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001955 lydict_remove(ctx, uses->refine[i].mod.dflt);
Radek Krejci76512572015-08-04 09:47:08 +02001956 } else if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001957 lydict_remove(ctx, uses->refine[i].mod.presence);
1958 }
1959 }
1960 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001961
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001962 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001963 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001964 }
1965 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001966
Radek Krejci1d82ef62015-08-07 14:44:40 +02001967 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001968}
1969
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001970void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001971lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys_node *node, void *priv), int shallow)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001972{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001973 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001974 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001975
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001976 if (!node) {
1977 return;
1978 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001979
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001980 assert(node->module);
1981 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001982
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001983 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001984
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001985 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01001986 if (node->priv && private_destructor) {
1987 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001988 }
1989
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001990 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001991 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001992 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
1993 free(node->features);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001994 lydict_remove(ctx, node->dsc);
1995 lydict_remove(ctx, node->ref);
1996 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001997
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001998 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001999 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002000 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002001 }
2002 }
2003
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002004 /* specific part */
2005 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002006 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002007 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002008 break;
Radek Krejci76512572015-08-04 09:47:08 +02002009 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002010 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002011 break;
Radek Krejci76512572015-08-04 09:47:08 +02002012 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002013 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002014 break;
Radek Krejci76512572015-08-04 09:47:08 +02002015 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002016 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002017 break;
Radek Krejci76512572015-08-04 09:47:08 +02002018 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002019 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002020 break;
Radek Krejci76512572015-08-04 09:47:08 +02002021 case LYS_ANYXML:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002022 lys_anyxml_free(ctx, (struct lys_node_anyxml *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002023 break;
Radek Krejci76512572015-08-04 09:47:08 +02002024 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002025 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002026 break;
Radek Krejci76512572015-08-04 09:47:08 +02002027 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002028 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002029 break;
Radek Krejci76512572015-08-04 09:47:08 +02002030 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002031 /* do nothing */
2032 break;
Radek Krejci76512572015-08-04 09:47:08 +02002033 case LYS_GROUPING:
2034 case LYS_RPC:
Radek Krejci76512572015-08-04 09:47:08 +02002035 case LYS_NOTIF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002036 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002037 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02002038
2039 case LYS_INPUT:
2040 case LYS_OUTPUT:
2041 lys_rpc_inout_free(ctx, (struct lys_node_rpc_inout *)node);
2042 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02002043 case LYS_UNKNOWN:
2044 LOGINT;
2045 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002046 }
Radek Krejci5a065542015-05-22 15:02:07 +02002047
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002048 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002049 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002050 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002051}
2052
Michal Vasko1e62a092015-12-01 12:27:20 +01002053const struct lys_module *
2054lys_get_import_module(const struct lys_module *module, const char *prefix, int pref_len, const char *name, int name_len)
Michal Vasko8ce24d72015-10-21 11:27:26 +02002055{
Radek Krejcic071c542016-01-27 14:57:51 +01002056 const struct lys_module *main_module;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002057 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02002058
Michal Vaskoa7789a82016-02-11 15:42:55 +01002059 assert(!prefix || !name);
2060
Michal Vaskob6729c62015-10-21 12:09:47 +02002061 if (prefix && !pref_len) {
2062 pref_len = strlen(prefix);
2063 }
2064 if (name && !name_len) {
2065 name_len = strlen(name);
2066 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02002067
Radek Krejcic4283442016-04-22 09:19:27 +02002068 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01002069
2070 /* module own prefix, submodule own prefix, (sub)module own name */
2071 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
2072 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01002073 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01002074 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02002075 }
2076
Michal Vasko8ce24d72015-10-21 11:27:26 +02002077 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002078 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2079 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002080 return module->imp[i].module;
2081 }
2082 }
2083
2084 return NULL;
2085}
2086
Michal Vasko13b15832015-08-19 11:04:48 +02002087/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002088static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002089module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002090{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002091 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002092 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002093 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002094
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002095 assert(module->ctx);
2096 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002097
Michal Vaskob746fff2016-02-11 11:37:50 +01002098 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002099 for (i = 0; i < module->imp_size; i++) {
Radek Krejci4f78b532016-02-17 13:43:00 +01002100 if (!module->imp[i].external) {
2101 lydict_remove(ctx, module->imp[i].prefix);
2102 }
Radek Krejci225376f2016-02-16 17:36:22 +01002103 }
Radek Krejcidce51452015-06-16 15:20:08 +02002104 free(module->imp);
2105
Radek Krejcic071c542016-01-27 14:57:51 +01002106 /* submodules don't have data tree, the data nodes
2107 * are placed in the main module altogether */
2108 if (!module->type) {
2109 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002110 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002111 }
Radek Krejci21181962015-06-30 14:11:00 +02002112 }
Radek Krejci5a065542015-05-22 15:02:07 +02002113
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002114 lydict_remove(ctx, module->dsc);
2115 lydict_remove(ctx, module->ref);
2116 lydict_remove(ctx, module->org);
2117 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002118 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002119
Radek Krejcieb00f512015-07-01 16:44:58 +02002120 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002121 for (i = 0; i < module->rev_size; i++) {
2122 lydict_remove(ctx, module->rev[i].dsc);
2123 lydict_remove(ctx, module->rev[i].ref);
2124 }
2125 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002126
Radek Krejcieb00f512015-07-01 16:44:58 +02002127 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002128 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002129 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002130 }
2131 module->ident_size = 0;
2132 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002133
Radek Krejcieb00f512015-07-01 16:44:58 +02002134 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002135 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002136 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002137 }
2138 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002139
Radek Krejcieb00f512015-07-01 16:44:58 +02002140 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002141 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01002142 /* complete submodule free is done only from main module since
2143 * submodules propagate their includes to the main module */
2144 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002145 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002146 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002147 }
2148 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002149
Radek Krejcieb00f512015-07-01 16:44:58 +02002150 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002151 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002152 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002153 }
2154 free(module->augment);
2155
Radek Krejcieb00f512015-07-01 16:44:58 +02002156 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002157 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002158 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002159 }
2160 free(module->features);
2161
Radek Krejcieb00f512015-07-01 16:44:58 +02002162 /* deviations */
2163 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002164 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002165 }
2166 free(module->deviation);
2167
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002168 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002169 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002170}
2171
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002172void
Michal Vaskob746fff2016-02-11 11:37:50 +01002173lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002174{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002175 if (!submodule) {
2176 return;
2177 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002178
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002179 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002180 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002181
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002182 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002183
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002184 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002185}
2186
Radek Krejci76512572015-08-04 09:47:08 +02002187struct lys_node *
Radek Krejcic071c542016-01-27 14:57:51 +01002188lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t flags,
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002189 uint8_t nacm, struct unres_schema *unres, int shallow)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002190{
Radek Krejcic071c542016-01-27 14:57:51 +01002191 struct lys_node *retval = NULL, *child;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002192 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002193 int i, j, rc;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002194
Michal Vaskoc07187d2015-08-13 15:20:57 +02002195 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002196 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002197 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002198 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002199 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002200 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002201 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002202 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002203 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002204 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002205 struct lys_node_anyxml *anyxml = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002206 struct lys_node_anyxml *anyxml_orig = (struct lys_node_anyxml *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002207 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002208 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002209 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002210 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002211 struct lys_node_rpc *rpc = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002212 struct lys_node_rpc *rpc_orig = (struct lys_node_rpc *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002213 struct lys_node_rpc_inout *io = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002214 struct lys_node_rpc_inout *io_orig = (struct lys_node_rpc_inout *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002215 struct lys_node_rpc *ntf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002216 struct lys_node_rpc *ntf_orig = (struct lys_node_rpc *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002217 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002218 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002219
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002220 /* we cannot just duplicate memory since the strings are stored in
2221 * dictionary and we need to update dictionary counters.
2222 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002223
Radek Krejci1d82ef62015-08-07 14:44:40 +02002224 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002225 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002226 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002227 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002228 break;
2229
Radek Krejci76512572015-08-04 09:47:08 +02002230 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002231 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002232 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002233 break;
2234
Radek Krejci76512572015-08-04 09:47:08 +02002235 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002236 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002237 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002238 break;
2239
Radek Krejci76512572015-08-04 09:47:08 +02002240 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002241 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002242 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002243 break;
2244
Radek Krejci76512572015-08-04 09:47:08 +02002245 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002246 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002247 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002248 break;
2249
Radek Krejci76512572015-08-04 09:47:08 +02002250 case LYS_ANYXML:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002251 anyxml = calloc(1, sizeof *anyxml);
Radek Krejci76512572015-08-04 09:47:08 +02002252 retval = (struct lys_node *)anyxml;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002253 break;
2254
Radek Krejci76512572015-08-04 09:47:08 +02002255 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002256 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002257 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002258 break;
2259
Radek Krejci76512572015-08-04 09:47:08 +02002260 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002261 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002262 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002263 break;
2264
Radek Krejci76512572015-08-04 09:47:08 +02002265 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002266 grp = calloc(1, sizeof *grp);
2267 retval = (struct lys_node *)grp;
2268 break;
2269
Radek Krejci76512572015-08-04 09:47:08 +02002270 case LYS_RPC:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002271 rpc = calloc(1, sizeof *rpc);
2272 retval = (struct lys_node *)rpc;
2273 break;
2274
Radek Krejci76512572015-08-04 09:47:08 +02002275 case LYS_INPUT:
2276 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002277 io = calloc(1, sizeof *io);
2278 retval = (struct lys_node *)io;
2279 break;
2280
Radek Krejci76512572015-08-04 09:47:08 +02002281 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002282 ntf = calloc(1, sizeof *ntf);
2283 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002284 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002285
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002286 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002287 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002288 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002289 }
Radek Krejcib388c152015-06-04 17:03:03 +02002290
Michal Vasko253035f2015-12-17 16:58:13 +01002291 if (!retval) {
2292 LOGMEM;
2293 return NULL;
2294 }
2295
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002296 /*
2297 * duplicate generic part of the structure
2298 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002299 retval->name = lydict_insert(ctx, node->name, 0);
2300 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2301 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002302 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002303 retval->flags = node->flags;
Radek Krejci1574a8d2015-08-03 14:16:52 +02002304 if (!(retval->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002305 /* set parent's config flag */
Radek Krejci1574a8d2015-08-03 14:16:52 +02002306 retval->flags |= flags & LYS_CONFIG_MASK;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002307 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002308
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002309 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002310 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002311
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002312 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002313
Radek Krejci1d82ef62015-08-07 14:44:40 +02002314 retval->features_size = node->features_size;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002315 retval->features = calloc(retval->features_size, sizeof *retval->features);
Michal Vasko253035f2015-12-17 16:58:13 +01002316 if (!retval->features) {
2317 LOGMEM;
2318 goto error;
2319 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002320
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002321 if (!shallow) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002322 for (i = 0; i < node->features_size; ++i) {
2323 retval->features[i] = (struct lys_feature *)retval;
2324 if (unres_schema_dup(module, unres, &node->features[i], UNRES_IFFEAT, &retval->features[i])) {
2325 retval->features[i] = node->features[i];
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002326 }
2327 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002328
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002329 /* connect it to the parent */
2330 if (lys_node_addchild(parent, retval->module, retval)) {
2331 goto error;
2332 }
Radek Krejcidce51452015-06-16 15:20:08 +02002333
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002334 /* go recursively */
2335 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2336 LY_TREE_FOR(node->child, child) {
2337 if (!lys_node_dup(module, retval, child, retval->flags, retval->nacm, unres, 0)) {
2338 goto error;
2339 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002340 }
2341 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002342 } else {
2343 memcpy(retval->features, node->features, retval->features_size * sizeof *retval->features);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002344 }
2345
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002346 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002347 * duplicate specific part of the structure
2348 */
2349 switch (node->nodetype) {
2350 case LYS_CONTAINER:
2351 if (cont_orig->when) {
2352 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002353 }
2354 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002355
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002356 cont->must_size = cont_orig->must_size;
2357 cont->tpdf_size = cont_orig->tpdf_size;
2358
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002359 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002360 cont->tpdf = lys_tpdf_dup(module, node->parent, cont_orig->tpdf, cont->tpdf_size, unres);
2361 break;
2362
2363 case LYS_CHOICE:
2364 if (choice_orig->when) {
2365 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002366 }
2367
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002368 if (!shallow) {
2369 if (choice_orig->dflt) {
2370 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, LYS_ANYXML
2371 | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST
2372 | LYS_LIST, (const struct lys_node **)&choice->dflt);
2373 if (rc) {
2374 if (rc == EXIT_FAILURE) {
2375 LOGINT;
2376 }
2377 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002378 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002379 } else {
2380 /* useless to check return value, we don't know whether
2381 * there really wasn't any default defined or it just hasn't
2382 * been resolved, we just hope for the best :)
2383 */
2384 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002385 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002386 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002387 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002388 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002389 break;
2390
2391 case LYS_LEAF:
Michal Vaskob84f88a2015-09-24 13:16:10 +02002392 if (lys_type_dup(module, node->parent, &(leaf->type), &(leaf_orig->type), unres)) {
2393 goto error;
2394 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002395 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2396
2397 if (leaf_orig->dflt) {
2398 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci48464ed2016-03-17 15:44:09 +01002399 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002400 goto error;
2401 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002402 }
2403
2404 leaf->must_size = leaf_orig->must_size;
2405 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002406
2407 if (leaf_orig->when) {
2408 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002409 }
2410 break;
2411
2412 case LYS_LEAFLIST:
Michal Vaskob84f88a2015-09-24 13:16:10 +02002413 if (lys_type_dup(module, node->parent, &(llist->type), &(llist_orig->type), unres)) {
2414 goto error;
2415 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002416 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2417
2418 llist->min = llist_orig->min;
2419 llist->max = llist_orig->max;
2420
2421 llist->must_size = llist_orig->must_size;
2422 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002423
2424 if (llist_orig->when) {
2425 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002426 }
2427 break;
2428
2429 case LYS_LIST:
2430 list->min = list_orig->min;
2431 list->max = list_orig->max;
2432
2433 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002434 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002435
Radek Krejci581ce772015-11-10 17:22:40 +01002436 list->tpdf_size = list_orig->tpdf_size;
Michal Vasko0ea41032015-06-16 08:53:55 +02002437 list->tpdf = lys_tpdf_dup(module, node->parent, list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002438
Radek Krejci581ce772015-11-10 17:22:40 +01002439 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002440 if (list->keys_size) {
2441 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01002442 if (!list->keys) {
2443 LOGMEM;
2444 goto error;
2445 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002446
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002447 if (!shallow) {
2448 /* we managed to resolve it before, resolve it again manually */
2449 if (list_orig->keys[0]) {
2450 for (i = 0; i < list->keys_size; ++i) {
2451 rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF,
2452 (const struct lys_node **)&list->keys[i]);
2453 if (rc) {
2454 if (rc == EXIT_FAILURE) {
2455 LOGINT;
2456 }
2457 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002458 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002459 }
2460 /* it was not resolved yet, add unres copy */
2461 } else {
2462 if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) {
2463 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002464 goto error;
2465 }
Michal Vasko38d01f72015-06-15 09:41:06 +02002466 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002467 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002468 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02002469 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002470 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002471
Radek Krejci581ce772015-11-10 17:22:40 +01002472 list->unique_size = list_orig->unique_size;
2473 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01002474 if (!list->unique) {
2475 LOGMEM;
2476 goto error;
2477 }
Radek Krejci581ce772015-11-10 17:22:40 +01002478 for (i = 0; i < list->unique_size; ++i) {
2479 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2480 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01002481 if (!list->unique[i].expr) {
2482 LOGMEM;
2483 goto error;
2484 }
Radek Krejci581ce772015-11-10 17:22:40 +01002485 for (j = 0; j < list->unique[i].expr_size; j++) {
2486 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
2487
2488 /* if it stays in unres list, duplicate it also there */
Michal Vasko0bd29d12015-08-19 11:45:49 +02002489 unres_schema_dup(module, unres, &list_orig->unique[i], UNRES_LIST_UNIQ, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002490 }
2491 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002492
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002493 if (list_orig->when) {
2494 list->when = lys_when_dup(ctx, list_orig->when);
Radek Krejciefaeba32015-05-27 14:30:57 +02002495 }
Radek Krejcidce51452015-06-16 15:20:08 +02002496 break;
2497
2498 case LYS_ANYXML:
2499 anyxml->must_size = anyxml_orig->must_size;
2500 anyxml->must = lys_restr_dup(ctx, anyxml_orig->must, anyxml->must_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002501
2502 if (anyxml_orig->when) {
2503 anyxml->when = lys_when_dup(ctx, anyxml_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002504 }
2505 break;
2506
2507 case LYS_USES:
2508 uses->grp = uses_orig->grp;
2509
2510 if (uses_orig->when) {
2511 uses->when = lys_when_dup(ctx, uses_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002512 }
2513
2514 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02002515 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002516 uses->augment_size = uses_orig->augment_size;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002517 if (!shallow) {
2518 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
2519 if (!uses->child) {
Radek Krejci48464ed2016-03-17 15:44:09 +01002520 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002521 goto error;
2522 }
Michal Vasko49168a22015-08-17 16:35:41 +02002523 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002524 } else {
2525 memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002526 }
2527 break;
2528
Radek Krejcidce51452015-06-16 15:20:08 +02002529 case LYS_CASE:
2530 if (cs_orig->when) {
2531 cs->when = lys_when_dup(ctx, cs_orig->when);
Radek Krejcidce51452015-06-16 15:20:08 +02002532 }
2533 break;
2534
2535 case LYS_GROUPING:
2536 grp->tpdf_size = grp_orig->tpdf_size;
2537 grp->tpdf = lys_tpdf_dup(module, node->parent, grp_orig->tpdf, grp->tpdf_size, unres);
2538 break;
2539
2540 case LYS_RPC:
2541 rpc->tpdf_size = rpc_orig->tpdf_size;
2542 rpc->tpdf = lys_tpdf_dup(module, node->parent, rpc_orig->tpdf, rpc->tpdf_size, unres);
2543 break;
2544
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002545 case LYS_INPUT:
2546 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02002547 io->tpdf_size = io_orig->tpdf_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002548 io->tpdf = lys_tpdf_dup(module, node->parent, io_orig->tpdf, io->tpdf_size, unres);
2549 break;
2550
Radek Krejcida04f4a2015-05-21 12:54:09 +02002551 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002552 ntf->tpdf_size = ntf_orig->tpdf_size;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002553 ntf->tpdf = lys_tpdf_dup(module, node->parent, ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejci7e97c352015-06-19 16:26:34 +02002554 break;
2555
2556 default:
2557 /* LY_NODE_AUGMENT */
2558 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002559 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002560 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002561
2562 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002563
2564error:
2565
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002566 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02002567 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002568}
2569
Michal Vasko13b15832015-08-19 11:04:48 +02002570void
Michal Vaskoff006c12016-02-17 11:15:19 +01002571lys_node_switch(struct lys_node *dst, struct lys_node *src)
2572{
2573 struct lys_node *child;
2574
2575 assert((dst->module == src->module) && (dst->name == src->name) && (dst->nodetype == src->nodetype));
2576
2577 /* sibling next */
2578 if (dst->prev != dst) {
2579 dst->prev->next = src;
2580 }
2581
2582 /* sibling prev */
2583 if (dst->next) {
2584 dst->next->prev = src;
2585 }
2586
2587 /* parent child prev */
2588 if (!dst->next && dst->parent) {
2589 dst->parent->child->prev = src;
2590 }
2591
2592 /* next */
2593 src->next = dst->next;
2594 dst->next = NULL;
2595
2596 /* prev */
2597 if (dst->prev != dst) {
2598 src->prev = dst->prev;
2599 }
2600 dst->prev = dst;
2601
2602 /* parent child */
2603 if (dst->parent && (dst->parent->child == dst)) {
2604 dst->parent->child = src;
2605 }
2606
2607 /* parent */
2608 src->parent = dst->parent;
2609 dst->parent = NULL;
2610
2611 /* child parent */
2612 LY_TREE_FOR(dst->child, child) {
2613 if (child->parent == dst) {
2614 child->parent = src;
2615 }
2616 }
2617
2618 /* child */
2619 src->child = dst->child;
2620 dst->child = NULL;
2621}
2622
2623void
Michal Vasko627975a2016-02-11 11:39:03 +01002624lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int remove_from_ctx)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002625{
2626 struct ly_ctx *ctx;
2627 int i;
2628
2629 if (!module) {
2630 return;
2631 }
2632
2633 /* remove schema from the context */
2634 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01002635 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02002636 for (i = 0; i < ctx->models.used; i++) {
2637 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01002638 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02002639 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01002640 memmove(&ctx->models.list[i], ctx->models.list[i + 1], (ctx->models.used - i) * sizeof *ctx->models.list);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002641 ctx->models.list[ctx->models.used] = NULL;
2642 /* we are done */
2643 break;
2644 }
2645 }
2646 }
2647
2648 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01002649 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002650
2651 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01002652 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002653
2654 free(module);
2655}
Radek Krejci7e97c352015-06-19 16:26:34 +02002656
2657/*
2658 * op: 1 - enable, 0 - disable
2659 */
2660static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002661lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002662{
2663 int all = 0;
2664 int i, j, k;
2665
2666 if (!module || !name || !strlen(name)) {
2667 return EXIT_FAILURE;
2668 }
2669
2670 if (!strcmp(name, "*")) {
2671 /* enable all */
2672 all = 1;
2673 }
2674
2675 /* module itself */
2676 for (i = 0; i < module->features_size; i++) {
2677 if (all || !strcmp(module->features[i].name, name)) {
2678 if (op) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002679 module->features[i].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002680 /* enable referenced features (recursion) */
2681 for (k = 0; k < module->features[i].features_size; k++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002682 lys_features_change(module->features[i].features[k]->module,
Radek Krejci7e97c352015-06-19 16:26:34 +02002683 module->features[i].features[k]->name, op);
2684 }
2685 } else {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002686 module->features[i].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002687 }
2688 if (!all) {
2689 return EXIT_SUCCESS;
2690 }
2691 }
2692 }
2693
2694 /* submodules */
2695 for (j = 0; j < module->inc_size; j++) {
2696 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2697 if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) {
2698 if (op) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002699 module->inc[j].submodule->features[i].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002700 } else {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002701 module->inc[j].submodule->features[i].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002702 }
2703 if (!all) {
2704 return EXIT_SUCCESS;
2705 }
2706 }
2707 }
2708 }
2709
2710 if (all) {
2711 return EXIT_SUCCESS;
2712 } else {
2713 return EXIT_FAILURE;
2714 }
2715}
2716
2717API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002718lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002719{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002720 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002721}
2722
2723API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002724lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002725{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002726 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002727}
2728
2729API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002730lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002731{
2732 int i, j;
2733
2734 if (!module || !feature) {
2735 return -1;
2736 }
2737
2738 /* search for the specified feature */
2739 /* module itself */
2740 for (i = 0; i < module->features_size; i++) {
2741 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002742 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002743 return 1;
2744 } else {
2745 return 0;
2746 }
2747 }
2748 }
2749
2750 /* submodules */
2751 for (j = 0; j < module->inc_size; j++) {
2752 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2753 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002754 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002755 return 1;
2756 } else {
2757 return 0;
2758 }
2759 }
2760 }
2761 }
2762
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002763 /* feature definition not found */
2764 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02002765}
Michal Vasko2367e7c2015-07-07 11:33:44 +02002766
Radek Krejci96a10da2015-07-30 11:00:14 +02002767API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01002768lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02002769{
Radek Krejci96a10da2015-07-30 11:00:14 +02002770 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002771 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002772 unsigned int count;
2773
2774 if (!module) {
2775 return NULL;
2776 }
2777
2778 count = module->features_size;
2779 for (i = 0; i < module->inc_size; i++) {
2780 count += module->inc[i].submodule->features_size;
2781 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002782 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01002783 if (!result) {
2784 LOGMEM;
2785 return NULL;
2786 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002787 if (states) {
2788 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01002789 if (!(*states)) {
2790 LOGMEM;
2791 free(result);
2792 return NULL;
2793 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002794 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002795 count = 0;
2796
2797 /* module itself */
2798 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002799 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002800 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002801 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002802 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002803 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002804 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002805 }
2806 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002807 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002808 }
2809
2810 /* submodules */
2811 for (j = 0; j < module->inc_size; j++) {
2812 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002813 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02002814 if (states) {
2815 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
2816 (*states)[count] = 1;
2817 } else {
2818 (*states)[count] = 0;
2819 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002820 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002821 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002822 }
2823 }
2824
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002825 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02002826 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002827
2828 return result;
2829}
Michal Vaskobaefb032015-09-24 14:52:10 +02002830
Radek Krejci6910a032016-04-13 10:06:21 +02002831API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01002832lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01002833{
2834 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
2835}
2836
Radek Krejci6910a032016-04-13 10:06:21 +02002837API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02002838lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01002839{
2840 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
2841}
2842
Michal Vaskobaefb032015-09-24 14:52:10 +02002843API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01002844lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02002845{
2846 if (!node || !node->parent) {
2847 return NULL;
2848 }
2849
2850 if (node->parent->nodetype == LYS_AUGMENT) {
2851 return ((struct lys_node_augment *)node->parent)->target;
2852 }
2853
2854 return node->parent;
2855}
Michal Vasko1b229152016-01-13 11:28:38 +01002856
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002857API void *
Michal Vasko1b229152016-01-13 11:28:38 +01002858lys_set_private(const struct lys_node *node, void *priv)
2859{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002860 void *prev;
2861
Michal Vasko1b229152016-01-13 11:28:38 +01002862 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002863 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
2864 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01002865 }
2866
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002867 prev = node->priv;
2868 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002869
2870 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01002871}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002872
2873static void
2874lys_switch_deviation(struct lys_deviation *dev, struct lys_module *dev_module)
2875{
2876 int ret;
2877 char *parent_path;
2878 struct lys_node *target;
2879 const struct lys_module *target_module;
2880
2881 target_module = lys_get_import_module(dev_module, NULL, 0, dev->target_name + 1,
2882 strcspn(dev->target_name, ":") - 1);
2883
2884 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
2885 if (dev->orig_node) {
2886 /* removing not-supported deviation ... */
2887 if (strrchr(dev->target_name, '/') != dev->target_name) {
2888 /* ... from a parent */
2889 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
2890
2891 target = NULL;
2892 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target);
2893 free(parent_path);
2894 if (ret || !target) {
2895 LOGINT;
2896 return;
2897 }
2898
2899 lys_node_addchild(target, NULL, dev->orig_node);
2900 } else {
2901 /* ... from top-level data */
2902 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
2903 }
2904
2905 dev->orig_node = NULL;
2906 } else {
2907 /* adding not-supported deviation */
2908 target = NULL;
2909 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
2910 if (ret || !target) {
2911 LOGINT;
2912 return;
2913 }
2914
2915 lys_node_unlink(target);
2916 dev->orig_node = target;
2917 }
2918 } else {
2919 target = NULL;
2920 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
2921 if (ret || !target) {
2922 LOGINT;
2923 return;
2924 }
2925
2926 lys_node_switch(target, dev->orig_node);
2927 dev->orig_node = target;
2928 }
2929}
2930
2931void
2932lys_deviation_add_ext_imports(struct lys_module *dev_target_module, struct lys_module *dev_module)
2933{
2934 int i, j;
2935
2936 /* mark the target module as deviated */
2937 dev_target_module->deviated = 1;
2938
2939 /* copy our imports to the deviated module (deviations may need them to work) */
2940 for (i = 0; i < dev_module->imp_size; ++i) {
2941 for (j = 0; j < dev_target_module->imp_size; ++j) {
2942 if (dev_module->imp[i].module == dev_target_module->imp[j].module) {
2943 break;
2944 }
2945 }
2946
2947 if (j < dev_target_module->imp_size) {
2948 /* import is already there */
2949 continue;
2950 }
2951
2952 /* copy the import, mark it as external */
2953 ++dev_target_module->imp_size;
2954 dev_target_module->imp = ly_realloc(dev_target_module->imp, dev_target_module->imp_size * sizeof *dev_target_module->imp);
2955 if (!dev_target_module->imp) {
2956 LOGMEM;
2957 return;
2958 }
2959 dev_target_module->imp[dev_target_module->imp_size - 1].module = dev_module->imp[i].module;
2960 dev_target_module->imp[dev_target_module->imp_size - 1].prefix = lydict_insert(dev_module->ctx, dev_module->imp[i].prefix, 0);
2961 memcpy(dev_target_module->imp[dev_target_module->imp_size - 1].rev, dev_module->imp[i].rev, LY_REV_SIZE);
2962 dev_target_module->imp[dev_target_module->imp_size - 1].external = 1;
2963 }
2964
2965 /* copy ourselves to the deviated module as a special import (if we haven't yet, there could be more deviations of the same module) */
2966 for (i = 0; i < dev_target_module->imp_size; ++i) {
2967 if (dev_target_module->imp[i].module == dev_module) {
2968 break;
2969 }
2970 }
2971
2972 if (i == dev_target_module->imp_size) {
2973 ++dev_target_module->imp_size;
2974 dev_target_module->imp = ly_realloc(dev_target_module->imp, dev_target_module->imp_size * sizeof *dev_target_module->imp);
2975 if (!dev_target_module->imp) {
2976 LOGMEM;
2977 return;
2978 }
2979 dev_target_module->imp[dev_target_module->imp_size - 1].module = dev_module;
2980 dev_target_module->imp[dev_target_module->imp_size - 1].prefix = lydict_insert(dev_module->ctx, dev_module->prefix, 0);
2981 if (dev_module->rev_size) {
2982 memcpy(dev_target_module->imp[dev_target_module->imp_size - 1].rev, dev_module->rev[0].date, LY_REV_SIZE);
2983 } else {
2984 memset(dev_target_module->imp[dev_target_module->imp_size - 1].rev, 0, LY_REV_SIZE);
2985 }
2986 dev_target_module->imp[dev_target_module->imp_size - 1].external = 2;
2987 } else {
2988 /* it could have been added by another deviating module that imported this deviating module */
2989 dev_target_module->imp[i].external = 2;
2990 }
2991}
2992
2993/* temporarily removes or applies deviations, updates module deviation flag accordingly */
2994void
2995lys_switch_deviations(struct lys_module *module)
2996{
2997 uint8_t i, j, changes = 0;
2998
2999 for (i = 0; i < module->imp_size; ++i) {
3000 if (module->imp[i].external == 2) {
3001 for (j = 0; j < module->imp[i].module->deviation_size; ++j) {
3002 lys_switch_deviation(&module->imp[i].module->deviation[j], module->imp[i].module);
3003 }
3004
3005 changes = 1;
3006 }
3007 }
3008
3009 if (changes) {
3010 if (module->deviated) {
3011 module->deviated = 0;
3012 } else {
3013 module->deviated = 1;
3014 }
3015 }
3016}
3017
3018/* not needed currently, but tested and working */
3019#if 0
3020
3021void
3022lys_sub_module_apply_devs_augs(struct lys_module *module)
3023{
3024 int i;
3025 struct lys_node_augment *aug;
3026 struct lys_node *last;
3027
3028 /* re-apply deviations */
3029 for (i = 0; i < module->deviation_size; ++i) {
3030 lys_switch_deviation(&module->deviation[i], module);
3031 assert(module->deviation[i].orig_node);
3032 lys_deviation_add_ext_imports(lys_node_module(module->deviation[i].orig_node), module);
3033 }
3034
3035 /* re-apply augments */
3036 for (i = 0; i < module->augment_size; ++i) {
3037 aug = &module->augment[i];
3038 assert(aug->target);
3039
3040 /* reconnect augmenting data into the target - add them to the target child list */
3041 if (aug->target->child) {
3042 last = aug->target->child->prev;
3043 last->next = aug->child;
3044 aug->target->child->prev = aug->child->prev;
3045 aug->child->prev = last;
3046 } else {
3047 aug->target->child = aug->child;
3048 }
3049 }
3050}
3051
3052#endif
3053
3054void
3055lys_sub_module_remove_devs_augs(struct lys_module *module)
3056{
3057 int i, j, flag;
3058 struct lys_node *last, *elem;
3059 struct lys_module *orig_mod;
3060
3061 /* remove applied deviations */
3062 for (i = 0; i < module->deviation_size; ++i) {
3063 lys_switch_deviation(&module->deviation[i], module);
3064
3065 /* remove our deviation import, clear deviated flag is possible */
3066 orig_mod = lys_node_module(module->deviation[i].orig_node);
3067 flag = 0;
3068 for (j = 0; j < orig_mod->imp_size; ++j) {
3069 if (orig_mod->imp[j].external == 2) {
3070 if (orig_mod->imp[j].module == lys_main_module(module)) {
3071 /* our deviation import, remove it */
3072 --orig_mod->imp_size;
3073 if (j < orig_mod->imp_size) {
3074 memcpy(&orig_mod->imp[j], &orig_mod->imp[j + 1], (orig_mod->imp_size - j) * sizeof *orig_mod->imp);
3075 }
3076 --j;
3077 } else {
3078 /* some other deviation, we cannot clear the deviated flag */
3079 flag = 1;
3080 }
3081 }
3082 }
3083 if (!flag) {
3084 /* it's safe to clear the deviated flag */
3085 orig_mod->deviated = 0;
3086 }
3087 }
3088
3089 /* remove applied augments */
3090 for (i = 0; i < module->augment_size; ++i) {
3091 assert(module->augment[i].target);
3092
3093 elem = module->augment[i].child;
3094 if (elem) {
3095 LY_TREE_FOR(elem, last) {
3096 if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) {
3097 break;
3098 }
3099 }
3100 /* elem is first augment child, last is the last child */
3101
3102 /* parent child ptr */
3103 if (module->augment[i].target->child == elem) {
3104 module->augment[i].target->child = last->next;
3105 }
3106
3107 /* parent child next ptr */
3108 if (elem->prev->next) {
3109 elem->prev->next = last->next;
3110 }
3111
3112 /* parent child prev ptr */
3113 if (last->next) {
3114 last->next->prev = elem->prev;
3115 } else if (module->augment[i].target->child) {
3116 module->augment[i].target->child->prev = elem->prev;
3117 }
3118
3119 /* update augment children themselves */
3120 elem->prev = last;
3121 last->next = NULL;
3122 }
3123 }
3124}
3125
3126void
3127lys_sub_module_set_dev_aug_target_implement(struct lys_module *module)
3128{
3129 int i;
3130 struct lys_module *trg_mod;
3131
3132 for (i = 0; i < module->deviation_size; ++i) {
3133 assert(module->deviation[i].orig_node);
3134 trg_mod = lys_node_module(module->deviation[i].orig_node);
3135 if (!trg_mod->implemented) {
3136 trg_mod->implemented = 1;
3137 }
3138 }
3139
3140 for (i = 0; i < module->augment_size; ++i) {
3141 assert(module->augment[i].target);
3142 trg_mod = lys_node_module(module->augment[i].target);
3143 if (!trg_mod->implemented) {
3144 trg_mod->implemented = 1;
3145 }
3146 }
3147}
3148
3149void
3150lys_submodule_module_data_free(struct lys_submodule *submodule)
3151{
3152 struct lys_node *next, *elem;
3153
3154 /* remove parsed data */
3155 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
3156 if (elem->module == (struct lys_module *)submodule) {
3157 lys_node_free(elem, NULL, 0);
3158 }
3159 }
3160}