blob: 29e6478fe3359f14dd0f4f53058dcc9c46ba4f3b [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
aPiecekf4a0a192021-08-03 15:14:17 +0200100API LY_ERR
101lys_identity_iffeature_value(const struct lysc_ident *ident)
102{
103 LY_ARRAY_COUNT_TYPE u;
104 ly_bool enabled;
105 const struct lysp_ident *idents_p;
106
107 assert(ident);
108
109 idents_p = ident->module->parsed->identities;
110 LY_ARRAY_FOR(idents_p, u) {
111 if (idents_p[u].name == ident->name) {
112 break;
113 }
114 }
115 assert(u != LY_ARRAY_COUNT(idents_p));
116
117 LY_CHECK_RET(lys_eval_iffeatures(ident->module->ctx, idents_p[u].iffeatures, &enabled));
118 if (!enabled) {
119 return LY_ENOT;
120 }
121
122 return LY_SUCCESS;
123}
124
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100125API struct lysp_feature *
126lysp_feature_next(const struct lysp_feature *last, const struct lysp_module *pmod, uint32_t *idx)
127{
128 struct lysp_feature *features;
129
130 if (!*idx) {
131 /* module features */
132 features = pmod->features;
Radek Krejcic7d13e32020-12-09 12:32:24 +0100133 } else if ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100134 /* submodule features */
135 features = pmod->includes[*idx - 1].submodule->features;
136 } else {
137 /* no more features */
138 return NULL;
139 }
140
141 /* get the next feature */
142 if (features && (!last || (&features[LY_ARRAY_COUNT(features) - 1] != last))) {
143 return !last ? &features[0] : (struct lysp_feature *)last + 1;
144 }
145
146 /* no more features in current (sub)module */
147 ++(*idx);
148 return lysp_feature_next(NULL, pmod, idx);
149}
150
151/**
152 * @brief Find a feature of the given name and referenced in the given module.
153 *
154 * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature).
155 * @param[in] name Name of the feature including possible prefix.
156 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
157 * @param[in] prefixed Whether the feature name can be prefixed.
158 * @return Pointer to the feature structure if found, NULL otherwise.
159 */
160static struct lysp_feature *
161lysp_feature_find(const struct lysp_module *pmod, const char *name, size_t len, ly_bool prefixed)
162{
163 const struct lys_module *mod;
164 const char *ptr;
165 struct lysp_feature *f = NULL;
166 uint32_t idx = 0;
167
168 assert(pmod);
169
170 if (prefixed && (ptr = ly_strnchr(name, ':', len))) {
171 /* we have a prefixed feature */
Radek Krejci8df109d2021-04-23 12:19:08 +0200172 mod = ly_resolve_prefix(pmod->mod->ctx, name, ptr - name, LY_VALUE_SCHEMA, (void *)pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100173 LY_CHECK_RET(!mod, NULL);
174
175 pmod = mod->parsed;
176 len = len - (ptr - name) - 1;
177 name = ptr + 1;
178 }
179
Michal Vaskoecd94572020-12-03 14:15:58 +0100180 /* feature without prefix, look in main module and all submodules */
181 if (pmod->is_submod) {
182 pmod = pmod->mod->parsed;
183 }
184
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100185 /* we have the correct module, get the feature */
186 while ((f = lysp_feature_next(f, pmod, &idx))) {
187 if (!ly_strncmp(f->name, name, len)) {
188 return f;
189 }
190 }
191
192 return NULL;
193}
194
195API LY_ERR
196lys_feature_value(const struct lys_module *module, const char *feature)
197{
198 const struct lysp_feature *f;
199
200 LY_CHECK_ARG_RET(NULL, module, module->parsed, feature, LY_EINVAL);
201
202 /* search for the specified feature */
203 f = lysp_feature_find(module->parsed, feature, strlen(feature), 0);
204 LY_CHECK_RET(!f, LY_ENOTFOUND);
205
206 /* feature disabled */
207 if (!(f->flags & LYS_FENABLED)) {
208 return LY_ENOT;
209 }
210
211 /* feature enabled */
212 return LY_SUCCESS;
213}
214
215/**
216 * @brief Stack for processing if-feature expressions.
217 */
218struct iff_stack {
219 size_t size; /**< number of items in the stack */
220 size_t index; /**< first empty item */
221 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
222};
Radek Krejcif13b87b2020-12-01 22:02:17 +0100223#define IFF_STACK_SIZE_STEP 4
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100224
225/**
226 * @brief Add @ref ifftokens into the stack.
227 * @param[in] stack The if-feature stack to use.
228 * @param[in] value One of the @ref ifftokens to store in the stack.
229 * @return LY_EMEM in case of memory allocation error
230 * @return LY_ESUCCESS if the value successfully stored.
231 */
232static LY_ERR
233iff_stack_push(struct iff_stack *stack, uint8_t value)
234{
235 if (stack->index == stack->size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100236 stack->size += IFF_STACK_SIZE_STEP;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100237 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
238 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
239 }
240 stack->stack[stack->index++] = value;
241 return LY_SUCCESS;
242}
243
244/**
245 * @brief Get (and remove) the last item form the stack.
246 * @param[in] stack The if-feature stack to use.
247 * @return The value from the top of the stack.
248 */
249static uint8_t
250iff_stack_pop(struct iff_stack *stack)
251{
252 assert(stack && stack->index);
253
254 stack->index--;
255 return stack->stack[stack->index];
256}
257
258/**
259 * @brief Clean up the stack.
260 * @param[in] stack The if-feature stack to use.
261 */
262static void
263iff_stack_clean(struct iff_stack *stack)
264{
265 stack->size = 0;
266 free(stack->stack);
267}
268
269/**
270 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
271 * (libyang format of the if-feature expression).
272 * @param[in,out] list The 2bits array to modify.
273 * @param[in] op The operand (@ref ifftokens) to store.
274 * @param[in] pos Position (0-based) where to store the given @p op.
275 */
276static void
277iff_setop(uint8_t *list, uint8_t op, size_t pos)
278{
279 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100280 uint8_t mask = IFF_RECORD_MASK;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100281
Radek Krejcif13b87b2020-12-01 22:02:17 +0100282 assert(op <= IFF_RECORD_MASK); /* max 2 bits */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100283
Radek Krejcif13b87b2020-12-01 22:02:17 +0100284 item = &list[pos / IFF_RECORDS_IN_BYTE];
285 mask = mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100286 *item = (*item) & ~mask;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100287 *item = (*item) | (op << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100288}
289
290#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
291#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
292
293static LY_ERR
294lys_compile_iffeature(const struct ly_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
295{
296 LY_ERR rc = LY_SUCCESS;
297 const char *c = qname->str;
298 int64_t i, j;
299 int8_t op_len, last_not = 0, checkversion = 0;
300 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
301 uint8_t op;
302 struct iff_stack stack = {0, 0, NULL};
303 struct lysp_feature *f;
304
305 assert(c);
306
307 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
308 for (i = j = 0; c[i]; i++) {
309 if (c[i] == '(') {
310 j++;
311 checkversion = 1;
312 continue;
313 } else if (c[i] == ')') {
314 j--;
315 continue;
316 } else if (isspace(c[i])) {
317 checkversion = 1;
318 continue;
319 }
320
Radek Krejcif13b87b2020-12-01 22:02:17 +0100321 if (!strncmp(&c[i], "not", op_len = ly_strlen_const("not")) ||
322 !strncmp(&c[i], "and", op_len = ly_strlen_const("and")) ||
323 !strncmp(&c[i], "or", op_len = ly_strlen_const("or"))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100324 uint64_t spaces;
325 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {}
326 if (c[i + op_len + spaces] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100327 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100328 return LY_EVALID;
329 } else if (!isspace(c[i + op_len])) {
330 /* feature name starting with the not/and/or */
331 last_not = 0;
332 f_size++;
333 } else if (c[i] == 'n') { /* not operation */
334 if (last_not) {
335 /* double not */
336 expr_size = expr_size - 2;
337 last_not = 0;
338 } else {
339 last_not = 1;
340 }
341 } else { /* and, or */
342 if (f_exp != f_size) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100343 LOGVAL(ctx, LYVE_SYNTAX_YANG,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100344 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
345 qname->str, op_len, &c[i]);
346 return LY_EVALID;
347 }
348 f_exp++;
349
350 /* not a not operation */
351 last_not = 0;
352 }
353 i += op_len;
354 } else {
355 f_size++;
356 last_not = 0;
357 }
358 expr_size++;
359
360 while (!isspace(c[i])) {
361 if (!c[i] || (c[i] == ')') || (c[i] == '(')) {
362 i--;
363 break;
364 }
365 i++;
366 }
367 }
368 if (j) {
369 /* not matching count of ( and ) */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100370 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 +0100371 return LY_EVALID;
372 }
373 if (f_exp != f_size) {
374 /* features do not match the needed arguments for the logical operations */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100375 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 +0100376 "the required number of operands for the operations.", qname->str);
377 return LY_EVALID;
378 }
379
380 if (checkversion || (expr_size > 1)) {
381 /* check that we have 1.1 module */
382 if (qname->mod->version != LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100383 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 +0100384 return LY_EVALID;
385 }
386 }
387
388 /* allocate the memory */
389 LY_ARRAY_CREATE_RET(ctx, iff->features, f_size, LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100390 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 +0100391 stack.stack = malloc(expr_size * sizeof *stack.stack);
392 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx); rc = LY_EMEM, error);
393
394 stack.size = expr_size;
395 f_size--; expr_size--; /* used as indexes from now */
396
397 for (i--; i >= 0; i--) {
398 if (c[i] == ')') {
399 /* push it on stack */
400 iff_stack_push(&stack, LYS_IFF_RP);
401 continue;
402 } else if (c[i] == '(') {
403 /* pop from the stack into result all operators until ) */
404 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
405 iff_setop(iff->expr, op, expr_size--);
406 }
407 continue;
408 } else if (isspace(c[i])) {
409 continue;
410 }
411
412 /* end of operator or operand -> find beginning and get what is it */
413 j = i + 1;
414 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
415 i--;
416 }
417 i++; /* go back by one step */
418
Radek Krejcif13b87b2020-12-01 22:02:17 +0100419 if (!strncmp(&c[i], "not", ly_strlen_const("not")) && isspace(c[i + ly_strlen_const("not")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100420 if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
421 /* double not */
422 iff_stack_pop(&stack);
423 } else {
424 /* not has the highest priority, so do not pop from the stack
425 * as in case of AND and OR */
426 iff_stack_push(&stack, LYS_IFF_NOT);
427 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100428 } else if (!strncmp(&c[i], "and", ly_strlen_const("and")) && isspace(c[i + ly_strlen_const("and")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100429 /* as for OR - pop from the stack all operators with the same or higher
430 * priority and store them to the result, then push the AND to the stack */
431 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
432 op = iff_stack_pop(&stack);
433 iff_setop(iff->expr, op, expr_size--);
434 }
435 iff_stack_push(&stack, LYS_IFF_AND);
436 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
437 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
438 op = iff_stack_pop(&stack);
439 iff_setop(iff->expr, op, expr_size--);
440 }
441 iff_stack_push(&stack, LYS_IFF_OR);
442 } else {
443 /* feature name, length is j - i */
444
445 /* add it to the expression */
446 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
447
448 /* now get the link to the feature definition */
449 f = lysp_feature_find(qname->mod, &c[i], j - i, 1);
450 if (!f) {
Radek Krejci422afb12021-03-04 16:38:16 +0100451 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".",
452 qname->str, (int)(j - i), &c[i]);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100453 rc = LY_EVALID;
454 goto error;
455 }
456 iff->features[f_size] = f;
457 LY_ARRAY_INCREMENT(iff->features);
458 f_size--;
459 }
460 }
461 while (stack.index) {
462 op = iff_stack_pop(&stack);
463 iff_setop(iff->expr, op, expr_size--);
464 }
465
466 if (++expr_size || ++f_size) {
467 /* not all expected operators and operands found */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100468 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - processing error.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100469 rc = LY_EINT;
470 } else {
471 rc = LY_SUCCESS;
472 }
473
474error:
475 /* cleanup */
476 iff_stack_clean(&stack);
477
478 return rc;
479}
480
481LY_ERR
482lys_eval_iffeatures(const struct ly_ctx *ctx, struct lysp_qname *iffeatures, ly_bool *enabled)
483{
484 LY_ERR ret;
Michal Vaskoc3f55732021-07-29 10:03:17 +0200485 LY_ARRAY_COUNT_TYPE u;
486 struct lysc_iffeature iff;
487
488 /* enabled by default */
489 *enabled = 1;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100490
491 if (!iffeatures) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100492 return LY_SUCCESS;
493 }
494
Michal Vaskoc3f55732021-07-29 10:03:17 +0200495 /* evaluate all if-feature conditions or until an unsatisfied one is found */
496 LY_ARRAY_FOR(iffeatures, u) {
497 memset(&iff, 0, sizeof iff);
498 LY_CHECK_RET(lys_compile_iffeature(ctx, &iffeatures[u], &iff));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100499
Michal Vaskoc3f55732021-07-29 10:03:17 +0200500 ret = lysc_iffeature_value(&iff);
501 lysc_iffeature_free((struct ly_ctx *)ctx, &iff);
502 if (ret == LY_ENOT) {
503 *enabled = 0;
504 break;
505 } else if (ret) {
506 return ret;
507 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100508 }
509
510 return LY_SUCCESS;
511}
512
Michal Vasko08c8b272020-11-24 18:11:30 +0100513/**
514 * @brief Check whether all enabled features have their if-features satisfied.
515 * Enabled features with false if-features are disabled with a warning.
516 *
517 * @param[in] pmod Parsed module features to check.
518 * @return LY_ERR value.
519 */
520static LY_ERR
521lys_check_features(struct lysp_module *pmod)
522{
523 LY_ERR r;
524 uint32_t i = 0;
525 struct lysp_feature *f = NULL;
526
527 while ((f = lysp_feature_next(f, pmod, &i))) {
528 if (!(f->flags & LYS_FENABLED) || !f->iffeatures) {
529 /* disabled feature or no if-features to check */
530 continue;
531 }
532
533 assert(f->iffeatures_c);
534 r = lysc_iffeature_value(f->iffeatures_c);
535 if (r == LY_ENOT) {
536 LOGWRN(pmod->mod->ctx, "Feature \"%s\" cannot be enabled because its \"if-feature\" is not satisfied.",
537 f->name);
538
539 /* disable feature and re-evaluate all the feature if-features again */
540 f->flags &= ~LYS_FENABLED;
541 return lys_check_features(pmod);
542 } else if (r) {
543 return r;
544 } /* else if-feature satisfied */
545 }
546
547 return LY_SUCCESS;
548}
549
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100550LY_ERR
Michal Vasko08c8b272020-11-24 18:11:30 +0100551lys_set_features(struct lysp_module *pmod, const char **features)
552{
553 uint32_t i = 0, j;
554 struct lysp_feature *f = 0;
555 ly_bool change = 0;
556
557 if (!features) {
Radek Krejci2415f882021-01-20 16:27:09 +0100558 /* do not touch the features */
559
560 } else if (!features[0]) {
Michal Vasko08c8b272020-11-24 18:11:30 +0100561 /* disable all the features */
562 while ((f = lysp_feature_next(f, pmod, &i))) {
563 if (f->flags & LYS_FENABLED) {
564 f->flags &= ~LYS_FENABLED;
565 change = 1;
566 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100567 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100568 } else if (!strcmp(features[0], "*")) {
569 /* enable all the features */
570 while ((f = lysp_feature_next(f, pmod, &i))) {
571 if (!(f->flags & LYS_FENABLED)) {
572 f->flags |= LYS_FENABLED;
573 change = 1;
574 }
575 }
576 } else {
577 /* enable specific features, disable the rest */
578 while ((f = lysp_feature_next(f, pmod, &i))) {
579 for (j = 0; features[j]; ++j) {
580 if (!strcmp(f->name, features[j])) {
581 break;
582 }
583 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100584
Michal Vasko08c8b272020-11-24 18:11:30 +0100585 if (features[j] && !(f->flags & LYS_FENABLED)) {
586 /* enable */
587 f->flags |= LYS_FENABLED;
588 change = 1;
589 } else if (!features[j] && (f->flags & LYS_FENABLED)) {
590 /* disable */
591 f->flags &= ~LYS_FENABLED;
592 change = 1;
593 }
594 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100595 }
596
Michal Vasko08c8b272020-11-24 18:11:30 +0100597 if (!change) {
598 /* features already set correctly */
599 return LY_EEXIST;
600 }
601
602 /* check final features if-feature state */
603 return lys_check_features(pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100604}
605
606/**
607 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
608 *
609 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
610 *
611 * @param[in] ctx Compile context for logging.
612 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
613 * being currently processed).
614 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
615 * @return LY_SUCCESS if everything is ok.
616 * @return LY_EVALID if the feature references indirectly itself.
617 */
618static LY_ERR
619lys_compile_feature_circular_check(const struct ly_ctx *ctx, struct lysp_feature *feature, struct lysp_feature **depfeatures)
620{
621 LY_ERR ret = LY_SUCCESS;
622 LY_ARRAY_COUNT_TYPE u, v;
623 struct ly_set recursion = {0};
624 struct lysp_feature *drv;
625
626 if (!depfeatures) {
627 return LY_SUCCESS;
628 }
629
630 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
631 if (feature == depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100632 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100633 ret = LY_EVALID;
634 goto cleanup;
635 }
636 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
637 LY_CHECK_GOTO(ret, cleanup);
638 }
639
640 for (v = 0; v < recursion.count; ++v) {
641 drv = recursion.objs[v];
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100642 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
643 if (feature == drv->depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100644 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100645 ret = LY_EVALID;
646 goto cleanup;
647 }
648 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
649 LY_CHECK_GOTO(ret, cleanup);
650 }
651 }
652
653cleanup:
654 ly_set_erase(&recursion, NULL);
655 return ret;
656}
657
658LY_ERR
659lys_compile_feature_iffeatures(struct lysp_module *pmod)
660{
661 LY_ARRAY_COUNT_TYPE u, v;
662 struct lysp_feature *f = NULL, **df;
663 uint32_t idx = 0;
664
665 while ((f = lysp_feature_next(f, pmod, &idx))) {
666 if (!f->iffeatures) {
667 continue;
668 }
669
670 /* compile if-features */
671 LY_ARRAY_CREATE_RET(pmod->mod->ctx, f->iffeatures_c, LY_ARRAY_COUNT(f->iffeatures), LY_EMEM);
672 LY_ARRAY_FOR(f->iffeatures, u) {
673 LY_ARRAY_INCREMENT(f->iffeatures_c);
674 LY_CHECK_RET(lys_compile_iffeature(pmod->mod->ctx, &(f->iffeatures)[u], &(f->iffeatures_c)[u]));
675 }
676 LY_ARRAY_FOR(f->iffeatures_c, u) {
677 LY_ARRAY_FOR(f->iffeatures_c[u].features, v) {
678 /* check for circular dependency - direct reference first,... */
679 if (f == f->iffeatures_c[u].features[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100680 LOGVAL(pmod->mod->ctx, LYVE_REFERENCE, "Feature \"%s\" is referenced from itself.", f->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100681 return LY_EVALID;
682 }
683 /* ... and indirect circular reference */
684 LY_CHECK_RET(lys_compile_feature_circular_check(pmod->mod->ctx, f->iffeatures_c[u].features[v], f->depfeatures));
685
686 /* add itself into the dependants list */
687 LY_ARRAY_NEW_RET(pmod->mod->ctx, f->iffeatures_c[u].features[v]->depfeatures, df, LY_EMEM);
688 *df = f;
689 }
690 }
691 }
692
693 return LY_SUCCESS;
694}