blob: c550dc64aa5573fd622ea3ed1b7f6dec0bd6c772 [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 */
Michal Vaskofbed4ea2020-07-08 10:43:30 +020014#include "validation.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010015
16#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include <stdint.h>
Michal Vasko52927e22020-03-16 17:26:14 +010018#include <stdio.h>
19#include <stdlib.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <string.h>
Michal Vaskocde73ac2019-11-14 16:10:27 +010021
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
23#include "config.h"
24#include "hash_table.h"
25#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020026#include "parser_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "plugins_types.h"
28#include "set.h"
29#include "tree.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010030#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010032#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010034
Michal Vaskobb844672020-07-03 11:06:12 +020035/**
36 * @brief Just like lys_getnext() but iterates over all data instances of the schema nodes.
37 *
38 * @param[in] last Last returned data node.
39 * @param[in] sibling Data node sibling to search in.
40 * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards.
41 * May not be set if the function is used only for any suitable node existence check (such as the existence
42 * of any choice case data).
43 * @param[in] parent Schema parent of the iterated children nodes.
44 * @param[in] module Schema module of the iterated top-level nodes.
45 * @return Next matching data node,
46 * @return NULL if last data node was already returned.
47 */
Michal Vaskof03ed032020-03-04 13:31:44 +010048static struct lyd_node *
49lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
50 const struct lysc_node *parent, const struct lysc_module *module)
51{
52 const struct lysc_node *siter = NULL;
53 struct lyd_node *match = NULL;
54
55 assert(parent || module);
56 assert(!last || (slast && *slast));
57
58 if (slast) {
59 siter = *slast;
60 }
61
62 if (last && last->next) {
63 /* find next data instance */
64 lyd_find_sibling_next2(last->next, siter, NULL, 0, &match);
65 if (match) {
66 return match;
67 }
68 }
69
70 /* find next schema node data instance */
71 while ((siter = lys_getnext(siter, parent, module, 0))) {
72 switch (siter->nodetype) {
73 case LYS_CONTAINER:
74 case LYS_ANYXML:
75 case LYS_ANYDATA:
76 case LYS_LEAF:
77 lyd_find_sibling_val(sibling, siter, NULL, 0, &match);
78 break;
79 case LYS_LIST:
80 case LYS_LEAFLIST:
81 lyd_find_sibling_next2(sibling, siter, NULL, 0, &match);
82 break;
83 default:
84 assert(0);
85 LOGINT(NULL);
86 }
87
88 if (match) {
89 break;
90 }
91 }
92
93 if (slast) {
94 *slast = siter;
95 }
96 return match;
97}
98
Michal Vaskocde73ac2019-11-14 16:10:27 +010099/**
100 * @brief Evaluate a single "when" condition.
101 *
Michal Vaskob1b5c262020-03-05 14:29:47 +0100102 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100103 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100104 * @param[in] when When to evaluate.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100105 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
106 */
107static LY_ERR
Michal Vaskoc193ce92020-03-06 11:04:48 +0100108lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100109{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100110 LY_ERR ret = LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100111 const struct lyd_node *ctx_node;
112 struct lyxp_set xp_set;
113
114 memset(&xp_set, 0, sizeof xp_set);
115
116 if (when->context == node->schema) {
117 ctx_node = node;
118 } else {
119 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
120 ctx_node = (struct lyd_node *)node->parent;
121 }
122
123 /* evaluate when */
Michal Vasko52927e22020-03-16 17:26:14 +0100124 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 +0100125 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100126 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
127
128 /* return error or LY_EINCOMPLETE for dependant unresolved when */
129 LY_CHECK_RET(ret);
130
131 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +0200132 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100133 if (node->flags & LYD_WHEN_TRUE) {
134 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100135 if (LYD_DEL_IS_ROOT(*tree, node)) {
136 *tree = (*tree)->next;
137 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100138 lyd_free_tree(node);
139 } else {
140 /* invalid data */
Michal Vaskocc048b22020-03-27 15:52:38 +0100141 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100142 ret = LY_EVALID;
143 }
144 } else {
145 /* remember that when evaluated to true */
146 node->flags |= LYD_WHEN_TRUE;
147 }
148
149 return ret;
150}
151
152LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100153lyd_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 +0100154 LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100155{
156 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200157 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100158
Michal Vaskob1b5c262020-03-05 14:29:47 +0100159 if (node_when) {
160 /* evaluate all when conditions */
161 uint32_t prev_count;
162 do {
163 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200164 i = 0;
165 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100166 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200167 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100168 const struct lysc_node *schema = node->schema;
169 int unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100170
Michal Vaskob1b5c262020-03-05 14:29:47 +0100171 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200172 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200173 LY_ARRAY_FOR(schema->when, u) {
174 ret = lyd_validate_when(tree, node, schema->when[u]);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100175 if (ret) {
176 break;
177 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100178 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100179 if (ret == LY_EINCOMPLETE) {
180 /* could not evaluate this when */
181 unres_when = 1;
182 break;
183 } else if (ret) {
184 /* error */
185 return ret;
186 }
187 schema = schema->parent;
188 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100189
Michal Vaskob1b5c262020-03-05 14:29:47 +0100190 if (unres_when) {
191 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200192 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100193 } else {
194 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200195 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100196 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100197 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100198
Michal Vaskob1b5c262020-03-05 14:29:47 +0100199 /* there must have been some when conditions resolved */
200 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100201
Michal Vaskob1b5c262020-03-05 14:29:47 +0100202 /* there could have been no cyclic when dependencies, checked during compilation */
203 assert(!node_when->count);
204 }
205
206 if (node_types && node_types->count) {
207 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200208 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100209 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200210 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100211
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200212 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100213
214 /* validate and store the value of the node */
215 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
216 parser_data, format, *tree);
217 LY_CHECK_RET(ret);
218
219 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200220 ly_set_rm_index(node_types, i, NULL);
221 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100222 }
223
Michal Vasko9f96a052020-03-10 09:41:45 +0100224 if (meta_types && meta_types->count) {
225 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200226 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100227 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200228 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100229
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200230 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100231
Michal Vasko9f96a052020-03-10 09:41:45 +0100232 /* validate and store the value of the metadata */
233 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original,
234 strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100235 LY_CHECK_RET(ret);
236
237 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200238 ly_set_rm_index(meta_types, i, NULL);
239 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100240 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100241
242 return ret;
243}
244
Michal Vaskobb844672020-07-03 11:06:12 +0200245/**
246 * @brief Validate instance duplication.
247 *
248 * @param[in] first First sibling to search in.
249 * @param[in] node Data node instance to check.
250 * @return LY_ERR value.
251 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100252static LY_ERR
253lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100254{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100255 struct lyd_node **match_p;
256 int fail = 0;
257
258 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
259 /* duplicate instances allowed */
260 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100261 }
262
Michal Vaskob1b5c262020-03-05 14:29:47 +0100263 /* find exactly the same next instance using hashes if possible */
264 if (node->parent && node->parent->children_ht) {
265 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
266 fail = 1;
267 }
268 } else {
269 for (; first; first = first->next) {
270 if (first == node) {
271 continue;
272 }
273
274 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
275 if (first->schema == node->schema) {
276 fail = 1;
277 break;
278 }
279 } else if (!lyd_compare(first, node, 0)) {
280 fail = 1;
281 break;
282 }
283 }
284 }
285
286 if (fail) {
287 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
288 return LY_EVALID;
289 }
290 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100291}
292
Michal Vaskobb844672020-07-03 11:06:12 +0200293/**
294 * @brief Validate multiple case data existence with possible autodelete.
295 *
296 * @param[in,out] first First sibling to search in, is updated if needed.
297 * @param[in] choic Choice node whose cases to check.
298 * @return LY_ERR value.
299 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100300static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100301lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic)
302{
303 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
304 struct lyd_node *match, *to_del;
305 int found;
306
307 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
308 found = 0;
309 iter = NULL;
310 match = NULL;
311 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
312 if (match->flags & LYD_NEW) {
313 /* a new case data found, nothing more to look for */
314 found = 2;
315 break;
316 } else {
317 /* and old case data found */
318 if (found == 0) {
319 found = 1;
320 }
321 }
322 }
323
324 if (found == 1) {
325 /* there should not be 2 old cases */
326 if (old_case) {
327 /* old data from 2 cases */
328 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
329 return LY_EVALID;
330 }
331
332 /* remember an old existing case */
333 old_case = scase;
334 } else if (found == 2) {
335 if (new_case) {
336 /* new data from 2 cases */
337 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
338 return LY_EVALID;
339 }
340
341 /* remember a new existing case */
342 new_case = scase;
343 }
344 }
345
346 if (old_case && new_case) {
347 /* auto-delete old case */
348 iter = NULL;
349 match = NULL;
350 to_del = NULL;
351 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
352 if (LYD_DEL_IS_ROOT(*first, to_del)) {
353 *first = (*first)->next;
354 }
355 lyd_free_tree(to_del);
356 to_del = match;
357 }
358 if (LYD_DEL_IS_ROOT(*first, to_del)) {
359 *first = (*first)->next;
360 }
361 lyd_free_tree(to_del);
362 }
363
364 return LY_SUCCESS;
365}
366
Michal Vaskobb844672020-07-03 11:06:12 +0200367/**
368 * @brief Check whether a schema node can have some default values (true for NP containers as well).
369 *
370 * @param[in] schema Schema node to check.
371 * @return non-zero if yes,
372 * @return 0 otherwise.
373 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100374static int
375lyd_val_has_default(const struct lysc_node *schema)
376{
377 switch (schema->nodetype) {
378 case LYS_LEAF:
379 if (((struct lysc_node_leaf *)schema)->dflt) {
380 return 1;
381 }
382 break;
383 case LYS_LEAFLIST:
384 if (((struct lysc_node_leaflist *)schema)->dflts) {
385 return 1;
386 }
387 break;
388 case LYS_CONTAINER:
389 if (!(schema->flags & LYS_PRESENCE)) {
390 return 1;
391 }
392 break;
393 default:
394 break;
395 }
396
397 return 0;
398}
399
Michal Vaskobb844672020-07-03 11:06:12 +0200400/**
401 * @brief Autodelete old instances to prevent validation errors.
402 *
403 * @param[in,out] first First sibling to search in, is updated if needed.
404 * @param[in] node Data node instance to check.
405 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
406 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100407static void
408lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p)
409{
410 struct lyd_node *match, *next;
411
412 if (lyd_val_has_default(node->schema)) {
413 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
414 if (node->schema->nodetype == LYS_LEAFLIST) {
415 lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match);
416 } else {
417 lyd_find_sibling_val(*first, node->schema, NULL, 0, &match);
418 }
419
420 while (match) {
421 next = match->next;
422 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
423 /* default instance found, remove it */
424 if (LYD_DEL_IS_ROOT(*first, match)) {
425 *first = (*first)->next;
426 }
427 if (match == *next_p) {
428 *next_p = (*next_p)->next;
429 }
430 lyd_free_tree(match);
431
432 /* remove only a single container/leaf default instance, if there are more, it is an error */
433 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
434 break;
435 }
436 }
437
438 lyd_find_sibling_next2(next, node->schema, NULL, 0, &match);
439 }
440 }
441}
442
443LY_ERR
444lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod)
445{
446 struct lyd_node *next, *node;
447 const struct lysc_node *snode = NULL;
448
449 assert(first && (sparent || mod));
450
451 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
452 /* check case duplicites */
453 if (snode->nodetype == LYS_CHOICE) {
454 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode));
455 }
456 }
457
458 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100459 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100460 /* all top-level data from this module checked */
461 break;
462 }
463
464 if (!(node->flags & LYD_NEW)) {
465 /* check only new nodes */
466 continue;
467 }
468
469 /* remove old default(s) if it exists */
470 lyd_validate_autodel_dup(first, node, &next);
471
472 /* then check new node instance duplicities */
473 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
474
475 /* this node is valid */
476 node->flags &= ~LYD_NEW;
477 }
478
479 return LY_SUCCESS;
480}
481
Michal Vaskobb844672020-07-03 11:06:12 +0200482/**
483 * @brief Validate mandatory node existence.
484 *
485 * @param[in] first First sibling to search in.
486 * @param[in] snode Schema node to validate.
487 * @return LY_ERR value.
488 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100489static LY_ERR
490lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100491{
Michal Vaskoa3881362020-01-21 15:57:35 +0100492 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100493 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100494 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100495 return LY_SUCCESS;
496 }
497 } else {
498 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100499
Michal Vaskob1b5c262020-03-05 14:29:47 +0100500 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100501 /* data instance found */
502 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100503 }
504 }
505
506 /* node instance not found */
507 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
508 return LY_EVALID;
509}
510
Michal Vaskobb844672020-07-03 11:06:12 +0200511/**
512 * @brief Validate min/max-elements constraints, if any.
513 *
514 * @param[in] first First sibling to search in.
515 * @param[in] snode Schema node to validate.
516 * @param[in] min Minimum number of elements, 0 for no restriction.
517 * @param[in] max Max number of elements, 0 for no restriction.
518 * @return LY_ERR value.
519 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100520static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100521lyd_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 +0100522{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100523 uint32_t count = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100524 const struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100525
Michal Vasko9b368d32020-02-14 13:53:31 +0100526 assert(min || max);
527
Michal Vaskob1b5c262020-03-05 14:29:47 +0100528 LY_LIST_FOR(first, iter) {
Michal Vaskoacd83e72020-02-04 14:12:01 +0100529 if (iter->schema == snode) {
530 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100531
532 if (min && (count == min)) {
533 /* satisfied */
534 min = 0;
535 if (!max) {
536 /* nothing more to check */
537 break;
538 }
539 }
540 if (max && (count > max)) {
541 /* not satisifed */
542 break;
543 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100544 }
545 }
546
Michal Vasko9b368d32020-02-14 13:53:31 +0100547 if (min) {
548 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100549 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
550 return LY_EVALID;
551 } else if (max && (count > max)) {
552 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
553 return LY_EVALID;
554 }
555
Michal Vaskoa3881362020-01-21 15:57:35 +0100556 return LY_SUCCESS;
557}
558
Michal Vaskobb844672020-07-03 11:06:12 +0200559/**
560 * @brief Find node referenced by a list unique statement.
561 *
562 * @param[in] uniq_leaf Unique leaf to find.
563 * @param[in] list List instance to use for the search.
564 * @return Found leaf,
565 * @return NULL if no leaf found.
566 */
Michal Vasko14654712020-02-06 08:35:21 +0100567static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200568lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100569{
Michal Vasko9b368d32020-02-14 13:53:31 +0100570 struct lyd_node *node;
571 const struct lysc_node *iter;
572 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100573
Michal Vasko9b368d32020-02-14 13:53:31 +0100574 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200575 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
576 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100577 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100578
Michal Vaskobb844672020-07-03 11:06:12 +0200579 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100580 while (node && depth) {
581 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200582 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
583 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100584 }
585
586 /* find iter instance in children */
587 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200588 lyd_find_sibling_val(lyd_node_children(node, 0), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100589 --depth;
590 }
591
Michal Vasko14654712020-02-06 08:35:21 +0100592 return node;
593}
594
Michal Vaskobb844672020-07-03 11:06:12 +0200595/**
596 * @brief Callback for comparing 2 list unique leaf values.
597 *
598 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100599 */
600static int
601lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
602{
603 struct ly_ctx *ctx;
604 struct lysc_node_list *slist;
605 struct lyd_node *diter, *first, *second;
606 struct lyd_value *val1, *val2;
607 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200608 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100609
610 assert(val1_p && val2_p);
611
612 first = *((struct lyd_node **)val1_p);
613 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200614 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100615
616 assert(first && (first->schema->nodetype == LYS_LIST));
617 assert(second && (second->schema == first->schema));
618
619 ctx = first->schema->module->ctx;
620
621 slist = (struct lysc_node_list *)first->schema;
622
623 /* compare unique leaves */
624 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200625 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200626 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100627 goto uniquecheck;
628 }
629 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200630 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100631uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200632 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100633 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200634 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100635 if (diter) {
636 val1 = &((struct lyd_node_term *)diter)->value;
637 } else {
638 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200639 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100640 }
641
642 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200643 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100644 if (diter) {
645 val2 = &((struct lyd_node_term *)diter)->value;
646 } else {
647 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200648 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100649 }
650
651 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
652 /* values differ or either one is not set */
653 break;
654 }
655 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200656 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100657 /* all unique leafs are the same in this set, create this nice error */
658 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
659 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
660
661 /* use buffer to rebuild the unique string */
662 uniq_str = malloc(1024);
663 uniq_str[0] = '\0';
664 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200665 LY_ARRAY_FOR(slist->uniques[u], v) {
666 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100667 strcpy(ptr, " ");
668 ++ptr;
669 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200670 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 +0100671 ptr, 1024 - (ptr - uniq_str));
672 if (!ptr) {
673 /* path will be incomplete, whatever */
674 break;
675 }
676
677 ptr += strlen(ptr);
678 }
679 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
680
681 free(path1);
682 free(path2);
683 free(uniq_str);
684 return 1;
685 }
686
687 if (action > 0) {
688 /* done */
689 return 0;
690 }
691 }
692
693 return 0;
694}
695
Michal Vaskobb844672020-07-03 11:06:12 +0200696/**
697 * @brief Validate list unique leaves.
698 *
699 * @param[in] first First sibling to search in.
700 * @param[in] snode Schema node to validate.
701 * @param[in] uniques List unique arrays to validate.
702 * @return LY_ERR value.
703 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100704static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200705lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, const struct lysc_node_leaf ***uniques)
Michal Vaskoa3881362020-01-21 15:57:35 +0100706{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100707 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100708 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200709 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100710 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200711 uint32_t hash, i, size = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100712 int dynamic;
713 const char *str;
714 struct hash_table **uniqtables = NULL;
715 struct lyd_value *val;
716 struct ly_ctx *ctx = snode->module->ctx;
717
718 assert(uniques);
719
720 /* get all list instances */
Michal Vasko9b368d32020-02-14 13:53:31 +0100721 set = ly_set_new();
722 LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100723 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100724 if (diter->schema == snode) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100725 ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100726 }
727 }
Michal Vasko14654712020-02-06 08:35:21 +0100728
729 if (set->count == 2) {
730 /* simple comparison */
731 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
732 /* instance duplication */
733 ret = LY_EVALID;
734 goto cleanup;
735 }
736 } else if (set->count > 2) {
737 /* use hashes for comparison */
738 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200739 for (i = 31; i > 0; i--) {
740 size = set->count << i;
741 size = size >> i;
742 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100743 break;
744 }
745 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200746 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
747 i = 32 - i;
748 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100749
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200750 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100751 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200752 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200753 for (v = 0; v < x; v++) {
754 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
755 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100756 }
757
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200758 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100759 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200760 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100761 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200762 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200763 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100764 if (diter) {
765 val = &((struct lyd_node_term *)diter)->value;
766 } else {
767 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200768 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100769 }
770 if (!val) {
771 /* unique item not present nor has default value */
772 break;
773 }
774
775 /* get canonical string value */
776 str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic);
777 hash = dict_hash_multi(hash, str, strlen(str));
778 if (dynamic) {
779 free((char *)str);
780 }
781 }
782 if (!val) {
783 /* skip this list instance since its unique set is incomplete */
784 continue;
785 }
786
787 /* finish the hash value */
788 hash = dict_hash_multi(hash, NULL, 0);
789
790 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200791 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100792 if (ret == LY_EEXIST) {
793 /* instance duplication */
794 ret = LY_EVALID;
795 }
796 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
797 }
798 }
799 }
800
801cleanup:
802 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200803 for (v = 0; v < x; v++) {
804 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100805 /* failed when allocating uniquetables[j], following j are not allocated */
806 break;
807 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200808 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100809 }
810 free(uniqtables);
811
812 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100813}
814
Michal Vaskobb844672020-07-03 11:06:12 +0200815/**
816 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
817 *
818 * @param[in] first First sibling to search in.
819 * @param[in] sparent Schema parent of the nodes to check.
820 * @param[in] mod Module of the nodes to check.
821 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
822 * @param[in] op Operation being validated, if any.
823 * @return LY_ERR value.
824 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100825static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100826lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci7931b192020-06-25 17:05:03 +0200827 const struct lysc_module *mod, int val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100828{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100829 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100830 struct lysc_node_list *slist;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200831 int getnext_opts;
832
Radek Krejci7931b192020-06-25 17:05:03 +0200833 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100834
Michal Vaskoa3881362020-01-21 15:57:35 +0100835 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200836 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200837 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100838 continue;
839 }
840
Michal Vaskoa3881362020-01-21 15:57:35 +0100841 /* check min-elements and max-elements */
842 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
843 slist = (struct lysc_node_list *)snode;
844 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100845 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100846 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100847
848 /* check generic mandatory existence */
849 } else if (snode->flags & LYS_MAND_TRUE) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100850 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100851 }
852
853 /* check unique */
854 if (snode->nodetype == LYS_LIST) {
855 slist = (struct lysc_node_list *)snode;
856 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200857 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100858 }
859 }
860
Michal Vaskoacd83e72020-02-04 14:12:01 +0100861 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
862 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200863 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100864 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100865 }
866
Michal Vaskoacd83e72020-02-04 14:12:01 +0100867 return LY_SUCCESS;
868}
869
Michal Vaskobb844672020-07-03 11:06:12 +0200870/**
871 * @brief Validate obsolete nodes, only warnings are printed.
872 *
873 * @param[in] node Node to check.
874 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100875static void
876lyd_validate_obsolete(const struct lyd_node *node)
877{
878 const struct lysc_node *snode;
879
880 snode = node->schema;
881 do {
882 if (snode->flags & LYS_STATUS_OBSLT) {
883 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
884 break;
885 }
886
887 snode = snode->parent;
888 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
889}
890
Michal Vaskobb844672020-07-03 11:06:12 +0200891/**
892 * @brief Validate must conditions of a data node.
893 *
894 * @param[in] node Node to validate.
895 * @param[in] op Operation being validated, if any.
896 * @return LY_ERR value.
897 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100898static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200899lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100900{
901 struct lyxp_set xp_set;
902 struct lysc_must *musts;
903 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200904 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100905
906 switch (node->schema->nodetype) {
907 case LYS_CONTAINER:
908 musts = ((struct lysc_node_container *)node->schema)->musts;
909 break;
910 case LYS_LEAF:
911 musts = ((struct lysc_node_leaf *)node->schema)->musts;
912 break;
913 case LYS_LEAFLIST:
914 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
915 break;
916 case LYS_LIST:
917 musts = ((struct lysc_node_list *)node->schema)->musts;
918 break;
919 case LYS_ANYXML:
920 case LYS_ANYDATA:
921 musts = ((struct lysc_node_anydata *)node->schema)->musts;
922 break;
923 case LYS_NOTIF:
924 musts = ((struct lysc_notif *)node->schema)->musts;
925 break;
926 case LYS_RPC:
927 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200928 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200929 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200930 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200931 musts = ((struct lysc_action *)node->schema)->output.musts;
932 } else {
933 LOGINT(LYD_NODE_CTX(node));
934 return LY_EINT;
935 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100936 break;
937 default:
938 LOGINT(LYD_NODE_CTX(node));
939 return LY_EINT;
940 }
941
942 if (!musts) {
943 /* no must to evaluate */
944 return LY_SUCCESS;
945 }
946
947 /* find first top-level node */
948 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent);
949 while (tree->prev->next) {
950 tree = tree->prev;
951 }
952
953 LY_ARRAY_FOR(musts, u) {
954 memset(&xp_set, 0, sizeof xp_set);
955
956 /* evaluate must */
957 LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA));
958
959 /* check the result */
960 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200961 if (!xp_set.val.bln) {
Michal Vaskocc048b22020-03-27 15:52:38 +0100962 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
963 return LY_EVALID;
964 }
965 }
966
967 return LY_SUCCESS;
968}
969
Michal Vaskob1b5c262020-03-05 14:29:47 +0100970LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200971lyd_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 +0100972{
Radek Krejci7f769d72020-07-11 23:13:56 +0200973 struct lyd_node *next = NULL, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100974 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100975
Michal Vasko14654712020-02-06 08:35:21 +0100976 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100977 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100978 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100979 /* all top-level data from this module checked */
980 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100981 }
982
Michal Vaskoa8c61722020-03-27 16:59:32 +0100983 /* opaque data */
984 if (!node->schema) {
985 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
986 ((struct lyd_node_opaq *)node)->name);
987 return LY_EVALID;
988 }
989
Michal Vaskocb7526d2020-03-30 15:08:26 +0200990 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +0200991 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200992 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
993 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200994 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200995 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
996 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200997 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200998 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +0100999 return LY_EVALID;
1000 }
1001
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001002 /* obsolete data */
1003 lyd_validate_obsolete(node);
1004
Michal Vaskoc193ce92020-03-06 11:04:48 +01001005 /* node's schema if-features */
1006 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskoa8c61722020-03-27 16:59:32 +01001007 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +01001008 return LY_EVALID;
1009 }
1010
Michal Vaskocc048b22020-03-27 15:52:38 +01001011 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001012 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001013
Michal Vaskoa8c61722020-03-27 16:59:32 +01001014 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +01001015 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001016
Michal Vasko14654712020-02-06 08:35:21 +01001017 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +02001018 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001019
Michal Vaskob1b5c262020-03-05 14:29:47 +01001020 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001021 /* validate all children recursively */
Radek Krejci7931b192020-06-25 17:05:03 +02001022 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 +01001023
Michal Vaskob1b5c262020-03-05 14:29:47 +01001024 /* set default for containers */
1025 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001026 LY_LIST_FOR(lyd_node_children(node, 0), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001027 if (!(next->flags & LYD_DEFAULT)) {
1028 break;
1029 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001030 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001031 if (!next) {
1032 node->flags |= LYD_DEFAULT;
1033 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001034 }
1035 }
1036
1037 return LY_SUCCESS;
1038}
1039
1040LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001041lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001042 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 +01001043{
Michal Vaskob1b5c262020-03-05 14:29:47 +01001044 LY_ERR ret;
Michal Vasko9b368d32020-02-14 13:53:31 +01001045 const struct lysc_node *iter = NULL;
1046 struct lyd_node *node;
1047 struct lyd_value **dflts;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001048 LY_ARRAY_COUNT_TYPE u;
Michal Vasko9b368d32020-02-14 13:53:31 +01001049
Michal Vaskob1b5c262020-03-05 14:29:47 +01001050 assert(first && (parent || sparent || mod) && node_types && node_when);
Michal Vasko9b368d32020-02-14 13:53:31 +01001051
Michal Vaskob1b5c262020-03-05 14:29:47 +01001052 if (!sparent && parent) {
1053 sparent = parent->schema;
1054 }
1055
1056 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
Radek Krejci7931b192020-06-25 17:05:03 +02001057 if ((val_opts & LYD_VALIDATE_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001058 continue;
1059 }
1060
Michal Vasko9b368d32020-02-14 13:53:31 +01001061 switch (iter->nodetype) {
1062 case LYS_CHOICE:
Michal Vaskof03ed032020-03-04 13:31:44 +01001063 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001064 /* create default case data */
1065 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 +01001066 NULL, node_types, node_when, val_opts));
Michal Vasko9b368d32020-02-14 13:53:31 +01001067 }
1068 break;
1069 case LYS_CONTAINER:
1070 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001071 /* create default NP container */
Michal Vasko9b368d32020-02-14 13:53:31 +01001072 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001073 node->flags = LYD_DEFAULT;
1074 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001075
1076 if (iter->when) {
1077 /* remember to resolve when */
1078 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1079 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001080
1081 /* create any default children */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001082 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 +01001083 }
1084 break;
1085 case LYS_LEAF:
1086 if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1087 /* create default leaf */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001088 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1089 if (ret == LY_EINCOMPLETE) {
1090 /* remember to resolve type */
1091 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1092 } else if (ret) {
1093 return ret;
1094 }
1095 node->flags = LYD_DEFAULT;
1096 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001097
1098 if (iter->when) {
1099 /* remember to resolve when */
1100 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1101 }
1102 }
1103 break;
1104 case LYS_LEAFLIST:
1105 if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_next2(*first, iter, NULL, 0, NULL)) {
1106 /* create all default leaf-lists */
1107 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001108 LY_ARRAY_FOR(dflts, u) {
1109 ret = lyd_create_term2(iter, dflts[u], &node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001110 if (ret == LY_EINCOMPLETE) {
1111 /* remember to resolve type */
1112 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1113 } else if (ret) {
1114 return ret;
1115 }
1116 node->flags = LYD_DEFAULT;
1117 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001118
1119 if (iter->when) {
1120 /* remember to resolve when */
1121 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1122 }
1123 }
1124 }
1125 break;
1126 default:
1127 /* without defaults */
1128 break;
1129 }
1130 }
1131
1132 return LY_SUCCESS;
1133}
1134
Radek Krejci7931b192020-06-25 17:05:03 +02001135/**
Michal Vaskobb844672020-07-03 11:06:12 +02001136 * @brief Validate the whole data subtree.
1137 *
1138 * @param[in] root Subtree root.
1139 * @param[in,out] type_check Set for unres node types.
1140 * @param[in,out] type_meta_check Set for unres metadata types.
1141 * @param[in,out] when_check Set for nodes with when conditions.
1142 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
1143 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001144 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001145static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001146lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Michal Vaskobb844672020-07-03 11:06:12 +02001147 struct ly_set *when_check, int val_opts)
Michal Vaskofea12c62020-03-30 11:00:15 +02001148{
1149 const struct lyd_meta *meta;
1150 struct lyd_node *next, *node;
1151
1152 LYD_TREE_DFS_BEGIN(root, next, node) {
1153 /* skip added default nodes */
1154 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1155 LY_LIST_FOR(node->meta, meta) {
1156 /* metadata type resolution */
1157 ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST);
1158 }
1159
1160 if (node->schema->nodetype & LYD_NODE_TERM) {
1161 /* node type resolution */
1162 ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST);
1163 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1164 /* new node validation, autodelete */
1165 LY_CHECK_RET(lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL));
1166
1167 /* add nested defaults */
1168 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
Michal Vaskobb844672020-07-03 11:06:12 +02001169 when_check, val_opts));
Michal Vaskofea12c62020-03-30 11:00:15 +02001170 }
1171
1172 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1173 /* when evaluation */
1174 ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST);
1175 }
1176 }
1177
1178 LYD_TREE_DFS_END(root, next, node);
1179 }
1180
1181 return LY_SUCCESS;
1182}
1183
Michal Vaskobb844672020-07-03 11:06:12 +02001184/**
1185 * @brief Validate data tree.
1186 *
1187 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1188 * @param[in] modules Array of modules to validate, NULL for all modules.
1189 * @param[in] mod_count Count of @p modules.
1190 * @param[in] ly_ctx libyang context.
1191 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
1192 * @return LY_ERR value.
1193 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001194static LY_ERR
Michal Vasko26e80012020-07-08 10:55:46 +02001195_lyd_validate(struct lyd_node **tree, const struct lys_module *module, const struct ly_ctx *ctx, int val_opts)
Michal Vaskof03ed032020-03-04 13:31:44 +01001196{
1197 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001198 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001199 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001200 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001201 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001202
Michal Vasko26e80012020-07-08 10:55:46 +02001203 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001204
Michal Vaskob1b5c262020-03-05 14:29:47 +01001205 next = *tree;
1206 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001207 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001208 mod = lyd_data_next_module(&next, &first);
1209 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001210 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001211 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001212 if (!mod) {
1213 break;
1214 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001215 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001216 /* make sure first2 changes are carried to tree */
1217 first2 = tree;
1218 } else {
1219 first2 = &first;
1220 }
1221
1222 /* validate new top-level nodes of this module, autodelete */
Michal Vaskofea12c62020-03-30 11:00:15 +02001223 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001224
1225 /* add all top-level defaults for this module */
Michal Vaskofea12c62020-03-30 11:00:15 +02001226 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 +01001227
1228 /* process nested nodes */
1229 LY_LIST_FOR(*first2, first) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001230 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 +01001231 }
1232
1233 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vasko9f96a052020-03-10 09:41:45 +01001234 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 +01001235 LY_CHECK_GOTO(ret, cleanup);
1236
1237 /* perform final validation that assumes the data tree is final */
Radek Krejci7931b192020-06-25 17:05:03 +02001238 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0), cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001239 }
1240
Michal Vaskof03ed032020-03-04 13:31:44 +01001241cleanup:
1242 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001243 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001244 ly_set_erase(&when_check, NULL);
1245 return ret;
1246}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001247
1248API LY_ERR
1249lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts)
1250{
Michal Vasko26e80012020-07-08 10:55:46 +02001251 return _lyd_validate(tree, NULL, ctx, val_opts);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001252}
1253
1254API LY_ERR
Michal Vasko26e80012020-07-08 10:55:46 +02001255lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, int val_opts)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001256{
Michal Vasko26e80012020-07-08 10:55:46 +02001257 return _lyd_validate(tree, module, NULL, val_opts);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001258}
Michal Vaskofea12c62020-03-30 11:00:15 +02001259
Michal Vaskobb844672020-07-03 11:06:12 +02001260/**
1261 * @brief Find nodes for merging an operation into data tree for validation.
1262 *
1263 * @param[in] op_tree Full operation data tree.
1264 * @param[in] op_node Operation node itself.
1265 * @param[in] tree Data tree to be merged into.
1266 * @param[out] op_subtree Operation subtree to merge.
1267 * @param[out] tree_sibling Data tree sibling to merge next to.
1268 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001269static void
Michal Vaskobb844672020-07-03 11:06:12 +02001270lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
1271 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001272{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001273 const struct lyd_node *tree_iter, *op_iter;
1274 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001275 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001276
Michal Vaskocb7526d2020-03-30 15:08:26 +02001277 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001278 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001279 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001280 ++op_depth;
1281 }
1282
1283 /* find where to merge op */
1284 tree_iter = tree;
1285 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001286 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001287 while (cur_depth) {
1288 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001289 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001290 for (i = 0; i < cur_depth; ++i) {
1291 op_iter = (struct lyd_node *)op_iter->parent;
1292 }
1293
1294 /* find op iter in tree */
1295 lyd_find_sibling_first(tree_iter, op_iter, &match);
1296 if (!match) {
1297 break;
1298 }
1299
1300 /* move tree_iter */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001301 tree_iter = lyd_node_children(match, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001302
1303 /* move depth */
1304 --cur_depth;
1305 }
1306
Michal Vaskobb844672020-07-03 11:06:12 +02001307 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001308 *tree_sibling = (struct lyd_node *)tree_iter;
1309}
1310
1311API LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +02001312lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, LYD_VALIDATE_OP op)
Michal Vaskocb7526d2020-03-30 15:08:26 +02001313{
1314 LY_ERR ret;
Radek Krejci7931b192020-06-25 17:05:03 +02001315 struct lyd_node *tree_sibling, *op_subtree, *op_next, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001316 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1317
1318 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci7931b192020-06-25 17:05:03 +02001319 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001320
1321 /* find the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001322 LYD_TREE_DFS_BEGIN(op_tree, op_next, op_node) {
1323 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 +02001324 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001325 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001326 break;
1327 }
Radek Krejci7931b192020-06-25 17:05:03 +02001328 LYD_TREE_DFS_END(op_tree, op_next, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001329 }
Radek Krejci7931b192020-06-25 17:05:03 +02001330 if (op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) {
1331 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001332 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
1333 return LY_EINVAL;
1334 }
1335 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001336 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001337 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
1338 return LY_EINVAL;
1339 }
1340 }
1341
1342 /* move op_tree to top-level node */
1343 while (op_tree->parent) {
1344 op_tree = (struct lyd_node *)op_tree->parent;
1345 }
1346
1347 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001348 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1349 op_parent = (struct lyd_node *)op_subtree->parent;
1350 lyd_unlink_tree(op_subtree);
1351 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001352 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001353 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001354 }
1355
1356 /* prevalidate whole operation subtree */
Radek Krejci7931b192020-06-25 17:05:03 +02001357 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 +02001358
1359 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1360 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
1361 LYD_JSON, lydjson_resolve_prefix, NULL), cleanup);
1362
1363 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001364 lyd_validate_obsolete(op_node);
1365 if (lysc_node_is_disabled(op_node->schema, 1)) {
1366 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 +02001367 ret = LY_EVALID;
1368 goto cleanup;
1369 }
Radek Krejci7931b192020-06-25 17:05:03 +02001370 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001371
1372 /* final validation of all the descendants */
Radek Krejci7931b192020-06-25 17:05:03 +02001373 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 +02001374
1375cleanup:
1376 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001377 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001378 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001379 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001380 }
1381
1382 ly_set_erase(&type_check, NULL);
1383 ly_set_erase(&type_meta_check, NULL);
1384 ly_set_erase(&when_check, NULL);
1385 return ret;
1386}