Coverage Report

Created: 2025-08-24 06:43

/src/libyang/src/validation.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file validation.c
3
 * @author Michal Vasko <mvasko@cesnet.cz>
4
 * @brief Validation
5
 *
6
 * Copyright (c) 2019 - 2024 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
#define _GNU_SOURCE /* asprintf, strdup */
15
16
#include "validation.h"
17
18
#include <assert.h>
19
#include <limits.h>
20
#include <stdint.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include "compat.h"
26
#include "diff.h"
27
#include "hash_table.h"
28
#include "log.h"
29
#include "ly_common.h"
30
#include "parser_data.h"
31
#include "parser_internal.h"
32
#include "plugins_exts.h"
33
#include "plugins_exts/metadata.h"
34
#include "plugins_types.h"
35
#include "set.h"
36
#include "tree.h"
37
#include "tree_data.h"
38
#include "tree_data_internal.h"
39
#include "tree_schema.h"
40
#include "tree_schema_internal.h"
41
#include "xpath.h"
42
43
/**
44
 * @brief Check validation error taking into account multi-error validation.
45
 *
46
 * @param[in] r Local return value.
47
 * @param[in] err_cmd Command to perform on any error.
48
 * @param[in] val_opts Validation options.
49
 * @param[in] label Label to go to on fatal error.
50
 */
51
#define LY_VAL_ERR_GOTO(r, err_cmd, val_opts, label) \
52
94.7k
        if (r) { \
53
589
            err_cmd; \
54
589
            if ((r != LY_EVALID) || !(val_opts & LYD_VALIDATE_MULTI_ERROR)) { \
55
589
                goto label; \
56
589
            } \
57
589
        }
58
59
/**
60
 * @brief Callback for freeing getnext HT values.
61
 */
62
static void
63
lyd_val_getnext_ht_free_cb(void *val_p)
64
1.93k
{
65
1.93k
    struct lyd_val_getnext *val = val_p;
66
67
1.93k
    free(val->snodes);
68
1.93k
    free(val->choices);
69
1.93k
}
70
71
/**
72
 * @brief Callback for checking getnext HT value equality.
73
 */
74
static ly_bool
75
lyd_val_getnext_ht_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
76
38.1k
{
77
38.1k
    struct lyd_val_getnext *val1 = val1_p;
78
38.1k
    struct lyd_val_getnext *val2 = val2_p;
79
80
38.1k
    if (val1->sparent == val2->sparent) {
81
38.1k
        return 1;
82
38.1k
    }
83
0
    return 0;
84
38.1k
}
85
86
LY_ERR
87
lyd_val_getnext_ht_new(struct ly_ht **getnext_ht_p)
88
1.14k
{
89
1.14k
    *getnext_ht_p = lyht_new(32, sizeof(struct lyd_val_getnext), lyd_val_getnext_ht_equal_cb, NULL, 1);
90
91
1.14k
    if (!*getnext_ht_p) {
92
0
        LOGMEM(NULL);
93
0
        return LY_EMEM;
94
0
    }
95
1.14k
    return LY_SUCCESS;
96
1.14k
}
97
98
void
99
lyd_val_getnext_ht_free(struct ly_ht *getnext_ht)
100
10.2k
{
101
10.2k
    lyht_free(getnext_ht, lyd_val_getnext_ht_free_cb);
102
10.2k
}
103
104
LY_ERR
105
lyd_val_getnext_get(const struct lysc_node *sparent, const struct lys_module *mod, const struct lysc_ext_instance *ext,
106
        ly_bool output, struct ly_ht *getnext_ht, const struct lysc_node ***choices, const struct lysc_node ***snodes)
107
40.1k
{
108
40.1k
    LY_ERR rc = LY_SUCCESS;
109
40.1k
    struct lyd_val_getnext val = {0}, *getnext = NULL;
110
40.1k
    const struct lysc_node *snode = NULL;
111
40.1k
    uint32_t getnext_opts, snode_count = 0, choice_count = 0;
112
113
    /* try to find the entry for this schema parent */
114
40.1k
    val.sparent = sparent;
115
40.1k
    if (!lyht_find(getnext_ht, &val, (uintptr_t)sparent, (void **)&getnext)) {
116
38.1k
        goto cleanup;
117
38.1k
    }
118
119
    /* traverse all the children using getnext and store them */
120
1.93k
    getnext_opts = LYS_GETNEXT_WITHCHOICE | (output ? LYS_GETNEXT_OUTPUT : 0);
121
18.5k
    while (ext ? (snode = lys_getnext_ext(snode, sparent, ext, getnext_opts)) :
122
18.5k
            (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
123
16.6k
        if (snode->nodetype == LYS_CHOICE) {
124
            /* store a choice node */
125
0
            val.choices = ly_realloc(val.choices, (choice_count + 2) * sizeof *val.choices);
126
0
            LY_CHECK_ERR_GOTO(!val.choices, LOGMEM(NULL); rc = LY_EMEM, cleanup);
127
0
            val.choices[choice_count] = snode;
128
0
            ++choice_count;
129
16.6k
        } else {
130
            /* store other nodes */
131
16.6k
            val.snodes = ly_realloc(val.snodes, (snode_count + 2) * sizeof *val.snodes);
132
16.6k
            LY_CHECK_ERR_GOTO(!val.snodes, LOGMEM(NULL); rc = LY_EMEM, cleanup);
133
16.6k
            val.snodes[snode_count] = snode;
134
16.6k
            ++snode_count;
135
16.6k
        }
136
16.6k
    }
137
138
    /* add terminating NULL items */
139
1.93k
    if (choice_count) {
140
0
        val.choices[choice_count] = NULL;
141
0
    }
142
1.93k
    if (snode_count) {
143
1.63k
        val.snodes[snode_count] = NULL;
144
1.63k
    }
145
146
    /* add into the hash table */
147
1.93k
    if ((rc = lyht_insert(getnext_ht, &val, (uintptr_t)sparent, (void **)&getnext))) {
148
0
        goto cleanup;
149
0
    }
150
151
40.1k
cleanup:
152
40.1k
    if (rc) {
153
0
        free(val.snodes);
154
0
        free(val.choices);
155
40.1k
    } else {
156
40.1k
        *choices = getnext->choices;
157
40.1k
        *snodes = getnext->snodes;
158
40.1k
    }
159
40.1k
    return rc;
160
1.93k
}
161
162
LY_ERR
163
lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
164
0
{
165
0
    LY_ERR ret = LY_SUCCESS;
166
0
    struct lyd_node *new_diff = NULL;
167
0
    const struct lyd_node *prev_inst;
168
0
    char *key = NULL, *value = NULL, *position = NULL;
169
0
    size_t buflen = 0, bufused = 0;
170
0
    uint32_t pos;
171
172
0
    assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
173
174
0
    if ((op == LYD_DIFF_OP_CREATE) && lysc_is_userordered(node->schema)) {
175
0
        if (lysc_is_dup_inst_list(node->schema)) {
176
0
            pos = lyd_list_pos(node);
177
178
            /* generate position meta */
179
0
            if (pos > 1) {
180
0
                if (asprintf(&position, "%" PRIu32, pos - 1) == -1) {
181
0
                    LOGMEM(LYD_CTX(node));
182
0
                    ret = LY_EMEM;
183
0
                    goto cleanup;
184
0
                }
185
0
            } else {
186
0
                position = strdup("");
187
0
                LY_CHECK_ERR_GOTO(!position, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
188
0
            }
189
0
        } else {
190
0
            if (node->prev->next && (node->prev->schema == node->schema)) {
191
0
                prev_inst = node->prev;
192
0
            } else {
193
                /* first instance */
194
0
                prev_inst = NULL;
195
0
            }
196
197
0
            if (node->schema->nodetype == LYS_LIST) {
198
                /* generate key meta */
199
0
                if (prev_inst) {
200
0
                    LY_CHECK_GOTO(ret = lyd_path_list_predicate(prev_inst, &key, &buflen, &bufused, 0), cleanup);
201
0
                } else {
202
0
                    key = strdup("");
203
0
                    LY_CHECK_ERR_GOTO(!key, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
204
0
                }
205
0
            } else {
206
                /* generate value meta */
207
0
                if (prev_inst) {
208
0
                    value = strdup(lyd_get_value(prev_inst));
209
0
                    LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
210
0
                } else {
211
0
                    value = strdup("");
212
0
                    LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
213
0
                }
214
0
            }
215
0
        }
216
0
    }
217
218
    /* create new diff tree */
219
0
    LY_CHECK_GOTO(ret = lyd_diff_add(node, op, NULL, NULL, key, value, position, NULL, NULL, &new_diff, NULL), cleanup);
220
221
    /* merge into existing diff */
222
0
    ret = lyd_diff_merge_all(diff, new_diff, 0);
223
224
0
cleanup:
225
0
    lyd_free_tree(new_diff);
226
0
    free(key);
227
0
    free(value);
228
0
    free(position);
229
0
    return ret;
230
0
}
231
232
/**
233
 * @brief Evaluate all relevant "when" conditions of a node.
234
 *
235
 * @param[in] tree Data tree.
236
 * @param[in] node Node whose relevant when conditions will be evaluated.
237
 * @param[in] schema Schema node of @p node. It may not be possible to use directly if @p node is opaque.
238
 * @param[in] xpath_options Additional XPath options to use.
239
 * @param[out] disabled First when that evaluated false, if any.
240
 * @return LY_SUCCESS on success.
241
 * @return LY_EINCOMPLETE if a referenced node does not have its when evaluated.
242
 * @return LY_ERR value on error.
243
 */
244
static LY_ERR
245
lyd_validate_node_when(const struct lyd_node *tree, const struct lyd_node *node, const struct lysc_node *schema,
246
        uint32_t xpath_options, const struct lysc_when **disabled)
247
0
{
248
0
    LY_ERR r;
249
0
    const struct lyd_node *ctx_node;
250
0
    struct lyxp_set xp_set;
251
0
    LY_ARRAY_COUNT_TYPE u;
252
253
0
    assert(!node->schema || (node->schema == schema));
254
255
0
    *disabled = NULL;
256
257
0
    do {
258
0
        const struct lysc_when *when;
259
0
        struct lysc_when **when_list = lysc_node_when(schema);
260
261
0
        LY_ARRAY_FOR(when_list, u) {
262
0
            when = when_list[u];
263
264
            /* get context node */
265
0
            if (when->context == schema) {
266
0
                ctx_node = node;
267
0
            } else {
268
0
                assert((!when->context && !node->parent) || (when->context == node->parent->schema));
269
0
                ctx_node = lyd_parent(node);
270
0
            }
271
272
            /* evaluate when */
273
0
            memset(&xp_set, 0, sizeof xp_set);
274
0
            r = lyxp_eval(LYD_CTX(node), when->cond, schema->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
275
0
                    ctx_node, ctx_node, tree, NULL, &xp_set, LYXP_SCHEMA | xpath_options);
276
0
            lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
277
278
            /* return error or LY_EINCOMPLETE for dependant unresolved when */
279
0
            LY_CHECK_RET(r);
280
281
0
            if (!xp_set.val.bln) {
282
                /* false when */
283
0
                *disabled = when;
284
0
                return LY_SUCCESS;
285
0
            }
286
0
        }
287
288
0
        schema = schema->parent;
289
0
    } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
290
291
0
    return LY_SUCCESS;
292
0
}
293
294
/**
295
 * @brief Properly delete a node as part of auto-delete validation tasks.
296
 *
297
 * @param[in,out] first First sibling, is updated if needed.
298
 * @param[in] del Node instance to delete.
299
 * @param[in] mod Module of the siblings, NULL for nested siblings.
300
 * @param[in] np_cont_diff Whether to put NP container into diff or only its children.
301
 * @param[in,out] node Optional current iteration node, update it if it is deleted.
302
 * @param[in,out] node_when Optional set with nodes with "when" conditions, may be removed from.
303
 * @param[in,out] node_types Optional set with unresolved type nodes, may be removed from.
304
 * @param[in,out] diff Validation diff.
305
 * @return 1 if @p node auto-deleted and updated to its next sibling.
306
 * @return 0 if @p node was not auto-deleted.
307
 */
308
static ly_bool
309
lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *del, const struct lys_module *mod,
310
        int np_cont_diff, struct lyd_node **node, struct ly_set *node_when, struct ly_set *node_types, struct lyd_node **diff)
311
0
{
312
0
    struct lyd_node *iter;
313
0
    ly_bool node_autodel = 0;
314
0
    uint32_t idx;
315
316
    /* update pointers */
317
0
    lyd_del_move_root(first, del, mod);
318
0
    if (node && (del == *node)) {
319
0
        *node = (*node)->next;
320
0
        node_autodel = 1;
321
0
    }
322
323
0
    if (diff) {
324
        /* add into diff */
325
0
        if (!np_cont_diff && (del->schema->nodetype == LYS_CONTAINER) && !(del->schema->flags & LYS_PRESENCE)) {
326
            /* we do not want to track NP container changes, but remember any removed children */
327
0
            LY_LIST_FOR(lyd_child(del), iter) {
328
0
                lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
329
0
            }
330
0
        } else {
331
0
            lyd_val_diff_add(del, LYD_DIFF_OP_DELETE, diff);
332
0
        }
333
0
    }
334
335
0
    if (node_when && node_when->count) {
336
        /* remove nested from node_when set */
337
0
        LYD_TREE_DFS_BEGIN(del, iter) {
338
0
            if ((del != iter) && ly_set_contains(node_when, iter, &idx)) {
339
0
                assert(0 && "Please contact libyang support with the use-case that triggered this assert.");
340
                // ly_set_rm_index(node_when, idx, NULL);
341
0
            }
342
0
            LYD_TREE_DFS_END(del, iter);
343
0
        }
344
0
    }
345
346
0
    if (node_types && node_types->count) {
347
        /* remove from node_types set */
348
0
        LYD_TREE_DFS_BEGIN(del, iter) {
349
0
            if ((iter->schema->nodetype & LYD_NODE_TERM) &&
350
0
                    ((struct lysc_node_leaf *)iter->schema)->type->plugin->validate &&
351
0
                    ly_set_contains(node_types, iter, &idx)) {
352
0
                ly_set_rm_index(node_types, idx, NULL);
353
0
            }
354
0
            LYD_TREE_DFS_END(del, iter);
355
0
        }
356
0
    }
357
358
    /* free */
359
0
    lyd_free_tree(del);
360
361
0
    return node_autodel;
362
0
}
363
364
/**
365
 * @brief Evaluate when conditions of collected unres nodes.
366
 *
367
 * @param[in,out] tree Data tree, is updated if some nodes are autodeleted.
368
 * @param[in] mod Module of the @p tree to take into consideration when deleting @p tree and moving it.
369
 * If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be
370
 * the first top-level sibling.
371
 * @param[in] node_when Set with nodes with "when" conditions.
372
 * @param[in] val_opts Validation options.
373
 * @param[in] xpath_options Additional XPath options to use.
374
 * @param[in,out] node_types Set with nodes with unresolved types, remove any with false "when" parents.
375
 * @param[in,out] diff Validation diff.
376
 * @return LY_SUCCESS on success.
377
 * @return LY_ERR value on error.
378
 */
379
static LY_ERR
380
lyd_validate_unres_when(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when, uint32_t val_opts,
381
        uint32_t xpath_options, struct ly_set *node_types, struct lyd_node **diff)
382
288
{
383
288
    LY_ERR rc = LY_SUCCESS, r;
384
288
    uint32_t i, count;
385
288
    const struct lysc_when *disabled;
386
288
    struct lyd_node *node = NULL;
387
388
288
    if (!node_when->count) {
389
288
        return LY_SUCCESS;
390
288
    }
391
392
0
    i = node_when->count;
393
0
    do {
394
0
        --i;
395
0
        node = node_when->dnodes[i];
396
0
        LOG_LOCSET(node->schema, node);
397
398
        /* evaluate all when expressions that affect this node's existence */
399
0
        r = lyd_validate_node_when(*tree, node, node->schema, xpath_options, &disabled);
400
0
        if (!r) {
401
0
            if (disabled) {
402
                /* when false */
403
0
                if (node->flags & LYD_WHEN_TRUE) {
404
                    /* autodelete */
405
0
                    count = node_when->count;
406
0
                    lyd_validate_autodel_node_del(tree, node, mod, 1, NULL, node_when, node_types, diff);
407
0
                    if (count > node_when->count) {
408
                        /* nested nodes removed, we lost the index */
409
0
                        ly_set_contains(node_when, node, &i);
410
0
                    }
411
0
                } else if (val_opts & LYD_VALIDATE_OPERATIONAL) {
412
                    /* only a warning */
413
0
                    LOGWRN(LYD_CTX(node), "When condition \"%s\" not satisfied.", disabled->cond->expr);
414
0
                } else {
415
                    /* invalid data */
416
0
                    LOGVAL(LYD_CTX(node), LY_VCODE_NOWHEN, disabled->cond->expr);
417
0
                    r = LY_EVALID;
418
0
                    LY_VAL_ERR_GOTO(r, rc = r, val_opts, error);
419
0
                }
420
0
            } else {
421
                /* when true */
422
0
                node->flags |= LYD_WHEN_TRUE;
423
0
            }
424
425
            /* remove this node from the set keeping the order, its when was resolved */
426
0
            ly_set_rm_index_ordered(node_when, i, NULL);
427
0
        } else if (r != LY_EINCOMPLETE) {
428
            /* error */
429
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, error);
430
0
        }
431
432
0
        LOG_LOCBACK(1, 1);
433
0
    } while (i);
434
435
0
    return rc;
436
437
0
error:
438
0
    LOG_LOCBACK(1, 1);
439
0
    return rc;
440
0
}
441
442
LY_ERR
443
lyd_validate_unres(struct lyd_node **tree, const struct lys_module *mod, enum lyd_type data_type, struct ly_set *node_when,
444
        uint32_t when_xp_opts, struct ly_set *node_types, struct ly_set *meta_types, struct ly_set *ext_node,
445
        struct ly_set *ext_val, uint32_t val_opts, struct lyd_node **diff)
446
288
{
447
288
    LY_ERR r, rc = LY_SUCCESS;
448
288
    uint32_t i;
449
450
288
    if (ext_val && ext_val->count) {
451
        /* first validate parsed extension data */
452
0
        i = ext_val->count;
453
0
        do {
454
0
            --i;
455
456
0
            struct lyd_ctx_ext_val *ext_v = ext_val->objs[i];
457
458
            /* validate extension data */
459
0
            r = ext_v->ext->def->plugin->validate(ext_v->ext, ext_v->sibling, *tree, data_type, val_opts, diff);
460
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
461
462
            /* remove this item from the set */
463
0
            ly_set_rm_index(ext_val, i, free);
464
0
        } while (i);
465
0
    }
466
467
288
    if (ext_node && ext_node->count) {
468
        /* validate data nodes with extension instances */
469
0
        i = ext_node->count;
470
0
        do {
471
0
            --i;
472
473
0
            struct lyd_ctx_ext_node *ext_n = ext_node->objs[i];
474
475
            /* validate the node */
476
0
            r = ext_n->ext->def->plugin->node(ext_n->ext, ext_n->node, val_opts);
477
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
478
479
            /* remove this item from the set */
480
0
            ly_set_rm_index(ext_node, i, free);
481
0
        } while (i);
482
0
    }
483
484
288
    if (node_when) {
485
        /* evaluate all when conditions */
486
288
        uint32_t prev_count;
487
488
288
        do {
489
288
            prev_count = node_when->count;
490
288
            r = lyd_validate_unres_when(tree, mod, node_when, val_opts, when_xp_opts, node_types, diff);
491
288
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
492
493
            /* there must have been some when conditions resolved */
494
288
        } while (prev_count > node_when->count);
495
496
288
        if (node_when->count) {
497
            /* there could have been no cyclic when dependencies, checked during compilation */
498
0
            assert((rc == LY_EVALID) && (val_opts & LYD_VALIDATE_MULTI_ERROR));
499
500
            /* when condition was validated and it is not satisfied, error printed, if kept in the set the following
501
             * unres (for the next module) can fail this assert */
502
0
            ly_set_erase(node_when, NULL);
503
0
        }
504
288
    }
505
506
288
    if (node_types && node_types->count) {
507
        /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */
508
166
        i = node_types->count;
509
180
        do {
510
180
            --i;
511
512
180
            struct lyd_node_term *node = node_types->objs[i];
513
180
            struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type;
514
515
            /* resolve the value of the node */
516
180
            LOG_LOCSET(NULL, &node->node);
517
180
            r = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, &node->node, *tree);
518
180
            LOG_LOCBACK(0, 1);
519
180
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
520
521
            /* remove this node from the set */
522
52
            ly_set_rm_index(node_types, i, NULL);
523
52
        } while (i);
524
166
    }
525
526
160
    if (meta_types && meta_types->count) {
527
        /* ... and metadata values */
528
0
        i = meta_types->count;
529
0
        do {
530
0
            --i;
531
532
0
            struct lyd_meta *meta = meta_types->objs[i];
533
0
            struct lysc_type *type;
534
535
            /* validate and store the value of the metadata */
536
0
            lyplg_ext_get_storage(meta->annotation, LY_STMT_TYPE, sizeof type, (const void **)&type);
537
0
            r = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree);
538
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
539
540
            /* remove this attr from the set */
541
0
            ly_set_rm_index(meta_types, i, NULL);
542
0
        } while (i);
543
0
    }
