blob: a4701e4f3e0c2ffc44f8c6f0a633474a02a77e75 [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 */
14
15#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include <stdint.h>
Michal Vasko52927e22020-03-16 17:26:14 +010017#include <stdio.h>
18#include <stdlib.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <string.h>
Michal Vaskocde73ac2019-11-14 16:10:27 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
22#include "config.h"
23#include "hash_table.h"
24#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "plugins_types.h"
27#include "set.h"
28#include "tree.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010029#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tree_schema.h"
Michal Vasko14654712020-02-06 08:35:21 +010031#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "xpath.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010033
Michal Vaskobb844672020-07-03 11:06:12 +020034/**
35 * @brief Just like lys_getnext() but iterates over all data instances of the schema nodes.
36 *
37 * @param[in] last Last returned data node.
38 * @param[in] sibling Data node sibling to search in.
39 * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards.
40 * May not be set if the function is used only for any suitable node existence check (such as the existence
41 * of any choice case data).
42 * @param[in] parent Schema parent of the iterated children nodes.
43 * @param[in] module Schema module of the iterated top-level nodes.
44 * @return Next matching data node,
45 * @return NULL if last data node was already returned.
46 */
Michal Vaskof03ed032020-03-04 13:31:44 +010047static struct lyd_node *
48lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
49 const struct lysc_node *parent, const struct lysc_module *module)
50{
51 const struct lysc_node *siter = NULL;
52 struct lyd_node *match = NULL;
53
54 assert(parent || module);
55 assert(!last || (slast && *slast));
56
57 if (slast) {
58 siter = *slast;
59 }
60
61 if (last && last->next) {
62 /* find next data instance */
63 lyd_find_sibling_next2(last->next, siter, NULL, 0, &match);
64 if (match) {
65 return match;
66 }
67 }
68
69 /* find next schema node data instance */
70 while ((siter = lys_getnext(siter, parent, module, 0))) {
71 switch (siter->nodetype) {
72 case LYS_CONTAINER:
73 case LYS_ANYXML:
74 case LYS_ANYDATA:
75 case LYS_LEAF:
76 lyd_find_sibling_val(sibling, siter, NULL, 0, &match);
77 break;
78 case LYS_LIST:
79 case LYS_LEAFLIST:
80 lyd_find_sibling_next2(sibling, siter, NULL, 0, &match);
81 break;
82 default:
83 assert(0);
84 LOGINT(NULL);
85 }
86
87 if (match) {
88 break;
89 }
90 }
91
92 if (slast) {
93 *slast = siter;
94 }
95 return match;
96}
97
Michal Vaskocde73ac2019-11-14 16:10:27 +010098/**
99 * @brief Evaluate a single "when" condition.
100 *
Michal Vaskob1b5c262020-03-05 14:29:47 +0100101 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100102 * @param[in] node Node whose existence depends on this when.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100103 * @param[in] when When to evaluate.
Michal Vaskocde73ac2019-11-14 16:10:27 +0100104 * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated)
105 */
106static LY_ERR
Michal Vaskoc193ce92020-03-06 11:04:48 +0100107lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100108{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100109 LY_ERR ret = LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100110 const struct lyd_node *ctx_node;
111 struct lyxp_set xp_set;
112
113 memset(&xp_set, 0, sizeof xp_set);
114
115 if (when->context == node->schema) {
116 ctx_node = node;
117 } else {
118 assert((!when->context && !node->parent) || (when->context == node->parent->schema));
119 ctx_node = (struct lyd_node *)node->parent;
120 }
121
122 /* evaluate when */
Michal Vasko52927e22020-03-16 17:26:14 +0100123 ret = lyxp_eval(when->cond, LYD_SCHEMA, when->module, ctx_node, ctx_node ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100124 *tree, &xp_set, LYXP_SCHEMA);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100125 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
126
127 /* return error or LY_EINCOMPLETE for dependant unresolved when */
128 LY_CHECK_RET(ret);
129
130 /* take action based on the result */
Michal Vasko004d3152020-06-11 19:59:22 +0200131 if (!xp_set.val.bln) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100132 if (node->flags & LYD_WHEN_TRUE) {
133 /* autodelete */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100134 if (LYD_DEL_IS_ROOT(*tree, node)) {
135 *tree = (*tree)->next;
136 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100137 lyd_free_tree(node);
138 } else {
139 /* invalid data */
Michal Vaskocc048b22020-03-27 15:52:38 +0100140 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100141 ret = LY_EVALID;
142 }
143 } else {
144 /* remember that when evaluated to true */
145 node->flags |= LYD_WHEN_TRUE;
146 }
147
148 return ret;
149}
150
151LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100152lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *meta_types,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100153 LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100154{
155 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200156 uint32_t i;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100157
Michal Vaskob1b5c262020-03-05 14:29:47 +0100158 if (node_when) {
159 /* evaluate all when conditions */
160 uint32_t prev_count;
161 do {
162 prev_count = node_when->count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200163 i = 0;
164 while (i < node_when->count) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100165 /* evaluate all when expressions that affect this node's existence */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200166 struct lyd_node *node = (struct lyd_node *)node_when->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100167 const struct lysc_node *schema = node->schema;
168 int unres_when = 0;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100169
Michal Vaskob1b5c262020-03-05 14:29:47 +0100170 do {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200171 LY_ARRAY_COUNT_TYPE u;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200172 LY_ARRAY_FOR(schema->when, u) {
173 ret = lyd_validate_when(tree, node, schema->when[u]);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100174 if (ret) {
175 break;
176 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100177 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100178 if (ret == LY_EINCOMPLETE) {
179 /* could not evaluate this when */
180 unres_when = 1;
181 break;
182 } else if (ret) {
183 /* error */
184 return ret;
185 }
186 schema = schema->parent;
187 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100188
Michal Vaskob1b5c262020-03-05 14:29:47 +0100189 if (unres_when) {
190 /* keep in set and go to the next node */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200191 ++i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100192 } else {
193 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200194 ly_set_rm_index(node_when, i, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100195 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100196 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100197
Michal Vaskob1b5c262020-03-05 14:29:47 +0100198 /* there must have been some when conditions resolved */
199 } while (prev_count > node_when->count);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100200
Michal Vaskob1b5c262020-03-05 14:29:47 +0100201 /* there could have been no cyclic when dependencies, checked during compilation */
202 assert(!node_when->count);
203 }
204
205 if (node_types && node_types->count) {
206 /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200207 i = node_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100208 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200209 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100210
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200211 struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100212
213 /* validate and store the value of the node */
214 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb,
215 parser_data, format, *tree);
216 LY_CHECK_RET(ret);
217
218 /* remove this node from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200219 ly_set_rm_index(node_types, i, NULL);
220 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100221 }
222
Michal Vasko9f96a052020-03-10 09:41:45 +0100223 if (meta_types && meta_types->count) {
224 /* ... and metadata values */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200225 i = meta_types->count;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100226 do {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200227 --i;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100228
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200229 struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i];
Michal Vaskob1b5c262020-03-05 14:29:47 +0100230
Michal Vasko9f96a052020-03-10 09:41:45 +0100231 /* validate and store the value of the metadata */
232 ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original,
233 strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100234 LY_CHECK_RET(ret);
235
236 /* remove this attr from the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200237 ly_set_rm_index(meta_types, i, NULL);
238 } while (i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100239 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100240
241 return ret;
242}
243
Michal Vaskobb844672020-07-03 11:06:12 +0200244/**
245 * @brief Validate instance duplication.
246 *
247 * @param[in] first First sibling to search in.
248 * @param[in] node Data node instance to check.
249 * @return LY_ERR value.
250 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100251static LY_ERR
252lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100253{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100254 struct lyd_node **match_p;
255 int fail = 0;
256
257 if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) {
258 /* duplicate instances allowed */
259 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100260 }
261
Michal Vaskob1b5c262020-03-05 14:29:47 +0100262 /* find exactly the same next instance using hashes if possible */
263 if (node->parent && node->parent->children_ht) {
264 if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
265 fail = 1;
266 }
267 } else {
268 for (; first; first = first->next) {
269 if (first == node) {
270 continue;
271 }
272
273 if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
274 if (first->schema == node->schema) {
275 fail = 1;
276 break;
277 }
278 } else if (!lyd_compare(first, node, 0)) {
279 fail = 1;
280 break;
281 }
282 }
283 }
284
285 if (fail) {
286 LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name);
287 return LY_EVALID;
288 }
289 return LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100290}
291
Michal Vaskobb844672020-07-03 11:06:12 +0200292/**
293 * @brief Validate multiple case data existence with possible autodelete.
294 *
295 * @param[in,out] first First sibling to search in, is updated if needed.
296 * @param[in] choic Choice node whose cases to check.
297 * @return LY_ERR value.
298 */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100299static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100300lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic)
301{
302 const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
303 struct lyd_node *match, *to_del;
304 int found;
305
306 LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
307 found = 0;
308 iter = NULL;
309 match = NULL;
310 while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
311 if (match->flags & LYD_NEW) {
312 /* a new case data found, nothing more to look for */
313 found = 2;
314 break;
315 } else {
316 /* and old case data found */
317 if (found == 0) {
318 found = 1;
319 }
320 }
321 }
322
323 if (found == 1) {
324 /* there should not be 2 old cases */
325 if (old_case) {
326 /* old data from 2 cases */
327 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name);
328 return LY_EVALID;
329 }
330
331 /* remember an old existing case */
332 old_case = scase;
333 } else if (found == 2) {
334 if (new_case) {
335 /* new data from 2 cases */
336 LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name);
337 return LY_EVALID;
338 }
339
340 /* remember a new existing case */
341 new_case = scase;
342 }
343 }
344
345 if (old_case && new_case) {
346 /* auto-delete old case */
347 iter = NULL;
348 match = NULL;
349 to_del = NULL;
350 while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
351 if (LYD_DEL_IS_ROOT(*first, to_del)) {
352 *first = (*first)->next;
353 }
354 lyd_free_tree(to_del);
355 to_del = match;
356 }
357 if (LYD_DEL_IS_ROOT(*first, to_del)) {
358 *first = (*first)->next;
359 }
360 lyd_free_tree(to_del);
361 }
362
363 return LY_SUCCESS;
364}
365
Michal Vaskobb844672020-07-03 11:06:12 +0200366/**
367 * @brief Check whether a schema node can have some default values (true for NP containers as well).
368 *
369 * @param[in] schema Schema node to check.
370 * @return non-zero if yes,
371 * @return 0 otherwise.
372 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100373static int
374lyd_val_has_default(const struct lysc_node *schema)
375{
376 switch (schema->nodetype) {
377 case LYS_LEAF:
378 if (((struct lysc_node_leaf *)schema)->dflt) {
379 return 1;
380 }
381 break;
382 case LYS_LEAFLIST:
383 if (((struct lysc_node_leaflist *)schema)->dflts) {
384 return 1;
385 }
386 break;
387 case LYS_CONTAINER:
388 if (!(schema->flags & LYS_PRESENCE)) {
389 return 1;
390 }
391 break;
392 default:
393 break;
394 }
395
396 return 0;
397}
398
Michal Vaskobb844672020-07-03 11:06:12 +0200399/**
400 * @brief Autodelete old instances to prevent validation errors.
401 *
402 * @param[in,out] first First sibling to search in, is updated if needed.
403 * @param[in] node Data node instance to check.
404 * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed.
405 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100406static void
407lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p)
408{
409 struct lyd_node *match, *next;
410
411 if (lyd_val_has_default(node->schema)) {
412 assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER));
413 if (node->schema->nodetype == LYS_LEAFLIST) {
414 lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match);
415 } else {
416 lyd_find_sibling_val(*first, node->schema, NULL, 0, &match);
417 }
418
419 while (match) {
420 next = match->next;
421 if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) {
422 /* default instance found, remove it */
423 if (LYD_DEL_IS_ROOT(*first, match)) {
424 *first = (*first)->next;
425 }
426 if (match == *next_p) {
427 *next_p = (*next_p)->next;
428 }
429 lyd_free_tree(match);
430
431 /* remove only a single container/leaf default instance, if there are more, it is an error */
432 if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) {
433 break;
434 }
435 }
436
437 lyd_find_sibling_next2(next, node->schema, NULL, 0, &match);
438 }
439 }
440}
441
442LY_ERR
443lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod)
444{
445 struct lyd_node *next, *node;
446 const struct lysc_node *snode = NULL;
447
448 assert(first && (sparent || mod));
449
450 while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
451 /* check case duplicites */
452 if (snode->nodetype == LYS_CHOICE) {
453 LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode));
454 }
455 }
456
457 LY_LIST_FOR_SAFE(*first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100458 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100459 /* all top-level data from this module checked */
460 break;
461 }
462
463 if (!(node->flags & LYD_NEW)) {
464 /* check only new nodes */
465 continue;
466 }
467
468 /* remove old default(s) if it exists */
469 lyd_validate_autodel_dup(first, node, &next);
470
471 /* then check new node instance duplicities */
472 LY_CHECK_RET(lyd_validate_duplicates(*first, node));
473
474 /* this node is valid */
475 node->flags &= ~LYD_NEW;
476 }
477
478 return LY_SUCCESS;
479}
480
Michal Vaskobb844672020-07-03 11:06:12 +0200481/**
482 * @brief Validate mandatory node existence.
483 *
484 * @param[in] first First sibling to search in.
485 * @param[in] snode Schema node to validate.
486 * @return LY_ERR value.
487 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100488static LY_ERR
489lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode)
Michal Vaskoa3881362020-01-21 15:57:35 +0100490{
Michal Vaskoa3881362020-01-21 15:57:35 +0100491 if (snode->nodetype == LYS_CHOICE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100492 /* some data of a choice case exist */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100493 if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100494 return LY_SUCCESS;
495 }
496 } else {
497 assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
Michal Vaskoa3881362020-01-21 15:57:35 +0100498
Michal Vaskob1b5c262020-03-05 14:29:47 +0100499 if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100500 /* data instance found */
501 return LY_SUCCESS;
Michal Vaskoa3881362020-01-21 15:57:35 +0100502 }
503 }
504
505 /* node instance not found */
506 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name);
507 return LY_EVALID;
508}
509
Michal Vaskobb844672020-07-03 11:06:12 +0200510/**
511 * @brief Validate min/max-elements constraints, if any.
512 *
513 * @param[in] first First sibling to search in.
514 * @param[in] snode Schema node to validate.
515 * @param[in] min Minimum number of elements, 0 for no restriction.
516 * @param[in] max Max number of elements, 0 for no restriction.
517 * @return LY_ERR value.
518 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100519static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +0100520lyd_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 +0100521{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100522 uint32_t count = 0;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100523 const struct lyd_node *iter;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100524
Michal Vasko9b368d32020-02-14 13:53:31 +0100525 assert(min || max);
526
Michal Vaskob1b5c262020-03-05 14:29:47 +0100527 LY_LIST_FOR(first, iter) {
Michal Vaskoacd83e72020-02-04 14:12:01 +0100528 if (iter->schema == snode) {
529 ++count;
Michal Vasko9b368d32020-02-14 13:53:31 +0100530
531 if (min && (count == min)) {
532 /* satisfied */
533 min = 0;
534 if (!max) {
535 /* nothing more to check */
536 break;
537 }
538 }
539 if (max && (count > max)) {
540 /* not satisifed */
541 break;
542 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100543 }
544 }
545
Michal Vasko9b368d32020-02-14 13:53:31 +0100546 if (min) {
547 assert(count < min);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100548 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name);
549 return LY_EVALID;
550 } else if (max && (count > max)) {
551 LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name);
552 return LY_EVALID;
553 }
554
Michal Vaskoa3881362020-01-21 15:57:35 +0100555 return LY_SUCCESS;
556}
557
Michal Vaskobb844672020-07-03 11:06:12 +0200558/**
559 * @brief Find node referenced by a list unique statement.
560 *
561 * @param[in] uniq_leaf Unique leaf to find.
562 * @param[in] list List instance to use for the search.
563 * @return Found leaf,
564 * @return NULL if no leaf found.
565 */
Michal Vasko14654712020-02-06 08:35:21 +0100566static struct lyd_node *
Michal Vaskobb844672020-07-03 11:06:12 +0200567lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
Michal Vasko14654712020-02-06 08:35:21 +0100568{
Michal Vasko9b368d32020-02-14 13:53:31 +0100569 struct lyd_node *node;
570 const struct lysc_node *iter;
571 size_t depth = 0, i;
Michal Vasko14654712020-02-06 08:35:21 +0100572
Michal Vasko9b368d32020-02-14 13:53:31 +0100573 /* get leaf depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200574 for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
575 ++depth;
Michal Vasko14654712020-02-06 08:35:21 +0100576 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100577
Michal Vaskobb844672020-07-03 11:06:12 +0200578 node = (struct lyd_node *)list;
Michal Vasko9b368d32020-02-14 13:53:31 +0100579 while (node && depth) {
580 /* find schema node with this depth */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200581 for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) {
582 --i;
Michal Vasko9b368d32020-02-14 13:53:31 +0100583 }
584
585 /* find iter instance in children */
586 assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200587 lyd_find_sibling_val(lyd_node_children(node, 0), iter, NULL, 0, &node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100588 --depth;
589 }
590
Michal Vasko14654712020-02-06 08:35:21 +0100591 return node;
592}
593
Michal Vaskobb844672020-07-03 11:06:12 +0200594/**
595 * @brief Callback for comparing 2 list unique leaf values.
596 *
597 * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique.
Michal Vasko14654712020-02-06 08:35:21 +0100598 */
599static int
600lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data)
601{
602 struct ly_ctx *ctx;
603 struct lysc_node_list *slist;
604 struct lyd_node *diter, *first, *second;
605 struct lyd_value *val1, *val2;
606 char *path1, *path2, *uniq_str, *ptr;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200607 LY_ARRAY_COUNT_TYPE u, v, action;
Michal Vasko14654712020-02-06 08:35:21 +0100608
609 assert(val1_p && val2_p);
610
611 first = *((struct lyd_node **)val1_p);
612 second = *((struct lyd_node **)val2_p);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200613 action = (LY_ARRAY_COUNT_TYPE)cb_data;
Michal Vasko14654712020-02-06 08:35:21 +0100614
615 assert(first && (first->schema->nodetype == LYS_LIST));
616 assert(second && (second->schema == first->schema));
617
618 ctx = first->schema->module->ctx;
619
620 slist = (struct lysc_node_list *)first->schema;
621
622 /* compare unique leaves */
623 if (action > 0) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200624 u = action - 1;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200625 if (u < LY_ARRAY_COUNT(slist->uniques)) {
Michal Vasko14654712020-02-06 08:35:21 +0100626 goto uniquecheck;
627 }
628 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200629 LY_ARRAY_FOR(slist->uniques, u) {
Michal Vasko14654712020-02-06 08:35:21 +0100630uniquecheck:
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200631 LY_ARRAY_FOR(slist->uniques[u], v) {
Michal Vasko14654712020-02-06 08:35:21 +0100632 /* first */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200633 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
Michal Vasko14654712020-02-06 08:35:21 +0100634 if (diter) {
635 val1 = &((struct lyd_node_term *)diter)->value;
636 } else {
637 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200638 val1 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100639 }
640
641 /* second */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200642 diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
Michal Vasko14654712020-02-06 08:35:21 +0100643 if (diter) {
644 val2 = &((struct lyd_node_term *)diter)->value;
645 } else {
646 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200647 val2 = slist->uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100648 }
649
650 if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) {
651 /* values differ or either one is not set */
652 break;
653 }
654 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200655 if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
Michal Vasko14654712020-02-06 08:35:21 +0100656 /* all unique leafs are the same in this set, create this nice error */
657 path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0);
658 path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0);
659
660 /* use buffer to rebuild the unique string */
661 uniq_str = malloc(1024);
662 uniq_str[0] = '\0';
663 ptr = uniq_str;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200664 LY_ARRAY_FOR(slist->uniques[u], v) {
665 if (v) {
Michal Vasko14654712020-02-06 08:35:21 +0100666 strcpy(ptr, " ");
667 ++ptr;
668 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200669 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 +0100670 ptr, 1024 - (ptr - uniq_str));
671 if (!ptr) {
672 /* path will be incomplete, whatever */
673 break;
674 }
675
676 ptr += strlen(ptr);
677 }
678 LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2);
679
680 free(path1);
681 free(path2);
682 free(uniq_str);
683 return 1;
684 }
685
686 if (action > 0) {
687 /* done */
688 return 0;
689 }
690 }
691
692 return 0;
693}
694
Michal Vaskobb844672020-07-03 11:06:12 +0200695/**
696 * @brief Validate list unique leaves.
697 *
698 * @param[in] first First sibling to search in.
699 * @param[in] snode Schema node to validate.
700 * @param[in] uniques List unique arrays to validate.
701 * @return LY_ERR value.
702 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100703static LY_ERR
Michal Vaskobb844672020-07-03 11:06:12 +0200704lyd_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 +0100705{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100706 const struct lyd_node *diter;
Michal Vasko14654712020-02-06 08:35:21 +0100707 struct ly_set *set;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200708 LY_ARRAY_COUNT_TYPE u, v, x = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100709 LY_ERR ret = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200710 uint32_t hash, i, size = 0;
Michal Vasko14654712020-02-06 08:35:21 +0100711 int dynamic;
712 const char *str;
713 struct hash_table **uniqtables = NULL;
714 struct lyd_value *val;
715 struct ly_ctx *ctx = snode->module->ctx;
716
717 assert(uniques);
718
719 /* get all list instances */
Michal Vasko9b368d32020-02-14 13:53:31 +0100720 set = ly_set_new();
721 LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100722 LY_LIST_FOR(first, diter) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100723 if (diter->schema == snode) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100724 ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100725 }
726 }
Michal Vasko14654712020-02-06 08:35:21 +0100727
728 if (set->count == 2) {
729 /* simple comparison */
730 if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) {
731 /* instance duplication */
732 ret = LY_EVALID;
733 goto cleanup;
734 }
735 } else if (set->count > 2) {
736 /* use hashes for comparison */
737 /* first, allocate the table, the size depends on number of items in the set */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200738 for (i = 31; i > 0; i--) {
739 size = set->count << i;
740 size = size >> i;
741 if (size == set->count) {
Michal Vasko14654712020-02-06 08:35:21 +0100742 break;
743 }
744 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200745 LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup);
746 i = 32 - i;
747 size = 1 << i;
Michal Vasko14654712020-02-06 08:35:21 +0100748
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200749 uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
Michal Vasko14654712020-02-06 08:35:21 +0100750 LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200751 x = LY_ARRAY_COUNT(uniques);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200752 for (v = 0; v < x; v++) {
753 uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0);
754 LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko14654712020-02-06 08:35:21 +0100755 }
756
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200757 for (i = 0; i < set->count; i++) {
Michal Vasko14654712020-02-06 08:35:21 +0100758 /* loop for unique - get the hash for the instances */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200759 for (u = 0; u < x; u++) {
Michal Vasko14654712020-02-06 08:35:21 +0100760 val = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200761 for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200762 diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
Michal Vasko14654712020-02-06 08:35:21 +0100763 if (diter) {
764 val = &((struct lyd_node_term *)diter)->value;
765 } else {
766 /* use default value */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200767 val = uniques[u][v]->dflt;
Michal Vasko14654712020-02-06 08:35:21 +0100768 }
769 if (!val) {
770 /* unique item not present nor has default value */
771 break;
772 }
773
774 /* get canonical string value */
775 str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic);
776 hash = dict_hash_multi(hash, str, strlen(str));
777 if (dynamic) {
778 free((char *)str);
779 }
780 }
781 if (!val) {
782 /* skip this list instance since its unique set is incomplete */
783 continue;
784 }
785
786 /* finish the hash value */
787 hash = dict_hash_multi(hash, NULL, 0);
788
789 /* insert into the hashtable */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200790 ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
Michal Vasko14654712020-02-06 08:35:21 +0100791 if (ret == LY_EEXIST) {
792 /* instance duplication */
793 ret = LY_EVALID;
794 }
795 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
796 }
797 }
798 }
799
800cleanup:
801 ly_set_free(set, NULL);
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200802 for (v = 0; v < x; v++) {
803 if (!uniqtables[v]) {
Michal Vasko14654712020-02-06 08:35:21 +0100804 /* failed when allocating uniquetables[j], following j are not allocated */
805 break;
806 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200807 lyht_free(uniqtables[v]);
Michal Vasko14654712020-02-06 08:35:21 +0100808 }
809 free(uniqtables);
810
811 return ret;
Michal Vaskoa3881362020-01-21 15:57:35 +0100812}
813
Michal Vaskobb844672020-07-03 11:06:12 +0200814/**
815 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
816 *
817 * @param[in] first First sibling to search in.
818 * @param[in] sparent Schema parent of the nodes to check.
819 * @param[in] mod Module of the nodes to check.
820 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
821 * @param[in] op Operation being validated, if any.
822 * @return LY_ERR value.
823 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100824static LY_ERR
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100825lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent,
Radek Krejci7931b192020-06-25 17:05:03 +0200826 const struct lysc_module *mod, int val_opts, LYD_VALIDATE_OP op)
Michal Vaskocde73ac2019-11-14 16:10:27 +0100827{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100828 const struct lysc_node *snode = NULL;
Michal Vaskoa3881362020-01-21 15:57:35 +0100829 struct lysc_node_list *slist;
Michal Vaskocb7526d2020-03-30 15:08:26 +0200830 int getnext_opts;
831
Radek Krejci7931b192020-06-25 17:05:03 +0200832 getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100833
Michal Vaskoa3881362020-01-21 15:57:35 +0100834 /* disabled nodes are skipped by lys_getnext */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200835 while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
Radek Krejci7931b192020-06-25 17:05:03 +0200836 if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100837 continue;
838 }
839
Michal Vaskoa3881362020-01-21 15:57:35 +0100840 /* check min-elements and max-elements */
841 if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
842 slist = (struct lysc_node_list *)snode;
843 if (slist->min || slist->max) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100844 LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max));
Michal Vaskoa3881362020-01-21 15:57:35 +0100845 }
Michal Vaskoacd83e72020-02-04 14:12:01 +0100846
847 /* check generic mandatory existence */
848 } else if (snode->flags & LYS_MAND_TRUE) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100849 LY_CHECK_RET(lyd_validate_mandatory(first, snode));
Michal Vaskoa3881362020-01-21 15:57:35 +0100850 }
851
852 /* check unique */
853 if (snode->nodetype == LYS_LIST) {
854 slist = (struct lysc_node_list *)snode;
855 if (slist->uniques) {
Michal Vaskobb844672020-07-03 11:06:12 +0200856 LY_CHECK_RET(lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques));
Michal Vaskoa3881362020-01-21 15:57:35 +0100857 }
858 }
859
Michal Vaskoacd83e72020-02-04 14:12:01 +0100860 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
861 /* go recursively for schema-only nodes */
Radek Krejci7931b192020-06-25 17:05:03 +0200862 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts, op));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100863 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100864 }
865
Michal Vaskoacd83e72020-02-04 14:12:01 +0100866 return LY_SUCCESS;
867}
868
Michal Vaskobb844672020-07-03 11:06:12 +0200869/**
870 * @brief Validate obsolete nodes, only warnings are printed.
871 *
872 * @param[in] node Node to check.
873 */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100874static void
875lyd_validate_obsolete(const struct lyd_node *node)
876{
877 const struct lysc_node *snode;
878
879 snode = node->schema;
880 do {
881 if (snode->flags & LYS_STATUS_OBSLT) {
882 LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
883 break;
884 }
885
886 snode = snode->parent;
887 } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
888}
889
Michal Vaskobb844672020-07-03 11:06:12 +0200890/**
891 * @brief Validate must conditions of a data node.
892 *
893 * @param[in] node Node to validate.
894 * @param[in] op Operation being validated, if any.
895 * @return LY_ERR value.
896 */
Michal Vaskocc048b22020-03-27 15:52:38 +0100897static LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200898lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op)
Michal Vaskocc048b22020-03-27 15:52:38 +0100899{
900 struct lyxp_set xp_set;
901 struct lysc_must *musts;
902 const struct lyd_node *tree;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200903 LY_ARRAY_COUNT_TYPE u;
Michal Vaskocc048b22020-03-27 15:52:38 +0100904
905 switch (node->schema->nodetype) {
906 case LYS_CONTAINER:
907 musts = ((struct lysc_node_container *)node->schema)->musts;
908 break;
909 case LYS_LEAF:
910 musts = ((struct lysc_node_leaf *)node->schema)->musts;
911 break;
912 case LYS_LEAFLIST:
913 musts = ((struct lysc_node_leaflist *)node->schema)->musts;
914 break;
915 case LYS_LIST:
916 musts = ((struct lysc_node_list *)node->schema)->musts;
917 break;
918 case LYS_ANYXML:
919 case LYS_ANYDATA:
920 musts = ((struct lysc_node_anydata *)node->schema)->musts;
921 break;
922 case LYS_NOTIF:
923 musts = ((struct lysc_notif *)node->schema)->musts;
924 break;
925 case LYS_RPC:
926 case LYS_ACTION:
Radek Krejci7931b192020-06-25 17:05:03 +0200927 if (op == LYD_VALIDATE_OP_RPC) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200928 musts = ((struct lysc_action *)node->schema)->input.musts;
Radek Krejci7931b192020-06-25 17:05:03 +0200929 } else if (op == LYD_VALIDATE_OP_REPLY) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200930 musts = ((struct lysc_action *)node->schema)->output.musts;
931 } else {
932 LOGINT(LYD_NODE_CTX(node));
933 return LY_EINT;
934 }
Michal Vaskocc048b22020-03-27 15:52:38 +0100935 break;
936 default:
937 LOGINT(LYD_NODE_CTX(node));
938 return LY_EINT;
939 }
940
941 if (!musts) {
942 /* no must to evaluate */
943 return LY_SUCCESS;
944 }
945
946 /* find first top-level node */
947 for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent);
948 while (tree->prev->next) {
949 tree = tree->prev;
950 }
951
952 LY_ARRAY_FOR(musts, u) {
953 memset(&xp_set, 0, sizeof xp_set);
954
955 /* evaluate must */
956 LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA));
957
958 /* check the result */
959 lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +0200960 if (!xp_set.val.bln) {
Michal Vaskocc048b22020-03-27 15:52:38 +0100961 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr);
962 return LY_EVALID;
963 }
964 }
965
966 return LY_SUCCESS;
967}
968
Michal Vaskob1b5c262020-03-05 14:29:47 +0100969LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +0200970lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, int val_opts, LYD_VALIDATE_OP op)
Michal Vaskoacd83e72020-02-04 14:12:01 +0100971{
Michal Vaskob1b5c262020-03-05 14:29:47 +0100972 struct lyd_node *next, *node;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100973 const struct lysc_node *snode;
Michal Vaskoacd83e72020-02-04 14:12:01 +0100974
Michal Vasko14654712020-02-06 08:35:21 +0100975 /* validate all restrictions of nodes themselves */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100976 LY_LIST_FOR_SAFE(first, next, node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100977 if (mod && (lyd_owner_module(node) != mod)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100978 /* all top-level data from this module checked */
979 break;
Michal Vaskof03ed032020-03-04 13:31:44 +0100980 }
981
Michal Vaskoa8c61722020-03-27 16:59:32 +0100982 /* opaque data */
983 if (!node->schema) {
984 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.",
985 ((struct lyd_node_opaq *)node)->name);
986 return LY_EVALID;
987 }
988
Michal Vaskocb7526d2020-03-30 15:08:26 +0200989 /* no state/input/output data */
Radek Krejci7931b192020-06-25 17:05:03 +0200990 if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200991 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name);
992 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200993 } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200994 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name);
995 return LY_EVALID;
Radek Krejci7931b192020-06-25 17:05:03 +0200996 } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_CONFIG_W)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200997 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name);
Michal Vasko5b37a352020-03-06 13:38:33 +0100998 return LY_EVALID;
999 }
1000
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001001 /* obsolete data */
1002 lyd_validate_obsolete(node);
1003
Michal Vaskoc193ce92020-03-06 11:04:48 +01001004 /* node's schema if-features */
1005 if ((snode = lysc_node_is_disabled(node->schema, 1))) {
Michal Vaskoa8c61722020-03-27 16:59:32 +01001006 LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name);
Michal Vaskoc193ce92020-03-06 11:04:48 +01001007 return LY_EVALID;
1008 }
1009
Michal Vaskocc048b22020-03-27 15:52:38 +01001010 /* node's musts */
Radek Krejci7931b192020-06-25 17:05:03 +02001011 LY_CHECK_RET(lyd_validate_must(node, op));
Michal Vaskocc048b22020-03-27 15:52:38 +01001012
Michal Vaskoa8c61722020-03-27 16:59:32 +01001013 /* node value including if-feature was checked by plugins */
Michal Vasko14654712020-02-06 08:35:21 +01001014 }
Michal Vaskocde73ac2019-11-14 16:10:27 +01001015
Michal Vasko14654712020-02-06 08:35:21 +01001016 /* validate schema-based restrictions */
Radek Krejci7931b192020-06-25 17:05:03 +02001017 LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts, op));
Michal Vasko14654712020-02-06 08:35:21 +01001018
Michal Vaskob1b5c262020-03-05 14:29:47 +01001019 LY_LIST_FOR(first, node) {
Michal Vasko14654712020-02-06 08:35:21 +01001020 /* validate all children recursively */
Radek Krejci7931b192020-06-25 17:05:03 +02001021 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 +01001022
Michal Vaskob1b5c262020-03-05 14:29:47 +01001023 /* set default for containers */
1024 if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001025 LY_LIST_FOR(lyd_node_children(node, 0), next) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001026 if (!(next->flags & LYD_DEFAULT)) {
1027 break;
1028 }
Michal Vaskoa3881362020-01-21 15:57:35 +01001029 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001030 if (!next) {
1031 node->flags |= LYD_DEFAULT;
1032 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001033 }
1034 }
1035
1036 return LY_SUCCESS;
1037}
1038
1039LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001040lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001041 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int val_opts)
Michal Vasko9b368d32020-02-14 13:53:31 +01001042{
Michal Vaskob1b5c262020-03-05 14:29:47 +01001043 LY_ERR ret;
Michal Vasko9b368d32020-02-14 13:53:31 +01001044 const struct lysc_node *iter = NULL;
1045 struct lyd_node *node;
1046 struct lyd_value **dflts;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001047 LY_ARRAY_COUNT_TYPE u;
Michal Vasko9b368d32020-02-14 13:53:31 +01001048
Michal Vaskob1b5c262020-03-05 14:29:47 +01001049 assert(first && (parent || sparent || mod) && node_types && node_when);
Michal Vasko9b368d32020-02-14 13:53:31 +01001050
Michal Vaskob1b5c262020-03-05 14:29:47 +01001051 if (!sparent && parent) {
1052 sparent = parent->schema;
1053 }
1054
1055 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
Radek Krejci7931b192020-06-25 17:05:03 +02001056 if ((val_opts & LYD_VALIDATE_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001057 continue;
1058 }
1059
Michal Vasko9b368d32020-02-14 13:53:31 +01001060 switch (iter->nodetype) {
1061 case LYS_CHOICE:
Michal Vaskof03ed032020-03-04 13:31:44 +01001062 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001063 /* create default case data */
1064 LY_CHECK_RET(lyd_validate_defaults_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001065 NULL, node_types, node_when, val_opts));
Michal Vasko9b368d32020-02-14 13:53:31 +01001066 }
1067 break;
1068 case LYS_CONTAINER:
1069 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001070 /* create default NP container */
Michal Vasko9b368d32020-02-14 13:53:31 +01001071 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001072 node->flags = LYD_DEFAULT;
1073 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001074
1075 if (iter->when) {
1076 /* remember to resolve when */
1077 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1078 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001079
1080 /* create any default children */
Michal Vaskoe75ecfd2020-03-06 14:12:28 +01001081 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when, val_opts));
Michal Vasko9b368d32020-02-14 13:53:31 +01001082 }
1083 break;
1084 case LYS_LEAF:
1085 if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1086 /* create default leaf */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001087 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1088 if (ret == LY_EINCOMPLETE) {
1089 /* remember to resolve type */
1090 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1091 } else if (ret) {
1092 return ret;
1093 }
1094 node->flags = LYD_DEFAULT;
1095 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001096
1097 if (iter->when) {
1098 /* remember to resolve when */
1099 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1100 }
1101 }
1102 break;
1103 case LYS_LEAFLIST:
1104 if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_next2(*first, iter, NULL, 0, NULL)) {
1105 /* create all default leaf-lists */
1106 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001107 LY_ARRAY_FOR(dflts, u) {
1108 ret = lyd_create_term2(iter, dflts[u], &node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001109 if (ret == LY_EINCOMPLETE) {
1110 /* remember to resolve type */
1111 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1112 } else if (ret) {
1113 return ret;
1114 }
1115 node->flags = LYD_DEFAULT;
1116 lyd_insert_node(parent, first, node);
Michal Vasko9b368d32020-02-14 13:53:31 +01001117
1118 if (iter->when) {
1119 /* remember to resolve when */
1120 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1121 }
1122 }
1123 }
1124 break;
1125 default:
1126 /* without defaults */
1127 break;
1128 }
1129 }
1130
1131 return LY_SUCCESS;
1132}
1133
Radek Krejci7931b192020-06-25 17:05:03 +02001134/**
Michal Vaskobb844672020-07-03 11:06:12 +02001135 * @brief Validate the whole data subtree.
1136 *
1137 * @param[in] root Subtree root.
1138 * @param[in,out] type_check Set for unres node types.
1139 * @param[in,out] type_meta_check Set for unres metadata types.
1140 * @param[in,out] when_check Set for nodes with when conditions.
1141 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
1142 * @return LY_ERR value.
Radek Krejci7931b192020-06-25 17:05:03 +02001143 */
Michal Vaskob1b5c262020-03-05 14:29:47 +01001144static LY_ERR
Michal Vaskofea12c62020-03-30 11:00:15 +02001145lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check,
Michal Vaskobb844672020-07-03 11:06:12 +02001146 struct ly_set *when_check, int val_opts)
Michal Vaskofea12c62020-03-30 11:00:15 +02001147{
1148 const struct lyd_meta *meta;
1149 struct lyd_node *next, *node;
1150
1151 LYD_TREE_DFS_BEGIN(root, next, node) {
1152 /* skip added default nodes */
1153 if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1154 LY_LIST_FOR(node->meta, meta) {
1155 /* metadata type resolution */
1156 ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST);
1157 }
1158
1159 if (node->schema->nodetype & LYD_NODE_TERM) {
1160 /* node type resolution */
1161 ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST);
1162 } else if (node->schema->nodetype & LYD_NODE_INNER) {
1163 /* new node validation, autodelete */
1164 LY_CHECK_RET(lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL));
1165
1166 /* add nested defaults */
1167 LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check,
Michal Vaskobb844672020-07-03 11:06:12 +02001168 when_check, val_opts));
Michal Vaskofea12c62020-03-30 11:00:15 +02001169 }
1170
1171 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
1172 /* when evaluation */
1173 ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST);
1174 }
1175 }
1176
1177 LYD_TREE_DFS_END(root, next, node);
1178 }
1179
1180 return LY_SUCCESS;
1181}
1182
Michal Vaskobb844672020-07-03 11:06:12 +02001183/**
1184 * @brief Validate data tree.
1185 *
1186 * @param[in,out] tree Data tree to validate, nodes may be autodeleted.
1187 * @param[in] modules Array of modules to validate, NULL for all modules.
1188 * @param[in] mod_count Count of @p modules.
1189 * @param[in] ly_ctx libyang context.
1190 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
1191 * @return LY_ERR value.
1192 */
Michal Vaskofea12c62020-03-30 11:00:15 +02001193static LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001194_lyd_validate(struct lyd_node **tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
1195 int val_opts)
Michal Vaskof03ed032020-03-04 13:31:44 +01001196{
1197 LY_ERR ret = LY_SUCCESS;
Michal Vaskofea12c62020-03-30 11:00:15 +02001198 struct lyd_node *first, *next, **first2;
Michal Vaskob1b5c262020-03-05 14:29:47 +01001199 const struct lys_module *mod;
Michal Vasko9f96a052020-03-10 09:41:45 +01001200 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +01001201 uint32_t i = 0;
Michal Vaskof03ed032020-03-04 13:31:44 +01001202
Michal Vaskob1b5c262020-03-05 14:29:47 +01001203 LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || (modules && mod_count), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001204
Michal Vaskob1b5c262020-03-05 14:29:47 +01001205 next = *tree;
1206 while (1) {
Radek Krejci7931b192020-06-25 17:05:03 +02001207 if (val_opts & LYD_VALIDATE_PRESENT) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001208 mod = lyd_data_next_module(&next, &first);
1209 } else {
1210 mod = lyd_mod_next_module(next, modules, mod_count, ctx, &i, &first);
Michal Vaskof03ed032020-03-04 13:31:44 +01001211 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001212 if (!mod) {
1213 break;
1214 }
Michal Vasko7c4cf1e2020-06-22 10:04:30 +02001215 if (!first || (first == *tree)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001216 /* make sure first2 changes are carried to tree */
1217 first2 = tree;
1218 } else {
1219 first2 = &first;
1220 }
1221
1222 /* validate new top-level nodes of this module, autodelete */
Michal Vaskofea12c62020-03-30 11:00:15 +02001223 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001224
1225 /* add all top-level defaults for this module */
Michal Vaskofea12c62020-03-30 11:00:15 +02001226 LY_CHECK_GOTO(ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &type_check, &when_check, val_opts), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001227
1228 /* process nested nodes */
1229 LY_LIST_FOR(*first2, first) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001230 LY_CHECK_GOTO(ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001231 }
1232
1233 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vasko9f96a052020-03-10 09:41:45 +01001234 ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, LYD_JSON, lydjson_resolve_prefix, NULL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001235 LY_CHECK_GOTO(ret, cleanup);
1236
1237 /* perform final validation that assumes the data tree is final */
Radek Krejci7931b192020-06-25 17:05:03 +02001238 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, val_opts, 0), cleanup);
Michal Vaskof03ed032020-03-04 13:31:44 +01001239 }
1240
Michal Vaskof03ed032020-03-04 13:31:44 +01001241cleanup:
1242 ly_set_erase(&type_check, NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +01001243 ly_set_erase(&type_meta_check, NULL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001244 ly_set_erase(&when_check, NULL);
1245 return ret;
1246}
Michal Vaskob1b5c262020-03-05 14:29:47 +01001247
1248API LY_ERR
1249lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts)
1250{
1251 return _lyd_validate(tree, NULL, 0, ctx, val_opts);
1252}
1253
1254API LY_ERR
1255lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts)
1256{
1257 return _lyd_validate(tree, modules, mod_count, NULL, val_opts);
1258}
Michal Vaskofea12c62020-03-30 11:00:15 +02001259
Michal Vaskobb844672020-07-03 11:06:12 +02001260/**
1261 * @brief Find nodes for merging an operation into data tree for validation.
1262 *
1263 * @param[in] op_tree Full operation data tree.
1264 * @param[in] op_node Operation node itself.
1265 * @param[in] tree Data tree to be merged into.
1266 * @param[out] op_subtree Operation subtree to merge.
1267 * @param[out] tree_sibling Data tree sibling to merge next to.
1268 */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001269static void
Michal Vaskobb844672020-07-03 11:06:12 +02001270lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
1271 struct lyd_node **op_subtree, struct lyd_node **tree_sibling)
Michal Vaskofea12c62020-03-30 11:00:15 +02001272{
Michal Vaskocb7526d2020-03-30 15:08:26 +02001273 const struct lyd_node *tree_iter, *op_iter;
1274 struct lyd_node *match;
Michal Vaskofea12c62020-03-30 11:00:15 +02001275 uint32_t i, cur_depth, op_depth;
Michal Vaskofea12c62020-03-30 11:00:15 +02001276
Michal Vaskocb7526d2020-03-30 15:08:26 +02001277 /* learn op depth (top-level being depth 0) */
Michal Vaskofea12c62020-03-30 11:00:15 +02001278 op_depth = 0;
Michal Vaskobb844672020-07-03 11:06:12 +02001279 for (op_iter = op_node; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) {
Michal Vaskofea12c62020-03-30 11:00:15 +02001280 ++op_depth;
1281 }
1282
1283 /* find where to merge op */
1284 tree_iter = tree;
1285 cur_depth = op_depth;
Michal Vaskobb844672020-07-03 11:06:12 +02001286 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001287 while (cur_depth) {
1288 /* find next op parent */
Michal Vaskobb844672020-07-03 11:06:12 +02001289 op_iter = op_node;
Michal Vaskofea12c62020-03-30 11:00:15 +02001290 for (i = 0; i < cur_depth; ++i) {
1291 op_iter = (struct lyd_node *)op_iter->parent;
1292 }
1293
1294 /* find op iter in tree */
1295 lyd_find_sibling_first(tree_iter, op_iter, &match);
1296 if (!match) {
1297 break;
1298 }
1299
1300 /* move tree_iter */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001301 tree_iter = lyd_node_children(match, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001302
1303 /* move depth */
1304 --cur_depth;
1305 }
1306
Michal Vaskobb844672020-07-03 11:06:12 +02001307 *op_subtree = (struct lyd_node *)op_iter;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001308 *tree_sibling = (struct lyd_node *)tree_iter;
1309}
1310
1311API LY_ERR
Radek Krejci7931b192020-06-25 17:05:03 +02001312lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, LYD_VALIDATE_OP op)
Michal Vaskocb7526d2020-03-30 15:08:26 +02001313{
1314 LY_ERR ret;
Radek Krejci7931b192020-06-25 17:05:03 +02001315 struct lyd_node *tree_sibling, *op_subtree, *op_next, *op_node, *op_parent;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001316 struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0};
1317
1318 LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent,
Radek Krejci7931b192020-06-25 17:05:03 +02001319 (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001320
1321 /* find the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001322 LYD_TREE_DFS_BEGIN(op_tree, op_next, op_node) {
1323 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 +02001324 break;
Radek Krejci7931b192020-06-25 17:05:03 +02001325 } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001326 break;
1327 }
Radek Krejci7931b192020-06-25 17:05:03 +02001328 LYD_TREE_DFS_END(op_tree, op_next, op_node);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001329 }
Radek Krejci7931b192020-06-25 17:05:03 +02001330 if (op == LYD_VALIDATE_OP_RPC || op == LYD_VALIDATE_OP_REPLY) {
1331 if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001332 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
1333 return LY_EINVAL;
1334 }
1335 } else {
Radek Krejci7931b192020-06-25 17:05:03 +02001336 if (op_node->schema->nodetype != LYS_NOTIF) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001337 LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
1338 return LY_EINVAL;
1339 }
1340 }
1341
1342 /* move op_tree to top-level node */
1343 while (op_tree->parent) {
1344 op_tree = (struct lyd_node *)op_tree->parent;
1345 }
1346
1347 /* merge op_tree into tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001348 lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling);
1349 op_parent = (struct lyd_node *)op_subtree->parent;
1350 lyd_unlink_tree(op_subtree);
1351 lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001352 if (!tree) {
Michal Vaskocb7526d2020-03-30 15:08:26 +02001353 tree = tree_sibling;
Michal Vaskofea12c62020-03-30 11:00:15 +02001354 }
1355
1356 /* prevalidate whole operation subtree */
Radek Krejci7931b192020-06-25 17:05:03 +02001357 LY_CHECK_GOTO(ret = lyd_validate_subtree(op_node, &type_check, &type_meta_check, &when_check, 0), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001358
1359 /* finish incompletely validated terminal values/attributes and when conditions on the full tree */
1360 LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check,
1361 LYD_JSON, lydjson_resolve_prefix, NULL), cleanup);
1362
1363 /* perform final validation of the operation/notification */
Radek Krejci7931b192020-06-25 17:05:03 +02001364 lyd_validate_obsolete(op_node);
1365 if (lysc_node_is_disabled(op_node->schema, 1)) {
1366 LOGVAL(LYD_NODE_CTX(op_tree), LY_VLOG_LYD, op_node, LY_VCODE_NOIFF, op_node->schema->name);
Michal Vaskofea12c62020-03-30 11:00:15 +02001367 ret = LY_EVALID;
1368 goto cleanup;
1369 }
Radek Krejci7931b192020-06-25 17:05:03 +02001370 LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup);
Michal Vaskofea12c62020-03-30 11:00:15 +02001371
1372 /* final validation of all the descendants */
Radek Krejci7931b192020-06-25 17:05:03 +02001373 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 +02001374
1375cleanup:
1376 /* restore operation tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001377 lyd_unlink_tree(op_subtree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001378 if (op_parent) {
Radek Krejci7931b192020-06-25 17:05:03 +02001379 lyd_insert_node(op_parent, NULL, op_subtree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001380 }
1381
1382 ly_set_erase(&type_check, NULL);
1383 ly_set_erase(&type_meta_check, NULL);
1384 ly_set_erase(&when_check, NULL);
1385 return ret;
1386}