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