Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file plugins_exts_yangdata.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang extension plugin - yang-data (RFC 8040) |
| 5 | * |
| 6 | * Copyright (c) 2021 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 | #include <stdlib.h> |
| 16 | |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 17 | #include "plugins_exts.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 18 | |
| 19 | #include "tree_edit.h" |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 20 | #include "tree_schema.h" |
| 21 | |
| 22 | /** |
| 23 | * @brief Storage for ID used to check plugin API version compatibility. |
| 24 | * Ignored here in the internal plugin. |
| 25 | LYEXT_VERSION_CHECK |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * @brief Free yang-data extension instances' data. |
| 30 | * |
| 31 | * Implementation of ::lyext_clb_free callback set as lyext_plugin::free. |
| 32 | */ |
| 33 | void |
| 34 | yangdata_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 35 | { |
| 36 | lysc_extension_instance_substatements_free(ctx, ext->substmts); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @brief Compile yang-data extension instances. |
| 41 | * |
| 42 | * Implementation of lyext_clb_compile callback set as lyext_plugin::compile. |
| 43 | */ |
| 44 | LY_ERR |
| 45 | yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 46 | { |
| 47 | LY_ERR ret; |
| 48 | LY_ARRAY_COUNT_TYPE u; |
| 49 | struct lysc_module *mod_c; |
| 50 | const struct lysc_node *child; |
| 51 | ly_bool valid = 1; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 52 | uint32_t prev_options = *lysc_ctx_get_options(cctx); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 53 | |
| 54 | /* yang-data can appear only at the top level of a YANG module or submodule */ |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 55 | if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) { |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 56 | lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 57 | "Extension %s is ignored since it appears as a non top-level statement in \"%s\" statement.", |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 58 | p_ext->name, ly_stmt2str(c_ext->parent_stmt)); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 59 | return LY_ENOT; |
| 60 | } |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 61 | |
| 62 | mod_c = (struct lysc_module *)c_ext->parent; |
| 63 | |
| 64 | /* check for duplication */ |
| 65 | LY_ARRAY_FOR(mod_c->exts, u) { |
| 66 | if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) { |
| 67 | /* duplication of the same yang-data extension in a single module */ |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 68 | lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 69 | return LY_EVALID; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* compile annotation substatements |
| 74 | * To let the compilation accept different statements possibly leading to the container top-level node, there are 3 |
| 75 | * allowed substatements pointing to a single storage. But when compiled, the substaments list is compressed just to |
| 76 | * a single item providing the schema tree. */ |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 77 | LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 3, ret, emem); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 78 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 79 | c_ext->substmts[0].stmt = LY_STMT_CONTAINER; |
| 80 | c_ext->substmts[0].cardinality = LY_STMT_CARD_OPT; |
| 81 | c_ext->substmts[0].storage = &c_ext->data; |
| 82 | |
| 83 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 84 | c_ext->substmts[1].stmt = LY_STMT_CHOICE; |
| 85 | c_ext->substmts[1].cardinality = LY_STMT_CARD_OPT; |
| 86 | c_ext->substmts[1].storage = &c_ext->data; |
| 87 | |
| 88 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 89 | c_ext->substmts[2].stmt = LY_STMT_USES; |
| 90 | c_ext->substmts[2].cardinality = LY_STMT_CARD_OPT; |
| 91 | c_ext->substmts[2].storage = &c_ext->data; |
| 92 | |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 93 | *lysc_ctx_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 94 | ret = lys_compile_extension_instance(cctx, p_ext, c_ext); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 95 | *lysc_ctx_get_options(cctx) = prev_options; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 96 | LY_ARRAY_DECREMENT(c_ext->substmts); |
| 97 | LY_ARRAY_DECREMENT(c_ext->substmts); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 98 | if (ret) { |
| 99 | return ret; |
| 100 | } |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 101 | |
| 102 | /* check that we have really just a single container data definition in the top */ |
| 103 | child = *(struct lysc_node **)c_ext->substmts[0].storage; |
| 104 | if (!child) { |
| 105 | valid = 0; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 106 | lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 107 | "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.", |
| 108 | p_ext->name); |
| 109 | } else if (child->next) { |
| 110 | valid = 0; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 111 | lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 112 | "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.", |
| 113 | p_ext->name); |
| 114 | } else if (child->nodetype == LYS_CHOICE) { |
| 115 | /* all the choice's case are expected to result to a single container node */ |
| 116 | const struct lysc_node *snode = NULL; |
| 117 | |
| 118 | while ((snode = lys_getnext(snode, child, mod_c, 0))) { |
| 119 | if (snode->next) { |
| 120 | valid = 0; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 121 | lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 122 | "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), " |
| 123 | "but only a single container data node is allowed.", p_ext->name); |
| 124 | break; |
| 125 | } else if (snode->nodetype != LYS_CONTAINER) { |
| 126 | valid = 0; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 127 | lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 128 | "Extension %s is instantiated with %s top level data node (inside a choice), " |
| 129 | "but only a single container data node is allowed.", p_ext->name, lys_nodetype2str(snode->nodetype)); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } else if (child->nodetype != LYS_CONTAINER) { |
| 134 | /* via uses */ |
| 135 | valid = 0; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 136 | lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 137 | "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.", |
| 138 | p_ext->name, lys_nodetype2str(child->nodetype)); |
| 139 | } |
| 140 | |
| 141 | if (!valid) { |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 142 | yangdata_free(lysc_ctx_get_ctx(cctx), c_ext); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 143 | c_ext->data = c_ext->substmts = NULL; |
| 144 | return LY_EVALID; |
| 145 | } |
| 146 | |
| 147 | return LY_SUCCESS; |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 148 | |
| 149 | emem: |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 150 | lyext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__); |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 151 | return LY_EMEM; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 155 | * @brief INFO printer |
| 156 | * |
| 157 | * Implementation of ::lyext_clb_schema_printer set as ::lyext_plugin::sprinter |
| 158 | */ |
| 159 | LY_ERR |
Radek Krejci | f8d7f9a | 2021-03-10 14:32:36 +0100 | [diff] [blame] | 160 | yangdata_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag) |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 161 | { |
| 162 | lysc_print_extension_instance(ctx, ext, flag); |
| 163 | return LY_SUCCESS; |
| 164 | } |
| 165 | |
| 166 | /** |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 167 | * @brief Plugin for the yang-data extension |
| 168 | */ |
| 169 | struct lyext_plugin yangdata_plugin = { |
| 170 | .id = "libyang 2 - yang-data, version 1", |
| 171 | .compile = &yangdata_compile, |
| 172 | .validate = NULL, |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 173 | .sprinter = &yangdata_schema_printer, |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 174 | .free = yangdata_free |
| 175 | }; |