blob: 042bebb832a167cd70400d1083acf5507f5cbce7 [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;
ekinzie0ab8b302022-10-10 03:03:57 -0400354 struct ly_ctx *new_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100355 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;
ekinzie0ab8b302022-10-10 03:03:57 -0400445 struct ly_ctx *new_ctx = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100446 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;
ekinzie0ab8b302022-10-10 03:03:57 -0400541 const struct ly_ctx *ext_ctx = NULL;
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
ekinzie0ab8b302022-10-10 03:03:57 -0400559static LY_ERR
560schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data,
561 struct ly_set **set)
562{
563 LY_ERR ret = LY_SUCCESS;
564 char *path = NULL;
565
566 /* get all parent references of this mount point */
567 if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']"
568 "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) {
569 EXT_LOGERR_MEM_GOTO(ext, ret, cleanup);
570 }
571 if ((ret = lyd_find_xpath(ext_data, path, set))) {
572 goto cleanup;
573 }
574
575cleanup:
576 free(path);
577 return ret;
578}
579
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100580/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100581 * @brief Duplicate all accessible parent references for a shared-schema mount point.
582 *
583 * @param[in] ext Compiled extension instance.
584 * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions.
585 * @param[in] ext_data Extension data retrieved by the callback.
586 * @param[in] trg_ctx Mounted data context to use for duplication.
587 * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty.
588 * @return LY_ERR value.
589 */
590static LY_ERR
591schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node,
592 const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set)
593{
594 LY_ERR ret = LY_SUCCESS;
595 char *path = NULL;
596 struct ly_set *set = NULL, *par_set = NULL;
597 struct lyd_node_term *term;
598 struct lyd_node *dup = NULL, *top_node, *first;
599 struct lyd_value_xpath10 *xp_val;
600 uint32_t i, j;
601
602 *ref_set = NULL;
603
604 if (!ext_data) {
605 /* we expect the same ext data as before and there must be some for data to be parsed */
606 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "No ext data provided.");
607 ret = LY_EINVAL;
608 goto cleanup;
609 }
610
ekinzie0ab8b302022-10-10 03:03:57 -0400611 if ((ret = schema_mount_get_parent_ref(ext, ext_data, &set))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100612 goto cleanup;
613 }
614
615 /* prepare result set */
616 if ((ret = ly_set_new(ref_set))) {
617 goto cleanup;
618 }
619
620 first = NULL;
621 for (i = 0; i < set->count; ++i) {
622 term = set->objs[i];
623
624 /* get the referenced nodes (subtrees) */
625 LYD_VALUE_GET(&term->value, xp_val);
626 if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data,
627 NULL, &par_set))) {
Michal Vaskofbbea932022-06-07 11:00:55 +0200628 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 +0100629 goto cleanup;
630 }
631
632 for (j = 0; j < par_set->count; ++j) {
633 /* duplicate with parents in the context of the mounted data */
634 if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL,
Michal Vaskofbbea932022-06-07 11:00:55 +0200635 LYD_DUP_RECURSIVE | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_EXT, &dup))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100636 goto cleanup;
637 }
638
639 /* go top-level */
640 while (dup->parent) {
641 dup = lyd_parent(dup);
642 }
643
644 /* check whether the top-level node exists */
645 if (first) {
646 if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) {
647 goto cleanup;
648 }
649 } else {
650 top_node = NULL;
651 }
652
653 if (top_node) {
654 /* merge */
655 ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT);
656 dup = NULL;
657 if (ret) {
658 goto cleanup;
659 }
660 } else {
661 /* insert */
662 if ((ret = lyd_insert_sibling(first, dup, &first))) {
663 goto cleanup;
664 }
665
666 /* add into the result set because a new top-level node was added */
667 if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) {
668 goto cleanup;
669 }
670 dup = NULL;
671 }
672 }
673 }
674
675cleanup:
676 free(path);
677 ly_set_free(set, NULL);
678 ly_set_free(par_set, NULL);
679 lyd_free_tree(dup);
680 if (ret && *ref_set) {
681 if ((*ref_set)->count) {
682 lyd_free_siblings((*ref_set)->dnodes[0]);
683 }
684 ly_set_free(*ref_set, NULL);
685 *ref_set = NULL;
686 }
687 return ret;
688}
689
ekinzie0ab8b302022-10-10 03:03:57 -0400690LY_ERR
691lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs)
692{
aPiecekb72a6df2022-10-10 10:02:38 +0200693 LY_ERR rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400694 struct ly_set *pref_set = NULL;
aPiecekb72a6df2022-10-10 10:02:38 +0200695 struct ly_set *snode_set = NULL;
ekinzie0ab8b302022-10-10 03:03:57 -0400696 struct ly_set *results_set = NULL;
697 struct lyd_node *ext_data;
698 ly_bool ext_data_free;
699
700 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
aPiecekb72a6df2022-10-10 10:02:38 +0200701 if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
702 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400703 }
704
aPiecekb72a6df2022-10-10 10:02:38 +0200705 LY_CHECK_GOTO(rc = schema_mount_get_parent_ref(ext, ext_data, &pref_set), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400706 if (pref_set->count == 0) {
aPiecekb72a6df2022-10-10 10:02:38 +0200707 goto cleanup;
ekinzie0ab8b302022-10-10 03:03:57 -0400708 }
709
aPiecekb72a6df2022-10-10 10:02:38 +0200710 LY_CHECK_GOTO(rc = ly_set_new(&results_set), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400711
712 for (uint32_t i = 0; i < pref_set->count; ++i) {
713 struct lyd_node_term *term;
714 struct lyd_value_xpath10 *xp_val;
715 char *value;
716 struct ly_err_item *err;
717
718 term = (struct lyd_node_term *)pref_set->dnodes[i];
719 LYD_VALUE_GET(&term->value, xp_val);
aPiecekb72a6df2022-10-10 10:02:38 +0200720 LY_CHECK_GOTO(rc = lyplg_type_print_xpath10_value(xp_val, LY_VALUE_JSON, NULL, &value, &err), cleanup);
721 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 -0400722 free(value);
723 for (uint32_t sn = 0; sn < snode_set->count; sn++) {
aPiecekb72a6df2022-10-10 10:02:38 +0200724 LY_CHECK_GOTO(rc = ly_set_add(results_set, snode_set->snodes[sn], 0, NULL), cleanup);
ekinzie0ab8b302022-10-10 03:03:57 -0400725 }
726 ly_set_free(snode_set, NULL);
aPiecekb72a6df2022-10-10 10:02:38 +0200727 snode_set = NULL;
ekinzie0ab8b302022-10-10 03:03:57 -0400728 }
729
730 *refs = results_set;
731
aPiecekb72a6df2022-10-10 10:02:38 +0200732cleanup:
733 if (rc) {
734 ly_set_free(results_set, NULL);
735 }
736 ly_set_free(snode_set, NULL);
ekinzie0ab8b302022-10-10 03:03:57 -0400737 if (ext_data_free) {
738 lyd_free_all(ext_data);
739 }
740 ly_set_free(pref_set, NULL);
aPiecekb72a6df2022-10-10 10:02:38 +0200741
742 return rc;
ekinzie0ab8b302022-10-10 03:03:57 -0400743}
744
Michal Vaskoddd76592022-01-17 13:34:48 +0100745/**
746 * @brief Validate callback for schema mount.
747 */
748static LY_ERR
Michal Vaskofbbea932022-06-07 11:00:55 +0200749schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree,
750 enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskoddd76592022-01-17 13:34:48 +0100751{
752 LY_ERR ret = LY_SUCCESS;
753 uint32_t old_log_opts, i;
754 struct ly_err_item *err;
Michal Vaskofbbea932022-06-07 11:00:55 +0200755 struct lyd_node *iter, *ext_data = NULL, *ref_first = NULL, *orig_parent = lyd_parent(sibling), *op_tree;
Michal Vaskof4c6f002022-04-01 09:12:22 +0200756 struct lyd_node *ext_diff = NULL, *diff_parent = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100757 ly_bool ext_data_free = 0;
758 struct ly_set *ref_set = NULL;
759
760 if (!sibling) {
761 /* some data had to be parsed for this callback to be called */
762 EXT_LOGERR_INT_RET(ext);
763 }
764
765 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
766 if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
767 goto cleanup;
768 }
769
770 LY_LIST_FOR(ext_data, iter) {
771 if (iter->flags & LYD_NEW) {
772 /* must be validated for the parent-reference prefix data to be stored */
773 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "Provided ext data have not been validated.");
774 ret = LY_EINVAL;
775 goto cleanup;
776 }
777 }
778
779 /* duplicate the referenced parent nodes into ext context */
780 if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) {
781 goto cleanup;
782 }
783
Michal Vaskofbbea932022-06-07 11:00:55 +0200784 if (data_type != LYD_TYPE_DATA_YANG) {
785 /* remember the operation data tree, it may be moved */
786 op_tree = sibling;
787 }
788
Michal Vaskoddd76592022-01-17 13:34:48 +0100789 /* create accessible tree, remove LYD_EXT to not call this callback recursively */
790 lyd_unlink_siblings(sibling);
791 LY_LIST_FOR(sibling, iter) {
792 iter->flags &= ~LYD_EXT;
793 }
794 if (ref_set->count) {
795 if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) {
796 goto cleanup;
797 }
798 }
799
800 /* only store messages in the context, log as an extension */
801 old_log_opts = ly_log_options(LY_LOSTORE_LAST);
802
Michal Vaskofbbea932022-06-07 11:00:55 +0200803 if (data_type == LYD_TYPE_DATA_YANG) {
804 /* validate all the data */
805 ret = lyd_validate_all(&sibling, NULL, val_opts, diff ? &ext_diff : NULL);
806 } else {
807 /* validate the operation */
808 ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL);
809 }
810
811 /* restore logging */
Michal Vaskoddd76592022-01-17 13:34:48 +0100812 ly_log_options(old_log_opts);
813
814 /* restore sibling tree */
815 for (i = 0; i < ref_set->count; ++i) {
816 if (ref_set->dnodes[i] == sibling) {
817 sibling = sibling->next;
818 }
819 lyd_free_tree(ref_set->dnodes[i]);
820 }
821 LY_LIST_FOR(sibling, iter) {
822 iter->flags |= LYD_EXT;
823 }
824 lyd_insert_ext(orig_parent, sibling);
825
826 if (ret) {
827 /* log the error in the original context */
828 err = ly_err_first(LYD_CTX(sibling));
829 if (!err) {
830 lyplg_ext_log(ext, LY_LLERR, ret, NULL, "Unknown validation error (err code %d).", ret);
831 } else {
832 lyplg_ext_log(ext, LY_LLERR, err->no, err->path, "%s", err->msg);
833 }
834 goto cleanup;
835 }
836
Michal Vaskof4c6f002022-04-01 09:12:22 +0200837 /* create proper diff */
838 if (diff && ext_diff) {
839 /* diff nodes from an extension instance */
840 LY_LIST_FOR(ext_diff, iter) {
841 iter->flags |= LYD_EXT;
842 }
843
844 /* create the parent and insert the diff */
845 if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) {
846 goto cleanup;
847 }
848 if ((ret = lyd_insert_ext(diff_parent, ext_diff))) {
849 goto cleanup;
850 }
851 ext_diff = NULL;
852
853 /* go top-level and set the operation */
854 while (lyd_parent(diff_parent)) {
855 diff_parent = lyd_parent(diff_parent);
856 }
857 if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) {
858 goto cleanup;
859 }
860
861 /* finally merge into the global diff */
862 if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) {
863 goto cleanup;
864 }
865 }
866
Michal Vaskoddd76592022-01-17 13:34:48 +0100867cleanup:
868 ly_set_free(ref_set, NULL);
869 lyd_free_siblings(ref_first);
Michal Vaskof4c6f002022-04-01 09:12:22 +0200870 lyd_free_tree(ext_diff);
871 lyd_free_all(diff_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +0100872 if (ext_data_free) {
873 lyd_free_all(ext_data);
874 }
875 return ret;
876}
877
878/**
879 * @brief Schema mount free.
880 *
881 * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free.
882 */
883static void
884schema_mount_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
885{
886 struct lyplg_ext_sm *sm_data = ext->data;
887 uint32_t i;
888
889 if (!sm_data) {
890 return;
891 }
892
893 if (!--sm_data->shared->ref_count) {
894 for (i = 0; i < sm_data->shared->schema_count; ++i) {
895 ly_ctx_destroy(sm_data->shared->schemas[i].ctx);
896 lydict_remove(ctx, sm_data->shared->schemas[i].content_id);
897 }
898 free(sm_data->shared->schemas);
899 pthread_mutex_destroy(&sm_data->shared->lock);
900 free(sm_data->shared);
901 }
902
903 for (i = 0; i < sm_data->inln.schema_count; ++i) {
904 ly_ctx_destroy(sm_data->inln.schemas[i].ctx);
905 }
906 free(sm_data->inln.schemas);
907 free(sm_data);
908}
909
ekinzie0ab8b302022-10-10 03:03:57 -0400910LIBYANG_API_DEF LY_ERR
911lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx)
912{
913 struct lyd_node *ext_data;
914 ly_bool ext_data_free;
915 ly_bool config;
916 ly_bool shared;
917 LY_ERR res;
918
919 if (!ext->module->ctx->ext_clb) {
920 return LY_EINVAL;
921 }
922
923 if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") ||
924 strcmp(ext->def->name, "mount-point")) {
925 return LY_EINVAL;
926 }
927
928 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
929 if ((res = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
930 return res;
931 }
932
933 /* learn about this mount point */
934 if ((res = schema_mount_get_smount(ext, ext_data, &config, &shared))) {
935 goto out;
936 }
937
938 res = schema_mount_create_ctx(ext, ext_data, config, ctx);
939
940out:
941 if (ext_data_free) {
942 lyd_free_all(ext_data);
943 }
944 return res;
945}
946
Michal Vaskoddd76592022-01-17 13:34:48 +0100947/**
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100948 * @brief Plugin descriptions for the Yang Schema Mount extension.
949 *
950 * Note that external plugins are supposed to use:
951 *
952 * LYPLG_EXTENSIONS = {
953 */
954const struct lyplg_ext_record plugins_schema_mount[] = {
955 {
956 .module = "ietf-yang-schema-mount",
957 .revision = "2019-01-14",
958 .name = "mount-point",
959
960 .plugin.id = "libyang 2 - Schema Mount, version 1",
961 .plugin.compile = &schema_mount_compile,
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100962 .plugin.sprinter = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100963 .plugin.free = &schema_mount_free,
Michal Vasko135719f2022-08-25 12:18:17 +0200964 .plugin.node = NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200965 .plugin.snode = &schema_mount_snode,
Michal Vaskoddd76592022-01-17 13:34:48 +0100966 .plugin.validate = &schema_mount_validate
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100967 },
968 {0} /* terminating zeroed item */
969};