blob: e7626a28cb367be1a2b9ec2d4c193d7242a56c2a [file] [log] [blame]
Radek Krejci0935f412019-08-20 16:15:18 +02001/**
2 * @file plugins_exts.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoddd76592022-01-17 13:34:48 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief helper functions for extension plugins
Radek Krejci0935f412019-08-20 16:15:18 +02006 *
Michal Vaskoddd76592022-01-17 13:34:48 +01007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejci0935f412019-08-20 16:15:18 +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 Krejci0935f412019-08-20 16:15:18 +020015
16#include "plugins_exts.h"
17
Michal Vasko193dacd2022-10-13 08:43:05 +020018#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Michal Vasko193dacd2022-10-13 08:43:05 +020020#include <stdlib.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021
Michal Vasko193dacd2022-10-13 08:43:05 +020022#include "dict.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010023#include "ly_common.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020024#include "parser_internal.h"
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010025#include "printer_internal.h"
Radek Krejci5f9a3672021-03-05 21:35:22 +010026#include "schema_compile.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020027#include "schema_compile_amend.h"
28#include "schema_compile_node.h"
29#include "schema_features.h"
30#include "tree_schema_internal.h"
31
32LIBYANG_API_DEF const struct lysp_module *
33lyplg_ext_parse_get_cur_pmod(const struct lysp_ctx *pctx)
34{
35 return PARSER_CUR_PMOD(pctx);
36}
37
38LIBYANG_API_DEF LY_ERR
39lyplg_ext_parse_extension_instance(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
40{
41 LY_ERR rc = LY_SUCCESS;
42 LY_ARRAY_COUNT_TYPE u;
43 struct lysp_stmt *stmt;
44
45 /* check for invalid substatements */
46 LY_LIST_FOR(ext->child, stmt) {
47 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
48 continue;
49 }
50 LY_ARRAY_FOR(ext->substmts, u) {
51 if (ext->substmts[u].stmt == stmt->kw) {
52 break;
53 }
54 }
55 if (u == LY_ARRAY_COUNT(ext->substmts)) {
56 LOGVAL(PARSER_CTX(pctx), LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
57 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
58 rc = LY_EVALID;
59 goto cleanup;
60 }
61 }
62
63 /* parse all the known statements */
64 LY_ARRAY_FOR(ext->substmts, u) {
65 LY_LIST_FOR(ext->child, stmt) {
66 if (ext->substmts[u].stmt != stmt->kw) {
67 continue;
68 }
69
70 if ((rc = lys_parse_ext_instance_stmt(pctx, &ext->substmts[u], stmt))) {
71 goto cleanup;
72 }
73 }
74 }
75
76cleanup:
77 return rc;
78}
79
80/**
81 * @brief Compile an instance extension statement.
82 *
83 * @param[in] ctx Compile context.
84 * @param[in] parsed Parsed ext instance substatement structure.
85 * @param[in] ext Compiled ext instance.
86 * @param[in] substmt Compled ext instance substatement info.
Michal Vasko193dacd2022-10-13 08:43:05 +020087 * @return LY_ERR value.
88 */
89static LY_ERR
Michal Vasko708a8bf2024-06-13 09:44:51 +020090lys_compile_ext_instance_stmt(struct lysc_ctx *ctx, uint64_t parsed, struct lysc_ext_instance *ext,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020091 struct lysc_ext_substmt *substmt)
Michal Vasko193dacd2022-10-13 08:43:05 +020092{
93 LY_ERR rc = LY_SUCCESS;
94 ly_bool length_restr = 0;
95 LY_DATA_TYPE basetype;
96
Michal Vaskod595eff2023-02-20 11:29:28 +010097 assert(parsed);
98
Michal Vasko193dacd2022-10-13 08:43:05 +020099 /* compilation wthout any storage */
100 if (substmt->stmt == LY_STMT_IF_FEATURE) {
101 ly_bool enabled;
102
103 /* evaluate */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200104 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, (struct lysp_qname *)parsed, &enabled), cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200105 if (!enabled) {
106 /* it is disabled, remove the whole extension instance */
107 rc = LY_ENOT;
108 }
109 }
110
Michal Vaskod595eff2023-02-20 11:29:28 +0100111 if (!substmt->storage) {
112 /* nothing to store */
Michal Vasko193dacd2022-10-13 08:43:05 +0200113 goto cleanup;
114 }
115
116 switch (substmt->stmt) {
117 case LY_STMT_NOTIFICATION:
118 case LY_STMT_INPUT:
119 case LY_STMT_OUTPUT:
120 case LY_STMT_ACTION:
121 case LY_STMT_RPC:
122 case LY_STMT_ANYDATA:
123 case LY_STMT_ANYXML:
124 case LY_STMT_CASE:
125 case LY_STMT_CHOICE:
126 case LY_STMT_CONTAINER:
127 case LY_STMT_LEAF:
128 case LY_STMT_LEAF_LIST:
129 case LY_STMT_LIST:
130 case LY_STMT_USES: {
131 const uint16_t flags;
132 struct lysp_node *pnodes, *pnode;
133 struct lysc_node *node;
134
Michal Vaskofbd037c2022-11-08 10:34:20 +0100135 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
Michal Vasko193dacd2022-10-13 08:43:05 +0200136 pnodes = (struct lysp_node *)parsed;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200137
138 /* compile nodes */
139 LY_LIST_FOR(pnodes, pnode) {
140 if (pnode->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
141 /* manual compile */
142 node = calloc(1, sizeof(struct lysc_node_action_inout));
143 LY_CHECK_ERR_GOTO(!node, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
144 LY_CHECK_GOTO(rc = lys_compile_node_action_inout(ctx, pnode, node), cleanup);
145 LY_CHECK_GOTO(rc = lys_compile_node_connect(ctx, NULL, node), cleanup);
146 } else {
147 /* ctx->ext substatement storage is used as the document root */
148 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, NULL, flags, NULL), cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200149 }
150 }
151 break;
152 }
153 case LY_STMT_ARGUMENT:
154 case LY_STMT_CONTACT:
155 case LY_STMT_DESCRIPTION:
156 case LY_STMT_ERROR_APP_TAG:
157 case LY_STMT_ERROR_MESSAGE:
158 case LY_STMT_KEY:
159 case LY_STMT_MODIFIER:
160 case LY_STMT_NAMESPACE:
161 case LY_STMT_ORGANIZATION:
162 case LY_STMT_PRESENCE:
163 case LY_STMT_REFERENCE:
164 case LY_STMT_UNITS:
165 /* just make a copy */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200166 LY_CHECK_GOTO(rc = lydict_insert(ctx->ctx, (const char *)parsed, 0, (const char **)substmt->storage), cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200167 break;
168
169 case LY_STMT_BIT:
170 basetype = LY_TYPE_BITS;
171 /* fallthrough */
172 case LY_STMT_ENUM:
173 if (substmt->stmt == LY_STMT_ENUM) {
174 basetype = LY_TYPE_ENUM;
175 }
176
177 /* compile */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200178 rc = lys_compile_type_enums(ctx, (struct lysp_type_enum *)parsed, basetype, NULL,
179 (struct lysc_type_bitenum_item **)substmt->storage);
180 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200181 break;
182
183 case LY_STMT_CONFIG: {
Michal Vasko118eb522023-01-12 11:04:51 +0100184 uint16_t flags;
Michal Vasko193dacd2022-10-13 08:43:05 +0200185
186 if (!(ctx->compile_opts & LYS_COMPILE_NO_CONFIG)) {
Michal Vasko118eb522023-01-12 11:04:51 +0100187 memcpy(&flags, &parsed, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200188 if (flags & LYS_CONFIG_MASK) {
189 /* explicitly set */
Michal Vasko118eb522023-01-12 11:04:51 +0100190 flags |= LYS_SET_CONFIG;
Michal Vasko193dacd2022-10-13 08:43:05 +0200191 } else if (ext->parent_stmt & LY_STMT_DATA_NODE_MASK) {
192 /* inherit */
Michal Vasko118eb522023-01-12 11:04:51 +0100193 flags = ((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_MASK;
Michal Vasko193dacd2022-10-13 08:43:05 +0200194 } else {
195 /* default config */
Michal Vasko118eb522023-01-12 11:04:51 +0100196 flags = LYS_CONFIG_W;
Michal Vasko193dacd2022-10-13 08:43:05 +0200197 }
Michal Vasko708a8bf2024-06-13 09:44:51 +0200198 memcpy((void *)substmt->storage, &flags, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200199 } /* else leave zero */
200 break;
201 }
202 case LY_STMT_MUST: {
Michal Vasko708a8bf2024-06-13 09:44:51 +0200203 const struct lysp_restr *restrs = (struct lysp_restr *)parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200204
205 /* sized array */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200206 COMPILE_ARRAY_GOTO(ctx, restrs, *(struct lysc_must **)substmt->storage, lys_compile_must, rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200207 break;
208 }
209 case LY_STMT_WHEN: {
210 const uint16_t flags;
Michal Vasko708a8bf2024-06-13 09:44:51 +0200211 const struct lysp_when *when = (struct lysp_when *)parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200212
213 /* read compiled status */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100214 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
Michal Vasko193dacd2022-10-13 08:43:05 +0200215
216 /* compile */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200217 LY_CHECK_GOTO(rc = lys_compile_when(ctx, when, flags, NULL, NULL, NULL, (struct lysc_when **)substmt->storage), cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200218 break;
219 }
220 case LY_STMT_FRACTION_DIGITS:
221 case LY_STMT_REQUIRE_INSTANCE:
222 /* just make a copy */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200223 memcpy((void *)substmt->storage, &parsed, 1);
Michal Vasko193dacd2022-10-13 08:43:05 +0200224 break;
225
226 case LY_STMT_MANDATORY:
227 case LY_STMT_ORDERED_BY:
228 case LY_STMT_STATUS:
229 /* just make a copy */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200230 memcpy((void *)substmt->storage, &parsed, 2);
Michal Vasko193dacd2022-10-13 08:43:05 +0200231 break;
232
233 case LY_STMT_MAX_ELEMENTS:
234 case LY_STMT_MIN_ELEMENTS:
235 /* just make a copy */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200236 memcpy((void *)substmt->storage, &parsed, 4);
Michal Vasko193dacd2022-10-13 08:43:05 +0200237 break;
238
239 case LY_STMT_POSITION:
240 case LY_STMT_VALUE:
241 /* just make a copy */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200242 memcpy((void *)substmt->storage, &parsed, 8);
Michal Vasko193dacd2022-10-13 08:43:05 +0200243 break;
244
245 case LY_STMT_IDENTITY:
246 /* compile */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200247 rc = lys_identity_precompile(ctx, NULL, NULL, (struct lysp_ident *)parsed, (struct lysc_ident **)substmt->storage);
248 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200249 break;
250
251 case LY_STMT_LENGTH:
252 length_restr = 1;
253 /* fallthrough */
254 case LY_STMT_RANGE:
255 /* compile, use uint64 default range */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200256 rc = lys_compile_type_range(ctx, (struct lysp_restr *)parsed, LY_TYPE_UINT64, length_restr, 0, NULL,
257 (struct lysc_range **)substmt->storage);
258 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200259 break;
260
261 case LY_STMT_PATTERN:
262 /* compile */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200263 rc = lys_compile_type_patterns(ctx, (struct lysp_restr *)parsed, NULL, (struct lysc_pattern ***)substmt->storage);
264 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200265 break;
266
267 case LY_STMT_TYPE: {
268 const uint16_t flags;
269 const char *units;
Michal Vasko708a8bf2024-06-13 09:44:51 +0200270 const struct lysp_type *ptype = (struct lysp_type *)parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200271
272 /* read compiled info */
Michal Vaskofbd037c2022-11-08 10:34:20 +0100273 lyplg_ext_get_storage(ext, LY_STMT_STATUS, sizeof flags, (const void **)&flags);
274 lyplg_ext_get_storage(ext, LY_STMT_UNITS, sizeof units, (const void **)&units);
Michal Vasko193dacd2022-10-13 08:43:05 +0200275
276 /* compile */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200277 rc = lys_compile_type(ctx, NULL, flags, ext->def->name, ptype, (struct lysc_type **)substmt->storage, &units, NULL);
278 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod595eff2023-02-20 11:29:28 +0100279 LY_ATOMIC_INC_BARRIER((*(struct lysc_type **)substmt->storage)->refcount);
Michal Vasko193dacd2022-10-13 08:43:05 +0200280 break;
281 }
282 case LY_STMT_EXTENSION_INSTANCE: {
283 struct lysp_ext_instance *extps = (struct lysp_ext_instance *)parsed;
Michal Vasko193dacd2022-10-13 08:43:05 +0200284
285 /* compile sized array */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200286 COMPILE_EXTS_GOTO(ctx, extps, *(struct lysc_ext_instance **)substmt->storage, ext, rc, cleanup);
Michal Vasko193dacd2022-10-13 08:43:05 +0200287 break;
288 }
289 case LY_STMT_AUGMENT:
290 case LY_STMT_GROUPING:
291 case LY_STMT_BASE:
292 case LY_STMT_BELONGS_TO:
293 case LY_STMT_DEFAULT:
294 case LY_STMT_DEVIATE:
295 case LY_STMT_DEVIATION:
296 case LY_STMT_EXTENSION:
297 case LY_STMT_FEATURE:
298 case LY_STMT_IF_FEATURE:
299 case LY_STMT_IMPORT:
300 case LY_STMT_INCLUDE:
301 case LY_STMT_MODULE:
302 case LY_STMT_PATH:
303 case LY_STMT_PREFIX:
304 case LY_STMT_REFINE:
305 case LY_STMT_REVISION:
306 case LY_STMT_REVISION_DATE:
307 case LY_STMT_SUBMODULE:
308 case LY_STMT_TYPEDEF:
309 case LY_STMT_UNIQUE:
310 case LY_STMT_YANG_VERSION:
311 case LY_STMT_YIN_ELEMENT:
312 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" compilation is not supported.", lyplg_ext_stmt2str(substmt->stmt));
313 rc = LY_EVALID;
314 goto cleanup;
315
316 default:
317 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension "
318 "(found in \"%s%s%s\") substatement.", lyplg_ext_stmt2str(substmt->stmt), ext->def->name,
319 ext->argument ? " " : "", ext->argument ? ext->argument : "");
320 rc = LY_EVALID;
321 goto cleanup;
322 }
323
324cleanup:
325 return rc;
326}
327
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200328LIBYANG_API_DEF LY_ERR
329lyplg_ext_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *extp,
330 struct lysc_ext_instance *ext)
Michal Vasko193dacd2022-10-13 08:43:05 +0200331{
332 LY_ERR rc = LY_SUCCESS;
333 LY_ARRAY_COUNT_TYPE u, v;
334 enum ly_stmt stmtp;
Michal Vasko708a8bf2024-06-13 09:44:51 +0200335 uint64_t storagep;
Michal Vasko193dacd2022-10-13 08:43:05 +0200336 struct ly_set storagep_compiled = {0};
337
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200338 LY_CHECK_ARG_RET(ctx ? ctx->ctx : NULL, ctx, extp, ext, LY_EINVAL);
339
Michal Vasko193dacd2022-10-13 08:43:05 +0200340 /* note into the compile context that we are processing extension now */
341 ctx->ext = ext;
342
343 LY_ARRAY_FOR(extp->substmts, u) {
344 stmtp = extp->substmts[u].stmt;
Michal Vasko708a8bf2024-06-13 09:44:51 +0200345 storagep = *(uint64_t *)extp->substmts[u].storage;
Michal Vasko193dacd2022-10-13 08:43:05 +0200346
Michal Vasko708a8bf2024-06-13 09:44:51 +0200347 if (!storagep || ly_set_contains(&storagep_compiled, (void *)storagep, NULL)) {
Michal Vaskod595eff2023-02-20 11:29:28 +0100348 /* nothing parsed or already compiled (for example, if it is a linked list of parsed nodes) */
Michal Vasko193dacd2022-10-13 08:43:05 +0200349 continue;
350 }
351
352 LY_ARRAY_FOR(ext->substmts, v) {
353 if (stmtp != ext->substmts[v].stmt) {
354 continue;
355 }
356
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200357 if ((rc = lys_compile_ext_instance_stmt(ctx, storagep, ext, &ext->substmts[v]))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200358 goto cleanup;
359 }
360
361 /* parsed substatement compiled */
362 break;
363 }
364
365 /* compiled */
Michal Vasko708a8bf2024-06-13 09:44:51 +0200366 ly_set_add(&storagep_compiled, (void *)storagep, 1, NULL);
Michal Vasko193dacd2022-10-13 08:43:05 +0200367 }
368
369cleanup:
370 ctx->ext = NULL;
371 ly_set_erase(&storagep_compiled, NULL);
372 return rc;
373}
374
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100375LIBYANG_API_DEF struct ly_ctx *
Michal Vasko193dacd2022-10-13 08:43:05 +0200376lyplg_ext_compile_get_ctx(const struct lysc_ctx *ctx)
Radek Krejci5f9a3672021-03-05 21:35:22 +0100377{
378 return ctx->ctx;
379}
380
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100381LIBYANG_API_DEF uint32_t *
Michal Vasko193dacd2022-10-13 08:43:05 +0200382lyplg_ext_compile_get_options(const struct lysc_ctx *ctx)
Radek Krejci5f9a3672021-03-05 21:35:22 +0100383{
Michal Vasko7c565922021-06-10 14:58:27 +0200384 return &((struct lysc_ctx *)ctx)->compile_opts;
Radek Krejci5f9a3672021-03-05 21:35:22 +0100385}
386
Michal Vasko6a914fb2022-01-17 10:36:14 +0100387LIBYANG_API_DEF const struct lys_module *
Michal Vasko193dacd2022-10-13 08:43:05 +0200388lyplg_ext_compile_get_cur_mod(const struct lysc_ctx *ctx)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100389{
390 return ctx->cur_mod;
391}
392
Michal Vasko6a914fb2022-01-17 10:36:14 +0100393LIBYANG_API_DEF struct lysp_module *
Michal Vasko193dacd2022-10-13 08:43:05 +0200394lyplg_ext_compile_get_pmod(const struct lysc_ctx *ctx)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100395{
396 return ctx->pmod;
397}
Michal Vaskoddd76592022-01-17 13:34:48 +0100398
Michal Vasko193dacd2022-10-13 08:43:05 +0200399LIBYANG_API_DEF struct ly_out **
400lyplg_ext_print_get_out(const struct lyspr_ctx *ctx)
401{
402 return &((struct lyspr_ctx *)ctx)->out;
403}
404
405LIBYANG_API_DEF uint32_t *
406lyplg_ext_print_get_options(const struct lyspr_ctx *ctx)
407{
408 return &((struct lyspr_ctx *)ctx)->options;
409}
410
411LIBYANG_API_DEF uint16_t *
412lyplg_ext_print_get_level(const struct lyspr_ctx *ctx)
413{
414 return &((struct lyspr_ctx *)ctx)->level;
415}
416
aPiecek03cb4872022-10-24 10:31:51 +0200417LIBYANG_API_DECL LY_ERR
418lyplg_ext_sprinter_ctree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_ext_instance *ext,
419 lyplg_ext_sprinter_ctree_override_clb clb)
420{
421 LY_ERR rc = LY_SUCCESS;
422 uint32_t i;
423 struct lysc_node *schema;
424
425 LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL);
426
427 LY_ARRAY_FOR(ext->substmts, i) {
428 switch (ext->substmts[i].stmt) {
429 case LY_STMT_NOTIFICATION:
430 case LY_STMT_INPUT:
431 case LY_STMT_OUTPUT:
432 case LY_STMT_ACTION:
433 case LY_STMT_RPC:
434 case LY_STMT_ANYDATA:
435 case LY_STMT_ANYXML:
436 case LY_STMT_CASE:
437 case LY_STMT_CHOICE:
438 case LY_STMT_CONTAINER:
439 case LY_STMT_LEAF:
440 case LY_STMT_LEAF_LIST:
441 case LY_STMT_LIST:
442 schema = *((struct lysc_node **)ext->substmts[i].storage);
443 if (schema) {
444 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, schema, clb);
445 return rc;
446 }
447 default:
448 break;
449 }
450 }
451
452 return rc;
453}
454
455LIBYANG_API_DECL LY_ERR
456lyplg_ext_sprinter_ptree_add_ext_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_ext_instance *ext,
457 lyplg_ext_sprinter_ptree_override_clb clb)
458{
459 LY_ERR rc = LY_SUCCESS;
460 uint32_t i;
461 struct lysp_node *schema;
462
463 LY_CHECK_ARG_RET2(NULL, ctx, ext, LY_EINVAL);
464
465 LY_ARRAY_FOR(ext->substmts, i) {
466 switch (ext->substmts[i].stmt) {
467 case LY_STMT_NOTIFICATION:
468 case LY_STMT_INPUT:
469 case LY_STMT_OUTPUT:
470 case LY_STMT_ACTION:
471 case LY_STMT_RPC:
472 case LY_STMT_ANYDATA:
473 case LY_STMT_ANYXML:
474 case LY_STMT_CASE:
475 case LY_STMT_CHOICE:
476 case LY_STMT_CONTAINER:
477 case LY_STMT_LEAF:
478 case LY_STMT_LEAF_LIST:
479 case LY_STMT_LIST:
480 schema = *((struct lysp_node **)ext->substmts[i].storage);
481 if (schema) {
482 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, schema, clb);
483 return rc;
484 }
485 default:
486 break;
487 }
488 }
489
490 return rc;
491}
492
493LIBYANG_API_DECL LY_ERR
494lyplg_ext_sprinter_ctree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysc_node *nodes,
495 lyplg_ext_sprinter_ctree_override_clb clb)
496{
497 struct lyspr_tree_schema *new;
498
499 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
500
501 if (!nodes) {
502 return LY_SUCCESS;
503 }
504
505 LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM);
506 new->compiled = 1;
507 new->ctree = nodes;
508 new->cn_overr = clb;
509
510 return LY_SUCCESS;
511}
512
513LIBYANG_API_DECL LY_ERR
514lyplg_ext_sprinter_ptree_add_nodes(const struct lyspr_tree_ctx *ctx, struct lysp_node *nodes,
515 lyplg_ext_sprinter_ptree_override_clb clb)
516{
517 struct lyspr_tree_schema *new;
518
519 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
520
521 if (!nodes) {
522 return LY_SUCCESS;
523 }
524
525 LY_ARRAY_NEW_RET(NULL, ((struct lyspr_tree_ctx *)ctx)->schemas, new, LY_EMEM);
526 new->compiled = 0;
527 new->ptree = nodes;
528 new->pn_overr = clb;
529
530 return LY_SUCCESS;
531}
532
533LIBYANG_API_DECL LY_ERR
534lyplg_ext_sprinter_tree_set_priv(const struct lyspr_tree_ctx *ctx, void *plugin_priv, void (*free_clb)(void *plugin_priv))
535{
536 LY_CHECK_ARG_RET1(NULL, ctx, LY_EINVAL);
537
538 ((struct lyspr_tree_ctx *)ctx)->plugin_priv = plugin_priv;
539 ((struct lyspr_tree_ctx *)ctx)->free_plugin_priv = free_clb;
540
541 return LY_SUCCESS;
542}
543
Michal Vasko193dacd2022-10-13 08:43:05 +0200544LIBYANG_API_DEF const char *
545lyplg_ext_stmt2str(enum ly_stmt stmt)
546{
547 if (stmt == LY_STMT_EXTENSION_INSTANCE) {
548 return "extension instance";
549 } else {
Michal Vaskob872d0f2022-12-08 09:36:54 +0100550 return lys_stmt_str(stmt);
Michal Vasko193dacd2022-10-13 08:43:05 +0200551 }
552}
553
554LIBYANG_API_DEF enum ly_stmt
555lyplg_ext_nodetype2stmt(uint16_t nodetype)
556{
557 switch (nodetype) {
558 case LYS_CONTAINER:
559 return LY_STMT_CONTAINER;
560 case LYS_CHOICE:
561 return LY_STMT_CHOICE;
562 case LYS_LEAF:
563 return LY_STMT_LEAF;
564 case LYS_LEAFLIST:
565 return LY_STMT_LEAF_LIST;
566 case LYS_LIST:
567 return LY_STMT_LIST;
568 case LYS_ANYXML:
569 return LY_STMT_ANYXML;
570 case LYS_ANYDATA:
571 return LY_STMT_ANYDATA;
572 case LYS_CASE:
573 return LY_STMT_CASE;
574 case LYS_RPC:
575 return LY_STMT_RPC;
576 case LYS_ACTION:
577 return LY_STMT_ACTION;
578 case LYS_NOTIF:
579 return LY_STMT_NOTIFICATION;
580 case LYS_USES:
581 return LY_STMT_USES;
582 case LYS_INPUT:
583 return LY_STMT_INPUT;
584 case LYS_OUTPUT:
585 return LY_STMT_OUTPUT;
586 default:
587 return LY_STMT_NONE;
588 }
589}
590
591LY_ERR
Michal Vasko708a8bf2024-06-13 09:44:51 +0200592lyplg_ext_get_storage_p(const struct lysc_ext_instance *ext, int stmt, uint64_t *storage_p)
Michal Vasko193dacd2022-10-13 08:43:05 +0200593{
594 LY_ARRAY_COUNT_TYPE u;
595 enum ly_stmt match = 0;
596
Michal Vasko708a8bf2024-06-13 09:44:51 +0200597 *storage_p = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +0200598
599 if (!(stmt & LY_STMT_NODE_MASK)) {
600 /* matching a non-node statement */
601 match = stmt;
602 }
603
604 LY_ARRAY_FOR(ext->substmts, u) {
605 if ((match && (ext->substmts[u].stmt == match)) || (!match && (ext->substmts[u].stmt & stmt))) {
606 *storage_p = ext->substmts[u].storage;
607 return LY_SUCCESS;
608 }
609 }
610
611 return LY_ENOT;
612}
613
614LIBYANG_API_DEF LY_ERR
Michal Vaskofbd037c2022-11-08 10:34:20 +0100615lyplg_ext_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage)
Michal Vasko193dacd2022-10-13 08:43:05 +0200616{
Michal Vaskob9878502022-11-11 10:00:05 +0100617 LY_ERR rc = LY_SUCCESS;
Michal Vasko708a8bf2024-06-13 09:44:51 +0200618 uint64_t s;
Michal Vasko193dacd2022-10-13 08:43:05 +0200619
Michal Vasko250ffba2022-11-08 11:31:05 +0100620 /* get pointer to the storage, is set even on error */
Michal Vaskob9878502022-11-11 10:00:05 +0100621 rc = lyplg_ext_get_storage_p(ext, stmt, &s);
Michal Vasko193dacd2022-10-13 08:43:05 +0200622
Michal Vasko250ffba2022-11-08 11:31:05 +0100623 /* assign */
Michal Vaskob9878502022-11-11 10:00:05 +0100624 if (s) {
Michal Vasko708a8bf2024-06-13 09:44:51 +0200625 memcpy(storage, (void *)s, storage_size);
Michal Vaskob9878502022-11-11 10:00:05 +0100626 } else {
627 memset(storage, 0, storage_size);
628 }
629
630 return rc;
Michal Vasko193dacd2022-10-13 08:43:05 +0200631}
632
633LIBYANG_API_DEF LY_ERR
Michal Vaskofbd037c2022-11-08 10:34:20 +0100634lyplg_ext_parsed_get_storage(const struct lysc_ext_instance *ext, int stmt, uint32_t storage_size, const void **storage)
Michal Vasko193dacd2022-10-13 08:43:05 +0200635{
636 LY_ARRAY_COUNT_TYPE u;
637 const struct lysp_ext_instance *extp = NULL;
638 enum ly_stmt match = 0;
Michal Vasko708a8bf2024-06-13 09:44:51 +0200639 uint64_t s = 0;
Michal Vasko193dacd2022-10-13 08:43:05 +0200640
Michal Vasko193dacd2022-10-13 08:43:05 +0200641 /* find the parsed ext instance */
642 LY_ARRAY_FOR(ext->module->parsed->exts, u) {
643 extp = &ext->module->parsed->exts[u];
644
645 if (ext->def == extp->def->compiled) {
646 break;
647 }
648 extp = NULL;
649 }
650 assert(extp);
651
652 if (!(stmt & LY_STMT_NODE_MASK)) {
653 /* matching a non-node statement */
654 match = stmt;
655 }
656
657 /* get the substatement */
658 LY_ARRAY_FOR(extp->substmts, u) {
659 if ((match && (extp->substmts[u].stmt == match)) || (!match && (extp->substmts[u].stmt & stmt))) {
Michal Vaskofbd037c2022-11-08 10:34:20 +0100660 s = extp->substmts[u].storage;
661 break;
Michal Vasko193dacd2022-10-13 08:43:05 +0200662 }
663 }
664
Michal Vasko250ffba2022-11-08 11:31:05 +0100665 /* assign */
Michal Vaskob9878502022-11-11 10:00:05 +0100666 if (s) {
Michal Vasko708a8bf2024-06-13 09:44:51 +0200667 memcpy(storage, (void *)s, storage_size);
Michal Vaskob9878502022-11-11 10:00:05 +0100668 } else {
669 memset(storage, 0, storage_size);
670 }
671
672 return LY_SUCCESS;
Michal Vasko193dacd2022-10-13 08:43:05 +0200673}
674
Michal Vaskoddd76592022-01-17 13:34:48 +0100675LIBYANG_API_DEF LY_ERR
676lyplg_ext_get_data(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, void **ext_data, ly_bool *ext_data_free)
677{
Michal Vasko40a7a442022-12-09 13:01:38 +0100678 LY_ERR rc;
679
Michal Vaskoddd76592022-01-17 13:34:48 +0100680 if (!ctx->ext_clb) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200681 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Failed to get extension data, no callback set.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100682 return LY_EINVAL;
683 }
684
Michal Vasko40a7a442022-12-09 13:01:38 +0100685 if ((rc = ctx->ext_clb(ext, ctx->ext_clb_data, ext_data, ext_data_free))) {
686 lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Callback for getting ext data failed.");
687 }
688 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100689}