blob: 3bdd8d4008b977bce010d3fdae4157b8217cf9d5 [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 Krejci859a15a2021-03-05 20:56:59 +010030#include "tree_edit.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#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;
Radek Krejcic7d13e32020-12-09 12:32:24 +0100108 } else if ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100109 /* 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') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100302 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100303 return LY_EVALID;
304 } else if (!isspace(c[i + op_len])) {
305 /* feature name starting with the not/and/or */
306 last_not = 0;
307 f_size++;
308 } else if (c[i] == 'n') { /* not operation */
309 if (last_not) {
310 /* double not */
311 expr_size = expr_size - 2;
312 last_not = 0;
313 } else {
314 last_not = 1;
315 }
316 } else { /* and, or */
317 if (f_exp != f_size) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100318 LOGVAL(ctx, LYVE_SYNTAX_YANG,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100319 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
320 qname->str, op_len, &c[i]);
321 return LY_EVALID;
322 }
323 f_exp++;
324
325 /* not a not operation */
326 last_not = 0;
327 }
328 i += op_len;
329 } else {
330 f_size++;
331 last_not = 0;
332 }
333 expr_size++;
334
335 while (!isspace(c[i])) {
336 if (!c[i] || (c[i] == ')') || (c[i] == '(')) {
337 i--;
338 break;
339 }
340 i++;
341 }
342 }
343 if (j) {
344 /* not matching count of ( and ) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100345 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100346 return LY_EVALID;
347 }
348 if (f_exp != f_size) {
349 /* features do not match the needed arguments for the logical operations */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100350 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - number of features in expression does not match "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100351 "the required number of operands for the operations.", qname->str);
352 return LY_EVALID;
353 }
354
355 if (checkversion || (expr_size > 1)) {
356 /* check that we have 1.1 module */
357 if (qname->mod->version != LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100358 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100359 return LY_EVALID;
360 }
361 }
362
363 /* allocate the memory */
364 LY_ARRAY_CREATE_RET(ctx, iff->features, f_size, LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 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 +0100366 stack.stack = malloc(expr_size * sizeof *stack.stack);
367 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx); rc = LY_EMEM, error);
368
369 stack.size = expr_size;
370 f_size--; expr_size--; /* used as indexes from now */
371
372 for (i--; i >= 0; i--) {
373 if (c[i] == ')') {
374 /* push it on stack */
375 iff_stack_push(&stack, LYS_IFF_RP);
376 continue;
377 } else if (c[i] == '(') {
378 /* pop from the stack into result all operators until ) */
379 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
380 iff_setop(iff->expr, op, expr_size--);
381 }
382 continue;
383 } else if (isspace(c[i])) {
384 continue;
385 }
386
387 /* end of operator or operand -> find beginning and get what is it */
388 j = i + 1;
389 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
390 i--;
391 }
392 i++; /* go back by one step */
393
Radek Krejcif13b87b2020-12-01 22:02:17 +0100394 if (!strncmp(&c[i], "not", ly_strlen_const("not")) && isspace(c[i + ly_strlen_const("not")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100395 if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
396 /* double not */
397 iff_stack_pop(&stack);
398 } else {
399 /* not has the highest priority, so do not pop from the stack
400 * as in case of AND and OR */
401 iff_stack_push(&stack, LYS_IFF_NOT);
402 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100403 } else if (!strncmp(&c[i], "and", ly_strlen_const("and")) && isspace(c[i + ly_strlen_const("and")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100404 /* as for OR - pop from the stack all operators with the same or higher
405 * priority and store them to the result, then push the AND to the stack */
406 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
407 op = iff_stack_pop(&stack);
408 iff_setop(iff->expr, op, expr_size--);
409 }
410 iff_stack_push(&stack, LYS_IFF_AND);
411 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
412 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
413 op = iff_stack_pop(&stack);
414 iff_setop(iff->expr, op, expr_size--);
415 }
416 iff_stack_push(&stack, LYS_IFF_OR);
417 } else {
418 /* feature name, length is j - i */
419
420 /* add it to the expression */
421 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
422
423 /* now get the link to the feature definition */
424 f = lysp_feature_find(qname->mod, &c[i], j - i, 1);
425 if (!f) {
Radek Krejci422afb12021-03-04 16:38:16 +0100426 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".",
427 qname->str, (int)(j - i), &c[i]);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100428 rc = LY_EVALID;
429 goto error;
430 }
431 iff->features[f_size] = f;
432 LY_ARRAY_INCREMENT(iff->features);
433 f_size--;
434 }
435 }
436 while (stack.index) {
437 op = iff_stack_pop(&stack);
438 iff_setop(iff->expr, op, expr_size--);
439 }
440
441 if (++expr_size || ++f_size) {
442 /* not all expected operators and operands found */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100443 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - processing error.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100444 rc = LY_EINT;
445 } else {
446 rc = LY_SUCCESS;
447 }
448
449error:
450 /* cleanup */
451 iff_stack_clean(&stack);
452
453 return rc;
454}
455
456LY_ERR
457lys_eval_iffeatures(const struct ly_ctx *ctx, struct lysp_qname *iffeatures, ly_bool *enabled)
458{
459 LY_ERR ret;
460 struct lysc_iffeature iff = {0};
461
462 if (!iffeatures) {
463 *enabled = 1;
464 return LY_SUCCESS;
465 }
466
467 LY_CHECK_RET(lys_compile_iffeature(ctx, iffeatures, &iff));
468
469 ret = lysc_iffeature_value(&iff);
470 lysc_iffeature_free((struct ly_ctx *)ctx, &iff);
471 if (ret == LY_ENOT) {
472 *enabled = 0;
473 } else if (ret) {
474 return ret;
475 } else {
476 *enabled = 1;
477 }
478
479 return LY_SUCCESS;
480}
481
Michal Vasko08c8b272020-11-24 18:11:30 +0100482/**
483 * @brief Check whether all enabled features have their if-features satisfied.
484 * Enabled features with false if-features are disabled with a warning.
485 *
486 * @param[in] pmod Parsed module features to check.
487 * @return LY_ERR value.
488 */
489static LY_ERR
490lys_check_features(struct lysp_module *pmod)
491{
492 LY_ERR r;
493 uint32_t i = 0;
494 struct lysp_feature *f = NULL;
495
496 while ((f = lysp_feature_next(f, pmod, &i))) {
497 if (!(f->flags & LYS_FENABLED) || !f->iffeatures) {
498 /* disabled feature or no if-features to check */
499 continue;
500 }
501
502 assert(f->iffeatures_c);
503 r = lysc_iffeature_value(f->iffeatures_c);
504 if (r == LY_ENOT) {
505 LOGWRN(pmod->mod->ctx, "Feature \"%s\" cannot be enabled because its \"if-feature\" is not satisfied.",
506 f->name);
507
508 /* disable feature and re-evaluate all the feature if-features again */
509 f->flags &= ~LYS_FENABLED;
510 return lys_check_features(pmod);
511 } else if (r) {
512 return r;
513 } /* else if-feature satisfied */
514 }
515
516 return LY_SUCCESS;
517}
518
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100519LY_ERR
520lys_enable_features(struct lysp_module *pmod, const char **features)
521{
Michal Vasko08c8b272020-11-24 18:11:30 +0100522 uint32_t i = 0;
523 struct lysp_feature *f = 0;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100524
525 if (!features) {
526 /* keep all features disabled */
527 return LY_SUCCESS;
528 }
529
530 if (!strcmp(features[0], "*")) {
531 /* enable all features */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100532 while ((f = lysp_feature_next(f, pmod, &i))) {
533 f->flags |= LYS_FENABLED;
534 }
535 } else {
536 /* enable selected features */
537 for (i = 0; features[i]; ++i) {
538 /* find the feature */
539 f = lysp_feature_find(pmod, features[i], strlen(features[i]), 0);
540 if (!f) {
541 LOGERR(pmod->mod->ctx, LY_ENOTFOUND, "Feature \"%s\" not found in module \"%s\".", features[i],
542 pmod->mod->name);
543 return LY_ENOTFOUND;
544 }
545
546 /* enable feature */
547 f->flags |= LYS_FENABLED;
548 }
549 }
550
Michal Vasko08c8b272020-11-24 18:11:30 +0100551 /* check final features if-feature state */
552 return lys_check_features(pmod);
553}
554
555LY_ERR
556lys_set_features(struct lysp_module *pmod, const char **features)
557{
558 uint32_t i = 0, j;
559 struct lysp_feature *f = 0;
560 ly_bool change = 0;
561
562 if (!features) {
Radek Krejci2415f882021-01-20 16:27:09 +0100563 /* do not touch the features */
564
565 } else if (!features[0]) {
Michal Vasko08c8b272020-11-24 18:11:30 +0100566 /* disable all the features */
567 while ((f = lysp_feature_next(f, pmod, &i))) {
568 if (f->flags & LYS_FENABLED) {
569 f->flags &= ~LYS_FENABLED;
570 change = 1;
571 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100572 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100573 } else if (!strcmp(features[0], "*")) {
574 /* enable all the features */
575 while ((f = lysp_feature_next(f, pmod, &i))) {
576 if (!(f->flags & LYS_FENABLED)) {
577 f->flags |= LYS_FENABLED;
578 change = 1;
579 }
580 }
581 } else {
582 /* enable specific features, disable the rest */
583 while ((f = lysp_feature_next(f, pmod, &i))) {
584 for (j = 0; features[j]; ++j) {
585 if (!strcmp(f->name, features[j])) {
586 break;
587 }
588 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100589
Michal Vasko08c8b272020-11-24 18:11:30 +0100590 if (features[j] && !(f->flags & LYS_FENABLED)) {
591 /* enable */
592 f->flags |= LYS_FENABLED;
593 change = 1;
594 } else if (!features[j] && (f->flags & LYS_FENABLED)) {
595 /* disable */
596 f->flags &= ~LYS_FENABLED;
597 change = 1;
598 }
599 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100600 }
601
Michal Vasko08c8b272020-11-24 18:11:30 +0100602 if (!change) {
603 /* features already set correctly */
604 return LY_EEXIST;
605 }
606
607 /* check final features if-feature state */
608 return lys_check_features(pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100609}
610
611/**
612 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
613 *
614 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
615 *
616 * @param[in] ctx Compile context for logging.
617 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
618 * being currently processed).
619 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
620 * @return LY_SUCCESS if everything is ok.
621 * @return LY_EVALID if the feature references indirectly itself.
622 */
623static LY_ERR
624lys_compile_feature_circular_check(const struct ly_ctx *ctx, struct lysp_feature *feature, struct lysp_feature **depfeatures)
625{
626 LY_ERR ret = LY_SUCCESS;
627 LY_ARRAY_COUNT_TYPE u, v;
628 struct ly_set recursion = {0};
629 struct lysp_feature *drv;
630
631 if (!depfeatures) {
632 return LY_SUCCESS;
633 }
634
635 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
636 if (feature == depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100637 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100638 ret = LY_EVALID;
639 goto cleanup;
640 }
641 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
642 LY_CHECK_GOTO(ret, cleanup);
643 }
644
645 for (v = 0; v < recursion.count; ++v) {
646 drv = recursion.objs[v];
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100647 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
648 if (feature == drv->depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100649 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100650 ret = LY_EVALID;
651 goto cleanup;
652 }
653 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
654 LY_CHECK_GOTO(ret, cleanup);
655 }
656 }
657
658cleanup:
659 ly_set_erase(&recursion, NULL);
660 return ret;
661}
662
663LY_ERR
664lys_compile_feature_iffeatures(struct lysp_module *pmod)
665{
666 LY_ARRAY_COUNT_TYPE u, v;
667 struct lysp_feature *f = NULL, **df;
668 uint32_t idx = 0;
669
670 while ((f = lysp_feature_next(f, pmod, &idx))) {
671 if (!f->iffeatures) {
672 continue;
673 }
674
675 /* compile if-features */
676 LY_ARRAY_CREATE_RET(pmod->mod->ctx, f->iffeatures_c, LY_ARRAY_COUNT(f->iffeatures), LY_EMEM);
677 LY_ARRAY_FOR(f->iffeatures, u) {
678 LY_ARRAY_INCREMENT(f->iffeatures_c);
679 LY_CHECK_RET(lys_compile_iffeature(pmod->mod->ctx, &(f->iffeatures)[u], &(f->iffeatures_c)[u]));
680 }
681 LY_ARRAY_FOR(f->iffeatures_c, u) {
682 LY_ARRAY_FOR(f->iffeatures_c[u].features, v) {
683 /* check for circular dependency - direct reference first,... */
684 if (f == f->iffeatures_c[u].features[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100685 LOGVAL(pmod->mod->ctx, LYVE_REFERENCE, "Feature \"%s\" is referenced from itself.", f->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100686 return LY_EVALID;
687 }
688 /* ... and indirect circular reference */
689 LY_CHECK_RET(lys_compile_feature_circular_check(pmod->mod->ctx, f->iffeatures_c[u].features[v], f->depfeatures));
690
691 /* add itself into the dependants list */
692 LY_ARRAY_NEW_RET(pmod->mod->ctx, f->iffeatures_c[u].features[v]->depfeatures, df, LY_EMEM);
693 *df = f;
694 }
695 }
696 }
697
698 return LY_SUCCESS;
699}
700
701void
702lys_free_feature_iffeatures(struct lysp_module *pmod)
703{
704 struct lysp_feature *f = NULL;
705 uint32_t idx = 0;
706
707 while ((f = lysp_feature_next(f, pmod, &idx))) {
708 FREE_ARRAY(pmod->mod->ctx, f->iffeatures_c, lysc_iffeature_free);
709 f->iffeatures_c = NULL;
710 LY_ARRAY_FREE(f->depfeatures);
711 f->depfeatures = NULL;
712 }
713}