Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file plugins_exts_nacm.c |
| 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 | */ |
| 14 | #include "common.h" |
| 15 | |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include "plugins_exts.h" |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 19 | #include "plugins_exts_metadata.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 20 | #include "schema_compile.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 21 | #include "tree_schema.h" |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 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 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 29 | #define ANNOTATION_SUBSTMT_IFF 0 |
| 30 | #define ANNOTATION_SUBSTMT_UNITS 1 |
| 31 | #define ANNOTATION_SUBSTMT_STATUS 2 |
| 32 | #define ANNOTATION_SUBSTMT_TYPE 3 |
| 33 | #define ANNOTATION_SUBSTMT_DSC 4 |
| 34 | #define ANNOTATION_SUBSTMT_REF 5 |
| 35 | |
Radek Krejci | 5a443cf | 2021-01-21 12:55:45 +0100 | [diff] [blame] | 36 | #define INIT_ANNOTATION_SUBSTMT { \ |
| 37 | {LY_STMT_IF_FEATURE, LY_STMT_CARD_ANY, NULL}, \ |
| 38 | {LY_STMT_UNITS, LY_STMT_CARD_OPT, NULL}, \ |
| 39 | {LY_STMT_STATUS, LY_STMT_CARD_OPT, NULL}, \ |
| 40 | {LY_STMT_TYPE, LY_STMT_CARD_MAND, NULL}, \ |
| 41 | {LY_STMT_DESCRIPTION, LY_STMT_CARD_OPT, NULL}, \ |
| 42 | {LY_STMT_REFERENCE, LY_STMT_CARD_OPT, NULL}, \ |
| 43 | {0, 0, 0} /* terminating item */ \ |
| 44 | } \ |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 45 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 46 | /** |
| 47 | * @brief Compile annotation extension instances. |
| 48 | * |
| 49 | * Implementation of lyext_clb_compile callback set as lyext_plugin::compile. |
| 50 | */ |
| 51 | LY_ERR |
| 52 | annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 53 | { |
| 54 | struct lyext_metadata *annotation; |
| 55 | struct lysc_module *mod_c; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 56 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 5a443cf | 2021-01-21 12:55:45 +0100 | [diff] [blame] | 57 | struct lysc_ext_substmt annotation_substmt[] = INIT_ANNOTATION_SUBSTMT; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 58 | |
| 59 | /* annotations can appear only at the top level of a YANG module or submodule */ |
| 60 | if (c_ext->parent_type != LYEXT_PAR_MODULE) { |
| 61 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 62 | p_ext->name, lyext_parent2str(c_ext->parent_type)); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 63 | return LY_EVALID; |
| 64 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 65 | /* check mandatory argument */ |
| 66 | if (!c_ext->argument) { |
| 67 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated without mandatory argument representing metadata name.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 68 | p_ext->name); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 69 | return LY_EVALID; |
| 70 | } |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 71 | |
| 72 | mod_c = (struct lysc_module *)c_ext->parent; |
| 73 | |
| 74 | /* check for duplication */ |
| 75 | LY_ARRAY_FOR(mod_c->exts, u) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 76 | 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] | 77 | /* duplication of the same annotation extension in a single module */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 78 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name); |
| 79 | return LY_EVALID; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* compile annotation substatements */ |
| 84 | c_ext->data = annotation = calloc(1, sizeof *annotation); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 85 | LY_CHECK_ERR_RET(!annotation, LOGMEM(cctx->ctx), LY_EMEM); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 86 | annotation_substmt[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures; |
| 87 | annotation_substmt[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units; |
| 88 | annotation_substmt[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags; |
| 89 | annotation_substmt[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 90 | /* description and reference are allowed, but not compiled */ |
| 91 | |
| 92 | LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, annotation_substmt)); |
| 93 | |
| 94 | return LY_SUCCESS; |
| 95 | } |
| 96 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 97 | /** |
| 98 | * @brief Free annotation extension instances' data. |
| 99 | * |
| 100 | * Implementation of lyext_clb_free callback set as lyext_plugin::free. |
| 101 | */ |
| 102 | void |
| 103 | annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
| 104 | { |
Radek Krejci | 5a443cf | 2021-01-21 12:55:45 +0100 | [diff] [blame] | 105 | struct lysc_ext_substmt annotation_substmt[] = INIT_ANNOTATION_SUBSTMT; |
| 106 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 107 | if (!ext->data) { |
| 108 | return; |
| 109 | } |
| 110 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 111 | struct lyext_metadata *annotation = (struct lyext_metadata *)ext->data; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 112 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 113 | annotation_substmt[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures; |
| 114 | annotation_substmt[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units; |
| 115 | annotation_substmt[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags; |
| 116 | annotation_substmt[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 117 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 118 | lysc_extension_instance_free(ctx, annotation_substmt); |
| 119 | free(ext->data); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 120 | } |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * @brief Plugin for the Metadata's annotation extension |
| 124 | */ |
| 125 | struct lyext_plugin metadata_plugin = { |
| 126 | .id = "libyang 2 - metadata, version 1", |
| 127 | .compile = &annotation_compile, |
| 128 | .validate = NULL, |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 129 | .free = annotation_free |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 130 | }; |