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" |
| 19 | #include "tree_schema.h" |
| 20 | #include "plugins_exts_metadata.h" |
| 21 | |
| 22 | /** |
| 23 | * @brief Storage for ID used to check plugin API version compatibility. |
| 24 | * Ignored here in the internal plugin. |
| 25 | LYEXT_VERSION_CHECK |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * @brief Compile annotation extension instances. |
| 30 | * |
| 31 | * Implementation of lyext_clb_compile callback set as lyext_plugin::compile. |
| 32 | */ |
| 33 | LY_ERR |
| 34 | annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext) |
| 35 | { |
| 36 | struct lyext_metadata *annotation; |
| 37 | struct lysc_module *mod_c; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 38 | unsigned int u; |
| 39 | struct lysc_ext_substmt annotation_substmt[7] = { |
| 40 | {LY_STMT_IF_FEATURE, LY_STMT_CARD_ANY, NULL}, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 41 | {LY_STMT_UNITS, LY_STMT_CARD_OPT, NULL}, |
| 42 | {LY_STMT_STATUS, LY_STMT_CARD_OPT, NULL}, |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 43 | {LY_STMT_TYPE, LY_STMT_CARD_MAND, NULL}, |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 44 | {LY_STMT_DESCRIPTION, LY_STMT_CARD_OPT, NULL}, |
| 45 | {LY_STMT_REFERENCE, LY_STMT_CARD_OPT, NULL}, |
| 46 | {0, 0, 0} /* terminating item */ |
| 47 | }; |
| 48 | |
| 49 | /* annotations can appear only at the top level of a YANG module or submodule */ |
| 50 | if (c_ext->parent_type != LYEXT_PAR_MODULE) { |
| 51 | 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.", |
| 52 | p_ext->name, lyext_parent2str(c_ext->parent_type)); |
| 53 | return LY_EVALID; |
| 54 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 55 | /* check mandatory argument */ |
| 56 | if (!c_ext->argument) { |
| 57 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated without mandatory argument representing metadata name.", |
| 58 | p_ext->name); |
| 59 | return LY_EVALID; |
| 60 | } |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 61 | |
| 62 | mod_c = (struct lysc_module *)c_ext->parent; |
| 63 | |
| 64 | /* check for duplication */ |
| 65 | LY_ARRAY_FOR(mod_c->exts, u) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 66 | if (&mod_c->exts[u] != c_ext && mod_c->exts[u].def == c_ext->def && !strcmp(mod_c->exts[u].argument, c_ext->argument)) { |
| 67 | /* duplication of the same annotation extension in a single module */ |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 68 | lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name); |
| 69 | return LY_EVALID; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* compile annotation substatements */ |
| 74 | c_ext->data = annotation = calloc(1, sizeof *annotation); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 75 | LY_CHECK_ERR_RET(!annotation, LOGMEM(cctx->ctx), LY_EMEM); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 76 | annotation_substmt[0].storage = &annotation->iffeatures; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 77 | annotation_substmt[1].storage = &annotation->units; |
| 78 | annotation_substmt[2].storage = &annotation->flags; |
| 79 | annotation_substmt[3].storage = &annotation->type; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 80 | /* description and reference are allowed, but not compiled */ |
| 81 | |
| 82 | LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, annotation_substmt)); |
| 83 | |
| 84 | return LY_SUCCESS; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * @brief Plugin for the Metadata's annotation extension |
| 90 | */ |
| 91 | struct lyext_plugin metadata_plugin = { |
| 92 | .id = "libyang 2 - metadata, version 1", |
| 93 | .compile = &annotation_compile, |
| 94 | .validate = NULL, |
| 95 | .free = NULL |
| 96 | }; |