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