blob: c7c1ae8ac688f2452a27591f3814c7266c412827 [file] [log] [blame]
Michal Vaskocde73ac2019-11-14 16:10:27 +01001/**
2 * @file validation.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Validation
5 *
6 * Copyright (c) 2019 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
Michal Vasko52927e22020-03-16 17:26:14 +010015#include "common.h"
16
Michal Vaskocde73ac2019-11-14 16:10:27 +010017#include <assert.h>
18#include <string.h>
Michal Vasko52927e22020-03-16 17:26:14 +010019#include <stdio.h>
20#include <stdlib.h>
Michal Vaskocde73ac2019-11-14 16:10:27 +010021
Michal Vaskocde73ac2019-11-14 16:10:27 +010022#include "xpath.h"
23#include "tree_data_internal.h"
Michal Vasko14654712020-02-06 08:35:21 +010024#include "tree_schema_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010025
Michal Vaskof03ed032020-03-04 13:31:44 +010026static struct lyd_node *
27lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
28 const struct lysc_node *parent, const struct lysc_module *module)
29{
30 const struct lysc_node *siter = NULL;
31 struct lyd_node *match = NULL;
32
33 assert(parent || module);
34 assert(!last || (slast && *slast));
35
36 if (slast) {
37 siter = *slast;
38 }
39
40 if (last && last->next) {
41 /* find next data instance */
42 lyd_find_sibling_next2(last->next, siter, NULL, 0, &match);
43 if (match) {
44 return match;
45 }
46 }
47
48 /* find next schema node data instance */
49 while ((siter = lys_getnext(siter, parent, module, 0))) {
50 switch (siter->nodetype) {
51 case LYS_CONTAINER:
52 case LYS_ANYXML:
53 case LYS_ANYDATA:
54 case LYS_LEAF:
55 lyd_find_sibling_val(sibling, siter, NULL, 0, &match);
56 break;
57 case LYS_LIST:
58 case LYS_LEAFLIST:
59 lyd_find_sibling_next2(sibling, siter, NULL, 0, &match);
60 break;
61 default:
62 assert(0);
63 LOGINT(NULL);
64 }
65
66 if (match) {
67 break;
68 }
69 }
70
71 if (slast) {
72 *slast = siter;
73 }
74 return match;
75}
76
Michal Vaskocde73ac2019-11-14 16:10:27 +010077/**
78 * @brief Evaluate a single "when" condition.
79 *
Michal Vaskob1b5c262020-03-05 14:29:47 +010080 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +010081 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +010082 * @param[in] when When to evaluate.
Michal Vaskocde73ac2019-11-14 16:10:27 +010083 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
84 */
85static LY_ERR
Michal Vaskoc193ce92020-03-06 11:04:48 +010086lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when)
Michal Vaskocde73ac2019-11-14 16:10:27 +010087{
Michal Vaskob1b5c262020-03-05 14:29:47 +010088 LY_ERR ret = LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +010089 const struct lyd_node *ctx_node;
90 struct lyxp_set xp_set;
91
92 memset(&xp_set, 0, sizeof xp_set);
93
94 if (when->context == node->schema) {
95 ctx_node = node;
96 } else {
97 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
98 ctx_node = (struct lyd_node *)node->parent;
99 }
100
101 /* evaluate when */
Michal Vasko52927e22020-03-16 17:26:14 +0100102 ret = lyxp_eval(when->cond, LYD_SCHEMA, when->module, ctx_node, ctx_node ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100103 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100104 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
105
106 /* return error or LY_EINCOMPLETE for dependant unresolved when */
107 LY_CHECK_RET(ret);
108
109 /* take action based on the result */
110 if (!xp_set.val.bool) {
111 if (node->flags & LYD_WHEN_TRUE) {
112 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100113 if (LYD_DEL_IS_ROOT(*tree, node)) {
114 *tree = (*tree)->next;
115 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100116 lyd_free_tree(node);
117 } else {
118 /* invalid data */
Michal Vaskocc048b22020-03-27 15:52:38 +0100119 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100120 ret = LY_EVALID;
121 }
122 } else {
123 /* remember that when evaluated to true */
124 node->flags |= LYD_WHEN_TRUE;
125 }
126
127 return ret;
128}
129
130LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100131lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *meta_types,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100132 LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100133{
134 LY_ERR ret = LY_SUCCESS;
135 uint32_t u;
136
Michal Vaskob1b5c262020-03-05 14:29:47 +0100137 if (node_when) {
138 /* evaluate all when conditions */
139 uint32_t prev_count;
140 do {
141 prev_count = node_when->count;
142 u = 0;
143 while (u < node_when->count) {
144 /* evaluate all when expressions that affect this node's existence */
145 struct lyd_node *node = (struct lyd_node *)node_when->objs[u];
146 const struct lysc_node *schema = node->schema;
147 int unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100148
Michal Vaskob1b5c262020-03-05 14:29:47 +0100149 do {
150 uint32_t i;
151 LY_ARRAY_FOR(schema->when, i) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100152 ret = lyd_validate_when(tree, node, schema->when[i]);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100153 if (ret) {
154 break;
155 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100156 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100157 if (ret == LY_EINCOMPLETE) {
158 /* could not evaluate this when */
159 unres_when = 1;
160 break;
161 } else if (ret) {
162 /* error */
163 return ret;
164 }
165 schema = schema->parent;
166 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100167
Michal Vaskob1b5c262020-03-05 14:29:47 +0100168 if (unres_when) {
169 /* keep in set and go to the next node */
170 ++u;
171 } else {
172 /* remove this node from the set */
173 ly_set_rm_index(node_when, u, NULL);
174 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100175 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100176
Michal Vaskob1b5c262020-03-05 14:29:47 +0100177 /* there must have been some when conditions resolved */
178 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100179
Michal Vaskob1b5c262020-03-05 14:29:47 +0100180 /* there could have been no cyclic when dependencies, checked during compilation */
181 assert(!node_when->count);
182 }
183
184 if (node_types && node_types->count) {
185 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
186 u = node_types->count;
187 do {
188 --u;
189
190 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[u];
191
192 /* validate and store the value of the node */
193 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
194 parser_data, format, *tree);
195 LY_CHECK_RET(ret);
196
197 /* remove this node from the set */
198 ly_set_rm_index(node_types, u, NULL);
199 } while (u);
200 }
201
Michal Vasko9f96a052020-03-10 09:41:45 +0100202 if (meta_types && meta_types->count) {
203 /* ... and metadata values */
204 u = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100205 do {
206 --u;
207
Michal Vasko9f96a052020-03-10 09:41:45 +0100208 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[u];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100209
Michal Vasko9f96a052020-03-10 09:41:45 +0100210 /* validate and store the value of the metadata */
211 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original,
212 strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100213 LY_CHECK_RET(ret);
214
215 /* remove this attr from the set */
Michal Vasko9f96a052020-03-10 09:41:45 +0100216 ly_set_rm_index(meta_types, u, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100217 } while (u);
218 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100219
220 return ret;
221}
222
Michal Vaskob1b5c262020-03-05 14:29:47 +0100223static LY_ERR
224lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100225{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100226 struct lyd_node **match_p;
227 int fail = 0;
228
229 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
230 /* duplicate instances allowed */
231 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100232 }
233
Michal Vaskob1b5c262020-03-05 14:29:47 +0100234 /* find exactly the same next instance using hashes if possible */
235 if (node->parent && node->parent->children_ht) {
236 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
237 fail = 1;
238 }
239 } else {
240 for (; first; first = first->next) {
241 if (first == node) {
242 continue;
243 }
244
245 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
246 if (first->schema == node->schema) {
247 fail = 1;
248 break;
249 }
250 } else if (!lyd_compare(first, node, 0)) {
251 fail = 1;
252 break;
253 }
254 }
255 }
256
257 if (fail) {
258 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
259 return LY_EVALID;
260 }
261 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100262}
263
264static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100265lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic)
266{
267 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
268 struct lyd_node *match, *to_del;
269 int found;
270
271 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
272 found = 0;
273 iter = NULL;
274 match = NULL;
275 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
276 if (match->flags & LYD_NEW) {
277 /* a new case data found, nothing more to look for */
278 found = 2;
279 break;
280 } else {
281 /* and old case data found */
282 if (found == 0) {
283 found = 1;
284 }
285 }
286 }
287
288 if (found == 1) {
289 /* there should not be 2 old cases */
290 if (old_case) {
291 /* old data from 2 cases */
292 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
293 return LY_EVALID;
294 }
295
296 /* remember an old existing case */
297 old_case = scase;
298 } else if (found == 2) {
299 if (new_case) {
300 /* new data from 2 cases */
301 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
302 return LY_EVALID;
303 }
304
305 /* remember a new existing case */
306 new_case = scase;
307 }
308 }
309
310 if (old_case && new_case) {
311 /* auto-delete old case */
312 iter = NULL;
313 match = NULL;
314 to_del = NULL;
315 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
316 if (LYD_DEL_IS_ROOT(*first, to_del)) {
317 *first = (*first)->next;
318 }
319 lyd_free_tree(to_del);
320 to_del = match;
321 }
322 if (LYD_DEL_IS_ROOT(*first, to_del)) {
323 *first = (*first)->next;
324 }
325 lyd_free_tree(to_del);
326 }
327
328 return LY_SUCCESS;
329}
330
331static int
332lyd_val_has_default(const struct lysc_node *schema)
333{
334 switch (schema->nodetype) {
335 case LYS_LEAF:
336 if (((struct lysc_node_leaf *)schema)->dflt) {
337 return 1;
338 }
339 break;
340 case LYS_LEAFLIST:
341 if (((struct lysc_node_leaflist *)schema)->dflts) {
342 return 1;
343 }
344 break;
345 case LYS_CONTAINER:
346 if (!(schema->flags & LYS_PRESENCE)) {
347 return 1;
348 }
349 break;
350 default:
351 break;
352 }
353
354 return 0;
355}
356
357static void
358lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p)
359{
360 struct lyd_node *match, *next;
361
362 if (lyd_val_has_default(node->schema)) {
363 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
364 if (node->schema->nodetype == LYS_LEAFLIST) {
365 lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match);
366 } else {
367 lyd_find_sibling_val(*first, node->schema, NULL, 0, &match);
368 }
369
370 while (match) {
371 next = match->next;
372 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
373 /* default instance found, remove it */
374 if (LYD_DEL_IS_ROOT(*first, match)) {
375 *first = (*first)->next;
376 }
377 if (match == *next_p) {
378 *next_p = (*next_p)->next;
379 }
380 lyd_free_tree(match);
381
382 /* remove only a single container/leaf default instance, if there are more, it is an error */
383 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
384 break;
385 }
386 }
387
388 lyd_find_sibling_next2(next, node->schema, NULL, 0, &match);
389 }
390 }
391}
392
393LY_ERR
394lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod)
395{
396 struct lyd_node *next, *node;
397 const struct lysc_node *snode = NULL;
398
399 assert(first && (sparent || mod));
400
401 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
402 /* check case duplicites */
403 if (snode->nodetype == LYS_CHOICE) {
404 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode));
405 }
406 }
407
408 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100409 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100410 /* all top-level data from this module checked */
411 break;
412 }
413
414 if (!(node->flags & LYD_NEW)) {
415 /* check only new nodes */
416 continue;
417 }
418
419 /* remove old default(s) if it exists */
420 lyd_validate_autodel_dup(first, node, &next);
421
422 /* then check new node instance duplicities */
423 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
424
425 /* this node is valid */
426 node->flags &= ~LYD_NEW;
427 }
428
429 return LY_SUCCESS;
430}
431
432static LY_ERR
433lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100434{
Michal Vaskoa3881362020-01-21 15:57:35 +0100435 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100436 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100437 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100438 return LY_SUCCESS;
439 }
440 } else {
441 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100442
Michal Vaskob1b5c262020-03-05 14:29:47 +0100443 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100444 /* data instance found */
445 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100446 }
447 }
448
449 /* node instance not found */
450 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
451 return LY_EVALID;
452}
453
454static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100455lyd_validate_minmax(const struct lyd_node *first, const struct lysc_node *snode, uint32_t min, uint32_t max)
Michal Vaskoa3881362020-01-21 15:57:35 +0100456{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100457 uint32_t count = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100458 const struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100459
Michal Vasko9b368d32020-02-14 13:53:31 +0100460 assert(min || max);
461
Michal Vaskob1b5c262020-03-05 14:29:47 +0100462 LY_LIST_FOR(first, iter) {
Michal Vaskoacd83e72020-02-04 14:12:01 +0100463 if (iter->schema == snode) {
464 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100465
466 if (min && (count == min)) {
467 /* satisfied */
468 min = 0;
469 if (!max) {
470 /* nothing more to check */
471 break;
472 }
473 }
474 if (max && (count > max)) {
475 /* not satisifed */
476 break;
477 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100478 }
479 }
480
Michal Vasko9b368d32020-02-14 13:53:31 +0100481 if (min) {
482 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100483 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
484 return LY_EVALID;
485 } else if (max && (count > max)) {
486 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
487 return LY_EVALID;
488 }
489
Michal Vaskoa3881362020-01-21 15:57:35 +0100490 return LY_SUCCESS;
491}
492
Michal Vasko14654712020-02-06 08:35:21 +0100493static struct lyd_node *
494lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, struct lyd_node *list)
495{
Michal Vasko9b368d32020-02-14 13:53:31 +0100496 struct lyd_node *node;
497 const struct lysc_node *iter;
498 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100499
Michal Vasko9b368d32020-02-14 13:53:31 +0100500 /* get leaf depth */
501 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = iter->parent) {
502 if (!(iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
503 ++depth;
504 }
Michal Vasko14654712020-02-06 08:35:21 +0100505 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100506
507 node = list;
508 while (node && depth) {
509 /* find schema node with this depth */
510 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = iter->parent) {
511 if (!(iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
512 --i;
513 }
514 }
515
516 /* find iter instance in children */
517 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
518 lyd_find_sibling_val(lyd_node_children(node), iter, NULL, 0, &node);
519 --depth;
520 }
521
Michal Vasko14654712020-02-06 08:35:21 +0100522 return node;
523}
524
525/*
526 * actions (cb_data):
527 * 0 - compare all uniques
528 * n - compare n-th unique
529 */
530static int
531lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
532{
533 struct ly_ctx *ctx;
534 struct lysc_node_list *slist;
535 struct lyd_node *diter, *first, *second;
536 struct lyd_value *val1, *val2;
537 char *path1, *path2, *uniq_str, *ptr;
538 uint32_t i, j, action;
539
540 assert(val1_p && val2_p);
541
542 first = *((struct lyd_node **)val1_p);
543 second = *((struct lyd_node **)val2_p);
544 action = (uintptr_t)cb_data;
545
546 assert(first && (first->schema->nodetype == LYS_LIST));
547 assert(second && (second->schema == first->schema));
548
549 ctx = first->schema->module->ctx;
550
551 slist = (struct lysc_node_list *)first->schema;
552
553 /* compare unique leaves */
554 if (action > 0) {
555 i = action - 1;
556 if (i < LY_ARRAY_SIZE(slist->uniques)) {
557 goto uniquecheck;
558 }
559 }
560 LY_ARRAY_FOR(slist->uniques, i) {
561uniquecheck:
562 LY_ARRAY_FOR(slist->uniques[i], j) {
563 /* first */
564 diter = lyd_val_uniq_find_leaf(slist->uniques[i][j], first);
565 if (diter) {
566 val1 = &((struct lyd_node_term *)diter)->value;
567 } else {
568 /* use default value */
569 val1 = slist->uniques[i][j]->dflt;
570 }
571
572 /* second */
573 diter = lyd_val_uniq_find_leaf(slist->uniques[i][j], second);
574 if (diter) {
575 val2 = &((struct lyd_node_term *)diter)->value;
576 } else {
577 /* use default value */
578 val2 = slist->uniques[i][j]->dflt;
579 }
580
581 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
582 /* values differ or either one is not set */
583 break;
584 }
585 }
586 if (j && (j == LY_ARRAY_SIZE(slist->uniques[i]))) {
587 /* all unique leafs are the same in this set, create this nice error */
588 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
589 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
590
591 /* use buffer to rebuild the unique string */
592 uniq_str = malloc(1024);
593 uniq_str[0] = '\0';
594 ptr = uniq_str;
595 LY_ARRAY_FOR(slist->uniques[i], j) {
596 if (j) {
597 strcpy(ptr, " ");
598 ++ptr;
599 }
600 ptr = lysc_path_until((struct lysc_node *)slist->uniques[i][j], (struct lysc_node *)slist, LYSC_PATH_LOG,
601 ptr, 1024 - (ptr - uniq_str));
602 if (!ptr) {
603 /* path will be incomplete, whatever */
604 break;
605 }
606
607 ptr += strlen(ptr);
608 }
609 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
610
611 free(path1);
612 free(path2);
613 free(uniq_str);
614 return 1;
615 }
616
617 if (action > 0) {
618 /* done */
619 return 0;
620 }
621 }
622
623 return 0;
624}
625
Michal Vaskoa3881362020-01-21 15:57:35 +0100626static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100627lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, struct lysc_node_leaf ***uniques)
Michal Vaskoa3881362020-01-21 15:57:35 +0100628{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100629 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100630 struct ly_set *set;
631 uint32_t i, j, n = 0;
632 LY_ERR ret = LY_SUCCESS;
633 uint32_t hash, u, usize = 0;
634 int dynamic;
635 const char *str;
636 struct hash_table **uniqtables = NULL;
637 struct lyd_value *val;
638 struct ly_ctx *ctx = snode->module->ctx;
639
640 assert(uniques);
641
642 /* get all list instances */
Michal Vasko9b368d32020-02-14 13:53:31 +0100643 set = ly_set_new();
644 LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100645 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100646 if (diter->schema == snode) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100647 ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100648 }
649 }
Michal Vasko14654712020-02-06 08:35:21 +0100650
651 if (set->count == 2) {
652 /* simple comparison */
653 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
654 /* instance duplication */
655 ret = LY_EVALID;
656 goto cleanup;
657 }
658 } else if (set->count > 2) {
659 /* use hashes for comparison */
660 /* first, allocate the table, the size depends on number of items in the set */
661 for (u = 31; u > 0; u--) {
662 usize = set->count << u;
663 usize = usize >> u;
664 if (usize == set->count) {
665 break;
666 }
667 }
668 LY_CHECK_ERR_GOTO(!u, LOGINT(ctx); ret = LY_EINT, cleanup);
669 u = 32 - u;
670 usize = 1 << u;
671
672 uniqtables = malloc(LY_ARRAY_SIZE(uniques) * sizeof *uniqtables);
673 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
674 n = LY_ARRAY_SIZE(uniques);
675 for (j = 0; j < n; j++) {
676 uniqtables[j] = lyht_new(usize, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(j + 1L), 0);
677 LY_CHECK_ERR_GOTO(!uniqtables[j], LOGMEM(ctx); ret = LY_EMEM, cleanup);
678 }
679
680 for (u = 0; u < set->count; u++) {
681 /* loop for unique - get the hash for the instances */
682 for (i = 0; i < n; i++) {
683 val = NULL;
684 for (j = hash = 0; j < LY_ARRAY_SIZE(uniques[i]); j++) {
685 diter = lyd_val_uniq_find_leaf(uniques[i][j], set->objs[u]);
686 if (diter) {
687 val = &((struct lyd_node_term *)diter)->value;
688 } else {
689 /* use default value */
690 val = uniques[i][j]->dflt;
691 }
692 if (!val) {
693 /* unique item not present nor has default value */
694 break;
695 }
696
697 /* get canonical string value */
698 str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic);
699 hash = dict_hash_multi(hash, str, strlen(str));
700 if (dynamic) {
701 free((char *)str);
702 }
703 }
704 if (!val) {
705 /* skip this list instance since its unique set is incomplete */
706 continue;
707 }
708
709 /* finish the hash value */
710 hash = dict_hash_multi(hash, NULL, 0);
711
712 /* insert into the hashtable */
713 ret = lyht_insert(uniqtables[i], &set->objs[u], hash, NULL);
714 if (ret == LY_EEXIST) {
715 /* instance duplication */
716 ret = LY_EVALID;
717 }
718 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
719 }
720 }
721 }
722
723cleanup:
724 ly_set_free(set, NULL);
725 for (j = 0; j < n; j++) {
726 if (!uniqtables[j]) {
727 /* failed when allocating uniquetables[j], following j are not allocated */
728 break;
729 }
730 lyht_free(uniqtables[j]);
731 }
732 free(uniqtables);
733
734 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100735}
736
737static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100738lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
739 const struct lysc_module *mod, int val_opts)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100740{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100741 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100742 struct lysc_node_list *slist;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200743 int getnext_opts;
744
745 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (val_opts & LYD_VALOPT_OUTPUT ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100746
Michal Vaskoa3881362020-01-21 15:57:35 +0100747 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200748 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100749 if ((val_opts & LYD_VALOPT_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
750 continue;
751 }
752
Michal Vaskoa3881362020-01-21 15:57:35 +0100753 /* check min-elements and max-elements */
754 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
755 slist = (struct lysc_node_list *)snode;
756 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100757 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100758 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100759
760 /* check generic mandatory existence */
761 } else if (snode->flags & LYS_MAND_TRUE) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100762 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100763 }
764
765 /* check unique */
766 if (snode->nodetype == LYS_LIST) {
767 slist = (struct lysc_node_list *)snode;
768 if (slist->uniques) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100769 LY_CHECK_RET(lyd_validate_unique(first, snode, slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100770 }
771 }
772
Michal Vaskoacd83e72020-02-04 14:12:01 +0100773 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
774 /* go recursively for schema-only nodes */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100775 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100776 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100777 }
778
Michal Vaskoacd83e72020-02-04 14:12:01 +0100779 return LY_SUCCESS;
780}
781
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100782static void
783lyd_validate_obsolete(const struct lyd_node *node)
784{
785 const struct lysc_node *snode;
786
787 snode = node->schema;
788 do {
789 if (snode->flags & LYS_STATUS_OBSLT) {
790 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
791 break;
792 }
793
794 snode = snode->parent;
795 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
796}
797
Michal Vaskocc048b22020-03-27 15:52:38 +0100798static LY_ERR
Michal Vaskocb7526d2020-03-30 15:08:26 +0200799lyd_validate_must(const struct lyd_node *node, int val_opts)
Michal Vaskocc048b22020-03-27 15:52:38 +0100800{
801 struct lyxp_set xp_set;
802 struct lysc_must *musts;
803 const struct lyd_node *tree;
804 uint32_t u;
805
806 switch (node->schema->nodetype) {
807 case LYS_CONTAINER:
808 musts = ((struct lysc_node_container *)node->schema)->musts;
809 break;
810 case LYS_LEAF:
811 musts = ((struct lysc_node_leaf *)node->schema)->musts;
812 break;
813 case LYS_LEAFLIST:
814 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
815 break;
816 case LYS_LIST:
817 musts = ((struct lysc_node_list *)node->schema)->musts;
818 break;
819 case LYS_ANYXML:
820 case LYS_ANYDATA:
821 musts = ((struct lysc_node_anydata *)node->schema)->musts;
822 break;
823 case LYS_NOTIF:
824 musts = ((struct lysc_notif *)node->schema)->musts;
825 break;
826 case LYS_RPC:
827 case LYS_ACTION:
Michal Vaskocb7526d2020-03-30 15:08:26 +0200828 if (val_opts & LYD_VALOPT_INPUT) {
829 musts = ((struct lysc_action *)node->schema)->input.musts;
830 } else if (val_opts & LYD_VALOPT_OUTPUT) {
831 musts = ((struct lysc_action *)node->schema)->output.musts;
832 } else {
833 LOGINT(LYD_NODE_CTX(node));
834 return LY_EINT;
835 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100836 break;
837 default:
838 LOGINT(LYD_NODE_CTX(node));
839 return LY_EINT;
840 }
841
842 if (!musts) {
843 /* no must to evaluate */
844 return LY_SUCCESS;
845 }
846
847 /* find first top-level node */
848 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent);
849 while (tree->prev->next) {
850 tree = tree->prev;
851 }
852
853 LY_ARRAY_FOR(musts, u) {
854 memset(&xp_set, 0, sizeof xp_set);
855
856 /* evaluate must */
857 LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA));
858
859 /* check the result */
860 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
861 if (!xp_set.val.bool) {
862 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
863 return LY_EVALID;
864 }
865 }
866
867 return LY_SUCCESS;
868}
869
Michal Vaskob1b5c262020-03-05 14:29:47 +0100870LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +0200871lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, int val_opts)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100872{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100873 struct lyd_node *next, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100874 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100875
Michal Vasko14654712020-02-06 08:35:21 +0100876 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100877 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100878 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100879 /* all top-level data from this module checked */
880 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100881 }
882
Michal Vaskoa8c61722020-03-27 16:59:32 +0100883 /* opaque data */
884 if (!node->schema) {
885 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
886 ((struct lyd_node_opaq *)node)->name);
887 return LY_EVALID;
888 }
889
Michal Vaskocb7526d2020-03-30 15:08:26 +0200890 /* no state/input/output data */
Michal Vasko5b37a352020-03-06 13:38:33 +0100891 if ((val_opts & LYD_VALOPT_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200892 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
893 return LY_EVALID;
894 } else if ((val_opts & LYD_VALOPT_INPUT) && (node->schema->flags & LYS_CONFIG_R)) {
895 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
896 return LY_EVALID;
897 } else if ((val_opts & LYD_VALOPT_OUTPUT) && (node->schema->flags & LYS_CONFIG_W)) {
898 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +0100899 return LY_EVALID;
900 }
901
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100902 /* obsolete data */
903 lyd_validate_obsolete(node);
904
Michal Vaskoc193ce92020-03-06 11:04:48 +0100905 /* node's schema if-features */
906 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskoa8c61722020-03-27 16:59:32 +0100907 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +0100908 return LY_EVALID;
909 }
910
Michal Vaskocc048b22020-03-27 15:52:38 +0100911 /* node's musts */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200912 LY_CHECK_RET(lyd_validate_must(node, val_opts));
Michal Vaskocc048b22020-03-27 15:52:38 +0100913
Michal Vaskoa8c61722020-03-27 16:59:32 +0100914 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +0100915 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100916
Michal Vasko14654712020-02-06 08:35:21 +0100917 /* validate schema-based restrictions */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100918 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts));
Michal Vasko14654712020-02-06 08:35:21 +0100919
Michal Vaskob1b5c262020-03-05 14:29:47 +0100920 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +0100921 /* validate all children recursively */
Radek Krejcidae0ee82020-05-06 16:53:24 +0200922 LY_CHECK_RET(lyd_validate_final_r(lyd_node_children(node), node->schema, NULL, val_opts));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100923
Michal Vaskob1b5c262020-03-05 14:29:47 +0100924 /* set default for containers */
925 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Radek Krejcidae0ee82020-05-06 16:53:24 +0200926 LY_LIST_FOR(lyd_node_children(node), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100927 if (!(next->flags & LYD_DEFAULT)) {
928 break;
929 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100930 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100931 if (!next) {
932 node->flags |= LYD_DEFAULT;
933 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100934 }
935 }
936
937 return LY_SUCCESS;
938}
939
940LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100941lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100942 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int val_opts)
Michal Vasko9b368d32020-02-14 13:53:31 +0100943{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100944 LY_ERR ret;
Michal Vasko9b368d32020-02-14 13:53:31 +0100945 const struct lysc_node *iter = NULL;
946 struct lyd_node *node;
947 struct lyd_value **dflts;
948 size_t i;
949
Michal Vaskob1b5c262020-03-05 14:29:47 +0100950 assert(first && (parent || sparent || mod) && node_types && node_when);
Michal Vasko9b368d32020-02-14 13:53:31 +0100951
Michal Vaskob1b5c262020-03-05 14:29:47 +0100952 if (!sparent && parent) {
953 sparent = parent->schema;
954 }
955
956 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100957 if ((val_opts & LYD_VALOPT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
958 continue;
959 }
960
Michal Vasko9b368d32020-02-14 13:53:31 +0100961 switch (iter->nodetype) {
962 case LYS_CHOICE:
Michal Vaskof03ed032020-03-04 13:31:44 +0100963 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100964 /* create default case data */
965 LY_CHECK_RET(lyd_validate_defaults_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100966 NULL, node_types, node_when, val_opts));
Michal Vasko9b368d32020-02-14 13:53:31 +0100967 }
968 break;
969 case LYS_CONTAINER:
970 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100971 /* create default NP container */
Michal Vasko9b368d32020-02-14 13:53:31 +0100972 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100973 node->flags = LYD_DEFAULT;
974 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100975
976 if (iter->when) {
977 /* remember to resolve when */
978 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
979 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100980
981 /* create any default children */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100982 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when, val_opts));
Michal Vasko9b368d32020-02-14 13:53:31 +0100983 }
984 break;
985 case LYS_LEAF:
986 if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
987 /* create default leaf */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100988 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
989 if (ret == LY_EINCOMPLETE) {
990 /* remember to resolve type */
991 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
992 } else if (ret) {
993 return ret;
994 }
995 node->flags = LYD_DEFAULT;
996 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100997
998 if (iter->when) {
999 /* remember to resolve when */
1000 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1001 }
1002 }
1003 break;
1004 case LYS_LEAFLIST:
1005 if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_next2(*first, iter, NULL, 0, NULL)) {
1006 /* create all default leaf-lists */
1007 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1008 LY_ARRAY_FOR(dflts, i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001009 ret = lyd_create_term2(iter, dflts[i], &node);
1010 if (ret == LY_EINCOMPLETE) {
1011 /* remember to resolve type */
1012 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1013 } else if (ret) {
1014 return ret;
1015 }
1016 node->flags = LYD_DEFAULT;
1017 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001018
1019 if (iter->when) {
1020 /* remember to resolve when */
1021 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1022 }
1023 }
1024 }
1025 break;
1026 default:
1027 /* without defaults */
1028 break;
1029 }
1030 }
1031
1032 return LY_SUCCESS;
1033}
1034
Michal Vaskob1b5c262020-03-05 14:29:47 +01001035static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001036lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
1037 struct ly_set *when_check, int val_opts)
1038{
1039 const struct lyd_meta *meta;
1040 struct lyd_node *next, *node;
1041
1042 LYD_TREE_DFS_BEGIN(root, next, node) {
1043 /* skip added default nodes */
1044 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1045 LY_LIST_FOR(node->meta, meta) {
1046 /* metadata type resolution */
1047 ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST);
1048 }
1049
1050 if (node->schema->nodetype & LYD_NODE_TERM) {
1051 /* node type resolution */
1052 ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST);
1053 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1054 /* new node validation, autodelete */
1055 LY_CHECK_RET(lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL));
1056
1057 /* add nested defaults */
1058 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
1059 when_check, val_opts));
1060 }
1061
1062 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1063 /* when evaluation */
1064 ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST);
1065 }
1066 }
1067
1068 LYD_TREE_DFS_END(root, next, node);
1069 }
1070
1071 return LY_SUCCESS;
1072}
1073
1074static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001075_lyd_validate(struct lyd_node **tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
1076 int val_opts)
Michal Vaskof03ed032020-03-04 13:31:44 +01001077{
1078 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001079 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001080 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001081 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001082 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001083
Michal Vaskob1b5c262020-03-05 14:29:47 +01001084 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || (modules && mod_count), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001085
Michal Vasko5b37a352020-03-06 13:38:33 +01001086 if (val_opts & ~LYD_VALOPT_MASK) {
1087 LOGERR(ctx, LY_EINVAL, "Some invalid flags passed to validation.");
1088 return LY_EINVAL;
1089 }
1090
Michal Vaskob1b5c262020-03-05 14:29:47 +01001091 next = *tree;
1092 while (1) {
1093 if (val_opts & LYD_VALOPT_DATA_ONLY) {
1094 mod = lyd_data_next_module(&next, &first);
1095 } else {
1096 mod = lyd_mod_next_module(next, modules, mod_count, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001097 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001098 if (!mod) {
1099 break;
1100 }
1101 if (first == *tree) {
1102 /* make sure first2 changes are carried to tree */
1103 first2 = tree;
1104 } else {
1105 first2 = &first;
1106 }
1107
1108 /* validate new top-level nodes of this module, autodelete */
Michal Vaskofea12c62020-03-30 11:00:15 +02001109 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001110
1111 /* add all top-level defaults for this module */
Michal Vaskofea12c62020-03-30 11:00:15 +02001112 LY_CHECK_GOTO(ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &type_check, &when_check, val_opts), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001113
1114 /* process nested nodes */
1115 LY_LIST_FOR(*first2, first) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001116 LY_CHECK_GOTO(ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001117 }
1118
1119 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vasko9f96a052020-03-10 09:41:45 +01001120 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, LYD_JSON, lydjson_resolve_prefix, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001121 LY_CHECK_GOTO(ret, cleanup);
1122
1123 /* perform final validation that assumes the data tree is final */
Michal Vaskofea12c62020-03-30 11:00:15 +02001124 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, val_opts), cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001125 }
1126
Michal Vaskof03ed032020-03-04 13:31:44 +01001127cleanup:
1128 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001129 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001130 ly_set_erase(&when_check, NULL);
1131 return ret;
1132}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001133
1134API LY_ERR
1135lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts)
1136{
1137 return _lyd_validate(tree, NULL, 0, ctx, val_opts);
1138}
1139
1140API LY_ERR
1141lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts)
1142{
1143 return _lyd_validate(tree, modules, mod_count, NULL, val_opts);
1144}
Michal Vaskofea12c62020-03-30 11:00:15 +02001145
Michal Vaskocb7526d2020-03-30 15:08:26 +02001146static void
1147lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op, const struct lyd_node *tree,
1148 struct lyd_node **op_node, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001149{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001150 const struct lyd_node *tree_iter, *op_iter;
1151 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001152 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001153
Michal Vaskocb7526d2020-03-30 15:08:26 +02001154 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001155 op_depth = 0;
1156 for (op_iter = op; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
1157 ++op_depth;
1158 }
1159
1160 /* find where to merge op */
1161 tree_iter = tree;
1162 cur_depth = op_depth;
1163 op_iter = op;
1164 while (cur_depth) {
1165 /* find next op parent */
1166 op_iter = op;
1167 for (i = 0; i < cur_depth; ++i) {
1168 op_iter = (struct lyd_node *)op_iter->parent;
1169 }
1170
1171 /* find op iter in tree */
1172 lyd_find_sibling_first(tree_iter, op_iter, &match);
1173 if (!match) {
1174 break;
1175 }
1176
1177 /* move tree_iter */
1178 tree_iter = lyd_node_children(match);
1179
1180 /* move depth */
1181 --cur_depth;
1182 }
1183
Michal Vaskocb7526d2020-03-30 15:08:26 +02001184 *op_node = (struct lyd_node *)op_iter;
1185 *tree_sibling = (struct lyd_node *)tree_iter;
1186}
1187
1188API LY_ERR
1189lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, int val_opts)
1190{
1191 LY_ERR ret;
1192 struct lyd_node *tree_sibling, *op_node, *op, *op_parent;
1193 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1194
1195 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
1196 !val_opts || (val_opts == LYD_VALOPT_INPUT) || (val_opts == LYD_VALOPT_OUTPUT), LY_EINVAL);
1197
1198 /* find the operation/notification */
1199 LYD_TREE_DFS_BEGIN(op_tree, op_node, op) {
1200 if ((val_opts & (LYD_VALOPT_INPUT | LYD_VALOPT_OUTPUT)) && (op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1201 break;
1202 } else if (!val_opts && (op->schema->nodetype == LYS_NOTIF)) {
1203 break;
1204 }
1205 LYD_TREE_DFS_END(op_tree, op_node, op);
1206 }
1207 if (val_opts & (LYD_VALOPT_INPUT | LYD_VALOPT_OUTPUT)) {
1208 if (!(op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1209 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
1210 return LY_EINVAL;
1211 }
1212 } else {
1213 if (op->schema->nodetype != LYS_NOTIF) {
1214 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
1215 return LY_EINVAL;
1216 }
1217 }
1218
1219 /* move op_tree to top-level node */
1220 while (op_tree->parent) {
1221 op_tree = (struct lyd_node *)op_tree->parent;
1222 }
1223
1224 /* merge op_tree into tree */
1225 lyd_val_op_merge_find(op_tree, op, tree, &op_node, &tree_sibling);
1226 op_parent = (struct lyd_node *)op_node->parent;
1227 lyd_unlink_tree(op_node);
1228 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001229 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001230 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001231 }
1232
1233 /* prevalidate whole operation subtree */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001234 LY_CHECK_GOTO(ret = lyd_validate_subtree(op, &type_check, &type_meta_check, &when_check, val_opts), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001235
1236 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1237 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
1238 LYD_JSON, lydjson_resolve_prefix, NULL), cleanup);
1239
1240 /* perform final validation of the operation/notification */
1241 lyd_validate_obsolete(op);
1242 if (lysc_node_is_disabled(op->schema, 1)) {
1243 LOGVAL(LYD_NODE_CTX(op_tree), LY_VLOG_LYD, op, LY_VCODE_NOIFF, op->schema->name);
1244 ret = LY_EVALID;
1245 goto cleanup;
1246 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001247 LY_CHECK_GOTO(ret = lyd_validate_must(op, val_opts), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001248
1249 /* final validation of all the descendants */
Radek Krejcidae0ee82020-05-06 16:53:24 +02001250 LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_node_children(op), op->schema, NULL, val_opts), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001251
1252cleanup:
1253 /* restore operation tree */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001254 lyd_unlink_tree(op_node);
1255 if (op_parent) {
1256 lyd_insert_node(op_parent, NULL, op_node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001257 }
1258
1259 ly_set_erase(&type_check, NULL);
1260 ly_set_erase(&type_meta_check, NULL);
1261 ly_set_erase(&when_check, NULL);
1262 return ret;
1263}