blob: 7aed9a9b2b97b62bab53458a37e03db3ff4663e8 [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"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010024#include "dict.h"
25#include "libyang.h"
26#include "log.h"
Michal Vaskofbbea932022-06-07 11:00:55 +020027#include "parser_data.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010028#include "plugins_exts.h"
Michal Vasko8cc3f662022-03-29 11:25:51 +020029#include "plugins_types.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010030#include "tree_data.h"
31#include "tree_schema.h"
32
33/**
Michal Vaskoddd76592022-01-17 13:34:48 +010034 * @brief Internal schema mount data structure for holding all the contexts of parsed data.
35 */
36struct lyplg_ext_sm {
37 struct lyplg_ext_sm_shared {
38 pthread_mutex_t lock; /**< lock for accessing this shared structure */
39
40 struct {
41 struct ly_ctx *ctx; /**< context shared between all data of this mount point */
42 const char *mount_point; /**< mount point name */
43 const char *content_id; /**< yang-library content-id (alternatively module-set-id),
44 stored in the dictionary of the ext instance context */
45 } *schemas; /**< array of shared schema schemas */
46 uint32_t schema_count; /**< length of schemas array */
47 uint32_t ref_count; /**< number of references to this structure (mount-points with the same name
48 in the module) */
49 } *shared; /**< shared schema mount points */
50
51 struct lyplg_ext_sm_inln {
52 struct {
53 struct ly_ctx *ctx; /**< context created for single inline schema data */
54 } *schemas; /**< array of inline schemas */
55 uint32_t schema_count; /**< length of schemas array */
56 } inln; /**< inline mount points */
57};
58
59#define EXT_LOGERR_MEM_RET(ext) \
60 lyplg_ext_log(ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
61 return LY_EMEM
62
63#define EXT_LOGERR_MEM_GOTO(ext, rc, label) \
64 lyplg_ext_log(ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
65 rc = LY_EMEM; \
66 goto label
67
68#define EXT_LOGERR_INT_RET(ext) \
69 lyplg_ext_log(ext, LY_LLERR, LY_EINT, NULL, "Internal error (%s:%d).", __FILE__, __LINE__); \
70 return LY_EINT
71
72/**
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010073 * @brief Check if given mount point is unique among its' siblings
74 *
75 * @param cctx Compilation context.
76 * @param c_ext Compiled extension instance for checking uniqueness.
77 * @param p_ext Extension instance of the mount-point for comparison.
Michal Vaskoddd76592022-01-17 13:34:48 +010078 * @return LY_SUCCESS if is unique;
79 * @return LY_EINVAL otherwise.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010080 */
81static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +010082schema_mount_compile_unique_mp(struct lysc_ctx *cctx, const struct lysc_ext_instance *c_ext,
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010083 const struct lysp_ext_instance *p_ext)
84{
Michal Vaskoddd76592022-01-17 13:34:48 +010085 struct lysc_ext_instance *exts;
86 LY_ARRAY_COUNT_TYPE u;
87 struct lysc_node *parent;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010088
Michal Vaskoddd76592022-01-17 13:34:48 +010089 /* check if it is the only instance of the mount-point among its' siblings */
90 parent = (struct lysc_node *)c_ext->parent;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010091 exts = parent->exts;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010092 LY_ARRAY_FOR(exts, u) {
Michal Vaskoddd76592022-01-17 13:34:48 +010093 if (&exts[u] == c_ext) {
94 continue;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010095 }
Michal Vaskoddd76592022-01-17 13:34:48 +010096
97 if (!strcmp(exts[u].def->module->name, "ietf-yang-schema-mount") && !strcmp(exts[u].def->name, "mount-point")) {
98 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Multiple extension \"%s\" instances.",
99 p_ext->name);
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100100 return LY_EINVAL;
101 }
102 }
103 return LY_SUCCESS;
104}
105
Michal Vaskoddd76592022-01-17 13:34:48 +0100106struct lyplg_ext_sm_shared_cb_data {
107 const struct lysc_ext_instance *ext;
108 struct lyplg_ext_sm_shared *sm_shared;
109};
110
111static LY_ERR
112schema_mount_compile_mod_dfs_cb(struct lysc_node *node, void *data, ly_bool *dfs_continue)
113{
114 struct lyplg_ext_sm_shared_cb_data *cb_data = data;
115 struct lyplg_ext_sm *sm_data;
116 struct lysc_ext_instance *exts;
117 LY_ARRAY_COUNT_TYPE u;
118
119 (void)dfs_continue;
120
121 if (node == cb_data->ext->parent) {
122 /* parent of the current compiled extension, skip */
123 return LY_SUCCESS;
124 }
125
126 /* find the same mount point */
127 exts = node->exts;
128 LY_ARRAY_FOR(exts, u) {
129 if (!strcmp(exts[u].def->module->name, "ietf-yang-schema-mount") && !strcmp(exts[u].def->name, "mount-point") &&
130 (exts[u].argument == cb_data->ext->argument)) {
131 /* same mount point, break the DFS search */
132 sm_data = exts[u].data;
133 cb_data->sm_shared = sm_data->shared;
134 return LY_EEXIST;
135 }
136 }
137
138 /* not found, continue search */
139 return LY_SUCCESS;
140}
141
142static struct lyplg_ext_sm_shared *
143schema_mount_compile_find_shared(const struct lys_module *mod, const struct lysc_ext_instance *ext)
144{
145 struct lyplg_ext_sm_shared_cb_data cb_data;
146 LY_ERR r;
147
148 /* prepare cb_data */
149 cb_data.ext = ext;
150 cb_data.sm_shared = NULL;
151
152 /* try to find the same mount point */
153 r = lysc_module_dfs_full(mod, schema_mount_compile_mod_dfs_cb, &cb_data);
Michal Vasko2a12cd92022-02-14 09:34:54 +0100154 (void)r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100155 assert((!r && !cb_data.sm_shared) || ((r == LY_EEXIST) && cb_data.sm_shared));
156
157 return cb_data.sm_shared;
158}
159
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100160/**
161 * @brief Schema mount compile.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100162 * Checks if it can be a valid extension instance for yang schema mount.
163 *
164 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
165 */
166static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100167schema_mount_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100168{
169 const struct lys_module *cur_mod;
Michal Vaskodb641792022-03-21 10:04:54 +0100170 const struct lysc_node *node;
Michal Vaskoddd76592022-01-17 13:34:48 +0100171 struct lyplg_ext_sm *sm_data;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100172
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100173 assert(!strcmp(p_ext->name, "yangmnt:mount-point"));
174
Michal Vaskoddd76592022-01-17 13:34:48 +0100175 /* check YANG version 1.1 */
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100176 cur_mod = lysc_ctx_get_cur_mod(cctx);
177 if (cur_mod->parsed->version != LYS_VERSION_1_1) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100178 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
179 "Extension \"%s\" instance not allowed in YANG version 1 module.", p_ext->name);
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100180 return LY_EINVAL;
181 }
182
Michal Vaskoddd76592022-01-17 13:34:48 +0100183 /* check parent nodetype */
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100184 if ((p_ext->parent_stmt != LY_STMT_CONTAINER) && (p_ext->parent_stmt != LY_STMT_LIST)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100185 lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
186 "Extension \"%s\" instance allowed only in container or list statement.", p_ext->name);
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100187 return LY_EINVAL;
188 }
189
Michal Vaskoddd76592022-01-17 13:34:48 +0100190 /* check uniqueness */
191 if (schema_mount_compile_unique_mp(cctx, c_ext, p_ext)) {
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100192 return LY_EINVAL;
193 }
194
Michal Vaskoddd76592022-01-17 13:34:48 +0100195 /* init internal data */
196 sm_data = calloc(1, sizeof *sm_data);
197 if (!sm_data) {
198 EXT_LOGERR_MEM_RET(c_ext);
199 }
200 c_ext->data = sm_data;
201
Michal Vaskodb641792022-03-21 10:04:54 +0100202 /* find the owner module */
203 node = c_ext->parent;
204 while (node->parent) {
205 node = node->parent;
206 }
207
Michal Vaskoddd76592022-01-17 13:34:48 +0100208 /* reuse/init shared schema */
Michal Vaskodb641792022-03-21 10:04:54 +0100209 sm_data->shared = schema_mount_compile_find_shared(node->module, c_ext);
Michal Vaskoddd76592022-01-17 13:34:48 +0100210 if (sm_data->shared) {
211 ++sm_data->shared->ref_count;
212 } else {
213 sm_data->shared = calloc(1, sizeof *sm_data->shared);
214 if (!sm_data->shared) {
215 free(sm_data);
216 EXT_LOGERR_MEM_RET(c_ext);
217 }
218 pthread_mutex_init(&sm_data->shared->lock, NULL);
219 sm_data->shared->ref_count = 1;
220 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100221
222 return LY_SUCCESS;
223}
224
225/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100226 * @brief Learn details about the current mount point.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100227 *
Michal Vaskoddd76592022-01-17 13:34:48 +0100228 * @param[in] ext Compiled extension instance.
229 * @param[in] ext_data Extension data retrieved by the callback.
230 * @param[out] config Whether the whole schema should keep its config or be set to false.
231 * @param[out] shared Whether the schema is shared or inline.
232 * @return LY_ERR value.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100233 */
234static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100235schema_mount_get_smount(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool *config,
236 ly_bool *shared)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100237{
Michal Vaskoddd76592022-01-17 13:34:48 +0100238 struct lyd_node *mpoint, *node;
239 char *path = NULL;
240 LY_ERR r;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100241
Michal Vaskoddd76592022-01-17 13:34:48 +0100242 /* find the mount point */
243 if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']", ext->module->name,
244 ext->argument) == -1) {
245 EXT_LOGERR_MEM_RET(ext);
246 }
247 r = ext_data ? lyd_find_path(ext_data, path, 0, &mpoint) : LY_ENOTFOUND;
248 free(path);
249 if (r) {
250 /* missing mount-point, cannot be data for this extension (https://datatracker.ietf.org/doc/html/rfc8528#page-10) */
251 return LY_ENOT;
252 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100253
Michal Vaskoddd76592022-01-17 13:34:48 +0100254 /* check config */
Michal Vasko9e12ffe2022-09-21 15:11:30 +0200255 *config = 1;
Michal Vaskoddd76592022-01-17 13:34:48 +0100256 if (!lyd_find_path(mpoint, "config", 0, &node) && !strcmp(lyd_get_value(node), "false")) {
257 *config = 0;
Michal Vasko9e12ffe2022-09-21 15:11:30 +0200258 }
259 assert((ext->parent_stmt == LY_STMT_CONTAINER) || (ext->parent_stmt == LY_STMT_LIST));
260 if (((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_R) {
261 *config = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100262 }
263
264 /* check schema-ref */
265 if (lyd_find_path(mpoint, "shared-schema", 0, NULL)) {
266 if (lyd_find_path(mpoint, "inline", 0, NULL)) {
267 EXT_LOGERR_INT_RET(ext);
268 }
269 *shared = 0;
270 } else {
271 *shared = 1;
272 }
273
274 return LY_SUCCESS;
275}
276
277/**
278 * @brief Create schema (context) based on retrieved extension data.
279 *
280 * @param[in] ext Compiled extension instance.
281 * @param[in] ext_data Extension data retrieved by the callback.
282 * @param[in] config Whether the whole schema should keep its config or be set to false.
283 * @param[out] ext_ctx Schema to use for parsing the data.
284 * @return LY_ERR value.
285 */
286static LY_ERR
287schema_mount_create_ctx(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
288 struct ly_ctx **ext_ctx)
289{
Michal Vasko0fca45c2022-06-07 10:57:32 +0200290 LY_ERR rc = LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +0100291 const char * const *searchdirs;
Michal Vasko0fca45c2022-06-07 10:57:32 +0200292 char *sdirs = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100293 const struct lys_module *mod;
294 struct lysc_node *root, *node;
Michal Vasko0fca45c2022-06-07 10:57:32 +0200295 uint32_t i, idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100296
297 /* get searchdirs from the current context */
298 searchdirs = ly_ctx_get_searchdirs(ext->module->ctx);
299
Michal Vasko0fca45c2022-06-07 10:57:32 +0200300 if (searchdirs) {
301 /* append them all into a single string */
302 for (i = 0; searchdirs[i]; ++i) {
Jan Kundrát1fbc90d2022-06-07 21:27:53 +0200303 if ((rc = ly_strcat(&sdirs, "%s" PATH_SEPARATOR, searchdirs[i]))) {
Michal Vasko0fca45c2022-06-07 10:57:32 +0200304 goto cleanup;
305 }
306 }
307 }
308
Michal Vaskoddd76592022-01-17 13:34:48 +0100309 /* create the context based on the data */
Michal Vasko0fca45c2022-06-07 10:57:32 +0200310 if ((rc = ly_ctx_new_yldata(sdirs, ext_data, ly_ctx_get_options(ext->module->ctx), ext_ctx))) {
311 lyplg_ext_log(ext, LY_LLERR, rc, NULL, "Failed to create context for the schema-mount data.");
312 goto cleanup;
Michal Vaskoddd76592022-01-17 13:34:48 +0100313 }
314
315 if (!config) {
316 /* manually change the config of all schema nodes in all the modules */
317 while ((mod = ly_ctx_get_module_iter(*ext_ctx, &idx))) {
318 if (!mod->implemented) {
319 continue;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100320 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100321
Michal Vaskoddd76592022-01-17 13:34:48 +0100322 LY_LIST_FOR(mod->compiled->data, root) {
323 LYSC_TREE_DFS_BEGIN(root, node) {
324 node->flags &= ~LYS_CONFIG_W;
325 node->flags |= LYS_CONFIG_R;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100326
Michal Vaskoddd76592022-01-17 13:34:48 +0100327 LYSC_TREE_DFS_END(root, node);
328 }
329 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100330 }
331 }
332
Michal Vasko0fca45c2022-06-07 10:57:32 +0200333cleanup:
334 free(sdirs);
335 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100336}
337
338/**
339 * @brief Get schema (context) for a shared-schema mount point.
340 *
341 * @param[in] ext Compiled extension instance.
342 * @param[in] ext_data Extension data retrieved by the callback.
343 * @param[in] config Whether the whole schema should keep its config or be set to false.
344 * @param[out] ext_ctx Schema to use for parsing the data.
345 * @return LY_ERR value.
346 */
347static LY_ERR
348schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
349 const struct ly_ctx **ext_ctx)
350{
351 struct lyplg_ext_sm *sm_data = ext->data;
352 LY_ERR ret = LY_SUCCESS, r;
353 struct lyd_node *node = NULL;
354 struct ly_ctx *new_ctx;
355 uint32_t i;
356 const char *content_id = NULL;
357 void *mem;
358
359 assert(sm_data && sm_data->shared);
360
361 /* get yang-library content-id or module-set-id */
362 if (ext_data) {
363 lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, &node);
364 if (!node) {
365 lyd_find_path(ext_data, "/ietf-yang-library:modules-state/module-set-id", 0, &node);
366 }
367 if (node) {
368 content_id = lyd_get_value(node);
369 }
370 }
371 if (!content_id) {
372 lyplg_ext_log(ext, LY_LLERR, LY_EVALID, NULL, "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data.");
373 return LY_EVALID;
374 }
375
376 /* LOCK */
377 if ((r = pthread_mutex_lock(&sm_data->shared->lock))) {
378 lyplg_ext_log(ext, LY_LLERR, LY_ESYS, NULL, "Mutex lock failed (%s).", strerror(r));
379 return LY_ESYS;
380 }
381
382 /* try to find this mount point */
383 for (i = 0; i < sm_data->shared->schema_count; ++i) {
384 if (ext->argument == sm_data->shared->schemas[i].mount_point) {
385 break;
386 }
387 }
388
389 if (i < sm_data->shared->schema_count) {
390 /* schema exists already */
391 if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) {
392 lyplg_ext_log(ext, LY_LLERR, LY_EVALID, "/ietf-yang-library:yang-library/content-id",
393 "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.",
394 content_id, sm_data->shared->schemas[i].content_id);
395 ret = LY_EVALID;
396 goto cleanup;
397 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100398 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +0100399 /* no schema found, create it */
400 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
401 ret = r;
402 goto cleanup;
403 }
404
405 /* new entry */
406 mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas);
407 if (!mem) {
408 ly_ctx_destroy(new_ctx);
409 EXT_LOGERR_MEM_GOTO(ext, ret, cleanup);
410 }
411 sm_data->shared->schemas = mem;
412 ++sm_data->shared->schema_count;
413
414 /* fill entry */
415 sm_data->shared->schemas[i].ctx = new_ctx;
416 sm_data->shared->schemas[i].mount_point = ext->argument;
417 lydict_insert(ext->module->ctx, content_id, 0, &sm_data->shared->schemas[i].content_id);
418 }
419
420 /* use the context */
421 *ext_ctx = sm_data->shared->schemas[i].ctx;
422
423cleanup:
424 /* UNLOCK */
425 pthread_mutex_unlock(&sm_data->shared->lock);
426
427 return ret;
428}
429
430/**
431 * @brief Get schema (context) for an inline mount point.
432 *
433 * @param[in] ext Compiled extension instance.
434 * @param[in] ext_data Extension data retrieved by the callback.
435 * @param[in] config Whether the whole schema should keep its config or be set to false.
436 * @param[out] ext_ctx Schema to use for parsing the data.
437 * @return LY_ERR value.
438 */
439static LY_ERR
440schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
441 const struct ly_ctx **ext_ctx)
442{
443 struct lyplg_ext_sm *sm_data = ext->data;
444 LY_ERR r;
445 struct ly_ctx *new_ctx;
446 uint32_t i;
447 void *mem;
448
449 assert(sm_data && sm_data->shared);
450
451 i = sm_data->inln.schema_count;
452
453 /* always new schema required, create context */
454 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
455 return r;
456 }
457
458 /* new entry */
459 mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas);
460 if (!mem) {
461 ly_ctx_destroy(new_ctx);
462 EXT_LOGERR_MEM_RET(ext);
463 }
464 sm_data->inln.schemas = mem;
465 ++sm_data->inln.schema_count;
466
467 /* fill entry */
468 sm_data->inln.schemas[i].ctx = new_ctx;
469
470 /* use the context */
471 *ext_ctx = sm_data->inln.schemas[i].ctx;
472 return LY_SUCCESS;
473}
474
475/**
476 * @brief Get schema (context) for a mount point.
477 *
478 * @param[in] ext Compiled extension instance.
479 * @param[out] ext_ctx Schema to use for parsing the data.
480 * @return LY_ERR value.
481 */
482static LY_ERR
483schema_mount_get_ctx(struct lysc_ext_instance *ext, const struct ly_ctx **ext_ctx)
484{
485 LY_ERR ret = LY_SUCCESS, r;
486 struct lyd_node *iter, *ext_data = NULL;
487 ly_bool ext_data_free = 0, config, shared;
488
489 *ext_ctx = NULL;
490
491 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
492 if ((r = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
493 ret = r;
494 goto cleanup;
495 }
496
497 LY_LIST_FOR(ext_data, iter) {
498 if (iter->flags & LYD_NEW) {
499 /* must be validated for the parent-reference prefix data to be stored */
500 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "Provided ext data have not been validated.");
501 ret = LY_EINVAL;
502 goto cleanup;
503 }
504 }
505
506 /* learn about this mount point */
507 if ((r = schema_mount_get_smount(ext, ext_data, &config, &shared))) {
508 ret = r;
509 goto cleanup;
510 }
511
512 /* create/get the context for parsing the data */
513 if (shared) {
514 r = schema_mount_get_ctx_shared(ext, ext_data, config, ext_ctx);
515 } else {
516 r = schema_mount_get_ctx_inline(ext, ext_data, config, ext_ctx);
517 }
518 if (r) {
519 ret = r;
520 goto cleanup;
521 }
522
523cleanup:
524 if (ext_data_free) {
525 lyd_free_all(ext_data);
526 }
527 return ret;
528}
529
530/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200531 * @brief Snode callback for schema mount.
532 * Check if data are valid for schema mount and returns their schema node.
Michal Vaskoddd76592022-01-17 13:34:48 +0100533 */
534static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +0200535schema_mount_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent,
536 const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
537 const struct lysc_node **snode)
Michal Vaskoddd76592022-01-17 13:34:48 +0100538{
Michal Vasko8cc3f662022-03-29 11:25:51 +0200539 LY_ERR r;
540 const struct lys_module *mod;
Michal Vaskoddd76592022-01-17 13:34:48 +0100541 const struct ly_ctx *ext_ctx;
Michal Vaskoddd76592022-01-17 13:34:48 +0100542
543 /* get context based on ietf-yang-library data */
544 if ((r = schema_mount_get_ctx(ext, &ext_ctx))) {
545 return r;
546 }
547
Michal Vasko8cc3f662022-03-29 11:25:51 +0200548 /* get the module */
549 mod = lyplg_type_identity_module(ext_ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data);
550 if (!mod) {
551 return LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100552 }
553
Michal Vasko8cc3f662022-03-29 11:25:51 +0200554 /* get the top-level schema node */
555 *snode = lys_find_child(NULL, mod, name, name_len, 0, 0);
556 return *snode ? LY_SUCCESS : LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100557}
558
559/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100560 * @brief Duplicate all accessible parent references for a shared-schema mount point.
561 *
562 * @param[in] ext Compiled extension instance.
563 * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions.
564 * @param[in] ext_data Extension data retrieved by the callback.
565 * @param[in] trg_ctx Mounted data context to use for duplication.
566 * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty.
567 * @return LY_ERR value.
568 */
569static LY_ERR
570schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node,
571 const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set)
572{
573 LY_ERR ret = LY_SUCCESS;
574 char *path = NULL;
575 struct ly_set *set = NULL, *par_set = NULL;
576 struct lyd_node_term *term;
577 struct lyd_node *dup = NULL, *top_node, *first;
578 struct lyd_value_xpath10 *xp_val;
579 uint32_t i, j;
580
581 *ref_set = NULL;
582
583 if (!ext_data) {
584 /* we expect the same ext data as before and there must be some for data to be parsed */
585 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "No ext data provided.");
586 ret = LY_EINVAL;
587 goto cleanup;
588 }
589
590 /* get all parent references of this mount point */
591 if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']"
592 "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) {
593 EXT_LOGERR_MEM_GOTO(ext, ret, cleanup);
594 }
595 if ((ret = lyd_find_xpath(ext_data, path, &set))) {
596 goto cleanup;
597 }
598
599 /* prepare result set */
600 if ((ret = ly_set_new(ref_set))) {
601 goto cleanup;
602 }
603
604 first = NULL;
605 for (i = 0; i < set->count; ++i) {
606 term = set->objs[i];
607
608 /* get the referenced nodes (subtrees) */
609 LYD_VALUE_GET(&term->value, xp_val);
610 if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data,
611 NULL, &par_set))) {
Michal Vaskofbbea932022-06-07 11:00:55 +0200612 lyplg_ext_log(ext, LY_LLERR, ret, NULL, "Parent reference \"%s\" evaluation failed.", lyxp_get_expr(xp_val->exp));
Michal Vaskoddd76592022-01-17 13:34:48 +0100613 goto cleanup;
614 }
615
616 for (j = 0; j < par_set->count; ++j) {
617 /* duplicate with parents in the context of the mounted data */
618 if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL,
Michal Vaskofbbea932022-06-07 11:00:55 +0200619 LYD_DUP_RECURSIVE | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_EXT, &dup))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100620 goto cleanup;
621 }
622
623 /* go top-level */
624 while (dup->parent) {
625 dup = lyd_parent(dup);
626 }
627
628 /* check whether the top-level node exists */
629 if (first) {
630 if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) {
631 goto cleanup;
632 }
633 } else {
634 top_node = NULL;
635 }
636
637 if (top_node) {
638 /* merge */
639 ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT);
640 dup = NULL;
641 if (ret) {
642 goto cleanup;
643 }
644 } else {
645 /* insert */
646 if ((ret = lyd_insert_sibling(first, dup, &first))) {
647 goto cleanup;
648 }
649
650 /* add into the result set because a new top-level node was added */
651 if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) {
652 goto cleanup;
653 }
654 dup = NULL;
655 }
656 }
657 }
658
659cleanup:
660 free(path);
661 ly_set_free(set, NULL);
662 ly_set_free(par_set, NULL);
663 lyd_free_tree(dup);
664 if (ret && *ref_set) {
665 if ((*ref_set)->count) {
666 lyd_free_siblings((*ref_set)->dnodes[0]);
667 }
668 ly_set_free(*ref_set, NULL);
669 *ref_set = NULL;
670 }
671 return ret;
672}
673
674/**
675 * @brief Validate callback for schema mount.
676 */
677static LY_ERR
Michal Vaskofbbea932022-06-07 11:00:55 +0200678schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree,
679 enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskoddd76592022-01-17 13:34:48 +0100680{
681 LY_ERR ret = LY_SUCCESS;
682 uint32_t old_log_opts, i;
683 struct ly_err_item *err;
Michal Vaskofbbea932022-06-07 11:00:55 +0200684 struct lyd_node *iter, *ext_data = NULL, *ref_first = NULL, *orig_parent = lyd_parent(sibling), *op_tree;
Michal Vaskof4c6f002022-04-01 09:12:22 +0200685 struct lyd_node *ext_diff = NULL, *diff_parent = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100686 ly_bool ext_data_free = 0;
687 struct ly_set *ref_set = NULL;
688
689 if (!sibling) {
690 /* some data had to be parsed for this callback to be called */
691 EXT_LOGERR_INT_RET(ext);
692 }
693
694 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
695 if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
696 goto cleanup;
697 }
698
699 LY_LIST_FOR(ext_data, iter) {
700 if (iter->flags & LYD_NEW) {
701 /* must be validated for the parent-reference prefix data to be stored */
702 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "Provided ext data have not been validated.");
703 ret = LY_EINVAL;
704 goto cleanup;
705 }
706 }
707
708 /* duplicate the referenced parent nodes into ext context */
709 if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) {
710 goto cleanup;
711 }
712
Michal Vaskofbbea932022-06-07 11:00:55 +0200713 if (data_type != LYD_TYPE_DATA_YANG) {
714 /* remember the operation data tree, it may be moved */
715 op_tree = sibling;
716 }
717
Michal Vaskoddd76592022-01-17 13:34:48 +0100718 /* create accessible tree, remove LYD_EXT to not call this callback recursively */
719 lyd_unlink_siblings(sibling);
720 LY_LIST_FOR(sibling, iter) {
721 iter->flags &= ~LYD_EXT;
722 }
723 if (ref_set->count) {
724 if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) {
725 goto cleanup;
726 }
727 }
728
729 /* only store messages in the context, log as an extension */
730 old_log_opts = ly_log_options(LY_LOSTORE_LAST);
731
Michal Vaskofbbea932022-06-07 11:00:55 +0200732 if (data_type == LYD_TYPE_DATA_YANG) {
733 /* validate all the data */
734 ret = lyd_validate_all(&sibling, NULL, val_opts, diff ? &ext_diff : NULL);
735 } else {
736 /* validate the operation */
737 ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL);
738 }
739
740 /* restore logging */
Michal Vaskoddd76592022-01-17 13:34:48 +0100741 ly_log_options(old_log_opts);
742
743 /* restore sibling tree */
744 for (i = 0; i < ref_set->count; ++i) {
745 if (ref_set->dnodes[i] == sibling) {
746 sibling = sibling->next;
747 }
748 lyd_free_tree(ref_set->dnodes[i]);
749 }
750 LY_LIST_FOR(sibling, iter) {
751 iter->flags |= LYD_EXT;
752 }
753 lyd_insert_ext(orig_parent, sibling);
754
755 if (ret) {
756 /* log the error in the original context */
757 err = ly_err_first(LYD_CTX(sibling));
758 if (!err) {
759 lyplg_ext_log(ext, LY_LLERR, ret, NULL, "Unknown validation error (err code %d).", ret);
760 } else {
761 lyplg_ext_log(ext, LY_LLERR, err->no, err->path, "%s", err->msg);
762 }
763 goto cleanup;
764 }
765
Michal Vaskof4c6f002022-04-01 09:12:22 +0200766 /* create proper diff */
767 if (diff && ext_diff) {
768 /* diff nodes from an extension instance */
769 LY_LIST_FOR(ext_diff, iter) {
770 iter->flags |= LYD_EXT;
771 }
772
773 /* create the parent and insert the diff */
774 if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) {
775 goto cleanup;
776 }
777 if ((ret = lyd_insert_ext(diff_parent, ext_diff))) {
778 goto cleanup;
779 }
780 ext_diff = NULL;
781
782 /* go top-level and set the operation */
783 while (lyd_parent(diff_parent)) {
784 diff_parent = lyd_parent(diff_parent);
785 }
786 if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) {
787 goto cleanup;
788 }
789
790 /* finally merge into the global diff */
791 if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) {
792 goto cleanup;
793 }
794 }
795
Michal Vaskoddd76592022-01-17 13:34:48 +0100796cleanup:
797 ly_set_free(ref_set, NULL);
798 lyd_free_siblings(ref_first);
Michal Vaskof4c6f002022-04-01 09:12:22 +0200799 lyd_free_tree(ext_diff);
800 lyd_free_all(diff_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +0100801 if (ext_data_free) {
802 lyd_free_all(ext_data);
803 }
804 return ret;
805}
806
807/**
808 * @brief Schema mount free.
809 *
810 * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free.
811 */
812static void
813schema_mount_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
814{
815 struct lyplg_ext_sm *sm_data = ext->data;
816 uint32_t i;
817
818 if (!sm_data) {
819 return;
820 }
821
822 if (!--sm_data->shared->ref_count) {
823 for (i = 0; i < sm_data->shared->schema_count; ++i) {
824 ly_ctx_destroy(sm_data->shared->schemas[i].ctx);
825 lydict_remove(ctx, sm_data->shared->schemas[i].content_id);
826 }
827 free(sm_data->shared->schemas);
828 pthread_mutex_destroy(&sm_data->shared->lock);
829 free(sm_data->shared);
830 }
831
832 for (i = 0; i < sm_data->inln.schema_count; ++i) {
833 ly_ctx_destroy(sm_data->inln.schemas[i].ctx);
834 }
835 free(sm_data->inln.schemas);
836 free(sm_data);
837}
838
839/**
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100840 * @brief Plugin descriptions for the Yang Schema Mount extension.
841 *
842 * Note that external plugins are supposed to use:
843 *
844 * LYPLG_EXTENSIONS = {
845 */
846const struct lyplg_ext_record plugins_schema_mount[] = {
847 {
848 .module = "ietf-yang-schema-mount",
849 .revision = "2019-01-14",
850 .name = "mount-point",
851
852 .plugin.id = "libyang 2 - Schema Mount, version 1",
853 .plugin.compile = &schema_mount_compile,
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100854 .plugin.sprinter = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100855 .plugin.free = &schema_mount_free,
Michal Vasko135719f2022-08-25 12:18:17 +0200856 .plugin.node = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200857 .plugin.snode = &schema_mount_snode,
Michal Vaskoddd76592022-01-17 13:34:48 +0100858 .plugin.validate = &schema_mount_validate
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100859 },
860 {0} /* terminating zeroed item */
861};