blob: 2810dce9a8874de3e4f006e3079ac5e46f84cd8d [file] [log] [blame]
Radek Krejcib1c12512015-08-11 11:22:04 +02001/**
2 * @file validation.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Data tree validation functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 */
21
Radek Krejcieab784a2015-08-27 09:56:53 +020022#include <assert.h>
Radek Krejcib1c12512015-08-11 11:22:04 +020023#include <stdlib.h>
Michal Vaskocf024702015-10-08 15:01:42 +020024#include <string.h>
Radek Krejcib1c12512015-08-11 11:22:04 +020025
Radek Krejcieab784a2015-08-27 09:56:53 +020026#include "common.h"
Michal Vaskocf024702015-10-08 15:01:42 +020027#include "validation.h"
Radek Krejcib1c12512015-08-11 11:22:04 +020028#include "libyang.h"
Michal Vaskocf024702015-10-08 15:01:42 +020029#include "xpath.h"
Radek Krejcicf509982015-12-15 09:22:44 +010030#include "parser.h"
Michal Vaskocf024702015-10-08 15:01:42 +020031#include "resolve.h"
32#include "tree_internal.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020033#include "xml_internal.h"
Radek Krejcib1c12512015-08-11 11:22:04 +020034
Radek Krejcieab784a2015-08-27 09:56:53 +020035static struct lys_node_leaf *
Michal Vasko1e62a092015-12-01 12:27:20 +010036lyv_keys_present(const struct lyd_node *list)
Radek Krejcib1c12512015-08-11 11:22:04 +020037{
38 struct lyd_node *aux;
39 struct lys_node_list *schema;
40 int i;
41
42 schema = (struct lys_node_list *)list->schema;
43
44 for (i = 0; i < schema->keys_size; i++) {
45 for (aux = list->child; aux; aux = aux->next) {
46 if (aux->schema == (struct lys_node *)schema->keys[i]) {
47 break;
48 }
49 }
50 if (!aux) {
51 /* key not found in the data */
Radek Krejci1073cc02015-08-12 20:37:39 +020052 return schema->keys[i];
Radek Krejcib1c12512015-08-11 11:22:04 +020053 }
54 }
55
56 return EXIT_SUCCESS;
57}
Radek Krejcieab784a2015-08-27 09:56:53 +020058
Radek Krejci3e0addb2015-08-27 16:37:59 +020059/**
60 * @brief Compare filter nodes
61 *
62 * @param[in] first The first data node to compare
63 * @param[in] second The second node to compare
64 * @return 0 if both filter nodes selects the same data.
65 */
66static int
Michal Vasko1e62a092015-12-01 12:27:20 +010067filter_compare(const struct lyd_node *first, const struct lyd_node *second)
Radek Krejci3e0addb2015-08-27 16:37:59 +020068{
69 struct lyd_node *diter1, *diter2;
70 int match, c1, c2;
71
72 assert(first);
73 assert(second);
74
75 if (first->schema != second->schema) {
76 return 1;
77 }
78
79
80 switch (first->schema->nodetype) {
81 case LYS_CONTAINER:
82 case LYS_LIST:
83 /* check if all the content match nodes are the same */
84 c1 = 0;
85 LY_TREE_FOR(first->child, diter1) {
86 if (!(diter1->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
87 continue;
Michal Vasko4c183312015-09-25 10:41:47 +020088 } else if (!((struct lyd_node_leaf_list *)diter1)->value_str) {
Radek Krejci3e0addb2015-08-27 16:37:59 +020089 /* selection node */
90 continue;
91 }
92
93 match = 0;
94 LY_TREE_FOR(second->child, diter2) {
95 if (diter2->schema != diter1->schema) {
96 continue;
Michal Vasko4c183312015-09-25 10:41:47 +020097 } else if (((struct lyd_node_leaf_list *)diter1)->value_str !=
98 ((struct lyd_node_leaf_list *)diter2)->value_str) {
Radek Krejci3e0addb2015-08-27 16:37:59 +020099 continue;
100 }
101 match = 1;
102 c1++;
103 }
104 if (!match) {
105 return 1;
106 }
107 }
108 /* get number of content match nodes in the second to get know if there are some
109 * that are not present in first
110 */
111 c2 = 0;
112 LY_TREE_FOR(second->child, diter2) {
113 if (!(diter2->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
114 continue;
Michal Vasko4c183312015-09-25 10:41:47 +0200115 } else if (!((struct lyd_node_leaf_list *)diter2)->value_str) {
Radek Krejci3e0addb2015-08-27 16:37:59 +0200116 /* selection node */
117 continue;
118 }
119 c2++;
120 }
121 if (c1 != c2) {
122 return 1;
123 }
124 break;
125 case LYS_LEAF:
126 case LYS_LEAFLIST:
Michal Vasko4c183312015-09-25 10:41:47 +0200127 if (((struct lyd_node_leaf_list *)first)->value_str != ((struct lyd_node_leaf_list *)second)->value_str) {
Radek Krejci3e0addb2015-08-27 16:37:59 +0200128 return 1;
129 }
130 break;
131 default:
132 /* no more tests are needed */
133 break;
134 }
135 return 0;
136}
137
138static int
139filter_merge(struct lyd_node *to, struct lyd_node *from)
140{
141 struct lyd_node *diter1, *diter2;
142 unsigned int i, j;
143 struct lyd_set *s1 = NULL, *s2 = NULL;
144 int copy;
Radek Krejci488fd542016-01-07 12:54:08 +0100145 int ret = EXIT_FAILURE;
Radek Krejci3e0addb2015-08-27 16:37:59 +0200146
147 if (!to || !from || to->schema != from->schema) {
148 ly_errno = LY_EINVAL;
149 return EXIT_FAILURE;
150 }
151
152 switch(to->schema->nodetype) {
153 case LYS_LIST:
154 case LYS_CONTAINER:
155 if (!from->child) {
156 /* from is selection node, so we want to make the to selection node now */
157 while (to->child) {
158 lyd_free(to->child);
159 }
160 } else if (to->child) {
161 /* both to and from are containment nodes and it was already checked
162 * (by calling filter_compare()) that they selects the same target.
163 * Therefore we can skip the content match nodes (they are the same in
164 * both of them) and merge only the selection and containment nodes */
165
166 /* first, get know if to and from contain some selection or containment
167 * nodes. Because if one of them does not contain any such a node it
168 * selects all the data so it does not make sense to limit it by any
169 * selection/containment node.
170 */
171 s1 = lyd_set_new();
172 s2 = lyd_set_new();
Radek Krejci488fd542016-01-07 12:54:08 +0100173 if (!s1 || !s2) {
174 LOGMEM;
175 goto cleanup;
176 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200177 LY_TREE_FOR(to->child, diter1) {
178 /* is selection node */
Michal Vasko4c183312015-09-25 10:41:47 +0200179 if ((diter1->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
180 && !((struct lyd_node_leaf_list *)diter1)->value_str) {
Radek Krejci488fd542016-01-07 12:54:08 +0100181 if (lyd_set_add(s1, diter1)) {
182 goto cleanup;
183 }
Michal Vasko17cc7062015-12-10 14:31:48 +0100184 } else if ((diter1->schema->nodetype == LYS_ANYXML) && !((struct lyd_node_anyxml *)diter1)->value->child) {
Radek Krejci488fd542016-01-07 12:54:08 +0100185 if (lyd_set_add(s1, diter1)) {
186 goto cleanup;
187 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200188 } else if (diter1->schema->nodetype & (LYS_CONTAINER | LYS_LIST)) {
189 /* or containment node */
Radek Krejci488fd542016-01-07 12:54:08 +0100190 if (lyd_set_add(s1, diter1)) {
191 goto cleanup;
192 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200193 }
194 }
195
196 LY_TREE_FOR(from->child, diter2) {
197 /* is selection node */
Michal Vasko4c183312015-09-25 10:41:47 +0200198 if ((diter2->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
199 && !((struct lyd_node_leaf_list *)diter2)->value_str) {
Radek Krejci488fd542016-01-07 12:54:08 +0100200 if (lyd_set_add(s2, diter2)) {
201 goto cleanup;
202 }
Michal Vasko17cc7062015-12-10 14:31:48 +0100203 } else if ((diter2->schema->nodetype == LYS_ANYXML) && !((struct lyd_node_anyxml *)diter2)->value->child) {
Radek Krejci488fd542016-01-07 12:54:08 +0100204 if (lyd_set_add(s2, diter2)) {
205 goto cleanup;
206 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200207 } else if (diter2->schema->nodetype & (LYS_CONTAINER | LYS_LIST)) {
208 /* or containment node */
Radek Krejci488fd542016-01-07 12:54:08 +0100209 if (lyd_set_add(s2, diter2)) {
210 goto cleanup;
211 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200212 }
213 }
214
215 if (!s1->number) {
216 /* to already selects all content, so nothing is needed */
217 break;
218 } else if (!s2->number) {
219 /* from selects all content, so make to select it too by
220 * removing all selection and containment nodes
221 */
222 for (i = 0; i < s1->number; i++) {
223 lyd_free(s1->set[i]);
224 }
225 break;
226 } else {
227 /* both contain some selection or containment node(s), so merge them */
228 for (j = 0; j < s2->number; j++) { /* from */
229 copy = 0;
230 for (i = 0; i < s1->number; i++) { /* to */
231 if (s1->set[i]->schema != s2->set[j]->schema) {
232 continue;
233 }
234
235 /* we have something similar to diter1, explore it more */
236 switch (s2->set[j]->schema->nodetype) {
237 case LYS_LIST:
238 case LYS_CONTAINER:
239 if (!filter_compare(s2->set[j], s1->set[i])) {
240 /* merge the two containers into the to */
241 filter_merge(s1->set[i], s2->set[j]);
242 } else {
243 /* check that some of them is not a selection node */
244 if (!s2->set[j]->child) {
245 /* from is selection node, so keep only it because to selects subset */
246 lyd_free(s1->set[i]);
247 /* set the flag to copy the from child at the end */
248 copy = 1;
249 continue;
250 } else if (!s1->set[i]->child) {
251 /* to is already selection node, so ignore the from child */
252 } else {
253 /* they are different so keep trying to search for some other matching instance */
254 continue;
255 }
256 }
257
258 break;
259 case LYS_ANYXML:
260 case LYS_LEAFLIST:
261 case LYS_LEAF:
262 /* here it can be only a selection node, so do not duplicate it (keep i < s1->number) */
263 break;
264 default:
265 /* keep compiler silent */
266 break;
267 }
268
269 /* we have a match, so do not duplicate the current from child and go to check next from child */
270 /* i < s1->number */
271 break;
272 }
273
274 if (copy || i == s1->number) {
275 /* the node is not yet present in to, so move it there */
276 lyd_unlink(s2->set[j]);
277 if (to->child) {
278 to->child->prev->next = s2->set[j];
279 s2->set[j]->prev = to->child->prev;
280 to->child->prev = s2->set[j];
281 } else {
282 to->child = s2->set[j];
283 }
284 s2->set[j]->parent = to;
285 }
286 }
287 }
288 } /* else from is empty, so nothing to do */
289
290 break;
291
292 default:
293 /* no other type needed to cover,
294 * keep the default branch to make compiler silent */
295 break;
296 }
Radek Krejci488fd542016-01-07 12:54:08 +0100297 ret = EXIT_SUCCESS;
Radek Krejci3e0addb2015-08-27 16:37:59 +0200298
Radek Krejci488fd542016-01-07 12:54:08 +0100299cleanup:
Radek Krejci3e0addb2015-08-27 16:37:59 +0200300 lyd_set_free(s1);
301 lyd_set_free(s2);
302
Radek Krejci488fd542016-01-07 12:54:08 +0100303 return ret;
Radek Krejci3e0addb2015-08-27 16:37:59 +0200304}
305
Radek Krejcieab784a2015-08-27 09:56:53 +0200306int
Michal Vasko1e62a092015-12-01 12:27:20 +0100307lyv_data_context(const struct lyd_node *node, int options, unsigned int line, struct unres_data *unres)
Radek Krejcieab784a2015-08-27 09:56:53 +0200308{
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100309 struct lyd_node *iter;
310 struct lys_node *siter = NULL;
311
Michal Vaskocf024702015-10-08 15:01:42 +0200312 assert(node);
Radek Krejcieab784a2015-08-27 09:56:53 +0200313
314 /* check if the node instance is enabled by if-feature */
Michal Vaskocf024702015-10-08 15:01:42 +0200315 if (lys_is_disabled(node->schema, 2)) {
316 LOGVAL(LYE_INELEM, line, node->schema->name);
317 return EXIT_FAILURE;
318 }
319
320 /* check all relevant when conditions */
Michal Vasko0d182ba2015-10-09 09:29:14 +0200321 if (unres) {
Michal Vasko1e62a092015-12-01 12:27:20 +0100322 if (unres_data_add(unres, (struct lyd_node *)node, UNRES_WHEN, line) == -1) {
Michal Vasko0d182ba2015-10-09 09:29:14 +0200323 return EXIT_FAILURE;
324 }
325 } else {
Michal Vasko1e62a092015-12-01 12:27:20 +0100326 if (resolve_unres_data_item((struct lyd_node *)node, UNRES_WHEN, 0, line)) {
Michal Vasko0d182ba2015-10-09 09:29:14 +0200327 return EXIT_FAILURE;
328 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200329 }
330
331 /* check for (non-)presence of status data in edit-config data */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100332 if ((options & (LYD_OPT_EDIT | LYD_OPT_GETCONFIG | LYD_OPT_CONFIG)) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocf024702015-10-08 15:01:42 +0200333 LOGVAL(LYE_INELEM, line, node->schema->name);
Radek Krejcieab784a2015-08-27 09:56:53 +0200334 return EXIT_FAILURE;
335 }
336
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100337 /* check elements order in case of RPC's input and output */
Radek Krejcica7efb72016-01-18 13:06:01 +0100338 if (node->validity && lyp_is_rpc(node->schema)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100339 siter = node->schema->prev;
340 for (iter = node->prev; iter->next; iter = iter->prev) {
341 while (siter->next) {
342 if (siter == iter->schema) {
343 break;
344 }
345 siter = siter->prev;
346 }
347
348 if (!siter->next) {
349 /* schema node of the node's predecessors not found in node's schema node predecessors
350 * so the elements are in wrong order */
351 LOGVAL(LYE_INORDER, line, node->schema->name, iter->schema->name);
352 return EXIT_FAILURE;
353 }
354 }
355 }
356
Radek Krejcieab784a2015-08-27 09:56:53 +0200357 return EXIT_SUCCESS;
358}
Michal Vaskocf024702015-10-08 15:01:42 +0200359
Radek Krejcieab784a2015-08-27 09:56:53 +0200360int
Michal Vaskocf024702015-10-08 15:01:42 +0200361lyv_data_content(struct lyd_node *node, int options, unsigned int line, struct unres_data *unres)
Radek Krejcieab784a2015-08-27 09:56:53 +0200362{
Michal Vasko1e62a092015-12-01 12:27:20 +0100363 const struct lys_node *schema, *siter;
364 const struct lys_node *cs, *ch;
Radek Krejcieab784a2015-08-27 09:56:53 +0200365 struct lyd_node *diter, *start;
Radek Krejcicf509982015-12-15 09:22:44 +0100366 struct lys_ident *ident;
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100367 struct lys_tpdf *tpdf;
Radek Krejcieab784a2015-08-27 09:56:53 +0200368
369 assert(node);
370 assert(node->schema);
371
372 schema = node->schema; /* shortcut */
373
Radek Krejcica7efb72016-01-18 13:06:01 +0100374 if (node->validity) {
375 /* check presence of all keys in case of list */
376 if (schema->nodetype == LYS_LIST && !(options & (LYD_OPT_FILTER | LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
377 siter = (struct lys_node *)lyv_keys_present(node);
378 if (siter) {
379 /* key not found in the data */
380 LOGVAL(LYE_MISSELEM, line, siter->name, schema->name);
381 return EXIT_FAILURE;
Radek Krejcieab784a2015-08-27 09:56:53 +0200382 }
383 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200384
Radek Krejcica7efb72016-01-18 13:06:01 +0100385 /* mandatory children */
386 if ((schema->nodetype & (LYS_CONTAINER | LYS_LIST))
387 && !(options & (LYD_OPT_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
388 siter = ly_check_mandatory(node);
389 if (siter) {
390 if (siter->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
391 LOGVAL(LYE_SPEC, line, "Number of \"%s\" instances in \"%s\" does not follow min/max constraints.",
392 siter->name, siter->parent->name);
Radek Krejci3e0addb2015-08-27 16:37:59 +0200393 } else {
Radek Krejcica7efb72016-01-18 13:06:01 +0100394 LOGVAL(LYE_MISSELEM, line, siter->name, siter->parent->name);
Radek Krejci3e0addb2015-08-27 16:37:59 +0200395 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100396 return EXIT_FAILURE;
Radek Krejci3e0addb2015-08-27 16:37:59 +0200397 }
398 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200399
Radek Krejcica7efb72016-01-18 13:06:01 +0100400 /* get the first sibling */
401 if (node->parent) {
402 start = node->parent->child;
Radek Krejci27aaa732015-09-04 15:24:04 +0200403 } else {
Radek Krejcica7efb72016-01-18 13:06:01 +0100404 for (start = node; start->prev->next; start = start->prev);
405 }
406
407 /* check that there are no data from different choice case */
408 if (!(options & LYD_OPT_FILTER)) {
409 /* init loop condition */
410 ch = schema;
411
412 while (ch->parent && (ch->parent->nodetype & (LYS_CASE | LYS_CHOICE))) {
413 if (ch->parent->nodetype == LYS_CHOICE) {
414 cs = NULL;
415 ch = ch->parent;
416 } else { /* ch->parent->nodetype == LYS_CASE */
417 cs = ch->parent;
418 ch = ch->parent->parent;
Radek Krejci9fdfd792015-11-09 15:41:14 +0100419 }
420
Radek Krejcica7efb72016-01-18 13:06:01 +0100421 for (diter = start; diter; diter = diter->next) {
422 if (diter == node) {
423 continue;
424 }
425
426 /* find correct level to compare */
427 for (siter = diter->schema->parent; siter; siter = siter->parent) {
428 if (siter->nodetype == LYS_CHOICE) {
429 if (siter == ch) {
430 LOGVAL(LYE_MCASEDATA, line, ch->name);
431 return EXIT_FAILURE;
432 } else {
433 continue;
434 }
435 }
436
437 if (siter->nodetype == LYS_CASE) {
438 if (siter->parent != ch) {
439 continue;
440 } else if (!cs || cs != siter) {
441 LOGVAL(LYE_MCASEDATA, line, ch->name);
442 return EXIT_FAILURE;
443 }
444 }
445
446 /* diter is from something else choice (subtree) */
447 break;
448 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200449 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200450 }
451 }
Radek Krejci3e0addb2015-08-27 16:37:59 +0200452
Radek Krejcica7efb72016-01-18 13:06:01 +0100453 /* keep this check the last since in case of filter it affects the data and can modify the tree */
454 /* check number of instances (similar to list uniqueness) for non-list nodes */
455 if (schema->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYXML)) {
456 /* find duplicity */
457 for (diter = start; diter; diter = diter->next) {
458 if (diter->schema == schema && diter != node) {
459 if (options & LYD_OPT_FILTER) {
460 /* normalize the filter if needed */
461 switch (schema->nodetype) {
462 case LYS_CONTAINER:
463 if (!filter_compare(diter, node)) {
464 /* merge the two containers, diter will be kept ... */
465 filter_merge(diter, node);
466 /* ... and node will be removed (ly_errno is not set) */
467 return EXIT_FAILURE;
468 } else {
469 /* check that some of them is not a selection node */
470 if (!diter->child) {
471 /* keep diter since it selects all such containers
472 * and let remove the node since it selects just a subset */
473 return EXIT_FAILURE;
474 } else if (!node->child) {
475 /* keep the node and remove diter since it selects subset
476 * of what is selected by node */
477 lyd_free(diter);
478 }
479 /* keep them as they are */
480 return EXIT_SUCCESS;
481 }
482 break;
483 case LYS_LEAF:
484 if (!((struct lyd_node_leaf_list *)diter)->value_str
485 && ((struct lyd_node_leaf_list *)node)->value_str) {
486 /* the first instance is selection node but the new instance is content match node ->
487 * since content match node also works as selection node. keep only the new instance
488 */
489 lyd_free(diter);
490 /* return success to keep the node in the tree */
491 return EXIT_SUCCESS;
492 } else if (!((struct lyd_node_leaf_list *)node)->value_str
493 || ((struct lyd_node_leaf_list *)diter)->value_str ==
494 ((struct lyd_node_leaf_list *)node)->value_str) {
495 /* keep the previous instance and remove the current one ->
496 * return failure but do not set ly_errno */
497 return EXIT_FAILURE;
498 }
499 break;
500 case LYS_ANYXML:
501 /* filtering according to the anyxml content is not allowed,
502 * so anyxml is always a selection node with no content.
503 * Therefore multiple instances of anyxml does not make sense
504 */
505 /* failure is returned but no ly_errno is set */
506 return EXIT_FAILURE;
507 default:
508 /* not possible, but necessary to silence compiler warnings */
509 break;
510 }
511 /* we are done */
Radek Krejci27aaa732015-09-04 15:24:04 +0200512 break;
Radek Krejcica7efb72016-01-18 13:06:01 +0100513 } else {
514 LOGVAL(LYE_TOOMANY, line, schema->name, schema->parent ? schema->parent->name : "data tree");
Radek Krejci27aaa732015-09-04 15:24:04 +0200515 return EXIT_FAILURE;
516 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200517 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200518 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100519 } else if (schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
520 /* uniqueness of list/leaflist instances */
Radek Krejcieab784a2015-08-27 09:56:53 +0200521
Radek Krejcica7efb72016-01-18 13:06:01 +0100522 /* get the first list/leaflist instance sibling */
523 if (options & (LYD_OPT_GET | LYD_OPT_GETCONFIG)) {
524 /* skip key uniqueness check in case of get/get-config data */
525 start = NULL;
526 } else {
527 diter = start;
528 start = NULL;
529 while(diter) {
530 if (diter == node) {
531 diter = diter->next;
532 continue;
533 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100534
Radek Krejcica7efb72016-01-18 13:06:01 +0100535 if (diter->schema == node->schema) {
536 /* the same list instance */
537 start = diter;
538 break;
539 }
540 diter = diter->next;
541 }
542 }
543
544 /* check uniqueness of the list/leaflist instances (compare values) */
545 for (diter = start; diter; diter = diter->next) {
546 if (diter->schema != node->schema || diter == node ||
547 diter->validity) { /* skip comparison that will be done in future when checking diter as node */
548 continue;
549 }
550
551 if (options & LYD_OPT_FILTER) {
552 /* compare content match nodes */
553 if (!filter_compare(diter, node)) {
554 /* merge both nodes */
555 /* add selection and containment nodes from result into the diter,
556 * but only in case the diter already contains some selection nodes,
557 * otherwise it already will return all the data */
558 filter_merge(diter, node);
559
560 /* not the error, just return no data */
561 /* failure is returned but no ly_errno is set */
562 return EXIT_FAILURE;
563 } else if (node->schema->nodetype == LYS_LEAFLIST) {
564 /* in contrast to lists, leaflists can be still safely optimized if one of them
565 * is selection node. In that case wee need to keep the other node, which is content
566 * match node and it somehow limit the data to be filtered.
567 */
568 if (!((struct lyd_node_leaf_list *)diter)->value_str) {
569 /* the other instance is selection node, keep the new one whatever it is */
570 lyd_free(diter);
571 break;
572 } else if (!((struct lyd_node_leaf_list *)node)->value_str) {
573 /* the new instance is selection node, keep the previous instance which is
574 * content match node */
575 /* failure is returned but no ly_errno is set */
576 return EXIT_FAILURE;
577 }
578 }
579 } else if (!lyd_compare(diter, node, 1)) { /* comparing keys and unique combinations */
580 LOGVAL(LYE_DUPLIST, line, schema->name);
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100581 return EXIT_FAILURE;
582 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100583 }
584 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100585
586 /* status - of the node's schema node itself and all its parents that
587 * cannot have their own instance (like a choice statement) */
588 siter = node->schema;
589 do {
590 if (((siter->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) && (options & LYD_OPT_OBSOLETE)) {
591 LOGVAL(LYE_OBSDATA, line, node->schema->name);
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100592 return EXIT_FAILURE;
593 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100594 siter = siter->parent;
595 } while(siter && !(siter->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)));
596
597 /* status of the identity value */
598 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
599 if (options & LYD_OPT_OBSOLETE) {
600 /* check that we are not instantiating obsolete type */
601 tpdf = ((struct lys_node_leaf *)node->schema)->type.der;
602 while(tpdf) {
603 if ((tpdf->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) {
604 LOGVAL(LYE_OBSTYPE, line, node->schema->name, tpdf->name);
605 return EXIT_FAILURE;
606 }
607 tpdf = tpdf->type.der;
608 }
609 }
610 if (((struct lyd_node_leaf_list *)node)->value_type == LY_TYPE_IDENT) {
611 ident = ((struct lyd_node_leaf_list *)node)->value.ident;
612 if (check_status(schema->flags, schema->module, schema->name,
613 ident->flags, ident->module, ident->name, line)) {
614 return EXIT_FAILURE;
615 }
616 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100617 }
Radek Krejcicf509982015-12-15 09:22:44 +0100618 }
619
Michal Vaskobf19d252015-10-08 15:39:17 +0200620 /* check must conditions */
Michal Vasko0d182ba2015-10-09 09:29:14 +0200621 if (unres) {
622 if (unres_data_add(unres, node, UNRES_MUST, line) == -1) {
623 return EXIT_FAILURE;
624 }
625 } else {
626 if (resolve_unres_data_item(node, UNRES_MUST, 0, line)) {
627 return EXIT_FAILURE;
628 }
Michal Vaskobf19d252015-10-08 15:39:17 +0200629 }
630
Radek Krejcieab784a2015-08-27 09:56:53 +0200631 return EXIT_SUCCESS;
632}