blob: 781cde05565cd1c5d0564cc2aeded7520a616546 [file] [log] [blame]
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001/**
2 * @file structure.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libyang extension plugin - strcture (RFC 8791)
5 *
6 * Copyright (c) 2022 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
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include "libyang.h"
20#include "plugins_exts.h"
21
22struct lysc_ext_instance_structure {
23 struct lysc_must *musts;
24 uint16_t flags;
25 const char *dsc;
26 const char *ref;
27 struct lysp_tpdf *typedefs;
28 struct lysp_node_grp *groupings;
29 struct lysc_node *child;
30};
31
32/**
33 * @brief Compile structure extension instances.
34 *
35 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
36 */
37static LY_ERR
38structure_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
39{
40 LY_ERR ret;
41 LY_ARRAY_COUNT_TYPE u;
42 struct lysc_module *mod_c;
43 const struct lysc_node *child;
44 struct lysc_ext_instance_structure *struct_ext;
45 uint32_t prev_options = *lysc_ctx_get_options(cctx);
46
47 /* structure can appear only at the top level of a YANG module or submodule */
48 if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
49 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
50 "Extension %s must not be used as a non top-level statement in \"%s\" statement.",
51 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
52 return LY_EVALID;
53 }
54
55 mod_c = (struct lysc_module *)c_ext->parent;
56
57 /* check identifier namespace */
58 LY_ARRAY_FOR(mod_c->exts, u) {
59 if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
60 /* duplication of the same structure extension in a single module */
61 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
62 return LY_EVALID;
63 }
64 }
65 LY_LIST_FOR(mod_c->data, child) {
66 if (!strcmp(child->name, c_ext->argument)) {
67 /* identifier collision */
68 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s collides "
69 "with a %s with the same identifier.", p_ext->name, lys_nodetype2str(child->nodetype));
70 return LY_EVALID;
71 }
72 }
73
74 /* allocate the storage */
75 struct_ext = calloc(1, sizeof *struct_ext);
76 if (!struct_ext) {
77 goto emem;
78 }
79 c_ext->data = struct_ext;
80
81 /* compile substatements */
82 LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 14, ret, emem);
83 LY_ARRAY_INCREMENT(c_ext->substmts);
84 c_ext->substmts[0].stmt = LY_STMT_MUST;
85 c_ext->substmts[0].cardinality = LY_STMT_CARD_ANY;
86 c_ext->substmts[0].storage = &struct_ext->musts;
87
88 LY_ARRAY_INCREMENT(c_ext->substmts);
89 c_ext->substmts[1].stmt = LY_STMT_STATUS;
90 c_ext->substmts[1].cardinality = LY_STMT_CARD_OPT;
91 c_ext->substmts[1].storage = &struct_ext->flags;
92
93 LY_ARRAY_INCREMENT(c_ext->substmts);
94 c_ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
95 c_ext->substmts[2].cardinality = LY_STMT_CARD_OPT;
96 c_ext->substmts[2].storage = &struct_ext->dsc;
97
98 LY_ARRAY_INCREMENT(c_ext->substmts);
99 c_ext->substmts[3].stmt = LY_STMT_REFERENCE;
100 c_ext->substmts[3].cardinality = LY_STMT_CARD_OPT;
101 c_ext->substmts[3].storage = &struct_ext->ref;
102
103 LY_ARRAY_INCREMENT(c_ext->substmts);
104 c_ext->substmts[4].stmt = LY_STMT_TYPEDEF;
105 c_ext->substmts[4].cardinality = LY_STMT_CARD_ANY;
106 c_ext->substmts[4].storage = &struct_ext->typedefs;
107
108 LY_ARRAY_INCREMENT(c_ext->substmts);
109 c_ext->substmts[5].stmt = LY_STMT_GROUPING;
110 c_ext->substmts[5].cardinality = LY_STMT_CARD_ANY;
111 c_ext->substmts[5].storage = &struct_ext->groupings;
112
113 /* data-def-stmt */
114 LY_ARRAY_INCREMENT(c_ext->substmts);
115 c_ext->substmts[6].stmt = LY_STMT_CONTAINER;
116 c_ext->substmts[6].cardinality = LY_STMT_CARD_ANY;
117 c_ext->substmts[6].storage = &struct_ext->child;
118
119 LY_ARRAY_INCREMENT(c_ext->substmts);
120 c_ext->substmts[7].stmt = LY_STMT_LEAF;
121 c_ext->substmts[7].cardinality = LY_STMT_CARD_ANY;
122 c_ext->substmts[7].storage = &struct_ext->child;
123
124 LY_ARRAY_INCREMENT(c_ext->substmts);
125 c_ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
126 c_ext->substmts[8].cardinality = LY_STMT_CARD_ANY;
127 c_ext->substmts[8].storage = &struct_ext->child;
128
129 LY_ARRAY_INCREMENT(c_ext->substmts);
130 c_ext->substmts[9].stmt = LY_STMT_LIST;
131 c_ext->substmts[9].cardinality = LY_STMT_CARD_ANY;
132 c_ext->substmts[9].storage = &struct_ext->child;
133
134 LY_ARRAY_INCREMENT(c_ext->substmts);
135 c_ext->substmts[10].stmt = LY_STMT_CHOICE;
136 c_ext->substmts[10].cardinality = LY_STMT_CARD_ANY;
137 c_ext->substmts[10].storage = &struct_ext->child;
138
139 LY_ARRAY_INCREMENT(c_ext->substmts);
140 c_ext->substmts[11].stmt = LY_STMT_ANYDATA;
141 c_ext->substmts[11].cardinality = LY_STMT_CARD_ANY;
142 c_ext->substmts[11].storage = &struct_ext->child;
143
144 LY_ARRAY_INCREMENT(c_ext->substmts);
145 c_ext->substmts[12].stmt = LY_STMT_ANYXML;
146 c_ext->substmts[12].cardinality = LY_STMT_CARD_ANY;
147 c_ext->substmts[12].storage = &struct_ext->child;
148
149 LY_ARRAY_INCREMENT(c_ext->substmts);
150 c_ext->substmts[13].stmt = LY_STMT_USES;
151 c_ext->substmts[13].cardinality = LY_STMT_CARD_ANY;
152 c_ext->substmts[13].storage = &struct_ext->child;
153
154 *lysc_ctx_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
155 ret = lys_compile_extension_instance(cctx, p_ext, c_ext);
156 *lysc_ctx_get_options(cctx) = prev_options;
157 if (ret) {
158 return ret;
159 }
160
161 return LY_SUCCESS;
162
163emem:
164 lyplg_ext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
165 return LY_EMEM;
166}
167
168/**
169 * @brief INFO printer
170 *
171 * Implementation of ::lyplg_ext_schema_printer_clb set as ::lyext_plugin::sprinter
172 */
173static LY_ERR
174structure_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
175{
176 lysc_print_extension_instance(ctx, ext, flag);
177 return LY_SUCCESS;
178}
179
180/**
181 * @brief Free structure extension instances' data.
182 *
183 * Implementation of ::lyplg_clb_free_clb callback set as lyext_plugin::free.
184 */
185static void
186structure_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
187{
188 lyplg_ext_instance_substatements_free(ctx, ext->substmts);
189 free(ext->data);
190}
191
192/**
193 * @brief Plugin descriptions for the structure extension
194 *
195 * Note that external plugins are supposed to use:
196 *
197 * LYPLG_EXTENSIONS = {
198 */
199const struct lyplg_ext_record plugins_structure[] = {
200 {
201 .module = "ietf-yang-structure-ext",
202 .revision = "2020-06-17",
203 .name = "structure",
204
205 .plugin.id = "libyang 2 - structure, version 1",
206 .plugin.compile = structure_compile,
207 .plugin.sprinter = structure_schema_printer,
208 .plugin.free = structure_free,
209 .plugin.node = NULL,
210 .plugin.snode = NULL,
211 .plugin.validate = NULL
212 },
213 {0} /* terminating zeroed record */
214};