blob: 6deb52cf2629f132bee90b0544a0a9f7e2d50d1d [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"
Radek Krejci859a15a2021-03-05 20:56:59 +010031#include "tree_edit.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "tree_schema.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010033#include "tree_schema_internal.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010034
Radek Krejcif13b87b2020-12-01 22:02:17 +010035#define IFF_RECORDS_IN_BYTE 4
36#define IFF_RECORD_BITS 2
37#define IFF_RECORD_MASK 0x3
38
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010039uint8_t
40lysc_iff_getop(uint8_t *list, size_t pos)
41{
42 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +010043 uint8_t mask = IFF_RECORD_MASK, result;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010044
Radek Krejcif13b87b2020-12-01 22:02:17 +010045 item = &list[pos / IFF_RECORDS_IN_BYTE];
46 result = (*item) & (mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE));
47 return result >> IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010048}
49
50static LY_ERR
51lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
52{
53 uint8_t op;
54 LY_ERR a, b;
55
56 op = lysc_iff_getop(iff->expr, *index_e);
57 (*index_e)++;
58
59 switch (op) {
60 case LYS_IFF_F:
61 /* resolve feature */
62 return (iff->features[(*index_f)++]->flags & LYS_FENABLED) ? LY_SUCCESS : LY_ENOT;
63 case LYS_IFF_NOT:
64 /* invert result */
65 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
66 case LYS_IFF_AND:
67 case LYS_IFF_OR:
68 a = lysc_iffeature_value_(iff, index_e, index_f);
69 b = lysc_iffeature_value_(iff, index_e, index_f);
70 if (op == LYS_IFF_AND) {
71 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
72 return LY_SUCCESS;
73 } else {
74 return LY_ENOT;
75 }
76 } else { /* LYS_IFF_OR */
77 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
78 return LY_SUCCESS;
79 } else {
80 return LY_ENOT;
81 }
82 }
83 }
84
85 return LY_ENOT;
86}
87
88API LY_ERR
89lysc_iffeature_value(const struct lysc_iffeature *iff)
90{
91 size_t index_e = 0, index_f = 0;
92
93 LY_CHECK_ARG_RET(NULL, iff, LY_EINVAL);
94
95 if (iff->expr) {
96 return lysc_iffeature_value_(iff, &index_e, &index_f);
97 }
98 return LY_ENOT;
99}
100
101API struct lysp_feature *
102lysp_feature_next(const struct lysp_feature *last, const struct lysp_module *pmod, uint32_t *idx)
103{
104 struct lysp_feature *features;
105
106 if (!*idx) {
107 /* module features */
108 features = pmod->features;
Radek Krejcic7d13e32020-12-09 12:32:24 +0100109 } else if ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100110 /* submodule features */
111 features = pmod->includes[*idx - 1].submodule->features;
112 } else {
113 /* no more features */
114 return NULL;
115 }
116
117 /* get the next feature */
118 if (features && (!last || (&features[LY_ARRAY_COUNT(features) - 1] != last))) {
119 return !last ? &features[0] : (struct lysp_feature *)last + 1;
120 }
121
122 /* no more features in current (sub)module */
123 ++(*idx);
124 return lysp_feature_next(NULL, pmod, idx);
125}
126
127/**
128 * @brief Find a feature of the given name and referenced in the given module.
129 *
130 * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature).
131 * @param[in] name Name of the feature including possible prefix.
132 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
133 * @param[in] prefixed Whether the feature name can be prefixed.
134 * @return Pointer to the feature structure if found, NULL otherwise.
135 */
136static struct lysp_feature *
137lysp_feature_find(const struct lysp_module *pmod, const char *name, size_t len, ly_bool prefixed)
138{
139 const struct lys_module *mod;
140 const char *ptr;
141 struct lysp_feature *f = NULL;
142 uint32_t idx = 0;
143
144 assert(pmod);
145
146 if (prefixed && (ptr = ly_strnchr(name, ':', len))) {
147 /* we have a prefixed feature */
148 mod = ly_resolve_prefix(pmod->mod->ctx, name, ptr - name, LY_PREF_SCHEMA, (void *)pmod);
149 LY_CHECK_RET(!mod, NULL);
150
151 pmod = mod->parsed;
152 len = len - (ptr - name) - 1;
153 name = ptr + 1;
154 }
155
Michal Vaskoecd94572020-12-03 14:15:58 +0100156 /* feature without prefix, look in main module and all submodules */
157 if (pmod->is_submod) {
158 pmod = pmod->mod->parsed;
159 }
160
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100161 /* we have the correct module, get the feature */
162 while ((f = lysp_feature_next(f, pmod, &idx))) {
163 if (!ly_strncmp(f->name, name, len)) {
164 return f;
165 }
166 }
167
168 return NULL;
169}
170
171API LY_ERR
172lys_feature_value(const struct lys_module *module, const char *feature)
173{
174 const struct lysp_feature *f;
175
176 LY_CHECK_ARG_RET(NULL, module, module->parsed, feature, LY_EINVAL);
177
178 /* search for the specified feature */
179 f = lysp_feature_find(module->parsed, feature, strlen(feature), 0);
180 LY_CHECK_RET(!f, LY_ENOTFOUND);
181
182 /* feature disabled */
183 if (!(f->flags & LYS_FENABLED)) {
184 return LY_ENOT;
185 }
186
187 /* feature enabled */
188 return LY_SUCCESS;
189}
190
191/**
192 * @brief Stack for processing if-feature expressions.
193 */
194struct iff_stack {
195 size_t size; /**< number of items in the stack */
196 size_t index; /**< first empty item */
197 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
198};
Radek Krejcif13b87b2020-12-01 22:02:17 +0100199#define IFF_STACK_SIZE_STEP 4
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100200
201/**
202 * @brief Add @ref ifftokens into the stack.
203 * @param[in] stack The if-feature stack to use.
204 * @param[in] value One of the @ref ifftokens to store in the stack.
205 * @return LY_EMEM in case of memory allocation error
206 * @return LY_ESUCCESS if the value successfully stored.
207 */
208static LY_ERR
209iff_stack_push(struct iff_stack *stack, uint8_t value)
210{
211 if (stack->index == stack->size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100212 stack->size += IFF_STACK_SIZE_STEP;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100213 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
214 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
215 }
216 stack->stack[stack->index++] = value;
217 return LY_SUCCESS;
218}
219
220/**
221 * @brief Get (and remove) the last item form the stack.
222 * @param[in] stack The if-feature stack to use.
223 * @return The value from the top of the stack.
224 */
225static uint8_t
226iff_stack_pop(struct iff_stack *stack)
227{
228 assert(stack && stack->index);
229
230 stack->index--;
231 return stack->stack[stack->index];
232}
233
234/**
235 * @brief Clean up the stack.
236 * @param[in] stack The if-feature stack to use.
237 */
238static void
239iff_stack_clean(struct iff_stack *stack)
240{
241 stack->size = 0;
242 free(stack->stack);
243}
244
245/**
246 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
247 * (libyang format of the if-feature expression).
248 * @param[in,out] list The 2bits array to modify.
249 * @param[in] op The operand (@ref ifftokens) to store.
250 * @param[in] pos Position (0-based) where to store the given @p op.
251 */
252static void
253iff_setop(uint8_t *list, uint8_t op, size_t pos)
254{
255 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100256 uint8_t mask = IFF_RECORD_MASK;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100257
Radek Krejcif13b87b2020-12-01 22:02:17 +0100258 assert(op <= IFF_RECORD_MASK); /* max 2 bits */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100259
Radek Krejcif13b87b2020-12-01 22:02:17 +0100260 item = &list[pos / IFF_RECORDS_IN_BYTE];
261 mask = mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100262 *item = (*item) & ~mask;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100263 *item = (*item) | (op << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100264}
265
266#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
267#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
268
269static LY_ERR
270lys_compile_iffeature(const struct ly_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
271{
272 LY_ERR rc = LY_SUCCESS;
273 const char *c = qname->str;
274 int64_t i, j;
275 int8_t op_len, last_not = 0, checkversion = 0;
276 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
277 uint8_t op;
278 struct iff_stack stack = {0, 0, NULL};
279 struct lysp_feature *f;
280
281 assert(c);
282
283 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
284 for (i = j = 0; c[i]; i++) {
285 if (c[i] == '(') {
286 j++;
287 checkversion = 1;
288 continue;
289 } else if (c[i] == ')') {
290 j--;
291 continue;
292 } else if (isspace(c[i])) {
293 checkversion = 1;
294 continue;
295 }
296
Radek Krejcif13b87b2020-12-01 22:02:17 +0100297 if (!strncmp(&c[i], "not", op_len = ly_strlen_const("not")) ||
298 !strncmp(&c[i], "and", op_len = ly_strlen_const("and")) ||
299 !strncmp(&c[i], "or", op_len = ly_strlen_const("or"))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100300 uint64_t spaces;
301 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {}
302 if (c[i + op_len + spaces] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100303 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100304 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) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100319 LOGVAL(ctx, LYVE_SYNTAX_YANG,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100320 "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 ) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100346 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 +0100347 return LY_EVALID;
348 }
349 if (f_exp != f_size) {
350 /* features do not match the needed arguments for the logical operations */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100351 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 +0100352 "the required number of operands for the operations.", qname->str);
353 return LY_EVALID;
354 }
355
356 if (checkversion || (expr_size > 1)) {
357 /* check that we have 1.1 module */
358 if (qname->mod->version != LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100359 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 +0100360 return LY_EVALID;
361 }
362 }
363
364 /* allocate the memory */
365 LY_ARRAY_CREATE_RET(ctx, iff->features, f_size, LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100366 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 +0100367 stack.stack = malloc(expr_size * sizeof *stack.stack);
368 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx); rc = LY_EMEM, error);
369
370 stack.size = expr_size;
371 f_size--; expr_size--; /* used as indexes from now */
372
373 for (i--; i >= 0; i--) {
374 if (c[i] == ')') {
375 /* push it on stack */
376 iff_stack_push(&stack, LYS_IFF_RP);
377 continue;
378 } else if (c[i] == '(') {
379 /* pop from the stack into result all operators until ) */
380 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
381 iff_setop(iff->expr, op, expr_size--);
382 }
383 continue;
384 } else if (isspace(c[i])) {
385 continue;
386 }
387
388 /* end of operator or operand -> find beginning and get what is it */
389 j = i + 1;
390 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
391 i--;
392 }
393 i++; /* go back by one step */
394
Radek Krejcif13b87b2020-12-01 22:02:17 +0100395 if (!strncmp(&c[i], "not", ly_strlen_const("not")) && isspace(c[i + ly_strlen_const("not")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100396 if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
397 /* double not */
398 iff_stack_pop(&stack);
399 } else {
400 /* not has the highest priority, so do not pop from the stack
401 * as in case of AND and OR */
402 iff_stack_push(&stack, LYS_IFF_NOT);
403 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100404 } else if (!strncmp(&c[i], "and", ly_strlen_const("and")) && isspace(c[i + ly_strlen_const("and")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100405 /* as for OR - pop from the stack all operators with the same or higher
406 * priority and store them to the result, then push the AND to the stack */
407 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
408 op = iff_stack_pop(&stack);
409 iff_setop(iff->expr, op, expr_size--);
410 }
411 iff_stack_push(&stack, LYS_IFF_AND);
412 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
413 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
414 op = iff_stack_pop(&stack);
415 iff_setop(iff->expr, op, expr_size--);
416 }
417 iff_stack_push(&stack, LYS_IFF_OR);
418 } else {
419 /* feature name, length is j - i */
420
421 /* add it to the expression */
422 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
423
424 /* now get the link to the feature definition */
425 f = lysp_feature_find(qname->mod, &c[i], j - i, 1);
426 if (!f) {
Radek Krejci422afb12021-03-04 16:38:16 +0100427 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".",
428 qname->str, (int)(j - i), &c[i]);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100429 rc = LY_EVALID;
430 goto error;
431 }
432 iff->features[f_size] = f;
433 LY_ARRAY_INCREMENT(iff->features);
434 f_size--;
435 }
436 }
437 while (stack.index) {
438 op = iff_stack_pop(&stack);
439 iff_setop(iff->expr, op, expr_size--);
440 }
441
442 if (++expr_size || ++f_size) {
443 /* not all expected operators and operands found */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100444 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - processing error.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100445 rc = LY_EINT;
446 } else {
447 rc = LY_SUCCESS;
448 }
449
450error:
451 /* cleanup */
452 iff_stack_clean(&stack);
453
454 return rc;
455}
456
457LY_ERR
458lys_eval_iffeatures(const struct ly_ctx *ctx, struct lysp_qname *iffeatures, ly_bool *enabled)
459{
460 LY_ERR ret;
461 struct lysc_iffeature iff = {0};
462
463 if (!iffeatures) {
464 *enabled = 1;
465 return LY_SUCCESS;
466 }
467
468 LY_CHECK_RET(lys_compile_iffeature(ctx, iffeatures, &iff));
469
470 ret = lysc_iffeature_value(&iff);
471 lysc_iffeature_free((struct ly_ctx *)ctx, &iff);
472 if (ret == LY_ENOT) {
473 *enabled = 0;
474 } else if (ret) {
475 return ret;
476 } else {
477 *enabled = 1;
478 }
479
480 return LY_SUCCESS;
481}
482
Michal Vasko08c8b272020-11-24 18:11:30 +0100483/**
484 * @brief Check whether all enabled features have their if-features satisfied.
485 * Enabled features with false if-features are disabled with a warning.
486 *
487 * @param[in] pmod Parsed module features to check.
488 * @return LY_ERR value.
489 */
490static LY_ERR
491lys_check_features(struct lysp_module *pmod)
492{
493 LY_ERR r;
494 uint32_t i = 0;
495 struct lysp_feature *f = NULL;
496
497 while ((f = lysp_feature_next(f, pmod, &i))) {
498 if (!(f->flags & LYS_FENABLED) || !f->iffeatures) {
499 /* disabled feature or no if-features to check */
500 continue;
501 }
502
503 assert(f->iffeatures_c);
504 r = lysc_iffeature_value(f->iffeatures_c);
505 if (r == LY_ENOT) {
506 LOGWRN(pmod->mod->ctx, "Feature \"%s\" cannot be enabled because its \"if-feature\" is not satisfied.",
507 f->name);
508
509 /* disable feature and re-evaluate all the feature if-features again */
510 f->flags &= ~LYS_FENABLED;
511 return lys_check_features(pmod);
512 } else if (r) {
513 return r;
514 } /* else if-feature satisfied */
515 }
516
517 return LY_SUCCESS;
518}
519
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100520LY_ERR
521lys_enable_features(struct lysp_module *pmod, const char **features)
522{
Michal Vasko08c8b272020-11-24 18:11:30 +0100523 uint32_t i = 0;
524 struct lysp_feature *f = 0;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100525
526 if (!features) {
527 /* keep all features disabled */
528 return LY_SUCCESS;
529 }
530
531 if (!strcmp(features[0], "*")) {
532 /* enable all features */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100533 while ((f = lysp_feature_next(f, pmod, &i))) {
534 f->flags |= LYS_FENABLED;
535 }
536 } else {
537 /* enable selected features */
538 for (i = 0; features[i]; ++i) {
539 /* find the feature */
540 f = lysp_feature_find(pmod, features[i], strlen(features[i]), 0);
541 if (!f) {
542 LOGERR(pmod->mod->ctx, LY_ENOTFOUND, "Feature \"%s\" not found in module \"%s\".", features[i],
543 pmod->mod->name);
544 return LY_ENOTFOUND;
545 }
546
547 /* enable feature */
548 f->flags |= LYS_FENABLED;
549 }
550 }
551
Michal Vasko08c8b272020-11-24 18:11:30 +0100552 /* check final features if-feature state */
553 return lys_check_features(pmod);
554}
555
556LY_ERR
557lys_set_features(struct lysp_module *pmod, const char **features)
558{
559 uint32_t i = 0, j;
560 struct lysp_feature *f = 0;
561 ly_bool change = 0;
562
563 if (!features) {
Radek Krejci2415f882021-01-20 16:27:09 +0100564 /* do not touch the features */
565
566 } else if (!features[0]) {
Michal Vasko08c8b272020-11-24 18:11:30 +0100567 /* disable all the features */
568 while ((f = lysp_feature_next(f, pmod, &i))) {
569 if (f->flags & LYS_FENABLED) {
570 f->flags &= ~LYS_FENABLED;
571 change = 1;
572 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100573 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100574 } else if (!strcmp(features[0], "*")) {
575 /* enable all the features */
576 while ((f = lysp_feature_next(f, pmod, &i))) {
577 if (!(f->flags & LYS_FENABLED)) {
578 f->flags |= LYS_FENABLED;
579 change = 1;
580 }
581 }
582 } else {
583 /* enable specific features, disable the rest */
584 while ((f = lysp_feature_next(f, pmod, &i))) {
585 for (j = 0; features[j]; ++j) {
586 if (!strcmp(f->name, features[j])) {
587 break;
588 }
589 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100590
Michal Vasko08c8b272020-11-24 18:11:30 +0100591 if (features[j] && !(f->flags & LYS_FENABLED)) {
592 /* enable */
593 f->flags |= LYS_FENABLED;
594 change = 1;
595 } else if (!features[j] && (f->flags & LYS_FENABLED)) {
596 /* disable */
597 f->flags &= ~LYS_FENABLED;
598 change = 1;
599 }
600 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100601 }
602
Michal Vasko08c8b272020-11-24 18:11:30 +0100603 if (!change) {
604 /* features already set correctly */
605 return LY_EEXIST;
606 }
607
608 /* check final features if-feature state */
609 return lys_check_features(pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100610}
611
612/**
613 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
614 *
615 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
616 *
617 * @param[in] ctx Compile context for logging.
618 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
619 * being currently processed).
620 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
621 * @return LY_SUCCESS if everything is ok.
622 * @return LY_EVALID if the feature references indirectly itself.
623 */
624static LY_ERR
625lys_compile_feature_circular_check(const struct ly_ctx *ctx, struct lysp_feature *feature, struct lysp_feature **depfeatures)
626{
627 LY_ERR ret = LY_SUCCESS;
628 LY_ARRAY_COUNT_TYPE u, v;
629 struct ly_set recursion = {0};
630 struct lysp_feature *drv;
631
632 if (!depfeatures) {
633 return LY_SUCCESS;
634 }
635
636 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
637 if (feature == depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100638 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100639 ret = LY_EVALID;
640 goto cleanup;
641 }
642 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
643 LY_CHECK_GOTO(ret, cleanup);
644 }
645
646 for (v = 0; v < recursion.count; ++v) {
647 drv = recursion.objs[v];
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100648 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
649 if (feature == drv->depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100650 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100651 ret = LY_EVALID;
652 goto cleanup;
653 }
654 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
655 LY_CHECK_GOTO(ret, cleanup);
656 }
657 }
658
659cleanup:
660 ly_set_erase(&recursion, NULL);
661 return ret;
662}
663
664LY_ERR
665lys_compile_feature_iffeatures(struct lysp_module *pmod)
666{
667 LY_ARRAY_COUNT_TYPE u, v;
668 struct lysp_feature *f = NULL, **df;
669 uint32_t idx = 0;
670
671 while ((f = lysp_feature_next(f, pmod, &idx))) {
672 if (!f->iffeatures) {
673 continue;
674 }
675
676 /* compile if-features */
677 LY_ARRAY_CREATE_RET(pmod->mod->ctx, f->iffeatures_c, LY_ARRAY_COUNT(f->iffeatures), LY_EMEM);
678 LY_ARRAY_FOR(f->iffeatures, u) {
679 LY_ARRAY_INCREMENT(f->iffeatures_c);
680 LY_CHECK_RET(lys_compile_iffeature(pmod->mod->ctx, &(f->iffeatures)[u], &(f->iffeatures_c)[u]));
681 }
682 LY_ARRAY_FOR(f->iffeatures_c, u) {
683 LY_ARRAY_FOR(f->iffeatures_c[u].features, v) {
684 /* check for circular dependency - direct reference first,... */
685 if (f == f->iffeatures_c[u].features[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100686 LOGVAL(pmod->mod->ctx, LYVE_REFERENCE, "Feature \"%s\" is referenced from itself.", f->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100687 return LY_EVALID;
688 }
689 /* ... and indirect circular reference */
690 LY_CHECK_RET(lys_compile_feature_circular_check(pmod->mod->ctx, f->iffeatures_c[u].features[v], f->depfeatures));
691
692 /* add itself into the dependants list */
693 LY_ARRAY_NEW_RET(pmod->mod->ctx, f->iffeatures_c[u].features[v]->depfeatures, df, LY_EMEM);
694 *df = f;
695 }
696 }
697 }
698
699 return LY_SUCCESS;
700}
701
702void
703lys_free_feature_iffeatures(struct lysp_module *pmod)
704{
705 struct lysp_feature *f = NULL;
706 uint32_t idx = 0;
707
708 while ((f = lysp_feature_next(f, pmod, &idx))) {
709 FREE_ARRAY(pmod->mod->ctx, f->iffeatures_c, lysc_iffeature_free);
710 f->iffeatures_c = NULL;
711 LY_ARRAY_FREE(f->depfeatures);
712 f->depfeatures = NULL;
713 }
714}