blob: 22919f0d69d1fd37868863934f2aa8da06d1231f [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
15#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include <stdint.h>
Michal Vasko52927e22020-03-16 17:26:14 +010017#include <stdio.h>
18#include <stdlib.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <string.h>
Michal Vaskocde73ac2019-11-14 16:10:27 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
22#include "config.h"
23#include "hash_table.h"
24#include "log.h"
25#include "plugins_types.h"
26#include "set.h"
27#include "tree.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010028#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010030#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010032
Michal Vaskof03ed032020-03-04 13:31:44 +010033static struct lyd_node *
34lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
35 const struct lysc_node *parent, const struct lysc_module *module)
36{
37 const struct lysc_node *siter = NULL;
38 struct lyd_node *match = NULL;
39
40 assert(parent || module);
41 assert(!last || (slast && *slast));
42
43 if (slast) {
44 siter = *slast;
45 }
46
47 if (last && last->next) {
48 /* find next data instance */
49 lyd_find_sibling_next2(last->next, siter, NULL, 0, &match);
50 if (match) {
51 return match;
52 }
53 }
54
55 /* find next schema node data instance */
56 while ((siter = lys_getnext(siter, parent, module, 0))) {
57 switch (siter->nodetype) {
58 case LYS_CONTAINER:
59 case LYS_ANYXML:
60 case LYS_ANYDATA:
61 case LYS_LEAF:
62 lyd_find_sibling_val(sibling, siter, NULL, 0, &match);
63 break;
64 case LYS_LIST:
65 case LYS_LEAFLIST:
66 lyd_find_sibling_next2(sibling, siter, NULL, 0, &match);
67 break;
68 default:
69 assert(0);
70 LOGINT(NULL);
71 }
72
73 if (match) {
74 break;
75 }
76 }
77
78 if (slast) {
79 *slast = siter;
80 }
81 return match;
82}
83
Michal Vaskocde73ac2019-11-14 16:10:27 +010084/**
85 * @brief Evaluate a single "when" condition.
86 *
Michal Vaskob1b5c262020-03-05 14:29:47 +010087 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +010088 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +010089 * @param[in] when When to evaluate.
Michal Vaskocde73ac2019-11-14 16:10:27 +010090 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
91 */
92static LY_ERR
Michal Vaskoc193ce92020-03-06 11:04:48 +010093lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when)
Michal Vaskocde73ac2019-11-14 16:10:27 +010094{
Michal Vaskob1b5c262020-03-05 14:29:47 +010095 LY_ERR ret = LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +010096 const struct lyd_node *ctx_node;
97 struct lyxp_set xp_set;
98
99 memset(&xp_set, 0, sizeof xp_set);
100
101 if (when->context == node->schema) {
102 ctx_node = node;
103 } else {
104 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
105 ctx_node = (struct lyd_node *)node->parent;
106 }
107
108 /* evaluate when */
Michal Vasko52927e22020-03-16 17:26:14 +0100109 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 +0100110 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100111 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
112
113 /* return error or LY_EINCOMPLETE for dependant unresolved when */
114 LY_CHECK_RET(ret);
115
116 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +0200117 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100118 if (node->flags & LYD_WHEN_TRUE) {
119 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100120 if (LYD_DEL_IS_ROOT(*tree, node)) {
121 *tree = (*tree)->next;
122 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100123 lyd_free_tree(node);
124 } else {
125 /* invalid data */
Michal Vaskocc048b22020-03-27 15:52:38 +0100126 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100127 ret = LY_EVALID;
128 }
129 } else {
130 /* remember that when evaluated to true */
131 node->flags |= LYD_WHEN_TRUE;
132 }
133
134 return ret;
135}
136
137LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100138lyd_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 +0100139 LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100140{
141 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200142 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100143
Michal Vaskob1b5c262020-03-05 14:29:47 +0100144 if (node_when) {
145 /* evaluate all when conditions */
146 uint32_t prev_count;
147 do {
148 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200149 i = 0;
150 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100151 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200152 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100153 const struct lysc_node *schema = node->schema;
154 int unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100155
Michal Vaskob1b5c262020-03-05 14:29:47 +0100156 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200157 LY_ARRAY_SIZE_TYPE u;
158 LY_ARRAY_FOR(schema->when, u) {
159 ret = lyd_validate_when(tree, node, schema->when[u]);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100160 if (ret) {
161 break;
162 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100163 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100164 if (ret == LY_EINCOMPLETE) {
165 /* could not evaluate this when */
166 unres_when = 1;
167 break;
168 } else if (ret) {
169 /* error */
170 return ret;
171 }
172 schema = schema->parent;
173 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100174
Michal Vaskob1b5c262020-03-05 14:29:47 +0100175 if (unres_when) {
176 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200177 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100178 } else {
179 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200180 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100181 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100182 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100183
Michal Vaskob1b5c262020-03-05 14:29:47 +0100184 /* there must have been some when conditions resolved */
185 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100186
Michal Vaskob1b5c262020-03-05 14:29:47 +0100187 /* there could have been no cyclic when dependencies, checked during compilation */
188 assert(!node_when->count);
189 }
190
191 if (node_types && node_types->count) {
192 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200193 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100194 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200195 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100196
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200197 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100198
199 /* validate and store the value of the node */
200 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
201 parser_data, format, *tree);
202 LY_CHECK_RET(ret);
203
204 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200205 ly_set_rm_index(node_types, i, NULL);
206 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100207 }
208
Michal Vasko9f96a052020-03-10 09:41:45 +0100209 if (meta_types && meta_types->count) {
210 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200211 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100212 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200213 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100214
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200215 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100216
Michal Vasko9f96a052020-03-10 09:41:45 +0100217 /* validate and store the value of the metadata */
218 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original,
219 strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100220 LY_CHECK_RET(ret);
221
222 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200223 ly_set_rm_index(meta_types, i, NULL);
224 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100225 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100226
227 return ret;
228}
229
Michal Vaskob1b5c262020-03-05 14:29:47 +0100230static LY_ERR
231lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100232{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100233 struct lyd_node **match_p;
234 int fail = 0;
235
236 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
237 /* duplicate instances allowed */
238 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100239 }
240
Michal Vaskob1b5c262020-03-05 14:29:47 +0100241 /* find exactly the same next instance using hashes if possible */
242 if (node->parent && node->parent->children_ht) {
243 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
244 fail = 1;
245 }
246 } else {
247 for (; first; first = first->next) {
248 if (first == node) {
249 continue;
250 }
251
252 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
253 if (first->schema == node->schema) {
254 fail = 1;
255 break;
256 }
257 } else if (!lyd_compare(first, node, 0)) {
258 fail = 1;
259 break;
260 }
261 }
262 }
263
264 if (fail) {
265 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
266 return LY_EVALID;
267 }
268 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100269}
270
271static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100272lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic)
273{
274 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
275 struct lyd_node *match, *to_del;
276 int found;
277
278 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
279 found = 0;
280 iter = NULL;
281 match = NULL;
282 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
283 if (match->flags & LYD_NEW) {
284 /* a new case data found, nothing more to look for */
285 found = 2;
286 break;
287 } else {
288 /* and old case data found */
289 if (found == 0) {
290 found = 1;
291 }
292 }
293 }
294
295 if (found == 1) {
296 /* there should not be 2 old cases */
297 if (old_case) {
298 /* old data from 2 cases */
299 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
300 return LY_EVALID;
301 }
302
303 /* remember an old existing case */
304 old_case = scase;
305 } else if (found == 2) {
306 if (new_case) {
307 /* new data from 2 cases */
308 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
309 return LY_EVALID;
310 }
311
312 /* remember a new existing case */
313 new_case = scase;
314 }
315 }
316
317 if (old_case && new_case) {
318 /* auto-delete old case */
319 iter = NULL;
320 match = NULL;
321 to_del = NULL;
322 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
323 if (LYD_DEL_IS_ROOT(*first, to_del)) {
324 *first = (*first)->next;
325 }
326 lyd_free_tree(to_del);
327 to_del = match;
328 }
329 if (LYD_DEL_IS_ROOT(*first, to_del)) {
330 *first = (*first)->next;
331 }
332 lyd_free_tree(to_del);
333 }
334
335 return LY_SUCCESS;
336}
337
338static int
339lyd_val_has_default(const struct lysc_node *schema)
340{
341 switch (schema->nodetype) {
342 case LYS_LEAF:
343 if (((struct lysc_node_leaf *)schema)->dflt) {
344 return 1;
345 }
346 break;
347 case LYS_LEAFLIST:
348 if (((struct lysc_node_leaflist *)schema)->dflts) {
349 return 1;
350 }
351 break;
352 case LYS_CONTAINER:
353 if (!(schema->flags & LYS_PRESENCE)) {
354 return 1;
355 }
356 break;
357 default:
358 break;
359 }
360
361 return 0;
362}
363
364static void
365lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p)
366{
367 struct lyd_node *match, *next;
368
369 if (lyd_val_has_default(node->schema)) {
370 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
371 if (node->schema->nodetype == LYS_LEAFLIST) {
372 lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match);
373 } else {
374 lyd_find_sibling_val(*first, node->schema, NULL, 0, &match);
375 }
376
377 while (match) {
378 next = match->next;
379 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
380 /* default instance found, remove it */
381 if (LYD_DEL_IS_ROOT(*first, match)) {
382 *first = (*first)->next;
383 }
384 if (match == *next_p) {
385 *next_p = (*next_p)->next;
386 }
387 lyd_free_tree(match);
388
389 /* remove only a single container/leaf default instance, if there are more, it is an error */
390 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
391 break;
392 }
393 }
394
395 lyd_find_sibling_next2(next, node->schema, NULL, 0, &match);
396 }
397 }
398}
399
400LY_ERR
401lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod)
402{
403 struct lyd_node *next, *node;
404 const struct lysc_node *snode = NULL;
405
406 assert(first && (sparent || mod));
407
408 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
409 /* check case duplicites */
410 if (snode->nodetype == LYS_CHOICE) {
411 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode));
412 }
413 }
414
415 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100416 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100417 /* all top-level data from this module checked */
418 break;
419 }
420
421 if (!(node->flags & LYD_NEW)) {
422 /* check only new nodes */
423 continue;
424 }
425
426 /* remove old default(s) if it exists */
427 lyd_validate_autodel_dup(first, node, &next);
428
429 /* then check new node instance duplicities */
430 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
431
432 /* this node is valid */
433 node->flags &= ~LYD_NEW;
434 }
435
436 return LY_SUCCESS;
437}
438
439static LY_ERR
440lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100441{
Michal Vaskoa3881362020-01-21 15:57:35 +0100442 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100443 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100444 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100445 return LY_SUCCESS;
446 }
447 } else {
448 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100449
Michal Vaskob1b5c262020-03-05 14:29:47 +0100450 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100451 /* data instance found */
452 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100453 }
454 }
455
456 /* node instance not found */
457 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
458 return LY_EVALID;
459}
460
461static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100462lyd_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 +0100463{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100464 uint32_t count = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100465 const struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100466
Michal Vasko9b368d32020-02-14 13:53:31 +0100467 assert(min || max);
468
Michal Vaskob1b5c262020-03-05 14:29:47 +0100469 LY_LIST_FOR(first, iter) {
Michal Vaskoacd83e72020-02-04 14:12:01 +0100470 if (iter->schema == snode) {
471 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100472
473 if (min && (count == min)) {
474 /* satisfied */
475 min = 0;
476 if (!max) {
477 /* nothing more to check */
478 break;
479 }
480 }
481 if (max && (count > max)) {
482 /* not satisifed */
483 break;
484 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100485 }
486 }
487
Michal Vasko9b368d32020-02-14 13:53:31 +0100488 if (min) {
489 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100490 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
491 return LY_EVALID;
492 } else if (max && (count > max)) {
493 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
494 return LY_EVALID;
495 }
496
Michal Vaskoa3881362020-01-21 15:57:35 +0100497 return LY_SUCCESS;
498}
499
Michal Vasko14654712020-02-06 08:35:21 +0100500static struct lyd_node *
501lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, struct lyd_node *list)
502{
Michal Vasko9b368d32020-02-14 13:53:31 +0100503 struct lyd_node *node;
504 const struct lysc_node *iter;
505 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100506
Michal Vasko9b368d32020-02-14 13:53:31 +0100507 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200508 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
509 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100510 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100511
512 node = list;
513 while (node && depth) {
514 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200515 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
516 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100517 }
518
519 /* find iter instance in children */
520 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200521 lyd_find_sibling_val(lyd_node_children(node, 0), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100522 --depth;
523 }
524
Michal Vasko14654712020-02-06 08:35:21 +0100525 return node;
526}
527
528/*
529 * actions (cb_data):
530 * 0 - compare all uniques
531 * n - compare n-th unique
532 */
533static int
534lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
535{
536 struct ly_ctx *ctx;
537 struct lysc_node_list *slist;
538 struct lyd_node *diter, *first, *second;
539 struct lyd_value *val1, *val2;
540 char *path1, *path2, *uniq_str, *ptr;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200541 LY_ARRAY_SIZE_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100542
543 assert(val1_p && val2_p);
544
545 first = *((struct lyd_node **)val1_p);
546 second = *((struct lyd_node **)val2_p);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200547 action = (LY_ARRAY_SIZE_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100548
549 assert(first && (first->schema->nodetype == LYS_LIST));
550 assert(second && (second->schema == first->schema));
551
552 ctx = first->schema->module->ctx;
553
554 slist = (struct lysc_node_list *)first->schema;
555
556 /* compare unique leaves */
557 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200558 u = action - 1;
559 if (u < LY_ARRAY_SIZE(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100560 goto uniquecheck;
561 }
562 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200563 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100564uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200565 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100566 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200567 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100568 if (diter) {
569 val1 = &((struct lyd_node_term *)diter)->value;
570 } else {
571 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200572 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100573 }
574
575 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200576 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100577 if (diter) {
578 val2 = &((struct lyd_node_term *)diter)->value;
579 } else {
580 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200581 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100582 }
583
584 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
585 /* values differ or either one is not set */
586 break;
587 }
588 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200589 if (v && (v == LY_ARRAY_SIZE(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100590 /* all unique leafs are the same in this set, create this nice error */
591 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
592 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
593
594 /* use buffer to rebuild the unique string */
595 uniq_str = malloc(1024);
596 uniq_str[0] = '\0';
597 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200598 LY_ARRAY_FOR(slist->uniques[u], v) {
599 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100600 strcpy(ptr, " ");
601 ++ptr;
602 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200603 ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], (struct lysc_node *)slist, LYSC_PATH_LOG,
Michal Vasko14654712020-02-06 08:35:21 +0100604 ptr, 1024 - (ptr - uniq_str));
605 if (!ptr) {
606 /* path will be incomplete, whatever */
607 break;
608 }
609
610 ptr += strlen(ptr);
611 }
612 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
613
614 free(path1);
615 free(path2);
616 free(uniq_str);
617 return 1;
618 }
619
620 if (action > 0) {
621 /* done */
622 return 0;
623 }
624 }
625
626 return 0;
627}
628
Michal Vaskoa3881362020-01-21 15:57:35 +0100629static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100630lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, struct lysc_node_leaf ***uniques)
Michal Vaskoa3881362020-01-21 15:57:35 +0100631{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100632 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100633 struct ly_set *set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200634 LY_ARRAY_SIZE_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100635 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200636 uint32_t hash, i, size = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100637 int dynamic;
638 const char *str;
639 struct hash_table **uniqtables = NULL;
640 struct lyd_value *val;
641 struct ly_ctx *ctx = snode->module->ctx;
642
643 assert(uniques);
644
645 /* get all list instances */
Michal Vasko9b368d32020-02-14 13:53:31 +0100646 set = ly_set_new();
647 LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100648 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100649 if (diter->schema == snode) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100650 ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100651 }
652 }
Michal Vasko14654712020-02-06 08:35:21 +0100653
654 if (set->count == 2) {
655 /* simple comparison */
656 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
657 /* instance duplication */
658 ret = LY_EVALID;
659 goto cleanup;
660 }
661 } else if (set->count > 2) {
662 /* use hashes for comparison */
663 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200664 for (i = 31; i > 0; i--) {
665 size = set->count << i;
666 size = size >> i;
667 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100668 break;
669 }
670 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200671 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
672 i = 32 - i;
673 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100674
675 uniqtables = malloc(LY_ARRAY_SIZE(uniques) * sizeof *uniqtables);
676 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200677 x = LY_ARRAY_SIZE(uniques);
678 for (v = 0; v < x; v++) {
679 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
680 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100681 }
682
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200683 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100684 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200685 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100686 val = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200687 for (v = hash = 0; v < LY_ARRAY_SIZE(uniques[u]); v++) {
688 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100689 if (diter) {
690 val = &((struct lyd_node_term *)diter)->value;
691 } else {
692 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200693 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100694 }
695 if (!val) {
696 /* unique item not present nor has default value */
697 break;
698 }
699
700 /* get canonical string value */
701 str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic);
702 hash = dict_hash_multi(hash, str, strlen(str));
703 if (dynamic) {
704 free((char *)str);
705 }
706 }
707 if (!val) {
708 /* skip this list instance since its unique set is incomplete */
709 continue;
710 }
711
712 /* finish the hash value */
713 hash = dict_hash_multi(hash, NULL, 0);
714
715 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200716 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100717 if (ret == LY_EEXIST) {
718 /* instance duplication */
719 ret = LY_EVALID;
720 }
721 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
722 }
723 }
724 }
725
726cleanup:
727 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200728 for (v = 0; v < x; v++) {
729 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100730 /* failed when allocating uniquetables[j], following j are not allocated */
731 break;
732 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200733 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100734 }
735 free(uniqtables);
736
737 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100738}
739
740static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100741lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
742 const struct lysc_module *mod, int val_opts)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100743{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100744 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100745 struct lysc_node_list *slist;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200746 int getnext_opts;
747
748 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (val_opts & LYD_VALOPT_OUTPUT ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100749
Michal Vaskoa3881362020-01-21 15:57:35 +0100750 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200751 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100752 if ((val_opts & LYD_VALOPT_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
753 continue;
754 }
755
Michal Vaskoa3881362020-01-21 15:57:35 +0100756 /* check min-elements and max-elements */
757 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
758 slist = (struct lysc_node_list *)snode;
759 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100760 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100761 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100762
763 /* check generic mandatory existence */
764 } else if (snode->flags & LYS_MAND_TRUE) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100765 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100766 }
767
768 /* check unique */
769 if (snode->nodetype == LYS_LIST) {
770 slist = (struct lysc_node_list *)snode;
771 if (slist->uniques) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100772 LY_CHECK_RET(lyd_validate_unique(first, snode, slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100773 }
774 }
775
Michal Vaskoacd83e72020-02-04 14:12:01 +0100776 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
777 /* go recursively for schema-only nodes */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100778 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100779 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100780 }
781
Michal Vaskoacd83e72020-02-04 14:12:01 +0100782 return LY_SUCCESS;
783}
784
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100785static void
786lyd_validate_obsolete(const struct lyd_node *node)
787{
788 const struct lysc_node *snode;
789
790 snode = node->schema;
791 do {
792 if (snode->flags & LYS_STATUS_OBSLT) {
793 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
794 break;
795 }
796
797 snode = snode->parent;
798 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
799}
800
Michal Vaskocc048b22020-03-27 15:52:38 +0100801static LY_ERR
Michal Vaskocb7526d2020-03-30 15:08:26 +0200802lyd_validate_must(const struct lyd_node *node, int val_opts)
Michal Vaskocc048b22020-03-27 15:52:38 +0100803{
804 struct lyxp_set xp_set;
805 struct lysc_must *musts;
806 const struct lyd_node *tree;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200807 LY_ARRAY_SIZE_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100808
809 switch (node->schema->nodetype) {
810 case LYS_CONTAINER:
811 musts = ((struct lysc_node_container *)node->schema)->musts;
812 break;
813 case LYS_LEAF:
814 musts = ((struct lysc_node_leaf *)node->schema)->musts;
815 break;
816 case LYS_LEAFLIST:
817 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
818 break;
819 case LYS_LIST:
820 musts = ((struct lysc_node_list *)node->schema)->musts;
821 break;
822 case LYS_ANYXML:
823 case LYS_ANYDATA:
824 musts = ((struct lysc_node_anydata *)node->schema)->musts;
825 break;
826 case LYS_NOTIF:
827 musts = ((struct lysc_notif *)node->schema)->musts;
828 break;
829 case LYS_RPC:
830 case LYS_ACTION:
Michal Vaskocb7526d2020-03-30 15:08:26 +0200831 if (val_opts & LYD_VALOPT_INPUT) {
832 musts = ((struct lysc_action *)node->schema)->input.musts;
833 } else if (val_opts & LYD_VALOPT_OUTPUT) {
834 musts = ((struct lysc_action *)node->schema)->output.musts;
835 } else {
836 LOGINT(LYD_NODE_CTX(node));
837 return LY_EINT;
838 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100839 break;
840 default:
841 LOGINT(LYD_NODE_CTX(node));
842 return LY_EINT;
843 }
844
845 if (!musts) {
846 /* no must to evaluate */
847 return LY_SUCCESS;
848 }
849
850 /* find first top-level node */
851 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent);
852 while (tree->prev->next) {
853 tree = tree->prev;
854 }
855
856 LY_ARRAY_FOR(musts, u) {
857 memset(&xp_set, 0, sizeof xp_set);
858
859 /* evaluate must */
860 LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA));
861
862 /* check the result */
863 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200864 if (!xp_set.val.bln) {
Michal Vaskocc048b22020-03-27 15:52:38 +0100865 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
866 return LY_EVALID;
867 }
868 }
869
870 return LY_SUCCESS;
871}
872
Michal Vaskob1b5c262020-03-05 14:29:47 +0100873LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +0200874lyd_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 +0100875{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100876 struct lyd_node *next, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100877 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100878
Michal Vasko14654712020-02-06 08:35:21 +0100879 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100880 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100881 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100882 /* all top-level data from this module checked */
883 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100884 }
885
Michal Vaskoa8c61722020-03-27 16:59:32 +0100886 /* opaque data */
887 if (!node->schema) {
888 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
889 ((struct lyd_node_opaq *)node)->name);
890 return LY_EVALID;
891 }
892
Michal Vaskocb7526d2020-03-30 15:08:26 +0200893 /* no state/input/output data */
Michal Vasko5b37a352020-03-06 13:38:33 +0100894 if ((val_opts & LYD_VALOPT_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200895 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
896 return LY_EVALID;
897 } else if ((val_opts & LYD_VALOPT_INPUT) && (node->schema->flags & LYS_CONFIG_R)) {
898 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
899 return LY_EVALID;
900 } else if ((val_opts & LYD_VALOPT_OUTPUT) && (node->schema->flags & LYS_CONFIG_W)) {
901 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +0100902 return LY_EVALID;
903 }
904
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100905 /* obsolete data */
906 lyd_validate_obsolete(node);
907
Michal Vaskoc193ce92020-03-06 11:04:48 +0100908 /* node's schema if-features */
909 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskoa8c61722020-03-27 16:59:32 +0100910 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +0100911 return LY_EVALID;
912 }
913
Michal Vaskocc048b22020-03-27 15:52:38 +0100914 /* node's musts */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200915 LY_CHECK_RET(lyd_validate_must(node, val_opts));
Michal Vaskocc048b22020-03-27 15:52:38 +0100916
Michal Vaskoa8c61722020-03-27 16:59:32 +0100917 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +0100918 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100919
Michal Vasko14654712020-02-06 08:35:21 +0100920 /* validate schema-based restrictions */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100921 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts));
Michal Vasko14654712020-02-06 08:35:21 +0100922
Michal Vaskob1b5c262020-03-05 14:29:47 +0100923 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +0100924 /* validate all children recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200925 LY_CHECK_RET(lyd_validate_final_r(lyd_node_children(node, 0), node->schema, NULL, val_opts));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100926
Michal Vaskob1b5c262020-03-05 14:29:47 +0100927 /* set default for containers */
928 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200929 LY_LIST_FOR(lyd_node_children(node, 0), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100930 if (!(next->flags & LYD_DEFAULT)) {
931 break;
932 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100933 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100934 if (!next) {
935 node->flags |= LYD_DEFAULT;
936 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100937 }
938 }
939
940 return LY_SUCCESS;
941}
942
943LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100944lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100945 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 +0100946{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100947 LY_ERR ret;
Michal Vasko9b368d32020-02-14 13:53:31 +0100948 const struct lysc_node *iter = NULL;
949 struct lyd_node *node;
950 struct lyd_value **dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200951 LY_ARRAY_SIZE_TYPE u;
Michal Vasko9b368d32020-02-14 13:53:31 +0100952
Michal Vaskob1b5c262020-03-05 14:29:47 +0100953 assert(first && (parent || sparent || mod) && node_types && node_when);
Michal Vasko9b368d32020-02-14 13:53:31 +0100954
Michal Vaskob1b5c262020-03-05 14:29:47 +0100955 if (!sparent && parent) {
956 sparent = parent->schema;
957 }
958
959 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100960 if ((val_opts & LYD_VALOPT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
961 continue;
962 }
963
Michal Vasko9b368d32020-02-14 13:53:31 +0100964 switch (iter->nodetype) {
965 case LYS_CHOICE:
Michal Vaskof03ed032020-03-04 13:31:44 +0100966 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100967 /* create default case data */
968 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 +0100969 NULL, node_types, node_when, val_opts));
Michal Vasko9b368d32020-02-14 13:53:31 +0100970 }
971 break;
972 case LYS_CONTAINER:
973 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100974 /* create default NP container */
Michal Vasko9b368d32020-02-14 13:53:31 +0100975 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100976 node->flags = LYD_DEFAULT;
977 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100978
979 if (iter->when) {
980 /* remember to resolve when */
981 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
982 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100983
984 /* create any default children */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100985 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 +0100986 }
987 break;
988 case LYS_LEAF:
989 if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
990 /* create default leaf */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100991 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
992 if (ret == LY_EINCOMPLETE) {
993 /* remember to resolve type */
994 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
995 } else if (ret) {
996 return ret;
997 }
998 node->flags = LYD_DEFAULT;
999 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001000
1001 if (iter->when) {
1002 /* remember to resolve when */
1003 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1004 }
1005 }
1006 break;
1007 case LYS_LEAFLIST:
1008 if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_next2(*first, iter, NULL, 0, NULL)) {
1009 /* create all default leaf-lists */
1010 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001011 LY_ARRAY_FOR(dflts, u) {
1012 ret = lyd_create_term2(iter, dflts[u], &node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001013 if (ret == LY_EINCOMPLETE) {
1014 /* remember to resolve type */
1015 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1016 } else if (ret) {
1017 return ret;
1018 }
1019 node->flags = LYD_DEFAULT;
1020 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001021
1022 if (iter->when) {
1023 /* remember to resolve when */
1024 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1025 }
1026 }
1027 }
1028 break;
1029 default:
1030 /* without defaults */
1031 break;
1032 }
1033 }
1034
1035 return LY_SUCCESS;
1036}
1037
Michal Vaskob1b5c262020-03-05 14:29:47 +01001038static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001039lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
1040 struct ly_set *when_check, int val_opts)
1041{
1042 const struct lyd_meta *meta;
1043 struct lyd_node *next, *node;
1044
1045 LYD_TREE_DFS_BEGIN(root, next, node) {
1046 /* skip added default nodes */
1047 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1048 LY_LIST_FOR(node->meta, meta) {
1049 /* metadata type resolution */
1050 ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST);
1051 }
1052
1053 if (node->schema->nodetype & LYD_NODE_TERM) {
1054 /* node type resolution */
1055 ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST);
1056 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1057 /* new node validation, autodelete */
1058 LY_CHECK_RET(lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL));
1059
1060 /* add nested defaults */
1061 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
1062 when_check, val_opts));
1063 }
1064
1065 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1066 /* when evaluation */
1067 ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST);
1068 }
1069 }
1070
1071 LYD_TREE_DFS_END(root, next, node);
1072 }
1073
1074 return LY_SUCCESS;
1075}
1076
1077static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001078_lyd_validate(struct lyd_node **tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
1079 int val_opts)
Michal Vaskof03ed032020-03-04 13:31:44 +01001080{
1081 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001082 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001083 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001084 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001085 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001086
Michal Vaskob1b5c262020-03-05 14:29:47 +01001087 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || (modules && mod_count), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001088
Michal Vasko5b37a352020-03-06 13:38:33 +01001089 if (val_opts & ~LYD_VALOPT_MASK) {
1090 LOGERR(ctx, LY_EINVAL, "Some invalid flags passed to validation.");
1091 return LY_EINVAL;
1092 }
1093
Michal Vaskob1b5c262020-03-05 14:29:47 +01001094 next = *tree;
1095 while (1) {
1096 if (val_opts & LYD_VALOPT_DATA_ONLY) {
1097 mod = lyd_data_next_module(&next, &first);
1098 } else {
1099 mod = lyd_mod_next_module(next, modules, mod_count, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001100 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001101 if (!mod) {
1102 break;
1103 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001104 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001105 /* make sure first2 changes are carried to tree */
1106 first2 = tree;
1107 } else {
1108 first2 = &first;
1109 }
1110
1111 /* validate new top-level nodes of this module, autodelete */
Michal Vaskofea12c62020-03-30 11:00:15 +02001112 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001113
1114 /* add all top-level defaults for this module */
Michal Vaskofea12c62020-03-30 11:00:15 +02001115 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 +01001116
1117 /* process nested nodes */
1118 LY_LIST_FOR(*first2, first) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001119 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 +01001120 }
1121
1122 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vasko9f96a052020-03-10 09:41:45 +01001123 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 +01001124 LY_CHECK_GOTO(ret, cleanup);
1125
1126 /* perform final validation that assumes the data tree is final */
Michal Vaskofea12c62020-03-30 11:00:15 +02001127 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, val_opts), cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001128 }
1129
Michal Vaskof03ed032020-03-04 13:31:44 +01001130cleanup:
1131 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001132 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001133 ly_set_erase(&when_check, NULL);
1134 return ret;
1135}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001136
1137API LY_ERR
1138lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts)
1139{
1140 return _lyd_validate(tree, NULL, 0, ctx, val_opts);
1141}
1142
1143API LY_ERR
1144lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts)
1145{
1146 return _lyd_validate(tree, modules, mod_count, NULL, val_opts);
1147}
Michal Vaskofea12c62020-03-30 11:00:15 +02001148
Michal Vaskocb7526d2020-03-30 15:08:26 +02001149static void
1150lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op, const struct lyd_node *tree,
1151 struct lyd_node **op_node, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001152{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001153 const struct lyd_node *tree_iter, *op_iter;
1154 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001155 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001156
Michal Vaskocb7526d2020-03-30 15:08:26 +02001157 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001158 op_depth = 0;
1159 for (op_iter = op; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
1160 ++op_depth;
1161 }
1162
1163 /* find where to merge op */
1164 tree_iter = tree;
1165 cur_depth = op_depth;
1166 op_iter = op;
1167 while (cur_depth) {
1168 /* find next op parent */
1169 op_iter = op;
1170 for (i = 0; i < cur_depth; ++i) {
1171 op_iter = (struct lyd_node *)op_iter->parent;
1172 }
1173
1174 /* find op iter in tree */
1175 lyd_find_sibling_first(tree_iter, op_iter, &match);
1176 if (!match) {
1177 break;
1178 }
1179
1180 /* move tree_iter */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001181 tree_iter = lyd_node_children(match, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001182
1183 /* move depth */
1184 --cur_depth;
1185 }
1186
Michal Vaskocb7526d2020-03-30 15:08:26 +02001187 *op_node = (struct lyd_node *)op_iter;
1188 *tree_sibling = (struct lyd_node *)tree_iter;
1189}
1190
1191API LY_ERR
1192lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, int val_opts)
1193{
1194 LY_ERR ret;
1195 struct lyd_node *tree_sibling, *op_node, *op, *op_parent;
1196 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1197
1198 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
1199 !val_opts || (val_opts == LYD_VALOPT_INPUT) || (val_opts == LYD_VALOPT_OUTPUT), LY_EINVAL);
1200
1201 /* find the operation/notification */
1202 LYD_TREE_DFS_BEGIN(op_tree, op_node, op) {
1203 if ((val_opts & (LYD_VALOPT_INPUT | LYD_VALOPT_OUTPUT)) && (op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1204 break;
1205 } else if (!val_opts && (op->schema->nodetype == LYS_NOTIF)) {
1206 break;
1207 }
1208 LYD_TREE_DFS_END(op_tree, op_node, op);
1209 }
1210 if (val_opts & (LYD_VALOPT_INPUT | LYD_VALOPT_OUTPUT)) {
1211 if (!(op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1212 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
1213 return LY_EINVAL;
1214 }
1215 } else {
1216 if (op->schema->nodetype != LYS_NOTIF) {
1217 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
1218 return LY_EINVAL;
1219 }
1220 }
1221
1222 /* move op_tree to top-level node */
1223 while (op_tree->parent) {
1224 op_tree = (struct lyd_node *)op_tree->parent;
1225 }
1226
1227 /* merge op_tree into tree */
1228 lyd_val_op_merge_find(op_tree, op, tree, &op_node, &tree_sibling);
1229 op_parent = (struct lyd_node *)op_node->parent;
1230 lyd_unlink_tree(op_node);
1231 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001232 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001233 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001234 }
1235
1236 /* prevalidate whole operation subtree */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001237 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 +02001238
1239 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1240 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
1241 LYD_JSON, lydjson_resolve_prefix, NULL), cleanup);
1242
1243 /* perform final validation of the operation/notification */
1244 lyd_validate_obsolete(op);
1245 if (lysc_node_is_disabled(op->schema, 1)) {
1246 LOGVAL(LYD_NODE_CTX(op_tree), LY_VLOG_LYD, op, LY_VCODE_NOIFF, op->schema->name);
1247 ret = LY_EVALID;
1248 goto cleanup;
1249 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001250 LY_CHECK_GOTO(ret = lyd_validate_must(op, val_opts), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001251
1252 /* final validation of all the descendants */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001253 LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_node_children(op, 0), op->schema, NULL, val_opts), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001254
1255cleanup:
1256 /* restore operation tree */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001257 lyd_unlink_tree(op_node);
1258 if (op_parent) {
1259 lyd_insert_node(op_parent, NULL, op_node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001260 }
1261
1262 ly_set_erase(&type_check, NULL);
1263 ly_set_erase(&type_meta_check, NULL);
1264 ly_set_erase(&when_check, NULL);
1265 return ret;
1266}