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> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 5 | * @brief libyang extension plugin - yang-data (RFC 8040) |
| 6 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 7 | * Copyright (c) 2021 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 16 | #include <assert.h> |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 17 | #include <stdint.h> |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 18 | #include <stdlib.h> |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 19 | #include <string.h> |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 20 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 21 | #include "compat.h" |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 22 | #include "libyang.h" |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 23 | #include "plugins_exts.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 24 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 25 | static void yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext); |
| 26 | |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 27 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 28 | * @brief Parse yang-data extension instances. |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 29 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 30 | * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 31 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 32 | static LY_ERR |
| 33 | yangdata_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 34 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 35 | LY_ERR ret; |
| 36 | LY_ARRAY_COUNT_TYPE u; |
| 37 | struct lysp_module *pmod; |
| 38 | |
| 39 | /* yang-data can appear only at the top level of a YANG module or submodule */ |
| 40 | if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) { |
| 41 | lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is ignored since it appears as a non top-level statement " |
| 42 | "in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt)); |
| 43 | return LY_ENOT; |
| 44 | } |
| 45 | |
| 46 | pmod = ext->parent; |
| 47 | |
| 48 | /* check for duplication */ |
| 49 | LY_ARRAY_FOR(pmod->exts, u) { |
| 50 | if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) { |
| 51 | /* duplication of the same yang-data extension in a single module */ |
| 52 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name); |
| 53 | return LY_EVALID; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* parse yang-data substatements */ |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 58 | LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 3, ret, emem); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 59 | LY_ARRAY_INCREMENT(ext->substmts); |
| 60 | ext->substmts[0].stmt = LY_STMT_CONTAINER; |
| 61 | ext->substmts[0].storage = &ext->parsed; |
| 62 | |
| 63 | LY_ARRAY_INCREMENT(ext->substmts); |
| 64 | ext->substmts[1].stmt = LY_STMT_CHOICE; |
| 65 | ext->substmts[1].storage = &ext->parsed; |
| 66 | |
| 67 | LY_ARRAY_INCREMENT(ext->substmts); |
| 68 | ext->substmts[2].stmt = LY_STMT_USES; |
| 69 | ext->substmts[2].storage = &ext->parsed; |
| 70 | |
| 71 | if ((ret = lyplg_ext_parse_extension_instance(pctx, ext))) { |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | return LY_SUCCESS; |
| 76 | |
| 77 | emem: |
| 78 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); |
| 79 | return LY_EMEM; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @brief Compile yang-data extension instances. |
| 84 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 85 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 86 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 87 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 88 | yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext) |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 89 | { |
| 90 | LY_ERR ret; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 91 | const struct lysc_node *child; |
| 92 | ly_bool valid = 1; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 93 | uint32_t prev_options = *lyplg_ext_compile_get_options(cctx); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 94 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 95 | /* compile yangg-data substatements */ |
| 96 | LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 3, ret, emem); |
| 97 | LY_ARRAY_INCREMENT(ext->substmts); |
| 98 | ext->substmts[0].stmt = LY_STMT_CONTAINER; |
| 99 | ext->substmts[0].storage = &ext->compiled; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 100 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 101 | LY_ARRAY_INCREMENT(ext->substmts); |
| 102 | ext->substmts[1].stmt = LY_STMT_CHOICE; |
| 103 | ext->substmts[1].storage = &ext->compiled; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 104 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 105 | LY_ARRAY_INCREMENT(ext->substmts); |
| 106 | ext->substmts[2].stmt = LY_STMT_USES; |
| 107 | ext->substmts[2].storage = &ext->compiled; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 108 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 109 | *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED; |
| 110 | ret = lyplg_ext_compile_extension_instance(cctx, extp, ext); |
| 111 | *lyplg_ext_compile_get_options(cctx) = prev_options; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 112 | if (ret) { |
| 113 | return ret; |
| 114 | } |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 115 | |
| 116 | /* check that we have really just a single container data definition in the top */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 117 | child = ext->compiled; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 118 | if (!child) { |
| 119 | valid = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 120 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 121 | "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.", |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 122 | extp->name); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 123 | } else if (child->next) { |
| 124 | valid = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 125 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 126 | "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.", |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 127 | extp->name); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 128 | } else if (child->nodetype == LYS_CHOICE) { |
| 129 | /* all the choice's case are expected to result to a single container node */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 130 | struct lysc_module *mod_c = ext->parent; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 131 | const struct lysc_node *snode = NULL; |
| 132 | |
| 133 | while ((snode = lys_getnext(snode, child, mod_c, 0))) { |
| 134 | if (snode->next) { |
| 135 | valid = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 136 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 137 | "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), " |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 138 | "but only a single container data node is allowed.", extp->name); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 139 | break; |
| 140 | } else if (snode->nodetype != LYS_CONTAINER) { |
| 141 | valid = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 142 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 143 | "Extension %s is instantiated with %s top level data node (inside a choice), " |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 144 | "but only a single container data node is allowed.", extp->name, lys_nodetype2str(snode->nodetype)); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } else if (child->nodetype != LYS_CONTAINER) { |
| 149 | /* via uses */ |
| 150 | valid = 0; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 151 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 152 | "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.", |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 153 | extp->name, lys_nodetype2str(child->nodetype)); |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (!valid) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 157 | yangdata_cfree(lyplg_ext_compile_get_ctx(cctx), ext); |
| 158 | ext->compiled = ext->substmts = NULL; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 159 | return LY_EVALID; |
| 160 | } |
| 161 | |
| 162 | return LY_SUCCESS; |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 163 | |
| 164 | emem: |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 165 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__); |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 166 | return LY_EMEM; |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 170 | * @brief INFO printer |
| 171 | * |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 172 | * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 173 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 174 | static LY_ERR |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 175 | yangdata_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag) |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 176 | { |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 177 | lyplg_ext_print_info_extension_instance(ctx, ext, flag); |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 178 | return LY_SUCCESS; |
| 179 | } |
| 180 | |
| 181 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 182 | * @brief Free parsed yang-data extension instance data. |
| 183 | * |
| 184 | * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree. |
| 185 | */ |
| 186 | static void |
| 187 | yangdata_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext) |
| 188 | { |
| 189 | lyplg_ext_pfree_instance_substatements(ctx, ext->substmts); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @brief Free compiled yang-data extension instance data. |
| 194 | * |
| 195 | * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree. |
| 196 | */ |
| 197 | static void |
| 198 | yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 199 | { |
| 200 | lyplg_ext_cfree_instance_substatements(ctx, ext->substmts); |
| 201 | } |
| 202 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 203 | static void |
| 204 | yangdata_sprinter_node(uint16_t nodetype, const char **flags) |
| 205 | { |
| 206 | if (nodetype & LYS_USES) { |
| 207 | *flags = "-u"; |
| 208 | } else { |
| 209 | *flags = "--"; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static LY_ERR |
| 214 | yangdata_sprinter_cnode(const struct lysc_node *node, const void *UNUSED(plugin_priv), ly_bool *UNUSED(skip), |
| 215 | const char **flags, const char **UNUSED(add_opts)) |
| 216 | { |
| 217 | yangdata_sprinter_node(node->nodetype, flags); |
| 218 | return LY_SUCCESS; |
| 219 | } |
| 220 | |
| 221 | static LY_ERR |
| 222 | yangdata_sprinter_pnode(const struct lysp_node *node, const void *UNUSED(plugin_priv), ly_bool *UNUSED(skip), |
| 223 | const char **flags, const char **UNUSED(add_opts)) |
| 224 | { |
| 225 | yangdata_sprinter_node(node->nodetype, flags); |
| 226 | return LY_SUCCESS; |
| 227 | } |
| 228 | |
| 229 | static LY_ERR |
| 230 | yangdata_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 231 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 232 | { |
| 233 | LY_ERR rc = LY_SUCCESS; |
| 234 | |
| 235 | assert(ctx); |
| 236 | rc = lyplg_ext_sprinter_ctree_add_ext_nodes(ctx, ext, yangdata_sprinter_cnode); |
| 237 | return rc; |
| 238 | } |
| 239 | |
| 240 | static LY_ERR |
| 241 | yangdata_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 242 | const char **UNUSED(flags), const char **UNUSED(add_opts)) |
| 243 | { |
| 244 | LY_ERR rc = LY_SUCCESS; |
| 245 | |
| 246 | assert(ctx); |
| 247 | rc = lyplg_ext_sprinter_ptree_add_ext_nodes(ctx, ext, yangdata_sprinter_pnode); |
| 248 | return rc; |
| 249 | } |
| 250 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 251 | /** |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 252 | * @brief Plugin descriptions for the yang-data extension |
Radek Krejci | a6f61e7 | 2021-03-24 21:00:19 +0100 | [diff] [blame] | 253 | * |
| 254 | * Note that external plugins are supposed to use: |
| 255 | * |
| 256 | * LYPLG_EXTENSIONS = { |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 257 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 258 | const struct lyplg_ext_record plugins_yangdata[] = { |
| 259 | { |
| 260 | .module = "ietf-restconf", |
| 261 | .revision = "2017-01-26", |
| 262 | .name = "yang-data", |
| 263 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 264 | .plugin.id = "ly2 yang-data v1", |
| 265 | .plugin.parse = yangdata_parse, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 266 | .plugin.compile = yangdata_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 267 | .plugin.printer_info = yangdata_printer_info, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 268 | .plugin.printer_ctree = yangdata_sprinter_ctree, |
| 269 | .plugin.printer_ptree = yangdata_sprinter_ptree, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 270 | .plugin.node = NULL, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 271 | .plugin.snode = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 272 | .plugin.validate = NULL, |
| 273 | .plugin.pfree = yangdata_pfree, |
| 274 | .plugin.cfree = yangdata_cfree |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 275 | }, |
| 276 | {0} /* terminating zeroed record */ |
Radek Krejci | 5fa32a3 | 2021-02-08 18:12:38 +0100 | [diff] [blame] | 277 | }; |