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