blob: ceae257ad2d609043410f1ec84d56a4859cec713 [file] [log] [blame]
Radek Krejcid6b76452019-09-03 17:03:03 +02001/**
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 Krejcid6b76452019-09-03 17:03:03 +020019#include "plugins_exts_metadata.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020020#include "schema_compile.h"
Michal Vasko69730152020-10-09 16:30:07 +020021#include "tree_schema.h"
Radek Krejcid6b76452019-09-03 17:03:03 +020022
23/**
24 * @brief Storage for ID used to check plugin API version compatibility.
25 * Ignored here in the internal plugin.
26LYEXT_VERSION_CHECK
27 */
28
Radek Krejci38d85362019-09-05 16:26:38 +020029struct lysc_ext_substmt annotation_substmt[7] = {
30 {LY_STMT_IF_FEATURE, LY_STMT_CARD_ANY, NULL},
31 {LY_STMT_UNITS, LY_STMT_CARD_OPT, NULL},
32 {LY_STMT_STATUS, LY_STMT_CARD_OPT, NULL},
33 {LY_STMT_TYPE, LY_STMT_CARD_MAND, NULL},
34 {LY_STMT_DESCRIPTION, LY_STMT_CARD_OPT, NULL},
35 {LY_STMT_REFERENCE, LY_STMT_CARD_OPT, NULL},
36 {0, 0, 0} /* terminating item */
37};
38
Radek Krejcid6b76452019-09-03 17:03:03 +020039/**
40 * @brief Compile annotation extension instances.
41 *
42 * Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
43 */
44LY_ERR
45annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
46{
47 struct lyext_metadata *annotation;
48 struct lysc_module *mod_c;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020049 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid6b76452019-09-03 17:03:03 +020050
51 /* annotations can appear only at the top level of a YANG module or submodule */
52 if (c_ext->parent_type != LYEXT_PAR_MODULE) {
53 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 Vasko69730152020-10-09 16:30:07 +020054 p_ext->name, lyext_parent2str(c_ext->parent_type));
Radek Krejcid6b76452019-09-03 17:03:03 +020055 return LY_EVALID;
56 }
Radek Krejci335332a2019-09-05 13:03:35 +020057 /* check mandatory argument */
58 if (!c_ext->argument) {
59 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated without mandatory argument representing metadata name.",
Michal Vasko69730152020-10-09 16:30:07 +020060 p_ext->name);
Radek Krejci335332a2019-09-05 13:03:35 +020061 return LY_EVALID;
62 }
Radek Krejcid6b76452019-09-03 17:03:03 +020063
64 mod_c = (struct lysc_module *)c_ext->parent;
65
66 /* check for duplication */
67 LY_ARRAY_FOR(mod_c->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +020068 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 Krejci335332a2019-09-05 13:03:35 +020069 /* duplication of the same annotation extension in a single module */
Radek Krejcid6b76452019-09-03 17:03:03 +020070 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
71 return LY_EVALID;
72 }
73 }
74
75 /* compile annotation substatements */
76 c_ext->data = annotation = calloc(1, sizeof *annotation);
Radek Krejci335332a2019-09-05 13:03:35 +020077 LY_CHECK_ERR_RET(!annotation, LOGMEM(cctx->ctx), LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +020078 annotation_substmt[0].storage = &annotation->iffeatures;
Radek Krejci335332a2019-09-05 13:03:35 +020079 annotation_substmt[1].storage = &annotation->units;
80 annotation_substmt[2].storage = &annotation->flags;
81 annotation_substmt[3].storage = &annotation->type;
Radek Krejcid6b76452019-09-03 17:03:03 +020082 /* description and reference are allowed, but not compiled */
83
84 LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, annotation_substmt));
85
86 return LY_SUCCESS;
87}
88
Radek Krejci38d85362019-09-05 16:26:38 +020089/**
90 * @brief Free annotation extension instances' data.
91 *
92 * Implementation of lyext_clb_free callback set as lyext_plugin::free.
93 */
94void
95annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
96{
Radek Krejciad5963b2019-09-06 16:03:05 +020097 if (!ext->data) {
98 return;
99 }
100
Michal Vasko22df3f02020-08-24 13:29:22 +0200101 struct lyext_metadata *annotation = (struct lyext_metadata *)ext->data;
Michal Vasko69730152020-10-09 16:30:07 +0200102
Radek Krejci38d85362019-09-05 16:26:38 +0200103 annotation_substmt[0].storage = &annotation->iffeatures;
104 annotation_substmt[1].storage = &annotation->units;
105 annotation_substmt[2].storage = &annotation->flags;
106 annotation_substmt[3].storage = &annotation->type;
107
Radek Krejci0f969882020-08-21 16:56:47 +0200108 lysc_extension_instance_free(ctx, annotation_substmt);
109 free(ext->data);
Radek Krejci38d85362019-09-05 16:26:38 +0200110}
Radek Krejcid6b76452019-09-03 17:03:03 +0200111
112/**
113 * @brief Plugin for the Metadata's annotation extension
114 */
115struct lyext_plugin metadata_plugin = {
116 .id = "libyang 2 - metadata, version 1",
117 .compile = &annotation_compile,
118 .validate = NULL,
Radek Krejci38d85362019-09-05 16:26:38 +0200119 .free = annotation_free
Radek Krejcid6b76452019-09-03 17:03:03 +0200120};