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