/src/libyang/src/validation.c
Line | Count | Source |
1 | | /** |
2 | | * @file validation.c |
3 | | * @author Michal Vasko <mvasko@cesnet.cz> |
4 | | * @brief Validation |
5 | | * |
6 | | * Copyright (c) 2019 - 2021 CESNET, z.s.p.o. |
7 | | * |
8 | | * 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 |
11 | | * |
12 | | * https://opensource.org/licenses/BSD-3-Clause |
13 | | */ |
14 | | #define _GNU_SOURCE /* asprintf, strdup */ |
15 | | #include <sys/cdefs.h> |
16 | | |
17 | | #include "validation.h" |
18 | | |
19 | | #include <assert.h> |
20 | | #include <limits.h> |
21 | | #include <stdint.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include "common.h" |
27 | | #include "compat.h" |
28 | | #include "diff.h" |
29 | | #include "hash_table.h" |
30 | | #include "log.h" |
31 | | #include "parser_data.h" |
32 | | #include "parser_internal.h" |
33 | | #include "plugins_exts.h" |
34 | | #include "plugins_exts/metadata.h" |
35 | | #include "plugins_types.h" |
36 | | #include "set.h" |
37 | | #include "tree.h" |
38 | | #include "tree_data.h" |
39 | | #include "tree_data_internal.h" |
40 | | #include "tree_schema.h" |
41 | | #include "tree_schema_internal.h" |
42 | | #include "xpath.h" |
43 | | |
44 | | LY_ERR |
45 | | lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff) |
46 | 0 | { |
47 | 0 | LY_ERR ret = LY_SUCCESS; |
48 | 0 | struct lyd_node *new_diff = NULL; |
49 | 0 | const struct lyd_node *prev_inst; |
50 | 0 | char *key = NULL, *value = NULL, *position = NULL; |
51 | 0 | size_t buflen = 0, bufused = 0; |
52 | 0 | uint32_t pos; |
53 | |
|
54 | 0 | assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE)); |
55 | |
|
56 | 0 | if ((op == LYD_DIFF_OP_CREATE) && lysc_is_userordered(node->schema)) { |
57 | 0 | if (lysc_is_dup_inst_list(node->schema)) { |
58 | 0 | pos = lyd_list_pos(node); |
59 | | |
60 | | /* generate position meta */ |
61 | 0 | if (pos > 1) { |
62 | 0 | if (asprintf(&position, "%" PRIu32, pos - 1) == -1) { |
63 | 0 | LOGMEM(LYD_CTX(node)); |
64 | 0 | ret = LY_EMEM; |
65 | 0 | goto cleanup; |
66 | 0 | } |
67 | 0 | } else { |
68 | 0 | position = strdup(""); |
69 | 0 | LY_CHECK_ERR_GOTO(!position, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup); |
70 | 0 | } |
71 | 0 | } else { |
72 | 0 | if (node->prev->next && (node->prev->schema == node->schema)) { |
73 | 0 | prev_inst = node->prev; |
74 | 0 | } else { |
75 | | /* first instance */ |
76 | 0 | prev_inst = NULL; |
77 | 0 | } |
78 | |
|
79 | 0 | if (node->schema->nodetype == LYS_LIST) { |
80 | | /* generate key meta */ |
81 | 0 | if (prev_inst) { |
82 | 0 | LY_CHECK_GOTO(ret = lyd_path_list_predicate(prev_inst, &key, &buflen, &bufused, 0), cleanup); |
83 | 0 | } else { |
84 | 0 | key = strdup(""); |
85 | 0 | LY_CHECK_ERR_GOTO(!key, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup); |
86 | 0 | } |
87 | 0 | } else { |
88 | | /* generate value meta */ |
89 | 0 | if (prev_inst) { |
90 | 0 | value = strdup(lyd_get_value(prev_inst)); |
91 | 0 | LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup); |
92 | 0 | } else { |
93 | 0 | value = strdup(""); |
94 | 0 | LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup); |
95 | 0 | } |
96 | 0 | } |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | /* create new diff tree */ |
101 | 0 | LY_CHECK_GOTO(ret = lyd_diff_add(node, op, NULL, NULL, key, value, position, NULL, NULL, &new_diff), cleanup); |
102 | | |
103 | | /* merge into existing diff */ |
104 | 0 | ret = lyd_diff_merge_all(diff, new_diff, 0); |
105 | |
|
106 | 0 | cleanup: |
107 | 0 | lyd_free_tree(new_diff); |
108 | 0 | free(key); |
109 | 0 | free(value); |
110 | 0 | free(position); |
111 | 0 | return ret; |
112 | 0 | } |
113 | | |
114 | | /** |
115 | | * @brief Evaluate all relevant "when" conditions of a node. |
116 | | * |
117 | | * @param[in] tree Data tree. |
118 | | * @param[in] node Node whose relevant when conditions will be evaluated. |
119 | | * @param[in] schema Schema node of @p node. It may not be possible to use directly if @p node is opaque. |
120 | | * @param[out] disabled First when that evaluated false, if any. |
121 | | * @return LY_SUCCESS on success. |
122 | | * @return LY_EINCOMPLETE if a referenced node does not have its when evaluated. |
123 | | * @return LY_ERR value on error. |
124 | | */ |
125 | | static LY_ERR |
126 | | lyd_validate_node_when(const struct lyd_node *tree, const struct lyd_node *node, const struct lysc_node *schema, |
127 | | const struct lysc_when **disabled) |
128 | 0 | { |
129 | 0 | LY_ERR ret; |
130 | 0 | const struct lyd_node *ctx_node; |
131 | 0 | struct lyxp_set xp_set; |
132 | 0 | LY_ARRAY_COUNT_TYPE u; |
133 | |
|
134 | 0 | assert(!node->schema || (node->schema == schema)); |
135 | |
|
136 | 0 | *disabled = NULL; |
137 | |
|
138 | 0 | do { |
139 | 0 | const struct lysc_when *when; |
140 | 0 | struct lysc_when **when_list = lysc_node_when(schema); |
141 | 0 | LY_ARRAY_FOR(when_list, u) { |
142 | 0 | when = when_list[u]; |
143 | | |
144 | | /* get context node */ |
145 | 0 | if (when->context == schema) { |
146 | 0 | ctx_node = node; |
147 | 0 | } else { |
148 | 0 | assert((!when->context && !node->parent) || (when->context == node->parent->schema)); |
149 | 0 | ctx_node = lyd_parent(node); |
150 | 0 | } |
151 | | |
152 | | /* evaluate when */ |
153 | 0 | memset(&xp_set, 0, sizeof xp_set); |
154 | 0 | ret = lyxp_eval(LYD_CTX(node), when->cond, schema->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes, |
155 | 0 | ctx_node, tree, &xp_set, LYXP_SCHEMA); |
156 | 0 | lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
157 | | |
158 | | /* return error or LY_EINCOMPLETE for dependant unresolved when */ |
159 | 0 | LY_CHECK_RET(ret); |
160 | |
|
161 | 0 | if (!xp_set.val.bln) { |
162 | | /* false when */ |
163 | 0 | *disabled = when; |
164 | 0 | return LY_SUCCESS; |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | 0 | schema = schema->parent; |
169 | 0 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
170 | | |
171 | 0 | return LY_SUCCESS; |
172 | 0 | } |
173 | | |
174 | | /** |
175 | | * @brief Evaluate when conditions of collected unres nodes. |
176 | | * |
177 | | * @param[in,out] tree Data tree, is updated if some nodes are autodeleted. |
178 | | * @param[in] mod Module of the @p tree to take into consideration when deleting @p tree and moving it. |
179 | | * If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be |
180 | | * the first top-level sibling. |
181 | | * @param[in] node_when Set with nodes with "when" conditions. |
182 | | * @param[in,out] diff Validation diff. |
183 | | * @return LY_SUCCESS on success. |
184 | | * @return LY_ERR value on error. |
185 | | */ |
186 | | static LY_ERR |
187 | | lyd_validate_unres_when(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when, |
188 | | struct lyd_node **diff) |
189 | 0 | { |
190 | 0 | LY_ERR ret; |
191 | 0 | uint32_t i; |
192 | 0 | const struct lysc_when *disabled; |
193 | 0 | struct lyd_node *node = NULL; |
194 | |
|
195 | 0 | if (!node_when->count) { |
196 | 0 | return LY_SUCCESS; |
197 | 0 | } |
198 | | |
199 | 0 | i = node_when->count; |
200 | 0 | do { |
201 | 0 | --i; |
202 | 0 | node = node_when->dnodes[i]; |
203 | 0 | LOG_LOCSET(node->schema, node, NULL, NULL); |
204 | | |
205 | | /* evaluate all when expressions that affect this node's existence */ |
206 | 0 | ret = lyd_validate_node_when(*tree, node, node->schema, &disabled); |
207 | 0 | if (!ret) { |
208 | 0 | if (disabled) { |
209 | | /* when false */ |
210 | 0 | if (node->flags & LYD_WHEN_TRUE) { |
211 | | /* autodelete */ |
212 | 0 | lyd_del_move_root(tree, node, mod); |
213 | 0 | if (diff) { |
214 | | /* add into diff */ |
215 | 0 | ret = lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff); |
216 | 0 | LY_CHECK_GOTO(ret, error); |
217 | 0 | } |
218 | 0 | lyd_free_tree(node); |
219 | 0 | } else { |
220 | | /* invalid data */ |
221 | 0 | LOGVAL(LYD_CTX(node), LY_VCODE_NOWHEN, disabled->cond->expr); |
222 | 0 | ret = LY_EVALID; |
223 | 0 | goto error; |
224 | 0 | } |
225 | 0 | } else { |
226 | | /* when true */ |
227 | 0 | node->flags |= LYD_WHEN_TRUE; |
228 | 0 | } |
229 | | |
230 | | /* remove this node from the set, its when was resolved */ |
231 | 0 | ly_set_rm_index(node_when, i, NULL); |
232 | 0 | } else if (ret != LY_EINCOMPLETE) { |
233 | | /* error */ |
234 | 0 | goto error; |
235 | 0 | } |
236 | | |
237 | 0 | LOG_LOCBACK(1, 1, 0, 0); |
238 | 0 | } while (i); |
239 | | |
240 | 0 | return LY_SUCCESS; |
241 | | |
242 | 0 | error: |
243 | 0 | LOG_LOCBACK(1, 1, 0, 0); |
244 | 0 | return ret; |
245 | 0 | } |
246 | | |
247 | | struct node_ext { |
248 | | struct lyd_node *node; |
249 | | struct lysc_ext_instance *ext; |
250 | | }; |
251 | | |
252 | | static LY_ERR |
253 | | node_ext_tovalidate_add(struct ly_set *node_exts, struct lysc_ext_instance *exts, struct lyd_node *node) |
254 | 0 | { |
255 | 0 | LY_ERR ret = LY_SUCCESS; |
256 | 0 | struct lysc_ext_instance *ext; |
257 | 0 | struct node_ext *rec; |
258 | |
|
259 | 0 | LY_ARRAY_FOR(exts, struct lysc_ext_instance, ext) { |
260 | 0 | if (ext->def->plugin && ext->def->plugin->validate) { |
261 | 0 | rec = malloc(sizeof *rec); |
262 | 0 | LY_CHECK_ERR_RET(!rec, LOGMEM(LYD_CTX(node)), LY_EMEM); |
263 | 0 | rec->ext = ext; |
264 | 0 | rec->node = node; |
265 | |
|
266 | 0 | ret = ly_set_add(node_exts, rec, 1, NULL); |
267 | 0 | if (ret) { |
268 | 0 | free(rec); |
269 | 0 | break; |
270 | 0 | } |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | 0 | return ret; |
275 | 0 | } |
276 | | |
277 | | LY_ERR |
278 | | lysc_node_ext_tovalidate(struct ly_set *node_exts, struct lyd_node *node) |
279 | 0 | { |
280 | 0 | const struct lysc_node *schema = node->schema; |
281 | |
|
282 | 0 | if (!schema) { |
283 | | /* nothing to do */ |
284 | 0 | return LY_SUCCESS; |
285 | 0 | } |
286 | | |
287 | | /* node's extensions */ |
288 | 0 | LY_CHECK_RET(node_ext_tovalidate_add(node_exts, schema->exts, node)); |
289 | |
|
290 | 0 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
291 | | /* type's extensions */ |
292 | 0 | LY_CHECK_RET(node_ext_tovalidate_add(node_exts, ((struct lysc_node_leaf *)schema)->type->exts, node)); |
293 | 0 | } |
294 | | |
295 | 0 | return LY_SUCCESS; |
296 | 0 | } |
297 | | |
298 | | LY_ERR |
299 | | lyd_validate_unres(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, |
300 | | struct ly_set *node_types, struct ly_set *meta_types, struct lyd_node **diff) |
301 | 0 | { |
302 | 0 | LY_ERR ret = LY_SUCCESS; |
303 | 0 | uint32_t i; |
304 | |
|
305 | 0 | if (node_when) { |
306 | | /* evaluate all when conditions */ |
307 | 0 | uint32_t prev_count; |
308 | 0 | do { |
309 | 0 | prev_count = node_when->count; |
310 | 0 | LY_CHECK_RET(lyd_validate_unres_when(tree, mod, node_when, diff)); |
311 | | /* there must have been some when conditions resolved */ |
312 | 0 | } while (prev_count > node_when->count); |
313 | | |
314 | | /* there could have been no cyclic when dependencies, checked during compilation */ |
315 | 0 | assert(!node_when->count); |
316 | 0 | } |
317 | | |
318 | 0 | if (node_exts && node_exts->count) { |
319 | 0 | i = node_exts->count; |
320 | 0 | do { |
321 | 0 | --i; |
322 | |
|
323 | 0 | struct node_ext *item = node_exts->objs[i]; |
324 | |
|
325 | 0 | LOG_LOCSET(item->node->schema, item->node, NULL, NULL); |
326 | 0 | ret = item->ext->def->plugin->validate(item->ext, item->node); |
327 | 0 | LOG_LOCBACK(item->node->schema ? 1 : 0, 1, 0, 0); |
328 | 0 | LY_CHECK_RET(ret); |
329 | | |
330 | | /* remove this node from the set */ |
331 | 0 | ly_set_rm_index(node_exts, i, free); |
332 | 0 | } while (i); |
333 | 0 | } |
334 | | |
335 | 0 | if (node_types && node_types->count) { |
336 | | /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */ |
337 | 0 | i = node_types->count; |
338 | 0 | do { |
339 | 0 | --i; |
340 | |
|
341 | 0 | struct lyd_node_term *node = node_types->objs[i]; |
342 | 0 | struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type; |
343 | | |
344 | | /* resolve the value of the node */ |
345 | 0 | LOG_LOCSET(node->schema, &node->node, NULL, NULL); |
346 | 0 | ret = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, &node->node, *tree); |
347 | 0 | LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0); |
348 | 0 | LY_CHECK_RET(ret); |
349 | | |
350 | | /* remove this node from the set */ |
351 | 0 | ly_set_rm_index(node_types, i, NULL); |
352 | 0 | } while (i); |
353 | 0 | } |
354 | | |
355 | 0 | if (meta_types && meta_types->count) { |
356 | | /* ... and metadata values */ |
357 | 0 | i = meta_types->count; |
358 | 0 | do { |
359 | 0 | --i; |
360 | |
|
361 | 0 | struct lyd_meta *meta = meta_types->objs[i]; |
362 | 0 | struct lysc_type *type = *(struct lysc_type **)meta->annotation->substmts[ANNOTATION_SUBSTMT_TYPE].storage; |
363 | | |
364 | | /* validate and store the value of the metadata */ |
365 | 0 | ret = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree); |
366 | 0 | LY_CHECK_RET(ret); |
367 | | |
368 | | /* remove this attr from the set */ |
369 | 0 | ly_set_rm_index(meta_types, i, NULL); |
370 | 0 | } while (i); |
371 | 0 | } |
372 | | |
373 | 0 | return ret; |
374 | 0 | } |
375 | | |
376 | | /** |
377 | | * @brief Validate instance duplication. |
378 | | * |
379 | | * @param[in] first First sibling to search in. |
380 | | * @param[in] node Data node instance to check. |
381 | | * @return LY_ERR value. |
382 | | */ |
383 | | static LY_ERR |
384 | | lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node) |
385 | 0 | { |
386 | 0 | struct lyd_node **match_p; |
387 | 0 | ly_bool fail = 0; |
388 | |
|
389 | 0 | assert(node->flags & LYD_NEW); |
390 | | |
391 | | /* key-less list or non-configuration leaf-list */ |
392 | 0 | if (lysc_is_dup_inst_list(node->schema)) { |
393 | | /* duplicate instances allowed */ |
394 | 0 | return LY_SUCCESS; |
395 | 0 | } |
396 | | |
397 | | /* find exactly the same next instance using hashes if possible */ |
398 | 0 | if (node->parent && node->parent->children_ht) { |
399 | 0 | if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) { |
400 | 0 | fail = 1; |
401 | 0 | } |
402 | 0 | } else { |
403 | 0 | for ( ; first; first = first->next) { |
404 | 0 | if (first == node) { |
405 | 0 | continue; |
406 | 0 | } |
407 | | |
408 | 0 | if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) { |
409 | 0 | if (first->schema == node->schema) { |
410 | 0 | fail = 1; |
411 | 0 | break; |
412 | 0 | } |
413 | 0 | } else if (!lyd_compare_single(first, node, 0)) { |
414 | 0 | fail = 1; |
415 | 0 | break; |
416 | 0 | } |
417 | 0 | } |
418 | 0 | } |
419 | |
|
420 | 0 | if (fail) { |
421 | 0 | LOGVAL(node->schema->module->ctx, LY_VCODE_DUP, node->schema->name); |
422 | 0 | return LY_EVALID; |
423 | 0 | } |
424 | 0 | return LY_SUCCESS; |
425 | 0 | } |
426 | | |
427 | | /** |
428 | | * @brief Validate multiple case data existence with possible autodelete. |
429 | | * |
430 | | * @param[in,out] first First sibling to search in, is updated if needed. |
431 | | * @param[in] mod Module of the siblings, NULL for nested siblings. |
432 | | * @param[in] choic Choice node whose cases to check. |
433 | | * @param[in,out] diff Validation diff. |
434 | | * @return LY_ERR value. |
435 | | */ |
436 | | static LY_ERR |
437 | | lyd_validate_cases(struct lyd_node **first, const struct lys_module *mod, const struct lysc_node_choice *choic, |
438 | | struct lyd_node **diff) |
439 | 0 | { |
440 | 0 | const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL; |
441 | 0 | struct lyd_node *match, *to_del; |
442 | 0 | ly_bool found; |
443 | |
|
444 | 0 | LOG_LOCSET(&choic->node, NULL, NULL, NULL); |
445 | |
|
446 | 0 | LY_LIST_FOR((struct lysc_node *)choic->cases, scase) { |
447 | 0 | found = 0; |
448 | 0 | iter = NULL; |
449 | 0 | match = NULL; |
450 | 0 | while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) { |
451 | 0 | if (match->flags & LYD_NEW) { |
452 | | /* a new case data found, nothing more to look for */ |
453 | 0 | found = 2; |
454 | 0 | break; |
455 | 0 | } else { |
456 | | /* and old case data found */ |
457 | 0 | if (found == 0) { |
458 | 0 | found = 1; |
459 | 0 | } |
460 | 0 | } |
461 | 0 | } |
462 | |
|
463 | 0 | if (found == 1) { |
464 | | /* there should not be 2 old cases */ |
465 | 0 | if (old_case) { |
466 | | /* old data from 2 cases */ |
467 | 0 | LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, old_case->name, scase->name); |
468 | 0 | LOG_LOCBACK(1, 0, 0, 0); |
469 | 0 | return LY_EVALID; |
470 | 0 | } |
471 | | |
472 | | /* remember an old existing case */ |
473 | 0 | old_case = scase; |
474 | 0 | } else if (found == 2) { |
475 | 0 | if (new_case) { |
476 | | /* new data from 2 cases */ |
477 | 0 | LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, new_case->name, scase->name); |
478 | 0 | LOG_LOCBACK(1, 0, 0, 0); |
479 | 0 | return LY_EVALID; |
480 | 0 | } |
481 | | |
482 | | /* remember a new existing case */ |
483 | 0 | new_case = scase; |
484 | 0 | } |
485 | 0 | } |
486 | | |
487 | 0 | LOG_LOCBACK(1, 0, 0, 0); |
488 | |
|
489 | 0 | if (old_case && new_case) { |
490 | | /* auto-delete old case */ |
491 | 0 | iter = NULL; |
492 | 0 | match = NULL; |
493 | 0 | to_del = NULL; |
494 | 0 | while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) { |
495 | 0 | lyd_del_move_root(first, to_del, mod); |
496 | | |
497 | | /* free previous node */ |
498 | 0 | lyd_free_tree(to_del); |
499 | 0 | if (diff) { |
500 | | /* add into diff */ |
501 | 0 | LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff)); |
502 | 0 | } |
503 | 0 | to_del = match; |
504 | 0 | } |
505 | 0 | lyd_del_move_root(first, to_del, mod); |
506 | 0 | lyd_free_tree(to_del); |
507 | 0 | } |
508 | | |
509 | 0 | return LY_SUCCESS; |
510 | 0 | } |
511 | | |
512 | | /** |
513 | | * @brief Check whether a schema node can have some default values (true for NP containers as well). |
514 | | * |
515 | | * @param[in] schema Schema node to check. |
516 | | * @return non-zero if yes, |
517 | | * @return 0 otherwise. |
518 | | */ |
519 | | static int |
520 | | lyd_val_has_default(const struct lysc_node *schema) |
521 | 0 | { |
522 | 0 | switch (schema->nodetype) { |
523 | 0 | case LYS_LEAF: |
524 | 0 | if (((struct lysc_node_leaf *)schema)->dflt) { |
525 | 0 | return 1; |
526 | 0 | } |
527 | 0 | break; |
528 | 0 | case LYS_LEAFLIST: |
529 | 0 | if (((struct lysc_node_leaflist *)schema)->dflts) { |
530 | 0 | return 1; |
531 | 0 | } |
532 | 0 | break; |
533 | 0 | case LYS_CONTAINER: |
534 | 0 | if (!(schema->flags & LYS_PRESENCE)) { |
535 | 0 | return 1; |
536 | 0 | } |
537 | 0 | break; |
538 | 0 | default: |
539 | 0 | break; |
540 | 0 | } |
541 | | |
542 | 0 | return 0; |
543 | 0 | } |
544 | | |
545 | | /** |
546 | | * @brief Properly delete a node as part of autodelete validation tasks. |
547 | | * |
548 | | * @param[in,out] first First sibling, is updated if needed. |
549 | | * @param[in] node Node instance to delete. |
550 | | * @param[in] mod Module of the siblings, NULL for nested siblings. |
551 | | * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed. |
552 | | * @param[in,out] diff Validation diff. |
553 | | */ |
554 | | static void |
555 | | lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod, |
556 | | struct lyd_node **next_p, struct lyd_node **diff) |
557 | 0 | { |
558 | 0 | struct lyd_node *iter; |
559 | |
|
560 | 0 | lyd_del_move_root(first, node, mod); |
561 | 0 | if (node == *next_p) { |
562 | 0 | *next_p = (*next_p)->next; |
563 | 0 | } |
564 | 0 | if (diff) { |
565 | | /* add into diff */ |
566 | 0 | if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) { |
567 | | /* we do not want to track NP container changes, but remember any removed children */ |
568 | 0 | LY_LIST_FOR(lyd_child(node), iter) { |
569 | 0 | lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff); |
570 | 0 | } |
571 | 0 | } else { |
572 | 0 | lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff); |
573 | 0 | } |
574 | 0 | } |
575 | 0 | lyd_free_tree(node); |
576 | 0 | } |
577 | | |
578 | | /** |
579 | | * @brief Autodelete old instances to prevent validation errors. |
580 | | * |
581 | | * @param[in,out] first First sibling to search in, is updated if needed. |
582 | | * @param[in] node New data node instance to check. |
583 | | * @param[in] mod Module of the siblings, NULL for nested siblings. |
584 | | * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed. |
585 | | * @param[in,out] diff Validation diff. |
586 | | */ |
587 | | static void |
588 | | lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod, |
589 | | struct lyd_node **next_p, struct lyd_node **diff) |
590 | 0 | { |
591 | 0 | struct lyd_node *match, *next; |
592 | |
|
593 | 0 | assert(node->flags & LYD_NEW); |
594 | |
|
595 | 0 | if (lyd_val_has_default(node->schema)) { |
596 | 0 | assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER)); |
597 | 0 | LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) { |
598 | 0 | if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) { |
599 | | /* default instance found, remove it */ |
600 | 0 | lyd_validate_autodel_node_del(first, match, mod, next_p, diff); |
601 | | |
602 | | /* remove only a single container/leaf default instance, if there are more, it is an error */ |
603 | 0 | if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) { |
604 | 0 | break; |
605 | 0 | } |
606 | 0 | } |
607 | 0 | } |
608 | 0 | } |
609 | 0 | } |
610 | | |
611 | | /** |
612 | | * @brief Autodelete leftover default nodes of deleted cases (that have no existing explicit data). |
613 | | * |
614 | | * @param[in,out] first First sibling to search in, is updated if needed. |
615 | | * @param[in] node Default data node instance to check. |
616 | | * @param[in] mod Module of the siblings, NULL for nested siblings. |
617 | | * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed. |
618 | | * @param[in,out] diff Validation diff. |
619 | | */ |
620 | | static void |
621 | | lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod, |
622 | | struct lyd_node **next_p, struct lyd_node **diff) |
623 | 0 | { |
624 | 0 | struct lysc_node_choice *choic; |
625 | 0 | struct lyd_node *iter = NULL; |
626 | 0 | const struct lysc_node *slast = NULL; |
627 | |
|
628 | 0 | assert(node->flags & LYD_DEFAULT); |
629 | |
|
630 | 0 | if (!node->schema->parent || (node->schema->parent->nodetype != LYS_CASE)) { |
631 | | /* the default node is not a descendant of a case */ |
632 | 0 | return; |
633 | 0 | } |
634 | | |
635 | 0 | choic = (struct lysc_node_choice *)node->schema->parent->parent; |
636 | 0 | assert(choic->nodetype == LYS_CHOICE); |
637 | |
|
638 | 0 | if (choic->dflt && (choic->dflt == (struct lysc_node_case *)node->schema->parent)) { |
639 | | /* data of a default case, keep them */ |
640 | 0 | return; |
641 | 0 | } |
642 | | |
643 | | /* try to find an explicit node of the case */ |
644 | 0 | while ((iter = lys_getnext_data(iter, *first, &slast, node->schema->parent, NULL))) { |
645 | 0 | if (!(iter->flags & LYD_DEFAULT)) { |
646 | 0 | break; |
647 | 0 | } |
648 | 0 | } |
649 | |
|
650 | 0 | if (!iter) { |
651 | | /* there are only default nodes of the case meaning it does not exist and neither should any default nodes |
652 | | * of the case, remove this one default node */ |
653 | 0 | lyd_validate_autodel_node_del(first, node, mod, next_p, diff); |
654 | 0 | } |
655 | 0 | } |
656 | | |
657 | | LY_ERR |
658 | | lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod, |
659 | | struct lyd_node **diff) |
660 | 0 | { |
661 | 0 | struct lyd_node *next, *node; |
662 | 0 | const struct lysc_node *snode = NULL; |
663 | |
|
664 | 0 | assert(first && (sparent || mod)); |
665 | |
|
666 | 0 | while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) { |
667 | | /* check case duplicites */ |
668 | 0 | if (snode->nodetype == LYS_CHOICE) { |
669 | 0 | LY_CHECK_RET(lyd_validate_cases(first, mod, (struct lysc_node_choice *)snode, diff)); |
670 | 0 | } |
671 | 0 | } |
672 | | |
673 | 0 | LY_LIST_FOR_SAFE(*first, next, node) { |
674 | 0 | if (mod && (lyd_owner_module(node) != mod)) { |
675 | | /* all top-level data from this module checked */ |
676 | 0 | break; |
677 | 0 | } |
678 | | |
679 | 0 | if (!(node->flags & (LYD_NEW | LYD_DEFAULT))) { |
680 | | /* check only new and default nodes */ |
681 | 0 | continue; |
682 | 0 | } |
683 | | |
684 | 0 | LOG_LOCSET(node->schema, node, NULL, NULL); |
685 | |
|
686 | 0 | if (node->flags & LYD_NEW) { |
687 | 0 | LY_ERR ret; |
688 | | |
689 | | /* remove old default(s) of the new node if it exists */ |
690 | 0 | lyd_validate_autodel_dup(first, node, mod, &next, diff); |
691 | | |
692 | | /* then check new node instance duplicities */ |
693 | 0 | ret = lyd_validate_duplicates(*first, node); |
694 | 0 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0), ret); |
695 | | |
696 | | /* this node is valid */ |
697 | 0 | node->flags &= ~LYD_NEW; |
698 | 0 | } |
699 | | |
700 | 0 | LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0); |
701 | |
|
702 | 0 | if (node->flags & LYD_DEFAULT) { |
703 | | /* remove leftover default nodes from a no-longer existing case */ |
704 | 0 | lyd_validate_autodel_case_dflt(first, node, mod, &next, diff); |
705 | 0 | } |
706 | 0 | } |
707 | | |
708 | 0 | return LY_SUCCESS; |
709 | 0 | } |
710 | | |
711 | | /** |
712 | | * @brief Evaluate any "when" conditions of a non-existent data node with existing parent. |
713 | | * |
714 | | * @param[in] first First data sibling of the non-existing node. |
715 | | * @param[in] parent Data parent of the non-existing node. |
716 | | * @param[in] snode Schema node of the non-existing node. |
717 | | * @param[out] disabled First when that evaluated false, if any. |
718 | | * @return LY_ERR value. |
719 | | */ |
720 | | static LY_ERR |
721 | | lyd_validate_dummy_when(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode, |
722 | | const struct lysc_when **disabled) |
723 | 0 | { |
724 | 0 | LY_ERR ret = LY_SUCCESS; |
725 | 0 | struct lyd_node *tree, *dummy = NULL; |
726 | | |
727 | | /* find root */ |
728 | 0 | if (parent) { |
729 | 0 | tree = (struct lyd_node *)parent; |
730 | 0 | while (tree->parent) { |
731 | 0 | tree = lyd_parent(tree); |
732 | 0 | } |
733 | 0 | tree = lyd_first_sibling(tree); |
734 | 0 | } else { |
735 | 0 | assert(!first || !first->prev->next); |
736 | 0 | tree = (struct lyd_node *)first; |
737 | 0 | } |
738 | | |
739 | | /* create dummy opaque node */ |
740 | 0 | ret = lyd_new_opaq((struct lyd_node *)parent, snode->module->ctx, snode->name, NULL, NULL, snode->module->name, &dummy); |
741 | 0 | LY_CHECK_GOTO(ret, cleanup); |
742 | | |
743 | | /* connect it if needed */ |
744 | 0 | if (!parent) { |
745 | 0 | if (first) { |
746 | 0 | lyd_insert_sibling((struct lyd_node *)first, dummy, &tree); |
747 | 0 | } else { |
748 | 0 | assert(!tree); |
749 | 0 | tree = dummy; |
750 | 0 | } |
751 | 0 | } |
752 | | |
753 | | /* evaluate all when */ |
754 | 0 | ret = lyd_validate_node_when(tree, dummy, snode, disabled); |
755 | 0 | if (ret == LY_EINCOMPLETE) { |
756 | | /* all other when must be resolved by now */ |
757 | 0 | LOGINT(snode->module->ctx); |
758 | 0 | ret = LY_EINT; |
759 | 0 | goto cleanup; |
760 | 0 | } else if (ret) { |
761 | | /* error */ |
762 | 0 | goto cleanup; |
763 | 0 | } |
764 | | |
765 | 0 | cleanup: |
766 | 0 | lyd_free_tree(dummy); |
767 | 0 | return ret; |
768 | 0 | } |
769 | | |
770 | | /** |
771 | | * @brief Validate mandatory node existence. |
772 | | * |
773 | | * @param[in] first First sibling to search in. |
774 | | * @param[in] parent Data parent. |
775 | | * @param[in] snode Schema node to validate. |
776 | | * @return LY_ERR value. |
777 | | */ |
778 | | static LY_ERR |
779 | | lyd_validate_mandatory(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode) |
780 | 0 | { |
781 | 0 | const struct lysc_when *disabled; |
782 | |
|
783 | 0 | if (snode->nodetype == LYS_CHOICE) { |
784 | | /* some data of a choice case exist */ |
785 | 0 | if (lys_getnext_data(NULL, first, NULL, snode, NULL)) { |
786 | 0 | return LY_SUCCESS; |
787 | 0 | } |
788 | 0 | } else { |
789 | 0 | assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY)); |
790 | |
|
791 | 0 | if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) { |
792 | | /* data instance found */ |
793 | 0 | return LY_SUCCESS; |
794 | 0 | } |
795 | 0 | } |
796 | | |
797 | 0 | disabled = NULL; |
798 | 0 | if (lysc_has_when(snode)) { |
799 | | /* if there are any when conditions, they must be true for a validation error */ |
800 | 0 | LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled)); |
801 | 0 | } |
802 | | |
803 | 0 | if (!disabled) { |
804 | | /* node instance not found */ |
805 | 0 | if (snode->nodetype == LYS_CHOICE) { |
806 | 0 | LOGVAL(snode->module->ctx, LY_VCODE_NOMAND_CHOIC, snode->name); |
807 | 0 | } else { |
808 | 0 | LOGVAL(snode->module->ctx, LY_VCODE_NOMAND, snode->name); |
809 | 0 | } |
810 | 0 | return LY_EVALID; |
811 | 0 | } |
812 | | |
813 | 0 | return LY_SUCCESS; |
814 | 0 | } |
815 | | |
816 | | /** |
817 | | * @brief Validate min/max-elements constraints, if any. |
818 | | * |
819 | | * @param[in] first First sibling to search in. |
820 | | * @param[in] parent Data parent. |
821 | | * @param[in] snode Schema node to validate. |
822 | | * @param[in] min Minimum number of elements, 0 for no restriction. |
823 | | * @param[in] max Max number of elements, 0 for no restriction. |
824 | | * @return LY_ERR value. |
825 | | */ |
826 | | static LY_ERR |
827 | | lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode, |
828 | | uint32_t min, uint32_t max) |
829 | 0 | { |
830 | 0 | uint32_t count = 0; |
831 | 0 | struct lyd_node *iter; |
832 | 0 | const struct lysc_when *disabled; |
833 | 0 | ly_bool invalid_instance = 0; |
834 | |
|
835 | 0 | assert(min || max); |
836 | |
|
837 | 0 | LYD_LIST_FOR_INST(first, snode, iter) { |
838 | 0 | ++count; |
839 | |
|
840 | 0 | if (min && (count == min)) { |
841 | | /* satisfied */ |
842 | 0 | min = 0; |
843 | 0 | if (!max) { |
844 | | /* nothing more to check */ |
845 | 0 | break; |
846 | 0 | } |
847 | 0 | } |
848 | 0 | if (max && (count > max)) { |
849 | | /* not satisifed */ |
850 | 0 | LOG_LOCSET(NULL, iter, NULL, NULL); |
851 | 0 | invalid_instance = 1; |
852 | 0 | break; |
853 | 0 | } |
854 | 0 | } |
855 | |
|
856 | 0 | if (min) { |
857 | 0 | assert(count < min); |
858 | |
|
859 | 0 | disabled = NULL; |
860 | 0 | if (lysc_has_when(snode)) { |
861 | | /* if there are any when conditions, they must be true for a validation error */ |
862 | 0 | LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled)); |
863 | 0 | } |
864 | | |
865 | 0 | if (!disabled) { |
866 | 0 | LOGVAL(snode->module->ctx, LY_VCODE_NOMIN, snode->name); |
867 | 0 | goto failure; |
868 | 0 | } |
869 | 0 | } else if (max && (count > max)) { |
870 | 0 | LOGVAL(snode->module->ctx, LY_VCODE_NOMAX, snode->name); |
871 | 0 | goto failure; |
872 | 0 | } |
873 | | |
874 | 0 | return LY_SUCCESS; |
875 | | |
876 | 0 | failure: |
877 | 0 | LOG_LOCBACK(0, invalid_instance, 0, 0); |
878 | 0 | return LY_EVALID; |
879 | 0 | } |
880 | | |
881 | | /** |
882 | | * @brief Find node referenced by a list unique statement. |
883 | | * |
884 | | * @param[in] uniq_leaf Unique leaf to find. |
885 | | * @param[in] list List instance to use for the search. |
886 | | * @return Found leaf, |
887 | | * @return NULL if no leaf found. |
888 | | */ |
889 | | static struct lyd_node * |
890 | | lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list) |
891 | 0 | { |
892 | 0 | struct lyd_node *node; |
893 | 0 | const struct lysc_node *iter; |
894 | 0 | size_t depth = 0, i; |
895 | | |
896 | | /* get leaf depth */ |
897 | 0 | for (iter = &uniq_leaf->node; iter && (iter != list->schema); iter = lysc_data_parent(iter)) { |
898 | 0 | ++depth; |
899 | 0 | } |
900 | |
|
901 | 0 | node = (struct lyd_node *)list; |
902 | 0 | while (node && depth) { |
903 | | /* find schema node with this depth */ |
904 | 0 | for (i = depth - 1, iter = &uniq_leaf->node; i; iter = lysc_data_parent(iter)) { |
905 | 0 | --i; |
906 | 0 | } |
907 | | |
908 | | /* find iter instance in children */ |
909 | 0 | assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF)); |
910 | 0 | lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node); |
911 | 0 | --depth; |
912 | 0 | } |
913 | |
|
914 | 0 | return node; |
915 | 0 | } |
916 | | |
917 | | /** |
918 | | * @brief Callback for comparing 2 list unique leaf values. |
919 | | * |
920 | | * Implementation of ::lyht_value_equal_cb. |
921 | | * |
922 | | * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique. |
923 | | */ |
924 | | static ly_bool |
925 | | lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data) |
926 | 0 | { |
927 | 0 | struct ly_ctx *ctx; |
928 | 0 | struct lysc_node_list *slist; |
929 | 0 | struct lyd_node *diter, *first, *second; |
930 | 0 | struct lyd_value *val1, *val2; |
931 | 0 | char *path1, *path2, *uniq_str, *ptr; |
932 | 0 | LY_ARRAY_COUNT_TYPE u, v, action; |
933 | |
|
934 | 0 | assert(val1_p && val2_p); |
935 | |
|
936 | 0 | first = *((struct lyd_node **)val1_p); |
937 | 0 | second = *((struct lyd_node **)val2_p); |
938 | 0 | action = (LY_ARRAY_COUNT_TYPE)cb_data; |
939 | |
|
940 | 0 | assert(first && (first->schema->nodetype == LYS_LIST)); |
941 | 0 | assert(second && (second->schema == first->schema)); |
942 | |
|
943 | 0 | ctx = first->schema->module->ctx; |
944 | |
|
945 | 0 | slist = (struct lysc_node_list *)first->schema; |
946 | | |
947 | | /* compare unique leaves */ |
948 | 0 | if (action > 0) { |
949 | 0 | u = action - 1; |
950 | 0 | if (u < LY_ARRAY_COUNT(slist->uniques)) { |
951 | 0 | goto uniquecheck; |
952 | 0 | } |
953 | 0 | } |
954 | 0 | LY_ARRAY_FOR(slist->uniques, u) { |
955 | 0 | uniquecheck: |
956 | 0 | LY_ARRAY_FOR(slist->uniques[u], v) { |
957 | | /* first */ |
958 | 0 | diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first); |
959 | 0 | if (diter) { |
960 | 0 | val1 = &((struct lyd_node_term *)diter)->value; |
961 | 0 | } else { |
962 | | /* use default value */ |
963 | 0 | val1 = slist->uniques[u][v]->dflt; |
964 | 0 | } |
965 | | |
966 | | /* second */ |
967 | 0 | diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second); |
968 | 0 | if (diter) { |
969 | 0 | val2 = &((struct lyd_node_term *)diter)->value; |
970 | 0 | } else { |
971 | | /* use default value */ |
972 | 0 | val2 = slist->uniques[u][v]->dflt; |
973 | 0 | } |
974 | |
|
975 | 0 | if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) { |
976 | | /* values differ or either one is not set */ |
977 | 0 | break; |
978 | 0 | } |
979 | 0 | } |
980 | 0 | if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) { |
981 | | /* all unique leafs are the same in this set, create this nice error */ |
982 | 0 | path1 = lyd_path(first, LYD_PATH_STD, NULL, 0); |
983 | 0 | path2 = lyd_path(second, LYD_PATH_STD, NULL, 0); |
984 | | |
985 | | /* use buffer to rebuild the unique string */ |
986 | 0 | #define UNIQ_BUF_SIZE 1024 |
987 | 0 | uniq_str = malloc(UNIQ_BUF_SIZE); |
988 | 0 | uniq_str[0] = '\0'; |
989 | 0 | ptr = uniq_str; |
990 | 0 | LY_ARRAY_FOR(slist->uniques[u], v) { |
991 | 0 | if (v) { |
992 | 0 | strcpy(ptr, " "); |
993 | 0 | ++ptr; |
994 | 0 | } |
995 | 0 | ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], &slist->node, LYSC_PATH_LOG, |
996 | 0 | ptr, UNIQ_BUF_SIZE - (ptr - uniq_str)); |
997 | 0 | if (!ptr) { |
998 | | /* path will be incomplete, whatever */ |
999 | 0 | break; |
1000 | 0 | } |
1001 | | |
1002 | 0 | ptr += strlen(ptr); |
1003 | 0 | } |
1004 | 0 | LOG_LOCSET(NULL, second, NULL, NULL); |
1005 | 0 | LOGVAL(ctx, LY_VCODE_NOUNIQ, uniq_str, path1, path2); |
1006 | 0 | LOG_LOCBACK(0, 1, 0, 0); |
1007 | |
|
1008 | 0 | free(path1); |
1009 | 0 | free(path2); |
1010 | 0 | free(uniq_str); |
1011 | 0 | #undef UNIQ_BUF_SIZE |
1012 | |
|
1013 | 0 | return 1; |
1014 | 0 | } |
1015 | | |
1016 | 0 | if (action > 0) { |
1017 | | /* done */ |
1018 | 0 | return 0; |
1019 | 0 | } |
1020 | 0 | } |
1021 | | |
1022 | 0 | return 0; |
1023 | 0 | } |
1024 | | |
1025 | | /** |
1026 | | * @brief Validate list unique leaves. |
1027 | | * |
1028 | | * @param[in] first First sibling to search in. |
1029 | | * @param[in] snode Schema node to validate. |
1030 | | * @param[in] uniques List unique arrays to validate. |
1031 | | * @return LY_ERR value. |
1032 | | */ |
1033 | | static LY_ERR |
1034 | | lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, const struct lysc_node_leaf ***uniques) |
1035 | 0 | { |
1036 | 0 | const struct lyd_node *diter; |
1037 | 0 | struct ly_set *set; |
1038 | 0 | LY_ARRAY_COUNT_TYPE u, v, x = 0; |
1039 | 0 | LY_ERR ret = LY_SUCCESS; |
1040 | 0 | uint32_t hash, i, size = 0; |
1041 | 0 | size_t key_len; |
1042 | 0 | ly_bool dyn; |
1043 | 0 | const void *hash_key; |
1044 | 0 | struct hash_table **uniqtables = NULL; |
1045 | 0 | struct lyd_value *val; |
1046 | 0 | struct ly_ctx *ctx = snode->module->ctx; |
1047 | |
|
1048 | 0 | assert(uniques); |
1049 | | |
1050 | | /* get all list instances */ |
1051 | 0 | LY_CHECK_RET(ly_set_new(&set)); |
1052 | 0 | LY_LIST_FOR(first, diter) { |
1053 | 0 | if (diter->schema == snode) { |
1054 | 0 | ret = ly_set_add(set, (void *)diter, 1, NULL); |
1055 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1056 | 0 | } |
1057 | 0 | } |
1058 | | |
1059 | 0 | if (set->count == 2) { |
1060 | | /* simple comparison */ |
1061 | 0 | if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) { |
1062 | | /* instance duplication */ |
1063 | 0 | ret = LY_EVALID; |
1064 | 0 | goto cleanup; |
1065 | 0 | } |
1066 | 0 | } else if (set->count > 2) { |
1067 | | /* use hashes for comparison */ |
1068 | | /* first, allocate the table, the size depends on number of items in the set, |
1069 | | * the following code detects number of upper zero bits in the items' counter value ... */ |
1070 | 0 | for (i = (sizeof set->count * CHAR_BIT) - 1; i > 0; i--) { |
1071 | 0 | size = set->count << i; |
1072 | 0 | size = size >> i; |
1073 | 0 | if (size == set->count) { |
1074 | 0 | break; |
1075 | 0 | } |
1076 | 0 | } |
1077 | 0 | LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup); |
1078 | | /* ... and then we convert it to the position of the highest non-zero bit ... */ |
1079 | 0 | i = (sizeof set->count * CHAR_BIT) - i; |
1080 | | /* ... and by using it to shift 1 to the left we get the closest sufficient hash table size */ |
1081 | 0 | size = 1 << i; |
1082 | |
|
1083 | 0 | uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables); |
1084 | 0 | LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
1085 | 0 | x = LY_ARRAY_COUNT(uniques); |
1086 | 0 | for (v = 0; v < x; v++) { |
1087 | 0 | uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0); |
1088 | 0 | LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
1089 | 0 | } |
1090 | | |
1091 | 0 | for (i = 0; i < set->count; i++) { |
1092 | | /* loop for unique - get the hash for the instances */ |
1093 | 0 | for (u = 0; u < x; u++) { |
1094 | 0 | val = NULL; |
1095 | 0 | for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) { |
1096 | 0 | diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]); |
1097 | 0 | if (diter) { |
1098 | 0 | val = &((struct lyd_node_term *)diter)->value; |
1099 | 0 | } else { |
1100 | | /* use default value */ |
1101 | 0 | val = uniques[u][v]->dflt; |
1102 | 0 | } |
1103 | 0 | if (!val) { |
1104 | | /* unique item not present nor has default value */ |
1105 | 0 | break; |
1106 | 0 | } |
1107 | | |
1108 | | /* get hash key */ |
1109 | 0 | hash_key = val->realtype->plugin->print(NULL, val, LY_VALUE_LYB, NULL, &dyn, &key_len); |
1110 | 0 | hash = dict_hash_multi(hash, hash_key, key_len); |
1111 | 0 | if (dyn) { |
1112 | 0 | free((void *)hash_key); |
1113 | 0 | } |
1114 | 0 | } |
1115 | 0 | if (!val) { |
1116 | | /* skip this list instance since its unique set is incomplete */ |
1117 | 0 | continue; |
1118 | 0 | } |
1119 | | |
1120 | | /* finish the hash value */ |
1121 | 0 | hash = dict_hash_multi(hash, NULL, 0); |
1122 | | |
1123 | | /* insert into the hashtable */ |
1124 | 0 | ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL); |
1125 | 0 | if (ret == LY_EEXIST) { |
1126 | | /* instance duplication */ |
1127 | 0 | ret = LY_EVALID; |
1128 | 0 | } |
1129 | 0 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
1130 | 0 | } |
1131 | 0 | } |
1132 | 0 | } |
1133 | | |
1134 | 0 | cleanup: |
1135 | 0 | ly_set_free(set, NULL); |
1136 | 0 | for (v = 0; v < x; v++) { |
1137 | 0 | if (!uniqtables[v]) { |
1138 | | /* failed when allocating uniquetables[j], following j are not allocated */ |
1139 | 0 | break; |
1140 | 0 | } |
1141 | 0 | lyht_free(uniqtables[v]); |
1142 | 0 | } |
1143 | 0 | free(uniqtables); |
1144 | |
|
1145 | 0 | return ret; |
1146 | 0 | } |
1147 | | |
1148 | | /** |
1149 | | * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes. |
1150 | | * |
1151 | | * @param[in] first First sibling to search in. |
1152 | | * @param[in] parent Data parent. |
1153 | | * @param[in] sparent Schema parent of the nodes to check. |
1154 | | * @param[in] mod Module of the nodes to check. |
1155 | | * @param[in] val_opts Validation options, see @ref datavalidationoptions. |
1156 | | * @param[in] int_opts Internal parser options. |
1157 | | * @return LY_ERR value. |
1158 | | */ |
1159 | | static LY_ERR |
1160 | | lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lyd_node *parent, |
1161 | | const struct lysc_node *sparent, const struct lysc_module *mod, uint32_t val_opts, uint32_t int_opts) |
1162 | 0 | { |
1163 | 0 | LY_ERR ret = LY_SUCCESS; |
1164 | 0 | const struct lysc_node *snode = NULL, *scase; |
1165 | 0 | struct lysc_node_list *slist; |
1166 | 0 | struct lysc_node_leaflist *sllist; |
1167 | 0 | uint32_t getnext_opts; |
1168 | |
|
1169 | 0 | getnext_opts = LYS_GETNEXT_WITHCHOICE | (int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0); |
1170 | | |
1171 | | /* disabled nodes are skipped by lys_getnext */ |
1172 | 0 | while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) { |
1173 | 0 | if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
1174 | 0 | continue; |
1175 | 0 | } |
1176 | | |
1177 | 0 | LOG_LOCSET(snode, NULL, NULL, NULL); |
1178 | | |
1179 | | /* check min-elements and max-elements */ |
1180 | 0 | if (snode->nodetype == LYS_LIST) { |
1181 | 0 | slist = (struct lysc_node_list *)snode; |
1182 | 0 | if (slist->min || slist->max) { |
1183 | 0 | ret = lyd_validate_minmax(first, parent, snode, slist->min, slist->max); |
1184 | 0 | LY_CHECK_GOTO(ret, error); |
1185 | 0 | } |
1186 | 0 | } else if (snode->nodetype == LYS_LEAFLIST) { |
1187 | 0 | sllist = (struct lysc_node_leaflist *)snode; |
1188 | 0 | if (sllist->min || sllist->max) { |
1189 | 0 | ret = lyd_validate_minmax(first, parent, snode, sllist->min, sllist->max); |
1190 | 0 | LY_CHECK_GOTO(ret, error); |
1191 | 0 | } |
1192 | |
|
1193 | 0 | } else if (snode->flags & LYS_MAND_TRUE) { |
1194 | | /* check generic mandatory existence */ |
1195 | 0 | ret = lyd_validate_mandatory(first, parent, snode); |
1196 | 0 | LY_CHECK_GOTO(ret, error); |
1197 | 0 | } |
1198 | | |
1199 | | /* check unique */ |
1200 | 0 | if (snode->nodetype == LYS_LIST) { |
1201 | 0 | slist = (struct lysc_node_list *)snode; |
1202 | 0 | if (slist->uniques) { |
1203 | 0 | ret = lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques); |
1204 | 0 | LY_CHECK_GOTO(ret, error); |
1205 | 0 | } |
1206 | 0 | } |
1207 | | |
1208 | 0 | if (snode->nodetype == LYS_CHOICE) { |
1209 | | /* find the existing case, if any */ |
1210 | 0 | LY_LIST_FOR(lysc_node_child(snode), scase) { |
1211 | 0 | if (lys_getnext_data(NULL, first, NULL, scase, NULL)) { |
1212 | | /* validate only this case */ |
1213 | 0 | ret = lyd_validate_siblings_schema_r(first, parent, scase, mod, val_opts, int_opts); |
1214 | 0 | LY_CHECK_GOTO(ret, error); |
1215 | 0 | break; |
1216 | 0 | } |
1217 | 0 | } |
1218 | 0 | } |
1219 | | |
1220 | 0 | LOG_LOCBACK(1, 0, 0, 0); |
1221 | 0 | } |
1222 | | |
1223 | 0 | return LY_SUCCESS; |
1224 | | |
1225 | 0 | error: |
1226 | 0 | LOG_LOCBACK(1, 0, 0, 0); |
1227 | 0 | return ret; |
1228 | 0 | } |
1229 | | |
1230 | | /** |
1231 | | * @brief Validate obsolete nodes, only warnings are printed. |
1232 | | * |
1233 | | * @param[in] node Node to check. |
1234 | | */ |
1235 | | static void |
1236 | | lyd_validate_obsolete(const struct lyd_node *node) |
1237 | 0 | { |
1238 | 0 | const struct lysc_node *snode; |
1239 | |
|
1240 | 0 | snode = node->schema; |
1241 | 0 | do { |
1242 | 0 | if (snode->flags & LYS_STATUS_OBSLT) { |
1243 | 0 | LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name); |
1244 | 0 | break; |
1245 | 0 | } |
1246 | | |
1247 | 0 | snode = snode->parent; |
1248 | 0 | } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE))); |
1249 | 0 | } |
1250 | | |
1251 | | /** |
1252 | | * @brief Validate must conditions of a data node. |
1253 | | * |
1254 | | * @param[in] node Node to validate. |
1255 | | * @param[in] int_opts Internal parser options. |
1256 | | * @return LY_ERR value. |
1257 | | */ |
1258 | | static LY_ERR |
1259 | | lyd_validate_must(const struct lyd_node *node, uint32_t int_opts) |
1260 | 0 | { |
1261 | 0 | struct lyxp_set xp_set; |
1262 | 0 | struct lysc_must *musts; |
1263 | 0 | const struct lyd_node *tree; |
1264 | 0 | const struct lysc_node *schema; |
1265 | 0 | LY_ARRAY_COUNT_TYPE u; |
1266 | |
|
1267 | 0 | assert((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) != (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)); |
1268 | 0 | assert((int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) != (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)); |
1269 | |
|
1270 | 0 | if (node->schema->nodetype & (LYS_ACTION | LYS_RPC)) { |
1271 | 0 | if (int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION)) { |
1272 | 0 | schema = &((struct lysc_node_action *)node->schema)->input.node; |
1273 | 0 | } else if (int_opts & LYD_INTOPT_REPLY) { |
1274 | 0 | schema = &((struct lysc_node_action *)node->schema)->output.node; |
1275 | 0 | } else { |
1276 | 0 | LOGINT(LYD_CTX(node)); |
1277 | 0 | return LY_EINT; |
1278 | 0 | } |
1279 | 0 | } else { |
1280 | 0 | schema = node->schema; |
1281 | 0 | } |
1282 | 0 | musts = lysc_node_musts(schema); |
1283 | 0 | if (!musts) { |
1284 | | /* no must to evaluate */ |
1285 | 0 | return LY_SUCCESS; |
1286 | 0 | } |
1287 | | |
1288 | | /* find first top-level node */ |
1289 | 0 | for (tree = node; tree->parent; tree = lyd_parent(tree)) {} |
1290 | 0 | tree = lyd_first_sibling(tree); |
1291 | |
|
1292 | 0 | LY_ARRAY_FOR(musts, u) { |
1293 | 0 | memset(&xp_set, 0, sizeof xp_set); |
1294 | | |
1295 | | /* evaluate must */ |
1296 | 0 | LY_CHECK_RET(lyxp_eval(LYD_CTX(node), musts[u].cond, node->schema->module, LY_VALUE_SCHEMA_RESOLVED, |
1297 | 0 | musts[u].prefixes, node, tree, &xp_set, LYXP_SCHEMA)); |
1298 | | |
1299 | | /* check the result */ |
1300 | 0 | lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
1301 | 0 | if (!xp_set.val.bln) { |
1302 | 0 | LOGVAL(LYD_CTX(node), LY_VCODE_NOMUST, musts[u].cond->expr); |
1303 | 0 | return LY_EVALID; |
1304 | 0 | } |
1305 | 0 | } |
1306 | | |
1307 | 0 | return LY_SUCCESS; |
1308 | 0 | } |
1309 | | |
1310 | | /** |
1311 | | * @brief Perform all remaining validation tasks, the data tree must be final when calling this function. |
1312 | | * |
1313 | | * @param[in] first First sibling. |
1314 | | * @param[in] parent Data parent. |
1315 | | * @param[in] sparent Schema parent of the siblings, NULL for top-level siblings. |
1316 | | * @param[in] mod Module of the siblings, NULL for nested siblings. |
1317 | | * @param[in] val_opts Validation options (@ref datavalidationoptions). |
1318 | | * @param[in] int_opts Internal parser options. |
1319 | | * @return LY_ERR value. |
1320 | | */ |
1321 | | static LY_ERR |
1322 | | lyd_validate_final_r(struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *sparent, |
1323 | | const struct lys_module *mod, uint32_t val_opts, uint32_t int_opts) |
1324 | 0 | { |
1325 | 0 | const char *innode = NULL; |
1326 | 0 | struct lyd_node *next = NULL, *node; |
1327 | | |
1328 | | /* validate all restrictions of nodes themselves */ |
1329 | 0 | LY_LIST_FOR_SAFE(first, next, node) { |
1330 | 0 | if (mod && (lyd_owner_module(node) != mod)) { |
1331 | | /* all top-level data from this module checked */ |
1332 | 0 | break; |
1333 | 0 | } |
1334 | | |
1335 | 0 | LOG_LOCSET(node->schema, node, NULL, NULL); |
1336 | | |
1337 | | /* opaque data */ |
1338 | 0 | if (!node->schema) { |
1339 | 0 | LOGVAL(LYD_CTX(node), LYVE_DATA, "Opaque node \"%s\" found.", ((struct lyd_node_opaq *)node)->name.name); |
1340 | 0 | LOG_LOCBACK(0, 1, 0, 0); |
1341 | 0 | return LY_EVALID; |
1342 | 0 | } |
1343 | | |
1344 | | /* no state/input/output data */ |
1345 | 0 | if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) { |
1346 | 0 | innode = "state"; |
1347 | 0 | goto unexpected_node; |
1348 | 0 | } else if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION)) && (node->schema->flags & LYS_IS_OUTPUT)) { |
1349 | 0 | innode = "output"; |
1350 | 0 | goto unexpected_node; |
1351 | 0 | } else if ((int_opts & LYD_INTOPT_REPLY) && (node->schema->flags & LYS_IS_INPUT)) { |
1352 | 0 | innode = "input"; |
1353 | 0 | goto unexpected_node; |
1354 | 0 | } |
1355 | | |
1356 | | /* obsolete data */ |
1357 | 0 | lyd_validate_obsolete(node); |
1358 | | |
1359 | | /* node's musts */ |
1360 | 0 | LY_CHECK_RET(lyd_validate_must(node, int_opts)); |
1361 | | |
1362 | | /* node value was checked by plugins */ |
1363 | |
|
1364 | 0 | LOG_LOCBACK(1, 1, 0, 0); |
1365 | 0 | } |
1366 | | |
1367 | | /* validate schema-based restrictions */ |
1368 | 0 | LY_CHECK_RET(lyd_validate_siblings_schema_r(first, parent, sparent, mod ? mod->compiled : NULL, val_opts, int_opts)); |
1369 | |
|
1370 | 0 | LY_LIST_FOR(first, node) { |
1371 | | /* validate all children recursively */ |
1372 | 0 | LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node, node->schema, NULL, val_opts, int_opts)); |
1373 | | |
1374 | | /* set default for containers */ |
1375 | 0 | if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) { |
1376 | 0 | LY_LIST_FOR(lyd_child(node), next) { |
1377 | 0 | if (!(next->flags & LYD_DEFAULT)) { |
1378 | 0 | break; |
1379 | 0 | } |
1380 | 0 | } |
1381 | 0 | if (!next) { |
1382 | 0 | node->flags |= LYD_DEFAULT; |
1383 | 0 | } |
1384 | 0 | } |
1385 | 0 | } |
1386 | | |
1387 | 0 | return LY_SUCCESS; |
1388 | | |
1389 | 0 | unexpected_node: |
1390 | 0 | LOGVAL(LYD_CTX(node), LY_VCODE_UNEXPNODE, innode, node->schema->name); |
1391 | 0 | LOG_LOCBACK(1, 1, 0, 0); |
1392 | 0 | return LY_EVALID; |
1393 | 0 | } |
1394 | | |
1395 | | /** |
1396 | | * @brief Validate the whole data subtree. |
1397 | | * |
1398 | | * @param[in] root Subtree root. |
1399 | | * @param[in,out] node_when Set for nodes with when conditions. |
1400 | | * @param[in,out] node_exts Set for nodes and extension instances with validation plugin callback. |
1401 | | * @param[in,out] node_types Set for unres node types. |
1402 | | * @param[in,out] meta_types Set for unres metadata types. |
1403 | | * @param[in] impl_opts Implicit options, see @ref implicitoptions. |
1404 | | * @param[in,out] diff Validation diff. |
1405 | | * @return LY_ERR value. |
1406 | | */ |
1407 | | static LY_ERR |
1408 | | lyd_validate_subtree(struct lyd_node *root, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types, |
1409 | | struct ly_set *meta_types, uint32_t impl_opts, struct lyd_node **diff) |
1410 | 0 | { |
1411 | 0 | const struct lyd_meta *meta; |
1412 | 0 | struct lyd_node *node; |
1413 | |
|
1414 | 0 | LYD_TREE_DFS_BEGIN(root, node) { |
1415 | 0 | LY_LIST_FOR(node->meta, meta) { |
1416 | 0 | if ((*(const struct lysc_type **)meta->annotation->substmts[ANNOTATION_SUBSTMT_TYPE].storage)->plugin->validate) { |
1417 | | /* metadata type resolution */ |
1418 | 0 | LY_CHECK_RET(ly_set_add(meta_types, (void *)meta, 1, NULL)); |
1419 | 0 | } |
1420 | 0 | } |
1421 | | |
1422 | 0 | if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) { |
1423 | | /* node type resolution */ |
1424 | 0 | LY_CHECK_RET(ly_set_add(node_types, (void *)node, 1, NULL)); |
1425 | 0 | } else if (node->schema->nodetype & LYD_NODE_INNER) { |
1426 | | /* new node validation, autodelete */ |
1427 | 0 | LY_CHECK_RET(lyd_validate_new(lyd_node_child_p(node), node->schema, NULL, diff)); |
1428 | | |
1429 | | /* add nested defaults */ |
1430 | 0 | LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, NULL, NULL, NULL, impl_opts, diff)); |
1431 | 0 | } |
1432 | | |
1433 | 0 | if (lysc_has_when(node->schema)) { |
1434 | | /* when evaluation */ |
1435 | 0 | LY_CHECK_RET(ly_set_add(node_when, (void *)node, 1, NULL)); |
1436 | 0 | } |
1437 | 0 | LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node)); |
1438 | |
|
1439 | 0 | LYD_TREE_DFS_END(root, node); |
1440 | 0 | } |
1441 | | |
1442 | 0 | return LY_SUCCESS; |
1443 | 0 | } |
1444 | | |
1445 | | LY_ERR |
1446 | | lyd_validate(struct lyd_node **tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t val_opts, |
1447 | | ly_bool validate_subtree, struct ly_set *node_when_p, struct ly_set *node_exts_p, |
1448 | | struct ly_set *node_types_p, struct ly_set *meta_types_p, struct lyd_node **diff) |
1449 | 0 | { |
1450 | 0 | LY_ERR ret = LY_SUCCESS; |
1451 | 0 | struct lyd_node *first, *next, **first2, *iter; |
1452 | 0 | const struct lys_module *mod; |
1453 | 0 | struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}, node_exts = {0}; |
1454 | 0 | uint32_t i = 0; |
1455 | |
|
1456 | 0 | assert(tree && ctx); |
1457 | 0 | assert((node_when_p && node_exts_p && node_types_p && meta_types_p) || |
1458 | 0 | (!node_when_p && !node_exts_p && !node_types_p && !meta_types_p)); |
1459 | |
|
1460 | 0 | if (!node_when_p) { |
1461 | 0 | node_when_p = &node_when; |
1462 | 0 | node_exts_p = &node_exts; |
1463 | 0 | node_types_p = &node_types; |
1464 | 0 | meta_types_p = &meta_types; |
1465 | 0 | } |
1466 | |
|
1467 | 0 | next = *tree; |
1468 | 0 | while (1) { |
1469 | 0 | if (val_opts & LYD_VALIDATE_PRESENT) { |
1470 | 0 | mod = lyd_data_next_module(&next, &first); |
1471 | 0 | } else { |
1472 | 0 | mod = lyd_mod_next_module(next, module, ctx, &i, &first); |
1473 | 0 | } |
1474 | 0 | if (!mod) { |
1475 | 0 | break; |
1476 | 0 | } |
1477 | 0 | if (!first || (first == *tree)) { |
1478 | | /* make sure first2 changes are carried to tree */ |
1479 | 0 | first2 = tree; |
1480 | 0 | } else { |
1481 | 0 | first2 = &first; |
1482 | 0 | } |
1483 | | |
1484 | | /* validate new top-level nodes of this module, autodelete */ |
1485 | 0 | ret = lyd_validate_new(first2, NULL, mod, diff); |
1486 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1487 | | |
1488 | | /* add all top-level defaults for this module, if going to validate subtree, do not add into unres sets |
1489 | | * (lyd_validate_subtree() adds all the nodes in that case) */ |
1490 | 0 | ret = lyd_new_implicit_r(NULL, first2, NULL, mod, validate_subtree ? NULL : node_when_p, validate_subtree ? NULL : node_exts_p, |
1491 | 0 | validate_subtree ? NULL : node_types_p, (val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, diff); |
1492 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1493 | | |
1494 | | /* our first module node pointer may no longer be the first */ |
1495 | 0 | while (*first2 && (*first2)->prev->next && (lyd_owner_module(*first2) == lyd_owner_module((*first2)->prev))) { |
1496 | 0 | *first2 = (*first2)->prev; |
1497 | 0 | } |
1498 | |
|
1499 | 0 | if (validate_subtree) { |
1500 | | /* process nested nodes */ |
1501 | 0 | LY_LIST_FOR(*first2, iter) { |
1502 | 0 | ret = lyd_validate_subtree(iter, node_when_p, node_exts_p, node_types_p, meta_types_p, |
1503 | 0 | (val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, diff); |
1504 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1505 | 0 | } |
1506 | 0 | } |
1507 | | |
1508 | | /* finish incompletely validated terminal values/attributes and when conditions */ |
1509 | 0 | ret = lyd_validate_unres(first2, mod, node_when_p, node_exts_p, node_types_p, meta_types_p, diff); |
1510 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1511 | | |
1512 | | /* perform final validation that assumes the data tree is final */ |
1513 | 0 | ret = lyd_validate_final_r(*first2, NULL, NULL, mod, val_opts, 0); |
1514 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1515 | 0 | } |
1516 | | |
1517 | 0 | cleanup: |
1518 | 0 | ly_set_erase(&node_when, NULL); |
1519 | 0 | ly_set_erase(&node_exts, NULL); |
1520 | 0 | ly_set_erase(&node_types, NULL); |
1521 | 0 | ly_set_erase(&meta_types, NULL); |
1522 | 0 | return ret; |
1523 | 0 | } |
1524 | | |
1525 | | API LY_ERR |
1526 | | lyd_validate_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t val_opts, struct lyd_node **diff) |
1527 | 0 | { |
1528 | 0 | LY_CHECK_ARG_RET(NULL, tree, *tree || ctx, LY_EINVAL); |
1529 | 0 | if (!ctx) { |
1530 | 0 | ctx = LYD_CTX(*tree); |
1531 | 0 | } |
1532 | 0 | if (diff) { |
1533 | 0 | *diff = NULL; |
1534 | 0 | } |
1535 | |
|
1536 | 0 | return lyd_validate(tree, NULL, ctx, val_opts, 1, NULL, NULL, NULL, NULL, diff); |
1537 | 0 | } |
1538 | | |
1539 | | API LY_ERR |
1540 | | lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, uint32_t val_opts, struct lyd_node **diff) |
1541 | 0 | { |
1542 | 0 | LY_CHECK_ARG_RET(NULL, tree, *tree || module, LY_EINVAL); |
1543 | 0 | if (diff) { |
1544 | 0 | *diff = NULL; |
1545 | 0 | } |
1546 | |
|
1547 | 0 | return lyd_validate(tree, module, (*tree) ? LYD_CTX(*tree) : module->ctx, val_opts, 1, NULL, NULL, NULL, NULL, diff); |
1548 | 0 | } |
1549 | | |
1550 | | /** |
1551 | | * @brief Find nodes for merging an operation into data tree for validation. |
1552 | | * |
1553 | | * @param[in] op_tree Full operation data tree. |
1554 | | * @param[in] op_node Operation node itself. |
1555 | | * @param[in] tree Data tree to be merged into. |
1556 | | * @param[out] op_subtree Operation subtree to merge. |
1557 | | * @param[out] tree_sibling Data tree sibling to merge next to, is set if @p tree_parent is NULL. |
1558 | | * @param[out] tree_parent Data tree parent to merge into, is set if @p tree_sibling is NULL. |
1559 | | */ |
1560 | | static void |
1561 | | lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree, |
1562 | | struct lyd_node **op_subtree, struct lyd_node **tree_sibling, struct lyd_node **tree_parent) |
1563 | 0 | { |
1564 | 0 | const struct lyd_node *tree_iter, *op_iter; |
1565 | 0 | struct lyd_node *match; |
1566 | 0 | uint32_t i, cur_depth, op_depth; |
1567 | |
|
1568 | 0 | *op_subtree = NULL; |
1569 | 0 | *tree_sibling = NULL; |
1570 | 0 | *tree_parent = NULL; |
1571 | | |
1572 | | /* learn op depth (top-level being depth 0) */ |
1573 | 0 | op_depth = 0; |
1574 | 0 | for (op_iter = op_node; op_iter != op_tree; op_iter = lyd_parent(op_iter)) { |
1575 | 0 | ++op_depth; |
1576 | 0 | } |
1577 | | |
1578 | | /* find where to merge op */ |
1579 | 0 | tree_iter = tree; |
1580 | 0 | cur_depth = op_depth; |
1581 | 0 | while (cur_depth && tree_iter) { |
1582 | | /* find op iter in tree */ |
1583 | 0 | lyd_find_sibling_first(tree_iter, op_iter, &match); |
1584 | 0 | if (!match) { |
1585 | 0 | break; |
1586 | 0 | } |
1587 | | |
1588 | | /* move tree_iter */ |
1589 | 0 | tree_iter = lyd_child(match); |
1590 | | |
1591 | | /* move depth */ |
1592 | 0 | --cur_depth; |
1593 | | |
1594 | | /* find next op parent */ |
1595 | 0 | op_iter = op_node; |
1596 | 0 | for (i = 0; i < cur_depth; ++i) { |
1597 | 0 | op_iter = lyd_parent(op_iter); |
1598 | 0 | } |
1599 | 0 | } |
1600 | |
|
1601 | 0 | assert(op_iter); |
1602 | 0 | *op_subtree = (struct lyd_node *)op_iter; |
1603 | 0 | if (!tree || tree_iter) { |
1604 | | /* there is no tree whatsoever or this is the last found sibling */ |
1605 | 0 | *tree_sibling = (struct lyd_node *)tree_iter; |
1606 | 0 | } else { |
1607 | | /* matching parent was found but it has no children to insert next to */ |
1608 | 0 | assert(match); |
1609 | 0 | *tree_parent = match; |
1610 | 0 | } |
1611 | 0 | } |
1612 | | |
1613 | | /** |
1614 | | * @brief Validate an RPC/action request, reply, or notification. |
1615 | | * |
1616 | | * @param[in] op_tree Full operation data tree. |
1617 | | * @param[in] op_node Operation node itself. |
1618 | | * @param[in] dep_tree Tree to be used for validating references from the operation subtree. |
1619 | | * @param[in] int_opts Internal parser options. |
1620 | | * @param[in] validate_subtree Whether subtree was already validated (as part of data parsing) or not (separate validation). |
1621 | | * @param[in] node_when_p Set of nodes with when conditions, if NULL a local set is used. |
1622 | | * @param[in] node_exts Set of nodes with extension instances with validation plugin callback, if NULL a local set is used. |
1623 | | * @param[in] node_types_p Set of unres node types, if NULL a local set is used. |
1624 | | * @param[in] meta_types_p Set of unres metadata types, if NULL a local set is used. |
1625 | | * @param[out] diff Optional diff with any changes made by the validation. |
1626 | | * @return LY_SUCCESS on success. |
1627 | | * @return LY_ERR error on error. |
1628 | | */ |
1629 | | static LY_ERR |
1630 | | _lyd_validate_op(struct lyd_node *op_tree, struct lyd_node *op_node, const struct lyd_node *dep_tree, |
1631 | | uint32_t int_opts, ly_bool validate_subtree, struct ly_set *node_when_p, struct ly_set *node_exts_p, |
1632 | | struct ly_set *node_types_p, struct ly_set *meta_types_p, struct lyd_node **diff) |
1633 | 0 | { |
1634 | 0 | LY_ERR rc = LY_SUCCESS; |
1635 | 0 | struct lyd_node *tree_sibling, *tree_parent, *op_subtree, *op_parent, *child; |
1636 | 0 | struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}, node_exts = {0}; |
1637 | |
|
1638 | 0 | assert(op_tree && op_node); |
1639 | 0 | assert((node_when_p && node_exts_p && node_types_p && meta_types_p) || |
1640 | 0 | (!node_when_p && !node_exts_p && !node_types_p && !meta_types_p)); |
1641 | |
|
1642 | 0 | if (!node_when_p) { |
1643 | 0 | node_when_p = &node_when; |
1644 | 0 | node_exts_p = &node_exts; |
1645 | 0 | node_types_p = &node_types; |
1646 | 0 | meta_types_p = &meta_types; |
1647 | 0 | } |
1648 | | |
1649 | | /* merge op_tree into dep_tree */ |
1650 | 0 | lyd_val_op_merge_find(op_tree, op_node, dep_tree, &op_subtree, &tree_sibling, &tree_parent); |
1651 | 0 | op_parent = lyd_parent(op_subtree); |
1652 | 0 | lyd_unlink_tree(op_subtree); |
1653 | 0 | lyd_insert_node(tree_parent, &tree_sibling, op_subtree); |
1654 | 0 | if (!dep_tree) { |
1655 | 0 | dep_tree = tree_sibling; |
1656 | 0 | } |
1657 | |
|
1658 | 0 | LOG_LOCSET(NULL, op_node, NULL, NULL); |
1659 | |
|
1660 | 0 | if (int_opts & LYD_INTOPT_REPLY) { |
1661 | | /* add output children defaults */ |
1662 | 0 | rc = lyd_new_implicit_r(op_node, lyd_node_child_p(op_node), NULL, NULL, node_when_p, node_exts_p, node_types_p, |
1663 | 0 | LYD_IMPLICIT_OUTPUT, diff); |
1664 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1665 | |
|
1666 | 0 | if (validate_subtree) { |
1667 | | /* skip validating the operation itself, go to children directly */ |
1668 | 0 | LY_LIST_FOR(lyd_child(op_node), child) { |
1669 | 0 | rc = lyd_validate_subtree(child, node_when_p, node_exts_p, node_types_p, meta_types_p, 0, diff); |
1670 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1671 | 0 | } |
1672 | 0 | } |
1673 | 0 | } else { |
1674 | 0 | if (validate_subtree) { |
1675 | | /* prevalidate whole operation subtree */ |
1676 | 0 | rc = lyd_validate_subtree(op_node, node_when_p, node_exts_p, node_types_p, meta_types_p, 0, diff); |
1677 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1678 | 0 | } |
1679 | 0 | } |
1680 | | |
1681 | | /* finish incompletely validated terminal values/attributes and when conditions on the full tree */ |
1682 | 0 | LY_CHECK_GOTO(rc = lyd_validate_unres((struct lyd_node **)&dep_tree, NULL, |
1683 | 0 | node_when_p, node_exts_p, node_types_p, meta_types_p, diff), cleanup); |
1684 | | |
1685 | | /* perform final validation of the operation/notification */ |
1686 | 0 | lyd_validate_obsolete(op_node); |
1687 | 0 | LY_CHECK_GOTO(rc = lyd_validate_must(op_node, int_opts), cleanup); |
1688 | | |
1689 | | /* final validation of all the descendants */ |
1690 | 0 | LY_CHECK_GOTO(rc = lyd_validate_final_r(lyd_child(op_node), op_node, op_node->schema, NULL, 0, int_opts), cleanup); |
1691 | |
|
1692 | 0 | cleanup: |
1693 | 0 | LOG_LOCBACK(0, 1, 0, 0); |
1694 | | /* restore operation tree */ |
1695 | 0 | lyd_unlink_tree(op_subtree); |
1696 | 0 | if (op_parent) { |
1697 | 0 | lyd_insert_node(op_parent, NULL, op_subtree); |
1698 | 0 | } |
1699 | |
|
1700 | 0 | ly_set_erase(&node_when, NULL); |
1701 | 0 | ly_set_erase(&node_exts, NULL); |
1702 | 0 | ly_set_erase(&node_types, NULL); |
1703 | 0 | ly_set_erase(&meta_types, NULL); |
1704 | 0 | return rc; |
1705 | 0 | } |
1706 | | |
1707 | | API LY_ERR |
1708 | | lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *dep_tree, enum lyd_type data_type, struct lyd_node **diff) |
1709 | 0 | { |
1710 | 0 | struct lyd_node *op_node; |
1711 | 0 | uint32_t int_opts; |
1712 | |
|
1713 | 0 | LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !dep_tree || !dep_tree->parent, (data_type == LYD_TYPE_RPC_YANG) || |
1714 | 0 | (data_type == LYD_TYPE_NOTIF_YANG) || (data_type == LYD_TYPE_REPLY_YANG), LY_EINVAL); |
1715 | 0 | if (diff) { |
1716 | 0 | *diff = NULL; |
1717 | 0 | } |
1718 | 0 | if (data_type == LYD_TYPE_RPC_YANG) { |
1719 | 0 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION; |
1720 | 0 | } else if (data_type == LYD_TYPE_NOTIF_YANG) { |
1721 | 0 | int_opts = LYD_INTOPT_NOTIF; |
1722 | 0 | } else { |
1723 | 0 | int_opts = LYD_INTOPT_REPLY; |
1724 | 0 | } |
1725 | | |
1726 | | /* find the operation/notification */ |
1727 | 0 | LYD_TREE_DFS_BEGIN(op_tree, op_node) { |
1728 | 0 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) && |
1729 | 0 | (op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
1730 | 0 | break; |
1731 | 0 | } else if ((int_opts & LYD_INTOPT_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) { |
1732 | 0 | break; |
1733 | 0 | } |
1734 | 0 | LYD_TREE_DFS_END(op_tree, op_node); |
1735 | 0 | } |
1736 | 0 | if (int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) { |
1737 | 0 | if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
1738 | 0 | LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found."); |
1739 | 0 | return LY_EINVAL; |
1740 | 0 | } |
1741 | 0 | } else { |
1742 | 0 | if (op_node->schema->nodetype != LYS_NOTIF) { |
1743 | 0 | LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found."); |
1744 | 0 | return LY_EINVAL; |
1745 | 0 | } |
1746 | 0 | } |
1747 | | |
1748 | | /* validate */ |
1749 | 0 | return _lyd_validate_op(op_tree, op_node, dep_tree, int_opts, 1, NULL, NULL, NULL, NULL, diff); |
1750 | 0 | } |