Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file validation.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Data tree validation functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 16 | #include <stdlib.h> |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 17 | #include <string.h> |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 18 | |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 19 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 20 | #include "validation.h" |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 21 | #include "libyang.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 22 | #include "xpath.h" |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 23 | #include "parser.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 24 | #include "resolve.h" |
| 25 | #include "tree_internal.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 26 | #include "xml_internal.h" |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 27 | |
Radek Krejci | b45b308 | 2016-09-09 16:08:51 +0200 | [diff] [blame] | 28 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 29 | lyv_keys(const struct lyd_node *list) |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 30 | { |
Radek Krejci | 702deb8 | 2016-03-09 12:37:57 +0100 | [diff] [blame] | 31 | struct lyd_node *child; |
| 32 | struct lys_node_list *schema = (struct lys_node_list *)list->schema; /* shortcut */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 33 | int i; |
| 34 | |
Radek Krejci | 702deb8 | 2016-03-09 12:37:57 +0100 | [diff] [blame] | 35 | for (i = 0, child = list->child; i < schema->keys_size; i++, child = child->next) { |
| 36 | if (!child || child->schema != (struct lys_node *)schema->keys[i]) { |
| 37 | /* key not found on the correct place */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 38 | LOGVAL(schema->module->ctx, LYE_MISSELEM, LY_VLOG_LYD, list, schema->keys[i]->name, schema->name); |
Radek Krejci | 702deb8 | 2016-03-09 12:37:57 +0100 | [diff] [blame] | 39 | for ( ; child; child = child->next) { |
| 40 | if (child->schema == (struct lys_node *)schema->keys[i]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 41 | LOGVAL(schema->module->ctx, LYE_SPEC, LY_VLOG_LYD, child, "Invalid position of the key element."); |
Radek Krejci | 702deb8 | 2016-03-09 12:37:57 +0100 | [diff] [blame] | 42 | break; |
| 43 | } |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 44 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 45 | return 1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 46 | } |
| 47 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 48 | return 0; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 49 | } |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 50 | |
Michal Vasko | 816f0f2 | 2018-03-14 09:56:37 +0100 | [diff] [blame] | 51 | /* |
| 52 | * actions (only for list): |
| 53 | * -1 - compare keys and all uniques |
| 54 | * 0 - compare only keys |
| 55 | * n - compare n-th unique |
| 56 | */ |
| 57 | static int |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 58 | lyv_list_equal(void *val1_p, void *val2_p, void *cb_data) |
Michal Vasko | 816f0f2 | 2018-03-14 09:56:37 +0100 | [diff] [blame] | 59 | { |
| 60 | struct ly_ctx *ctx; |
| 61 | struct lys_node_list *slist; |
| 62 | const struct lys_node *snode = NULL; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 63 | struct lyd_node *diter, *first, *second; |
Michal Vasko | 816f0f2 | 2018-03-14 09:56:37 +0100 | [diff] [blame] | 64 | const char *val1, *val2; |
| 65 | char *path1, *path2, *uniq_str; |
| 66 | uint16_t idx_uniq; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 67 | int i, j, r, action; |
Michal Vasko | 816f0f2 | 2018-03-14 09:56:37 +0100 | [diff] [blame] | 68 | |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 69 | assert(val1_p && val2_p); |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 70 | |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 71 | first = *((struct lyd_node **)val1_p); |
| 72 | second = *((struct lyd_node **)val2_p); |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 73 | action = (intptr_t)cb_data; |
| 74 | |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 75 | assert(first && (first->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))); |
| 76 | assert(second && (second->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))); |
Michal Vasko | 816f0f2 | 2018-03-14 09:56:37 +0100 | [diff] [blame] | 77 | assert(first->schema->nodetype == second->schema->nodetype); |
| 78 | |
| 79 | ctx = first->schema->module->ctx; |
| 80 | |
| 81 | if (first->schema != second->schema) { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | switch (first->schema->nodetype) { |
| 86 | case LYS_LEAFLIST: |
| 87 | if ((first->schema->flags & LYS_CONFIG_R) && first->schema->module->version >= 2) { |
| 88 | /* same values are allowed for status data */ |
| 89 | return 0; |
| 90 | } |
| 91 | /* compare values */ |
| 92 | if (ly_strequal(((struct lyd_node_leaf_list *)first)->value_str, |
| 93 | ((struct lyd_node_leaf_list *)second)->value_str, 1)) { |
| 94 | LOGVAL(ctx, LYE_DUPLEAFLIST, LY_VLOG_LYD, second, second->schema->name, |
| 95 | ((struct lyd_node_leaf_list *)second)->value_str); |
| 96 | return 1; |
| 97 | } |
| 98 | return 0; |
| 99 | case LYS_LIST: |
| 100 | slist = (struct lys_node_list *)first->schema; |
| 101 | |
| 102 | /* compare unique leafs */ |
| 103 | if (action) { |
| 104 | if (action > 0) { |
| 105 | i = action - 1; |
| 106 | if (i < slist->unique_size) { |
| 107 | goto uniquecheck; |
| 108 | } |
| 109 | } |
| 110 | for (i = 0; i < slist->unique_size; i++) { |
| 111 | uniquecheck: |
| 112 | for (j = 0; j < slist->unique[i].expr_size; j++) { |
| 113 | /* first */ |
| 114 | diter = resolve_data_descendant_schema_nodeid(slist->unique[i].expr[j], first->child); |
| 115 | if (diter) { |
| 116 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 117 | } else { |
| 118 | /* use default value */ |
| 119 | if (lyd_get_unique_default(slist->unique[i].expr[j], first, &val1)) { |
| 120 | return 1; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* second */ |
| 125 | diter = resolve_data_descendant_schema_nodeid(slist->unique[i].expr[j], second->child); |
| 126 | if (diter) { |
| 127 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 128 | } else { |
| 129 | /* use default value */ |
| 130 | if (lyd_get_unique_default(slist->unique[i].expr[j], second, &val2)) { |
| 131 | return 1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!val1 || !val2 || !ly_strequal(val1, val2, 1)) { |
| 136 | /* values differ or either one is not set */ |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | if (j && (j == slist->unique[i].expr_size)) { |
| 141 | /* all unique leafs are the same in this set, create this nice error */ |
| 142 | ly_vlog_build_path(LY_VLOG_LYD, first, &path1, 0); |
| 143 | ly_vlog_build_path(LY_VLOG_LYD, second, &path2, 0); |
| 144 | |
| 145 | /* use buffer to rebuild the unique string */ |
| 146 | uniq_str = malloc(1024); |
| 147 | idx_uniq = 0; |
| 148 | for (j = 0; j < slist->unique[i].expr_size; ++j) { |
| 149 | if (j) { |
| 150 | uniq_str[idx_uniq++] = ' '; |
| 151 | } |
| 152 | r = lyd_build_relative_data_path(lys_node_module((struct lys_node *)slist), first, |
| 153 | slist->unique[i].expr[j], &uniq_str[idx_uniq]); |
| 154 | if (r == -1) { |
| 155 | goto unique_errmsg_cleanup; |
| 156 | } |
| 157 | idx_uniq += r; |
| 158 | } |
| 159 | |
| 160 | LOGVAL(ctx, LYE_NOUNIQ, LY_VLOG_LYD, second, uniq_str, path1, path2); |
| 161 | unique_errmsg_cleanup: |
| 162 | free(path1); |
| 163 | free(path2); |
| 164 | free(uniq_str); |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | if (action > 0) { |
| 169 | /* done */ |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* compare keys */ |
| 176 | if (!slist->keys_size) { |
| 177 | /* status lists without keys */ |
| 178 | return 0; |
| 179 | } else { |
| 180 | for (i = 0; i < slist->keys_size; i++) { |
| 181 | snode = (struct lys_node *)slist->keys[i]; |
| 182 | val1 = val2 = NULL; |
| 183 | LY_TREE_FOR(first->child, diter) { |
| 184 | if (diter->schema == snode) { |
| 185 | val1 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | LY_TREE_FOR(second->child, diter) { |
| 190 | if (diter->schema == snode) { |
| 191 | val2 = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | if (!ly_strequal(val1, val2, 1)) { |
| 196 | return 0; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | LOGVAL(ctx, LYE_DUPLIST, LY_VLOG_LYD, second, second->schema->name); |
| 202 | return 1; |
| 203 | |
| 204 | default: |
| 205 | LOGINT(ctx); |
| 206 | return 1; |
| 207 | } |
| 208 | } |
| 209 | |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 210 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 211 | lyv_data_context(const struct lyd_node *node, int options, struct unres_data *unres) |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 212 | { |
Radek Krejci | 6baaa9a | 2016-02-23 16:07:12 +0100 | [diff] [blame] | 213 | const struct lys_node *siter = NULL; |
Radek Krejci | b1f318b | 2016-08-22 16:18:37 +0200 | [diff] [blame] | 214 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 215 | struct ly_ctx *ctx = node->schema->module->ctx; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 216 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 217 | assert(node); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 218 | assert(unres); |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 219 | |
| 220 | /* check if the node instance is enabled by if-feature */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 221 | if (lys_is_disabled(node->schema, 2)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 222 | LOGVAL(ctx, LYE_INELEM, LY_VLOG_LYD, node, node->schema->name); |
| 223 | return 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Radek Krejci | b1f318b | 2016-08-22 16:18:37 +0200 | [diff] [blame] | 226 | if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 227 | /* if union with leafref/intsid, leafref itself (invalid) or instance-identifier, store the node for later resolving */ |
| 228 | if ((((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_UNION) |
| 229 | && ((struct lys_node_leaf *)leaf->schema)->type.info.uni.has_ptr_type) { |
| 230 | if (unres_data_add(unres, (struct lyd_node *)node, UNRES_UNION)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 231 | return 1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 232 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 233 | } else if ((((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_LEAFREF) && (leaf->validity & LYD_VAL_LEAFREF)) { |
| 234 | if (unres_data_add(unres, (struct lyd_node *)node, UNRES_LEAFREF)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 235 | return 1; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 236 | } |
| 237 | } else if (((struct lys_node_leaf *)leaf->schema)->type.base == LY_TYPE_INST) { |
| 238 | if (unres_data_add(unres, (struct lyd_node *)node, UNRES_INSTID)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 239 | return 1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 240 | } |
Michal Vasko | 0d182ba | 2015-10-09 09:29:14 +0200 | [diff] [blame] | 241 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 244 | /* check all relevant when conditions */ |
Michal Vasko | ebf7df2 | 2017-03-28 16:08:07 +0200 | [diff] [blame] | 245 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) |
| 246 | && (node->when_status & LYD_WHEN)) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 247 | if (unres_data_add(unres, (struct lyd_node *)node, UNRES_WHEN)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 248 | return 1; |
Michal Vasko | 0d182ba | 2015-10-09 09:29:14 +0200 | [diff] [blame] | 249 | } |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* check for (non-)presence of status data in edit-config data */ |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 253 | if ((options & (LYD_OPT_EDIT | LYD_OPT_GETCONFIG | LYD_OPT_CONFIG)) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 254 | LOGVAL(ctx, LYE_INELEM, LY_VLOG_LYD, node, node->schema->name); |
| 255 | return 1; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 258 | /* check elements order in case of RPC's input and output */ |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 259 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER)) && (node->validity & LYD_VAL_MAND) && lyp_is_rpc_action(node->schema)) { |
Michal Vasko | 15e0bab | 2016-02-24 13:58:21 +0100 | [diff] [blame] | 260 | if ((node->prev != node) && node->prev->next) { |
Michal Vasko | 9e78049 | 2017-03-28 14:48:31 +0200 | [diff] [blame] | 261 | for (siter = lys_getnext(node->schema, lys_parent(node->schema), lyd_node_module(node), LYS_GETNEXT_PARENTUSES); |
Radek Krejci | 6baaa9a | 2016-02-23 16:07:12 +0100 | [diff] [blame] | 262 | siter; |
Michal Vasko | 9e78049 | 2017-03-28 14:48:31 +0200 | [diff] [blame] | 263 | siter = lys_getnext(siter, lys_parent(node->schema), lyd_node_module(node), LYS_GETNEXT_PARENTUSES)) { |
Radek Krejci | 6baaa9a | 2016-02-23 16:07:12 +0100 | [diff] [blame] | 264 | if (siter == node->prev->schema) { |
| 265 | /* data predecessor has the schema node after |
| 266 | * the schema node of the data node being checked */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 267 | LOGVAL(ctx, LYE_INORDER, LY_VLOG_LYD, node, node->schema->name, siter->name); |
| 268 | return 1; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 269 | } |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 275 | return 0; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 276 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 277 | |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 278 | int |
| 279 | lyv_data_unique(struct lyd_node *node, struct lyd_node *start) |
| 280 | { |
| 281 | struct lyd_node *diter, *key; |
| 282 | struct lys_node_list *slist; |
| 283 | struct ly_set *set; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 284 | int i, j, n = 0, ret = 0; |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 285 | uint32_t hash, u, usize = 0; |
| 286 | struct hash_table *keystable = NULL, **uniquetables = NULL; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 287 | const char *id; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 288 | struct ly_ctx *ctx = node->schema->module->ctx; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 289 | |
| 290 | /* get the first list/leaflist instance sibling */ |
| 291 | if (!start) { |
| 292 | start = lyd_first_sibling(node); |
| 293 | } |
| 294 | |
| 295 | /* check uniqueness of the list/leaflist instances (compare values) */ |
| 296 | set = ly_set_new(); |
| 297 | for (diter = start; diter; diter = diter->next) { |
| 298 | if (diter->schema != node->schema) { |
| 299 | /* check only instances of the same list/leaflist */ |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | /* remove the flag */ |
| 304 | diter->validity &= ~LYD_VAL_UNIQUE; |
| 305 | |
| 306 | /* store for comparison */ |
| 307 | ly_set_add(set, diter, LY_SET_OPT_USEASLIST); |
| 308 | } |
| 309 | |
| 310 | if (set->number == 2) { |
| 311 | /* simple comparison */ |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 312 | if (lyv_list_equal(&set->set.d[0], &set->set.d[1], (void *)-1)) { |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 313 | /* instance duplication */ |
| 314 | ly_set_free(set); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 315 | return 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 316 | } |
| 317 | } else if (set->number > 2) { |
| 318 | /* use hashes for comparison */ |
| 319 | /* first, allocate the table, the size depends on number of items in the set */ |
| 320 | for (u = 31; u > 0; u--) { |
| 321 | usize = set->number << u; |
| 322 | usize = usize >> u; |
| 323 | if (usize == set->number) { |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | if (u == 0) { |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 328 | LOGINT(ctx); |
| 329 | return 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 330 | } else { |
| 331 | u = 32 - u; |
| 332 | usize = 1 << u; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 333 | } |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 334 | keystable = lyht_new(usize, sizeof(struct lyd_node *), lyv_list_equal, 0, 0); |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 335 | if (!keystable) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 336 | LOGMEM(ctx); |
| 337 | ret = 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 338 | goto unique_cleanup; |
| 339 | } |
| 340 | n = 0; |
| 341 | if (node->schema->nodetype == LYS_LIST) { |
| 342 | n = ((struct lys_node_list *)node->schema)->unique_size; |
| 343 | uniquetables = malloc(n * sizeof *uniquetables); |
| 344 | if (!uniquetables) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 345 | LOGMEM(ctx); |
| 346 | ret = 1; |
Radek Krejci | 70292d2 | 2016-08-15 09:39:22 +0200 | [diff] [blame] | 347 | n = 0; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 348 | goto unique_cleanup; |
| 349 | } |
| 350 | for (j = 0; j < n; j++) { |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 351 | uniquetables[j] = lyht_new(usize, sizeof(struct lyd_node *), lyv_list_equal, (void *)(j + 1L), 0); |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 352 | if (!uniquetables[j]) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 353 | LOGMEM(ctx); |
| 354 | ret = 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 355 | goto unique_cleanup; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | for (u = 0; u < set->number; u++) { |
| 361 | /* get the hash for the instance - keys */ |
| 362 | if (node->schema->nodetype == LYS_LEAFLIST) { |
| 363 | id = ((struct lyd_node_leaf_list *)set->set.d[u])->value_str; |
| 364 | hash = dict_hash_multi(0, id, strlen(id)); |
| 365 | } else { /* LYS_LIST */ |
| 366 | for (hash = i = 0, key = set->set.d[u]->child; |
| 367 | i < ((struct lys_node_list *)set->set.d[u]->schema)->keys_size; |
| 368 | i++, key = key->next) { |
| 369 | id = ((struct lyd_node_leaf_list *)key)->value_str; |
| 370 | hash = dict_hash_multi(hash, id, strlen(id)); |
| 371 | } |
| 372 | } |
| 373 | /* finish the hash value */ |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 374 | hash = dict_hash_multi(hash, NULL, 0); |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 375 | |
| 376 | /* insert into the hashtable */ |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 377 | if (lyht_insert(keystable, &set->set.d[u], hash)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 378 | ret = 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 379 | goto unique_cleanup; |
| 380 | } |
| 381 | |
| 382 | /* and the same loop for unique (n is !0 only in case of list) - get the hash for the instances */ |
| 383 | for (j = 0; j < n; j++) { |
| 384 | slist = (struct lys_node_list *)node->schema; |
Radek Krejci | 50dc7ae | 2016-09-01 09:56:37 +0200 | [diff] [blame] | 385 | id = NULL; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 386 | for (i = hash = 0; i < slist->unique[j].expr_size; i++) { |
| 387 | diter = resolve_data_descendant_schema_nodeid(slist->unique[j].expr[i], set->set.d[u]->child); |
| 388 | if (diter) { |
| 389 | id = ((struct lyd_node_leaf_list *)diter)->value_str; |
| 390 | } else { |
| 391 | /* use default value */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 392 | if (lyd_get_unique_default(slist->unique[j].expr[i], set->set.d[u], &id)) { |
| 393 | ret = 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 394 | goto unique_cleanup; |
| 395 | } |
| 396 | } |
Radek Krejci | 2a5f52b | 2016-08-15 09:14:36 +0200 | [diff] [blame] | 397 | if (!id) { |
| 398 | /* unique item not present nor has default value */ |
| 399 | break; |
| 400 | } |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 401 | hash = dict_hash_multi(hash, id, strlen(id)); |
| 402 | } |
Radek Krejci | 2a5f52b | 2016-08-15 09:14:36 +0200 | [diff] [blame] | 403 | if (!id) { |
| 404 | /* skip this list instance since its unique set is incomplete */ |
| 405 | continue; |
| 406 | } |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 407 | |
| 408 | /* finish the hash value */ |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 409 | hash = dict_hash_multi(hash, NULL, 0); |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 410 | |
| 411 | /* insert into the hashtable */ |
Michal Vasko | 419fce0 | 2018-03-21 11:55:09 +0100 | [diff] [blame^] | 412 | if (lyht_insert(uniquetables[j], &set->set.d[u], hash)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 413 | ret = 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 414 | goto unique_cleanup; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | unique_cleanup: |
| 421 | /* cleanup */ |
| 422 | ly_set_free(set); |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 423 | lyht_free(keystable); |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 424 | for (j = 0; j < n; j++) { |
| 425 | if (!uniquetables[j]) { |
| 426 | /* failed when allocating uniquetables[j], following j are not allocated */ |
| 427 | break; |
| 428 | } |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 429 | lyht_free(uniquetables[j]); |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 430 | } |
| 431 | free(uniquetables); |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
Michal Vasko | 91ff459 | 2017-05-24 15:41:49 +0200 | [diff] [blame] | 436 | static struct lys_type * |
| 437 | find_orig_type(struct lys_type *par_type, LY_DATA_TYPE base_type) |
| 438 | { |
| 439 | struct lys_type *type, *prev_type, *tmp_type; |
| 440 | int found; |
| 441 | |
| 442 | /* go through typedefs */ |
| 443 | for (type = par_type; type->der->type.der; type = &type->der->type); |
| 444 | |
| 445 | if (type->base == base_type) { |
| 446 | /* we have the result */ |
| 447 | return type; |
| 448 | } else if (type->base == LY_TYPE_UNION) { |
| 449 | /* go through all the union types */ |
| 450 | prev_type = NULL; |
| 451 | found = 0; |
| 452 | while ((prev_type = lyp_get_next_union_type(type, prev_type, &found))) { |
| 453 | tmp_type = find_orig_type(prev_type, base_type); |
| 454 | if (tmp_type) { |
| 455 | return tmp_type; |
| 456 | } |
| 457 | found = 0; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /* not found */ |
| 462 | return NULL; |
| 463 | } |
| 464 | |
PavolVican | 556559e | 2017-12-12 13:39:36 +0100 | [diff] [blame] | 465 | static int |
| 466 | lyv_extension(struct lys_ext_instance **ext, uint8_t size, struct lyd_node *node) |
| 467 | { |
| 468 | uint i; |
| 469 | |
| 470 | for (i = 0; i < size; ++i) { |
| 471 | if ((ext[i]->flags & LYEXT_OPT_VALID) && ext[i]->def->plugin->valid_data) { |
| 472 | if (ext[i]->def->plugin->valid_data(ext[i], node)) { |
| 473 | return EXIT_FAILURE; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int |
| 481 | lyv_type_extension(struct lyd_node_leaf_list *leaf, struct lys_type *type, int first_type) |
| 482 | { |
| 483 | struct lyd_node *node = (struct lyd_node *)leaf; |
| 484 | unsigned int i; |
| 485 | |
| 486 | switch (type->base) { |
| 487 | case LY_TYPE_ENUM: |
| 488 | if (first_type && lyv_extension(leaf->value.enm->ext, leaf->value.enm->ext_size, node)) { |
| 489 | return EXIT_FAILURE; |
| 490 | } |
| 491 | break; |
| 492 | case LY_TYPE_STRING: |
| 493 | if (type->info.str.length && |
| 494 | lyv_extension(type->info.str.length->ext, type->info.str.length->ext_size, node)) { |
| 495 | return EXIT_FAILURE; |
| 496 | } |
| 497 | for(i = 0; i < type->info.str.pat_count; ++i) { |
| 498 | if (lyv_extension(type->info.str.patterns[i].ext, type->info.str.patterns[i].ext_size, node)) { |
| 499 | return EXIT_FAILURE; |
| 500 | } |
| 501 | } |
| 502 | break; |
| 503 | case LY_TYPE_DEC64: |
| 504 | if (type->info.dec64.range && |
| 505 | lyv_extension(type->info.dec64.range->ext, type->info.dec64.range->ext_size, node)) { |
| 506 | return EXIT_FAILURE; |
| 507 | } |
| 508 | break; |
| 509 | case LY_TYPE_INT8: |
| 510 | case LY_TYPE_INT16: |
| 511 | case LY_TYPE_INT32: |
| 512 | case LY_TYPE_INT64: |
| 513 | case LY_TYPE_UINT8: |
| 514 | case LY_TYPE_UINT16: |
| 515 | case LY_TYPE_UINT32: |
| 516 | case LY_TYPE_UINT64: |
| 517 | if (type->info.num.range && |
| 518 | lyv_extension(type->info.num.range->ext, type->info.num.range->ext_size, node)) { |
| 519 | return EXIT_FAILURE; |
| 520 | } |
| 521 | break; |
| 522 | case LY_TYPE_BITS: |
| 523 | if (first_type) { |
| 524 | /* get the count of bits */ |
| 525 | type = find_orig_type(&((struct lys_node_leaf *) leaf->schema)->type, LY_TYPE_BITS); |
| 526 | for (i = 0; i < type->info.bits.count; ++i) { |
| 527 | if (!leaf->value.bit[i]) { |
| 528 | continue; |
| 529 | } |
| 530 | if (lyv_extension(leaf->value.bit[i]->ext, leaf->value.bit[i]->ext_size, node)) { |
| 531 | return EXIT_FAILURE; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | break; |
| 536 | case LY_TYPE_UNION: |
| 537 | for (i = 0; i < type->info.uni.count; ++i) { |
| 538 | if (type->info.uni.types[i].base == leaf->value_type) { |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | if (i < type->info.uni.count && |
| 543 | lyv_type_extension(leaf, &type->info.uni.types[i], first_type)) { |
| 544 | return EXIT_FAILURE; |
| 545 | } |
| 546 | break; |
| 547 | default: |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | if (lyv_extension(type->ext, type->ext_size, node)) { |
| 553 | return EXIT_FAILURE; |
| 554 | } |
| 555 | |
| 556 | while (type->der->type.der) { |
| 557 | type = &type->der->type; |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 558 | if ((type->parent->flags & LYS_VALID_EXT)) { |
| 559 | if (lyv_type_extension(leaf, type, 0) || lyv_extension(type->parent->ext, type->parent->ext_size, node)) { |
PavolVican | 556559e | 2017-12-12 13:39:36 +0100 | [diff] [blame] | 560 | return EXIT_FAILURE; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return EXIT_SUCCESS; |
| 566 | } |
| 567 | |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 568 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 569 | lyv_data_content(struct lyd_node *node, int options, struct unres_data *unres) |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 570 | { |
PavolVican | 832f543 | 2018-02-21 00:54:45 +0100 | [diff] [blame] | 571 | const struct lys_node *schema, *siter, *parent; |
Radek Krejci | d788a52 | 2016-07-25 14:57:38 +0200 | [diff] [blame] | 572 | struct lyd_node *diter, *start = NULL; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 573 | struct lys_ident *ident; |
Radek Krejci | 4eaf5a8 | 2015-12-15 15:10:38 +0100 | [diff] [blame] | 574 | struct lys_tpdf *tpdf; |
Radek Krejci | e6a71b5 | 2016-08-10 15:11:16 +0200 | [diff] [blame] | 575 | struct lys_type *type = NULL; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 576 | struct lyd_node_leaf_list *leaf; |
Radek Krejci | dce5f97 | 2017-09-12 15:47:49 +0200 | [diff] [blame] | 577 | unsigned int i, j = 0; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 578 | uint8_t iff_size; |
| 579 | struct lys_iffeature *iff; |
| 580 | const char *id, *idname; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 581 | struct ly_ctx *ctx; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 582 | |
| 583 | assert(node); |
| 584 | assert(node->schema); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 585 | assert(unres); |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 586 | |
| 587 | schema = node->schema; /* shortcut */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 588 | ctx = schema->module->ctx; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 589 | |
Michal Vasko | 091dd84 | 2017-02-08 14:07:32 +0100 | [diff] [blame] | 590 | if (node->validity & LYD_VAL_MAND) { |
| 591 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER))) { |
| 592 | /* check presence and correct order of all keys in case of list */ |
| 593 | if (schema->nodetype == LYS_LIST && !(options & (LYD_OPT_GET | LYD_OPT_GETCONFIG))) { |
| 594 | if (lyv_keys(node)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 595 | return 1; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 596 | } |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 597 | } |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 598 | |
Michal Vasko | 091dd84 | 2017-02-08 14:07:32 +0100 | [diff] [blame] | 599 | if (schema->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) { |
| 600 | /* check number of instances (similar to list uniqueness) for non-list nodes */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 601 | |
Michal Vasko | 091dd84 | 2017-02-08 14:07:32 +0100 | [diff] [blame] | 602 | /* find duplicity */ |
| 603 | start = lyd_first_sibling(node); |
| 604 | for (diter = start; diter; diter = diter->next) { |
| 605 | if (diter->schema == schema && diter != node) { |
PavolVican | 832f543 | 2018-02-21 00:54:45 +0100 | [diff] [blame] | 606 | parent = lys_parent(schema); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 607 | LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, node, schema->name, |
PavolVican | 832f543 | 2018-02-21 00:54:45 +0100 | [diff] [blame] | 608 | parent ? (parent->nodetype == LYS_EXT) ? ((struct lys_ext_instance *)parent)->arg_value : parent->name : "data tree"); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 609 | return 1; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 610 | } |
| 611 | } |
Michal Vasko | 091dd84 | 2017-02-08 14:07:32 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | if (options & LYD_OPT_OBSOLETE) { |
| 615 | /* status - of the node's schema node itself and all its parents that |
| 616 | * cannot have their own instance (like a choice statement) */ |
| 617 | siter = node->schema; |
| 618 | do { |
| 619 | if (((siter->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) && (options & LYD_OPT_OBSOLETE)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 620 | LOGVAL(ctx, LYE_OBSDATA, LY_VLOG_LYD, node, schema->name); |
| 621 | return 1; |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 622 | } |
Michal Vasko | 091dd84 | 2017-02-08 14:07:32 +0100 | [diff] [blame] | 623 | siter = lys_parent(siter); |
| 624 | } while (siter && !(siter->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA))); |
| 625 | |
| 626 | /* status of the identity value */ |
| 627 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 628 | if (options & LYD_OPT_OBSOLETE) { |
| 629 | /* check that we are not instantiating obsolete type */ |
| 630 | tpdf = ((struct lys_node_leaf *)node->schema)->type.der; |
| 631 | while (tpdf) { |
| 632 | if ((tpdf->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 633 | LOGVAL(ctx, LYE_OBSTYPE, LY_VLOG_LYD, node, schema->name, tpdf->name); |
| 634 | return 1; |
Michal Vasko | 091dd84 | 2017-02-08 14:07:32 +0100 | [diff] [blame] | 635 | } |
| 636 | tpdf = tpdf->type.der; |
| 637 | } |
| 638 | } |
| 639 | if (((struct lyd_node_leaf_list *)node)->value_type == LY_TYPE_IDENT) { |
| 640 | ident = ((struct lyd_node_leaf_list *)node)->value.ident; |
| 641 | if (lyp_check_status(schema->flags, schema->module, schema->name, |
| 642 | ident->flags, ident->module, ident->name, NULL)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 643 | LOGPATH(ctx, LY_VLOG_LYD, node); |
| 644 | return 1; |
Michal Vasko | 091dd84 | 2017-02-08 14:07:32 +0100 | [diff] [blame] | 645 | } |
| 646 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
PavolVican | 556559e | 2017-12-12 13:39:36 +0100 | [diff] [blame] | 651 | /* check validation function for extension */ |
Michal Vasko | 1bdfd43 | 2018-03-09 09:30:19 +0100 | [diff] [blame] | 652 | if (schema->flags & LYS_VALID_EXT) { |
PavolVican | 556559e | 2017-12-12 13:39:36 +0100 | [diff] [blame] | 653 | // check extension in node |
| 654 | if (lyv_extension(schema->ext, schema->ext_size, node)) { |
| 655 | return EXIT_FAILURE; |
| 656 | } |
| 657 | |
| 658 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 659 | type = &((struct lys_node_leaf *) schema)->type; |
| 660 | leaf = (struct lyd_node_leaf_list *) node; |
| 661 | if (lyv_type_extension(leaf, type, 1)) { |
| 662 | return EXIT_FAILURE; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | |
| 667 | } |
| 668 | |
Radek Krejci | d788a52 | 2016-07-25 14:57:38 +0200 | [diff] [blame] | 669 | /* remove the flag */ |
| 670 | node->validity &= ~LYD_VAL_MAND; |
| 671 | } |
Radek Krejci | 4eaf5a8 | 2015-12-15 15:10:38 +0100 | [diff] [blame] | 672 | |
Michal Vasko | b1bc1d9 | 2017-01-05 11:47:41 +0100 | [diff] [blame] | 673 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_GET | LYD_OPT_GETCONFIG))) { |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 674 | /* skip key uniqueness check in case of get/get-config data */ |
| 675 | if (schema->nodetype & (LYS_LIST | LYS_CONTAINER)) { |
| 676 | LY_TREE_FOR(schema->child, siter) { |
| 677 | if (siter->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 678 | LY_TREE_FOR(node->child, diter) { |
| 679 | if (diter->schema == siter && (diter->validity & LYD_VAL_UNIQUE)) { |
| 680 | if (lyv_data_unique(diter, node->child)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 681 | return 1; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 682 | } |
| 683 | /* all schema instances checked, continue with another schema node */ |
| 684 | break; |
| 685 | } |
| 686 | } |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 687 | } |
Radek Krejci | 4eaf5a8 | 2015-12-15 15:10:38 +0100 | [diff] [blame] | 688 | } |
| 689 | } |
Radek Krejci | d788a52 | 2016-07-25 14:57:38 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 692 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 693 | /* since feature can be enabled/disabled, do this check despite the validity flag, |
| 694 | * - check if the type value (enum, bit, identity) is disabled via feature */ |
| 695 | leaf = (struct lyd_node_leaf_list *)node; |
| 696 | switch (leaf->value_type) { |
| 697 | case LY_TYPE_BITS: |
| 698 | id = "Bit"; |
| 699 | /* get the count of bits */ |
Michal Vasko | 91ff459 | 2017-05-24 15:41:49 +0200 | [diff] [blame] | 700 | type = find_orig_type(&((struct lys_node_leaf *)leaf->schema)->type, LY_TYPE_BITS); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 701 | for (j = iff_size = 0; j < type->info.bits.count; j++) { |
| 702 | if (!leaf->value.bit[j]) { |
| 703 | continue; |
| 704 | } |
| 705 | idname = leaf->value.bit[j]->name; |
| 706 | iff_size = leaf->value.bit[j]->iffeature_size; |
| 707 | iff = leaf->value.bit[j]->iffeature; |
| 708 | break; |
| 709 | nextbit: |
| 710 | iff_size = 0; |
| 711 | } |
| 712 | break; |
| 713 | case LY_TYPE_ENUM: |
| 714 | id = "Enum"; |
| 715 | idname = leaf->value_str; |
| 716 | iff_size = leaf->value.enm->iffeature_size; |
| 717 | iff = leaf->value.enm->iffeature; |
| 718 | break; |
| 719 | case LY_TYPE_IDENT: |
| 720 | id = "Identity"; |
| 721 | idname = leaf->value_str; |
| 722 | iff_size = leaf->value.ident->iffeature_size; |
| 723 | iff = leaf->value.ident->iffeature; |
| 724 | break; |
| 725 | default: |
| 726 | iff_size = 0; |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | if (iff_size) { |
| 731 | for (i = 0; i < iff_size; i++) { |
| 732 | if (!resolve_iffeature(&iff[i])) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 733 | LOGVAL(ctx, LYE_INVAL, LY_VLOG_LYD, node, leaf->value_str, schema->name); |
| 734 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "%s \"%s\" is disabled by its if-feature condition.", |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 735 | id, idname); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 736 | return 1; |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | if (leaf->value_type == LY_TYPE_BITS) { |
| 740 | goto nextbit; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 745 | /* check must conditions */ |
| 746 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG))) { |
Michal Vasko | 4b5b625 | 2016-10-11 12:18:00 +0000 | [diff] [blame] | 747 | i = resolve_applies_must(node); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 748 | if ((i & 0x1) && unres_data_add(unres, node, UNRES_MUST)) { |
| 749 | return 1; |
Michal Vasko | 4b5b625 | 2016-10-11 12:18:00 +0000 | [diff] [blame] | 750 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 751 | if ((i & 0x2) && unres_data_add(unres, node, UNRES_MUST_INOUT)) { |
| 752 | return 1; |
Michal Vasko | 4b5b625 | 2016-10-11 12:18:00 +0000 | [diff] [blame] | 753 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 754 | } |
| 755 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | int |
| 760 | lyv_multicases(struct lyd_node *node, struct lys_node *schemanode, struct lyd_node **first_sibling, |
| 761 | int autodelete, struct lyd_node *nodel) |
| 762 | { |
| 763 | struct lys_node *sparent, *schoice, *scase, *saux; |
| 764 | struct lyd_node *next, *iter; |
| 765 | assert(node || schemanode); |
| 766 | |
| 767 | if (!schemanode) { |
| 768 | schemanode = node->schema; |
| 769 | } |
| 770 | |
| 771 | sparent = lys_parent(schemanode); |
| 772 | if (!sparent || !(sparent->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 773 | /* node is not under any choice */ |
| 774 | return 0; |
| 775 | } else if (!first_sibling || !(*first_sibling)) { |
| 776 | /* nothing to check */ |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /* remember which case to skip in which choice */ |
| 781 | if (sparent->nodetype == LYS_CHOICE) { |
| 782 | schoice = sparent; |
| 783 | scase = schemanode; |
| 784 | } else { |
| 785 | schoice = lys_parent(sparent); |
| 786 | scase = sparent; |
| 787 | } |
| 788 | |
| 789 | autodelete: |
| 790 | /* remove all nodes from other cases than 'sparent' */ |
| 791 | LY_TREE_FOR_SAFE(*first_sibling, next, iter) { |
| 792 | if (schemanode == iter->schema) { |
| 793 | continue; |
| 794 | } |
| 795 | |
| 796 | sparent = lys_parent(iter->schema); |
| 797 | if (sparent && ((sparent->nodetype == LYS_CHOICE && sparent == schoice) /* another implicit case */ |
| 798 | || (sparent->nodetype == LYS_CASE && sparent != scase && lys_parent(sparent) == schoice)) /* another case */ |
| 799 | ) { |
| 800 | if (autodelete) { |
| 801 | if (iter == nodel) { |
| 802 | LOGVAL(schemanode->module->ctx, LYE_MCASEDATA, LY_VLOG_LYD, iter, schoice->name); |
| 803 | return 2; |
| 804 | } |
| 805 | if (iter == *first_sibling) { |
| 806 | *first_sibling = next; |
| 807 | } |
| 808 | lyd_free(iter); |
| 809 | } else { |
| 810 | LOGVAL(schemanode->module->ctx, LYE_MCASEDATA, node ? LY_VLOG_LYD : LY_VLOG_NONE, node, schoice->name); |
| 811 | return 1; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | if (*first_sibling && (saux = lys_parent(schoice)) && (saux->nodetype & LYS_CASE)) { |
| 817 | /* go recursively in case of nested choices */ |
| 818 | schoice = lys_parent(saux); |
| 819 | scase = saux; |
| 820 | goto autodelete; |
| 821 | } |
| 822 | |
| 823 | return 0; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 824 | } |