blob: 437ce21c8c0595581a05ef5cb8765ab0aea3f5a3 [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"
Michal Vasko8104fd42020-07-13 11:09:51 +020024#include "diff.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "hash_table.h"
26#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020027#include "parser_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "plugins_types.h"
29#include "set.h"
30#include "tree.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010031#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010033#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010035
Michal Vaskobb844672020-07-03 11:06:12 +020036/**
37 * @brief Just like lys_getnext() but iterates over all data instances of the schema nodes.
38 *
39 * @param[in] last Last returned data node.
40 * @param[in] sibling Data node sibling to search in.
41 * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards.
42 * May not be set if the function is used only for any suitable node existence check (such as the existence
43 * of any choice case data).
44 * @param[in] parent Schema parent of the iterated children nodes.
45 * @param[in] module Schema module of the iterated top-level nodes.
46 * @return Next matching data node,
47 * @return NULL if last data node was already returned.
48 */
Michal Vaskof03ed032020-03-04 13:31:44 +010049static struct lyd_node *
50lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
51 const struct lysc_node *parent, const struct lysc_module *module)
52{
53 const struct lysc_node *siter = NULL;
54 struct lyd_node *match = NULL;
55
56 assert(parent || module);
57 assert(!last || (slast && *slast));
58
59 if (slast) {
60 siter = *slast;
61 }
62
63 if (last && last->next) {
64 /* find next data instance */
65 lyd_find_sibling_next2(last->next, siter, NULL, 0, &match);
66 if (match) {
67 return match;
68 }
69 }
70
71 /* find next schema node data instance */
72 while ((siter = lys_getnext(siter, parent, module, 0))) {
73 switch (siter->nodetype) {
74 case LYS_CONTAINER:
75 case LYS_ANYXML:
76 case LYS_ANYDATA:
77 case LYS_LEAF:
78 lyd_find_sibling_val(sibling, siter, NULL, 0, &match);
79 break;
80 case LYS_LIST:
81 case LYS_LEAFLIST:
82 lyd_find_sibling_next2(sibling, siter, NULL, 0, &match);
83 break;
84 default:
85 assert(0);
86 LOGINT(NULL);
87 }
88
89 if (match) {
90 break;
91 }
92 }
93
94 if (slast) {
95 *slast = siter;
96 }
97 return match;
98}
99
Michal Vaskocde73ac2019-11-14 16:10:27 +0100100/**
Michal Vasko8104fd42020-07-13 11:09:51 +0200101 * @brief Add new changes into validation diff. They are always merged.
102 *
103 * @param[in] node Node/subtree to add.
104 * @param[in] op Operation of the change.
105 * @param[in,out] diff Validation diff.
106 * @return LY_ERR value.
107 */
108static LY_ERR
109lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
110{
111 LY_ERR ret = LY_SUCCESS;
112 struct lyd_node *new_diff = NULL;
113
114 assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
115
116 /* create new diff tree */
117 LY_CHECK_RET(lyd_diff_add(node, op, NULL, NULL, NULL, NULL, NULL, &new_diff));
118
119 /* merge into existing diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200120 ret = lyd_diff_merge_all(new_diff, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +0200121
122 lyd_free_tree(new_diff);
123 return ret;
124}
125
126/**
Michal Vaskocde73ac2019-11-14 16:10:27 +0100127 * @brief Evaluate a single "when" condition.
128 *
Michal Vaskob1b5c262020-03-05 14:29:47 +0100129 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100130 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100131 * @param[in] when When to evaluate.
Michal Vasko8104fd42020-07-13 11:09:51 +0200132 * @param[in,out] diff Validation diff.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100133 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
134 */
135static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200136lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when, struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100137{
Michal Vasko8104fd42020-07-13 11:09:51 +0200138 LY_ERR ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100139 const struct lyd_node *ctx_node;
140 struct lyxp_set xp_set;
141
142 memset(&xp_set, 0, sizeof xp_set);
143
144 if (when->context == node->schema) {
145 ctx_node = node;
146 } else {
147 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
148 ctx_node = (struct lyd_node *)node->parent;
149 }
150
151 /* evaluate when */
Michal Vasko52927e22020-03-16 17:26:14 +0100152 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 +0100153 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100154 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
155
156 /* return error or LY_EINCOMPLETE for dependant unresolved when */
157 LY_CHECK_RET(ret);
158
159 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +0200160 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100161 if (node->flags & LYD_WHEN_TRUE) {
162 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100163 if (LYD_DEL_IS_ROOT(*tree, node)) {
164 *tree = (*tree)->next;
165 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200166 if (diff) {
167 /* add into diff */
168 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff));
169 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100170 lyd_free_tree(node);
171 } else {
172 /* invalid data */
Michal Vaskocc048b22020-03-27 15:52:38 +0100173 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vasko8104fd42020-07-13 11:09:51 +0200174 return LY_EVALID;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100175 }
176 } else {
177 /* remember that when evaluated to true */
178 node->flags |= LYD_WHEN_TRUE;
179 }
180
181 return ret;
182}
183
184LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100185lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *meta_types,
Michal Vasko8104fd42020-07-13 11:09:51 +0200186 LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data, struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100187{
188 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200189 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100190
Michal Vaskob1b5c262020-03-05 14:29:47 +0100191 if (node_when) {
192 /* evaluate all when conditions */
193 uint32_t prev_count;
194 do {
195 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200196 i = 0;
197 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100198 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200199 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100200 const struct lysc_node *schema = node->schema;
201 int unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100202
Michal Vaskob1b5c262020-03-05 14:29:47 +0100203 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200204 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200205 LY_ARRAY_FOR(schema->when, u) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200206 ret = lyd_validate_when(tree, node, schema->when[u], diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100207 if (ret) {
208 break;
209 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100210 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100211 if (ret == LY_EINCOMPLETE) {
212 /* could not evaluate this when */
213 unres_when = 1;
214 break;
215 } else if (ret) {
216 /* error */
217 return ret;
218 }
219 schema = schema->parent;
220 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100221
Michal Vaskob1b5c262020-03-05 14:29:47 +0100222 if (unres_when) {
223 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200224 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100225 } else {
226 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200227 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100228 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100229 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100230
Michal Vaskob1b5c262020-03-05 14:29:47 +0100231 /* there must have been some when conditions resolved */
232 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100233
Michal Vaskob1b5c262020-03-05 14:29:47 +0100234 /* there could have been no cyclic when dependencies, checked during compilation */
235 assert(!node_when->count);
236 }
237
238 if (node_types && node_types->count) {
239 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200240 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100241 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200242 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100243
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200244 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100245
246 /* validate and store the value of the node */
247 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
248 parser_data, format, *tree);
249 LY_CHECK_RET(ret);
250
251 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200252 ly_set_rm_index(node_types, i, NULL);
253 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100254 }
255
Michal Vasko9f96a052020-03-10 09:41:45 +0100256 if (meta_types && meta_types->count) {
257 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200258 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100259 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200260 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100261
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200262 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100263
Michal Vasko9f96a052020-03-10 09:41:45 +0100264 /* validate and store the value of the metadata */
265 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original,
266 strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100267 LY_CHECK_RET(ret);
268
269 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200270 ly_set_rm_index(meta_types, i, NULL);
271 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100272 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100273
274 return ret;
275}
276
Michal Vaskobb844672020-07-03 11:06:12 +0200277/**
278 * @brief Validate instance duplication.
279 *
280 * @param[in] first First sibling to search in.
281 * @param[in] node Data node instance to check.
282 * @return LY_ERR value.
283 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100284static LY_ERR
285lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100286{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100287 struct lyd_node **match_p;
288 int fail = 0;
289
290 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
291 /* duplicate instances allowed */
292 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100293 }
294
Michal Vaskob1b5c262020-03-05 14:29:47 +0100295 /* find exactly the same next instance using hashes if possible */
296 if (node->parent && node->parent->children_ht) {
297 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
298 fail = 1;
299 }
300 } else {
301 for (; first; first = first->next) {
302 if (first == node) {
303 continue;
304 }
305
306 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
307 if (first->schema == node->schema) {
308 fail = 1;
309 break;
310 }
311 } else if (!lyd_compare(first, node, 0)) {
312 fail = 1;
313 break;
314 }
315 }
316 }
317
318 if (fail) {
319 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
320 return LY_EVALID;
321 }
322 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100323}
324
Michal Vaskobb844672020-07-03 11:06:12 +0200325/**
326 * @brief Validate multiple case data existence with possible autodelete.
327 *
328 * @param[in,out] first First sibling to search in, is updated if needed.
329 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200330 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200331 * @return LY_ERR value.
332 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100333static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200334lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100335{
336 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
337 struct lyd_node *match, *to_del;
338 int found;
339
340 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
341 found = 0;
342 iter = NULL;
343 match = NULL;
344 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
345 if (match->flags & LYD_NEW) {
346 /* a new case data found, nothing more to look for */
347 found = 2;
348 break;
349 } else {
350 /* and old case data found */
351 if (found == 0) {
352 found = 1;
353 }
354 }
355 }
356
357 if (found == 1) {
358 /* there should not be 2 old cases */
359 if (old_case) {
360 /* old data from 2 cases */
361 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
362 return LY_EVALID;
363 }
364
365 /* remember an old existing case */
366 old_case = scase;
367 } else if (found == 2) {
368 if (new_case) {
369 /* new data from 2 cases */
370 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
371 return LY_EVALID;
372 }
373
374 /* remember a new existing case */
375 new_case = scase;
376 }
377 }
378
379 if (old_case && new_case) {
380 /* auto-delete old case */
381 iter = NULL;
382 match = NULL;
383 to_del = NULL;
384 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
385 if (LYD_DEL_IS_ROOT(*first, to_del)) {
386 *first = (*first)->next;
387 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200388 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100389 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200390 if (diff) {
391 /* add into diff */
392 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
393 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100394 to_del = match;
395 }
396 if (LYD_DEL_IS_ROOT(*first, to_del)) {
397 *first = (*first)->next;
398 }
399 lyd_free_tree(to_del);
400 }
401
402 return LY_SUCCESS;
403}
404
Michal Vaskobb844672020-07-03 11:06:12 +0200405/**
406 * @brief Check whether a schema node can have some default values (true for NP containers as well).
407 *
408 * @param[in] schema Schema node to check.
409 * @return non-zero if yes,
410 * @return 0 otherwise.
411 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100412static int
413lyd_val_has_default(const struct lysc_node *schema)
414{
415 switch (schema->nodetype) {
416 case LYS_LEAF:
417 if (((struct lysc_node_leaf *)schema)->dflt) {
418 return 1;
419 }
420 break;
421 case LYS_LEAFLIST:
422 if (((struct lysc_node_leaflist *)schema)->dflts) {
423 return 1;
424 }
425 break;
426 case LYS_CONTAINER:
427 if (!(schema->flags & LYS_PRESENCE)) {
428 return 1;
429 }
430 break;
431 default:
432 break;
433 }
434
435 return 0;
436}
437
Michal Vaskobb844672020-07-03 11:06:12 +0200438/**
439 * @brief Autodelete old instances to prevent validation errors.
440 *
441 * @param[in,out] first First sibling to search in, is updated if needed.
442 * @param[in] node Data node instance to check.
443 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200444 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200445 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100446static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200447lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100448{
Michal Vasko8104fd42020-07-13 11:09:51 +0200449 struct lyd_node *match, *next, *iter;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100450
451 if (lyd_val_has_default(node->schema)) {
452 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
453 if (node->schema->nodetype == LYS_LEAFLIST) {
454 lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match);
455 } else {
456 lyd_find_sibling_val(*first, node->schema, NULL, 0, &match);
457 }
458
459 while (match) {
460 next = match->next;
461 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
462 /* default instance found, remove it */
463 if (LYD_DEL_IS_ROOT(*first, match)) {
464 *first = (*first)->next;
465 }
466 if (match == *next_p) {
467 *next_p = (*next_p)->next;
468 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200469 if (diff) {
470 /* add into diff */
471 if ((match->schema->nodetype == LYS_CONTAINER) && !(match->schema->flags & LYS_PRESENCE)) {
472 /* we do not want to track NP container changes, but remember any removed children */
473 LY_LIST_FOR(LYD_CHILD(match), iter) {
474 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
475 }
476 } else {
477 lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff);
478 }
479 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100480 lyd_free_tree(match);
481
482 /* remove only a single container/leaf default instance, if there are more, it is an error */
483 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
484 break;
485 }
486 }
487
488 lyd_find_sibling_next2(next, node->schema, NULL, 0, &match);
489 }
490 }
491}
492
493LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200494lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
495 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100496{
497 struct lyd_node *next, *node;
498 const struct lysc_node *snode = NULL;
499
500 assert(first && (sparent || mod));
501
502 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
503 /* check case duplicites */
504 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200505 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100506 }
507 }
508
509 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100510 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100511 /* all top-level data from this module checked */
512 break;
513 }
514
515 if (!(node->flags & LYD_NEW)) {
516 /* check only new nodes */
517 continue;
518 }
519
520 /* remove old default(s) if it exists */
Michal Vasko8104fd42020-07-13 11:09:51 +0200521 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100522
523 /* then check new node instance duplicities */
524 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
525
526 /* this node is valid */
527 node->flags &= ~LYD_NEW;
528 }
529
530 return LY_SUCCESS;
531}
532
Michal Vaskobb844672020-07-03 11:06:12 +0200533/**
534 * @brief Validate mandatory node existence.
535 *
536 * @param[in] first First sibling to search in.
537 * @param[in] snode Schema node to validate.
538 * @return LY_ERR value.
539 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100540static LY_ERR
541lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100542{
Michal Vaskoa3881362020-01-21 15:57:35 +0100543 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100544 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100545 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100546 return LY_SUCCESS;
547 }
548 } else {
549 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100550
Michal Vaskob1b5c262020-03-05 14:29:47 +0100551 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100552 /* data instance found */
553 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100554 }
555 }
556
557 /* node instance not found */
558 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
559 return LY_EVALID;
560}
561
Michal Vaskobb844672020-07-03 11:06:12 +0200562/**
563 * @brief Validate min/max-elements constraints, if any.
564 *
565 * @param[in] first First sibling to search in.
566 * @param[in] snode Schema node to validate.
567 * @param[in] min Minimum number of elements, 0 for no restriction.
568 * @param[in] max Max number of elements, 0 for no restriction.
569 * @return LY_ERR value.
570 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100571static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100572lyd_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 +0100573{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100574 uint32_t count = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100575 const struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100576
Michal Vasko9b368d32020-02-14 13:53:31 +0100577 assert(min || max);
578
Michal Vaskob1b5c262020-03-05 14:29:47 +0100579 LY_LIST_FOR(first, iter) {
Michal Vaskoacd83e72020-02-04 14:12:01 +0100580 if (iter->schema == snode) {
581 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100582
583 if (min && (count == min)) {
584 /* satisfied */
585 min = 0;
586 if (!max) {
587 /* nothing more to check */
588 break;
589 }
590 }
591 if (max && (count > max)) {
592 /* not satisifed */
593 break;
594 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100595 }
596 }
597
Michal Vasko9b368d32020-02-14 13:53:31 +0100598 if (min) {
599 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100600 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
601 return LY_EVALID;
602 } else if (max && (count > max)) {
603 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
604 return LY_EVALID;
605 }
606
Michal Vaskoa3881362020-01-21 15:57:35 +0100607 return LY_SUCCESS;
608}
609
Michal Vaskobb844672020-07-03 11:06:12 +0200610/**
611 * @brief Find node referenced by a list unique statement.
612 *
613 * @param[in] uniq_leaf Unique leaf to find.
614 * @param[in] list List instance to use for the search.
615 * @return Found leaf,
616 * @return NULL if no leaf found.
617 */
Michal Vasko14654712020-02-06 08:35:21 +0100618static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200619lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100620{
Michal Vasko9b368d32020-02-14 13:53:31 +0100621 struct lyd_node *node;
622 const struct lysc_node *iter;
623 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100624
Michal Vasko9b368d32020-02-14 13:53:31 +0100625 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200626 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
627 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100628 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100629
Michal Vaskobb844672020-07-03 11:06:12 +0200630 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100631 while (node && depth) {
632 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200633 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
634 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100635 }
636
637 /* find iter instance in children */
638 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200639 lyd_find_sibling_val(lyd_node_children(node, 0), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100640 --depth;
641 }
642
Michal Vasko14654712020-02-06 08:35:21 +0100643 return node;
644}
645
Michal Vaskobb844672020-07-03 11:06:12 +0200646/**
647 * @brief Callback for comparing 2 list unique leaf values.
648 *
649 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100650 */
651static int
652lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
653{
654 struct ly_ctx *ctx;
655 struct lysc_node_list *slist;
656 struct lyd_node *diter, *first, *second;
657 struct lyd_value *val1, *val2;
658 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200659 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100660
661 assert(val1_p && val2_p);
662
663 first = *((struct lyd_node **)val1_p);
664 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200665 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100666
667 assert(first && (first->schema->nodetype == LYS_LIST));
668 assert(second && (second->schema == first->schema));
669
670 ctx = first->schema->module->ctx;
671
672 slist = (struct lysc_node_list *)first->schema;
673
674 /* compare unique leaves */
675 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200676 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200677 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100678 goto uniquecheck;
679 }
680 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200681 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100682uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200683 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100684 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200685 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100686 if (diter) {
687 val1 = &((struct lyd_node_term *)diter)->value;
688 } else {
689 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200690 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100691 }
692
693 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200694 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100695 if (diter) {
696 val2 = &((struct lyd_node_term *)diter)->value;
697 } else {
698 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200699 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100700 }
701
702 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
703 /* values differ or either one is not set */
704 break;
705 }
706 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200707 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100708 /* all unique leafs are the same in this set, create this nice error */
709 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
710 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
711
712 /* use buffer to rebuild the unique string */
713 uniq_str = malloc(1024);
714 uniq_str[0] = '\0';
715 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200716 LY_ARRAY_FOR(slist->uniques[u], v) {
717 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100718 strcpy(ptr, " ");
719 ++ptr;
720 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200721 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 +0100722 ptr, 1024 - (ptr - uniq_str));
723 if (!ptr) {
724 /* path will be incomplete, whatever */
725 break;
726 }
727
728 ptr += strlen(ptr);
729 }
730 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
731
732 free(path1);
733 free(path2);
734 free(uniq_str);
735 return 1;
736 }
737
738 if (action > 0) {
739 /* done */
740 return 0;
741 }
742 }
743
744 return 0;
745}
746
Michal Vaskobb844672020-07-03 11:06:12 +0200747/**
748 * @brief Validate list unique leaves.
749 *
750 * @param[in] first First sibling to search in.
751 * @param[in] snode Schema node to validate.
752 * @param[in] uniques List unique arrays to validate.
753 * @return LY_ERR value.
754 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100755static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200756lyd_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 +0100757{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100758 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100759 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200760 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100761 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200762 uint32_t hash, i, size = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100763 int dynamic;
764 const char *str;
765 struct hash_table **uniqtables = NULL;
766 struct lyd_value *val;
767 struct ly_ctx *ctx = snode->module->ctx;
768
769 assert(uniques);
770
771 /* get all list instances */
Michal Vasko9b368d32020-02-14 13:53:31 +0100772 set = ly_set_new();
773 LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100774 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100775 if (diter->schema == snode) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100776 ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100777 }
778 }
Michal Vasko14654712020-02-06 08:35:21 +0100779
780 if (set->count == 2) {
781 /* simple comparison */
782 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
783 /* instance duplication */
784 ret = LY_EVALID;
785 goto cleanup;
786 }
787 } else if (set->count > 2) {
788 /* use hashes for comparison */
789 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200790 for (i = 31; i > 0; i--) {
791 size = set->count << i;
792 size = size >> i;
793 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100794 break;
795 }
796 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200797 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
798 i = 32 - i;
799 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100800
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200801 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100802 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200803 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200804 for (v = 0; v < x; v++) {
805 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
806 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100807 }
808
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200809 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100810 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200811 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100812 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200813 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200814 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100815 if (diter) {
816 val = &((struct lyd_node_term *)diter)->value;
817 } else {
818 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200819 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100820 }
821 if (!val) {
822 /* unique item not present nor has default value */
823 break;
824 }
825
826 /* get canonical string value */
827 str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic);
828 hash = dict_hash_multi(hash, str, strlen(str));
829 if (dynamic) {
830 free((char *)str);
831 }
832 }
833 if (!val) {
834 /* skip this list instance since its unique set is incomplete */
835 continue;
836 }
837
838 /* finish the hash value */
839 hash = dict_hash_multi(hash, NULL, 0);
840
841 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200842 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100843 if (ret == LY_EEXIST) {
844 /* instance duplication */
845 ret = LY_EVALID;
846 }
847 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
848 }
849 }
850 }
851
852cleanup:
853 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200854 for (v = 0; v < x; v++) {
855 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100856 /* failed when allocating uniquetables[j], following j are not allocated */
857 break;
858 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200859 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100860 }
861 free(uniqtables);
862
863 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100864}
865
Michal Vaskobb844672020-07-03 11:06:12 +0200866/**
867 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
868 *
869 * @param[in] first First sibling to search in.
870 * @param[in] sparent Schema parent of the nodes to check.
871 * @param[in] mod Module of the nodes to check.
872 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
873 * @param[in] op Operation being validated, if any.
874 * @return LY_ERR value.
875 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100876static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100877lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci7931b192020-06-25 17:05:03 +0200878 const struct lysc_module *mod, int val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100879{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100880 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100881 struct lysc_node_list *slist;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200882 int getnext_opts;
883
Radek Krejci7931b192020-06-25 17:05:03 +0200884 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100885
Michal Vaskoa3881362020-01-21 15:57:35 +0100886 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200887 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200888 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100889 continue;
890 }
891
Michal Vaskoa3881362020-01-21 15:57:35 +0100892 /* check min-elements and max-elements */
893 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
894 slist = (struct lysc_node_list *)snode;
895 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100896 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100897 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100898
899 /* check generic mandatory existence */
900 } else if (snode->flags & LYS_MAND_TRUE) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100901 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100902 }
903
904 /* check unique */
905 if (snode->nodetype == LYS_LIST) {
906 slist = (struct lysc_node_list *)snode;
907 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200908 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100909 }
910 }
911
Michal Vaskoacd83e72020-02-04 14:12:01 +0100912 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
913 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200914 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100915 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100916 }
917
Michal Vaskoacd83e72020-02-04 14:12:01 +0100918 return LY_SUCCESS;
919}
920
Michal Vaskobb844672020-07-03 11:06:12 +0200921/**
922 * @brief Validate obsolete nodes, only warnings are printed.
923 *
924 * @param[in] node Node to check.
925 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100926static void
927lyd_validate_obsolete(const struct lyd_node *node)
928{
929 const struct lysc_node *snode;
930
931 snode = node->schema;
932 do {
933 if (snode->flags & LYS_STATUS_OBSLT) {
934 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
935 break;
936 }
937
938 snode = snode->parent;
939 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
940}
941
Michal Vaskobb844672020-07-03 11:06:12 +0200942/**
943 * @brief Validate must conditions of a data node.
944 *
945 * @param[in] node Node to validate.
946 * @param[in] op Operation being validated, if any.
947 * @return LY_ERR value.
948 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100949static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200950lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100951{
952 struct lyxp_set xp_set;
953 struct lysc_must *musts;
954 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200955 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100956
957 switch (node->schema->nodetype) {
958 case LYS_CONTAINER:
959 musts = ((struct lysc_node_container *)node->schema)->musts;
960 break;
961 case LYS_LEAF:
962 musts = ((struct lysc_node_leaf *)node->schema)->musts;
963 break;
964 case LYS_LEAFLIST:
965 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
966 break;
967 case LYS_LIST:
968 musts = ((struct lysc_node_list *)node->schema)->musts;
969 break;
970 case LYS_ANYXML:
971 case LYS_ANYDATA:
972 musts = ((struct lysc_node_anydata *)node->schema)->musts;
973 break;
974 case LYS_NOTIF:
975 musts = ((struct lysc_notif *)node->schema)->musts;
976 break;
977 case LYS_RPC:
978 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200979 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200980 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200981 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200982 musts = ((struct lysc_action *)node->schema)->output.musts;
983 } else {
984 LOGINT(LYD_NODE_CTX(node));
985 return LY_EINT;
986 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100987 break;
988 default:
989 LOGINT(LYD_NODE_CTX(node));
990 return LY_EINT;
991 }
992
993 if (!musts) {
994 /* no must to evaluate */
995 return LY_SUCCESS;
996 }
997
998 /* find first top-level node */
999 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent);
1000 while (tree->prev->next) {
1001 tree = tree->prev;
1002 }
1003
1004 LY_ARRAY_FOR(musts, u) {
1005 memset(&xp_set, 0, sizeof xp_set);
1006
1007 /* evaluate must */
1008 LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA));
1009
1010 /* check the result */
1011 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02001012 if (!xp_set.val.bln) {
Michal Vaskocc048b22020-03-27 15:52:38 +01001013 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
1014 return LY_EVALID;
1015 }
1016 }
1017
1018 return LY_SUCCESS;
1019}
1020
Michal Vaskob1b5c262020-03-05 14:29:47 +01001021LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001022lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, int val_opts,
1023 LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +01001024{
Radek Krejci7f769d72020-07-11 23:13:56 +02001025 struct lyd_node *next = NULL, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001026 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +01001027
Michal Vasko14654712020-02-06 08:35:21 +01001028 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001029 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001030 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001031 /* all top-level data from this module checked */
1032 break;
Michal Vaskof03ed032020-03-04 13:31:44 +01001033 }
1034
Michal Vaskoa8c61722020-03-27 16:59:32 +01001035 /* opaque data */
1036 if (!node->schema) {
1037 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
1038 ((struct lyd_node_opaq *)node)->name);
1039 return LY_EVALID;
1040 }
1041
Michal Vaskocb7526d2020-03-30 15:08:26 +02001042 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +02001043 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001044 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
1045 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001046 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001047 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
1048 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001049 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001050 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +01001051 return LY_EVALID;
1052 }
1053
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001054 /* obsolete data */
1055 lyd_validate_obsolete(node);
1056
Michal Vaskoc193ce92020-03-06 11:04:48 +01001057 /* node's schema if-features */
1058 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskoa8c61722020-03-27 16:59:32 +01001059 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +01001060 return LY_EVALID;
1061 }
1062
Michal Vaskocc048b22020-03-27 15:52:38 +01001063 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001064 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001065
Michal Vaskoa8c61722020-03-27 16:59:32 +01001066 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +01001067 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001068
Michal Vasko14654712020-02-06 08:35:21 +01001069 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +02001070 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001071
Michal Vaskob1b5c262020-03-05 14:29:47 +01001072 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001073 /* validate all children recursively */
Radek Krejci7931b192020-06-25 17:05:03 +02001074 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 +01001075
Michal Vaskob1b5c262020-03-05 14:29:47 +01001076 /* set default for containers */
1077 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001078 LY_LIST_FOR(lyd_node_children(node, 0), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001079 if (!(next->flags & LYD_DEFAULT)) {
1080 break;
1081 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001082 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001083 if (!next) {
1084 node->flags |= LYD_DEFAULT;
1085 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001086 }
1087 }
1088
1089 return LY_SUCCESS;
1090}
1091
1092LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001093lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vasko8104fd42020-07-13 11:09:51 +02001094 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int val_opts,
1095 struct lyd_node **diff)
Michal Vasko9b368d32020-02-14 13:53:31 +01001096{
Michal Vaskob1b5c262020-03-05 14:29:47 +01001097 LY_ERR ret;
Michal Vasko9b368d32020-02-14 13:53:31 +01001098 const struct lysc_node *iter = NULL;
1099 struct lyd_node *node;
1100 struct lyd_value **dflts;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001101 LY_ARRAY_COUNT_TYPE u;
Michal Vasko9b368d32020-02-14 13:53:31 +01001102
Michal Vaskob1b5c262020-03-05 14:29:47 +01001103 assert(first && (parent || sparent || mod) && node_types && node_when);
Michal Vasko9b368d32020-02-14 13:53:31 +01001104
Michal Vaskob1b5c262020-03-05 14:29:47 +01001105 if (!sparent && parent) {
1106 sparent = parent->schema;
1107 }
1108
1109 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
Radek Krejci7931b192020-06-25 17:05:03 +02001110 if ((val_opts & LYD_VALIDATE_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001111 continue;
1112 }
1113
Michal Vasko9b368d32020-02-14 13:53:31 +01001114 switch (iter->nodetype) {
1115 case LYS_CHOICE:
Michal Vaskof03ed032020-03-04 13:31:44 +01001116 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001117 /* create default case data */
1118 LY_CHECK_RET(lyd_validate_defaults_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
Michal Vasko8104fd42020-07-13 11:09:51 +02001119 NULL, node_types, node_when, val_opts, diff));
Michal Vasko9b368d32020-02-14 13:53:31 +01001120 }
1121 break;
1122 case LYS_CONTAINER:
1123 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001124 /* create default NP container */
Michal Vasko9b368d32020-02-14 13:53:31 +01001125 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001126 node->flags = LYD_DEFAULT;
1127 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001128
1129 if (iter->when) {
1130 /* remember to resolve when */
1131 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1132 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001133
1134 /* create any default children */
Michal Vasko8104fd42020-07-13 11:09:51 +02001135 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1136 val_opts, diff));
Michal Vasko9b368d32020-02-14 13:53:31 +01001137 }
1138 break;
1139 case LYS_LEAF:
1140 if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1141 /* create default leaf */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001142 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1143 if (ret == LY_EINCOMPLETE) {
1144 /* remember to resolve type */
1145 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1146 } else if (ret) {
1147 return ret;
1148 }
1149 node->flags = LYD_DEFAULT;
1150 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001151
1152 if (iter->when) {
1153 /* remember to resolve when */
1154 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1155 }
Michal Vasko8104fd42020-07-13 11:09:51 +02001156 if (diff) {
1157 /* add into diff */
1158 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1159 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001160 }
1161 break;
1162 case LYS_LEAFLIST:
1163 if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_next2(*first, iter, NULL, 0, NULL)) {
1164 /* create all default leaf-lists */
1165 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001166 LY_ARRAY_FOR(dflts, u) {
1167 ret = lyd_create_term2(iter, dflts[u], &node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001168 if (ret == LY_EINCOMPLETE) {
1169 /* remember to resolve type */
1170 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1171 } else if (ret) {
1172 return ret;
1173 }
1174 node->flags = LYD_DEFAULT;
1175 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001176
1177 if (iter->when) {
1178 /* remember to resolve when */
1179 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1180 }
Michal Vasko8104fd42020-07-13 11:09:51 +02001181 if (diff) {
1182 /* add into diff */
1183 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1184 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001185 }
1186 }
1187 break;
1188 default:
1189 /* without defaults */
1190 break;
1191 }
1192 }
1193
1194 return LY_SUCCESS;
1195}
1196
Radek Krejci7931b192020-06-25 17:05:03 +02001197/**
Michal Vaskobb844672020-07-03 11:06:12 +02001198 * @brief Validate the whole data subtree.
1199 *
1200 * @param[in] root Subtree root.
1201 * @param[in,out] type_check Set for unres node types.
1202 * @param[in,out] type_meta_check Set for unres metadata types.
1203 * @param[in,out] when_check Set for nodes with when conditions.
1204 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001205 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001206 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001207 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001208static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001209lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Michal Vasko8104fd42020-07-13 11:09:51 +02001210 struct ly_set *when_check, int val_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001211{
1212 const struct lyd_meta *meta;
1213 struct lyd_node *next, *node;
1214
1215 LYD_TREE_DFS_BEGIN(root, next, node) {
1216 /* skip added default nodes */
1217 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1218 LY_LIST_FOR(node->meta, meta) {
1219 /* metadata type resolution */
1220 ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST);
1221 }
1222
1223 if (node->schema->nodetype & LYD_NODE_TERM) {
1224 /* node type resolution */
1225 ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST);
1226 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1227 /* new node validation, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001228 LY_CHECK_RET(lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL, diff));
Michal Vaskofea12c62020-03-30 11:00:15 +02001229
1230 /* add nested defaults */
1231 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
Michal Vasko8104fd42020-07-13 11:09:51 +02001232 when_check, val_opts, diff));
Michal Vaskofea12c62020-03-30 11:00:15 +02001233 }
1234
1235 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1236 /* when evaluation */
1237 ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST);
1238 }
1239 }
1240
1241 LYD_TREE_DFS_END(root, next, node);
1242 }
1243
1244 return LY_SUCCESS;
1245}
1246
Michal Vaskobb844672020-07-03 11:06:12 +02001247/**
1248 * @brief Validate data tree.
1249 *
1250 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1251 * @param[in] modules Array of modules to validate, NULL for all modules.
1252 * @param[in] mod_count Count of @p modules.
1253 * @param[in] ly_ctx libyang context.
1254 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001255 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001256 * @return LY_ERR value.
1257 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001258static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001259lyd_validate(struct lyd_node **tree, const struct lys_module *module, const struct ly_ctx *ctx, int val_opts,
Michal Vasko8104fd42020-07-13 11:09:51 +02001260 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001261{
1262 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001263 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001264 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001265 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001266 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001267
Michal Vasko26e80012020-07-08 10:55:46 +02001268 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001269 if (diff) {
1270 *diff = NULL;
1271 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001272
Michal Vaskob1b5c262020-03-05 14:29:47 +01001273 next = *tree;
1274 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001275 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001276 mod = lyd_data_next_module(&next, &first);
1277 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001278 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001279 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001280 if (!mod) {
1281 break;
1282 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001283 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001284 /* make sure first2 changes are carried to tree */
1285 first2 = tree;
1286 } else {
1287 first2 = &first;
1288 }
1289
1290 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001291 ret = lyd_validate_new(first2, NULL, mod, diff);
1292 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001293
1294 /* add all top-level defaults for this module */
Michal Vasko8104fd42020-07-13 11:09:51 +02001295 ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &type_check, &when_check, val_opts, diff);
1296 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001297
1298 /* process nested nodes */
1299 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001300 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1301 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001302 }
1303
1304 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vasko8104fd42020-07-13 11:09:51 +02001305 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, LYD_JSON, lydjson_resolve_prefix,
1306 NULL, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001307 LY_CHECK_GOTO(ret, cleanup);
1308
1309 /* perform final validation that assumes the data tree is final */
Michal Vasko8104fd42020-07-13 11:09:51 +02001310 ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0);
1311 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001312 }
1313
Michal Vaskof03ed032020-03-04 13:31:44 +01001314cleanup:
1315 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001316 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001317 ly_set_erase(&when_check, NULL);
1318 return ret;
1319}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001320
1321API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001322lyd_validate_all(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001323{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001324 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001325}
1326
1327API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001328lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, int val_opts, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001329{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001330 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001331}
Michal Vaskofea12c62020-03-30 11:00:15 +02001332
Michal Vaskobb844672020-07-03 11:06:12 +02001333/**
1334 * @brief Find nodes for merging an operation into data tree for validation.
1335 *
1336 * @param[in] op_tree Full operation data tree.
1337 * @param[in] op_node Operation node itself.
1338 * @param[in] tree Data tree to be merged into.
1339 * @param[out] op_subtree Operation subtree to merge.
1340 * @param[out] tree_sibling Data tree sibling to merge next to.
1341 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001342static void
Michal Vaskobb844672020-07-03 11:06:12 +02001343lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
1344 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001345{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001346 const struct lyd_node *tree_iter, *op_iter;
1347 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001348 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001349
Michal Vaskocb7526d2020-03-30 15:08:26 +02001350 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001351 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001352 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001353 ++op_depth;
1354 }
1355
1356 /* find where to merge op */
1357 tree_iter = tree;
1358 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001359 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001360 while (cur_depth) {
1361 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001362 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001363 for (i = 0; i < cur_depth; ++i) {
1364 op_iter = (struct lyd_node *)op_iter->parent;
1365 }
1366
1367 /* find op iter in tree */
1368 lyd_find_sibling_first(tree_iter, op_iter, &match);
1369 if (!match) {
1370 break;
1371 }
1372
1373 /* move tree_iter */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001374 tree_iter = lyd_node_children(match, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001375
1376 /* move depth */
1377 --cur_depth;
1378 }
1379
Michal Vaskobb844672020-07-03 11:06:12 +02001380 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001381 *tree_sibling = (struct lyd_node *)tree_iter;
1382}
1383
1384API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001385lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, LYD_VALIDATE_OP op, struct lyd_node **diff)
Michal Vaskocb7526d2020-03-30 15:08:26 +02001386{
1387 LY_ERR ret;
Radek Krejci7931b192020-06-25 17:05:03 +02001388 struct lyd_node *tree_sibling, *op_subtree, *op_next, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001389 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1390
1391 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci7931b192020-06-25 17:05:03 +02001392 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001393 if (diff) {
1394 *diff = NULL;
1395 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001396
1397 /* find the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001398 LYD_TREE_DFS_BEGIN(op_tree, op_next, op_node) {
1399 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 +02001400 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001401 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001402 break;
1403 }
Radek Krejci7931b192020-06-25 17:05:03 +02001404 LYD_TREE_DFS_END(op_tree, op_next, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001405 }
Radek Krejci7931b192020-06-25 17:05:03 +02001406 if (op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) {
1407 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001408 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
1409 return LY_EINVAL;
1410 }
1411 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001412 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001413 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
1414 return LY_EINVAL;
1415 }
1416 }
1417
1418 /* move op_tree to top-level node */
1419 while (op_tree->parent) {
1420 op_tree = (struct lyd_node *)op_tree->parent;
1421 }
1422
1423 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001424 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1425 op_parent = (struct lyd_node *)op_subtree->parent;
1426 lyd_unlink_tree(op_subtree);
1427 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001428 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001429 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001430 }
1431
1432 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001433 LY_CHECK_GOTO(ret = lyd_validate_subtree(op_node, &type_check, &type_meta_check, &when_check, 0, diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001434
1435 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1436 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
Michal Vasko8104fd42020-07-13 11:09:51 +02001437 LYD_JSON, lydjson_resolve_prefix, NULL, diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001438
1439 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001440 lyd_validate_obsolete(op_node);
1441 if (lysc_node_is_disabled(op_node->schema, 1)) {
1442 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 +02001443 ret = LY_EVALID;
1444 goto cleanup;
1445 }
Radek Krejci7931b192020-06-25 17:05:03 +02001446 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001447
1448 /* final validation of all the descendants */
Radek Krejci7931b192020-06-25 17:05:03 +02001449 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 +02001450
1451cleanup:
1452 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001453 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001454 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001455 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001456 }
1457
1458 ly_set_erase(&type_check, NULL);
1459 ly_set_erase(&type_meta_check, NULL);
1460 ly_set_erase(&when_check, NULL);
1461 return ret;
1462}