544
545
288
cleanup:
546
288
    return rc;
547
160
}
548
549
/**
550
 * @brief Compare callback for finding duplicates in hash table.
551
 *
552
 * Implementation of ::lyht_value_equal_cb.
553
 */
554
static ly_bool
555
lyd_val_dup_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
556
55.0k
{
557
    /* always find the same instance, not the exact same pointer (set mod = 0) */
558
55.0k
    return lyd_hash_table_val_equal(val1_p, val2_p, 0, cb_data);
559
55.0k
}
560
561
/**
562
 * @brief Validate instance duplication.
563
 *
564
 * @param[in] first First sibling to search in.
565
 * @param[in] node Data node instance to check.
566
 * @param[in] val_opts Validation options.
567
 * @return LY_ERR value.
568
 */
569
static LY_ERR
570
lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node, uint32_t val_opts)
571
66.5k
{
572
66.5k
    ly_bool fail = 0;
573
574
66.5k
    assert(node->flags & LYD_NEW);
575
576
    /* key-less list or non-configuration leaf-list */
577
66.5k
    if (lysc_is_dup_inst_list(node->schema)) {
578
        /* duplicate instances allowed */
579
0
        return LY_SUCCESS;
580
0
    }
581
582
    /* find exactly the same next instance using hashes if possible */
583
66.5k
    if (node->parent && node->parent->children_ht) {
584
        /* because of the callback used, an instance must always be found (pointer may or may not be equal to node),
585
         * so if we find another instance, there is a duplicate */
586
55.0k
        if (!lyht_find_next_with_collision_cb(node->parent->children_ht, &node, node->hash, lyd_val_dup_val_equal, NULL)) {
587
60
            fail = 1;
588
60
        }
589
55.0k
    } else {
590
61.2k
        for ( ; first; first = first->next) {
591
49.8k
            if (first == node) {
592
11.5k
                continue;
593
11.5k
            }
594
595
38.3k
            if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
596
8.50k
                if (first->schema == node->schema) {
597
72
                    fail = 1;
598
72
                    break;
599
72
                }
600
29.8k
            } else if (!lyd_compare_single(first, node, 0)) {
601
68
                fail = 1;
602
68
                break;
603
68
            }
604
38.3k
        }
605
11.5k
    }
606
607
66.5k
    if (fail) {
608
200
        if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (val_opts & LYD_VALIDATE_OPERATIONAL)) {
609
            /* only a warning */
610
0
            LOG_LOCSET(NULL, node);
611
0
            LOGWRN(node->schema->module->ctx, "Duplicate instance of \"%s\".", node->schema->name);
612
0
            LOG_LOCBACK(0, 1);
613
200
        } else {
614
200
            LOG_LOCSET(NULL, node);
615
200
            LOGVAL(node->schema->module->ctx, LY_VCODE_DUP, node->schema->name);
616
200
            LOG_LOCBACK(0, 1);
617
200
            return LY_EVALID;
618
200
        }
619
200
    }
620
66.3k
    return LY_SUCCESS;
621
66.5k
}
622
623
/**
624
 * @brief Validate multiple case data existence with possible autodelete.
625
 *
626
 * @param[in,out] first First sibling to search in, is updated if needed.
627
 * @param[in] mod Module of the siblings, NULL for nested siblings.
628
 * @param[in] choic Choice node whose cases to check.
629
 * @param[in,out] diff Validation diff.
630
 * @return LY_ERR value.
631
 */
632
static LY_ERR
633
lyd_validate_cases(struct lyd_node **first, const struct lys_module *mod, const struct lysc_node_choice *choic,
634
        struct lyd_node **diff)
635
0
{
636
0
    const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
637
0
    struct lyd_node *match, *to_del;
638
0
    ly_bool found;
639
640
0
    LOG_LOCSET(&choic->node, NULL);
641
642
0
    LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
643
0
        found = 0;
644
0
        iter = NULL;
645
0
        match = NULL;
646
0
        while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
647
0
            if (match->flags & LYD_NEW) {
648
                /* a new case data found, nothing more to look for */
649
0
                found = 2;
650
0
                break;
651
0
            } else {
652
                /* and old case data found */
653
0
                if (found == 0) {
654
0
                    found = 1;
655
0
                }
656
0
            }
657
0
        }
658
659
0
        if (found == 1) {
660
            /* there should not be 2 old cases */
661
0
            if (old_case) {
662
                /* old data from 2 cases */
663
0
                LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, old_case->name, scase->name);
664
0
                LOG_LOCBACK(1, 0);
665
0
                return LY_EVALID;
666
0
            }
667
668
            /* remember an old existing case */
669
0
            old_case = scase;
670
0
        } else if (found == 2) {
671
0
            if (new_case) {
672
                /* new data from 2 cases */
673
0
                LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, new_case->name, scase->name);
674
0
                LOG_LOCBACK(1, 0);
675
0
                return LY_EVALID;
676
0
            }
677
678
            /* remember a new existing case */
