blob: d2e9dca30fcf0d18d0d1d780d7788694a48b164f [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"
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.
25LYEXT_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 */
33LY_ERR
34annotation_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;
38 struct lysp_stmt *stmt;
39 unsigned int u;
40 struct lysc_ext_substmt annotation_substmt[7] = {
41 {LY_STMT_IF_FEATURE, LY_STMT_CARD_ANY, NULL},
42 {LY_STMT_TYPE, LY_STMT_CARD_MAND, NULL},
43 {LY_STMT_UNITS, LY_STMT_CARD_OPT, NULL},
44 {LY_STMT_STATUS, LY_STMT_CARD_OPT, NULL},
45 {LY_STMT_DESCRIPTION, LY_STMT_CARD_OPT, NULL},
46 {LY_STMT_REFERENCE, LY_STMT_CARD_OPT, NULL},
47 {0, 0, 0} /* terminating item */
48 };
49
50 /* annotations can appear only at the top level of a YANG module or submodule */
51 if (c_ext->parent_type != LYEXT_PAR_MODULE) {
52 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.",
53 p_ext->name, lyext_parent2str(c_ext->parent_type));
54 return LY_EVALID;
55 }
56
57 mod_c = (struct lysc_module *)c_ext->parent;
58
59 /* check for duplication */
60 LY_ARRAY_FOR(mod_c->exts, u) {
61 if (&mod_c->exts[u] != c_ext && mod_c->exts[u].def == c_ext->def) {
62 /* duplication of a annotation extension in a single module */
63 lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
64 return LY_EVALID;
65 }
66 }
67
68 /* compile annotation substatements */
69 c_ext->data = annotation = calloc(1, sizeof *annotation);
70 LY_CHECK_RET(annotation, LY_EMEM);
71 annotation_substmt[0].storage = &annotation->iffeatures;
72 annotation_substmt[1].storage = &annotation->type;
73 annotation_substmt[2].storage = &annotation->units;
74 annotation_substmt[3].storage = &annotation->flags;
75 /* description and reference are allowed, but not compiled */
76
77 LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, annotation_substmt));
78
79 return LY_SUCCESS;
80}
81
82
83/**
84 * @brief Plugin for the Metadata's annotation extension
85 */
86struct lyext_plugin metadata_plugin = {
87 .id = "libyang 2 - metadata, version 1",
88 .compile = &annotation_compile,
89 .validate = NULL,
90 .free = NULL
91};