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 | |
| 17 | #include "common.h" |
| 18 | #include "plugins_exts.h" |
| 19 | #include "schema_compile.h" |
| 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; |
| 52 | uint32_t prev_options = cctx->options; |
| 53 | |
| 54 | /* yang-data can appear only at the top level of a YANG module or submodule */ |
| 55 | if (c_ext->parent_type != LYEXT_PAR_MODULE) { |
| 56 | lyext_log(c_ext, LY_LLWRN, 0, cctx->path, |
| 57 | "Extension %s is ignored since it appears as a non top-level statement in \"%s\" statement.", |
| 58 | p_ext->name, lyext_parent2str(c_ext->parent_type)); |
| 59 | return LY_ENOT; |
| 60 | } |
| 61 | /* check mandatory argument */ |
| 62 | if (!c_ext->argument) { |
| 63 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, |
| 64 | "Extension %s is instantiated without mandatory argument representing YANG data template name.", |
| 65 | p_ext->name); |
| 66 | return LY_EVALID; |
| 67 | } |
| 68 | |
| 69 | mod_c = (struct lysc_module *)c_ext->parent; |
| 70 | |
| 71 | /* check for duplication */ |
| 72 | LY_ARRAY_FOR(mod_c->exts, u) { |
| 73 | if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) { |
| 74 | /* duplication of the same yang-data extension in a single module */ |
| 75 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name); |
| 76 | return LY_EVALID; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* compile annotation substatements |
| 81 | * To let the compilation accept different statements possibly leading to the container top-level node, there are 3 |
| 82 | * allowed substatements pointing to a single storage. But when compiled, the substaments list is compressed just to |
| 83 | * a single item providing the schema tree. */ |
| 84 | LY_ARRAY_CREATE_RET(cctx->ctx, c_ext->substmts, 3, LY_EMEM); |
| 85 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 86 | c_ext->substmts[0].stmt = LY_STMT_CONTAINER; |
| 87 | c_ext->substmts[0].cardinality = LY_STMT_CARD_OPT; |
| 88 | c_ext->substmts[0].storage = &c_ext->data; |
| 89 | |
| 90 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 91 | c_ext->substmts[1].stmt = LY_STMT_CHOICE; |
| 92 | c_ext->substmts[1].cardinality = LY_STMT_CARD_OPT; |
| 93 | c_ext->substmts[1].storage = &c_ext->data; |
| 94 | |
| 95 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 96 | c_ext->substmts[2].stmt = LY_STMT_USES; |
| 97 | c_ext->substmts[2].cardinality = LY_STMT_CARD_OPT; |
| 98 | c_ext->substmts[2].storage = &c_ext->data; |
| 99 | |
| 100 | cctx->options |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED; |
| 101 | ret = lys_compile_extension_instance(cctx, p_ext, c_ext); |
| 102 | cctx->options = prev_options; |
| 103 | LY_ARRAY_DECREMENT(c_ext->substmts); |
| 104 | LY_ARRAY_DECREMENT(c_ext->substmts); |
| 105 | LY_CHECK_RET(ret); |
| 106 | |
| 107 | /* check that we have really just a single container data definition in the top */ |
| 108 | child = *(struct lysc_node **)c_ext->substmts[0].storage; |
| 109 | if (!child) { |
| 110 | valid = 0; |
| 111 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, |
| 112 | "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.", |
| 113 | p_ext->name); |
| 114 | } else if (child->next) { |
| 115 | valid = 0; |
| 116 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, |
| 117 | "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.", |
| 118 | p_ext->name); |
| 119 | } else if (child->nodetype == LYS_CHOICE) { |
| 120 | /* all the choice's case are expected to result to a single container node */ |
| 121 | const struct lysc_node *snode = NULL; |
| 122 | |
| 123 | while ((snode = lys_getnext(snode, child, mod_c, 0))) { |
| 124 | if (snode->next) { |
| 125 | valid = 0; |
| 126 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, |
| 127 | "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), " |
| 128 | "but only a single container data node is allowed.", p_ext->name); |
| 129 | break; |
| 130 | } else if (snode->nodetype != LYS_CONTAINER) { |
| 131 | valid = 0; |
| 132 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, |
| 133 | "Extension %s is instantiated with %s top level data node (inside a choice), " |
| 134 | "but only a single container data node is allowed.", p_ext->name, lys_nodetype2str(snode->nodetype)); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } else if (child->nodetype != LYS_CONTAINER) { |
| 139 | /* via uses */ |
| 140 | valid = 0; |
| 141 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, |
| 142 | "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.", |
| 143 | p_ext->name, lys_nodetype2str(child->nodetype)); |
| 144 | } |
| 145 | |
| 146 | if (!valid) { |
| 147 | yangdata_free(cctx->ctx, c_ext); |
| 148 | c_ext->data = c_ext->substmts = NULL; |
| 149 | return LY_EVALID; |
| 150 | } |
| 151 | |
| 152 | return LY_SUCCESS; |
| 153 | } |
| 154 | |
| 155 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 156 | * @brief INFO printer |
| 157 | * |
| 158 | * Implementation of ::lyext_clb_schema_printer set as ::lyext_plugin::sprinter |
| 159 | */ |
| 160 | LY_ERR |
| 161 | yangdata_schema_printer(struct lys_ypr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag) |
| 162 | { |
| 163 | lysc_print_extension_instance(ctx, ext, flag); |
| 164 | return LY_SUCCESS; |
| 165 | } |
| 166 | |
| 167 | /** |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 168 | * @brief Plugin for the yang-data extension |
| 169 | */ |
| 170 | struct lyext_plugin yangdata_plugin = { |
| 171 | .id = "libyang 2 - yang-data, version 1", |
| 172 | .compile = &yangdata_compile, |
| 173 | .validate = NULL, |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 174 | .sprinter = &yangdata_schema_printer, |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 175 | .free = yangdata_free |
| 176 | }; |