blob: 0c8f37bca13b6e31639931aca22c3f93bc54f3c8 [file] [log] [blame]
Radek Krejci5fa32a32021-02-08 18:12:38 +01001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file yangdata.c
Radek Krejci5fa32a32021-02-08 18:12:38 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko193dacd2022-10-13 08:43:05 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci5fa32a32021-02-08 18:12:38 +01005 * @brief libyang extension plugin - yang-data (RFC 8040)
6 *
Michal Vasko193dacd2022-10-13 08:43:05 +02007 * Copyright (c) 2021 - 2022 CESNET, z.s.p.o.
Radek Krejci5fa32a32021-02-08 18:12:38 +01008 *
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 */
15
aPiecek03cb4872022-10-24 10:31:51 +020016#include <assert.h>
Radek Krejci883355a2021-03-11 11:54:41 +010017#include <stdint.h>
Radek Krejci5fa32a32021-02-08 18:12:38 +010018#include <stdlib.h>
Radek Krejci883355a2021-03-11 11:54:41 +010019#include <string.h>
Radek Krejci5fa32a32021-02-08 18:12:38 +010020
aPiecek03cb4872022-10-24 10:31:51 +020021#include "compat.h"
Radek Krejci883355a2021-03-11 11:54:41 +010022#include "libyang.h"
Radek Krejci5fa32a32021-02-08 18:12:38 +010023#include "plugins_exts.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010024
Michal Vasko193dacd2022-10-13 08:43:05 +020025static void yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext);
26
Radek Krejci5fa32a32021-02-08 18:12:38 +010027/**
Michal Vasko193dacd2022-10-13 08:43:05 +020028 * @brief Parse yang-data extension instances.
Radek Krejci5fa32a32021-02-08 18:12:38 +010029 *
Michal Vasko193dacd2022-10-13 08:43:05 +020030 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
Radek Krejci5fa32a32021-02-08 18:12:38 +010031 */
Michal Vasko193dacd2022-10-13 08:43:05 +020032static LY_ERR
33yangdata_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
Radek Krejci5fa32a32021-02-08 18:12:38 +010034{
Michal Vasko193dacd2022-10-13 08:43:05 +020035 LY_ERR ret;
36 LY_ARRAY_COUNT_TYPE u;
37 struct lysp_module *pmod;
38
39 /* yang-data can appear only at the top level of a YANG module or submodule */
40 if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
41 lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is ignored since it appears as a non top-level statement "
42 "in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt));
43 return LY_ENOT;
44 }
45
46 pmod = ext->parent;
47
48 /* check for duplication */
49 LY_ARRAY_FOR(pmod->exts, u) {
50 if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
51 /* duplication of the same yang-data extension in a single module */
52 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
53 return LY_EVALID;
54 }
55 }
56
57 /* parse yang-data substatements */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020058 LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 3, ret, emem);
Michal Vasko193dacd2022-10-13 08:43:05 +020059 LY_ARRAY_INCREMENT(ext->substmts);
60 ext->substmts[0].stmt = LY_STMT_CONTAINER;
61 ext->substmts[0].storage = &ext->parsed;
62
63 LY_ARRAY_INCREMENT(ext->substmts);
64 ext->substmts[1].stmt = LY_STMT_CHOICE;
65 ext->substmts[1].storage = &ext->parsed;
66
67 LY_ARRAY_INCREMENT(ext->substmts);
68 ext->substmts[2].stmt = LY_STMT_USES;
69 ext->substmts[2].storage = &ext->parsed;
70
71 if ((ret = lyplg_ext_parse_extension_instance(pctx, ext))) {
72 return ret;
73 }
74
75 return LY_SUCCESS;
76
77emem:
78 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
79 return LY_EMEM;
Radek Krejci5fa32a32021-02-08 18:12:38 +010080}
81
82/**
83 * @brief Compile yang-data extension instances.
84 *
Radek Krejci0b013302021-03-29 15:22:32 +020085 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
Radek Krejci5fa32a32021-02-08 18:12:38 +010086 */
Radek Krejci3e6632f2021-03-22 22:08:21 +010087static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +020088yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
Radek Krejci5fa32a32021-02-08 18:12:38 +010089{
90 LY_ERR ret;
Radek Krejci5fa32a32021-02-08 18:12:38 +010091 const struct lysc_node *child;
92 ly_bool valid = 1;
Michal Vasko193dacd2022-10-13 08:43:05 +020093 uint32_t prev_options = *lyplg_ext_compile_get_options(cctx);
Radek Krejci5fa32a32021-02-08 18:12:38 +010094
Michal Vasko193dacd2022-10-13 08:43:05 +020095 /* compile yangg-data substatements */
96 LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 3, ret, emem);
97 LY_ARRAY_INCREMENT(ext->substmts);
98 ext->substmts[0].stmt = LY_STMT_CONTAINER;
99 ext->substmts[0].storage = &ext->compiled;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100100
Michal Vasko193dacd2022-10-13 08:43:05 +0200101 LY_ARRAY_INCREMENT(ext->substmts);
102 ext->substmts[1].stmt = LY_STMT_CHOICE;
103 ext->substmts[1].storage = &ext->compiled;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100104
Michal Vasko193dacd2022-10-13 08:43:05 +0200105 LY_ARRAY_INCREMENT(ext->substmts);
106 ext->substmts[2].stmt = LY_STMT_USES;
107 ext->substmts[2].storage = &ext->compiled;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100108
Michal Vasko193dacd2022-10-13 08:43:05 +0200109 *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
110 ret = lyplg_ext_compile_extension_instance(cctx, extp, ext);
111 *lyplg_ext_compile_get_options(cctx) = prev_options;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100112 if (ret) {
113 return ret;
114 }
Radek Krejci5fa32a32021-02-08 18:12:38 +0100115
116 /* check that we have really just a single container data definition in the top */
Michal Vasko193dacd2022-10-13 08:43:05 +0200117 child = ext->compiled;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100118 if (!child) {
119 valid = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +0200120 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
Radek Krejci5fa32a32021-02-08 18:12:38 +0100121 "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.",
Michal Vasko193dacd2022-10-13 08:43:05 +0200122 extp->name);
Radek Krejci5fa32a32021-02-08 18:12:38 +0100123 } else if (child->next) {
124 valid = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +0200125 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
Radek Krejci5fa32a32021-02-08 18:12:38 +0100126 "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.",
Michal Vasko193dacd2022-10-13 08:43:05 +0200127 extp->name);
Radek Krejci5fa32a32021-02-08 18:12:38 +0100128 } else if (child->nodetype == LYS_CHOICE) {
129 /* all the choice's case are expected to result to a single container node */
Michal Vasko193dacd2022-10-13 08:43:05 +0200130 struct lysc_module *mod_c = ext->parent;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100131 const struct lysc_node *snode = NULL;
132
133 while ((snode = lys_getnext(snode, child, mod_c, 0))) {
134 if (snode->next) {
135 valid = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +0200136 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
Radek Krejci5fa32a32021-02-08 18:12:38 +0100137 "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), "
Michal Vasko193dacd2022-10-13 08:43:05 +0200138 "but only a single container data node is allowed.", extp->name);
Radek Krejci5fa32a32021-02-08 18:12:38 +0100139 break;
140 } else if (snode->nodetype != LYS_CONTAINER) {
141 valid = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +0200142 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
Radek Krejci5fa32a32021-02-08 18:12:38 +0100143 "Extension %s is instantiated with %s top level data node (inside a choice), "
Michal Vasko193dacd2022-10-13 08:43:05 +0200144 "but only a single container data node is allowed.", extp->name, lys_nodetype2str(snode->nodetype));
Radek Krejci5fa32a32021-02-08 18:12:38 +0100145 break;
146 }
147 }
148 } else if (child->nodetype != LYS_CONTAINER) {
149 /* via uses */
150 valid = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +0200151 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
Radek Krejci5fa32a32021-02-08 18:12:38 +0100152 "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.",
Michal Vasko193dacd2022-10-13 08:43:05 +0200153 extp->name, lys_nodetype2str(child->nodetype));
Radek Krejci5fa32a32021-02-08 18:12:38 +0100154 }
155
156 if (!valid) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200157 yangdata_cfree(lyplg_ext_compile_get_ctx(cctx), ext);
158 ext->compiled = ext->substmts = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100159 return LY_EVALID;
160 }
161
162 return LY_SUCCESS;
Radek Krejci859a15a2021-03-05 20:56:59 +0100163
164emem:
Michal Vasko193dacd2022-10-13 08:43:05 +0200165 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
Radek Krejci859a15a2021-03-05 20:56:59 +0100166 return LY_EMEM;
Radek Krejci5fa32a32021-02-08 18:12:38 +0100167}
168
169/**
Radek Krejciadcf63d2021-02-09 10:21:18 +0100170 * @brief INFO printer
171 *
Michal Vasko941e0562022-10-18 10:35:00 +0200172 * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info
Radek Krejciadcf63d2021-02-09 10:21:18 +0100173 */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100174static LY_ERR
Michal Vasko941e0562022-10-18 10:35:00 +0200175yangdata_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
Radek Krejciadcf63d2021-02-09 10:21:18 +0100176{
Michal Vasko941e0562022-10-18 10:35:00 +0200177 lyplg_ext_print_info_extension_instance(ctx, ext, flag);
Radek Krejciadcf63d2021-02-09 10:21:18 +0100178 return LY_SUCCESS;
179}
180
181/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200182 * @brief Free parsed yang-data extension instance data.
183 *
184 * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree.
185 */
186static void
187yangdata_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
188{
189 lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
190}
191
192/**
193 * @brief Free compiled yang-data extension instance data.
194 *
195 * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree.
196 */
197static void
198yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
199{
200 lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
201}
202
aPiecek03cb4872022-10-24 10:31:51 +0200203static void
204yangdata_sprinter_node(uint16_t nodetype, const char **flags)
205{
206 if (nodetype & LYS_USES) {
207 *flags = "-u";
208 } else {
209 *flags = "--";
210 }
211}
212
213static LY_ERR
214yangdata_sprinter_cnode(const struct lysc_node *node, const void *UNUSED(plugin_priv), ly_bool *UNUSED(skip),
215 const char **flags, const char **UNUSED(add_opts))
216{
217 yangdata_sprinter_node(node->nodetype, flags);
218 return LY_SUCCESS;
219}
220
221static LY_ERR
222yangdata_sprinter_pnode(const struct lysp_node *node, const void *UNUSED(plugin_priv), ly_bool *UNUSED(skip),
223 const char **flags, const char **UNUSED(add_opts))
224{
225 yangdata_sprinter_node(node->nodetype, flags);
226 return LY_SUCCESS;
227}
228
229static LY_ERR
230yangdata_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx,
231 const char **UNUSED(flags), const char **UNUSED(add_opts))
232{
233 LY_ERR rc = LY_SUCCESS;
234
235 assert(ctx);
236 rc = lyplg_ext_sprinter_ctree_add_ext_nodes(ctx, ext, yangdata_sprinter_cnode);
237 return rc;
238}
239
240static LY_ERR
241yangdata_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx,
242 const char **UNUSED(flags), const char **UNUSED(add_opts))
243{
244 LY_ERR rc = LY_SUCCESS;
245
246 assert(ctx);
247 rc = lyplg_ext_sprinter_ptree_add_ext_nodes(ctx, ext, yangdata_sprinter_pnode);
248 return rc;
249}
250
Michal Vasko193dacd2022-10-13 08:43:05 +0200251/**
Radek Krejci3e6632f2021-03-22 22:08:21 +0100252 * @brief Plugin descriptions for the yang-data extension
Radek Krejcia6f61e72021-03-24 21:00:19 +0100253 *
254 * Note that external plugins are supposed to use:
255 *
256 * LYPLG_EXTENSIONS = {
Radek Krejci5fa32a32021-02-08 18:12:38 +0100257 */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100258const struct lyplg_ext_record plugins_yangdata[] = {
259 {
260 .module = "ietf-restconf",
261 .revision = "2017-01-26",
262 .name = "yang-data",
263
Michal Vasko193dacd2022-10-13 08:43:05 +0200264 .plugin.id = "ly2 yang-data v1",
265 .plugin.parse = yangdata_parse,
Michal Vasko135719f2022-08-25 12:18:17 +0200266 .plugin.compile = yangdata_compile,
Michal Vasko941e0562022-10-18 10:35:00 +0200267 .plugin.printer_info = yangdata_printer_info,
aPiecek03cb4872022-10-24 10:31:51 +0200268 .plugin.printer_ctree = yangdata_sprinter_ctree,
269 .plugin.printer_ptree = yangdata_sprinter_ptree,
Michal Vasko135719f2022-08-25 12:18:17 +0200270 .plugin.node = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200271 .plugin.snode = NULL,
Michal Vasko193dacd2022-10-13 08:43:05 +0200272 .plugin.validate = NULL,
273 .plugin.pfree = yangdata_pfree,
274 .plugin.cfree = yangdata_cfree
Radek Krejci3e6632f2021-03-22 22:08:21 +0100275 },
276 {0} /* terminating zeroed record */
Radek Krejci5fa32a32021-02-08 18:12:38 +0100277};