blob: c16928de6b4bf4553b37d24089b575f31b196139 [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"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "plugins_types.h"
29#include "set.h"
30#include "tree.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010031#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010033#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010035
Michal Vaskoa6669ba2020-08-06 16:14:26 +020036LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +020037lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
38{
39 LY_ERR ret = LY_SUCCESS;
40 struct lyd_node *new_diff = NULL;
41
42 assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
43
44 /* create new diff tree */
45 LY_CHECK_RET(lyd_diff_add(node, op, NULL, NULL, NULL, NULL, NULL, &new_diff));
46
47 /* merge into existing diff */
Michal Vaskofb737aa2020-08-06 13:53:53 +020048 ret = lyd_diff_merge_all(diff, new_diff);
Michal Vasko8104fd42020-07-13 11:09:51 +020049
50 lyd_free_tree(new_diff);
51 return ret;
52}
53
54/**
Michal Vaskocde73ac2019-11-14 16:10:27 +010055 * @brief Evaluate a single "when" condition.
56 *
Michal Vaskob1b5c262020-03-05 14:29:47 +010057 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +010058 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +010059 * @param[in] when When to evaluate.
Michal Vasko8104fd42020-07-13 11:09:51 +020060 * @param[in,out] diff Validation diff.
Michal Vaskocde73ac2019-11-14 16:10:27 +010061 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
62 */
63static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +020064lyd_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 +010065{
Michal Vasko8104fd42020-07-13 11:09:51 +020066 LY_ERR ret;
Michal Vaskocde73ac2019-11-14 16:10:27 +010067 const struct lyd_node *ctx_node;
68 struct lyxp_set xp_set;
69
70 memset(&xp_set, 0, sizeof xp_set);
71
72 if (when->context == node->schema) {
73 ctx_node = node;
74 } else {
75 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
76 ctx_node = (struct lyd_node *)node->parent;
77 }
78
79 /* evaluate when */
Michal Vaskoc8a230d2020-08-14 12:17:10 +020080 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 +010081 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +010082 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
83
84 /* return error or LY_EINCOMPLETE for dependant unresolved when */
85 LY_CHECK_RET(ret);
86
87 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +020088 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +010089 if (node->flags & LYD_WHEN_TRUE) {
90 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +010091 if (LYD_DEL_IS_ROOT(*tree, node)) {
92 *tree = (*tree)->next;
93 }
Michal Vasko8104fd42020-07-13 11:09:51 +020094 if (diff) {
95 /* add into diff */
96 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff));
97 }
Michal Vaskocde73ac2019-11-14 16:10:27 +010098 lyd_free_tree(node);
99 } else {
100 /* invalid data */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200101 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vasko8104fd42020-07-13 11:09:51 +0200102 return LY_EVALID;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100103 }
104 } else {
105 /* remember that when evaluated to true */
106 node->flags |= LYD_WHEN_TRUE;
107 }
108
109 return ret;
110}
111
112LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100113lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *meta_types,
Radek Krejci0f969882020-08-21 16:56:47 +0200114 LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **diff)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100115{
116 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200117 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100118
Michal Vaskob1b5c262020-03-05 14:29:47 +0100119 if (node_when) {
120 /* evaluate all when conditions */
121 uint32_t prev_count;
122 do {
123 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200124 i = 0;
125 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100126 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200127 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100128 const struct lysc_node *schema = node->schema;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200129 uint8_t unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100130
Michal Vaskob1b5c262020-03-05 14:29:47 +0100131 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200132 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200133 LY_ARRAY_FOR(schema->when, u) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200134 ret = lyd_validate_when(tree, node, schema->when[u], diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100135 if (ret) {
136 break;
137 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100138 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100139 if (ret == LY_EINCOMPLETE) {
140 /* could not evaluate this when */
141 unres_when = 1;
142 break;
143 } else if (ret) {
144 /* error */
145 return ret;
146 }
147 schema = schema->parent;
148 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100149
Michal Vaskob1b5c262020-03-05 14:29:47 +0100150 if (unres_when) {
151 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200152 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100153 } else {
154 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200155 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100156 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100157 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100158
Radek Krejci0f969882020-08-21 16:56:47 +0200159 /* there must have been some when conditions resolved */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100160 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100161
Michal Vaskob1b5c262020-03-05 14:29:47 +0100162 /* there could have been no cyclic when dependencies, checked during compilation */
163 assert(!node_when->count);
164 }
165
166 if (node_types && node_types->count) {
167 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200168 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100169 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200170 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100171
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200172 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100173
174 /* validate and store the value of the node */
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200175 ret = lyd_value_parse(node, node->value.canonical, strlen(node->value.canonical), 0, 1, 0, format,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200176 prefix_data, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100177 LY_CHECK_RET(ret);
178
179 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200180 ly_set_rm_index(node_types, i, NULL);
181 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100182 }
183
Michal Vasko9f96a052020-03-10 09:41:45 +0100184 if (meta_types && meta_types->count) {
185 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200186 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100187 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200188 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100189
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200190 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100191
Michal Vasko9f96a052020-03-10 09:41:45 +0100192 /* validate and store the value of the metadata */
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200193 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.canonical,
194 strlen(meta->value.canonical), 0, 1, 0, format, prefix_data, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100195 LY_CHECK_RET(ret);
196
197 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200198 ly_set_rm_index(meta_types, i, NULL);
199 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100200 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100201
202 return ret;
203}
204
Michal Vaskobb844672020-07-03 11:06:12 +0200205/**
206 * @brief Validate instance duplication.
207 *
208 * @param[in] first First sibling to search in.
209 * @param[in] node Data node instance to check.
210 * @return LY_ERR value.
211 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100212static LY_ERR
213lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100214{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100215 struct lyd_node **match_p;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200216 uint8_t fail = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100217
218 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
219 /* duplicate instances allowed */
220 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100221 }
222
Michal Vaskob1b5c262020-03-05 14:29:47 +0100223 /* find exactly the same next instance using hashes if possible */
224 if (node->parent && node->parent->children_ht) {
225 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
226 fail = 1;
227 }
228 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200229 for ( ; first; first = first->next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100230 if (first == node) {
231 continue;
232 }
233
234 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
235 if (first->schema == node->schema) {
236 fail = 1;
237 break;
238 }
Michal Vasko8f359bf2020-07-28 10:41:15 +0200239 } else if (!lyd_compare_single(first, node, 0)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100240 fail = 1;
241 break;
242 }
243 }
244 }
245
246 if (fail) {
247 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
248 return LY_EVALID;
249 }
250 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100251}
252
Michal Vaskobb844672020-07-03 11:06:12 +0200253/**
254 * @brief Validate multiple case data existence with possible autodelete.
255 *
256 * @param[in,out] first First sibling to search in, is updated if needed.
257 * @param[in] choic Choice node whose cases to check.
Michal Vasko8104fd42020-07-13 11:09:51 +0200258 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200259 * @return LY_ERR value.
260 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100261static LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200262lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic, struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100263{
264 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
265 struct lyd_node *match, *to_del;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200266 uint8_t found;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100267
268 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
269 found = 0;
270 iter = NULL;
271 match = NULL;
272 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
273 if (match->flags & LYD_NEW) {
274 /* a new case data found, nothing more to look for */
275 found = 2;
276 break;
277 } else {
278 /* and old case data found */
279 if (found == 0) {
280 found = 1;
281 }
282 }
283 }
284
285 if (found == 1) {
286 /* there should not be 2 old cases */
287 if (old_case) {
288 /* old data from 2 cases */
289 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
290 return LY_EVALID;
291 }
292
293 /* remember an old existing case */
294 old_case = scase;
295 } else if (found == 2) {
296 if (new_case) {
297 /* new data from 2 cases */
298 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
299 return LY_EVALID;
300 }
301
302 /* remember a new existing case */
303 new_case = scase;
304 }
305 }
306
307 if (old_case && new_case) {
308 /* auto-delete old case */
309 iter = NULL;
310 match = NULL;
311 to_del = NULL;
312 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
313 if (LYD_DEL_IS_ROOT(*first, to_del)) {
314 *first = (*first)->next;
315 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200316 /* free previous node */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100317 lyd_free_tree(to_del);
Michal Vasko8104fd42020-07-13 11:09:51 +0200318 if (diff) {
319 /* add into diff */
320 LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
321 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100322 to_del = match;
323 }
324 if (LYD_DEL_IS_ROOT(*first, to_del)) {
325 *first = (*first)->next;
326 }
327 lyd_free_tree(to_del);
328 }
329
330 return LY_SUCCESS;
331}
332
Michal Vaskobb844672020-07-03 11:06:12 +0200333/**
334 * @brief Check whether a schema node can have some default values (true for NP containers as well).
335 *
336 * @param[in] schema Schema node to check.
337 * @return non-zero if yes,
338 * @return 0 otherwise.
339 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100340static int
341lyd_val_has_default(const struct lysc_node *schema)
342{
343 switch (schema->nodetype) {
344 case LYS_LEAF:
345 if (((struct lysc_node_leaf *)schema)->dflt) {
346 return 1;
347 }
348 break;
349 case LYS_LEAFLIST:
350 if (((struct lysc_node_leaflist *)schema)->dflts) {
351 return 1;
352 }
353 break;
354 case LYS_CONTAINER:
355 if (!(schema->flags & LYS_PRESENCE)) {
356 return 1;
357 }
358 break;
359 default:
360 break;
361 }
362
363 return 0;
364}
365
Michal Vaskobb844672020-07-03 11:06:12 +0200366/**
367 * @brief Autodelete old instances to prevent validation errors.
368 *
369 * @param[in,out] first First sibling to search in, is updated if needed.
370 * @param[in] node Data node instance to check.
371 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
Michal Vasko8104fd42020-07-13 11:09:51 +0200372 * @param[in,out] diff Validation diff.
Michal Vaskobb844672020-07-03 11:06:12 +0200373 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100374static void
Michal Vasko8104fd42020-07-13 11:09:51 +0200375lyd_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 +0100376{
Michal Vasko8104fd42020-07-13 11:09:51 +0200377 struct lyd_node *match, *next, *iter;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100378
379 if (lyd_val_has_default(node->schema)) {
380 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
Michal Vasko4c583e82020-07-17 12:16:14 +0200381 LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100382 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
383 /* default instance found, remove it */
384 if (LYD_DEL_IS_ROOT(*first, match)) {
385 *first = (*first)->next;
386 }
387 if (match == *next_p) {
388 *next_p = (*next_p)->next;
389 }
Michal Vasko8104fd42020-07-13 11:09:51 +0200390 if (diff) {
391 /* add into diff */
392 if ((match->schema->nodetype == LYS_CONTAINER) && !(match->schema->flags & LYS_PRESENCE)) {
393 /* we do not want to track NP container changes, but remember any removed children */
394 LY_LIST_FOR(LYD_CHILD(match), iter) {
395 lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
396 }
397 } else {
398 lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff);
399 }
400 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100401 lyd_free_tree(match);
402
403 /* remove only a single container/leaf default instance, if there are more, it is an error */
404 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
405 break;
406 }
407 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100408 }
409 }
410}
411
412LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +0200413lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200414 struct lyd_node **diff)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100415{
416 struct lyd_node *next, *node;
417 const struct lysc_node *snode = NULL;
418
419 assert(first && (sparent || mod));
420
421 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
422 /* check case duplicites */
423 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko8104fd42020-07-13 11:09:51 +0200424 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode, diff));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100425 }
426 }
427
428 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100429 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100430 /* all top-level data from this module checked */
431 break;
432 }
433
434 if (!(node->flags & LYD_NEW)) {
435 /* check only new nodes */
436 continue;
437 }
438
439 /* remove old default(s) if it exists */
Michal Vasko8104fd42020-07-13 11:09:51 +0200440 lyd_validate_autodel_dup(first, node, &next, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100441
442 /* then check new node instance duplicities */
443 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
444
445 /* this node is valid */
446 node->flags &= ~LYD_NEW;
447 }
448
449 return LY_SUCCESS;
450}
451
Michal Vaskobb844672020-07-03 11:06:12 +0200452/**
453 * @brief Validate mandatory node existence.
454 *
455 * @param[in] first First sibling to search in.
456 * @param[in] snode Schema node to validate.
457 * @return LY_ERR value.
458 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100459static LY_ERR
460lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100461{
Michal Vaskoa3881362020-01-21 15:57:35 +0100462 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100463 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100464 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100465 return LY_SUCCESS;
466 }
467 } else {
468 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100469
Michal Vaskob1b5c262020-03-05 14:29:47 +0100470 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100471 /* data instance found */
472 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100473 }
474 }
475
476 /* node instance not found */
477 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
478 return LY_EVALID;
479}
480
Michal Vaskobb844672020-07-03 11:06:12 +0200481/**
482 * @brief Validate min/max-elements constraints, if any.
483 *
484 * @param[in] first First sibling to search in.
485 * @param[in] snode Schema node to validate.
486 * @param[in] min Minimum number of elements, 0 for no restriction.
487 * @param[in] max Max number of elements, 0 for no restriction.
488 * @return LY_ERR value.
489 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100490static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100491lyd_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 +0100492{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100493 uint32_t count = 0;
Michal Vasko4c583e82020-07-17 12:16:14 +0200494 struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100495
Michal Vasko9b368d32020-02-14 13:53:31 +0100496 assert(min || max);
497
Michal Vasko4c583e82020-07-17 12:16:14 +0200498 LYD_LIST_FOR_INST(first, snode, iter) {
499 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100500
Michal Vasko4c583e82020-07-17 12:16:14 +0200501 if (min && (count == min)) {
502 /* satisfied */
503 min = 0;
504 if (!max) {
505 /* nothing more to check */
Michal Vasko9b368d32020-02-14 13:53:31 +0100506 break;
507 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100508 }
Michal Vasko4c583e82020-07-17 12:16:14 +0200509 if (max && (count > max)) {
510 /* not satisifed */
511 break;
512 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100513 }
514
Michal Vasko9b368d32020-02-14 13:53:31 +0100515 if (min) {
516 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100517 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
518 return LY_EVALID;
519 } else if (max && (count > max)) {
520 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
521 return LY_EVALID;
522 }
523
Michal Vaskoa3881362020-01-21 15:57:35 +0100524 return LY_SUCCESS;
525}
526
Michal Vaskobb844672020-07-03 11:06:12 +0200527/**
528 * @brief Find node referenced by a list unique statement.
529 *
530 * @param[in] uniq_leaf Unique leaf to find.
531 * @param[in] list List instance to use for the search.
532 * @return Found leaf,
533 * @return NULL if no leaf found.
534 */
Michal Vasko14654712020-02-06 08:35:21 +0100535static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200536lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100537{
Michal Vasko9b368d32020-02-14 13:53:31 +0100538 struct lyd_node *node;
539 const struct lysc_node *iter;
540 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100541
Michal Vasko9b368d32020-02-14 13:53:31 +0100542 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200543 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
544 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100545 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100546
Michal Vaskobb844672020-07-03 11:06:12 +0200547 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100548 while (node && depth) {
549 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200550 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
551 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100552 }
553
554 /* find iter instance in children */
555 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200556 lyd_find_sibling_val(lyd_node_children(node, 0), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100557 --depth;
558 }
559
Michal Vasko14654712020-02-06 08:35:21 +0100560 return node;
561}
562
Michal Vaskobb844672020-07-03 11:06:12 +0200563/**
564 * @brief Callback for comparing 2 list unique leaf values.
565 *
566 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100567 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200568static uint8_t
569lyd_val_uniq_list_equal(void *val1_p, void *val2_p, uint8_t UNUSED(mod), void *cb_data)
Michal Vasko14654712020-02-06 08:35:21 +0100570{
571 struct ly_ctx *ctx;
572 struct lysc_node_list *slist;
573 struct lyd_node *diter, *first, *second;
574 struct lyd_value *val1, *val2;
575 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200576 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100577
578 assert(val1_p && val2_p);
579
580 first = *((struct lyd_node **)val1_p);
581 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200582 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100583
584 assert(first && (first->schema->nodetype == LYS_LIST));
585 assert(second && (second->schema == first->schema));
586
587 ctx = first->schema->module->ctx;
588
589 slist = (struct lysc_node_list *)first->schema;
590
591 /* compare unique leaves */
592 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200593 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200594 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100595 goto uniquecheck;
596 }
597 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200598 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100599uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200600 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100601 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200602 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100603 if (diter) {
604 val1 = &((struct lyd_node_term *)diter)->value;
605 } else {
606 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200607 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100608 }
609
610 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200611 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100612 if (diter) {
613 val2 = &((struct lyd_node_term *)diter)->value;
614 } else {
615 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200616 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100617 }
618
619 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
620 /* values differ or either one is not set */
621 break;
622 }
623 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200624 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100625 /* all unique leafs are the same in this set, create this nice error */
626 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
627 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
628
629 /* use buffer to rebuild the unique string */
630 uniq_str = malloc(1024);
631 uniq_str[0] = '\0';
632 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200633 LY_ARRAY_FOR(slist->uniques[u], v) {
634 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100635 strcpy(ptr, " ");
636 ++ptr;
637 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200638 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 +0100639 ptr, 1024 - (ptr - uniq_str));
640 if (!ptr) {
641 /* path will be incomplete, whatever */
642 break;
643 }
644
645 ptr += strlen(ptr);
646 }
647 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
648
649 free(path1);
650 free(path2);
651 free(uniq_str);
652 return 1;
653 }
654
655 if (action > 0) {
656 /* done */
657 return 0;
658 }
659 }
660
661 return 0;
662}
663
Michal Vaskobb844672020-07-03 11:06:12 +0200664/**
665 * @brief Validate list unique leaves.
666 *
667 * @param[in] first First sibling to search in.
668 * @param[in] snode Schema node to validate.
669 * @param[in] uniques List unique arrays to validate.
670 * @return LY_ERR value.
671 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100672static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200673lyd_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 +0100674{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100675 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100676 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200677 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100678 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200679 uint32_t hash, i, size = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200680 uint8_t dynamic;
Michal Vasko14654712020-02-06 08:35:21 +0100681 const char *str;
682 struct hash_table **uniqtables = NULL;
683 struct lyd_value *val;
684 struct ly_ctx *ctx = snode->module->ctx;
685
686 assert(uniques);
687
688 /* get all list instances */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200689 LY_CHECK_RET(ly_set_new(&set));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100690 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100691 if (diter->schema == snode) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200692 LY_CHECK_RET(ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST, NULL));
Michal Vasko9b368d32020-02-14 13:53:31 +0100693 }
694 }
Michal Vasko14654712020-02-06 08:35:21 +0100695
696 if (set->count == 2) {
697 /* simple comparison */
698 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
699 /* instance duplication */
700 ret = LY_EVALID;
701 goto cleanup;
702 }
703 } else if (set->count > 2) {
704 /* use hashes for comparison */
705 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200706 for (i = 31; i > 0; i--) {
707 size = set->count << i;
708 size = size >> i;
709 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100710 break;
711 }
712 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200713 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
714 i = 32 - i;
715 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100716
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200717 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100718 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200719 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200720 for (v = 0; v < x; v++) {
721 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
722 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100723 }
724
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200725 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100726 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200727 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100728 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200729 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200730 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100731 if (diter) {
732 val = &((struct lyd_node_term *)diter)->value;
733 } else {
734 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200735 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100736 }
737 if (!val) {
738 /* unique item not present nor has default value */
739 break;
740 }
741
742 /* get canonical string value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200743 str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +0100744 hash = dict_hash_multi(hash, str, strlen(str));
745 if (dynamic) {
746 free((char *)str);
747 }
748 }
749 if (!val) {
750 /* skip this list instance since its unique set is incomplete */
751 continue;
752 }
753
754 /* finish the hash value */
755 hash = dict_hash_multi(hash, NULL, 0);
756
757 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200758 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100759 if (ret == LY_EEXIST) {
760 /* instance duplication */
761 ret = LY_EVALID;
762 }
763 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
764 }
765 }
766 }
767
768cleanup:
769 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200770 for (v = 0; v < x; v++) {
771 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100772 /* failed when allocating uniquetables[j], following j are not allocated */
773 break;
774 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200775 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100776 }
777 free(uniqtables);
778
779 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100780}
781
Michal Vaskobb844672020-07-03 11:06:12 +0200782/**
783 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
784 *
785 * @param[in] first First sibling to search in.
786 * @param[in] sparent Schema parent of the nodes to check.
787 * @param[in] mod Module of the nodes to check.
788 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
789 * @param[in] op Operation being validated, if any.
790 * @return LY_ERR value.
791 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100792static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100793lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200794 const struct lysc_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100795{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100796 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100797 struct lysc_node_list *slist;
Michal Vaskod8958df2020-08-05 13:27:36 +0200798 struct lysc_node_leaflist *sllist;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200799 uint32_t getnext_opts;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200800
Radek Krejci7931b192020-06-25 17:05:03 +0200801 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100802
Michal Vaskoa3881362020-01-21 15:57:35 +0100803 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200804 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200805 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100806 continue;
807 }
808
Michal Vaskoa3881362020-01-21 15:57:35 +0100809 /* check min-elements and max-elements */
Michal Vaskod8958df2020-08-05 13:27:36 +0200810 if (snode->nodetype == LYS_LIST) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100811 slist = (struct lysc_node_list *)snode;
812 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100813 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100814 }
Michal Vaskod8958df2020-08-05 13:27:36 +0200815 } else if (snode->nodetype == LYS_LEAFLIST) {
816 sllist = (struct lysc_node_leaflist *)snode;
817 if (sllist->min || sllist->max) {
818 LY_CHECK_RET(lyd_validate_minmax(first, snode, sllist->min, sllist->max));
819 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100820
Michal Vaskoacd83e72020-02-04 14:12:01 +0100821 } else if (snode->flags & LYS_MAND_TRUE) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200822 /* check generic mandatory existence */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100823 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100824 }
825
826 /* check unique */
827 if (snode->nodetype == LYS_LIST) {
828 slist = (struct lysc_node_list *)snode;
829 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200830 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100831 }
832 }
833
Michal Vaskoacd83e72020-02-04 14:12:01 +0100834 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
835 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200836 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100837 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100838 }
839
Michal Vaskoacd83e72020-02-04 14:12:01 +0100840 return LY_SUCCESS;
841}
842
Michal Vaskobb844672020-07-03 11:06:12 +0200843/**
844 * @brief Validate obsolete nodes, only warnings are printed.
845 *
846 * @param[in] node Node to check.
847 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100848static void
849lyd_validate_obsolete(const struct lyd_node *node)
850{
851 const struct lysc_node *snode;
852
853 snode = node->schema;
854 do {
855 if (snode->flags & LYS_STATUS_OBSLT) {
856 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
857 break;
858 }
859
860 snode = snode->parent;
861 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
862}
863
Michal Vaskobb844672020-07-03 11:06:12 +0200864/**
865 * @brief Validate must conditions of a data node.
866 *
867 * @param[in] node Node to validate.
868 * @param[in] op Operation being validated, if any.
869 * @return LY_ERR value.
870 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100871static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200872lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100873{
874 struct lyxp_set xp_set;
875 struct lysc_must *musts;
876 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200877 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100878
879 switch (node->schema->nodetype) {
880 case LYS_CONTAINER:
881 musts = ((struct lysc_node_container *)node->schema)->musts;
882 break;
883 case LYS_LEAF:
884 musts = ((struct lysc_node_leaf *)node->schema)->musts;
885 break;
886 case LYS_LEAFLIST:
887 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
888 break;
889 case LYS_LIST:
890 musts = ((struct lysc_node_list *)node->schema)->musts;
891 break;
892 case LYS_ANYXML:
893 case LYS_ANYDATA:
894 musts = ((struct lysc_node_anydata *)node->schema)->musts;
895 break;
896 case LYS_NOTIF:
897 musts = ((struct lysc_notif *)node->schema)->musts;
898 break;
899 case LYS_RPC:
900 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200901 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200902 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200903 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200904 musts = ((struct lysc_action *)node->schema)->output.musts;
905 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200906 LOGINT(LYD_CTX(node));
Michal Vaskocb7526d2020-03-30 15:08:26 +0200907 return LY_EINT;
908 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100909 break;
910 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200911 LOGINT(LYD_CTX(node));
Michal Vaskocc048b22020-03-27 15:52:38 +0100912 return LY_EINT;
913 }
914
915 if (!musts) {
916 /* no must to evaluate */
917 return LY_SUCCESS;
918 }
919
920 /* find first top-level node */
Radek Krejci1e008d22020-08-17 11:37:37 +0200921 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent) {}
Michal Vaskocc048b22020-03-27 15:52:38 +0100922 while (tree->prev->next) {
923 tree = tree->prev;
924 }
925
926 LY_ARRAY_FOR(musts, u) {
927 memset(&xp_set, 0, sizeof xp_set);
928
929 /* evaluate must */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200930 LY_CHECK_RET(lyxp_eval(musts[u].cond, LY_PREF_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set,
931 LYXP_SCHEMA));
Michal Vaskocc048b22020-03-27 15:52:38 +0100932
933 /* check the result */
934 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200935 if (!xp_set.val.bln) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200936 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
Michal Vaskocc048b22020-03-27 15:52:38 +0100937 return LY_EVALID;
938 }
939 }
940
941 return LY_SUCCESS;
942}
943
Michal Vaskob1b5c262020-03-05 14:29:47 +0100944LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200945lyd_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 +0200946 LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100947{
Radek Krejci7f769d72020-07-11 23:13:56 +0200948 struct lyd_node *next = NULL, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100949 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100950
Michal Vasko14654712020-02-06 08:35:21 +0100951 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100952 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100953 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100954 /* all top-level data from this module checked */
955 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100956 }
957
Michal Vaskoa8c61722020-03-27 16:59:32 +0100958 /* opaque data */
959 if (!node->schema) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200960 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
Radek Krejci0f969882020-08-21 16:56:47 +0200961 ((struct lyd_node_opaq *)node)->name);
Michal Vaskoa8c61722020-03-27 16:59:32 +0100962 return LY_EVALID;
963 }
964
Michal Vaskocb7526d2020-03-30 15:08:26 +0200965 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +0200966 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200967 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +0200968 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200969 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200970 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
Michal Vaskocb7526d2020-03-30 15:08:26 +0200971 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200972 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200973 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +0100974 return LY_EVALID;
975 }
976
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100977 /* obsolete data */
978 lyd_validate_obsolete(node);
979
Michal Vaskoc193ce92020-03-06 11:04:48 +0100980 /* node's schema if-features */
981 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200982 LOGVAL(LYD_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +0100983 return LY_EVALID;
984 }
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 Vaskoa8c61722020-03-27 16:59:32 +0100989 /* node value including if-feature 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 Krejci7931b192020-06-25 17:05:03 +0200997 LY_CHECK_RET(lyd_validate_final_r(lyd_node_children(node, 0), 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)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001001 LY_LIST_FOR(lyd_node_children(node, 0), 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 Vaskofea12c62020-03-30 11:00:15 +02001034 /* skip added default nodes */
1035 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1036 LY_LIST_FOR(node->meta, meta) {
1037 /* metadata type resolution */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001038 LY_CHECK_RET(ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001039 }
1040
1041 if (node->schema->nodetype & LYD_NODE_TERM) {
1042 /* node type resolution */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001043 LY_CHECK_RET(ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001044 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1045 /* new node validation, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001046 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
1048 /* add nested defaults */
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001049 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,
1051 diff));
Michal Vaskofea12c62020-03-30 11:00:15 +02001052 }
1053
1054 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1055 /* when evaluation */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001056 LY_CHECK_RET(ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001057 }
1058 }
1059
Michal Vasko56daf732020-08-10 10:57:18 +02001060 LYD_TREE_DFS_END(root, node);
Michal Vaskofea12c62020-03-30 11:00:15 +02001061 }
1062
1063 return LY_SUCCESS;
1064}
1065
Michal Vaskobb844672020-07-03 11:06:12 +02001066/**
1067 * @brief Validate data tree.
1068 *
1069 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1070 * @param[in] modules Array of modules to validate, NULL for all modules.
1071 * @param[in] mod_count Count of @p modules.
1072 * @param[in] ly_ctx libyang context.
1073 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vasko8104fd42020-07-13 11:09:51 +02001074 * @param[out] diff Generated validation diff, not generated if NULL.
Michal Vaskobb844672020-07-03 11:06:12 +02001075 * @return LY_ERR value.
1076 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001077static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001078lyd_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 +02001079 struct lyd_node **diff)
Michal Vaskof03ed032020-03-04 13:31:44 +01001080{
1081 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001082 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001083 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001084 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001085 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001086
Michal Vasko26e80012020-07-08 10:55:46 +02001087 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001088 if (diff) {
1089 *diff = NULL;
1090 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001091
Michal Vaskob1b5c262020-03-05 14:29:47 +01001092 next = *tree;
1093 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001094 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001095 mod = lyd_data_next_module(&next, &first);
1096 } else {
Michal Vasko26e80012020-07-08 10:55:46 +02001097 mod = lyd_mod_next_module(next, module, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001098 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001099 if (!mod) {
1100 break;
1101 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001102 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001103 /* make sure first2 changes are carried to tree */
1104 first2 = tree;
1105 } else {
1106 first2 = &first;
1107 }
1108
1109 /* validate new top-level nodes of this module, autodelete */
Michal Vasko8104fd42020-07-13 11:09:51 +02001110 ret = lyd_validate_new(first2, NULL, mod, diff);
1111 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001112
1113 /* add all top-level defaults for this module */
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001114 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &type_check, &when_check, val_opts & LYD_VALIDATE_NO_STATE
1115 ? LYD_IMPLICIT_NO_STATE : 0, diff);
Michal Vasko8104fd42020-07-13 11:09:51 +02001116 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001117
1118 /* process nested nodes */
1119 LY_LIST_FOR(*first2, first) {
Michal Vasko8104fd42020-07-13 11:09:51 +02001120 ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts, diff);
1121 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001122 }
1123
1124 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001125 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, LY_PREF_JSON, NULL, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001126 LY_CHECK_GOTO(ret, cleanup);
1127
1128 /* perform final validation that assumes the data tree is final */
Michal Vasko8104fd42020-07-13 11:09:51 +02001129 ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0);
1130 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001131 }
1132
Michal Vaskof03ed032020-03-04 13:31:44 +01001133cleanup:
1134 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001135 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001136 ly_set_erase(&when_check, NULL);
1137 return ret;
1138}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001139
1140API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001141lyd_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 +01001142{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001143 return lyd_validate(tree, NULL, ctx, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001144}
1145
1146API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001147lyd_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 +01001148{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001149 return lyd_validate(tree, module, NULL, val_opts, diff);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001150}
Michal Vaskofea12c62020-03-30 11:00:15 +02001151
Michal Vaskobb844672020-07-03 11:06:12 +02001152/**
1153 * @brief Find nodes for merging an operation into data tree for validation.
1154 *
1155 * @param[in] op_tree Full operation data tree.
1156 * @param[in] op_node Operation node itself.
1157 * @param[in] tree Data tree to be merged into.
1158 * @param[out] op_subtree Operation subtree to merge.
1159 * @param[out] tree_sibling Data tree sibling to merge next to.
1160 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001161static void
Michal Vaskobb844672020-07-03 11:06:12 +02001162lyd_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 +02001163 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001164{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001165 const struct lyd_node *tree_iter, *op_iter;
1166 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001167 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001168
Michal Vaskocb7526d2020-03-30 15:08:26 +02001169 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001170 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001171 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001172 ++op_depth;
1173 }
1174
1175 /* find where to merge op */
1176 tree_iter = tree;
1177 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001178 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001179 while (cur_depth) {
1180 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001181 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001182 for (i = 0; i < cur_depth; ++i) {
1183 op_iter = (struct lyd_node *)op_iter->parent;
1184 }
1185
1186 /* find op iter in tree */
1187 lyd_find_sibling_first(tree_iter, op_iter, &match);
1188 if (!match) {
1189 break;
1190 }
1191
1192 /* move tree_iter */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001193 tree_iter = lyd_node_children(match, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001194
1195 /* move depth */
1196 --cur_depth;
1197 }
1198
Michal Vaskobb844672020-07-03 11:06:12 +02001199 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001200 *tree_sibling = (struct lyd_node *)tree_iter;
1201}
1202
1203API LY_ERR
Michal Vasko8104fd42020-07-13 11:09:51 +02001204lyd_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 +02001205{
1206 LY_ERR ret;
Michal Vasko56daf732020-08-10 10:57:18 +02001207 struct lyd_node *tree_sibling, *op_subtree, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001208 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1209
1210 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci0f969882020-08-21 16:56:47 +02001211 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001212 if (diff) {
1213 *diff = NULL;
1214 }
Michal Vaskocb7526d2020-03-30 15:08:26 +02001215
1216 /* find the operation/notification */
Michal Vasko56daf732020-08-10 10:57:18 +02001217 LYD_TREE_DFS_BEGIN(op_tree, op_node) {
Radek Krejci7931b192020-06-25 17:05:03 +02001218 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 +02001219 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001220 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001221 break;
1222 }
Michal Vasko56daf732020-08-10 10:57:18 +02001223 LYD_TREE_DFS_END(op_tree, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001224 }
Radek Krejci7931b192020-06-25 17:05:03 +02001225 if (op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) {
1226 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001227 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001228 return LY_EINVAL;
1229 }
1230 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001231 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001232 LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001233 return LY_EINVAL;
1234 }
1235 }
1236
1237 /* move op_tree to top-level node */
1238 while (op_tree->parent) {
1239 op_tree = (struct lyd_node *)op_tree->parent;
1240 }
1241
1242 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001243 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1244 op_parent = (struct lyd_node *)op_subtree->parent;
1245 lyd_unlink_tree(op_subtree);
1246 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001247 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001248 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001249 }
1250
1251 /* prevalidate whole operation subtree */
Michal Vasko8104fd42020-07-13 11:09:51 +02001252 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 +02001253
1254 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1255 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001256 LY_PREF_JSON, NULL, diff), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001257
1258 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001259 lyd_validate_obsolete(op_node);
1260 if (lysc_node_is_disabled(op_node->schema, 1)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001261 LOGVAL(LYD_CTX(op_tree), LY_VLOG_LYD, op_node, LY_VCODE_NOIFF, op_node->schema->name);
Michal Vaskofea12c62020-03-30 11:00:15 +02001262 ret = LY_EVALID;
1263 goto cleanup;
1264 }
Radek Krejci7931b192020-06-25 17:05:03 +02001265 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001266
1267 /* final validation of all the descendants */
Radek Krejci7931b192020-06-25 17:05:03 +02001268 LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_node_children(op_node, 0), op_node->schema, NULL, 0, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001269
1270cleanup:
1271 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001272 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001273 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001274 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001275 }
1276
1277 ly_set_erase(&type_check, NULL);
1278 ly_set_erase(&type_meta_check, NULL);
1279 ly_set_erase(&when_check, NULL);
1280 return ret;
1281}