679
0
            new_case = scase;
680
0
        }
681
0
    }
682
683
0
    LOG_LOCBACK(1, 0);
684
685
0
    if (old_case && new_case) {
686
        /* auto-delete old case */
687
0
        iter = NULL;
688
0
        match = NULL;
689
0
        to_del = NULL;
690
0
        while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
691
0
            lyd_del_move_root(first, to_del, mod);
692
693
            /* free previous node */
694
0
            lyd_free_tree(to_del);
695
0
            if (diff) {
696
                /* add into diff */
697
0
                LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
698
0
            }
699
0
            to_del = match;
700
0
        }
701
0
        lyd_del_move_root(first, to_del, mod);
702
0
        lyd_free_tree(to_del);
703
0
    }
704
705
0
    return LY_SUCCESS;
706
0
}
707
708
/**
709
 * @brief Check whether a schema node can have some default values (true for NP containers as well).
710
 *
711
 * @param[in] schema Schema node to check.
712
 * @return non-zero if yes,
713
 * @return 0 otherwise.
714
 */
715
static int
716
lyd_val_has_default(const struct lysc_node *schema)
717
66.5k
{
718
66.5k
    switch (schema->nodetype) {
719
19.6k
    case LYS_LEAF:
720
19.6k
        if (((struct lysc_node_leaf *)schema)->dflt) {
721
0
            return 1;
722
0
        }
723
19.6k
        break;
724
45.8k
    case LYS_LEAFLIST:
725
45.8k
        if (((struct lysc_node_leaflist *)schema)->dflts) {
726
0
            return 1;
727
0
        }
728
45.8k
        break;
729
45.8k
    case LYS_CONTAINER:
730
45
        if (!(schema->flags & LYS_PRESENCE)) {
731
45
            return 1;
732
45
        }
733
0
        break;
734
954
    default:
735
954
        break;
736
66.5k
    }
737
738
66.4k
    return 0;
739
66.5k
}
740
741
/**
742
 * @brief Auto-delete leaf-list default instances to prevent validation errors.
743
 *
744
 * @param[in,out] first First sibling to search in, is updated if needed.
745
 * @param[in,out] node New data node instance to check, is updated if auto-deleted.
746
 * @param[in] mod Module of the siblings, NULL for nested siblings.
747
 * @param[in,out] diff Validation diff.
748
 * @return 1 if @p node auto-deleted and updated to its next sibling.
749
 * @return 0 if @p node was not auto-deleted.
750
 */
751
static ly_bool
752
lyd_validate_autodel_leaflist_dflt(struct lyd_node **first, struct lyd_node **node, const struct lys_module *mod,
753
        struct lyd_node **diff)
754
0
{
755
0
    const struct lysc_node *schema;
756
0
    struct lyd_node *iter, *next;
757
0
    ly_bool found = 0, node_autodel = 0;
758
759
0
    assert((*node)->flags & LYD_NEW);
760
761
0
    schema = (*node)->schema;
762
0
    assert(schema->nodetype == LYS_LEAFLIST);
763
764
    /* check whether there is any explicit instance */
765
0
    LYD_LIST_FOR_INST(*first, schema, iter) {
766
0
        if (!(iter->flags & LYD_DEFAULT)) {
767
0
            found = 1;
768
0
            break;
769
0
        }
770
0
    }
771
0
    if (!found) {
772
        /* no explicit instance, keep defaults as they are */
773
0
        return 0;
774
0
    }
775
776
0
    LYD_LIST_FOR_INST_SAFE(*first, schema, next, iter) {
777
0
        if (iter->flags & LYD_DEFAULT) {
778
            /* default instance found, remove it */
779
0
            if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, NULL, diff)) {
780
0
                node_autodel = 1;
781
0
            }
782
0
        }
783
0
    }
784
785
0
    return node_autodel;
786
0
}
787
788
/**
789
 * @brief Auto-delete container or leaf default instances to prevent validation errors.
790
 *
791
 * @param[in,out] first First sibling to search in, is updated if needed.
792
 * @param[in,out] node New data node instance to check, is updated if auto-deleted.
793
 * @param[in] mod Module of the siblings, NULL for nested siblings.
794
 * @param[in,out] diff Validation diff.
795
 * @return 1 if @p node auto-deleted and updated to its next sibling.
796
 * @return 0 if @p node was not auto-deleted.
797
 */
798
static ly_bool
799
lyd_validate_autodel_cont_leaf_dflt(struct lyd_node **first, struct lyd_node **node, const struct lys_module *mod,
800
        struct lyd_node **diff)
801
45
{
802
45
    const struct lysc_node *schema;
803
45
    struct lyd_node *iter, *next;
804
45
    ly_bool found = 0, node_autodel = 0;
805
806
45
    assert((*node)->flags & LYD_NEW);
807
808
45
    schema = (*node)->schema;
809
45
    assert(schema->nodetype & (LYS_LEAF | LYS_CONTAINER));
810
811
    /* check whether there is any explicit instance */
812
3.73k
    LYD_LIST_FOR_INST(*first, schema, iter) {
813
3.73k
        if (!(iter->flags & LYD_DEFAULT)) {
814
0
            found = 1;
815
0
            break;
816
0
        }
817
3.73k
    }
818
819
45
    if (found) {
820
        /* remove all default instances */
821
0
        LYD_LIST_FOR_INST_SAFE(*first, schema, next, iter) {
822
0
            if (iter->flags & LYD_DEFAULT) {
823
                /* default instance, remove it */
824
0
                if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, NULL, diff)) {
825
0
                    node_autodel = 1;
826
0
                }
827
0
            }
828
0
        }
829
45
    } else {
830
        /* remove a single old default instance, if any */
831
3.73k
        LYD_LIST_FOR_INST(*first, schema, iter) {
832
3.73k
            if ((iter->flags & LYD_DEFAULT) && !(iter->flags & LYD_NEW)) {
833
                /* old default instance, remove it */
834
0
                if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, NULL, diff)) {
835
0
                    node_autodel = 1;
836
0
                }
837
0
                break;
838
0
            }
839
3.73k
        }
840
45
    }
841
842
45
    return node_autodel;
843
45
}
844
845
/**
846
 * @brief Auto-delete leftover default nodes of deleted cases (that have no existing explicit data).
847
 *
848
 * @param[in,out] first First sibling to search in, is updated if needed.
849
 * @param[in,out] node Default data node instance to check.
850
 * @param[in] mod Module of the siblings, NULL for nested siblings.
851
 * @param[in,out] diff Validation diff.
852
 * @return 1 if @p node auto-deleted and updated to its next sibling.
853
 * @return 0 if @p node was not auto-deleted.
854
 */
855
static ly_bool
856
lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node **node, const struct lys_module *mod,
857
        struct lyd_node **diff)
858
18
{
859
18
    const struct lysc_node *schema;
860
18
    struct lysc_node_choice *choic;
861
18
    struct lyd_node *iter = NULL;
862
18
    const struct lysc_node *slast = NULL;
863
18
    ly_bool node_autodel = 0;
864
865
18
    assert((*node)->flags & LYD_DEFAULT);
866
867
18
    schema = (*node)->schema;
868
869
18
    if (!schema->parent || (schema->parent->nodetype != LYS_CASE)) {
870
        /* the default node is not a descendant of a case */
871
18
        return 0;
872
18
    }
873
874
0
    choic = (struct lysc_node_choice *)schema->parent->parent;
875
0
    assert(choic->nodetype == LYS_CHOICE);
876
877
0
    if (choic->dflt && (choic->dflt == (struct lysc_node_case *)schema->parent)) {
878
        /* data of a default case, keep them */
879
0
        return 0;
880
0
    }
881
882
    /* try to find an explicit node of the case */
883
0
    while ((iter = lys_getnext_data(iter, *first, &slast, schema->parent, NULL))) {
884
0
        if (!(iter->flags & LYD_DEFAULT)) {
885
0
            break;
886
0
        }
887
0
    }
888
889
0
    if (!iter) {
890
        /* there are only default nodes of the case meaning it does not exist and neither should any default nodes
891
         * of the case, remove this one default node */
892
0
        if (lyd_validate_autodel_node_del(first, *node, mod, 0, node, NULL, NULL, diff)) {
893
0
            node_autodel = 1;
894
0
        }
895
0
    }
896
897
0
    return node_autodel;
898
0
}
899
900
/**
901
 * @brief Validate new siblings in choices, recursively for nested choices.
902
 *
903
 * @param[in,out] first First sibling.
904
 * @param[in] sparent Schema parent of the siblings, NULL for top-level siblings.
905
 * @param[in] mod Module of the siblings, NULL for nested siblings.
906
 * @param[in] ext Extension instance to use, if relevant.
907
 * @param[in] val_opts Validation options.
908
 * @param[in] int_opts Internal parser options.
909
 * @param[in,out] getnext_ht Getnext HT to use, new @p sparent is added to it.
910
 * @param[in,out] diff Validation diff.
911
 * @return LY_ERR value.
912
 */
913
static LY_ERR
914
lyd_validate_choice_r(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
915
        const struct lysc_ext_instance *ext, uint32_t val_opts, uint32_t int_opts, struct ly_ht *getnext_ht,
916
        struct lyd_node **diff)
917
18.6k
{
918
18.6k
    LY_ERR r, rc = LY_SUCCESS;
919
18.6k
    const struct lysc_node **choices, **snodes;
920
18.6k
    uint32_t i;
921
922
    /* get cached getnext schema nodes */
923
18.6k
    rc = lyd_val_getnext_get(sparent, mod, ext, int_opts & LYD_INTOPT_REPLY, getnext_ht, &choices, &snodes);
924
18.6k
    LY_CHECK_GOTO(rc, cleanup);
925
18.6k
    if (!choices) {
926
18.6k
        goto cleanup;
927
18.6k
    }
928
929
0
    for (i = 0; *first && choices[i]; ++i) {
930
        /* check case duplicites */
931
0
        r = lyd_validate_cases(first, mod, (struct lysc_node_choice *)choices[i], diff);
932
0
        LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
933
934
        /* check for nested choice */
935
0
        r = lyd_validate_choice_r(first, choices[i], mod, ext, val_opts, int_opts, getnext_ht, diff);
936
0
        LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
937
0
    }
938
939
18.6k
cleanup:
940
18.6k
    return rc;
941
0
}
942
943
LY_ERR
944
lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
945
        const struct lysc_ext_instance *ext, uint32_t val_opts, uint32_t int_opts, struct ly_ht *getnext_ht,
946
        struct lyd_node **diff)
947
18.6k
{
948
18.6k
    LY_ERR r, rc = LY_SUCCESS;
949
18.6k
    struct lyd_node *node;
950
18.6k
    const struct lysc_node *last_dflt_schema = NULL;
951
952
18.6k
    assert(first && (sparent || mod));
953
954
    /* validate choices */
955
18.6k
    r = lyd_validate_choice_r(first, sparent, mod, ext, val_opts, int_opts, getnext_ht, diff);
956
18.6k
    LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
957
958
18.6k
    node = *first;
959
84.9k
    while (node) {
960
66.5k
        if (!node->schema || (mod && (lyd_owner_module(node) != mod))) {
961
            /* opaque node or all top-level data from this module checked */
962
0
            break;
963
0
        }
964
965
66.5k
        if (!(node->flags & (LYD_NEW | LYD_DEFAULT))) {
966
            /* check only new and default nodes */
967
0
            node = node->next;
968
0
            continue;
969
0
        }
970
971
66.5k
        if (lyd_val_has_default(node->schema) && (node->schema != last_dflt_schema) && (node->flags & LYD_NEW)) {
972
            /* remove old default(s) of the new node if an explicit instance exists */
973
45
            last_dflt_schema = node->schema;
974
45
            if (node->schema->nodetype == LYS_LEAFLIST) {
975
0
                if (lyd_validate_autodel_leaflist_dflt(first, &node, mod, diff)) {
976
0
                    continue;
977
0
                }
978
45
            } else {
979
45
                if (lyd_validate_autodel_cont_leaf_dflt(first, &node, mod, diff)) {
980
0
                    continue;
981
0
                }
982
45
            }
983
45
        }
984
985
66.5k
        if (node->flags & LYD_NEW) {
986
            /* then check new node instance duplicities */
987
66.5k
            r = lyd_validate_duplicates(*first, node, val_opts);
988
66.5k
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
989
990
            /* this node is valid */
991
66.3k
            node->flags &= ~LYD_NEW;
992
66.3k
        }
993
994
66.3k
        if (node->flags & LYD_DEFAULT) {
995
            /* remove leftover default nodes from a no-longer existing case */
996
18
            if (lyd_validate_autodel_case_dflt(first, &node, mod, diff)) {
997
0
                continue;
998
0
            }
999
18
        }
1000
1001
        /* next iter */
1002
66.3k
        node = node->next;
1003
66.3k
    }
1004
1005
18.6k
cleanup:
1006
18.6k
    return rc;
1007
18.6k
}
1008
1009
/**
1010
 * @brief Evaluate any "when" conditions of a non-existent data node with existing parent.
1011
 *
1012
 * @param[in] first First data sibling of the non-existing node.
1013
 * @param[in] parent Data parent of the non-existing node.
1014
 * @param[in] snode Schema node of the non-existing node.
1015
 * @param[out] disabled First when that evaluated false, if any.
1016
 * @return LY_ERR value.
1017
 */
