blob: 3eeacb8cdef3fc213fb7bc5b6be72cad73832858 [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
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020023#include "compat.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"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "tree_data.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010033#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010035#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010037
Michal Vaskoa6669ba2020-08-06 16:14:26 +020038LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +020039lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
40{
41 LY_ERR ret = LY_SUCCESS;
42 struct lyd_node *new_diff = NULL;
43
44 assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
45
46 /* create new diff tree */
47 LY_CHECK_RET(lyd_diff_add(node, op, NULL, NULL, NULL, NULL, NULL, &new_diff));
48
49 /* merge into existing diff */
Michal Vaskofb737aa2020-08-06 13:53:53 +020050 ret = lyd_diff_merge_all(diff, new_diff);
Michal Vasko8104fd42020-07-13 11:09:51 +020051
52 lyd_free_tree(new_diff);
53 return ret;
54}
55
56/**
Michal Vaskocde73ac2019-11-14 16:10:27 +010057 * @brief Evaluate a single "when" condition.
58 *
Michal Vaskob1b5c262020-03-05 14:29:47 +010059 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +010060 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +010061 * @param[in] when When to evaluate.
Michal Vasko8104fd42020-07-13 11:09:51 +020062 * @param[in,out] diff Validation diff.
Michal Vaskocde73ac2019-11-14 16:10:27 +010063 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
64 */
65static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +020066lyd_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 +010067{
Michal Vasko8104fd42020-07-13 11:09:51 +020068 LY_ERR ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +010069 const struct lyd_node *ctx_node;
70 struct lyxp_set xp_set;
71
72 memset(&xp_set, 0, sizeof xp_set);
73
74 if (when->context == node->schema) {
75 ctx_node = node;
76 } else {
77 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
78 ctx_node = (struct lyd_node *)node->parent;
79 }
80
81 /* evaluate when */
Michal Vasko5d24f6c2020-10-13 13:49:06 +020082 ret = lyxp_eval(when->cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes, ctx_node, *tree,
83 &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +010084 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
85
86 /* return error or LY_EINCOMPLETE for dependant unresolved when */
87 LY_CHECK_RET(ret);
88
89 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +020090 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +010091 if (node->flags & LYD_WHEN_TRUE) {
92 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +010093 if (LYD_DEL_IS_ROOT(*tree, node)) {
94 *tree = (*tree)->next;
95 }
Michal Vasko8104fd42020-07-13 11:09:51 +020096 if (diff) {
97 /* add into diff */
98 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff));
99 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100100 lyd_free_tree(node);
101 } else {
102 /* invalid data */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200103 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vasko8104fd42020-07-13 11:09:51 +0200104 return LY_EVALID;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100105 }
106 } else {
107 /* remember that when evaluated to true */
108 node->flags |= LYD_WHEN_TRUE;
109 }
110
111 return ret;
112}
113
114LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100115lyd_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 +0200116 struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100117{
118 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200119 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100120
Michal Vaskob1b5c262020-03-05 14:29:47 +0100121 if (node_when) {
122 /* evaluate all when conditions */
123 uint32_t prev_count;
124 do {
125 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200126 i = 0;
127 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100128 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200129 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100130 const struct lysc_node *schema = node->schema;
Radek Krejci857189e2020-09-01 13:26:36 +0200131 ly_bool unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100132
Michal Vaskob1b5c262020-03-05 14:29:47 +0100133 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200134 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200135 LY_ARRAY_FOR(schema->when, u) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200136 ret = lyd_validate_when(tree, node, schema->when[u], diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100137 if (ret) {
138 break;
139 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100140 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100141 if (ret == LY_EINCOMPLETE) {
142 /* could not evaluate this when */
143 unres_when = 1;
144 break;
145 } else if (ret) {
146 /* error */
147 return ret;
148 }
149 schema = schema->parent;
150 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100151
Michal Vaskob1b5c262020-03-05 14:29:47 +0100152 if (unres_when) {
153 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200154 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100155 } else {
156 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200157 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100158 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100159 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100160
Radek Krejci0f969882020-08-21 16:56:47 +0200161 /* there must have been some when conditions resolved */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100162 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100163
Michal Vaskob1b5c262020-03-05 14:29:47 +0100164 /* there could have been no cyclic when dependencies, checked during compilation */
165 assert(!node_when->count);
166 }
167
168 if (node_types && node_types->count) {
169 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200170 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100171 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200172 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100173
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200174 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200175 struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100176
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200177 /* resolve the value of the node */
178 ret = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, (struct lyd_node *)node, *tree,
179 LY_VLOG_LYD, node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100180 LY_CHECK_RET(ret);
181
182 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200183 ly_set_rm_index(node_types, i, NULL);
184 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100185 }
186
Michal Vasko9f96a052020-03-10 09:41:45 +0100187 if (meta_types && meta_types->count) {
188 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200189 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100190 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200191 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100192
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200193 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200194 struct lysc_type *type = ((struct lyext_metadata *)meta->annotation->data)->type;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100195
Michal Vasko9f96a052020-03-10 09:41:45 +0100196 /* validate and store the value of the metadata */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200197 ret = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree,
198 LY_VLOG_NONE, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100199 LY_CHECK_RET(ret);
200
201 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200202 ly_set_rm_index(meta_types, i, NULL);
203 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100204 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100205
206 return ret;
207}
208
Michal Vaskobb844672020-07-03 11:06:12 +0200209/**
210 * @brief Validate instance duplication.
211 *
212 * @param[in] first First sibling to search in.
213 * @param[in] node Data node instance to check.
214 * @return LY_ERR value.
215 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100216static LY_ERR
217lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100218{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100219 struct lyd_node **match_p;
Radek Krejci857189e2020-09-01 13:26:36 +0200220 ly_bool fail = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100221
222 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
223 /* duplicate instances allowed */
224 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100225 }
226
Michal Vaskob1b5c262020-03-05 14:29:47 +0100227 /* find exactly the same next instance using hashes if possible */
228 if (node->parent && node->parent->children_ht) {
229 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
230 fail = 1;
231 }
232 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200233 for ( ; first; first = first->next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100234 if (first == node) {
235 continue;
236 }
237
238 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
239 if (first->schema == node->schema) {
240 fail = 1;
241 break;
242 }
Michal Vasko8f359bf2020-07-28 10:41:15 +0200243 } else if (!lyd_compare_single(first, node, 0)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100244 fail = 1;
245 break;
246 }
247 }
248 }
249
250 if (fail) {
251 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
252 return LY_EVALID;
253 }
254 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100255}
256
Michal Vaskobb844672020-07-03 11:06:12 +0200257/**
258 * @brief Validate multiple case data existence with possible autodelete.
259 *
260 * @param[in,out] first First sibling to search in, is updated if needed.
261 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200262 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200263 * @return LY_ERR value.
264 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100265static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200266lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100267{
268 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
269 struct lyd_node *match, *to_del;
Radek Krejci857189e2020-09-01 13:26:36 +0200270 ly_bool found;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100271
272 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
273 found = 0;
274 iter = NULL;
275 match = NULL;
276 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
277 if (match->flags & LYD_NEW) {
278 /* a new case data found, nothing more to look for */
279 found = 2;
280 break;
281 } else {
282 /* and old case data found */
283 if (found == 0) {
284 found = 1;
285 }
286 }
287 }
288
289 if (found == 1) {
290 /* there should not be 2 old cases */
291 if (old_case) {
292 /* old data from 2 cases */
293 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
294 return LY_EVALID;
295 }
296
297 /* remember an old existing case */
298 old_case = scase;
299 } else if (found == 2) {
300 if (new_case) {
301 /* new data from 2 cases */
302 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
303 return LY_EVALID;
304 }
305
306 /* remember a new existing case */
307 new_case = scase;
308 }
309 }
310
311 if (old_case && new_case) {
312 /* auto-delete old case */
313 iter = NULL;
314 match = NULL;
315 to_del = NULL;
316 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
317 if (LYD_DEL_IS_ROOT(*first, to_del)) {
318 *first = (*first)->next;
319 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200320 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100321 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200322 if (diff) {
323 /* add into diff */
324 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
325 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100326 to_del = match;
327 }
328 if (LYD_DEL_IS_ROOT(*first, to_del)) {
329 *first = (*first)->next;
330 }
331 lyd_free_tree(to_del);
332 }
333
334 return LY_SUCCESS;
335}
336
Michal Vaskobb844672020-07-03 11:06:12 +0200337/**
338 * @brief Check whether a schema node can have some default values (true for NP containers as well).
339 *
340 * @param[in] schema Schema node to check.
341 * @return non-zero if yes,
342 * @return 0 otherwise.
343 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100344static int
345lyd_val_has_default(const struct lysc_node *schema)
346{
347 switch (schema->nodetype) {
348 case LYS_LEAF:
349 if (((struct lysc_node_leaf *)schema)->dflt) {
350 return 1;
351 }
352 break;
353 case LYS_LEAFLIST:
354 if (((struct lysc_node_leaflist *)schema)->dflts) {
355 return 1;
356 }
357 break;
358 case LYS_CONTAINER:
359 if (!(schema->flags & LYS_PRESENCE)) {
360 return 1;
361 }
362 break;
363 default:
364 break;
365 }
366
367 return 0;
368}
369
Michal Vaskobb844672020-07-03 11:06:12 +0200370/**
371 * @brief Autodelete old instances to prevent validation errors.
372 *
373 * @param[in,out] first First sibling to search in, is updated if needed.
374 * @param[in] node Data node instance to check.
375 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200376 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200377 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100378static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200379lyd_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 +0100380{
Michal Vasko8104fd42020-07-13 11:09:51 +0200381 struct lyd_node *match, *next, *iter;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100382
383 if (lyd_val_has_default(node->schema)) {
384 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200385 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100386 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
387 /* default instance found, remove it */
388 if (LYD_DEL_IS_ROOT(*first, match)) {
389 *first = (*first)->next;
390 }
391 if (match == *next_p) {
392 *next_p = (*next_p)->next;
393 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200394 if (diff) {
395 /* add into diff */
396 if ((match->schema->nodetype == LYS_CONTAINER) && !(match->schema->flags & LYS_PRESENCE)) {
397 /* we do not want to track NP container changes, but remember any removed children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200398 LY_LIST_FOR(lyd_child(match), iter) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200399 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
400 }
401 } else {
402 lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff);
403 }
404 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100405 lyd_free_tree(match);
406
407 /* remove only a single container/leaf default instance, if there are more, it is an error */
408 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
409 break;
410 }
411 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100412 }
413 }
414}
415
416LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200417lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200418 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100419{
420 struct lyd_node *next, *node;
421 const struct lysc_node *snode = NULL;
422
423 assert(first && (sparent || mod));
424
425 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
426 /* check case duplicites */
427 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200428 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100429 }
430 }
431
432 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100433 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100434 /* all top-level data from this module checked */
435 break;
436 }
437
438 if (!(node->flags & LYD_NEW)) {
439 /* check only new nodes */
440 continue;
441 }
442
443 /* remove old default(s) if it exists */
Michal Vasko8104fd42020-07-13 11:09:51 +0200444 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100445
446 /* then check new node instance duplicities */
447 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
448
449 /* this node is valid */
450 node->flags &= ~LYD_NEW;
451 }
452
453 return LY_SUCCESS;
454}
455
Michal Vaskobb844672020-07-03 11:06:12 +0200456/**
457 * @brief Validate mandatory node existence.
458 *
459 * @param[in] first First sibling to search in.
460 * @param[in] snode Schema node to validate.
461 * @return LY_ERR value.
462 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100463static LY_ERR
464lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100465{
Michal Vaskoa3881362020-01-21 15:57:35 +0100466 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100467 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100468 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100469 return LY_SUCCESS;
470 }
471 } else {
472 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100473
Michal Vaskob1b5c262020-03-05 14:29:47 +0100474 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100475 /* data instance found */
476 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100477 }
478 }
479
480 /* node instance not found */
481 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
482 return LY_EVALID;
483}
484
Michal Vaskobb844672020-07-03 11:06:12 +0200485/**
486 * @brief Validate min/max-elements constraints, if any.
487 *
488 * @param[in] first First sibling to search in.
489 * @param[in] snode Schema node to validate.
490 * @param[in] min Minimum number of elements, 0 for no restriction.
491 * @param[in] max Max number of elements, 0 for no restriction.
492 * @return LY_ERR value.
493 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100494static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100495lyd_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 +0100496{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100497 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200498 struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100499
Michal Vasko9b368d32020-02-14 13:53:31 +0100500 assert(min || max);
501
Michal Vasko4c583e82020-07-17 12:16:14 +0200502 LYD_LIST_FOR_INST(first, snode, iter) {
503 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100504
Michal Vasko4c583e82020-07-17 12:16:14 +0200505 if (min && (count == min)) {
506 /* satisfied */
507 min = 0;
508 if (!max) {
509 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100510 break;
511 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100512 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200513 if (max && (count > max)) {
514 /* not satisifed */
515 break;
516 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100517 }
518
Michal Vasko9b368d32020-02-14 13:53:31 +0100519 if (min) {
520 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100521 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
522 return LY_EVALID;
523 } else if (max && (count > max)) {
524 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
525 return LY_EVALID;
526 }
527
Michal Vaskoa3881362020-01-21 15:57:35 +0100528 return LY_SUCCESS;
529}
530
Michal Vaskobb844672020-07-03 11:06:12 +0200531/**
532 * @brief Find node referenced by a list unique statement.
533 *
534 * @param[in] uniq_leaf Unique leaf to find.
535 * @param[in] list List instance to use for the search.
536 * @return Found leaf,
537 * @return NULL if no leaf found.
538 */
Michal Vasko14654712020-02-06 08:35:21 +0100539static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200540lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100541{
Michal Vasko9b368d32020-02-14 13:53:31 +0100542 struct lyd_node *node;
543 const struct lysc_node *iter;
544 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100545
Michal Vasko9b368d32020-02-14 13:53:31 +0100546 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200547 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
548 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100549 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100550
Michal Vaskobb844672020-07-03 11:06:12 +0200551 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100552 while (node && depth) {
553 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200554 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
555 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100556 }
557
558 /* find iter instance in children */
559 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Radek Krejcia1c1e542020-09-29 16:06:52 +0200560 lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100561 --depth;
562 }
563
Michal Vasko14654712020-02-06 08:35:21 +0100564 return node;
565}
566
Michal Vaskobb844672020-07-03 11:06:12 +0200567/**
568 * @brief Callback for comparing 2 list unique leaf values.
569 *
Radek Krejci857189e2020-09-01 13:26:36 +0200570 * Implementation of ::values_equal_cb.
571 *
Michal Vaskobb844672020-07-03 11:06:12 +0200572 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100573 */
Radek Krejci857189e2020-09-01 13:26:36 +0200574static ly_bool
575lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
Michal Vasko14654712020-02-06 08:35:21 +0100576{
577 struct ly_ctx *ctx;
578 struct lysc_node_list *slist;
579 struct lyd_node *diter, *first, *second;
580 struct lyd_value *val1, *val2;
581 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200582 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100583
584 assert(val1_p && val2_p);
585
586 first = *((struct lyd_node **)val1_p);
587 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200588 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100589
590 assert(first && (first->schema->nodetype == LYS_LIST));
591 assert(second && (second->schema == first->schema));
592
593 ctx = first->schema->module->ctx;
594
595 slist = (struct lysc_node_list *)first->schema;
596
597 /* compare unique leaves */
598 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200599 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200600 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100601 goto uniquecheck;
602 }
603 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200604 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100605uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200606 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100607 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200608 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100609 if (diter) {
610 val1 = &((struct lyd_node_term *)diter)->value;
611 } else {
612 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200613 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100614 }
615
616 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200617 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100618 if (diter) {
619 val2 = &((struct lyd_node_term *)diter)->value;
620 } else {
621 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200622 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100623 }
624
625 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
626 /* values differ or either one is not set */
627 break;
628 }
629 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200630 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100631 /* all unique leafs are the same in this set, create this nice error */
632 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
633 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
634
635 /* use buffer to rebuild the unique string */
636 uniq_str = malloc(1024);
637 uniq_str[0] = '\0';
638 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200639 LY_ARRAY_FOR(slist->uniques[u], v) {
640 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100641 strcpy(ptr, " ");
642 ++ptr;
643 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200644 ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], (struct lysc_node *)slist, LYSC_PATH_LOG,
Michal Vasko69730152020-10-09 16:30:07 +0200645 ptr, 1024 - (ptr - uniq_str));
Michal Vasko14654712020-02-06 08:35:21 +0100646 if (!ptr) {
647 /* path will be incomplete, whatever */
648 break;
649 }
650
651 ptr += strlen(ptr);
652 }
653 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
654
655 free(path1);
656 free(path2);
657 free(uniq_str);
658 return 1;
659 }
660
661 if (action > 0) {
662 /* done */
663 return 0;
664 }
665 }
666
667 return 0;
668}
669
Michal Vaskobb844672020-07-03 11:06:12 +0200670/**
671 * @brief Validate list unique leaves.
672 *
673 * @param[in] first First sibling to search in.
674 * @param[in] snode Schema node to validate.
675 * @param[in] uniques List unique arrays to validate.
676 * @return LY_ERR value.
677 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100678static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200679lyd_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 +0100680{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100681 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100682 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200683 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100684 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200685 uint32_t hash, i, size = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200686 ly_bool dynamic;
Michal Vasko14654712020-02-06 08:35:21 +0100687 const char *str;
688 struct hash_table **uniqtables = NULL;
689 struct lyd_value *val;
690 struct ly_ctx *ctx = snode->module->ctx;
691
692 assert(uniques);
693
694 /* get all list instances */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200695 LY_CHECK_RET(ly_set_new(&set));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100696 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100697 if (diter->schema == snode) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200698 ret = ly_set_add(set, (void *)diter, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200699 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko9b368d32020-02-14 13:53:31 +0100700 }
701 }
Michal Vasko14654712020-02-06 08:35:21 +0100702
703 if (set->count == 2) {
704 /* simple comparison */
705 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
706 /* instance duplication */
707 ret = LY_EVALID;
708 goto cleanup;
709 }
710 } else if (set->count > 2) {
711 /* use hashes for comparison */
712 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200713 for (i = 31; i > 0; i--) {
714 size = set->count << i;
715 size = size >> i;
716 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100717 break;
718 }
719 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200720 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
721 i = 32 - i;
722 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100723
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200724 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100725 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200726 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200727 for (v = 0; v < x; v++) {
728 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
729 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100730 }
731
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200732 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100733 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200734 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100735 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200736 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200737 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100738 if (diter) {
739 val = &((struct lyd_node_term *)diter)->value;
740 } else {
741 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200742 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100743 }
744 if (!val) {
745 /* unique item not present nor has default value */
746 break;
747 }
748
749 /* get canonical string value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200750 str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +0100751 hash = dict_hash_multi(hash, str, strlen(str));
752 if (dynamic) {
753 free((char *)str);
754 }
755 }
756 if (!val) {
757 /* skip this list instance since its unique set is incomplete */
758 continue;
759 }
760
761 /* finish the hash value */
762 hash = dict_hash_multi(hash, NULL, 0);
763
764 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200765 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100766 if (ret == LY_EEXIST) {
767 /* instance duplication */
768 ret = LY_EVALID;
769 }
770 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
771 }
772 }
773 }
774
775cleanup:
776 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200777 for (v = 0; v < x; v++) {
778 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100779 /* failed when allocating uniquetables[j], following j are not allocated */
780 break;
781 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200782 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100783 }
784 free(uniqtables);
785
786 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100787}
788
Michal Vaskobb844672020-07-03 11:06:12 +0200789/**
790 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
791 *
792 * @param[in] first First sibling to search in.
793 * @param[in] sparent Schema parent of the nodes to check.
794 * @param[in] mod Module of the nodes to check.
795 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
796 * @param[in] op Operation being validated, if any.
797 * @return LY_ERR value.
798 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100799static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100800lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200801 const struct lysc_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100802{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100803 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100804 struct lysc_node_list *slist;
Michal Vaskod8958df2020-08-05 13:27:36 +0200805 struct lysc_node_leaflist *sllist;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200806 uint32_t getnext_opts;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200807
Radek Krejci7931b192020-06-25 17:05:03 +0200808 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100809
Michal Vaskoa3881362020-01-21 15:57:35 +0100810 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200811 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200812 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100813 continue;
814 }
815
Michal Vaskoa3881362020-01-21 15:57:35 +0100816 /* check min-elements and max-elements */
Michal Vaskod8958df2020-08-05 13:27:36 +0200817 if (snode->nodetype == LYS_LIST) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100818 slist = (struct lysc_node_list *)snode;
819 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100820 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100821 }
Michal Vaskod8958df2020-08-05 13:27:36 +0200822 } else if (snode->nodetype == LYS_LEAFLIST) {
823 sllist = (struct lysc_node_leaflist *)snode;
824 if (sllist->min || sllist->max) {
825 LY_CHECK_RET(lyd_validate_minmax(first, snode, sllist->min, sllist->max));
826 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100827
Michal Vaskoacd83e72020-02-04 14:12:01 +0100828 } else if (snode->flags & LYS_MAND_TRUE) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200829 /* check generic mandatory existence */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100830 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100831 }
832
833 /* check unique */
834 if (snode->nodetype == LYS_LIST) {
835 slist = (struct lysc_node_list *)snode;
836 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200837 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100838 }
839 }
840
Michal Vaskoacd83e72020-02-04 14:12:01 +0100841 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
842 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200843 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100844 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100845 }
846
Michal Vaskoacd83e72020-02-04 14:12:01 +0100847 return LY_SUCCESS;
848}
849
Michal Vaskobb844672020-07-03 11:06:12 +0200850/**
851 * @brief Validate obsolete nodes, only warnings are printed.
852 *
853 * @param[in] node Node to check.
854 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100855static void
856lyd_validate_obsolete(const struct lyd_node *node)
857{
858 const struct lysc_node *snode;
859
860 snode = node->schema;
861 do {
862 if (snode->flags & LYS_STATUS_OBSLT) {
863 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
864 break;
865 }
866
867 snode = snode->parent;
868 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
869}
870
Michal Vaskobb844672020-07-03 11:06:12 +0200871/**
872 * @brief Validate must conditions of a data node.
873 *
874 * @param[in] node Node to validate.
875 * @param[in] op Operation being validated, if any.
876 * @return LY_ERR value.
877 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100878static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200879lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100880{
881 struct lyxp_set xp_set;
882 struct lysc_must *musts;
883 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200884 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100885
886 switch (node->schema->nodetype) {
887 case LYS_CONTAINER:
888 musts = ((struct lysc_node_container *)node->schema)->musts;
889 break;
890 case LYS_LEAF:
891 musts = ((struct lysc_node_leaf *)node->schema)->musts;
892 break;
893 case LYS_LEAFLIST:
894 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
895 break;
896 case LYS_LIST:
897 musts = ((struct lysc_node_list *)node->schema)->musts;
898 break;
899 case LYS_ANYXML:
900 case LYS_ANYDATA:
901 musts = ((struct lysc_node_anydata *)node->schema)->musts;
902 break;
903 case LYS_NOTIF:
904 musts = ((struct lysc_notif *)node->schema)->musts;
905 break;
906 case LYS_RPC:
907 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200908 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200909 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200910 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200911 musts = ((struct lysc_action *)node->schema)->output.musts;
912 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200913 LOGINT(LYD_CTX(node));
Michal Vaskocb7526d2020-03-30 15:08:26 +0200914 return LY_EINT;
915 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100916 break;
917 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200918 LOGINT(LYD_CTX(node));
Michal Vaskocc048b22020-03-27 15:52:38 +0100919 return LY_EINT;
920 }
921
922 if (!musts) {
923 /* no must to evaluate */
924 return LY_SUCCESS;
925 }
926
927 /* find first top-level node */
Radek Krejci1e008d22020-08-17 11:37:37 +0200928 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent) {}
Michal Vaskocc048b22020-03-27 15:52:38 +0100929 while (tree->prev->next) {
930 tree = tree->prev;
931 }
932
933 LY_ARRAY_FOR(musts, u) {
934 memset(&xp_set, 0, sizeof xp_set);
935
936 /* evaluate must */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200937 LY_CHECK_RET(lyxp_eval(musts[u].cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED, musts[u].prefixes, node,
938 tree, &xp_set, LYXP_SCHEMA));
Michal Vaskocc048b22020-03-27 15:52:38 +0100939
940 /* check the result */
941 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200942 if (!xp_set.val.bln) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200943 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
Michal Vaskocc048b22020-03-27 15:52:38 +0100944 return LY_EVALID;
945 }
946 }
947
948 return LY_SUCCESS;
949}
950
Michal Vaskob1b5c262020-03-05 14:29:47 +0100951LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200952lyd_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 +0200953 LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100954{
Radek Krejci7f769d72020-07-11 23:13:56 +0200955 struct lyd_node *next = NULL, *node;
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 Vaskocc048b22020-03-27 15:52:38 +0100986 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +0200987 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +0100988
Michal Vasko53d97a12020-11-05 17:39:10 +0100989 /* node value was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +0100990 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100991
Michal Vasko14654712020-02-06 08:35:21 +0100992 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +0200993 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +0100994
Michal Vaskob1b5c262020-03-05 14:29:47 +0100995 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +0100996 /* validate all children recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200997 LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node->schema, NULL, val_opts, op));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100998
Michal Vaskob1b5c262020-03-05 14:29:47 +0100999 /* set default for containers */
1000 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Radek Krejcia1c1e542020-09-29 16:06:52 +02001001 LY_LIST_FOR(lyd_child(node), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001002 if (!(next->flags & LYD_DEFAULT)) {
1003 break;
1004 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001005 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001006 if (!next) {
1007 node->flags |= LYD_DEFAULT;
1008 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001009 }
1010 }
1011
1012 return LY_SUCCESS;
1013}
1014
Radek Krejci7931b192020-06-25 17:05:03 +02001015/**
Michal Vaskobb844672020-07-03 11:06:12 +02001016 * @brief Validate the whole data subtree.
1017 *
1018 * @param[in] root Subtree root.
1019 * @param[in,out] type_check Set for unres node types.
1020 * @param[in,out] type_meta_check Set for unres metadata types.
1021 * @param[in,out] when_check Set for nodes with when conditions.
1022 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001023 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +02001024 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001025 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001026static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001027lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001028 struct ly_set *when_check, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskofea12c62020-03-30 11:00:15 +02001029{
1030 const struct lyd_meta *meta;
Michal Vasko56daf732020-08-10 10:57:18 +02001031 struct lyd_node *node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001032
Michal Vasko56daf732020-08-10 10:57:18 +02001033 LYD_TREE_DFS_BEGIN(root, node) {
Michal Vasko0275cf62020-11-05 17:40:30 +01001034 LY_LIST_FOR(node->meta, meta) {
1035 if (((struct lyext_metadata *)meta->annotation->data)->type->plugin->validate) {
1036 /* metadata type resolution */
1037 LY_CHECK_RET(ly_set_add(type_meta_check, (void *)meta, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001038 }
Michal Vasko0275cf62020-11-05 17:40:30 +01001039 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001040
Michal Vasko0275cf62020-11-05 17:40:30 +01001041 if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
1042 /* node type resolution */
1043 LY_CHECK_RET(ly_set_add(type_check, (void *)node, 1, NULL));
1044 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1045 /* new node validation, autodelete */
1046 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 +02001047
Michal Vasko0275cf62020-11-05 17:40:30 +01001048 /* add nested defaults */
1049 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
1050 when_check, val_opts & LYD_VALIDATE_NO_STATE ? LYD_IMPLICIT_NO_STATE : 0, diff));
1051 }
Michal Vaskofea12c62020-03-30 11:00:15 +02001052
Michal Vasko0275cf62020-11-05 17:40:30 +01001053 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1054 /* when evaluation */
1055 LY_CHECK_RET(ly_set_add(when_check, (void *)node, 1, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001056 }
1057
Michal Vasko56daf732020-08-10 10:57:18 +02001058 LYD_TREE_DFS_END(root, node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001059 }
1060
1061 return LY_SUCCESS;
1062}
1063
Michal Vaskobb844672020-07-03 11:06:12 +02001064/**
1065 * @brief Validate data tree.
1066 *
1067 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1068 * @param[in] modules Array of modules to validate, NULL for all modules.
1069 * @param[in] mod_count Count of @p modules.
1070 * @param[in] ly_ctx libyang context.
1071 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001072 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001073 * @return LY_ERR value.
1074 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001075static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001076lyd_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 +02001077 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001078{
1079 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001080 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001081 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001082 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001083 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001084
Michal Vasko26e80012020-07-08 10:55:46 +02001085 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001086 if (diff) {
1087 *diff = NULL;
1088 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001089
Michal Vaskob1b5c262020-03-05 14:29:47 +01001090 next = *tree;
1091 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001092 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001093 mod = lyd_data_next_module(&next, &first);
1094 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001095 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001096 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001097 if (!mod) {
1098 break;
1099 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001100 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001101 /* make sure first2 changes are carried to tree */
1102 first2 = tree;
1103 } else {
1104 first2 = &first;
1105 }
1106
1107 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001108 ret = lyd_validate_new(first2, NULL, mod, diff);
1109 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001110
Michal Vasko0275cf62020-11-05 17:40:30 +01001111 /* add all top-level defaults for this module, do not add into unres sets, will occur in the next step */
1112 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, NULL, NULL, val_opts & LYD_VALIDATE_NO_STATE ?
Michal Vasko69730152020-10-09 16:30:07 +02001113 LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001114 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001115
1116 /* process nested nodes */
1117 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001118 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1119 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001120 }
1121
1122 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001123 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001124 LY_CHECK_GOTO(ret, cleanup);
1125
1126 /* perform final validation that assumes the data tree is final */
Michal Vasko8104fd42020-07-13 11:09:51 +02001127 ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0);
1128 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001129 }
1130
Michal Vaskof03ed032020-03-04 13:31:44 +01001131cleanup:
1132 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001133 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001134 ly_set_erase(&when_check, NULL);
1135 return ret;
1136}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001137
1138API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001139lyd_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 +01001140{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001141 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001142}
1143
1144API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001145lyd_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 +01001146{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001147 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001148}
Michal Vaskofea12c62020-03-30 11:00:15 +02001149
Michal Vaskobb844672020-07-03 11:06:12 +02001150/**
1151 * @brief Find nodes for merging an operation into data tree for validation.
1152 *
1153 * @param[in] op_tree Full operation data tree.
1154 * @param[in] op_node Operation node itself.
1155 * @param[in] tree Data tree to be merged into.
1156 * @param[out] op_subtree Operation subtree to merge.
1157 * @param[out] tree_sibling Data tree sibling to merge next to.
1158 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001159static void
Michal Vaskobb844672020-07-03 11:06:12 +02001160lyd_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 +02001161 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001162{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001163 const struct lyd_node *tree_iter, *op_iter;
1164 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001165 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001166
Michal Vaskocb7526d2020-03-30 15:08:26 +02001167 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001168 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001169 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001170 ++op_depth;
1171 }
1172
1173 /* find where to merge op */
1174 tree_iter = tree;
1175 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001176 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001177 while (cur_depth) {
1178 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001179 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001180 for (i = 0; i < cur_depth; ++i) {
1181 op_iter = (struct lyd_node *)op_iter->parent;
1182 }
1183
1184 /* find op iter in tree */
1185 lyd_find_sibling_first(tree_iter, op_iter, &match);
1186 if (!match) {
1187 break;
1188 }
1189
1190 /* move tree_iter */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001191 tree_iter = lyd_child(match);
Michal Vaskofea12c62020-03-30 11:00:15 +02001192
1193 /* move depth */
1194 --cur_depth;
1195 }
1196
Michal Vaskobb844672020-07-03 11:06:12 +02001197 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001198 *tree_sibling = (struct lyd_node *)tree_iter;
1199}
1200
1201API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001202lyd_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 +02001203{
1204 LY_ERR ret;
Michal Vasko56daf732020-08-10 10:57:18 +02001205 struct lyd_node *tree_sibling, *op_subtree, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001206 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1207
1208 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci0f969882020-08-21 16:56:47 +02001209 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001210 if (diff) {
1211 *diff = NULL;
1212 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001213
1214 /* find the operation/notification */
Michal Vasko56daf732020-08-10 10:57:18 +02001215 LYD_TREE_DFS_BEGIN(op_tree, op_node) {
Michal Vasko69730152020-10-09 16:30:07 +02001216 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 +02001217 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001218 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001219 break;
1220 }
Michal Vasko56daf732020-08-10 10:57:18 +02001221 LYD_TREE_DFS_END(op_tree, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001222 }
Michal Vasko69730152020-10-09 16:30:07 +02001223 if ((op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY)) {
Radek Krejci7931b192020-06-25 17:05:03 +02001224 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001225 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001226 return LY_EINVAL;
1227 }
1228 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001229 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001230 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001231 return LY_EINVAL;
1232 }
1233 }
1234
1235 /* move op_tree to top-level node */
1236 while (op_tree->parent) {
1237 op_tree = (struct lyd_node *)op_tree->parent;
1238 }
1239
1240 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001241 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1242 op_parent = (struct lyd_node *)op_subtree->parent;
1243 lyd_unlink_tree(op_subtree);
1244 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001245 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001246 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001247 }
1248
1249 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001250 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 +02001251
1252 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1253 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 +02001254 diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001255
1256 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001257 lyd_validate_obsolete(op_node);
Radek Krejci7931b192020-06-25 17:05:03 +02001258 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001259
1260 /* final validation of all the descendants */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001261 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 +02001262
1263cleanup:
1264 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001265 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001266 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001267 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001268 }
1269
1270 ly_set_erase(&type_check, NULL);
1271 ly_set_erase(&type_meta_check, NULL);
1272 ly_set_erase(&when_check, NULL);
1273 return ret;
1274}