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