blob: ee7a52e5c391b4ecb0aa1194e47a9f79fa5609fa [file] [log] [blame]
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001/**
2 * @file structure.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko193dacd2022-10-13 08:43:05 +02004 * @brief libyang extension plugin - structure (RFC 8791)
Michal Vaskoedb0fa52022-10-04 10:36:00 +02005 *
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
aPiecek03cb4872022-10-24 10:31:51 +020015#include <assert.h>
Michal Vaskoedb0fa52022-10-04 10:36:00 +020016#include <stdint.h>
17#include <stdlib.h>
18#include <string.h>
19
Michal Vasko0b50f6b2022-10-05 15:07:55 +020020#include "compat.h"
Michal Vaskoedb0fa52022-10-04 10:36:00 +020021#include "libyang.h"
22#include "plugins_exts.h"
23
Michal Vasko193dacd2022-10-13 08:43:05 +020024struct lysp_ext_instance_structure {
25 struct lysp_restr *musts;
Michal Vaskoedb0fa52022-10-04 10:36:00 +020026 uint16_t flags;
27 const char *dsc;
28 const char *ref;
29 struct lysp_tpdf *typedefs;
30 struct lysp_node_grp *groupings;
Michal Vasko193dacd2022-10-13 08:43:05 +020031 struct lysp_node *child;
32};
33
34struct lysc_ext_instance_structure {
35 struct lysc_must *musts;
36 uint16_t flags;
37 const char *dsc;
38 const char *ref;
Michal Vaskoedb0fa52022-10-04 10:36:00 +020039 struct lysc_node *child;
40};
41
Michal Vasko193dacd2022-10-13 08:43:05 +020042struct lysp_ext_instance_augment_structure {
43 uint16_t flags;
44 const char *dsc;
45 const char *ref;
46 struct lysp_node *child;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020047 struct lysp_node_augment *aug;
Michal Vasko0b50f6b2022-10-05 15:07:55 +020048};
49
Michal Vaskoedb0fa52022-10-04 10:36:00 +020050/**
Michal Vasko193dacd2022-10-13 08:43:05 +020051 * @brief Parse structure extension instances.
52 *
53 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
54 */
55static LY_ERR
56structure_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
57{
58 LY_ERR rc;
59 LY_ARRAY_COUNT_TYPE u;
60 struct lysp_module *pmod;
61 struct lysp_ext_instance_structure *struct_pdata;
62
63 /* structure can appear only at the top level of a YANG module or submodule */
64 if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
65 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID,
66 "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name,
67 lyplg_ext_stmt2str(ext->parent_stmt));
68 return LY_EVALID;
69 }
70
71 pmod = ext->parent;
72
73 /* check for duplication */
74 LY_ARRAY_FOR(pmod->exts, u) {
75 if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
76 /* duplication of the same structure extension in a single module */
77 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
78 return LY_EVALID;
79 }
80 }
81
82 /* allocate the storage */
83 struct_pdata = calloc(1, sizeof *struct_pdata);
84 if (!struct_pdata) {
85 goto emem;
86 }
87 ext->parsed = struct_pdata;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020088 LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 14, rc, emem);
Michal Vasko193dacd2022-10-13 08:43:05 +020089
90 /* parse substatements */
91 LY_ARRAY_INCREMENT(ext->substmts);
92 ext->substmts[0].stmt = LY_STMT_MUST;
93 ext->substmts[0].storage = &struct_pdata->musts;
94
95 LY_ARRAY_INCREMENT(ext->substmts);
96 ext->substmts[1].stmt = LY_STMT_STATUS;
97 ext->substmts[1].storage = &struct_pdata->flags;
98
99 LY_ARRAY_INCREMENT(ext->substmts);
100 ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
101 ext->substmts[2].storage = &struct_pdata->dsc;
102
103 LY_ARRAY_INCREMENT(ext->substmts);
104 ext->substmts[3].stmt = LY_STMT_REFERENCE;
105 ext->substmts[3].storage = &struct_pdata->ref;
106
107 LY_ARRAY_INCREMENT(ext->substmts);
108 ext->substmts[4].stmt = LY_STMT_TYPEDEF;
109 ext->substmts[4].storage = &struct_pdata->typedefs;
110
111 LY_ARRAY_INCREMENT(ext->substmts);
112 ext->substmts[5].stmt = LY_STMT_GROUPING;
113 ext->substmts[5].storage = &struct_pdata->groupings;
114
115 /* data-def-stmt */
116 LY_ARRAY_INCREMENT(ext->substmts);
117 ext->substmts[6].stmt = LY_STMT_CONTAINER;
118 ext->substmts[6].storage = &struct_pdata->child;
119
120 LY_ARRAY_INCREMENT(ext->substmts);
121 ext->substmts[7].stmt = LY_STMT_LEAF;
122 ext->substmts[7].storage = &struct_pdata->child;
123
124 LY_ARRAY_INCREMENT(ext->substmts);
125 ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
126 ext->substmts[8].storage = &struct_pdata->child;
127
128 LY_ARRAY_INCREMENT(ext->substmts);
129 ext->substmts[9].stmt = LY_STMT_LIST;
130 ext->substmts[9].storage = &struct_pdata->child;
131
132 LY_ARRAY_INCREMENT(ext->substmts);
133 ext->substmts[10].stmt = LY_STMT_CHOICE;
134 ext->substmts[10].storage = &struct_pdata->child;
135
136 LY_ARRAY_INCREMENT(ext->substmts);
137 ext->substmts[11].stmt = LY_STMT_ANYDATA;
138 ext->substmts[11].storage = &struct_pdata->child;
139
140 LY_ARRAY_INCREMENT(ext->substmts);
141 ext->substmts[12].stmt = LY_STMT_ANYXML;
142 ext->substmts[12].storage = &struct_pdata->child;
143
144 LY_ARRAY_INCREMENT(ext->substmts);
145 ext->substmts[13].stmt = LY_STMT_USES;
146 ext->substmts[13].storage = &struct_pdata->child;
147
148 rc = lyplg_ext_parse_extension_instance(pctx, ext);
149 return rc;
150
151emem:
152 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
153 return LY_EMEM;
154}
155
156/**
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200157 * @brief Compile structure extension instances.
158 *
159 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
160 */
161static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200162structure_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200163{
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200164 LY_ERR rc;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200165 struct lysc_module *mod_c;
166 const struct lysc_node *child;
Michal Vasko193dacd2022-10-13 08:43:05 +0200167 struct lysc_ext_instance_structure *struct_cdata;
168 uint32_t prev_options = *lyplg_ext_compile_get_options(cctx);
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200169
Michal Vasko193dacd2022-10-13 08:43:05 +0200170 mod_c = ext->parent;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200171
Michal Vasko193dacd2022-10-13 08:43:05 +0200172 /* check identifier namespace with the compiled nodes */
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200173 LY_LIST_FOR(mod_c->data, child) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200174 if (!strcmp(child->name, ext->argument)) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200175 /* identifier collision */
Michal Vasko193dacd2022-10-13 08:43:05 +0200176 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID, "Extension %s collides with a %s with the same identifier.",
177 extp->name, lys_nodetype2str(child->nodetype));
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200178 return LY_EVALID;
179 }
180 }
181
182 /* allocate the storage */
Michal Vasko193dacd2022-10-13 08:43:05 +0200183 struct_cdata = calloc(1, sizeof *struct_cdata);
184 if (!struct_cdata) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200185 goto emem;
186 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200187 ext->compiled = struct_cdata;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200188
189 /* compile substatements */
Michal Vasko193dacd2022-10-13 08:43:05 +0200190 LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 14, rc, emem);
191 LY_ARRAY_INCREMENT(ext->substmts);
192 ext->substmts[0].stmt = LY_STMT_MUST;
193 ext->substmts[0].storage = &struct_cdata->musts;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200194
Michal Vasko193dacd2022-10-13 08:43:05 +0200195 LY_ARRAY_INCREMENT(ext->substmts);
196 ext->substmts[1].stmt = LY_STMT_STATUS;
197 ext->substmts[1].storage = &struct_cdata->flags;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200198
Michal Vasko193dacd2022-10-13 08:43:05 +0200199 LY_ARRAY_INCREMENT(ext->substmts);
200 ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
201 ext->substmts[2].storage = &struct_cdata->dsc;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200202
Michal Vasko193dacd2022-10-13 08:43:05 +0200203 LY_ARRAY_INCREMENT(ext->substmts);
204 ext->substmts[3].stmt = LY_STMT_REFERENCE;
205 ext->substmts[3].storage = &struct_cdata->ref;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200206
Michal Vasko193dacd2022-10-13 08:43:05 +0200207 LY_ARRAY_INCREMENT(ext->substmts);
208 ext->substmts[4].stmt = LY_STMT_TYPEDEF;
209 ext->substmts[4].storage = NULL;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200210
Michal Vasko193dacd2022-10-13 08:43:05 +0200211 LY_ARRAY_INCREMENT(ext->substmts);
212 ext->substmts[5].stmt = LY_STMT_GROUPING;
213 ext->substmts[5].storage = NULL;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200214
215 /* data-def-stmt */
Michal Vasko193dacd2022-10-13 08:43:05 +0200216 LY_ARRAY_INCREMENT(ext->substmts);
217 ext->substmts[6].stmt = LY_STMT_CONTAINER;
218 ext->substmts[6].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200219
Michal Vasko193dacd2022-10-13 08:43:05 +0200220 LY_ARRAY_INCREMENT(ext->substmts);
221 ext->substmts[7].stmt = LY_STMT_LEAF;
222 ext->substmts[7].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200223
Michal Vasko193dacd2022-10-13 08:43:05 +0200224 LY_ARRAY_INCREMENT(ext->substmts);
225 ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
226 ext->substmts[8].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200227
Michal Vasko193dacd2022-10-13 08:43:05 +0200228 LY_ARRAY_INCREMENT(ext->substmts);
229 ext->substmts[9].stmt = LY_STMT_LIST;
230 ext->substmts[9].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200231
Michal Vasko193dacd2022-10-13 08:43:05 +0200232 LY_ARRAY_INCREMENT(ext->substmts);
233 ext->substmts[10].stmt = LY_STMT_CHOICE;
234 ext->substmts[10].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200235
Michal Vasko193dacd2022-10-13 08:43:05 +0200236 LY_ARRAY_INCREMENT(ext->substmts);
237 ext->substmts[11].stmt = LY_STMT_ANYDATA;
238 ext->substmts[11].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200239
Michal Vasko193dacd2022-10-13 08:43:05 +0200240 LY_ARRAY_INCREMENT(ext->substmts);
241 ext->substmts[12].stmt = LY_STMT_ANYXML;
242 ext->substmts[12].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200243
Michal Vasko193dacd2022-10-13 08:43:05 +0200244 LY_ARRAY_INCREMENT(ext->substmts);
245 ext->substmts[13].stmt = LY_STMT_USES;
246 ext->substmts[13].storage = &struct_cdata->child;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200247
Michal Vasko193dacd2022-10-13 08:43:05 +0200248 *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
249 rc = lyplg_ext_compile_extension_instance(cctx, extp, ext);
250 *lyplg_ext_compile_get_options(cctx) = prev_options;
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200251 if (rc) {
252 return rc;
253 }
254
255 return LY_SUCCESS;
256
257emem:
Michal Vasko193dacd2022-10-13 08:43:05 +0200258 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200259 return LY_EMEM;
260}
261
262/**
Michal Vasko220f0552022-11-08 09:04:06 +0100263 * @brief Structure schema info printer.
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200264 *
Michal Vasko941e0562022-10-18 10:35:00 +0200265 * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200266 */
267static LY_ERR
Michal Vasko941e0562022-10-18 10:35:00 +0200268structure_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200269{
Michal Vasko941e0562022-10-18 10:35:00 +0200270 lyplg_ext_print_info_extension_instance(ctx, ext, flag);
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200271 return LY_SUCCESS;
272}
273
274/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200275 * @brief Free parsed structure extension instance data.
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200276 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200277 * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree.
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200278 */
279static void
Michal Vasko193dacd2022-10-13 08:43:05 +0200280structure_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200281{
Michal Vasko193dacd2022-10-13 08:43:05 +0200282 lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
283 free(ext->parsed);
284}
285
286/**
287 * @brief Free compiled structure extension instance data.
288 *
289 * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree.
290 */
291static void
292structure_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
293{
294 lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
295 free(ext->compiled);
296}
297
298/**
299 * @brief Parse augment-structure extension instances.
300 *
301 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
302 */
303static LY_ERR
304structure_aug_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
305{
306 LY_ERR rc;
307 struct lysp_stmt *stmt;
308 struct lysp_ext_instance_augment_structure *aug_pdata;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200309 const struct ly_ctx *ctx = lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx;
Michal Vasko193dacd2022-10-13 08:43:05 +0200310
311 /* augment-structure can appear only at the top level of a YANG module or submodule */
312 if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
313 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID,
314 "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name,
315 lyplg_ext_stmt2str(ext->parent_stmt));
316 return LY_EVALID;
317 }
318
319 /* augment-structure must define some data-def-stmt */
320 LY_LIST_FOR(ext->child, stmt) {
321 if (stmt->kw & LY_STMT_DATA_NODE_MASK) {
322 break;
323 }
324 }
325 if (!stmt) {
326 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s does not define any data-def-stmt statements.",
327 ext->name);
328 return LY_EVALID;
329 }
330
331 /* allocate the storage */
332 aug_pdata = calloc(1, sizeof *aug_pdata);
333 if (!aug_pdata) {
334 goto emem;
335 }
336 ext->parsed = aug_pdata;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200337 LY_ARRAY_CREATE_GOTO(ctx, ext->substmts, 13, rc, emem);
Michal Vasko193dacd2022-10-13 08:43:05 +0200338
339 /* parse substatements */
340 LY_ARRAY_INCREMENT(ext->substmts);
341 ext->substmts[0].stmt = LY_STMT_STATUS;
342 ext->substmts[0].storage = &aug_pdata->flags;
343
344 LY_ARRAY_INCREMENT(ext->substmts);
345 ext->substmts[1].stmt = LY_STMT_DESCRIPTION;
346 ext->substmts[1].storage = &aug_pdata->dsc;
347
348 LY_ARRAY_INCREMENT(ext->substmts);
349 ext->substmts[2].stmt = LY_STMT_REFERENCE;
350 ext->substmts[2].storage = &aug_pdata->ref;
351
352 /* data-def-stmt */
353 LY_ARRAY_INCREMENT(ext->substmts);
354 ext->substmts[3].stmt = LY_STMT_CONTAINER;
355 ext->substmts[3].storage = &aug_pdata->child;
356
357 LY_ARRAY_INCREMENT(ext->substmts);
358 ext->substmts[4].stmt = LY_STMT_LEAF;
359 ext->substmts[4].storage = &aug_pdata->child;
360
361 LY_ARRAY_INCREMENT(ext->substmts);
362 ext->substmts[5].stmt = LY_STMT_LEAF_LIST;
363 ext->substmts[5].storage = &aug_pdata->child;
364
365 LY_ARRAY_INCREMENT(ext->substmts);
366 ext->substmts[6].stmt = LY_STMT_LIST;
367 ext->substmts[6].storage = &aug_pdata->child;
368
369 LY_ARRAY_INCREMENT(ext->substmts);
370 ext->substmts[7].stmt = LY_STMT_CHOICE;
371 ext->substmts[7].storage = &aug_pdata->child;
372
373 LY_ARRAY_INCREMENT(ext->substmts);
374 ext->substmts[8].stmt = LY_STMT_ANYDATA;
375 ext->substmts[8].storage = &aug_pdata->child;
376
377 LY_ARRAY_INCREMENT(ext->substmts);
378 ext->substmts[9].stmt = LY_STMT_ANYXML;
379 ext->substmts[9].storage = &aug_pdata->child;
380
381 LY_ARRAY_INCREMENT(ext->substmts);
382 ext->substmts[10].stmt = LY_STMT_USES;
383 ext->substmts[10].storage = &aug_pdata->child;
384
385 /* case */
386 LY_ARRAY_INCREMENT(ext->substmts);
387 ext->substmts[11].stmt = LY_STMT_CASE;
388 ext->substmts[11].storage = &aug_pdata->child;
389
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200390 if ((rc = lyplg_ext_parse_extension_instance(pctx, ext))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200391 return rc;
392 }
393
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200394 /* add fake parsed augment node */
395 LY_ARRAY_INCREMENT(ext->substmts);
396 ext->substmts[12].stmt = LY_STMT_AUGMENT;
397 ext->substmts[12].storage = &aug_pdata->aug;
Michal Vasko193dacd2022-10-13 08:43:05 +0200398
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200399 aug_pdata->aug = calloc(1, sizeof *aug_pdata->aug);
400 if (!aug_pdata->aug) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200401 goto emem;
402 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200403 aug_pdata->aug->nodetype = LYS_AUGMENT;
404 aug_pdata->aug->flags = aug_pdata->flags;
Michal Vasko5b3edcb2022-11-08 10:50:15 +0100405 if (lydict_insert(ctx, ext->argument, 0, &aug_pdata->aug->nodeid)) {
406 goto emem;
407 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200408 aug_pdata->aug->child = aug_pdata->child;
409 /* avoid double free */
410 aug_pdata->child = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200411
412 return LY_SUCCESS;
413
414emem:
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200415 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
Michal Vasko193dacd2022-10-13 08:43:05 +0200416 return LY_EMEM;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200417}
418
aPiecek03cb4872022-10-24 10:31:51 +0200419static LY_ERR
420structure_sprinter_pnode(const struct lysp_node *UNUSED(node), const void *UNUSED(plugin_priv),
421 ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts))
422{
423 *flags = "";
424 return LY_SUCCESS;
425}
426
427static LY_ERR
428structure_sprinter_cnode(const struct lysc_node *UNUSED(node), const void *UNUSED(plugin_priv),
429 ly_bool *UNUSED(skip), const char **flags, const char **UNUSED(add_opts))
430{
431 *flags = "";
432 return LY_SUCCESS;
433}
434
Michal Vasko220f0552022-11-08 09:04:06 +0100435/**
436 * @brief Structure schema compiled tree printer.
437 *
438 * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree.
439 */
aPiecek03cb4872022-10-24 10:31:51 +0200440static LY_ERR
441structure_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx,
442 const char **UNUSED(flags), const char **UNUSED(add_opts))
443{
444 LY_ERR rc;
445
446 rc = lyplg_ext_sprinter_ctree_add_ext_nodes(ctx, ext, structure_sprinter_cnode);
447 return rc;
448}
449
Michal Vasko220f0552022-11-08 09:04:06 +0100450/**
451 * @brief Structure schema parsed tree printer.
452 *
453 * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree.
454 */
aPiecek03cb4872022-10-24 10:31:51 +0200455static LY_ERR
456structure_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx,
457 const char **UNUSED(flags), const char **UNUSED(add_opts))
458{
459 LY_ERR rc;
460
461 rc = lyplg_ext_sprinter_ptree_add_ext_nodes(ctx, ext, structure_sprinter_pnode);
462 return rc;
463}
464
Michal Vasko220f0552022-11-08 09:04:06 +0100465/**
466 * @brief Augment structure schema parsed tree printer.
467 *
468 * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree.
469 */
aPiecek03cb4872022-10-24 10:31:51 +0200470static LY_ERR
471structure_aug_sprinter_ptree(struct lysp_ext_instance *ext, const struct lyspr_tree_ctx *ctx,
472 const char **UNUSED(flags), const char **UNUSED(add_opts))
473{
474 LY_ERR rc = LY_SUCCESS;
475 struct lysp_node_augment **aug;
476
477 assert(ctx);
478
479 aug = ext->substmts[12].storage;
480 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, (*aug)->child, structure_sprinter_pnode);
481
482 return rc;
483}
484
Michal Vasko220f0552022-11-08 09:04:06 +0100485/**
486 * @brief Augment structure schema compiled tree printer.
487 *
488 * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree.
489 */
aPiecek03cb4872022-10-24 10:31:51 +0200490static LY_ERR
Michal Vasko220f0552022-11-08 09:04:06 +0100491structure_aug_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, const char **flags,
492 const char **add_opts)
aPiecek03cb4872022-10-24 10:31:51 +0200493{
494 LY_ERR rc = LY_SUCCESS;
495
496 LY_ARRAY_COUNT_TYPE i;
497 struct lysp_ext_instance *parsed_ext;
498
499 assert(ctx);
500
Michal Vasko220f0552022-11-08 09:04:06 +0100501 /* find the parsed ext structure */
aPiecek03cb4872022-10-24 10:31:51 +0200502 parsed_ext = ext->module->parsed->exts;
503 LY_ARRAY_FOR(parsed_ext, i) {
Michal Vasko220f0552022-11-08 09:04:06 +0100504 if (!strcmp(parsed_ext[i].name, "sx:augment-structure") && !strcmp(parsed_ext[i].argument, ext->argument)) {
505 break;
aPiecek03cb4872022-10-24 10:31:51 +0200506 }
aPiecek03cb4872022-10-24 10:31:51 +0200507 }
Michal Vasko220f0552022-11-08 09:04:06 +0100508 assert(i < LY_ARRAY_COUNT(parsed_ext));
aPiecek03cb4872022-10-24 10:31:51 +0200509
Michal Vasko220f0552022-11-08 09:04:06 +0100510 /* for augments print the parsed tree */
511 rc = structure_aug_sprinter_ptree(parsed_ext, ctx, flags, add_opts);
aPiecek03cb4872022-10-24 10:31:51 +0200512 return rc;
513}
514
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200515/**
516 * @brief Plugin descriptions for the structure extension
517 *
518 * Note that external plugins are supposed to use:
519 *
520 * LYPLG_EXTENSIONS = {
521 */
522const struct lyplg_ext_record plugins_structure[] = {
523 {
524 .module = "ietf-yang-structure-ext",
525 .revision = "2020-06-17",
526 .name = "structure",
527
Michal Vasko193dacd2022-10-13 08:43:05 +0200528 .plugin.id = "ly2 structure v1",
529 .plugin.parse = structure_parse,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200530 .plugin.compile = structure_compile,
Michal Vasko941e0562022-10-18 10:35:00 +0200531 .plugin.printer_info = structure_printer_info,
aPiecek03cb4872022-10-24 10:31:51 +0200532 .plugin.printer_ctree = structure_sprinter_ctree,
533 .plugin.printer_ptree = structure_sprinter_ptree,
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200534 .plugin.node = NULL,
535 .plugin.snode = NULL,
Michal Vasko193dacd2022-10-13 08:43:05 +0200536 .plugin.validate = NULL,
537 .plugin.pfree = structure_pfree,
538 .plugin.cfree = structure_cfree
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200539 },
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200540 {
541 .module = "ietf-yang-structure-ext",
542 .revision = "2020-06-17",
543 .name = "augment-structure",
544
Michal Vasko193dacd2022-10-13 08:43:05 +0200545 .plugin.id = "ly2 structure v1",
546 .plugin.parse = structure_aug_parse,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200547 .plugin.compile = NULL,
548 .plugin.printer_info = NULL,
aPiecek03cb4872022-10-24 10:31:51 +0200549 .plugin.printer_ctree = structure_aug_sprinter_ctree,
550 .plugin.printer_ptree = structure_aug_sprinter_ptree,
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200551 .plugin.node = NULL,
552 .plugin.snode = NULL,
Michal Vasko193dacd2022-10-13 08:43:05 +0200553 .plugin.validate = NULL,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200554 .plugin.pfree = structure_pfree,
555 .plugin.cfree = NULL
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200556 },
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200557 {0} /* terminating zeroed record */
558};