blob: 070cb572a3cef90c0df4cfbacf60d6d1c4bb0c00 [file] [log] [blame]
Radek Krejcib1c12512015-08-11 11:22:04 +02001/**
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 Krejci54f6fb32016-02-24 12:56:39 +01008 * 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 Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcib1c12512015-08-11 11:22:04 +020013 */
14
Radek Krejcieab784a2015-08-27 09:56:53 +020015#include <assert.h>
Radek Krejcib1c12512015-08-11 11:22:04 +020016#include <stdlib.h>
Michal Vaskocf024702015-10-08 15:01:42 +020017#include <string.h>
Radek Krejcib1c12512015-08-11 11:22:04 +020018
Radek Krejcieab784a2015-08-27 09:56:53 +020019#include "common.h"
Michal Vaskocf024702015-10-08 15:01:42 +020020#include "validation.h"
Radek Krejcib1c12512015-08-11 11:22:04 +020021#include "libyang.h"
Michal Vaskocf024702015-10-08 15:01:42 +020022#include "xpath.h"
Radek Krejcicf509982015-12-15 09:22:44 +010023#include "parser.h"
Michal Vaskocf024702015-10-08 15:01:42 +020024#include "resolve.h"
25#include "tree_internal.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020026#include "xml_internal.h"
Radek Krejcib1c12512015-08-11 11:22:04 +020027
Radek Krejcib45b3082016-09-09 16:08:51 +020028static int
Radek Krejci48464ed2016-03-17 15:44:09 +010029lyv_keys(const struct lyd_node *list)
Radek Krejcib1c12512015-08-11 11:22:04 +020030{
Radek Krejci702deb82016-03-09 12:37:57 +010031 struct lyd_node *child;
32 struct lys_node_list *schema = (struct lys_node_list *)list->schema; /* shortcut */
Radek Krejcib1c12512015-08-11 11:22:04 +020033 int i;
34
Radek Krejci702deb82016-03-09 12:37:57 +010035 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 */
Radek Krejci48464ed2016-03-17 15:44:09 +010038 LOGVAL(LYE_MISSELEM, LY_VLOG_LYD, list, schema->keys[i]->name, schema->name);
Radek Krejci702deb82016-03-09 12:37:57 +010039 for ( ; child; child = child->next) {
40 if (child->schema == (struct lys_node *)schema->keys[i]) {
Radek Krejci48464ed2016-03-17 15:44:09 +010041 LOGVAL(LYE_SPEC, LY_VLOG_LYD, child, "Invalid position of the key element.");
Radek Krejci702deb82016-03-09 12:37:57 +010042 break;
43 }
Radek Krejcib1c12512015-08-11 11:22:04 +020044 }
Radek Krejci702deb82016-03-09 12:37:57 +010045 return EXIT_FAILURE;
Radek Krejcib1c12512015-08-11 11:22:04 +020046 }
47 }
Radek Krejcib1c12512015-08-11 11:22:04 +020048 return EXIT_SUCCESS;
49}
Radek Krejcieab784a2015-08-27 09:56:53 +020050
51int
Radek Krejci48464ed2016-03-17 15:44:09 +010052lyv_data_context(const struct lyd_node *node, int options, struct unres_data *unres)
Radek Krejcieab784a2015-08-27 09:56:53 +020053{
Radek Krejci6baaa9a2016-02-23 16:07:12 +010054 const struct lys_node *siter = NULL;
Radek Krejcib1f318b2016-08-22 16:18:37 +020055 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node;
Radek Krejci4a49bdf2016-01-12 17:17:01 +010056
Michal Vaskocf024702015-10-08 15:01:42 +020057 assert(node);
Radek Krejci03b71f72016-03-16 11:10:09 +010058 assert(unres);
Radek Krejcieab784a2015-08-27 09:56:53 +020059
60 /* check if the node instance is enabled by if-feature */
Michal Vaskocf024702015-10-08 15:01:42 +020061 if (lys_is_disabled(node->schema, 2)) {
Radek Krejci48464ed2016-03-17 15:44:09 +010062 LOGVAL(LYE_INELEM, LY_VLOG_LYD, node, node->schema->name);
Michal Vaskocf024702015-10-08 15:01:42 +020063 return EXIT_FAILURE;
64 }
65
Radek Krejci0b7704f2016-03-18 12:16:14 +010066 /* check leafref/instance-identifier */
Radek Krejcib1f318b2016-08-22 16:18:37 +020067 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
68 if (options & (LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) {
69 /* if leafref or instance-identifier, parse the value according to the
70 * target's type, because the target leaf does not need to be present */
71 if (leaf->value_type == LY_TYPE_LEAFREF || leaf->value_type == LY_TYPE_INST) {
72 memset(&leaf->value, 0, sizeof leaf->value);
73 if (lyp_parse_value_type(leaf, &((struct lys_node_leaf *)leaf->schema)->type, 0)) {
74 return EXIT_FAILURE;
75 }
Radek Krejci0b7704f2016-03-18 12:16:14 +010076 }
Radek Krejcib1f318b2016-08-22 16:18:37 +020077 } else {
78 /* if leafref or instance-identifier, store the node for later resolving */
79 if (leaf->value_type == LY_TYPE_LEAFREF && !leaf->value.leafref) {
80 if (unres_data_add(unres, (struct lyd_node *)node, UNRES_LEAFREF)) {
81 return EXIT_FAILURE;
82 }
83 } else if (leaf->value_type == LY_TYPE_INST) {
84 if (unres_data_add(unres, (struct lyd_node *)node, UNRES_INSTID)) {
85 return EXIT_FAILURE;
86 }
Radek Krejci0b7704f2016-03-18 12:16:14 +010087 }
Michal Vasko0d182ba2015-10-09 09:29:14 +020088 }
Radek Krejci0b7704f2016-03-18 12:16:14 +010089 }
90
Michal Vaskocf024702015-10-08 15:01:42 +020091 /* check all relevant when conditions */
Radek Krejci66853d02016-09-05 17:03:14 +020092 if ((node->when_status & LYD_WHEN) &&
Radek Krejcif833a582016-09-06 09:21:59 +020093 (!(options & LYD_OPT_TYPEMASK) || (options & (LYD_OPT_CONFIG | LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) {
Radek Krejci0b7704f2016-03-18 12:16:14 +010094 if (unres_data_add(unres, (struct lyd_node *)node, UNRES_WHEN)) {
Michal Vasko0d182ba2015-10-09 09:29:14 +020095 return EXIT_FAILURE;
96 }
Radek Krejcieab784a2015-08-27 09:56:53 +020097 }
98
99 /* check for (non-)presence of status data in edit-config data */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100100 if ((options & (LYD_OPT_EDIT | LYD_OPT_GETCONFIG | LYD_OPT_CONFIG)) && (node->schema->flags & LYS_CONFIG_R)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100101 LOGVAL(LYE_INELEM, LY_VLOG_LYD, node, node->schema->name);
Radek Krejcieab784a2015-08-27 09:56:53 +0200102 return EXIT_FAILURE;
103 }
104
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100105 /* check elements order in case of RPC's input and output */
Michal Vaskob1b19442016-07-13 12:26:01 +0200106 if (node->validity && lyp_is_rpc_action(node->schema)) {
Michal Vasko15e0bab2016-02-24 13:58:21 +0100107 if ((node->prev != node) && node->prev->next) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200108 for (siter = lys_getnext(node->schema, lys_parent(node->schema), node->schema->module, 0);
Radek Krejci6baaa9a2016-02-23 16:07:12 +0100109 siter;
Michal Vaskodcf98e62016-05-05 17:53:53 +0200110 siter = lys_getnext(siter, lys_parent(siter), siter->module, 0)) {
Radek Krejci6baaa9a2016-02-23 16:07:12 +0100111 if (siter == node->prev->schema) {
112 /* data predecessor has the schema node after
113 * the schema node of the data node being checked */
Radek Krejci48464ed2016-03-17 15:44:09 +0100114 LOGVAL(LYE_INORDER, LY_VLOG_LYD, node, node->schema->name, siter->name);
Radek Krejci6baaa9a2016-02-23 16:07:12 +0100115 return EXIT_FAILURE;
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100116 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100117 }
118
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100119 }
120 }
121
Radek Krejcieab784a2015-08-27 09:56:53 +0200122 return EXIT_SUCCESS;
123}
Michal Vaskocf024702015-10-08 15:01:42 +0200124
Radek Krejci63b79c82016-08-10 10:09:33 +0200125struct eq_item {
126 struct lyd_node *node;
127 uint32_t hash;
128 uint32_t over;
129};
130
131static int
132eq_table_insert(struct eq_item *table, struct lyd_node *node, uint32_t hash, uint32_t tablesize, int action)
133{
134 uint32_t i, c;
135
136 if (table[hash].node) {
137 /* is it collision or is the cell just filled by an overflow item? */
138 for (i = hash; table[i].node && table[i].hash != hash; i = (i + 1) % tablesize);
139 if (!table[i].node) {
140 goto first;
141 }
142
143 /* collision or instance duplication */
144 c = table[i].over;
145 do {
146 if (table[i].hash != hash) {
147 i = (i + 1) % tablesize;
148 continue;
149 }
150
151 /* compare nodes */
152 if (lyd_list_equal(node, table[i].node, action, 1)) {
153 /* instance duplication */
154 return EXIT_FAILURE;
155 }
156 } while (c--);
157
158 /* collision, insert item into next free cell */
159 table[hash].over++;
160 for (i = (i + 1) % tablesize; table[i].node; i = (i + 1) % tablesize);
161 table[i].hash = hash;
162 table[i].node = node;
163 } else {
164first:
165 /* first hash instance */
166 table[hash].node = node;
167 table[hash].hash = hash;
168 }
169
170 return EXIT_SUCCESS;
171}
172
173int
174lyv_data_unique(struct lyd_node *node, struct lyd_node *start)
175{
176 struct lyd_node *diter, *key;
177 struct lys_node_list *slist;
178 struct ly_set *set;
179 int i, j, n = 0, ret = EXIT_SUCCESS;
180 uint32_t hash, u, usize = 0, hashmask;
181 struct eq_item *keystable = NULL, **uniquetables = NULL;
182 const char *id;
183
184 /* get the first list/leaflist instance sibling */
185 if (!start) {
186 start = lyd_first_sibling(node);
187 }
188
189 /* check uniqueness of the list/leaflist instances (compare values) */
190 set = ly_set_new();
191 for (diter = start; diter; diter = diter->next) {
192 if (diter->schema != node->schema) {
193 /* check only instances of the same list/leaflist */
194 continue;
195 }
196
197 /* remove the flag */
198 diter->validity &= ~LYD_VAL_UNIQUE;
199
200 /* store for comparison */
201 ly_set_add(set, diter, LY_SET_OPT_USEASLIST);
202 }
203
204 if (set->number == 2) {
205 /* simple comparison */
206 if (lyd_list_equal(set->set.d[0], set->set.d[1], -1, 1)) {
207 /* instance duplication */
208 ly_set_free(set);
209 return EXIT_FAILURE;
210 }
211 } else if (set->number > 2) {
212 /* use hashes for comparison */
213 /* first, allocate the table, the size depends on number of items in the set */
214 for (u = 31; u > 0; u--) {
215 usize = set->number << u;
216 usize = usize >> u;
217 if (usize == set->number) {
218 break;
219 }
220 }
221 if (u == 0) {
222 usize = hashmask = 0xffffffff;
223 } else {
224 u = 32 - u;
225 usize = 1 << u;
226 hashmask = usize - 1;
227 }
228 keystable = calloc(usize, sizeof *keystable);
229 if (!keystable) {
230 LOGMEM;
231 ret = EXIT_FAILURE;
232 goto unique_cleanup;
233 }
234 n = 0;
235 if (node->schema->nodetype == LYS_LIST) {
236 n = ((struct lys_node_list *)node->schema)->unique_size;
237 uniquetables = malloc(n * sizeof *uniquetables);
238 if (!uniquetables) {
239 LOGMEM;
240 ret = EXIT_FAILURE;
Radek Krejci70292d22016-08-15 09:39:22 +0200241 n = 0;
Radek Krejci63b79c82016-08-10 10:09:33 +0200242 goto unique_cleanup;
243 }
244 for (j = 0; j < n; j++) {
245 uniquetables[j] = calloc(usize, sizeof **uniquetables);
246 if (!uniquetables[j]) {
247 LOGMEM;
248 ret = EXIT_FAILURE;
249 goto unique_cleanup;
250 }
251 }
252 }
253
254 for (u = 0; u < set->number; u++) {
255 /* get the hash for the instance - keys */
256 if (node->schema->nodetype == LYS_LEAFLIST) {
257 id = ((struct lyd_node_leaf_list *)set->set.d[u])->value_str;
258 hash = dict_hash_multi(0, id, strlen(id));
259 } else { /* LYS_LIST */
260 for (hash = i = 0, key = set->set.d[u]->child;
261 i < ((struct lys_node_list *)set->set.d[u]->schema)->keys_size;
262 i++, key = key->next) {
263 id = ((struct lyd_node_leaf_list *)key)->value_str;
264 hash = dict_hash_multi(hash, id, strlen(id));
265 }
266 }
267 /* finish the hash value */
268 hash = dict_hash_multi(hash, NULL, 0) & hashmask;
269
270 /* insert into the hashtable */
271 if (eq_table_insert(keystable, set->set.d[u], hash, usize, 0)) {
272 ret = EXIT_FAILURE;
273 goto unique_cleanup;
274 }
275
276 /* and the same loop for unique (n is !0 only in case of list) - get the hash for the instances */
277 for (j = 0; j < n; j++) {
278 slist = (struct lys_node_list *)node->schema;
Radek Krejci50dc7ae2016-09-01 09:56:37 +0200279 id = NULL;
Radek Krejci63b79c82016-08-10 10:09:33 +0200280 for (i = hash = 0; i < slist->unique[j].expr_size; i++) {
281 diter = resolve_data_descendant_schema_nodeid(slist->unique[j].expr[i], set->set.d[u]->child);
282 if (diter) {
283 id = ((struct lyd_node_leaf_list *)diter)->value_str;
284 } else {
285 /* use default value */
Radek Krejcid5a5c282016-08-15 15:38:08 +0200286 id = lyd_get_unique_default(slist->unique[j].expr[i], set->set.d[u]);
Radek Krejci63b79c82016-08-10 10:09:33 +0200287 if (ly_errno) {
288 ret = EXIT_FAILURE;
289 goto unique_cleanup;
290 }
291 }
Radek Krejci2a5f52b2016-08-15 09:14:36 +0200292 if (!id) {
293 /* unique item not present nor has default value */
294 break;
295 }
Radek Krejci63b79c82016-08-10 10:09:33 +0200296 hash = dict_hash_multi(hash, id, strlen(id));
297 }
Radek Krejci2a5f52b2016-08-15 09:14:36 +0200298 if (!id) {
299 /* skip this list instance since its unique set is incomplete */
300 continue;
301 }
Radek Krejci63b79c82016-08-10 10:09:33 +0200302
303 /* finish the hash value */
304 hash = dict_hash_multi(hash, NULL, 0) & hashmask;
305
306 /* insert into the hashtable */
307 if (eq_table_insert(uniquetables[j], set->set.d[u], hash, usize, j + 1)) {
308 ret = EXIT_FAILURE;
309 goto unique_cleanup;
310 }
311 }
312 }
313 }
314
315unique_cleanup:
316 /* cleanup */
317 ly_set_free(set);
318 free(keystable);
319 for (j = 0; j < n; j++) {
320 if (!uniquetables[j]) {
321 /* failed when allocating uniquetables[j], following j are not allocated */
322 break;
323 }
324 free(uniquetables[j]);
325 }
326 free(uniquetables);
327
328 return ret;
329}
330
Radek Krejcieab784a2015-08-27 09:56:53 +0200331int
Radek Krejci48464ed2016-03-17 15:44:09 +0100332lyv_data_content(struct lyd_node *node, int options, struct unres_data *unres)
Radek Krejcieab784a2015-08-27 09:56:53 +0200333{
Michal Vasko1e62a092015-12-01 12:27:20 +0100334 const struct lys_node *schema, *siter;
Radek Krejcid788a522016-07-25 14:57:38 +0200335 struct lyd_node *diter, *start = NULL;
Radek Krejcicf509982015-12-15 09:22:44 +0100336 struct lys_ident *ident;
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100337 struct lys_tpdf *tpdf;
Radek Krejcie6a71b52016-08-10 15:11:16 +0200338 struct lys_type *type = NULL;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200339 struct lyd_node_leaf_list *leaf;
Radek Krejcie6a71b52016-08-10 15:11:16 +0200340 int i, j = 0;
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200341 uint8_t iff_size;
342 struct lys_iffeature *iff;
343 const char *id, *idname;
Radek Krejcieab784a2015-08-27 09:56:53 +0200344
345 assert(node);
346 assert(node->schema);
Radek Krejci0b7704f2016-03-18 12:16:14 +0100347 assert(unres);
Radek Krejcieab784a2015-08-27 09:56:53 +0200348
349 schema = node->schema; /* shortcut */
350
Radek Krejcid788a522016-07-25 14:57:38 +0200351 if (node->validity & LYD_VAL_MAND) {
Radek Krejci702deb82016-03-09 12:37:57 +0100352 /* check presence and correct order of all keys in case of list */
Radek Krejci92ece002016-04-04 15:45:05 +0200353 if (schema->nodetype == LYS_LIST && !(options & (LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100354 if (lyv_keys(node)) {
Radek Krejcica7efb72016-01-18 13:06:01 +0100355 return EXIT_FAILURE;
Radek Krejcieab784a2015-08-27 09:56:53 +0200356 }
357 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200358
Radek Krejci46165822016-08-26 14:06:27 +0200359 if (schema->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYDATA)) {
Radek Krejcid788a522016-07-25 14:57:38 +0200360 /* check number of instances (similar to list uniqueness) for non-list nodes */
Radek Krejci3e0addb2015-08-27 16:37:59 +0200361
Radek Krejcica7efb72016-01-18 13:06:01 +0100362 /* find duplicity */
Radek Krejcid788a522016-07-25 14:57:38 +0200363 start = lyd_first_sibling(node);
Radek Krejcica7efb72016-01-18 13:06:01 +0100364 for (diter = start; diter; diter = diter->next) {
365 if (diter->schema == schema && diter != node) {
Radek Krejci92ece002016-04-04 15:45:05 +0200366 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, node, schema->name,
Michal Vaskodcf98e62016-05-05 17:53:53 +0200367 lys_parent(schema) ? lys_parent(schema)->name : "data tree");
Radek Krejci92ece002016-04-04 15:45:05 +0200368 return EXIT_FAILURE;
Radek Krejcieab784a2015-08-27 09:56:53 +0200369 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200370 }
Radek Krejcid788a522016-07-25 14:57:38 +0200371 }
Radek Krejcieab784a2015-08-27 09:56:53 +0200372
Radek Krejcid788a522016-07-25 14:57:38 +0200373 /* remove the flag */
374 node->validity &= ~LYD_VAL_MAND;
375 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100376
Radek Krejci63b79c82016-08-10 10:09:33 +0200377 if (!(options & (LYD_OPT_GET | LYD_OPT_GETCONFIG))) {
378 /* skip key uniqueness check in case of get/get-config data */
379 if (schema->nodetype & (LYS_LIST | LYS_CONTAINER)) {
380 LY_TREE_FOR(schema->child, siter) {
381 if (siter->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
382 LY_TREE_FOR(node->child, diter) {
383 if (diter->schema == siter && (diter->validity & LYD_VAL_UNIQUE)) {
384 if (lyv_data_unique(diter, node->child)) {
385 return EXIT_FAILURE;
386 }
387 /* all schema instances checked, continue with another schema node */
388 break;
389 }
390 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100391 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100392 }
393 }
Radek Krejcid788a522016-07-25 14:57:38 +0200394 }
395
396 if (node->validity) {
Radek Krejcica7efb72016-01-18 13:06:01 +0100397 /* status - of the node's schema node itself and all its parents that
398 * cannot have their own instance (like a choice statement) */
399 siter = node->schema;
400 do {
401 if (((siter->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) && (options & LYD_OPT_OBSOLETE)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100402 LOGVAL(LYE_OBSDATA, LY_VLOG_LYD, node, schema->name);
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100403 return EXIT_FAILURE;
404 }
Michal Vaskodcf98e62016-05-05 17:53:53 +0200405 siter = lys_parent(siter);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200406 } while (siter && !(siter->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)));
Radek Krejcica7efb72016-01-18 13:06:01 +0100407
408 /* status of the identity value */
409 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
410 if (options & LYD_OPT_OBSOLETE) {
411 /* check that we are not instantiating obsolete type */
412 tpdf = ((struct lys_node_leaf *)node->schema)->type.der;
Michal Vaskodcf98e62016-05-05 17:53:53 +0200413 while (tpdf) {
Radek Krejcica7efb72016-01-18 13:06:01 +0100414 if ((tpdf->flags & LYS_STATUS_MASK) == LYS_STATUS_OBSLT) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100415 LOGVAL(LYE_OBSTYPE, LY_VLOG_LYD, node, schema->name, tpdf->name);
Radek Krejcica7efb72016-01-18 13:06:01 +0100416 return EXIT_FAILURE;
417 }
418 tpdf = tpdf->type.der;
419 }
420 }
421 if (((struct lyd_node_leaf_list *)node)->value_type == LY_TYPE_IDENT) {
422 ident = ((struct lyd_node_leaf_list *)node)->value.ident;
Radek Krejcic6556022016-01-27 15:16:45 +0100423 if (lyp_check_status(schema->flags, schema->module, schema->name,
Radek Krejci3cc10962016-04-13 15:03:27 +0200424 ident->flags, ident->module, ident->name, NULL)) {
425 LOGPATH(LY_VLOG_LYD, node);
Radek Krejcica7efb72016-01-18 13:06:01 +0100426 return EXIT_FAILURE;
427 }
428 }
Radek Krejci4eaf5a82015-12-15 15:10:38 +0100429 }
Radek Krejcicf509982015-12-15 09:22:44 +0100430 }
431
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200432 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
433 /* since feature can be enabled/disabled, do this check despite the validity flag,
434 * - check if the type value (enum, bit, identity) is disabled via feature */
435 leaf = (struct lyd_node_leaf_list *)node;
436 switch (leaf->value_type) {
437 case LY_TYPE_BITS:
438 id = "Bit";
439 /* get the count of bits */
440 for (type = &((struct lys_node_leaf *)leaf->schema)->type; !type->info.bits.count; type = &type->der->type);
441 for (j = iff_size = 0; j < type->info.bits.count; j++) {
442 if (!leaf->value.bit[j]) {
443 continue;
444 }
445 idname = leaf->value.bit[j]->name;
446 iff_size = leaf->value.bit[j]->iffeature_size;
447 iff = leaf->value.bit[j]->iffeature;
448 break;
449nextbit:
450 iff_size = 0;
451 }
452 break;
453 case LY_TYPE_ENUM:
454 id = "Enum";
455 idname = leaf->value_str;
456 iff_size = leaf->value.enm->iffeature_size;
457 iff = leaf->value.enm->iffeature;
458 break;
459 case LY_TYPE_IDENT:
460 id = "Identity";
461 idname = leaf->value_str;
462 iff_size = leaf->value.ident->iffeature_size;
463 iff = leaf->value.ident->iffeature;
464 break;
465 default:
466 iff_size = 0;
467 break;
468 }
469
470 if (iff_size) {
471 for (i = 0; i < iff_size; i++) {
472 if (!resolve_iffeature(&iff[i])) {
473 LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, leaf->value_str, schema->name);
474 LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, "%s \"%s\" is disabled by its if-feature condition.",
475 id, idname);
476 return EXIT_FAILURE;
477 }
478 }
479 if (leaf->value_type == LY_TYPE_BITS) {
480 goto nextbit;
481 }
482 }
483 }
484
Michal Vaskobf19d252015-10-08 15:39:17 +0200485 /* check must conditions */
Radek Krejci46165822016-08-26 14:06:27 +0200486 if (resolve_applies_must(node->schema) && unres_data_add(unres, node, UNRES_MUST) == -1) {
Radek Krejci0b7704f2016-03-18 12:16:14 +0100487 return EXIT_FAILURE;
Michal Vaskobf19d252015-10-08 15:39:17 +0200488 }
489
Radek Krejcieab784a2015-08-27 09:56:53 +0200490 return EXIT_SUCCESS;
491}