blob: 336ea73da62216393d03c9108711cbf53158315b [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);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200389 DUP_ARRAY(ctx, orig->iffeatures, node->iffeatures, lysp_qname_dup);
390 DUP_ARRAY(ctx, orig->exts, node->exts, lysp_ext_dup);
391
392 return ret;
393}
394
Radek Krejci9a3823e2021-01-27 20:26:46 +0100395#define DUP_PWHEN(CTX, ORIG, NEW) \
396 if (ORIG) { \
397 NEW = calloc(1, sizeof *NEW); \
398 LY_CHECK_ERR_RET(!NEW, LOGMEM(CTX), LY_EMEM); \
399 LY_CHECK_RET(lysp_when_dup(CTX, NEW, ORIG)); \
400 }
401
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200402static 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
Radek Krejci9a3823e2021-01-27 20:26:46 +0100439 DUP_PWHEN(ctx, orig_cont->when, cont->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200440 DUP_ARRAY(ctx, orig_cont->musts, cont->musts, lysp_restr_dup);
441 DUP_STRING(ctx, orig_cont->presence, cont->presence, ret);
442 /* we do not need the rest */
443 break;
444 case LYS_LEAF:
445 leaf = (struct lysp_node_leaf *)node;
446 orig_leaf = (const struct lysp_node_leaf *)orig;
447
Radek Krejci9a3823e2021-01-27 20:26:46 +0100448 DUP_PWHEN(ctx, orig_leaf->when, leaf->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200449 DUP_ARRAY(ctx, orig_leaf->musts, leaf->musts, lysp_restr_dup);
450 LY_CHECK_RET(lysp_type_dup(ctx, &leaf->type, &orig_leaf->type));
451 DUP_STRING(ctx, orig_leaf->units, leaf->units, ret);
452 LY_CHECK_RET(lysp_qname_dup(ctx, &leaf->dflt, &orig_leaf->dflt));
453 break;
454 case LYS_LEAFLIST:
455 llist = (struct lysp_node_leaflist *)node;
456 orig_llist = (const struct lysp_node_leaflist *)orig;
457
Radek Krejci9a3823e2021-01-27 20:26:46 +0100458 DUP_PWHEN(ctx, orig_llist->when, llist->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200459 DUP_ARRAY(ctx, orig_llist->musts, llist->musts, lysp_restr_dup);
460 LY_CHECK_RET(lysp_type_dup(ctx, &llist->type, &orig_llist->type));
461 DUP_STRING(ctx, orig_llist->units, llist->units, ret);
462 DUP_ARRAY(ctx, orig_llist->dflts, llist->dflts, lysp_qname_dup);
463 llist->min = orig_llist->min;
464 llist->max = orig_llist->max;
465 break;
466 case LYS_LIST:
467 list = (struct lysp_node_list *)node;
468 orig_list = (const struct lysp_node_list *)orig;
469
Radek Krejci9a3823e2021-01-27 20:26:46 +0100470 DUP_PWHEN(ctx, orig_list->when, list->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200471 DUP_ARRAY(ctx, orig_list->musts, list->musts, lysp_restr_dup);
472 DUP_STRING(ctx, orig_list->key, list->key, ret);
473 /* we do not need these arrays */
474 DUP_ARRAY(ctx, orig_list->uniques, list->uniques, lysp_qname_dup);
475 list->min = orig_list->min;
476 list->max = orig_list->max;
477 break;
478 case LYS_CHOICE:
479 choice = (struct lysp_node_choice *)node;
480 orig_choice = (const struct lysp_node_choice *)orig;
481
Radek Krejci9a3823e2021-01-27 20:26:46 +0100482 DUP_PWHEN(ctx, orig_choice->when, choice->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200483 /* we do not need children */
484 LY_CHECK_RET(lysp_qname_dup(ctx, &choice->dflt, &orig_choice->dflt));
485 break;
486 case LYS_CASE:
487 cas = (struct lysp_node_case *)node;
488 orig_cas = (const struct lysp_node_case *)orig;
489
Radek Krejci9a3823e2021-01-27 20:26:46 +0100490 DUP_PWHEN(ctx, orig_cas->when, cas->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200491 /* we do not need children */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200492 break;
493 case LYS_ANYDATA:
494 case LYS_ANYXML:
495 any = (struct lysp_node_anydata *)node;
496 orig_any = (const struct lysp_node_anydata *)orig;
497
Radek Krejci9a3823e2021-01-27 20:26:46 +0100498 DUP_PWHEN(ctx, orig_any->when, any->when);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499 DUP_ARRAY(ctx, orig_any->musts, any->musts, lysp_restr_dup);
500 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100501 case LYS_RPC:
502 case LYS_ACTION:
503 action = (struct lysp_node_action *)node;
504 orig_action = (const struct lysp_node_action *)orig;
505
506 action->input.nodetype = orig_action->input.nodetype;
507 action->output.nodetype = orig_action->output.nodetype;
508 /* we do not need the rest */
509 break;
510 case LYS_INPUT:
511 case LYS_OUTPUT:
512 action_inout = (struct lysp_node_action_inout *)node;
513 orig_action_inout = (const struct lysp_node_action_inout *)orig;
514
515 DUP_ARRAY(ctx, orig_action_inout->musts, action_inout->musts, lysp_restr_dup);
516 /* we do not need the rest */
517 break;
518 case LYS_NOTIF:
519 notif = (struct lysp_node_notif *)node;
520 orig_notif = (const struct lysp_node_notif *)orig;
521
522 DUP_ARRAY(ctx, orig_notif->musts, notif->musts, lysp_restr_dup);
523 /* we do not need the rest */
524 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200525 default:
526 LOGINT_RET(ctx);
527 }
528
529 return ret;
530}
531
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200532/**
533 * @brief Duplicate a single parsed node. Only attributes that are used in compilation are copied.
534 *
535 * @param[in] ctx libyang context.
536 * @param[in] pnode Node to duplicate.
537 * @param[in] with_links Whether to also copy any links (child, parent pointers).
538 * @param[out] dup_p Duplicated parsed node.
539 * @return LY_ERR value.
540 */
541static LY_ERR
542lysp_dup_single(const struct ly_ctx *ctx, const struct lysp_node *pnode, ly_bool with_links, struct lysp_node **dup_p)
543{
544 LY_ERR ret = LY_SUCCESS;
545 void *mem = NULL;
546
547 if (!pnode) {
548 *dup_p = NULL;
549 return LY_SUCCESS;
550 }
551
552 switch (pnode->nodetype) {
553 case LYS_CONTAINER:
554 mem = calloc(1, sizeof(struct lysp_node_container));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200555 break;
556 case LYS_LEAF:
557 mem = calloc(1, sizeof(struct lysp_node_leaf));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200558 break;
559 case LYS_LEAFLIST:
560 mem = calloc(1, sizeof(struct lysp_node_leaflist));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200561 break;
562 case LYS_LIST:
563 mem = calloc(1, sizeof(struct lysp_node_list));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200564 break;
565 case LYS_CHOICE:
566 mem = calloc(1, sizeof(struct lysp_node_choice));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200567 break;
568 case LYS_CASE:
569 mem = calloc(1, sizeof(struct lysp_node_case));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200570 break;
571 case LYS_ANYDATA:
572 case LYS_ANYXML:
573 mem = calloc(1, sizeof(struct lysp_node_anydata));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 break;
575 case LYS_INPUT:
576 case LYS_OUTPUT:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100577 mem = calloc(1, sizeof(struct lysp_node_action_inout));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200578 break;
579 case LYS_ACTION:
580 case LYS_RPC:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100581 mem = calloc(1, sizeof(struct lysp_node_action));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200582 break;
583 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100584 mem = calloc(1, sizeof(struct lysp_node_notif));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200585 break;
586 default:
587 LOGINT_RET(ctx);
588 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100589 LY_CHECK_ERR_GOTO(!mem, LOGMEM(ctx); ret = LY_EMEM, cleanup);
590 LY_CHECK_GOTO(ret = lysp_node_dup(ctx, mem, pnode), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200591
592 if (with_links) {
593 /* copy also parent and child pointers */
594 ((struct lysp_node *)mem)->parent = pnode->parent;
595 switch (pnode->nodetype) {
596 case LYS_CONTAINER:
597 ((struct lysp_node_container *)mem)->child = ((struct lysp_node_container *)pnode)->child;
598 break;
599 case LYS_LIST:
600 ((struct lysp_node_list *)mem)->child = ((struct lysp_node_list *)pnode)->child;
601 break;
602 case LYS_CHOICE:
603 ((struct lysp_node_choice *)mem)->child = ((struct lysp_node_choice *)pnode)->child;
604 break;
605 case LYS_CASE:
606 ((struct lysp_node_case *)mem)->child = ((struct lysp_node_case *)pnode)->child;
607 break;
608 default:
609 break;
610 }
611 }
612
613cleanup:
614 if (ret) {
615 free(mem);
616 } else {
617 *dup_p = mem;
618 }
619 return ret;
620}
621
622#define AMEND_WRONG_NODETYPE(AMEND_STR, OP_STR, PROPERTY) \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 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 +0200624 AMEND_STR, lys_nodetype2str(target->nodetype), OP_STR, PROPERTY);\
625 ret = LY_EVALID; \
626 goto cleanup;
627
628#define AMEND_CHECK_CARDINALITY(ARRAY, MAX, AMEND_STR, PROPERTY) \
629 if (LY_ARRAY_COUNT(ARRAY) > MAX) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100630 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 +0200631 AMEND_STR, lys_nodetype2str(target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \
632 ret = LY_EVALID; \
633 goto cleanup; \
634 }
635
636/**
637 * @brief Apply refine.
638 *
639 * @param[in] ctx Compile context.
640 * @param[in] rfn Refine to apply.
641 * @param[in,out] target Refine target.
642 * @return LY_ERR value.
643 */
644static LY_ERR
645lys_apply_refine(struct lysc_ctx *ctx, struct lysp_refine *rfn, struct lysp_node *target)
646{
647 LY_ERR ret = LY_SUCCESS;
648 LY_ARRAY_COUNT_TYPE u;
649 struct lysp_qname *qname;
650 struct lysp_restr **musts, *must;
651 uint32_t *num;
652
653 /* default value */
654 if (rfn->dflts) {
655 switch (target->nodetype) {
656 case LYS_LEAF:
657 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
658
659 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
660 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &rfn->dflts[0]), cleanup);
661 break;
662 case LYS_LEAFLIST:
663 if (rfn->dflts[0].mod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100664 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200665 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
666 ret = LY_EVALID;
667 goto cleanup;
668 }
669
670 FREE_ARRAY(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, lysp_qname_free);
671 ((struct lysp_node_leaflist *)target)->dflts = NULL;
672 LY_ARRAY_FOR(rfn->dflts, u) {
673 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
674 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->dflts[u]), cleanup);
675 }
676 break;
677 case LYS_CHOICE:
678 AMEND_CHECK_CARDINALITY(rfn->dflts, 1, "refine", "default");
679
680 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
681 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &rfn->dflts[0]), cleanup);
682 break;
683 default:
684 AMEND_WRONG_NODETYPE("refine", "replace", "default");
685 }
686 }
687
688 /* description */
689 if (rfn->dsc) {
690 FREE_STRING(ctx->ctx, target->dsc);
691 DUP_STRING_GOTO(ctx->ctx, rfn->dsc, target->dsc, ret, cleanup);
692 }
693
694 /* reference */
695 if (rfn->ref) {
696 FREE_STRING(ctx->ctx, target->ref);
697 DUP_STRING_GOTO(ctx->ctx, rfn->ref, target->ref, ret, cleanup);
698 }
699
700 /* config */
701 if (rfn->flags & LYS_CONFIG_MASK) {
702 if (ctx->options & (LYS_COMPILE_NOTIFICATION | LYS_COMPILE_RPC_INPUT | LYS_COMPILE_RPC_OUTPUT)) {
703 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
704 ctx->options & LYS_COMPILE_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
705 } else {
706 target->flags &= ~LYS_CONFIG_MASK;
707 target->flags |= rfn->flags & LYS_CONFIG_MASK;
708 }
709 }
710
711 /* mandatory */
712 if (rfn->flags & LYS_MAND_MASK) {
713 switch (target->nodetype) {
714 case LYS_LEAF:
715 case LYS_CHOICE:
716 case LYS_ANYDATA:
717 case LYS_ANYXML:
718 break;
719 default:
720 AMEND_WRONG_NODETYPE("refine", "replace", "mandatory");
721 }
722
723 target->flags &= ~LYS_MAND_MASK;
724 target->flags |= rfn->flags & LYS_MAND_MASK;
725 }
726
727 /* presence */
728 if (rfn->presence) {
729 switch (target->nodetype) {
730 case LYS_CONTAINER:
731 break;
732 default:
733 AMEND_WRONG_NODETYPE("refine", "replace", "presence");
734 }
735
736 FREE_STRING(ctx->ctx, ((struct lysp_node_container *)target)->presence);
737 DUP_STRING_GOTO(ctx->ctx, rfn->presence, ((struct lysp_node_container *)target)->presence, ret, cleanup);
738 }
739
740 /* must */
741 if (rfn->musts) {
742 switch (target->nodetype) {
743 case LYS_CONTAINER:
744 case LYS_LIST:
745 case LYS_LEAF:
746 case LYS_LEAFLIST:
747 case LYS_ANYDATA:
748 case LYS_ANYXML:
749 musts = &((struct lysp_node_container *)target)->musts;
750 break;
751 default:
752 AMEND_WRONG_NODETYPE("refine", "add", "must");
753 }
754
755 LY_ARRAY_FOR(rfn->musts, u) {
756 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
757 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &rfn->musts[u]), cleanup);
758 }
759 }
760
761 /* min-elements */
762 if (rfn->flags & LYS_SET_MIN) {
763 switch (target->nodetype) {
764 case LYS_LEAFLIST:
765 num = &((struct lysp_node_leaflist *)target)->min;
766 break;
767 case LYS_LIST:
768 num = &((struct lysp_node_list *)target)->min;
769 break;
770 default:
771 AMEND_WRONG_NODETYPE("refine", "replace", "min-elements");
772 }
773
774 *num = rfn->min;
775 }
776
777 /* max-elements */
778 if (rfn->flags & LYS_SET_MAX) {
779 switch (target->nodetype) {
780 case LYS_LEAFLIST:
781 num = &((struct lysp_node_leaflist *)target)->max;
782 break;
783 case LYS_LIST:
784 num = &((struct lysp_node_list *)target)->max;
785 break;
786 default:
787 AMEND_WRONG_NODETYPE("refine", "replace", "max-elements");
788 }
789
790 *num = rfn->max;
791 }
792
793 /* if-feature */
794 if (rfn->iffeatures) {
795 switch (target->nodetype) {
796 case LYS_LEAF:
797 case LYS_LEAFLIST:
798 case LYS_LIST:
799 case LYS_CONTAINER:
800 case LYS_CHOICE:
801 case LYS_CASE:
802 case LYS_ANYDATA:
803 case LYS_ANYXML:
804 break;
805 default:
806 AMEND_WRONG_NODETYPE("refine", "add", "if-feature");
807 }
808
809 LY_ARRAY_FOR(rfn->iffeatures, u) {
810 LY_ARRAY_NEW_GOTO(ctx->ctx, target->iffeatures, qname, ret, cleanup);
811 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &rfn->iffeatures[u]), cleanup);
812 }
813 }
814
815 /* extension */
816 /* TODO refine extensions */
817
818cleanup:
819 return ret;
820}
821
822/**
823 * @brief Apply deviate add.
824 *
825 * @param[in] ctx Compile context.
826 * @param[in] d Deviate add to apply.
827 * @param[in,out] target Deviation target.
828 * @return LY_ERR value.
829 */
830static LY_ERR
831lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct lysp_node *target)
832{
833 LY_ERR ret = LY_SUCCESS;
834 LY_ARRAY_COUNT_TYPE u;
835 struct lysp_qname *qname;
836 uint32_t *num;
837 struct lysp_restr **musts, *must;
838
839#define DEV_CHECK_NONPRESENCE(TYPE, MEMBER, PROPERTY, VALUEMEMBER) \
840 if (((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842 PROPERTY, ((TYPE)target)->VALUEMEMBER); \
843 ret = LY_EVALID; \
844 goto cleanup; \
845 }
846
847 /* [units-stmt] */
848 if (d->units) {
849 switch (target->nodetype) {
850 case LYS_LEAF:
851 case LYS_LEAFLIST:
852 break;
853 default:
854 AMEND_WRONG_NODETYPE("deviation", "add", "units");
855 }
856
857 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, units, "units", units);
858 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
859 }
860
861 /* *must-stmt */
862 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +0100863 musts = lysp_node_musts_p(target);
864 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200865 AMEND_WRONG_NODETYPE("deviation", "add", "must");
866 }
867
868 LY_ARRAY_FOR(d->musts, u) {
869 LY_ARRAY_NEW_GOTO(ctx->ctx, *musts, must, ret, cleanup);
870 LY_CHECK_GOTO(ret = lysp_restr_dup(ctx->ctx, must, &d->musts[u]), cleanup);
871 }
872 }
873
874 /* *unique-stmt */
875 if (d->uniques) {
876 switch (target->nodetype) {
877 case LYS_LIST:
878 break;
879 default:
880 AMEND_WRONG_NODETYPE("deviation", "add", "unique");
881 }
882
883 LY_ARRAY_FOR(d->uniques, u) {
884 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_list *)target)->uniques, qname, ret, cleanup);
885 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->uniques[u]), cleanup);
886 }
887 }
888
889 /* *default-stmt */
890 if (d->dflts) {
891 switch (target->nodetype) {
892 case LYS_LEAF:
893 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
894 DEV_CHECK_NONPRESENCE(struct lysp_node_leaf *, dflt.str, "default", dflt.str);
895
896 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflts[0]), cleanup);
897 break;
898 case LYS_LEAFLIST:
899 LY_ARRAY_FOR(d->dflts, u) {
900 LY_ARRAY_NEW_GOTO(ctx->ctx, ((struct lysp_node_leaflist *)target)->dflts, qname, ret, cleanup);
901 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, qname, &d->dflts[u]), cleanup);
902 }
903 break;
904 case LYS_CHOICE:
905 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
906 DEV_CHECK_NONPRESENCE(struct lysp_node_choice *, dflt.str, "default", dflt.str);
907
908 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflts[0]), cleanup);
909 break;
910 default:
911 AMEND_WRONG_NODETYPE("deviation", "add", "default");
912 }
913 }
914
915 /* [config-stmt] */
916 if (d->flags & LYS_CONFIG_MASK) {
917 switch (target->nodetype) {
918 case LYS_CONTAINER:
919 case LYS_LEAF:
920 case LYS_LEAFLIST:
921 case LYS_LIST:
922 case LYS_CHOICE:
923 case LYS_ANYDATA:
924 case LYS_ANYXML:
925 break;
926 default:
927 AMEND_WRONG_NODETYPE("deviation", "add", "config");
928 }
929
930 if (target->flags & LYS_CONFIG_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100931 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200932 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
933 target->flags & LYS_CONFIG_W ? "true" : "false");
934 ret = LY_EVALID;
935 goto cleanup;
936 }
937
938 target->flags |= d->flags & LYS_CONFIG_MASK;
939 }
940
941 /* [mandatory-stmt] */
942 if (d->flags & LYS_MAND_MASK) {
943 switch (target->nodetype) {
944 case LYS_LEAF:
945 case LYS_CHOICE:
946 case LYS_ANYDATA:
947 case LYS_ANYXML:
948 break;
949 default:
950 AMEND_WRONG_NODETYPE("deviation", "add", "mandatory");
951 }
952
953 if (target->flags & LYS_MAND_MASK) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100954 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200955 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
956 target->flags & LYS_MAND_TRUE ? "true" : "false");
957 ret = LY_EVALID;
958 goto cleanup;
959 }
960
961 target->flags |= d->flags & LYS_MAND_MASK;
962 }
963
964 /* [min-elements-stmt] */
965 if (d->flags & LYS_SET_MIN) {
966 switch (target->nodetype) {
967 case LYS_LEAFLIST:
968 num = &((struct lysp_node_leaflist *)target)->min;
969 break;
970 case LYS_LIST:
971 num = &((struct lysp_node_list *)target)->min;
972 break;
973 default:
974 AMEND_WRONG_NODETYPE("deviation", "add", "min-elements");
975 }
976
977 if (target->flags & LYS_SET_MIN) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100978 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200979 "Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
980 ret = LY_EVALID;
981 goto cleanup;
982 }
983
984 *num = d->min;
985 }
986
987 /* [max-elements-stmt] */
988 if (d->flags & LYS_SET_MAX) {
989 switch (target->nodetype) {
990 case LYS_LEAFLIST:
991 num = &((struct lysp_node_leaflist *)target)->max;
992 break;
993 case LYS_LIST:
994 num = &((struct lysp_node_list *)target)->max;
995 break;
996 default:
997 AMEND_WRONG_NODETYPE("deviation", "add", "max-elements");
998 }
999
1000 if (target->flags & LYS_SET_MAX) {
1001 if (*num) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001002 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001003 "Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
1004 *num);
1005 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001006 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001007 "Invalid deviation adding \"max-elements\" property which already exists (with value \"unbounded\").");
1008 }
1009 ret = LY_EVALID;
1010 goto cleanup;
1011 }
1012
1013 *num = d->max;
1014 }
1015
1016cleanup:
1017 return ret;
1018}
1019
1020/**
1021 * @brief Apply deviate delete.
1022 *
1023 * @param[in] ctx Compile context.
1024 * @param[in] d Deviate delete to apply.
1025 * @param[in,out] target Deviation target.
1026 * @return LY_ERR value.
1027 */
1028static LY_ERR
1029lys_apply_deviate_delete(struct lysc_ctx *ctx, struct lysp_deviate_del *d, struct lysp_node *target)
1030{
1031 LY_ERR ret = LY_SUCCESS;
1032 struct lysp_restr **musts;
1033 LY_ARRAY_COUNT_TYPE u, v;
1034 struct lysp_qname **uniques, **dflts;
1035
1036#define DEV_DEL_ARRAY(DEV_ARRAY, ORIG_ARRAY, DEV_MEMBER, ORIG_MEMBER, FREE_FUNC, PROPERTY) \
1037 LY_ARRAY_FOR(d->DEV_ARRAY, u) { \
1038 int found = 0; \
1039 LY_ARRAY_FOR(ORIG_ARRAY, v) { \
1040 if (!strcmp(d->DEV_ARRAY[u]DEV_MEMBER, (ORIG_ARRAY)[v]ORIG_MEMBER)) { \
1041 found = 1; \
1042 break; \
1043 } \
1044 } \
1045 if (!found) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001046 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001047 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
1048 PROPERTY, d->DEV_ARRAY[u]DEV_MEMBER); \
1049 ret = LY_EVALID; \
1050 goto cleanup; \
1051 } \
1052 LY_ARRAY_DECREMENT(ORIG_ARRAY); \
1053 FREE_FUNC(ctx->ctx, &(ORIG_ARRAY)[v]); \
1054 memmove(&(ORIG_ARRAY)[v], &(ORIG_ARRAY)[v + 1], (LY_ARRAY_COUNT(ORIG_ARRAY) - v) * sizeof *(ORIG_ARRAY)); \
1055 } \
1056 if (!LY_ARRAY_COUNT(ORIG_ARRAY)) { \
1057 LY_ARRAY_FREE(ORIG_ARRAY); \
1058 ORIG_ARRAY = NULL; \
1059 }
1060
1061#define DEV_CHECK_PRESENCE_VALUE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1062 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001063 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001064 ret = LY_EVALID; \
1065 goto cleanup; \
1066 } else if (strcmp(((TYPE)target)->MEMBER, VALUE)) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001067 LOGVAL(ctx->ctx, LYVE_REFERENCE, \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001068 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
1069 PROPERTY, VALUE, ((TYPE)target)->MEMBER); \
1070 ret = LY_EVALID; \
1071 goto cleanup; \
1072 }
1073
1074 /* [units-stmt] */
1075 if (d->units) {
1076 switch (target->nodetype) {
1077 case LYS_LEAF:
1078 case LYS_LEAFLIST:
1079 break;
1080 default:
1081 AMEND_WRONG_NODETYPE("deviation", "delete", "units");
1082 }
1083
1084 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, units, "deleting", "units", d->units);
1085 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1086 ((struct lysp_node_leaf *)target)->units = NULL;
1087 }
1088
1089 /* *must-stmt */
1090 if (d->musts) {
Radek Krejci9a3823e2021-01-27 20:26:46 +01001091 musts = lysp_node_musts_p(target);
1092 if (!musts) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001093 AMEND_WRONG_NODETYPE("deviation", "delete", "must");
1094 }
1095
1096 DEV_DEL_ARRAY(musts, *musts, .arg.str, .arg.str, lysp_restr_free, "must");
1097 }
1098
1099 /* *unique-stmt */
1100 if (d->uniques) {
1101 switch (target->nodetype) {
1102 case LYS_LIST:
1103 break;
1104 default:
1105 AMEND_WRONG_NODETYPE("deviation", "delete", "unique");
1106 }
1107
1108 uniques = &((struct lysp_node_list *)target)->uniques;
1109 DEV_DEL_ARRAY(uniques, *uniques, .str, .str, lysp_qname_free, "unique");
1110 }
1111
1112 /* *default-stmt */
1113 if (d->dflts) {
1114 switch (target->nodetype) {
1115 case LYS_LEAF:
1116 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1117 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_leaf *, dflt.str, "deleting", "default", d->dflts[0].str);
1118
1119 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1120 ((struct lysp_node_leaf *)target)->dflt.str = NULL;
1121 break;
1122 case LYS_LEAFLIST:
1123 dflts = &((struct lysp_node_leaflist *)target)->dflts;
1124 DEV_DEL_ARRAY(dflts, *dflts, .str, .str, lysp_qname_free, "default");
1125 break;
1126 case LYS_CHOICE:
1127 AMEND_CHECK_CARDINALITY(d->dflts, 1, "deviation", "default");
1128 DEV_CHECK_PRESENCE_VALUE(struct lysp_node_choice *, dflt.str, "deleting", "default", d->dflts[0].str);
1129
1130 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1131 ((struct lysp_node_choice *)target)->dflt.str = NULL;
1132 break;
1133 default:
1134 AMEND_WRONG_NODETYPE("deviation", "delete", "default");
1135 }
1136 }
1137
1138cleanup:
1139 return ret;
1140}
1141
1142/**
1143 * @brief Apply deviate replace.
1144 *
1145 * @param[in] ctx Compile context.
1146 * @param[in] d Deviate replace to apply.
1147 * @param[in,out] target Deviation target.
1148 * @return LY_ERR value.
1149 */
1150static LY_ERR
1151lys_apply_deviate_replace(struct lysc_ctx *ctx, struct lysp_deviate_rpl *d, struct lysp_node *target)
1152{
1153 LY_ERR ret = LY_SUCCESS;
1154 uint32_t *num;
1155
1156#define DEV_CHECK_PRESENCE(TYPE, MEMBER, DEVTYPE, PROPERTY, VALUE) \
1157 if (!((TYPE)target)->MEMBER) { \
Radek Krejci2efc45b2020-12-22 16:25:44 +01001158 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001159 ret = LY_EVALID; \
1160 goto cleanup; \
1161 }
1162
1163 /* [type-stmt] */
1164 if (d->type) {
1165 switch (target->nodetype) {
1166 case LYS_LEAF:
1167 case LYS_LEAFLIST:
1168 break;
1169 default:
1170 AMEND_WRONG_NODETYPE("deviation", "replace", "type");
1171 }
1172
1173 lysp_type_free(ctx->ctx, &((struct lysp_node_leaf *)target)->type);
1174 lysp_type_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->type, d->type);
1175 }
1176
1177 /* [units-stmt] */
1178 if (d->units) {
1179 switch (target->nodetype) {
1180 case LYS_LEAF:
1181 case LYS_LEAFLIST:
1182 break;
1183 default:
1184 AMEND_WRONG_NODETYPE("deviation", "replace", "units");
1185 }
1186
1187 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, units, "replacing", "units", d->units);
1188 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->units);
1189 DUP_STRING_GOTO(ctx->ctx, d->units, ((struct lysp_node_leaf *)target)->units, ret, cleanup);
1190 }
1191
1192 /* [default-stmt] */
1193 if (d->dflt.str) {
1194 switch (target->nodetype) {
1195 case LYS_LEAF:
1196 DEV_CHECK_PRESENCE(struct lysp_node_leaf *, dflt.str, "replacing", "default", d->dflt.str);
1197
1198 FREE_STRING(ctx->ctx, ((struct lysp_node_leaf *)target)->dflt.str);
1199 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_leaf *)target)->dflt, &d->dflt), cleanup);
1200 break;
1201 case LYS_CHOICE:
1202 DEV_CHECK_PRESENCE(struct lysp_node_choice *, dflt.str, "replacing", "default", d->dflt);
1203
1204 FREE_STRING(ctx->ctx, ((struct lysp_node_choice *)target)->dflt.str);
1205 LY_CHECK_GOTO(ret = lysp_qname_dup(ctx->ctx, &((struct lysp_node_choice *)target)->dflt, &d->dflt), cleanup);
1206 break;
1207 default:
1208 AMEND_WRONG_NODETYPE("deviation", "replace", "default");
1209 }
1210 }
1211
1212 /* [config-stmt] */
1213 if (d->flags & LYS_CONFIG_MASK) {
1214 switch (target->nodetype) {
1215 case LYS_CONTAINER:
1216 case LYS_LEAF:
1217 case LYS_LEAFLIST:
1218 case LYS_LIST:
1219 case LYS_CHOICE:
1220 case LYS_ANYDATA:
1221 case LYS_ANYXML:
1222 break;
1223 default:
1224 AMEND_WRONG_NODETYPE("deviation", "replace", "config");
1225 }
1226
1227 if (!(target->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001228 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "config",
1229 d->flags & LYS_CONFIG_W ? "config true" : "config false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001230 ret = LY_EVALID;
1231 goto cleanup;
1232 }
1233
1234 target->flags &= ~LYS_CONFIG_MASK;
1235 target->flags |= d->flags & LYS_CONFIG_MASK;
1236 }
1237
1238 /* [mandatory-stmt] */
1239 if (d->flags & LYS_MAND_MASK) {
1240 switch (target->nodetype) {
1241 case LYS_LEAF:
1242 case LYS_CHOICE:
1243 case LYS_ANYDATA:
1244 case LYS_ANYXML:
1245 break;
1246 default:
1247 AMEND_WRONG_NODETYPE("deviation", "replace", "mandatory");
1248 }
1249
1250 if (!(target->flags & LYS_MAND_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001251 LOGVAL(ctx->ctx, LY_VCODE_DEV_NOT_PRESENT, "replacing", "mandatory",
1252 d->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001253 ret = LY_EVALID;
1254 goto cleanup;
1255 }
1256
1257 target->flags &= ~LYS_MAND_MASK;
1258 target->flags |= d->flags & LYS_MAND_MASK;
1259 }
1260
1261 /* [min-elements-stmt] */
1262 if (d->flags & LYS_SET_MIN) {
1263 switch (target->nodetype) {
1264 case LYS_LEAFLIST:
1265 num = &((struct lysp_node_leaflist *)target)->min;
1266 break;
1267 case LYS_LIST:
1268 num = &((struct lysp_node_list *)target)->min;
1269 break;
1270 default:
1271 AMEND_WRONG_NODETYPE("deviation", "replace", "min-elements");
1272 }
1273
1274 if (!(target->flags & LYS_SET_MIN)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001275 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"min-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001276 ret = LY_EVALID;
1277 goto cleanup;
1278 }
1279
1280 *num = d->min;
1281 }
1282
1283 /* [max-elements-stmt] */
1284 if (d->flags & LYS_SET_MAX) {
1285 switch (target->nodetype) {
1286 case LYS_LEAFLIST:
1287 num = &((struct lysp_node_leaflist *)target)->max;
1288 break;
1289 case LYS_LIST:
1290 num = &((struct lysp_node_list *)target)->max;
1291 break;
1292 default:
1293 AMEND_WRONG_NODETYPE("deviation", "replace", "max-elements");
1294 }
1295
1296 if (!(target->flags & LYS_SET_MAX)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001297 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid deviation replacing \"max-elements\" property which is not present.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001298 ret = LY_EVALID;
1299 goto cleanup;
1300 }
1301
1302 *num = d->max;
1303 }
1304
1305cleanup:
1306 return ret;
1307}
1308
1309/**
1310 * @brief Get module of a single nodeid node name test.
1311 *
1312 * @param[in] ctx libyang context.
1313 * @param[in] nametest Nametest with an optional prefix.
1314 * @param[in] nametest_len Length of @p nametest.
1315 * @param[in] mod Both current and prefix module for resolving prefixes and to return in case of no prefix.
1316 * @param[out] name Optional pointer to the name test without the prefix.
1317 * @param[out] name_len Length of @p name.
1318 * @return Resolved module.
1319 */
1320static const struct lys_module *
1321lys_schema_node_get_module(const struct ly_ctx *ctx, const char *nametest, size_t nametest_len,
1322 const struct lysp_module *mod, const char **name, size_t *name_len)
1323{
1324 const struct lys_module *target_mod;
1325 const char *ptr;
1326
1327 ptr = ly_strnchr(nametest, ':', nametest_len);
1328 if (ptr) {
1329 target_mod = ly_resolve_prefix(ctx, nametest, ptr - nametest, LY_PREF_SCHEMA, (void *)mod);
1330 if (!target_mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001331 LOGVAL(ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001332 "Invalid absolute-schema-nodeid nametest \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1333 nametest_len, nametest, ptr - nametest, nametest, LYSP_MODULE_NAME(mod));
1334 return NULL;
1335 }
1336
1337 if (name) {
1338 *name = ptr + 1;
1339 *name_len = nametest_len - ((ptr - nametest) + 1);
1340 }
1341 } else {
1342 target_mod = mod->mod;
1343 if (name) {
1344 *name = nametest;
1345 *name_len = nametest_len;
1346 }
1347 }
1348
1349 return target_mod;
1350}
1351
1352/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001353 * @brief Check whether a compiled node matches a single schema nodeid name test.
1354 *
1355 * @param[in,out] node Compiled node to consider. On a match it is moved to its parent.
1356 * @param[in] mod Expected module.
1357 * @param[in] name Expected name.
1358 * @param[in] name_len Length of @p name.
1359 * @return Whether it is a match or not.
1360 */
1361static ly_bool
1362lysp_schema_nodeid_match_node(const struct lysc_node **node, const struct lys_module *mod, const char *name,
1363 size_t name_len)
1364{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001365 /* compare with the module of the node */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001366 if ((*node)->module != mod) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001367 return 0;
1368 }
1369
1370 /* compare names */
Michal Vasko544e58a2021-01-28 14:33:41 +01001371 if (ly_strncmp((*node)->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001372 return 0;
1373 }
1374
Michal Vasko2a668712020-10-21 11:48:09 +02001375 /* move to next parent */
Radek Krejci7d95fbb2021-01-26 17:33:13 +01001376 *node = (*node)->parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001377
1378 return 1;
1379}
1380
1381/**
1382 * @brief Check whether a node matches specific schema nodeid.
1383 *
1384 * @param[in] exp Parsed nodeid to match.
1385 * @param[in] exp_pmod Module to use for nodes in @p exp without a prefix.
1386 * @param[in] ctx_node Initial context node that should match, only for descendant paths.
1387 * @param[in] parent First compiled parent to consider. If @p pnode is NULL, it is condered the node to be matched.
1388 * @param[in] pnode Parsed node to be matched. May be NULL if the target node was already compiled.
1389 * @param[in] pnode_mod Compiled @p pnode to-be module.
1390 * @return Whether it is a match or not.
1391 */
1392static ly_bool
1393lysp_schema_nodeid_match(const struct lyxp_expr *exp, const struct lysp_module *exp_pmod, const struct lysc_node *ctx_node,
1394 const struct lysc_node *parent, const struct lysp_node *pnode, const struct lys_module *pnode_mod)
1395{
1396 uint32_t i;
1397 const struct lys_module *mod;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001398 const char *name = NULL;
1399 size_t name_len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001400
1401 /* compare last node in the node ID */
1402 i = exp->used - 1;
1403
1404 /* get exp node ID module */
1405 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);
1406 assert(mod);
1407
1408 if (pnode) {
1409 /* compare on the last parsed-only node */
Radek Krejci2d5f6df2021-01-28 14:00:13 +01001410 if ((pnode_mod != mod) || ly_strncmp(pnode->name, name, name_len)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001411 return 0;
1412 }
1413 } else {
1414 /* using parent directly */
1415 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1416 return 0;
1417 }
1418 }
1419
1420 /* now compare all the compiled parents */
1421 while (i > 1) {
1422 i -= 2;
1423 assert(exp->tokens[i] == LYXP_TOKEN_NAMETEST);
1424
1425 if (!parent) {
1426 /* no more parents but path continues */
1427 return 0;
1428 }
1429
1430 /* get exp node ID module */
1431 mod = lys_schema_node_get_module(exp_pmod->mod->ctx, exp->expr + exp->tok_pos[i], exp->tok_len[i], exp_pmod, &name,
1432 &name_len);
1433 assert(mod);
1434
1435 /* compare with the parent */
1436 if (!lysp_schema_nodeid_match_node(&parent, mod, name, name_len)) {
1437 return 0;
1438 }
1439 }
1440
1441 if (ctx_node && (ctx_node != parent)) {
1442 /* descendant path has not finished in the context node */
1443 return 0;
1444 } else if (!ctx_node && parent) {
1445 /* some parent was not matched */
1446 return 0;
1447 }
1448
1449 return 1;
1450}
1451
1452void
1453lysc_augment_free(const struct ly_ctx *ctx, struct lysc_augment *aug)
1454{
1455 if (aug) {
1456 lyxp_expr_free(ctx, aug->nodeid);
1457
1458 free(aug);
1459 }
1460}
1461
1462void
1463lysc_deviation_free(const struct ly_ctx *ctx, struct lysc_deviation *dev)
1464{
1465 if (dev) {
1466 lyxp_expr_free(ctx, dev->nodeid);
1467 LY_ARRAY_FREE(dev->devs);
1468 LY_ARRAY_FREE(dev->dev_pmods);
1469
1470 free(dev);
1471 }
1472}
1473
1474void
1475lysc_refine_free(const struct ly_ctx *ctx, struct lysc_refine *rfn)
1476{
1477 if (rfn) {
1478 lyxp_expr_free(ctx, rfn->nodeid);
1479 LY_ARRAY_FREE(rfn->rfns);
1480
1481 free(rfn);
1482 }
1483}
1484
1485void
1486lysp_dev_node_free(const struct ly_ctx *ctx, struct lysp_node *dev_pnode)
1487{
1488 if (!dev_pnode) {
1489 return;
1490 }
1491
1492 switch (dev_pnode->nodetype) {
1493 case LYS_CONTAINER:
1494 ((struct lysp_node_container *)dev_pnode)->child = NULL;
1495 break;
1496 case LYS_LIST:
1497 ((struct lysp_node_list *)dev_pnode)->child = NULL;
1498 break;
1499 case LYS_CHOICE:
1500 ((struct lysp_node_choice *)dev_pnode)->child = NULL;
1501 break;
1502 case LYS_CASE:
1503 ((struct lysp_node_case *)dev_pnode)->child = NULL;
1504 break;
1505 case LYS_LEAF:
1506 case LYS_LEAFLIST:
1507 case LYS_ANYXML:
1508 case LYS_ANYDATA:
1509 /* no children */
1510 break;
1511 case LYS_NOTIF:
Radek Krejci01180ac2021-01-27 08:48:22 +01001512 ((struct lysp_node_notif *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001513 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001514 case LYS_RPC:
1515 case LYS_ACTION:
Radek Krejci01180ac2021-01-27 08:48:22 +01001516 ((struct lysp_node_action *)dev_pnode)->input.child = NULL;
1517 ((struct lysp_node_action *)dev_pnode)->output.child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001518 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001519 case LYS_INPUT:
1520 case LYS_OUTPUT:
Radek Krejci01180ac2021-01-27 08:48:22 +01001521 ((struct lysp_node_action_inout *)dev_pnode)->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001522 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001523 free(dev_pnode);
1524 return;
1525 default:
1526 LOGINT(ctx);
1527 return;
1528 }
1529
1530 lysp_node_free((struct ly_ctx *)ctx, dev_pnode);
1531}
1532
1533LY_ERR
1534lys_compile_node_deviations_refines(struct lysc_ctx *ctx, const struct lysp_node *pnode, const struct lysc_node *parent,
1535 struct lysp_node **dev_pnode, ly_bool *not_supported)
1536{
1537 LY_ERR ret = LY_SUCCESS;
1538 uint32_t i;
1539 LY_ARRAY_COUNT_TYPE u;
1540 struct lys_module *orig_mod = ctx->cur_mod;
1541 struct lysp_module *orig_pmod = ctx->pmod;
1542 char orig_path[LYSC_CTX_BUFSIZE];
1543 struct lysc_refine *rfn;
1544 struct lysc_deviation *dev;
1545 struct lysp_deviation *dev_p;
1546 struct lysp_deviate *d;
1547
1548 *dev_pnode = NULL;
1549 *not_supported = 0;
1550
1551 for (i = 0; i < ctx->uses_rfns.count; ++i) {
1552 rfn = ctx->uses_rfns.objs[i];
1553
1554 if (!lysp_schema_nodeid_match(rfn->nodeid, rfn->nodeid_pmod, rfn->nodeid_ctx_node, parent, pnode, ctx->cur_mod)) {
1555 /* not our target node */
1556 continue;
1557 }
1558
1559 if (!*dev_pnode) {
1560 /* first refine on this node, create a copy first */
1561 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1562 }
1563
Michal Vaskob8df5762021-01-12 15:15:53 +01001564 /* use modules from the refine */
1565 ctx->cur_mod = rfn->nodeid_pmod->mod;
1566 ctx->pmod = (struct lysp_module *)rfn->nodeid_pmod;
1567
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001568 /* apply all the refines by changing (the copy of) the parsed node */
1569 LY_ARRAY_FOR(rfn->rfns, u) {
Michal Vaskob8df5762021-01-12 15:15:53 +01001570 /* keep the current path and add to it */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 lysc_update_path(ctx, NULL, "{refine}");
1572 lysc_update_path(ctx, NULL, rfn->rfns[u]->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001573
1574 /* apply refine and restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001575 ret = lys_apply_refine(ctx, rfn->rfns[u], *dev_pnode);
1576 lysc_update_path(ctx, NULL, NULL);
1577 lysc_update_path(ctx, NULL, NULL);
1578 LY_CHECK_GOTO(ret, cleanup);
1579 }
1580
1581 /* refine was applied, remove it */
1582 lysc_refine_free(ctx->ctx, rfn);
1583 ly_set_rm_index(&ctx->uses_rfns, i, NULL);
1584
1585 /* all the refines for one target node are in one structure, we are done */
1586 break;
1587 }
1588
1589 for (i = 0; i < ctx->devs.count; ++i) {
1590 dev = ctx->devs.objs[i];
1591
1592 if (!lysp_schema_nodeid_match(dev->nodeid, dev->dev_pmods[0], NULL, parent, pnode, ctx->cur_mod)) {
1593 /* not our target node */
1594 continue;
1595 }
1596
1597 if (dev->not_supported) {
1598 /* it is not supported, no more deviations */
1599 *not_supported = 1;
1600 goto dev_applied;
1601 }
1602
1603 if (!*dev_pnode) {
1604 /* first deviation on this node, create a copy first */
1605 LY_CHECK_GOTO(ret = lysp_dup_single(ctx->ctx, pnode, 1, dev_pnode), cleanup);
1606 }
1607
1608 /* apply all the deviates by changing (the copy of) the parsed node */
1609 LY_ARRAY_FOR(dev->devs, u) {
1610 dev_p = dev->devs[u];
1611 LY_LIST_FOR(dev_p->deviates, d) {
1612 /* generate correct path */
1613 strcpy(orig_path, ctx->path);
1614 ctx->path_len = 1;
1615 ctx->cur_mod = dev->dev_pmods[u]->mod;
1616 ctx->pmod = (struct lysp_module *)dev->dev_pmods[u];
1617 lysc_update_path(ctx, NULL, "{deviation}");
1618 lysc_update_path(ctx, NULL, dev_p->nodeid);
1619
1620 switch (d->mod) {
1621 case LYS_DEV_ADD:
1622 ret = lys_apply_deviate_add(ctx, (struct lysp_deviate_add *)d, *dev_pnode);
1623 break;
1624 case LYS_DEV_DELETE:
1625 ret = lys_apply_deviate_delete(ctx, (struct lysp_deviate_del *)d, *dev_pnode);
1626 break;
1627 case LYS_DEV_REPLACE:
1628 ret = lys_apply_deviate_replace(ctx, (struct lysp_deviate_rpl *)d, *dev_pnode);
1629 break;
1630 default:
1631 LOGINT(ctx->ctx);
1632 ret = LY_EINT;
1633 }
1634
1635 /* restore previous path */
1636 strcpy(ctx->path, orig_path);
1637 ctx->path_len = strlen(ctx->path);
1638 ctx->cur_mod = orig_mod;
1639 ctx->pmod = orig_pmod;
1640
1641 LY_CHECK_GOTO(ret, cleanup);
1642 }
1643 }
1644
1645dev_applied:
1646 /* deviation was applied, remove it */
1647 lysc_deviation_free(ctx->ctx, dev);
1648 ly_set_rm_index(&ctx->devs, i, NULL);
1649
1650 /* all the deviations for one target node are in one structure, we are done */
1651 break;
1652 }
1653
1654cleanup:
Michal Vasko20316b32021-01-12 15:16:54 +01001655 ctx->cur_mod = orig_mod;
1656 ctx->pmod = orig_pmod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001657 if (ret) {
1658 lysp_dev_node_free(ctx->ctx, *dev_pnode);
1659 *dev_pnode = NULL;
1660 *not_supported = 0;
1661 }
1662 return ret;
1663}
1664
1665/**
1666 * @brief Compile the parsed augment connecting it into its target.
1667 *
1668 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
1669 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
1670 * are already implemented and compiled.
1671 *
1672 * @param[in] ctx Compile context.
1673 * @param[in] aug_p Parsed augment to compile.
1674 * @param[in] target Target node of the augment.
1675 * @return LY_SUCCESS on success.
1676 * @return LY_EVALID on failure.
1677 */
1678static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001679lys_compile_augment(struct lysc_ctx *ctx, struct lysp_node_augment *aug_p, struct lysc_node *target)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001680{
1681 LY_ERR ret = LY_SUCCESS;
1682 struct lysp_node *pnode;
1683 struct lysc_node *node;
1684 struct lysc_when *when_shared = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001685 struct lysc_node_action **actions;
1686 struct lysc_node_notif **notifs;
Michal Vasko29dd11e2020-11-23 16:52:22 +01001687 ly_bool allow_mandatory = 0, enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001688 struct ly_set child_set = {0};
Michal Vasko29dd11e2020-11-23 16:52:22 +01001689 uint32_t i, opt_prev = ctx->options;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001690
1691 if (!(target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001692 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001693 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
1694 aug_p->nodeid[0] == '/' ? "absolute" : "descendant", aug_p->nodeid, lys_nodetype2str(target->nodetype));
1695 ret = LY_EVALID;
1696 goto cleanup;
1697 }
1698
1699 /* check for mandatory nodes
1700 * - new cases augmenting some choice can have mandatory nodes
1701 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
1702 */
1703 if (aug_p->when || (target->nodetype == LYS_CHOICE) || (ctx->cur_mod == target->module)) {
1704 allow_mandatory = 1;
1705 }
1706
1707 LY_LIST_FOR(aug_p->child, pnode) {
1708 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
1709 if (((pnode->nodetype == LYS_CASE) && (target->nodetype != LYS_CHOICE)) ||
1710 ((pnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && !(target->nodetype & (LYS_CONTAINER | LYS_LIST))) ||
1711 ((pnode->nodetype == LYS_USES) && (target->nodetype == LYS_CHOICE))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001712 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001713 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
1714 lys_nodetype2str(target->nodetype), lys_nodetype2str(pnode->nodetype), pnode->name);
1715 ret = LY_EVALID;
1716 goto cleanup;
1717 }
1718
1719 /* compile the children */
1720 if (target->nodetype == LYS_CHOICE) {
1721 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, pnode, target, &child_set), cleanup);
Michal Vasko6fb4c042020-11-24 18:05:26 +01001722 } else if (target->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
1723 if (target->nodetype == LYS_INPUT) {
1724 ctx->options |= LYS_COMPILE_RPC_INPUT;
1725 } else {
1726 ctx->options |= LYS_COMPILE_RPC_OUTPUT;
1727 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001728 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001729 } else {
1730 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
1731 }
1732
Michal Vasko29dd11e2020-11-23 16:52:22 +01001733 /* eval if-features again for the rest of this node processing */
1734 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1735 if (!enabled) {
1736 ctx->options |= LYS_COMPILE_DISABLED;
1737 }
1738
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001739 /* since the augment node is not present in the compiled tree, we need to pass some of its
1740 * statements to all its children */
1741 for (i = 0; i < child_set.count; ++i) {
1742 node = child_set.snodes[i];
1743 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
1744 node->flags &= ~LYS_MAND_TRUE;
1745 lys_compile_mandatory_parents(target, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001746 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001747 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
1748 ret = LY_EVALID;
1749 goto cleanup;
1750 }
1751
1752 if (aug_p->when) {
1753 /* pass augment's when to all the children */
Michal Vasko72244882021-01-12 15:21:05 +01001754 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 +02001755 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod9062b02020-11-04 17:15:50 +01001756
1757 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
1758 /* container with a when condition */
1759 LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a meaning from its "
1760 "inherited \"when\" condition.", node->name);
1761 node->flags |= LYS_PRESENCE;
1762 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001763 }
1764 }
1765 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001766
1767 /* restore options */
1768 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001769 }
1770
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001771 actions = lysc_node_actions_p(target);
1772 notifs = lysc_node_notifs_p(target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001773
1774 if (aug_p->actions) {
1775 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001776 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001777 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001778 lys_nodetype2str(target->nodetype), aug_p->actions->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001779 ret = LY_EVALID;
1780 goto cleanup;
1781 }
1782
1783 /* compile actions into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001784 LY_LIST_FOR((struct lysp_node *)aug_p->actions, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001785 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001786
Michal Vasko012807e2021-02-03 09:52:18 +01001787 /* eval if-features again for the rest of this node processing */
1788 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1789 if (!enabled) {
1790 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001791 }
Michal Vasko012807e2021-02-03 09:52:18 +01001792
1793 /* since the augment node is not present in the compiled tree, we need to pass some of its
1794 * statements to all its children */
1795 for (i = 0; i < child_set.count; ++i) {
1796 node = child_set.snodes[i];
1797 if (aug_p->when) {
1798 /* pass augment's when to all the actions */
1799 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1800 LY_CHECK_GOTO(ret, cleanup);
1801 }
1802 }
1803 ly_set_erase(&child_set, NULL);
1804
1805 /* restore options */
1806 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001807 }
1808 }
1809 if (aug_p->notifs) {
1810 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001811 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001812 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001813 lys_nodetype2str(target->nodetype), aug_p->notifs->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001814 ret = LY_EVALID;
1815 goto cleanup;
1816 }
1817
1818 /* compile notifications into the target */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001819 LY_LIST_FOR((struct lysp_node *)aug_p->notifs, pnode) {
Michal Vasko012807e2021-02-03 09:52:18 +01001820 LY_CHECK_GOTO(ret = lys_compile_node(ctx, pnode, target, 0, &child_set), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001821
Michal Vasko012807e2021-02-03 09:52:18 +01001822 /* eval if-features again for the rest of this node processing */
1823 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
1824 if (!enabled) {
1825 ctx->options |= LYS_COMPILE_DISABLED;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 }
Michal Vasko012807e2021-02-03 09:52:18 +01001827
1828 /* since the augment node is not present in the compiled tree, we need to pass some of its
1829 * statements to all its children */
1830 for (i = 0; i < child_set.count; ++i) {
1831 node = child_set.snodes[i];
1832 if (aug_p->when) {
1833 /* pass augment's when to all the actions */
1834 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
1835 LY_CHECK_GOTO(ret, cleanup);
1836 }
1837 }
1838 ly_set_erase(&child_set, NULL);
1839
1840 /* restore options */
1841 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001842 }
1843 }
1844
1845cleanup:
1846 ly_set_erase(&child_set, NULL);
Michal Vasko29dd11e2020-11-23 16:52:22 +01001847 ctx->options = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001848 return ret;
1849}
1850
1851LY_ERR
1852lys_compile_node_augments(struct lysc_ctx *ctx, struct lysc_node *node)
1853{
1854 LY_ERR ret = LY_SUCCESS;
1855 struct lys_module *orig_mod = ctx->cur_mod;
1856 struct lysp_module *orig_pmod = ctx->pmod;
1857 uint32_t i;
1858 char orig_path[LYSC_CTX_BUFSIZE];
1859 struct lysc_augment *aug;
1860
1861 /* uses augments */
1862 for (i = 0; i < ctx->uses_augs.count; ) {
1863 aug = ctx->uses_augs.objs[i];
1864
1865 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, aug->nodeid_ctx_node, node, NULL, NULL)) {
1866 /* not our target node */
1867 ++i;
1868 continue;
1869 }
1870
Michal Vaskob8df5762021-01-12 15:15:53 +01001871 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001872 lysc_update_path(ctx, NULL, "{augment}");
1873 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
Michal Vaskob8df5762021-01-12 15:15:53 +01001874 ctx->cur_mod = aug->nodeid_pmod->mod;
1875 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
1876
1877 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001878 ret = lys_compile_augment(ctx, aug->aug_p, node);
1879 lysc_update_path(ctx, NULL, NULL);
1880 lysc_update_path(ctx, NULL, NULL);
1881 LY_CHECK_GOTO(ret, cleanup);
1882
1883 /* augment was applied, remove it (index may have changed because other augments could have been applied) */
1884 ly_set_rm(&ctx->uses_augs, aug, NULL);
1885 lysc_augment_free(ctx->ctx, aug);
1886 }
1887
1888 /* top-level augments */
1889 for (i = 0; i < ctx->augs.count; ) {
1890 aug = ctx->augs.objs[i];
1891
1892 if (!lysp_schema_nodeid_match(aug->nodeid, aug->nodeid_pmod, NULL, node, NULL, NULL)) {
1893 /* not our target node */
1894 ++i;
1895 continue;
1896 }
1897
Michal Vaskob8df5762021-01-12 15:15:53 +01001898 /* use the path and modules from the augment */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001899 strcpy(orig_path, ctx->path);
1900 ctx->path_len = 1;
1901 lysc_update_path(ctx, NULL, "{augment}");
1902 lysc_update_path(ctx, NULL, aug->aug_p->nodeid);
1903 ctx->cur_mod = aug->nodeid_pmod->mod;
1904 ctx->pmod = (struct lysp_module *)aug->nodeid_pmod;
Michal Vaskob8df5762021-01-12 15:15:53 +01001905
1906 /* apply augment, restore the path */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001907 ret = lys_compile_augment(ctx, aug->aug_p, node);
1908 strcpy(ctx->path, orig_path);
1909 ctx->path_len = strlen(ctx->path);
1910 LY_CHECK_GOTO(ret, cleanup);
1911
1912 /* augment was applied, remove it */
1913 ly_set_rm(&ctx->augs, aug, NULL);
1914 lysc_augment_free(ctx->ctx, aug);
1915 }
1916
1917cleanup:
1918 ctx->cur_mod = orig_mod;
1919 ctx->pmod = orig_pmod;
1920 return ret;
1921}
1922
1923/**
1924 * @brief Prepare a top-level augment to be applied during data nodes compilation.
1925 *
1926 * @param[in] ctx Compile context.
1927 * @param[in] aug_p Parsed augment to be applied.
1928 * @param[in] pmod Both current and prefix module for @p aug_p.
1929 * @return LY_ERR value.
1930 */
1931static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01001932lys_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 +02001933{
1934 LY_ERR ret = LY_SUCCESS;
1935 struct lyxp_expr *exp = NULL;
1936 struct lysc_augment *aug;
1937 const struct lys_module *mod;
1938
1939 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
1940 ret = lyxp_expr_parse(ctx->ctx, aug_p->nodeid, strlen(aug_p->nodeid), 0, &exp);
1941 LY_CHECK_GOTO(ret, cleanup);
1942
1943 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
1944 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
1945 if (mod != ctx->cur_mod) {
1946 /* augment for another module, ignore */
1947 goto cleanup;
1948 }
1949
1950 /* allocate new compiled augment and store it in the set */
1951 aug = calloc(1, sizeof *aug);
1952 LY_CHECK_ERR_GOTO(!aug, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
1953 LY_CHECK_GOTO(ret = ly_set_add(&ctx->augs, aug, 1, NULL), cleanup);
1954
1955 aug->nodeid = exp;
1956 exp = NULL;
1957 aug->nodeid_pmod = pmod;
1958 aug->aug_p = aug_p;
1959
1960cleanup:
1961 lyxp_expr_free(ctx->ctx, exp);
1962 return ret;
1963}
1964
1965LY_ERR
1966lys_precompile_own_augments(struct lysc_ctx *ctx)
1967{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001968 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001969
1970 LY_ARRAY_FOR(ctx->cur_mod->augmented_by, u) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001971 const struct lys_module *aug_mod = ctx->cur_mod->augmented_by[u];
1972 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001973
1974 /* collect all module augments */
Radek Krejci2a9fc652021-01-22 17:44:34 +01001975 LY_LIST_FOR(aug_mod->parsed->augments, aug) {
1976 LY_CHECK_RET(lys_precompile_own_augment(ctx, aug, aug_mod->parsed));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001977 }
1978
1979 /* collect all submodules augments */
1980 LY_ARRAY_FOR(aug_mod->parsed->includes, v) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001981 LY_LIST_FOR(aug_mod->parsed->includes[v].submodule->augments, aug) {
1982 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 +02001983 }
1984 }
1985 }
1986
1987 return LY_SUCCESS;
1988}
1989
1990/**
1991 * @brief Prepare a deviation to be applied during data nodes compilation.
1992 *
1993 * @param[in] ctx Compile context.
1994 * @param[in] dev_p Parsed deviation to be applied.
1995 * @param[in] pmod Both current and prefix module for @p dev_p.
1996 * @return LY_ERR value.
1997 */
1998static LY_ERR
1999lys_precompile_own_deviation(struct lysc_ctx *ctx, struct lysp_deviation *dev_p, const struct lysp_module *pmod)
2000{
2001 LY_ERR ret = LY_SUCCESS;
2002 struct lysc_deviation *dev = NULL;
2003 struct lyxp_expr *exp = NULL;
2004 struct lysp_deviation **new_dev;
2005 const struct lys_module *mod;
2006 const struct lysp_module **new_dev_pmod;
2007 uint32_t i;
2008
2009 /* parse its target, it was already parsed and fully checked (except for the existence of the nodes) */
2010 ret = lyxp_expr_parse(ctx->ctx, dev_p->nodeid, strlen(dev_p->nodeid), 0, &exp);
2011 LY_CHECK_GOTO(ret, cleanup);
2012
2013 mod = lys_schema_node_get_module(ctx->ctx, exp->expr + exp->tok_pos[1], exp->tok_len[1], pmod, NULL, NULL);
2014 LY_CHECK_ERR_GOTO(!mod, LOGINT(ctx->ctx); ret = LY_EINT, cleanup);
2015 if (mod != ctx->cur_mod) {
2016 /* deviation for another module, ignore */
2017 goto cleanup;
2018 }
2019
2020 /* try to find the node in already compiled deviations */
2021 for (i = 0; i < ctx->devs.count; ++i) {
2022 if (lys_abs_schema_nodeid_match(ctx->ctx, exp, pmod, ((struct lysc_deviation *)ctx->devs.objs[i])->nodeid,
2023 ((struct lysc_deviation *)ctx->devs.objs[i])->dev_pmods[0])) {
2024 dev = ctx->devs.objs[i];
2025 break;
2026 }
2027 }
2028
2029 if (!dev) {
2030 /* allocate new compiled deviation */
2031 dev = calloc(1, sizeof *dev);
2032 LY_CHECK_ERR_GOTO(!dev, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
2033 LY_CHECK_GOTO(ret = ly_set_add(&ctx->devs, dev, 1, NULL), cleanup);
2034
2035 dev->nodeid = exp;
2036 exp = NULL;
2037 }
2038
2039 /* add new parsed deviation structure */
2040 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->devs, new_dev, ret, cleanup);
2041 *new_dev = dev_p;
2042 LY_ARRAY_NEW_GOTO(ctx->ctx, dev->dev_pmods, new_dev_pmod, ret, cleanup);
2043 *new_dev_pmod = pmod;
2044
2045cleanup:
2046 lyxp_expr_free(ctx->ctx, exp);
2047 return ret;
2048}
2049
2050LY_ERR
2051lys_precompile_own_deviations(struct lysc_ctx *ctx)
2052{
2053 LY_ARRAY_COUNT_TYPE u, v, w;
2054 const struct lys_module *dev_mod;
2055 struct lysc_deviation *dev;
2056 struct lysp_deviate *d;
2057 int not_supported;
2058 uint32_t i;
2059
2060 LY_ARRAY_FOR(ctx->cur_mod->deviated_by, u) {
2061 dev_mod = ctx->cur_mod->deviated_by[u];
2062
2063 /* compile all module deviations */
2064 LY_ARRAY_FOR(dev_mod->parsed->deviations, v) {
2065 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->deviations[v], dev_mod->parsed));
2066 }
2067
2068 /* compile all submodules deviations */
2069 LY_ARRAY_FOR(dev_mod->parsed->includes, v) {
2070 LY_ARRAY_FOR(dev_mod->parsed->includes[v].submodule->deviations, w) {
2071 LY_CHECK_RET(lys_precompile_own_deviation(ctx, &dev_mod->parsed->includes[v].submodule->deviations[w],
2072 (struct lysp_module *)dev_mod->parsed->includes[v].submodule));
2073 }
2074 }
2075 }
2076
2077 /* set not-supported flags for all the deviations */
2078 for (i = 0; i < ctx->devs.count; ++i) {
2079 dev = ctx->devs.objs[i];
2080 not_supported = 0;
2081
2082 LY_ARRAY_FOR(dev->devs, u) {
2083 LY_LIST_FOR(dev->devs[u]->deviates, d) {
2084 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
2085 not_supported = 1;
2086 break;
2087 }
2088 }
2089 if (not_supported) {
2090 break;
2091 }
2092 }
2093 if (not_supported && (LY_ARRAY_COUNT(dev->devs) > 1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002094 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002095 "Multiple deviations of \"%s\" with one of them being \"not-supported\".", dev->nodeid->expr);
2096 return LY_EVALID;
2097 }
2098
2099 dev->not_supported = not_supported;
2100 }
2101
2102 return LY_SUCCESS;
2103}
2104
2105/**
2106 * @brief Add a module reference into an array, checks for duplicities.
2107 *
2108 * @param[in] ctx Compile context.
2109 * @param[in] mod Module reference to add.
2110 * @param[in,out] mod_array Module sized array to add to.
2111 * @return LY_ERR value.
2112 */
2113static LY_ERR
2114lys_array_add_mod_ref(struct lysc_ctx *ctx, struct lys_module *mod, struct lys_module ***mod_array)
2115{
2116 LY_ARRAY_COUNT_TYPE u;
2117 struct lys_module **new_mod;
2118
2119 LY_ARRAY_FOR(*mod_array, u) {
2120 if ((*mod_array)[u] == mod) {
2121 /* already there */
2122 return LY_EEXIST;
2123 }
2124 }
2125
2126 /* add the new module ref */
2127 LY_ARRAY_NEW_RET(ctx->ctx, *mod_array, new_mod, LY_EMEM);
2128 *new_mod = mod;
2129
2130 return LY_SUCCESS;
2131}
2132
2133LY_ERR
2134lys_precompile_augments_deviations(struct lysc_ctx *ctx)
2135{
Michal Vasko405cc9e2020-12-01 12:01:27 +01002136 LY_ERR ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002137 LY_ARRAY_COUNT_TYPE u, v;
2138 const struct lysp_module *mod_p;
2139 const struct lysc_node *target;
2140 struct lys_module *mod;
2141 struct lysp_submodule *submod;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002142 struct lysp_node_augment *aug;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002143 ly_bool has_dev = 0;
2144 uint16_t flags;
2145 uint32_t idx, opt_prev = ctx->options;
2146
Michal Vasko405cc9e2020-12-01 12:01:27 +01002147 for (idx = 0; idx < ctx->unres->implementing.count; ++idx) {
2148 if (ctx->cur_mod == ctx->unres->implementing.objs[idx]) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002149 break;
2150 }
2151 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01002152 if (idx == ctx->unres->implementing.count) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002153 /* it was already implemented and all the augments and deviations fully applied */
2154 return LY_SUCCESS;
2155 }
2156
2157 mod_p = ctx->cur_mod->parsed;
2158
Radek Krejci2a9fc652021-01-22 17:44:34 +01002159 LY_LIST_FOR(mod_p->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002160 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002161 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002162
2163 /* get target module */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002164 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002165 LY_CHECK_RET(ret);
2166
2167 /* add this module into the target module augmented_by, if not there already from previous augments */
2168 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2169
2170 /* if we are compiling this module, we cannot add augments to it yet */
2171 if (mod != ctx->cur_mod) {
2172 /* apply the augment, find the target node first */
2173 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002174 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002175 (void *)mod_p, 0, &target, &flags);
2176 LY_CHECK_RET(ret);
2177
2178 /* apply the augment */
2179 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002180 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002181 ctx->options = opt_prev;
2182 LY_CHECK_RET(ret);
2183 }
2184
2185 lysc_update_path(ctx, NULL, NULL);
2186 lysc_update_path(ctx, NULL, NULL);
2187 }
2188
2189 LY_ARRAY_FOR(mod_p->deviations, u) {
2190 /* get target module */
2191 lysc_update_path(ctx, NULL, "{deviation}");
2192 lysc_update_path(ctx, NULL, mod_p->deviations[u].nodeid);
2193 ret = lys_nodeid_check(ctx, mod_p->deviations[u].nodeid, 1, &mod, NULL);
2194 lysc_update_path(ctx, NULL, NULL);
2195 lysc_update_path(ctx, NULL, NULL);
2196 LY_CHECK_RET(ret);
2197
2198 /* add this module into the target module deviated_by, if not there already from previous deviations */
2199 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2200
2201 /* new deviation added to the target module */
2202 has_dev = 1;
2203 }
2204
2205 /* the same for augments and deviations in submodules */
2206 LY_ARRAY_FOR(mod_p->includes, v) {
2207 submod = mod_p->includes[v].submodule;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002208 LY_LIST_FOR(submod->augments, aug) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209 lysc_update_path(ctx, NULL, "{augment}");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002210 lysc_update_path(ctx, NULL, aug->nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002211
Radek Krejci2a9fc652021-01-22 17:44:34 +01002212 ret = lys_nodeid_check(ctx, aug->nodeid, 1, &mod, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002213 LY_CHECK_RET(ret);
2214
2215 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->augmented_by);
2216 if (mod != ctx->cur_mod) {
2217 flags = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002218 ret = lysc_resolve_schema_nodeid(ctx, aug->nodeid, 0, NULL, ctx->cur_mod, LY_PREF_SCHEMA,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002219 submod, 0, &target, &flags);
2220 LY_CHECK_RET(ret);
2221
2222 ctx->options |= flags;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002223 ret = lys_compile_augment(ctx, aug, (struct lysc_node *)target);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002224 ctx->options = opt_prev;
2225 LY_CHECK_RET(ret);
2226 }
2227
2228 lysc_update_path(ctx, NULL, NULL);
2229 lysc_update_path(ctx, NULL, NULL);
2230 }
2231
2232 LY_ARRAY_FOR(submod->deviations, u) {
2233 lysc_update_path(ctx, NULL, "{deviation}");
2234 lysc_update_path(ctx, NULL, submod->deviations[u].nodeid);
2235 ret = lys_nodeid_check(ctx, submod->deviations[u].nodeid, 1, &mod, NULL);
2236 lysc_update_path(ctx, NULL, NULL);
2237 lysc_update_path(ctx, NULL, NULL);
2238 LY_CHECK_RET(ret);
2239
2240 lys_array_add_mod_ref(ctx, ctx->cur_mod, &mod->deviated_by);
2241 has_dev = 1;
2242 }
2243 }
2244
Michal Vasko405cc9e2020-12-01 12:01:27 +01002245 if (has_dev) {
2246 /* all modules (may) need to be recompiled */
2247 ctx->unres->recompile = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002248 }
2249
Michal Vasko405cc9e2020-12-01 12:01:27 +01002250 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002251}
2252
2253void
2254lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
2255{
2256 uint32_t i;
2257 LY_ARRAY_COUNT_TYPE u, count;
2258 struct lys_module *m;
2259
2260 for (i = 0; i < ctx->list.count; ++i) {
2261 m = ctx->list.objs[i];
2262
2263 if (m->augmented_by) {
2264 count = LY_ARRAY_COUNT(m->augmented_by);
2265 for (u = 0; u < count; ++u) {
2266 if (m->augmented_by[u] == mod) {
2267 /* keep the order */
2268 if (u < count - 1) {
2269 memmove(m->augmented_by + u, m->augmented_by + u + 1, (count - u) * sizeof *m->augmented_by);
2270 }
2271 LY_ARRAY_DECREMENT(m->augmented_by);
2272 break;
2273 }
2274 }
2275 if (!LY_ARRAY_COUNT(m->augmented_by)) {
2276 LY_ARRAY_FREE(m->augmented_by);
2277 m->augmented_by = NULL;
2278 }
2279 }
2280
2281 if (m->deviated_by) {
2282 count = LY_ARRAY_COUNT(m->deviated_by);
2283 for (u = 0; u < count; ++u) {
2284 if (m->deviated_by[u] == mod) {
2285 /* keep the order */
2286 if (u < count - 1) {
2287 memmove(m->deviated_by + u, m->deviated_by + u + 1, (count - u) * sizeof *m->deviated_by);
2288 }
2289 LY_ARRAY_DECREMENT(m->deviated_by);
2290 break;
2291 }
2292 }
2293 if (!LY_ARRAY_COUNT(m->deviated_by)) {
2294 LY_ARRAY_FREE(m->deviated_by);
2295 m->deviated_by = NULL;
2296 }
2297 }
2298 }
2299}