blob: f75dc0428b990bfad4ac4f42edbd3df37c1e3551 [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"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "plugins_types.h"
27#include "set.h"
28#include "tree.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010029#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010031#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010033
Michal Vaskof03ed032020-03-04 13:31:44 +010034static struct lyd_node *
35lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
36 const struct lysc_node *parent, const struct lysc_module *module)
37{
38 const struct lysc_node *siter = NULL;
39 struct lyd_node *match = NULL;
40
41 assert(parent || module);
42 assert(!last || (slast && *slast));
43
44 if (slast) {
45 siter = *slast;
46 }
47
48 if (last && last->next) {
49 /* find next data instance */
50 lyd_find_sibling_next2(last->next, siter, NULL, 0, &match);
51 if (match) {
52 return match;
53 }
54 }
55
56 /* find next schema node data instance */
57 while ((siter = lys_getnext(siter, parent, module, 0))) {
58 switch (siter->nodetype) {
59 case LYS_CONTAINER:
60 case LYS_ANYXML:
61 case LYS_ANYDATA:
62 case LYS_LEAF:
63 lyd_find_sibling_val(sibling, siter, NULL, 0, &match);
64 break;
65 case LYS_LIST:
66 case LYS_LEAFLIST:
67 lyd_find_sibling_next2(sibling, siter, NULL, 0, &match);
68 break;
69 default:
70 assert(0);
71 LOGINT(NULL);
72 }
73
74 if (match) {
75 break;
76 }
77 }
78
79 if (slast) {
80 *slast = siter;
81 }
82 return match;
83}
84
Michal Vaskocde73ac2019-11-14 16:10:27 +010085/**
86 * @brief Evaluate a single "when" condition.
87 *
Michal Vaskob1b5c262020-03-05 14:29:47 +010088 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +010089 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +010090 * @param[in] when When to evaluate.
Michal Vaskocde73ac2019-11-14 16:10:27 +010091 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
92 */
93static LY_ERR
Michal Vaskoc193ce92020-03-06 11:04:48 +010094lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when)
Michal Vaskocde73ac2019-11-14 16:10:27 +010095{
Michal Vaskob1b5c262020-03-05 14:29:47 +010096 LY_ERR ret = LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +010097 const struct lyd_node *ctx_node;
98 struct lyxp_set xp_set;
99
100 memset(&xp_set, 0, sizeof xp_set);
101
102 if (when->context == node->schema) {
103 ctx_node = node;
104 } else {
105 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
106 ctx_node = (struct lyd_node *)node->parent;
107 }
108
109 /* evaluate when */
Michal Vasko52927e22020-03-16 17:26:14 +0100110 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 +0100111 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100112 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
113
114 /* return error or LY_EINCOMPLETE for dependant unresolved when */
115 LY_CHECK_RET(ret);
116
117 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +0200118 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100119 if (node->flags & LYD_WHEN_TRUE) {
120 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100121 if (LYD_DEL_IS_ROOT(*tree, node)) {
122 *tree = (*tree)->next;
123 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100124 lyd_free_tree(node);
125 } else {
126 /* invalid data */
Michal Vaskocc048b22020-03-27 15:52:38 +0100127 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100128 ret = LY_EVALID;
129 }
130 } else {
131 /* remember that when evaluated to true */
132 node->flags |= LYD_WHEN_TRUE;
133 }
134
135 return ret;
136}
137
138LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100139lyd_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 +0100140 LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100141{
142 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200143 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100144
Michal Vaskob1b5c262020-03-05 14:29:47 +0100145 if (node_when) {
146 /* evaluate all when conditions */
147 uint32_t prev_count;
148 do {
149 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200150 i = 0;
151 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100152 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200153 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100154 const struct lysc_node *schema = node->schema;
155 int unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100156
Michal Vaskob1b5c262020-03-05 14:29:47 +0100157 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200158 LY_ARRAY_SIZE_TYPE u;
159 LY_ARRAY_FOR(schema->when, u) {
160 ret = lyd_validate_when(tree, node, schema->when[u]);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100161 if (ret) {
162 break;
163 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100164 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100165 if (ret == LY_EINCOMPLETE) {
166 /* could not evaluate this when */
167 unres_when = 1;
168 break;
169 } else if (ret) {
170 /* error */
171 return ret;
172 }
173 schema = schema->parent;
174 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100175
Michal Vaskob1b5c262020-03-05 14:29:47 +0100176 if (unres_when) {
177 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200178 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100179 } else {
180 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200181 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100182 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100183 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100184
Michal Vaskob1b5c262020-03-05 14:29:47 +0100185 /* there must have been some when conditions resolved */
186 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100187
Michal Vaskob1b5c262020-03-05 14:29:47 +0100188 /* there could have been no cyclic when dependencies, checked during compilation */
189 assert(!node_when->count);
190 }
191
192 if (node_types && node_types->count) {
193 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200194 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100195 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200196 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100197
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200198 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100199
200 /* validate and store the value of the node */
201 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
202 parser_data, format, *tree);
203 LY_CHECK_RET(ret);
204
205 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200206 ly_set_rm_index(node_types, i, NULL);
207 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100208 }
209
Michal Vasko9f96a052020-03-10 09:41:45 +0100210 if (meta_types && meta_types->count) {
211 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200212 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100213 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200214 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100215
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200216 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100217
Michal Vasko9f96a052020-03-10 09:41:45 +0100218 /* validate and store the value of the metadata */
219 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original,
220 strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100221 LY_CHECK_RET(ret);
222
223 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200224 ly_set_rm_index(meta_types, i, NULL);
225 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100226 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100227
228 return ret;
229}
230
Michal Vaskob1b5c262020-03-05 14:29:47 +0100231static LY_ERR
232lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100233{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100234 struct lyd_node **match_p;
235 int fail = 0;
236
237 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
238 /* duplicate instances allowed */
239 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100240 }
241
Michal Vaskob1b5c262020-03-05 14:29:47 +0100242 /* find exactly the same next instance using hashes if possible */
243 if (node->parent && node->parent->children_ht) {
244 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
245 fail = 1;
246 }
247 } else {
248 for (; first; first = first->next) {
249 if (first == node) {
250 continue;
251 }
252
253 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
254 if (first->schema == node->schema) {
255 fail = 1;
256 break;
257 }
258 } else if (!lyd_compare(first, node, 0)) {
259 fail = 1;
260 break;
261 }
262 }
263 }
264
265 if (fail) {
266 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
267 return LY_EVALID;
268 }
269 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100270}
271
272static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100273lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic)
274{
275 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
276 struct lyd_node *match, *to_del;
277 int found;
278
279 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
280 found = 0;
281 iter = NULL;
282 match = NULL;
283 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
284 if (match->flags & LYD_NEW) {
285 /* a new case data found, nothing more to look for */
286 found = 2;
287 break;
288 } else {
289 /* and old case data found */
290 if (found == 0) {
291 found = 1;
292 }
293 }
294 }
295
296 if (found == 1) {
297 /* there should not be 2 old cases */
298 if (old_case) {
299 /* old data from 2 cases */
300 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
301 return LY_EVALID;
302 }
303
304 /* remember an old existing case */
305 old_case = scase;
306 } else if (found == 2) {
307 if (new_case) {
308 /* new data from 2 cases */
309 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
310 return LY_EVALID;
311 }
312
313 /* remember a new existing case */
314 new_case = scase;
315 }
316 }
317
318 if (old_case && new_case) {
319 /* auto-delete old case */
320 iter = NULL;
321 match = NULL;
322 to_del = NULL;
323 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
324 if (LYD_DEL_IS_ROOT(*first, to_del)) {
325 *first = (*first)->next;
326 }
327 lyd_free_tree(to_del);
328 to_del = match;
329 }
330 if (LYD_DEL_IS_ROOT(*first, to_del)) {
331 *first = (*first)->next;
332 }
333 lyd_free_tree(to_del);
334 }
335
336 return LY_SUCCESS;
337}
338
339static int
340lyd_val_has_default(const struct lysc_node *schema)
341{
342 switch (schema->nodetype) {
343 case LYS_LEAF:
344 if (((struct lysc_node_leaf *)schema)->dflt) {
345 return 1;
346 }
347 break;
348 case LYS_LEAFLIST:
349 if (((struct lysc_node_leaflist *)schema)->dflts) {
350 return 1;
351 }
352 break;
353 case LYS_CONTAINER:
354 if (!(schema->flags & LYS_PRESENCE)) {
355 return 1;
356 }
357 break;
358 default:
359 break;
360 }
361
362 return 0;
363}
364
365static void
366lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p)
367{
368 struct lyd_node *match, *next;
369
370 if (lyd_val_has_default(node->schema)) {
371 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
372 if (node->schema->nodetype == LYS_LEAFLIST) {
373 lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match);
374 } else {
375 lyd_find_sibling_val(*first, node->schema, NULL, 0, &match);
376 }
377
378 while (match) {
379 next = match->next;
380 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
381 /* default instance found, remove it */
382 if (LYD_DEL_IS_ROOT(*first, match)) {
383 *first = (*first)->next;
384 }
385 if (match == *next_p) {
386 *next_p = (*next_p)->next;
387 }
388 lyd_free_tree(match);
389
390 /* remove only a single container/leaf default instance, if there are more, it is an error */
391 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
392 break;
393 }
394 }
395
396 lyd_find_sibling_next2(next, node->schema, NULL, 0, &match);
397 }
398 }
399}
400
401LY_ERR
402lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod)
403{
404 struct lyd_node *next, *node;
405 const struct lysc_node *snode = NULL;
406
407 assert(first && (sparent || mod));
408
409 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
410 /* check case duplicites */
411 if (snode->nodetype == LYS_CHOICE) {
412 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode));
413 }
414 }
415
416 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100417 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100418 /* all top-level data from this module checked */
419 break;
420 }
421
422 if (!(node->flags & LYD_NEW)) {
423 /* check only new nodes */
424 continue;
425 }
426
427 /* remove old default(s) if it exists */
428 lyd_validate_autodel_dup(first, node, &next);
429
430 /* then check new node instance duplicities */
431 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
432
433 /* this node is valid */
434 node->flags &= ~LYD_NEW;
435 }
436
437 return LY_SUCCESS;
438}
439
440static LY_ERR
441lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100442{
Michal Vaskoa3881362020-01-21 15:57:35 +0100443 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100444 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100445 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100446 return LY_SUCCESS;
447 }
448 } else {
449 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100450
Michal Vaskob1b5c262020-03-05 14:29:47 +0100451 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100452 /* data instance found */
453 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100454 }
455 }
456
457 /* node instance not found */
458 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
459 return LY_EVALID;
460}
461
462static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100463lyd_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 +0100464{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100465 uint32_t count = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100466 const struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100467
Michal Vasko9b368d32020-02-14 13:53:31 +0100468 assert(min || max);
469
Michal Vaskob1b5c262020-03-05 14:29:47 +0100470 LY_LIST_FOR(first, iter) {
Michal Vaskoacd83e72020-02-04 14:12:01 +0100471 if (iter->schema == snode) {
472 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100473
474 if (min && (count == min)) {
475 /* satisfied */
476 min = 0;
477 if (!max) {
478 /* nothing more to check */
479 break;
480 }
481 }
482 if (max && (count > max)) {
483 /* not satisifed */
484 break;
485 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100486 }
487 }
488
Michal Vasko9b368d32020-02-14 13:53:31 +0100489 if (min) {
490 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100491 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
492 return LY_EVALID;
493 } else if (max && (count > max)) {
494 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
495 return LY_EVALID;
496 }
497
Michal Vaskoa3881362020-01-21 15:57:35 +0100498 return LY_SUCCESS;
499}
500
Michal Vasko14654712020-02-06 08:35:21 +0100501static struct lyd_node *
502lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, struct lyd_node *list)
503{
Michal Vasko9b368d32020-02-14 13:53:31 +0100504 struct lyd_node *node;
505 const struct lysc_node *iter;
506 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100507
Michal Vasko9b368d32020-02-14 13:53:31 +0100508 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200509 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
510 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100511 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100512
513 node = list;
514 while (node && depth) {
515 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200516 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
517 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100518 }
519
520 /* find iter instance in children */
521 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200522 lyd_find_sibling_val(lyd_node_children(node, 0), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100523 --depth;
524 }
525
Michal Vasko14654712020-02-06 08:35:21 +0100526 return node;
527}
528
529/*
530 * actions (cb_data):
531 * 0 - compare all uniques
532 * n - compare n-th unique
533 */
534static int
535lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
536{
537 struct ly_ctx *ctx;
538 struct lysc_node_list *slist;
539 struct lyd_node *diter, *first, *second;
540 struct lyd_value *val1, *val2;
541 char *path1, *path2, *uniq_str, *ptr;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200542 LY_ARRAY_SIZE_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100543
544 assert(val1_p && val2_p);
545
546 first = *((struct lyd_node **)val1_p);
547 second = *((struct lyd_node **)val2_p);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200548 action = (LY_ARRAY_SIZE_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100549
550 assert(first && (first->schema->nodetype == LYS_LIST));
551 assert(second && (second->schema == first->schema));
552
553 ctx = first->schema->module->ctx;
554
555 slist = (struct lysc_node_list *)first->schema;
556
557 /* compare unique leaves */
558 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200559 u = action - 1;
560 if (u < LY_ARRAY_SIZE(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100561 goto uniquecheck;
562 }
563 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200564 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100565uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200566 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100567 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200568 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100569 if (diter) {
570 val1 = &((struct lyd_node_term *)diter)->value;
571 } else {
572 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200573 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100574 }
575
576 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200577 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100578 if (diter) {
579 val2 = &((struct lyd_node_term *)diter)->value;
580 } else {
581 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200582 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100583 }
584
585 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
586 /* values differ or either one is not set */
587 break;
588 }
589 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200590 if (v && (v == LY_ARRAY_SIZE(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100591 /* all unique leafs are the same in this set, create this nice error */
592 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
593 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
594
595 /* use buffer to rebuild the unique string */
596 uniq_str = malloc(1024);
597 uniq_str[0] = '\0';
598 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200599 LY_ARRAY_FOR(slist->uniques[u], v) {
600 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100601 strcpy(ptr, " ");
602 ++ptr;
603 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200604 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 +0100605 ptr, 1024 - (ptr - uniq_str));
606 if (!ptr) {
607 /* path will be incomplete, whatever */
608 break;
609 }
610
611 ptr += strlen(ptr);
612 }
613 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
614
615 free(path1);
616 free(path2);
617 free(uniq_str);
618 return 1;
619 }
620
621 if (action > 0) {
622 /* done */
623 return 0;
624 }
625 }
626
627 return 0;
628}
629
Michal Vaskoa3881362020-01-21 15:57:35 +0100630static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100631lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, struct lysc_node_leaf ***uniques)
Michal Vaskoa3881362020-01-21 15:57:35 +0100632{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100633 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100634 struct ly_set *set;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200635 LY_ARRAY_SIZE_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100636 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200637 uint32_t hash, i, size = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100638 int dynamic;
639 const char *str;
640 struct hash_table **uniqtables = NULL;
641 struct lyd_value *val;
642 struct ly_ctx *ctx = snode->module->ctx;
643
644 assert(uniques);
645
646 /* get all list instances */
Michal Vasko9b368d32020-02-14 13:53:31 +0100647 set = ly_set_new();
648 LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100649 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100650 if (diter->schema == snode) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100651 ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100652 }
653 }
Michal Vasko14654712020-02-06 08:35:21 +0100654
655 if (set->count == 2) {
656 /* simple comparison */
657 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
658 /* instance duplication */
659 ret = LY_EVALID;
660 goto cleanup;
661 }
662 } else if (set->count > 2) {
663 /* use hashes for comparison */
664 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200665 for (i = 31; i > 0; i--) {
666 size = set->count << i;
667 size = size >> i;
668 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100669 break;
670 }
671 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200672 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
673 i = 32 - i;
674 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100675
676 uniqtables = malloc(LY_ARRAY_SIZE(uniques) * sizeof *uniqtables);
677 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200678 x = LY_ARRAY_SIZE(uniques);
679 for (v = 0; v < x; v++) {
680 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
681 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100682 }
683
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200684 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100685 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200686 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100687 val = NULL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200688 for (v = hash = 0; v < LY_ARRAY_SIZE(uniques[u]); v++) {
689 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100690 if (diter) {
691 val = &((struct lyd_node_term *)diter)->value;
692 } else {
693 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200694 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100695 }
696 if (!val) {
697 /* unique item not present nor has default value */
698 break;
699 }
700
701 /* get canonical string value */
702 str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic);
703 hash = dict_hash_multi(hash, str, strlen(str));
704 if (dynamic) {
705 free((char *)str);
706 }
707 }
708 if (!val) {
709 /* skip this list instance since its unique set is incomplete */
710 continue;
711 }
712
713 /* finish the hash value */
714 hash = dict_hash_multi(hash, NULL, 0);
715
716 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200717 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100718 if (ret == LY_EEXIST) {
719 /* instance duplication */
720 ret = LY_EVALID;
721 }
722 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
723 }
724 }
725 }
726
727cleanup:
728 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200729 for (v = 0; v < x; v++) {
730 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100731 /* failed when allocating uniquetables[j], following j are not allocated */
732 break;
733 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200734 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100735 }
736 free(uniqtables);
737
738 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100739}
740
741static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100742lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci7931b192020-06-25 17:05:03 +0200743 const struct lysc_module *mod, int val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100744{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100745 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100746 struct lysc_node_list *slist;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200747 int getnext_opts;
748
Radek Krejci7931b192020-06-25 17:05:03 +0200749 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100750
Michal Vaskoa3881362020-01-21 15:57:35 +0100751 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200752 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200753 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100754 continue;
755 }
756
Michal Vaskoa3881362020-01-21 15:57:35 +0100757 /* check min-elements and max-elements */
758 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
759 slist = (struct lysc_node_list *)snode;
760 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100761 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100762 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100763
764 /* check generic mandatory existence */
765 } else if (snode->flags & LYS_MAND_TRUE) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100766 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100767 }
768
769 /* check unique */
770 if (snode->nodetype == LYS_LIST) {
771 slist = (struct lysc_node_list *)snode;
772 if (slist->uniques) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100773 LY_CHECK_RET(lyd_validate_unique(first, snode, slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100774 }
775 }
776
Michal Vaskoacd83e72020-02-04 14:12:01 +0100777 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
778 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200779 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100780 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100781 }
782
Michal Vaskoacd83e72020-02-04 14:12:01 +0100783 return LY_SUCCESS;
784}
785
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100786static void
787lyd_validate_obsolete(const struct lyd_node *node)
788{
789 const struct lysc_node *snode;
790
791 snode = node->schema;
792 do {
793 if (snode->flags & LYS_STATUS_OBSLT) {
794 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
795 break;
796 }
797
798 snode = snode->parent;
799 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
800}
801
Michal Vaskocc048b22020-03-27 15:52:38 +0100802static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200803lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100804{
805 struct lyxp_set xp_set;
806 struct lysc_must *musts;
807 const struct lyd_node *tree;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200808 LY_ARRAY_SIZE_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100809
810 switch (node->schema->nodetype) {
811 case LYS_CONTAINER:
812 musts = ((struct lysc_node_container *)node->schema)->musts;
813 break;
814 case LYS_LEAF:
815 musts = ((struct lysc_node_leaf *)node->schema)->musts;
816 break;
817 case LYS_LEAFLIST:
818 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
819 break;
820 case LYS_LIST:
821 musts = ((struct lysc_node_list *)node->schema)->musts;
822 break;
823 case LYS_ANYXML:
824 case LYS_ANYDATA:
825 musts = ((struct lysc_node_anydata *)node->schema)->musts;
826 break;
827 case LYS_NOTIF:
828 musts = ((struct lysc_notif *)node->schema)->musts;
829 break;
830 case LYS_RPC:
831 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200832 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200833 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200834 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200835 musts = ((struct lysc_action *)node->schema)->output.musts;
836 } else {
837 LOGINT(LYD_NODE_CTX(node));
838 return LY_EINT;
839 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100840 break;
841 default:
842 LOGINT(LYD_NODE_CTX(node));
843 return LY_EINT;
844 }
845
846 if (!musts) {
847 /* no must to evaluate */
848 return LY_SUCCESS;
849 }
850
851 /* find first top-level node */
852 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent);
853 while (tree->prev->next) {
854 tree = tree->prev;
855 }
856
857 LY_ARRAY_FOR(musts, u) {
858 memset(&xp_set, 0, sizeof xp_set);
859
860 /* evaluate must */
861 LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA));
862
863 /* check the result */
864 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200865 if (!xp_set.val.bln) {
Michal Vaskocc048b22020-03-27 15:52:38 +0100866 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
867 return LY_EVALID;
868 }
869 }
870
871 return LY_SUCCESS;
872}
873
Michal Vaskob1b5c262020-03-05 14:29:47 +0100874LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200875lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, int val_opts, LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100876{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100877 struct lyd_node *next, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100878 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100879
Michal Vasko14654712020-02-06 08:35:21 +0100880 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100881 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100882 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100883 /* all top-level data from this module checked */
884 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100885 }
886
Michal Vaskoa8c61722020-03-27 16:59:32 +0100887 /* opaque data */
888 if (!node->schema) {
889 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
890 ((struct lyd_node_opaq *)node)->name);
891 return LY_EVALID;
892 }
893
Michal Vaskocb7526d2020-03-30 15:08:26 +0200894 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +0200895 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200896 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
897 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200898 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200899 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
900 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200901 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200902 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +0100903 return LY_EVALID;
904 }
905
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100906 /* obsolete data */
907 lyd_validate_obsolete(node);
908
Michal Vaskoc193ce92020-03-06 11:04:48 +0100909 /* node's schema if-features */
910 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskoa8c61722020-03-27 16:59:32 +0100911 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +0100912 return LY_EVALID;
913 }
914
Michal Vaskocc048b22020-03-27 15:52:38 +0100915 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +0200916 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +0100917
Michal Vaskoa8c61722020-03-27 16:59:32 +0100918 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +0100919 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100920
Michal Vasko14654712020-02-06 08:35:21 +0100921 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +0200922 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +0100923
Michal Vaskob1b5c262020-03-05 14:29:47 +0100924 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +0100925 /* validate all children recursively */
Radek Krejci7931b192020-06-25 17:05:03 +0200926 LY_CHECK_RET(lyd_validate_final_r(lyd_node_children(node, 0), node->schema, NULL, val_opts, op));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100927
Michal Vaskob1b5c262020-03-05 14:29:47 +0100928 /* set default for containers */
929 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200930 LY_LIST_FOR(lyd_node_children(node, 0), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100931 if (!(next->flags & LYD_DEFAULT)) {
932 break;
933 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100934 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100935 if (!next) {
936 node->flags |= LYD_DEFAULT;
937 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100938 }
939 }
940
941 return LY_SUCCESS;
942}
943
944LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100945lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100946 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 +0100947{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100948 LY_ERR ret;
Michal Vasko9b368d32020-02-14 13:53:31 +0100949 const struct lysc_node *iter = NULL;
950 struct lyd_node *node;
951 struct lyd_value **dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200952 LY_ARRAY_SIZE_TYPE u;
Michal Vasko9b368d32020-02-14 13:53:31 +0100953
Michal Vaskob1b5c262020-03-05 14:29:47 +0100954 assert(first && (parent || sparent || mod) && node_types && node_when);
Michal Vasko9b368d32020-02-14 13:53:31 +0100955
Michal Vaskob1b5c262020-03-05 14:29:47 +0100956 if (!sparent && parent) {
957 sparent = parent->schema;
958 }
959
960 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200961 if ((val_opts & LYD_VALIDATE_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100962 continue;
963 }
964
Michal Vasko9b368d32020-02-14 13:53:31 +0100965 switch (iter->nodetype) {
966 case LYS_CHOICE:
Michal Vaskof03ed032020-03-04 13:31:44 +0100967 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100968 /* create default case data */
969 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 +0100970 NULL, node_types, node_when, val_opts));
Michal Vasko9b368d32020-02-14 13:53:31 +0100971 }
972 break;
973 case LYS_CONTAINER:
974 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100975 /* create default NP container */
Michal Vasko9b368d32020-02-14 13:53:31 +0100976 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100977 node->flags = LYD_DEFAULT;
978 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100979
980 if (iter->when) {
981 /* remember to resolve when */
982 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
983 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100984
985 /* create any default children */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100986 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 +0100987 }
988 break;
989 case LYS_LEAF:
990 if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
991 /* create default leaf */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100992 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
993 if (ret == LY_EINCOMPLETE) {
994 /* remember to resolve type */
995 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
996 } else if (ret) {
997 return ret;
998 }
999 node->flags = LYD_DEFAULT;
1000 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001001
1002 if (iter->when) {
1003 /* remember to resolve when */
1004 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1005 }
1006 }
1007 break;
1008 case LYS_LEAFLIST:
1009 if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_next2(*first, iter, NULL, 0, NULL)) {
1010 /* create all default leaf-lists */
1011 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001012 LY_ARRAY_FOR(dflts, u) {
1013 ret = lyd_create_term2(iter, dflts[u], &node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001014 if (ret == LY_EINCOMPLETE) {
1015 /* remember to resolve type */
1016 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1017 } else if (ret) {
1018 return ret;
1019 }
1020 node->flags = LYD_DEFAULT;
1021 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001022
1023 if (iter->when) {
1024 /* remember to resolve when */
1025 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1026 }
1027 }
1028 }
1029 break;
1030 default:
1031 /* without defaults */
1032 break;
1033 }
1034 }
1035
1036 return LY_SUCCESS;
1037}
1038
Radek Krejci7931b192020-06-25 17:05:03 +02001039/**
1040 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1041 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001042static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001043lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Radek Krejci7931b192020-06-25 17:05:03 +02001044 struct ly_set *when_check, int validate_options)
Michal Vaskofea12c62020-03-30 11:00:15 +02001045{
1046 const struct lyd_meta *meta;
1047 struct lyd_node *next, *node;
1048
1049 LYD_TREE_DFS_BEGIN(root, next, node) {
1050 /* skip added default nodes */
1051 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1052 LY_LIST_FOR(node->meta, meta) {
1053 /* metadata type resolution */
1054 ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST);
1055 }
1056
1057 if (node->schema->nodetype & LYD_NODE_TERM) {
1058 /* node type resolution */
1059 ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST);
1060 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1061 /* new node validation, autodelete */
1062 LY_CHECK_RET(lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL));
1063
1064 /* add nested defaults */
1065 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
Radek Krejci7931b192020-06-25 17:05:03 +02001066 when_check, validate_options));
Michal Vaskofea12c62020-03-30 11:00:15 +02001067 }
1068
1069 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1070 /* when evaluation */
1071 ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST);
1072 }
1073 }
1074
1075 LYD_TREE_DFS_END(root, next, node);
1076 }
1077
1078 return LY_SUCCESS;
1079}
1080
1081static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001082_lyd_validate(struct lyd_node **tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
1083 int val_opts)
Michal Vaskof03ed032020-03-04 13:31:44 +01001084{
1085 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001086 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001087 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001088 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001089 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001090
Michal Vaskob1b5c262020-03-05 14:29:47 +01001091 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || (modules && mod_count), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001092
Michal Vaskob1b5c262020-03-05 14:29:47 +01001093 next = *tree;
1094 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001095 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001096 mod = lyd_data_next_module(&next, &first);
1097 } else {
1098 mod = lyd_mod_next_module(next, modules, mod_count, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001099 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001100 if (!mod) {
1101 break;
1102 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001103 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001104 /* make sure first2 changes are carried to tree */
1105 first2 = tree;
1106 } else {
1107 first2 = &first;
1108 }
1109
1110 /* validate new top-level nodes of this module, autodelete */
Michal Vaskofea12c62020-03-30 11:00:15 +02001111 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001112
1113 /* add all top-level defaults for this module */
Michal Vaskofea12c62020-03-30 11:00:15 +02001114 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 +01001115
1116 /* process nested nodes */
1117 LY_LIST_FOR(*first2, first) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001118 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 +01001119 }
1120
1121 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vasko9f96a052020-03-10 09:41:45 +01001122 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 +01001123 LY_CHECK_GOTO(ret, cleanup);
1124
1125 /* perform final validation that assumes the data tree is final */
Radek Krejci7931b192020-06-25 17:05:03 +02001126 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0), cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001127 }
1128
Michal Vaskof03ed032020-03-04 13:31:44 +01001129cleanup:
1130 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001131 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001132 ly_set_erase(&when_check, NULL);
1133 return ret;
1134}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001135
1136API LY_ERR
1137lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts)
1138{
1139 return _lyd_validate(tree, NULL, 0, ctx, val_opts);
1140}
1141
1142API LY_ERR
1143lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts)
1144{
1145 return _lyd_validate(tree, modules, mod_count, NULL, val_opts);
1146}
Michal Vaskofea12c62020-03-30 11:00:15 +02001147
Michal Vaskocb7526d2020-03-30 15:08:26 +02001148static void
1149lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op, const struct lyd_node *tree,
1150 struct lyd_node **op_node, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001151{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001152 const struct lyd_node *tree_iter, *op_iter;
1153 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001154 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001155
Michal Vaskocb7526d2020-03-30 15:08:26 +02001156 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001157 op_depth = 0;
1158 for (op_iter = op; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
1159 ++op_depth;
1160 }
1161
1162 /* find where to merge op */
1163 tree_iter = tree;
1164 cur_depth = op_depth;
1165 op_iter = op;
1166 while (cur_depth) {
1167 /* find next op parent */
1168 op_iter = op;
1169 for (i = 0; i < cur_depth; ++i) {
1170 op_iter = (struct lyd_node *)op_iter->parent;
1171 }
1172
1173 /* find op iter in tree */
1174 lyd_find_sibling_first(tree_iter, op_iter, &match);
1175 if (!match) {
1176 break;
1177 }
1178
1179 /* move tree_iter */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001180 tree_iter = lyd_node_children(match, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001181
1182 /* move depth */
1183 --cur_depth;
1184 }
1185
Michal Vaskocb7526d2020-03-30 15:08:26 +02001186 *op_node = (struct lyd_node *)op_iter;
1187 *tree_sibling = (struct lyd_node *)tree_iter;
1188}
1189
1190API LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +02001191lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, LYD_VALIDATE_OP op)
Michal Vaskocb7526d2020-03-30 15:08:26 +02001192{
1193 LY_ERR ret;
Radek Krejci7931b192020-06-25 17:05:03 +02001194 struct lyd_node *tree_sibling, *op_subtree, *op_next, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001195 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1196
1197 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci7931b192020-06-25 17:05:03 +02001198 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001199
1200 /* find the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001201 LYD_TREE_DFS_BEGIN(op_tree, op_next, op_node) {
1202 if ((op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) && (op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001203 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001204 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001205 break;
1206 }
Radek Krejci7931b192020-06-25 17:05:03 +02001207 LYD_TREE_DFS_END(op_tree, op_next, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001208 }
Radek Krejci7931b192020-06-25 17:05:03 +02001209 if (op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) {
1210 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001211 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
1212 return LY_EINVAL;
1213 }
1214 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001215 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001216 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
1217 return LY_EINVAL;
1218 }
1219 }
1220
1221 /* move op_tree to top-level node */
1222 while (op_tree->parent) {
1223 op_tree = (struct lyd_node *)op_tree->parent;
1224 }
1225
1226 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001227 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1228 op_parent = (struct lyd_node *)op_subtree->parent;
1229 lyd_unlink_tree(op_subtree);
1230 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001231 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001232 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001233 }
1234
1235 /* prevalidate whole operation subtree */
Radek Krejci7931b192020-06-25 17:05:03 +02001236 LY_CHECK_GOTO(ret = lyd_validate_subtree(op_node, &type_check, &type_meta_check, &when_check, 0), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001237
1238 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1239 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
1240 LYD_JSON, lydjson_resolve_prefix, NULL), cleanup);
1241
1242 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001243 lyd_validate_obsolete(op_node);
1244 if (lysc_node_is_disabled(op_node->schema, 1)) {
1245 LOGVAL(LYD_NODE_CTX(op_tree), LY_VLOG_LYD, op_node, LY_VCODE_NOIFF, op_node->schema->name);
Michal Vaskofea12c62020-03-30 11:00:15 +02001246 ret = LY_EVALID;
1247 goto cleanup;
1248 }
Radek Krejci7931b192020-06-25 17:05:03 +02001249 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001250
1251 /* final validation of all the descendants */
Radek Krejci7931b192020-06-25 17:05:03 +02001252 LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_node_children(op_node, 0), op_node->schema, NULL, 0, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001253
1254cleanup:
1255 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001256 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001257 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001258 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001259 }
1260
1261 ly_set_erase(&type_check, NULL);
1262 ly_set_erase(&type_meta_check, NULL);
1263 ly_set_erase(&when_check, NULL);
1264 return ret;
1265}