blob: 4fc7158a3c06187f97044fd6937f824eb7b9d840 [file] [log] [blame]
Michal Vaskocde73ac2019-11-14 16:10:27 +01001/**
2 * @file validation.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Validation
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vaskofbed4ea2020-07-08 10:43:30 +020014#include "validation.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010015
16#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include <stdint.h>
Michal Vasko52927e22020-03-16 17:26:14 +010018#include <stdio.h>
19#include <stdlib.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <string.h>
Michal Vaskocde73ac2019-11-14 16:10:27 +010021
Michal Vaskoc5a22832020-08-20 13:21:33 +020022#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vasko8104fd42020-07-13 11:09:51 +020024#include "diff.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "hash_table.h"
26#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020027#include "parser_data.h"
Michal Vaskofeca4fb2020-10-05 08:58:40 +020028#include "plugins_exts_metadata.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "plugins_types.h"
30#include "set.h"
31#include "tree.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010032#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010034#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010036
Michal Vaskoa6669ba2020-08-06 16:14:26 +020037LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +020038lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
39{
40 LY_ERR ret = LY_SUCCESS;
41 struct lyd_node *new_diff = NULL;
42
43 assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
44
45 /* create new diff tree */
46 LY_CHECK_RET(lyd_diff_add(node, op, NULL, NULL, NULL, NULL, NULL, &new_diff));
47
48 /* merge into existing diff */
Michal Vaskofb737aa2020-08-06 13:53:53 +020049 ret = lyd_diff_merge_all(diff, new_diff);
Michal Vasko8104fd42020-07-13 11:09:51 +020050
51 lyd_free_tree(new_diff);
52 return ret;
53}
54
55/**
Michal Vaskocde73ac2019-11-14 16:10:27 +010056 * @brief Evaluate a single "when" condition.
57 *
Michal Vaskob1b5c262020-03-05 14:29:47 +010058 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +010059 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +010060 * @param[in] when When to evaluate.
Michal Vasko8104fd42020-07-13 11:09:51 +020061 * @param[in,out] diff Validation diff.
Michal Vaskocde73ac2019-11-14 16:10:27 +010062 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
63 */
64static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +020065lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when, struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +010066{
Michal Vasko8104fd42020-07-13 11:09:51 +020067 LY_ERR ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +010068 const struct lyd_node *ctx_node;
69 struct lyxp_set xp_set;
70
71 memset(&xp_set, 0, sizeof xp_set);
72
73 if (when->context == node->schema) {
74 ctx_node = node;
75 } else {
76 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
77 ctx_node = (struct lyd_node *)node->parent;
78 }
79
80 /* evaluate when */
Michal Vaskoc8a230d2020-08-14 12:17:10 +020081 ret = lyxp_eval(when->cond, LY_PREF_SCHEMA, when->module, ctx_node, ctx_node ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG,
Michal Vaskob1b5c262020-03-05 14:29:47 +010082 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +010083 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
84
85 /* return error or LY_EINCOMPLETE for dependant unresolved when */
86 LY_CHECK_RET(ret);
87
88 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +020089 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +010090 if (node->flags & LYD_WHEN_TRUE) {
91 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +010092 if (LYD_DEL_IS_ROOT(*tree, node)) {
93 *tree = (*tree)->next;
94 }
Michal Vasko8104fd42020-07-13 11:09:51 +020095 if (diff) {
96 /* add into diff */
97 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff));
98 }
Michal Vaskocde73ac2019-11-14 16:10:27 +010099 lyd_free_tree(node);
100 } else {
101 /* invalid data */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200102 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vasko8104fd42020-07-13 11:09:51 +0200103 return LY_EVALID;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100104 }
105 } else {
106 /* remember that when evaluated to true */
107 node->flags |= LYD_WHEN_TRUE;
108 }
109
110 return ret;
111}
112
113LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100114lyd_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 +0200115 struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100116{
117 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200118 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100119
Michal Vaskob1b5c262020-03-05 14:29:47 +0100120 if (node_when) {
121 /* evaluate all when conditions */
122 uint32_t prev_count;
123 do {
124 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200125 i = 0;
126 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100127 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200128 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100129 const struct lysc_node *schema = node->schema;
Radek Krejci857189e2020-09-01 13:26:36 +0200130 ly_bool unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100131
Michal Vaskob1b5c262020-03-05 14:29:47 +0100132 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200133 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200134 LY_ARRAY_FOR(schema->when, u) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200135 ret = lyd_validate_when(tree, node, schema->when[u], diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100136 if (ret) {
137 break;
138 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100139 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100140 if (ret == LY_EINCOMPLETE) {
141 /* could not evaluate this when */
142 unres_when = 1;
143 break;
144 } else if (ret) {
145 /* error */
146 return ret;
147 }
148 schema = schema->parent;
149 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100150
Michal Vaskob1b5c262020-03-05 14:29:47 +0100151 if (unres_when) {
152 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200153 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100154 } else {
155 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200156 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100157 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100158 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100159
Radek Krejci0f969882020-08-21 16:56:47 +0200160 /* there must have been some when conditions resolved */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100161 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100162
Michal Vaskob1b5c262020-03-05 14:29:47 +0100163 /* there could have been no cyclic when dependencies, checked during compilation */
164 assert(!node_when->count);
165 }
166
167 if (node_types && node_types->count) {
168 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200169 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100170 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200171 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100172
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200173 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200174 struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100175
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200176 /* resolve the value of the node */
177 ret = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, (struct lyd_node *)node, *tree,
178 LY_VLOG_LYD, node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100179 LY_CHECK_RET(ret);
180
181 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200182 ly_set_rm_index(node_types, i, NULL);
183 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100184 }
185
Michal Vasko9f96a052020-03-10 09:41:45 +0100186 if (meta_types && meta_types->count) {
187 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200188 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100189 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200190 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100191
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200192 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200193 struct lysc_type *type = ((struct lyext_metadata *)meta->annotation->data)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100194
Michal Vasko9f96a052020-03-10 09:41:45 +0100195 /* validate and store the value of the metadata */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200196 ret = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree,
197 LY_VLOG_NONE, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100198 LY_CHECK_RET(ret);
199
200 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200201 ly_set_rm_index(meta_types, i, NULL);
202 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100203 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100204
205 return ret;
206}
207
Michal Vaskobb844672020-07-03 11:06:12 +0200208/**
209 * @brief Validate instance duplication.
210 *
211 * @param[in] first First sibling to search in.
212 * @param[in] node Data node instance to check.
213 * @return LY_ERR value.
214 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100215static LY_ERR
216lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100217{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100218 struct lyd_node **match_p;
Radek Krejci857189e2020-09-01 13:26:36 +0200219 ly_bool fail = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100220
221 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
222 /* duplicate instances allowed */
223 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100224 }
225
Michal Vaskob1b5c262020-03-05 14:29:47 +0100226 /* find exactly the same next instance using hashes if possible */
227 if (node->parent && node->parent->children_ht) {
228 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
229 fail = 1;
230 }
231 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200232 for ( ; first; first = first->next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100233 if (first == node) {
234 continue;
235 }
236
237 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
238 if (first->schema == node->schema) {
239 fail = 1;
240 break;
241 }
Michal Vasko8f359bf2020-07-28 10:41:15 +0200242 } else if (!lyd_compare_single(first, node, 0)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100243 fail = 1;
244 break;
245 }
246 }
247 }
248
249 if (fail) {
250 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
251 return LY_EVALID;
252 }
253 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100254}
255
Michal Vaskobb844672020-07-03 11:06:12 +0200256/**
257 * @brief Validate multiple case data existence with possible autodelete.
258 *
259 * @param[in,out] first First sibling to search in, is updated if needed.
260 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200261 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200262 * @return LY_ERR value.
263 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100264static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200265lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100266{
267 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
268 struct lyd_node *match, *to_del;
Radek Krejci857189e2020-09-01 13:26:36 +0200269 ly_bool found;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100270
271 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
272 found = 0;
273 iter = NULL;
274 match = NULL;
275 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
276 if (match->flags & LYD_NEW) {
277 /* a new case data found, nothing more to look for */
278 found = 2;
279 break;
280 } else {
281 /* and old case data found */
282 if (found == 0) {
283 found = 1;
284 }
285 }
286 }
287
288 if (found == 1) {
289 /* there should not be 2 old cases */
290 if (old_case) {
291 /* old data from 2 cases */
292 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
293 return LY_EVALID;
294 }
295
296 /* remember an old existing case */
297 old_case = scase;
298 } else if (found == 2) {
299 if (new_case) {
300 /* new data from 2 cases */
301 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
302 return LY_EVALID;
303 }
304
305 /* remember a new existing case */
306 new_case = scase;
307 }
308 }
309
310 if (old_case && new_case) {
311 /* auto-delete old case */
312 iter = NULL;
313 match = NULL;
314 to_del = NULL;
315 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
316 if (LYD_DEL_IS_ROOT(*first, to_del)) {
317 *first = (*first)->next;
318 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200319 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100320 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200321 if (diff) {
322 /* add into diff */
323 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
324 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100325 to_del = match;
326 }
327 if (LYD_DEL_IS_ROOT(*first, to_del)) {
328 *first = (*first)->next;
329 }
330 lyd_free_tree(to_del);
331 }
332
333 return LY_SUCCESS;
334}
335
Michal Vaskobb844672020-07-03 11:06:12 +0200336/**
337 * @brief Check whether a schema node can have some default values (true for NP containers as well).
338 *
339 * @param[in] schema Schema node to check.
340 * @return non-zero if yes,
341 * @return 0 otherwise.
342 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100343static int
344lyd_val_has_default(const struct lysc_node *schema)
345{
346 switch (schema->nodetype) {
347 case LYS_LEAF:
348 if (((struct lysc_node_leaf *)schema)->dflt) {
349 return 1;
350 }
351 break;
352 case LYS_LEAFLIST:
353 if (((struct lysc_node_leaflist *)schema)->dflts) {
354 return 1;
355 }
356 break;
357 case LYS_CONTAINER:
358 if (!(schema->flags & LYS_PRESENCE)) {
359 return 1;
360 }
361 break;
362 default:
363 break;
364 }
365
366 return 0;
367}
368
Michal Vaskobb844672020-07-03 11:06:12 +0200369/**
370 * @brief Autodelete old instances to prevent validation errors.
371 *
372 * @param[in,out] first First sibling to search in, is updated if needed.
373 * @param[in] node Data node instance to check.
374 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200375 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200376 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100377static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200378lyd_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 +0100379{
Michal Vasko8104fd42020-07-13 11:09:51 +0200380 struct lyd_node *match, *next, *iter;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100381
382 if (lyd_val_has_default(node->schema)) {
383 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200384 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100385 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
386 /* default instance found, remove it */
387 if (LYD_DEL_IS_ROOT(*first, match)) {
388 *first = (*first)->next;
389 }
390 if (match == *next_p) {
391 *next_p = (*next_p)->next;
392 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200393 if (diff) {
394 /* add into diff */
395 if ((match->schema->nodetype == LYS_CONTAINER) && !(match->schema->flags & LYS_PRESENCE)) {
396 /* we do not want to track NP container changes, but remember any removed children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200397 LY_LIST_FOR(lyd_child(match), iter) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200398 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
399 }
400 } else {
401 lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff);
402 }
403 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100404 lyd_free_tree(match);
405
406 /* remove only a single container/leaf default instance, if there are more, it is an error */
407 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
408 break;
409 }
410 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100411 }
412 }
413}
414
415LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200416lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200417 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100418{
419 struct lyd_node *next, *node;
420 const struct lysc_node *snode = NULL;
421
422 assert(first && (sparent || mod));
423
424 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
425 /* check case duplicites */
426 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200427 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100428 }
429 }
430
431 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100432 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100433 /* all top-level data from this module checked */
434 break;
435 }
436
437 if (!(node->flags & LYD_NEW)) {
438 /* check only new nodes */
439 continue;
440 }
441
442 /* remove old default(s) if it exists */
Michal Vasko8104fd42020-07-13 11:09:51 +0200443 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100444
445 /* then check new node instance duplicities */
446 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
447
448 /* this node is valid */
449 node->flags &= ~LYD_NEW;
450 }
451
452 return LY_SUCCESS;
453}
454
Michal Vaskobb844672020-07-03 11:06:12 +0200455/**
456 * @brief Validate mandatory node existence.
457 *
458 * @param[in] first First sibling to search in.
459 * @param[in] snode Schema node to validate.
460 * @return LY_ERR value.
461 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100462static LY_ERR
463lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100464{
Michal Vaskoa3881362020-01-21 15:57:35 +0100465 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100466 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100467 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100468 return LY_SUCCESS;
469 }
470 } else {
471 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100472
Michal Vaskob1b5c262020-03-05 14:29:47 +0100473 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100474 /* data instance found */
475 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100476 }
477 }
478
479 /* node instance not found */
480 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
481 return LY_EVALID;
482}
483
Michal Vaskobb844672020-07-03 11:06:12 +0200484/**
485 * @brief Validate min/max-elements constraints, if any.
486 *
487 * @param[in] first First sibling to search in.
488 * @param[in] snode Schema node to validate.
489 * @param[in] min Minimum number of elements, 0 for no restriction.
490 * @param[in] max Max number of elements, 0 for no restriction.
491 * @return LY_ERR value.
492 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100493static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100494lyd_validate_minmax(const struct lyd_node *first, const struct lysc_node *snode, uint32_t min, uint32_t max)
Michal Vaskoa3881362020-01-21 15:57:35 +0100495{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100496 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200497 struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100498
Michal Vasko9b368d32020-02-14 13:53:31 +0100499 assert(min || max);
500
Michal Vasko4c583e82020-07-17 12:16:14 +0200501 LYD_LIST_FOR_INST(first, snode, iter) {
502 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100503
Michal Vasko4c583e82020-07-17 12:16:14 +0200504 if (min && (count == min)) {
505 /* satisfied */
506 min = 0;
507 if (!max) {
508 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100509 break;
510 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100511 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200512 if (max && (count > max)) {
513 /* not satisifed */
514 break;
515 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100516 }
517
Michal Vasko9b368d32020-02-14 13:53:31 +0100518 if (min) {
519 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100520 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
521 return LY_EVALID;
522 } else if (max && (count > max)) {
523 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
524 return LY_EVALID;
525 }
526
Michal Vaskoa3881362020-01-21 15:57:35 +0100527 return LY_SUCCESS;
528}
529
Michal Vaskobb844672020-07-03 11:06:12 +0200530/**
531 * @brief Find node referenced by a list unique statement.
532 *
533 * @param[in] uniq_leaf Unique leaf to find.
534 * @param[in] list List instance to use for the search.
535 * @return Found leaf,
536 * @return NULL if no leaf found.
537 */
Michal Vasko14654712020-02-06 08:35:21 +0100538static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200539lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100540{
Michal Vasko9b368d32020-02-14 13:53:31 +0100541 struct lyd_node *node;
542 const struct lysc_node *iter;
543 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100544
Michal Vasko9b368d32020-02-14 13:53:31 +0100545 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200546 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
547 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100548 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100549
Michal Vaskobb844672020-07-03 11:06:12 +0200550 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100551 while (node && depth) {
552 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200553 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
554 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100555 }
556
557 /* find iter instance in children */
558 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Radek Krejcia1c1e542020-09-29 16:06:52 +0200559 lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100560 --depth;
561 }
562
Michal Vasko14654712020-02-06 08:35:21 +0100563 return node;
564}
565
Michal Vaskobb844672020-07-03 11:06:12 +0200566/**
567 * @brief Callback for comparing 2 list unique leaf values.
568 *
Radek Krejci857189e2020-09-01 13:26:36 +0200569 * Implementation of ::values_equal_cb.
570 *
Michal Vaskobb844672020-07-03 11:06:12 +0200571 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100572 */
Radek Krejci857189e2020-09-01 13:26:36 +0200573static ly_bool
574lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Michal Vasko14654712020-02-06 08:35:21 +0100575{
576 struct ly_ctx *ctx;
577 struct lysc_node_list *slist;
578 struct lyd_node *diter, *first, *second;
579 struct lyd_value *val1, *val2;
580 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200581 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100582
583 assert(val1_p && val2_p);
584
585 first = *((struct lyd_node **)val1_p);
586 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200587 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100588
589 assert(first && (first->schema->nodetype == LYS_LIST));
590 assert(second && (second->schema == first->schema));
591
592 ctx = first->schema->module->ctx;
593
594 slist = (struct lysc_node_list *)first->schema;
595
596 /* compare unique leaves */
597 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200598 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200599 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100600 goto uniquecheck;
601 }
602 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200603 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100604uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200605 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100606 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200607 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100608 if (diter) {
609 val1 = &((struct lyd_node_term *)diter)->value;
610 } else {
611 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200612 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100613 }
614
615 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200616 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100617 if (diter) {
618 val2 = &((struct lyd_node_term *)diter)->value;
619 } else {
620 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200621 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100622 }
623
624 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
625 /* values differ or either one is not set */
626 break;
627 }
628 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200629 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100630 /* all unique leafs are the same in this set, create this nice error */
631 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
632 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
633
634 /* use buffer to rebuild the unique string */
635 uniq_str = malloc(1024);
636 uniq_str[0] = '\0';
637 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200638 LY_ARRAY_FOR(slist->uniques[u], v) {
639 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100640 strcpy(ptr, " ");
641 ++ptr;
642 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200643 ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], (struct lysc_node *)slist, LYSC_PATH_LOG,
Michal Vasko14654712020-02-06 08:35:21 +0100644 ptr, 1024 - (ptr - uniq_str));
645 if (!ptr) {
646 /* path will be incomplete, whatever */
647 break;
648 }
649
650 ptr += strlen(ptr);
651 }
652 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
653
654 free(path1);
655 free(path2);
656 free(uniq_str);
657 return 1;
658 }
659
660 if (action > 0) {
661 /* done */
662 return 0;
663 }
664 }
665
666 return 0;
667}
668
Michal Vaskobb844672020-07-03 11:06:12 +0200669/**
670 * @brief Validate list unique leaves.
671 *
672 * @param[in] first First sibling to search in.
673 * @param[in] snode Schema node to validate.
674 * @param[in] uniques List unique arrays to validate.
675 * @return LY_ERR value.
676 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100677static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200678lyd_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 +0100679{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100680 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100681 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200682 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100683 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200684 uint32_t hash, i, size = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200685 ly_bool dynamic;
Michal Vasko14654712020-02-06 08:35:21 +0100686 const char *str;
687 struct hash_table **uniqtables = NULL;
688 struct lyd_value *val;
689 struct ly_ctx *ctx = snode->module->ctx;
690
691 assert(uniques);
692
693 /* get all list instances */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200694 LY_CHECK_RET(ly_set_new(&set));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100695 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100696 if (diter->schema == snode) {
Michal Vaskob0099a92020-08-31 14:55:23 +0200697 ret = ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST, NULL);
698 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko9b368d32020-02-14 13:53:31 +0100699 }
700 }
Michal Vasko14654712020-02-06 08:35:21 +0100701
702 if (set->count == 2) {
703 /* simple comparison */
704 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
705 /* instance duplication */
706 ret = LY_EVALID;
707 goto cleanup;
708 }
709 } else if (set->count > 2) {
710 /* use hashes for comparison */
711 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200712 for (i = 31; i > 0; i--) {
713 size = set->count << i;
714 size = size >> i;
715 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100716 break;
717 }
718 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200719 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
720 i = 32 - i;
721 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100722
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200723 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100724 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200725 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200726 for (v = 0; v < x; v++) {
727 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
728 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100729 }
730
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200731 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100732 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200733 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100734 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200735 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200736 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100737 if (diter) {
738 val = &((struct lyd_node_term *)diter)->value;
739 } else {
740 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200741 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100742 }
743 if (!val) {
744 /* unique item not present nor has default value */
745 break;
746 }
747
748 /* get canonical string value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200749 str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +0100750 hash = dict_hash_multi(hash, str, strlen(str));
751 if (dynamic) {
752 free((char *)str);
753 }
754 }
755 if (!val) {
756 /* skip this list instance since its unique set is incomplete */
757 continue;
758 }
759
760 /* finish the hash value */
761 hash = dict_hash_multi(hash, NULL, 0);
762
763 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200764 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100765 if (ret == LY_EEXIST) {
766 /* instance duplication */
767 ret = LY_EVALID;
768 }
769 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
770 }
771 }
772 }
773
774cleanup:
775 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200776 for (v = 0; v < x; v++) {
777 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100778 /* failed when allocating uniquetables[j], following j are not allocated */
779 break;
780 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200781 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100782 }
783 free(uniqtables);
784
785 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100786}
787
Michal Vaskobb844672020-07-03 11:06:12 +0200788/**
789 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
790 *
791 * @param[in] first First sibling to search in.
792 * @param[in] sparent Schema parent of the nodes to check.
793 * @param[in] mod Module of the nodes to check.
794 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
795 * @param[in] op Operation being validated, if any.
796 * @return LY_ERR value.
797 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100798static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100799lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200800 const struct lysc_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100801{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100802 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100803 struct lysc_node_list *slist;
Michal Vaskod8958df2020-08-05 13:27:36 +0200804 struct lysc_node_leaflist *sllist;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200805 uint32_t getnext_opts;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200806
Radek Krejci7931b192020-06-25 17:05:03 +0200807 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100808
Michal Vaskoa3881362020-01-21 15:57:35 +0100809 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200810 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200811 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100812 continue;
813 }
814
Michal Vaskoa3881362020-01-21 15:57:35 +0100815 /* check min-elements and max-elements */
Michal Vaskod8958df2020-08-05 13:27:36 +0200816 if (snode->nodetype == LYS_LIST) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100817 slist = (struct lysc_node_list *)snode;
818 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100819 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100820 }
Michal Vaskod8958df2020-08-05 13:27:36 +0200821 } else if (snode->nodetype == LYS_LEAFLIST) {
822 sllist = (struct lysc_node_leaflist *)snode;
823 if (sllist->min || sllist->max) {
824 LY_CHECK_RET(lyd_validate_minmax(first, snode, sllist->min, sllist->max));
825 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100826
Michal Vaskoacd83e72020-02-04 14:12:01 +0100827 } else if (snode->flags & LYS_MAND_TRUE) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200828 /* check generic mandatory existence */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100829 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100830 }
831
832 /* check unique */
833 if (snode->nodetype == LYS_LIST) {
834 slist = (struct lysc_node_list *)snode;
835 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200836 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100837 }
838 }
839
Michal Vaskoacd83e72020-02-04 14:12:01 +0100840 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
841 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200842 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100843 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100844 }
845
Michal Vaskoacd83e72020-02-04 14:12:01 +0100846 return LY_SUCCESS;
847}
848
Michal Vaskobb844672020-07-03 11:06:12 +0200849/**
850 * @brief Validate obsolete nodes, only warnings are printed.
851 *
852 * @param[in] node Node to check.
853 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100854static void
855lyd_validate_obsolete(const struct lyd_node *node)
856{
857 const struct lysc_node *snode;
858
859 snode = node->schema;
860 do {
861 if (snode->flags & LYS_STATUS_OBSLT) {
862 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
863 break;
864 }
865
866 snode = snode->parent;
867 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
868}
869
Michal Vaskobb844672020-07-03 11:06:12 +0200870/**
871 * @brief Validate must conditions of a data node.
872 *
873 * @param[in] node Node to validate.
874 * @param[in] op Operation being validated, if any.
875 * @return LY_ERR value.
876 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100877static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200878lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100879{
880 struct lyxp_set xp_set;
881 struct lysc_must *musts;
882 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200883 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100884
885 switch (node->schema->nodetype) {
886 case LYS_CONTAINER:
887 musts = ((struct lysc_node_container *)node->schema)->musts;
888 break;
889 case LYS_LEAF:
890 musts = ((struct lysc_node_leaf *)node->schema)->musts;
891 break;
892 case LYS_LEAFLIST:
893 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
894 break;
895 case LYS_LIST:
896 musts = ((struct lysc_node_list *)node->schema)->musts;
897 break;
898 case LYS_ANYXML:
899 case LYS_ANYDATA:
900 musts = ((struct lysc_node_anydata *)node->schema)->musts;
901 break;
902 case LYS_NOTIF:
903 musts = ((struct lysc_notif *)node->schema)->musts;
904 break;
905 case LYS_RPC:
906 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200907 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200908 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200909 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200910 musts = ((struct lysc_action *)node->schema)->output.musts;
911 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200912 LOGINT(LYD_CTX(node));
Michal Vaskocb7526d2020-03-30 15:08:26 +0200913 return LY_EINT;
914 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100915 break;
916 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200917 LOGINT(LYD_CTX(node));
Michal Vaskocc048b22020-03-27 15:52:38 +0100918 return LY_EINT;
919 }
920
921 if (!musts) {
922 /* no must to evaluate */
923 return LY_SUCCESS;
924 }
925
926 /* find first top-level node */
Radek Krejci1e008d22020-08-17 11:37:37 +0200927 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent) {}
Michal Vaskocc048b22020-03-27 15:52:38 +0100928 while (tree->prev->next) {
929 tree = tree->prev;
930 }
931
932 LY_ARRAY_FOR(musts, u) {
933 memset(&xp_set, 0, sizeof xp_set);
934
935 /* evaluate must */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200936 LY_CHECK_RET(lyxp_eval(musts[u].cond, LY_PREF_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set,
937 LYXP_SCHEMA));
Michal Vaskocc048b22020-03-27 15:52:38 +0100938
939 /* check the result */
940 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200941 if (!xp_set.val.bln) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200942 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
Michal Vaskocc048b22020-03-27 15:52:38 +0100943 return LY_EVALID;
944 }
945 }
946
947 return LY_SUCCESS;
948}
949
Michal Vaskob1b5c262020-03-05 14:29:47 +0100950LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200951lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, uint32_t val_opts,
Radek Krejci0f969882020-08-21 16:56:47 +0200952 LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100953{
Radek Krejci7f769d72020-07-11 23:13:56 +0200954 struct lyd_node *next = NULL, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100955 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100956
Michal Vasko14654712020-02-06 08:35:21 +0100957 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100958 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100959 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100960 /* all top-level data from this module checked */
961 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100962 }
963
Michal Vaskoa8c61722020-03-27 16:59:32 +0100964 /* opaque data */
965 if (!node->schema) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200966 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
Radek Krejci0f969882020-08-21 16:56:47 +0200967 ((struct lyd_node_opaq *)node)->name);
Michal Vaskoa8c61722020-03-27 16:59:32 +0100968 return LY_EVALID;
969 }
970
Michal Vaskocb7526d2020-03-30 15:08:26 +0200971 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +0200972 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200973 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +0200974 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200975 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200976 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +0200977 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200978 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200979 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +0100980 return LY_EVALID;
981 }
982
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100983 /* obsolete data */
984 lyd_validate_obsolete(node);
985
Michal Vaskoc193ce92020-03-06 11:04:48 +0100986 /* node's schema if-features */
987 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200988 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +0100989 return LY_EVALID;
990 }
991
Michal Vaskocc048b22020-03-27 15:52:38 +0100992 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +0200993 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +0100994
Michal Vaskoa8c61722020-03-27 16:59:32 +0100995 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +0100996 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100997
Michal Vasko14654712020-02-06 08:35:21 +0100998 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +0200999 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001000
Michal Vaskob1b5c262020-03-05 14:29:47 +01001001 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001002 /* validate all children recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001003 LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node->schema, NULL, val_opts, op));
Michal Vaskocde73ac2019-11-14 16:10:27 +01001004
Michal Vaskob1b5c262020-03-05 14:29:47 +01001005 /* set default for containers */
1006 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Radek Krejcia1c1e542020-09-29 16:06:52 +02001007 LY_LIST_FOR(lyd_child(node), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001008 if (!(next->flags & LYD_DEFAULT)) {
1009 break;
1010 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001011 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001012 if (!next) {
1013 node->flags |= LYD_DEFAULT;
1014 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001015 }
1016 }
1017
1018 return LY_SUCCESS;
1019}
1020
Radek Krejci7931b192020-06-25 17:05:03 +02001021/**
Michal Vaskobb844672020-07-03 11:06:12 +02001022 * @brief Validate the whole data subtree.
1023 *
1024 * @param[in] root Subtree root.
1025 * @param[in,out] type_check Set for unres node types.
1026 * @param[in,out] type_meta_check Set for unres metadata types.
1027 * @param[in,out] when_check Set for nodes with when conditions.
1028 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001029 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001030 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001031 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001032static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001033lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001034 struct ly_set *when_check, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001035{
1036 const struct lyd_meta *meta;
Michal Vasko56daf732020-08-10 10:57:18 +02001037 struct lyd_node *node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001038
Michal Vasko56daf732020-08-10 10:57:18 +02001039 LYD_TREE_DFS_BEGIN(root, node) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001040 /* skip added default nodes */
1041 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1042 LY_LIST_FOR(node->meta, meta) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001043 if (((struct lyext_metadata *)meta->annotation->data)->type->plugin->validate) {
1044 /* metadata type resolution */
1045 LY_CHECK_RET(ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST, NULL));
1046 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001047 }
1048
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001049 if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001050 /* node type resolution */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001051 LY_CHECK_RET(ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001052 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1053 /* new node validation, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001054 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 +02001055
1056 /* add nested defaults */
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001057 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
1058 when_check, val_opts & LYD_VALIDATE_NO_STATE ? LYD_IMPLICIT_NO_STATE : 0,
1059 diff));
Michal Vaskofea12c62020-03-30 11:00:15 +02001060 }
1061
1062 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1063 /* when evaluation */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001064 LY_CHECK_RET(ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001065 }
1066 }
1067
Michal Vasko56daf732020-08-10 10:57:18 +02001068 LYD_TREE_DFS_END(root, node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001069 }
1070
1071 return LY_SUCCESS;
1072}
1073
Michal Vaskobb844672020-07-03 11:06:12 +02001074/**
1075 * @brief Validate data tree.
1076 *
1077 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1078 * @param[in] modules Array of modules to validate, NULL for all modules.
1079 * @param[in] mod_count Count of @p modules.
1080 * @param[in] ly_ctx libyang context.
1081 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001082 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001083 * @return LY_ERR value.
1084 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001085static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001086lyd_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 +02001087 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001088{
1089 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001090 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001091 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001092 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001093 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001094
Michal Vasko26e80012020-07-08 10:55:46 +02001095 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001096 if (diff) {
1097 *diff = NULL;
1098 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001099
Michal Vaskob1b5c262020-03-05 14:29:47 +01001100 next = *tree;
1101 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001102 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001103 mod = lyd_data_next_module(&next, &first);
1104 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001105 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001106 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001107 if (!mod) {
1108 break;
1109 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001110 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001111 /* make sure first2 changes are carried to tree */
1112 first2 = tree;
1113 } else {
1114 first2 = &first;
1115 }
1116
1117 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001118 ret = lyd_validate_new(first2, NULL, mod, diff);
1119 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001120
1121 /* add all top-level defaults for this module */
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001122 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &type_check, &when_check, val_opts & LYD_VALIDATE_NO_STATE
1123 ? LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001124 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001125
1126 /* process nested nodes */
1127 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001128 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1129 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001130 }
1131
1132 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001133 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001134 LY_CHECK_GOTO(ret, cleanup);
1135
1136 /* perform final validation that assumes the data tree is final */
Michal Vasko8104fd42020-07-13 11:09:51 +02001137 ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0);
1138 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001139 }
1140
Michal Vaskof03ed032020-03-04 13:31:44 +01001141cleanup:
1142 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001143 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001144 ly_set_erase(&when_check, NULL);
1145 return ret;
1146}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001147
1148API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001149lyd_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 +01001150{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001151 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001152}
1153
1154API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001155lyd_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 +01001156{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001157 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001158}
Michal Vaskofea12c62020-03-30 11:00:15 +02001159
Michal Vaskobb844672020-07-03 11:06:12 +02001160/**
1161 * @brief Find nodes for merging an operation into data tree for validation.
1162 *
1163 * @param[in] op_tree Full operation data tree.
1164 * @param[in] op_node Operation node itself.
1165 * @param[in] tree Data tree to be merged into.
1166 * @param[out] op_subtree Operation subtree to merge.
1167 * @param[out] tree_sibling Data tree sibling to merge next to.
1168 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001169static void
Michal Vaskobb844672020-07-03 11:06:12 +02001170lyd_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 +02001171 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001172{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001173 const struct lyd_node *tree_iter, *op_iter;
1174 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001175 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001176
Michal Vaskocb7526d2020-03-30 15:08:26 +02001177 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001178 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001179 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001180 ++op_depth;
1181 }
1182
1183 /* find where to merge op */
1184 tree_iter = tree;
1185 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001186 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001187 while (cur_depth) {
1188 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001189 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001190 for (i = 0; i < cur_depth; ++i) {
1191 op_iter = (struct lyd_node *)op_iter->parent;
1192 }
1193
1194 /* find op iter in tree */
1195 lyd_find_sibling_first(tree_iter, op_iter, &match);
1196 if (!match) {
1197 break;
1198 }
1199
1200 /* move tree_iter */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001201 tree_iter = lyd_child(match);
Michal Vaskofea12c62020-03-30 11:00:15 +02001202
1203 /* move depth */
1204 --cur_depth;
1205 }
1206
Michal Vaskobb844672020-07-03 11:06:12 +02001207 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001208 *tree_sibling = (struct lyd_node *)tree_iter;
1209}
1210
1211API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001212lyd_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 +02001213{
1214 LY_ERR ret;
Michal Vasko56daf732020-08-10 10:57:18 +02001215 struct lyd_node *tree_sibling, *op_subtree, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001216 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1217
1218 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci0f969882020-08-21 16:56:47 +02001219 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001220 if (diff) {
1221 *diff = NULL;
1222 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001223
1224 /* find the operation/notification */
Michal Vasko56daf732020-08-10 10:57:18 +02001225 LYD_TREE_DFS_BEGIN(op_tree, op_node) {
Radek Krejci7931b192020-06-25 17:05:03 +02001226 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 +02001227 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001228 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001229 break;
1230 }
Michal Vasko56daf732020-08-10 10:57:18 +02001231 LYD_TREE_DFS_END(op_tree, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001232 }
Radek Krejci7931b192020-06-25 17:05:03 +02001233 if (op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) {
1234 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001235 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001236 return LY_EINVAL;
1237 }
1238 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001239 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001240 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001241 return LY_EINVAL;
1242 }
1243 }
1244
1245 /* move op_tree to top-level node */
1246 while (op_tree->parent) {
1247 op_tree = (struct lyd_node *)op_tree->parent;
1248 }
1249
1250 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001251 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1252 op_parent = (struct lyd_node *)op_subtree->parent;
1253 lyd_unlink_tree(op_subtree);
1254 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001255 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001256 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001257 }
1258
1259 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001260 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 +02001261
1262 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1263 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 +02001264 diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001265
1266 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001267 lyd_validate_obsolete(op_node);
1268 if (lysc_node_is_disabled(op_node->schema, 1)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001269 LOGVAL(LYD_CTX(op_tree), LY_VLOG_LYD, op_node, LY_VCODE_NOIFF, op_node->schema->name);
Michal Vaskofea12c62020-03-30 11:00:15 +02001270 ret = LY_EVALID;
1271 goto cleanup;
1272 }
Radek Krejci7931b192020-06-25 17:05:03 +02001273 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001274
1275 /* final validation of all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001276 LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_child(op_node), op_node->schema, NULL, 0, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001277
1278cleanup:
1279 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001280 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001281 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001282 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001283 }
1284
1285 ly_set_erase(&type_check, NULL);
1286 ly_set_erase(&type_meta_check, NULL);
1287 ly_set_erase(&when_check, NULL);
1288 return ret;
1289}