blob: 1c4bc451d5ea5c66178b33fc1763515663194afd [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
Michal Vasko4c583e82020-07-17 12:16:14 +020063 if (last && last->next && (last->next->schema == siter)) {
64 /* return next data instance */
65 return last->next;
Michal Vaskof03ed032020-03-04 13:31:44 +010066 }
67
68 /* find next schema node data instance */
69 while ((siter = lys_getnext(siter, parent, module, 0))) {
Michal Vasko4c583e82020-07-17 12:16:14 +020070 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
Michal Vaskof03ed032020-03-04 13:31:44 +010071 break;
72 }
73 }
74
75 if (slast) {
76 *slast = siter;
77 }
78 return match;
79}
80
Michal Vaskocde73ac2019-11-14 16:10:27 +010081/**
Michal Vasko8104fd42020-07-13 11:09:51 +020082 * @brief Add new changes into validation diff. They are always merged.
83 *
84 * @param[in] node Node/subtree to add.
85 * @param[in] op Operation of the change.
86 * @param[in,out] diff Validation diff.
87 * @return LY_ERR value.
88 */
89static LY_ERR
90lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
91{
92 LY_ERR ret = LY_SUCCESS;
93 struct lyd_node *new_diff = NULL;
94
95 assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
96
97 /* create new diff tree */
98 LY_CHECK_RET(lyd_diff_add(node, op, NULL, NULL, NULL, NULL, NULL, &new_diff));
99
100 /* merge into existing diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200101 ret = lyd_diff_merge_all(new_diff, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +0200102
103 lyd_free_tree(new_diff);
104 return ret;
105}
106
107/**
Michal Vaskocde73ac2019-11-14 16:10:27 +0100108 * @brief Evaluate a single "when" condition.
109 *
Michal Vaskob1b5c262020-03-05 14:29:47 +0100110 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100111 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100112 * @param[in] when When to evaluate.
Michal Vasko8104fd42020-07-13 11:09:51 +0200113 * @param[in,out] diff Validation diff.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100114 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
115 */
116static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200117lyd_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 +0100118{
Michal Vasko8104fd42020-07-13 11:09:51 +0200119 LY_ERR ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100120 const struct lyd_node *ctx_node;
121 struct lyxp_set xp_set;
122
123 memset(&xp_set, 0, sizeof xp_set);
124
125 if (when->context == node->schema) {
126 ctx_node = node;
127 } else {
128 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
129 ctx_node = (struct lyd_node *)node->parent;
130 }
131
132 /* evaluate when */
Michal Vasko52927e22020-03-16 17:26:14 +0100133 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 +0100134 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100135 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
136
137 /* return error or LY_EINCOMPLETE for dependant unresolved when */
138 LY_CHECK_RET(ret);
139
140 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +0200141 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100142 if (node->flags & LYD_WHEN_TRUE) {
143 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100144 if (LYD_DEL_IS_ROOT(*tree, node)) {
145 *tree = (*tree)->next;
146 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200147 if (diff) {
148 /* add into diff */
149 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff));
150 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100151 lyd_free_tree(node);
152 } else {
153 /* invalid data */
Michal Vaskocc048b22020-03-27 15:52:38 +0100154 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vasko8104fd42020-07-13 11:09:51 +0200155 return LY_EVALID;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100156 }
157 } else {
158 /* remember that when evaluated to true */
159 node->flags |= LYD_WHEN_TRUE;
160 }
161
162 return ret;
163}
164
165LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100166lyd_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 +0200167 LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data, struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100168{
169 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200170 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100171
Michal Vaskob1b5c262020-03-05 14:29:47 +0100172 if (node_when) {
173 /* evaluate all when conditions */
174 uint32_t prev_count;
175 do {
176 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200177 i = 0;
178 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100179 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200180 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100181 const struct lysc_node *schema = node->schema;
182 int unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100183
Michal Vaskob1b5c262020-03-05 14:29:47 +0100184 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200185 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200186 LY_ARRAY_FOR(schema->when, u) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200187 ret = lyd_validate_when(tree, node, schema->when[u], diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100188 if (ret) {
189 break;
190 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100191 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100192 if (ret == LY_EINCOMPLETE) {
193 /* could not evaluate this when */
194 unres_when = 1;
195 break;
196 } else if (ret) {
197 /* error */
198 return ret;
199 }
200 schema = schema->parent;
201 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100202
Michal Vaskob1b5c262020-03-05 14:29:47 +0100203 if (unres_when) {
204 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200205 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100206 } else {
207 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200208 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100209 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100210 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100211
Michal Vaskob1b5c262020-03-05 14:29:47 +0100212 /* there must have been some when conditions resolved */
213 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100214
Michal Vaskob1b5c262020-03-05 14:29:47 +0100215 /* there could have been no cyclic when dependencies, checked during compilation */
216 assert(!node_when->count);
217 }
218
219 if (node_types && node_types->count) {
220 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200221 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100222 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200223 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100224
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200225 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100226
227 /* validate and store the value of the node */
228 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
229 parser_data, format, *tree);
230 LY_CHECK_RET(ret);
231
232 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200233 ly_set_rm_index(node_types, i, NULL);
234 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100235 }
236
Michal Vasko9f96a052020-03-10 09:41:45 +0100237 if (meta_types && meta_types->count) {
238 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200239 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100240 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200241 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100242
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200243 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100244
Michal Vasko9f96a052020-03-10 09:41:45 +0100245 /* validate and store the value of the metadata */
246 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original,
247 strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100248 LY_CHECK_RET(ret);
249
250 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200251 ly_set_rm_index(meta_types, i, NULL);
252 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100253 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100254
255 return ret;
256}
257
Michal Vaskobb844672020-07-03 11:06:12 +0200258/**
259 * @brief Validate instance duplication.
260 *
261 * @param[in] first First sibling to search in.
262 * @param[in] node Data node instance to check.
263 * @return LY_ERR value.
264 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100265static LY_ERR
266lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100267{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100268 struct lyd_node **match_p;
269 int fail = 0;
270
271 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
272 /* duplicate instances allowed */
273 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100274 }
275
Michal Vaskob1b5c262020-03-05 14:29:47 +0100276 /* find exactly the same next instance using hashes if possible */
277 if (node->parent && node->parent->children_ht) {
278 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
279 fail = 1;
280 }
281 } else {
282 for (; first; first = first->next) {
283 if (first == node) {
284 continue;
285 }
286
287 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
288 if (first->schema == node->schema) {
289 fail = 1;
290 break;
291 }
292 } else if (!lyd_compare(first, node, 0)) {
293 fail = 1;
294 break;
295 }
296 }
297 }
298
299 if (fail) {
300 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
301 return LY_EVALID;
302 }
303 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100304}
305
Michal Vaskobb844672020-07-03 11:06:12 +0200306/**
307 * @brief Validate multiple case data existence with possible autodelete.
308 *
309 * @param[in,out] first First sibling to search in, is updated if needed.
310 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200311 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200312 * @return LY_ERR value.
313 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100314static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200315lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100316{
317 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
318 struct lyd_node *match, *to_del;
319 int found;
320
321 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
322 found = 0;
323 iter = NULL;
324 match = NULL;
325 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
326 if (match->flags & LYD_NEW) {
327 /* a new case data found, nothing more to look for */
328 found = 2;
329 break;
330 } else {
331 /* and old case data found */
332 if (found == 0) {
333 found = 1;
334 }
335 }
336 }
337
338 if (found == 1) {
339 /* there should not be 2 old cases */
340 if (old_case) {
341 /* old data from 2 cases */
342 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
343 return LY_EVALID;
344 }
345
346 /* remember an old existing case */
347 old_case = scase;
348 } else if (found == 2) {
349 if (new_case) {
350 /* new data from 2 cases */
351 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
352 return LY_EVALID;
353 }
354
355 /* remember a new existing case */
356 new_case = scase;
357 }
358 }
359
360 if (old_case && new_case) {
361 /* auto-delete old case */
362 iter = NULL;
363 match = NULL;
364 to_del = NULL;
365 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
366 if (LYD_DEL_IS_ROOT(*first, to_del)) {
367 *first = (*first)->next;
368 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200369 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100370 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200371 if (diff) {
372 /* add into diff */
373 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
374 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100375 to_del = match;
376 }
377 if (LYD_DEL_IS_ROOT(*first, to_del)) {
378 *first = (*first)->next;
379 }
380 lyd_free_tree(to_del);
381 }
382
383 return LY_SUCCESS;
384}
385
Michal Vaskobb844672020-07-03 11:06:12 +0200386/**
387 * @brief Check whether a schema node can have some default values (true for NP containers as well).
388 *
389 * @param[in] schema Schema node to check.
390 * @return non-zero if yes,
391 * @return 0 otherwise.
392 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100393static int
394lyd_val_has_default(const struct lysc_node *schema)
395{
396 switch (schema->nodetype) {
397 case LYS_LEAF:
398 if (((struct lysc_node_leaf *)schema)->dflt) {
399 return 1;
400 }
401 break;
402 case LYS_LEAFLIST:
403 if (((struct lysc_node_leaflist *)schema)->dflts) {
404 return 1;
405 }
406 break;
407 case LYS_CONTAINER:
408 if (!(schema->flags & LYS_PRESENCE)) {
409 return 1;
410 }
411 break;
412 default:
413 break;
414 }
415
416 return 0;
417}
418
Michal Vaskobb844672020-07-03 11:06:12 +0200419/**
420 * @brief Autodelete old instances to prevent validation errors.
421 *
422 * @param[in,out] first First sibling to search in, is updated if needed.
423 * @param[in] node Data node instance to check.
424 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200425 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200426 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100427static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200428lyd_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 +0100429{
Michal Vasko8104fd42020-07-13 11:09:51 +0200430 struct lyd_node *match, *next, *iter;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100431
432 if (lyd_val_has_default(node->schema)) {
433 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200434 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100435 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
436 /* default instance found, remove it */
437 if (LYD_DEL_IS_ROOT(*first, match)) {
438 *first = (*first)->next;
439 }
440 if (match == *next_p) {
441 *next_p = (*next_p)->next;
442 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200443 if (diff) {
444 /* add into diff */
445 if ((match->schema->nodetype == LYS_CONTAINER) && !(match->schema->flags & LYS_PRESENCE)) {
446 /* we do not want to track NP container changes, but remember any removed children */
447 LY_LIST_FOR(LYD_CHILD(match), iter) {
448 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
449 }
450 } else {
451 lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff);
452 }
453 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100454 lyd_free_tree(match);
455
456 /* remove only a single container/leaf default instance, if there are more, it is an error */
457 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
458 break;
459 }
460 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100461 }
462 }
463}
464
465LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200466lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
467 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100468{
469 struct lyd_node *next, *node;
470 const struct lysc_node *snode = NULL;
471
472 assert(first && (sparent || mod));
473
474 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
475 /* check case duplicites */
476 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200477 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100478 }
479 }
480
481 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100482 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100483 /* all top-level data from this module checked */
484 break;
485 }
486
487 if (!(node->flags & LYD_NEW)) {
488 /* check only new nodes */
489 continue;
490 }
491
492 /* remove old default(s) if it exists */
Michal Vasko8104fd42020-07-13 11:09:51 +0200493 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100494
495 /* then check new node instance duplicities */
496 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
497
498 /* this node is valid */
499 node->flags &= ~LYD_NEW;
500 }
501
502 return LY_SUCCESS;
503}
504
Michal Vaskobb844672020-07-03 11:06:12 +0200505/**
506 * @brief Validate mandatory node existence.
507 *
508 * @param[in] first First sibling to search in.
509 * @param[in] snode Schema node to validate.
510 * @return LY_ERR value.
511 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100512static LY_ERR
513lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100514{
Michal Vaskoa3881362020-01-21 15:57:35 +0100515 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100516 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100517 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100518 return LY_SUCCESS;
519 }
520 } else {
521 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100522
Michal Vaskob1b5c262020-03-05 14:29:47 +0100523 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100524 /* data instance found */
525 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100526 }
527 }
528
529 /* node instance not found */
530 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
531 return LY_EVALID;
532}
533
Michal Vaskobb844672020-07-03 11:06:12 +0200534/**
535 * @brief Validate min/max-elements constraints, if any.
536 *
537 * @param[in] first First sibling to search in.
538 * @param[in] snode Schema node to validate.
539 * @param[in] min Minimum number of elements, 0 for no restriction.
540 * @param[in] max Max number of elements, 0 for no restriction.
541 * @return LY_ERR value.
542 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100543static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100544lyd_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 +0100545{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100546 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200547 struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100548
Michal Vasko9b368d32020-02-14 13:53:31 +0100549 assert(min || max);
550
Michal Vasko4c583e82020-07-17 12:16:14 +0200551 LYD_LIST_FOR_INST(first, snode, iter) {
552 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100553
Michal Vasko4c583e82020-07-17 12:16:14 +0200554 if (min && (count == min)) {
555 /* satisfied */
556 min = 0;
557 if (!max) {
558 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100559 break;
560 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100561 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200562 if (max && (count > max)) {
563 /* not satisifed */
564 break;
565 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100566 }
567
Michal Vasko9b368d32020-02-14 13:53:31 +0100568 if (min) {
569 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100570 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
571 return LY_EVALID;
572 } else if (max && (count > max)) {
573 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
574 return LY_EVALID;
575 }
576
Michal Vaskoa3881362020-01-21 15:57:35 +0100577 return LY_SUCCESS;
578}
579
Michal Vaskobb844672020-07-03 11:06:12 +0200580/**
581 * @brief Find node referenced by a list unique statement.
582 *
583 * @param[in] uniq_leaf Unique leaf to find.
584 * @param[in] list List instance to use for the search.
585 * @return Found leaf,
586 * @return NULL if no leaf found.
587 */
Michal Vasko14654712020-02-06 08:35:21 +0100588static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200589lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100590{
Michal Vasko9b368d32020-02-14 13:53:31 +0100591 struct lyd_node *node;
592 const struct lysc_node *iter;
593 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100594
Michal Vasko9b368d32020-02-14 13:53:31 +0100595 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200596 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
597 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100598 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100599
Michal Vaskobb844672020-07-03 11:06:12 +0200600 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100601 while (node && depth) {
602 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200603 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
604 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100605 }
606
607 /* find iter instance in children */
608 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200609 lyd_find_sibling_val(lyd_node_children(node, 0), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100610 --depth;
611 }
612
Michal Vasko14654712020-02-06 08:35:21 +0100613 return node;
614}
615
Michal Vaskobb844672020-07-03 11:06:12 +0200616/**
617 * @brief Callback for comparing 2 list unique leaf values.
618 *
619 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100620 */
621static int
622lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
623{
624 struct ly_ctx *ctx;
625 struct lysc_node_list *slist;
626 struct lyd_node *diter, *first, *second;
627 struct lyd_value *val1, *val2;
628 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200629 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100630
631 assert(val1_p && val2_p);
632
633 first = *((struct lyd_node **)val1_p);
634 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200635 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100636
637 assert(first && (first->schema->nodetype == LYS_LIST));
638 assert(second && (second->schema == first->schema));
639
640 ctx = first->schema->module->ctx;
641
642 slist = (struct lysc_node_list *)first->schema;
643
644 /* compare unique leaves */
645 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200646 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200647 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100648 goto uniquecheck;
649 }
650 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200651 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100652uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200653 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100654 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200655 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100656 if (diter) {
657 val1 = &((struct lyd_node_term *)diter)->value;
658 } else {
659 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200660 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100661 }
662
663 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200664 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100665 if (diter) {
666 val2 = &((struct lyd_node_term *)diter)->value;
667 } else {
668 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200669 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100670 }
671
672 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
673 /* values differ or either one is not set */
674 break;
675 }
676 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200677 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100678 /* all unique leafs are the same in this set, create this nice error */
679 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
680 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
681
682 /* use buffer to rebuild the unique string */
683 uniq_str = malloc(1024);
684 uniq_str[0] = '\0';
685 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200686 LY_ARRAY_FOR(slist->uniques[u], v) {
687 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100688 strcpy(ptr, " ");
689 ++ptr;
690 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200691 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 +0100692 ptr, 1024 - (ptr - uniq_str));
693 if (!ptr) {
694 /* path will be incomplete, whatever */
695 break;
696 }
697
698 ptr += strlen(ptr);
699 }
700 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
701
702 free(path1);
703 free(path2);
704 free(uniq_str);
705 return 1;
706 }
707
708 if (action > 0) {
709 /* done */
710 return 0;
711 }
712 }
713
714 return 0;
715}
716
Michal Vaskobb844672020-07-03 11:06:12 +0200717/**
718 * @brief Validate list unique leaves.
719 *
720 * @param[in] first First sibling to search in.
721 * @param[in] snode Schema node to validate.
722 * @param[in] uniques List unique arrays to validate.
723 * @return LY_ERR value.
724 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100725static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200726lyd_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 +0100727{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100728 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100729 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200730 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100731 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200732 uint32_t hash, i, size = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100733 int dynamic;
734 const char *str;
735 struct hash_table **uniqtables = NULL;
736 struct lyd_value *val;
737 struct ly_ctx *ctx = snode->module->ctx;
738
739 assert(uniques);
740
741 /* get all list instances */
Michal Vasko9b368d32020-02-14 13:53:31 +0100742 set = ly_set_new();
743 LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100744 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100745 if (diter->schema == snode) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100746 ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100747 }
748 }
Michal Vasko14654712020-02-06 08:35:21 +0100749
750 if (set->count == 2) {
751 /* simple comparison */
752 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
753 /* instance duplication */
754 ret = LY_EVALID;
755 goto cleanup;
756 }
757 } else if (set->count > 2) {
758 /* use hashes for comparison */
759 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200760 for (i = 31; i > 0; i--) {
761 size = set->count << i;
762 size = size >> i;
763 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100764 break;
765 }
766 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200767 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
768 i = 32 - i;
769 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100770
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200771 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100772 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200773 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200774 for (v = 0; v < x; v++) {
775 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
776 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100777 }
778
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200779 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100780 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200781 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100782 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200783 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200784 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100785 if (diter) {
786 val = &((struct lyd_node_term *)diter)->value;
787 } else {
788 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200789 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100790 }
791 if (!val) {
792 /* unique item not present nor has default value */
793 break;
794 }
795
796 /* get canonical string value */
797 str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic);
798 hash = dict_hash_multi(hash, str, strlen(str));
799 if (dynamic) {
800 free((char *)str);
801 }
802 }
803 if (!val) {
804 /* skip this list instance since its unique set is incomplete */
805 continue;
806 }
807
808 /* finish the hash value */
809 hash = dict_hash_multi(hash, NULL, 0);
810
811 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200812 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100813 if (ret == LY_EEXIST) {
814 /* instance duplication */
815 ret = LY_EVALID;
816 }
817 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
818 }
819 }
820 }
821
822cleanup:
823 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200824 for (v = 0; v < x; v++) {
825 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100826 /* failed when allocating uniquetables[j], following j are not allocated */
827 break;
828 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200829 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100830 }
831 free(uniqtables);
832
833 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100834}
835
Michal Vaskobb844672020-07-03 11:06:12 +0200836/**
837 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
838 *
839 * @param[in] first First sibling to search in.
840 * @param[in] sparent Schema parent of the nodes to check.
841 * @param[in] mod Module of the nodes to check.
842 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
843 * @param[in] op Operation being validated, if any.
844 * @return LY_ERR value.
845 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100846static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100847lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci7931b192020-06-25 17:05:03 +0200848 const struct lysc_module *mod, int val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100849{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100850 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100851 struct lysc_node_list *slist;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200852 int getnext_opts;
853
Radek Krejci7931b192020-06-25 17:05:03 +0200854 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100855
Michal Vaskoa3881362020-01-21 15:57:35 +0100856 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200857 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200858 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100859 continue;
860 }
861
Michal Vaskoa3881362020-01-21 15:57:35 +0100862 /* check min-elements and max-elements */
863 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
864 slist = (struct lysc_node_list *)snode;
865 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100866 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100867 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100868
869 /* check generic mandatory existence */
870 } else if (snode->flags & LYS_MAND_TRUE) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100871 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100872 }
873
874 /* check unique */
875 if (snode->nodetype == LYS_LIST) {
876 slist = (struct lysc_node_list *)snode;
877 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200878 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100879 }
880 }
881
Michal Vaskoacd83e72020-02-04 14:12:01 +0100882 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
883 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200884 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100885 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100886 }
887
Michal Vaskoacd83e72020-02-04 14:12:01 +0100888 return LY_SUCCESS;
889}
890
Michal Vaskobb844672020-07-03 11:06:12 +0200891/**
892 * @brief Validate obsolete nodes, only warnings are printed.
893 *
894 * @param[in] node Node to check.
895 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100896static void
897lyd_validate_obsolete(const struct lyd_node *node)
898{
899 const struct lysc_node *snode;
900
901 snode = node->schema;
902 do {
903 if (snode->flags & LYS_STATUS_OBSLT) {
904 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
905 break;
906 }
907
908 snode = snode->parent;
909 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
910}
911
Michal Vaskobb844672020-07-03 11:06:12 +0200912/**
913 * @brief Validate must conditions of a data node.
914 *
915 * @param[in] node Node to validate.
916 * @param[in] op Operation being validated, if any.
917 * @return LY_ERR value.
918 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100919static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200920lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100921{
922 struct lyxp_set xp_set;
923 struct lysc_must *musts;
924 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200925 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100926
927 switch (node->schema->nodetype) {
928 case LYS_CONTAINER:
929 musts = ((struct lysc_node_container *)node->schema)->musts;
930 break;
931 case LYS_LEAF:
932 musts = ((struct lysc_node_leaf *)node->schema)->musts;
933 break;
934 case LYS_LEAFLIST:
935 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
936 break;
937 case LYS_LIST:
938 musts = ((struct lysc_node_list *)node->schema)->musts;
939 break;
940 case LYS_ANYXML:
941 case LYS_ANYDATA:
942 musts = ((struct lysc_node_anydata *)node->schema)->musts;
943 break;
944 case LYS_NOTIF:
945 musts = ((struct lysc_notif *)node->schema)->musts;
946 break;
947 case LYS_RPC:
948 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200949 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200950 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200951 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200952 musts = ((struct lysc_action *)node->schema)->output.musts;
953 } else {
954 LOGINT(LYD_NODE_CTX(node));
955 return LY_EINT;
956 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100957 break;
958 default:
959 LOGINT(LYD_NODE_CTX(node));
960 return LY_EINT;
961 }
962
963 if (!musts) {
964 /* no must to evaluate */
965 return LY_SUCCESS;
966 }
967
968 /* find first top-level node */
969 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent);
970 while (tree->prev->next) {
971 tree = tree->prev;
972 }
973
974 LY_ARRAY_FOR(musts, u) {
975 memset(&xp_set, 0, sizeof xp_set);
976
977 /* evaluate must */
978 LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA));
979
980 /* check the result */
981 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200982 if (!xp_set.val.bln) {
Michal Vaskocc048b22020-03-27 15:52:38 +0100983 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
984 return LY_EVALID;
985 }
986 }
987
988 return LY_SUCCESS;
989}
990
Michal Vaskob1b5c262020-03-05 14:29:47 +0100991LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200992lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, int val_opts,
993 LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100994{
Radek Krejci7f769d72020-07-11 23:13:56 +0200995 struct lyd_node *next = NULL, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100996 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100997
Michal Vasko14654712020-02-06 08:35:21 +0100998 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100999 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001000 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001001 /* all top-level data from this module checked */
1002 break;
Michal Vaskof03ed032020-03-04 13:31:44 +01001003 }
1004
Michal Vaskoa8c61722020-03-27 16:59:32 +01001005 /* opaque data */
1006 if (!node->schema) {
1007 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
1008 ((struct lyd_node_opaq *)node)->name);
1009 return LY_EVALID;
1010 }
1011
Michal Vaskocb7526d2020-03-30 15:08:26 +02001012 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +02001013 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001014 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
1015 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001016 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001017 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
1018 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001019 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001020 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +01001021 return LY_EVALID;
1022 }
1023
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001024 /* obsolete data */
1025 lyd_validate_obsolete(node);
1026
Michal Vaskoc193ce92020-03-06 11:04:48 +01001027 /* node's schema if-features */
1028 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskoa8c61722020-03-27 16:59:32 +01001029 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +01001030 return LY_EVALID;
1031 }
1032
Michal Vaskocc048b22020-03-27 15:52:38 +01001033 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001034 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001035
Michal Vaskoa8c61722020-03-27 16:59:32 +01001036 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +01001037 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001038
Michal Vasko14654712020-02-06 08:35:21 +01001039 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +02001040 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001041
Michal Vaskob1b5c262020-03-05 14:29:47 +01001042 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001043 /* validate all children recursively */
Radek Krejci7931b192020-06-25 17:05:03 +02001044 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 +01001045
Michal Vaskob1b5c262020-03-05 14:29:47 +01001046 /* set default for containers */
1047 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001048 LY_LIST_FOR(lyd_node_children(node, 0), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001049 if (!(next->flags & LYD_DEFAULT)) {
1050 break;
1051 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001052 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001053 if (!next) {
1054 node->flags |= LYD_DEFAULT;
1055 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001056 }
1057 }
1058
1059 return LY_SUCCESS;
1060}
1061
1062LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001063lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vasko8104fd42020-07-13 11:09:51 +02001064 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int val_opts,
1065 struct lyd_node **diff)
Michal Vasko9b368d32020-02-14 13:53:31 +01001066{
Michal Vaskob1b5c262020-03-05 14:29:47 +01001067 LY_ERR ret;
Michal Vasko9b368d32020-02-14 13:53:31 +01001068 const struct lysc_node *iter = NULL;
1069 struct lyd_node *node;
1070 struct lyd_value **dflts;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001071 LY_ARRAY_COUNT_TYPE u;
Michal Vasko9b368d32020-02-14 13:53:31 +01001072
Michal Vaskob1b5c262020-03-05 14:29:47 +01001073 assert(first && (parent || sparent || mod) && node_types && node_when);
Michal Vasko9b368d32020-02-14 13:53:31 +01001074
Michal Vaskob1b5c262020-03-05 14:29:47 +01001075 if (!sparent && parent) {
1076 sparent = parent->schema;
1077 }
1078
1079 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
Radek Krejci7931b192020-06-25 17:05:03 +02001080 if ((val_opts & LYD_VALIDATE_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001081 continue;
1082 }
1083
Michal Vasko9b368d32020-02-14 13:53:31 +01001084 switch (iter->nodetype) {
1085 case LYS_CHOICE:
Michal Vaskof03ed032020-03-04 13:31:44 +01001086 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001087 /* create default case data */
1088 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 +02001089 NULL, node_types, node_when, val_opts, diff));
Michal Vasko9b368d32020-02-14 13:53:31 +01001090 }
1091 break;
1092 case LYS_CONTAINER:
1093 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001094 /* create default NP container */
Michal Vasko9b368d32020-02-14 13:53:31 +01001095 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001096 node->flags = LYD_DEFAULT;
1097 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001098
1099 if (iter->when) {
1100 /* remember to resolve when */
1101 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1102 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001103
1104 /* create any default children */
Michal Vasko8104fd42020-07-13 11:09:51 +02001105 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1106 val_opts, diff));
Michal Vasko9b368d32020-02-14 13:53:31 +01001107 }
1108 break;
1109 case LYS_LEAF:
1110 if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1111 /* create default leaf */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001112 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1113 if (ret == LY_EINCOMPLETE) {
1114 /* remember to resolve type */
1115 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1116 } else if (ret) {
1117 return ret;
1118 }
1119 node->flags = LYD_DEFAULT;
1120 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001121
1122 if (iter->when) {
1123 /* remember to resolve when */
1124 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1125 }
Michal Vasko8104fd42020-07-13 11:09:51 +02001126 if (diff) {
1127 /* add into diff */
1128 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1129 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001130 }
1131 break;
1132 case LYS_LEAFLIST:
Michal Vasko4c583e82020-07-17 12:16:14 +02001133 if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001134 /* create all default leaf-lists */
1135 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001136 LY_ARRAY_FOR(dflts, u) {
1137 ret = lyd_create_term2(iter, dflts[u], &node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001138 if (ret == LY_EINCOMPLETE) {
1139 /* remember to resolve type */
1140 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1141 } else if (ret) {
1142 return ret;
1143 }
1144 node->flags = LYD_DEFAULT;
1145 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001146
1147 if (iter->when) {
1148 /* remember to resolve when */
1149 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1150 }
Michal Vasko8104fd42020-07-13 11:09:51 +02001151 if (diff) {
1152 /* add into diff */
1153 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1154 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001155 }
1156 }
1157 break;
1158 default:
1159 /* without defaults */
1160 break;
1161 }
1162 }
1163
1164 return LY_SUCCESS;
1165}
1166
Radek Krejci7931b192020-06-25 17:05:03 +02001167/**
Michal Vaskobb844672020-07-03 11:06:12 +02001168 * @brief Validate the whole data subtree.
1169 *
1170 * @param[in] root Subtree root.
1171 * @param[in,out] type_check Set for unres node types.
1172 * @param[in,out] type_meta_check Set for unres metadata types.
1173 * @param[in,out] when_check Set for nodes with when conditions.
1174 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001175 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001176 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001177 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001178static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001179lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Michal Vasko8104fd42020-07-13 11:09:51 +02001180 struct ly_set *when_check, int val_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001181{
1182 const struct lyd_meta *meta;
1183 struct lyd_node *next, *node;
1184
1185 LYD_TREE_DFS_BEGIN(root, next, node) {
1186 /* skip added default nodes */
1187 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1188 LY_LIST_FOR(node->meta, meta) {
1189 /* metadata type resolution */
1190 ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST);
1191 }
1192
1193 if (node->schema->nodetype & LYD_NODE_TERM) {
1194 /* node type resolution */
1195 ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST);
1196 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1197 /* new node validation, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001198 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 +02001199
1200 /* add nested defaults */
1201 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 +02001202 when_check, val_opts, diff));
Michal Vaskofea12c62020-03-30 11:00:15 +02001203 }
1204
1205 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1206 /* when evaluation */
1207 ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST);
1208 }
1209 }
1210
1211 LYD_TREE_DFS_END(root, next, node);
1212 }
1213
1214 return LY_SUCCESS;
1215}
1216
Michal Vaskobb844672020-07-03 11:06:12 +02001217/**
1218 * @brief Validate data tree.
1219 *
1220 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1221 * @param[in] modules Array of modules to validate, NULL for all modules.
1222 * @param[in] mod_count Count of @p modules.
1223 * @param[in] ly_ctx libyang context.
1224 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001225 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001226 * @return LY_ERR value.
1227 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001228static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001229lyd_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 +02001230 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001231{
1232 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001233 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001234 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001235 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001236 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001237
Michal Vasko26e80012020-07-08 10:55:46 +02001238 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001239 if (diff) {
1240 *diff = NULL;
1241 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001242
Michal Vaskob1b5c262020-03-05 14:29:47 +01001243 next = *tree;
1244 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001245 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001246 mod = lyd_data_next_module(&next, &first);
1247 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001248 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001249 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001250 if (!mod) {
1251 break;
1252 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001253 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001254 /* make sure first2 changes are carried to tree */
1255 first2 = tree;
1256 } else {
1257 first2 = &first;
1258 }
1259
1260 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001261 ret = lyd_validate_new(first2, NULL, mod, diff);
1262 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001263
1264 /* add all top-level defaults for this module */
Michal Vasko8104fd42020-07-13 11:09:51 +02001265 ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &type_check, &when_check, val_opts, diff);
1266 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001267
1268 /* process nested nodes */
1269 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001270 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1271 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001272 }
1273
1274 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vasko8104fd42020-07-13 11:09:51 +02001275 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, LYD_JSON, lydjson_resolve_prefix,
1276 NULL, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001277 LY_CHECK_GOTO(ret, cleanup);
1278
1279 /* perform final validation that assumes the data tree is final */
Michal Vasko8104fd42020-07-13 11:09:51 +02001280 ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0);
1281 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001282 }
1283
Michal Vaskof03ed032020-03-04 13:31:44 +01001284cleanup:
1285 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001286 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001287 ly_set_erase(&when_check, NULL);
1288 return ret;
1289}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001290
1291API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001292lyd_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 +01001293{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001294 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001295}
1296
1297API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001298lyd_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 +01001299{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001300 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001301}
Michal Vaskofea12c62020-03-30 11:00:15 +02001302
Michal Vaskobb844672020-07-03 11:06:12 +02001303/**
1304 * @brief Find nodes for merging an operation into data tree for validation.
1305 *
1306 * @param[in] op_tree Full operation data tree.
1307 * @param[in] op_node Operation node itself.
1308 * @param[in] tree Data tree to be merged into.
1309 * @param[out] op_subtree Operation subtree to merge.
1310 * @param[out] tree_sibling Data tree sibling to merge next to.
1311 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001312static void
Michal Vaskobb844672020-07-03 11:06:12 +02001313lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
1314 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001315{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001316 const struct lyd_node *tree_iter, *op_iter;
1317 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001318 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001319
Michal Vaskocb7526d2020-03-30 15:08:26 +02001320 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001321 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001322 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001323 ++op_depth;
1324 }
1325
1326 /* find where to merge op */
1327 tree_iter = tree;
1328 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001329 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001330 while (cur_depth) {
1331 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001332 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001333 for (i = 0; i < cur_depth; ++i) {
1334 op_iter = (struct lyd_node *)op_iter->parent;
1335 }
1336
1337 /* find op iter in tree */
1338 lyd_find_sibling_first(tree_iter, op_iter, &match);
1339 if (!match) {
1340 break;
1341 }
1342
1343 /* move tree_iter */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001344 tree_iter = lyd_node_children(match, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001345
1346 /* move depth */
1347 --cur_depth;
1348 }
1349
Michal Vaskobb844672020-07-03 11:06:12 +02001350 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001351 *tree_sibling = (struct lyd_node *)tree_iter;
1352}
1353
1354API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001355lyd_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 +02001356{
1357 LY_ERR ret;
Radek Krejci7931b192020-06-25 17:05:03 +02001358 struct lyd_node *tree_sibling, *op_subtree, *op_next, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001359 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1360
1361 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci7931b192020-06-25 17:05:03 +02001362 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001363 if (diff) {
1364 *diff = NULL;
1365 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001366
1367 /* find the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001368 LYD_TREE_DFS_BEGIN(op_tree, op_next, op_node) {
1369 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 +02001370 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001371 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001372 break;
1373 }
Radek Krejci7931b192020-06-25 17:05:03 +02001374 LYD_TREE_DFS_END(op_tree, op_next, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001375 }
Radek Krejci7931b192020-06-25 17:05:03 +02001376 if (op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) {
1377 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001378 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
1379 return LY_EINVAL;
1380 }
1381 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001382 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001383 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
1384 return LY_EINVAL;
1385 }
1386 }
1387
1388 /* move op_tree to top-level node */
1389 while (op_tree->parent) {
1390 op_tree = (struct lyd_node *)op_tree->parent;
1391 }
1392
1393 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001394 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1395 op_parent = (struct lyd_node *)op_subtree->parent;
1396 lyd_unlink_tree(op_subtree);
1397 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001398 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001399 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001400 }
1401
1402 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001403 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 +02001404
1405 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1406 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 +02001407 LYD_JSON, lydjson_resolve_prefix, NULL, diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001408
1409 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001410 lyd_validate_obsolete(op_node);
1411 if (lysc_node_is_disabled(op_node->schema, 1)) {
1412 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 +02001413 ret = LY_EVALID;
1414 goto cleanup;
1415 }
Radek Krejci7931b192020-06-25 17:05:03 +02001416 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001417
1418 /* final validation of all the descendants */
Radek Krejci7931b192020-06-25 17:05:03 +02001419 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 +02001420
1421cleanup:
1422 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001423 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001424 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001425 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001426 }
1427
1428 ly_set_erase(&type_check, NULL);
1429 ly_set_erase(&type_meta_check, NULL);
1430 ly_set_erase(&when_check, NULL);
1431 return ret;
1432}