blob: f8ec013ed07cd002b9e781844a3bc3659ca679ec [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
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100258 assert(node->flags & LYD_NEW);
259
Michal Vaskob1b5c262020-03-05 14:29:47 +0100260 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
261 /* duplicate instances allowed */
262 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100263 }
264
Michal Vaskob1b5c262020-03-05 14:29:47 +0100265 /* find exactly the same next instance using hashes if possible */
266 if (node->parent && node->parent->children_ht) {
267 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
268 fail = 1;
269 }
270 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200271 for ( ; first; first = first->next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100272 if (first == node) {
273 continue;
274 }
275
276 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
277 if (first->schema == node->schema) {
278 fail = 1;
279 break;
280 }
Michal Vasko8f359bf2020-07-28 10:41:15 +0200281 } else if (!lyd_compare_single(first, node, 0)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100282 fail = 1;
283 break;
284 }
285 }
286 }
287
288 if (fail) {
289 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
290 return LY_EVALID;
291 }
292 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100293}
294
Michal Vaskobb844672020-07-03 11:06:12 +0200295/**
296 * @brief Validate multiple case data existence with possible autodelete.
297 *
298 * @param[in,out] first First sibling to search in, is updated if needed.
299 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200300 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200301 * @return LY_ERR value.
302 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100303static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200304lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100305{
306 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
307 struct lyd_node *match, *to_del;
Radek Krejci857189e2020-09-01 13:26:36 +0200308 ly_bool found;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100309
310 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
311 found = 0;
312 iter = NULL;
313 match = NULL;
314 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
315 if (match->flags & LYD_NEW) {
316 /* a new case data found, nothing more to look for */
317 found = 2;
318 break;
319 } else {
320 /* and old case data found */
321 if (found == 0) {
322 found = 1;
323 }
324 }
325 }
326
327 if (found == 1) {
328 /* there should not be 2 old cases */
329 if (old_case) {
330 /* old data from 2 cases */
331 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
332 return LY_EVALID;
333 }
334
335 /* remember an old existing case */
336 old_case = scase;
337 } else if (found == 2) {
338 if (new_case) {
339 /* new data from 2 cases */
340 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
341 return LY_EVALID;
342 }
343
344 /* remember a new existing case */
345 new_case = scase;
346 }
347 }
348
349 if (old_case && new_case) {
350 /* auto-delete old case */
351 iter = NULL;
352 match = NULL;
353 to_del = NULL;
354 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
355 if (LYD_DEL_IS_ROOT(*first, to_del)) {
356 *first = (*first)->next;
357 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200358 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100359 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200360 if (diff) {
361 /* add into diff */
362 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
363 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100364 to_del = match;
365 }
366 if (LYD_DEL_IS_ROOT(*first, to_del)) {
367 *first = (*first)->next;
368 }
369 lyd_free_tree(to_del);
370 }
371
372 return LY_SUCCESS;
373}
374
Michal Vaskobb844672020-07-03 11:06:12 +0200375/**
376 * @brief Check whether a schema node can have some default values (true for NP containers as well).
377 *
378 * @param[in] schema Schema node to check.
379 * @return non-zero if yes,
380 * @return 0 otherwise.
381 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100382static int
383lyd_val_has_default(const struct lysc_node *schema)
384{
385 switch (schema->nodetype) {
386 case LYS_LEAF:
387 if (((struct lysc_node_leaf *)schema)->dflt) {
388 return 1;
389 }
390 break;
391 case LYS_LEAFLIST:
392 if (((struct lysc_node_leaflist *)schema)->dflts) {
393 return 1;
394 }
395 break;
396 case LYS_CONTAINER:
397 if (!(schema->flags & LYS_PRESENCE)) {
398 return 1;
399 }
400 break;
401 default:
402 break;
403 }
404
405 return 0;
406}
407
Michal Vaskobb844672020-07-03 11:06:12 +0200408/**
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100409 * @brief Properly delete a node as part of autodelete validation tasks.
410 *
411 * @param[in,out] first First sibling, is updated if needed.
412 * @param[in] node Node instance to delete.
413 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
414 * @param[in,out] diff Validation diff.
415 */
416static void
417lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p,
418 struct lyd_node **diff)
419{
420 struct lyd_node *iter;
421
422 if (LYD_DEL_IS_ROOT(*first, node)) {
423 *first = (*first)->next;
424 }
425 if (node == *next_p) {
426 *next_p = (*next_p)->next;
427 }
428 if (diff) {
429 /* add into diff */
430 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
431 /* we do not want to track NP container changes, but remember any removed children */
432 LY_LIST_FOR(lyd_child(node), iter) {
433 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
434 }
435 } else {
436 lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff);
437 }
438 }
439 lyd_free_tree(node);
440}
441
442/**
Michal Vaskobb844672020-07-03 11:06:12 +0200443 * @brief Autodelete old instances to prevent validation errors.
444 *
445 * @param[in,out] first First sibling to search in, is updated if needed.
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100446 * @param[in] node New data node instance to check.
Michal Vaskobb844672020-07-03 11:06:12 +0200447 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200448 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200449 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100450static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200451lyd_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 +0100452{
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100453 struct lyd_node *match, *next;
454
455 assert(node->flags & LYD_NEW);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100456
457 if (lyd_val_has_default(node->schema)) {
458 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200459 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100460 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
461 /* default instance found, remove it */
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100462 lyd_validate_autodel_node_del(first, match, next_p, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100463
464 /* remove only a single container/leaf default instance, if there are more, it is an error */
465 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
466 break;
467 }
468 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100469 }
470 }
471}
472
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100473/**
474 * @brief Autodelete leftover default nodes of deleted cases (that have no existing explicit data).
475 *
476 * @param[in,out] first First sibling to search in, is updated if needed.
477 * @param[in] node Default data node instance to check.
478 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
479 * @param[in,out] diff Validation diff.
480 */
481static void
482lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p,
483 struct lyd_node **diff)
484{
485 struct lysc_node_choice *choic;
486 struct lyd_node *iter = NULL;
487 const struct lysc_node *slast = NULL;
488
489 assert(node->flags & LYD_DEFAULT);
490
491 if (!node->schema->parent || (node->schema->parent->nodetype != LYS_CASE)) {
492 /* the default node is not a descendant of a case */
493 return;
494 }
495
496 choic = (struct lysc_node_choice *)node->schema->parent->parent;
497 assert(choic->nodetype == LYS_CHOICE);
498
499 if (choic->dflt && (choic->dflt == (struct lysc_node_case *)node->schema->parent)) {
500 /* data of a default case, keep them */
501 return;
502 }
503
504 /* try to find an explicit node of the case */
505 while ((iter = lys_getnext_data(iter, *first, &slast, node->schema->parent, NULL))) {
506 if (!(iter->flags & LYD_DEFAULT)) {
507 break;
508 }
509 }
510
511 if (!iter) {
512 /* there are only default nodes of the case meaning it does not exist and neither should any default nodes
513 * of the case, remove this one default node */
514 lyd_validate_autodel_node_del(first, node, next_p, diff);
515 }
516}
517
Michal Vaskob1b5c262020-03-05 14:29:47 +0100518LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200519lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200520 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100521{
522 struct lyd_node *next, *node;
523 const struct lysc_node *snode = NULL;
524
525 assert(first && (sparent || mod));
526
527 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
528 /* check case duplicites */
529 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200530 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100531 }
532 }
533
534 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100535 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100536 /* all top-level data from this module checked */
537 break;
538 }
539
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100540 if (!(node->flags & (LYD_NEW | LYD_DEFAULT))) {
541 /* check only new and default nodes */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100542 continue;
543 }
544
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100545 if (node->flags & LYD_NEW) {
546 /* remove old default(s) of the new node if it exists */
547 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100548
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100549 /* then check new node instance duplicities */
550 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100551
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100552 /* this node is valid */
553 node->flags &= ~LYD_NEW;
554 }
555
556 if (node->flags & LYD_DEFAULT) {
557 /* remove leftover default nodes from a no-longer existing case */
558 lyd_validate_autodel_case_dflt(first, node, &next, diff);
559 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100560 }
561
562 return LY_SUCCESS;
563}
564
Michal Vaskobb844672020-07-03 11:06:12 +0200565/**
566 * @brief Validate mandatory node existence.
567 *
568 * @param[in] first First sibling to search in.
569 * @param[in] snode Schema node to validate.
570 * @return LY_ERR value.
571 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100572static LY_ERR
573lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100574{
Michal Vaskoa3881362020-01-21 15:57:35 +0100575 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100576 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100577 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100578 return LY_SUCCESS;
579 }
580 } else {
581 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100582
Michal Vaskob1b5c262020-03-05 14:29:47 +0100583 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100584 /* data instance found */
585 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100586 }
587 }
588
589 /* node instance not found */
590 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
591 return LY_EVALID;
592}
593
Michal Vaskobb844672020-07-03 11:06:12 +0200594/**
595 * @brief Validate min/max-elements constraints, if any.
596 *
597 * @param[in] first First sibling to search in.
598 * @param[in] snode Schema node to validate.
599 * @param[in] min Minimum number of elements, 0 for no restriction.
600 * @param[in] max Max number of elements, 0 for no restriction.
601 * @return LY_ERR value.
602 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100603static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100604lyd_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 +0100605{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100606 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200607 struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100608
Michal Vasko9b368d32020-02-14 13:53:31 +0100609 assert(min || max);
610
Michal Vasko4c583e82020-07-17 12:16:14 +0200611 LYD_LIST_FOR_INST(first, snode, iter) {
612 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100613
Michal Vasko4c583e82020-07-17 12:16:14 +0200614 if (min && (count == min)) {
615 /* satisfied */
616 min = 0;
617 if (!max) {
618 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100619 break;
620 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100621 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200622 if (max && (count > max)) {
623 /* not satisifed */
624 break;
625 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100626 }
627
Michal Vasko9b368d32020-02-14 13:53:31 +0100628 if (min) {
629 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100630 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
631 return LY_EVALID;
632 } else if (max && (count > max)) {
633 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
634 return LY_EVALID;
635 }
636
Michal Vaskoa3881362020-01-21 15:57:35 +0100637 return LY_SUCCESS;
638}
639
Michal Vaskobb844672020-07-03 11:06:12 +0200640/**
641 * @brief Find node referenced by a list unique statement.
642 *
643 * @param[in] uniq_leaf Unique leaf to find.
644 * @param[in] list List instance to use for the search.
645 * @return Found leaf,
646 * @return NULL if no leaf found.
647 */
Michal Vasko14654712020-02-06 08:35:21 +0100648static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200649lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100650{
Michal Vasko9b368d32020-02-14 13:53:31 +0100651 struct lyd_node *node;
652 const struct lysc_node *iter;
653 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100654
Michal Vasko9b368d32020-02-14 13:53:31 +0100655 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200656 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
657 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100658 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100659
Michal Vaskobb844672020-07-03 11:06:12 +0200660 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100661 while (node && depth) {
662 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200663 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
664 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100665 }
666
667 /* find iter instance in children */
668 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Radek Krejcia1c1e542020-09-29 16:06:52 +0200669 lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100670 --depth;
671 }
672
Michal Vasko14654712020-02-06 08:35:21 +0100673 return node;
674}
675
Michal Vaskobb844672020-07-03 11:06:12 +0200676/**
677 * @brief Callback for comparing 2 list unique leaf values.
678 *
Radek Krejci857189e2020-09-01 13:26:36 +0200679 * Implementation of ::values_equal_cb.
680 *
Michal Vaskobb844672020-07-03 11:06:12 +0200681 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100682 */
Radek Krejci857189e2020-09-01 13:26:36 +0200683static ly_bool
684lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Michal Vasko14654712020-02-06 08:35:21 +0100685{
686 struct ly_ctx *ctx;
687 struct lysc_node_list *slist;
688 struct lyd_node *diter, *first, *second;
689 struct lyd_value *val1, *val2;
690 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200691 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100692
693 assert(val1_p && val2_p);
694
695 first = *((struct lyd_node **)val1_p);
696 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200697 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100698
699 assert(first && (first->schema->nodetype == LYS_LIST));
700 assert(second && (second->schema == first->schema));
701
702 ctx = first->schema->module->ctx;
703
704 slist = (struct lysc_node_list *)first->schema;
705
706 /* compare unique leaves */
707 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200708 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200709 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100710 goto uniquecheck;
711 }
712 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200713 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100714uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200715 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100716 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200717 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100718 if (diter) {
719 val1 = &((struct lyd_node_term *)diter)->value;
720 } else {
721 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200722 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100723 }
724
725 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200726 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100727 if (diter) {
728 val2 = &((struct lyd_node_term *)diter)->value;
729 } else {
730 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200731 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100732 }
733
734 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
735 /* values differ or either one is not set */
736 break;
737 }
738 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200739 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100740 /* all unique leafs are the same in this set, create this nice error */
741 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
742 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
743
744 /* use buffer to rebuild the unique string */
745 uniq_str = malloc(1024);
746 uniq_str[0] = '\0';
747 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200748 LY_ARRAY_FOR(slist->uniques[u], v) {
749 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100750 strcpy(ptr, " ");
751 ++ptr;
752 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200753 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 +0200754 ptr, 1024 - (ptr - uniq_str));
Michal Vasko14654712020-02-06 08:35:21 +0100755 if (!ptr) {
756 /* path will be incomplete, whatever */
757 break;
758 }
759
760 ptr += strlen(ptr);
761 }
762 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
763
764 free(path1);
765 free(path2);
766 free(uniq_str);
767 return 1;
768 }
769
770 if (action > 0) {
771 /* done */
772 return 0;
773 }
774 }
775
776 return 0;
777}
778
Michal Vaskobb844672020-07-03 11:06:12 +0200779/**
780 * @brief Validate list unique leaves.
781 *
782 * @param[in] first First sibling to search in.
783 * @param[in] snode Schema node to validate.
784 * @param[in] uniques List unique arrays to validate.
785 * @return LY_ERR value.
786 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100787static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200788lyd_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 +0100789{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100790 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100791 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200792 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100793 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200794 uint32_t hash, i, size = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200795 ly_bool dynamic;
Michal Vasko14654712020-02-06 08:35:21 +0100796 const char *str;
797 struct hash_table **uniqtables = NULL;
798 struct lyd_value *val;
799 struct ly_ctx *ctx = snode->module->ctx;
800
801 assert(uniques);
802
803 /* get all list instances */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200804 LY_CHECK_RET(ly_set_new(&set));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100805 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100806 if (diter->schema == snode) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200807 ret = ly_set_add(set, (void *)diter, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200808 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko9b368d32020-02-14 13:53:31 +0100809 }
810 }
Michal Vasko14654712020-02-06 08:35:21 +0100811
812 if (set->count == 2) {
813 /* simple comparison */
814 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
815 /* instance duplication */
816 ret = LY_EVALID;
817 goto cleanup;
818 }
819 } else if (set->count > 2) {
820 /* use hashes for comparison */
821 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200822 for (i = 31; i > 0; i--) {
823 size = set->count << i;
824 size = size >> i;
825 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100826 break;
827 }
828 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200829 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
830 i = 32 - i;
831 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100832
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200833 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100834 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200835 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200836 for (v = 0; v < x; v++) {
837 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
838 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100839 }
840
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200841 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100842 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200843 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100844 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200845 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200846 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100847 if (diter) {
848 val = &((struct lyd_node_term *)diter)->value;
849 } else {
850 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200851 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100852 }
853 if (!val) {
854 /* unique item not present nor has default value */
855 break;
856 }
857
858 /* get canonical string value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200859 str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +0100860 hash = dict_hash_multi(hash, str, strlen(str));
861 if (dynamic) {
862 free((char *)str);
863 }
864 }
865 if (!val) {
866 /* skip this list instance since its unique set is incomplete */
867 continue;
868 }
869
870 /* finish the hash value */
871 hash = dict_hash_multi(hash, NULL, 0);
872
873 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200874 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100875 if (ret == LY_EEXIST) {
876 /* instance duplication */
877 ret = LY_EVALID;
878 }
879 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
880 }
881 }
882 }
883
884cleanup:
885 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200886 for (v = 0; v < x; v++) {
887 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100888 /* failed when allocating uniquetables[j], following j are not allocated */
889 break;
890 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200891 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100892 }
893 free(uniqtables);
894
895 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100896}
897
Michal Vaskobb844672020-07-03 11:06:12 +0200898/**
899 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
900 *
901 * @param[in] first First sibling to search in.
902 * @param[in] sparent Schema parent of the nodes to check.
903 * @param[in] mod Module of the nodes to check.
904 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
905 * @param[in] op Operation being validated, if any.
906 * @return LY_ERR value.
907 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100908static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100909lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200910 const struct lysc_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100911{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100912 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100913 struct lysc_node_list *slist;
Michal Vaskod8958df2020-08-05 13:27:36 +0200914 struct lysc_node_leaflist *sllist;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200915 uint32_t getnext_opts;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200916
Radek Krejci7931b192020-06-25 17:05:03 +0200917 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100918
Michal Vaskoa3881362020-01-21 15:57:35 +0100919 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200920 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200921 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100922 continue;
923 }
924
Michal Vaskoa3881362020-01-21 15:57:35 +0100925 /* check min-elements and max-elements */
Michal Vaskod8958df2020-08-05 13:27:36 +0200926 if (snode->nodetype == LYS_LIST) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100927 slist = (struct lysc_node_list *)snode;
928 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100929 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100930 }
Michal Vaskod8958df2020-08-05 13:27:36 +0200931 } else if (snode->nodetype == LYS_LEAFLIST) {
932 sllist = (struct lysc_node_leaflist *)snode;
933 if (sllist->min || sllist->max) {
934 LY_CHECK_RET(lyd_validate_minmax(first, snode, sllist->min, sllist->max));
935 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100936
Michal Vaskoacd83e72020-02-04 14:12:01 +0100937 } else if (snode->flags & LYS_MAND_TRUE) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200938 /* check generic mandatory existence */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100939 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100940 }
941
942 /* check unique */
943 if (snode->nodetype == LYS_LIST) {
944 slist = (struct lysc_node_list *)snode;
945 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200946 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100947 }
948 }
949
Michal Vaskoacd83e72020-02-04 14:12:01 +0100950 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
951 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200952 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100953 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100954 }
955
Michal Vaskoacd83e72020-02-04 14:12:01 +0100956 return LY_SUCCESS;
957}
958
Michal Vaskobb844672020-07-03 11:06:12 +0200959/**
960 * @brief Validate obsolete nodes, only warnings are printed.
961 *
962 * @param[in] node Node to check.
963 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100964static void
965lyd_validate_obsolete(const struct lyd_node *node)
966{
967 const struct lysc_node *snode;
968
969 snode = node->schema;
970 do {
971 if (snode->flags & LYS_STATUS_OBSLT) {
972 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
973 break;
974 }
975
976 snode = snode->parent;
977 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
978}
979
Michal Vaskobb844672020-07-03 11:06:12 +0200980/**
981 * @brief Validate must conditions of a data node.
982 *
983 * @param[in] node Node to validate.
984 * @param[in] op Operation being validated, if any.
985 * @return LY_ERR value.
986 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100987static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200988lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100989{
990 struct lyxp_set xp_set;
991 struct lysc_must *musts;
992 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200993 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100994
995 switch (node->schema->nodetype) {
996 case LYS_CONTAINER:
997 musts = ((struct lysc_node_container *)node->schema)->musts;
998 break;
999 case LYS_LEAF:
1000 musts = ((struct lysc_node_leaf *)node->schema)->musts;
1001 break;
1002 case LYS_LEAFLIST:
1003 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
1004 break;
1005 case LYS_LIST:
1006 musts = ((struct lysc_node_list *)node->schema)->musts;
1007 break;
1008 case LYS_ANYXML:
1009 case LYS_ANYDATA:
1010 musts = ((struct lysc_node_anydata *)node->schema)->musts;
1011 break;
1012 case LYS_NOTIF:
1013 musts = ((struct lysc_notif *)node->schema)->musts;
1014 break;
1015 case LYS_RPC:
1016 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +02001017 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001018 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +02001019 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001020 musts = ((struct lysc_action *)node->schema)->output.musts;
1021 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001022 LOGINT(LYD_CTX(node));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001023 return LY_EINT;
1024 }
Michal Vaskocc048b22020-03-27 15:52:38 +01001025 break;
1026 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001027 LOGINT(LYD_CTX(node));
Michal Vaskocc048b22020-03-27 15:52:38 +01001028 return LY_EINT;
1029 }
1030
1031 if (!musts) {
1032 /* no must to evaluate */
1033 return LY_SUCCESS;
1034 }
1035
1036 /* find first top-level node */
Radek Krejci1e008d22020-08-17 11:37:37 +02001037 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent) {}
Michal Vaskocc048b22020-03-27 15:52:38 +01001038 while (tree->prev->next) {
1039 tree = tree->prev;
1040 }
1041
1042 LY_ARRAY_FOR(musts, u) {
1043 memset(&xp_set, 0, sizeof xp_set);
1044
1045 /* evaluate must */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001046 LY_CHECK_RET(lyxp_eval(musts[u].cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes, node,
1047 tree, &xp_set, LYXP_SCHEMA));
Michal Vaskocc048b22020-03-27 15:52:38 +01001048
1049 /* check the result */
1050 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02001051 if (!xp_set.val.bln) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001052 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
Michal Vaskocc048b22020-03-27 15:52:38 +01001053 return LY_EVALID;
1054 }
1055 }
1056
1057 return LY_SUCCESS;
1058}
1059
Michal Vaskob1b5c262020-03-05 14:29:47 +01001060LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001061lyd_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 +02001062 LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +01001063{
Radek Krejci7f769d72020-07-11 23:13:56 +02001064 struct lyd_node *next = NULL, *node;
Michal Vaskoacd83e72020-02-04 14:12:01 +01001065
Michal Vasko14654712020-02-06 08:35:21 +01001066 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001067 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001068 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001069 /* all top-level data from this module checked */
1070 break;
Michal Vaskof03ed032020-03-04 13:31:44 +01001071 }
1072
Michal Vaskoa8c61722020-03-27 16:59:32 +01001073 /* opaque data */
1074 if (!node->schema) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001075 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
Radek Krejci0f969882020-08-21 16:56:47 +02001076 ((struct lyd_node_opaq *)node)->name);
Michal Vaskoa8c61722020-03-27 16:59:32 +01001077 return LY_EVALID;
1078 }
1079
Michal Vaskocb7526d2020-03-30 15:08:26 +02001080 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +02001081 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001082 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001083 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001084 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001085 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001086 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001087 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001088 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +01001089 return LY_EVALID;
1090 }
1091
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001092 /* obsolete data */
1093 lyd_validate_obsolete(node);
1094
Michal Vaskocc048b22020-03-27 15:52:38 +01001095 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001096 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001097
Michal Vasko53d97a12020-11-05 17:39:10 +01001098 /* node value was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +01001099 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001100
Michal Vasko14654712020-02-06 08:35:21 +01001101 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +02001102 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001103
Michal Vaskob1b5c262020-03-05 14:29:47 +01001104 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001105 /* validate all children recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001106 LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node->schema, NULL, val_opts, op));
Michal Vaskocde73ac2019-11-14 16:10:27 +01001107
Michal Vaskob1b5c262020-03-05 14:29:47 +01001108 /* set default for containers */
1109 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Radek Krejcia1c1e542020-09-29 16:06:52 +02001110 LY_LIST_FOR(lyd_child(node), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001111 if (!(next->flags & LYD_DEFAULT)) {
1112 break;
1113 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001114 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001115 if (!next) {
1116 node->flags |= LYD_DEFAULT;
1117 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001118 }
1119 }
1120
1121 return LY_SUCCESS;
1122}
1123
Radek Krejci7931b192020-06-25 17:05:03 +02001124/**
Michal Vaskobb844672020-07-03 11:06:12 +02001125 * @brief Validate the whole data subtree.
1126 *
1127 * @param[in] root Subtree root.
1128 * @param[in,out] type_check Set for unres node types.
1129 * @param[in,out] type_meta_check Set for unres metadata types.
1130 * @param[in,out] when_check Set for nodes with when conditions.
1131 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001132 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001133 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001134 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001135static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001136lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001137 struct ly_set *when_check, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001138{
1139 const struct lyd_meta *meta;
Michal Vasko56daf732020-08-10 10:57:18 +02001140 struct lyd_node *node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001141
Michal Vasko56daf732020-08-10 10:57:18 +02001142 LYD_TREE_DFS_BEGIN(root, node) {
Michal Vasko0275cf62020-11-05 17:40:30 +01001143 LY_LIST_FOR(node->meta, meta) {
1144 if (((struct lyext_metadata *)meta->annotation->data)->type->plugin->validate) {
1145 /* metadata type resolution */
1146 LY_CHECK_RET(ly_set_add(type_meta_check, (void *)meta, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001147 }
Michal Vasko0275cf62020-11-05 17:40:30 +01001148 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001149
Michal Vasko0275cf62020-11-05 17:40:30 +01001150 if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
1151 /* node type resolution */
1152 LY_CHECK_RET(ly_set_add(type_check, (void *)node, 1, NULL));
1153 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1154 /* new node validation, autodelete */
1155 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 +02001156
Michal Vasko0275cf62020-11-05 17:40:30 +01001157 /* add nested defaults */
1158 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
1159 when_check, val_opts & LYD_VALIDATE_NO_STATE ? LYD_IMPLICIT_NO_STATE : 0, diff));
1160 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001161
Michal Vasko0275cf62020-11-05 17:40:30 +01001162 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1163 /* when evaluation */
1164 LY_CHECK_RET(ly_set_add(when_check, (void *)node, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001165 }
1166
Michal Vasko56daf732020-08-10 10:57:18 +02001167 LYD_TREE_DFS_END(root, node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001168 }
1169
1170 return LY_SUCCESS;
1171}
1172
Michal Vaskobb844672020-07-03 11:06:12 +02001173/**
1174 * @brief Validate data tree.
1175 *
1176 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1177 * @param[in] modules Array of modules to validate, NULL for all modules.
1178 * @param[in] mod_count Count of @p modules.
1179 * @param[in] ly_ctx libyang context.
1180 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001181 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001182 * @return LY_ERR value.
1183 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001184static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001185lyd_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 +02001186 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001187{
1188 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001189 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001190 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001191 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001192 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001193
Michal Vasko26e80012020-07-08 10:55:46 +02001194 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001195 if (diff) {
1196 *diff = NULL;
1197 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001198
Michal Vaskob1b5c262020-03-05 14:29:47 +01001199 next = *tree;
1200 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001201 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001202 mod = lyd_data_next_module(&next, &first);
1203 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001204 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001205 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001206 if (!mod) {
1207 break;
1208 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001209 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001210 /* make sure first2 changes are carried to tree */
1211 first2 = tree;
1212 } else {
1213 first2 = &first;
1214 }
1215
1216 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001217 ret = lyd_validate_new(first2, NULL, mod, diff);
1218 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001219
Michal Vasko0275cf62020-11-05 17:40:30 +01001220 /* add all top-level defaults for this module, do not add into unres sets, will occur in the next step */
1221 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, NULL, NULL, val_opts & LYD_VALIDATE_NO_STATE ?
Michal Vasko69730152020-10-09 16:30:07 +02001222 LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001223 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001224
1225 /* process nested nodes */
1226 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001227 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1228 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001229 }
1230
1231 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001232 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001233 LY_CHECK_GOTO(ret, cleanup);
1234
1235 /* perform final validation that assumes the data tree is final */
Michal Vasko8104fd42020-07-13 11:09:51 +02001236 ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0);
1237 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001238 }
1239
Michal Vaskof03ed032020-03-04 13:31:44 +01001240cleanup:
1241 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001242 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001243 ly_set_erase(&when_check, NULL);
1244 return ret;
1245}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001246
1247API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001248lyd_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 +01001249{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001250 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001251}
1252
1253API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001254lyd_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 +01001255{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001256 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001257}
Michal Vaskofea12c62020-03-30 11:00:15 +02001258
Michal Vaskobb844672020-07-03 11:06:12 +02001259/**
1260 * @brief Find nodes for merging an operation into data tree for validation.
1261 *
1262 * @param[in] op_tree Full operation data tree.
1263 * @param[in] op_node Operation node itself.
1264 * @param[in] tree Data tree to be merged into.
1265 * @param[out] op_subtree Operation subtree to merge.
1266 * @param[out] tree_sibling Data tree sibling to merge next to.
1267 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001268static void
Michal Vaskobb844672020-07-03 11:06:12 +02001269lyd_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 +02001270 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001271{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001272 const struct lyd_node *tree_iter, *op_iter;
1273 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001274 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001275
Michal Vaskocb7526d2020-03-30 15:08:26 +02001276 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001277 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001278 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001279 ++op_depth;
1280 }
1281
1282 /* find where to merge op */
1283 tree_iter = tree;
1284 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001285 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001286 while (cur_depth) {
1287 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001288 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001289 for (i = 0; i < cur_depth; ++i) {
1290 op_iter = (struct lyd_node *)op_iter->parent;
1291 }
1292
1293 /* find op iter in tree */
1294 lyd_find_sibling_first(tree_iter, op_iter, &match);
1295 if (!match) {
1296 break;
1297 }
1298
1299 /* move tree_iter */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001300 tree_iter = lyd_child(match);
Michal Vaskofea12c62020-03-30 11:00:15 +02001301
1302 /* move depth */
1303 --cur_depth;
1304 }
1305
Michal Vaskobb844672020-07-03 11:06:12 +02001306 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001307 *tree_sibling = (struct lyd_node *)tree_iter;
1308}
1309
1310API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001311lyd_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 +02001312{
1313 LY_ERR ret;
Michal Vasko56daf732020-08-10 10:57:18 +02001314 struct lyd_node *tree_sibling, *op_subtree, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001315 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1316
1317 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci0f969882020-08-21 16:56:47 +02001318 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001319 if (diff) {
1320 *diff = NULL;
1321 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001322
1323 /* find the operation/notification */
Michal Vasko56daf732020-08-10 10:57:18 +02001324 LYD_TREE_DFS_BEGIN(op_tree, op_node) {
Michal Vasko69730152020-10-09 16:30:07 +02001325 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 +02001326 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001327 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001328 break;
1329 }
Michal Vasko56daf732020-08-10 10:57:18 +02001330 LYD_TREE_DFS_END(op_tree, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001331 }
Michal Vasko69730152020-10-09 16:30:07 +02001332 if ((op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY)) {
Radek Krejci7931b192020-06-25 17:05:03 +02001333 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001334 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001335 return LY_EINVAL;
1336 }
1337 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001338 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001339 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001340 return LY_EINVAL;
1341 }
1342 }
1343
1344 /* move op_tree to top-level node */
1345 while (op_tree->parent) {
1346 op_tree = (struct lyd_node *)op_tree->parent;
1347 }
1348
1349 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001350 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1351 op_parent = (struct lyd_node *)op_subtree->parent;
1352 lyd_unlink_tree(op_subtree);
1353 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001354 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001355 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001356 }
1357
1358 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001359 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 +02001360
1361 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1362 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 +02001363 diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001364
1365 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001366 lyd_validate_obsolete(op_node);
Radek Krejci7931b192020-06-25 17:05:03 +02001367 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001368
1369 /* final validation of all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001370 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 +02001371
1372cleanup:
1373 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001374 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001375 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001376 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001377 }
1378
1379 ly_set_erase(&type_check, NULL);
1380 ly_set_erase(&type_meta_check, NULL);
1381 ly_set_erase(&when_check, NULL);
1382 return ret;
1383}