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 | */ |
| 14 | |
| 15 | #include <assert.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include <stdint.h> |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include <string.h> |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include "common.h" |
| 22 | #include "config.h" |
| 23 | #include "hash_table.h" |
| 24 | #include "log.h" |
| 25 | #include "plugins_types.h" |
| 26 | #include "set.h" |
| 27 | #include "tree.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 28 | #include "tree_data_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "tree_schema.h" |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 30 | #include "tree_schema_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "xpath.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 32 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 33 | static struct lyd_node * |
| 34 | lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast, |
| 35 | const struct lysc_node *parent, const struct lysc_module *module) |
| 36 | { |
| 37 | const struct lysc_node *siter = NULL; |
| 38 | struct lyd_node *match = NULL; |
| 39 | |
| 40 | assert(parent || module); |
| 41 | assert(!last || (slast && *slast)); |
| 42 | |
| 43 | if (slast) { |
| 44 | siter = *slast; |
| 45 | } |
| 46 | |
| 47 | if (last && last->next) { |
| 48 | /* find next data instance */ |
| 49 | lyd_find_sibling_next2(last->next, siter, NULL, 0, &match); |
| 50 | if (match) { |
| 51 | return match; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /* find next schema node data instance */ |
| 56 | while ((siter = lys_getnext(siter, parent, module, 0))) { |
| 57 | switch (siter->nodetype) { |
| 58 | case LYS_CONTAINER: |
| 59 | case LYS_ANYXML: |
| 60 | case LYS_ANYDATA: |
| 61 | case LYS_LEAF: |
| 62 | lyd_find_sibling_val(sibling, siter, NULL, 0, &match); |
| 63 | break; |
| 64 | case LYS_LIST: |
| 65 | case LYS_LEAFLIST: |
| 66 | lyd_find_sibling_next2(sibling, siter, NULL, 0, &match); |
| 67 | break; |
| 68 | default: |
| 69 | assert(0); |
| 70 | LOGINT(NULL); |
| 71 | } |
| 72 | |
| 73 | if (match) { |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (slast) { |
| 79 | *slast = siter; |
| 80 | } |
| 81 | return match; |
| 82 | } |
| 83 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 84 | /** |
| 85 | * @brief Evaluate a single "when" condition. |
| 86 | * |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 87 | * @param[in,out] tree Data tree, is updated if some nodes are autodeleted. |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 88 | * @param[in] node Node whose existence depends on this when. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 89 | * @param[in] when When to evaluate. |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 90 | * @return LY_ERR value (LY_EINCOMPLETE if a referenced node does not have its when evaluated) |
| 91 | */ |
| 92 | static LY_ERR |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 93 | lyd_validate_when(struct lyd_node **tree, struct lyd_node *node, struct lysc_when *when) |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 94 | { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 95 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 96 | const struct lyd_node *ctx_node; |
| 97 | struct lyxp_set xp_set; |
| 98 | |
| 99 | memset(&xp_set, 0, sizeof xp_set); |
| 100 | |
| 101 | if (when->context == node->schema) { |
| 102 | ctx_node = node; |
| 103 | } else { |
| 104 | assert((!when->context && !node->parent) || (when->context == node->parent->schema)); |
| 105 | ctx_node = (struct lyd_node *)node->parent; |
| 106 | } |
| 107 | |
| 108 | /* evaluate when */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 109 | ret = lyxp_eval(when->cond, LYD_SCHEMA, when->module, ctx_node, ctx_node ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 110 | *tree, &xp_set, LYXP_SCHEMA); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 111 | lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
| 112 | |
| 113 | /* return error or LY_EINCOMPLETE for dependant unresolved when */ |
| 114 | LY_CHECK_RET(ret); |
| 115 | |
| 116 | /* take action based on the result */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 117 | if (!xp_set.val.bln) { |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 118 | if (node->flags & LYD_WHEN_TRUE) { |
| 119 | /* autodelete */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 120 | if (LYD_DEL_IS_ROOT(*tree, node)) { |
| 121 | *tree = (*tree)->next; |
| 122 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 123 | lyd_free_tree(node); |
| 124 | } else { |
| 125 | /* invalid data */ |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 126 | LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOWHEN, when->cond->expr); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 127 | ret = LY_EVALID; |
| 128 | } |
| 129 | } else { |
| 130 | /* remember that when evaluated to true */ |
| 131 | node->flags |= LYD_WHEN_TRUE; |
| 132 | } |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 138 | lyd_validate_unres(struct lyd_node **tree, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *meta_types, |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 139 | LYD_FORMAT format, ly_clb_resolve_prefix get_prefix_clb, void *parser_data) |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 140 | { |
| 141 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 142 | uint32_t i; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 143 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 144 | if (node_when) { |
| 145 | /* evaluate all when conditions */ |
| 146 | uint32_t prev_count; |
| 147 | do { |
| 148 | prev_count = node_when->count; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 149 | i = 0; |
| 150 | while (i < node_when->count) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 151 | /* evaluate all when expressions that affect this node's existence */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 152 | struct lyd_node *node = (struct lyd_node *)node_when->objs[i]; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 153 | const struct lysc_node *schema = node->schema; |
| 154 | int unres_when = 0; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 155 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 156 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 157 | LY_ARRAY_SIZE_TYPE u; |
| 158 | LY_ARRAY_FOR(schema->when, u) { |
| 159 | ret = lyd_validate_when(tree, node, schema->when[u]); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 160 | if (ret) { |
| 161 | break; |
| 162 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 163 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 164 | if (ret == LY_EINCOMPLETE) { |
| 165 | /* could not evaluate this when */ |
| 166 | unres_when = 1; |
| 167 | break; |
| 168 | } else if (ret) { |
| 169 | /* error */ |
| 170 | return ret; |
| 171 | } |
| 172 | schema = schema->parent; |
| 173 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 174 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 175 | if (unres_when) { |
| 176 | /* keep in set and go to the next node */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 177 | ++i; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 178 | } else { |
| 179 | /* remove this node from the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 180 | ly_set_rm_index(node_when, i, NULL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 181 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 182 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 183 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 184 | /* there must have been some when conditions resolved */ |
| 185 | } while (prev_count > node_when->count); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 186 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 187 | /* there could have been no cyclic when dependencies, checked during compilation */ |
| 188 | assert(!node_when->count); |
| 189 | } |
| 190 | |
| 191 | if (node_types && node_types->count) { |
| 192 | /* 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] | 193 | i = node_types->count; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 194 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 195 | --i; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 196 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 197 | struct lyd_node_term *node = (struct lyd_node_term *)node_types->objs[i]; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 198 | |
| 199 | /* validate and store the value of the node */ |
| 200 | ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1, get_prefix_clb, |
| 201 | parser_data, format, *tree); |
| 202 | LY_CHECK_RET(ret); |
| 203 | |
| 204 | /* remove this node from the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 205 | ly_set_rm_index(node_types, i, NULL); |
| 206 | } while (i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 209 | if (meta_types && meta_types->count) { |
| 210 | /* ... and metadata values */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 211 | i = meta_types->count; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 212 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 213 | --i; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 214 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 215 | struct lyd_meta *meta = (struct lyd_meta *)meta_types->objs[i]; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 216 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 217 | /* validate and store the value of the metadata */ |
| 218 | ret = lyd_value_parse_meta(meta->parent->schema->module->ctx, meta, meta->value.original, |
| 219 | strlen(meta->value.original), 0, 1, get_prefix_clb, parser_data, format, NULL, *tree); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 220 | LY_CHECK_RET(ret); |
| 221 | |
| 222 | /* remove this attr from the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 223 | ly_set_rm_index(meta_types, i, NULL); |
| 224 | } while (i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 225 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 230 | static LY_ERR |
| 231 | 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] | 232 | { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 233 | struct lyd_node **match_p; |
| 234 | int fail = 0; |
| 235 | |
| 236 | if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (node->schema->flags & LYS_CONFIG_R)) { |
| 237 | /* duplicate instances allowed */ |
| 238 | return LY_SUCCESS; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 241 | /* find exactly the same next instance using hashes if possible */ |
| 242 | if (node->parent && node->parent->children_ht) { |
| 243 | if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) { |
| 244 | fail = 1; |
| 245 | } |
| 246 | } else { |
| 247 | for (; first; first = first->next) { |
| 248 | if (first == node) { |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) { |
| 253 | if (first->schema == node->schema) { |
| 254 | fail = 1; |
| 255 | break; |
| 256 | } |
| 257 | } else if (!lyd_compare(first, node, 0)) { |
| 258 | fail = 1; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (fail) { |
| 265 | LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_DUP, node->schema->name); |
| 266 | return LY_EVALID; |
| 267 | } |
| 268 | return LY_SUCCESS; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static LY_ERR |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 272 | lyd_validate_cases(struct lyd_node **first, const struct lysc_node_choice *choic) |
| 273 | { |
| 274 | const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL; |
| 275 | struct lyd_node *match, *to_del; |
| 276 | int found; |
| 277 | |
| 278 | LY_LIST_FOR((struct lysc_node *)choic->cases, scase) { |
| 279 | found = 0; |
| 280 | iter = NULL; |
| 281 | match = NULL; |
| 282 | while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) { |
| 283 | if (match->flags & LYD_NEW) { |
| 284 | /* a new case data found, nothing more to look for */ |
| 285 | found = 2; |
| 286 | break; |
| 287 | } else { |
| 288 | /* and old case data found */ |
| 289 | if (found == 0) { |
| 290 | found = 1; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (found == 1) { |
| 296 | /* there should not be 2 old cases */ |
| 297 | if (old_case) { |
| 298 | /* old data from 2 cases */ |
| 299 | LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, old_case->name, scase->name); |
| 300 | return LY_EVALID; |
| 301 | } |
| 302 | |
| 303 | /* remember an old existing case */ |
| 304 | old_case = scase; |
| 305 | } else if (found == 2) { |
| 306 | if (new_case) { |
| 307 | /* new data from 2 cases */ |
| 308 | LOGVAL(choic->module->ctx, LY_VLOG_LYSC, choic, LY_VCODE_DUPCASE, new_case->name, scase->name); |
| 309 | return LY_EVALID; |
| 310 | } |
| 311 | |
| 312 | /* remember a new existing case */ |
| 313 | new_case = scase; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (old_case && new_case) { |
| 318 | /* auto-delete old case */ |
| 319 | iter = NULL; |
| 320 | match = NULL; |
| 321 | to_del = NULL; |
| 322 | while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) { |
| 323 | if (LYD_DEL_IS_ROOT(*first, to_del)) { |
| 324 | *first = (*first)->next; |
| 325 | } |
| 326 | lyd_free_tree(to_del); |
| 327 | to_del = match; |
| 328 | } |
| 329 | if (LYD_DEL_IS_ROOT(*first, to_del)) { |
| 330 | *first = (*first)->next; |
| 331 | } |
| 332 | lyd_free_tree(to_del); |
| 333 | } |
| 334 | |
| 335 | return LY_SUCCESS; |
| 336 | } |
| 337 | |
| 338 | static int |
| 339 | lyd_val_has_default(const struct lysc_node *schema) |
| 340 | { |
| 341 | switch (schema->nodetype) { |
| 342 | case LYS_LEAF: |
| 343 | if (((struct lysc_node_leaf *)schema)->dflt) { |
| 344 | return 1; |
| 345 | } |
| 346 | break; |
| 347 | case LYS_LEAFLIST: |
| 348 | if (((struct lysc_node_leaflist *)schema)->dflts) { |
| 349 | return 1; |
| 350 | } |
| 351 | break; |
| 352 | case LYS_CONTAINER: |
| 353 | if (!(schema->flags & LYS_PRESENCE)) { |
| 354 | return 1; |
| 355 | } |
| 356 | break; |
| 357 | default: |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static void |
| 365 | lyd_validate_autodel_dup(struct lyd_node **first, struct lyd_node *node, struct lyd_node **next_p) |
| 366 | { |
| 367 | struct lyd_node *match, *next; |
| 368 | |
| 369 | if (lyd_val_has_default(node->schema)) { |
| 370 | assert(node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CONTAINER)); |
| 371 | if (node->schema->nodetype == LYS_LEAFLIST) { |
| 372 | lyd_find_sibling_next2(*first, node->schema, NULL, 0, &match); |
| 373 | } else { |
| 374 | lyd_find_sibling_val(*first, node->schema, NULL, 0, &match); |
| 375 | } |
| 376 | |
| 377 | while (match) { |
| 378 | next = match->next; |
| 379 | if ((match->flags & LYD_DEFAULT) && !(match->flags & LYD_NEW)) { |
| 380 | /* default instance found, remove it */ |
| 381 | if (LYD_DEL_IS_ROOT(*first, match)) { |
| 382 | *first = (*first)->next; |
| 383 | } |
| 384 | if (match == *next_p) { |
| 385 | *next_p = (*next_p)->next; |
| 386 | } |
| 387 | lyd_free_tree(match); |
| 388 | |
| 389 | /* remove only a single container/leaf default instance, if there are more, it is an error */ |
| 390 | if (node->schema->nodetype & (LYS_LEAF | LYS_CONTAINER)) { |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | lyd_find_sibling_next2(next, node->schema, NULL, 0, &match); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | LY_ERR |
| 401 | lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod) |
| 402 | { |
| 403 | struct lyd_node *next, *node; |
| 404 | const struct lysc_node *snode = NULL; |
| 405 | |
| 406 | assert(first && (sparent || mod)); |
| 407 | |
| 408 | while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) { |
| 409 | /* check case duplicites */ |
| 410 | if (snode->nodetype == LYS_CHOICE) { |
| 411 | LY_CHECK_RET(lyd_validate_cases(first, (struct lysc_node_choice *)snode)); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | LY_LIST_FOR_SAFE(*first, next, node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 416 | if (mod && (lyd_owner_module(node) != mod)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 417 | /* all top-level data from this module checked */ |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | if (!(node->flags & LYD_NEW)) { |
| 422 | /* check only new nodes */ |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | /* remove old default(s) if it exists */ |
| 427 | lyd_validate_autodel_dup(first, node, &next); |
| 428 | |
| 429 | /* then check new node instance duplicities */ |
| 430 | LY_CHECK_RET(lyd_validate_duplicates(*first, node)); |
| 431 | |
| 432 | /* this node is valid */ |
| 433 | node->flags &= ~LYD_NEW; |
| 434 | } |
| 435 | |
| 436 | return LY_SUCCESS; |
| 437 | } |
| 438 | |
| 439 | static LY_ERR |
| 440 | lyd_validate_mandatory(const struct lyd_node *first, const struct lysc_node *snode) |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 441 | { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 442 | if (snode->nodetype == LYS_CHOICE) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 443 | /* some data of a choice case exist */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 444 | if (lys_getnext_data(NULL, first, NULL, snode, NULL)) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 445 | return LY_SUCCESS; |
| 446 | } |
| 447 | } else { |
| 448 | assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 449 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 450 | if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 451 | /* data instance found */ |
| 452 | return LY_SUCCESS; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | /* node instance not found */ |
| 457 | LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAND, snode->name); |
| 458 | return LY_EVALID; |
| 459 | } |
| 460 | |
| 461 | static LY_ERR |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 462 | lyd_validate_minmax(const struct lyd_node *first, const struct lysc_node *snode, uint32_t min, uint32_t max) |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 463 | { |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 464 | uint32_t count = 0; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 465 | const struct lyd_node *iter; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 466 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 467 | assert(min || max); |
| 468 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 469 | LY_LIST_FOR(first, iter) { |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 470 | if (iter->schema == snode) { |
| 471 | ++count; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 472 | |
| 473 | if (min && (count == min)) { |
| 474 | /* satisfied */ |
| 475 | min = 0; |
| 476 | if (!max) { |
| 477 | /* nothing more to check */ |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | if (max && (count > max)) { |
| 482 | /* not satisifed */ |
| 483 | break; |
| 484 | } |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 488 | if (min) { |
| 489 | assert(count < min); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 490 | LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMIN, snode->name); |
| 491 | return LY_EVALID; |
| 492 | } else if (max && (count > max)) { |
| 493 | LOGVAL(snode->module->ctx, LY_VLOG_LYSC, snode, LY_VCODE_NOMAX, snode->name); |
| 494 | return LY_EVALID; |
| 495 | } |
| 496 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 497 | return LY_SUCCESS; |
| 498 | } |
| 499 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 500 | static struct lyd_node * |
| 501 | lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, struct lyd_node *list) |
| 502 | { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 503 | struct lyd_node *node; |
| 504 | const struct lysc_node *iter; |
| 505 | size_t depth = 0, i; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 506 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 507 | /* get leaf depth */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 508 | for (iter = (struct lysc_node *)uniq_leaf; iter && (iter != list->schema); iter = lysc_data_parent(iter)) { |
| 509 | ++depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 510 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 511 | |
| 512 | node = list; |
| 513 | while (node && depth) { |
| 514 | /* find schema node with this depth */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 515 | for (i = depth - 1, iter = (struct lysc_node *)uniq_leaf; i; iter = lysc_data_parent(iter)) { |
| 516 | --i; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* find iter instance in children */ |
| 520 | assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF)); |
| 521 | lyd_find_sibling_val(lyd_node_children(node), iter, NULL, 0, &node); |
| 522 | --depth; |
| 523 | } |
| 524 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 525 | return node; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * actions (cb_data): |
| 530 | * 0 - compare all uniques |
| 531 | * n - compare n-th unique |
| 532 | */ |
| 533 | static int |
| 534 | lyd_val_uniq_list_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *cb_data) |
| 535 | { |
| 536 | struct ly_ctx *ctx; |
| 537 | struct lysc_node_list *slist; |
| 538 | struct lyd_node *diter, *first, *second; |
| 539 | struct lyd_value *val1, *val2; |
| 540 | char *path1, *path2, *uniq_str, *ptr; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 541 | LY_ARRAY_SIZE_TYPE u, v, action; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 542 | |
| 543 | assert(val1_p && val2_p); |
| 544 | |
| 545 | first = *((struct lyd_node **)val1_p); |
| 546 | second = *((struct lyd_node **)val2_p); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 547 | action = (LY_ARRAY_SIZE_TYPE)cb_data; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 548 | |
| 549 | assert(first && (first->schema->nodetype == LYS_LIST)); |
| 550 | assert(second && (second->schema == first->schema)); |
| 551 | |
| 552 | ctx = first->schema->module->ctx; |
| 553 | |
| 554 | slist = (struct lysc_node_list *)first->schema; |
| 555 | |
| 556 | /* compare unique leaves */ |
| 557 | if (action > 0) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 558 | u = action - 1; |
| 559 | if (u < LY_ARRAY_SIZE(slist->uniques)) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 560 | goto uniquecheck; |
| 561 | } |
| 562 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 563 | LY_ARRAY_FOR(slist->uniques, u) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 564 | uniquecheck: |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 565 | LY_ARRAY_FOR(slist->uniques[u], v) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 566 | /* first */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 567 | diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 568 | if (diter) { |
| 569 | val1 = &((struct lyd_node_term *)diter)->value; |
| 570 | } else { |
| 571 | /* use default value */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 572 | val1 = slist->uniques[u][v]->dflt; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* second */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 576 | diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 577 | if (diter) { |
| 578 | val2 = &((struct lyd_node_term *)diter)->value; |
| 579 | } else { |
| 580 | /* use default value */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 581 | val2 = slist->uniques[u][v]->dflt; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | if (!val1 || !val2 || val1->realtype->plugin->compare(val1, val2)) { |
| 585 | /* values differ or either one is not set */ |
| 586 | break; |
| 587 | } |
| 588 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 589 | if (v && (v == LY_ARRAY_SIZE(slist->uniques[u]))) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 590 | /* all unique leafs are the same in this set, create this nice error */ |
| 591 | path1 = lyd_path(first, LYD_PATH_LOG, NULL, 0); |
| 592 | path2 = lyd_path(second, LYD_PATH_LOG, NULL, 0); |
| 593 | |
| 594 | /* use buffer to rebuild the unique string */ |
| 595 | uniq_str = malloc(1024); |
| 596 | uniq_str[0] = '\0'; |
| 597 | ptr = uniq_str; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 598 | LY_ARRAY_FOR(slist->uniques[u], v) { |
| 599 | if (v) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 600 | strcpy(ptr, " "); |
| 601 | ++ptr; |
| 602 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 603 | ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], (struct lysc_node *)slist, LYSC_PATH_LOG, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 604 | ptr, 1024 - (ptr - uniq_str)); |
| 605 | if (!ptr) { |
| 606 | /* path will be incomplete, whatever */ |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | ptr += strlen(ptr); |
| 611 | } |
| 612 | LOGVAL(ctx, LY_VLOG_LYD, second, LY_VCODE_NOUNIQ, uniq_str, path1, path2); |
| 613 | |
| 614 | free(path1); |
| 615 | free(path2); |
| 616 | free(uniq_str); |
| 617 | return 1; |
| 618 | } |
| 619 | |
| 620 | if (action > 0) { |
| 621 | /* done */ |
| 622 | return 0; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 629 | static LY_ERR |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 630 | lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, struct lysc_node_leaf ***uniques) |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 631 | { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 632 | const struct lyd_node *diter; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 633 | struct ly_set *set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 634 | LY_ARRAY_SIZE_TYPE u, v, x = 0; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 635 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 636 | uint32_t hash, i, size = 0; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 637 | int dynamic; |
| 638 | const char *str; |
| 639 | struct hash_table **uniqtables = NULL; |
| 640 | struct lyd_value *val; |
| 641 | struct ly_ctx *ctx = snode->module->ctx; |
| 642 | |
| 643 | assert(uniques); |
| 644 | |
| 645 | /* get all list instances */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 646 | set = ly_set_new(); |
| 647 | LY_CHECK_ERR_RET(!set, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 648 | LY_LIST_FOR(first, diter) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 649 | if (diter->schema == snode) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 650 | ly_set_add(set, (void *)diter, LY_SET_OPT_USEASLIST); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 651 | } |
| 652 | } |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 653 | |
| 654 | if (set->count == 2) { |
| 655 | /* simple comparison */ |
| 656 | if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, (void *)0)) { |
| 657 | /* instance duplication */ |
| 658 | ret = LY_EVALID; |
| 659 | goto cleanup; |
| 660 | } |
| 661 | } else if (set->count > 2) { |
| 662 | /* use hashes for comparison */ |
| 663 | /* first, allocate the table, the size depends on number of items in the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 664 | for (i = 31; i > 0; i--) { |
| 665 | size = set->count << i; |
| 666 | size = size >> i; |
| 667 | if (size == set->count) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 668 | break; |
| 669 | } |
| 670 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 671 | LY_CHECK_ERR_GOTO(!i, LOGINT(ctx); ret = LY_EINT, cleanup); |
| 672 | i = 32 - i; |
| 673 | size = 1 << i; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 674 | |
| 675 | uniqtables = malloc(LY_ARRAY_SIZE(uniques) * sizeof *uniqtables); |
| 676 | LY_CHECK_ERR_GOTO(!uniqtables, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 677 | x = LY_ARRAY_SIZE(uniques); |
| 678 | for (v = 0; v < x; v++) { |
| 679 | uniqtables[v] = lyht_new(size, sizeof(struct lyd_node *), lyd_val_uniq_list_equal, (void *)(v + 1L), 0); |
| 680 | LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 683 | for (i = 0; i < set->count; i++) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 684 | /* loop for unique - get the hash for the instances */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 685 | for (u = 0; u < x; u++) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 686 | val = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 687 | for (v = hash = 0; v < LY_ARRAY_SIZE(uniques[u]); v++) { |
| 688 | diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 689 | if (diter) { |
| 690 | val = &((struct lyd_node_term *)diter)->value; |
| 691 | } else { |
| 692 | /* use default value */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 693 | val = uniques[u][v]->dflt; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 694 | } |
| 695 | if (!val) { |
| 696 | /* unique item not present nor has default value */ |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | /* get canonical string value */ |
| 701 | str = val->realtype->plugin->print(val, LYD_JSON, json_print_get_prefix, NULL, &dynamic); |
| 702 | hash = dict_hash_multi(hash, str, strlen(str)); |
| 703 | if (dynamic) { |
| 704 | free((char *)str); |
| 705 | } |
| 706 | } |
| 707 | if (!val) { |
| 708 | /* skip this list instance since its unique set is incomplete */ |
| 709 | continue; |
| 710 | } |
| 711 | |
| 712 | /* finish the hash value */ |
| 713 | hash = dict_hash_multi(hash, NULL, 0); |
| 714 | |
| 715 | /* insert into the hashtable */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 716 | ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 717 | if (ret == LY_EEXIST) { |
| 718 | /* instance duplication */ |
| 719 | ret = LY_EVALID; |
| 720 | } |
| 721 | LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | cleanup: |
| 727 | ly_set_free(set, NULL); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 728 | for (v = 0; v < x; v++) { |
| 729 | if (!uniqtables[v]) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 730 | /* failed when allocating uniquetables[j], following j are not allocated */ |
| 731 | break; |
| 732 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 733 | lyht_free(uniqtables[v]); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 734 | } |
| 735 | free(uniqtables); |
| 736 | |
| 737 | return ret; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static LY_ERR |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 741 | lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lysc_node *sparent, |
| 742 | const struct lysc_module *mod, int val_opts) |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 743 | { |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 744 | const struct lysc_node *snode = NULL; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 745 | struct lysc_node_list *slist; |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 746 | int getnext_opts; |
| 747 | |
| 748 | getnext_opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | (val_opts & LYD_VALOPT_OUTPUT ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 749 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 750 | /* disabled nodes are skipped by lys_getnext */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 751 | while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) { |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 752 | if ((val_opts & LYD_VALOPT_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
| 753 | continue; |
| 754 | } |
| 755 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 756 | /* check min-elements and max-elements */ |
| 757 | if (snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 758 | slist = (struct lysc_node_list *)snode; |
| 759 | if (slist->min || slist->max) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 760 | LY_CHECK_RET(lyd_validate_minmax(first, snode, slist->min, slist->max)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 761 | } |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 762 | |
| 763 | /* check generic mandatory existence */ |
| 764 | } else if (snode->flags & LYS_MAND_TRUE) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 765 | LY_CHECK_RET(lyd_validate_mandatory(first, snode)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* check unique */ |
| 769 | if (snode->nodetype == LYS_LIST) { |
| 770 | slist = (struct lysc_node_list *)snode; |
| 771 | if (slist->uniques) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 772 | LY_CHECK_RET(lyd_validate_unique(first, snode, slist->uniques)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 776 | if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 777 | /* go recursively for schema-only nodes */ |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 778 | LY_CHECK_RET(lyd_validate_siblings_schema_r(first, snode, mod, val_opts)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 779 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 780 | } |
| 781 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 782 | return LY_SUCCESS; |
| 783 | } |
| 784 | |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 785 | static void |
| 786 | lyd_validate_obsolete(const struct lyd_node *node) |
| 787 | { |
| 788 | const struct lysc_node *snode; |
| 789 | |
| 790 | snode = node->schema; |
| 791 | do { |
| 792 | if (snode->flags & LYS_STATUS_OBSLT) { |
| 793 | LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name); |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | snode = snode->parent; |
| 798 | } while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE))); |
| 799 | } |
| 800 | |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 801 | static LY_ERR |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 802 | lyd_validate_must(const struct lyd_node *node, int val_opts) |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 803 | { |
| 804 | struct lyxp_set xp_set; |
| 805 | struct lysc_must *musts; |
| 806 | const struct lyd_node *tree; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 807 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 808 | |
| 809 | switch (node->schema->nodetype) { |
| 810 | case LYS_CONTAINER: |
| 811 | musts = ((struct lysc_node_container *)node->schema)->musts; |
| 812 | break; |
| 813 | case LYS_LEAF: |
| 814 | musts = ((struct lysc_node_leaf *)node->schema)->musts; |
| 815 | break; |
| 816 | case LYS_LEAFLIST: |
| 817 | musts = ((struct lysc_node_leaflist *)node->schema)->musts; |
| 818 | break; |
| 819 | case LYS_LIST: |
| 820 | musts = ((struct lysc_node_list *)node->schema)->musts; |
| 821 | break; |
| 822 | case LYS_ANYXML: |
| 823 | case LYS_ANYDATA: |
| 824 | musts = ((struct lysc_node_anydata *)node->schema)->musts; |
| 825 | break; |
| 826 | case LYS_NOTIF: |
| 827 | musts = ((struct lysc_notif *)node->schema)->musts; |
| 828 | break; |
| 829 | case LYS_RPC: |
| 830 | case LYS_ACTION: |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 831 | if (val_opts & LYD_VALOPT_INPUT) { |
| 832 | musts = ((struct lysc_action *)node->schema)->input.musts; |
| 833 | } else if (val_opts & LYD_VALOPT_OUTPUT) { |
| 834 | musts = ((struct lysc_action *)node->schema)->output.musts; |
| 835 | } else { |
| 836 | LOGINT(LYD_NODE_CTX(node)); |
| 837 | return LY_EINT; |
| 838 | } |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 839 | break; |
| 840 | default: |
| 841 | LOGINT(LYD_NODE_CTX(node)); |
| 842 | return LY_EINT; |
| 843 | } |
| 844 | |
| 845 | if (!musts) { |
| 846 | /* no must to evaluate */ |
| 847 | return LY_SUCCESS; |
| 848 | } |
| 849 | |
| 850 | /* find first top-level node */ |
| 851 | for (tree = node; tree->parent; tree = (struct lyd_node *)tree->parent); |
| 852 | while (tree->prev->next) { |
| 853 | tree = tree->prev; |
| 854 | } |
| 855 | |
| 856 | LY_ARRAY_FOR(musts, u) { |
| 857 | memset(&xp_set, 0, sizeof xp_set); |
| 858 | |
| 859 | /* evaluate must */ |
| 860 | LY_CHECK_RET(lyxp_eval(musts[u].cond, LYD_SCHEMA, musts[u].module, node, LYXP_NODE_ELEM, tree, &xp_set, LYXP_SCHEMA)); |
| 861 | |
| 862 | /* check the result */ |
| 863 | lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 864 | if (!xp_set.val.bln) { |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 865 | LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOMUST, musts[u].cond->expr); |
| 866 | return LY_EVALID; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | return LY_SUCCESS; |
| 871 | } |
| 872 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 873 | LY_ERR |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 874 | lyd_validate_final_r(struct lyd_node *first, const struct lysc_node *sparent, const struct lys_module *mod, int val_opts) |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 875 | { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 876 | struct lyd_node *next, *node; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 877 | const struct lysc_node *snode; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 878 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 879 | /* validate all restrictions of nodes themselves */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 880 | LY_LIST_FOR_SAFE(first, next, node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 881 | if (mod && (lyd_owner_module(node) != mod)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 882 | /* all top-level data from this module checked */ |
| 883 | break; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 884 | } |
| 885 | |
Michal Vasko | a8c6172 | 2020-03-27 16:59:32 +0100 | [diff] [blame] | 886 | /* opaque data */ |
| 887 | if (!node->schema) { |
| 888 | LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LYVE_DATA, "Opaque node \"%s\" found.", |
| 889 | ((struct lyd_node_opaq *)node)->name); |
| 890 | return LY_EVALID; |
| 891 | } |
| 892 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 893 | /* no state/input/output data */ |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 894 | if ((val_opts & LYD_VALOPT_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 895 | LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "state", node->schema->name); |
| 896 | return LY_EVALID; |
| 897 | } else if ((val_opts & LYD_VALOPT_INPUT) && (node->schema->flags & LYS_CONFIG_R)) { |
| 898 | LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "output", node->schema->name); |
| 899 | return LY_EVALID; |
| 900 | } else if ((val_opts & LYD_VALOPT_OUTPUT) && (node->schema->flags & LYS_CONFIG_W)) { |
| 901 | LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_INNODE, "input", node->schema->name); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 902 | return LY_EVALID; |
| 903 | } |
| 904 | |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 905 | /* obsolete data */ |
| 906 | lyd_validate_obsolete(node); |
| 907 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 908 | /* node's schema if-features */ |
| 909 | if ((snode = lysc_node_is_disabled(node->schema, 1))) { |
Michal Vasko | a8c6172 | 2020-03-27 16:59:32 +0100 | [diff] [blame] | 910 | LOGVAL(LYD_NODE_CTX(node), LY_VLOG_LYD, node, LY_VCODE_NOIFF, snode->name); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 911 | return LY_EVALID; |
| 912 | } |
| 913 | |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 914 | /* node's musts */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 915 | LY_CHECK_RET(lyd_validate_must(node, val_opts)); |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 916 | |
Michal Vasko | a8c6172 | 2020-03-27 16:59:32 +0100 | [diff] [blame] | 917 | /* node value including if-feature was checked by plugins */ |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 918 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 919 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 920 | /* validate schema-based restrictions */ |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 921 | LY_CHECK_RET(lyd_validate_siblings_schema_r(first, sparent, mod ? mod->compiled : NULL, val_opts)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 922 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 923 | LY_LIST_FOR(first, node) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 924 | /* validate all children recursively */ |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 925 | LY_CHECK_RET(lyd_validate_final_r(lyd_node_children(node), node->schema, NULL, val_opts)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 926 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 927 | /* set default for containers */ |
| 928 | if ((node->schema->nodetype == LYS_CONTAINER) && !(node->schema->flags & LYS_PRESENCE)) { |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 929 | LY_LIST_FOR(lyd_node_children(node), next) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 930 | if (!(next->flags & LYD_DEFAULT)) { |
| 931 | break; |
| 932 | } |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 933 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 934 | if (!next) { |
| 935 | node->flags |= LYD_DEFAULT; |
| 936 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
| 940 | return LY_SUCCESS; |
| 941 | } |
| 942 | |
| 943 | LY_ERR |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 944 | lyd_validate_defaults_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent, |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 945 | const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int val_opts) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 946 | { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 947 | LY_ERR ret; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 948 | const struct lysc_node *iter = NULL; |
| 949 | struct lyd_node *node; |
| 950 | struct lyd_value **dflts; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 951 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 952 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 953 | assert(first && (parent || sparent || mod) && node_types && node_when); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 954 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 955 | if (!sparent && parent) { |
| 956 | sparent = parent->schema; |
| 957 | } |
| 958 | |
| 959 | while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) { |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 960 | if ((val_opts & LYD_VALOPT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) { |
| 961 | continue; |
| 962 | } |
| 963 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 964 | switch (iter->nodetype) { |
| 965 | case LYS_CHOICE: |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 966 | if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 967 | /* create default case data */ |
| 968 | LY_CHECK_RET(lyd_validate_defaults_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt, |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 969 | NULL, node_types, node_when, val_opts)); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 970 | } |
| 971 | break; |
| 972 | case LYS_CONTAINER: |
| 973 | if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 974 | /* create default NP container */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 975 | LY_CHECK_RET(lyd_create_inner(iter, &node)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 976 | node->flags = LYD_DEFAULT; |
| 977 | lyd_insert_node(parent, first, node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 978 | |
| 979 | if (iter->when) { |
| 980 | /* remember to resolve when */ |
| 981 | ly_set_add(node_when, node, LY_SET_OPT_USEASLIST); |
| 982 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 983 | |
| 984 | /* create any default children */ |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 985 | LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when, val_opts)); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 986 | } |
| 987 | break; |
| 988 | case LYS_LEAF: |
| 989 | if (((struct lysc_node_leaf *)iter)->dflt && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 990 | /* create default leaf */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 991 | ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node); |
| 992 | if (ret == LY_EINCOMPLETE) { |
| 993 | /* remember to resolve type */ |
| 994 | ly_set_add(node_types, node, LY_SET_OPT_USEASLIST); |
| 995 | } else if (ret) { |
| 996 | return ret; |
| 997 | } |
| 998 | node->flags = LYD_DEFAULT; |
| 999 | lyd_insert_node(parent, first, node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1000 | |
| 1001 | if (iter->when) { |
| 1002 | /* remember to resolve when */ |
| 1003 | ly_set_add(node_when, node, LY_SET_OPT_USEASLIST); |
| 1004 | } |
| 1005 | } |
| 1006 | break; |
| 1007 | case LYS_LEAFLIST: |
| 1008 | if (((struct lysc_node_leaflist *)iter)->dflts && lyd_find_sibling_next2(*first, iter, NULL, 0, NULL)) { |
| 1009 | /* create all default leaf-lists */ |
| 1010 | dflts = ((struct lysc_node_leaflist *)iter)->dflts; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1011 | LY_ARRAY_FOR(dflts, u) { |
| 1012 | ret = lyd_create_term2(iter, dflts[u], &node); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1013 | if (ret == LY_EINCOMPLETE) { |
| 1014 | /* remember to resolve type */ |
| 1015 | ly_set_add(node_types, node, LY_SET_OPT_USEASLIST); |
| 1016 | } else if (ret) { |
| 1017 | return ret; |
| 1018 | } |
| 1019 | node->flags = LYD_DEFAULT; |
| 1020 | lyd_insert_node(parent, first, node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1021 | |
| 1022 | if (iter->when) { |
| 1023 | /* remember to resolve when */ |
| 1024 | ly_set_add(node_when, node, LY_SET_OPT_USEASLIST); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | break; |
| 1029 | default: |
| 1030 | /* without defaults */ |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | return LY_SUCCESS; |
| 1036 | } |
| 1037 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1038 | static LY_ERR |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1039 | lyd_validate_subtree(struct lyd_node *root, struct ly_set *type_check, struct ly_set *type_meta_check, |
| 1040 | struct ly_set *when_check, int val_opts) |
| 1041 | { |
| 1042 | const struct lyd_meta *meta; |
| 1043 | struct lyd_node *next, *node; |
| 1044 | |
| 1045 | LYD_TREE_DFS_BEGIN(root, next, node) { |
| 1046 | /* skip added default nodes */ |
| 1047 | if ((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) { |
| 1048 | LY_LIST_FOR(node->meta, meta) { |
| 1049 | /* metadata type resolution */ |
| 1050 | ly_set_add(type_meta_check, (void *)meta, LY_SET_OPT_USEASLIST); |
| 1051 | } |
| 1052 | |
| 1053 | if (node->schema->nodetype & LYD_NODE_TERM) { |
| 1054 | /* node type resolution */ |
| 1055 | ly_set_add(type_check, (void *)node, LY_SET_OPT_USEASLIST); |
| 1056 | } else if (node->schema->nodetype & LYD_NODE_INNER) { |
| 1057 | /* new node validation, autodelete */ |
| 1058 | LY_CHECK_RET(lyd_validate_new(lyd_node_children_p((struct lyd_node *)node), node->schema, NULL)); |
| 1059 | |
| 1060 | /* add nested defaults */ |
| 1061 | LY_CHECK_RET(lyd_validate_defaults_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, type_check, |
| 1062 | when_check, val_opts)); |
| 1063 | } |
| 1064 | |
| 1065 | if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) { |
| 1066 | /* when evaluation */ |
| 1067 | ly_set_add(when_check, (void *)node, LY_SET_OPT_USEASLIST); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | LYD_TREE_DFS_END(root, next, node); |
| 1072 | } |
| 1073 | |
| 1074 | return LY_SUCCESS; |
| 1075 | } |
| 1076 | |
| 1077 | static LY_ERR |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1078 | _lyd_validate(struct lyd_node **tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx, |
| 1079 | int val_opts) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1080 | { |
| 1081 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1082 | struct lyd_node *first, *next, **first2; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1083 | const struct lys_module *mod; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1084 | struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0}; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1085 | uint32_t i = 0; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1086 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1087 | LY_CHECK_ARG_RET(NULL, tree, *tree || ctx || (modules && mod_count), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1088 | |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1089 | if (val_opts & ~LYD_VALOPT_MASK) { |
| 1090 | LOGERR(ctx, LY_EINVAL, "Some invalid flags passed to validation."); |
| 1091 | return LY_EINVAL; |
| 1092 | } |
| 1093 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1094 | next = *tree; |
| 1095 | while (1) { |
| 1096 | if (val_opts & LYD_VALOPT_DATA_ONLY) { |
| 1097 | mod = lyd_data_next_module(&next, &first); |
| 1098 | } else { |
| 1099 | mod = lyd_mod_next_module(next, modules, mod_count, ctx, &i, &first); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1100 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1101 | if (!mod) { |
| 1102 | break; |
| 1103 | } |
Michal Vasko | 7c4cf1e | 2020-06-22 10:04:30 +0200 | [diff] [blame] | 1104 | if (!first || (first == *tree)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1105 | /* make sure first2 changes are carried to tree */ |
| 1106 | first2 = tree; |
| 1107 | } else { |
| 1108 | first2 = &first; |
| 1109 | } |
| 1110 | |
| 1111 | /* validate new top-level nodes of this module, autodelete */ |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1112 | LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod), cleanup); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1113 | |
| 1114 | /* add all top-level defaults for this module */ |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1115 | LY_CHECK_GOTO(ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &type_check, &when_check, val_opts), cleanup); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1116 | |
| 1117 | /* process nested nodes */ |
| 1118 | LY_LIST_FOR(*first2, first) { |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1119 | LY_CHECK_GOTO(ret = lyd_validate_subtree(first, &type_check, &type_meta_check, &when_check, val_opts), cleanup); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /* finish incompletely validated terminal values/attributes and when conditions */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1123 | ret = lyd_validate_unres(tree, &when_check, &type_check, &type_meta_check, LYD_JSON, lydjson_resolve_prefix, NULL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1124 | LY_CHECK_GOTO(ret, cleanup); |
| 1125 | |
| 1126 | /* perform final validation that assumes the data tree is final */ |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1127 | LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, val_opts), cleanup); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1130 | cleanup: |
| 1131 | ly_set_erase(&type_check, NULL); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1132 | ly_set_erase(&type_meta_check, NULL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1133 | ly_set_erase(&when_check, NULL); |
| 1134 | return ret; |
| 1135 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1136 | |
| 1137 | API LY_ERR |
| 1138 | lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts) |
| 1139 | { |
| 1140 | return _lyd_validate(tree, NULL, 0, ctx, val_opts); |
| 1141 | } |
| 1142 | |
| 1143 | API LY_ERR |
| 1144 | lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts) |
| 1145 | { |
| 1146 | return _lyd_validate(tree, modules, mod_count, NULL, val_opts); |
| 1147 | } |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1148 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1149 | static void |
| 1150 | lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op, const struct lyd_node *tree, |
| 1151 | struct lyd_node **op_node, struct lyd_node **tree_sibling) |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1152 | { |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1153 | const struct lyd_node *tree_iter, *op_iter; |
| 1154 | struct lyd_node *match; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1155 | uint32_t i, cur_depth, op_depth; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1156 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1157 | /* learn op depth (top-level being depth 0) */ |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1158 | op_depth = 0; |
| 1159 | for (op_iter = op; op_iter != op_tree; op_iter = (struct lyd_node *)op_iter->parent) { |
| 1160 | ++op_depth; |
| 1161 | } |
| 1162 | |
| 1163 | /* find where to merge op */ |
| 1164 | tree_iter = tree; |
| 1165 | cur_depth = op_depth; |
| 1166 | op_iter = op; |
| 1167 | while (cur_depth) { |
| 1168 | /* find next op parent */ |
| 1169 | op_iter = op; |
| 1170 | for (i = 0; i < cur_depth; ++i) { |
| 1171 | op_iter = (struct lyd_node *)op_iter->parent; |
| 1172 | } |
| 1173 | |
| 1174 | /* find op iter in tree */ |
| 1175 | lyd_find_sibling_first(tree_iter, op_iter, &match); |
| 1176 | if (!match) { |
| 1177 | break; |
| 1178 | } |
| 1179 | |
| 1180 | /* move tree_iter */ |
| 1181 | tree_iter = lyd_node_children(match); |
| 1182 | |
| 1183 | /* move depth */ |
| 1184 | --cur_depth; |
| 1185 | } |
| 1186 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1187 | *op_node = (struct lyd_node *)op_iter; |
| 1188 | *tree_sibling = (struct lyd_node *)tree_iter; |
| 1189 | } |
| 1190 | |
| 1191 | API LY_ERR |
| 1192 | lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, int val_opts) |
| 1193 | { |
| 1194 | LY_ERR ret; |
| 1195 | struct lyd_node *tree_sibling, *op_node, *op, *op_parent; |
| 1196 | struct ly_set type_check = {0}, type_meta_check = {0}, when_check = {0}; |
| 1197 | |
| 1198 | LY_CHECK_ARG_RET(NULL, op_tree, !op_tree->parent, !tree || !tree->parent, |
| 1199 | !val_opts || (val_opts == LYD_VALOPT_INPUT) || (val_opts == LYD_VALOPT_OUTPUT), LY_EINVAL); |
| 1200 | |
| 1201 | /* find the operation/notification */ |
| 1202 | LYD_TREE_DFS_BEGIN(op_tree, op_node, op) { |
| 1203 | if ((val_opts & (LYD_VALOPT_INPUT | LYD_VALOPT_OUTPUT)) && (op->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 1204 | break; |
| 1205 | } else if (!val_opts && (op->schema->nodetype == LYS_NOTIF)) { |
| 1206 | break; |
| 1207 | } |
| 1208 | LYD_TREE_DFS_END(op_tree, op_node, op); |
| 1209 | } |
| 1210 | if (val_opts & (LYD_VALOPT_INPUT | LYD_VALOPT_OUTPUT)) { |
| 1211 | if (!(op->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 1212 | LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found."); |
| 1213 | return LY_EINVAL; |
| 1214 | } |
| 1215 | } else { |
| 1216 | if (op->schema->nodetype != LYS_NOTIF) { |
| 1217 | LOGERR(LYD_NODE_CTX(op_tree), LY_EINVAL, "No notification to validate found."); |
| 1218 | return LY_EINVAL; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /* move op_tree to top-level node */ |
| 1223 | while (op_tree->parent) { |
| 1224 | op_tree = (struct lyd_node *)op_tree->parent; |
| 1225 | } |
| 1226 | |
| 1227 | /* merge op_tree into tree */ |
| 1228 | lyd_val_op_merge_find(op_tree, op, tree, &op_node, &tree_sibling); |
| 1229 | op_parent = (struct lyd_node *)op_node->parent; |
| 1230 | lyd_unlink_tree(op_node); |
| 1231 | lyd_insert_node(NULL, (struct lyd_node **)&tree_sibling, op_node); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1232 | if (!tree) { |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1233 | tree = tree_sibling; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* prevalidate whole operation subtree */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1237 | LY_CHECK_GOTO(ret = lyd_validate_subtree(op, &type_check, &type_meta_check, &when_check, val_opts), cleanup); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1238 | |
| 1239 | /* finish incompletely validated terminal values/attributes and when conditions on the full tree */ |
| 1240 | LY_CHECK_GOTO(ret = lyd_validate_unres((struct lyd_node **)&tree, &when_check, &type_check, &type_meta_check, |
| 1241 | LYD_JSON, lydjson_resolve_prefix, NULL), cleanup); |
| 1242 | |
| 1243 | /* perform final validation of the operation/notification */ |
| 1244 | lyd_validate_obsolete(op); |
| 1245 | if (lysc_node_is_disabled(op->schema, 1)) { |
| 1246 | LOGVAL(LYD_NODE_CTX(op_tree), LY_VLOG_LYD, op, LY_VCODE_NOIFF, op->schema->name); |
| 1247 | ret = LY_EVALID; |
| 1248 | goto cleanup; |
| 1249 | } |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1250 | LY_CHECK_GOTO(ret = lyd_validate_must(op, val_opts), cleanup); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1251 | |
| 1252 | /* final validation of all the descendants */ |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 1253 | LY_CHECK_GOTO(ret = lyd_validate_final_r(lyd_node_children(op), op->schema, NULL, val_opts), cleanup); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1254 | |
| 1255 | cleanup: |
| 1256 | /* restore operation tree */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1257 | lyd_unlink_tree(op_node); |
| 1258 | if (op_parent) { |
| 1259 | lyd_insert_node(op_parent, NULL, op_node); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | ly_set_erase(&type_check, NULL); |
| 1263 | ly_set_erase(&type_meta_check, NULL); |
| 1264 | ly_set_erase(&when_check, NULL); |
| 1265 | return ret; |
| 1266 | } |