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