blob: 1cfea526e75607b6855b0e6bbdc336faf4430f47 [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 */
255 if (!lyd_find_path(mpoint, "config", 0, &node) && !strcmp(lyd_get_value(node), "false")) {
256 *config = 0;
257 } else {
258 *config = 1;
259 }
260
261 /* check schema-ref */
262 if (lyd_find_path(mpoint, "shared-schema", 0, NULL)) {
263 if (lyd_find_path(mpoint, "inline", 0, NULL)) {
264 EXT_LOGERR_INT_RET(ext);
265 }
266 *shared = 0;
267 } else {
268 *shared = 1;
269 }
270
271 return LY_SUCCESS;
272}
273
274/**
275 * @brief Create schema (context) based on retrieved extension data.
276 *
277 * @param[in] ext Compiled extension instance.
278 * @param[in] ext_data Extension data retrieved by the callback.
279 * @param[in] config Whether the whole schema should keep its config or be set to false.
280 * @param[out] ext_ctx Schema to use for parsing the data.
281 * @return LY_ERR value.
282 */
283static LY_ERR
284schema_mount_create_ctx(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
285 struct ly_ctx **ext_ctx)
286{
Michal Vasko0fca45c2022-06-07 10:57:32 +0200287 LY_ERR rc = LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +0100288 const char * const *searchdirs;
Michal Vasko0fca45c2022-06-07 10:57:32 +0200289 char *sdirs = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100290 const struct lys_module *mod;
291 struct lysc_node *root, *node;
Michal Vasko0fca45c2022-06-07 10:57:32 +0200292 uint32_t i, idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100293
294 /* get searchdirs from the current context */
295 searchdirs = ly_ctx_get_searchdirs(ext->module->ctx);
296
Michal Vasko0fca45c2022-06-07 10:57:32 +0200297 if (searchdirs) {
298 /* append them all into a single string */
299 for (i = 0; searchdirs[i]; ++i) {
Jan Kundrát1fbc90d2022-06-07 21:27:53 +0200300 if ((rc = ly_strcat(&sdirs, "%s" PATH_SEPARATOR, searchdirs[i]))) {
Michal Vasko0fca45c2022-06-07 10:57:32 +0200301 goto cleanup;
302 }
303 }
304 }
305
Michal Vaskoddd76592022-01-17 13:34:48 +0100306 /* create the context based on the data */
Michal Vasko0fca45c2022-06-07 10:57:32 +0200307 if ((rc = ly_ctx_new_yldata(sdirs, ext_data, ly_ctx_get_options(ext->module->ctx), ext_ctx))) {
308 lyplg_ext_log(ext, LY_LLERR, rc, NULL, "Failed to create context for the schema-mount data.");
309 goto cleanup;
Michal Vaskoddd76592022-01-17 13:34:48 +0100310 }
311
312 if (!config) {
313 /* manually change the config of all schema nodes in all the modules */
314 while ((mod = ly_ctx_get_module_iter(*ext_ctx, &idx))) {
315 if (!mod->implemented) {
316 continue;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100317 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100318
Michal Vaskoddd76592022-01-17 13:34:48 +0100319 LY_LIST_FOR(mod->compiled->data, root) {
320 LYSC_TREE_DFS_BEGIN(root, node) {
321 node->flags &= ~LYS_CONFIG_W;
322 node->flags |= LYS_CONFIG_R;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100323
Michal Vaskoddd76592022-01-17 13:34:48 +0100324 LYSC_TREE_DFS_END(root, node);
325 }
326 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100327 }
328 }
329
Michal Vasko0fca45c2022-06-07 10:57:32 +0200330cleanup:
331 free(sdirs);
332 return rc;
Michal Vaskoddd76592022-01-17 13:34:48 +0100333}
334
335/**
336 * @brief Get schema (context) for a shared-schema mount point.
337 *
338 * @param[in] ext Compiled extension instance.
339 * @param[in] ext_data Extension data retrieved by the callback.
340 * @param[in] config Whether the whole schema should keep its config or be set to false.
341 * @param[out] ext_ctx Schema to use for parsing the data.
342 * @return LY_ERR value.
343 */
344static LY_ERR
345schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
346 const struct ly_ctx **ext_ctx)
347{
348 struct lyplg_ext_sm *sm_data = ext->data;
349 LY_ERR ret = LY_SUCCESS, r;
350 struct lyd_node *node = NULL;
351 struct ly_ctx *new_ctx;
352 uint32_t i;
353 const char *content_id = NULL;
354 void *mem;
355
356 assert(sm_data && sm_data->shared);
357
358 /* get yang-library content-id or module-set-id */
359 if (ext_data) {
360 lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, &node);
361 if (!node) {
362 lyd_find_path(ext_data, "/ietf-yang-library:modules-state/module-set-id", 0, &node);
363 }
364 if (node) {
365 content_id = lyd_get_value(node);
366 }
367 }
368 if (!content_id) {
369 lyplg_ext_log(ext, LY_LLERR, LY_EVALID, NULL, "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data.");
370 return LY_EVALID;
371 }
372
373 /* LOCK */
374 if ((r = pthread_mutex_lock(&sm_data->shared->lock))) {
375 lyplg_ext_log(ext, LY_LLERR, LY_ESYS, NULL, "Mutex lock failed (%s).", strerror(r));
376 return LY_ESYS;
377 }
378
379 /* try to find this mount point */
380 for (i = 0; i < sm_data->shared->schema_count; ++i) {
381 if (ext->argument == sm_data->shared->schemas[i].mount_point) {
382 break;
383 }
384 }
385
386 if (i < sm_data->shared->schema_count) {
387 /* schema exists already */
388 if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) {
389 lyplg_ext_log(ext, LY_LLERR, LY_EVALID, "/ietf-yang-library:yang-library/content-id",
390 "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.",
391 content_id, sm_data->shared->schemas[i].content_id);
392 ret = LY_EVALID;
393 goto cleanup;
394 }
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100395 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +0100396 /* no schema found, create it */
397 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
398 ret = r;
399 goto cleanup;
400 }
401
402 /* new entry */
403 mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas);
404 if (!mem) {
405 ly_ctx_destroy(new_ctx);
406 EXT_LOGERR_MEM_GOTO(ext, ret, cleanup);
407 }
408 sm_data->shared->schemas = mem;
409 ++sm_data->shared->schema_count;
410
411 /* fill entry */
412 sm_data->shared->schemas[i].ctx = new_ctx;
413 sm_data->shared->schemas[i].mount_point = ext->argument;
414 lydict_insert(ext->module->ctx, content_id, 0, &sm_data->shared->schemas[i].content_id);
415 }
416
417 /* use the context */
418 *ext_ctx = sm_data->shared->schemas[i].ctx;
419
420cleanup:
421 /* UNLOCK */
422 pthread_mutex_unlock(&sm_data->shared->lock);
423
424 return ret;
425}
426
427/**
428 * @brief Get schema (context) for an inline mount point.
429 *
430 * @param[in] ext Compiled extension instance.
431 * @param[in] ext_data Extension data retrieved by the callback.
432 * @param[in] config Whether the whole schema should keep its config or be set to false.
433 * @param[out] ext_ctx Schema to use for parsing the data.
434 * @return LY_ERR value.
435 */
436static LY_ERR
437schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
438 const struct ly_ctx **ext_ctx)
439{
440 struct lyplg_ext_sm *sm_data = ext->data;
441 LY_ERR r;
442 struct ly_ctx *new_ctx;
443 uint32_t i;
444 void *mem;
445
446 assert(sm_data && sm_data->shared);
447
448 i = sm_data->inln.schema_count;
449
450 /* always new schema required, create context */
451 if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) {
452 return r;
453 }
454
455 /* new entry */
456 mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas);
457 if (!mem) {
458 ly_ctx_destroy(new_ctx);
459 EXT_LOGERR_MEM_RET(ext);
460 }
461 sm_data->inln.schemas = mem;
462 ++sm_data->inln.schema_count;
463
464 /* fill entry */
465 sm_data->inln.schemas[i].ctx = new_ctx;
466
467 /* use the context */
468 *ext_ctx = sm_data->inln.schemas[i].ctx;
469 return LY_SUCCESS;
470}
471
472/**
473 * @brief Get schema (context) for a mount point.
474 *
475 * @param[in] ext Compiled extension instance.
476 * @param[out] ext_ctx Schema to use for parsing the data.
477 * @return LY_ERR value.
478 */
479static LY_ERR
480schema_mount_get_ctx(struct lysc_ext_instance *ext, const struct ly_ctx **ext_ctx)
481{
482 LY_ERR ret = LY_SUCCESS, r;
483 struct lyd_node *iter, *ext_data = NULL;
484 ly_bool ext_data_free = 0, config, shared;
485
486 *ext_ctx = NULL;
487
488 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
489 if ((r = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
490 ret = r;
491 goto cleanup;
492 }
493
494 LY_LIST_FOR(ext_data, iter) {
495 if (iter->flags & LYD_NEW) {
496 /* must be validated for the parent-reference prefix data to be stored */
497 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "Provided ext data have not been validated.");
498 ret = LY_EINVAL;
499 goto cleanup;
500 }
501 }
502
503 /* learn about this mount point */
504 if ((r = schema_mount_get_smount(ext, ext_data, &config, &shared))) {
505 ret = r;
506 goto cleanup;
507 }
508
509 /* create/get the context for parsing the data */
510 if (shared) {
511 r = schema_mount_get_ctx_shared(ext, ext_data, config, ext_ctx);
512 } else {
513 r = schema_mount_get_ctx_inline(ext, ext_data, config, ext_ctx);
514 }
515 if (r) {
516 ret = r;
517 goto cleanup;
518 }
519
520cleanup:
521 if (ext_data_free) {
522 lyd_free_all(ext_data);
523 }
524 return ret;
525}
526
527/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200528 * @brief Snode callback for schema mount.
529 * Check if data are valid for schema mount and returns their schema node.
Michal Vaskoddd76592022-01-17 13:34:48 +0100530 */
531static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +0200532schema_mount_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent,
533 const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
534 const struct lysc_node **snode)
Michal Vaskoddd76592022-01-17 13:34:48 +0100535{
Michal Vasko8cc3f662022-03-29 11:25:51 +0200536 LY_ERR r;
537 const struct lys_module *mod;
Michal Vaskoddd76592022-01-17 13:34:48 +0100538 const struct ly_ctx *ext_ctx;
Michal Vaskoddd76592022-01-17 13:34:48 +0100539
540 /* get context based on ietf-yang-library data */
541 if ((r = schema_mount_get_ctx(ext, &ext_ctx))) {
542 return r;
543 }
544
Michal Vasko8cc3f662022-03-29 11:25:51 +0200545 /* get the module */
546 mod = lyplg_type_identity_module(ext_ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data);
547 if (!mod) {
548 return LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100549 }
550
Michal Vasko8cc3f662022-03-29 11:25:51 +0200551 /* get the top-level schema node */
552 *snode = lys_find_child(NULL, mod, name, name_len, 0, 0);
553 return *snode ? LY_SUCCESS : LY_ENOT;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100554}
555
556/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100557 * @brief Duplicate all accessible parent references for a shared-schema mount point.
558 *
559 * @param[in] ext Compiled extension instance.
560 * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions.
561 * @param[in] ext_data Extension data retrieved by the callback.
562 * @param[in] trg_ctx Mounted data context to use for duplication.
563 * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty.
564 * @return LY_ERR value.
565 */
566static LY_ERR
567schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node,
568 const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set)
569{
570 LY_ERR ret = LY_SUCCESS;
571 char *path = NULL;
572 struct ly_set *set = NULL, *par_set = NULL;
573 struct lyd_node_term *term;
574 struct lyd_node *dup = NULL, *top_node, *first;
575 struct lyd_value_xpath10 *xp_val;
576 uint32_t i, j;
577
578 *ref_set = NULL;
579
580 if (!ext_data) {
581 /* we expect the same ext data as before and there must be some for data to be parsed */
582 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "No ext data provided.");
583 ret = LY_EINVAL;
584 goto cleanup;
585 }
586
587 /* get all parent references of this mount point */
588 if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']"
589 "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) {
590 EXT_LOGERR_MEM_GOTO(ext, ret, cleanup);
591 }
592 if ((ret = lyd_find_xpath(ext_data, path, &set))) {
593 goto cleanup;
594 }
595
596 /* prepare result set */
597 if ((ret = ly_set_new(ref_set))) {
598 goto cleanup;
599 }
600
601 first = NULL;
602 for (i = 0; i < set->count; ++i) {
603 term = set->objs[i];
604
605 /* get the referenced nodes (subtrees) */
606 LYD_VALUE_GET(&term->value, xp_val);
607 if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data,
608 NULL, &par_set))) {
Michal Vaskofbbea932022-06-07 11:00:55 +0200609 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 +0100610 goto cleanup;
611 }
612
613 for (j = 0; j < par_set->count; ++j) {
614 /* duplicate with parents in the context of the mounted data */
615 if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL,
Michal Vaskofbbea932022-06-07 11:00:55 +0200616 LYD_DUP_RECURSIVE | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_EXT, &dup))) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100617 goto cleanup;
618 }
619
620 /* go top-level */
621 while (dup->parent) {
622 dup = lyd_parent(dup);
623 }
624
625 /* check whether the top-level node exists */
626 if (first) {
627 if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) {
628 goto cleanup;
629 }
630 } else {
631 top_node = NULL;
632 }
633
634 if (top_node) {
635 /* merge */
636 ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT);
637 dup = NULL;
638 if (ret) {
639 goto cleanup;
640 }
641 } else {
642 /* insert */
643 if ((ret = lyd_insert_sibling(first, dup, &first))) {
644 goto cleanup;
645 }
646
647 /* add into the result set because a new top-level node was added */
648 if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) {
649 goto cleanup;
650 }
651 dup = NULL;
652 }
653 }
654 }
655
656cleanup:
657 free(path);
658 ly_set_free(set, NULL);
659 ly_set_free(par_set, NULL);
660 lyd_free_tree(dup);
661 if (ret && *ref_set) {
662 if ((*ref_set)->count) {
663 lyd_free_siblings((*ref_set)->dnodes[0]);
664 }
665 ly_set_free(*ref_set, NULL);
666 *ref_set = NULL;
667 }
668 return ret;
669}
670
671/**
672 * @brief Validate callback for schema mount.
673 */
674static LY_ERR
Michal Vaskofbbea932022-06-07 11:00:55 +0200675schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree,
676 enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff)
Michal Vaskoddd76592022-01-17 13:34:48 +0100677{
678 LY_ERR ret = LY_SUCCESS;
679 uint32_t old_log_opts, i;
680 struct ly_err_item *err;
Michal Vaskofbbea932022-06-07 11:00:55 +0200681 struct lyd_node *iter, *ext_data = NULL, *ref_first = NULL, *orig_parent = lyd_parent(sibling), *op_tree;
Michal Vaskof4c6f002022-04-01 09:12:22 +0200682 struct lyd_node *ext_diff = NULL, *diff_parent = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100683 ly_bool ext_data_free = 0;
684 struct ly_set *ref_set = NULL;
685
686 if (!sibling) {
687 /* some data had to be parsed for this callback to be called */
688 EXT_LOGERR_INT_RET(ext);
689 }
690
691 /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
692 if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) {
693 goto cleanup;
694 }
695
696 LY_LIST_FOR(ext_data, iter) {
697 if (iter->flags & LYD_NEW) {
698 /* must be validated for the parent-reference prefix data to be stored */
699 lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "Provided ext data have not been validated.");
700 ret = LY_EINVAL;
701 goto cleanup;
702 }
703 }
704
705 /* duplicate the referenced parent nodes into ext context */
706 if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) {
707 goto cleanup;
708 }
709
Michal Vaskofbbea932022-06-07 11:00:55 +0200710 if (data_type != LYD_TYPE_DATA_YANG) {
711 /* remember the operation data tree, it may be moved */
712 op_tree = sibling;
713 }
714
Michal Vaskoddd76592022-01-17 13:34:48 +0100715 /* create accessible tree, remove LYD_EXT to not call this callback recursively */
716 lyd_unlink_siblings(sibling);
717 LY_LIST_FOR(sibling, iter) {
718 iter->flags &= ~LYD_EXT;
719 }
720 if (ref_set->count) {
721 if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) {
722 goto cleanup;
723 }
724 }
725
726 /* only store messages in the context, log as an extension */
727 old_log_opts = ly_log_options(LY_LOSTORE_LAST);
728
Michal Vaskofbbea932022-06-07 11:00:55 +0200729 if (data_type == LYD_TYPE_DATA_YANG) {
730 /* validate all the data */
731 ret = lyd_validate_all(&sibling, NULL, val_opts, diff ? &ext_diff : NULL);
732 } else {
733 /* validate the operation */
734 ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL);
735 }
736
737 /* restore logging */
Michal Vaskoddd76592022-01-17 13:34:48 +0100738 ly_log_options(old_log_opts);
739
740 /* restore sibling tree */
741 for (i = 0; i < ref_set->count; ++i) {
742 if (ref_set->dnodes[i] == sibling) {
743 sibling = sibling->next;
744 }
745 lyd_free_tree(ref_set->dnodes[i]);
746 }
747 LY_LIST_FOR(sibling, iter) {
748 iter->flags |= LYD_EXT;
749 }
750 lyd_insert_ext(orig_parent, sibling);
751
752 if (ret) {
753 /* log the error in the original context */
754 err = ly_err_first(LYD_CTX(sibling));
755 if (!err) {
756 lyplg_ext_log(ext, LY_LLERR, ret, NULL, "Unknown validation error (err code %d).", ret);
757 } else {
758 lyplg_ext_log(ext, LY_LLERR, err->no, err->path, "%s", err->msg);
759 }
760 goto cleanup;
761 }
762
Michal Vaskof4c6f002022-04-01 09:12:22 +0200763 /* create proper diff */
764 if (diff && ext_diff) {
765 /* diff nodes from an extension instance */
766 LY_LIST_FOR(ext_diff, iter) {
767 iter->flags |= LYD_EXT;
768 }
769
770 /* create the parent and insert the diff */
771 if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) {
772 goto cleanup;
773 }
774 if ((ret = lyd_insert_ext(diff_parent, ext_diff))) {
775 goto cleanup;
776 }
777 ext_diff = NULL;
778
779 /* go top-level and set the operation */
780 while (lyd_parent(diff_parent)) {
781 diff_parent = lyd_parent(diff_parent);
782 }
783 if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) {
784 goto cleanup;
785 }
786
787 /* finally merge into the global diff */
788 if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) {
789 goto cleanup;
790 }
791 }
792
Michal Vaskoddd76592022-01-17 13:34:48 +0100793cleanup:
794 ly_set_free(ref_set, NULL);
795 lyd_free_siblings(ref_first);
Michal Vaskof4c6f002022-04-01 09:12:22 +0200796 lyd_free_tree(ext_diff);
797 lyd_free_all(diff_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +0100798 if (ext_data_free) {
799 lyd_free_all(ext_data);
800 }
801 return ret;
802}
803
804/**
805 * @brief Schema mount free.
806 *
807 * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free.
808 */
809static void
810schema_mount_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
811{
812 struct lyplg_ext_sm *sm_data = ext->data;
813 uint32_t i;
814
815 if (!sm_data) {
816 return;
817 }
818
819 if (!--sm_data->shared->ref_count) {
820 for (i = 0; i < sm_data->shared->schema_count; ++i) {
821 ly_ctx_destroy(sm_data->shared->schemas[i].ctx);
822 lydict_remove(ctx, sm_data->shared->schemas[i].content_id);
823 }
824 free(sm_data->shared->schemas);
825 pthread_mutex_destroy(&sm_data->shared->lock);
826 free(sm_data->shared);
827 }
828
829 for (i = 0; i < sm_data->inln.schema_count; ++i) {
830 ly_ctx_destroy(sm_data->inln.schemas[i].ctx);
831 }
832 free(sm_data->inln.schemas);
833 free(sm_data);
834}
835
836/**
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100837 * @brief Plugin descriptions for the Yang Schema Mount extension.
838 *
839 * Note that external plugins are supposed to use:
840 *
841 * LYPLG_EXTENSIONS = {
842 */
843const struct lyplg_ext_record plugins_schema_mount[] = {
844 {
845 .module = "ietf-yang-schema-mount",
846 .revision = "2019-01-14",
847 .name = "mount-point",
848
849 .plugin.id = "libyang 2 - Schema Mount, version 1",
850 .plugin.compile = &schema_mount_compile,
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100851 .plugin.sprinter = NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +0100852 .plugin.free = &schema_mount_free,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200853 .plugin.snode = &schema_mount_snode,
Michal Vaskoddd76592022-01-17 13:34:48 +0100854 .plugin.validate = &schema_mount_validate
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100855 },
856 {0} /* terminating zeroed item */
857};