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