1018
static LY_ERR
1019
lyd_validate_dummy_when(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
1020
        const struct lysc_when **disabled)
1021
0
{
1022
0
    LY_ERR rc = LY_SUCCESS;
1023
0
    struct lyd_node *tree, *dummy = NULL;
1024
0
    uint32_t xp_opts;
1025
1026
    /* find root */
1027
0
    if (parent) {
1028
0
        tree = (struct lyd_node *)parent;
1029
0
        while (tree->parent) {
1030
0
            tree = lyd_parent(tree);
1031
0
        }
1032
0
        tree = lyd_first_sibling(tree);
1033
0
    } else {
1034
        /* is the first sibling from the same module, but may not be the actual first */
1035
0
        tree = lyd_first_sibling(first);
1036
0
    }
1037
1038
    /* create dummy opaque node */
1039
0
    rc = lyd_new_opaq((struct lyd_node *)parent, snode->module->ctx, snode->name, NULL, NULL, snode->module->name, &dummy);
1040
0
    LY_CHECK_GOTO(rc, cleanup);
1041
1042
    /* connect it if needed */
1043
0
    if (!parent) {
1044
0
        if (first) {
1045
0
            lyd_insert_sibling((struct lyd_node *)first, dummy, &tree);
1046
0
        } else {
1047
0
            assert(!tree);
1048
0
            tree = dummy;
1049
0
        }
1050
0
    }
1051
1052
    /* explicitly specified accesible tree */
1053
0
    if (snode->flags & LYS_CONFIG_W) {
1054
0
        xp_opts = LYXP_ACCESS_TREE_CONFIG;
1055
0
    } else {
1056
0
        xp_opts = LYXP_ACCESS_TREE_ALL;
1057
0
    }
1058
1059
    /* evaluate all when */
1060
0
    rc = lyd_validate_node_when(tree, dummy, snode, xp_opts, disabled);
1061
0
    if (rc == LY_EINCOMPLETE) {
1062
        /* all other when must be resolved by now */
1063
0
        LOGINT(snode->module->ctx);
1064
0
        rc = LY_EINT;
1065
0
        goto cleanup;
1066
0
    } else if (rc) {
1067
        /* error */
1068
0
        goto cleanup;
1069
0
    }
1070
1071
0
cleanup:
1072
0
    lyd_free_tree(dummy);
1073
0
    return rc;
1074
0
}
1075
1076
/**
1077
 * @brief Validate mandatory node existence.
1078
 *
1079
 * @param[in] first First sibling to search in.
1080
 * @param[in] parent Data parent.
1081
 * @param[in] snode Schema node to validate.
1082
 * @param[in] val_opts Validation options.
1083
 * @return LY_ERR value.
1084
 */
1085
static LY_ERR
1086
lyd_validate_mandatory(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
1087
        uint32_t val_opts)
1088
0
{
1089
0
    const struct lysc_when *disabled;
1090
1091
0
    if (snode->nodetype == LYS_CHOICE) {
1092
        /* some data of a choice case exist */
1093
0
        if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
1094
0
            return LY_SUCCESS;
1095
0
        }
1096
0
    } else {
1097
0
        assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
1098
1099
0
        if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
1100
            /* data instance found */
1101
0
            return LY_SUCCESS;
1102
0
        }
1103
0
    }
1104
1105
0
    disabled = NULL;
1106
0
    if (lysc_has_when(snode)) {
1107
        /* if there are any when conditions, they must be true for a validation error */
1108
0
        LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
1109
0
    }
1110
1111
0
    if (!disabled) {
1112
0
        if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1113
            /* only a warning */
1114
0
            LOG_LOCSET(parent ? NULL : snode, parent);
1115
0
            if (snode->nodetype == LYS_CHOICE) {
1116
0
                LOGWRN(snode->module->ctx, "Mandatory choice \"%s\" data do not exist.", snode->name);
1117
0
            } else {
1118
0
                LOGWRN(snode->module->ctx, "Mandatory node \"%s\" instance does not exist.", snode->name);
1119
0
            }
1120
0
            LOG_LOCBACK(parent ? 0 : 1, parent ? 1 : 0);
1121
0
        } else {
1122
            /* node instance not found */
1123
0
            LOG_LOCSET(parent ? NULL : snode, parent);
1124
0
            if (snode->nodetype == LYS_CHOICE) {
1125
0
                LOGVAL_APPTAG(snode->module->ctx, "missing-choice", LY_VCODE_NOMAND_CHOIC, snode->name);
1126
0
            } else {
1127
0
                LOGVAL(snode->module->ctx, LY_VCODE_NOMAND, snode->name);
1128
0
            }
1129
0
            LOG_LOCBACK(parent ? 0 : 1, parent ? 1 : 0);
1130
0
            return LY_EVALID;
1131
0
        }
1132
0
    }
1133
1134
0
    return LY_SUCCESS;
1135
0
}
1136
1137
/**
1138
 * @brief Validate min/max-elements constraints, if any.
1139
 *
1140
 * @param[in] first First sibling to search in.
1141
 * @param[in] parent Data parent.
1142
 * @param[in] snode Schema node to validate.
1143
 * @param[in] min Minimum number of elements, 0 for no restriction.
1144
 * @param[in] max Max number of elements, 0 for no restriction.
1145
 * @param[in] val_opts Validation options.
1146
 * @return LY_ERR value.
1147
 */
1148
static LY_ERR
1149
lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
1150
        uint32_t min, uint32_t max, uint32_t val_opts)
1151
320
{
1152
320
    LY_ERR rc = LY_SUCCESS;
1153
320
    uint32_t count = 0;
1154
320
    struct lyd_node *iter, *last_iter = NULL;
1155
320
    const struct lysc_when *disabled;
1156
320
    char *log_path;
1157
320
    int r;
1158
1159
320
    assert(min || max);
1160
1161
320
    LYD_LIST_FOR_INST(first, snode, iter) {
1162
0
        last_iter = iter;
1163
0
        ++count;
1164
1165
0
        if (min && (count == min)) {
1166
            /* satisfied */
1167
0
            min = 0;
1168
0
            if (!max) {
1169
                /* nothing more to check */
1170
0
                break;
1171
0
            }
1172
0
        }
1173
0
        if (max && (count > max)) {
1174
            /* not satisifed */
1175
0
            break;
1176
0
        }
1177
0
    }
1178
1179
320
    if (min) {
1180
0
        assert(count < min);
1181
1182
0
        disabled = NULL;
1183
0
        if (lysc_has_when(snode)) {
1184
            /* if there are any when conditions, they must be true for a validation error */
1185
0
            LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
1186
0
        }
1187
1188
0
        if (disabled) {
1189
            /* satisfied */
1190
0
            min = 0;
1191
0
        }
1192
0
    }
1193
320
    if (max && (count <= max)) {
1194
        /* satisfied */
1195
320
        max = 0;
1196
320
    }
1197
1198
320
    if (min || max) {
1199
        /* set log path */
1200
0
        if (last_iter) {
1201
            /* standard data path */
1202
0
            LOG_LOCSET(NULL, last_iter);
1203
0
        } else {
1204
            /* data path with last schema node name or only the schema node if !parent */
1205
0
            if (lyd_node_module(parent) != snode->module) {
1206
0
                r = asprintf(&log_path, "/%s:%s", snode->module->name, snode->name);
1207
0
            } else {
1208
0
                r = asprintf(&log_path, "/%s", snode->name);
1209
0
            }
1210
0
            if (r == -1) {
1211
0
                LOGMEM_RET(snode->module->ctx);
1212
0
            }
1213
0
            ly_log_location(NULL, parent, log_path, NULL);
1214
0
            free(log_path);
1215
0
        }
1216
1217
0
        if (min) {
1218
0
            if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1219
                /* only a warning */
1220
0
                LOGWRN(snode->module->ctx, "Too few \"%s\" instances.", snode->name);
1221
0
            } else {
1222
0
                LOGVAL_APPTAG(snode->module->ctx, "too-few-elements", LY_VCODE_NOMIN, snode->name);
1223
0
                rc = LY_EVALID;
1224
0
            }
1225
0
        } else if (max) {
1226
0
            if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1227
                /* only a warning */
1228
0
                LOGWRN(snode->module->ctx, "Too many \"%s\" instances.", snode->name);
1229
0
            } else {
1230
0
                LOGVAL_APPTAG(snode->module->ctx, "too-many-elements", LY_VCODE_NOMAX, snode->name);
1231
0
                rc = LY_EVALID;
1232
0
            }
1233
0
        }
1234
1235
        /* revert log path */
1236
0
        if (last_iter) {
1237
0
            LOG_LOCBACK(0, 1);
1238
0
        } else {
1239
0
            ly_log_location_revert(0, parent ? 1 : 0, 1, 0);
1240
0
        }
1241
0
    }
1242
1243
320
    return rc;
1244
320
}
1245
1246
/**
1247
 * @brief Find node referenced by a list unique statement.
1248
 *
1249
 * @param[in] uniq_leaf Unique leaf to find.
1250
 * @param[in] list List instance to use for the search.
1251
 * @return Found leaf,
1252
 * @return NULL if no leaf found.
1253
 */
1254
static struct lyd_node *
1255
lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
1256
0
{
1257
0
    struct lyd_node *node;
1258
0
    const struct lysc_node *iter;
1259
0
    size_t depth = 0, i;
1260
1261
    /* get leaf depth */
1262
0
    for (iter = &uniq_leaf->node; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
1263
0
        ++depth;
1264
0
    }
1265
1266
0
    node = (struct lyd_node *)list;
1267
0
    while (node && depth) {
1268
        /* find schema node with this depth */
1269
0
        for (i = depth - 1, iter = &uniq_leaf->node; i; iter = lysc_data_parent(iter)) {
1270
0
            --i;
1271
0
        }
1272
1273
        /* find iter instance in children */
1274
0
        assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
1275
0
        lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
1276
0
        --depth;
1277
0
    }
1278
1279
0
    return node;
1280
0
}
1281
1282
/**
1283
 * @brief Unique list validation callback argument.
1284
 */
1285
struct lyd_val_uniq_arg {
1286
    LY_ARRAY_COUNT_TYPE action; /**< Action to perform - 0 to compare all uniques, n to compare only n-th unique. */
1287
    uint32_t val_opts;          /**< Validation options. */
1288
};
1289
1290
/**
1291
 * @brief Callback for comparing 2 list unique leaf values.
1292
 *
1293
 * Implementation of ::lyht_value_equal_cb.
1294
 */
1295
static ly_bool
1296
lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
1297
0
{
1298
0
    struct ly_ctx *ctx;
1299
0
    struct lysc_node_list *slist;
1300
0
    struct lyd_node *diter, *first, *second;
1301
0
    struct lyd_value *val1, *val2;
1302
0
    char *path1, *path2, *uniq_str, *ptr;
1303
0
    LY_ARRAY_COUNT_TYPE u, v;
1304
0
    struct lyd_val_uniq_arg *arg = cb_data;
1305
0
    const uint32_t uniq_err_msg_size = 1024;
1306
1307
0
    assert(val1_p && val2_p);
1308
1309
0
    first = *((struct lyd_node **)val1_p);
1310
0
    second = *((struct lyd_node **)val2_p);
1311
1312
0
    assert(first && (first->schema->nodetype == LYS_LIST));
1313
0
    assert(second && (second->schema == first->schema));
1314
1315
0
    ctx = first->schema->module->ctx;
1316
1317
0
    slist = (struct lysc_node_list *)first->schema;
1318
1319
    /* compare unique leaves */
1320
0
    if (arg->action > 0) {
1321
0
        u = arg->action - 1;
1322
0
        if (u < LY_ARRAY_COUNT(slist->uniques)) {
1323
0
            goto uniquecheck;
1324
0
        }
1325
0
    }
1326
0
    LY_ARRAY_FOR(slist->uniques, u) {
1327
0
uniquecheck:
1328
0
        LY_ARRAY_FOR(slist->uniques[u], v) {
1329
            /* first */
1330
0
            diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
1331
0
            if (diter) {
1332
0
                val1 = &((struct lyd_node_term *)diter)->value;
1333
0
            } else {
1334
                /* use default value */
1335
0
                val1 = slist->uniques[u][v]->dflt;
1336
0
            }
1337
1338
            /* second */
1339
0
            diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
1340
0
            if (diter) {
1341
0
                val2 = &((struct lyd_node_term *)diter)->value;
1342
0
            } else {
1343
                /* use default value */
1344
0
                val2 = slist->uniques[u][v]->dflt;
1345
0
            }
1346
1347
0
            if (!val1 || !val2 || val1->realtype->plugin->compare(ctx, val1, val2)) {
1348
                /* values differ or either one is not set */
1349
0
                break;
1350
0
            }
1351
0
        }
1352
0
        if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
1353
            /* all unique leaves are the same in this set, create this nice error */
1354
0
            path1 = lyd_path(first, LYD_PATH_STD, NULL, 0);
1355
0
            path2 = lyd_path(second, LYD_PATH_STD, NULL, 0);
1356
1357
            /* use buffer to rebuild the unique string */
1358
0
            uniq_str = malloc(uniq_err_msg_size);
1359
0
            uniq_str[0] = '\0';
1360
0
            ptr = uniq_str;
1361
0
            LY_ARRAY_FOR(slist->uniques[u], v) {
1362
0
                if (v) {
1363
0
                    strcpy(ptr, " ");
1364
0
                    ++ptr;
1365
0
                }
1366
0
                ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], &slist->node, LYSC_PATH_LOG,
1367
0
                        ptr, uniq_err_msg_size - (ptr - uniq_str));
1368
0
                if (!ptr) {
1369
                    /* path will be incomplete, whatever */
1370
0
                    break;
1371
0
                }
1372
1373
0
                ptr += strlen(ptr);
1374
0
            }
