blob: 03d6c03116edfaf3ea63a28a2872fcdc5eba4382 [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 Krejcif13b87b2020-12-01 22:02:17 +010029#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
36struct lysc_ext_substmt annotation_substmt[] = {
Radek Krejci38d85362019-09-05 16:26:38 +020037 {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};
45
Radek Krejcid6b76452019-09-03 17:03:03 +020046/**
47 * @brief Compile annotation extension instances.
48 *
49 * Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
50 */
51LY_ERR
52annotation_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 Vaskofd69e1d2020-07-03 11:57:17 +020056 LY_ARRAY_COUNT_TYPE u;
Radek Krejcid6b76452019-09-03 17:03:03 +020057
58 /* annotations can appear only at the top level of a YANG module or submodule */
59 if (c_ext->parent_type != LYEXT_PAR_MODULE) {
60 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 +020061 p_ext->name, lyext_parent2str(c_ext->parent_type));
Radek Krejcid6b76452019-09-03 17:03:03 +020062 return LY_EVALID;
63 }
Radek Krejci335332a2019-09-05 13:03:35 +020064 /* check mandatory argument */
65 if (!c_ext->argument) {
66 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 +020067 p_ext->name);
Radek Krejci335332a2019-09-05 13:03:35 +020068 return LY_EVALID;
69 }
Radek Krejcid6b76452019-09-03 17:03:03 +020070
71 mod_c = (struct lysc_module *)c_ext->parent;
72
73 /* check for duplication */
74 LY_ARRAY_FOR(mod_c->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +020075 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 +020076 /* duplication of the same annotation extension in a single module */
Radek Krejcid6b76452019-09-03 17:03:03 +020077 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
78 return LY_EVALID;
79 }
80 }
81
82 /* compile annotation substatements */
83 c_ext->data = annotation = calloc(1, sizeof *annotation);
Radek Krejci335332a2019-09-05 13:03:35 +020084 LY_CHECK_ERR_RET(!annotation, LOGMEM(cctx->ctx), LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +010085 annotation_substmt[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
86 annotation_substmt[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
87 annotation_substmt[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
88 annotation_substmt[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
Radek Krejcid6b76452019-09-03 17:03:03 +020089 /* description and reference are allowed, but not compiled */
90
91 LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, annotation_substmt));
92
93 return LY_SUCCESS;
94}
95
Radek Krejci38d85362019-09-05 16:26:38 +020096/**
97 * @brief Free annotation extension instances' data.
98 *
99 * Implementation of lyext_clb_free callback set as lyext_plugin::free.
100 */
101void
102annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
103{
Radek Krejciad5963b2019-09-06 16:03:05 +0200104 if (!ext->data) {
105 return;
106 }
107
Michal Vasko22df3f02020-08-24 13:29:22 +0200108 struct lyext_metadata *annotation = (struct lyext_metadata *)ext->data;
Michal Vasko69730152020-10-09 16:30:07 +0200109
Radek Krejcif13b87b2020-12-01 22:02:17 +0100110 annotation_substmt[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
111 annotation_substmt[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
112 annotation_substmt[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
113 annotation_substmt[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200114
Radek Krejci0f969882020-08-21 16:56:47 +0200115 lysc_extension_instance_free(ctx, annotation_substmt);
116 free(ext->data);
Radek Krejci38d85362019-09-05 16:26:38 +0200117}
Radek Krejcid6b76452019-09-03 17:03:03 +0200118
119/**
120 * @brief Plugin for the Metadata's annotation extension
121 */
122struct lyext_plugin metadata_plugin = {
123 .id = "libyang 2 - metadata, version 1",
124 .compile = &annotation_compile,
125 .validate = NULL,
Radek Krejci38d85362019-09-05 16:26:38 +0200126 .free = annotation_free
Radek Krejcid6b76452019-09-03 17:03:03 +0200127};