blob: c59f012da26f146875b4509ecc8e950ac899a7fd [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 */
Radek Krejcif8dc59a2020-11-25 13:47:44 +010014#define _POSIX_C_SOURCE 200809L /* strdup */
Michal Vasko81bc5512020-11-13 18:05:18 +010015
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 Vaskobd4db892020-11-23 16:58:20 +010093 * @brief Evaluate all relevant "when" conditions of a node.
Michal Vaskocde73ac2019-11-14 16:10:27 +010094 *
Michal Vaskobd4db892020-11-23 16:58:20 +010095 * @param[in] tree Data tree.
96 * @param[in] node Node whose relevant when conditions will be evaluated.
97 * @param[in] schema Schema node of @p node. It may not be possible to use directly if @p node is opaque.
98 * @param[out] disabled First when that evaluated false, if any.
99 * @return LY_SUCCESS on success.
100 * @return LY_EINCOMPLETE if a referenced node does not have its when evaluated.
101 * @return LY_ERR value on error.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100102 */
103static LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +0100104lyd_validate_node_when(const struct lyd_node *tree, const struct lyd_node *node, const struct lysc_node *schema,
105 const struct lysc_when **disabled)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100106{
Michal Vasko8104fd42020-07-13 11:09:51 +0200107 LY_ERR ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100108 const struct lyd_node *ctx_node;
Michal Vaskobd4db892020-11-23 16:58:20 +0100109 const struct lysc_when *when;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100110 struct lyxp_set xp_set;
Michal Vaskobd4db892020-11-23 16:58:20 +0100111 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100112
Michal Vaskobd4db892020-11-23 16:58:20 +0100113 assert(!node->schema || (node->schema == schema));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100114
Michal Vaskobd4db892020-11-23 16:58:20 +0100115 *disabled = NULL;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100116
Michal Vaskobd4db892020-11-23 16:58:20 +0100117 do {
118 LY_ARRAY_FOR(schema->when, u) {
119 when = schema->when[u];
Michal Vaskocde73ac2019-11-14 16:10:27 +0100120
Michal Vaskobd4db892020-11-23 16:58:20 +0100121 /* get context node */
122 if (when->context == schema) {
123 ctx_node = node;
124 } else {
125 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
126 ctx_node = (struct lyd_node *)node->parent;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100127 }
Michal Vaskobd4db892020-11-23 16:58:20 +0100128
129 /* evaluate when */
130 memset(&xp_set, 0, sizeof xp_set);
131 ret = lyxp_eval(when->cond, schema->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes, ctx_node, tree,
132 &xp_set, LYXP_SCHEMA);
133 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
134
135 /* return error or LY_EINCOMPLETE for dependant unresolved when */
136 LY_CHECK_RET(ret);
137
138 if (!xp_set.val.bln) {
139 /* false when */
140 *disabled = when;
141 return LY_SUCCESS;
Michal Vasko8104fd42020-07-13 11:09:51 +0200142 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100143 }
Michal Vaskobd4db892020-11-23 16:58:20 +0100144
145 schema = schema->parent;
146 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
147
148 return LY_SUCCESS;
149}
150
151/**
152 * @brief Evaluate when conditions of collected unres nodes.
153 *
154 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
155 * @param[in] node_when Set with nodes with "when" conditions.
156 * @param[in,out] diff Validation diff.
157 * @return LY_SUCCESS on success.
158 * @return LY_ERR value on error.
159 */
160static LY_ERR
161lyd_validate_unres_when(struct lyd_node **tree, struct ly_set *node_when, struct lyd_node **diff)
162{
163 LY_ERR ret;
164 uint32_t i;
165 const struct lysc_when *disabled;
166 struct lyd_node *node;
167
168 if (!node_when->count) {
169 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100170 }
171
Michal Vaskobd4db892020-11-23 16:58:20 +0100172 i = node_when->count;
173 do {
174 --i;
175 node = node_when->dnodes[i];
176
177 /* evaluate all when expressions that affect this node's existence */
178 ret = lyd_validate_node_when(*tree, node, node->schema, &disabled);
179 if (!ret) {
180 if (disabled) {
181 /* when false */
182 if (node->flags & LYD_WHEN_TRUE) {
183 /* autodelete */
184 if (LYD_DEL_IS_ROOT(*tree, node)) {
185 *tree = (*tree)->next;
186 }
187 if (diff) {
188 /* add into diff */
189 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff));
190 }
191 lyd_free_tree(node);
192 } else {
193 /* invalid data */
194 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, disabled->cond->expr);
195 return LY_EVALID;
196 }
197 } else {
198 /* when true */
199 node->flags |= LYD_WHEN_TRUE;
200 }
201
202 /* remove this node from the set, its when was resolved */
203 ly_set_rm_index(node_when, i, NULL);
204 } else if (ret != LY_EINCOMPLETE) {
205 /* error */
206 return ret;
207 }
208 } while (i);
209
210 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100211}
212
213LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100214lyd_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 +0200215 struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100216{
217 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200218 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100219
Michal Vaskob1b5c262020-03-05 14:29:47 +0100220 if (node_when) {
221 /* evaluate all when conditions */
222 uint32_t prev_count;
223 do {
224 prev_count = node_when->count;
Michal Vaskobd4db892020-11-23 16:58:20 +0100225 LY_CHECK_RET(lyd_validate_unres_when(tree, node_when, diff));
Radek Krejci0f969882020-08-21 16:56:47 +0200226 /* there must have been some when conditions resolved */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100227 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100228
Michal Vaskob1b5c262020-03-05 14:29:47 +0100229 /* there could have been no cyclic when dependencies, checked during compilation */
230 assert(!node_when->count);
231 }
232
233 if (node_types && node_types->count) {
234 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200235 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100236 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200237 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100238
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200239 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200240 struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100241
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200242 /* resolve the value of the node */
243 ret = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, (struct lyd_node *)node, *tree,
244 LY_VLOG_LYD, node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100245 LY_CHECK_RET(ret);
246
247 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200248 ly_set_rm_index(node_types, i, NULL);
249 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100250 }
251
Michal Vasko9f96a052020-03-10 09:41:45 +0100252 if (meta_types && meta_types->count) {
253 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200254 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100255 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200256 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100257
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200258 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200259 struct lysc_type *type = ((struct lyext_metadata *)meta->annotation->data)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100260
Michal Vasko9f96a052020-03-10 09:41:45 +0100261 /* validate and store the value of the metadata */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200262 ret = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree,
263 LY_VLOG_NONE, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100264 LY_CHECK_RET(ret);
265
266 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200267 ly_set_rm_index(meta_types, i, NULL);
268 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100269 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100270
271 return ret;
272}
273
Michal Vaskobb844672020-07-03 11:06:12 +0200274/**
275 * @brief Validate instance duplication.
276 *
277 * @param[in] first First sibling to search in.
278 * @param[in] node Data node instance to check.
279 * @return LY_ERR value.
280 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100281static LY_ERR
282lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100283{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100284 struct lyd_node **match_p;
Radek Krejci857189e2020-09-01 13:26:36 +0200285 ly_bool fail = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100286
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100287 assert(node->flags & LYD_NEW);
288
Michal Vaskob1b5c262020-03-05 14:29:47 +0100289 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
290 /* duplicate instances allowed */
291 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100292 }
293
Michal Vaskob1b5c262020-03-05 14:29:47 +0100294 /* find exactly the same next instance using hashes if possible */
295 if (node->parent && node->parent->children_ht) {
296 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
297 fail = 1;
298 }
299 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200300 for ( ; first; first = first->next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100301 if (first == node) {
302 continue;
303 }
304
305 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
306 if (first->schema == node->schema) {
307 fail = 1;
308 break;
309 }
Michal Vasko8f359bf2020-07-28 10:41:15 +0200310 } else if (!lyd_compare_single(first, node, 0)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100311 fail = 1;
312 break;
313 }
314 }
315 }
316
317 if (fail) {
318 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
319 return LY_EVALID;
320 }
321 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100322}
323
Michal Vaskobb844672020-07-03 11:06:12 +0200324/**
325 * @brief Validate multiple case data existence with possible autodelete.
326 *
327 * @param[in,out] first First sibling to search in, is updated if needed.
328 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200329 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200330 * @return LY_ERR value.
331 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100332static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200333lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100334{
335 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
336 struct lyd_node *match, *to_del;
Radek Krejci857189e2020-09-01 13:26:36 +0200337 ly_bool found;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100338
339 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
340 found = 0;
341 iter = NULL;
342 match = NULL;
343 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
344 if (match->flags & LYD_NEW) {
345 /* a new case data found, nothing more to look for */
346 found = 2;
347 break;
348 } else {
349 /* and old case data found */
350 if (found == 0) {
351 found = 1;
352 }
353 }
354 }
355
356 if (found == 1) {
357 /* there should not be 2 old cases */
358 if (old_case) {
359 /* old data from 2 cases */
360 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
361 return LY_EVALID;
362 }
363
364 /* remember an old existing case */
365 old_case = scase;
366 } else if (found == 2) {
367 if (new_case) {
368 /* new data from 2 cases */
369 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
370 return LY_EVALID;
371 }
372
373 /* remember a new existing case */
374 new_case = scase;
375 }
376 }
377
378 if (old_case && new_case) {
379 /* auto-delete old case */
380 iter = NULL;
381 match = NULL;
382 to_del = NULL;
383 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
384 if (LYD_DEL_IS_ROOT(*first, to_del)) {
385 *first = (*first)->next;
386 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200387 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100388 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200389 if (diff) {
390 /* add into diff */
391 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
392 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100393 to_del = match;
394 }
395 if (LYD_DEL_IS_ROOT(*first, to_del)) {
396 *first = (*first)->next;
397 }
398 lyd_free_tree(to_del);
399 }
400
401 return LY_SUCCESS;
402}
403
Michal Vaskobb844672020-07-03 11:06:12 +0200404/**
405 * @brief Check whether a schema node can have some default values (true for NP containers as well).
406 *
407 * @param[in] schema Schema node to check.
408 * @return non-zero if yes,
409 * @return 0 otherwise.
410 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100411static int
412lyd_val_has_default(const struct lysc_node *schema)
413{
414 switch (schema->nodetype) {
415 case LYS_LEAF:
416 if (((struct lysc_node_leaf *)schema)->dflt) {
417 return 1;
418 }
419 break;
420 case LYS_LEAFLIST:
421 if (((struct lysc_node_leaflist *)schema)->dflts) {
422 return 1;
423 }
424 break;
425 case LYS_CONTAINER:
426 if (!(schema->flags & LYS_PRESENCE)) {
427 return 1;
428 }
429 break;
430 default:
431 break;
432 }
433
434 return 0;
435}
436
Michal Vaskobb844672020-07-03 11:06:12 +0200437/**
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100438 * @brief Properly delete a node as part of autodelete validation tasks.
439 *
440 * @param[in,out] first First sibling, is updated if needed.
441 * @param[in] node Node instance to delete.
442 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
443 * @param[in,out] diff Validation diff.
444 */
445static void
446lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p,
447 struct lyd_node **diff)
448{
449 struct lyd_node *iter;
450
451 if (LYD_DEL_IS_ROOT(*first, node)) {
452 *first = (*first)->next;
453 }
454 if (node == *next_p) {
455 *next_p = (*next_p)->next;
456 }
457 if (diff) {
458 /* add into diff */
459 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
460 /* we do not want to track NP container changes, but remember any removed children */
461 LY_LIST_FOR(lyd_child(node), iter) {
462 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
463 }
464 } else {
465 lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff);
466 }
467 }
468 lyd_free_tree(node);
469}
470
471/**
Michal Vaskobb844672020-07-03 11:06:12 +0200472 * @brief Autodelete old instances to prevent validation errors.
473 *
474 * @param[in,out] first First sibling to search in, is updated if needed.
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100475 * @param[in] node New data node instance to check.
Michal Vaskobb844672020-07-03 11:06:12 +0200476 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200477 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200478 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100479static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200480lyd_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 +0100481{
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100482 struct lyd_node *match, *next;
483
484 assert(node->flags & LYD_NEW);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100485
486 if (lyd_val_has_default(node->schema)) {
487 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200488 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100489 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
490 /* default instance found, remove it */
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100491 lyd_validate_autodel_node_del(first, match, next_p, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100492
493 /* remove only a single container/leaf default instance, if there are more, it is an error */
494 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
495 break;
496 }
497 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100498 }
499 }
500}
501
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100502/**
503 * @brief Autodelete leftover default nodes of deleted cases (that have no existing explicit data).
504 *
505 * @param[in,out] first First sibling to search in, is updated if needed.
506 * @param[in] node Default data node instance to check.
507 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
508 * @param[in,out] diff Validation diff.
509 */
510static void
511lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p,
512 struct lyd_node **diff)
513{
514 struct lysc_node_choice *choic;
515 struct lyd_node *iter = NULL;
516 const struct lysc_node *slast = NULL;
517
518 assert(node->flags & LYD_DEFAULT);
519
520 if (!node->schema->parent || (node->schema->parent->nodetype != LYS_CASE)) {
521 /* the default node is not a descendant of a case */
522 return;
523 }
524
525 choic = (struct lysc_node_choice *)node->schema->parent->parent;
526 assert(choic->nodetype == LYS_CHOICE);
527
528 if (choic->dflt && (choic->dflt == (struct lysc_node_case *)node->schema->parent)) {
529 /* data of a default case, keep them */
530 return;
531 }
532
533 /* try to find an explicit node of the case */
534 while ((iter = lys_getnext_data(iter, *first, &slast, node->schema->parent, NULL))) {
535 if (!(iter->flags & LYD_DEFAULT)) {
536 break;
537 }
538 }
539
540 if (!iter) {
541 /* there are only default nodes of the case meaning it does not exist and neither should any default nodes
542 * of the case, remove this one default node */
543 lyd_validate_autodel_node_del(first, node, next_p, diff);
544 }
545}
546
Michal Vaskob1b5c262020-03-05 14:29:47 +0100547LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200548lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200549 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100550{
551 struct lyd_node *next, *node;
552 const struct lysc_node *snode = NULL;
553
554 assert(first && (sparent || mod));
555
556 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
557 /* check case duplicites */
558 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200559 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100560 }
561 }
562
563 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100564 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100565 /* all top-level data from this module checked */
566 break;
567 }
568
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100569 if (!(node->flags & (LYD_NEW | LYD_DEFAULT))) {
570 /* check only new and default nodes */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100571 continue;
572 }
573
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100574 if (node->flags & LYD_NEW) {
575 /* remove old default(s) of the new node if it exists */
576 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100577
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100578 /* then check new node instance duplicities */
579 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100580
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100581 /* this node is valid */
582 node->flags &= ~LYD_NEW;
583 }
584
585 if (node->flags & LYD_DEFAULT) {
586 /* remove leftover default nodes from a no-longer existing case */
587 lyd_validate_autodel_case_dflt(first, node, &next, diff);
588 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100589 }
590
591 return LY_SUCCESS;
592}
593
Michal Vaskobb844672020-07-03 11:06:12 +0200594/**
Michal Vaskobd4db892020-11-23 16:58:20 +0100595 * @brief Evaluate any "when" conditions of a non-existent data node with existing parent.
596 *
597 * @param[in] first First data sibling of the non-existing node.
598 * @param[in] parent Data parent of the non-existing node.
599 * @param[in] snode Schema node of the non-existing node.
600 * @param[out] disabled First when that evaluated false, if any.
601 * @return LY_ERR value.
602 */
603static LY_ERR
604lyd_validate_dummy_when(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
605 const struct lysc_when **disabled)
606{
607 LY_ERR ret = LY_SUCCESS;
608 struct lyd_node *tree, *dummy = NULL;
609
610 /* find root */
611 if (parent) {
612 tree = (struct lyd_node *)parent;
613 while (tree->parent) {
614 tree = lyd_parent(tree);
615 }
616 tree = lyd_first_sibling(tree);
617 } else {
618 assert(!first || !first->prev->next);
619 tree = (struct lyd_node *)first;
620 }
621
622 /* create dummy opaque node */
623 ret = lyd_new_opaq((struct lyd_node *)parent, snode->module->ctx, snode->name, NULL, snode->module->name, &dummy);
624 LY_CHECK_GOTO(ret, cleanup);
625
626 /* connect it if needed */
627 if (!parent) {
628 if (first) {
629 lyd_insert_sibling((struct lyd_node *)first, dummy, &tree);
630 } else {
631 assert(!tree);
632 tree = dummy;
633 }
634 }
635
636 /* evaluate all when */
637 ret = lyd_validate_node_when(tree, dummy, snode, disabled);
638 if (ret == LY_EINCOMPLETE) {
639 /* all other when must be resolved by now */
640 LOGINT(snode->module->ctx);
641 ret = LY_EINT;
642 goto cleanup;
643 } else if (ret) {
644 /* error */
645 goto cleanup;
646 }
647
648cleanup:
649 lyd_free_tree(dummy);
650 return ret;
651}
652
653/**
Michal Vaskobb844672020-07-03 11:06:12 +0200654 * @brief Validate mandatory node existence.
655 *
656 * @param[in] first First sibling to search in.
Michal Vaskobd4db892020-11-23 16:58:20 +0100657 * @param[in] parent Data parent.
Michal Vaskobb844672020-07-03 11:06:12 +0200658 * @param[in] snode Schema node to validate.
659 * @return LY_ERR value.
660 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100661static LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +0100662lyd_validate_mandatory(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100663{
Michal Vaskobd4db892020-11-23 16:58:20 +0100664 const struct lysc_when *disabled;
665
Michal Vaskoa3881362020-01-21 15:57:35 +0100666 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100667 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100668 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100669 return LY_SUCCESS;
670 }
671 } else {
672 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100673
Michal Vaskob1b5c262020-03-05 14:29:47 +0100674 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100675 /* data instance found */
676 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100677 }
678 }
679
Michal Vaskobd4db892020-11-23 16:58:20 +0100680 disabled = NULL;
681 if (lysc_has_when(snode)) {
682 /* if there are any when conditions, they must be true for a validation error */
683 LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
684 }
685
686 if (!disabled) {
687 /* node instance not found */
688 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
689 return LY_EVALID;
690 }
691
692 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100693}
694
Michal Vaskobb844672020-07-03 11:06:12 +0200695/**
696 * @brief Validate min/max-elements constraints, if any.
697 *
698 * @param[in] first First sibling to search in.
Michal Vaskobd4db892020-11-23 16:58:20 +0100699 * @param[in] parent Data parent.
Michal Vaskobb844672020-07-03 11:06:12 +0200700 * @param[in] snode Schema node to validate.
701 * @param[in] min Minimum number of elements, 0 for no restriction.
702 * @param[in] max Max number of elements, 0 for no restriction.
703 * @return LY_ERR value.
704 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100705static LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +0100706lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
707 uint32_t min, uint32_t max)
Michal Vaskoa3881362020-01-21 15:57:35 +0100708{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100709 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200710 struct lyd_node *iter;
Michal Vaskobd4db892020-11-23 16:58:20 +0100711 const struct lysc_when *disabled;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100712
Michal Vasko9b368d32020-02-14 13:53:31 +0100713 assert(min || max);
714
Michal Vasko4c583e82020-07-17 12:16:14 +0200715 LYD_LIST_FOR_INST(first, snode, iter) {
716 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100717
Michal Vasko4c583e82020-07-17 12:16:14 +0200718 if (min && (count == min)) {
719 /* satisfied */
720 min = 0;
721 if (!max) {
722 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100723 break;
724 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100725 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200726 if (max && (count > max)) {
727 /* not satisifed */
728 break;
729 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100730 }
731
Michal Vasko9b368d32020-02-14 13:53:31 +0100732 if (min) {
733 assert(count < min);
Michal Vaskobd4db892020-11-23 16:58:20 +0100734
735 disabled = NULL;
736 if (lysc_has_when(snode)) {
737 /* if there are any when conditions, they must be true for a validation error */
738 LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
739 }
740
741 if (!disabled) {
742 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
743 return LY_EVALID;
744 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100745 } else if (max && (count > max)) {
746 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
747 return LY_EVALID;
748 }
749
Michal Vaskoa3881362020-01-21 15:57:35 +0100750 return LY_SUCCESS;
751}
752
Michal Vaskobb844672020-07-03 11:06:12 +0200753/**
754 * @brief Find node referenced by a list unique statement.
755 *
756 * @param[in] uniq_leaf Unique leaf to find.
757 * @param[in] list List instance to use for the search.
758 * @return Found leaf,
759 * @return NULL if no leaf found.
760 */
Michal Vasko14654712020-02-06 08:35:21 +0100761static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200762lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100763{
Michal Vasko9b368d32020-02-14 13:53:31 +0100764 struct lyd_node *node;
765 const struct lysc_node *iter;
766 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100767
Michal Vasko9b368d32020-02-14 13:53:31 +0100768 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200769 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
770 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100771 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100772
Michal Vaskobb844672020-07-03 11:06:12 +0200773 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100774 while (node && depth) {
775 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200776 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
777 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100778 }
779
780 /* find iter instance in children */
781 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Radek Krejcia1c1e542020-09-29 16:06:52 +0200782 lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100783 --depth;
784 }
785
Michal Vasko14654712020-02-06 08:35:21 +0100786 return node;
787}
788
Michal Vaskobb844672020-07-03 11:06:12 +0200789/**
790 * @brief Callback for comparing 2 list unique leaf values.
791 *
Radek Krejci857189e2020-09-01 13:26:36 +0200792 * Implementation of ::values_equal_cb.
793 *
Michal Vaskobb844672020-07-03 11:06:12 +0200794 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100795 */
Radek Krejci857189e2020-09-01 13:26:36 +0200796static ly_bool
797lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Michal Vasko14654712020-02-06 08:35:21 +0100798{
799 struct ly_ctx *ctx;
800 struct lysc_node_list *slist;
801 struct lyd_node *diter, *first, *second;
802 struct lyd_value *val1, *val2;
803 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200804 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100805
806 assert(val1_p && val2_p);
807
808 first = *((struct lyd_node **)val1_p);
809 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200810 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100811
812 assert(first && (first->schema->nodetype == LYS_LIST));
813 assert(second && (second->schema == first->schema));
814
815 ctx = first->schema->module->ctx;
816
817 slist = (struct lysc_node_list *)first->schema;
818
819 /* compare unique leaves */
820 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200821 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200822 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100823 goto uniquecheck;
824 }
825 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200826 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100827uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200828 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100829 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200830 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100831 if (diter) {
832 val1 = &((struct lyd_node_term *)diter)->value;
833 } else {
834 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200835 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100836 }
837
838 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200839 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100840 if (diter) {
841 val2 = &((struct lyd_node_term *)diter)->value;
842 } else {
843 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200844 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100845 }
846
847 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
848 /* values differ or either one is not set */
849 break;
850 }
851 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200852 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100853 /* all unique leafs are the same in this set, create this nice error */
854 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
855 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
856
857 /* use buffer to rebuild the unique string */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100858#define UNIQ_BUF_SIZE 1024
859 uniq_str = malloc(UNIQ_BUF_SIZE);
Michal Vasko14654712020-02-06 08:35:21 +0100860 uniq_str[0] = '\0';
861 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200862 LY_ARRAY_FOR(slist->uniques[u], v) {
863 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100864 strcpy(ptr, " ");
865 ++ptr;
866 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200867 ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], (struct lysc_node *)slist, LYSC_PATH_LOG,
Radek Krejcif13b87b2020-12-01 22:02:17 +0100868 ptr, UNIQ_BUF_SIZE - (ptr - uniq_str));
Michal Vasko14654712020-02-06 08:35:21 +0100869 if (!ptr) {
870 /* path will be incomplete, whatever */
871 break;
872 }
873
874 ptr += strlen(ptr);
875 }
876 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
877
878 free(path1);
879 free(path2);
880 free(uniq_str);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100881#undef UNIQ_BUF_SIZE
882
Michal Vasko14654712020-02-06 08:35:21 +0100883 return 1;
884 }
885
886 if (action > 0) {
887 /* done */
888 return 0;
889 }
890 }
891
892 return 0;
893}
894
Michal Vaskobb844672020-07-03 11:06:12 +0200895/**
896 * @brief Validate list unique leaves.
897 *
898 * @param[in] first First sibling to search in.
899 * @param[in] snode Schema node to validate.
900 * @param[in] uniques List unique arrays to validate.
901 * @return LY_ERR value.
902 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100903static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200904lyd_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 +0100905{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100906 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100907 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200908 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100909 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200910 uint32_t hash, i, size = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200911 ly_bool dynamic;
Michal Vasko14654712020-02-06 08:35:21 +0100912 const char *str;
913 struct hash_table **uniqtables = NULL;
914 struct lyd_value *val;
915 struct ly_ctx *ctx = snode->module->ctx;
916
917 assert(uniques);
918
919 /* get all list instances */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200920 LY_CHECK_RET(ly_set_new(&set));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100921 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100922 if (diter->schema == snode) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200923 ret = ly_set_add(set, (void *)diter, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200924 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko9b368d32020-02-14 13:53:31 +0100925 }
926 }
Michal Vasko14654712020-02-06 08:35:21 +0100927
928 if (set->count == 2) {
929 /* simple comparison */
930 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
931 /* instance duplication */
932 ret = LY_EVALID;
933 goto cleanup;
934 }
935 } else if (set->count > 2) {
936 /* use hashes for comparison */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100937 /* first, allocate the table, the size depends on number of items in the set,
938 * the following code detects number of upper zero bits in the items' counter value ... */
939 for (i = (sizeof set->count * CHAR_BIT) - 1; i > 0; i--) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200940 size = set->count << i;
941 size = size >> i;
942 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100943 break;
944 }
945 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200946 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100947 /* ... and then we convert it to the position of the highest non-zero bit ... */
948 i = (sizeof set->count * CHAR_BIT) - i;
949 /* ... and by using it to shift 1 to the left we get the closest sufficient hash table size */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200950 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100951
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200952 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100953 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200954 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200955 for (v = 0; v < x; v++) {
956 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
957 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100958 }
959
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200960 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100961 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200962 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100963 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200964 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200965 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100966 if (diter) {
967 val = &((struct lyd_node_term *)diter)->value;
968 } else {
969 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200970 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100971 }
972 if (!val) {
973 /* unique item not present nor has default value */
974 break;
975 }
976
977 /* get canonical string value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200978 str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +0100979 hash = dict_hash_multi(hash, str, strlen(str));
980 if (dynamic) {
981 free((char *)str);
982 }
983 }
984 if (!val) {
985 /* skip this list instance since its unique set is incomplete */
986 continue;
987 }
988
989 /* finish the hash value */
990 hash = dict_hash_multi(hash, NULL, 0);
991
992 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200993 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100994 if (ret == LY_EEXIST) {
995 /* instance duplication */
996 ret = LY_EVALID;
997 }
998 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
999 }
1000 }
1001 }
1002
1003cleanup:
1004 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001005 for (v = 0; v < x; v++) {
1006 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +01001007 /* failed when allocating uniquetables[j], following j are not allocated */
1008 break;
1009 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001010 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +01001011 }
1012 free(uniqtables);
1013
1014 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +01001015}
1016
Michal Vaskobb844672020-07-03 11:06:12 +02001017/**
1018 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
1019 *
1020 * @param[in] first First sibling to search in.
Michal Vaskobd4db892020-11-23 16:58:20 +01001021 * @param[in] parent Data parent.
Michal Vaskobb844672020-07-03 11:06:12 +02001022 * @param[in] sparent Schema parent of the nodes to check.
1023 * @param[in] mod Module of the nodes to check.
1024 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
1025 * @param[in] op Operation being validated, if any.
1026 * @return LY_ERR value.
1027 */
Michal Vaskoa3881362020-01-21 15:57:35 +01001028static LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +01001029lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lyd_node *parent,
1030 const struct lysc_node *sparent, const struct lysc_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +01001031{
Michal Vaskocde73ac2019-11-14 16:10:27 +01001032 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +01001033 struct lysc_node_list *slist;
Michal Vaskod8958df2020-08-05 13:27:36 +02001034 struct lysc_node_leaflist *sllist;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001035 uint32_t getnext_opts;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001036
Radek Krejci7931b192020-06-25 17:05:03 +02001037 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001038
Michal Vaskoa3881362020-01-21 15:57:35 +01001039 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001040 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +02001041 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001042 continue;
1043 }
1044
Michal Vaskoa3881362020-01-21 15:57:35 +01001045 /* check min-elements and max-elements */
Michal Vaskod8958df2020-08-05 13:27:36 +02001046 if (snode->nodetype == LYS_LIST) {
Michal Vaskoa3881362020-01-21 15:57:35 +01001047 slist = (struct lysc_node_list *)snode;
1048 if (slist->min || slist->max) {
Michal Vaskobd4db892020-11-23 16:58:20 +01001049 LY_CHECK_RET(lyd_validate_minmax(first, parent, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +01001050 }
Michal Vaskod8958df2020-08-05 13:27:36 +02001051 } else if (snode->nodetype == LYS_LEAFLIST) {
1052 sllist = (struct lysc_node_leaflist *)snode;
1053 if (sllist->min || sllist->max) {
Michal Vaskobd4db892020-11-23 16:58:20 +01001054 LY_CHECK_RET(lyd_validate_minmax(first, parent, snode, sllist->min, sllist->max));
Michal Vaskod8958df2020-08-05 13:27:36 +02001055 }
Michal Vaskoacd83e72020-02-04 14:12:01 +01001056
Michal Vaskoacd83e72020-02-04 14:12:01 +01001057 } else if (snode->flags & LYS_MAND_TRUE) {
Radek Krejcif6a11002020-08-21 13:29:07 +02001058 /* check generic mandatory existence */
Michal Vaskobd4db892020-11-23 16:58:20 +01001059 LY_CHECK_RET(lyd_validate_mandatory(first, parent, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +01001060 }
1061
1062 /* check unique */
1063 if (snode->nodetype == LYS_LIST) {
1064 slist = (struct lysc_node_list *)snode;
1065 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +02001066 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +01001067 }
1068 }
1069
Michal Vaskoacd83e72020-02-04 14:12:01 +01001070 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
1071 /* go recursively for schema-only nodes */
Michal Vaskobd4db892020-11-23 16:58:20 +01001072 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, parent, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +01001073 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001074 }
1075
Michal Vaskoacd83e72020-02-04 14:12:01 +01001076 return LY_SUCCESS;
1077}
1078
Michal Vaskobb844672020-07-03 11:06:12 +02001079/**
1080 * @brief Validate obsolete nodes, only warnings are printed.
1081 *
1082 * @param[in] node Node to check.
1083 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001084static void
1085lyd_validate_obsolete(const struct lyd_node *node)
1086{
1087 const struct lysc_node *snode;
1088
1089 snode = node->schema;
1090 do {
1091 if (snode->flags & LYS_STATUS_OBSLT) {
1092 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
1093 break;
1094 }
1095
1096 snode = snode->parent;
1097 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
1098}
1099
Michal Vaskobb844672020-07-03 11:06:12 +02001100/**
1101 * @brief Validate must conditions of a data node.
1102 *
1103 * @param[in] node Node to validate.
1104 * @param[in] op Operation being validated, if any.
1105 * @return LY_ERR value.
1106 */
Michal Vaskocc048b22020-03-27 15:52:38 +01001107static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +02001108lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +01001109{
1110 struct lyxp_set xp_set;
1111 struct lysc_must *musts;
1112 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001113 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +01001114
1115 switch (node->schema->nodetype) {
1116 case LYS_CONTAINER:
1117 musts = ((struct lysc_node_container *)node->schema)->musts;
1118 break;
1119 case LYS_LEAF:
1120 musts = ((struct lysc_node_leaf *)node->schema)->musts;
1121 break;
1122 case LYS_LEAFLIST:
1123 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
1124 break;
1125 case LYS_LIST:
1126 musts = ((struct lysc_node_list *)node->schema)->musts;
1127 break;
1128 case LYS_ANYXML:
1129 case LYS_ANYDATA:
1130 musts = ((struct lysc_node_anydata *)node->schema)->musts;
1131 break;
1132 case LYS_NOTIF:
1133 musts = ((struct lysc_notif *)node->schema)->musts;
1134 break;
1135 case LYS_RPC:
1136 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +02001137 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001138 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +02001139 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001140 musts = ((struct lysc_action *)node->schema)->output.musts;
1141 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001142 LOGINT(LYD_CTX(node));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001143 return LY_EINT;
1144 }
Michal Vaskocc048b22020-03-27 15:52:38 +01001145 break;
1146 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001147 LOGINT(LYD_CTX(node));
Michal Vaskocc048b22020-03-27 15:52:38 +01001148 return LY_EINT;
1149 }
1150
1151 if (!musts) {
1152 /* no must to evaluate */
1153 return LY_SUCCESS;
1154 }
1155
1156 /* find first top-level node */
Radek Krejci1e008d22020-08-17 11:37:37 +02001157 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent) {}
Michal Vaskocc048b22020-03-27 15:52:38 +01001158 while (tree->prev->next) {
1159 tree = tree->prev;
1160 }
1161
1162 LY_ARRAY_FOR(musts, u) {
1163 memset(&xp_set, 0, sizeof xp_set);
1164
1165 /* evaluate must */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001166 LY_CHECK_RET(lyxp_eval(musts[u].cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes, node,
1167 tree, &xp_set, LYXP_SCHEMA));
Michal Vaskocc048b22020-03-27 15:52:38 +01001168
1169 /* check the result */
1170 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02001171 if (!xp_set.val.bln) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001172 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
Michal Vaskocc048b22020-03-27 15:52:38 +01001173 return LY_EVALID;
1174 }
1175 }
1176
1177 return LY_SUCCESS;
1178}
1179
Michal Vaskob1b5c262020-03-05 14:29:47 +01001180LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +01001181lyd_validate_final_r(struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *sparent,
1182 const struct lys_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +01001183{
Radek Krejci7f769d72020-07-11 23:13:56 +02001184 struct lyd_node *next = NULL, *node;
Michal Vaskoacd83e72020-02-04 14:12:01 +01001185
Michal Vasko14654712020-02-06 08:35:21 +01001186 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001187 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001188 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001189 /* all top-level data from this module checked */
1190 break;
Michal Vaskof03ed032020-03-04 13:31:44 +01001191 }
1192
Michal Vaskoa8c61722020-03-27 16:59:32 +01001193 /* opaque data */
1194 if (!node->schema) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001195 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
Michal Vasko6a66ffb2020-11-23 16:59:22 +01001196 ((struct lyd_node_opaq *)node)->name.name);
Michal Vaskoa8c61722020-03-27 16:59:32 +01001197 return LY_EVALID;
1198 }
1199
Michal Vaskocb7526d2020-03-30 15:08:26 +02001200 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +02001201 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001202 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001203 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001204 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001205 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001206 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +02001207 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001208 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +01001209 return LY_EVALID;
1210 }
1211
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001212 /* obsolete data */
1213 lyd_validate_obsolete(node);
1214
Michal Vaskocc048b22020-03-27 15:52:38 +01001215 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001216 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001217
Michal Vasko53d97a12020-11-05 17:39:10 +01001218 /* node value was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +01001219 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001220
Michal Vasko14654712020-02-06 08:35:21 +01001221 /* validate schema-based restrictions */
Michal Vaskobd4db892020-11-23 16:58:20 +01001222 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, parent, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001223
Michal Vaskob1b5c262020-03-05 14:29:47 +01001224 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001225 /* validate all children recursively */
Michal Vaskobd4db892020-11-23 16:58:20 +01001226 LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node, node->schema, NULL, val_opts, op));
Michal Vaskocde73ac2019-11-14 16:10:27 +01001227
Michal Vaskob1b5c262020-03-05 14:29:47 +01001228 /* set default for containers */
1229 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Radek Krejcia1c1e542020-09-29 16:06:52 +02001230 LY_LIST_FOR(lyd_child(node), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001231 if (!(next->flags & LYD_DEFAULT)) {
1232 break;
1233 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001234 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001235 if (!next) {
1236 node->flags |= LYD_DEFAULT;
1237 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001238 }
1239 }
1240
1241 return LY_SUCCESS;
1242}
1243
Radek Krejci7931b192020-06-25 17:05:03 +02001244/**
Michal Vaskobb844672020-07-03 11:06:12 +02001245 * @brief Validate the whole data subtree.
1246 *
1247 * @param[in] root Subtree root.
1248 * @param[in,out] type_check Set for unres node types.
1249 * @param[in,out] type_meta_check Set for unres metadata types.
1250 * @param[in,out] when_check Set for nodes with when conditions.
1251 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001252 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001253 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001254 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001255static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001256lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001257 struct ly_set *when_check, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001258{
1259 const struct lyd_meta *meta;
Michal Vasko56daf732020-08-10 10:57:18 +02001260 struct lyd_node *node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001261
Michal Vasko56daf732020-08-10 10:57:18 +02001262 LYD_TREE_DFS_BEGIN(root, node) {
Michal Vasko0275cf62020-11-05 17:40:30 +01001263 LY_LIST_FOR(node->meta, meta) {
1264 if (((struct lyext_metadata *)meta->annotation->data)->type->plugin->validate) {
1265 /* metadata type resolution */
1266 LY_CHECK_RET(ly_set_add(type_meta_check, (void *)meta, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001267 }
Michal Vasko0275cf62020-11-05 17:40:30 +01001268 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001269
Michal Vasko0275cf62020-11-05 17:40:30 +01001270 if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
1271 /* node type resolution */
1272 LY_CHECK_RET(ly_set_add(type_check, (void *)node, 1, NULL));
1273 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1274 /* new node validation, autodelete */
1275 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 +02001276
Michal Vasko0275cf62020-11-05 17:40:30 +01001277 /* add nested defaults */
Michal Vaskobd4db892020-11-23 16:58:20 +01001278 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1279 NULL, val_opts & LYD_VALIDATE_NO_STATE ? LYD_IMPLICIT_NO_STATE : 0, diff));
Michal Vasko0275cf62020-11-05 17:40:30 +01001280 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001281
Michal Vasko0275cf62020-11-05 17:40:30 +01001282 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1283 /* when evaluation */
1284 LY_CHECK_RET(ly_set_add(when_check, (void *)node, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001285 }
1286
Michal Vasko56daf732020-08-10 10:57:18 +02001287 LYD_TREE_DFS_END(root, node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001288 }
1289
1290 return LY_SUCCESS;
1291}
1292
Michal Vaskobb844672020-07-03 11:06:12 +02001293/**
1294 * @brief Validate data tree.
1295 *
1296 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1297 * @param[in] modules Array of modules to validate, NULL for all modules.
1298 * @param[in] mod_count Count of @p modules.
1299 * @param[in] ly_ctx libyang context.
1300 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001301 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001302 * @return LY_ERR value.
1303 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001304static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001305lyd_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 +02001306 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001307{
1308 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001309 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001310 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001311 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001312 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001313
Michal Vasko26e80012020-07-08 10:55:46 +02001314 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001315 if (diff) {
1316 *diff = NULL;
1317 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001318
Michal Vaskob1b5c262020-03-05 14:29:47 +01001319 next = *tree;
1320 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001321 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001322 mod = lyd_data_next_module(&next, &first);
1323 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001324 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001325 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001326 if (!mod) {
1327 break;
1328 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001329 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001330 /* make sure first2 changes are carried to tree */
1331 first2 = tree;
1332 } else {
1333 first2 = &first;
1334 }
1335
1336 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001337 ret = lyd_validate_new(first2, NULL, mod, diff);
1338 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001339
Michal Vasko0275cf62020-11-05 17:40:30 +01001340 /* add all top-level defaults for this module, do not add into unres sets, will occur in the next step */
1341 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, NULL, NULL, val_opts & LYD_VALIDATE_NO_STATE ?
Michal Vasko69730152020-10-09 16:30:07 +02001342 LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001343 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001344
1345 /* process nested nodes */
1346 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001347 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1348 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001349 }
1350
1351 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001352 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001353 LY_CHECK_GOTO(ret, cleanup);
1354
1355 /* perform final validation that assumes the data tree is final */
Michal Vaskobd4db892020-11-23 16:58:20 +01001356 ret = lyd_validate_final_r(*first2, NULL, NULL, mod, val_opts, 0);
Michal Vasko8104fd42020-07-13 11:09:51 +02001357 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001358 }
1359
Michal Vaskof03ed032020-03-04 13:31:44 +01001360cleanup:
1361 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001362 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001363 ly_set_erase(&when_check, NULL);
1364 return ret;
1365}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001366
1367API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001368lyd_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 +01001369{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001370 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001371}
1372
1373API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001374lyd_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 +01001375{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001376 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001377}
Michal Vaskofea12c62020-03-30 11:00:15 +02001378
Michal Vaskobb844672020-07-03 11:06:12 +02001379/**
1380 * @brief Find nodes for merging an operation into data tree for validation.
1381 *
1382 * @param[in] op_tree Full operation data tree.
1383 * @param[in] op_node Operation node itself.
1384 * @param[in] tree Data tree to be merged into.
1385 * @param[out] op_subtree Operation subtree to merge.
1386 * @param[out] tree_sibling Data tree sibling to merge next to.
1387 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001388static void
Michal Vaskobb844672020-07-03 11:06:12 +02001389lyd_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 +02001390 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001391{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001392 const struct lyd_node *tree_iter, *op_iter;
1393 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001394 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001395
Michal Vaskocb7526d2020-03-30 15:08:26 +02001396 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001397 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001398 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001399 ++op_depth;
1400 }
1401
1402 /* find where to merge op */
1403 tree_iter = tree;
1404 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001405 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001406 while (cur_depth) {
1407 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001408 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001409 for (i = 0; i < cur_depth; ++i) {
1410 op_iter = (struct lyd_node *)op_iter->parent;
1411 }
1412
1413 /* find op iter in tree */
1414 lyd_find_sibling_first(tree_iter, op_iter, &match);
1415 if (!match) {
1416 break;
1417 }
1418
1419 /* move tree_iter */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001420 tree_iter = lyd_child(match);
Michal Vaskofea12c62020-03-30 11:00:15 +02001421
1422 /* move depth */
1423 --cur_depth;
1424 }
1425
Michal Vaskobb844672020-07-03 11:06:12 +02001426 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001427 *tree_sibling = (struct lyd_node *)tree_iter;
1428}
1429
1430API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001431lyd_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 +02001432{
1433 LY_ERR ret;
Michal Vasko56daf732020-08-10 10:57:18 +02001434 struct lyd_node *tree_sibling, *op_subtree, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001435 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1436
1437 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci0f969882020-08-21 16:56:47 +02001438 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001439 if (diff) {
1440 *diff = NULL;
1441 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001442
1443 /* find the operation/notification */
Michal Vasko56daf732020-08-10 10:57:18 +02001444 LYD_TREE_DFS_BEGIN(op_tree, op_node) {
Michal Vasko69730152020-10-09 16:30:07 +02001445 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 +02001446 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001447 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001448 break;
1449 }
Michal Vasko56daf732020-08-10 10:57:18 +02001450 LYD_TREE_DFS_END(op_tree, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001451 }
Michal Vasko69730152020-10-09 16:30:07 +02001452 if ((op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY)) {
Radek Krejci7931b192020-06-25 17:05:03 +02001453 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001454 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001455 return LY_EINVAL;
1456 }
1457 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001458 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001459 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001460 return LY_EINVAL;
1461 }
1462 }
1463
1464 /* move op_tree to top-level node */
1465 while (op_tree->parent) {
1466 op_tree = (struct lyd_node *)op_tree->parent;
1467 }
1468
1469 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001470 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1471 op_parent = (struct lyd_node *)op_subtree->parent;
1472 lyd_unlink_tree(op_subtree);
1473 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001474 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001475 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001476 }
1477
1478 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001479 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 +02001480
1481 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1482 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 +02001483 diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001484
1485 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001486 lyd_validate_obsolete(op_node);
Radek Krejci7931b192020-06-25 17:05:03 +02001487 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001488
1489 /* final validation of all the descendants */
Michal Vaskobd4db892020-11-23 16:58:20 +01001490 LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_child(op_node), op_node, op_node->schema, NULL, 0, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001491
1492cleanup:
1493 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001494 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001495 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001496 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001497 }
1498
1499 ly_set_erase(&type_check, NULL);
1500 ly_set_erase(&type_meta_check, NULL);
1501 ly_set_erase(&when_check, NULL);
1502 return ret;
1503}