Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file validation.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief Validation |
| 5 | * |
| 6 | * Copyright (c) 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 14 | #define _POSIX_C_SOURCE 200809L /* strdup */ |
Michal Vasko | 81bc551 | 2020-11-13 18:05:18 +0100 | [diff] [blame] | 15 | |
Michal Vasko | fbed4ea | 2020-07-08 10:43:30 +0200 | [diff] [blame] | 16 | #include "validation.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 17 | |
| 18 | #include <assert.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include <string.h> |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "common.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 25 | #include "compat.h" |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 26 | #include "diff.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "hash_table.h" |
| 28 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 29 | #include "parser_data.h" |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 30 | #include "plugins_exts_metadata.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "plugins_types.h" |
| 32 | #include "set.h" |
| 33 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 34 | #include "tree_data.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 35 | #include "tree_data_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "tree_schema.h" |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 37 | #include "tree_schema_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "xpath.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 39 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 40 | LY_ERR |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 41 | lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff) |
| 42 | { |
| 43 | LY_ERR ret = LY_SUCCESS; |
| 44 | struct lyd_node *new_diff = NULL; |
Michal Vasko | 81bc551 | 2020-11-13 18:05:18 +0100 | [diff] [blame] | 45 | const struct lyd_node *prev_inst; |
| 46 | char *key = NULL, *value = NULL; |
| 47 | size_t buflen = 0, bufused = 0; |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 48 | |
| 49 | assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE)); |
| 50 | |
Michal Vasko | 81bc551 | 2020-11-13 18:05:18 +0100 | [diff] [blame] | 51 | if ((op == LYD_DIFF_OP_CREATE) && lysc_is_userordered(node->schema)) { |
| 52 | if (node->prev->next && (node->prev->schema == node->schema)) { |
| 53 | prev_inst = node->prev; |
| 54 | } else { |
| 55 | /* first instance */ |
| 56 | prev_inst = NULL; |
| 57 | } |
| 58 | |
| 59 | if (node->schema->nodetype == LYS_LIST) { |
| 60 | /* generate key meta */ |
| 61 | if (prev_inst) { |
| 62 | LY_CHECK_GOTO(ret = lyd_path_list_predicate(prev_inst, &key, &buflen, &bufused, 0), cleanup); |
| 63 | } else { |
| 64 | key = strdup(""); |
| 65 | LY_CHECK_ERR_GOTO(!key, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup); |
| 66 | } |
| 67 | } else { |
| 68 | /* generate value meta */ |
| 69 | if (prev_inst) { |
| 70 | value = strdup(LYD_CANON_VALUE(prev_inst)); |
| 71 | LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup); |
| 72 | } else { |
| 73 | value = strdup(""); |
| 74 | LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 79 | /* create new diff tree */ |
Michal Vasko | 81bc551 | 2020-11-13 18:05:18 +0100 | [diff] [blame] | 80 | LY_CHECK_GOTO(ret = lyd_diff_add(node, op, NULL, NULL, key, value, NULL, &new_diff), cleanup); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 81 | |
| 82 | /* merge into existing diff */ |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 83 | ret = lyd_diff_merge_all(diff, new_diff, 0); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 84 | |
Michal Vasko | 81bc551 | 2020-11-13 18:05:18 +0100 | [diff] [blame] | 85 | cleanup: |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 86 | lyd_free_tree(new_diff); |
Michal Vasko | 81bc551 | 2020-11-13 18:05:18 +0100 | [diff] [blame] | 87 | free(key); |
| 88 | free(value); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | /** |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 93 | * @brief Evaluate all relevant "when" conditions of a node. |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 94 | * |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 95 | * @param[in] tree Data tree. |
| 96 | * @param[in] node Node whose relevant when conditions will be evaluated. |
| 97 | * @param[in] schema Schema node of @p node. It may not be possible to use directly if @p node is opaque. |
| 98 | * @param[out] disabled First when that evaluated false, if any. |
| 99 | * @return LY_SUCCESS on success. |
| 100 | * @return LY_EINCOMPLETE if a referenced node does not have its when evaluated. |
| 101 | * @return LY_ERR value on error. |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 102 | */ |
| 103 | static LY_ERR |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 104 | lyd_validate_node_when(const struct lyd_node *tree, const struct lyd_node *node, const struct lysc_node *schema, |
| 105 | const struct lysc_when **disabled) |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 106 | { |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 107 | LY_ERR ret; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 108 | const struct lyd_node *ctx_node; |
| 109 | struct lyxp_set xp_set; |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 110 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 111 | |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 112 | assert(!node->schema || (node->schema == schema)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 113 | |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 114 | *disabled = NULL; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 115 | |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 116 | do { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 117 | const struct lysc_when *when; |
| 118 | struct lysc_when **when_list = lysc_node_when(schema); |
| 119 | LY_ARRAY_FOR(when_list, u) { |
| 120 | when = when_list[u]; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 121 | |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 122 | /* get context node */ |
| 123 | if (when->context == schema) { |
| 124 | ctx_node = node; |
| 125 | } else { |
| 126 | assert((!when->context && !node->parent) || (when->context == node->parent->schema)); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 127 | ctx_node = lyd_parent(node); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 128 | } |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 129 | |
| 130 | /* evaluate when */ |
| 131 | memset(&xp_set, 0, sizeof xp_set); |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 132 | ret = lyxp_eval(LYD_CTX(node), when->cond, schema->module, LY_PREF_SCHEMA_RESOLVED, when->prefixes, |
| 133 | ctx_node, tree, &xp_set, LYXP_SCHEMA); |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 134 | lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
| 135 | |
| 136 | /* return error or LY_EINCOMPLETE for dependant unresolved when */ |
| 137 | LY_CHECK_RET(ret); |
| 138 | |
| 139 | if (!xp_set.val.bln) { |
| 140 | /* false when */ |
| 141 | *disabled = when; |
| 142 | return LY_SUCCESS; |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 143 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 144 | } |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 145 | |
| 146 | schema = schema->parent; |
| 147 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 148 | |
| 149 | return LY_SUCCESS; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @brief Evaluate when conditions of collected unres nodes. |
| 154 | * |
| 155 | * @param[in,out] tree Data tree, is updated if some nodes are autodeleted. |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 156 | * @param[in] mod Module of the @p tree to take into consideration when deleting @p tree and moving it. |
| 157 | * If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be |
| 158 | * the first top-level sibling. |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 159 | * @param[in] node_when Set with nodes with "when" conditions. |
| 160 | * @param[in,out] diff Validation diff. |
| 161 | * @return LY_SUCCESS on success. |
| 162 | * @return LY_ERR value on error. |
| 163 | */ |
| 164 | static LY_ERR |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 165 | lyd_validate_unres_when(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when, |
| 166 | struct lyd_node **diff) |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 167 | { |
| 168 | LY_ERR ret; |
| 169 | uint32_t i; |
| 170 | const struct lysc_when *disabled; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 171 | struct lyd_node *node = NULL; |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 172 | |
| 173 | if (!node_when->count) { |
| 174 | return LY_SUCCESS; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 177 | i = node_when->count; |
| 178 | do { |
| 179 | --i; |
| 180 | node = node_when->dnodes[i]; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 181 | LOG_LOCSET(node->schema, node, NULL, NULL); |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 182 | |
| 183 | /* evaluate all when expressions that affect this node's existence */ |
| 184 | ret = lyd_validate_node_when(*tree, node, node->schema, &disabled); |
| 185 | if (!ret) { |
| 186 | if (disabled) { |
| 187 | /* when false */ |
| 188 | if (node->flags & LYD_WHEN_TRUE) { |
| 189 | /* autodelete */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 190 | lyd_del_move_root(tree, node, mod); |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 191 | if (diff) { |
| 192 | /* add into diff */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 193 | ret = lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff); |
| 194 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 195 | } |
| 196 | lyd_free_tree(node); |
| 197 | } else { |
| 198 | /* invalid data */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 199 | LOGVAL(LYD_CTX(node), LY_VCODE_NOWHEN, disabled->cond->expr); |
| 200 | ret = LY_EVALID; |
| 201 | goto error; |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 202 | } |
| 203 | } else { |
| 204 | /* when true */ |
| 205 | node->flags |= LYD_WHEN_TRUE; |
| 206 | } |
| 207 | |
| 208 | /* remove this node from the set, its when was resolved */ |
| 209 | ly_set_rm_index(node_when, i, NULL); |
| 210 | } else if (ret != LY_EINCOMPLETE) { |
| 211 | /* error */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 212 | goto error; |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 213 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 214 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 215 | LOG_LOCBACK(1, 1, 0, 0); |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 216 | } while (i); |
| 217 | |
| 218 | return LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 219 | |
| 220 | error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 221 | LOG_LOCBACK(1, 1, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 222 | return ret; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | LY_ERR |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 226 | lyd_validate_unres(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when, |
| 227 | struct ly_set *node_types, struct ly_set *meta_types, struct lyd_node **diff) |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 228 | { |
| 229 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 230 | uint32_t i; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 231 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 232 | if (node_when) { |
| 233 | /* evaluate all when conditions */ |
| 234 | uint32_t prev_count; |
| 235 | do { |
| 236 | prev_count = node_when->count; |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 237 | LY_CHECK_RET(lyd_validate_unres_when(tree, mod, node_when, diff)); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 238 | /* there must have been some when conditions resolved */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 239 | } while (prev_count > node_when->count); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 240 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 241 | /* there could have been no cyclic when dependencies, checked during compilation */ |
| 242 | assert(!node_when->count); |
| 243 | } |
| 244 | |
| 245 | if (node_types && node_types->count) { |
| 246 | /* finish incompletely validated terminal values (traverse from the end for efficient set removal) */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 247 | i = node_types->count; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 248 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 249 | --i; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 250 | |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 251 | struct lyd_node_term *node = node_types->objs[i]; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 252 | struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 253 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 254 | /* resolve the value of the node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 255 | LOG_LOCSET(node->schema, &node->node, NULL, NULL); |
| 256 | ret = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, &node->node, *tree); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 257 | LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 258 | LY_CHECK_RET(ret); |
| 259 | |
| 260 | /* remove this node from the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 261 | ly_set_rm_index(node_types, i, NULL); |
| 262 | } while (i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 265 | if (meta_types && meta_types->count) { |
| 266 | /* ... and metadata values */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 267 | i = meta_types->count; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 268 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 269 | --i; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 270 | |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 271 | struct lyd_meta *meta = meta_types->objs[i]; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 272 | struct lysc_type *type = ((struct lyext_metadata *)meta->annotation->data)->type; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 273 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 274 | /* validate and store the value of the metadata */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 275 | ret = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 276 | LY_CHECK_RET(ret); |
| 277 | |
| 278 | /* remove this attr from the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 279 | ly_set_rm_index(meta_types, i, NULL); |
| 280 | } while (i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 281 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 286 | /** |
| 287 | * @brief Validate instance duplication. |
| 288 | * |
| 289 | * @param[in] first First sibling to search in. |
| 290 | * @param[in] node Data node instance to check. |
| 291 | * @return LY_ERR value. |
| 292 | */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 293 | static LY_ERR |
| 294 | lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node) |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 295 | { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 296 | struct lyd_node **match_p; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 297 | ly_bool fail = 0; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 298 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 299 | assert(node->flags & LYD_NEW); |
| 300 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 301 | if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) { |
| 302 | /* duplicate instances allowed */ |
| 303 | return LY_SUCCESS; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 306 | /* find exactly the same next instance using hashes if possible */ |
| 307 | if (node->parent && node->parent->children_ht) { |
| 308 | if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) { |
| 309 | fail = 1; |
| 310 | } |
| 311 | } else { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 312 | for ( ; first; first = first->next) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 313 | if (first == node) { |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) { |
| 318 | if (first->schema == node->schema) { |
| 319 | fail = 1; |
| 320 | break; |
| 321 | } |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 322 | } else if (!lyd_compare_single(first, node, 0)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 323 | fail = 1; |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (fail) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 330 | LOGVAL(node->schema->module->ctx, LY_VCODE_DUP, node->schema->name); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 331 | return LY_EVALID; |
| 332 | } |
| 333 | return LY_SUCCESS; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 336 | /** |
| 337 | * @brief Validate multiple case data existence with possible autodelete. |
| 338 | * |
| 339 | * @param[in,out] first First sibling to search in, is updated if needed. |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 340 | * @param[in] mod Module of the siblings, NULL for nested siblings. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 341 | * @param[in] choic Choice node whose cases to check. |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 342 | * @param[in,out] diff Validation diff. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 343 | * @return LY_ERR value. |
| 344 | */ |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 345 | static LY_ERR |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 346 | lyd_validate_cases(struct lyd_node **first, const struct lys_module *mod, const struct lysc_node_choice *choic, |
| 347 | struct lyd_node **diff) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 348 | { |
| 349 | const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL; |
| 350 | struct lyd_node *match, *to_del; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 351 | ly_bool found; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 352 | |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 353 | LOG_LOCSET(&choic->node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 354 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 355 | LY_LIST_FOR((struct lysc_node *)choic->cases, scase) { |
| 356 | found = 0; |
| 357 | iter = NULL; |
| 358 | match = NULL; |
| 359 | while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) { |
| 360 | if (match->flags & LYD_NEW) { |
| 361 | /* a new case data found, nothing more to look for */ |
| 362 | found = 2; |
| 363 | break; |
| 364 | } else { |
| 365 | /* and old case data found */ |
| 366 | if (found == 0) { |
| 367 | found = 1; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (found == 1) { |
| 373 | /* there should not be 2 old cases */ |
| 374 | if (old_case) { |
| 375 | /* old data from 2 cases */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 376 | LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, old_case->name, scase->name); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 377 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 378 | return LY_EVALID; |
| 379 | } |
| 380 | |
| 381 | /* remember an old existing case */ |
| 382 | old_case = scase; |
| 383 | } else if (found == 2) { |
| 384 | if (new_case) { |
| 385 | /* new data from 2 cases */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 386 | LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, new_case->name, scase->name); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 387 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 388 | return LY_EVALID; |
| 389 | } |
| 390 | |
| 391 | /* remember a new existing case */ |
| 392 | new_case = scase; |
| 393 | } |
| 394 | } |
| 395 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 396 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 397 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 398 | if (old_case && new_case) { |
| 399 | /* auto-delete old case */ |
| 400 | iter = NULL; |
| 401 | match = NULL; |
| 402 | to_del = NULL; |
| 403 | while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) { |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 404 | lyd_del_move_root(first, to_del, mod); |
| 405 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 406 | /* free previous node */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 407 | lyd_free_tree(to_del); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 408 | if (diff) { |
| 409 | /* add into diff */ |
| 410 | LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff)); |
| 411 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 412 | to_del = match; |
| 413 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 414 | lyd_del_move_root(first, to_del, mod); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 415 | lyd_free_tree(to_del); |
| 416 | } |
| 417 | |
| 418 | return LY_SUCCESS; |
| 419 | } |
| 420 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 421 | /** |
| 422 | * @brief Check whether a schema node can have some default values (true for NP containers as well). |
| 423 | * |
| 424 | * @param[in] schema Schema node to check. |
| 425 | * @return non-zero if yes, |
| 426 | * @return 0 otherwise. |
| 427 | */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 428 | static int |
| 429 | lyd_val_has_default(const struct lysc_node *schema) |
| 430 | { |
| 431 | switch (schema->nodetype) { |
| 432 | case LYS_LEAF: |
| 433 | if (((struct lysc_node_leaf *)schema)->dflt) { |
| 434 | return 1; |
| 435 | } |
| 436 | break; |
| 437 | case LYS_LEAFLIST: |
| 438 | if (((struct lysc_node_leaflist *)schema)->dflts) { |
| 439 | return 1; |
| 440 | } |
| 441 | break; |
| 442 | case LYS_CONTAINER: |
| 443 | if (!(schema->flags & LYS_PRESENCE)) { |
| 444 | return 1; |
| 445 | } |
| 446 | break; |
| 447 | default: |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 454 | /** |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 455 | * @brief Properly delete a node as part of autodelete validation tasks. |
| 456 | * |
| 457 | * @param[in,out] first First sibling, is updated if needed. |
| 458 | * @param[in] node Node instance to delete. |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 459 | * @param[in] mod Module of the siblings, NULL for nested siblings. |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 460 | * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed. |
| 461 | * @param[in,out] diff Validation diff. |
| 462 | */ |
| 463 | static void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 464 | lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod, |
| 465 | struct lyd_node **next_p, struct lyd_node **diff) |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 466 | { |
| 467 | struct lyd_node *iter; |
| 468 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 469 | lyd_del_move_root(first, node, mod); |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 470 | if (node == *next_p) { |
| 471 | *next_p = (*next_p)->next; |
| 472 | } |
| 473 | if (diff) { |
| 474 | /* add into diff */ |
| 475 | if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) { |
| 476 | /* we do not want to track NP container changes, but remember any removed children */ |
| 477 | LY_LIST_FOR(lyd_child(node), iter) { |
| 478 | lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff); |
| 479 | } |
| 480 | } else { |
| 481 | lyd_val_diff_add(node, LYD_DIFF_OP_DELETE, diff); |
| 482 | } |
| 483 | } |
| 484 | lyd_free_tree(node); |
| 485 | } |
| 486 | |
| 487 | /** |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 488 | * @brief Autodelete old instances to prevent validation errors. |
| 489 | * |
| 490 | * @param[in,out] first First sibling to search in, is updated if needed. |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 491 | * @param[in] node New data node instance to check. |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 492 | * @param[in] mod Module of the siblings, NULL for nested siblings. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 493 | * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed. |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 494 | * @param[in,out] diff Validation diff. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 495 | */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 496 | static void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 497 | lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod, |
| 498 | struct lyd_node **next_p, struct lyd_node **diff) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 499 | { |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 500 | struct lyd_node *match, *next; |
| 501 | |
| 502 | assert(node->flags & LYD_NEW); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 503 | |
| 504 | if (lyd_val_has_default(node->schema)) { |
| 505 | assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER)); |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 506 | LYD_LIST_FOR_INST_SAFE(*first, node->schema, next, match) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 507 | if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) { |
| 508 | /* default instance found, remove it */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 509 | lyd_validate_autodel_node_del(first, match, mod, next_p, diff); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 510 | |
| 511 | /* remove only a single container/leaf default instance, if there are more, it is an error */ |
| 512 | if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) { |
| 513 | break; |
| 514 | } |
| 515 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 520 | /** |
| 521 | * @brief Autodelete leftover default nodes of deleted cases (that have no existing explicit data). |
| 522 | * |
| 523 | * @param[in,out] first First sibling to search in, is updated if needed. |
| 524 | * @param[in] node Default data node instance to check. |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 525 | * @param[in] mod Module of the siblings, NULL for nested siblings. |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 526 | * @param[in,out] next_p Temporary LY_LIST_FOR_SAFE next pointer, is updated if needed. |
| 527 | * @param[in,out] diff Validation diff. |
| 528 | */ |
| 529 | static void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 530 | lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node *node, const struct lys_module *mod, |
| 531 | struct lyd_node **next_p, struct lyd_node **diff) |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 532 | { |
| 533 | struct lysc_node_choice *choic; |
| 534 | struct lyd_node *iter = NULL; |
| 535 | const struct lysc_node *slast = NULL; |
| 536 | |
| 537 | assert(node->flags & LYD_DEFAULT); |
| 538 | |
| 539 | if (!node->schema->parent || (node->schema->parent->nodetype != LYS_CASE)) { |
| 540 | /* the default node is not a descendant of a case */ |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | choic = (struct lysc_node_choice *)node->schema->parent->parent; |
| 545 | assert(choic->nodetype == LYS_CHOICE); |
| 546 | |
| 547 | if (choic->dflt && (choic->dflt == (struct lysc_node_case *)node->schema->parent)) { |
| 548 | /* data of a default case, keep them */ |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | /* try to find an explicit node of the case */ |
| 553 | while ((iter = lys_getnext_data(iter, *first, &slast, node->schema->parent, NULL))) { |
| 554 | if (!(iter->flags & LYD_DEFAULT)) { |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | if (!iter) { |
| 560 | /* there are only default nodes of the case meaning it does not exist and neither should any default nodes |
| 561 | * of the case, remove this one default node */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 562 | lyd_validate_autodel_node_del(first, node, mod, next_p, diff); |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 566 | LY_ERR |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 567 | lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 568 | struct lyd_node **diff) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 569 | { |
| 570 | struct lyd_node *next, *node; |
| 571 | const struct lysc_node *snode = NULL; |
| 572 | |
| 573 | assert(first && (sparent || mod)); |
| 574 | |
| 575 | while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) { |
| 576 | /* check case duplicites */ |
| 577 | if (snode->nodetype == LYS_CHOICE) { |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 578 | LY_CHECK_RET(lyd_validate_cases(first, mod, (struct lysc_node_choice *)snode, diff)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | LY_LIST_FOR_SAFE(*first, next, node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 583 | if (mod && (lyd_owner_module(node) != mod)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 584 | /* all top-level data from this module checked */ |
| 585 | break; |
| 586 | } |
| 587 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 588 | if (!(node->flags & (LYD_NEW | LYD_DEFAULT))) { |
| 589 | /* check only new and default nodes */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 590 | continue; |
| 591 | } |
| 592 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 593 | LOG_LOCSET(node->schema, node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 594 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 595 | if (node->flags & LYD_NEW) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 596 | LY_ERR ret; |
| 597 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 598 | /* remove old default(s) of the new node if it exists */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 599 | lyd_validate_autodel_dup(first, node, mod, &next, diff); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 600 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 601 | /* then check new node instance duplicities */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 602 | ret = lyd_validate_duplicates(*first, node); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 603 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0), ret); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 604 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 605 | /* this node is valid */ |
| 606 | node->flags &= ~LYD_NEW; |
| 607 | } |
| 608 | |
Michal Vasko | 0e6de51 | 2021-01-11 13:39:44 +0100 | [diff] [blame] | 609 | LOG_LOCBACK(node->schema ? 1 : 0, 1, 0, 0); |
| 610 | |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 611 | if (node->flags & LYD_DEFAULT) { |
| 612 | /* remove leftover default nodes from a no-longer existing case */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 613 | lyd_validate_autodel_case_dflt(first, node, mod, &next, diff); |
Michal Vasko | dcacf2f | 2020-11-18 18:18:15 +0100 | [diff] [blame] | 614 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | return LY_SUCCESS; |
| 618 | } |
| 619 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 620 | /** |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 621 | * @brief Evaluate any "when" conditions of a non-existent data node with existing parent. |
| 622 | * |
| 623 | * @param[in] first First data sibling of the non-existing node. |
| 624 | * @param[in] parent Data parent of the non-existing node. |
| 625 | * @param[in] snode Schema node of the non-existing node. |
| 626 | * @param[out] disabled First when that evaluated false, if any. |
| 627 | * @return LY_ERR value. |
| 628 | */ |
| 629 | static LY_ERR |
| 630 | lyd_validate_dummy_when(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode, |
| 631 | const struct lysc_when **disabled) |
| 632 | { |
| 633 | LY_ERR ret = LY_SUCCESS; |
| 634 | struct lyd_node *tree, *dummy = NULL; |
| 635 | |
| 636 | /* find root */ |
| 637 | if (parent) { |
| 638 | tree = (struct lyd_node *)parent; |
| 639 | while (tree->parent) { |
| 640 | tree = lyd_parent(tree); |
| 641 | } |
| 642 | tree = lyd_first_sibling(tree); |
| 643 | } else { |
| 644 | assert(!first || !first->prev->next); |
| 645 | tree = (struct lyd_node *)first; |
| 646 | } |
| 647 | |
| 648 | /* create dummy opaque node */ |
| 649 | ret = lyd_new_opaq((struct lyd_node *)parent, snode->module->ctx, snode->name, NULL, snode->module->name, &dummy); |
| 650 | LY_CHECK_GOTO(ret, cleanup); |
| 651 | |
| 652 | /* connect it if needed */ |
| 653 | if (!parent) { |
| 654 | if (first) { |
| 655 | lyd_insert_sibling((struct lyd_node *)first, dummy, &tree); |
| 656 | } else { |
| 657 | assert(!tree); |
| 658 | tree = dummy; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* evaluate all when */ |
| 663 | ret = lyd_validate_node_when(tree, dummy, snode, disabled); |
| 664 | if (ret == LY_EINCOMPLETE) { |
| 665 | /* all other when must be resolved by now */ |
| 666 | LOGINT(snode->module->ctx); |
| 667 | ret = LY_EINT; |
| 668 | goto cleanup; |
| 669 | } else if (ret) { |
| 670 | /* error */ |
| 671 | goto cleanup; |
| 672 | } |
| 673 | |
| 674 | cleanup: |
| 675 | lyd_free_tree(dummy); |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | /** |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 680 | * @brief Validate mandatory node existence. |
| 681 | * |
| 682 | * @param[in] first First sibling to search in. |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 683 | * @param[in] parent Data parent. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 684 | * @param[in] snode Schema node to validate. |
| 685 | * @return LY_ERR value. |
| 686 | */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 687 | static LY_ERR |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 688 | lyd_validate_mandatory(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode) |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 689 | { |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 690 | const struct lysc_when *disabled; |
| 691 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 692 | if (snode->nodetype == LYS_CHOICE) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 693 | /* some data of a choice case exist */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 694 | if (lys_getnext_data(NULL, first, NULL, snode, NULL)) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 695 | return LY_SUCCESS; |
| 696 | } |
| 697 | } else { |
| 698 | assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 699 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 700 | if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 701 | /* data instance found */ |
| 702 | return LY_SUCCESS; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 706 | disabled = NULL; |
| 707 | if (lysc_has_when(snode)) { |
| 708 | /* if there are any when conditions, they must be true for a validation error */ |
| 709 | LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled)); |
| 710 | } |
| 711 | |
| 712 | if (!disabled) { |
| 713 | /* node instance not found */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 714 | LOGVAL(snode->module->ctx, LY_VCODE_NOMAND, snode->name); |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 715 | return LY_EVALID; |
| 716 | } |
| 717 | |
| 718 | return LY_SUCCESS; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 721 | /** |
| 722 | * @brief Validate min/max-elements constraints, if any. |
| 723 | * |
| 724 | * @param[in] first First sibling to search in. |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 725 | * @param[in] parent Data parent. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 726 | * @param[in] snode Schema node to validate. |
| 727 | * @param[in] min Minimum number of elements, 0 for no restriction. |
| 728 | * @param[in] max Max number of elements, 0 for no restriction. |
| 729 | * @return LY_ERR value. |
| 730 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 731 | static LY_ERR |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 732 | lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode, |
| 733 | uint32_t min, uint32_t max) |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 734 | { |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 735 | uint32_t count = 0; |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 736 | struct lyd_node *iter; |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 737 | const struct lysc_when *disabled; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 738 | ly_bool invalid_instance = 0; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 739 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 740 | assert(min || max); |
| 741 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 742 | LYD_LIST_FOR_INST(first, snode, iter) { |
| 743 | ++count; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 744 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 745 | if (min && (count == min)) { |
| 746 | /* satisfied */ |
| 747 | min = 0; |
| 748 | if (!max) { |
| 749 | /* nothing more to check */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 750 | break; |
| 751 | } |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 752 | } |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 753 | if (max && (count > max)) { |
| 754 | /* not satisifed */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 755 | LOG_LOCSET(NULL, iter, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 756 | invalid_instance = 1; |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 757 | break; |
| 758 | } |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 761 | if (min) { |
| 762 | assert(count < min); |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 763 | |
| 764 | disabled = NULL; |
| 765 | if (lysc_has_when(snode)) { |
| 766 | /* if there are any when conditions, they must be true for a validation error */ |
| 767 | LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled)); |
| 768 | } |
| 769 | |
| 770 | if (!disabled) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 771 | LOGVAL(snode->module->ctx, LY_VCODE_NOMIN, snode->name); |
| 772 | goto failure; |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 773 | } |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 774 | } else if (max && (count > max)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 775 | LOGVAL(snode->module->ctx, LY_VCODE_NOMAX, snode->name); |
| 776 | goto failure; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 779 | return LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 780 | |
| 781 | failure: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 782 | LOG_LOCBACK(0, invalid_instance, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 783 | return LY_EVALID; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 786 | /** |
| 787 | * @brief Find node referenced by a list unique statement. |
| 788 | * |
| 789 | * @param[in] uniq_leaf Unique leaf to find. |
| 790 | * @param[in] list List instance to use for the search. |
| 791 | * @return Found leaf, |
| 792 | * @return NULL if no leaf found. |
| 793 | */ |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 794 | static struct lyd_node * |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 795 | lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list) |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 796 | { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 797 | struct lyd_node *node; |
| 798 | const struct lysc_node *iter; |
| 799 | size_t depth = 0, i; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 800 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 801 | /* get leaf depth */ |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 802 | for (iter = &uniq_leaf->node; iter && (iter != list->schema); iter = lysc_data_parent(iter)) { |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 803 | ++depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 804 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 805 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 806 | node = (struct lyd_node *)list; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 807 | while (node && depth) { |
| 808 | /* find schema node with this depth */ |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 809 | for (i = depth - 1, iter = &uniq_leaf->node; i; iter = lysc_data_parent(iter)) { |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 810 | --i; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /* find iter instance in children */ |
| 814 | assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF)); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 815 | lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 816 | --depth; |
| 817 | } |
| 818 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 819 | return node; |
| 820 | } |
| 821 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 822 | /** |
| 823 | * @brief Callback for comparing 2 list unique leaf values. |
| 824 | * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 825 | * Implementation of ::values_equal_cb. |
| 826 | * |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 827 | * @param[in] cb_data 0 to compare all uniques, n to compare only n-th unique. |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 828 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 829 | static ly_bool |
| 830 | lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data) |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 831 | { |
| 832 | struct ly_ctx *ctx; |
| 833 | struct lysc_node_list *slist; |
| 834 | struct lyd_node *diter, *first, *second; |
| 835 | struct lyd_value *val1, *val2; |
| 836 | char *path1, *path2, *uniq_str, *ptr; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 837 | LY_ARRAY_COUNT_TYPE u, v, action; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 838 | |
| 839 | assert(val1_p && val2_p); |
| 840 | |
| 841 | first = *((struct lyd_node **)val1_p); |
| 842 | second = *((struct lyd_node **)val2_p); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 843 | action = (LY_ARRAY_COUNT_TYPE)cb_data; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 844 | |
| 845 | assert(first && (first->schema->nodetype == LYS_LIST)); |
| 846 | assert(second && (second->schema == first->schema)); |
| 847 | |
| 848 | ctx = first->schema->module->ctx; |
| 849 | |
| 850 | slist = (struct lysc_node_list *)first->schema; |
| 851 | |
| 852 | /* compare unique leaves */ |
| 853 | if (action > 0) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 854 | u = action - 1; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 855 | if (u < LY_ARRAY_COUNT(slist->uniques)) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 856 | goto uniquecheck; |
| 857 | } |
| 858 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 859 | LY_ARRAY_FOR(slist->uniques, u) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 860 | uniquecheck: |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 861 | LY_ARRAY_FOR(slist->uniques[u], v) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 862 | /* first */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 863 | diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 864 | if (diter) { |
| 865 | val1 = &((struct lyd_node_term *)diter)->value; |
| 866 | } else { |
| 867 | /* use default value */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 868 | val1 = slist->uniques[u][v]->dflt; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* second */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 872 | diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 873 | if (diter) { |
| 874 | val2 = &((struct lyd_node_term *)diter)->value; |
| 875 | } else { |
| 876 | /* use default value */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 877 | val2 = slist->uniques[u][v]->dflt; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) { |
| 881 | /* values differ or either one is not set */ |
| 882 | break; |
| 883 | } |
| 884 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 885 | if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 886 | /* all unique leafs are the same in this set, create this nice error */ |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 887 | path1 = lyd_path(first, LYD_PATH_STD, NULL, 0); |
| 888 | path2 = lyd_path(second, LYD_PATH_STD, NULL, 0); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 889 | |
| 890 | /* use buffer to rebuild the unique string */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 891 | #define UNIQ_BUF_SIZE 1024 |
| 892 | uniq_str = malloc(UNIQ_BUF_SIZE); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 893 | uniq_str[0] = '\0'; |
| 894 | ptr = uniq_str; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 895 | LY_ARRAY_FOR(slist->uniques[u], v) { |
| 896 | if (v) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 897 | strcpy(ptr, " "); |
| 898 | ++ptr; |
| 899 | } |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 900 | ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], &slist->node, LYSC_PATH_LOG, |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 901 | ptr, UNIQ_BUF_SIZE - (ptr - uniq_str)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 902 | if (!ptr) { |
| 903 | /* path will be incomplete, whatever */ |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | ptr += strlen(ptr); |
| 908 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 909 | LOG_LOCSET(NULL, second, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 910 | LOGVAL(ctx, LY_VCODE_NOUNIQ, uniq_str, path1, path2); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 911 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 912 | |
| 913 | free(path1); |
| 914 | free(path2); |
| 915 | free(uniq_str); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 916 | #undef UNIQ_BUF_SIZE |
| 917 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 918 | return 1; |
| 919 | } |
| 920 | |
| 921 | if (action > 0) { |
| 922 | /* done */ |
| 923 | return 0; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 930 | /** |
| 931 | * @brief Validate list unique leaves. |
| 932 | * |
| 933 | * @param[in] first First sibling to search in. |
| 934 | * @param[in] snode Schema node to validate. |
| 935 | * @param[in] uniques List unique arrays to validate. |
| 936 | * @return LY_ERR value. |
| 937 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 938 | static LY_ERR |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 939 | lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, const struct lysc_node_leaf ***uniques) |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 940 | { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 941 | const struct lyd_node *diter; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 942 | struct ly_set *set; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 943 | LY_ARRAY_COUNT_TYPE u, v, x = 0; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 944 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 945 | uint32_t hash, i, size = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 946 | ly_bool dynamic; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 947 | const char *str; |
| 948 | struct hash_table **uniqtables = NULL; |
| 949 | struct lyd_value *val; |
| 950 | struct ly_ctx *ctx = snode->module->ctx; |
| 951 | |
| 952 | assert(uniques); |
| 953 | |
| 954 | /* get all list instances */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 955 | LY_CHECK_RET(ly_set_new(&set)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 956 | LY_LIST_FOR(first, diter) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 957 | if (diter->schema == snode) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 958 | ret = ly_set_add(set, (void *)diter, 1, NULL); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 959 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 960 | } |
| 961 | } |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 962 | |
| 963 | if (set->count == 2) { |
| 964 | /* simple comparison */ |
| 965 | if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) { |
| 966 | /* instance duplication */ |
| 967 | ret = LY_EVALID; |
| 968 | goto cleanup; |
| 969 | } |
| 970 | } else if (set->count > 2) { |
| 971 | /* use hashes for comparison */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 972 | /* first, allocate the table, the size depends on number of items in the set, |
| 973 | * the following code detects number of upper zero bits in the items' counter value ... */ |
| 974 | for (i = (sizeof set->count * CHAR_BIT) - 1; i > 0; i--) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 975 | size = set->count << i; |
| 976 | size = size >> i; |
| 977 | if (size == set->count) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 978 | break; |
| 979 | } |
| 980 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 981 | LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 982 | /* ... and then we convert it to the position of the highest non-zero bit ... */ |
| 983 | i = (sizeof set->count * CHAR_BIT) - i; |
| 984 | /* ... and by using it to shift 1 to the left we get the closest sufficient hash table size */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 985 | size = 1 << i; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 986 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 987 | uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 988 | LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 989 | x = LY_ARRAY_COUNT(uniques); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 990 | for (v = 0; v < x; v++) { |
| 991 | uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0); |
| 992 | LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 993 | } |
| 994 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 995 | for (i = 0; i < set->count; i++) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 996 | /* loop for unique - get the hash for the instances */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 997 | for (u = 0; u < x; u++) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 998 | val = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 999 | for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1000 | diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1001 | if (diter) { |
| 1002 | val = &((struct lyd_node_term *)diter)->value; |
| 1003 | } else { |
| 1004 | /* use default value */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1005 | val = uniques[u][v]->dflt; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1006 | } |
| 1007 | if (!val) { |
| 1008 | /* unique item not present nor has default value */ |
| 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | /* get canonical string value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1013 | str = val->realtype->plugin->print(val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1014 | hash = dict_hash_multi(hash, str, strlen(str)); |
| 1015 | if (dynamic) { |
| 1016 | free((char *)str); |
| 1017 | } |
| 1018 | } |
| 1019 | if (!val) { |
| 1020 | /* skip this list instance since its unique set is incomplete */ |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | /* finish the hash value */ |
| 1025 | hash = dict_hash_multi(hash, NULL, 0); |
| 1026 | |
| 1027 | /* insert into the hashtable */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1028 | ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1029 | if (ret == LY_EEXIST) { |
| 1030 | /* instance duplication */ |
| 1031 | ret = LY_EVALID; |
| 1032 | } |
| 1033 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | cleanup: |
| 1039 | ly_set_free(set, NULL); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1040 | for (v = 0; v < x; v++) { |
| 1041 | if (!uniqtables[v]) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1042 | /* failed when allocating uniquetables[j], following j are not allocated */ |
| 1043 | break; |
| 1044 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1045 | lyht_free(uniqtables[v]); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1046 | } |
| 1047 | free(uniqtables); |
| 1048 | |
| 1049 | return ret; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1052 | /** |
| 1053 | * @brief Validate data siblings based on generic schema node restrictions, recursively for schema-only nodes. |
| 1054 | * |
| 1055 | * @param[in] first First sibling to search in. |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 1056 | * @param[in] parent Data parent. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1057 | * @param[in] sparent Schema parent of the nodes to check. |
| 1058 | * @param[in] mod Module of the nodes to check. |
| 1059 | * @param[in] val_opts Validation options, see @ref datavalidationoptions. |
| 1060 | * @param[in] op Operation being validated, if any. |
| 1061 | * @return LY_ERR value. |
| 1062 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1063 | static LY_ERR |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 1064 | lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lyd_node *parent, |
| 1065 | const struct lysc_node *sparent, const struct lysc_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op) |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1066 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1067 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1068 | const struct lysc_node *snode = NULL; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1069 | struct lysc_node_list *slist; |
Michal Vasko | d8958df | 2020-08-05 13:27:36 +0200 | [diff] [blame] | 1070 | struct lysc_node_leaflist *sllist; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1071 | uint32_t getnext_opts; |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1072 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1073 | getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (op == LYD_VALIDATE_OP_REPLY ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1074 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1075 | /* disabled nodes are skipped by lys_getnext */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1076 | while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1077 | if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 1078 | continue; |
| 1079 | } |
| 1080 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1081 | LOG_LOCSET(snode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1082 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1083 | /* check min-elements and max-elements */ |
Michal Vasko | d8958df | 2020-08-05 13:27:36 +0200 | [diff] [blame] | 1084 | if (snode->nodetype == LYS_LIST) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1085 | slist = (struct lysc_node_list *)snode; |
| 1086 | if (slist->min || slist->max) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1087 | ret = lyd_validate_minmax(first, parent, snode, slist->min, slist->max); |
| 1088 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1089 | } |
Michal Vasko | d8958df | 2020-08-05 13:27:36 +0200 | [diff] [blame] | 1090 | } else if (snode->nodetype == LYS_LEAFLIST) { |
| 1091 | sllist = (struct lysc_node_leaflist *)snode; |
| 1092 | if (sllist->min || sllist->max) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1093 | ret = lyd_validate_minmax(first, parent, snode, sllist->min, sllist->max); |
| 1094 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | d8958df | 2020-08-05 13:27:36 +0200 | [diff] [blame] | 1095 | } |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1096 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1097 | } else if (snode->flags & LYS_MAND_TRUE) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 1098 | /* check generic mandatory existence */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1099 | ret = lyd_validate_mandatory(first, parent, snode); |
| 1100 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* check unique */ |
| 1104 | if (snode->nodetype == LYS_LIST) { |
| 1105 | slist = (struct lysc_node_list *)snode; |
| 1106 | if (slist->uniques) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1107 | ret = lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques); |
| 1108 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1112 | if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1113 | /* go recursively for schema-only nodes */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1114 | ret = lyd_validate_siblings_schema_r(first, parent, snode, mod, val_opts, op); |
| 1115 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1116 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1117 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1118 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1121 | return LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1122 | |
| 1123 | error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1124 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1125 | return ret; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1128 | /** |
| 1129 | * @brief Validate obsolete nodes, only warnings are printed. |
| 1130 | * |
| 1131 | * @param[in] node Node to check. |
| 1132 | */ |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 1133 | static void |
| 1134 | lyd_validate_obsolete(const struct lyd_node *node) |
| 1135 | { |
| 1136 | const struct lysc_node *snode; |
| 1137 | |
| 1138 | snode = node->schema; |
| 1139 | do { |
| 1140 | if (snode->flags & LYS_STATUS_OBSLT) { |
| 1141 | LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name); |
| 1142 | break; |
| 1143 | } |
| 1144 | |
| 1145 | snode = snode->parent; |
| 1146 | } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE))); |
| 1147 | } |
| 1148 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1149 | /** |
| 1150 | * @brief Validate must conditions of a data node. |
| 1151 | * |
| 1152 | * @param[in] node Node to validate. |
| 1153 | * @param[in] op Operation being validated, if any. |
| 1154 | * @return LY_ERR value. |
| 1155 | */ |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1156 | static LY_ERR |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1157 | lyd_validate_must(const struct lyd_node *node, LYD_VALIDATE_OP op) |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1158 | { |
| 1159 | struct lyxp_set xp_set; |
| 1160 | struct lysc_must *musts; |
| 1161 | const struct lyd_node *tree; |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1162 | const struct lysc_node *schema; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1163 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1164 | |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1165 | if (node->schema->nodetype & (LYS_ACTION | LYS_RPC)) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1166 | if (op == LYD_VALIDATE_OP_RPC) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1167 | schema = &((struct lysc_node_action *)node->schema)->input.node; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1168 | } else if (op == LYD_VALIDATE_OP_REPLY) { |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1169 | schema = &((struct lysc_node_action *)node->schema)->output.node; |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1170 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1171 | LOGINT(LYD_CTX(node)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1172 | return LY_EINT; |
| 1173 | } |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1174 | } else { |
| 1175 | schema = node->schema; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1176 | } |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 1177 | musts = lysc_node_musts(schema); |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1178 | if (!musts) { |
| 1179 | /* no must to evaluate */ |
| 1180 | return LY_SUCCESS; |
| 1181 | } |
| 1182 | |
| 1183 | /* find first top-level node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1184 | for (tree = node; tree->parent; tree = lyd_parent(tree)) {} |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1185 | while (tree->prev->next) { |
| 1186 | tree = tree->prev; |
| 1187 | } |
| 1188 | |
| 1189 | LY_ARRAY_FOR(musts, u) { |
| 1190 | memset(&xp_set, 0, sizeof xp_set); |
| 1191 | |
| 1192 | /* evaluate must */ |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 1193 | LY_CHECK_RET(lyxp_eval(LYD_CTX(node), musts[u].cond, node->schema->module, LY_PREF_SCHEMA_RESOLVED, |
| 1194 | musts[u].prefixes, node, tree, &xp_set, LYXP_SCHEMA)); |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1195 | |
| 1196 | /* check the result */ |
| 1197 | lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1198 | if (!xp_set.val.bln) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1199 | LOGVAL(LYD_CTX(node), LY_VCODE_NOMUST, musts[u].cond->expr); |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1200 | return LY_EVALID; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | return LY_SUCCESS; |
| 1205 | } |
| 1206 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1207 | LY_ERR |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 1208 | lyd_validate_final_r(struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *sparent, |
| 1209 | const struct lys_module *mod, uint32_t val_opts, LYD_VALIDATE_OP op) |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1210 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1211 | const char *innode = NULL; |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 1212 | struct lyd_node *next = NULL, *node; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1213 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1214 | /* validate all restrictions of nodes themselves */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1215 | LY_LIST_FOR_SAFE(first, next, node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1216 | if (mod && (lyd_owner_module(node) != mod)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1217 | /* all top-level data from this module checked */ |
| 1218 | break; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1221 | LOG_LOCSET(node->schema, node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1222 | |
Michal Vasko | a8c6172 | 2020-03-27 16:59:32 +0100 | [diff] [blame] | 1223 | /* opaque data */ |
| 1224 | if (!node->schema) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1225 | LOGVAL(LYD_CTX(node), LYVE_DATA, "Opaque node \"%s\" found.", ((struct lyd_node_opaq *)node)->name.name); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1226 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | a8c6172 | 2020-03-27 16:59:32 +0100 | [diff] [blame] | 1227 | return LY_EVALID; |
| 1228 | } |
| 1229 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1230 | /* no state/input/output data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1231 | if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1232 | innode = "state"; |
| 1233 | goto invalid_node; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 1234 | } else if ((op == LYD_VALIDATE_OP_RPC) && (node->schema->flags & LYS_IS_OUTPUT)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1235 | innode = "output"; |
| 1236 | goto invalid_node; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 1237 | } else if ((op == LYD_VALIDATE_OP_REPLY) && (node->schema->flags & LYS_IS_INPUT)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1238 | innode = "input"; |
| 1239 | goto invalid_node; |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 1242 | /* obsolete data */ |
| 1243 | lyd_validate_obsolete(node); |
| 1244 | |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1245 | /* node's musts */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1246 | LY_CHECK_RET(lyd_validate_must(node, op)); |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1247 | |
Michal Vasko | 53d97a1 | 2020-11-05 17:39:10 +0100 | [diff] [blame] | 1248 | /* node value was checked by plugins */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1249 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1250 | LOG_LOCBACK(1, 1, 0, 0); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1251 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1252 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1253 | /* validate schema-based restrictions */ |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 1254 | LY_CHECK_RET(lyd_validate_siblings_schema_r(first, parent, sparent, mod ? mod->compiled : NULL, val_opts, op)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1255 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1256 | LY_LIST_FOR(first, node) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1257 | /* validate all children recursively */ |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 1258 | LY_CHECK_RET(lyd_validate_final_r(lyd_child(node), node, node->schema, NULL, val_opts, op)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1259 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1260 | /* set default for containers */ |
| 1261 | if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) { |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1262 | LY_LIST_FOR(lyd_child(node), next) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1263 | if (!(next->flags & LYD_DEFAULT)) { |
| 1264 | break; |
| 1265 | } |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1266 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1267 | if (!next) { |
| 1268 | node->flags |= LYD_DEFAULT; |
| 1269 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | return LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1274 | |
| 1275 | invalid_node: |
| 1276 | LOGVAL(LYD_CTX(node), LY_VCODE_INNODE, innode, node->schema->name); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1277 | LOG_LOCBACK(1, 1, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1278 | return LY_EVALID; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1281 | /** |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1282 | * @brief Validate the whole data subtree. |
| 1283 | * |
| 1284 | * @param[in] root Subtree root. |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 1285 | * @param[in,out] node_types Set for unres node types. |
| 1286 | * @param[in,out] meta_types Set for unres metadata types. |
| 1287 | * @param[in,out] node_when Set for nodes with when conditions. |
Michal Vasko | 29adfbe | 2020-12-08 17:12:03 +0100 | [diff] [blame] | 1288 | * @param[in] impl_opts Implicit options, see @ref implicitoptions. |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1289 | * @param[in,out] diff Validation diff. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1290 | * @return LY_ERR value. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1291 | */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1292 | static LY_ERR |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 1293 | lyd_validate_subtree(struct lyd_node *root, struct ly_set *node_types, struct ly_set *meta_types, |
Michal Vasko | 29adfbe | 2020-12-08 17:12:03 +0100 | [diff] [blame] | 1294 | struct ly_set *node_when, uint32_t impl_opts, struct lyd_node **diff) |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1295 | { |
| 1296 | const struct lyd_meta *meta; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1297 | struct lyd_node *node; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1298 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1299 | LYD_TREE_DFS_BEGIN(root, node) { |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1300 | LY_LIST_FOR(node->meta, meta) { |
| 1301 | if (((struct lyext_metadata *)meta->annotation->data)->type->plugin->validate) { |
| 1302 | /* metadata type resolution */ |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 1303 | LY_CHECK_RET(ly_set_add(meta_types, (void *)meta, 1, NULL)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1304 | } |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1305 | } |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1306 | |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1307 | if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) { |
| 1308 | /* node type resolution */ |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 1309 | LY_CHECK_RET(ly_set_add(node_types, (void *)node, 1, NULL)); |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1310 | } else if (node->schema->nodetype & LYD_NODE_INNER) { |
| 1311 | /* new node validation, autodelete */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1312 | LY_CHECK_RET(lyd_validate_new(lyd_node_children_p(node), node->schema, NULL, diff)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1313 | |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1314 | /* add nested defaults */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1315 | LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, NULL, NULL, impl_opts, diff)); |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1316 | } |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1317 | |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 1318 | if (lysc_node_when(node->schema)) { |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1319 | /* when evaluation */ |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 1320 | LY_CHECK_RET(ly_set_add(node_when, (void *)node, 1, NULL)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1321 | } |
| 1322 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1323 | LYD_TREE_DFS_END(root, node); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | return LY_SUCCESS; |
| 1327 | } |
| 1328 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1329 | /** |
| 1330 | * @brief Validate data tree. |
| 1331 | * |
| 1332 | * @param[in,out] tree Data tree to validate, nodes may be autodeleted. |
| 1333 | * @param[in] modules Array of modules to validate, NULL for all modules. |
| 1334 | * @param[in] mod_count Count of @p modules. |
| 1335 | * @param[in] ly_ctx libyang context. |
| 1336 | * @param[in] val_opts Validation options, see @ref datavalidationoptions. |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1337 | * @param[out] diff Generated validation diff, not generated if NULL. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1338 | * @return LY_ERR value. |
| 1339 | */ |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1340 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1341 | lyd_validate(struct lyd_node **tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t val_opts, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1342 | struct lyd_node **diff) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1343 | { |
| 1344 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 73e4721 | 2020-12-03 14:20:16 +0100 | [diff] [blame] | 1345 | struct lyd_node *first, *next, **first2, *iter; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1346 | const struct lys_module *mod; |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 1347 | struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1348 | uint32_t i = 0; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1349 | |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 1350 | LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || module, LY_EINVAL); |
Michal Vasko | 5a532f3 | 2020-12-10 12:18:47 +0100 | [diff] [blame] | 1351 | if (!ctx && !module) { |
| 1352 | ctx = LYD_CTX(*tree); |
| 1353 | } |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1354 | if (diff) { |
| 1355 | *diff = NULL; |
| 1356 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1357 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1358 | next = *tree; |
| 1359 | while (1) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1360 | if (val_opts & LYD_VALIDATE_PRESENT) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1361 | mod = lyd_data_next_module(&next, &first); |
| 1362 | } else { |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 1363 | mod = lyd_mod_next_module(next, module, ctx, &i, &first); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1364 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1365 | if (!mod) { |
| 1366 | break; |
| 1367 | } |
Michal Vasko | 7c4cf1e | 2020-06-22 10:04:30 +0200 | [diff] [blame] | 1368 | if (!first || (first == *tree)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1369 | /* make sure first2 changes are carried to tree */ |
| 1370 | first2 = tree; |
| 1371 | } else { |
| 1372 | first2 = &first; |
| 1373 | } |
| 1374 | |
| 1375 | /* validate new top-level nodes of this module, autodelete */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1376 | ret = lyd_validate_new(first2, NULL, mod, diff); |
| 1377 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1378 | |
Michal Vasko | 0275cf6 | 2020-11-05 17:40:30 +0100 | [diff] [blame] | 1379 | /* add all top-level defaults for this module, do not add into unres sets, will occur in the next step */ |
| 1380 | ret = lyd_new_implicit_r(NULL, first2, NULL, mod, NULL, NULL, val_opts & LYD_VALIDATE_NO_STATE ? |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1381 | LYD_IMPLICIT_NO_STATE : 0, diff); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1382 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1383 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 1384 | /* our first module node pointer may no longer be the first */ |
| 1385 | while (*first2 && (*first2)->prev->next && (lyd_owner_module(*first2) == lyd_owner_module((*first2)->prev))) { |
| 1386 | *first2 = (*first2)->prev; |
| 1387 | } |
| 1388 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1389 | /* process nested nodes */ |
Michal Vasko | 73e4721 | 2020-12-03 14:20:16 +0100 | [diff] [blame] | 1390 | LY_LIST_FOR(*first2, iter) { |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 1391 | ret = lyd_validate_subtree(iter, &node_types, &meta_types, &node_when, val_opts & LYD_VALIDATE_NO_STATE ? |
| 1392 | LYD_IMPLICIT_NO_STATE : 0, diff); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1393 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | /* finish incompletely validated terminal values/attributes and when conditions */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 1397 | ret = lyd_validate_unres(first2, mod, &node_when, &node_types, &meta_types, diff); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1398 | LY_CHECK_GOTO(ret, cleanup); |
| 1399 | |
| 1400 | /* perform final validation that assumes the data tree is final */ |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 1401 | ret = lyd_validate_final_r(*first2, NULL, NULL, mod, val_opts, 0); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1402 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1403 | } |
| 1404 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1405 | cleanup: |
Michal Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame] | 1406 | ly_set_erase(&node_types, NULL); |
| 1407 | ly_set_erase(&meta_types, NULL); |
| 1408 | ly_set_erase(&node_when, NULL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1409 | return ret; |
| 1410 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1411 | |
| 1412 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1413 | lyd_validate_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t val_opts, struct lyd_node **diff) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1414 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1415 | return lyd_validate(tree, NULL, ctx, val_opts, diff); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1419 | lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, uint32_t val_opts, struct lyd_node **diff) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1420 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1421 | return lyd_validate(tree, module, NULL, val_opts, diff); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1422 | } |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1423 | |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1424 | /** |
| 1425 | * @brief Find nodes for merging an operation into data tree for validation. |
| 1426 | * |
| 1427 | * @param[in] op_tree Full operation data tree. |
| 1428 | * @param[in] op_node Operation node itself. |
| 1429 | * @param[in] tree Data tree to be merged into. |
| 1430 | * @param[out] op_subtree Operation subtree to merge. |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1431 | * @param[out] tree_sibling Data tree sibling to merge next to, is set if @p tree_parent is NULL. |
| 1432 | * @param[out] tree_parent Data tree parent to merge into, is set if @p tree_sibling is NULL. |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1433 | */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1434 | static void |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1435 | lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree, |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1436 | struct lyd_node **op_subtree, struct lyd_node **tree_sibling, struct lyd_node **tree_parent) |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1437 | { |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1438 | const struct lyd_node *tree_iter, *op_iter; |
| 1439 | struct lyd_node *match; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1440 | uint32_t i, cur_depth, op_depth; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1441 | |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1442 | *op_subtree = NULL; |
| 1443 | *tree_sibling = NULL; |
| 1444 | *tree_parent = NULL; |
| 1445 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1446 | /* learn op depth (top-level being depth 0) */ |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1447 | op_depth = 0; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1448 | for (op_iter = op_node; op_iter != op_tree; op_iter = lyd_parent(op_iter)) { |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1449 | ++op_depth; |
| 1450 | } |
| 1451 | |
| 1452 | /* find where to merge op */ |
| 1453 | tree_iter = tree; |
| 1454 | cur_depth = op_depth; |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1455 | while (cur_depth && tree_iter) { |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1456 | /* find op iter in tree */ |
| 1457 | lyd_find_sibling_first(tree_iter, op_iter, &match); |
| 1458 | if (!match) { |
| 1459 | break; |
| 1460 | } |
| 1461 | |
| 1462 | /* move tree_iter */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1463 | tree_iter = lyd_child(match); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1464 | |
| 1465 | /* move depth */ |
| 1466 | --cur_depth; |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1467 | |
| 1468 | /* find next op parent */ |
| 1469 | op_iter = op_node; |
| 1470 | for (i = 0; i < cur_depth; ++i) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1471 | op_iter = lyd_parent(op_iter); |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1472 | } |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1473 | } |
| 1474 | |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1475 | assert(op_iter); |
Michal Vasko | bb84467 | 2020-07-03 11:06:12 +0200 | [diff] [blame] | 1476 | *op_subtree = (struct lyd_node *)op_iter; |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1477 | if (!tree || tree_iter) { |
| 1478 | /* there is no tree whatsoever or this is the last found sibling */ |
| 1479 | *tree_sibling = (struct lyd_node *)tree_iter; |
| 1480 | } else { |
| 1481 | /* matching parent was found but it has no children to insert next to */ |
| 1482 | assert(match); |
| 1483 | *tree_parent = match; |
| 1484 | } |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | API LY_ERR |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1488 | lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, LYD_VALIDATE_OP op, struct lyd_node **diff) |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1489 | { |
| 1490 | LY_ERR ret; |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1491 | struct lyd_node *tree_sibling, *tree_parent, *op_subtree, *op_node, *op_parent; |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1492 | struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0}; |
| 1493 | |
| 1494 | LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1495 | (op == LYD_VALIDATE_OP_NOTIF) || (op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY), LY_EINVAL); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1496 | if (diff) { |
| 1497 | *diff = NULL; |
| 1498 | } |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1499 | |
| 1500 | /* find the operation/notification */ |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1501 | LYD_TREE_DFS_BEGIN(op_tree, op_node) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1502 | if (((op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY)) && (op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1503 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1504 | } else if ((op == LYD_VALIDATE_OP_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) { |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1505 | break; |
| 1506 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1507 | LYD_TREE_DFS_END(op_tree, op_node); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1508 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1509 | if ((op == LYD_VALIDATE_OP_RPC) || (op == LYD_VALIDATE_OP_REPLY)) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1510 | if (!(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1511 | LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found."); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1512 | return LY_EINVAL; |
| 1513 | } |
| 1514 | } else { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1515 | if (op_node->schema->nodetype != LYS_NOTIF) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1516 | LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found."); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1517 | return LY_EINVAL; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /* move op_tree to top-level node */ |
| 1522 | while (op_tree->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1523 | op_tree = lyd_parent(op_tree); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /* merge op_tree into tree */ |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1527 | lyd_val_op_merge_find(op_tree, op_node, tree, &op_subtree, &tree_sibling, &tree_parent); |
| 1528 | op_parent = lyd_parent(op_subtree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1529 | lyd_unlink_tree(op_subtree); |
Michal Vasko | 2f03d22 | 2020-12-09 18:15:51 +0100 | [diff] [blame] | 1530 | lyd_insert_node(tree_parent, &tree_sibling, op_subtree); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1531 | if (!tree) { |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1532 | tree = tree_sibling; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1533 | } |
| 1534 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1535 | LOG_LOCSET(NULL, op_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1536 | |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1537 | /* prevalidate whole operation subtree */ |
Michal Vasko | 29adfbe | 2020-12-08 17:12:03 +0100 | [diff] [blame] | 1538 | LY_CHECK_GOTO(ret = lyd_validate_subtree(op_node, &type_check, &type_meta_check, &when_check, |
| 1539 | op == LYD_VALIDATE_OP_REPLY ? LYD_IMPLICIT_OUTPUT : 0, diff), cleanup); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1540 | |
| 1541 | /* finish incompletely validated terminal values/attributes and when conditions on the full tree */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 1542 | LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, NULL, &when_check, &type_check, &type_meta_check, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1543 | diff), cleanup); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1544 | |
| 1545 | /* perform final validation of the operation/notification */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1546 | lyd_validate_obsolete(op_node); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1547 | LY_CHECK_GOTO(ret = lyd_validate_must(op_node, op), cleanup); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1548 | |
| 1549 | /* final validation of all the descendants */ |
Michal Vasko | bd4db89 | 2020-11-23 16:58:20 +0100 | [diff] [blame] | 1550 | LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_child(op_node), op_node, op_node->schema, NULL, 0, op), cleanup); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1551 | |
| 1552 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1553 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1554 | /* restore operation tree */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1555 | lyd_unlink_tree(op_subtree); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1556 | if (op_parent) { |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1557 | lyd_insert_node(op_parent, NULL, op_subtree); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | ly_set_erase(&type_check, NULL); |
| 1561 | ly_set_erase(&type_meta_check, NULL); |
| 1562 | ly_set_erase(&when_check, NULL); |
| 1563 | return ret; |
| 1564 | } |