blob: 42ca4e7b06fee10033a21cdaaadfadb45d113340 [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{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001426 const char *node_name;
1427
1428 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001429 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001430 return 0;
1431 }
1432
1433 /* compare names */
1434 if ((*node)->nodetype == LYS_INPUT) {
1435 node_name = "input";
1436 } else if ((*node)->nodetype == LYS_OUTPUT) {
1437 node_name = "output";
1438 } else {
1439 node_name = (*node)->name;
1440 }
1441 if (ly_strncmp(node_name, name, name_len)) {
1442 return 0;
1443 }
1444
Michal Vasko2a668712020-10-21 11:48:09 +02001445 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001446 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001447
1448 return 1;
1449}
1450
1451/**
1452 * @brief Check whether a node matches specific schema nodeid.
1453 *
1454 * @param[in] exp Parsed nodeid to match.
1455 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1456 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1457 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1458 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1459 * @param[in] pnode_mod Compiled @p pnode to-be module.
1460 * @return Whether it is a match or not.
1461 */
1462static ly_bool
1463lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1464 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1465{
1466 uint32_t i;
1467 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001468 const char *name = NULL;
1469 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001470
1471 /* compare last node in the node ID */
1472 i = exp->used - 1;
1473
1474 /* get exp node ID module */
1475 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);
1476 assert(mod);
1477
1478 if (pnode) {
1479 /* compare on the last parsed-only node */
1480 if (!lysp_schema_nodeid_match_pnode(pnode, pnode_mod, mod, name, name_len)) {
1481 return 0;
1482 }
1483 } else {
1484 /* using parent directly */
1485 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1486 return 0;
1487 }
1488 }
1489
1490 /* now compare all the compiled parents */
1491 while (i > 1) {
1492 i -= 2;
1493 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1494
1495 if (!parent) {
1496 /* no more parents but path continues */
1497 return 0;
1498 }
1499
1500 /* get exp node ID module */
1501 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1502 &name_len);
1503 assert(mod);
1504
1505 /* compare with the parent */
1506 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1507 return 0;
1508 }
1509 }
1510
1511 if (ctx_node && (ctx_node != parent)) {
1512 /* descendant path has not finished in the context node */
1513 return 0;
1514 } else if (!ctx_node && parent) {
1515 /* some parent was not matched */
1516 return 0;
1517 }
1518
1519 return 1;
1520}
1521
1522void
1523lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1524{
1525 if (aug) {
1526 lyxp_expr_free(ctx, aug->nodeid);
1527
1528 free(aug);
1529 }
1530}
1531
1532void
1533lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1534{
1535 if (dev) {
1536 lyxp_expr_free(ctx, dev->nodeid);
1537 LY_ARRAY_FREE(dev->devs);
1538 LY_ARRAY_FREE(dev->dev_pmods);
1539
1540 free(dev);
1541 }
1542}
1543
1544void
1545lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1546{
1547 if (rfn) {
1548 lyxp_expr_free(ctx, rfn->nodeid);
1549 LY_ARRAY_FREE(rfn->rfns);
1550
1551 free(rfn);
1552 }
1553}
1554
1555void
1556lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1557{
1558 if (!dev_pnode) {
1559 return;
1560 }
1561
1562 switch (dev_pnode->nodetype) {
1563 case LYS_CONTAINER:
1564 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1565 break;
1566 case LYS_LIST:
1567 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1568 break;
1569 case LYS_CHOICE:
1570 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1571 break;
1572 case LYS_CASE:
1573 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1574 break;
1575 case LYS_LEAF:
1576 case LYS_LEAFLIST:
1577 case LYS_ANYXML:
1578 case LYS_ANYDATA:
1579 /* no children */
1580 break;
1581 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001582 ((struct lysp_node_notif *)dev_pnode)->data = NULL;
1583 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001584 case LYS_RPC:
1585 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001586 ((struct lysp_node_action *)dev_pnode)->input.data = NULL;
1587 ((struct lysp_node_action *)dev_pnode)->output.data = NULL;
1588 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001589 case LYS_INPUT:
1590 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +01001591 ((struct lysp_node_action_inout *)dev_pnode)->data = NULL;
1592 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001593 free(dev_pnode);
1594 return;
1595 default:
1596 LOGINT(ctx);
1597 return;
1598 }
1599
1600 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1601}
1602
1603LY_ERR
1604lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1605 struct lysp_node **dev_pnode, ly_bool *not_supported)
1606{
1607 LY_ERR ret = LY_SUCCESS;
1608 uint32_t i;
1609 LY_ARRAY_COUNT_TYPE u;
1610 struct lys_module *orig_mod = ctx->cur_mod;
1611 struct lysp_module *orig_pmod = ctx->pmod;
1612 char orig_path[LYSC_CTX_BUFSIZE];
1613 struct lysc_refine *rfn;
1614 struct lysc_deviation *dev;
1615 struct lysp_deviation *dev_p;
1616 struct lysp_deviate *d;
1617
1618 *dev_pnode = NULL;
1619 *not_supported = 0;
1620
1621 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1622 rfn = ctx->uses_rfns.objs[i];
1623
1624 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) {
1625 /* not our target node */
1626 continue;
1627 }
1628
1629 if (!*dev_pnode) {
1630 /* first refine on this node, create a copy first */
1631 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1632 }
1633
Michal Vaskob8df5762021-01-12 15:15:53 +01001634 /* use modules from the refine */
1635 ctx->cur_mod = rfn->nodeid_pmod->mod;
1636 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1637
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001638 /* apply all the refines by changing (the copy of) the parsed node */
1639 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001640 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001641 lysc_update_path(ctx, NULL, "{refine}");
1642 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001643
1644 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001645 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1646 lysc_update_path(ctx, NULL, NULL);
1647 lysc_update_path(ctx, NULL, NULL);
1648 LY_CHECK_GOTO(ret, cleanup);
1649 }
1650
1651 /* refine was applied, remove it */
1652 lysc_refine_free(ctx->ctx, rfn);
1653 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1654
1655 /* all the refines for one target node are in one structure, we are done */
1656 break;
1657 }
1658
1659 for (i = 0; i < ctx->devs.count; ++i) {
1660 dev = ctx->devs.objs[i];
1661
1662 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) {
1663 /* not our target node */
1664 continue;
1665 }
1666
1667 if (dev->not_supported) {
1668 /* it is not supported, no more deviations */
1669 *not_supported = 1;
1670 goto dev_applied;
1671 }
1672
1673 if (!*dev_pnode) {
1674 /* first deviation on this node, create a copy first */
1675 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1676 }
1677
1678 /* apply all the deviates by changing (the copy of) the parsed node */
1679 LY_ARRAY_FOR(dev->devs, u) {
1680 dev_p = dev->devs[u];
1681 LY_LIST_FOR(dev_p->deviates, d) {
1682 /* generate correct path */
1683 strcpy(orig_path, ctx->path);
1684 ctx->path_len = 1;
1685 ctx->cur_mod = dev->dev_pmods[u]->mod;
1686 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1687 lysc_update_path(ctx, NULL, "{deviation}");
1688 lysc_update_path(ctx, NULL, dev_p->nodeid);
1689
1690 switch (d->mod) {
1691 case LYS_DEV_ADD:
1692 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1693 break;
1694 case LYS_DEV_DELETE:
1695 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1696 break;
1697 case LYS_DEV_REPLACE:
1698 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1699 break;
1700 default:
1701 LOGINT(ctx->ctx);
1702 ret = LY_EINT;
1703 }
1704
1705 /* restore previous path */
1706 strcpy(ctx->path, orig_path);
1707 ctx->path_len = strlen(ctx->path);
1708 ctx->cur_mod = orig_mod;
1709 ctx->pmod = orig_pmod;
1710
1711 LY_CHECK_GOTO(ret, cleanup);
1712 }
1713 }
1714
1715dev_applied:
1716 /* deviation was applied, remove it */
1717 lysc_deviation_free(ctx->ctx, dev);
1718 ly_set_rm_index(&ctx->devs, i, NULL);
1719
1720 /* all the deviations for one target node are in one structure, we are done */
1721 break;
1722 }
1723
1724cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001725 ctx->cur_mod = orig_mod;
1726 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001727 if (ret) {
1728 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1729 *dev_pnode = NULL;
1730 *not_supported = 0;
1731 }
1732 return ret;
1733}
1734
1735/**
1736 * @brief Compile the parsed augment connecting it into its target.
1737 *
1738 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1739 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1740 * are already implemented and compiled.
1741 *
1742 * @param[in] ctx Compile context.
1743 * @param[in] aug_p Parsed augment to compile.
1744 * @param[in] target Target node of the augment.
1745 * @return LY_SUCCESS on success.
1746 * @return LY_EVALID on failure.
1747 */
1748static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001749lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001750{
1751 LY_ERR ret = LY_SUCCESS;
1752 struct lysp_node *pnode;
1753 struct lysc_node *node;
1754 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001755 struct lysc_node_action **actions;
1756 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001757 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001758 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001759 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001760
1761 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001762 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001763 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1764 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1765 ret = LY_EVALID;
1766 goto cleanup;
1767 }
1768
1769 /* check for mandatory nodes
1770 * - new cases augmenting some choice can have mandatory nodes
1771 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1772 */
1773 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1774 allow_mandatory = 1;
1775 }
1776
1777 LY_LIST_FOR(aug_p->child, pnode) {
1778 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1779 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1780 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1781 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001782 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001783 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1784 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1785 ret = LY_EVALID;
1786 goto cleanup;
1787 }
1788
1789 /* compile the children */
1790 if (target->nodetype == LYS_CHOICE) {
1791 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001792 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1793 if (target->nodetype == LYS_INPUT) {
1794 ctx->options |= LYS_COMPILE_RPC_INPUT;
1795 } else {
1796 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1797 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001798 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001799 } else {
1800 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1801 }
1802
Michal Vasko29dd11e2020-11-23 16:52:22 +01001803 /* eval if-features again for the rest of this node processing */
1804 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1805 if (!enabled) {
1806 ctx->options |= LYS_COMPILE_DISABLED;
1807 }
1808
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001809 /* since the augment node is not present in the compiled tree, we need to pass some of its
1810 * statements to all its children */
1811 for (i = 0; i < child_set.count; ++i) {
1812 node = child_set.snodes[i];
1813 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1814 node->flags &= ~LYS_MAND_TRUE;
1815 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001816 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001817 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1818 ret = LY_EVALID;
1819 goto cleanup;
1820 }
1821
1822 if (aug_p->when) {
1823 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001824 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 +02001825 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod9062b02020-11-04 17:15:50 +01001826
1827 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
1828 /* container with a when condition */
1829 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its "
1830 "inherited \"when\" condition.", node->name);
1831 node->flags |= LYS_PRESENCE;
1832 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001833 }
1834 }
1835 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001836
1837 /* restore options */
1838 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001839 }
1840
1841 switch (target->nodetype) {
1842 case LYS_CONTAINER:
1843 actions = &((struct lysc_node_container *)target)->actions;
1844 notifs = &((struct lysc_node_container *)target)->notifs;
1845 break;
1846 case LYS_LIST:
1847 actions = &((struct lysc_node_list *)target)->actions;
1848 notifs = &((struct lysc_node_list *)target)->notifs;
1849 break;
1850 default:
1851 actions = NULL;
1852 notifs = NULL;
1853 break;
1854 }
1855
1856 if (aug_p->actions) {
1857 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001858 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001859 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001860 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001861 ret = LY_EVALID;
1862 goto cleanup;
1863 }
1864
1865 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001866 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
1867 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, NULL), cleanup);
1868 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001869
1870 if (aug_p->when) {
1871 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001872 struct lysc_node *iter;
1873
1874 LY_LIST_FOR((struct lysc_node *)*actions, iter) {
1875 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 +02001876 LY_CHECK_GOTO(ret, cleanup);
1877 }
1878 }
1879 }
1880 if (aug_p->notifs) {
1881 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001882 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001883 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001884 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001885 ret = LY_EVALID;
1886 goto cleanup;
1887 }
1888
1889 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001890 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
1891 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, NULL), cleanup);
1892 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001893
1894 if (aug_p->when) {
1895 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001896 struct lysc_node *iter;
1897
1898 LY_LIST_FOR((struct lysc_node *)*notifs, iter) {
1899 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 +02001900 LY_CHECK_GOTO(ret, cleanup);
1901 }
1902 }
1903 }
1904
1905cleanup:
1906 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001907 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001908 return ret;
1909}
1910
1911LY_ERR
1912lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1913{
1914 LY_ERR ret = LY_SUCCESS;
1915 struct lys_module *orig_mod = ctx->cur_mod;
1916 struct lysp_module *orig_pmod = ctx->pmod;
1917 uint32_t i;
1918 char orig_path[LYSC_CTX_BUFSIZE];
1919 struct lysc_augment *aug;
1920
1921 /* uses augments */
1922 for (i = 0; i < ctx->uses_augs.count; ) {
1923 aug = ctx->uses_augs.objs[i];
1924
1925 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, aug->nodeid_ctx_node, node, NULL, NULL)) {
1926 /* not our target node */
1927 ++i;
1928 continue;
1929 }
1930
Michal Vaskob8df5762021-01-12 15:15:53 +01001931 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001932 lysc_update_path(ctx, NULL, "{augment}");
1933 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001934 ctx->cur_mod = aug->nodeid_pmod->mod;
1935 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
1936
1937 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001938 ret = lys_compile_augment(ctx, aug->aug_p, node);
1939 lysc_update_path(ctx, NULL, NULL);
1940 lysc_update_path(ctx, NULL, NULL);
1941 LY_CHECK_GOTO(ret, cleanup);
1942
1943 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1944 ly_set_rm(&ctx->uses_augs, aug, NULL);
1945 lysc_augment_free(ctx->ctx, aug);
1946 }
1947
1948 /* top-level augments */
1949 for (i = 0; i < ctx->augs.count; ) {
1950 aug = ctx->augs.objs[i];
1951
1952 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, NULL, node, NULL, NULL)) {
1953 /* not our target node */
1954 ++i;
1955 continue;
1956 }
1957
Michal Vaskob8df5762021-01-12 15:15:53 +01001958 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001959 strcpy(orig_path, ctx->path);
1960 ctx->path_len = 1;
1961 lysc_update_path(ctx, NULL, "{augment}");
1962 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1963 ctx->cur_mod = aug->nodeid_pmod->mod;
1964 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001965
1966 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001967 ret = lys_compile_augment(ctx, aug->aug_p, node);
1968 strcpy(ctx->path, orig_path);
1969 ctx->path_len = strlen(ctx->path);
1970 LY_CHECK_GOTO(ret, cleanup);
1971
1972 /* augment was applied, remove it */
1973 ly_set_rm(&ctx->augs, aug, NULL);
1974 lysc_augment_free(ctx->ctx, aug);
1975 }
1976
1977cleanup:
1978 ctx->cur_mod = orig_mod;
1979 ctx->pmod = orig_pmod;
1980 return ret;
1981}
1982
1983/**
1984 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1985 *
1986 * @param[in] ctx Compile context.
1987 * @param[in] aug_p Parsed augment to be applied.
1988 * @param[in] pmod Both current and prefix module for @p aug_p.
1989 * @return LY_ERR value.
1990 */
1991static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001992lys_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 +02001993{
1994 LY_ERR ret = LY_SUCCESS;
1995 struct lyxp_expr *exp = NULL;
1996 struct lysc_augment *aug;
1997 const struct lys_module *mod;
1998
1999 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2000 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
2001 LY_CHECK_GOTO(ret, cleanup);
2002
2003 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2004 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2005 if (mod != ctx->cur_mod) {
2006 /* augment for another module, ignore */
2007 goto cleanup;
2008 }
2009
2010 /* allocate new compiled augment and store it in the set */
2011 aug = calloc(1, sizeof *aug);
2012 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2013 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
2014
2015 aug->nodeid = exp;
2016 exp = NULL;
2017 aug->nodeid_pmod = pmod;
2018 aug->aug_p = aug_p;
2019
2020cleanup:
2021 lyxp_expr_free(ctx->ctx, exp);
2022 return ret;
2023}
2024
2025LY_ERR
2026lys_precompile_own_augments(struct lysc_ctx *ctx)
2027{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002028 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002029
2030 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002031 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
2032 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002033
2034 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002035 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
2036 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002037 }
2038
2039 /* collect all submodules augments */
2040 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002041 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
2042 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 +02002043 }
2044 }
2045 }
2046
2047 return LY_SUCCESS;
2048}
2049
2050/**
2051 * @brief Prepare a deviation to be applied during data nodes compilation.
2052 *
2053 * @param[in] ctx Compile context.
2054 * @param[in] dev_p Parsed deviation to be applied.
2055 * @param[in] pmod Both current and prefix module for @p dev_p.
2056 * @return LY_ERR value.
2057 */
2058static LY_ERR
2059lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2060{
2061 LY_ERR ret = LY_SUCCESS;
2062 struct lysc_deviation *dev = NULL;
2063 struct lyxp_expr *exp = NULL;
2064 struct lysp_deviation **new_dev;
2065 const struct lys_module *mod;
2066 const struct lysp_module **new_dev_pmod;
2067 uint32_t i;
2068
2069 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2070 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2071 LY_CHECK_GOTO(ret, cleanup);
2072
2073 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2074 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2075 if (mod != ctx->cur_mod) {
2076 /* deviation for another module, ignore */
2077 goto cleanup;
2078 }
2079
2080 /* try to find the node in already compiled deviations */
2081 for (i = 0; i < ctx->devs.count; ++i) {
2082 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2083 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2084 dev = ctx->devs.objs[i];
2085 break;
2086 }
2087 }
2088
2089 if (!dev) {
2090 /* allocate new compiled deviation */
2091 dev = calloc(1, sizeof *dev);
2092 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2093 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2094
2095 dev->nodeid = exp;
2096 exp = NULL;
2097 }
2098
2099 /* add new parsed deviation structure */
2100 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2101 *new_dev = dev_p;
2102 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2103 *new_dev_pmod = pmod;
2104
2105cleanup:
2106 lyxp_expr_free(ctx->ctx, exp);
2107 return ret;
2108}
2109
2110LY_ERR
2111lys_precompile_own_deviations(struct lysc_ctx *ctx)
2112{
2113 LY_ARRAY_COUNT_TYPE u, v, w;
2114 const struct lys_module *dev_mod;
2115 struct lysc_deviation *dev;
2116 struct lysp_deviate *d;
2117 int not_supported;
2118 uint32_t i;
2119
2120 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2121 dev_mod = ctx->cur_mod->deviated_by[u];
2122
2123 /* compile all module deviations */
2124 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2125 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2126 }
2127
2128 /* compile all submodules deviations */
2129 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2130 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2131 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2132 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2133 }
2134 }
2135 }
2136
2137 /* set not-supported flags for all the deviations */
2138 for (i = 0; i < ctx->devs.count; ++i) {
2139 dev = ctx->devs.objs[i];
2140 not_supported = 0;
2141
2142 LY_ARRAY_FOR(dev->devs, u) {
2143 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2144 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2145 not_supported = 1;
2146 break;
2147 }
2148 }
2149 if (not_supported) {
2150 break;
2151 }
2152 }
2153 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002154 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002155 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2156 return LY_EVALID;
2157 }
2158
2159 dev->not_supported = not_supported;
2160 }
2161
2162 return LY_SUCCESS;
2163}
2164
2165/**
2166 * @brief Add a module reference into an array, checks for duplicities.
2167 *
2168 * @param[in] ctx Compile context.
2169 * @param[in] mod Module reference to add.
2170 * @param[in,out] mod_array Module sized array to add to.
2171 * @return LY_ERR value.
2172 */
2173static LY_ERR
2174lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2175{
2176 LY_ARRAY_COUNT_TYPE u;
2177 struct lys_module **new_mod;
2178
2179 LY_ARRAY_FOR(*mod_array, u) {
2180 if ((*mod_array)[u] == mod) {
2181 /* already there */
2182 return LY_EEXIST;
2183 }
2184 }
2185
2186 /* add the new module ref */
2187 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2188 *new_mod = mod;
2189
2190 return LY_SUCCESS;
2191}
2192
2193LY_ERR
2194lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2195{
Michal Vasko405cc9e2020-12-01 12:01:27 +01002196 LY_ERR ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002197 LY_ARRAY_COUNT_TYPE u, v;
2198 const struct lysp_module *mod_p;
2199 const struct lysc_node *target;
2200 struct lys_module *mod;
2201 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002202 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002203 ly_bool has_dev = 0;
2204 uint16_t flags;
2205 uint32_t idx, opt_prev = ctx->options;
2206
Michal Vasko405cc9e2020-12-01 12:01:27 +01002207 for (idx = 0; idx < ctx->unres->implementing.count; ++idx) {
2208 if (ctx->cur_mod == ctx->unres->implementing.objs[idx]) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209 break;
2210 }
2211 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01002212 if (idx == ctx->unres->implementing.count) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002213 /* it was already implemented and all the augments and deviations fully applied */
2214 return LY_SUCCESS;
2215 }
2216
2217 mod_p = ctx->cur_mod->parsed;
2218
Radek Krejci2a9fc652021-01-22 17:44:34 +01002219 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002220 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002221 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002222
2223 /* get target module */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002224 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002225 LY_CHECK_RET(ret);
2226
2227 /* add this module into the target module augmented_by, if not there already from previous augments */
2228 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2229
2230 /* if we are compiling this module, we cannot add augments to it yet */
2231 if (mod != ctx->cur_mod) {
2232 /* apply the augment, find the target node first */
2233 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002234 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002235 (void *)mod_p, 0, &target, &flags);
2236 LY_CHECK_RET(ret);
2237
2238 /* apply the augment */
2239 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002240 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002241 ctx->options = opt_prev;
2242 LY_CHECK_RET(ret);
2243 }
2244
2245 lysc_update_path(ctx, NULL, NULL);
2246 lysc_update_path(ctx, NULL, NULL);
2247 }
2248
2249 LY_ARRAY_FOR(mod_p->deviations, u) {
2250 /* get target module */
2251 lysc_update_path(ctx, NULL, "{deviation}");
2252 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
2253 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
2254 lysc_update_path(ctx, NULL, NULL);
2255 lysc_update_path(ctx, NULL, NULL);
2256 LY_CHECK_RET(ret);
2257
2258 /* add this module into the target module deviated_by, if not there already from previous deviations */
2259 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2260
2261 /* new deviation added to the target module */
2262 has_dev = 1;
2263 }
2264
2265 /* the same for augments and deviations in submodules */
2266 LY_ARRAY_FOR(mod_p->includes, v) {
2267 submod = mod_p->includes[v].submodule;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002268 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002269 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002270 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002271
Radek Krejci2a9fc652021-01-22 17:44:34 +01002272 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002273 LY_CHECK_RET(ret);
2274
2275 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2276 if (mod != ctx->cur_mod) {
2277 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002278 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002279 submod, 0, &target, &flags);
2280 LY_CHECK_RET(ret);
2281
2282 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002283 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002284 ctx->options = opt_prev;
2285 LY_CHECK_RET(ret);
2286 }
2287
2288 lysc_update_path(ctx, NULL, NULL);
2289 lysc_update_path(ctx, NULL, NULL);
2290 }
2291
2292 LY_ARRAY_FOR(submod->deviations, u) {
2293 lysc_update_path(ctx, NULL, "{deviation}");
2294 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
2295 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
2296 lysc_update_path(ctx, NULL, NULL);
2297 lysc_update_path(ctx, NULL, NULL);
2298 LY_CHECK_RET(ret);
2299
2300 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2301 has_dev = 1;
2302 }
2303 }
2304
Michal Vasko405cc9e2020-12-01 12:01:27 +01002305 if (has_dev) {
2306 /* all modules (may) need to be recompiled */
2307 ctx->unres->recompile = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002308 }
2309
Michal Vasko405cc9e2020-12-01 12:01:27 +01002310 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002311}
2312
2313void
2314lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2315{
2316 uint32_t i;
2317 LY_ARRAY_COUNT_TYPE u, count;
2318 struct lys_module *m;
2319
2320 for (i = 0; i < ctx->list.count; ++i) {
2321 m = ctx->list.objs[i];
2322
2323 if (m->augmented_by) {
2324 count = LY_ARRAY_COUNT(m->augmented_by);
2325 for (u = 0; u < count; ++u) {
2326 if (m->augmented_by[u] == mod) {
2327 /* keep the order */
2328 if (u < count - 1) {
2329 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2330 }
2331 LY_ARRAY_DECREMENT(m->augmented_by);
2332 break;
2333 }
2334 }
2335 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2336 LY_ARRAY_FREE(m->augmented_by);
2337 m->augmented_by = NULL;
2338 }
2339 }
2340
2341 if (m->deviated_by) {
2342 count = LY_ARRAY_COUNT(m->deviated_by);
2343 for (u = 0; u < count; ++u) {
2344 if (m->deviated_by[u] == mod) {
2345 /* keep the order */
2346 if (u < count - 1) {
2347 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2348 }
2349 LY_ARRAY_DECREMENT(m->deviated_by);
2350 break;
2351 }
2352 }
2353 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2354 LY_ARRAY_FREE(m->deviated_by);
2355 m->deviated_by = NULL;
2356 }
2357 }
2358 }
2359}