blob: 16ccaed422badadfa5613a7bf0881fbe3ae5f9c9 [file] [log] [blame]
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001/**
2 * @file schema_features.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema feature handling
5 *
6 * Copyright (c) 2015 - 2020 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 */
14
15#define _GNU_SOURCE
16
17#include "schema_features.h"
18
19#include <assert.h>
20#include <ctype.h>
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010021#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010025
26#include "common.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010027#include "log.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010028#include "set.h"
29#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "tree_data.h"
31#include "tree_schema.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010032#include "tree_schema_internal.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010033
Radek Krejcif13b87b2020-12-01 22:02:17 +010034#define IFF_RECORDS_IN_BYTE 4
35#define IFF_RECORD_BITS 2
36#define IFF_RECORD_MASK 0x3
37
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010038uint8_t
39lysc_iff_getop(uint8_t *list, size_t pos)
40{
41 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +010042 uint8_t mask = IFF_RECORD_MASK, result;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010043
Radek Krejcif13b87b2020-12-01 22:02:17 +010044 item = &list[pos / IFF_RECORDS_IN_BYTE];
45 result = (*item) & (mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE));
46 return result >> IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010047}
48
49static LY_ERR
50lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
51{
52 uint8_t op;
53 LY_ERR a, b;
54
55 op = lysc_iff_getop(iff->expr, *index_e);
56 (*index_e)++;
57
58 switch (op) {
59 case LYS_IFF_F:
60 /* resolve feature */
61 return (iff->features[(*index_f)++]->flags & LYS_FENABLED) ? LY_SUCCESS : LY_ENOT;
62 case LYS_IFF_NOT:
63 /* invert result */
64 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
65 case LYS_IFF_AND:
66 case LYS_IFF_OR:
67 a = lysc_iffeature_value_(iff, index_e, index_f);
68 b = lysc_iffeature_value_(iff, index_e, index_f);
69 if (op == LYS_IFF_AND) {
70 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
71 return LY_SUCCESS;
72 } else {
73 return LY_ENOT;
74 }
75 } else { /* LYS_IFF_OR */
76 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
77 return LY_SUCCESS;
78 } else {
79 return LY_ENOT;
80 }
81 }
82 }
83
84 return LY_ENOT;
85}
86
87API LY_ERR
88lysc_iffeature_value(const struct lysc_iffeature *iff)
89{
90 size_t index_e = 0, index_f = 0;
91
92 LY_CHECK_ARG_RET(NULL, iff, LY_EINVAL);
93
94 if (iff->expr) {
95 return lysc_iffeature_value_(iff, &index_e, &index_f);
96 }
97 return LY_ENOT;
98}
99
100API struct lysp_feature *
101lysp_feature_next(const struct lysp_feature *last, const struct lysp_module *pmod, uint32_t *idx)
102{
103 struct lysp_feature *features;
104
105 if (!*idx) {
106 /* module features */
107 features = pmod->features;
108 } else if (pmod->includes && ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes))) {
109 /* submodule features */
110 features = pmod->includes[*idx - 1].submodule->features;
111 } else {
112 /* no more features */
113 return NULL;
114 }
115
116 /* get the next feature */
117 if (features && (!last || (&features[LY_ARRAY_COUNT(features) - 1] != last))) {
118 return !last ? &features[0] : (struct lysp_feature *)last + 1;
119 }
120
121 /* no more features in current (sub)module */
122 ++(*idx);
123 return lysp_feature_next(NULL, pmod, idx);
124}
125
126/**
127 * @brief Find a feature of the given name and referenced in the given module.
128 *
129 * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature).
130 * @param[in] name Name of the feature including possible prefix.
131 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
132 * @param[in] prefixed Whether the feature name can be prefixed.
133 * @return Pointer to the feature structure if found, NULL otherwise.
134 */
135static struct lysp_feature *
136lysp_feature_find(const struct lysp_module *pmod, const char *name, size_t len, ly_bool prefixed)
137{
138 const struct lys_module *mod;
139 const char *ptr;
140 struct lysp_feature *f = NULL;
141 uint32_t idx = 0;
142
143 assert(pmod);
144
145 if (prefixed && (ptr = ly_strnchr(name, ':', len))) {
146 /* we have a prefixed feature */
147 mod = ly_resolve_prefix(pmod->mod->ctx, name, ptr - name, LY_PREF_SCHEMA, (void *)pmod);
148 LY_CHECK_RET(!mod, NULL);
149
150 pmod = mod->parsed;
151 len = len - (ptr - name) - 1;
152 name = ptr + 1;
153 }
154
Michal Vaskoecd94572020-12-03 14:15:58 +0100155 /* feature without prefix, look in main module and all submodules */
156 if (pmod->is_submod) {
157 pmod = pmod->mod->parsed;
158 }
159
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100160 /* we have the correct module, get the feature */
161 while ((f = lysp_feature_next(f, pmod, &idx))) {
162 if (!ly_strncmp(f->name, name, len)) {
163 return f;
164 }
165 }
166
167 return NULL;
168}
169
170API LY_ERR
171lys_feature_value(const struct lys_module *module, const char *feature)
172{
173 const struct lysp_feature *f;
174
175 LY_CHECK_ARG_RET(NULL, module, module->parsed, feature, LY_EINVAL);
176
177 /* search for the specified feature */
178 f = lysp_feature_find(module->parsed, feature, strlen(feature), 0);
179 LY_CHECK_RET(!f, LY_ENOTFOUND);
180
181 /* feature disabled */
182 if (!(f->flags & LYS_FENABLED)) {
183 return LY_ENOT;
184 }
185
186 /* feature enabled */
187 return LY_SUCCESS;
188}
189
190/**
191 * @brief Stack for processing if-feature expressions.
192 */
193struct iff_stack {
194 size_t size; /**< number of items in the stack */
195 size_t index; /**< first empty item */
196 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
197};
Radek Krejcif13b87b2020-12-01 22:02:17 +0100198#define IFF_STACK_SIZE_STEP 4
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100199
200/**
201 * @brief Add @ref ifftokens into the stack.
202 * @param[in] stack The if-feature stack to use.
203 * @param[in] value One of the @ref ifftokens to store in the stack.
204 * @return LY_EMEM in case of memory allocation error
205 * @return LY_ESUCCESS if the value successfully stored.
206 */
207static LY_ERR
208iff_stack_push(struct iff_stack *stack, uint8_t value)
209{
210 if (stack->index == stack->size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100211 stack->size += IFF_STACK_SIZE_STEP;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100212 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
213 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
214 }
215 stack->stack[stack->index++] = value;
216 return LY_SUCCESS;
217}
218
219/**
220 * @brief Get (and remove) the last item form the stack.
221 * @param[in] stack The if-feature stack to use.
222 * @return The value from the top of the stack.
223 */
224static uint8_t
225iff_stack_pop(struct iff_stack *stack)
226{
227 assert(stack && stack->index);
228
229 stack->index--;
230 return stack->stack[stack->index];
231}
232
233/**
234 * @brief Clean up the stack.
235 * @param[in] stack The if-feature stack to use.
236 */
237static void
238iff_stack_clean(struct iff_stack *stack)
239{
240 stack->size = 0;
241 free(stack->stack);
242}
243
244/**
245 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
246 * (libyang format of the if-feature expression).
247 * @param[in,out] list The 2bits array to modify.
248 * @param[in] op The operand (@ref ifftokens) to store.
249 * @param[in] pos Position (0-based) where to store the given @p op.
250 */
251static void
252iff_setop(uint8_t *list, uint8_t op, size_t pos)
253{
254 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 uint8_t mask = IFF_RECORD_MASK;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100256
Radek Krejcif13b87b2020-12-01 22:02:17 +0100257 assert(op <= IFF_RECORD_MASK); /* max 2 bits */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100258
Radek Krejcif13b87b2020-12-01 22:02:17 +0100259 item = &list[pos / IFF_RECORDS_IN_BYTE];
260 mask = mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100261 *item = (*item) & ~mask;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100262 *item = (*item) | (op << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100263}
264
265#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
266#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
267
268static LY_ERR
269lys_compile_iffeature(const struct ly_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
270{
271 LY_ERR rc = LY_SUCCESS;
272 const char *c = qname->str;
273 int64_t i, j;
274 int8_t op_len, last_not = 0, checkversion = 0;
275 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
276 uint8_t op;
277 struct iff_stack stack = {0, 0, NULL};
278 struct lysp_feature *f;
279
280 assert(c);
281
282 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
283 for (i = j = 0; c[i]; i++) {
284 if (c[i] == '(') {
285 j++;
286 checkversion = 1;
287 continue;
288 } else if (c[i] == ')') {
289 j--;
290 continue;
291 } else if (isspace(c[i])) {
292 checkversion = 1;
293 continue;
294 }
295
Radek Krejcif13b87b2020-12-01 22:02:17 +0100296 if (!strncmp(&c[i], "not", op_len = ly_strlen_const("not")) ||
297 !strncmp(&c[i], "and", op_len = ly_strlen_const("and")) ||
298 !strncmp(&c[i], "or", op_len = ly_strlen_const("or"))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100299 uint64_t spaces;
300 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {}
301 if (c[i + op_len + spaces] == '\0') {
302 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
303 "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
304 return LY_EVALID;
305 } else if (!isspace(c[i + op_len])) {
306 /* feature name starting with the not/and/or */
307 last_not = 0;
308 f_size++;
309 } else if (c[i] == 'n') { /* not operation */
310 if (last_not) {
311 /* double not */
312 expr_size = expr_size - 2;
313 last_not = 0;
314 } else {
315 last_not = 1;
316 }
317 } else { /* and, or */
318 if (f_exp != f_size) {
319 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
320 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
321 qname->str, op_len, &c[i]);
322 return LY_EVALID;
323 }
324 f_exp++;
325
326 /* not a not operation */
327 last_not = 0;
328 }
329 i += op_len;
330 } else {
331 f_size++;
332 last_not = 0;
333 }
334 expr_size++;
335
336 while (!isspace(c[i])) {
337 if (!c[i] || (c[i] == ')') || (c[i] == '(')) {
338 i--;
339 break;
340 }
341 i++;
342 }
343 }
344 if (j) {
345 /* not matching count of ( and ) */
346 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
347 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", qname->str);
348 return LY_EVALID;
349 }
350 if (f_exp != f_size) {
351 /* features do not match the needed arguments for the logical operations */
352 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
353 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
354 "the required number of operands for the operations.", qname->str);
355 return LY_EVALID;
356 }
357
358 if (checkversion || (expr_size > 1)) {
359 /* check that we have 1.1 module */
360 if (qname->mod->version != LYS_VERSION_1_1) {
361 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
362 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", qname->str);
363 return LY_EVALID;
364 }
365 }
366
367 /* allocate the memory */
368 LY_ARRAY_CREATE_RET(ctx, iff->features, f_size, LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100369 iff->expr = calloc((j = (expr_size / IFF_RECORDS_IN_BYTE) + ((expr_size % IFF_RECORDS_IN_BYTE) ? 1 : 0)), sizeof *iff->expr);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100370 stack.stack = malloc(expr_size * sizeof *stack.stack);
371 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx); rc = LY_EMEM, error);
372
373 stack.size = expr_size;
374 f_size--; expr_size--; /* used as indexes from now */
375
376 for (i--; i >= 0; i--) {
377 if (c[i] == ')') {
378 /* push it on stack */
379 iff_stack_push(&stack, LYS_IFF_RP);
380 continue;
381 } else if (c[i] == '(') {
382 /* pop from the stack into result all operators until ) */
383 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
384 iff_setop(iff->expr, op, expr_size--);
385 }
386 continue;
387 } else if (isspace(c[i])) {
388 continue;
389 }
390
391 /* end of operator or operand -> find beginning and get what is it */
392 j = i + 1;
393 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
394 i--;
395 }
396 i++; /* go back by one step */
397
Radek Krejcif13b87b2020-12-01 22:02:17 +0100398 if (!strncmp(&c[i], "not", ly_strlen_const("not")) && isspace(c[i + ly_strlen_const("not")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100399 if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
400 /* double not */
401 iff_stack_pop(&stack);
402 } else {
403 /* not has the highest priority, so do not pop from the stack
404 * as in case of AND and OR */
405 iff_stack_push(&stack, LYS_IFF_NOT);
406 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100407 } else if (!strncmp(&c[i], "and", ly_strlen_const("and")) && isspace(c[i + ly_strlen_const("and")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100408 /* as for OR - pop from the stack all operators with the same or higher
409 * priority and store them to the result, then push the AND to the stack */
410 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
411 op = iff_stack_pop(&stack);
412 iff_setop(iff->expr, op, expr_size--);
413 }
414 iff_stack_push(&stack, LYS_IFF_AND);
415 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
416 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
417 op = iff_stack_pop(&stack);
418 iff_setop(iff->expr, op, expr_size--);
419 }
420 iff_stack_push(&stack, LYS_IFF_OR);
421 } else {
422 /* feature name, length is j - i */
423
424 /* add it to the expression */
425 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
426
427 /* now get the link to the feature definition */
428 f = lysp_feature_find(qname->mod, &c[i], j - i, 1);
429 if (!f) {
430 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
431 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", qname->str, j - i, &c[i]);
432 rc = LY_EVALID;
433 goto error;
434 }
435 iff->features[f_size] = f;
436 LY_ARRAY_INCREMENT(iff->features);
437 f_size--;
438 }
439 }
440 while (stack.index) {
441 op = iff_stack_pop(&stack);
442 iff_setop(iff->expr, op, expr_size--);
443 }
444
445 if (++expr_size || ++f_size) {
446 /* not all expected operators and operands found */
447 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_SYNTAX_YANG,
448 "Invalid value \"%s\" of if-feature - processing error.", qname->str);
449 rc = LY_EINT;
450 } else {
451 rc = LY_SUCCESS;
452 }
453
454error:
455 /* cleanup */
456 iff_stack_clean(&stack);
457
458 return rc;
459}
460
461LY_ERR
462lys_eval_iffeatures(const struct ly_ctx *ctx, struct lysp_qname *iffeatures, ly_bool *enabled)
463{
464 LY_ERR ret;
465 struct lysc_iffeature iff = {0};
466
467 if (!iffeatures) {
468 *enabled = 1;
469 return LY_SUCCESS;
470 }
471
472 LY_CHECK_RET(lys_compile_iffeature(ctx, iffeatures, &iff));
473
474 ret = lysc_iffeature_value(&iff);
475 lysc_iffeature_free((struct ly_ctx *)ctx, &iff);
476 if (ret == LY_ENOT) {
477 *enabled = 0;
478 } else if (ret) {
479 return ret;
480 } else {
481 *enabled = 1;
482 }
483
484 return LY_SUCCESS;
485}
486
Michal Vasko08c8b272020-11-24 18:11:30 +0100487/**
488 * @brief Check whether all enabled features have their if-features satisfied.
489 * Enabled features with false if-features are disabled with a warning.
490 *
491 * @param[in] pmod Parsed module features to check.
492 * @return LY_ERR value.
493 */
494static LY_ERR
495lys_check_features(struct lysp_module *pmod)
496{
497 LY_ERR r;
498 uint32_t i = 0;
499 struct lysp_feature *f = NULL;
500
501 while ((f = lysp_feature_next(f, pmod, &i))) {
502 if (!(f->flags & LYS_FENABLED) || !f->iffeatures) {
503 /* disabled feature or no if-features to check */
504 continue;
505 }
506
507 assert(f->iffeatures_c);
508 r = lysc_iffeature_value(f->iffeatures_c);
509 if (r == LY_ENOT) {
510 LOGWRN(pmod->mod->ctx, "Feature \"%s\" cannot be enabled because its \"if-feature\" is not satisfied.",
511 f->name);
512
513 /* disable feature and re-evaluate all the feature if-features again */
514 f->flags &= ~LYS_FENABLED;
515 return lys_check_features(pmod);
516 } else if (r) {
517 return r;
518 } /* else if-feature satisfied */
519 }
520
521 return LY_SUCCESS;
522}
523
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100524LY_ERR
525lys_enable_features(struct lysp_module *pmod, const char **features)
526{
Michal Vasko08c8b272020-11-24 18:11:30 +0100527 uint32_t i = 0;
528 struct lysp_feature *f = 0;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100529
530 if (!features) {
531 /* keep all features disabled */
532 return LY_SUCCESS;
533 }
534
535 if (!strcmp(features[0], "*")) {
536 /* enable all features */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100537 while ((f = lysp_feature_next(f, pmod, &i))) {
538 f->flags |= LYS_FENABLED;
539 }
540 } else {
541 /* enable selected features */
542 for (i = 0; features[i]; ++i) {
543 /* find the feature */
544 f = lysp_feature_find(pmod, features[i], strlen(features[i]), 0);
545 if (!f) {
546 LOGERR(pmod->mod->ctx, LY_ENOTFOUND, "Feature \"%s\" not found in module \"%s\".", features[i],
547 pmod->mod->name);
548 return LY_ENOTFOUND;
549 }
550
551 /* enable feature */
552 f->flags |= LYS_FENABLED;
553 }
554 }
555
Michal Vasko08c8b272020-11-24 18:11:30 +0100556 /* check final features if-feature state */
557 return lys_check_features(pmod);
558}
559
560LY_ERR
561lys_set_features(struct lysp_module *pmod, const char **features)
562{
563 uint32_t i = 0, j;
564 struct lysp_feature *f = 0;
565 ly_bool change = 0;
566
567 if (!features) {
568 /* disable all the features */
569 while ((f = lysp_feature_next(f, pmod, &i))) {
570 if (f->flags & LYS_FENABLED) {
571 f->flags &= ~LYS_FENABLED;
572 change = 1;
573 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100574 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100575 } else if (!strcmp(features[0], "*")) {
576 /* enable all the features */
577 while ((f = lysp_feature_next(f, pmod, &i))) {
578 if (!(f->flags & LYS_FENABLED)) {
579 f->flags |= LYS_FENABLED;
580 change = 1;
581 }
582 }
583 } else {
584 /* enable specific features, disable the rest */
585 while ((f = lysp_feature_next(f, pmod, &i))) {
586 for (j = 0; features[j]; ++j) {
587 if (!strcmp(f->name, features[j])) {
588 break;
589 }
590 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100591
Michal Vasko08c8b272020-11-24 18:11:30 +0100592 if (features[j] && !(f->flags & LYS_FENABLED)) {
593 /* enable */
594 f->flags |= LYS_FENABLED;
595 change = 1;
596 } else if (!features[j] && (f->flags & LYS_FENABLED)) {
597 /* disable */
598 f->flags &= ~LYS_FENABLED;
599 change = 1;
600 }
601 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100602 }
603
Michal Vasko08c8b272020-11-24 18:11:30 +0100604 if (!change) {
605 /* features already set correctly */
606 return LY_EEXIST;
607 }
608
609 /* check final features if-feature state */
610 return lys_check_features(pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100611}
612
613/**
614 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
615 *
616 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
617 *
618 * @param[in] ctx Compile context for logging.
619 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
620 * being currently processed).
621 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
622 * @return LY_SUCCESS if everything is ok.
623 * @return LY_EVALID if the feature references indirectly itself.
624 */
625static LY_ERR
626lys_compile_feature_circular_check(const struct ly_ctx *ctx, struct lysp_feature *feature, struct lysp_feature **depfeatures)
627{
628 LY_ERR ret = LY_SUCCESS;
629 LY_ARRAY_COUNT_TYPE u, v;
630 struct ly_set recursion = {0};
631 struct lysp_feature *drv;
632
633 if (!depfeatures) {
634 return LY_SUCCESS;
635 }
636
637 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
638 if (feature == depfeatures[u]) {
639 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.",
640 feature->name);
641 ret = LY_EVALID;
642 goto cleanup;
643 }
644 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
645 LY_CHECK_GOTO(ret, cleanup);
646 }
647
648 for (v = 0; v < recursion.count; ++v) {
649 drv = recursion.objs[v];
650 if (!drv->depfeatures) {
651 continue;
652 }
653 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
654 if (feature == drv->depfeatures[u]) {
655 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.",
656 feature->name);
657 ret = LY_EVALID;
658 goto cleanup;
659 }
660 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
661 LY_CHECK_GOTO(ret, cleanup);
662 }
663 }
664
665cleanup:
666 ly_set_erase(&recursion, NULL);
667 return ret;
668}
669
670LY_ERR
671lys_compile_feature_iffeatures(struct lysp_module *pmod)
672{
673 LY_ARRAY_COUNT_TYPE u, v;
674 struct lysp_feature *f = NULL, **df;
675 uint32_t idx = 0;
676
677 while ((f = lysp_feature_next(f, pmod, &idx))) {
678 if (!f->iffeatures) {
679 continue;
680 }
681
682 /* compile if-features */
683 LY_ARRAY_CREATE_RET(pmod->mod->ctx, f->iffeatures_c, LY_ARRAY_COUNT(f->iffeatures), LY_EMEM);
684 LY_ARRAY_FOR(f->iffeatures, u) {
685 LY_ARRAY_INCREMENT(f->iffeatures_c);
686 LY_CHECK_RET(lys_compile_iffeature(pmod->mod->ctx, &(f->iffeatures)[u], &(f->iffeatures_c)[u]));
687 }
688 LY_ARRAY_FOR(f->iffeatures_c, u) {
689 LY_ARRAY_FOR(f->iffeatures_c[u].features, v) {
690 /* check for circular dependency - direct reference first,... */
691 if (f == f->iffeatures_c[u].features[v]) {
692 LOGVAL(pmod->mod->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Feature \"%s\" is referenced from itself.",
693 f->name);
694 return LY_EVALID;
695 }
696 /* ... and indirect circular reference */
697 LY_CHECK_RET(lys_compile_feature_circular_check(pmod->mod->ctx, f->iffeatures_c[u].features[v], f->depfeatures));
698
699 /* add itself into the dependants list */
700 LY_ARRAY_NEW_RET(pmod->mod->ctx, f->iffeatures_c[u].features[v]->depfeatures, df, LY_EMEM);
701 *df = f;
702 }
703 }
704 }
705
706 return LY_SUCCESS;
707}
708
709void
710lys_free_feature_iffeatures(struct lysp_module *pmod)
711{
712 struct lysp_feature *f = NULL;
713 uint32_t idx = 0;
714
715 while ((f = lysp_feature_next(f, pmod, &idx))) {
716 FREE_ARRAY(pmod->mod->ctx, f->iffeatures_c, lysc_iffeature_free);
717 f->iffeatures_c = NULL;
718 LY_ARRAY_FREE(f->depfeatures);
719 f->depfeatures = NULL;
720 }
721}