blob: c768b5fc002bd2d3bf2f2fb0eaeede15ee4c75d0 [file] [log] [blame]
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001/**
2 * @file schema_mount.c
3 * @author Tadeas Vintrlik <xvintr04@stud.fit.vutbr.cz>
4 * @brief libyang extension plugin - Schema Mount (RFC 8528)
5 *
6 * Copyright (c) 2021 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#define _GNU_SOURCE
16
17#include <assert.h>
Michal Vaskoddd76592022-01-17 13:34:48 +010018#include <pthread.h>
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
Michal Vaskofbbea932022-06-07 11:00:55 +020023#include "common.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020024#include "compat.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010025#include "dict.h"
26#include "libyang.h"
27#include "log.h"
Michal Vaskofbbea932022-06-07 11:00:55 +020028#include "parser_data.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010029#include "plugins_exts.h"
Michal Vasko8cc3f662022-03-29 11:25:51 +020030#include "plugins_types.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010031#include "tree_data.h"
32#include "tree_schema.h"
33
34/**
Michal Vaskoddd76592022-01-17 13:34:48 +010035 * @brief Internal schema mount data structure for holding all the contexts of parsed data.
36 */
37struct lyplg_ext_sm {
38 struct lyplg_ext_sm_shared {
39 pthread_mutex_t lock; /**< lock for accessing this shared structure */
40
41 struct {
42 struct ly_ctx *ctx; /**< context shared between all data of this mount point */
43 const char *mount_point; /**< mount point name */
44 const char *content_id; /**< yang-library content-id (alternatively module-set-id),
45 stored in the dictionary of the ext instance context */
46 } *schemas; /**< array of shared schema schemas */
47 uint32_t schema_count; /**< length of schemas array */
48 uint32_t ref_count; /**< number of references to this structure (mount-points with the same name
49 in the module) */
50 } *shared; /**< shared schema mount points */
51
52 struct lyplg_ext_sm_inln {
53 struct {
54 struct ly_ctx *ctx; /**< context created for single inline schema data */
55 } *schemas; /**< array of inline schemas */
56 uint32_t schema_count; /**< length of schemas array */
57 } inln; /**< inline mount points */
58};
59
aPiecek03cb4872022-10-24 10:31:51 +020060struct sprinter_tree_priv {
61 struct ly_ctx *ext_ctx;
62 struct ly_set *refs;
63};
64
Michal Vasko193dacd2022-10-13 08:43:05 +020065#define EXT_LOGERR_MEM_RET(cctx, ext) \
66 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
Michal Vaskoddd76592022-01-17 13:34:48 +010067 return LY_EMEM
68
Michal Vasko193dacd2022-10-13 08:43:05 +020069#define EXT_LOGERR_MEM_GOTO(cctx, ext, rc, label) \
70 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
Michal Vaskoddd76592022-01-17 13:34:48 +010071 rc = LY_EMEM; \
72 goto label
73
Michal Vasko193dacd2022-10-13 08:43:05 +020074#define EXT_LOGERR_INT_RET(cctx, ext) \
75 lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__); \
Michal Vaskoddd76592022-01-17 13:34:48 +010076 return LY_EINT
77
78/**
Michal Vasko193dacd2022-10-13 08:43:05 +020079 * @brief Check if given mount point is unique among its siblings
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010080 *
Michal Vasko193dacd2022-10-13 08:43:05 +020081 * @param[in] pctx Parse context.
82 * @param[in] ext Parsed extension instance.
Michal Vaskoddd76592022-01-17 13:34:48 +010083 * @return LY_SUCCESS if is unique;
84 * @return LY_EINVAL otherwise.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010085 */
86static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +020087schema_mount_parse_unique_mp(struct lysp_ctx *pctx, const struct lysp_ext_instance *ext)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010088{
Michal Vasko193dacd2022-10-13 08:43:05 +020089 struct lysp_ext_instance *exts;
Michal Vaskoddd76592022-01-17 13:34:48 +010090 LY_ARRAY_COUNT_TYPE u;
Michal Vasko193dacd2022-10-13 08:43:05 +020091 struct lysp_node *parent;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010092
Michal Vasko193dacd2022-10-13 08:43:05 +020093 /* check if it is the only instance of the mount-point among its siblings */
94 parent = ext->parent;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010095 exts = parent->exts;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010096 LY_ARRAY_FOR(exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +020097 if (&exts[u] == ext) {
Michal Vaskoddd76592022-01-17 13:34:48 +010098 continue;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010099 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100100
Michal Vasko193dacd2022-10-13 08:43:05 +0200101 if (!strcmp(exts[u].name, ext->name)) {
102 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Multiple extension \"%s\" instances.", ext->name);
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100103 return LY_EINVAL;
104 }
105 }
106 return LY_SUCCESS;
107}
108
Michal Vasko193dacd2022-10-13 08:43:05 +0200109/**
110 * @brief Schema mount parse.
111 * Checks if it can be a valid extension instance for yang schema mount.
112 *
113 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
114 */
115static LY_ERR
116schema_mount_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
117{
118 /* check YANG version 1.1 */
119 if (lyplg_ext_parse_get_cur_pmod(pctx)->version != LYS_VERSION_1_1) {
120 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance not allowed in YANG version 1 module.",
121 ext->name);
122 return LY_EINVAL;
123 }
124
125 /* check parent nodetype */
126 if ((ext->parent_stmt != LY_STMT_CONTAINER) && (ext->parent_stmt != LY_STMT_LIST)) {
127 lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance allowed only in container or list statement.",
128 ext->name);
129 return LY_EINVAL;
130 }
131
132 /* check uniqueness */
133 if (schema_mount_parse_unique_mp(pctx, ext)) {
134 return LY_EINVAL;
135 }
136
137 /* nothing to actually parse */
138 return LY_SUCCESS;
139}
140
Michal Vaskoddd76592022-01-17 13:34:48 +0100141struct lyplg_ext_sm_shared_cb_data {
142 const struct lysc_ext_instance *ext;
143 struct lyplg_ext_sm_shared *sm_shared;
144};
145
146static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200147schema_mount_compile_mod_dfs_cb(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
Michal Vaskoddd76592022-01-17 13:34:48 +0100148{
149 struct lyplg_ext_sm_shared_cb_data *cb_data = data;
150 struct lyplg_ext_sm *sm_data;
151 struct lysc_ext_instance *exts;
152 LY_ARRAY_COUNT_TYPE u;
153
Michal Vaskoddd76592022-01-17 13:34:48 +0100154 if (node == cb_data->ext->parent) {
155 /* parent of the current compiled extension, skip */
156 return LY_SUCCESS;
157 }
158
159 /* find the same mount point */
160 exts = node->exts;
161 LY_ARRAY_FOR(exts, u) {
162 if (!strcmp(exts[u].def->module->name, "ietf-yang-schema-mount") && !strcmp(exts[u].def->name, "mount-point") &&
163 (exts[u].argument == cb_data->ext->argument)) {
164 /* same mount point, break the DFS search */
Michal Vasko193dacd2022-10-13 08:43:05 +0200165 sm_data = exts[u].compiled;
Michal Vaskoddd76592022-01-17 13:34:48 +0100166 cb_data->sm_shared = sm_data->shared;
167 return LY_EEXIST;
168 }
169 }
170
171 /* not found, continue search */
172 return LY_SUCCESS;
173}
174
175static struct lyplg_ext_sm_shared *
176schema_mount_compile_find_shared(const struct lys_module *mod, const struct lysc_ext_instance *ext)
177{
178 struct lyplg_ext_sm_shared_cb_data cb_data;
179 LY_ERR r;
180
181 /* prepare cb_data */
182 cb_data.ext = ext;
183 cb_data.sm_shared = NULL;
184
185 /* try to find the same mount point */
186 r = lysc_module_dfs_full(mod, schema_mount_compile_mod_dfs_cb, &cb_data);
Michal Vasko2a12cd92022-02-14 09:34:54 +0100187 (void)r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100188 assert((!r && !cb_data.sm_shared) || ((r == LY_EEXIST) && cb_data.sm_shared));
189
190 return cb_data.sm_shared;
191}
192
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100193/**
194 * @brief Schema mount compile.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100195 * Checks if it can be a valid extension instance for yang schema mount.
196 *
197 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
198 */
199static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200200schema_mount_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *UNUSED(extp), struct lysc_ext_instance *ext)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100201{
Michal Vaskodb641792022-03-21 10:04:54 +0100202 const struct lysc_node *node;
Michal Vaskoddd76592022-01-17 13:34:48 +0100203 struct lyplg_ext_sm *sm_data;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100204
Michal Vaskoddd76592022-01-17 13:34:48 +0100205 /* init internal data */
206 sm_data = calloc(1, sizeof *sm_data);
207 if (!sm_data) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200208 EXT_LOGERR_MEM_RET(cctx, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100209 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200210 ext->compiled = sm_data;
Michal Vaskoddd76592022-01-17 13:34:48 +0100211
Michal Vaskodb641792022-03-21 10:04:54 +0100212 /* find the owner module */
Michal Vasko193dacd2022-10-13 08:43:05 +0200213 node = ext->parent;
Michal Vaskodb641792022-03-21 10:04:54 +0100214 while (node->parent) {
215 node = node->parent;
216 }
217
Michal Vaskoddd76592022-01-17 13:34:48 +0100218 /* reuse/init shared schema */
Michal Vasko193dacd2022-10-13 08:43:05 +0200219 sm_data->shared = schema_mount_compile_find_shared(node->module, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100220 if (sm_data->shared) {
221 ++sm_data->shared->ref_count;
222 } else {
223 sm_data->shared = calloc(1, sizeof *sm_data->shared);
224 if (!sm_data->shared) {
225 free(sm_data);
Michal Vasko193dacd2022-10-13 08:43:05 +0200226 EXT_LOGERR_MEM_RET(cctx, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100227 }
228 pthread_mutex_init(&sm_data->shared->lock, NULL);
229 sm_data->shared->ref_count = 1;
230 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100231
232 return LY_SUCCESS;
233}
234
235/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100236 * @brief Learn details about the current mount point.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100237 *
Michal Vaskoddd76592022-01-17 13:34:48 +0100238 * @param[in] ext Compiled extension instance.
239 * @param[in] ext_data Extension data retrieved by the callback.
240 * @param[out] config Whether the whole schema should keep its config or be set to false.
Michal Vasko266308d2022-11-08 08:45:31 +0100241 * @param[out] shared Optional flag whether the schema is shared or inline.
Michal Vaskoddd76592022-01-17 13:34:48 +0100242 * @return LY_ERR value.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100243 */
244static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100245schema_mount_get_smount(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool *config,
246 ly_bool *shared)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100247{
Michal Vaskoddd76592022-01-17 13:34:48 +0100248 struct lyd_node *mpoint, *node;
249 char *path = NULL;
250 LY_ERR r;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100251
Michal Vaskoddd76592022-01-17 13:34:48 +0100252 /* find the mount point */
253 if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']", ext->module->name,
254 ext->argument) == -1) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200255 EXT_LOGERR_MEM_RET(NULL, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100256 }
257 r = ext_data ? lyd_find_path(ext_data, path, 0, &mpoint) : LY_ENOTFOUND;
258 free(path);
259 if (r) {
260 /* missing mount-point, cannot be data for this extension (https://datatracker.ietf.org/doc/html/rfc8528#page-10) */
261 return LY_ENOT;
262 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100263
Michal Vaskoddd76592022-01-17 13:34:48 +0100264 /* check config */
Michal Vasko9e12ffe2022-09-21 15:11:30 +0200265 *config = 1;
Michal Vaskoddd76592022-01-17 13:34:48 +0100266 if (!lyd_find_path(mpoint, "config", 0, &node) && !strcmp(lyd_get_value(node), "false")) {
267 *config = 0;
Michal Vasko9e12ffe2022-09-21 15:11:30 +0200268 }
269 assert((ext->parent_stmt == LY_STMT_CONTAINER) || (ext->parent_stmt == LY_STMT_LIST));
270 if (((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_R) {
271 *config = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100272 }
273
Michal Vasko266308d2022-11-08 08:45:31 +0100274 if (shared) {
275 /* check schema-ref */
276 if (lyd_find_path(mpoint, "shared-schema", 0, NULL)) {
277 if (lyd_find_path(mpoint, "inline", 0, NULL)) {
278 EXT_LOGERR_INT_RET(NULL, ext);
279 }
280 *shared = 0;
281 } else {
282 *shared = 1;
Michal Vaskoddd76592022-01-17 13:34:48 +0100283 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100284 }
285
286 return LY_SUCCESS;
287}
288
289/**
290 * @brief Create schema (context) based on retrieved extension data.
291 *
292 * @param[in] ext Compiled extension instance.
293 * @param[in] ext_data Extension data retrieved by the callback.
294 * @param[in] config Whether the whole schema should keep its config or be set to false.
295 * @param[out] ext_ctx Schema to use for parsing the data.
296 * @return LY_ERR value.
297 */
298static LY_ERR
299schema_mount_create_ctx(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
300 struct ly_ctx **ext_ctx)
301{
Michal Vasko0fca45c2022-06-07 10:57:32 +0200302 LY_ERR rc = LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +0100303 const char * const *searchdirs;
Michal Vasko0fca45c2022-06-07 10:57:32 +0200304 char *sdirs = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100305 const struct lys_module *mod;
306 struct lysc_node *root, *node;
Michal Vasko0fca45c2022-06-07 10:57:32 +0200307 uint32_t i, idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100308
309 /* get searchdirs from the current context */
310 searchdirs = ly_ctx_get_searchdirs(ext->module->ctx);
311
Michal Vasko0fca45c2022-06-07 10:57:32 +0200312 if (searchdirs) {
313 /* append them all into a single string */
314 for (i = 0; searchdirs[i]; ++i) {
Jan Kundrát1fbc90d2022-06-07 21:27:53 +0200315 if ((rc = ly_strcat(&sdirs, "%s" PATH_SEPARATOR, searchdirs[i]))) {
Michal Vasko0fca45c2022-06-07 10:57:32 +0200316 goto cleanup;
317 }
318 }
319 }
320
Michal Vaskoddd76592022-01-17 13:34:48 +0100321 /* create the context based on the data */
Michal Vasko0fca45c2022-06-07 10:57:32 +0200322 if ((rc = ly_ctx_new_yldata(sdirs, ext_data, ly_ctx_get_options(ext->module->ctx), ext_ctx))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200323 lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Failed to create context for the schema-mount data.");
Michal Vasko0fca45c2022-06-07 10:57:32 +0200324 goto cleanup;
Michal Vaskoddd76592022-01-17 13:34:48 +0100325 }
326
327 if (!config) {
328 /* manually change the config of all schema nodes in all the modules */
329 while ((mod = ly_ctx_get_module_iter(*ext_ctx, &idx))) {
330 if (!mod->implemented) {
331 continue;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100332 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100333
Michal Vaskoddd76592022-01-17 13:34:48 +0100334 LY_LIST_FOR(mod->compiled->data, root) {
335 LYSC_TREE_DFS_BEGIN(root, node) {
336 node->flags &= ~LYS_CONFIG_W;
337 node->flags |= LYS_CONFIG_R;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100338
Michal Vaskoddd76592022-01-17 13:34:48 +0100339 LYSC_TREE_DFS_END(root, node);
340 }
341 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100342 }
343 }
344
Michal Vasko0fca45c2022-06-07 10:57:32 +0200345cleanup:
346 free(sdirs);
347 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100348}
349
350/**
351 * @brief Get schema (context) for a shared-schema mount point.
352 *
353 * @param[in] ext Compiled extension instance.
354 * @param[in] ext_data Extension data retrieved by the callback.
355 * @param[in] config Whether the whole schema should keep its config or be set to false.
356 * @param[out] ext_ctx Schema to use for parsing the data.
357 * @return LY_ERR value.
358 */
359static LY_ERR
360schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
361 const struct ly_ctx **ext_ctx)
362{
Michal Vasko193dacd2022-10-13 08:43:05 +0200363 struct lyplg_ext_sm *sm_data = ext->compiled;
Michal Vaskoddd76592022-01-17 13:34:48 +0100364 LY_ERR ret = LY_SUCCESS, r;
365 struct lyd_node *node = NULL;
ekinzie0ab8b302022-10-10 03:03:57 -0400366 struct ly_ctx *new_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100367 uint32_t i;
368 const char *content_id = NULL;
369 void *mem;
370
371 assert(sm_data && sm_data->shared);
372
373 /* get yang-library content-id or module-set-id */
374 if (ext_data) {
375 lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, &node);
376 if (!node) {
377 lyd_find_path(ext_data, "/ietf-yang-library:modules-state/module-set-id", 0, &node);
378 }
379 if (node) {
380 content_id = lyd_get_value(node);
381 }
382 }
383 if (!content_id) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200384 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID,
385 "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100386 return LY_EVALID;
387 }
388
389 /* LOCK */
390 if ((r = pthread_mutex_lock(&sm_data->shared->lock))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200391 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r));
Michal Vaskoddd76592022-01-17 13:34:48 +0100392 return LY_ESYS;
393 }
394
395 /* try to find this mount point */
396 for (i = 0; i < sm_data->shared->schema_count; ++i) {
397 if (ext->argument == sm_data->shared->schemas[i].mount_point) {
398 break;
399 }
400 }
401
402 if (i < sm_data->shared->schema_count) {
403 /* schema exists already */
404 if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200405 lyplg_ext_compile_log_path("/ietf-yang-library:yang-library/content-id", ext, LY_LLERR, LY_EVALID,
Michal Vaskoddd76592022-01-17 13:34:48 +0100406 "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.",
407 content_id, sm_data->shared->schemas[i].content_id);
408 ret = LY_EVALID;
409 goto cleanup;
410 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100411 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +0100412 /* no schema found, create it */
413 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
414 ret = r;
415 goto cleanup;
416 }
417
418 /* new entry */
419 mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas);
420 if (!mem) {
421 ly_ctx_destroy(new_ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200422 EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +0100423 }
424 sm_data->shared->schemas = mem;
425 ++sm_data->shared->schema_count;
426
427 /* fill entry */
428 sm_data->shared->schemas[i].ctx = new_ctx;
429 sm_data->shared->schemas[i].mount_point = ext->argument;
430 lydict_insert(ext->module->ctx, content_id, 0, &sm_data->shared->schemas[i].content_id);
431 }
432
433 /* use the context */
434 *ext_ctx = sm_data->shared->schemas[i].ctx;
435
436cleanup:
437 /* UNLOCK */
438 pthread_mutex_unlock(&sm_data->shared->lock);
439
440 return ret;
441}
442
443/**
444 * @brief Get schema (context) for an inline mount point.
445 *
446 * @param[in] ext Compiled extension instance.
447 * @param[in] ext_data Extension data retrieved by the callback.
448 * @param[in] config Whether the whole schema should keep its config or be set to false.
449 * @param[out] ext_ctx Schema to use for parsing the data.
450 * @return LY_ERR value.
451 */
452static LY_ERR
453schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
454 const struct ly_ctx **ext_ctx)
455{
Michal Vasko193dacd2022-10-13 08:43:05 +0200456 struct lyplg_ext_sm *sm_data = ext->compiled;
Michal Vaskoddd76592022-01-17 13:34:48 +0100457 LY_ERR r;
ekinzie0ab8b302022-10-10 03:03:57 -0400458 struct ly_ctx *new_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100459 uint32_t i;
460 void *mem;
461
462 assert(sm_data && sm_data->shared);
463
464 i = sm_data->inln.schema_count;
465
466 /* always new schema required, create context */
467 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
468 return r;
469 }
470
471 /* new entry */
472 mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas);
473 if (!mem) {
474 ly_ctx_destroy(new_ctx);
Michal Vasko193dacd2022-10-13 08:43:05 +0200475 EXT_LOGERR_MEM_RET(NULL, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100476 }
477 sm_data->inln.schemas = mem;
478 ++sm_data->inln.schema_count;
479
480 /* fill entry */
481 sm_data->inln.schemas[i].ctx = new_ctx;
482
483 /* use the context */
484 *ext_ctx = sm_data->inln.schemas[i].ctx;
485 return LY_SUCCESS;
486}
487
488/**
489 * @brief Get schema (context) for a mount point.
490 *
491 * @param[in] ext Compiled extension instance.
492 * @param[out] ext_ctx Schema to use for parsing the data.
493 * @return LY_ERR value.
494 */
495static LY_ERR
496schema_mount_get_ctx(struct lysc_ext_instance *ext, const struct ly_ctx **ext_ctx)
497{
498 LY_ERR ret = LY_SUCCESS, r;
499 struct lyd_node *iter, *ext_data = NULL;
500 ly_bool ext_data_free = 0, config, shared;
501
502 *ext_ctx = NULL;
503
504 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
505 if ((r = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
506 ret = r;
507 goto cleanup;
508 }
509
510 LY_LIST_FOR(ext_data, iter) {
511 if (iter->flags & LYD_NEW) {
512 /* must be validated for the parent-reference prefix data to be stored */
Michal Vasko193dacd2022-10-13 08:43:05 +0200513 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100514 ret = LY_EINVAL;
515 goto cleanup;
516 }
517 }
518
519 /* learn about this mount point */
520 if ((r = schema_mount_get_smount(ext, ext_data, &config, &shared))) {
521 ret = r;
522 goto cleanup;
523 }
524
525 /* create/get the context for parsing the data */
526 if (shared) {
527 r = schema_mount_get_ctx_shared(ext, ext_data, config, ext_ctx);
528 } else {
529 r = schema_mount_get_ctx_inline(ext, ext_data, config, ext_ctx);
530 }
531 if (r) {
532 ret = r;
533 goto cleanup;
534 }
535
536cleanup:
537 if (ext_data_free) {
538 lyd_free_all(ext_data);
539 }
540 return ret;
541}
542
543/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200544 * @brief Snode callback for schema mount.
545 * Check if data are valid for schema mount and returns their schema node.
Michal Vaskoddd76592022-01-17 13:34:48 +0100546 */
547static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +0200548schema_mount_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent,
549 const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
550 const struct lysc_node **snode)
Michal Vaskoddd76592022-01-17 13:34:48 +0100551{
Michal Vasko8cc3f662022-03-29 11:25:51 +0200552 LY_ERR r;
553 const struct lys_module *mod;
ekinzie0ab8b302022-10-10 03:03:57 -0400554 const struct ly_ctx *ext_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100555
556 /* get context based on ietf-yang-library data */
557 if ((r = schema_mount_get_ctx(ext, &ext_ctx))) {
558 return r;
559 }
560
Michal Vasko8cc3f662022-03-29 11:25:51 +0200561 /* get the module */
562 mod = lyplg_type_identity_module(ext_ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data);
563 if (!mod) {
564 return LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100565 }
566
Michal Vasko8cc3f662022-03-29 11:25:51 +0200567 /* get the top-level schema node */
568 *snode = lys_find_child(NULL, mod, name, name_len, 0, 0);
569 return *snode ? LY_SUCCESS : LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100570}
571
ekinzie0ab8b302022-10-10 03:03:57 -0400572static LY_ERR
573schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data,
574 struct ly_set **set)
575{
576 LY_ERR ret = LY_SUCCESS;
577 char *path = NULL;
578
579 /* get all parent references of this mount point */
580 if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']"
581 "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200582 EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400583 }
584 if ((ret = lyd_find_xpath(ext_data, path, set))) {
585 goto cleanup;
586 }
587
588cleanup:
589 free(path);
590 return ret;
591}
592
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100593/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100594 * @brief Duplicate all accessible parent references for a shared-schema mount point.
595 *
596 * @param[in] ext Compiled extension instance.
597 * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions.
598 * @param[in] ext_data Extension data retrieved by the callback.
599 * @param[in] trg_ctx Mounted data context to use for duplication.
600 * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty.
601 * @return LY_ERR value.
602 */
603static LY_ERR
604schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node,
605 const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set)
606{
607 LY_ERR ret = LY_SUCCESS;
608 char *path = NULL;
609 struct ly_set *set = NULL, *par_set = NULL;
610 struct lyd_node_term *term;
611 struct lyd_node *dup = NULL, *top_node, *first;
612 struct lyd_value_xpath10 *xp_val;
613 uint32_t i, j;
614
615 *ref_set = NULL;
616
617 if (!ext_data) {
618 /* we expect the same ext data as before and there must be some for data to be parsed */
Michal Vasko193dacd2022-10-13 08:43:05 +0200619 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "No ext data provided.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100620 ret = LY_EINVAL;
621 goto cleanup;
622 }
623
ekinzie0ab8b302022-10-10 03:03:57 -0400624 if ((ret = schema_mount_get_parent_ref(ext, ext_data, &set))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100625 goto cleanup;
626 }
627
628 /* prepare result set */
629 if ((ret = ly_set_new(ref_set))) {
630 goto cleanup;
631 }
632
633 first = NULL;
634 for (i = 0; i < set->count; ++i) {
635 term = set->objs[i];
636
637 /* get the referenced nodes (subtrees) */
638 LYD_VALUE_GET(&term->value, xp_val);
639 if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data,
640 NULL, &par_set))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200641 lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Parent reference \"%s\" evaluation failed.",
642 lyxp_get_expr(xp_val->exp));
Michal Vaskoddd76592022-01-17 13:34:48 +0100643 goto cleanup;
644 }
645
646 for (j = 0; j < par_set->count; ++j) {
647 /* duplicate with parents in the context of the mounted data */
648 if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL,
Michal Vaskofbbea932022-06-07 11:00:55 +0200649 LYD_DUP_RECURSIVE | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_EXT, &dup))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100650 goto cleanup;
651 }
652
653 /* go top-level */
654 while (dup->parent) {
655 dup = lyd_parent(dup);
656 }
657
658 /* check whether the top-level node exists */
659 if (first) {
660 if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) {
661 goto cleanup;
662 }
663 } else {
664 top_node = NULL;
665 }
666
667 if (top_node) {
668 /* merge */
669 ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT);
670 dup = NULL;
671 if (ret) {
672 goto cleanup;
673 }
674 } else {
675 /* insert */
676 if ((ret = lyd_insert_sibling(first, dup, &first))) {
677 goto cleanup;
678 }
679
680 /* add into the result set because a new top-level node was added */
681 if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) {
682 goto cleanup;
683 }
684 dup = NULL;
685 }
686 }
687 }
688
689cleanup:
690 free(path);
691 ly_set_free(set, NULL);
692 ly_set_free(par_set, NULL);
693 lyd_free_tree(dup);
694 if (ret && *ref_set) {
695 if ((*ref_set)->count) {
696 lyd_free_siblings((*ref_set)->dnodes[0]);
697 }
698 ly_set_free(*ref_set, NULL);
699 *ref_set = NULL;
700 }
701 return ret;
702}
703
ekinzie0ab8b302022-10-10 03:03:57 -0400704LY_ERR
705lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs)
706{
aPiecekb72a6df2022-10-10 10:02:38 +0200707 LY_ERR rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400708 struct ly_set *pref_set = NULL;
aPiecekb72a6df2022-10-10 10:02:38 +0200709 struct ly_set *snode_set = NULL;
ekinzie0ab8b302022-10-10 03:03:57 -0400710 struct ly_set *results_set = NULL;
711 struct lyd_node *ext_data;
712 ly_bool ext_data_free;
713
714 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
aPiecekb72a6df2022-10-10 10:02:38 +0200715 if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
716 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400717 }
718
aPiecekb72a6df2022-10-10 10:02:38 +0200719 LY_CHECK_GOTO(rc = schema_mount_get_parent_ref(ext, ext_data, &pref_set), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400720 if (pref_set->count == 0) {
aPiecekb72a6df2022-10-10 10:02:38 +0200721 goto cleanup;
ekinzie0ab8b302022-10-10 03:03:57 -0400722 }
723
aPiecekb72a6df2022-10-10 10:02:38 +0200724 LY_CHECK_GOTO(rc = ly_set_new(&results_set), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400725
726 for (uint32_t i = 0; i < pref_set->count; ++i) {
727 struct lyd_node_term *term;
728 struct lyd_value_xpath10 *xp_val;
729 char *value;
730 struct ly_err_item *err;
731
732 term = (struct lyd_node_term *)pref_set->dnodes[i];
733 LYD_VALUE_GET(&term->value, xp_val);
aPiecekb72a6df2022-10-10 10:02:38 +0200734 LY_CHECK_GOTO(rc = lyplg_type_print_xpath10_value(xp_val, LY_VALUE_JSON, NULL, &value, &err), cleanup);
735 LY_CHECK_ERR_GOTO(rc = lys_find_xpath(ext->module->ctx, NULL, value, 0, &snode_set), free(value), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400736 free(value);
737 for (uint32_t sn = 0; sn < snode_set->count; sn++) {
aPiecekb72a6df2022-10-10 10:02:38 +0200738 LY_CHECK_GOTO(rc = ly_set_add(results_set, snode_set->snodes[sn], 0, NULL), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400739 }
740 ly_set_free(snode_set, NULL);
aPiecekb72a6df2022-10-10 10:02:38 +0200741 snode_set = NULL;
ekinzie0ab8b302022-10-10 03:03:57 -0400742 }
743
744 *refs = results_set;
745
aPiecekb72a6df2022-10-10 10:02:38 +0200746cleanup:
747 if (rc) {
748 ly_set_free(results_set, NULL);
749 }
750 ly_set_free(snode_set, NULL);
ekinzie0ab8b302022-10-10 03:03:57 -0400751 if (ext_data_free) {
752 lyd_free_all(ext_data);
753 }
754 ly_set_free(pref_set, NULL);
aPiecekb72a6df2022-10-10 10:02:38 +0200755
756 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400757}
758
Michal Vaskoddd76592022-01-17 13:34:48 +0100759/**
760 * @brief Validate callback for schema mount.
761 */
762static LY_ERR
Michal Vaskofbbea932022-06-07 11:00:55 +0200763schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree,
764 enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskoddd76592022-01-17 13:34:48 +0100765{
766 LY_ERR ret = LY_SUCCESS;
767 uint32_t old_log_opts, i;
768 struct ly_err_item *err;
Michal Vaskofbbea932022-06-07 11:00:55 +0200769 struct lyd_node *iter, *ext_data = NULL, *ref_first = NULL, *orig_parent = lyd_parent(sibling), *op_tree;
Michal Vaskof4c6f002022-04-01 09:12:22 +0200770 struct lyd_node *ext_diff = NULL, *diff_parent = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100771 ly_bool ext_data_free = 0;
772 struct ly_set *ref_set = NULL;
773
774 if (!sibling) {
775 /* some data had to be parsed for this callback to be called */
Michal Vasko193dacd2022-10-13 08:43:05 +0200776 EXT_LOGERR_INT_RET(NULL, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100777 }
778
779 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
780 if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
781 goto cleanup;
782 }
783
784 LY_LIST_FOR(ext_data, iter) {
785 if (iter->flags & LYD_NEW) {
786 /* must be validated for the parent-reference prefix data to be stored */
Michal Vasko193dacd2022-10-13 08:43:05 +0200787 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100788 ret = LY_EINVAL;
789 goto cleanup;
790 }
791 }
792
793 /* duplicate the referenced parent nodes into ext context */
794 if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) {
795 goto cleanup;
796 }
797
Michal Vaskofbbea932022-06-07 11:00:55 +0200798 if (data_type != LYD_TYPE_DATA_YANG) {
799 /* remember the operation data tree, it may be moved */
800 op_tree = sibling;
801 }
802
Michal Vaskoddd76592022-01-17 13:34:48 +0100803 /* create accessible tree, remove LYD_EXT to not call this callback recursively */
804 lyd_unlink_siblings(sibling);
805 LY_LIST_FOR(sibling, iter) {
806 iter->flags &= ~LYD_EXT;
807 }
808 if (ref_set->count) {
809 if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) {
810 goto cleanup;
811 }
812 }
813
814 /* only store messages in the context, log as an extension */
815 old_log_opts = ly_log_options(LY_LOSTORE_LAST);
816
Michal Vaskofbbea932022-06-07 11:00:55 +0200817 if (data_type == LYD_TYPE_DATA_YANG) {
818 /* validate all the data */
819 ret = lyd_validate_all(&sibling, NULL, val_opts, diff ? &ext_diff : NULL);
820 } else {
821 /* validate the operation */
822 ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL);
823 }
824
825 /* restore logging */
Michal Vaskoddd76592022-01-17 13:34:48 +0100826 ly_log_options(old_log_opts);
827
828 /* restore sibling tree */
829 for (i = 0; i < ref_set->count; ++i) {
830 if (ref_set->dnodes[i] == sibling) {
831 sibling = sibling->next;
832 }
833 lyd_free_tree(ref_set->dnodes[i]);
834 }
835 LY_LIST_FOR(sibling, iter) {
836 iter->flags |= LYD_EXT;
837 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200838 lyplg_ext_insert(orig_parent, sibling);
Michal Vaskoddd76592022-01-17 13:34:48 +0100839
840 if (ret) {
841 /* log the error in the original context */
842 err = ly_err_first(LYD_CTX(sibling));
843 if (!err) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200844 lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Unknown validation error (err code %d).", ret);
Michal Vaskoddd76592022-01-17 13:34:48 +0100845 } else {
Michal Vasko193dacd2022-10-13 08:43:05 +0200846 lyplg_ext_compile_log_path(err->path, ext, LY_LLERR, err->no, "%s", err->msg);
Michal Vaskoddd76592022-01-17 13:34:48 +0100847 }
848 goto cleanup;
849 }
850
Michal Vaskof4c6f002022-04-01 09:12:22 +0200851 /* create proper diff */
852 if (diff && ext_diff) {
853 /* diff nodes from an extension instance */
854 LY_LIST_FOR(ext_diff, iter) {
855 iter->flags |= LYD_EXT;
856 }
857
858 /* create the parent and insert the diff */
859 if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) {
860 goto cleanup;
861 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200862 if ((ret = lyplg_ext_insert(diff_parent, ext_diff))) {
Michal Vaskof4c6f002022-04-01 09:12:22 +0200863 goto cleanup;
864 }
865 ext_diff = NULL;
866
867 /* go top-level and set the operation */
868 while (lyd_parent(diff_parent)) {
869 diff_parent = lyd_parent(diff_parent);
870 }
871 if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) {
872 goto cleanup;
873 }
874
875 /* finally merge into the global diff */
876 if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) {
877 goto cleanup;
878 }
879 }
880
Michal Vaskoddd76592022-01-17 13:34:48 +0100881cleanup:
882 ly_set_free(ref_set, NULL);
883 lyd_free_siblings(ref_first);
Michal Vaskof4c6f002022-04-01 09:12:22 +0200884 lyd_free_tree(ext_diff);
885 lyd_free_all(diff_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +0100886 if (ext_data_free) {
887 lyd_free_all(ext_data);
888 }
889 return ret;
890}
891
892/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200893 * @brief Schema mount compile free.
Michal Vaskoddd76592022-01-17 13:34:48 +0100894 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200895 * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree.
Michal Vaskoddd76592022-01-17 13:34:48 +0100896 */
897static void
Michal Vasko193dacd2022-10-13 08:43:05 +0200898schema_mount_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100899{
Michal Vasko193dacd2022-10-13 08:43:05 +0200900 struct lyplg_ext_sm *sm_data = ext->compiled;
Michal Vaskoddd76592022-01-17 13:34:48 +0100901 uint32_t i;
902
903 if (!sm_data) {
904 return;
905 }
906
907 if (!--sm_data->shared->ref_count) {
908 for (i = 0; i < sm_data->shared->schema_count; ++i) {
909 ly_ctx_destroy(sm_data->shared->schemas[i].ctx);
910 lydict_remove(ctx, sm_data->shared->schemas[i].content_id);
911 }
912 free(sm_data->shared->schemas);
913 pthread_mutex_destroy(&sm_data->shared->lock);
914 free(sm_data->shared);
915 }
916
917 for (i = 0; i < sm_data->inln.schema_count; ++i) {
918 ly_ctx_destroy(sm_data->inln.schemas[i].ctx);
919 }
920 free(sm_data->inln.schemas);
921 free(sm_data);
922}
923
ekinzie0ab8b302022-10-10 03:03:57 -0400924LIBYANG_API_DEF LY_ERR
925lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx)
926{
Michal Vasko266308d2022-11-08 08:45:31 +0100927 struct lyd_node *ext_data = NULL;
928 ly_bool ext_data_free = 0, config;
929 LY_ERR rc = LY_SUCCESS;
ekinzie0ab8b302022-10-10 03:03:57 -0400930
931 if (!ext->module->ctx->ext_clb) {
932 return LY_EINVAL;
933 }
934
Michal Vasko266308d2022-11-08 08:45:31 +0100935 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) {
ekinzie0ab8b302022-10-10 03:03:57 -0400936 return LY_EINVAL;
937 }
938
939 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
Michal Vasko266308d2022-11-08 08:45:31 +0100940 if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
941 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400942 }
943
944 /* learn about this mount point */
Michal Vasko266308d2022-11-08 08:45:31 +0100945 if ((rc = schema_mount_get_smount(ext, ext_data, &config, NULL))) {
946 goto cleanup;
ekinzie0ab8b302022-10-10 03:03:57 -0400947 }
948
Michal Vasko266308d2022-11-08 08:45:31 +0100949 /* create the context */
950 rc = schema_mount_create_ctx(ext, ext_data, config, ctx);
ekinzie0ab8b302022-10-10 03:03:57 -0400951
Michal Vasko266308d2022-11-08 08:45:31 +0100952cleanup:
ekinzie0ab8b302022-10-10 03:03:57 -0400953 if (ext_data_free) {
954 lyd_free_all(ext_data);
955 }
Michal Vasko266308d2022-11-08 08:45:31 +0100956 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400957}
958
aPiecek03cb4872022-10-24 10:31:51 +0200959static void
960schema_mount_spriter_tree_free(void *priv)
961{
962 struct sprinter_tree_priv *st_priv;
963
964 st_priv = priv;
965 ly_set_free(st_priv->refs, NULL);
966 ly_ctx_destroy(st_priv->ext_ctx);
967 free(st_priv);
968}
969
970static LY_ERR
971schema_mount_sprinter_tree_cnode_override_mounted(const struct lysc_node *node, const void *UNUSED(plugin_priv),
972 ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts)
973{
974 if (!node->parent) {
975 *add_opts = "/";
976 }
977
978 return LY_SUCCESS;
979}
980
981static LY_ERR
982schema_mount_sprinter_tree_pnode_override_mounted(const struct lysp_node *node, const void *UNUSED(plugin_priv),
983 ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts)
984{
985 if (!node->parent) {
986 *add_opts = "/";
987 }
988
989 return LY_SUCCESS;
990}
991
992static LY_ERR
993schema_mount_sprinter_tree_node_override_parent_refs(const struct lysc_node *node, const void *plugin_priv,
994 ly_bool *skip, const char **UNUSED(flags), const char **add_opts)
995{
996 uint32_t i;
997 const struct ly_set *refs;
998 const struct lysc_module *mod;
999 struct lysc_node *ref, *iter;
1000
1001 refs = ((struct sprinter_tree_priv *)plugin_priv)->refs;
1002 mod = node->module->compiled;
1003
1004 /* Assume the @p node will be skipped. */
1005 *skip = 1;
1006 for (i = 0; (i < refs->count) && *skip; i++) {
1007 ref = refs->snodes[i];
1008 if (ref->module->compiled != mod) {
1009 /* parent-reference points to different module */
1010 continue;
1011 }
1012
1013 for (iter = ref; iter; iter = iter->parent) {
1014 if (iter == node) {
1015 /* @p node is not skipped because it is parent-rererence node or his parent */
1016 *skip = 0;
1017 break;
1018 }
1019 }
1020 }
1021
1022 if (!*skip && !node->parent) {
1023 /* top-node has additional opts */
1024 *add_opts = "@";
1025 }
1026
1027 return LY_SUCCESS;
1028}
1029
Michal Vasko266308d2022-11-08 08:45:31 +01001030/**
1031 * @brief Schema mount schema parsed tree printer.
1032 *
1033 * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree.
1034 */
aPiecek03cb4872022-10-24 10:31:51 +02001035static LY_ERR
1036schema_mount_sprinter_ptree(struct lysp_ext_instance *UNUSED(ext), const struct lyspr_tree_ctx *ctx,
1037 const char **flags, const char **UNUSED(add_opts))
1038{
1039 if (!ctx) {
1040 *flags = "mp";
1041 }
1042
1043 return LY_SUCCESS;
1044}
1045
Michal Vasko266308d2022-11-08 08:45:31 +01001046/**
1047 * @brief Schema mount schema compiled tree printer.
1048 *
1049 * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree.
1050 */
aPiecek03cb4872022-10-24 10:31:51 +02001051static LY_ERR
1052schema_mount_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx,
1053 const char **flags, const char **UNUSED(add_opts))
1054{
1055 LY_ERR rc = LY_SUCCESS;
1056 struct ly_ctx *ext_ctx = NULL;
1057 const struct lys_module *mod;
1058 struct ly_set *refs = NULL;
1059 struct lysc_node *tree1, *tree2;
1060 uint32_t i, j;
1061 ly_bool from_parent_ref, is_first;
1062 struct sprinter_tree_priv *st_priv;
1063
1064 if (!ctx) {
1065 *flags = "mp";
1066 return LY_SUCCESS;
1067 }
1068
1069 if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) {
1070 /* Void mount point */
1071 return LY_SUCCESS;
1072 }
1073
1074 rc = lyplg_ext_schema_mount_get_parent_ref(ext, &refs);
1075 LY_CHECK_GOTO(rc, cleanup);
1076
1077 /* build new list of modules to print. This list will omit internal
1078 * modules, modules with no nodes (e.g., iana-if-types) and modules
1079 * that were loaded as the result of a parent-reference.
1080 */
1081 i = ly_ctx_internal_modules_count(ext_ctx);
1082 while ((mod = ly_ctx_get_module_iter(ext_ctx, &i))) {
1083 from_parent_ref = 0;
1084
1085 for (j = 0; refs && j < refs->count; j++) {
1086 if (!strcmp(mod->ns, refs->snodes[j]->module->ns)) {
1087 from_parent_ref = 1;
1088 break;
1089 }
1090 }
1091 if (from_parent_ref) {
1092 /* Modules loaded as the result of a parent-reference are added later. */
1093 continue;
1094 }
1095
1096 /* Add data nodes, rpcs and notifications. */
1097 if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) {
1098 /* For compiled module. */
1099 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, mod->compiled->data,
1100 schema_mount_sprinter_tree_cnode_override_mounted);
1101 LY_CHECK_GOTO(rc, cleanup);
1102 if (mod->compiled->rpcs) {
1103 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->rpcs->node,
1104 schema_mount_sprinter_tree_cnode_override_mounted);
1105 }
1106 LY_CHECK_GOTO(rc, cleanup);
1107 if (mod->compiled->notifs) {
1108 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->notifs->node,
1109 schema_mount_sprinter_tree_cnode_override_mounted);
1110 }
1111 LY_CHECK_GOTO(rc, cleanup);
1112 } else {
1113 /* For parsed module. */
1114 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, mod->parsed->data,
1115 schema_mount_sprinter_tree_pnode_override_mounted);
1116 LY_CHECK_GOTO(rc, cleanup);
1117 if (mod->parsed->rpcs) {
1118 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->rpcs->node,
1119 schema_mount_sprinter_tree_pnode_override_mounted);
1120 }
1121 LY_CHECK_GOTO(rc, cleanup);
1122 if (mod->parsed->notifs) {
1123 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->notifs->node,
1124 schema_mount_sprinter_tree_pnode_override_mounted);
1125 }
1126 LY_CHECK_GOTO(rc, cleanup);
1127 }
1128 }
1129
1130 /* Add modules loaded as the result of a parent-reference. */
1131 for (i = 0; refs && (i < refs->count); i++) {
1132 tree1 = refs->snodes[i]->module->compiled->data;
1133
1134 /* Add data nodes from the module only once. */
1135 is_first = 1;
1136 for (j = 0; j < i; j++) {
1137 tree2 = refs->snodes[j]->module->compiled->data;
1138 if (tree1 == tree2) {
1139 is_first = 0;
1140 break;
1141 }
1142 }
1143 if (is_first) {
1144 /* Add all data nodes but unavailable nodes are skipped in the callback. */
1145 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, tree1, schema_mount_sprinter_tree_node_override_parent_refs);
1146 LY_CHECK_GOTO(rc, cleanup);
1147 }
1148 }
1149
1150 /* Add private plugin data. */
1151 st_priv = calloc(1, sizeof(*st_priv));
1152 st_priv->ext_ctx = ext_ctx;
1153 st_priv->refs = refs;
1154 rc = lyplg_ext_sprinter_tree_set_priv(ctx, st_priv, schema_mount_spriter_tree_free);
1155
1156cleanup:
1157 if (rc) {
1158 ly_set_free(refs, NULL);
1159 ly_ctx_destroy(ext_ctx);
1160 }
1161
1162 return rc;
1163}
1164
Michal Vaskoddd76592022-01-17 13:34:48 +01001165/**
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001166 * @brief Plugin descriptions for the Yang Schema Mount extension.
1167 *
1168 * Note that external plugins are supposed to use:
1169 *
1170 * LYPLG_EXTENSIONS = {
1171 */
1172const struct lyplg_ext_record plugins_schema_mount[] = {
1173 {
1174 .module = "ietf-yang-schema-mount",
1175 .revision = "2019-01-14",
1176 .name = "mount-point",
1177
Michal Vasko193dacd2022-10-13 08:43:05 +02001178 .plugin.id = "ly2 schema mount v1",
1179 .plugin.parse = schema_mount_parse,
1180 .plugin.compile = schema_mount_compile,
Michal Vasko941e0562022-10-18 10:35:00 +02001181 .plugin.printer_info = NULL,
aPiecek03cb4872022-10-24 10:31:51 +02001182 .plugin.printer_ctree = schema_mount_sprinter_ctree,
1183 .plugin.printer_ptree = schema_mount_sprinter_ptree,
Michal Vasko135719f2022-08-25 12:18:17 +02001184 .plugin.node = NULL,
Michal Vasko193dacd2022-10-13 08:43:05 +02001185 .plugin.snode = schema_mount_snode,
1186 .plugin.validate = schema_mount_validate,
1187 .plugin.pfree = NULL,
1188 .plugin.cfree = schema_mount_cfree
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001189 },
1190 {0} /* terminating zeroed item */
1191};