blob: 7a24c76026dc675ee24450ebeef516e2c293716d [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_amend.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of augments, deviations, and refines.
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_compile_amend.h"
18
19#include <assert.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020020#include <stddef.h>
21#include <stdint.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020022#include <stdlib.h>
23#include <string.h>
24
25#include "common.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020026#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020027#include "plugins_exts.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020028#include "schema_compile.h"
29#include "schema_compile_node.h"
Michal Vasko29dd11e2020-11-23 16:52:22 +010030#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020031#include "set.h"
32#include "tree.h"
33#include "tree_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "tree_schema.h"
35#include "tree_schema_internal.h"
36#include "xpath.h"
37
38static const struct lys_module *lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest,
39 size_t nametest_len, const struct lysp_module *mod, const char **name, size_t *name_len);
40
41static LY_ERR
42lys_nodeid_check(struct lysc_ctx *ctx, const char *nodeid, ly_bool abs, struct lys_module **target_mod,
43 struct lyxp_expr **expr)
44{
45 LY_ERR ret = LY_SUCCESS;
46 struct lyxp_expr *e = NULL;
47 struct lys_module *tmod = NULL, *mod;
48 const char *nodeid_type = abs ? "absolute-schema-nodeid" : "descendant-schema-nodeid";
49 uint32_t i;
50
51 /* parse */
52 ret = lyxp_expr_parse(ctx->ctx, nodeid, strlen(nodeid), 0, &e);
53 if (ret) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010054 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s value \"%s\" - invalid syntax.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020055 nodeid_type, nodeid);
56 ret = LY_EVALID;
57 goto cleanup;
58 }
59
60 if (abs) {
61 /* absolute schema nodeid */
62 i = 0;
63 } else {
64 /* descendant schema nodeid */
65 if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010066 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020067 nodeid_type, nodeid, e->tok_len[0], e->expr + e->tok_pos[0]);
68 ret = LY_EVALID;
69 goto cleanup;
70 }
71 i = 1;
72 }
73
74 /* check all the tokens */
75 for ( ; i < e->used; i += 2) {
76 if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010077 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020078 nodeid_type, nodeid, e->tok_len[i], e->expr + e->tok_pos[i]);
79 ret = LY_EVALID;
80 goto cleanup;
81 } else if (e->used == i + 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010082 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020083 "Invalid %s value \"%s\" - unexpected end of expression.", nodeid_type, e->expr);
84 ret = LY_EVALID;
85 goto cleanup;
86 } else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010087 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020088 nodeid_type, nodeid, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
89 ret = LY_EVALID;
90 goto cleanup;
91 } else if (abs) {
92 mod = (struct lys_module *)lys_schema_node_get_module(ctx->ctx, e->expr + e->tok_pos[i + 1],
93 e->tok_len[i + 1], ctx->pmod, NULL, NULL);
94 LY_CHECK_ERR_GOTO(!mod, ret = LY_EVALID, cleanup);
95
96 /* only keep the first module */
97 if (!tmod) {
98 tmod = mod;
99 }
100
101 /* all the modules must be implemented */
102 if (!mod->implemented) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100103 ret = lys_set_implemented_r(mod, NULL, ctx->unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200104 LY_CHECK_GOTO(ret, cleanup);
105 }
106 }
107 }
108
109cleanup:
110 if (ret || !expr) {
111 lyxp_expr_free(ctx->ctx, e);
112 e = NULL;
113 }
114 if (expr) {
115 *expr = ret ? NULL : e;
116 }
117 if (target_mod) {
118 *target_mod = ret ? NULL : tmod;
119 }
120 return ret;
121}
122
123/**
124 * @brief Check whether 2 schema nodeids match.
125 *
126 * @param[in] ctx libyang context.
127 * @param[in] exp1 First schema nodeid.
128 * @param[in] exp1p_mod Module of @p exp1 nodes without any prefix.
129 * @param[in] exp2 Second schema nodeid.
130 * @param[in] exp2_pmod Module of @p exp2 nodes without any prefix.
131 * @return Whether the schema nodeids match or not.
132 */
133static ly_bool
134lys_abs_schema_nodeid_match(const struct ly_ctx *ctx, const struct lyxp_expr *exp1, const struct lysp_module *exp1_pmod,
135 const struct lyxp_expr *exp2, const struct lysp_module *exp2_pmod)
136{
137 uint32_t i;
138 const struct lys_module *mod1, *mod2;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100139 const char *name1 = NULL, *name2 = NULL;
140 size_t name1_len = 0, name2_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200141
142 if (exp1->used != exp2->used) {
143 return 0;
144 }
145
146 for (i = 0; i < exp1->used; ++i) {
147 assert(exp1->tokens[i] == exp2->tokens[i]);
148
149 if (exp1->tokens[i] == LYXP_TOKEN_NAMETEST) {
150 /* check modules of all the nodes in the node ID */
151 mod1 = lys_schema_node_get_module(ctx, exp1->expr + exp1->tok_pos[i], exp1->tok_len[i], exp1_pmod,
152 &name1, &name1_len);
153 assert(mod1);
154 mod2 = lys_schema_node_get_module(ctx, exp2->expr + exp2->tok_pos[i], exp2->tok_len[i], exp2_pmod,
155 &name2, &name2_len);
156 assert(mod2);
157
158 /* compare modules */
159 if (mod1 != mod2) {
160 return 0;
161 }
162
163 /* compare names */
164 if ((name1_len != name2_len) || strncmp(name1, name2, name1_len)) {
165 return 0;
166 }
167 }
168 }
169
170 return 1;
171}
172
173LY_ERR
174lys_precompile_uses_augments_refines(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, const struct lysc_node *ctx_node)
175{
176 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200177 struct lyxp_expr *exp = NULL;
178 struct lysc_augment *aug;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100179 struct lysp_node_augment *aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200180 struct lysc_refine *rfn;
181 struct lysp_refine **new_rfn;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100182 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200183 uint32_t i;
184
Radek Krejci2a9fc652021-01-22 17:44:34 +0100185 LY_LIST_FOR(uses_p->augments, aug_p) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200186 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +0100187 lysc_update_path(ctx, NULL, aug_p->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200188
189 /* parse the nodeid */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100190 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, aug_p->nodeid, 0, NULL, &exp), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200191
192 /* allocate new compiled augment and store it in the set */
193 aug = calloc(1, sizeof *aug);
194 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
195 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_augs, aug, 1, NULL), cleanup);
196
197 aug->nodeid = exp;
198 exp = NULL;
199 aug->nodeid_pmod = ctx->pmod;
200 aug->nodeid_ctx_node = ctx_node;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100201 aug->aug_p = aug_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200202
203 lysc_update_path(ctx, NULL, NULL);
204 lysc_update_path(ctx, NULL, NULL);
205 }
206
207 LY_ARRAY_FOR(uses_p->refines, u) {
208 lysc_update_path(ctx, NULL, "{refine}");
209 lysc_update_path(ctx, NULL, uses_p->refines[u].nodeid);
210
211 /* parse the nodeid */
212 LY_CHECK_GOTO(ret = lys_nodeid_check(ctx, uses_p->refines[u].nodeid, 0, NULL, &exp), cleanup);
213
214 /* try to find the node in already compiled refines */
215 rfn = NULL;
216 for (i = 0; i < ctx->uses_rfns.count; ++i) {
217 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, ctx->pmod, ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid,
218 ctx->pmod)) {
219 rfn = ctx->uses_rfns.objs[i];
220 break;
221 }
222 }
223
224 if (!rfn) {
225 /* allocate new compiled refine */
226 rfn = calloc(1, sizeof *rfn);
227 LY_CHECK_ERR_GOTO(!rfn, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
228 LY_CHECK_GOTO(ret = ly_set_add(&ctx->uses_rfns, rfn, 1, NULL), cleanup);
229
230 rfn->nodeid = exp;
231 exp = NULL;
232 rfn->nodeid_pmod = ctx->pmod;
233 rfn->nodeid_ctx_node = ctx_node;
Michal Vaskod8655722021-01-12 15:20:36 +0100234 rfn->uses_p = uses_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200235 } else {
236 /* just free exp */
237 lyxp_expr_free(ctx->ctx, exp);
238 exp = NULL;
239 }
240
241 /* add new parsed refine structure */
242 LY_ARRAY_NEW_GOTO(ctx->ctx, rfn->rfns, new_rfn, ret, cleanup);
243 *new_rfn = &uses_p->refines[u];
244
245 lysc_update_path(ctx, NULL, NULL);
246 lysc_update_path(ctx, NULL, NULL);
247 }
248
249cleanup:
250 lyxp_expr_free(ctx->ctx, exp);
251 return ret;
252}
253
254static LY_ERR
255lysp_ext_dup(const struct ly_ctx *ctx, struct lysp_ext_instance *ext, const struct lysp_ext_instance *orig_ext)
256{
257 LY_ERR ret = LY_SUCCESS;
258
259 *ext = *orig_ext;
260 DUP_STRING(ctx, orig_ext->name, ext->name, ret);
261 DUP_STRING(ctx, orig_ext->argument, ext->argument, ret);
262
263 return ret;
264}
265
266static LY_ERR
267lysp_restr_dup(const struct ly_ctx *ctx, struct lysp_restr *restr, const struct lysp_restr *orig_restr)
268{
269 LY_ERR ret = LY_SUCCESS;
270
271 if (orig_restr) {
272 DUP_STRING(ctx, orig_restr->arg.str, restr->arg.str, ret);
273 restr->arg.mod = orig_restr->arg.mod;
274 DUP_STRING(ctx, orig_restr->emsg, restr->emsg, ret);
275 DUP_STRING(ctx, orig_restr->eapptag, restr->eapptag, ret);
276 DUP_STRING(ctx, orig_restr->dsc, restr->dsc, ret);
277 DUP_STRING(ctx, orig_restr->ref, restr->ref, ret);
278 DUP_ARRAY(ctx, orig_restr->exts, restr->exts, lysp_ext_dup);
279 }
280
281 return ret;
282}
283
284static LY_ERR
285lysp_string_dup(const struct ly_ctx *ctx, const char **str, const char **orig_str)
286{
287 LY_ERR ret = LY_SUCCESS;
288
289 DUP_STRING(ctx, *orig_str, *str, ret);
290
291 return ret;
292}
293
294LY_ERR
295lysp_qname_dup(const struct ly_ctx *ctx, struct lysp_qname *qname, const struct lysp_qname *orig_qname)
296{
297 LY_ERR ret = LY_SUCCESS;
298
299 if (!orig_qname->str) {
300 return LY_SUCCESS;
301 }
302
303 DUP_STRING(ctx, orig_qname->str, qname->str, ret);
304 assert(orig_qname->mod);
305 qname->mod = orig_qname->mod;
306
307 return ret;
308}
309
310static LY_ERR
311lysp_type_enum_dup(const struct ly_ctx *ctx, struct lysp_type_enum *enm, const struct lysp_type_enum *orig_enm)
312{
313 LY_ERR ret = LY_SUCCESS;
314
315 DUP_STRING(ctx, orig_enm->name, enm->name, ret);
316 DUP_STRING(ctx, orig_enm->dsc, enm->dsc, ret);
317 DUP_STRING(ctx, orig_enm->ref, enm->ref, ret);
318 enm->value = orig_enm->value;
319 DUP_ARRAY(ctx, orig_enm->iffeatures, enm->iffeatures, lysp_qname_dup);
320 DUP_ARRAY(ctx, orig_enm->exts, enm->exts, lysp_ext_dup);
321 enm->flags = orig_enm->flags;
322
323 return ret;
324}
325
326static LY_ERR
327lysp_type_dup(const struct ly_ctx *ctx, struct lysp_type *type, const struct lysp_type *orig_type)
328{
329 LY_ERR ret = LY_SUCCESS;
330
331 DUP_STRING_GOTO(ctx, orig_type->name, type->name, ret, done);
332
333 if (orig_type->range) {
334 type->range = calloc(1, sizeof *type->range);
335 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx), LY_EMEM);
336 LY_CHECK_RET(lysp_restr_dup(ctx, type->range, orig_type->range));
337 }
338
339 if (orig_type->length) {
340 type->length = calloc(1, sizeof *type->length);
341 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx), LY_EMEM);
342 LY_CHECK_RET(lysp_restr_dup(ctx, type->length, orig_type->length));
343 }
344
345 DUP_ARRAY(ctx, orig_type->patterns, type->patterns, lysp_restr_dup);
346 DUP_ARRAY(ctx, orig_type->enums, type->enums, lysp_type_enum_dup);
347 DUP_ARRAY(ctx, orig_type->bits, type->bits, lysp_type_enum_dup);
348 LY_CHECK_GOTO(ret = lyxp_expr_dup(ctx, orig_type->path, &type->path), done);
349 DUP_ARRAY(ctx, orig_type->bases, type->bases, lysp_string_dup);
350 DUP_ARRAY(ctx, orig_type->types, type->types, lysp_type_dup);
351 DUP_ARRAY(ctx, orig_type->exts, type->exts, lysp_ext_dup);
352
353 type->pmod = orig_type->pmod;
354 type->compiled = orig_type->compiled;
355
356 type->fraction_digits = orig_type->fraction_digits;
357 type->require_instance = orig_type->require_instance;
358 type->flags = orig_type->flags;
359
360done:
361 return ret;
362}
363
364static LY_ERR
365lysp_when_dup(const struct ly_ctx *ctx, struct lysp_when *when, const struct lysp_when *orig_when)
366{
367 LY_ERR ret = LY_SUCCESS;
368
369 DUP_STRING(ctx, orig_when->cond, when->cond, ret);
370 DUP_STRING(ctx, orig_when->dsc, when->dsc, ret);
371 DUP_STRING(ctx, orig_when->ref, when->ref, ret);
372 DUP_ARRAY(ctx, orig_when->exts, when->exts, lysp_ext_dup);
373
374 return ret;
375}
376
377static LY_ERR
378lysp_node_common_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
379{
380 LY_ERR ret = LY_SUCCESS;
381
382 node->parent = NULL;
383 node->nodetype = orig->nodetype;
384 node->flags = orig->flags;
385 node->next = NULL;
386 DUP_STRING(ctx, orig->name, node->name, ret);
387 DUP_STRING(ctx, orig->dsc, node->dsc, ret);
388 DUP_STRING(ctx, orig->ref, node->ref, ret);
389
390 if (orig->when) {
391 node->when = calloc(1, sizeof *node->when);
392 LY_CHECK_ERR_RET(!node->when, LOGMEM(ctx), LY_EMEM);
393 LY_CHECK_RET(lysp_when_dup(ctx, node->when, orig->when));
394 }
395
396 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
397 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
398
399 return ret;
400}
401
402static LY_ERR
403lysp_node_dup(const struct ly_ctx *ctx, struct lysp_node *node, const struct lysp_node *orig)
404{
405 LY_ERR ret = LY_SUCCESS;
406 struct lysp_node_container *cont;
407 const struct lysp_node_container *orig_cont;
408 struct lysp_node_leaf *leaf;
409 const struct lysp_node_leaf *orig_leaf;
410 struct lysp_node_leaflist *llist;
411 const struct lysp_node_leaflist *orig_llist;
412 struct lysp_node_list *list;
413 const struct lysp_node_list *orig_list;
414 struct lysp_node_choice *choice;
415 const struct lysp_node_choice *orig_choice;
416 struct lysp_node_case *cas;
417 const struct lysp_node_case *orig_cas;
418 struct lysp_node_anydata *any;
419 const struct lysp_node_anydata *orig_any;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100420 struct lysp_node_action *action;
421 const struct lysp_node_action *orig_action;
422 struct lysp_node_action_inout *action_inout;
423 const struct lysp_node_action_inout *orig_action_inout;
424 struct lysp_node_notif *notif;
425 const struct lysp_node_notif *orig_notif;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200426
Radek Krejci2a9fc652021-01-22 17:44:34 +0100427 assert(orig->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_ANYDATA |
428 LYS_RPC | LYS_ACTION | LYS_NOTIF));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200429
430 /* common part */
431 LY_CHECK_RET(lysp_node_common_dup(ctx, node, orig));
432
433 /* specific part */
434 switch (node->nodetype) {
435 case LYS_CONTAINER:
436 cont = (struct lysp_node_container *)node;
437 orig_cont = (const struct lysp_node_container *)orig;
438
439 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
440 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
441 /* we do not need the rest */
442 break;
443 case LYS_LEAF:
444 leaf = (struct lysp_node_leaf *)node;
445 orig_leaf = (const struct lysp_node_leaf *)orig;
446
447 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
448 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
449 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
450 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
451 break;
452 case LYS_LEAFLIST:
453 llist = (struct lysp_node_leaflist *)node;
454 orig_llist = (const struct lysp_node_leaflist *)orig;
455
456 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
457 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
458 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
459 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
460 llist->min = orig_llist->min;
461 llist->max = orig_llist->max;
462 break;
463 case LYS_LIST:
464 list = (struct lysp_node_list *)node;
465 orig_list = (const struct lysp_node_list *)orig;
466
467 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
468 DUP_STRING(ctx, orig_list->key, list->key, ret);
469 /* we do not need these arrays */
470 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
471 list->min = orig_list->min;
472 list->max = orig_list->max;
473 break;
474 case LYS_CHOICE:
475 choice = (struct lysp_node_choice *)node;
476 orig_choice = (const struct lysp_node_choice *)orig;
477
478 /* we do not need children */
479 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
480 break;
481 case LYS_CASE:
482 cas = (struct lysp_node_case *)node;
483 orig_cas = (const struct lysp_node_case *)orig;
484
485 /* we do not need children */
486 (void)cas;
487 (void)orig_cas;
488 break;
489 case LYS_ANYDATA:
490 case LYS_ANYXML:
491 any = (struct lysp_node_anydata *)node;
492 orig_any = (const struct lysp_node_anydata *)orig;
493
494 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
495 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100496 case LYS_RPC:
497 case LYS_ACTION:
498 action = (struct lysp_node_action *)node;
499 orig_action = (const struct lysp_node_action *)orig;
500
501 action->input.nodetype = orig_action->input.nodetype;
502 action->output.nodetype = orig_action->output.nodetype;
503 /* we do not need the rest */
504 break;
505 case LYS_INPUT:
506 case LYS_OUTPUT:
507 action_inout = (struct lysp_node_action_inout *)node;
508 orig_action_inout = (const struct lysp_node_action_inout *)orig;
509
510 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
511 /* we do not need the rest */
512 break;
513 case LYS_NOTIF:
514 notif = (struct lysp_node_notif *)node;
515 orig_notif = (const struct lysp_node_notif *)orig;
516
517 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
518 /* we do not need the rest */
519 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200520 default:
521 LOGINT_RET(ctx);
522 }
523
524 return ret;
525}
526
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200527/**
528 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
529 *
530 * @param[in] ctx libyang context.
531 * @param[in] pnode Node to duplicate.
532 * @param[in] with_links Whether to also copy any links (child, parent pointers).
533 * @param[out] dup_p Duplicated parsed node.
534 * @return LY_ERR value.
535 */
536static LY_ERR
537lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
538{
539 LY_ERR ret = LY_SUCCESS;
540 void *mem = NULL;
541
542 if (!pnode) {
543 *dup_p = NULL;
544 return LY_SUCCESS;
545 }
546
547 switch (pnode->nodetype) {
548 case LYS_CONTAINER:
549 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200550 break;
551 case LYS_LEAF:
552 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200553 break;
554 case LYS_LEAFLIST:
555 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200556 break;
557 case LYS_LIST:
558 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200559 break;
560 case LYS_CHOICE:
561 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200562 break;
563 case LYS_CASE:
564 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200565 break;
566 case LYS_ANYDATA:
567 case LYS_ANYXML:
568 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200569 break;
570 case LYS_INPUT:
571 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100572 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200573 break;
574 case LYS_ACTION:
575 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100576 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200577 break;
578 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100579 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200580 break;
581 default:
582 LOGINT_RET(ctx);
583 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100584 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
585 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200586
587 if (with_links) {
588 /* copy also parent and child pointers */
589 ((struct lysp_node *)mem)->parent = pnode->parent;
590 switch (pnode->nodetype) {
591 case LYS_CONTAINER:
592 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
593 break;
594 case LYS_LIST:
595 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
596 break;
597 case LYS_CHOICE:
598 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
599 break;
600 case LYS_CASE:
601 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
602 break;
603 default:
604 break;
605 }
606 }
607
608cleanup:
609 if (ret) {
610 free(mem);
611 } else {
612 *dup_p = mem;
613 }
614 return ret;
615}
616
617#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100618 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s of %s node - it is not possible to %s \"%s\" property.", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200619 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
620 ret = LY_EVALID; \
621 goto cleanup;
622
623#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
624 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100625 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid %s of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200626 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
627 ret = LY_EVALID; \
628 goto cleanup; \
629 }
630
631/**
632 * @brief Apply refine.
633 *
634 * @param[in] ctx Compile context.
635 * @param[in] rfn Refine to apply.
636 * @param[in,out] target Refine target.
637 * @return LY_ERR value.
638 */
639static LY_ERR
640lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
641{
642 LY_ERR ret = LY_SUCCESS;
643 LY_ARRAY_COUNT_TYPE u;
644 struct lysp_qname *qname;
645 struct lysp_restr **musts, *must;
646 uint32_t *num;
647
648 /* default value */
649 if (rfn->dflts) {
650 switch (target->nodetype) {
651 case LYS_LEAF:
652 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
653
654 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
655 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
656 break;
657 case LYS_LEAFLIST:
658 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100659 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200660 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
661 ret = LY_EVALID;
662 goto cleanup;
663 }
664
665 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
666 ((struct lysp_node_leaflist *)target)->dflts = NULL;
667 LY_ARRAY_FOR(rfn->dflts, u) {
668 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
669 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
670 }
671 break;
672 case LYS_CHOICE:
673 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
674
675 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
676 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
677 break;
678 default:
679 AMEND_WRONG_NODETYPE("refine", "replace", "default");
680 }
681 }
682
683 /* description */
684 if (rfn->dsc) {
685 FREE_STRING(ctx->ctx, target->dsc);
686 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
687 }
688
689 /* reference */
690 if (rfn->ref) {
691 FREE_STRING(ctx->ctx, target->ref);
692 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
693 }
694
695 /* config */
696 if (rfn->flags & LYS_CONFIG_MASK) {
697 if (ctx->options & (LYS_COMPILE_NOTIFICATION | LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
698 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
699 ctx->options & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
700 } else {
701 target->flags &= ~LYS_CONFIG_MASK;
702 target->flags |= rfn->flags & LYS_CONFIG_MASK;
703 }
704 }
705
706 /* mandatory */
707 if (rfn->flags & LYS_MAND_MASK) {
708 switch (target->nodetype) {
709 case LYS_LEAF:
710 case LYS_CHOICE:
711 case LYS_ANYDATA:
712 case LYS_ANYXML:
713 break;
714 default:
715 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
716 }
717
718 target->flags &= ~LYS_MAND_MASK;
719 target->flags |= rfn->flags & LYS_MAND_MASK;
720 }
721
722 /* presence */
723 if (rfn->presence) {
724 switch (target->nodetype) {
725 case LYS_CONTAINER:
726 break;
727 default:
728 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
729 }
730
731 FREE_STRING(ctx->ctx, ((struct lysp_node_container *)target)->presence);
732 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
733 }
734
735 /* must */
736 if (rfn->musts) {
737 switch (target->nodetype) {
738 case LYS_CONTAINER:
739 case LYS_LIST:
740 case LYS_LEAF:
741 case LYS_LEAFLIST:
742 case LYS_ANYDATA:
743 case LYS_ANYXML:
744 musts = &((struct lysp_node_container *)target)->musts;
745 break;
746 default:
747 AMEND_WRONG_NODETYPE("refine", "add", "must");
748 }
749
750 LY_ARRAY_FOR(rfn->musts, u) {
751 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
752 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
753 }
754 }
755
756 /* min-elements */
757 if (rfn->flags & LYS_SET_MIN) {
758 switch (target->nodetype) {
759 case LYS_LEAFLIST:
760 num = &((struct lysp_node_leaflist *)target)->min;
761 break;
762 case LYS_LIST:
763 num = &((struct lysp_node_list *)target)->min;
764 break;
765 default:
766 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
767 }
768
769 *num = rfn->min;
770 }
771
772 /* max-elements */
773 if (rfn->flags & LYS_SET_MAX) {
774 switch (target->nodetype) {
775 case LYS_LEAFLIST:
776 num = &((struct lysp_node_leaflist *)target)->max;
777 break;
778 case LYS_LIST:
779 num = &((struct lysp_node_list *)target)->max;
780 break;
781 default:
782 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
783 }
784
785 *num = rfn->max;
786 }
787
788 /* if-feature */
789 if (rfn->iffeatures) {
790 switch (target->nodetype) {
791 case LYS_LEAF:
792 case LYS_LEAFLIST:
793 case LYS_LIST:
794 case LYS_CONTAINER:
795 case LYS_CHOICE:
796 case LYS_CASE:
797 case LYS_ANYDATA:
798 case LYS_ANYXML:
799 break;
800 default:
801 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
802 }
803
804 LY_ARRAY_FOR(rfn->iffeatures, u) {
805 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
806 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
807 }
808 }
809
810 /* extension */
811 /* TODO refine extensions */
812
813cleanup:
814 return ret;
815}
816
817/**
818 * @brief Apply deviate add.
819 *
820 * @param[in] ctx Compile context.
821 * @param[in] d Deviate add to apply.
822 * @param[in,out] target Deviation target.
823 * @return LY_ERR value.
824 */
825static LY_ERR
826lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
827{
828 LY_ERR ret = LY_SUCCESS;
829 LY_ARRAY_COUNT_TYPE u;
830 struct lysp_qname *qname;
831 uint32_t *num;
832 struct lysp_restr **musts, *must;
833
834#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
835 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100836 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200837 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
838 ret = LY_EVALID; \
839 goto cleanup; \
840 }
841
842 /* [units-stmt] */
843 if (d->units) {
844 switch (target->nodetype) {
845 case LYS_LEAF:
846 case LYS_LEAFLIST:
847 break;
848 default:
849 AMEND_WRONG_NODETYPE("deviation", "add", "units");
850 }
851
852 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
853 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
854 }
855
856 /* *must-stmt */
857 if (d->musts) {
858 switch (target->nodetype) {
859 case LYS_CONTAINER:
860 case LYS_LIST:
861 case LYS_LEAF:
862 case LYS_LEAFLIST:
863 case LYS_ANYDATA:
864 case LYS_ANYXML:
865 musts = &((struct lysp_node_container *)target)->musts;
866 break;
867 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100868 musts = &((struct lysp_node_notif *)target)->musts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200869 break;
870 case LYS_INPUT:
871 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100872 musts = &((struct lysp_node_action_inout *)target)->musts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200873 break;
874 default:
875 AMEND_WRONG_NODETYPE("deviation", "add", "must");
876 }
877
878 LY_ARRAY_FOR(d->musts, u) {
879 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
880 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
881 }
882 }
883
884 /* *unique-stmt */
885 if (d->uniques) {
886 switch (target->nodetype) {
887 case LYS_LIST:
888 break;
889 default:
890 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
891 }
892
893 LY_ARRAY_FOR(d->uniques, u) {
894 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
895 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
896 }
897 }
898
899 /* *default-stmt */
900 if (d->dflts) {
901 switch (target->nodetype) {
902 case LYS_LEAF:
903 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
904 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
905
906 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
907 break;
908 case LYS_LEAFLIST:
909 LY_ARRAY_FOR(d->dflts, u) {
910 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
911 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
912 }
913 break;
914 case LYS_CHOICE:
915 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
916 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
917
918 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
919 break;
920 default:
921 AMEND_WRONG_NODETYPE("deviation", "add", "default");
922 }
923 }
924
925 /* [config-stmt] */
926 if (d->flags & LYS_CONFIG_MASK) {
927 switch (target->nodetype) {
928 case LYS_CONTAINER:
929 case LYS_LEAF:
930 case LYS_LEAFLIST:
931 case LYS_LIST:
932 case LYS_CHOICE:
933 case LYS_ANYDATA:
934 case LYS_ANYXML:
935 break;
936 default:
937 AMEND_WRONG_NODETYPE("deviation", "add", "config");
938 }
939
940 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100941 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200942 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
943 target->flags & LYS_CONFIG_W ? "true" : "false");
944 ret = LY_EVALID;
945 goto cleanup;
946 }
947
948 target->flags |= d->flags & LYS_CONFIG_MASK;
949 }
950
951 /* [mandatory-stmt] */
952 if (d->flags & LYS_MAND_MASK) {
953 switch (target->nodetype) {
954 case LYS_LEAF:
955 case LYS_CHOICE:
956 case LYS_ANYDATA:
957 case LYS_ANYXML:
958 break;
959 default:
960 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
961 }
962
963 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100964 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200965 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
966 target->flags & LYS_MAND_TRUE ? "true" : "false");
967 ret = LY_EVALID;
968 goto cleanup;
969 }
970
971 target->flags |= d->flags & LYS_MAND_MASK;
972 }
973
974 /* [min-elements-stmt] */
975 if (d->flags & LYS_SET_MIN) {
976 switch (target->nodetype) {
977 case LYS_LEAFLIST:
978 num = &((struct lysp_node_leaflist *)target)->min;
979 break;
980 case LYS_LIST:
981 num = &((struct lysp_node_list *)target)->min;
982 break;
983 default:
984 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
985 }
986
987 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100988 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200989 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
990 ret = LY_EVALID;
991 goto cleanup;
992 }
993
994 *num = d->min;
995 }
996
997 /* [max-elements-stmt] */
998 if (d->flags & LYS_SET_MAX) {
999 switch (target->nodetype) {
1000 case LYS_LEAFLIST:
1001 num = &((struct lysp_node_leaflist *)target)->max;
1002 break;
1003 case LYS_LIST:
1004 num = &((struct lysp_node_list *)target)->max;
1005 break;
1006 default:
1007 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
1008 }
1009
1010 if (target->flags & LYS_SET_MAX) {
1011 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001012 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001013 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1014 *num);
1015 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001016 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001017 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1018 }
1019 ret = LY_EVALID;
1020 goto cleanup;
1021 }
1022
1023 *num = d->max;
1024 }
1025
1026cleanup:
1027 return ret;
1028}
1029
1030/**
1031 * @brief Apply deviate delete.
1032 *
1033 * @param[in] ctx Compile context.
1034 * @param[in] d Deviate delete to apply.
1035 * @param[in,out] target Deviation target.
1036 * @return LY_ERR value.
1037 */
1038static LY_ERR
1039lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1040{
1041 LY_ERR ret = LY_SUCCESS;
1042 struct lysp_restr **musts;
1043 LY_ARRAY_COUNT_TYPE u, v;
1044 struct lysp_qname **uniques, **dflts;
1045
1046#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1047 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1048 int found = 0; \
1049 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1050 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1051 found = 1; \
1052 break; \
1053 } \
1054 } \
1055 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001056 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001057 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1058 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1059 ret = LY_EVALID; \
1060 goto cleanup; \
1061 } \
1062 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1063 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
1064 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1065 } \
1066 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1067 LY_ARRAY_FREE(ORIG_ARRAY); \
1068 ORIG_ARRAY = NULL; \
1069 }
1070
1071#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1072 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001073 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001074 ret = LY_EVALID; \
1075 goto cleanup; \
1076 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001077 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001078 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1079 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1080 ret = LY_EVALID; \
1081 goto cleanup; \
1082 }
1083
1084 /* [units-stmt] */
1085 if (d->units) {
1086 switch (target->nodetype) {
1087 case LYS_LEAF:
1088 case LYS_LEAFLIST:
1089 break;
1090 default:
1091 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1092 }
1093
1094 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
1095 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1096 ((struct lysp_node_leaf *)target)->units = NULL;
1097 }
1098
1099 /* *must-stmt */
1100 if (d->musts) {
1101 switch (target->nodetype) {
1102 case LYS_CONTAINER:
1103 case LYS_LIST:
1104 case LYS_LEAF:
1105 case LYS_LEAFLIST:
1106 case LYS_ANYDATA:
1107 case LYS_ANYXML:
1108 musts = &((struct lysp_node_container *)target)->musts;
1109 break;
1110 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001111 musts = &((struct lysp_node_notif *)target)->musts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001112 break;
1113 case LYS_INPUT:
1114 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001115 musts = &((struct lysp_node_action_inout *)target)->musts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001116 break;
1117 default:
1118 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1119 }
1120
1121 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1122 }
1123
1124 /* *unique-stmt */
1125 if (d->uniques) {
1126 switch (target->nodetype) {
1127 case LYS_LIST:
1128 break;
1129 default:
1130 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1131 }
1132
1133 uniques = &((struct lysp_node_list *)target)->uniques;
1134 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1135 }
1136
1137 /* *default-stmt */
1138 if (d->dflts) {
1139 switch (target->nodetype) {
1140 case LYS_LEAF:
1141 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1142 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1143
1144 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1145 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1146 break;
1147 case LYS_LEAFLIST:
1148 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1149 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1150 break;
1151 case LYS_CHOICE:
1152 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1153 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1154
1155 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1156 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1157 break;
1158 default:
1159 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1160 }
1161 }
1162
1163cleanup:
1164 return ret;
1165}
1166
1167/**
1168 * @brief Apply deviate replace.
1169 *
1170 * @param[in] ctx Compile context.
1171 * @param[in] d Deviate replace to apply.
1172 * @param[in,out] target Deviation target.
1173 * @return LY_ERR value.
1174 */
1175static LY_ERR
1176lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1177{
1178 LY_ERR ret = LY_SUCCESS;
1179 uint32_t *num;
1180
1181#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1182 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001183 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001184 ret = LY_EVALID; \
1185 goto cleanup; \
1186 }
1187
1188 /* [type-stmt] */
1189 if (d->type) {
1190 switch (target->nodetype) {
1191 case LYS_LEAF:
1192 case LYS_LEAFLIST:
1193 break;
1194 default:
1195 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1196 }
1197
1198 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1199 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1200 }
1201
1202 /* [units-stmt] */
1203 if (d->units) {
1204 switch (target->nodetype) {
1205 case LYS_LEAF:
1206 case LYS_LEAFLIST:
1207 break;
1208 default:
1209 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1210 }
1211
1212 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
1213 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1214 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1215 }
1216
1217 /* [default-stmt] */
1218 if (d->dflt.str) {
1219 switch (target->nodetype) {
1220 case LYS_LEAF:
1221 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1222
1223 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1224 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1225 break;
1226 case LYS_CHOICE:
1227 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1228
1229 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1230 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1231 break;
1232 default:
1233 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1234 }
1235 }
1236
1237 /* [config-stmt] */
1238 if (d->flags & LYS_CONFIG_MASK) {
1239 switch (target->nodetype) {
1240 case LYS_CONTAINER:
1241 case LYS_LEAF:
1242 case LYS_LEAFLIST:
1243 case LYS_LIST:
1244 case LYS_CHOICE:
1245 case LYS_ANYDATA:
1246 case LYS_ANYXML:
1247 break;
1248 default:
1249 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1250 }
1251
1252 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001253 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1254 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001255 ret = LY_EVALID;
1256 goto cleanup;
1257 }
1258
1259 target->flags &= ~LYS_CONFIG_MASK;
1260 target->flags |= d->flags & LYS_CONFIG_MASK;
1261 }
1262
1263 /* [mandatory-stmt] */
1264 if (d->flags & LYS_MAND_MASK) {
1265 switch (target->nodetype) {
1266 case LYS_LEAF:
1267 case LYS_CHOICE:
1268 case LYS_ANYDATA:
1269 case LYS_ANYXML:
1270 break;
1271 default:
1272 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1273 }
1274
1275 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001276 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1277 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001278 ret = LY_EVALID;
1279 goto cleanup;
1280 }
1281
1282 target->flags &= ~LYS_MAND_MASK;
1283 target->flags |= d->flags & LYS_MAND_MASK;
1284 }
1285
1286 /* [min-elements-stmt] */
1287 if (d->flags & LYS_SET_MIN) {
1288 switch (target->nodetype) {
1289 case LYS_LEAFLIST:
1290 num = &((struct lysp_node_leaflist *)target)->min;
1291 break;
1292 case LYS_LIST:
1293 num = &((struct lysp_node_list *)target)->min;
1294 break;
1295 default:
1296 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1297 }
1298
1299 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001300 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001301 ret = LY_EVALID;
1302 goto cleanup;
1303 }
1304
1305 *num = d->min;
1306 }
1307
1308 /* [max-elements-stmt] */
1309 if (d->flags & LYS_SET_MAX) {
1310 switch (target->nodetype) {
1311 case LYS_LEAFLIST:
1312 num = &((struct lysp_node_leaflist *)target)->max;
1313 break;
1314 case LYS_LIST:
1315 num = &((struct lysp_node_list *)target)->max;
1316 break;
1317 default:
1318 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1319 }
1320
1321 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001322 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001323 ret = LY_EVALID;
1324 goto cleanup;
1325 }
1326
1327 *num = d->max;
1328 }
1329
1330cleanup:
1331 return ret;
1332}
1333
1334/**
1335 * @brief Get module of a single nodeid node name test.
1336 *
1337 * @param[in] ctx libyang context.
1338 * @param[in] nametest Nametest with an optional prefix.
1339 * @param[in] nametest_len Length of @p nametest.
1340 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1341 * @param[out] name Optional pointer to the name test without the prefix.
1342 * @param[out] name_len Length of @p name.
1343 * @return Resolved module.
1344 */
1345static const struct lys_module *
1346lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1347 const struct lysp_module *mod, const char **name, size_t *name_len)
1348{
1349 const struct lys_module *target_mod;
1350 const char *ptr;
1351
1352 ptr = ly_strnchr(nametest, ':', nametest_len);
1353 if (ptr) {
1354 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)mod);
1355 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001356 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001357 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1358 nametest_len, nametest, ptr - nametest, nametest, LYSP_MODULE_NAME(mod));
1359 return NULL;
1360 }
1361
1362 if (name) {
1363 *name = ptr + 1;
1364 *name_len = nametest_len - ((ptr - nametest) + 1);
1365 }
1366 } else {
1367 target_mod = mod->mod;
1368 if (name) {
1369 *name = nametest;
1370 *name_len = nametest_len;
1371 }
1372 }
1373
1374 return target_mod;
1375}
1376
1377/**
1378 * @brief Check whether a parsed node matches a single schema nodeid name test.
1379 *
1380 * @param[in] pnode Parsed node to consider.
1381 * @param[in] pnode_mod Compiled @p pnode to-be module.
1382 * @param[in] mod Expected module.
1383 * @param[in] name Expected name.
1384 * @param[in] name_len Length of @p name.
1385 * @return Whether it is a match or not.
1386 */
1387static ly_bool
1388lysp_schema_nodeid_match_pnode(const struct lysp_node *pnode, const struct lys_module *pnode_mod,
1389 const struct lys_module *mod, const char *name, size_t name_len)
1390{
1391 const char *pname;
1392
1393 /* compare with the module of the node */
1394 if (pnode_mod != mod) {
1395 return 0;
1396 }
1397
1398 /* compare names */
1399 if (pnode->nodetype & (LYS_ACTION | LYS_RPC)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001400 pname = ((struct lysp_node_action *)pnode)->name;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001401 } else if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1402 pname = (pnode->nodetype & LYS_INPUT) ? "input" : "output";
1403 } else {
1404 pname = pnode->name;
1405 }
1406 if (ly_strncmp(pname, name, name_len)) {
1407 return 0;
1408 }
1409
1410 return 1;
1411}
1412
1413/**
1414 * @brief Check whether a compiled node matches a single schema nodeid name test.
1415 *
1416 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1417 * @param[in] mod Expected module.
1418 * @param[in] name Expected name.
1419 * @param[in] name_len Length of @p name.
1420 * @return Whether it is a match or not.
1421 */
1422static ly_bool
1423lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1424 size_t name_len)
1425{
1426 const struct lys_module *node_mod;
1427 const char *node_name;
1428
1429 /* compare with the module of the node */
Michal Vasko2a668712020-10-21 11:48:09 +02001430 if ((*node)->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
Michal Vasko208a04a2020-10-21 15:17:12 +02001431 node_mod = lysc_node_parent_full(*node)->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001432 } else {
1433 node_mod = (*node)->module;
1434 }
1435 if (node_mod != mod) {
1436 return 0;
1437 }
1438
1439 /* compare names */
1440 if ((*node)->nodetype == LYS_INPUT) {
1441 node_name = "input";
1442 } else if ((*node)->nodetype == LYS_OUTPUT) {
1443 node_name = "output";
1444 } else {
1445 node_name = (*node)->name;
1446 }
1447 if (ly_strncmp(node_name, name, name_len)) {
1448 return 0;
1449 }
1450
Michal Vasko2a668712020-10-21 11:48:09 +02001451 /* move to next parent */
Michal Vasko208a04a2020-10-21 15:17:12 +02001452 *node = lysc_node_parent_full(*node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001453
1454 return 1;
1455}
1456
1457/**
1458 * @brief Check whether a node matches specific schema nodeid.
1459 *
1460 * @param[in] exp Parsed nodeid to match.
1461 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1462 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1463 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1464 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1465 * @param[in] pnode_mod Compiled @p pnode to-be module.
1466 * @return Whether it is a match or not.
1467 */
1468static ly_bool
1469lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1470 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1471{
1472 uint32_t i;
1473 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001474 const char *name = NULL;
1475 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001476
1477 /* compare last node in the node ID */
1478 i = exp->used - 1;
1479
1480 /* get exp node ID module */
1481 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name, &name_len);
1482 assert(mod);
1483
1484 if (pnode) {
1485 /* compare on the last parsed-only node */
1486 if (!lysp_schema_nodeid_match_pnode(pnode, pnode_mod, mod, name, name_len)) {
1487 return 0;
1488 }
1489 } else {
1490 /* using parent directly */
1491 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1492 return 0;
1493 }
1494 }
1495
1496 /* now compare all the compiled parents */
1497 while (i > 1) {
1498 i -= 2;
1499 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1500
1501 if (!parent) {
1502 /* no more parents but path continues */
1503 return 0;
1504 }
1505
1506 /* get exp node ID module */
1507 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1508 &name_len);
1509 assert(mod);
1510
1511 /* compare with the parent */
1512 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1513 return 0;
1514 }
1515 }
1516
1517 if (ctx_node && (ctx_node != parent)) {
1518 /* descendant path has not finished in the context node */
1519 return 0;
1520 } else if (!ctx_node && parent) {
1521 /* some parent was not matched */
1522 return 0;
1523 }
1524
1525 return 1;
1526}
1527
1528void
1529lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1530{
1531 if (aug) {
1532 lyxp_expr_free(ctx, aug->nodeid);
1533
1534 free(aug);
1535 }
1536}
1537
1538void
1539lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1540{
1541 if (dev) {
1542 lyxp_expr_free(ctx, dev->nodeid);
1543 LY_ARRAY_FREE(dev->devs);
1544 LY_ARRAY_FREE(dev->dev_pmods);
1545
1546 free(dev);
1547 }
1548}
1549
1550void
1551lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1552{
1553 if (rfn) {
1554 lyxp_expr_free(ctx, rfn->nodeid);
1555 LY_ARRAY_FREE(rfn->rfns);
1556
1557 free(rfn);
1558 }
1559}
1560
1561void
1562lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1563{
1564 if (!dev_pnode) {
1565 return;
1566 }
1567
1568 switch (dev_pnode->nodetype) {
1569 case LYS_CONTAINER:
1570 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1571 break;
1572 case LYS_LIST:
1573 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1574 break;
1575 case LYS_CHOICE:
1576 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1577 break;
1578 case LYS_CASE:
1579 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1580 break;
1581 case LYS_LEAF:
1582 case LYS_LEAFLIST:
1583 case LYS_ANYXML:
1584 case LYS_ANYDATA:
1585 /* no children */
1586 break;
1587 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001588 ((struct lysp_node_notif *)dev_pnode)->data = NULL;
1589 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590 case LYS_RPC:
1591 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001592 ((struct lysp_node_action *)dev_pnode)->input.data = NULL;
1593 ((struct lysp_node_action *)dev_pnode)->output.data = NULL;
1594 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001595 case LYS_INPUT:
1596 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001597 ((struct lysp_node_action_inout *)dev_pnode)->data = NULL;
1598 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001599 free(dev_pnode);
1600 return;
1601 default:
1602 LOGINT(ctx);
1603 return;
1604 }
1605
1606 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1607}
1608
1609LY_ERR
1610lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1611 struct lysp_node **dev_pnode, ly_bool *not_supported)
1612{
1613 LY_ERR ret = LY_SUCCESS;
1614 uint32_t i;
1615 LY_ARRAY_COUNT_TYPE u;
1616 struct lys_module *orig_mod = ctx->cur_mod;
1617 struct lysp_module *orig_pmod = ctx->pmod;
1618 char orig_path[LYSC_CTX_BUFSIZE];
1619 struct lysc_refine *rfn;
1620 struct lysc_deviation *dev;
1621 struct lysp_deviation *dev_p;
1622 struct lysp_deviate *d;
1623
1624 *dev_pnode = NULL;
1625 *not_supported = 0;
1626
1627 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1628 rfn = ctx->uses_rfns.objs[i];
1629
1630 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) {
1631 /* not our target node */
1632 continue;
1633 }
1634
1635 if (!*dev_pnode) {
1636 /* first refine on this node, create a copy first */
1637 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1638 }
1639
Michal Vaskob8df5762021-01-12 15:15:53 +01001640 /* use modules from the refine */
1641 ctx->cur_mod = rfn->nodeid_pmod->mod;
1642 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1643
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001644 /* apply all the refines by changing (the copy of) the parsed node */
1645 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001646 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001647 lysc_update_path(ctx, NULL, "{refine}");
1648 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001649
1650 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001651 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1652 lysc_update_path(ctx, NULL, NULL);
1653 lysc_update_path(ctx, NULL, NULL);
1654 LY_CHECK_GOTO(ret, cleanup);
1655 }
1656
1657 /* refine was applied, remove it */
1658 lysc_refine_free(ctx->ctx, rfn);
1659 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1660
1661 /* all the refines for one target node are in one structure, we are done */
1662 break;
1663 }
1664
1665 for (i = 0; i < ctx->devs.count; ++i) {
1666 dev = ctx->devs.objs[i];
1667
1668 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) {
1669 /* not our target node */
1670 continue;
1671 }
1672
1673 if (dev->not_supported) {
1674 /* it is not supported, no more deviations */
1675 *not_supported = 1;
1676 goto dev_applied;
1677 }
1678
1679 if (!*dev_pnode) {
1680 /* first deviation on this node, create a copy first */
1681 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1682 }
1683
1684 /* apply all the deviates by changing (the copy of) the parsed node */
1685 LY_ARRAY_FOR(dev->devs, u) {
1686 dev_p = dev->devs[u];
1687 LY_LIST_FOR(dev_p->deviates, d) {
1688 /* generate correct path */
1689 strcpy(orig_path, ctx->path);
1690 ctx->path_len = 1;
1691 ctx->cur_mod = dev->dev_pmods[u]->mod;
1692 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1693 lysc_update_path(ctx, NULL, "{deviation}");
1694 lysc_update_path(ctx, NULL, dev_p->nodeid);
1695
1696 switch (d->mod) {
1697 case LYS_DEV_ADD:
1698 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1699 break;
1700 case LYS_DEV_DELETE:
1701 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1702 break;
1703 case LYS_DEV_REPLACE:
1704 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1705 break;
1706 default:
1707 LOGINT(ctx->ctx);
1708 ret = LY_EINT;
1709 }
1710
1711 /* restore previous path */
1712 strcpy(ctx->path, orig_path);
1713 ctx->path_len = strlen(ctx->path);
1714 ctx->cur_mod = orig_mod;
1715 ctx->pmod = orig_pmod;
1716
1717 LY_CHECK_GOTO(ret, cleanup);
1718 }
1719 }
1720
1721dev_applied:
1722 /* deviation was applied, remove it */
1723 lysc_deviation_free(ctx->ctx, dev);
1724 ly_set_rm_index(&ctx->devs, i, NULL);
1725
1726 /* all the deviations for one target node are in one structure, we are done */
1727 break;
1728 }
1729
1730cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001731 ctx->cur_mod = orig_mod;
1732 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001733 if (ret) {
1734 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1735 *dev_pnode = NULL;
1736 *not_supported = 0;
1737 }
1738 return ret;
1739}
1740
1741/**
1742 * @brief Compile the parsed augment connecting it into its target.
1743 *
1744 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1745 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1746 * are already implemented and compiled.
1747 *
1748 * @param[in] ctx Compile context.
1749 * @param[in] aug_p Parsed augment to compile.
1750 * @param[in] target Target node of the augment.
1751 * @return LY_SUCCESS on success.
1752 * @return LY_EVALID on failure.
1753 */
1754static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001755lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001756{
1757 LY_ERR ret = LY_SUCCESS;
1758 struct lysp_node *pnode;
1759 struct lysc_node *node;
1760 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001761 struct lysc_node_action **actions;
1762 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001763 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001764 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001765 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001766
1767 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001768 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001769 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1770 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1771 ret = LY_EVALID;
1772 goto cleanup;
1773 }
1774
1775 /* check for mandatory nodes
1776 * - new cases augmenting some choice can have mandatory nodes
1777 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1778 */
1779 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1780 allow_mandatory = 1;
1781 }
1782
1783 LY_LIST_FOR(aug_p->child, pnode) {
1784 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1785 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1786 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1787 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001788 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001789 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1790 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1791 ret = LY_EVALID;
1792 goto cleanup;
1793 }
1794
1795 /* compile the children */
1796 if (target->nodetype == LYS_CHOICE) {
1797 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001798 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1799 if (target->nodetype == LYS_INPUT) {
1800 ctx->options |= LYS_COMPILE_RPC_INPUT;
1801 } else {
1802 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1803 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001804 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001805 } else {
1806 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1807 }
1808
Michal Vasko29dd11e2020-11-23 16:52:22 +01001809 /* eval if-features again for the rest of this node processing */
1810 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1811 if (!enabled) {
1812 ctx->options |= LYS_COMPILE_DISABLED;
1813 }
1814
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001815 /* since the augment node is not present in the compiled tree, we need to pass some of its
1816 * statements to all its children */
1817 for (i = 0; i < child_set.count; ++i) {
1818 node = child_set.snodes[i];
1819 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1820 node->flags &= ~LYS_MAND_TRUE;
1821 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001822 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1824 ret = LY_EVALID;
1825 goto cleanup;
1826 }
1827
1828 if (aug_p->when) {
1829 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001830 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001831 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod9062b02020-11-04 17:15:50 +01001832
1833 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
1834 /* container with a when condition */
1835 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its "
1836 "inherited \"when\" condition.", node->name);
1837 node->flags |= LYS_PRESENCE;
1838 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001839 }
1840 }
1841 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001842
1843 /* restore options */
1844 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001845 }
1846
1847 switch (target->nodetype) {
1848 case LYS_CONTAINER:
1849 actions = &((struct lysc_node_container *)target)->actions;
1850 notifs = &((struct lysc_node_container *)target)->notifs;
1851 break;
1852 case LYS_LIST:
1853 actions = &((struct lysc_node_list *)target)->actions;
1854 notifs = &((struct lysc_node_list *)target)->notifs;
1855 break;
1856 default:
1857 actions = NULL;
1858 notifs = NULL;
1859 break;
1860 }
1861
1862 if (aug_p->actions) {
1863 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001864 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001865 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001866 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001867 ret = LY_EVALID;
1868 goto cleanup;
1869 }
1870
1871 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001872 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
1873 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, NULL), cleanup);
1874 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001875
1876 if (aug_p->when) {
1877 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001878 struct lysc_node *iter;
1879
1880 LY_LIST_FOR((struct lysc_node *)*actions, iter) {
1881 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), iter, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001882 LY_CHECK_GOTO(ret, cleanup);
1883 }
1884 }
1885 }
1886 if (aug_p->notifs) {
1887 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001888 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001889 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001890 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001891 ret = LY_EVALID;
1892 goto cleanup;
1893 }
1894
1895 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001896 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
1897 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, NULL), cleanup);
1898 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001899
1900 if (aug_p->when) {
1901 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001902 struct lysc_node *iter;
1903
1904 LY_LIST_FOR((struct lysc_node *)*notifs, iter) {
1905 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), iter, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001906 LY_CHECK_GOTO(ret, cleanup);
1907 }
1908 }
1909 }
1910
1911cleanup:
1912 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001913 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001914 return ret;
1915}
1916
1917LY_ERR
1918lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1919{
1920 LY_ERR ret = LY_SUCCESS;
1921 struct lys_module *orig_mod = ctx->cur_mod;
1922 struct lysp_module *orig_pmod = ctx->pmod;
1923 uint32_t i;
1924 char orig_path[LYSC_CTX_BUFSIZE];
1925 struct lysc_augment *aug;
1926
1927 /* uses augments */
1928 for (i = 0; i < ctx->uses_augs.count; ) {
1929 aug = ctx->uses_augs.objs[i];
1930
1931 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, aug->nodeid_ctx_node, node, NULL, NULL)) {
1932 /* not our target node */
1933 ++i;
1934 continue;
1935 }
1936
Michal Vaskob8df5762021-01-12 15:15:53 +01001937 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001938 lysc_update_path(ctx, NULL, "{augment}");
1939 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001940 ctx->cur_mod = aug->nodeid_pmod->mod;
1941 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
1942
1943 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001944 ret = lys_compile_augment(ctx, aug->aug_p, node);
1945 lysc_update_path(ctx, NULL, NULL);
1946 lysc_update_path(ctx, NULL, NULL);
1947 LY_CHECK_GOTO(ret, cleanup);
1948
1949 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1950 ly_set_rm(&ctx->uses_augs, aug, NULL);
1951 lysc_augment_free(ctx->ctx, aug);
1952 }
1953
1954 /* top-level augments */
1955 for (i = 0; i < ctx->augs.count; ) {
1956 aug = ctx->augs.objs[i];
1957
1958 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, NULL, node, NULL, NULL)) {
1959 /* not our target node */
1960 ++i;
1961 continue;
1962 }
1963
Michal Vaskob8df5762021-01-12 15:15:53 +01001964 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001965 strcpy(orig_path, ctx->path);
1966 ctx->path_len = 1;
1967 lysc_update_path(ctx, NULL, "{augment}");
1968 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1969 ctx->cur_mod = aug->nodeid_pmod->mod;
1970 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001971
1972 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001973 ret = lys_compile_augment(ctx, aug->aug_p, node);
1974 strcpy(ctx->path, orig_path);
1975 ctx->path_len = strlen(ctx->path);
1976 LY_CHECK_GOTO(ret, cleanup);
1977
1978 /* augment was applied, remove it */
1979 ly_set_rm(&ctx->augs, aug, NULL);
1980 lysc_augment_free(ctx->ctx, aug);
1981 }
1982
1983cleanup:
1984 ctx->cur_mod = orig_mod;
1985 ctx->pmod = orig_pmod;
1986 return ret;
1987}
1988
1989/**
1990 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1991 *
1992 * @param[in] ctx Compile context.
1993 * @param[in] aug_p Parsed augment to be applied.
1994 * @param[in] pmod Both current and prefix module for @p aug_p.
1995 * @return LY_ERR value.
1996 */
1997static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001998lys_precompile_own_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, const struct lysp_module *pmod)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001999{
2000 LY_ERR ret = LY_SUCCESS;
2001 struct lyxp_expr *exp = NULL;
2002 struct lysc_augment *aug;
2003 const struct lys_module *mod;
2004
2005 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2006 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
2007 LY_CHECK_GOTO(ret, cleanup);
2008
2009 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2010 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2011 if (mod != ctx->cur_mod) {
2012 /* augment for another module, ignore */
2013 goto cleanup;
2014 }
2015
2016 /* allocate new compiled augment and store it in the set */
2017 aug = calloc(1, sizeof *aug);
2018 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2019 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
2020
2021 aug->nodeid = exp;
2022 exp = NULL;
2023 aug->nodeid_pmod = pmod;
2024 aug->aug_p = aug_p;
2025
2026cleanup:
2027 lyxp_expr_free(ctx->ctx, exp);
2028 return ret;
2029}
2030
2031LY_ERR
2032lys_precompile_own_augments(struct lysc_ctx *ctx)
2033{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002034 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002035
2036 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002037 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
2038 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002039
2040 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002041 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
2042 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002043 }
2044
2045 /* collect all submodules augments */
2046 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002047 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
2048 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, (struct lysp_module *)aug_mod->parsed->includes[v].submodule));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002049 }
2050 }
2051 }
2052
2053 return LY_SUCCESS;
2054}
2055
2056/**
2057 * @brief Prepare a deviation to be applied during data nodes compilation.
2058 *
2059 * @param[in] ctx Compile context.
2060 * @param[in] dev_p Parsed deviation to be applied.
2061 * @param[in] pmod Both current and prefix module for @p dev_p.
2062 * @return LY_ERR value.
2063 */
2064static LY_ERR
2065lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2066{
2067 LY_ERR ret = LY_SUCCESS;
2068 struct lysc_deviation *dev = NULL;
2069 struct lyxp_expr *exp = NULL;
2070 struct lysp_deviation **new_dev;
2071 const struct lys_module *mod;
2072 const struct lysp_module **new_dev_pmod;
2073 uint32_t i;
2074
2075 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2076 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2077 LY_CHECK_GOTO(ret, cleanup);
2078
2079 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2080 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2081 if (mod != ctx->cur_mod) {
2082 /* deviation for another module, ignore */
2083 goto cleanup;
2084 }
2085
2086 /* try to find the node in already compiled deviations */
2087 for (i = 0; i < ctx->devs.count; ++i) {
2088 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2089 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2090 dev = ctx->devs.objs[i];
2091 break;
2092 }
2093 }
2094
2095 if (!dev) {
2096 /* allocate new compiled deviation */
2097 dev = calloc(1, sizeof *dev);
2098 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2099 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2100
2101 dev->nodeid = exp;
2102 exp = NULL;
2103 }
2104
2105 /* add new parsed deviation structure */
2106 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2107 *new_dev = dev_p;
2108 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2109 *new_dev_pmod = pmod;
2110
2111cleanup:
2112 lyxp_expr_free(ctx->ctx, exp);
2113 return ret;
2114}
2115
2116LY_ERR
2117lys_precompile_own_deviations(struct lysc_ctx *ctx)
2118{
2119 LY_ARRAY_COUNT_TYPE u, v, w;
2120 const struct lys_module *dev_mod;
2121 struct lysc_deviation *dev;
2122 struct lysp_deviate *d;
2123 int not_supported;
2124 uint32_t i;
2125
2126 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2127 dev_mod = ctx->cur_mod->deviated_by[u];
2128
2129 /* compile all module deviations */
2130 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2131 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2132 }
2133
2134 /* compile all submodules deviations */
2135 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2136 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2137 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2138 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2139 }
2140 }
2141 }
2142
2143 /* set not-supported flags for all the deviations */
2144 for (i = 0; i < ctx->devs.count; ++i) {
2145 dev = ctx->devs.objs[i];
2146 not_supported = 0;
2147
2148 LY_ARRAY_FOR(dev->devs, u) {
2149 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2150 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2151 not_supported = 1;
2152 break;
2153 }
2154 }
2155 if (not_supported) {
2156 break;
2157 }
2158 }
2159 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002160 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002161 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2162 return LY_EVALID;
2163 }
2164
2165 dev->not_supported = not_supported;
2166 }
2167
2168 return LY_SUCCESS;
2169}
2170
2171/**
2172 * @brief Add a module reference into an array, checks for duplicities.
2173 *
2174 * @param[in] ctx Compile context.
2175 * @param[in] mod Module reference to add.
2176 * @param[in,out] mod_array Module sized array to add to.
2177 * @return LY_ERR value.
2178 */
2179static LY_ERR
2180lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2181{
2182 LY_ARRAY_COUNT_TYPE u;
2183 struct lys_module **new_mod;
2184
2185 LY_ARRAY_FOR(*mod_array, u) {
2186 if ((*mod_array)[u] == mod) {
2187 /* already there */
2188 return LY_EEXIST;
2189 }
2190 }
2191
2192 /* add the new module ref */
2193 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2194 *new_mod = mod;
2195
2196 return LY_SUCCESS;
2197}
2198
2199LY_ERR
2200lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2201{
Michal Vasko405cc9e2020-12-01 12:01:27 +01002202 LY_ERR ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002203 LY_ARRAY_COUNT_TYPE u, v;
2204 const struct lysp_module *mod_p;
2205 const struct lysc_node *target;
2206 struct lys_module *mod;
2207 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002208 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209 ly_bool has_dev = 0;
2210 uint16_t flags;
2211 uint32_t idx, opt_prev = ctx->options;
2212
Michal Vasko405cc9e2020-12-01 12:01:27 +01002213 for (idx = 0; idx < ctx->unres->implementing.count; ++idx) {
2214 if (ctx->cur_mod == ctx->unres->implementing.objs[idx]) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002215 break;
2216 }
2217 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01002218 if (idx == ctx->unres->implementing.count) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002219 /* it was already implemented and all the augments and deviations fully applied */
2220 return LY_SUCCESS;
2221 }
2222
2223 mod_p = ctx->cur_mod->parsed;
2224
Radek Krejci2a9fc652021-01-22 17:44:34 +01002225 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002226 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002227 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002228
2229 /* get target module */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002230 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002231 LY_CHECK_RET(ret);
2232
2233 /* add this module into the target module augmented_by, if not there already from previous augments */
2234 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2235
2236 /* if we are compiling this module, we cannot add augments to it yet */
2237 if (mod != ctx->cur_mod) {
2238 /* apply the augment, find the target node first */
2239 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002240 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002241 (void *)mod_p, 0, &target, &flags);
2242 LY_CHECK_RET(ret);
2243
2244 /* apply the augment */
2245 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002246 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002247 ctx->options = opt_prev;
2248 LY_CHECK_RET(ret);
2249 }
2250
2251 lysc_update_path(ctx, NULL, NULL);
2252 lysc_update_path(ctx, NULL, NULL);
2253 }
2254
2255 LY_ARRAY_FOR(mod_p->deviations, u) {
2256 /* get target module */
2257 lysc_update_path(ctx, NULL, "{deviation}");
2258 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
2259 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
2260 lysc_update_path(ctx, NULL, NULL);
2261 lysc_update_path(ctx, NULL, NULL);
2262 LY_CHECK_RET(ret);
2263
2264 /* add this module into the target module deviated_by, if not there already from previous deviations */
2265 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2266
2267 /* new deviation added to the target module */
2268 has_dev = 1;
2269 }
2270
2271 /* the same for augments and deviations in submodules */
2272 LY_ARRAY_FOR(mod_p->includes, v) {
2273 submod = mod_p->includes[v].submodule;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002274 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002275 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002276 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002277
Radek Krejci2a9fc652021-01-22 17:44:34 +01002278 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002279 LY_CHECK_RET(ret);
2280
2281 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2282 if (mod != ctx->cur_mod) {
2283 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002284 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002285 submod, 0, &target, &flags);
2286 LY_CHECK_RET(ret);
2287
2288 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002289 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002290 ctx->options = opt_prev;
2291 LY_CHECK_RET(ret);
2292 }
2293
2294 lysc_update_path(ctx, NULL, NULL);
2295 lysc_update_path(ctx, NULL, NULL);
2296 }
2297
2298 LY_ARRAY_FOR(submod->deviations, u) {
2299 lysc_update_path(ctx, NULL, "{deviation}");
2300 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
2301 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
2302 lysc_update_path(ctx, NULL, NULL);
2303 lysc_update_path(ctx, NULL, NULL);
2304 LY_CHECK_RET(ret);
2305
2306 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2307 has_dev = 1;
2308 }
2309 }
2310
Michal Vasko405cc9e2020-12-01 12:01:27 +01002311 if (has_dev) {
2312 /* all modules (may) need to be recompiled */
2313 ctx->unres->recompile = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002314 }
2315
Michal Vasko405cc9e2020-12-01 12:01:27 +01002316 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002317}
2318
2319void
2320lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2321{
2322 uint32_t i;
2323 LY_ARRAY_COUNT_TYPE u, count;
2324 struct lys_module *m;
2325
2326 for (i = 0; i < ctx->list.count; ++i) {
2327 m = ctx->list.objs[i];
2328
2329 if (m->augmented_by) {
2330 count = LY_ARRAY_COUNT(m->augmented_by);
2331 for (u = 0; u < count; ++u) {
2332 if (m->augmented_by[u] == mod) {
2333 /* keep the order */
2334 if (u < count - 1) {
2335 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2336 }
2337 LY_ARRAY_DECREMENT(m->augmented_by);
2338 break;
2339 }
2340 }
2341 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2342 LY_ARRAY_FREE(m->augmented_by);
2343 m->augmented_by = NULL;
2344 }
2345 }
2346
2347 if (m->deviated_by) {
2348 count = LY_ARRAY_COUNT(m->deviated_by);
2349 for (u = 0; u < count; ++u) {
2350 if (m->deviated_by[u] == mod) {
2351 /* keep the order */
2352 if (u < count - 1) {
2353 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2354 }
2355 LY_ARRAY_DECREMENT(m->deviated_by);
2356 break;
2357 }
2358 }
2359 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2360 LY_ARRAY_FREE(m->deviated_by);
2361 m->deviated_by = NULL;
2362 }
2363 }
2364 }
2365}