blob: 4aa4fdbfb8a86399613f04f3d07ee998770790a4 [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;
109 struct lyxp_set xp_set;
Michal Vaskobd4db892020-11-23 16:58:20 +0100110 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100111
Michal Vaskobd4db892020-11-23 16:58:20 +0100112 assert(!node->schema || (node->schema == schema));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100113
Michal Vaskobd4db892020-11-23 16:58:20 +0100114 *disabled = NULL;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100115
Michal Vaskobd4db892020-11-23 16:58:20 +0100116 do {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100117 const struct lysc_when *when;
118 struct lysc_when **when_list = lysc_node_when(schema);
119 LY_ARRAY_FOR(when_list, u) {
120 when = when_list[u];
Michal Vaskocde73ac2019-11-14 16:10:27 +0100121
Michal Vaskobd4db892020-11-23 16:58:20 +0100122 /* get context node */
123 if (when->context == schema) {
124 ctx_node = node;
125 } else {
126 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
127 ctx_node = (struct lyd_node *)node->parent;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100128 }
Michal Vaskobd4db892020-11-23 16:58:20 +0100129
130 /* evaluate when */
131 memset(&xp_set, 0, sizeof xp_set);
Michal Vasko400e9672021-01-11 13:39:17 +0100132 ret = lyxp_eval(LYD_CTX(node), when->cond, schema->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes,
133 ctx_node, tree, &xp_set, LYXP_SCHEMA);
Michal Vaskobd4db892020-11-23 16:58:20 +0100134 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
135
136 /* return error or LY_EINCOMPLETE for dependant unresolved when */
137 LY_CHECK_RET(ret);
138
139 if (!xp_set.val.bln) {
140 /* false when */
141 *disabled = when;
142 return LY_SUCCESS;
Michal Vasko8104fd42020-07-13 11:09:51 +0200143 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100144 }
Michal Vaskobd4db892020-11-23 16:58:20 +0100145
146 schema = schema->parent;
147 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
148
149 return LY_SUCCESS;
150}
151
152/**
153 * @brief Evaluate when conditions of collected unres nodes.
154 *
155 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100156 * @param[in] mod Module of the @p tree to take into consideration when deleting @p tree and moving it.
157 * If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be
158 * the first top-level sibling.
Michal Vaskobd4db892020-11-23 16:58:20 +0100159 * @param[in] node_when Set with nodes with "when" conditions.
160 * @param[in,out] diff Validation diff.
161 * @return LY_SUCCESS on success.
162 * @return LY_ERR value on error.
163 */
164static LY_ERR
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100165lyd_validate_unres_when(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when,
166 struct lyd_node **diff)
Michal Vaskobd4db892020-11-23 16:58:20 +0100167{
168 LY_ERR ret;
169 uint32_t i;
170 const struct lysc_when *disabled;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100171 struct lyd_node *node = NULL;
Michal Vaskobd4db892020-11-23 16:58:20 +0100172
173 if (!node_when->count) {
174 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100175 }
176
Michal Vaskobd4db892020-11-23 16:58:20 +0100177 i = node_when->count;
178 do {
179 --i;
180 node = node_when->dnodes[i];
Radek Krejciddace2c2021-01-08 11:30:56 +0100181 LOG_LOCSET(node->schema, node, NULL, NULL);
Michal Vaskobd4db892020-11-23 16:58:20 +0100182
183 /* evaluate all when expressions that affect this node's existence */
184 ret = lyd_validate_node_when(*tree, node, node->schema, &disabled);
185 if (!ret) {
186 if (disabled) {
187 /* when false */
188 if (node->flags & LYD_WHEN_TRUE) {
189 /* autodelete */
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100190 lyd_del_move_root(tree, node, mod);
Michal Vaskobd4db892020-11-23 16:58:20 +0100191 if (diff) {
192 /* add into diff */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100193 ret = lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff);
194 LY_CHECK_GOTO(ret, error);
Michal Vaskobd4db892020-11-23 16:58:20 +0100195 }
196 lyd_free_tree(node);
197 } else {
198 /* invalid data */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100199 LOGVAL(LYD_CTX(node), LY_VCODE_NOWHEN, disabled->cond->expr);
200 ret = LY_EVALID;
201 goto error;
Michal Vaskobd4db892020-11-23 16:58:20 +0100202 }
203 } else {
204 /* when true */
205 node->flags |= LYD_WHEN_TRUE;
206 }
207
208 /* remove this node from the set, its when was resolved */
209 ly_set_rm_index(node_when, i, NULL);
210 } else if (ret != LY_EINCOMPLETE) {
211 /* error */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100212 goto error;
Michal Vaskobd4db892020-11-23 16:58:20 +0100213 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100214
Radek Krejciddace2c2021-01-08 11:30:56 +0100215 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskobd4db892020-11-23 16:58:20 +0100216 } while (i);
217
218 return LY_SUCCESS;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100219
220error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100221 LOG_LOCBACK(1, 1, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100222 return ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100223}
224
225LY_ERR
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100226lyd_validate_unres(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when,
227 struct ly_set *node_types, struct ly_set *meta_types, struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100228{
229 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200230 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100231
Michal Vaskob1b5c262020-03-05 14:29:47 +0100232 if (node_when) {
233 /* evaluate all when conditions */
234 uint32_t prev_count;
235 do {
236 prev_count = node_when->count;
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100237 LY_CHECK_RET(lyd_validate_unres_when(tree, mod, node_when, diff));
Radek Krejci0f969882020-08-21 16:56:47 +0200238 /* there must have been some when conditions resolved */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100239 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100240
Michal Vaskob1b5c262020-03-05 14:29:47 +0100241 /* there could have been no cyclic when dependencies, checked during compilation */
242 assert(!node_when->count);
243 }
244
245 if (node_types && node_types->count) {
246 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200247 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100248 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200249 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100250
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200251 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200252 struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100253
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200254 /* resolve the value of the node */
Radek Krejciddace2c2021-01-08 11:30:56 +0100255 LOG_LOCSET(node->schema, (struct lyd_node *)node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100256 ret = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, (struct lyd_node *)node, *tree);
Radek Krejciddace2c2021-01-08 11:30:56 +0100257 LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100258 LY_CHECK_RET(ret);
259
260 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200261 ly_set_rm_index(node_types, i, NULL);
262 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100263 }
264
Michal Vasko9f96a052020-03-10 09:41:45 +0100265 if (meta_types && meta_types->count) {
266 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200267 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100268 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200269 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100270
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200271 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200272 struct lysc_type *type = ((struct lyext_metadata *)meta->annotation->data)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100273
Michal Vasko9f96a052020-03-10 09:41:45 +0100274 /* validate and store the value of the metadata */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100275 ret = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100276 LY_CHECK_RET(ret);
277
278 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200279 ly_set_rm_index(meta_types, i, NULL);
280 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100281 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100282
283 return ret;
284}
285
Michal Vaskobb844672020-07-03 11:06:12 +0200286/**
287 * @brief Validate instance duplication.
288 *
289 * @param[in] first First sibling to search in.
290 * @param[in] node Data node instance to check.
291 * @return LY_ERR value.
292 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100293static LY_ERR
294lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100295{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100296 struct lyd_node **match_p;
Radek Krejci857189e2020-09-01 13:26:36 +0200297 ly_bool fail = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100298
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100299 assert(node->flags & LYD_NEW);
300
Michal Vaskob1b5c262020-03-05 14:29:47 +0100301 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
302 /* duplicate instances allowed */
303 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100304 }
305
Michal Vaskob1b5c262020-03-05 14:29:47 +0100306 /* find exactly the same next instance using hashes if possible */
307 if (node->parent && node->parent->children_ht) {
308 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
309 fail = 1;
310 }
311 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200312 for ( ; first; first = first->next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100313 if (first == node) {
314 continue;
315 }
316
317 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
318 if (first->schema == node->schema) {
319 fail = 1;
320 break;
321 }
Michal Vasko8f359bf2020-07-28 10:41:15 +0200322 } else if (!lyd_compare_single(first, node, 0)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100323 fail = 1;
324 break;
325 }
326 }
327 }
328
329 if (fail) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100330 LOGVAL(node->schema->module->ctx, LY_VCODE_DUP, node->schema->name);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100331 return LY_EVALID;
332 }
333 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100334}
335
Michal Vaskobb844672020-07-03 11:06:12 +0200336/**
337 * @brief Validate multiple case data existence with possible autodelete.
338 *
339 * @param[in,out] first First sibling to search in, is updated if needed.
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100340 * @param[in] mod Module of the siblings, NULL for nested siblings.
Michal Vaskobb844672020-07-03 11:06:12 +0200341 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200342 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200343 * @return LY_ERR value.
344 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100345static LY_ERR
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100346lyd_validate_cases(struct lyd_node **first, const struct lys_module *mod, const struct lysc_node_choice *choic,
347 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100348{
349 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
350 struct lyd_node *match, *to_del;
Radek Krejci857189e2020-09-01 13:26:36 +0200351 ly_bool found;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100352
Radek Krejciddace2c2021-01-08 11:30:56 +0100353 LOG_LOCSET((const struct lysc_node *)choic, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100354
Michal Vaskob1b5c262020-03-05 14:29:47 +0100355 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
356 found = 0;
357 iter = NULL;
358 match = NULL;
359 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
360 if (match->flags & LYD_NEW) {
361 /* a new case data found, nothing more to look for */
362 found = 2;
363 break;
364 } else {
365 /* and old case data found */
366 if (found == 0) {
367 found = 1;
368 }
369 }
370 }
371
372 if (found == 1) {
373 /* there should not be 2 old cases */
374 if (old_case) {
375 /* old data from 2 cases */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100376 LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, old_case->name, scase->name);
Radek Krejciddace2c2021-01-08 11:30:56 +0100377 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100378 return LY_EVALID;
379 }
380
381 /* remember an old existing case */
382 old_case = scase;
383 } else if (found == 2) {
384 if (new_case) {
385 /* new data from 2 cases */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100386 LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, new_case->name, scase->name);
Radek Krejciddace2c2021-01-08 11:30:56 +0100387 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100388 return LY_EVALID;
389 }
390
391 /* remember a new existing case */
392 new_case = scase;
393 }
394 }
395
Radek Krejciddace2c2021-01-08 11:30:56 +0100396 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100397
Michal Vaskob1b5c262020-03-05 14:29:47 +0100398 if (old_case && new_case) {
399 /* auto-delete old case */
400 iter = NULL;
401 match = NULL;
402 to_del = NULL;
403 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100404 lyd_del_move_root(first, to_del, mod);
405
Michal Vasko8104fd42020-07-13 11:09:51 +0200406 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100407 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200408 if (diff) {
409 /* add into diff */
410 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
411 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100412 to_del = match;
413 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100414 lyd_del_move_root(first, to_del, mod);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100415 lyd_free_tree(to_del);
416 }
417
418 return LY_SUCCESS;
419}
420
Michal Vaskobb844672020-07-03 11:06:12 +0200421/**
422 * @brief Check whether a schema node can have some default values (true for NP containers as well).
423 *
424 * @param[in] schema Schema node to check.
425 * @return non-zero if yes,
426 * @return 0 otherwise.
427 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100428static int
429lyd_val_has_default(const struct lysc_node *schema)
430{
431 switch (schema->nodetype) {
432 case LYS_LEAF:
433 if (((struct lysc_node_leaf *)schema)->dflt) {
434 return 1;
435 }
436 break;
437 case LYS_LEAFLIST:
438 if (((struct lysc_node_leaflist *)schema)->dflts) {
439 return 1;
440 }
441 break;
442 case LYS_CONTAINER:
443 if (!(schema->flags & LYS_PRESENCE)) {
444 return 1;
445 }
446 break;
447 default:
448 break;
449 }
450
451 return 0;
452}
453
Michal Vaskobb844672020-07-03 11:06:12 +0200454/**
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100455 * @brief Properly delete a node as part of autodelete validation tasks.
456 *
457 * @param[in,out] first First sibling, is updated if needed.
458 * @param[in] node Node instance to delete.
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100459 * @param[in] mod Module of the siblings, NULL for nested siblings.
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100460 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
461 * @param[in,out] diff Validation diff.
462 */
463static void
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100464lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod,
465 struct lyd_node **next_p, struct lyd_node **diff)
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100466{
467 struct lyd_node *iter;
468
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100469 lyd_del_move_root(first, node, mod);
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100470 if (node == *next_p) {
471 *next_p = (*next_p)->next;
472 }
473 if (diff) {
474 /* add into diff */
475 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
476 /* we do not want to track NP container changes, but remember any removed children */
477 LY_LIST_FOR(lyd_child(node), iter) {
478 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
479 }
480 } else {
481 lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff);
482 }
483 }
484 lyd_free_tree(node);
485}
486
487/**
Michal Vaskobb844672020-07-03 11:06:12 +0200488 * @brief Autodelete old instances to prevent validation errors.
489 *
490 * @param[in,out] first First sibling to search in, is updated if needed.
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100491 * @param[in] node New data node instance to check.
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100492 * @param[in] mod Module of the siblings, NULL for nested siblings.
Michal Vaskobb844672020-07-03 11:06:12 +0200493 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200494 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200495 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100496static void
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100497lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod,
498 struct lyd_node **next_p, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100499{
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100500 struct lyd_node *match, *next;
501
502 assert(node->flags & LYD_NEW);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100503
504 if (lyd_val_has_default(node->schema)) {
505 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200506 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100507 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
508 /* default instance found, remove it */
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100509 lyd_validate_autodel_node_del(first, match, mod, next_p, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100510
511 /* remove only a single container/leaf default instance, if there are more, it is an error */
512 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
513 break;
514 }
515 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100516 }
517 }
518}
519
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100520/**
521 * @brief Autodelete leftover default nodes of deleted cases (that have no existing explicit data).
522 *
523 * @param[in,out] first First sibling to search in, is updated if needed.
524 * @param[in] node Default data node instance to check.
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100525 * @param[in] mod Module of the siblings, NULL for nested siblings.
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100526 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
527 * @param[in,out] diff Validation diff.
528 */
529static void
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100530lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod,
531 struct lyd_node **next_p, struct lyd_node **diff)
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100532{
533 struct lysc_node_choice *choic;
534 struct lyd_node *iter = NULL;
535 const struct lysc_node *slast = NULL;
536
537 assert(node->flags & LYD_DEFAULT);
538
539 if (!node->schema->parent || (node->schema->parent->nodetype != LYS_CASE)) {
540 /* the default node is not a descendant of a case */
541 return;
542 }
543
544 choic = (struct lysc_node_choice *)node->schema->parent->parent;
545 assert(choic->nodetype == LYS_CHOICE);
546
547 if (choic->dflt && (choic->dflt == (struct lysc_node_case *)node->schema->parent)) {
548 /* data of a default case, keep them */
549 return;
550 }
551
552 /* try to find an explicit node of the case */
553 while ((iter = lys_getnext_data(iter, *first, &slast, node->schema->parent, NULL))) {
554 if (!(iter->flags & LYD_DEFAULT)) {
555 break;
556 }
557 }
558
559 if (!iter) {
560 /* there are only default nodes of the case meaning it does not exist and neither should any default nodes
561 * of the case, remove this one default node */
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100562 lyd_validate_autodel_node_del(first, node, mod, next_p, diff);
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100563 }
564}
565
Michal Vaskob1b5c262020-03-05 14:29:47 +0100566LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200567lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200568 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100569{
570 struct lyd_node *next, *node;
571 const struct lysc_node *snode = NULL;
572
573 assert(first && (sparent || mod));
574
575 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
576 /* check case duplicites */
577 if (snode->nodetype == LYS_CHOICE) {
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100578 LY_CHECK_RET(lyd_validate_cases(first, mod, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100579 }
580 }
581
582 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100583 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100584 /* all top-level data from this module checked */
585 break;
586 }
587
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100588 if (!(node->flags & (LYD_NEW | LYD_DEFAULT))) {
589 /* check only new and default nodes */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100590 continue;
591 }
592
Radek Krejciddace2c2021-01-08 11:30:56 +0100593 LOG_LOCSET(node->schema, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100594
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100595 if (node->flags & LYD_NEW) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100596 LY_ERR ret;
597
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100598 /* remove old default(s) of the new node if it exists */
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100599 lyd_validate_autodel_dup(first, node, mod, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100600
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100601 /* then check new node instance duplicities */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100602 ret = lyd_validate_duplicates(*first, node);
Radek Krejciddace2c2021-01-08 11:30:56 +0100603 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0), ret);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100604
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100605 /* this node is valid */
606 node->flags &= ~LYD_NEW;
607 }
608
Michal Vasko0e6de512021-01-11 13:39:44 +0100609 LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0);
610
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100611 if (node->flags & LYD_DEFAULT) {
612 /* remove leftover default nodes from a no-longer existing case */
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100613 lyd_validate_autodel_case_dflt(first, node, mod, &next, diff);
Michal Vaskodcacf2f2020-11-18 18:18:15 +0100614 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100615 }
616
617 return LY_SUCCESS;
618}
619
Michal Vaskobb844672020-07-03 11:06:12 +0200620/**
Michal Vaskobd4db892020-11-23 16:58:20 +0100621 * @brief Evaluate any "when" conditions of a non-existent data node with existing parent.
622 *
623 * @param[in] first First data sibling of the non-existing node.
624 * @param[in] parent Data parent of the non-existing node.
625 * @param[in] snode Schema node of the non-existing node.
626 * @param[out] disabled First when that evaluated false, if any.
627 * @return LY_ERR value.
628 */
629static LY_ERR
630lyd_validate_dummy_when(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
631 const struct lysc_when **disabled)
632{
633 LY_ERR ret = LY_SUCCESS;
634 struct lyd_node *tree, *dummy = NULL;
635
636 /* find root */
637 if (parent) {
638 tree = (struct lyd_node *)parent;
639 while (tree->parent) {
640 tree = lyd_parent(tree);
641 }
642 tree = lyd_first_sibling(tree);
643 } else {
644 assert(!first || !first->prev->next);
645 tree = (struct lyd_node *)first;
646 }
647
648 /* create dummy opaque node */
649 ret = lyd_new_opaq((struct lyd_node *)parent, snode->module->ctx, snode->name, NULL, snode->module->name, &dummy);
650 LY_CHECK_GOTO(ret, cleanup);
651
652 /* connect it if needed */
653 if (!parent) {
654 if (first) {
655 lyd_insert_sibling((struct lyd_node *)first, dummy, &tree);
656 } else {
657 assert(!tree);
658 tree = dummy;
659 }
660 }
661
662 /* evaluate all when */
663 ret = lyd_validate_node_when(tree, dummy, snode, disabled);
664 if (ret == LY_EINCOMPLETE) {
665 /* all other when must be resolved by now */
666 LOGINT(snode->module->ctx);
667 ret = LY_EINT;
668 goto cleanup;
669 } else if (ret) {
670 /* error */
671 goto cleanup;
672 }
673
674cleanup:
675 lyd_free_tree(dummy);
676 return ret;
677}
678
679/**
Michal Vaskobb844672020-07-03 11:06:12 +0200680 * @brief Validate mandatory node existence.
681 *
682 * @param[in] first First sibling to search in.
Michal Vaskobd4db892020-11-23 16:58:20 +0100683 * @param[in] parent Data parent.
Michal Vaskobb844672020-07-03 11:06:12 +0200684 * @param[in] snode Schema node to validate.
685 * @return LY_ERR value.
686 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100687static LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +0100688lyd_validate_mandatory(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100689{
Michal Vaskobd4db892020-11-23 16:58:20 +0100690 const struct lysc_when *disabled;
691
Michal Vaskoa3881362020-01-21 15:57:35 +0100692 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100693 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100694 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100695 return LY_SUCCESS;
696 }
697 } else {
698 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100699
Michal Vaskob1b5c262020-03-05 14:29:47 +0100700 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100701 /* data instance found */
702 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100703 }
704 }
705
Michal Vaskobd4db892020-11-23 16:58:20 +0100706 disabled = NULL;
707 if (lysc_has_when(snode)) {
708 /* if there are any when conditions, they must be true for a validation error */
709 LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
710 }
711
712 if (!disabled) {
713 /* node instance not found */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100714 LOGVAL(snode->module->ctx, LY_VCODE_NOMAND, snode->name);
Michal Vaskobd4db892020-11-23 16:58:20 +0100715 return LY_EVALID;
716 }
717
718 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100719}
720
Michal Vaskobb844672020-07-03 11:06:12 +0200721/**
722 * @brief Validate min/max-elements constraints, if any.
723 *
724 * @param[in] first First sibling to search in.
Michal Vaskobd4db892020-11-23 16:58:20 +0100725 * @param[in] parent Data parent.
Michal Vaskobb844672020-07-03 11:06:12 +0200726 * @param[in] snode Schema node to validate.
727 * @param[in] min Minimum number of elements, 0 for no restriction.
728 * @param[in] max Max number of elements, 0 for no restriction.
729 * @return LY_ERR value.
730 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100731static LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +0100732lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
733 uint32_t min, uint32_t max)
Michal Vaskoa3881362020-01-21 15:57:35 +0100734{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100735 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200736 struct lyd_node *iter;
Michal Vaskobd4db892020-11-23 16:58:20 +0100737 const struct lysc_when *disabled;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100738 ly_bool invalid_instance = 0;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100739
Michal Vasko9b368d32020-02-14 13:53:31 +0100740 assert(min || max);
741
Michal Vasko4c583e82020-07-17 12:16:14 +0200742 LYD_LIST_FOR_INST(first, snode, iter) {
743 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100744
Michal Vasko4c583e82020-07-17 12:16:14 +0200745 if (min && (count == min)) {
746 /* satisfied */
747 min = 0;
748 if (!max) {
749 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100750 break;
751 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100752 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200753 if (max && (count > max)) {
754 /* not satisifed */
Radek Krejciddace2c2021-01-08 11:30:56 +0100755 LOG_LOCSET(NULL, iter, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100756 invalid_instance = 1;
Michal Vasko4c583e82020-07-17 12:16:14 +0200757 break;
758 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100759 }
760
Michal Vasko9b368d32020-02-14 13:53:31 +0100761 if (min) {
762 assert(count < min);
Michal Vaskobd4db892020-11-23 16:58:20 +0100763
764 disabled = NULL;
765 if (lysc_has_when(snode)) {
766 /* if there are any when conditions, they must be true for a validation error */
767 LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
768 }
769
770 if (!disabled) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100771 LOGVAL(snode->module->ctx, LY_VCODE_NOMIN, snode->name);
772 goto failure;
Michal Vaskobd4db892020-11-23 16:58:20 +0100773 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100774 } else if (max && (count > max)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100775 LOGVAL(snode->module->ctx, LY_VCODE_NOMAX, snode->name);
776 goto failure;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100777 }
778
Michal Vaskoa3881362020-01-21 15:57:35 +0100779 return LY_SUCCESS;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100780
781failure:
Radek Krejciddace2c2021-01-08 11:30:56 +0100782 LOG_LOCBACK(0, invalid_instance, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100783 return LY_EVALID;
Michal Vaskoa3881362020-01-21 15:57:35 +0100784}
785
Michal Vaskobb844672020-07-03 11:06:12 +0200786/**
787 * @brief Find node referenced by a list unique statement.
788 *
789 * @param[in] uniq_leaf Unique leaf to find.
790 * @param[in] list List instance to use for the search.
791 * @return Found leaf,
792 * @return NULL if no leaf found.
793 */
Michal Vasko14654712020-02-06 08:35:21 +0100794static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200795lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100796{
Michal Vasko9b368d32020-02-14 13:53:31 +0100797 struct lyd_node *node;
798 const struct lysc_node *iter;
799 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100800
Michal Vasko9b368d32020-02-14 13:53:31 +0100801 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200802 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
803 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100804 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100805
Michal Vaskobb844672020-07-03 11:06:12 +0200806 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100807 while (node && depth) {
808 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200809 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
810 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100811 }
812
813 /* find iter instance in children */
814 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Radek Krejcia1c1e542020-09-29 16:06:52 +0200815 lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100816 --depth;
817 }
818
Michal Vasko14654712020-02-06 08:35:21 +0100819 return node;
820}
821
Michal Vaskobb844672020-07-03 11:06:12 +0200822/**
823 * @brief Callback for comparing 2 list unique leaf values.
824 *
Radek Krejci857189e2020-09-01 13:26:36 +0200825 * Implementation of ::values_equal_cb.
826 *
Michal Vaskobb844672020-07-03 11:06:12 +0200827 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100828 */
Radek Krejci857189e2020-09-01 13:26:36 +0200829static ly_bool
830lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Michal Vasko14654712020-02-06 08:35:21 +0100831{
832 struct ly_ctx *ctx;
833 struct lysc_node_list *slist;
834 struct lyd_node *diter, *first, *second;
835 struct lyd_value *val1, *val2;
836 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200837 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100838
839 assert(val1_p && val2_p);
840
841 first = *((struct lyd_node **)val1_p);
842 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200843 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100844
845 assert(first && (first->schema->nodetype == LYS_LIST));
846 assert(second && (second->schema == first->schema));
847
848 ctx = first->schema->module->ctx;
849
850 slist = (struct lysc_node_list *)first->schema;
851
852 /* compare unique leaves */
853 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200854 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200855 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100856 goto uniquecheck;
857 }
858 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200859 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100860uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200861 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100862 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200863 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100864 if (diter) {
865 val1 = &((struct lyd_node_term *)diter)->value;
866 } else {
867 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200868 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100869 }
870
871 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200872 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100873 if (diter) {
874 val2 = &((struct lyd_node_term *)diter)->value;
875 } else {
876 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200877 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100878 }
879
880 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
881 /* values differ or either one is not set */
882 break;
883 }
884 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200885 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100886 /* all unique leafs are the same in this set, create this nice error */
Radek Krejci635d2b82021-01-04 11:26:51 +0100887 path1 = lyd_path(first, LYD_PATH_STD, NULL, 0);
888 path2 = lyd_path(second, LYD_PATH_STD, NULL, 0);
Michal Vasko14654712020-02-06 08:35:21 +0100889
890 /* use buffer to rebuild the unique string */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100891#define UNIQ_BUF_SIZE 1024
892 uniq_str = malloc(UNIQ_BUF_SIZE);
Michal Vasko14654712020-02-06 08:35:21 +0100893 uniq_str[0] = '\0';
894 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200895 LY_ARRAY_FOR(slist->uniques[u], v) {
896 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100897 strcpy(ptr, " ");
898 ++ptr;
899 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200900 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 +0100901 ptr, UNIQ_BUF_SIZE - (ptr - uniq_str));
Michal Vasko14654712020-02-06 08:35:21 +0100902 if (!ptr) {
903 /* path will be incomplete, whatever */
904 break;
905 }
906
907 ptr += strlen(ptr);
908 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100909 LOG_LOCSET(NULL, second, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100910 LOGVAL(ctx, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
Radek Krejciddace2c2021-01-08 11:30:56 +0100911 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko14654712020-02-06 08:35:21 +0100912
913 free(path1);
914 free(path2);
915 free(uniq_str);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100916#undef UNIQ_BUF_SIZE
917
Michal Vasko14654712020-02-06 08:35:21 +0100918 return 1;
919 }
920
921 if (action > 0) {
922 /* done */
923 return 0;
924 }
925 }
926
927 return 0;
928}
929
Michal Vaskobb844672020-07-03 11:06:12 +0200930/**
931 * @brief Validate list unique leaves.
932 *
933 * @param[in] first First sibling to search in.
934 * @param[in] snode Schema node to validate.
935 * @param[in] uniques List unique arrays to validate.
936 * @return LY_ERR value.
937 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100938static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200939lyd_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 +0100940{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100941 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100942 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200943 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100944 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200945 uint32_t hash, i, size = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200946 ly_bool dynamic;
Michal Vasko14654712020-02-06 08:35:21 +0100947 const char *str;
948 struct hash_table **uniqtables = NULL;
949 struct lyd_value *val;
950 struct ly_ctx *ctx = snode->module->ctx;
951
952 assert(uniques);
953
954 /* get all list instances */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200955 LY_CHECK_RET(ly_set_new(&set));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100956 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100957 if (diter->schema == snode) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200958 ret = ly_set_add(set, (void *)diter, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200959 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko9b368d32020-02-14 13:53:31 +0100960 }
961 }
Michal Vasko14654712020-02-06 08:35:21 +0100962
963 if (set->count == 2) {
964 /* simple comparison */
965 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
966 /* instance duplication */
967 ret = LY_EVALID;
968 goto cleanup;
969 }
970 } else if (set->count > 2) {
971 /* use hashes for comparison */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100972 /* first, allocate the table, the size depends on number of items in the set,
973 * the following code detects number of upper zero bits in the items' counter value ... */
974 for (i = (sizeof set->count * CHAR_BIT) - 1; i > 0; i--) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200975 size = set->count << i;
976 size = size >> i;
977 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100978 break;
979 }
980 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200981 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100982 /* ... and then we convert it to the position of the highest non-zero bit ... */
983 i = (sizeof set->count * CHAR_BIT) - i;
984 /* ... 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 +0200985 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100986
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200987 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100988 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200989 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200990 for (v = 0; v < x; v++) {
991 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
992 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100993 }
994
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200995 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100996 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200997 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100998 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200999 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001000 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +01001001 if (diter) {
1002 val = &((struct lyd_node_term *)diter)->value;
1003 } else {
1004 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001005 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +01001006 }
1007 if (!val) {
1008 /* unique item not present nor has default value */
1009 break;
1010 }
1011
1012 /* get canonical string value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001013 str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +01001014 hash = dict_hash_multi(hash, str, strlen(str));
1015 if (dynamic) {
1016 free((char *)str);
1017 }
1018 }
1019 if (!val) {
1020 /* skip this list instance since its unique set is incomplete */
1021 continue;
1022 }
1023
1024 /* finish the hash value */
1025 hash = dict_hash_multi(hash, NULL, 0);
1026
1027 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001028 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +01001029 if (ret == LY_EEXIST) {
1030 /* instance duplication */
1031 ret = LY_EVALID;
1032 }
1033 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
1034 }
1035 }
1036 }
1037
1038cleanup:
1039 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001040 for (v = 0; v < x; v++) {
1041 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +01001042 /* failed when allocating uniquetables[j], following j are not allocated */
1043 break;
1044 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001045 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +01001046 }
1047 free(uniqtables);
1048
1049 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +01001050}
1051
Michal Vaskobb844672020-07-03 11:06:12 +02001052/**
1053 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
1054 *
1055 * @param[in] first First sibling to search in.
Michal Vaskobd4db892020-11-23 16:58:20 +01001056 * @param[in] parent Data parent.
Michal Vaskobb844672020-07-03 11:06:12 +02001057 * @param[in] sparent Schema parent of the nodes to check.
1058 * @param[in] mod Module of the nodes to check.
1059 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
1060 * @param[in] op Operation being validated, if any.
1061 * @return LY_ERR value.
1062 */
Michal Vaskoa3881362020-01-21 15:57:35 +01001063static LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +01001064lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lyd_node *parent,
1065 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 +01001066{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001067 LY_ERR ret = LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +01001068 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +01001069 struct lysc_node_list *slist;
Michal Vaskod8958df2020-08-05 13:27:36 +02001070 struct lysc_node_leaflist *sllist;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001071 uint32_t getnext_opts;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001072
Radek Krejci7931b192020-06-25 17:05:03 +02001073 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001074
Michal Vaskoa3881362020-01-21 15:57:35 +01001075 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001076 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +02001077 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001078 continue;
1079 }
1080
Radek Krejciddace2c2021-01-08 11:30:56 +01001081 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001082
Michal Vaskoa3881362020-01-21 15:57:35 +01001083 /* check min-elements and max-elements */
Michal Vaskod8958df2020-08-05 13:27:36 +02001084 if (snode->nodetype == LYS_LIST) {
Michal Vaskoa3881362020-01-21 15:57:35 +01001085 slist = (struct lysc_node_list *)snode;
1086 if (slist->min || slist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001087 ret = lyd_validate_minmax(first, parent, snode, slist->min, slist->max);
1088 LY_CHECK_GOTO(ret, error);
Michal Vaskoa3881362020-01-21 15:57:35 +01001089 }
Michal Vaskod8958df2020-08-05 13:27:36 +02001090 } else if (snode->nodetype == LYS_LEAFLIST) {
1091 sllist = (struct lysc_node_leaflist *)snode;
1092 if (sllist->min || sllist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001093 ret = lyd_validate_minmax(first, parent, snode, sllist->min, sllist->max);
1094 LY_CHECK_GOTO(ret, error);
Michal Vaskod8958df2020-08-05 13:27:36 +02001095 }
Michal Vaskoacd83e72020-02-04 14:12:01 +01001096
Michal Vaskoacd83e72020-02-04 14:12:01 +01001097 } else if (snode->flags & LYS_MAND_TRUE) {
Radek Krejcif6a11002020-08-21 13:29:07 +02001098 /* check generic mandatory existence */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001099 ret = lyd_validate_mandatory(first, parent, snode);
1100 LY_CHECK_GOTO(ret, error);
Michal Vaskoa3881362020-01-21 15:57:35 +01001101 }
1102
1103 /* check unique */
1104 if (snode->nodetype == LYS_LIST) {
1105 slist = (struct lysc_node_list *)snode;
1106 if (slist->uniques) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001107 ret = lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques);
1108 LY_CHECK_GOTO(ret, error);
Michal Vaskoa3881362020-01-21 15:57:35 +01001109 }
1110 }
1111
Michal Vaskoacd83e72020-02-04 14:12:01 +01001112 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
1113 /* go recursively for schema-only nodes */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001114 ret = lyd_validate_siblings_schema_r(first, parent, snode, mod, val_opts, op);
1115 LY_CHECK_GOTO(ret, error);
Michal Vaskoacd83e72020-02-04 14:12:01 +01001116 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001117
Radek Krejciddace2c2021-01-08 11:30:56 +01001118 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001119 }
1120
Michal Vaskoacd83e72020-02-04 14:12:01 +01001121 return LY_SUCCESS;
Radek Krejci2efc45b2020-12-22 16:25:44 +01001122
1123error:
Radek Krejciddace2c2021-01-08 11:30:56 +01001124 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001125 return ret;
Michal Vaskoacd83e72020-02-04 14:12:01 +01001126}
1127
Michal Vaskobb844672020-07-03 11:06:12 +02001128/**
1129 * @brief Validate obsolete nodes, only warnings are printed.
1130 *
1131 * @param[in] node Node to check.
1132 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001133static void
1134lyd_validate_obsolete(const struct lyd_node *node)
1135{
1136 const struct lysc_node *snode;
1137
1138 snode = node->schema;
1139 do {
1140 if (snode->flags & LYS_STATUS_OBSLT) {
1141 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
1142 break;
1143 }
1144
1145 snode = snode->parent;
1146 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
1147}
1148
Michal Vaskobb844672020-07-03 11:06:12 +02001149/**
1150 * @brief Validate must conditions of a data node.
1151 *
1152 * @param[in] node Node to validate.
1153 * @param[in] op Operation being validated, if any.
1154 * @return LY_ERR value.
1155 */
Michal Vaskocc048b22020-03-27 15:52:38 +01001156static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +02001157lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +01001158{
1159 struct lyxp_set xp_set;
1160 struct lysc_must *musts;
1161 const struct lyd_node *tree;
Radek Krejci9a3823e2021-01-27 20:26:46 +01001162 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001163 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +01001164
Radek Krejci9a3823e2021-01-27 20:26:46 +01001165 if (node->schema->nodetype & (LYS_ACTION | LYS_RPC)) {
Radek Krejci7931b192020-06-25 17:05:03 +02001166 if (op == LYD_VALIDATE_OP_RPC) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001167 schema = &((struct lysc_node_action *)node->schema)->input.node;
Radek Krejci7931b192020-06-25 17:05:03 +02001168 } else if (op == LYD_VALIDATE_OP_REPLY) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001169 schema = &((struct lysc_node_action *)node->schema)->output.node;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001170 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001171 LOGINT(LYD_CTX(node));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001172 return LY_EINT;
1173 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001174 } else {
1175 schema = node->schema;
Michal Vaskocc048b22020-03-27 15:52:38 +01001176 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001177 musts = lysc_node_musts(schema);
Michal Vaskocc048b22020-03-27 15:52:38 +01001178 if (!musts) {
1179 /* no must to evaluate */
1180 return LY_SUCCESS;
1181 }
1182
1183 /* find first top-level node */
Radek Krejci1e008d22020-08-17 11:37:37 +02001184 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent) {}
Michal Vaskocc048b22020-03-27 15:52:38 +01001185 while (tree->prev->next) {
1186 tree = tree->prev;
1187 }
1188
1189 LY_ARRAY_FOR(musts, u) {
1190 memset(&xp_set, 0, sizeof xp_set);
1191
1192 /* evaluate must */
Michal Vasko400e9672021-01-11 13:39:17 +01001193 LY_CHECK_RET(lyxp_eval(LYD_CTX(node), musts[u].cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED,
1194 musts[u].prefixes, node, tree, &xp_set, LYXP_SCHEMA));
Michal Vaskocc048b22020-03-27 15:52:38 +01001195
1196 /* check the result */
1197 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02001198 if (!xp_set.val.bln) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001199 LOGVAL(LYD_CTX(node), LY_VCODE_NOMUST, musts[u].cond->expr);
Michal Vaskocc048b22020-03-27 15:52:38 +01001200 return LY_EVALID;
1201 }
1202 }
1203
1204 return LY_SUCCESS;
1205}
1206
Michal Vaskob1b5c262020-03-05 14:29:47 +01001207LY_ERR
Michal Vaskobd4db892020-11-23 16:58:20 +01001208lyd_validate_final_r(struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *sparent,
1209 const struct lys_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +01001210{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001211 const char *innode = NULL;
Radek Krejci7f769d72020-07-11 23:13:56 +02001212 struct lyd_node *next = NULL, *node;
Michal Vaskoacd83e72020-02-04 14:12:01 +01001213
Michal Vasko14654712020-02-06 08:35:21 +01001214 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001215 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001216 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001217 /* all top-level data from this module checked */
1218 break;
Michal Vaskof03ed032020-03-04 13:31:44 +01001219 }
1220
Radek Krejciddace2c2021-01-08 11:30:56 +01001221 LOG_LOCSET(node->schema, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001222
Michal Vaskoa8c61722020-03-27 16:59:32 +01001223 /* opaque data */
1224 if (!node->schema) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001225 LOGVAL(LYD_CTX(node), LYVE_DATA, "Opaque node \"%s\" found.", ((struct lyd_node_opaq *)node)->name.name);
Radek Krejciddace2c2021-01-08 11:30:56 +01001226 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskoa8c61722020-03-27 16:59:32 +01001227 return LY_EVALID;
1228 }
1229
Michal Vaskocb7526d2020-03-30 15:08:26 +02001230 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +02001231 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001232 innode = "state";
1233 goto invalid_node;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001234 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_IS_OUTPUT)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001235 innode = "output";
1236 goto invalid_node;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001237 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_IS_INPUT)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001238 innode = "input";
1239 goto invalid_node;
Michal Vasko5b37a352020-03-06 13:38:33 +01001240 }
1241
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001242 /* obsolete data */
1243 lyd_validate_obsolete(node);
1244
Michal Vaskocc048b22020-03-27 15:52:38 +01001245 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001246 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001247
Michal Vasko53d97a12020-11-05 17:39:10 +01001248 /* node value was checked by plugins */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001249
Radek Krejciddace2c2021-01-08 11:30:56 +01001250 LOG_LOCBACK(1, 1, 0, 0);
Michal Vasko14654712020-02-06 08:35:21 +01001251 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001252
Michal Vasko14654712020-02-06 08:35:21 +01001253 /* validate schema-based restrictions */
Michal Vaskobd4db892020-11-23 16:58:20 +01001254 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 +01001255
Michal Vaskob1b5c262020-03-05 14:29:47 +01001256 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001257 /* validate all children recursively */
Michal Vaskobd4db892020-11-23 16:58:20 +01001258 LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node, node->schema, NULL, val_opts, op));
Michal Vaskocde73ac2019-11-14 16:10:27 +01001259
Michal Vaskob1b5c262020-03-05 14:29:47 +01001260 /* set default for containers */
1261 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Radek Krejcia1c1e542020-09-29 16:06:52 +02001262 LY_LIST_FOR(lyd_child(node), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001263 if (!(next->flags & LYD_DEFAULT)) {
1264 break;
1265 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001266 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001267 if (!next) {
1268 node->flags |= LYD_DEFAULT;
1269 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001270 }
1271 }
1272
1273 return LY_SUCCESS;
Radek Krejci2efc45b2020-12-22 16:25:44 +01001274
1275invalid_node:
1276 LOGVAL(LYD_CTX(node), LY_VCODE_INNODE, innode, node->schema->name);
Radek Krejciddace2c2021-01-08 11:30:56 +01001277 LOG_LOCBACK(1, 1, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001278 return LY_EVALID;
Michal Vasko9b368d32020-02-14 13:53:31 +01001279}
1280
Radek Krejci7931b192020-06-25 17:05:03 +02001281/**
Michal Vaskobb844672020-07-03 11:06:12 +02001282 * @brief Validate the whole data subtree.
1283 *
1284 * @param[in] root Subtree root.
Michal Vasko32711382020-12-03 14:14:31 +01001285 * @param[in,out] node_types Set for unres node types.
1286 * @param[in,out] meta_types Set for unres metadata types.
1287 * @param[in,out] node_when Set for nodes with when conditions.
Michal Vasko29adfbe2020-12-08 17:12:03 +01001288 * @param[in] impl_opts Implicit options, see @ref implicitoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001289 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001290 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001291 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001292static LY_ERR
Michal Vasko32711382020-12-03 14:14:31 +01001293lyd_validate_subtree(struct lyd_node *root, struct ly_set *node_types, struct ly_set *meta_types,
Michal Vasko29adfbe2020-12-08 17:12:03 +01001294 struct ly_set *node_when, uint32_t impl_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001295{
1296 const struct lyd_meta *meta;
Michal Vasko56daf732020-08-10 10:57:18 +02001297 struct lyd_node *node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001298
Michal Vasko56daf732020-08-10 10:57:18 +02001299 LYD_TREE_DFS_BEGIN(root, node) {
Michal Vasko0275cf62020-11-05 17:40:30 +01001300 LY_LIST_FOR(node->meta, meta) {
1301 if (((struct lyext_metadata *)meta->annotation->data)->type->plugin->validate) {
1302 /* metadata type resolution */
Michal Vasko32711382020-12-03 14:14:31 +01001303 LY_CHECK_RET(ly_set_add(meta_types, (void *)meta, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001304 }
Michal Vasko0275cf62020-11-05 17:40:30 +01001305 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001306
Michal Vasko0275cf62020-11-05 17:40:30 +01001307 if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
1308 /* node type resolution */
Michal Vasko32711382020-12-03 14:14:31 +01001309 LY_CHECK_RET(ly_set_add(node_types, (void *)node, 1, NULL));
Michal Vasko0275cf62020-11-05 17:40:30 +01001310 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1311 /* new node validation, autodelete */
1312 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 +02001313
Michal Vasko0275cf62020-11-05 17:40:30 +01001314 /* add nested defaults */
Michal Vaskobd4db892020-11-23 16:58:20 +01001315 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
Michal Vasko29adfbe2020-12-08 17:12:03 +01001316 NULL, impl_opts, diff));
Michal Vasko0275cf62020-11-05 17:40:30 +01001317 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001318
Michal Vaskod1e53b92021-01-28 13:11:06 +01001319 if (lysc_node_when(node->schema)) {
Michal Vasko0275cf62020-11-05 17:40:30 +01001320 /* when evaluation */
Michal Vasko32711382020-12-03 14:14:31 +01001321 LY_CHECK_RET(ly_set_add(node_when, (void *)node, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001322 }
1323
Michal Vasko56daf732020-08-10 10:57:18 +02001324 LYD_TREE_DFS_END(root, node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001325 }
1326
1327 return LY_SUCCESS;
1328}
1329
Michal Vaskobb844672020-07-03 11:06:12 +02001330/**
1331 * @brief Validate data tree.
1332 *
1333 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1334 * @param[in] modules Array of modules to validate, NULL for all modules.
1335 * @param[in] mod_count Count of @p modules.
1336 * @param[in] ly_ctx libyang context.
1337 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001338 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001339 * @return LY_ERR value.
1340 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001341static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001342lyd_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 +02001343 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001344{
1345 LY_ERR ret = LY_SUCCESS;
Michal Vasko73e47212020-12-03 14:20:16 +01001346 struct lyd_node *first, *next, **first2, *iter;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001347 const struct lys_module *mod;
Michal Vasko32711382020-12-03 14:14:31 +01001348 struct ly_set node_types = {0}, meta_types = {0}, node_when = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001349 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001350
Michal Vasko26e80012020-07-08 10:55:46 +02001351 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko5a532f32020-12-10 12:18:47 +01001352 if (!ctx && !module) {
1353 ctx = LYD_CTX(*tree);
1354 }
Michal Vasko8104fd42020-07-13 11:09:51 +02001355 if (diff) {
1356 *diff = NULL;
1357 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001358
Michal Vaskob1b5c262020-03-05 14:29:47 +01001359 next = *tree;
1360 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001361 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001362 mod = lyd_data_next_module(&next, &first);
1363 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001364 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001365 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001366 if (!mod) {
1367 break;
1368 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001369 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001370 /* make sure first2 changes are carried to tree */
1371 first2 = tree;
1372 } else {
1373 first2 = &first;
1374 }
1375
1376 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001377 ret = lyd_validate_new(first2, NULL, mod, diff);
1378 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001379
Michal Vasko0275cf62020-11-05 17:40:30 +01001380 /* add all top-level defaults for this module, do not add into unres sets, will occur in the next step */
1381 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, NULL, NULL, val_opts & LYD_VALIDATE_NO_STATE ?
Michal Vasko69730152020-10-09 16:30:07 +02001382 LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001383 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001384
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001385 /* our first module node pointer may no longer be the first */
1386 while (*first2 && (*first2)->prev->next && (lyd_owner_module(*first2) == lyd_owner_module((*first2)->prev))) {
1387 *first2 = (*first2)->prev;
1388 }
1389
Michal Vaskob1b5c262020-03-05 14:29:47 +01001390 /* process nested nodes */
Michal Vasko73e47212020-12-03 14:20:16 +01001391 LY_LIST_FOR(*first2, iter) {
Michal Vasko79135ae2020-12-16 10:08:35 +01001392 ret = lyd_validate_subtree(iter, &node_types, &meta_types, &node_when, val_opts & LYD_VALIDATE_NO_STATE ?
1393 LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001394 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001395 }
1396
1397 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001398 ret = lyd_validate_unres(first2, mod, &node_when, &node_types, &meta_types, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001399 LY_CHECK_GOTO(ret, cleanup);
1400
1401 /* perform final validation that assumes the data tree is final */
Michal Vaskobd4db892020-11-23 16:58:20 +01001402 ret = lyd_validate_final_r(*first2, NULL, NULL, mod, val_opts, 0);
Michal Vasko8104fd42020-07-13 11:09:51 +02001403 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001404 }
1405
Michal Vaskof03ed032020-03-04 13:31:44 +01001406cleanup:
Michal Vasko32711382020-12-03 14:14:31 +01001407 ly_set_erase(&node_types, NULL);
1408 ly_set_erase(&meta_types, NULL);
1409 ly_set_erase(&node_when, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001410 return ret;
1411}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001412
1413API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001414lyd_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 +01001415{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001416 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001417}
1418
1419API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001420lyd_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 +01001421{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001422 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001423}
Michal Vaskofea12c62020-03-30 11:00:15 +02001424
Michal Vaskobb844672020-07-03 11:06:12 +02001425/**
1426 * @brief Find nodes for merging an operation into data tree for validation.
1427 *
1428 * @param[in] op_tree Full operation data tree.
1429 * @param[in] op_node Operation node itself.
1430 * @param[in] tree Data tree to be merged into.
1431 * @param[out] op_subtree Operation subtree to merge.
Michal Vasko2f03d222020-12-09 18:15:51 +01001432 * @param[out] tree_sibling Data tree sibling to merge next to, is set if @p tree_parent is NULL.
1433 * @param[out] tree_parent Data tree parent to merge into, is set if @p tree_sibling is NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001434 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001435static void
Michal Vaskobb844672020-07-03 11:06:12 +02001436lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
Michal Vasko2f03d222020-12-09 18:15:51 +01001437 struct lyd_node **op_subtree, struct lyd_node **tree_sibling, struct lyd_node **tree_parent)
Michal Vaskofea12c62020-03-30 11:00:15 +02001438{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001439 const struct lyd_node *tree_iter, *op_iter;
1440 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001441 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001442
Michal Vasko2f03d222020-12-09 18:15:51 +01001443 *op_subtree = NULL;
1444 *tree_sibling = NULL;
1445 *tree_parent = NULL;
1446
Michal Vaskocb7526d2020-03-30 15:08:26 +02001447 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001448 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001449 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001450 ++op_depth;
1451 }
1452
1453 /* find where to merge op */
1454 tree_iter = tree;
1455 cur_depth = op_depth;
Michal Vasko2f03d222020-12-09 18:15:51 +01001456 while (cur_depth && tree_iter) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001457 /* find op iter in tree */
1458 lyd_find_sibling_first(tree_iter, op_iter, &match);
1459 if (!match) {
1460 break;
1461 }
1462
1463 /* move tree_iter */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001464 tree_iter = lyd_child(match);
Michal Vaskofea12c62020-03-30 11:00:15 +02001465
1466 /* move depth */
1467 --cur_depth;
Michal Vasko2f03d222020-12-09 18:15:51 +01001468
1469 /* find next op parent */
1470 op_iter = op_node;
1471 for (i = 0; i < cur_depth; ++i) {
1472 op_iter = (struct lyd_node *)op_iter->parent;
1473 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001474 }
1475
Michal Vasko2f03d222020-12-09 18:15:51 +01001476 assert(op_iter);
Michal Vaskobb844672020-07-03 11:06:12 +02001477 *op_subtree = (struct lyd_node *)op_iter;
Michal Vasko2f03d222020-12-09 18:15:51 +01001478 if (!tree || tree_iter) {
1479 /* there is no tree whatsoever or this is the last found sibling */
1480 *tree_sibling = (struct lyd_node *)tree_iter;
1481 } else {
1482 /* matching parent was found but it has no children to insert next to */
1483 assert(match);
1484 *tree_parent = match;
1485 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001486}
1487
1488API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001489lyd_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 +02001490{
1491 LY_ERR ret;
Michal Vasko2f03d222020-12-09 18:15:51 +01001492 struct lyd_node *tree_sibling, *tree_parent, *op_subtree, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001493 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1494
1495 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci0f969882020-08-21 16:56:47 +02001496 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001497 if (diff) {
1498 *diff = NULL;
1499 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001500
1501 /* find the operation/notification */
Michal Vasko56daf732020-08-10 10:57:18 +02001502 LYD_TREE_DFS_BEGIN(op_tree, op_node) {
Michal Vasko69730152020-10-09 16:30:07 +02001503 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 +02001504 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001505 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001506 break;
1507 }
Michal Vasko56daf732020-08-10 10:57:18 +02001508 LYD_TREE_DFS_END(op_tree, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001509 }
Michal Vasko69730152020-10-09 16:30:07 +02001510 if ((op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY)) {
Radek Krejci7931b192020-06-25 17:05:03 +02001511 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001512 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001513 return LY_EINVAL;
1514 }
1515 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001516 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001517 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001518 return LY_EINVAL;
1519 }
1520 }
1521
1522 /* move op_tree to top-level node */
1523 while (op_tree->parent) {
1524 op_tree = (struct lyd_node *)op_tree->parent;
1525 }
1526
1527 /* merge op_tree into tree */
Michal Vasko2f03d222020-12-09 18:15:51 +01001528 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling, &tree_parent);
1529 op_parent = lyd_parent(op_subtree);
Radek Krejci7931b192020-06-25 17:05:03 +02001530 lyd_unlink_tree(op_subtree);
Michal Vasko2f03d222020-12-09 18:15:51 +01001531 lyd_insert_node(tree_parent, &tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001532 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001533 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001534 }
1535
Radek Krejciddace2c2021-01-08 11:30:56 +01001536 LOG_LOCSET(NULL, op_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001537
Michal Vaskofea12c62020-03-30 11:00:15 +02001538 /* prevalidate whole operation subtree */
Michal Vasko29adfbe2020-12-08 17:12:03 +01001539 LY_CHECK_GOTO(ret = lyd_validate_subtree(op_node, &type_check, &type_meta_check, &when_check,
1540 op == LYD_VALIDATE_OP_REPLY ? LYD_IMPLICIT_OUTPUT : 0, diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001541
1542 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001543 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, NULL, &when_check, &type_check, &type_meta_check,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001544 diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001545
1546 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001547 lyd_validate_obsolete(op_node);
Radek Krejci7931b192020-06-25 17:05:03 +02001548 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001549
1550 /* final validation of all the descendants */
Michal Vaskobd4db892020-11-23 16:58:20 +01001551 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 +02001552
1553cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01001554 LOG_LOCBACK(0, 1, 0, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001555 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001556 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001557 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001558 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001559 }
1560
1561 ly_set_erase(&type_check, NULL);
1562 ly_set_erase(&type_meta_check, NULL);
1563 ly_set_erase(&when_check, NULL);
1564 return ret;
1565}