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