Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file metadata.c |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang extension plugin - Metadata (RFC 7952) |
| 5 | * |
| 6 | * Copyright (c) 2019 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 | */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 14 | |
Radek Krejci | f1ca0ac | 2021-04-12 16:00:06 +0200 | [diff] [blame] | 15 | #include "metadata.h" |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 16 | |
Radek Krejci | 883355a | 2021-03-11 11:54:41 +0100 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "libyang.h" |
| 22 | #include "plugins_exts.h" |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 23 | |
| 24 | /** |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 25 | * @brief Representation of the compiled metadata substatements - simplify storage for the items available via |
| 26 | * ::lysc_ext_substmt. |
| 27 | */ |
| 28 | struct lyext_metadata { |
| 29 | struct lysc_type *type; /**< type of the metadata (mandatory) */ |
| 30 | const char *units; /**< units of the leaf's type */ |
| 31 | struct lysc_iffeature *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */ |
| 32 | const char *dsc; /**< description */ |
| 33 | const char *ref; /**< reference */ |
| 34 | uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */ |
| 35 | }; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 36 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 37 | /** |
| 38 | * @brief Compile annotation extension instances. |
| 39 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 40 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 41 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 42 | static LY_ERR |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 43 | annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 44 | { |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 45 | LY_ERR ret; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 46 | struct lyext_metadata *annotation; |
| 47 | struct lysc_module *mod_c; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 48 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 49 | |
| 50 | /* annotations 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] | 51 | 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] | 52 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 53 | "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.", |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 54 | p_ext->name, ly_stmt2str(c_ext->parent_stmt)); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 55 | return LY_EVALID; |
| 56 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 57 | /* check mandatory argument */ |
| 58 | if (!c_ext->argument) { |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 59 | lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 60 | "Extension %s is instantiated without mandatory argument representing metadata name.", p_ext->name); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 61 | return LY_EVALID; |
| 62 | } |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 63 | |
| 64 | mod_c = (struct lysc_module *)c_ext->parent; |
| 65 | |
| 66 | /* check for duplication */ |
| 67 | LY_ARRAY_FOR(mod_c->exts, u) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 68 | if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 69 | /* duplication of the same annotation extension in a single module */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 70 | 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 | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 71 | return LY_EVALID; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* compile annotation substatements */ |
| 76 | c_ext->data = annotation = calloc(1, sizeof *annotation); |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 77 | if (!annotation) { |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 78 | goto emem; |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 79 | } |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 80 | LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), c_ext->substmts, 6, ret, emem); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 81 | |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 82 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 83 | c_ext->substmts[ANNOTATION_SUBSTMT_IFF].stmt = LY_STMT_IF_FEATURE; |
| 84 | c_ext->substmts[ANNOTATION_SUBSTMT_IFF].cardinality = LY_STMT_CARD_ANY; |
| 85 | c_ext->substmts[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures; |
| 86 | |
| 87 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 88 | c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].stmt = LY_STMT_UNITS; |
| 89 | c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].cardinality = LY_STMT_CARD_OPT; |
| 90 | c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units; |
| 91 | |
| 92 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 93 | c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].stmt = LY_STMT_STATUS; |
| 94 | c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].cardinality = LY_STMT_CARD_OPT; |
| 95 | c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags; |
| 96 | |
| 97 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 98 | c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].stmt = LY_STMT_TYPE; |
| 99 | c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].cardinality = LY_STMT_CARD_MAND; |
| 100 | c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type; |
| 101 | |
| 102 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 103 | c_ext->substmts[ANNOTATION_SUBSTMT_DSC].stmt = LY_STMT_DESCRIPTION; |
| 104 | c_ext->substmts[ANNOTATION_SUBSTMT_DSC].cardinality = LY_STMT_CARD_OPT; |
| 105 | c_ext->substmts[ANNOTATION_SUBSTMT_DSC].storage = &annotation->dsc; |
| 106 | |
| 107 | LY_ARRAY_INCREMENT(c_ext->substmts); |
| 108 | c_ext->substmts[ANNOTATION_SUBSTMT_REF].stmt = LY_STMT_REFERENCE; |
| 109 | c_ext->substmts[ANNOTATION_SUBSTMT_REF].cardinality = LY_STMT_CARD_OPT; |
| 110 | c_ext->substmts[ANNOTATION_SUBSTMT_REF].storage = &annotation->ref; |
| 111 | |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 112 | ret = lys_compile_extension_instance(cctx, p_ext, c_ext); |
| 113 | return ret; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 114 | |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 115 | emem: |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 116 | 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] | 117 | return LY_EMEM; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 120 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 121 | * @brief INFO printer |
| 122 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 123 | * Implementation of ::lyplg_ext_schema_printer_clb set as ::lyext_plugin::sprinter |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 124 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 125 | static LY_ERR |
Radek Krejci | f8d7f9a | 2021-03-10 14:32:36 +0100 | [diff] [blame] | 126 | annotation_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] | 127 | { |
| 128 | lysc_print_extension_instance(ctx, ext, flag); |
| 129 | |
| 130 | return LY_SUCCESS; |
| 131 | } |
| 132 | |
| 133 | /** |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 134 | * @brief Free annotation extension instances' data. |
| 135 | * |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 136 | * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free. |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 137 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 138 | static void |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 139 | annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 140 | { |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 141 | if (!ext->substmts) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 145 | lyplg_ext_instance_substatements_free(ctx, ext->substmts); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 146 | free(ext->data); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 147 | } |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 150 | * @brief Plugin descriptions for the Metadata's annotation extension |
Radek Krejci | a6f61e7 | 2021-03-24 21:00:19 +0100 | [diff] [blame] | 151 | * |
| 152 | * Note that external plugins are supposed to use: |
| 153 | * |
| 154 | * LYPLG_EXTENSIONS = { |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 155 | */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 156 | const struct lyplg_ext_record plugins_metadata[] = { |
| 157 | { |
| 158 | .module = "ietf-yang-metadata", |
| 159 | .revision = "2016-08-05", |
| 160 | .name = "annotation", |
| 161 | |
| 162 | .plugin.id = "libyang 2 - metadata, version 1", |
| 163 | .plugin.compile = &annotation_compile, |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 164 | .plugin.sprinter = &annotation_schema_printer, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame^] | 165 | .plugin.free = annotation_free, |
| 166 | .plugin.parse = NULL, |
| 167 | .plugin.validate = NULL |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 168 | }, |
| 169 | {0} /* terminating zeroed record */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 170 | }; |