blob: 29f376a2b8c031d6bc4e365efb7cc694babb443d [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 Vasko81bc5512020-11-13 18:05:18 +010014#define _XOPEN_SOURCE 500 /* strdup */
15
Michal Vaskofbed4ea2020-07-08 10:43:30 +020016#include "validation.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010017
18#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <stdint.h>
Michal Vasko52927e22020-03-16 17:26:14 +010020#include <stdio.h>
21#include <stdlib.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <string.h>
Michal Vaskocde73ac2019-11-14 16:10:27 +010023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020025#include "compat.h"
Michal Vasko8104fd42020-07-13 11:09:51 +020026#include "diff.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "hash_table.h"
28#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020029#include "parser_data.h"
Michal Vaskofeca4fb2020-10-05 08:58:40 +020030#include "plugins_exts_metadata.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "plugins_types.h"
32#include "set.h"
33#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "tree_data.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010035#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010037#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010039
Michal Vaskoa6669ba2020-08-06 16:14:26 +020040LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +020041lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
42{
43 LY_ERR ret = LY_SUCCESS;
44 struct lyd_node *new_diff = NULL;
Michal Vasko81bc5512020-11-13 18:05:18 +010045 const struct lyd_node *prev_inst;
46 char *key = NULL, *value = NULL;
47 size_t buflen = 0, bufused = 0;
Michal Vasko8104fd42020-07-13 11:09:51 +020048
49 assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
50
Michal Vasko81bc5512020-11-13 18:05:18 +010051 if ((op == LYD_DIFF_OP_CREATE) && lysc_is_userordered(node->schema)) {
52 if (node->prev->next && (node->prev->schema == node->schema)) {
53 prev_inst = node->prev;
54 } else {
55 /* first instance */
56 prev_inst = NULL;
57 }
58
59 if (node->schema->nodetype == LYS_LIST) {
60 /* generate key meta */
61 if (prev_inst) {
62 LY_CHECK_GOTO(ret = lyd_path_list_predicate(prev_inst, &key, &buflen, &bufused, 0), cleanup);
63 } else {
64 key = strdup("");
65 LY_CHECK_ERR_GOTO(!key, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
66 }
67 } else {
68 /* generate value meta */
69 if (prev_inst) {
70 value = strdup(LYD_CANON_VALUE(prev_inst));
71 LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
72 } else {
73 value = strdup("");
74 LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
75 }
76 }
77 }
78
Michal Vasko8104fd42020-07-13 11:09:51 +020079 /* create new diff tree */
Michal Vasko81bc5512020-11-13 18:05:18 +010080 LY_CHECK_GOTO(ret = lyd_diff_add(node, op, NULL, NULL, key, value, NULL, &new_diff), cleanup);
Michal Vasko8104fd42020-07-13 11:09:51 +020081
82 /* merge into existing diff */
Michal Vaskoc0e58e82020-11-11 19:04:33 +010083 ret = lyd_diff_merge_all(diff, new_diff, 0);
Michal Vasko8104fd42020-07-13 11:09:51 +020084
Michal Vasko81bc5512020-11-13 18:05:18 +010085cleanup:
Michal Vasko8104fd42020-07-13 11:09:51 +020086 lyd_free_tree(new_diff);
Michal Vasko81bc5512020-11-13 18:05:18 +010087 free(key);
88 free(value);
Michal Vasko8104fd42020-07-13 11:09:51 +020089 return ret;
90}
91
92/**
Michal Vaskocde73ac2019-11-14 16:10:27 +010093 * @brief Evaluate a single "when" condition.
94 *
Michal Vaskob1b5c262020-03-05 14:29:47 +010095 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +010096 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +010097 * @param[in] when When to evaluate.
Michal Vasko8104fd42020-07-13 11:09:51 +020098 * @param[in,out] diff Validation diff.
Michal Vaskocde73ac2019-11-14 16:10:27 +010099 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
100 */
101static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200102lyd_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 +0100103{
Michal Vasko8104fd42020-07-13 11:09:51 +0200104 LY_ERR ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100105 const struct lyd_node *ctx_node;
106 struct lyxp_set xp_set;
107
108 memset(&xp_set, 0, sizeof xp_set);
109
110 if (when->context == node->schema) {
111 ctx_node = node;
112 } else {
113 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
114 ctx_node = (struct lyd_node *)node->parent;
115 }
116
117 /* evaluate when */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200118 ret = lyxp_eval(when->cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes, ctx_node, *tree,
119 &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100120 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
121
122 /* return error or LY_EINCOMPLETE for dependant unresolved when */
123 LY_CHECK_RET(ret);
124
125 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +0200126 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100127 if (node->flags & LYD_WHEN_TRUE) {
128 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100129 if (LYD_DEL_IS_ROOT(*tree, node)) {
130 *tree = (*tree)->next;
131 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200132 if (diff) {
133 /* add into diff */
134 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff));
135 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100136 lyd_free_tree(node);
137 } else {
138 /* invalid data */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200139 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vasko8104fd42020-07-13 11:09:51 +0200140 return LY_EVALID;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100141 }
142 } else {
143 /* remember that when evaluated to true */
144 node->flags |= LYD_WHEN_TRUE;
145 }
146
147 return ret;
148}
149
150LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100151lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *meta_types,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200152 struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100153{
154 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200155 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100156
Michal Vaskob1b5c262020-03-05 14:29:47 +0100157 if (node_when) {
158 /* evaluate all when conditions */
159 uint32_t prev_count;
160 do {
161 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200162 i = 0;
163 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100164 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200165 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100166 const struct lysc_node *schema = node->schema;
Radek Krejci857189e2020-09-01 13:26:36 +0200167 ly_bool unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100168
Michal Vaskob1b5c262020-03-05 14:29:47 +0100169 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200170 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200171 LY_ARRAY_FOR(schema->when, u) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200172 ret = lyd_validate_when(tree, node, schema->when[u], diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100173 if (ret) {
174 break;
175 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100176 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100177 if (ret == LY_EINCOMPLETE) {
178 /* could not evaluate this when */
179 unres_when = 1;
180 break;
181 } else if (ret) {
182 /* error */
183 return ret;
184 }
185 schema = schema->parent;
186 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100187
Michal Vaskob1b5c262020-03-05 14:29:47 +0100188 if (unres_when) {
189 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200190 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100191 } else {
192 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200193 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100194 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100195 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100196
Radek Krejci0f969882020-08-21 16:56:47 +0200197 /* there must have been some when conditions resolved */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100198 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100199
Michal Vaskob1b5c262020-03-05 14:29:47 +0100200 /* there could have been no cyclic when dependencies, checked during compilation */
201 assert(!node_when->count);
202 }
203
204 if (node_types && node_types->count) {
205 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200206 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100207 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200208 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100209
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200210 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200211 struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100212
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200213 /* resolve the value of the node */
214 ret = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, (struct lyd_node *)node, *tree,
215 LY_VLOG_LYD, node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100216 LY_CHECK_RET(ret);
217
218 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200219 ly_set_rm_index(node_types, i, NULL);
220 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100221 }
222
Michal Vasko9f96a052020-03-10 09:41:45 +0100223 if (meta_types && meta_types->count) {
224 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200225 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100226 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200227 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100228
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200229 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200230 struct lysc_type *type = ((struct lyext_metadata *)meta->annotation->data)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100231
Michal Vasko9f96a052020-03-10 09:41:45 +0100232 /* validate and store the value of the metadata */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200233 ret = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree,
234 LY_VLOG_NONE, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100235 LY_CHECK_RET(ret);
236
237 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200238 ly_set_rm_index(meta_types, i, NULL);
239 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100240 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100241
242 return ret;
243}
244
Michal Vaskobb844672020-07-03 11:06:12 +0200245/**
246 * @brief Validate instance duplication.
247 *
248 * @param[in] first First sibling to search in.
249 * @param[in] node Data node instance to check.
250 * @return LY_ERR value.
251 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100252static LY_ERR
253lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100254{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100255 struct lyd_node **match_p;
Radek Krejci857189e2020-09-01 13:26:36 +0200256 ly_bool fail = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100257
258 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
259 /* duplicate instances allowed */
260 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100261 }
262
Michal Vaskob1b5c262020-03-05 14:29:47 +0100263 /* find exactly the same next instance using hashes if possible */
264 if (node->parent && node->parent->children_ht) {
265 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
266 fail = 1;
267 }
268 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200269 for ( ; first; first = first->next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100270 if (first == node) {
271 continue;
272 }
273
274 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
275 if (first->schema == node->schema) {
276 fail = 1;
277 break;
278 }
Michal Vasko8f359bf2020-07-28 10:41:15 +0200279 } else if (!lyd_compare_single(first, node, 0)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100280 fail = 1;
281 break;
282 }
283 }
284 }
285
286 if (fail) {
287 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
288 return LY_EVALID;
289 }
290 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100291}
292
Michal Vaskobb844672020-07-03 11:06:12 +0200293/**
294 * @brief Validate multiple case data existence with possible autodelete.
295 *
296 * @param[in,out] first First sibling to search in, is updated if needed.
297 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200298 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200299 * @return LY_ERR value.
300 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100301static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200302lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100303{
304 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
305 struct lyd_node *match, *to_del;
Radek Krejci857189e2020-09-01 13:26:36 +0200306 ly_bool found;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100307
308 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
309 found = 0;
310 iter = NULL;
311 match = NULL;
312 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
313 if (match->flags & LYD_NEW) {
314 /* a new case data found, nothing more to look for */
315 found = 2;
316 break;
317 } else {
318 /* and old case data found */
319 if (found == 0) {
320 found = 1;
321 }
322 }
323 }
324
325 if (found == 1) {
326 /* there should not be 2 old cases */
327 if (old_case) {
328 /* old data from 2 cases */
329 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
330 return LY_EVALID;
331 }
332
333 /* remember an old existing case */
334 old_case = scase;
335 } else if (found == 2) {
336 if (new_case) {
337 /* new data from 2 cases */
338 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
339 return LY_EVALID;
340 }
341
342 /* remember a new existing case */
343 new_case = scase;
344 }
345 }
346
347 if (old_case && new_case) {
348 /* auto-delete old case */
349 iter = NULL;
350 match = NULL;
351 to_del = NULL;
352 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
353 if (LYD_DEL_IS_ROOT(*first, to_del)) {
354 *first = (*first)->next;
355 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200356 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100357 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200358 if (diff) {
359 /* add into diff */
360 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
361 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100362 to_del = match;
363 }
364 if (LYD_DEL_IS_ROOT(*first, to_del)) {
365 *first = (*first)->next;
366 }
367 lyd_free_tree(to_del);
368 }
369
370 return LY_SUCCESS;
371}
372
Michal Vaskobb844672020-07-03 11:06:12 +0200373/**
374 * @brief Check whether a schema node can have some default values (true for NP containers as well).
375 *
376 * @param[in] schema Schema node to check.
377 * @return non-zero if yes,
378 * @return 0 otherwise.
379 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100380static int
381lyd_val_has_default(const struct lysc_node *schema)
382{
383 switch (schema->nodetype) {
384 case LYS_LEAF:
385 if (((struct lysc_node_leaf *)schema)->dflt) {
386 return 1;
387 }
388 break;
389 case LYS_LEAFLIST:
390 if (((struct lysc_node_leaflist *)schema)->dflts) {
391 return 1;
392 }
393 break;
394 case LYS_CONTAINER:
395 if (!(schema->flags & LYS_PRESENCE)) {
396 return 1;
397 }
398 break;
399 default:
400 break;
401 }
402
403 return 0;
404}
405
Michal Vaskobb844672020-07-03 11:06:12 +0200406/**
407 * @brief Autodelete old instances to prevent validation errors.
408 *
409 * @param[in,out] first First sibling to search in, is updated if needed.
410 * @param[in] node Data node instance to check.
411 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200412 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200413 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100414static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200415lyd_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 +0100416{
Michal Vasko8104fd42020-07-13 11:09:51 +0200417 struct lyd_node *match, *next, *iter;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100418
419 if (lyd_val_has_default(node->schema)) {
420 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200421 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100422 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
423 /* default instance found, remove it */
424 if (LYD_DEL_IS_ROOT(*first, match)) {
425 *first = (*first)->next;
426 }
427 if (match == *next_p) {
428 *next_p = (*next_p)->next;
429 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200430 if (diff) {
431 /* add into diff */
432 if ((match->schema->nodetype == LYS_CONTAINER) && !(match->schema->flags & LYS_PRESENCE)) {
433 /* we do not want to track NP container changes, but remember any removed children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200434 LY_LIST_FOR(lyd_child(match), iter) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200435 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
436 }
437 } else {
438 lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff);
439 }
440 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100441 lyd_free_tree(match);
442
443 /* remove only a single container/leaf default instance, if there are more, it is an error */
444 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
445 break;
446 }
447 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100448 }
449 }
450}
451
452LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200453lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200454 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100455{
456 struct lyd_node *next, *node;
457 const struct lysc_node *snode = NULL;
458
459 assert(first && (sparent || mod));
460
461 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
462 /* check case duplicites */
463 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200464 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100465 }
466 }
467
468 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100469 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100470 /* all top-level data from this module checked */
471 break;
472 }
473
474 if (!(node->flags & LYD_NEW)) {
475 /* check only new nodes */
476 continue;
477 }
478
479 /* remove old default(s) if it exists */
Michal Vasko8104fd42020-07-13 11:09:51 +0200480 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100481
482 /* then check new node instance duplicities */
483 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
484
485 /* this node is valid */
486 node->flags &= ~LYD_NEW;
487 }
488
489 return LY_SUCCESS;
490}
491
Michal Vaskobb844672020-07-03 11:06:12 +0200492/**
493 * @brief Validate mandatory node existence.
494 *
495 * @param[in] first First sibling to search in.
496 * @param[in] snode Schema node to validate.
497 * @return LY_ERR value.
498 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100499static LY_ERR
500lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100501{
Michal Vaskoa3881362020-01-21 15:57:35 +0100502 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100503 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100504 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100505 return LY_SUCCESS;
506 }
507 } else {
508 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100509
Michal Vaskob1b5c262020-03-05 14:29:47 +0100510 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100511 /* data instance found */
512 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100513 }
514 }
515
516 /* node instance not found */
517 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
518 return LY_EVALID;
519}
520
Michal Vaskobb844672020-07-03 11:06:12 +0200521/**
522 * @brief Validate min/max-elements constraints, if any.
523 *
524 * @param[in] first First sibling to search in.
525 * @param[in] snode Schema node to validate.
526 * @param[in] min Minimum number of elements, 0 for no restriction.
527 * @param[in] max Max number of elements, 0 for no restriction.
528 * @return LY_ERR value.
529 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100530static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100531lyd_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 +0100532{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100533 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200534 struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100535
Michal Vasko9b368d32020-02-14 13:53:31 +0100536 assert(min || max);
537
Michal Vasko4c583e82020-07-17 12:16:14 +0200538 LYD_LIST_FOR_INST(first, snode, iter) {
539 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100540
Michal Vasko4c583e82020-07-17 12:16:14 +0200541 if (min && (count == min)) {
542 /* satisfied */
543 min = 0;
544 if (!max) {
545 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100546 break;
547 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100548 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200549 if (max && (count > max)) {
550 /* not satisifed */
551 break;
552 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100553 }
554
Michal Vasko9b368d32020-02-14 13:53:31 +0100555 if (min) {
556 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100557 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
558 return LY_EVALID;
559 } else if (max && (count > max)) {
560 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
561 return LY_EVALID;
562 }
563
Michal Vaskoa3881362020-01-21 15:57:35 +0100564 return LY_SUCCESS;
565}
566
Michal Vaskobb844672020-07-03 11:06:12 +0200567/**
568 * @brief Find node referenced by a list unique statement.
569 *
570 * @param[in] uniq_leaf Unique leaf to find.
571 * @param[in] list List instance to use for the search.
572 * @return Found leaf,
573 * @return NULL if no leaf found.
574 */
Michal Vasko14654712020-02-06 08:35:21 +0100575static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200576lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100577{
Michal Vasko9b368d32020-02-14 13:53:31 +0100578 struct lyd_node *node;
579 const struct lysc_node *iter;
580 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100581
Michal Vasko9b368d32020-02-14 13:53:31 +0100582 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200583 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
584 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100585 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100586
Michal Vaskobb844672020-07-03 11:06:12 +0200587 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100588 while (node && depth) {
589 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200590 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
591 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100592 }
593
594 /* find iter instance in children */
595 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Radek Krejcia1c1e542020-09-29 16:06:52 +0200596 lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100597 --depth;
598 }
599
Michal Vasko14654712020-02-06 08:35:21 +0100600 return node;
601}
602
Michal Vaskobb844672020-07-03 11:06:12 +0200603/**
604 * @brief Callback for comparing 2 list unique leaf values.
605 *
Radek Krejci857189e2020-09-01 13:26:36 +0200606 * Implementation of ::values_equal_cb.
607 *
Michal Vaskobb844672020-07-03 11:06:12 +0200608 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100609 */
Radek Krejci857189e2020-09-01 13:26:36 +0200610static ly_bool
611lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Michal Vasko14654712020-02-06 08:35:21 +0100612{
613 struct ly_ctx *ctx;
614 struct lysc_node_list *slist;
615 struct lyd_node *diter, *first, *second;
616 struct lyd_value *val1, *val2;
617 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200618 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100619
620 assert(val1_p && val2_p);
621
622 first = *((struct lyd_node **)val1_p);
623 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200624 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100625
626 assert(first && (first->schema->nodetype == LYS_LIST));
627 assert(second && (second->schema == first->schema));
628
629 ctx = first->schema->module->ctx;
630
631 slist = (struct lysc_node_list *)first->schema;
632
633 /* compare unique leaves */
634 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200635 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200636 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100637 goto uniquecheck;
638 }
639 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200640 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100641uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200642 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100643 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200644 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100645 if (diter) {
646 val1 = &((struct lyd_node_term *)diter)->value;
647 } else {
648 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200649 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100650 }
651
652 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200653 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100654 if (diter) {
655 val2 = &((struct lyd_node_term *)diter)->value;
656 } else {
657 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200658 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100659 }
660
661 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
662 /* values differ or either one is not set */
663 break;
664 }
665 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200666 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100667 /* all unique leafs are the same in this set, create this nice error */
668 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
669 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
670
671 /* use buffer to rebuild the unique string */
672 uniq_str = malloc(1024);
673 uniq_str[0] = '\0';
674 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200675 LY_ARRAY_FOR(slist->uniques[u], v) {
676 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100677 strcpy(ptr, " ");
678 ++ptr;
679 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200680 ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], (struct lysc_node *)slist, LYSC_PATH_LOG,
Michal Vasko69730152020-10-09 16:30:07 +0200681 ptr, 1024 - (ptr - uniq_str));
Michal Vasko14654712020-02-06 08:35:21 +0100682 if (!ptr) {
683 /* path will be incomplete, whatever */
684 break;
685 }
686
687 ptr += strlen(ptr);
688 }
689 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
690
691 free(path1);
692 free(path2);
693 free(uniq_str);
694 return 1;
695 }
696
697 if (action > 0) {
698 /* done */
699 return 0;
700 }
701 }
702
703 return 0;
704}
705
Michal Vaskobb844672020-07-03 11:06:12 +0200706/**
707 * @brief Validate list unique leaves.
708 *
709 * @param[in] first First sibling to search in.
710 * @param[in] snode Schema node to validate.
711 * @param[in] uniques List unique arrays to validate.
712 * @return LY_ERR value.
713 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100714static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200715lyd_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 +0100716{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100717 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100718 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200719 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100720 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200721 uint32_t hash, i, size = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200722 ly_bool dynamic;
Michal Vasko14654712020-02-06 08:35:21 +0100723 const char *str;
724 struct hash_table **uniqtables = NULL;
725 struct lyd_value *val;
726 struct ly_ctx *ctx = snode->module->ctx;
727
728 assert(uniques);
729
730 /* get all list instances */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200731 LY_CHECK_RET(ly_set_new(&set));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100732 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100733 if (diter->schema == snode) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200734 ret = ly_set_add(set, (void *)diter, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200735 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko9b368d32020-02-14 13:53:31 +0100736 }
737 }
Michal Vasko14654712020-02-06 08:35:21 +0100738
739 if (set->count == 2) {
740 /* simple comparison */
741 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
742 /* instance duplication */
743 ret = LY_EVALID;
744 goto cleanup;
745 }
746 } else if (set->count > 2) {
747 /* use hashes for comparison */
748 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200749 for (i = 31; i > 0; i--) {
750 size = set->count << i;
751 size = size >> i;
752 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100753 break;
754 }
755 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200756 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
757 i = 32 - i;
758 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100759
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200760 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100761 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200762 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200763 for (v = 0; v < x; v++) {
764 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
765 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100766 }
767
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200768 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100769 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200770 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100771 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200772 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200773 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100774 if (diter) {
775 val = &((struct lyd_node_term *)diter)->value;
776 } else {
777 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200778 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100779 }
780 if (!val) {
781 /* unique item not present nor has default value */
782 break;
783 }
784
785 /* get canonical string value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200786 str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +0100787 hash = dict_hash_multi(hash, str, strlen(str));
788 if (dynamic) {
789 free((char *)str);
790 }
791 }
792 if (!val) {
793 /* skip this list instance since its unique set is incomplete */
794 continue;
795 }
796
797 /* finish the hash value */
798 hash = dict_hash_multi(hash, NULL, 0);
799
800 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200801 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100802 if (ret == LY_EEXIST) {
803 /* instance duplication */
804 ret = LY_EVALID;
805 }
806 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
807 }
808 }
809 }
810
811cleanup:
812 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200813 for (v = 0; v < x; v++) {
814 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100815 /* failed when allocating uniquetables[j], following j are not allocated */
816 break;
817 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200818 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100819 }
820 free(uniqtables);
821
822 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100823}
824
Michal Vaskobb844672020-07-03 11:06:12 +0200825/**
826 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
827 *
828 * @param[in] first First sibling to search in.
829 * @param[in] sparent Schema parent of the nodes to check.
830 * @param[in] mod Module of the nodes to check.
831 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
832 * @param[in] op Operation being validated, if any.
833 * @return LY_ERR value.
834 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100835static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100836lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200837 const struct lysc_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100838{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100839 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100840 struct lysc_node_list *slist;
Michal Vaskod8958df2020-08-05 13:27:36 +0200841 struct lysc_node_leaflist *sllist;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200842 uint32_t getnext_opts;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200843
Radek Krejci7931b192020-06-25 17:05:03 +0200844 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100845
Michal Vaskoa3881362020-01-21 15:57:35 +0100846 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200847 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200848 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100849 continue;
850 }
851
Michal Vaskoa3881362020-01-21 15:57:35 +0100852 /* check min-elements and max-elements */
Michal Vaskod8958df2020-08-05 13:27:36 +0200853 if (snode->nodetype == LYS_LIST) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100854 slist = (struct lysc_node_list *)snode;
855 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100856 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100857 }
Michal Vaskod8958df2020-08-05 13:27:36 +0200858 } else if (snode->nodetype == LYS_LEAFLIST) {
859 sllist = (struct lysc_node_leaflist *)snode;
860 if (sllist->min || sllist->max) {
861 LY_CHECK_RET(lyd_validate_minmax(first, snode, sllist->min, sllist->max));
862 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100863
Michal Vaskoacd83e72020-02-04 14:12:01 +0100864 } else if (snode->flags & LYS_MAND_TRUE) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200865 /* check generic mandatory existence */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100866 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100867 }
868
869 /* check unique */
870 if (snode->nodetype == LYS_LIST) {
871 slist = (struct lysc_node_list *)snode;
872 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200873 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100874 }
875 }
876
Michal Vaskoacd83e72020-02-04 14:12:01 +0100877 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
878 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200879 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100880 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100881 }
882
Michal Vaskoacd83e72020-02-04 14:12:01 +0100883 return LY_SUCCESS;
884}
885
Michal Vaskobb844672020-07-03 11:06:12 +0200886/**
887 * @brief Validate obsolete nodes, only warnings are printed.
888 *
889 * @param[in] node Node to check.
890 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100891static void
892lyd_validate_obsolete(const struct lyd_node *node)
893{
894 const struct lysc_node *snode;
895
896 snode = node->schema;
897 do {
898 if (snode->flags & LYS_STATUS_OBSLT) {
899 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
900 break;
901 }
902
903 snode = snode->parent;
904 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
905}
906
Michal Vaskobb844672020-07-03 11:06:12 +0200907/**
908 * @brief Validate must conditions of a data node.
909 *
910 * @param[in] node Node to validate.
911 * @param[in] op Operation being validated, if any.
912 * @return LY_ERR value.
913 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100914static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200915lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100916{
917 struct lyxp_set xp_set;
918 struct lysc_must *musts;
919 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200920 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100921
922 switch (node->schema->nodetype) {
923 case LYS_CONTAINER:
924 musts = ((struct lysc_node_container *)node->schema)->musts;
925 break;
926 case LYS_LEAF:
927 musts = ((struct lysc_node_leaf *)node->schema)->musts;
928 break;
929 case LYS_LEAFLIST:
930 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
931 break;
932 case LYS_LIST:
933 musts = ((struct lysc_node_list *)node->schema)->musts;
934 break;
935 case LYS_ANYXML:
936 case LYS_ANYDATA:
937 musts = ((struct lysc_node_anydata *)node->schema)->musts;
938 break;
939 case LYS_NOTIF:
940 musts = ((struct lysc_notif *)node->schema)->musts;
941 break;
942 case LYS_RPC:
943 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200944 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200945 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200946 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200947 musts = ((struct lysc_action *)node->schema)->output.musts;
948 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200949 LOGINT(LYD_CTX(node));
Michal Vaskocb7526d2020-03-30 15:08:26 +0200950 return LY_EINT;
951 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100952 break;
953 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200954 LOGINT(LYD_CTX(node));
Michal Vaskocc048b22020-03-27 15:52:38 +0100955 return LY_EINT;
956 }
957
958 if (!musts) {
959 /* no must to evaluate */
960 return LY_SUCCESS;
961 }
962
963 /* find first top-level node */
Radek Krejci1e008d22020-08-17 11:37:37 +0200964 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent) {}
Michal Vaskocc048b22020-03-27 15:52:38 +0100965 while (tree->prev->next) {
966 tree = tree->prev;
967 }
968
969 LY_ARRAY_FOR(musts, u) {
970 memset(&xp_set, 0, sizeof xp_set);
971
972 /* evaluate must */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200973 LY_CHECK_RET(lyxp_eval(musts[u].cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes, node,
974 tree, &xp_set, LYXP_SCHEMA));
Michal Vaskocc048b22020-03-27 15:52:38 +0100975
976 /* check the result */
977 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200978 if (!xp_set.val.bln) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200979 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
Michal Vaskocc048b22020-03-27 15:52:38 +0100980 return LY_EVALID;
981 }
982 }
983
984 return LY_SUCCESS;
985}
986
Michal Vaskob1b5c262020-03-05 14:29:47 +0100987LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200988lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, uint32_t val_opts,
Radek Krejci0f969882020-08-21 16:56:47 +0200989 LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100990{
Radek Krejci7f769d72020-07-11 23:13:56 +0200991 struct lyd_node *next = NULL, *node;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100992
Michal Vasko14654712020-02-06 08:35:21 +0100993 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100994 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100995 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100996 /* all top-level data from this module checked */
997 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100998 }
999
Michal Vaskoa8c61722020-03-27 16:59:32 +01001000 /* opaque data */
1001 if (!node->schema) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001002 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
Radek Krejci0f969882020-08-21 16:56:47 +02001003 ((struct lyd_node_opaq *)node)->name);
Michal Vaskoa8c61722020-03-27 16:59:32 +01001004 return LY_EVALID;
1005 }
1006
Michal Vaskocb7526d2020-03-30 15:08:26 +02001007 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +02001008 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001009 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001010 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001011 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001012 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001013 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001014 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001015 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +01001016 return LY_EVALID;
1017 }
1018
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001019 /* obsolete data */
1020 lyd_validate_obsolete(node);
1021
Michal Vaskocc048b22020-03-27 15:52:38 +01001022 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001023 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001024
Michal Vasko53d97a12020-11-05 17:39:10 +01001025 /* node value was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +01001026 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001027
Michal Vasko14654712020-02-06 08:35:21 +01001028 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +02001029 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001030
Michal Vaskob1b5c262020-03-05 14:29:47 +01001031 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001032 /* validate all children recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001033 LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node->schema, NULL, val_opts, op));
Michal Vaskocde73ac2019-11-14 16:10:27 +01001034
Michal Vaskob1b5c262020-03-05 14:29:47 +01001035 /* set default for containers */
1036 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Radek Krejcia1c1e542020-09-29 16:06:52 +02001037 LY_LIST_FOR(lyd_child(node), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001038 if (!(next->flags & LYD_DEFAULT)) {
1039 break;
1040 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001041 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001042 if (!next) {
1043 node->flags |= LYD_DEFAULT;
1044 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001045 }
1046 }
1047
1048 return LY_SUCCESS;
1049}
1050
Radek Krejci7931b192020-06-25 17:05:03 +02001051/**
Michal Vaskobb844672020-07-03 11:06:12 +02001052 * @brief Validate the whole data subtree.
1053 *
1054 * @param[in] root Subtree root.
1055 * @param[in,out] type_check Set for unres node types.
1056 * @param[in,out] type_meta_check Set for unres metadata types.
1057 * @param[in,out] when_check Set for nodes with when conditions.
1058 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001059 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001060 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001061 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001062static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001063lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001064 struct ly_set *when_check, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001065{
1066 const struct lyd_meta *meta;
Michal Vasko56daf732020-08-10 10:57:18 +02001067 struct lyd_node *node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001068
Michal Vasko56daf732020-08-10 10:57:18 +02001069 LYD_TREE_DFS_BEGIN(root, node) {
Michal Vasko0275cf62020-11-05 17:40:30 +01001070 LY_LIST_FOR(node->meta, meta) {
1071 if (((struct lyext_metadata *)meta->annotation->data)->type->plugin->validate) {
1072 /* metadata type resolution */
1073 LY_CHECK_RET(ly_set_add(type_meta_check, (void *)meta, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001074 }
Michal Vasko0275cf62020-11-05 17:40:30 +01001075 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001076
Michal Vasko0275cf62020-11-05 17:40:30 +01001077 if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
1078 /* node type resolution */
1079 LY_CHECK_RET(ly_set_add(type_check, (void *)node, 1, NULL));
1080 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1081 /* new node validation, autodelete */
1082 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 +02001083
Michal Vasko0275cf62020-11-05 17:40:30 +01001084 /* add nested defaults */
1085 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
1086 when_check, val_opts & LYD_VALIDATE_NO_STATE ? LYD_IMPLICIT_NO_STATE : 0, diff));
1087 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001088
Michal Vasko0275cf62020-11-05 17:40:30 +01001089 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1090 /* when evaluation */
1091 LY_CHECK_RET(ly_set_add(when_check, (void *)node, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001092 }
1093
Michal Vasko56daf732020-08-10 10:57:18 +02001094 LYD_TREE_DFS_END(root, node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001095 }
1096
1097 return LY_SUCCESS;
1098}
1099
Michal Vaskobb844672020-07-03 11:06:12 +02001100/**
1101 * @brief Validate data tree.
1102 *
1103 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1104 * @param[in] modules Array of modules to validate, NULL for all modules.
1105 * @param[in] mod_count Count of @p modules.
1106 * @param[in] ly_ctx libyang context.
1107 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001108 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001109 * @return LY_ERR value.
1110 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001111static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001112lyd_validate(struct lyd_node **tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t val_opts,
Radek Krejci0f969882020-08-21 16:56:47 +02001113 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001114{
1115 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001116 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001117 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001118 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001119 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001120
Michal Vasko26e80012020-07-08 10:55:46 +02001121 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001122 if (diff) {
1123 *diff = NULL;
1124 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001125
Michal Vaskob1b5c262020-03-05 14:29:47 +01001126 next = *tree;
1127 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001128 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001129 mod = lyd_data_next_module(&next, &first);
1130 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001131 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001132 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001133 if (!mod) {
1134 break;
1135 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001136 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001137 /* make sure first2 changes are carried to tree */
1138 first2 = tree;
1139 } else {
1140 first2 = &first;
1141 }
1142
1143 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001144 ret = lyd_validate_new(first2, NULL, mod, diff);
1145 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001146
Michal Vasko0275cf62020-11-05 17:40:30 +01001147 /* add all top-level defaults for this module, do not add into unres sets, will occur in the next step */
1148 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, NULL, NULL, val_opts & LYD_VALIDATE_NO_STATE ?
Michal Vasko69730152020-10-09 16:30:07 +02001149 LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001150 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001151
1152 /* process nested nodes */
1153 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001154 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1155 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001156 }
1157
1158 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001159 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001160 LY_CHECK_GOTO(ret, cleanup);
1161
1162 /* perform final validation that assumes the data tree is final */
Michal Vasko8104fd42020-07-13 11:09:51 +02001163 ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0);
1164 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001165 }
1166
Michal Vaskof03ed032020-03-04 13:31:44 +01001167cleanup:
1168 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001169 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001170 ly_set_erase(&when_check, NULL);
1171 return ret;
1172}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001173
1174API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001175lyd_validate_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001176{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001177 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001178}
1179
1180API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001181lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001182{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001183 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001184}
Michal Vaskofea12c62020-03-30 11:00:15 +02001185
Michal Vaskobb844672020-07-03 11:06:12 +02001186/**
1187 * @brief Find nodes for merging an operation into data tree for validation.
1188 *
1189 * @param[in] op_tree Full operation data tree.
1190 * @param[in] op_node Operation node itself.
1191 * @param[in] tree Data tree to be merged into.
1192 * @param[out] op_subtree Operation subtree to merge.
1193 * @param[out] tree_sibling Data tree sibling to merge next to.
1194 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001195static void
Michal Vaskobb844672020-07-03 11:06:12 +02001196lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
Radek Krejci0f969882020-08-21 16:56:47 +02001197 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001198{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001199 const struct lyd_node *tree_iter, *op_iter;
1200 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001201 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001202
Michal Vaskocb7526d2020-03-30 15:08:26 +02001203 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001204 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001205 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001206 ++op_depth;
1207 }
1208
1209 /* find where to merge op */
1210 tree_iter = tree;
1211 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001212 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001213 while (cur_depth) {
1214 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001215 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001216 for (i = 0; i < cur_depth; ++i) {
1217 op_iter = (struct lyd_node *)op_iter->parent;
1218 }
1219
1220 /* find op iter in tree */
1221 lyd_find_sibling_first(tree_iter, op_iter, &match);
1222 if (!match) {
1223 break;
1224 }
1225
1226 /* move tree_iter */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001227 tree_iter = lyd_child(match);
Michal Vaskofea12c62020-03-30 11:00:15 +02001228
1229 /* move depth */
1230 --cur_depth;
1231 }
1232
Michal Vaskobb844672020-07-03 11:06:12 +02001233 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001234 *tree_sibling = (struct lyd_node *)tree_iter;
1235}
1236
1237API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001238lyd_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 +02001239{
1240 LY_ERR ret;
Michal Vasko56daf732020-08-10 10:57:18 +02001241 struct lyd_node *tree_sibling, *op_subtree, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001242 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1243
1244 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci0f969882020-08-21 16:56:47 +02001245 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001246 if (diff) {
1247 *diff = NULL;
1248 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001249
1250 /* find the operation/notification */
Michal Vasko56daf732020-08-10 10:57:18 +02001251 LYD_TREE_DFS_BEGIN(op_tree, op_node) {
Michal Vasko69730152020-10-09 16:30:07 +02001252 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 +02001253 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001254 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001255 break;
1256 }
Michal Vasko56daf732020-08-10 10:57:18 +02001257 LYD_TREE_DFS_END(op_tree, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001258 }
Michal Vasko69730152020-10-09 16:30:07 +02001259 if ((op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY)) {
Radek Krejci7931b192020-06-25 17:05:03 +02001260 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001261 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001262 return LY_EINVAL;
1263 }
1264 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001265 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001266 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001267 return LY_EINVAL;
1268 }
1269 }
1270
1271 /* move op_tree to top-level node */
1272 while (op_tree->parent) {
1273 op_tree = (struct lyd_node *)op_tree->parent;
1274 }
1275
1276 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001277 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1278 op_parent = (struct lyd_node *)op_subtree->parent;
1279 lyd_unlink_tree(op_subtree);
1280 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001281 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001282 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001283 }
1284
1285 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001286 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 +02001287
1288 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1289 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001290 diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001291
1292 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001293 lyd_validate_obsolete(op_node);
Radek Krejci7931b192020-06-25 17:05:03 +02001294 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001295
1296 /* final validation of all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001297 LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_child(op_node), op_node->schema, NULL, 0, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001298
1299cleanup:
1300 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001301 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001302 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001303 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001304 }
1305
1306 ly_set_erase(&type_check, NULL);
1307 ly_set_erase(&type_meta_check, NULL);
1308 ly_set_erase(&when_check, NULL);
1309 return ret;
1310}