1375
0
            LOG_LOCSET(NULL, second);
1376
0
            if (arg->val_opts & LYD_VALIDATE_OPERATIONAL) {
1377
                /* only a warning */
1378
0
                LOGWRN(ctx, "Unique data leaf(s) \"%s\" not satisfied in \"%s\" and \"%s\".", uniq_str, path1, path2);
1379
0
            } else {
1380
0
                LOGVAL_APPTAG(ctx, "data-not-unique", LY_VCODE_NOUNIQ, uniq_str, path1, path2);
1381
0
            }
1382
0
            LOG_LOCBACK(0, 1);
1383
1384
0
            free(path1);
1385
0
            free(path2);
1386
0
            free(uniq_str);
1387
1388
0
            if (!(arg->val_opts & LYD_VALIDATE_OPERATIONAL)) {
1389
0
                return 1;
1390
0
            }
1391
0
        }
1392
1393
0
        if (arg->action > 0) {
1394
            /* done */
1395
0
            return 0;
1396
0
        }
1397
0
    }
1398
1399
0
    return 0;
1400
0
}
1401
1402
/**
1403
 * @brief Validate list unique leaves.
1404
 *
1405
 * @param[in] first First sibling to search in.
1406
 * @param[in] snode Schema node to validate.
1407
 * @param[in] uniques List unique arrays to validate.
1408
 * @param[in] val_opts Validation options.
1409
 * @return LY_ERR value.
1410
 */
1411
static LY_ERR
1412
lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, const struct lysc_node_leaf ***uniques,
1413
        uint32_t val_opts)
1414
0
{
1415
0
    const struct lyd_node *diter;
1416
0
    struct ly_set *set;
1417
0
    LY_ARRAY_COUNT_TYPE u, v, x = 0;
1418
0
    LY_ERR ret = LY_SUCCESS;
1419
0
    uint32_t hash, i;
1420
0
    size_t key_len;
1421
0
    ly_bool dyn;
1422
0
    const void *hash_key;
1423
0
    struct lyd_val_uniq_arg arg, *args = NULL;
1424
0
    struct ly_ht **uniqtables = NULL;
1425
0
    struct lyd_value *val;
1426
0
    struct ly_ctx *ctx = snode->module->ctx;
1427
1428
0
    assert(uniques);
1429
1430
    /* get all list instances */
1431
0
    LY_CHECK_RET(ly_set_new(&set));
1432
0
    LY_LIST_FOR(first, diter) {
1433
0
        if (diter->schema == snode) {
1434
0
            ret = ly_set_add(set, (void *)diter, 1, NULL);
1435
0
            LY_CHECK_GOTO(ret, cleanup);
1436
0
        }
1437
0
    }
1438
1439
0
    if (set->count == 2) {
1440
        /* simple comparison */
1441
0
        arg.action = 0;
1442
0
        arg.val_opts = val_opts;
1443
0
        if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, &arg)) {
1444
            /* instance duplication */
1445
0
            ret = LY_EVALID;
1446
0
            goto cleanup;
1447
0
        }
1448
0
    } else if (set->count > 2) {
1449
        /* use hashes for comparison */
1450
0
        uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
1451
0
        args = malloc(LY_ARRAY_COUNT(uniques) * sizeof *args);
1452
0
        LY_CHECK_ERR_GOTO(!uniqtables || !args, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1453
0
        x = LY_ARRAY_COUNT(uniques);
1454
0
        for (v = 0; v < x; v++) {
1455
0
            args[v].action = v + 1;
1456
0
            args[v].val_opts = val_opts;
1457
0
            uniqtables[v] = lyht_new(lyht_get_fixed_size(set->count), sizeof(struct lyd_node *),
1458
0
                    lyd_val_uniq_list_equal, &args[v], 0);
1459
0
            LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
1460
0
        }
1461
1462
0
        for (i = 0; i < set->count; i++) {
1463
            /* loop for unique - get the hash for the instances */
1464
0
            for (u = 0; u < x; u++) {
1465
0
                val = NULL;
1466
0
                for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
1467
0
                    diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
1468
0
                    if (diter) {
1469
0
                        val = &((struct lyd_node_term *)diter)->value;
1470
0
                    } else {
1471
                        /* use default value */
1472
0
                        val = uniques[u][v]->dflt;
1473
0
                    }
1474
0
                    if (!val) {
1475
                        /* unique item not present nor has default value */
1476
0
                        break;
1477
0
                    }
1478
1479
                    /* get hash key */
1480
0
                    hash_key = val->realtype->plugin->print(NULL, val, LY_VALUE_LYB, NULL, &dyn, &key_len);
1481
0
                    hash = lyht_hash_multi(hash, hash_key, key_len);
1482
0
                    if (dyn) {
1483
0
                        free((void *)hash_key);
1484
0
                    }
1485
0
                }
1486
0
                if (!val) {
1487
                    /* skip this list instance since its unique set is incomplete */
1488
0
                    continue;
1489
0
                }
1490
1491
                /* finish the hash value */
1492
0
                hash = lyht_hash_multi(hash, NULL, 0);
1493
1494
                /* insert into the hashtable */
1495
0
                ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
1496
0
                if (ret == LY_EEXIST) {
1497
                    /* instance duplication */
1498
0
                    ret = LY_EVALID;
1499
0
                }
1500
0
                LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
1501
0
            }
1502
0
        }
1503
0
    }
1504
1505
0
cleanup:
1506
0
    ly_set_free(set, NULL);
1507
0
    for (v = 0; v < x; v++) {
1508
0
        if (!uniqtables[v]) {
1509
            /* failed when allocating uniquetables[j], following j are not allocated */
1510
0
            break;
1511
0
        }
1512
0
        lyht_free(uniqtables[v], NULL);
1513
0
    }
1514
0
    free(uniqtables);
1515
0
    free(args);
1516
1517
0
    return ret;
1518
0
}
1519
1520
/**
1521
 * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes.
1522
 *
1523
 * @param[in] first First sibling to search in.
1524
 * @param[in] parent Data parent.
1525
 * @param[in] sparent Schema parent of the nodes to check.
1526
 * @param[in] mod Module of the nodes to check.
1527
 * @param[in] ext Extension instance to use, if relevant.
1528
 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
1529
 * @param[in] int_opts Internal parser options.
1530
 * @param[in,out] getnext_ht Getnext HT to use, new @p sparent is added to it.
1531
 * @return LY_ERR value.
1532
 */
1533
static LY_ERR
1534
lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lyd_node *parent,
1535
        const struct lysc_node *sparent, const struct lys_module *mod, const struct lysc_ext_instance *ext,
1536
        uint32_t val_opts, uint32_t int_opts, struct ly_ht *getnext_ht)
1537
2.73k
{
1538
2.73k
    LY_ERR r, rc = LY_SUCCESS;
1539
2.73k
    const struct lysc_node *snode, *scase, **choices, **snodes;
1540
2.73k
    struct lysc_node_list *slist;
1541
2.73k
    struct lysc_node_leaflist *sllist;
1542
2.73k
    uint32_t i;
1543
1544
    /* get cached getnext schema nodes */
1545
2.73k
    rc = lyd_val_getnext_get(sparent, mod, ext, int_opts & LYD_INTOPT_REPLY, getnext_ht, &choices, &snodes);
1546
2.73k
    LY_CHECK_GOTO(rc, cleanup);
1547
1548
2.73k
    for (i = 0; choices && choices[i]; ++i) {
1549
0
        snode = choices[i];
1550
1551
0
        if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
1552
            /* skip state nodes */
1553
0
            continue;
1554
0
        }
1555
1556
0
        if (snode->flags & LYS_MAND_TRUE) {
1557
            /* check generic mandatory existence */
1558
0
            r = lyd_validate_mandatory(first, parent, snode, val_opts);
1559
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1560
0
        }
1561
1562
        /* find the existing case, if any */
1563
0
        LY_LIST_FOR(lysc_node_child(snode), scase) {
1564
0
            if (lys_getnext_data(NULL, first, NULL, scase, NULL)) {
1565
                /* validate only this case */
1566
0
                r = lyd_validate_siblings_schema_r(first, parent, scase, mod, ext, val_opts, int_opts, getnext_ht);
1567
0
                LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1568
0
                break;
1569
0
            }
1570
0
        }
1571
0
    }
1572
1573
10.0k
    for (i = 0; snodes && snodes[i]; ++i) {
1574
7.35k
        snode = snodes[i];
1575
1576
7.35k
        if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
1577
            /* skip state nodes */
1578
0
            continue;
1579
0
        }
1580
1581
        /* check min-elements and max-elements */
1582
7.35k
        if (snode->nodetype == LYS_LIST) {
1583
800
            slist = (struct lysc_node_list *)snode;
1584
800
            if (slist->min || (slist->max < UINT32_MAX)) {
1585
160
                r = lyd_validate_minmax(first, parent, snode, slist->min, slist->max, val_opts);
1586
160
                LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1587
160
            }
1588
1589
            /* check unique */
1590
800
            if (slist->uniques) {
1591
0
                r = lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques, val_opts);
1592
0
                LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1593
0
            }
1594
6.55k
        } else if (snode->nodetype == LYS_LEAFLIST) {
1595
955
            sllist = (struct lysc_node_leaflist *)snode;
1596
955
            if (sllist->min || (sllist->max < UINT32_MAX)) {
1597
160
                r = lyd_validate_minmax(first, parent, snode, sllist->min, sllist->max, val_opts);
1598
160
                LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1599
160
            }
1600
1601
5.59k
        } else if (snode->flags & LYS_MAND_TRUE) {
1602
            /* check generic mandatory existence */
1603
0
            r = lyd_validate_mandatory(first, parent, snode, val_opts);
1604
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1605
0
        }
1606
7.35k
    }
1607
1608
2.73k
cleanup:
1609
2.73k
    return rc;
1610
2.73k
}
1611
1612
/**
1613
 * @brief Validate obsolete nodes, only warnings are printed.
1614
 *
1615
 * @param[in] node Node to check.
1616
 */
1617
static void
1618
lyd_validate_obsolete(const struct lyd_node *node)
1619
2.57k
{
1620
2.57k
    const struct lysc_node *snode;
1621
1622
2.57k
    snode = node->schema;
1623
2.57k
    do {
1624
2.57k
        if (snode->flags & LYS_STATUS_OBSLT &&
1625
2.57k
                (!(snode->nodetype & LYD_NODE_INNER) || lyd_child(node))) {
1626
0
            LOG_LOCSET(NULL, node);
1627
0
            LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
1628
0
            LOG_LOCBACK(0, 1);
1629
0
            break;
1630
0
        }
1631
1632
2.57k
        snode = snode->parent;
1633
2.57k
    } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
1634
2.57k
}
1635
1636
/**
1637
 * @brief Validate must conditions of a data node.
1638
 *
1639
 * @param[in] node Node to validate.
1640
 * @param[in] val_opts Validation options.
1641
 * @param[in] int_opts Internal parser options.
1642
 * @param[in] xpath_options Additional XPath options to use.
1643
 * @return LY_ERR value.
1644
 */
1645
static LY_ERR
1646
lyd_validate_must(const struct lyd_node *node, uint32_t val_opts, uint32_t int_opts, uint32_t xpath_options)
1647
2.57k
{
1648
2.57k
    LY_ERR r, rc = LY_SUCCESS;
1649
2.57k
    struct lyxp_set xp_set;
1650
2.57k
    struct lysc_must *musts;
1651
2.57k
    const struct lyd_node *tree;
1652
2.57k
    const struct lysc_node *schema;
1653
2.57k
    const char *emsg, *eapptag;
1654
2.57k
    LY_ARRAY_COUNT_TYPE u;
1655
1656
2.57k
    assert((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) != (LYD_INTOPT_RPC | LYD_INTOPT_REPLY));
1657
2.57k
    assert((int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) != (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY));
1658
1659
2.57k
    if (node->schema->nodetype & (LYS_ACTION | LYS_RPC)) {
1660
0
        if (int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION)) {
1661
0
            schema = &((struct lysc_node_action *)node->schema)->input.node;
1662
0
        } else if (int_opts & LYD_INTOPT_REPLY) {
1663
0
            schema = &((struct lysc_node_action *)node->schema)->output.node;
1664
0
        } else {
1665
0
            LOGINT_RET(LYD_CTX(node));
1666
0
        }
1667
2.57k
    } else {
1668
2.57k
        schema = node->schema;
1669
2.57k
    }
1670
2.57k
    musts = lysc_node_musts(schema);
