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