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