1671
2.57k
    if (!musts) {
1672
        /* no must to evaluate */
1673
2.57k
        return LY_SUCCESS;
1674
2.57k
    }
1675
1676
    /* find first top-level node */
1677
0
    for (tree = node; tree->parent; tree = lyd_parent(tree)) {}
1678
0
    tree = lyd_first_sibling(tree);
1679
1680
0
    LY_ARRAY_FOR(musts, u) {
1681
0
        memset(&xp_set, 0, sizeof xp_set);
1682
1683
        /* evaluate must */
1684
0
        r = lyxp_eval(LYD_CTX(node), musts[u].cond, node->schema->module, LY_VALUE_SCHEMA_RESOLVED,
1685
0
                musts[u].prefixes, node, node, tree, NULL, &xp_set, LYXP_SCHEMA | xpath_options);
1686
0
        if (r == LY_EINCOMPLETE) {
1687
0
            LOGERR(LYD_CTX(node), LY_EINCOMPLETE,
1688
0
                    "Must \"%s\" depends on a node with a when condition, which has not been evaluated.", musts[u].cond->expr);
1689
0
        }
1690
0
        LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1691
1692
        /* check the result */
1693
0
        lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
1694
0
        if (!xp_set.val.bln) {
1695
0
            if (val_opts & LYD_VALIDATE_OPERATIONAL) {
1696
                /* only a warning */
1697
0
                emsg = musts[u].emsg;
1698
0
                LOG_LOCSET(NULL, node);
1699
0
                if (emsg) {
1700
0
                    LOGWRN(LYD_CTX(node), "%s", emsg);
1701
0
                } else {
1702
0
                    LOGWRN(LYD_CTX(node), "Must condition \"%s\" not satisfied.", musts[u].cond->expr);
1703
0
                }
1704
0
                LOG_LOCBACK(0, 1);
1705
0
            } else {
1706
                /* use specific error information */
1707
0
                emsg = musts[u].emsg;
1708
0
                eapptag = musts[u].eapptag ? musts[u].eapptag : "must-violation";
1709
0
                LOG_LOCSET(NULL, node);
1710
0
                if (emsg) {
1711
0
                    LOGVAL_APPTAG(LYD_CTX(node), eapptag, LYVE_DATA, "%s", emsg);
1712
0
                } else {
1713
0
                    LOGVAL_APPTAG(LYD_CTX(node), eapptag, LY_VCODE_NOMUST, musts[u].cond->expr);
1714
0
                }
1715
0
                LOG_LOCBACK(0, 1);
1716
0
                r = LY_EVALID;
1717
0
                LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1718
0
            }
1719
0
        }
1720
0
    }
1721
1722
0
cleanup:
1723
0
    return rc;
1724
0
}
1725
1726
/**
1727
 * @brief Perform all remaining validation tasks, the data tree must be final when calling this function.
1728
 *
1729
 * @param[in] first First sibling.
1730
 * @param[in] parent Data parent.
1731
 * @param[in] sparent Schema parent of the siblings, NULL for top-level siblings.
1732
 * @param[in] mod Module of the siblings, NULL for nested siblings.
1733
 * @param[in] ext Extension instance to use, if relevant.
1734
 * @param[in] val_opts Validation options (@ref datavalidationoptions).
1735
 * @param[in] int_opts Internal parser options.
1736
 * @param[in] must_xp_opts Additional XPath options to use for evaluating "must".
1737
 * @param[in,out] getnext_ht Getnext HT to use.
1738
 * @return LY_ERR value.
1739
 */
1740
static LY_ERR
1741
lyd_validate_final_r(struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *sparent,
1742
        const struct lys_module *mod, const struct lysc_ext_instance *ext, uint32_t val_opts, uint32_t int_opts,
1743
        uint32_t must_xp_opts, struct ly_ht *getnext_ht)
1744
2.73k
{
1745
2.73k
    LY_ERR r, rc = LY_SUCCESS;
1746
2.73k
    const char *innode;
1747
2.73k
    struct lyd_node *node;
1748
1749
    /* validate all restrictions of nodes themselves */
1750
2.73k
    LY_LIST_FOR(first, node) {
1751
2.57k
        if (node->flags & LYD_EXT) {
1752
            /* ext instance data should have already been validated */
1753
0
            continue;
1754
0
        }
1755
1756
        /* opaque data */
1757
2.57k
        if (!node->schema) {
1758
0
            r = lyd_parse_opaq_error(node);
1759
0
            goto next_iter;
1760
0
        }
1761
1762
2.57k
        if (!node->parent && mod && (lyd_owner_module(node) != mod)) {
1763
            /* all top-level data from this module checked */
1764
0
            break;
1765
0
        }
1766
1767
        /* no state/input/output/op data */
1768
2.57k
        innode = NULL;
1769
2.57k
        if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
1770
0
            innode = "state";
1771
2.57k
        } else if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION)) && (node->schema->flags & LYS_IS_OUTPUT)) {
1772
0
            innode = "output";
1773
2.57k
        } else if ((int_opts & LYD_INTOPT_REPLY) && (node->schema->flags & LYS_IS_INPUT)) {
1774
0
            innode = "input";
1775
2.57k
        } else if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) && (node->schema->nodetype == LYS_RPC)) {
1776
0
            innode = "rpc";
1777
2.57k
        } else if (!(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) && (node->schema->nodetype == LYS_ACTION)) {
1778
0
            innode = "action";
1779
2.57k
        } else if (!(int_opts & LYD_INTOPT_NOTIF) && (node->schema->nodetype == LYS_NOTIF)) {
1780
0
            innode = "notification";
1781
0
        }
1782
2.57k
        if (innode) {
1783
0
            LOG_LOCSET(NULL, node);
1784
0
            LOGVAL(LYD_CTX(node), LY_VCODE_UNEXPNODE, innode, node->schema->name);
1785
0
            LOG_LOCBACK(0, 1);
1786
0
            r = LY_EVALID;
1787
0
            goto next_iter;
1788
0
        }
1789
1790
        /* obsolete data */
1791
2.57k
        lyd_validate_obsolete(node);
1792
1793
        /* node's musts */
1794
2.57k
        if ((r = lyd_validate_must(node, val_opts, int_opts, must_xp_opts))) {
1795
0
            goto next_iter;
1796
0
        }
1797
1798
        /* node value was checked by plugins */
1799
1800
2.57k
next_iter:
1801
2.57k
        LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1802
2.57k
    }
1803
1804
    /* validate schema-based restrictions */
1805
2.73k
    r = lyd_validate_siblings_schema_r(first, parent, sparent, mod, ext, val_opts, int_opts, getnext_ht);
1806
2.73k
    LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1807
1808
2.73k
    LY_LIST_FOR(first, node) {
1809
2.57k
        if ((node->flags & LYD_EXT) || !node->schema || (!node->parent && mod && (lyd_owner_module(node) != mod))) {
1810
            /* condensed condition of the previous loop */
1811
0
            break;
1812
0
        }
1813
1814
        /* validate all children recursively */
1815
2.57k
        r = lyd_validate_final_r(lyd_child(node), node, node->schema, NULL, NULL, val_opts, int_opts, must_xp_opts,
1816
2.57k
                getnext_ht);
1817
2.57k
        LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1818
1819
        /* set default for containers */
1820
2.57k
        lyd_np_cont_dflt_set(node);
1821
2.57k
    }
1822
1823
2.73k
cleanup:
1824
2.73k
    return rc;
1825
2.73k
}
1826
1827
/**
1828
 * @brief Validate extension instance data by storing it in its unres set.
1829
 *
1830
 * @param[in] sibling First sibling with ::LYD_EXT flag, all the following ones are expected to have it, too.
1831
 * @param[in,out] ext_val Set with parsed extension instance data to validate.
1832
 * @return LY_ERR value.
1833
 */
1834
static LY_ERR
1835
lyd_validate_nested_ext(struct lyd_node *sibling, struct ly_set *ext_val)
1836
0
{
1837
0
    struct lyd_node *node;
1838
0
    struct lyd_ctx_ext_val *ext_v;
1839
0
    struct lysc_ext_instance *nested_exts, *ext = NULL;
1840
0
    LY_ARRAY_COUNT_TYPE u;
1841
1842
    /* check of basic assumptions */
1843
0
    if (!sibling->parent || !sibling->parent->schema) {
1844
0
        LOGINT_RET(LYD_CTX(sibling));
1845
0
    }
1846
0
    LY_LIST_FOR(sibling, node) {
1847
0
        if (!(node->flags & LYD_EXT)) {
1848
0
            LOGINT_RET(LYD_CTX(sibling));
1849
0
        }
1850
0
    }
1851
1852
    /* try to find the extension instance */
1853
0
    nested_exts = sibling->parent->schema->exts;
1854
0
    LY_ARRAY_FOR(nested_exts, u) {
1855
0
        if (nested_exts[u].def->plugin->validate) {
1856
0
            if (ext) {
1857
                /* more extension instances with validate callback */
1858
0
                LOGINT_RET(LYD_CTX(sibling));
1859
0
            }
1860
0
            ext = &nested_exts[u];
1861
0
        }
1862
0
    }
1863
0
    if (!ext) {
1864
        /* no extension instance with validate callback */
1865
0
        LOGINT_RET(LYD_CTX(sibling));
1866
0
    }
1867
1868
    /* store for validation */
1869
0
    ext_v = malloc(sizeof *ext_v);
1870
0
    LY_CHECK_ERR_RET(!ext_v, LOGMEM(LYD_CTX(sibling)), LY_EMEM);
1871
0
    ext_v->ext = ext;
1872
0
    ext_v->sibling = sibling;
1873
0
    LY_CHECK_RET(ly_set_add(ext_val, ext_v, 1, NULL));
1874
1875
0
    return LY_SUCCESS;
1876
0
}
1877
1878
LY_ERR
1879
lyd_validate_node_ext(struct lyd_node *node, struct ly_set *ext_node)
1880
425k
{
1881
425k
    struct lyd_ctx_ext_node *ext_n;
1882
425k
    struct lysc_ext_instance *exts;
1883
425k
    LY_ARRAY_COUNT_TYPE u;
1884
1885
    /* try to find a relevant extension instance with node callback */
1886
425k
    exts = node->schema->exts;
1887
425k
    LY_ARRAY_FOR(exts, u) {
1888
0
        if (exts[u].def->plugin && exts[u].def->plugin->node) {
1889
            /* store for validation */
1890
0
            ext_n = malloc(sizeof *ext_n);
1891
0
            LY_CHECK_ERR_RET(!ext_n, LOGMEM(LYD_CTX(node)), LY_EMEM);
1892
0
            ext_n->ext = &exts[u];
1893
0
            ext_n->node = node;
1894
0
            LY_CHECK_RET(ly_set_add(ext_node, ext_n, 1, NULL));
1895
0
        }
1896
0
    }
1897
1898
425k
    return LY_SUCCESS;
1899
425k
}
1900
1901
/**
1902
 * @brief Validate the whole data subtree.
1903
 *
1904
 * @param[in] root Subtree root.
1905
 * @param[in,out] node_when Set for nodes with when conditions.
1906
 * @param[in,out] node_types Set for unres node types.
1907
 * @param[in,out] meta_types Set for unres metadata types.
1908
 * @param[in,out] ext_node Set with nodes with extensions to validate.
1909
 * @param[in,out] ext_val Set for parsed extension data to validate.
1910
 * @param[in] val_opts Validation options.
1911
 * @param[in] int_opts Internal parser options.
1912
 * @param[in,out] getnext_ht Getnext HT to use.
1913
 * @param[in,out] diff Validation diff.
1914
 * @return LY_ERR value.
1915
 */
1916
static LY_ERR
1917
lyd_validate_subtree(struct lyd_node *root, struct ly_set *node_when, struct ly_set *node_types,
1918
        struct ly_set *meta_types, struct ly_set *ext_node, struct ly_set *ext_val, uint32_t val_opts,
1919
        uint32_t int_opts, struct ly_ht *getnext_ht, struct lyd_node **diff)
