blob: e812053ef43b405067f26e0c12bcaccab24658f3 [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 Vasko193dacd2022-10-13 08:43:05 +020023#include "compat.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010024#include "dict.h"
25#include "libyang.h"
26#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010027#include "ly_common.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 {
Michal Vasko27653162022-11-21 13:58:44 +010038 pthread_mutex_t lock; /**< lock for accessing this shared structure */
Michal Vaskoddd76592022-01-17 13:34:48 +010039
Michal Vasko27653162022-11-21 13:58:44 +010040 struct lyplg_ext_sm_shared {
Michal Vaskoddd76592022-01-17 13:34:48 +010041 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 {
Michal Vasko27653162022-11-21 13:58:44 +010054 struct ly_ctx *ctx; /**< context created for inline schema data, may be reused if possible */
Michal Vaskoddd76592022-01-17 13:34:48 +010055 } *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 Vasko27653162022-11-21 13:58:44 +0100210 pthread_mutex_init(&sm_data->lock, NULL);
Michal Vasko193dacd2022-10-13 08:43:05 +0200211 ext->compiled = sm_data;
Michal Vaskoddd76592022-01-17 13:34:48 +0100212
Michal Vaskodb641792022-03-21 10:04:54 +0100213 /* find the owner module */
Michal Vasko193dacd2022-10-13 08:43:05 +0200214 node = ext->parent;
Michal Vaskodb641792022-03-21 10:04:54 +0100215 while (node->parent) {
216 node = node->parent;
217 }
218
Michal Vaskoddd76592022-01-17 13:34:48 +0100219 /* reuse/init shared schema */
Michal Vasko193dacd2022-10-13 08:43:05 +0200220 sm_data->shared = schema_mount_compile_find_shared(node->module, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100221 if (sm_data->shared) {
222 ++sm_data->shared->ref_count;
223 } else {
224 sm_data->shared = calloc(1, sizeof *sm_data->shared);
225 if (!sm_data->shared) {
226 free(sm_data);
Michal Vasko193dacd2022-10-13 08:43:05 +0200227 EXT_LOGERR_MEM_RET(cctx, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100228 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100229 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/**
Michal Vasko27653162022-11-21 13:58:44 +0100351 * @brief Get ietf-yang-library context-id from its data.
352 *
353 * @param[in] ext Compiled extension instance for logging.
354 * @param[in] ext_data Extension data retrieved by the callback with the yang-library data.
355 * @param[out] content_id Content ID in @p ext_data.
356 * @return LY_ERR value.
357 */
358static LY_ERR
359schema_mount_get_content_id(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, const char **content_id)
360{
361 struct lyd_node *node = NULL;
362
363 *content_id = NULL;
364
365 /* get yang-library content-id or module-set-id */
366 if (ext_data) {
367 lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, &node);
368 if (!node) {
369 lyd_find_path(ext_data, "/ietf-yang-library:modules-state/module-set-id", 0, &node);
370 }
371 if (node) {
372 *content_id = lyd_get_value(node);
373 }
374 }
375
376 if (!*content_id) {
377 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID,
378 "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data.");
379 return LY_EVALID;
380 }
381 return LY_SUCCESS;
382}
383
384/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100385 * @brief Get schema (context) for a shared-schema mount point.
386 *
387 * @param[in] ext Compiled extension instance.
388 * @param[in] ext_data Extension data retrieved by the callback.
389 * @param[in] config Whether the whole schema should keep its config or be set to false.
390 * @param[out] ext_ctx Schema to use for parsing the data.
391 * @return LY_ERR value.
392 */
393static LY_ERR
394schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
395 const struct ly_ctx **ext_ctx)
396{
Michal Vasko193dacd2022-10-13 08:43:05 +0200397 struct lyplg_ext_sm *sm_data = ext->compiled;
Michal Vasko27653162022-11-21 13:58:44 +0100398 LY_ERR rc = LY_SUCCESS, r;
ekinzie0ab8b302022-10-10 03:03:57 -0400399 struct ly_ctx *new_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100400 uint32_t i;
Michal Vasko27653162022-11-21 13:58:44 +0100401 const char *content_id;
Michal Vaskoddd76592022-01-17 13:34:48 +0100402 void *mem;
403
404 assert(sm_data && sm_data->shared);
405
406 /* get yang-library content-id or module-set-id */
Michal Vasko27653162022-11-21 13:58:44 +0100407 if ((r = schema_mount_get_content_id(ext, ext_data, &content_id))) {
408 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100409 }
410
411 /* LOCK */
Michal Vasko27653162022-11-21 13:58:44 +0100412 if ((r = pthread_mutex_lock(&sm_data->lock))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200413 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r));
Michal Vaskoddd76592022-01-17 13:34:48 +0100414 return LY_ESYS;
415 }
416
417 /* try to find this mount point */
418 for (i = 0; i < sm_data->shared->schema_count; ++i) {
419 if (ext->argument == sm_data->shared->schemas[i].mount_point) {
420 break;
421 }
422 }
423
424 if (i < sm_data->shared->schema_count) {
425 /* schema exists already */
426 if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200427 lyplg_ext_compile_log_path("/ietf-yang-library:yang-library/content-id", ext, LY_LLERR, LY_EVALID,
Michal Vaskoddd76592022-01-17 13:34:48 +0100428 "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.",
429 content_id, sm_data->shared->schemas[i].content_id);
Michal Vasko27653162022-11-21 13:58:44 +0100430 rc = LY_EVALID;
Michal Vaskoddd76592022-01-17 13:34:48 +0100431 goto cleanup;
432 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100433 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +0100434 /* no schema found, create it */
435 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
Michal Vasko27653162022-11-21 13:58:44 +0100436 rc = r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100437 goto cleanup;
438 }
439
440 /* new entry */
441 mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas);
442 if (!mem) {
443 ly_ctx_destroy(new_ctx);
Michal Vasko27653162022-11-21 13:58:44 +0100444 EXT_LOGERR_MEM_GOTO(NULL, ext, rc, cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +0100445 }
446 sm_data->shared->schemas = mem;
447 ++sm_data->shared->schema_count;
448
449 /* fill entry */
450 sm_data->shared->schemas[i].ctx = new_ctx;
451 sm_data->shared->schemas[i].mount_point = ext->argument;
452 lydict_insert(ext->module->ctx, content_id, 0, &sm_data->shared->schemas[i].content_id);
453 }
454
455 /* use the context */
456 *ext_ctx = sm_data->shared->schemas[i].ctx;
457
458cleanup:
459 /* UNLOCK */
Michal Vasko27653162022-11-21 13:58:44 +0100460 pthread_mutex_unlock(&sm_data->lock);
Michal Vaskoddd76592022-01-17 13:34:48 +0100461
Michal Vasko27653162022-11-21 13:58:44 +0100462 return rc;
463}
464
465/**
466 * @brief Check whether ietf-yang-library data describe an existing context meaning whether it includes
467 * at least exactly all the mentioned modules.
468 *
469 * @param[in] ext Compiled extension instance for logging.
470 * @param[in] ext_data Extension data retrieved by the callback with the yang-library data.
471 * @param[in] ctx Context to consider.
472 * @return LY_SUCCESS if the context matches.
473 * @return LY_ENOT if the context differs.
474 * @return LY_ERR on error.
475 */
476static LY_ERR
477schema_mount_ctx_match(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, const struct ly_ctx *ctx)
478{
479 struct ly_set *impl_mods = NULL, *imp_mods = NULL;
480 struct lyd_node *node;
481 const struct lys_module *mod;
482 const char *name, *revision;
483 LY_ERR rc = LY_ENOT, r;
484 uint32_t i;
485
486 /* collect all the implemented and imported modules, we do not really care about content-id */
487 if (!lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, NULL)) {
488 if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:yang-library/module-set[1]/module", &impl_mods))) {
489 rc = r;
490 goto cleanup;
491 }
492 if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:yang-library/module-set[1]/import-only-module", &imp_mods))) {
493 rc = r;
494 goto cleanup;
495 }
496 } else {
497 if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:modules-state/module[conformance-type='implement']", &impl_mods))) {
498 rc = r;
499 goto cleanup;
500 }
501 if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:modules-state/module[conformance-type='import']", &imp_mods))) {
502 rc = r;
503 goto cleanup;
504 }
505 }
506
507 if (!impl_mods->count) {
508 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID, "No implemented modules included in ietf-yang-library data.");
509 rc = LY_EVALID;
510 goto cleanup;
511 }
512
513 /* check all the implemented modules */
514 for (i = 0; i < impl_mods->count; ++i) {
515 lyd_find_path(impl_mods->dnodes[i], "name", 0, &node);
516 name = lyd_get_value(node);
517
518 lyd_find_path(impl_mods->dnodes[i], "revision", 0, &node);
519 if (node && strlen(lyd_get_value(node))) {
520 revision = lyd_get_value(node);
521 } else {
522 revision = NULL;
523 }
524
525 if (!(mod = ly_ctx_get_module(ctx, name, revision)) || !mod->implemented) {
526 /* unsuitable module */
527 goto cleanup;
528 }
529 }
530
531 /* check all the imported modules */
532 for (i = 0; i < imp_mods->count; ++i) {
533 lyd_find_path(imp_mods->dnodes[i], "name", 0, &node);
534 name = lyd_get_value(node);
535
536 lyd_find_path(imp_mods->dnodes[i], "revision", 0, &node);
537 if (node && strlen(lyd_get_value(node))) {
538 revision = lyd_get_value(node);
539 } else {
540 revision = NULL;
541 }
542
543 if (!ly_ctx_get_module(ctx, name, revision)) {
544 /* unsuitable module */
545 goto cleanup;
546 }
547 }
548
549 /* context matches and can be reused */
550 rc = LY_SUCCESS;
551
552cleanup:
553 ly_set_free(impl_mods, NULL);
554 ly_set_free(imp_mods, NULL);
555 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100556}
557
558/**
559 * @brief Get schema (context) for an inline mount point.
560 *
561 * @param[in] ext Compiled extension instance.
562 * @param[in] ext_data Extension data retrieved by the callback.
563 * @param[in] config Whether the whole schema should keep its config or be set to false.
564 * @param[out] ext_ctx Schema to use for parsing the data.
565 * @return LY_ERR value.
566 */
567static LY_ERR
568schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
569 const struct ly_ctx **ext_ctx)
570{
Michal Vasko193dacd2022-10-13 08:43:05 +0200571 struct lyplg_ext_sm *sm_data = ext->compiled;
ekinzie0ab8b302022-10-10 03:03:57 -0400572 struct ly_ctx *new_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100573 uint32_t i;
574 void *mem;
Michal Vasko27653162022-11-21 13:58:44 +0100575 LY_ERR rc = LY_SUCCESS, r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100576
577 assert(sm_data && sm_data->shared);
578
Michal Vasko27653162022-11-21 13:58:44 +0100579 /* LOCK */
580 if ((r = pthread_mutex_lock(&sm_data->lock))) {
581 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r));
582 return LY_ESYS;
583 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100584
Michal Vasko27653162022-11-21 13:58:44 +0100585 /* try to find a context we can reuse */
586 for (i = 0; i < sm_data->inln.schema_count; ++i) {
587 r = schema_mount_ctx_match(ext, ext_data, sm_data->inln.schemas[i].ctx);
588 if (!r) {
589 /* match */
590 *ext_ctx = sm_data->inln.schemas[i].ctx;
591 goto cleanup;
592 } else if (r != LY_ENOT) {
593 /* error */
594 rc = r;
595 goto cleanup;
596 }
597 }
598
599 /* new schema required, create context */
Michal Vaskoddd76592022-01-17 13:34:48 +0100600 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
Michal Vasko27653162022-11-21 13:58:44 +0100601 rc = r;
602 goto cleanup;
Michal Vaskoddd76592022-01-17 13:34:48 +0100603 }
604
605 /* new entry */
606 mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas);
607 if (!mem) {
608 ly_ctx_destroy(new_ctx);
Michal Vasko27653162022-11-21 13:58:44 +0100609 EXT_LOGERR_MEM_GOTO(NULL, ext, rc, cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +0100610 }
611 sm_data->inln.schemas = mem;
612 ++sm_data->inln.schema_count;
613
614 /* fill entry */
615 sm_data->inln.schemas[i].ctx = new_ctx;
616
617 /* use the context */
618 *ext_ctx = sm_data->inln.schemas[i].ctx;
Michal Vasko27653162022-11-21 13:58:44 +0100619
620cleanup:
621 /* UNLOCK */
622 pthread_mutex_unlock(&sm_data->lock);
623
624 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100625}
626
627/**
628 * @brief Get schema (context) for a mount point.
629 *
630 * @param[in] ext Compiled extension instance.
631 * @param[out] ext_ctx Schema to use for parsing the data.
632 * @return LY_ERR value.
633 */
634static LY_ERR
635schema_mount_get_ctx(struct lysc_ext_instance *ext, const struct ly_ctx **ext_ctx)
636{
637 LY_ERR ret = LY_SUCCESS, r;
638 struct lyd_node *iter, *ext_data = NULL;
639 ly_bool ext_data_free = 0, config, shared;
640
641 *ext_ctx = NULL;
642
643 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
644 if ((r = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
645 ret = r;
646 goto cleanup;
647 }
648
649 LY_LIST_FOR(ext_data, iter) {
650 if (iter->flags & LYD_NEW) {
651 /* must be validated for the parent-reference prefix data to be stored */
Michal Vasko193dacd2022-10-13 08:43:05 +0200652 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100653 ret = LY_EINVAL;
654 goto cleanup;
655 }
656 }
657
658 /* learn about this mount point */
659 if ((r = schema_mount_get_smount(ext, ext_data, &config, &shared))) {
660 ret = r;
661 goto cleanup;
662 }
663
664 /* create/get the context for parsing the data */
665 if (shared) {
666 r = schema_mount_get_ctx_shared(ext, ext_data, config, ext_ctx);
667 } else {
668 r = schema_mount_get_ctx_inline(ext, ext_data, config, ext_ctx);
669 }
670 if (r) {
671 ret = r;
672 goto cleanup;
673 }
674
675cleanup:
676 if (ext_data_free) {
677 lyd_free_all(ext_data);
678 }
679 return ret;
680}
681
682/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200683 * @brief Snode callback for schema mount.
684 * Check if data are valid for schema mount and returns their schema node.
Michal Vaskoddd76592022-01-17 13:34:48 +0100685 */
686static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +0200687schema_mount_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent,
688 const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
689 const struct lysc_node **snode)
Michal Vaskoddd76592022-01-17 13:34:48 +0100690{
Michal Vasko8cc3f662022-03-29 11:25:51 +0200691 LY_ERR r;
692 const struct lys_module *mod;
ekinzie0ab8b302022-10-10 03:03:57 -0400693 const struct ly_ctx *ext_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100694
695 /* get context based on ietf-yang-library data */
696 if ((r = schema_mount_get_ctx(ext, &ext_ctx))) {
697 return r;
698 }
699
Michal Vasko8cc3f662022-03-29 11:25:51 +0200700 /* get the module */
701 mod = lyplg_type_identity_module(ext_ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data);
702 if (!mod) {
703 return LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100704 }
705
Michal Vasko8cc3f662022-03-29 11:25:51 +0200706 /* get the top-level schema node */
707 *snode = lys_find_child(NULL, mod, name, name_len, 0, 0);
708 return *snode ? LY_SUCCESS : LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100709}
710
ekinzie0ab8b302022-10-10 03:03:57 -0400711static LY_ERR
712schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data,
713 struct ly_set **set)
714{
715 LY_ERR ret = LY_SUCCESS;
716 char *path = NULL;
717
718 /* get all parent references of this mount point */
719 if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']"
720 "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200721 EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400722 }
723 if ((ret = lyd_find_xpath(ext_data, path, set))) {
724 goto cleanup;
725 }
726
727cleanup:
728 free(path);
729 return ret;
730}
731
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100732/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100733 * @brief Duplicate all accessible parent references for a shared-schema mount point.
734 *
735 * @param[in] ext Compiled extension instance.
736 * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions.
737 * @param[in] ext_data Extension data retrieved by the callback.
738 * @param[in] trg_ctx Mounted data context to use for duplication.
739 * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty.
740 * @return LY_ERR value.
741 */
742static LY_ERR
743schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node,
744 const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set)
745{
746 LY_ERR ret = LY_SUCCESS;
747 char *path = NULL;
748 struct ly_set *set = NULL, *par_set = NULL;
749 struct lyd_node_term *term;
750 struct lyd_node *dup = NULL, *top_node, *first;
751 struct lyd_value_xpath10 *xp_val;
752 uint32_t i, j;
753
754 *ref_set = NULL;
755
756 if (!ext_data) {
757 /* 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 +0200758 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "No ext data provided.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100759 ret = LY_EINVAL;
760 goto cleanup;
761 }
762
ekinzie0ab8b302022-10-10 03:03:57 -0400763 if ((ret = schema_mount_get_parent_ref(ext, ext_data, &set))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100764 goto cleanup;
765 }
766
767 /* prepare result set */
768 if ((ret = ly_set_new(ref_set))) {
769 goto cleanup;
770 }
771
772 first = NULL;
773 for (i = 0; i < set->count; ++i) {
774 term = set->objs[i];
775
776 /* get the referenced nodes (subtrees) */
777 LYD_VALUE_GET(&term->value, xp_val);
Michal Vaskobe1b0cb2024-01-22 14:32:15 +0100778 if ((ret = lyd_find_xpath3(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data,
Michal Vaskoddd76592022-01-17 13:34:48 +0100779 NULL, &par_set))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200780 lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Parent reference \"%s\" evaluation failed.",
781 lyxp_get_expr(xp_val->exp));
Michal Vaskoddd76592022-01-17 13:34:48 +0100782 goto cleanup;
783 }
784
785 for (j = 0; j < par_set->count; ++j) {
786 /* duplicate with parents in the context of the mounted data */
787 if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL,
Michal Vaskofbbea932022-06-07 11:00:55 +0200788 LYD_DUP_RECURSIVE | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_EXT, &dup))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100789 goto cleanup;
790 }
791
792 /* go top-level */
793 while (dup->parent) {
794 dup = lyd_parent(dup);
795 }
796
797 /* check whether the top-level node exists */
798 if (first) {
799 if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) {
800 goto cleanup;
801 }
802 } else {
803 top_node = NULL;
804 }
805
806 if (top_node) {
807 /* merge */
808 ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT);
809 dup = NULL;
810 if (ret) {
811 goto cleanup;
812 }
813 } else {
814 /* insert */
815 if ((ret = lyd_insert_sibling(first, dup, &first))) {
816 goto cleanup;
817 }
818
819 /* add into the result set because a new top-level node was added */
820 if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) {
821 goto cleanup;
822 }
823 dup = NULL;
824 }
825 }
826 }
827
828cleanup:
829 free(path);
830 ly_set_free(set, NULL);
831 ly_set_free(par_set, NULL);
832 lyd_free_tree(dup);
833 if (ret && *ref_set) {
834 if ((*ref_set)->count) {
835 lyd_free_siblings((*ref_set)->dnodes[0]);
836 }
837 ly_set_free(*ref_set, NULL);
838 *ref_set = NULL;
839 }
840 return ret;
841}
842
ekinzie0ab8b302022-10-10 03:03:57 -0400843LY_ERR
844lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs)
845{
aPiecekb72a6df2022-10-10 10:02:38 +0200846 LY_ERR rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400847 struct ly_set *pref_set = NULL;
aPiecekb72a6df2022-10-10 10:02:38 +0200848 struct ly_set *snode_set = NULL;
ekinzie0ab8b302022-10-10 03:03:57 -0400849 struct ly_set *results_set = NULL;
850 struct lyd_node *ext_data;
851 ly_bool ext_data_free;
852
853 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
aPiecekb72a6df2022-10-10 10:02:38 +0200854 if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
855 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400856 }
857
aPiecekb72a6df2022-10-10 10:02:38 +0200858 LY_CHECK_GOTO(rc = schema_mount_get_parent_ref(ext, ext_data, &pref_set), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400859 if (pref_set->count == 0) {
aPiecekb72a6df2022-10-10 10:02:38 +0200860 goto cleanup;
ekinzie0ab8b302022-10-10 03:03:57 -0400861 }
862
aPiecekb72a6df2022-10-10 10:02:38 +0200863 LY_CHECK_GOTO(rc = ly_set_new(&results_set), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400864
865 for (uint32_t i = 0; i < pref_set->count; ++i) {
866 struct lyd_node_term *term;
867 struct lyd_value_xpath10 *xp_val;
868 char *value;
869 struct ly_err_item *err;
870
871 term = (struct lyd_node_term *)pref_set->dnodes[i];
872 LYD_VALUE_GET(&term->value, xp_val);
aPiecekb72a6df2022-10-10 10:02:38 +0200873 LY_CHECK_GOTO(rc = lyplg_type_print_xpath10_value(xp_val, LY_VALUE_JSON, NULL, &value, &err), cleanup);
874 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 -0400875 free(value);
876 for (uint32_t sn = 0; sn < snode_set->count; sn++) {
aPiecekb72a6df2022-10-10 10:02:38 +0200877 LY_CHECK_GOTO(rc = ly_set_add(results_set, snode_set->snodes[sn], 0, NULL), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400878 }
879 ly_set_free(snode_set, NULL);
aPiecekb72a6df2022-10-10 10:02:38 +0200880 snode_set = NULL;
ekinzie0ab8b302022-10-10 03:03:57 -0400881 }
882
883 *refs = results_set;
884
aPiecekb72a6df2022-10-10 10:02:38 +0200885cleanup:
886 if (rc) {
887 ly_set_free(results_set, NULL);
888 }
889 ly_set_free(snode_set, NULL);
ekinzie0ab8b302022-10-10 03:03:57 -0400890 if (ext_data_free) {
891 lyd_free_all(ext_data);
892 }
893 ly_set_free(pref_set, NULL);
aPiecekb72a6df2022-10-10 10:02:38 +0200894
895 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400896}
897
Michal Vaskoddd76592022-01-17 13:34:48 +0100898/**
899 * @brief Validate callback for schema mount.
900 */
901static LY_ERR
Michal Vaskofbbea932022-06-07 11:00:55 +0200902schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree,
903 enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskoddd76592022-01-17 13:34:48 +0100904{
905 LY_ERR ret = LY_SUCCESS;
Michal Vasko59004e32024-01-30 16:09:54 +0100906 uint32_t *prev_lo, temp_lo = LY_LOSTORE_LAST, i;
Michal Vasko7a266772024-01-23 11:02:38 +0100907 const struct ly_err_item *err;
Michal Vaskofbbea932022-06-07 11:00:55 +0200908 struct lyd_node *iter, *ext_data = NULL, *ref_first = NULL, *orig_parent = lyd_parent(sibling), *op_tree;
Michal Vaskof4c6f002022-04-01 09:12:22 +0200909 struct lyd_node *ext_diff = NULL, *diff_parent = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100910 ly_bool ext_data_free = 0;
911 struct ly_set *ref_set = NULL;
912
913 if (!sibling) {
914 /* some data had to be parsed for this callback to be called */
Michal Vasko193dacd2022-10-13 08:43:05 +0200915 EXT_LOGERR_INT_RET(NULL, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100916 }
917
918 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
919 if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
920 goto cleanup;
921 }
922
923 LY_LIST_FOR(ext_data, iter) {
924 if (iter->flags & LYD_NEW) {
925 /* must be validated for the parent-reference prefix data to be stored */
Michal Vasko193dacd2022-10-13 08:43:05 +0200926 lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated.");
Michal Vaskoddd76592022-01-17 13:34:48 +0100927 ret = LY_EINVAL;
928 goto cleanup;
929 }
930 }
931
932 /* duplicate the referenced parent nodes into ext context */
933 if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) {
934 goto cleanup;
935 }
936
Michal Vaskofbbea932022-06-07 11:00:55 +0200937 if (data_type != LYD_TYPE_DATA_YANG) {
938 /* remember the operation data tree, it may be moved */
939 op_tree = sibling;
940 }
941
Michal Vaskoddd76592022-01-17 13:34:48 +0100942 /* create accessible tree, remove LYD_EXT to not call this callback recursively */
aPiecekdfde77d2024-01-15 15:16:01 +0100943 LY_CHECK_GOTO(lyd_unlink_siblings(sibling), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +0100944 LY_LIST_FOR(sibling, iter) {
945 iter->flags &= ~LYD_EXT;
946 }
947 if (ref_set->count) {
948 if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) {
949 goto cleanup;
950 }
951 }
952
953 /* only store messages in the context, log as an extension */
Michal Vasko59004e32024-01-30 16:09:54 +0100954 prev_lo = ly_temp_log_options(&temp_lo);
Michal Vaskoddd76592022-01-17 13:34:48 +0100955
Michal Vaskofbbea932022-06-07 11:00:55 +0200956 if (data_type == LYD_TYPE_DATA_YANG) {
Michal Vaskob1b431e2022-12-08 08:33:31 +0100957 /* validate all the modules with data */
958 ret = lyd_validate_all(&sibling, NULL, val_opts | LYD_VALIDATE_PRESENT, diff ? &ext_diff : NULL);
Michal Vaskofbbea932022-06-07 11:00:55 +0200959 } else {
960 /* validate the operation */
961 ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL);
962 }
963
964 /* restore logging */
Michal Vasko59004e32024-01-30 16:09:54 +0100965 ly_temp_log_options(prev_lo);
Michal Vaskoddd76592022-01-17 13:34:48 +0100966
967 /* restore sibling tree */
968 for (i = 0; i < ref_set->count; ++i) {
969 if (ref_set->dnodes[i] == sibling) {
970 sibling = sibling->next;
971 }
972 lyd_free_tree(ref_set->dnodes[i]);
973 }
974 LY_LIST_FOR(sibling, iter) {
975 iter->flags |= LYD_EXT;
976 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200977 lyplg_ext_insert(orig_parent, sibling);
Michal Vaskoddd76592022-01-17 13:34:48 +0100978
979 if (ret) {
980 /* log the error in the original context */
981 err = ly_err_first(LYD_CTX(sibling));
982 if (!err) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200983 lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Unknown validation error (err code %d).", ret);
Michal Vaskoddd76592022-01-17 13:34:48 +0100984 } else {
Michal Vasko6727c682023-02-17 10:40:26 +0100985 lyplg_ext_compile_log_err(err, ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100986 }
987 goto cleanup;
988 }
989
Michal Vaskof4c6f002022-04-01 09:12:22 +0200990 /* create proper diff */
991 if (diff && ext_diff) {
992 /* diff nodes from an extension instance */
993 LY_LIST_FOR(ext_diff, iter) {
994 iter->flags |= LYD_EXT;
995 }
996
997 /* create the parent and insert the diff */
998 if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) {
999 goto cleanup;
1000 }
Michal Vasko193dacd2022-10-13 08:43:05 +02001001 if ((ret = lyplg_ext_insert(diff_parent, ext_diff))) {
Michal Vaskof4c6f002022-04-01 09:12:22 +02001002 goto cleanup;
1003 }
1004 ext_diff = NULL;
1005
1006 /* go top-level and set the operation */
1007 while (lyd_parent(diff_parent)) {
1008 diff_parent = lyd_parent(diff_parent);
1009 }
1010 if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) {
1011 goto cleanup;
1012 }
1013
1014 /* finally merge into the global diff */
1015 if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) {
1016 goto cleanup;
1017 }
1018 }
1019
Michal Vaskoddd76592022-01-17 13:34:48 +01001020cleanup:
1021 ly_set_free(ref_set, NULL);
1022 lyd_free_siblings(ref_first);
Michal Vaskof4c6f002022-04-01 09:12:22 +02001023 lyd_free_tree(ext_diff);
1024 lyd_free_all(diff_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001025 if (ext_data_free) {
1026 lyd_free_all(ext_data);
1027 }
1028 return ret;
1029}
1030
1031/**
Michal Vasko193dacd2022-10-13 08:43:05 +02001032 * @brief Schema mount compile free.
Michal Vaskoddd76592022-01-17 13:34:48 +01001033 *
Michal Vasko193dacd2022-10-13 08:43:05 +02001034 * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree.
Michal Vaskoddd76592022-01-17 13:34:48 +01001035 */
1036static void
Michal Vasko193dacd2022-10-13 08:43:05 +02001037schema_mount_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
Michal Vaskoddd76592022-01-17 13:34:48 +01001038{
Michal Vasko193dacd2022-10-13 08:43:05 +02001039 struct lyplg_ext_sm *sm_data = ext->compiled;
Michal Vaskoddd76592022-01-17 13:34:48 +01001040 uint32_t i;
1041
1042 if (!sm_data) {
1043 return;
1044 }
1045
1046 if (!--sm_data->shared->ref_count) {
1047 for (i = 0; i < sm_data->shared->schema_count; ++i) {
1048 ly_ctx_destroy(sm_data->shared->schemas[i].ctx);
1049 lydict_remove(ctx, sm_data->shared->schemas[i].content_id);
1050 }
1051 free(sm_data->shared->schemas);
Michal Vaskoddd76592022-01-17 13:34:48 +01001052 free(sm_data->shared);
1053 }
1054
1055 for (i = 0; i < sm_data->inln.schema_count; ++i) {
1056 ly_ctx_destroy(sm_data->inln.schemas[i].ctx);
1057 }
1058 free(sm_data->inln.schemas);
Michal Vasko27653162022-11-21 13:58:44 +01001059
1060 pthread_mutex_destroy(&sm_data->lock);
Michal Vaskoddd76592022-01-17 13:34:48 +01001061 free(sm_data);
1062}
1063
ekinzie0ab8b302022-10-10 03:03:57 -04001064LIBYANG_API_DEF LY_ERR
1065lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx)
1066{
Michal Vasko266308d2022-11-08 08:45:31 +01001067 struct lyd_node *ext_data = NULL;
1068 ly_bool ext_data_free = 0, config;
1069 LY_ERR rc = LY_SUCCESS;
ekinzie0ab8b302022-10-10 03:03:57 -04001070
1071 if (!ext->module->ctx->ext_clb) {
1072 return LY_EINVAL;
1073 }
1074
Michal Vasko266308d2022-11-08 08:45:31 +01001075 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) {
ekinzie0ab8b302022-10-10 03:03:57 -04001076 return LY_EINVAL;
1077 }
1078
1079 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
Michal Vasko266308d2022-11-08 08:45:31 +01001080 if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
1081 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -04001082 }
1083
1084 /* learn about this mount point */
Michal Vasko266308d2022-11-08 08:45:31 +01001085 if ((rc = schema_mount_get_smount(ext, ext_data, &config, NULL))) {
1086 goto cleanup;
ekinzie0ab8b302022-10-10 03:03:57 -04001087 }
1088
Michal Vasko266308d2022-11-08 08:45:31 +01001089 /* create the context */
1090 rc = schema_mount_create_ctx(ext, ext_data, config, ctx);
ekinzie0ab8b302022-10-10 03:03:57 -04001091
Michal Vasko266308d2022-11-08 08:45:31 +01001092cleanup:
ekinzie0ab8b302022-10-10 03:03:57 -04001093 if (ext_data_free) {
1094 lyd_free_all(ext_data);
1095 }
Michal Vasko266308d2022-11-08 08:45:31 +01001096 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -04001097}
1098
aPiecek03cb4872022-10-24 10:31:51 +02001099static void
1100schema_mount_spriter_tree_free(void *priv)
1101{
1102 struct sprinter_tree_priv *st_priv;
1103
1104 st_priv = priv;
1105 ly_set_free(st_priv->refs, NULL);
1106 ly_ctx_destroy(st_priv->ext_ctx);
1107 free(st_priv);
1108}
1109
1110static LY_ERR
1111schema_mount_sprinter_tree_cnode_override_mounted(const struct lysc_node *node, const void *UNUSED(plugin_priv),
1112 ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts)
1113{
1114 if (!node->parent) {
1115 *add_opts = "/";
1116 }
1117
1118 return LY_SUCCESS;
1119}
1120
1121static LY_ERR
1122schema_mount_sprinter_tree_pnode_override_mounted(const struct lysp_node *node, const void *UNUSED(plugin_priv),
1123 ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts)
1124{
1125 if (!node->parent) {
1126 *add_opts = "/";
1127 }
1128
1129 return LY_SUCCESS;
1130}
1131
1132static LY_ERR
1133schema_mount_sprinter_tree_node_override_parent_refs(const struct lysc_node *node, const void *plugin_priv,
1134 ly_bool *skip, const char **UNUSED(flags), const char **add_opts)
1135{
1136 uint32_t i;
1137 const struct ly_set *refs;
1138 const struct lysc_module *mod;
1139 struct lysc_node *ref, *iter;
1140
1141 refs = ((struct sprinter_tree_priv *)plugin_priv)->refs;
1142 mod = node->module->compiled;
1143
1144 /* Assume the @p node will be skipped. */
1145 *skip = 1;
1146 for (i = 0; (i < refs->count) && *skip; i++) {
1147 ref = refs->snodes[i];
1148 if (ref->module->compiled != mod) {
1149 /* parent-reference points to different module */
1150 continue;
1151 }
1152
1153 for (iter = ref; iter; iter = iter->parent) {
1154 if (iter == node) {
1155 /* @p node is not skipped because it is parent-rererence node or his parent */
1156 *skip = 0;
1157 break;
1158 }
1159 }
1160 }
1161
1162 if (!*skip && !node->parent) {
1163 /* top-node has additional opts */
1164 *add_opts = "@";
1165 }
1166
1167 return LY_SUCCESS;
1168}
1169
Michal Vasko266308d2022-11-08 08:45:31 +01001170/**
1171 * @brief Schema mount schema parsed tree printer.
1172 *
1173 * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree.
1174 */
aPiecek03cb4872022-10-24 10:31:51 +02001175static LY_ERR
1176schema_mount_sprinter_ptree(struct lysp_ext_instance *UNUSED(ext), const struct lyspr_tree_ctx *ctx,
1177 const char **flags, const char **UNUSED(add_opts))
1178{
1179 if (!ctx) {
1180 *flags = "mp";
1181 }
1182
1183 return LY_SUCCESS;
1184}
1185
Michal Vasko266308d2022-11-08 08:45:31 +01001186/**
1187 * @brief Schema mount schema compiled tree printer.
1188 *
1189 * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree.
1190 */
aPiecek03cb4872022-10-24 10:31:51 +02001191static LY_ERR
1192schema_mount_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx,
1193 const char **flags, const char **UNUSED(add_opts))
1194{
1195 LY_ERR rc = LY_SUCCESS;
1196 struct ly_ctx *ext_ctx = NULL;
1197 const struct lys_module *mod;
1198 struct ly_set *refs = NULL;
1199 struct lysc_node *tree1, *tree2;
1200 uint32_t i, j;
1201 ly_bool from_parent_ref, is_first;
1202 struct sprinter_tree_priv *st_priv;
1203
1204 if (!ctx) {
1205 *flags = "mp";
1206 return LY_SUCCESS;
1207 }
1208
1209 if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) {
1210 /* Void mount point */
1211 return LY_SUCCESS;
1212 }
1213
1214 rc = lyplg_ext_schema_mount_get_parent_ref(ext, &refs);
1215 LY_CHECK_GOTO(rc, cleanup);
1216
1217 /* build new list of modules to print. This list will omit internal
1218 * modules, modules with no nodes (e.g., iana-if-types) and modules
1219 * that were loaded as the result of a parent-reference.
1220 */
1221 i = ly_ctx_internal_modules_count(ext_ctx);
1222 while ((mod = ly_ctx_get_module_iter(ext_ctx, &i))) {
1223 from_parent_ref = 0;
1224
1225 for (j = 0; refs && j < refs->count; j++) {
1226 if (!strcmp(mod->ns, refs->snodes[j]->module->ns)) {
1227 from_parent_ref = 1;
1228 break;
1229 }
1230 }
1231 if (from_parent_ref) {
1232 /* Modules loaded as the result of a parent-reference are added later. */
1233 continue;
1234 }
1235
1236 /* Add data nodes, rpcs and notifications. */
1237 if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) {
1238 /* For compiled module. */
1239 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, mod->compiled->data,
1240 schema_mount_sprinter_tree_cnode_override_mounted);
1241 LY_CHECK_GOTO(rc, cleanup);
1242 if (mod->compiled->rpcs) {
1243 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->rpcs->node,
1244 schema_mount_sprinter_tree_cnode_override_mounted);
1245 }
1246 LY_CHECK_GOTO(rc, cleanup);
1247 if (mod->compiled->notifs) {
1248 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->notifs->node,
1249 schema_mount_sprinter_tree_cnode_override_mounted);
1250 }
1251 LY_CHECK_GOTO(rc, cleanup);
1252 } else {
1253 /* For parsed module. */
1254 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, mod->parsed->data,
1255 schema_mount_sprinter_tree_pnode_override_mounted);
1256 LY_CHECK_GOTO(rc, cleanup);
1257 if (mod->parsed->rpcs) {
1258 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->rpcs->node,
1259 schema_mount_sprinter_tree_pnode_override_mounted);
1260 }
1261 LY_CHECK_GOTO(rc, cleanup);
1262 if (mod->parsed->notifs) {
1263 rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->notifs->node,
1264 schema_mount_sprinter_tree_pnode_override_mounted);
1265 }
1266 LY_CHECK_GOTO(rc, cleanup);
1267 }
1268 }
1269
1270 /* Add modules loaded as the result of a parent-reference. */
1271 for (i = 0; refs && (i < refs->count); i++) {
1272 tree1 = refs->snodes[i]->module->compiled->data;
1273
1274 /* Add data nodes from the module only once. */
1275 is_first = 1;
1276 for (j = 0; j < i; j++) {
1277 tree2 = refs->snodes[j]->module->compiled->data;
1278 if (tree1 == tree2) {
1279 is_first = 0;
1280 break;
1281 }
1282 }
1283 if (is_first) {
1284 /* Add all data nodes but unavailable nodes are skipped in the callback. */
1285 rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, tree1, schema_mount_sprinter_tree_node_override_parent_refs);
1286 LY_CHECK_GOTO(rc, cleanup);
1287 }
1288 }
1289
Michal Vaskodaa3f012022-12-05 10:40:38 +01001290 /* add private plugin data */
aPiecek03cb4872022-10-24 10:31:51 +02001291 st_priv = calloc(1, sizeof(*st_priv));
Michal Vaskodaa3f012022-12-05 10:40:38 +01001292 LY_CHECK_ERR_GOTO(!st_priv, rc = LY_EMEM, cleanup);
aPiecek03cb4872022-10-24 10:31:51 +02001293 st_priv->ext_ctx = ext_ctx;
1294 st_priv->refs = refs;
1295 rc = lyplg_ext_sprinter_tree_set_priv(ctx, st_priv, schema_mount_spriter_tree_free);
1296
1297cleanup:
1298 if (rc) {
1299 ly_set_free(refs, NULL);
1300 ly_ctx_destroy(ext_ctx);
1301 }
1302
1303 return rc;
1304}
1305
Michal Vaskoddd76592022-01-17 13:34:48 +01001306/**
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001307 * @brief Plugin descriptions for the Yang Schema Mount extension.
1308 *
1309 * Note that external plugins are supposed to use:
1310 *
1311 * LYPLG_EXTENSIONS = {
1312 */
1313const struct lyplg_ext_record plugins_schema_mount[] = {
1314 {
1315 .module = "ietf-yang-schema-mount",
1316 .revision = "2019-01-14",
1317 .name = "mount-point",
1318
Michal Vasko193dacd2022-10-13 08:43:05 +02001319 .plugin.id = "ly2 schema mount v1",
1320 .plugin.parse = schema_mount_parse,
1321 .plugin.compile = schema_mount_compile,
Michal Vasko941e0562022-10-18 10:35:00 +02001322 .plugin.printer_info = NULL,
aPiecek03cb4872022-10-24 10:31:51 +02001323 .plugin.printer_ctree = schema_mount_sprinter_ctree,
1324 .plugin.printer_ptree = schema_mount_sprinter_ptree,
Michal Vasko135719f2022-08-25 12:18:17 +02001325 .plugin.node = NULL,
Michal Vasko193dacd2022-10-13 08:43:05 +02001326 .plugin.snode = schema_mount_snode,
1327 .plugin.validate = schema_mount_validate,
1328 .plugin.pfree = NULL,
1329 .plugin.cfree = schema_mount_cfree
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001330 },
1331 {0} /* terminating zeroed item */
1332};