blob: aa6cb43fdca689e059c00f5bb61337d42653d42d [file] [log] [blame]
Radek Krejcid6b76452019-09-03 17:03:03 +02001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file metadata.c
Radek Krejcid6b76452019-09-03 17:03:03 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko193dacd2022-10-13 08:43:05 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcid6b76452019-09-03 17:03:03 +02005 * @brief libyang extension plugin - Metadata (RFC 7952)
6 *
Michal Vasko193dacd2022-10-13 08:43:05 +02007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejcid6b76452019-09-03 17:03:03 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Krejcid6b76452019-09-03 17:03:03 +020015
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020016#include "metadata.h"
Radek Krejci5f9a3672021-03-05 21:35:22 +010017
Radek Krejci883355a2021-03-11 11:54:41 +010018#include <stdint.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "libyang.h"
23#include "plugins_exts.h"
Radek Krejcid6b76452019-09-03 17:03:03 +020024
Michal Vasko193dacd2022-10-13 08:43:05 +020025struct lysp_ext_metadata {
26 struct lysp_type *type; /**< type of the metadata (mandatory) */
Radek Krejci1b2eef82021-02-17 11:17:27 +010027 const char *units; /**< units of the leaf's type */
Michal Vasko193dacd2022-10-13 08:43:05 +020028 struct lysp_qname *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
Radek Krejci1b2eef82021-02-17 11:17:27 +010029 const char *dsc; /**< description */
30 const char *ref; /**< reference */
31 uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
32};
Radek Krejci38d85362019-09-05 16:26:38 +020033
Michal Vasko193dacd2022-10-13 08:43:05 +020034struct lysc_ext_metadata {
35 struct lysc_type *type; /**< type of the metadata (mandatory) */
36 const char *units; /**< units of the leaf's type */
37 const char *dsc; /**< description */
38 const char *ref; /**< reference */
39 uint16_t flags; /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
40};
41
42/**
43 * @brief Parse annotation extension instances.
44 *
45 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
46 */
47static LY_ERR
48annotation_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
49{
50 LY_ERR r;
51 struct lysp_ext_metadata *ann_pdata;
52 struct lysp_module *pmod;
53 LY_ARRAY_COUNT_TYPE u;
54
55 /* annotations can appear only at the top level of a YANG module or submodule */
56 if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
57 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is allowed only at the top level of a YANG module or "
58 "submodule, but it is placed in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt));
59 return LY_EVALID;
60 }
61
62 pmod = ext->parent;
63
64 /* check for duplication */
65 LY_ARRAY_FOR(pmod->exts, u) {
66 if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
67 /* duplication of the same annotation extension in a single module */
68 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
69 return LY_EVALID;
70 }
71 }
72
73 /* parse annotation substatements */
74 ext->parsed = ann_pdata = calloc(1, sizeof *ann_pdata);
75 if (!ann_pdata) {
76 goto emem;
77 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020078 LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 6, r, emem);
Michal Vasko193dacd2022-10-13 08:43:05 +020079
80 LY_ARRAY_INCREMENT(ext->substmts);
81 ext->substmts[0].stmt = LY_STMT_IF_FEATURE;
Michal Vasko909434e2024-06-13 16:08:37 +020082 ext->substmts[0].storage = (uint64_t)(uintptr_t)&ann_pdata->iffeatures;
Michal Vasko193dacd2022-10-13 08:43:05 +020083
84 LY_ARRAY_INCREMENT(ext->substmts);
85 ext->substmts[1].stmt = LY_STMT_UNITS;
Michal Vasko909434e2024-06-13 16:08:37 +020086 ext->substmts[1].storage = (uint64_t)(uintptr_t)&ann_pdata->units;
Michal Vasko193dacd2022-10-13 08:43:05 +020087
88 LY_ARRAY_INCREMENT(ext->substmts);
89 ext->substmts[2].stmt = LY_STMT_STATUS;
Michal Vasko909434e2024-06-13 16:08:37 +020090 ext->substmts[2].storage = (uint64_t)(uintptr_t)&ann_pdata->flags;
Michal Vasko193dacd2022-10-13 08:43:05 +020091
92 LY_ARRAY_INCREMENT(ext->substmts);
93 ext->substmts[3].stmt = LY_STMT_TYPE;
Michal Vasko909434e2024-06-13 16:08:37 +020094 ext->substmts[3].storage = (uint64_t)(uintptr_t)&ann_pdata->type;
Michal Vasko193dacd2022-10-13 08:43:05 +020095
96 LY_ARRAY_INCREMENT(ext->substmts);
97 ext->substmts[4].stmt = LY_STMT_DESCRIPTION;
Michal Vasko909434e2024-06-13 16:08:37 +020098 ext->substmts[4].storage = (uint64_t)(uintptr_t)&ann_pdata->dsc;
Michal Vasko193dacd2022-10-13 08:43:05 +020099
100 LY_ARRAY_INCREMENT(ext->substmts);
101 ext->substmts[5].stmt = LY_STMT_REFERENCE;
Michal Vasko909434e2024-06-13 16:08:37 +0200102 ext->substmts[5].storage = (uint64_t)(uintptr_t)&ann_pdata->ref;
Michal Vasko193dacd2022-10-13 08:43:05 +0200103
104 if ((r = lyplg_ext_parse_extension_instance(pctx, ext))) {
105 return r;
106 }
107
108 /* check for mandatory substatements */
109 if (!ann_pdata->type) {
110 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Missing mandatory keyword \"type\" as a child of \"%s %s\".",
111 ext->name, ext->argument);
112 return LY_EVALID;
113 }
114
115 return LY_SUCCESS;
116
117emem:
118 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
119 return LY_EMEM;
120}
121
Radek Krejcid6b76452019-09-03 17:03:03 +0200122/**
123 * @brief Compile annotation extension instances.
124 *
Radek Krejci0b013302021-03-29 15:22:32 +0200125 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
Radek Krejcid6b76452019-09-03 17:03:03 +0200126 */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100127static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200128annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
Radek Krejcid6b76452019-09-03 17:03:03 +0200129{
Radek Krejci859a15a2021-03-05 20:56:59 +0100130 LY_ERR ret;
Michal Vasko193dacd2022-10-13 08:43:05 +0200131 struct lysc_ext_metadata *ann_cdata;
Radek Krejcid6b76452019-09-03 17:03:03 +0200132
133 /* compile annotation substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +0200134 ext->compiled = ann_cdata = calloc(1, sizeof *ann_cdata);
135 if (!ann_cdata) {
Radek Krejci859a15a2021-03-05 20:56:59 +0100136 goto emem;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100137 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200138 LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), ext->substmts, 6, ret, emem);
Radek Krejcid6b76452019-09-03 17:03:03 +0200139
Michal Vasko193dacd2022-10-13 08:43:05 +0200140 LY_ARRAY_INCREMENT(ext->substmts);
141 ext->substmts[0].stmt = LY_STMT_IF_FEATURE;
Michal Vasko708a8bf2024-06-13 09:44:51 +0200142 ext->substmts[0].storage = 0;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100143
Michal Vasko193dacd2022-10-13 08:43:05 +0200144 LY_ARRAY_INCREMENT(ext->substmts);
145 ext->substmts[1].stmt = LY_STMT_UNITS;
Michal Vasko909434e2024-06-13 16:08:37 +0200146 ext->substmts[1].storage = (uint64_t)(uintptr_t)&ann_cdata->units;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100147
Michal Vasko193dacd2022-10-13 08:43:05 +0200148 LY_ARRAY_INCREMENT(ext->substmts);
149 ext->substmts[2].stmt = LY_STMT_STATUS;
Michal Vasko909434e2024-06-13 16:08:37 +0200150 ext->substmts[2].storage = (uint64_t)(uintptr_t)&ann_cdata->flags;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100151
Michal Vasko193dacd2022-10-13 08:43:05 +0200152 LY_ARRAY_INCREMENT(ext->substmts);
153 ext->substmts[3].stmt = LY_STMT_TYPE;
Michal Vasko909434e2024-06-13 16:08:37 +0200154 ext->substmts[3].storage = (uint64_t)(uintptr_t)&ann_cdata->type;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100155
Michal Vasko193dacd2022-10-13 08:43:05 +0200156 LY_ARRAY_INCREMENT(ext->substmts);
157 ext->substmts[4].stmt = LY_STMT_DESCRIPTION;
Michal Vasko909434e2024-06-13 16:08:37 +0200158 ext->substmts[4].storage = (uint64_t)(uintptr_t)&ann_cdata->dsc;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100159
Michal Vasko193dacd2022-10-13 08:43:05 +0200160 LY_ARRAY_INCREMENT(ext->substmts);
161 ext->substmts[5].stmt = LY_STMT_REFERENCE;
Michal Vasko909434e2024-06-13 16:08:37 +0200162 ext->substmts[5].storage = (uint64_t)(uintptr_t)&ann_cdata->ref;
Radek Krejci1b2eef82021-02-17 11:17:27 +0100163
Michal Vasko193dacd2022-10-13 08:43:05 +0200164 ret = lyplg_ext_compile_extension_instance(cctx, extp, ext);
Radek Krejci859a15a2021-03-05 20:56:59 +0100165 return ret;
Radek Krejcid6b76452019-09-03 17:03:03 +0200166
Radek Krejci859a15a2021-03-05 20:56:59 +0100167emem:
Michal Vasko193dacd2022-10-13 08:43:05 +0200168 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
Radek Krejci859a15a2021-03-05 20:56:59 +0100169 return LY_EMEM;
Radek Krejcid6b76452019-09-03 17:03:03 +0200170}
171
Radek Krejci38d85362019-09-05 16:26:38 +0200172/**
Radek Krejciadcf63d2021-02-09 10:21:18 +0100173 * @brief INFO printer
174 *
Michal Vasko941e0562022-10-18 10:35:00 +0200175 * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info
Radek Krejciadcf63d2021-02-09 10:21:18 +0100176 */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100177static LY_ERR
Michal Vasko941e0562022-10-18 10:35:00 +0200178annotation_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
Radek Krejciadcf63d2021-02-09 10:21:18 +0100179{
Michal Vasko941e0562022-10-18 10:35:00 +0200180 lyplg_ext_print_info_extension_instance(ctx, ext, flag);
Radek Krejciadcf63d2021-02-09 10:21:18 +0100181
182 return LY_SUCCESS;
183}
184
185/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200186 * @brief Free parsed annotation extension instance data.
Radek Krejci38d85362019-09-05 16:26:38 +0200187 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200188 * Implementation of ::lyplg_ext_parse_free_clb callback set as ::lyext_plugin::pfree.
Radek Krejci38d85362019-09-05 16:26:38 +0200189 */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100190static void
Michal Vasko193dacd2022-10-13 08:43:05 +0200191annotation_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
Radek Krejci38d85362019-09-05 16:26:38 +0200192{
Radek Krejci1b2eef82021-02-17 11:17:27 +0100193 if (!ext->substmts) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200194 return;
195 }
196
Michal Vasko193dacd2022-10-13 08:43:05 +0200197 lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
198 free(ext->parsed);
199}
200
201/**
202 * @brief Free compiled annotation extension instance data.
203 *
204 * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree.
205 */
206static void
207annotation_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
208{
209 if (!ext->substmts) {
210 return;
211 }
212
213 lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
214 free(ext->compiled);
Radek Krejci38d85362019-09-05 16:26:38 +0200215}
Radek Krejcid6b76452019-09-03 17:03:03 +0200216
217/**
Radek Krejci3e6632f2021-03-22 22:08:21 +0100218 * @brief Plugin descriptions for the Metadata's annotation extension
Radek Krejcia6f61e72021-03-24 21:00:19 +0100219 *
220 * Note that external plugins are supposed to use:
221 *
222 * LYPLG_EXTENSIONS = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200223 */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100224const struct lyplg_ext_record plugins_metadata[] = {
225 {
226 .module = "ietf-yang-metadata",
227 .revision = "2016-08-05",
228 .name = "annotation",
229
Michal Vasko193dacd2022-10-13 08:43:05 +0200230 .plugin.id = "ly2 metadata v1",
231 .plugin.parse = annotation_parse,
232 .plugin.compile = annotation_compile,
Michal Vasko941e0562022-10-18 10:35:00 +0200233 .plugin.printer_info = annotation_printer_info,
aPiecek03cb4872022-10-24 10:31:51 +0200234 .plugin.printer_ctree = NULL,
235 .plugin.printer_ptree = NULL,
Michal Vasko135719f2022-08-25 12:18:17 +0200236 .plugin.node = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200237 .plugin.snode = NULL,
Michal Vasko193dacd2022-10-13 08:43:05 +0200238 .plugin.validate = NULL,
239 .plugin.pfree = annotation_pfree,
240 .plugin.cfree = annotation_cfree,
Radek Krejci3e6632f2021-03-22 22:08:21 +0100241 },
242 {0} /* terminating zeroed record */
Radek Krejcid6b76452019-09-03 17:03:03 +0200243};