1920
0
{
1921
0
    LY_ERR r, rc = LY_SUCCESS;
1922
0
    const struct lyd_meta *meta;
1923
0
    const struct lysc_type *type;
1924
0
    struct lyd_node *node;
1925
0
    uint32_t impl_opts;
1926
1927
0
    LYD_TREE_DFS_BEGIN(root, node) {
1928
0
        if (node->flags & LYD_EXT) {
1929
            /* validate using the extension instance callback */
1930
0
            return lyd_validate_nested_ext(node, ext_val);
1931
0
        }
1932
1933
0
        if (!node->schema) {
1934
            /* do not validate opaque nodes */
1935
0
            goto next_node;
1936
0
        }
1937
1938
0
        LY_LIST_FOR(node->meta, meta) {
1939
0
            lyplg_ext_get_storage(meta->annotation, LY_STMT_TYPE, sizeof type, (const void **)&type);
1940
0
            if (type->plugin->validate) {
1941
                /* metadata type resolution */
1942
0
                r = ly_set_add(meta_types, (void *)meta, 1, NULL);
1943
0
                LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1944
0
            }
1945
0
        }
1946
1947
0
        if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
1948
            /* node type resolution */
1949
0
            r = ly_set_add(node_types, (void *)node, 1, NULL);
1950
0
            LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1951
0
        } else if (node->schema->nodetype & LYD_NODE_INNER) {
1952
            /* new node validation, autodelete */
1953
0
            r = lyd_validate_new(lyd_node_child_p(node), node->schema, NULL, NULL, val_opts, int_opts, getnext_ht, diff);
1954
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
1955
1956
            /* add nested defaults */
1957
0
            impl_opts = 0;
1958
0
            if (val_opts & LYD_VALIDATE_NO_STATE) {
1959
0
                impl_opts |= LYD_IMPLICIT_NO_STATE;
1960
0
            }
1961
0
            if (val_opts & LYD_VALIDATE_NO_DEFAULTS) {
1962
0
                impl_opts |= LYD_IMPLICIT_NO_DEFAULTS;
1963
0
            }
1964
0
            r = lyd_new_implicit(node, lyd_node_child_p(node), NULL, NULL, NULL, NULL, NULL, impl_opts, getnext_ht, diff);
1965
0
            LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1966
0
        }
1967
1968
0
        if (lysc_has_when(node->schema)) {
1969
            /* when evaluation */
1970
0
            r = ly_set_add(node_when, (void *)node, 1, NULL);
1971
0
            LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1972
0
        }
1973
1974
        /* store for ext instance node validation, if needed */
1975
0
        r = lyd_validate_node_ext(node, ext_node);
1976
0
        LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1977
1978
0
next_node:
1979
0
        LYD_TREE_DFS_END(root, node);
1980
0
    }
1981
1982
0
cleanup:
1983
0
    return rc;
1984
0
}
1985
1986
LY_ERR
1987
lyd_validate(struct lyd_node **tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t val_opts,
1988
        ly_bool validate_subtree, struct ly_set *node_when_p, struct ly_set *node_types_p, struct ly_set *meta_types_p,
1989
        struct ly_set *ext_node_p, struct ly_set *ext_val_p, struct lyd_node **diff)
1990
482
{
1991
482
    LY_ERR r, rc = LY_SUCCESS;
1992
482
    struct lyd_node *first, *next, **first2, *iter;
1993
482
    const struct lys_module *mod;
1994
482
    struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}, ext_node = {0}, ext_val = {0};
1995
482
    uint32_t i = 0, impl_opts;
1996
482
    struct ly_ht *getnext_ht = NULL;
1997
1998
482
    assert(tree && ctx);
1999
482
    assert((node_when_p && node_types_p && meta_types_p && ext_node_p && ext_val_p) ||
2000
482
            (!node_when_p && !node_types_p && !meta_types_p && !ext_node_p && !ext_val_p));
2001
2002
482
    if (!node_when_p) {
2003
0
        node_when_p = &node_when;
2004
0
        node_types_p = &node_types;
2005
0
        meta_types_p = &meta_types;
2006
0
        ext_node_p = &ext_node;
2007
0
        ext_val_p = &ext_val;
2008
0
    }
2009
2010
482
    next = *tree;
2011
642
    while (1) {
2012
642
        if (val_opts & LYD_VALIDATE_PRESENT) {
2013
642
            mod = lyd_data_next_module(&next, &first);
2014
642
        } else {
2015
0
            mod = lyd_mod_next_module(next, module, ctx, &i, &first);
2016
0
        }
2017
642
        if (!mod) {
2018
221
            break;
2019
221
        }
2020
421
        if (!first || (first == *tree)) {
2021
            /* make sure first2 changes are carried to tree */
2022
421
            first2 = tree;
2023
421
        } else {
2024
0
            first2 = &first;
2025
0
        }
2026
2027
        /* create the getnext hash table for this module */
2028
421
        r = lyd_val_getnext_ht_new(&getnext_ht);
2029
421
        LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2030
2031
        /* validate new top-level nodes of this module, autodelete */
2032
421
        r = lyd_validate_new(first2, *first2 ? lysc_data_parent((*first2)->schema) : NULL, mod, NULL, val_opts, 0,
2033
421
                getnext_ht, diff);
2034
421
        LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2035
2036
        /* add all top-level defaults for this module, if going to validate subtree, do not add into unres sets
2037
         * (lyd_validate_subtree() adds all the nodes in that case) */
2038
288
        impl_opts = 0;
2039
288
        if (val_opts & LYD_VALIDATE_NO_STATE) {
2040
0
            impl_opts |= LYD_IMPLICIT_NO_STATE;
2041
0
        }
2042
288
        if (val_opts & LYD_VALIDATE_NO_DEFAULTS) {
2043
0
            impl_opts |= LYD_IMPLICIT_NO_DEFAULTS;
2044
0
        }
2045
288
        if (validate_subtree) {
2046
0
            r = lyd_new_implicit(lyd_parent(*first2), first2, NULL, mod, NULL, NULL, NULL, impl_opts, getnext_ht, diff);
2047
0
            LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2048
288
        } else {
2049
            /* descendants will not be validated, create them all */
2050
288
            r = lyd_new_implicit_r(lyd_parent(*first2), first2, NULL, mod, node_when_p, node_types_p, ext_node_p,
2051
288
                    impl_opts, getnext_ht, diff);
2052
288
            LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2053
288
        }
2054
2055
        /* our first module node pointer may no longer be the first */
2056
288
        first = *first2;
2057
288
        lyd_first_module_sibling(&first, mod);
2058
288
        if (!first || (first == *tree)) {
2059
288
            first2 = tree;
2060
288
        } else {
2061
0
            first2 = &first;
2062
0
        }
2063
2064
288
        if (validate_subtree) {
2065
            /* process nested nodes */
2066
0
            LY_LIST_FOR(*first2, iter) {
2067
0
                if (lyd_owner_module(iter) != mod) {
2068
0
                    break;
2069
0
                }
2070
2071
0
                r = lyd_validate_subtree(iter, node_when_p, node_types_p, meta_types_p, ext_node_p, ext_val_p,
2072
0
                        val_opts, 0, getnext_ht, diff);
2073
0
                LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2074
0
            }
2075
0
        }
2076
2077
        /* finish incompletely validated terminal values/attributes and when conditions */
2078
288
        r = lyd_validate_unres(first2, mod, LYD_TYPE_DATA_YANG, node_when_p, 0, node_types_p, meta_types_p,
2079
288
                ext_node_p, ext_val_p, val_opts, diff);
2080
288
        LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2081
2082
160
        if (!(val_opts & LYD_VALIDATE_NOT_FINAL)) {
2083
            /* perform final validation that assumes the data tree is final */
2084
160
            r = lyd_validate_final_r(*first2, NULL, NULL, mod, NULL, val_opts, 0, 0, getnext_ht);
2085
160
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2086
160
        }
2087
2088
        /* free the getnext hash table */
2089
160
        lyht_free(getnext_ht, lyd_val_getnext_ht_free_cb);
2090
160
        getnext_ht = NULL;
2091
160
    }
2092
2093
482
cleanup:
2094
482
    ly_set_erase(&node_when, NULL);
2095
482
    ly_set_erase(&node_types, NULL);
2096
482
    ly_set_erase(&meta_types, NULL);
2097
482
    ly_set_erase(&ext_node, free);
2098
482
    ly_set_erase(&ext_val, free);
2099
482
    lyd_val_getnext_ht_free(getnext_ht);
2100
482
    return rc;
2101
482
}
2102
2103
LY_ERR
2104
lyd_validate_ext(struct lyd_node **tree, const struct lysc_ext_instance *ext, uint32_t val_opts,
2105
        ly_bool validate_subtree, struct ly_set *node_when_p, struct ly_set *node_types_p, struct ly_set *meta_types_p,
2106
        struct ly_set *ext_node_p, struct ly_set *ext_val_p, struct lyd_node **diff)
2107
0
{
2108
0
    LY_ERR r, rc = LY_SUCCESS;
2109
0
    struct lyd_node *iter;
2110
0
    struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}, ext_node = {0}, ext_val = {0};
2111
0
    struct ly_ht *getnext_ht = NULL;
2112
2113
0
    assert(tree);
2114
0
    assert((node_when_p && node_types_p && meta_types_p && ext_node_p && ext_val_p) ||
2115
0
            (!node_when_p && !node_types_p && !meta_types_p && !ext_node_p && !ext_val_p));
2116
2117
0
    if (!node_when_p) {
2118
0
        node_when_p = &node_when;
2119
0
        node_types_p = &node_types;
2120
0
        meta_types_p = &meta_types;
2121
0
        ext_node_p = &ext_node;
2122
0
        ext_val_p = &ext_val;
2123
0
    }
2124
2125
    /* create the getnext hash table for these data */
2126
0
    r = lyd_val_getnext_ht_new(&getnext_ht);
2127
0
    LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2128
2129
0
    if (validate_subtree) {
2130
        /* process nested nodes */
2131
0
        LY_LIST_FOR(*tree, iter) {
2132
0
            r = lyd_validate_subtree(iter, node_when_p, node_types_p, meta_types_p, ext_node_p, ext_val_p,
2133
0
                    val_opts, 0, getnext_ht, diff);
2134
0
            LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2135
0
        }
2136
0
    }
2137
2138
    /* finish incompletely validated terminal values/attributes and when conditions */
2139
0
    r = lyd_validate_unres(tree, NULL, LYD_TYPE_DATA_YANG, node_when_p, 0, node_types_p, meta_types_p,
2140
0
            ext_node_p, ext_val_p, val_opts, diff);
2141
0
    LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2142
2143
0
    if (!(val_opts & LYD_VALIDATE_NOT_FINAL)) {
2144
        /* perform final validation that assumes the data tree is final */
2145
0
        r = lyd_validate_final_r(*tree, NULL, NULL, NULL, ext, val_opts, 0, 0, getnext_ht);
2146
0
        LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2147
0
    }
2148
2149
0
cleanup:
2150
0
    ly_set_erase(&node_when, NULL);
2151
0
    ly_set_erase(&node_types, NULL);
2152
0
    ly_set_erase(&meta_types, NULL);
2153
0
    ly_set_erase(&ext_node, free);
2154
0
    ly_set_erase(&ext_val, free);
2155
0
    lyd_val_getnext_ht_free(getnext_ht);
2156
0
    return rc;
2157
0
}
2158
2159
LIBYANG_API_DEF LY_ERR
2160
lyd_validate_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t val_opts, struct lyd_node **diff)
2161
0
{
2162
0
    LY_CHECK_ARG_RET(NULL, tree, *tree || ctx, LY_EINVAL);
2163
0
    LY_CHECK_CTX_EQUAL_RET(__func__, *tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
2164
0
    if (!ctx) {
2165
0
        ctx = LYD_CTX(*tree);
2166
0
    }
2167
0
    if (diff) {
2168
0
        *diff = NULL;
2169
0
    }
2170
2171
0
    return lyd_validate(tree, NULL, ctx, val_opts, 1, NULL, NULL, NULL, NULL, NULL, diff);
2172
0
}
2173
2174
LIBYANG_API_DEF LY_ERR
2175
lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, uint32_t val_opts, struct lyd_node **diff)
2176
0
{
2177
0
    LY_CHECK_ARG_RET(NULL, tree, module, !(val_opts & LYD_VALIDATE_PRESENT), LY_EINVAL);
2178
0
    LY_CHECK_CTX_EQUAL_RET(__func__, *tree ? LYD_CTX(*tree) : NULL, module->ctx, LY_EINVAL);
2179
0
    if (diff) {
2180
0
        *diff = NULL;
2181
0
    }
2182
2183
0
    return lyd_validate(tree, module, module->ctx, val_opts, 1, NULL, NULL, NULL, NULL, NULL, diff);
2184
0
}
2185
2186
LIBYANG_API_DEF LY_ERR
2187
lyd_validate_module_final(struct lyd_node *tree, const struct lys_module *module, uint32_t val_opts)
2188
0
{
2189
0
    LY_ERR r, rc = LY_SUCCESS;
2190
0
    struct lyd_node *first;
2191
0
    const struct lys_module *mod;
2192
0
    uint32_t i = 0;
2193
0
    struct ly_ht *getnext_ht = NULL;
2194
2195
0
    LY_CHECK_ARG_RET(NULL, module, !(val_opts & (LYD_VALIDATE_PRESENT | LYD_VALIDATE_NOT_FINAL)), LY_EINVAL);
2196
0
    LY_CHECK_CTX_EQUAL_RET(__func__, tree ? LYD_CTX(tree) : NULL, module->ctx, LY_EINVAL);
2197
2198
    /* module is unchanged but we need to get the first module data node */
2199
0
    mod = lyd_mod_next_module(tree, module, module->ctx, &i, &first);
2200
0
    assert(mod);
2201
2202
    /* create the getnext hash table for this module */
2203
0
    r = lyd_val_getnext_ht_new(&getnext_ht);
2204
0
    LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2205
2206
    /* perform final validation that assumes the data tree is final */
2207
0
    r = lyd_validate_final_r(first, NULL, NULL, mod, NULL, val_opts, 0, 0, getnext_ht);
2208
0
    LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
2209
2210
0
cleanup:
2211
0
    lyd_val_getnext_ht_free(getnext_ht);
2212
0
    return rc;
2213
0
}
2214
2215
/**
2216
 * @brief Find nodes for merging an operation into data tree for validation.
2217
 *
2218
 * @param[in] op_tree Full operation data tree.
2219
 * @param[in] op_node Operation node itself.
2220
 * @param[in] tree Data tree to be merged into.
2221
 * @param[out] op_subtree Operation subtree to merge.
2222
 * @param[out] tree_sibling Data tree sibling to merge next to, is set if @p tree_parent is NULL.
2223
 * @param[out] tree_parent Data tree parent to merge into, is set if @p tree_sibling is NULL.
2224
 */
