Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file yangdata.c |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 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 | |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 15 | #include <stdint.h> |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 16 | #include <stdlib.h> |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 17 | #include <string.h> |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 18 | |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 19 | #include "libyang.h" |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 20 | #include "plugins_exts.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 21 | |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 22 | /** |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 23 | * @brief Free yang-data extension instances' data. |
| 24 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 25 | * Implementation of ::lyplg_clb_free_clb callback set as lyext_plugin::free. |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 26 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 27 | static void |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 28 | yangdata_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 29 | { |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 30 | lyplg_ext_instance_substatements_free(ctx, ext->substmts); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @brief Compile yang-data extension instances. |
| 35 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 36 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 37 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 38 | static LY_ERR |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 39 | yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 40 | { |
| 41 | LY_ERR ret; |
| 42 | LY_ARRAY_COUNT_TYPE u; |
| 43 | struct lysc_module *mod_c; |
| 44 | const struct lysc_node *child; |
| 45 | ly_bool valid = 1; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 46 | uint32_t prev_options = *lysc_ctx_get_options(cctx); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 47 | |
| 48 | /* 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] | 49 | if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) { |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 50 | lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 51 | "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] | 52 | p_ext->name, ly_stmt2str(c_ext->parent_stmt)); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 53 | return LY_ENOT; |
| 54 | } |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 55 | |
| 56 | mod_c = (struct lysc_module *)c_ext->parent; |
| 57 | |
| 58 | /* check for duplication */ |
| 59 | LY_ARRAY_FOR(mod_c->exts, u) { |
| 60 | if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) { |
| 61 | /* duplication of the same yang-data extension in a single module */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 62 | lyplg_ext_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] | 63 | return LY_EVALID; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* compile annotation substatements |
| 68 | * To let the compilation accept different statements possibly leading to the container top-level node, there are 3 |
| 69 | * allowed substatements pointing to a single storage. But when compiled, the substaments list is compressed just to |
| 70 | * a single item providing the schema tree. */ |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 71 | LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 3, ret, emem); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 72 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 73 | c_ext->substmts[0].stmt = LY_STMT_CONTAINER; |
| 74 | c_ext->substmts[0].cardinality = LY_STMT_CARD_OPT; |
| 75 | c_ext->substmts[0].storage = &c_ext->data; |
| 76 | |
| 77 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 78 | c_ext->substmts[1].stmt = LY_STMT_CHOICE; |
| 79 | c_ext->substmts[1].cardinality = LY_STMT_CARD_OPT; |
| 80 | c_ext->substmts[1].storage = &c_ext->data; |
| 81 | |
| 82 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 83 | c_ext->substmts[2].stmt = LY_STMT_USES; |
| 84 | c_ext->substmts[2].cardinality = LY_STMT_CARD_OPT; |
| 85 | c_ext->substmts[2].storage = &c_ext->data; |
| 86 | |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 87 | *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] | 88 | ret = lys_compile_extension_instance(cctx, p_ext, c_ext); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 89 | *lysc_ctx_get_options(cctx) = prev_options; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 90 | LY_ARRAY_DECREMENT(c_ext->substmts); |
| 91 | LY_ARRAY_DECREMENT(c_ext->substmts); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 92 | if (ret) { |
| 93 | return ret; |
| 94 | } |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 95 | |
| 96 | /* check that we have really just a single container data definition in the top */ |
| 97 | child = *(struct lysc_node **)c_ext->substmts[0].storage; |
| 98 | if (!child) { |
| 99 | valid = 0; |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 100 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 101 | "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.", |
| 102 | p_ext->name); |
| 103 | } else if (child->next) { |
| 104 | valid = 0; |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 105 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 106 | "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.", |
| 107 | p_ext->name); |
| 108 | } else if (child->nodetype == LYS_CHOICE) { |
| 109 | /* all the choice's case are expected to result to a single container node */ |
| 110 | const struct lysc_node *snode = NULL; |
| 111 | |
| 112 | while ((snode = lys_getnext(snode, child, mod_c, 0))) { |
| 113 | if (snode->next) { |
| 114 | valid = 0; |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 115 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 116 | "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), " |
| 117 | "but only a single container data node is allowed.", p_ext->name); |
| 118 | break; |
| 119 | } else if (snode->nodetype != LYS_CONTAINER) { |
| 120 | valid = 0; |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 121 | lyplg_ext_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 %s top level data node (inside a choice), " |
| 123 | "but only a single container data node is allowed.", p_ext->name, lys_nodetype2str(snode->nodetype)); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | } else if (child->nodetype != LYS_CONTAINER) { |
| 128 | /* via uses */ |
| 129 | valid = 0; |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 130 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 131 | "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.", |
| 132 | p_ext->name, lys_nodetype2str(child->nodetype)); |
| 133 | } |
| 134 | |
| 135 | if (!valid) { |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 136 | yangdata_free(lysc_ctx_get_ctx(cctx), c_ext); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 137 | c_ext->data = c_ext->substmts = NULL; |
| 138 | return LY_EVALID; |
| 139 | } |
| 140 | |
| 141 | return LY_SUCCESS; |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 142 | |
| 143 | emem: |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 144 | lyplg_ext_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] | 145 | return LY_EMEM; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 149 | * @brief INFO printer |
| 150 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 151 | * Implementation of ::lyplg_ext_schema_printer_clb set as ::lyext_plugin::sprinter |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 152 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 153 | static LY_ERR |
Radek Krejci | f8d7f9a | 2021-03-10 14:32:36 +0100 | [diff] [blame] | 154 | 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] | 155 | { |
| 156 | lysc_print_extension_instance(ctx, ext, flag); |
| 157 | return LY_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | /** |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 161 | * @brief Plugin descriptions for the yang-data extension |
Radek Krejci | a6f61e7 | 2021-03-24 21:00:19 +0100 | [diff] [blame] | 162 | * |
| 163 | * Note that external plugins are supposed to use: |
| 164 | * |
| 165 | * LYPLG_EXTENSIONS = { |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 166 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 167 | const struct lyplg_ext_record plugins_yangdata[] = { |
| 168 | { |
| 169 | .module = "ietf-restconf", |
| 170 | .revision = "2017-01-26", |
| 171 | .name = "yang-data", |
| 172 | |
| 173 | .plugin.id = "libyang 2 - yang-data, version 1", |
| 174 | .plugin.compile = &yangdata_compile, |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 175 | .plugin.parse = NULL, |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 176 | .plugin.validate = NULL, |
| 177 | .plugin.sprinter = &yangdata_schema_printer, |
| 178 | .plugin.free = yangdata_free |
| 179 | }, |
| 180 | {0} /* terminating zeroed record */ |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 181 | }; |