blob: e770cd77d505fe9b772d67e1f0e6ddfac0e47411 [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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010087LIBYANG_API_DEF LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010088lysc_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100100LIBYANG_API_DEF LY_ERR
aPiecekf4a0a192021-08-03 15:14:17 +0200101lys_identity_iffeature_value(const struct lysc_ident *ident)
102{
aPiecekf3e2db82021-08-05 10:18:43 +0200103 LY_ARRAY_COUNT_TYPE u, v;
aPiecekf4a0a192021-08-03 15:14:17 +0200104 ly_bool enabled;
aPiecekf3e2db82021-08-05 10:18:43 +0200105 const struct lysp_ident *idents_p, *found_ident = NULL;
106 struct lysp_include *includes;
aPiecekf4a0a192021-08-03 15:14:17 +0200107
108 assert(ident);
109
aPiecekf3e2db82021-08-05 10:18:43 +0200110 /* Search parsed identity in the module. */
aPiecekf4a0a192021-08-03 15:14:17 +0200111 idents_p = ident->module->parsed->identities;
112 LY_ARRAY_FOR(idents_p, u) {
113 if (idents_p[u].name == ident->name) {
aPiecekf3e2db82021-08-05 10:18:43 +0200114 found_ident = &idents_p[u];
aPiecekf4a0a192021-08-03 15:14:17 +0200115 break;
116 }
117 }
aPiecekf4a0a192021-08-03 15:14:17 +0200118
aPiecekf3e2db82021-08-05 10:18:43 +0200119 if (!found_ident) {
120 /* It is not in the module, so it must be in some submodule. */
121 includes = ident->module->parsed->includes;
122 LY_ARRAY_FOR(includes, u) {
123 idents_p = includes[u].submodule->identities;
124 LY_ARRAY_FOR(idents_p, v) {
125 if (idents_p[v].name == ident->name) {
126 found_ident = &idents_p[v];
127 break;
128 }
129 }
130 }
131 }
132
133 assert(found_ident);
134
135 /* Evaluate its if-feature. */
136 LY_CHECK_RET(lys_eval_iffeatures(ident->module->ctx, found_ident->iffeatures, &enabled));
aPiecekf4a0a192021-08-03 15:14:17 +0200137 if (!enabled) {
138 return LY_ENOT;
139 }
140
141 return LY_SUCCESS;
142}
143
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100144LIBYANG_API_DEF struct lysp_feature *
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100145lysp_feature_next(const struct lysp_feature *last, const struct lysp_module *pmod, uint32_t *idx)
146{
147 struct lysp_feature *features;
148
149 if (!*idx) {
150 /* module features */
151 features = pmod->features;
Radek Krejcic7d13e32020-12-09 12:32:24 +0100152 } else if ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100153 /* submodule features */
154 features = pmod->includes[*idx - 1].submodule->features;
155 } else {
156 /* no more features */
157 return NULL;
158 }
159
160 /* get the next feature */
161 if (features && (!last || (&features[LY_ARRAY_COUNT(features) - 1] != last))) {
162 return !last ? &features[0] : (struct lysp_feature *)last + 1;
163 }
164
165 /* no more features in current (sub)module */
166 ++(*idx);
167 return lysp_feature_next(NULL, pmod, idx);
168}
169
170/**
171 * @brief Find a feature of the given name and referenced in the given module.
172 *
173 * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature).
174 * @param[in] name Name of the feature including possible prefix.
175 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
176 * @param[in] prefixed Whether the feature name can be prefixed.
177 * @return Pointer to the feature structure if found, NULL otherwise.
178 */
179static struct lysp_feature *
180lysp_feature_find(const struct lysp_module *pmod, const char *name, size_t len, ly_bool prefixed)
181{
182 const struct lys_module *mod;
183 const char *ptr;
184 struct lysp_feature *f = NULL;
185 uint32_t idx = 0;
186
187 assert(pmod);
188
189 if (prefixed && (ptr = ly_strnchr(name, ':', len))) {
190 /* we have a prefixed feature */
Radek Krejci8df109d2021-04-23 12:19:08 +0200191 mod = ly_resolve_prefix(pmod->mod->ctx, name, ptr - name, LY_VALUE_SCHEMA, (void *)pmod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100192 LY_CHECK_RET(!mod, NULL);
193
194 pmod = mod->parsed;
195 len = len - (ptr - name) - 1;
196 name = ptr + 1;
197 }
198
Michal Vaskoecd94572020-12-03 14:15:58 +0100199 /* feature without prefix, look in main module and all submodules */
200 if (pmod->is_submod) {
201 pmod = pmod->mod->parsed;
202 }
203
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100204 /* we have the correct module, get the feature */
205 while ((f = lysp_feature_next(f, pmod, &idx))) {
206 if (!ly_strncmp(f->name, name, len)) {
207 return f;
208 }
209 }
210
211 return NULL;
212}
213
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100214LIBYANG_API_DEF LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100215lys_feature_value(const struct lys_module *module, const char *feature)
216{
217 const struct lysp_feature *f;
218
219 LY_CHECK_ARG_RET(NULL, module, module->parsed, feature, LY_EINVAL);
220
221 /* search for the specified feature */
222 f = lysp_feature_find(module->parsed, feature, strlen(feature), 0);
223 LY_CHECK_RET(!f, LY_ENOTFOUND);
224
225 /* feature disabled */
226 if (!(f->flags & LYS_FENABLED)) {
227 return LY_ENOT;
228 }
229
230 /* feature enabled */
231 return LY_SUCCESS;
232}
233
234/**
235 * @brief Stack for processing if-feature expressions.
236 */
237struct iff_stack {
238 size_t size; /**< number of items in the stack */
239 size_t index; /**< first empty item */
240 uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
241};
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242#define IFF_STACK_SIZE_STEP 4
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100243
244/**
245 * @brief Add @ref ifftokens into the stack.
246 * @param[in] stack The if-feature stack to use.
247 * @param[in] value One of the @ref ifftokens to store in the stack.
248 * @return LY_EMEM in case of memory allocation error
249 * @return LY_ESUCCESS if the value successfully stored.
250 */
251static LY_ERR
252iff_stack_push(struct iff_stack *stack, uint8_t value)
253{
254 if (stack->index == stack->size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 stack->size += IFF_STACK_SIZE_STEP;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100256 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
257 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
258 }
259 stack->stack[stack->index++] = value;
260 return LY_SUCCESS;
261}
262
263/**
264 * @brief Get (and remove) the last item form the stack.
265 * @param[in] stack The if-feature stack to use.
266 * @return The value from the top of the stack.
267 */
268static uint8_t
269iff_stack_pop(struct iff_stack *stack)
270{
271 assert(stack && stack->index);
272
273 stack->index--;
274 return stack->stack[stack->index];
275}
276
277/**
278 * @brief Clean up the stack.
279 * @param[in] stack The if-feature stack to use.
280 */
281static void
282iff_stack_clean(struct iff_stack *stack)
283{
284 stack->size = 0;
285 free(stack->stack);
286}
287
288/**
289 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
290 * (libyang format of the if-feature expression).
291 * @param[in,out] list The 2bits array to modify.
292 * @param[in] op The operand (@ref ifftokens) to store.
293 * @param[in] pos Position (0-based) where to store the given @p op.
294 */
295static void
296iff_setop(uint8_t *list, uint8_t op, size_t pos)
297{
298 uint8_t *item;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100299 uint8_t mask = IFF_RECORD_MASK;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100300
Radek Krejcif13b87b2020-12-01 22:02:17 +0100301 assert(op <= IFF_RECORD_MASK); /* max 2 bits */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100302
Radek Krejcif13b87b2020-12-01 22:02:17 +0100303 item = &list[pos / IFF_RECORDS_IN_BYTE];
304 mask = mask << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100305 *item = (*item) & ~mask;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100306 *item = (*item) | (op << IFF_RECORD_BITS * (pos % IFF_RECORDS_IN_BYTE));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100307}
308
309#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
310#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
311
312static LY_ERR
313lys_compile_iffeature(const struct ly_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
314{
315 LY_ERR rc = LY_SUCCESS;
316 const char *c = qname->str;
317 int64_t i, j;
318 int8_t op_len, last_not = 0, checkversion = 0;
319 LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
320 uint8_t op;
321 struct iff_stack stack = {0, 0, NULL};
322 struct lysp_feature *f;
323
324 assert(c);
325
326 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
327 for (i = j = 0; c[i]; i++) {
328 if (c[i] == '(') {
329 j++;
330 checkversion = 1;
331 continue;
332 } else if (c[i] == ')') {
333 j--;
334 continue;
335 } else if (isspace(c[i])) {
336 checkversion = 1;
337 continue;
338 }
339
Radek Krejcif13b87b2020-12-01 22:02:17 +0100340 if (!strncmp(&c[i], "not", op_len = ly_strlen_const("not")) ||
341 !strncmp(&c[i], "and", op_len = ly_strlen_const("and")) ||
342 !strncmp(&c[i], "or", op_len = ly_strlen_const("or"))) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100343 uint64_t spaces;
Michal Vasko26bbb272022-08-02 14:54:33 +0200344
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100345 for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {}
346 if (c[i + op_len + spaces] == '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100347 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100348 return LY_EVALID;
349 } else if (!isspace(c[i + op_len])) {
350 /* feature name starting with the not/and/or */
351 last_not = 0;
352 f_size++;
353 } else if (c[i] == 'n') { /* not operation */
354 if (last_not) {
355 /* double not */
356 expr_size = expr_size - 2;
357 last_not = 0;
358 } else {
359 last_not = 1;
360 }
361 } else { /* and, or */
362 if (f_exp != f_size) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100363 LOGVAL(ctx, LYVE_SYNTAX_YANG,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100364 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
365 qname->str, op_len, &c[i]);
366 return LY_EVALID;
367 }
368 f_exp++;
369
370 /* not a not operation */
371 last_not = 0;
372 }
373 i += op_len;
374 } else {
375 f_size++;
376 last_not = 0;
377 }
378 expr_size++;
379
380 while (!isspace(c[i])) {
381 if (!c[i] || (c[i] == ')') || (c[i] == '(')) {
382 i--;
383 break;
384 }
385 i++;
386 }
387 }
388 if (j) {
389 /* not matching count of ( and ) */
Michal Vasko6a4ef772022-02-08 15:07:44 +0100390 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.",
391 qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100392 return LY_EVALID;
393 }
394 if (f_exp != f_size) {
395 /* features do not match the needed arguments for the logical operations */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100396 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 +0100397 "the required number of operands for the operations.", qname->str);
398 return LY_EVALID;
399 }
400
401 if (checkversion || (expr_size > 1)) {
402 /* check that we have 1.1 module */
403 if (qname->mod->version != LYS_VERSION_1_1) {
Michal Vasko6a4ef772022-02-08 15:07:44 +0100404 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.",
405 qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100406 return LY_EVALID;
407 }
408 }
409
410 /* allocate the memory */
411 LY_ARRAY_CREATE_RET(ctx, iff->features, f_size, LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100412 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 +0100413 stack.stack = malloc(expr_size * sizeof *stack.stack);
414 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx); rc = LY_EMEM, error);
415
416 stack.size = expr_size;
417 f_size--; expr_size--; /* used as indexes from now */
418
419 for (i--; i >= 0; i--) {
420 if (c[i] == ')') {
421 /* push it on stack */
422 iff_stack_push(&stack, LYS_IFF_RP);
423 continue;
424 } else if (c[i] == '(') {
425 /* pop from the stack into result all operators until ) */
426 while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
427 iff_setop(iff->expr, op, expr_size--);
428 }
429 continue;
430 } else if (isspace(c[i])) {
431 continue;
432 }
433
434 /* end of operator or operand -> find beginning and get what is it */
435 j = i + 1;
436 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
437 i--;
438 }
439 i++; /* go back by one step */
440
Radek Krejcif13b87b2020-12-01 22:02:17 +0100441 if (!strncmp(&c[i], "not", ly_strlen_const("not")) && isspace(c[i + ly_strlen_const("not")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100442 if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
443 /* double not */
444 iff_stack_pop(&stack);
445 } else {
446 /* not has the highest priority, so do not pop from the stack
447 * as in case of AND and OR */
448 iff_stack_push(&stack, LYS_IFF_NOT);
449 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 } else if (!strncmp(&c[i], "and", ly_strlen_const("and")) && isspace(c[i + ly_strlen_const("and")])) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100451 /* as for OR - pop from the stack all operators with the same or higher
452 * priority and store them to the result, then push the AND to the stack */
453 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
454 op = iff_stack_pop(&stack);
455 iff_setop(iff->expr, op, expr_size--);
456 }
457 iff_stack_push(&stack, LYS_IFF_AND);
458 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
459 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
460 op = iff_stack_pop(&stack);
461 iff_setop(iff->expr, op, expr_size--);
462 }
463 iff_stack_push(&stack, LYS_IFF_OR);
464 } else {
465 /* feature name, length is j - i */
466
467 /* add it to the expression */
468 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
469
470 /* now get the link to the feature definition */
471 f = lysp_feature_find(qname->mod, &c[i], j - i, 1);
472 if (!f) {
Radek Krejci422afb12021-03-04 16:38:16 +0100473 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".",
474 qname->str, (int)(j - i), &c[i]);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100475 rc = LY_EVALID;
476 goto error;
477 }
478 iff->features[f_size] = f;
479 LY_ARRAY_INCREMENT(iff->features);
480 f_size--;
481 }
482 }
483 while (stack.index) {
484 op = iff_stack_pop(&stack);
485 iff_setop(iff->expr, op, expr_size--);
486 }
487
488 if (++expr_size || ++f_size) {
489 /* not all expected operators and operands found */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100490 LOGVAL(ctx, LYVE_SYNTAX_YANG, "Invalid value \"%s\" of if-feature - processing error.", qname->str);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100491 rc = LY_EINT;
492 } else {
493 rc = LY_SUCCESS;
494 }
495
496error:
497 /* cleanup */
498 iff_stack_clean(&stack);
499
500 return rc;
501}
502
503LY_ERR
504lys_eval_iffeatures(const struct ly_ctx *ctx, struct lysp_qname *iffeatures, ly_bool *enabled)
505{
506 LY_ERR ret;
Michal Vaskoc3f55732021-07-29 10:03:17 +0200507 LY_ARRAY_COUNT_TYPE u;
508 struct lysc_iffeature iff;
509
510 /* enabled by default */
511 *enabled = 1;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100512
513 if (!iffeatures) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100514 return LY_SUCCESS;
515 }
516
Michal Vaskoc3f55732021-07-29 10:03:17 +0200517 /* evaluate all if-feature conditions or until an unsatisfied one is found */
518 LY_ARRAY_FOR(iffeatures, u) {
519 memset(&iff, 0, sizeof iff);
520 LY_CHECK_RET(lys_compile_iffeature(ctx, &iffeatures[u], &iff));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100521
Michal Vaskoc3f55732021-07-29 10:03:17 +0200522 ret = lysc_iffeature_value(&iff);
523 lysc_iffeature_free((struct ly_ctx *)ctx, &iff);
524 if (ret == LY_ENOT) {
525 *enabled = 0;
526 break;
527 } else if (ret) {
528 return ret;
529 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100530 }
531
532 return LY_SUCCESS;
533}
534
Michal Vasko6a4ef772022-02-08 15:07:44 +0100535LY_ERR
536lys_check_features(const struct lysp_module *pmod)
Michal Vasko08c8b272020-11-24 18:11:30 +0100537{
538 LY_ERR r;
539 uint32_t i = 0;
540 struct lysp_feature *f = NULL;
541
542 while ((f = lysp_feature_next(f, pmod, &i))) {
543 if (!(f->flags & LYS_FENABLED) || !f->iffeatures) {
544 /* disabled feature or no if-features to check */
545 continue;
546 }
547
548 assert(f->iffeatures_c);
549 r = lysc_iffeature_value(f->iffeatures_c);
550 if (r == LY_ENOT) {
Michal Vasko6a4ef772022-02-08 15:07:44 +0100551 LOGERR(pmod->mod->ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled because its \"if-feature\" is not satisfied.",
Michal Vasko08c8b272020-11-24 18:11:30 +0100552 f->name);
Michal Vasko6a4ef772022-02-08 15:07:44 +0100553 return LY_EDENIED;
Michal Vasko08c8b272020-11-24 18:11:30 +0100554 } else if (r) {
555 return r;
556 } /* else if-feature satisfied */
557 }
558
559 return LY_SUCCESS;
560}
561
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100562LY_ERR
Michal Vasko08c8b272020-11-24 18:11:30 +0100563lys_set_features(struct lysp_module *pmod, const char **features)
564{
565 uint32_t i = 0, j;
566 struct lysp_feature *f = 0;
567 ly_bool change = 0;
568
569 if (!features) {
Radek Krejci2415f882021-01-20 16:27:09 +0100570 /* do not touch the features */
571
572 } else if (!features[0]) {
Michal Vasko08c8b272020-11-24 18:11:30 +0100573 /* disable all the features */
574 while ((f = lysp_feature_next(f, pmod, &i))) {
575 if (f->flags & LYS_FENABLED) {
576 f->flags &= ~LYS_FENABLED;
577 change = 1;
578 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100579 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100580 } else if (!strcmp(features[0], "*")) {
581 /* enable all the features */
582 while ((f = lysp_feature_next(f, pmod, &i))) {
583 if (!(f->flags & LYS_FENABLED)) {
584 f->flags |= LYS_FENABLED;
585 change = 1;
586 }
587 }
588 } else {
Michal Vaskoc8997c52021-08-23 15:38:34 +0200589 /* check that all the features exist */
590 for (j = 0; features[j]; ++j) {
591 if (!lysp_feature_find(pmod, features[j], strlen(features[j]), 0)) {
592 LOGERR(pmod->mod->ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", features[j], pmod->mod->name);
593 return LY_EINVAL;
594 }
595 }
596
Michal Vasko08c8b272020-11-24 18:11:30 +0100597 /* enable specific features, disable the rest */
598 while ((f = lysp_feature_next(f, pmod, &i))) {
599 for (j = 0; features[j]; ++j) {
600 if (!strcmp(f->name, features[j])) {
601 break;
602 }
603 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100604
Michal Vasko08c8b272020-11-24 18:11:30 +0100605 if (features[j] && !(f->flags & LYS_FENABLED)) {
606 /* enable */
607 f->flags |= LYS_FENABLED;
608 change = 1;
609 } else if (!features[j] && (f->flags & LYS_FENABLED)) {
610 /* disable */
611 f->flags &= ~LYS_FENABLED;
612 change = 1;
613 }
614 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100615 }
616
Michal Vasko08c8b272020-11-24 18:11:30 +0100617 if (!change) {
618 /* features already set correctly */
619 return LY_EEXIST;
620 }
621
Michal Vasko6a4ef772022-02-08 15:07:44 +0100622 return LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100623}
624
625/**
626 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
627 *
628 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
629 *
630 * @param[in] ctx Compile context for logging.
631 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
632 * being currently processed).
633 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
634 * @return LY_SUCCESS if everything is ok.
635 * @return LY_EVALID if the feature references indirectly itself.
636 */
637static LY_ERR
638lys_compile_feature_circular_check(const struct ly_ctx *ctx, struct lysp_feature *feature, struct lysp_feature **depfeatures)
639{
640 LY_ERR ret = LY_SUCCESS;
641 LY_ARRAY_COUNT_TYPE u, v;
642 struct ly_set recursion = {0};
643 struct lysp_feature *drv;
644
645 if (!depfeatures) {
646 return LY_SUCCESS;
647 }
648
649 for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
650 if (feature == depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100651 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100652 ret = LY_EVALID;
653 goto cleanup;
654 }
655 ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
656 LY_CHECK_GOTO(ret, cleanup);
657 }
658
659 for (v = 0; v < recursion.count; ++v) {
660 drv = recursion.objs[v];
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100661 for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
662 if (feature == drv->depfeatures[u]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100663 LOGVAL(ctx, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.", feature->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100664 ret = LY_EVALID;
665 goto cleanup;
666 }
667 ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
668 LY_CHECK_GOTO(ret, cleanup);
669 }
670 }
671
672cleanup:
673 ly_set_erase(&recursion, NULL);
674 return ret;
675}
676
677LY_ERR
678lys_compile_feature_iffeatures(struct lysp_module *pmod)
679{
680 LY_ARRAY_COUNT_TYPE u, v;
681 struct lysp_feature *f = NULL, **df;
682 uint32_t idx = 0;
683
684 while ((f = lysp_feature_next(f, pmod, &idx))) {
685 if (!f->iffeatures) {
686 continue;
687 }
688
689 /* compile if-features */
690 LY_ARRAY_CREATE_RET(pmod->mod->ctx, f->iffeatures_c, LY_ARRAY_COUNT(f->iffeatures), LY_EMEM);
691 LY_ARRAY_FOR(f->iffeatures, u) {
692 LY_ARRAY_INCREMENT(f->iffeatures_c);
693 LY_CHECK_RET(lys_compile_iffeature(pmod->mod->ctx, &(f->iffeatures)[u], &(f->iffeatures_c)[u]));
694 }
695 LY_ARRAY_FOR(f->iffeatures_c, u) {
696 LY_ARRAY_FOR(f->iffeatures_c[u].features, v) {
697 /* check for circular dependency - direct reference first,... */
698 if (f == f->iffeatures_c[u].features[v]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100699 LOGVAL(pmod->mod->ctx, LYVE_REFERENCE, "Feature \"%s\" is referenced from itself.", f->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100700 return LY_EVALID;
701 }
702 /* ... and indirect circular reference */
703 LY_CHECK_RET(lys_compile_feature_circular_check(pmod->mod->ctx, f->iffeatures_c[u].features[v], f->depfeatures));
704
705 /* add itself into the dependants list */
706 LY_ARRAY_NEW_RET(pmod->mod->ctx, f->iffeatures_c[u].features[v]->depfeatures, df, LY_EMEM);
707 *df = f;
708 }
709 }
710 }
711
712 return LY_SUCCESS;
713}