2225
static void
2226
lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
2227
        struct lyd_node **op_subtree, struct lyd_node **tree_sibling, struct lyd_node **tree_parent)
2228
0
{
2229
0
    const struct lyd_node *tree_iter, *op_iter;
2230
0
    struct lyd_node *match = NULL;
2231
0
    uint32_t i, cur_depth, op_depth;
2232
2233
0
    *op_subtree = NULL;
2234
0
    *tree_sibling = NULL;
2235
0
    *tree_parent = NULL;
2236
2237
    /* learn op depth (top-level being depth 0) */
2238
0
    op_depth = 0;
2239
0
    for (op_iter = op_node; op_iter != op_tree; op_iter = lyd_parent(op_iter)) {
2240
0
        ++op_depth;
2241
0
    }
2242
2243
    /* find where to merge op */
2244
0
    tree_iter = tree;
2245
0
    cur_depth = op_depth;
2246
0
    while (cur_depth && tree_iter) {
2247
        /* find op iter in tree */
2248
0
        lyd_find_sibling_first(tree_iter, op_iter, &match);
2249
0
        if (!match) {
2250
0
            break;
2251
0
        }
2252
2253
        /* move tree_iter */
2254
0
        tree_iter = lyd_child(match);
2255
2256
        /* move depth */
2257
0
        --cur_depth;
2258
2259
        /* find next op parent */
2260
0
        op_iter = op_node;
2261
0
        for (i = 0; i < cur_depth; ++i) {
2262
0
            op_iter = lyd_parent(op_iter);
2263
0
        }
2264
0
    }
2265
2266
0
    assert(op_iter);
2267
0
    *op_subtree = (struct lyd_node *)op_iter;
2268
0
    if (!tree || tree_iter) {
2269
        /* there is no tree whatsoever or this is the last found sibling */
2270
0
        *tree_sibling = (struct lyd_node *)tree_iter;
2271
0
    } else {
2272
        /* matching parent was found but it has no children to insert next to */
2273
0
        assert(match);
2274
0
        *tree_parent = match;
2275
0
    }
2276
0
}
2277
2278
/**
2279
 * @brief Validate an RPC/action request, reply, or notification.
2280
 *
2281
 * @param[in] op_tree Full operation data tree.
2282
 * @param[in] op_node Operation node itself.
2283
 * @param[in] dep_tree Tree to be used for validating references from the operation subtree.
2284
 * @param[in] int_opts Internal parser options.
2285
 * @param[in] data_type Type of validated data.
2286
 * @param[in] validate_subtree Whether subtree was already validated (as part of data parsing) or not (separate validation).
2287
 * @param[in] node_when_p Set of nodes with when conditions, if NULL a local set is used.
2288
 * @param[in] node_types_p Set of unres node types, if NULL a local set is used.
2289
 * @param[in] meta_types_p Set of unres metadata types, if NULL a local set is used.
2290
 * @param[in] ext_node_p Set of unres nodes with extensions to validate, if NULL a local set is used.
2291
 * @param[in] ext_val_p Set of parsed extension data to validate, if NULL a local set is used.
2292
 * @param[out] diff Optional diff with any changes made by the validation.
2293
 * @return LY_SUCCESS on success.
2294
 * @return LY_ERR error on error.
2295
 */
2296
static LY_ERR
2297
_lyd_validate_op(struct lyd_node *op_tree, struct lyd_node *op_node, const struct lyd_node *dep_tree, enum lyd_type data_type,
2298
        uint32_t int_opts, ly_bool validate_subtree, struct ly_set *node_when_p, struct ly_set *node_types_p,
2299
        struct ly_set *meta_types_p,  struct ly_set *ext_node_p, struct ly_set *ext_val_p, struct lyd_node **diff)
2300
0
{
2301
0
    LY_ERR rc = LY_SUCCESS;
2302
0
    struct lyd_node *tree_sibling, *tree_parent, *op_subtree, *op_parent, *op_sibling_before, *op_sibling_after, *child;
2303
0
    struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}, ext_node = {0}, ext_val = {0};
2304
0
    struct ly_ht *getnext_ht = NULL;
2305
2306
0
    assert(op_tree && op_node);
2307
0
    assert((node_when_p && node_types_p && meta_types_p && ext_node_p && ext_val_p) ||
2308
0
            (!node_when_p && !node_types_p && !meta_types_p && !ext_node_p && !ext_val_p));
2309
2310
0
    if (!node_when_p) {
2311
0
        node_when_p = &node_when;
2312
0
        node_types_p = &node_types;
2313
0
        meta_types_p = &meta_types;
2314
0
        ext_node_p = &ext_node;
2315
0
        ext_val_p = &ext_val;
2316
0
    }
2317
2318
    /* merge op_tree into dep_tree */
2319
0
    lyd_val_op_merge_find(op_tree, op_node, dep_tree, &op_subtree, &tree_sibling, &tree_parent);
2320
0
    op_sibling_before = op_subtree->prev->next ? op_subtree->prev : NULL;
2321
0
    op_sibling_after = op_subtree->next;
2322
0
    op_parent = lyd_parent(op_subtree);
2323
2324
0
    lyd_unlink(op_subtree);
2325
0
    lyd_insert_node(tree_parent, &tree_sibling, op_subtree, LYD_INSERT_NODE_DEFAULT);
2326
0
    if (!dep_tree) {
2327
0
        dep_tree = tree_sibling;
2328
0
    }
2329
2330
    /* create the getnext hash table for this module */
2331
0
    rc = lyd_val_getnext_ht_new(&getnext_ht);
2332
0
    LY_CHECK_GOTO(rc, cleanup);
2333
2334
0
    if (int_opts & LYD_INTOPT_REPLY) {
2335
0
        if (validate_subtree) {
2336
            /* add output children defaults */
2337
0
            rc = lyd_new_implicit(op_node, lyd_node_child_p(op_node), NULL, NULL, node_when_p, node_types_p,
2338
0
                    ext_node_p, LYD_IMPLICIT_OUTPUT, getnext_ht, diff);
2339
0
            LY_CHECK_GOTO(rc, cleanup);
2340
2341
            /* skip validating the operation itself, go to children directly */
2342
0
            LY_LIST_FOR(lyd_child(op_node), child) {
2343
0
                rc = lyd_validate_subtree(child, node_when_p, node_types_p, meta_types_p, ext_node_p, ext_val_p, 0,
2344
0
                        int_opts, getnext_ht, diff);
2345
0
                LY_CHECK_GOTO(rc, cleanup);
2346
0
            }
2347
0
        } else {
2348
            /* add output children defaults and their descendants */
2349
0
            rc = lyd_new_implicit_r(op_node, lyd_node_child_p(op_node), NULL, NULL, node_when_p, node_types_p,
2350
0
                    ext_node_p, LYD_IMPLICIT_OUTPUT, getnext_ht, diff);
2351
0
            LY_CHECK_GOTO(rc, cleanup);
2352
0
        }
2353
0
    } else {
2354
0
        if (validate_subtree) {
2355
            /* prevalidate whole operation subtree */
2356
0
            rc = lyd_validate_subtree(op_node, node_when_p, node_types_p, meta_types_p, ext_node_p, ext_val_p, 0,
2357
0
                    int_opts, getnext_ht, diff);
2358
0
            LY_CHECK_GOTO(rc, cleanup);
2359
0
        }
2360
0
    }
2361
2362
    /* finish incompletely validated terminal values/attributes and when conditions on the full tree,
2363
     * account for unresolved 'when' that may appear in the non-validated dependency data tree */
2364
0
    LY_CHECK_GOTO(rc = lyd_validate_unres((struct lyd_node **)&dep_tree, NULL, data_type, node_when_p, LYXP_IGNORE_WHEN,
2365
0
            node_types_p, meta_types_p, ext_node_p, ext_val_p, 0, diff), cleanup);
2366
2367
    /* perform final validation of the operation/notification */
2368
0
    lyd_validate_obsolete(op_node);
2369
0
    LY_CHECK_GOTO(rc = lyd_validate_must(op_node, 0, int_opts, LYXP_IGNORE_WHEN), cleanup);
2370
2371
    /* final validation of all the descendants */
2372
0
    rc = lyd_validate_final_r(lyd_child(op_node), op_node, op_node->schema, NULL, NULL, 0, int_opts, LYXP_IGNORE_WHEN,
2373
0
            getnext_ht);
2374
0
    LY_CHECK_GOTO(rc, cleanup);
2375
2376
0
cleanup:
2377
    /* restore operation tree */
2378
0
    lyd_unlink(op_subtree);
2379
0
    if (op_sibling_before) {
2380
0
        lyd_insert_after_node(NULL, op_sibling_before, op_subtree);
2381
0
        lyd_insert_hash(op_subtree);
2382
0
    } else if (op_sibling_after) {
2383
0
        lyd_insert_before_node(op_sibling_after, op_subtree);
2384
0
        lyd_insert_hash(op_subtree);
2385
0
    } else if (op_parent) {
2386
0
        lyd_insert_node(op_parent, NULL, op_subtree, LYD_INSERT_NODE_DEFAULT);
2387
0
    }
2388
2389
0
    ly_set_erase(&node_when, NULL);
2390
0
    ly_set_erase(&node_types, NULL);
2391
0
    ly_set_erase(&meta_types, NULL);
2392
0
    ly_set_erase(&ext_node, free);
2393
0
    ly_set_erase(&ext_val, free);
2394
0
    lyd_val_getnext_ht_free(getnext_ht);
2395
0
    return rc;
2396
0
}
2397
2398
LIBYANG_API_DEF LY_ERR
2399
lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *dep_tree, enum lyd_type data_type, struct lyd_node **diff)
2400
0
{
2401
0
    struct lyd_node *op_node;
2402
0
    uint32_t int_opts;
2403
0
    struct ly_set ext_val = {0};
2404
0
    LY_ERR rc;
2405
2406
0
    LY_CHECK_ARG_RET(NULL, op_tree, !dep_tree || !dep_tree->parent, (data_type == LYD_TYPE_RPC_YANG) ||
2407
0
            (data_type == LYD_TYPE_NOTIF_YANG) || (data_type == LYD_TYPE_REPLY_YANG), LY_EINVAL);
2408
0
    if (diff) {
2409
0
        *diff = NULL;
2410
0
    }
2411
0
    if (data_type == LYD_TYPE_RPC_YANG) {
2412
0
        int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
2413
0
    } else if (data_type == LYD_TYPE_NOTIF_YANG) {
2414
0
        int_opts = LYD_INTOPT_NOTIF;
2415
0
    } else {
2416
0
        int_opts = LYD_INTOPT_REPLY;
2417
0
    }
2418
2419
0
    if (op_tree->schema && (op_tree->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
2420
        /* we have the operation/notification, adjust the pointers */
2421
0
        op_node = op_tree;
2422
0
        while (op_tree->parent) {
2423
0
            op_tree = lyd_parent(op_tree);
2424
0
        }
2425
0
    } else {
2426
        /* find the operation/notification */
2427
0
        while (op_tree->parent) {
2428
0
            op_tree = lyd_parent(op_tree);
2429
0
        }
2430
0
        LYD_TREE_DFS_BEGIN(op_tree, op_node) {
2431
0
            if (!op_node->schema) {
2432
0
                return lyd_parse_opaq_error(op_node);
2433
0
            } else if (op_node->flags & LYD_EXT) {
2434
                /* fully validate the rest using the extension instance callback */
2435
0
                LY_CHECK_RET(lyd_validate_nested_ext(op_node, &ext_val));
2436
0
                rc = lyd_validate_unres((struct lyd_node **)&dep_tree, NULL, data_type, NULL, 0, NULL, NULL, NULL,
2437
0
                        &ext_val, 0, diff);
2438
0
                ly_set_erase(&ext_val, free);
2439
0
                return rc;
2440
0
            }
2441
2442
0
            if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) &&
2443
0
                    (op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
2444
0
                break;
2445
0
            } else if ((int_opts & LYD_INTOPT_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
2446
0
                break;
2447
0
            }
2448
0
            LYD_TREE_DFS_END(op_tree, op_node);
2449
0
        }
2450
0
    }
2451
2452
0
    if (int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) {
2453
0
        if (!op_node || !(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
2454
0
            LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
2455
0
            return LY_EINVAL;
2456
0
        }
2457
0
    } else {
2458
0
        if (!op_node || (op_node->schema->nodetype != LYS_NOTIF)) {
2459
0
            LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
2460
0
            return LY_EINVAL;
2461
0
        }
2462
0
    }
2463
2464
    /* validate */
2465
0
    return _lyd_validate_op(op_tree, op_node, dep_tree, data_type, int_opts, 1, NULL, NULL, NULL, NULL, NULL, diff);
2466
0
}