tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 1 | /** |
| 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 Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 18 | #include <pthread.h> |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 23 | #include "common.h" |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 24 | #include "compat.h" |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 25 | #include "dict.h" |
| 26 | #include "libyang.h" |
| 27 | #include "log.h" |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 28 | #include "parser_data.h" |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 29 | #include "plugins_exts.h" |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 30 | #include "plugins_types.h" |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 31 | #include "tree_data.h" |
| 32 | #include "tree_schema.h" |
| 33 | |
| 34 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 35 | * @brief Internal schema mount data structure for holding all the contexts of parsed data. |
| 36 | */ |
| 37 | struct lyplg_ext_sm { |
| 38 | struct lyplg_ext_sm_shared { |
| 39 | pthread_mutex_t lock; /**< lock for accessing this shared structure */ |
| 40 | |
| 41 | struct { |
| 42 | struct ly_ctx *ctx; /**< context shared between all data of this mount point */ |
| 43 | const char *mount_point; /**< mount point name */ |
| 44 | const char *content_id; /**< yang-library content-id (alternatively module-set-id), |
| 45 | stored in the dictionary of the ext instance context */ |
| 46 | } *schemas; /**< array of shared schema schemas */ |
| 47 | uint32_t schema_count; /**< length of schemas array */ |
| 48 | uint32_t ref_count; /**< number of references to this structure (mount-points with the same name |
| 49 | in the module) */ |
| 50 | } *shared; /**< shared schema mount points */ |
| 51 | |
| 52 | struct lyplg_ext_sm_inln { |
| 53 | struct { |
| 54 | struct ly_ctx *ctx; /**< context created for single inline schema data */ |
| 55 | } *schemas; /**< array of inline schemas */ |
| 56 | uint32_t schema_count; /**< length of schemas array */ |
| 57 | } inln; /**< inline mount points */ |
| 58 | }; |
| 59 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 60 | struct sprinter_tree_priv { |
| 61 | struct ly_ctx *ext_ctx; |
| 62 | struct ly_set *refs; |
| 63 | }; |
| 64 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 65 | #define EXT_LOGERR_MEM_RET(cctx, ext) \ |
| 66 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 67 | return LY_EMEM |
| 68 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 69 | #define EXT_LOGERR_MEM_GOTO(cctx, ext, rc, label) \ |
| 70 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 71 | rc = LY_EMEM; \ |
| 72 | goto label |
| 73 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 74 | #define EXT_LOGERR_INT_RET(cctx, ext) \ |
| 75 | lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__); \ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 76 | return LY_EINT |
| 77 | |
| 78 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 79 | * @brief Check if given mount point is unique among its siblings |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 80 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 81 | * @param[in] pctx Parse context. |
| 82 | * @param[in] ext Parsed extension instance. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 83 | * @return LY_SUCCESS if is unique; |
| 84 | * @return LY_EINVAL otherwise. |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 85 | */ |
| 86 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 87 | schema_mount_parse_unique_mp(struct lysp_ctx *pctx, const struct lysp_ext_instance *ext) |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 88 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 89 | struct lysp_ext_instance *exts; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 90 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 91 | struct lysp_node *parent; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 92 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 93 | /* check if it is the only instance of the mount-point among its siblings */ |
| 94 | parent = ext->parent; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 95 | exts = parent->exts; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 96 | LY_ARRAY_FOR(exts, u) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 97 | if (&exts[u] == ext) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 98 | continue; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 99 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 100 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 101 | if (!strcmp(exts[u].name, ext->name)) { |
| 102 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Multiple extension \"%s\" instances.", ext->name); |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 103 | return LY_EINVAL; |
| 104 | } |
| 105 | } |
| 106 | return LY_SUCCESS; |
| 107 | } |
| 108 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 109 | /** |
| 110 | * @brief Schema mount parse. |
| 111 | * Checks if it can be a valid extension instance for yang schema mount. |
| 112 | * |
| 113 | * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse. |
| 114 | */ |
| 115 | static LY_ERR |
| 116 | schema_mount_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext) |
| 117 | { |
| 118 | /* check YANG version 1.1 */ |
| 119 | if (lyplg_ext_parse_get_cur_pmod(pctx)->version != LYS_VERSION_1_1) { |
| 120 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance not allowed in YANG version 1 module.", |
| 121 | ext->name); |
| 122 | return LY_EINVAL; |
| 123 | } |
| 124 | |
| 125 | /* check parent nodetype */ |
| 126 | if ((ext->parent_stmt != LY_STMT_CONTAINER) && (ext->parent_stmt != LY_STMT_LIST)) { |
| 127 | lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance allowed only in container or list statement.", |
| 128 | ext->name); |
| 129 | return LY_EINVAL; |
| 130 | } |
| 131 | |
| 132 | /* check uniqueness */ |
| 133 | if (schema_mount_parse_unique_mp(pctx, ext)) { |
| 134 | return LY_EINVAL; |
| 135 | } |
| 136 | |
| 137 | /* nothing to actually parse */ |
| 138 | return LY_SUCCESS; |
| 139 | } |
| 140 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 141 | struct lyplg_ext_sm_shared_cb_data { |
| 142 | const struct lysc_ext_instance *ext; |
| 143 | struct lyplg_ext_sm_shared *sm_shared; |
| 144 | }; |
| 145 | |
| 146 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 147 | schema_mount_compile_mod_dfs_cb(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue)) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 148 | { |
| 149 | struct lyplg_ext_sm_shared_cb_data *cb_data = data; |
| 150 | struct lyplg_ext_sm *sm_data; |
| 151 | struct lysc_ext_instance *exts; |
| 152 | LY_ARRAY_COUNT_TYPE u; |
| 153 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 154 | if (node == cb_data->ext->parent) { |
| 155 | /* parent of the current compiled extension, skip */ |
| 156 | return LY_SUCCESS; |
| 157 | } |
| 158 | |
| 159 | /* find the same mount point */ |
| 160 | exts = node->exts; |
| 161 | LY_ARRAY_FOR(exts, u) { |
| 162 | if (!strcmp(exts[u].def->module->name, "ietf-yang-schema-mount") && !strcmp(exts[u].def->name, "mount-point") && |
| 163 | (exts[u].argument == cb_data->ext->argument)) { |
| 164 | /* same mount point, break the DFS search */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 165 | sm_data = exts[u].compiled; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 166 | cb_data->sm_shared = sm_data->shared; |
| 167 | return LY_EEXIST; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* not found, continue search */ |
| 172 | return LY_SUCCESS; |
| 173 | } |
| 174 | |
| 175 | static struct lyplg_ext_sm_shared * |
| 176 | schema_mount_compile_find_shared(const struct lys_module *mod, const struct lysc_ext_instance *ext) |
| 177 | { |
| 178 | struct lyplg_ext_sm_shared_cb_data cb_data; |
| 179 | LY_ERR r; |
| 180 | |
| 181 | /* prepare cb_data */ |
| 182 | cb_data.ext = ext; |
| 183 | cb_data.sm_shared = NULL; |
| 184 | |
| 185 | /* try to find the same mount point */ |
| 186 | r = lysc_module_dfs_full(mod, schema_mount_compile_mod_dfs_cb, &cb_data); |
Michal Vasko | 2a12cd9 | 2022-02-14 09:34:54 +0100 | [diff] [blame] | 187 | (void)r; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 188 | assert((!r && !cb_data.sm_shared) || ((r == LY_EEXIST) && cb_data.sm_shared)); |
| 189 | |
| 190 | return cb_data.sm_shared; |
| 191 | } |
| 192 | |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 193 | /** |
| 194 | * @brief Schema mount compile. |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 195 | * Checks if it can be a valid extension instance for yang schema mount. |
| 196 | * |
| 197 | * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile. |
| 198 | */ |
| 199 | static LY_ERR |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 200 | schema_mount_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *UNUSED(extp), struct lysc_ext_instance *ext) |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 201 | { |
Michal Vasko | db64179 | 2022-03-21 10:04:54 +0100 | [diff] [blame] | 202 | const struct lysc_node *node; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 203 | struct lyplg_ext_sm *sm_data; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 204 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 205 | /* init internal data */ |
| 206 | sm_data = calloc(1, sizeof *sm_data); |
| 207 | if (!sm_data) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 208 | EXT_LOGERR_MEM_RET(cctx, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 209 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 210 | ext->compiled = sm_data; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 211 | |
Michal Vasko | db64179 | 2022-03-21 10:04:54 +0100 | [diff] [blame] | 212 | /* find the owner module */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 213 | node = ext->parent; |
Michal Vasko | db64179 | 2022-03-21 10:04:54 +0100 | [diff] [blame] | 214 | while (node->parent) { |
| 215 | node = node->parent; |
| 216 | } |
| 217 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 218 | /* reuse/init shared schema */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 219 | sm_data->shared = schema_mount_compile_find_shared(node->module, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 220 | if (sm_data->shared) { |
| 221 | ++sm_data->shared->ref_count; |
| 222 | } else { |
| 223 | sm_data->shared = calloc(1, sizeof *sm_data->shared); |
| 224 | if (!sm_data->shared) { |
| 225 | free(sm_data); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 226 | EXT_LOGERR_MEM_RET(cctx, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 227 | } |
| 228 | pthread_mutex_init(&sm_data->shared->lock, NULL); |
| 229 | sm_data->shared->ref_count = 1; |
| 230 | } |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 231 | |
| 232 | return LY_SUCCESS; |
| 233 | } |
| 234 | |
| 235 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 236 | * @brief Learn details about the current mount point. |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 237 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 238 | * @param[in] ext Compiled extension instance. |
| 239 | * @param[in] ext_data Extension data retrieved by the callback. |
| 240 | * @param[out] config Whether the whole schema should keep its config or be set to false. |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 241 | * @param[out] shared Optional flag whether the schema is shared or inline. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 242 | * @return LY_ERR value. |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 243 | */ |
| 244 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 245 | schema_mount_get_smount(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool *config, |
| 246 | ly_bool *shared) |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 247 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 248 | struct lyd_node *mpoint, *node; |
| 249 | char *path = NULL; |
| 250 | LY_ERR r; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 251 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 252 | /* find the mount point */ |
| 253 | if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']", ext->module->name, |
| 254 | ext->argument) == -1) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 255 | EXT_LOGERR_MEM_RET(NULL, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 256 | } |
| 257 | r = ext_data ? lyd_find_path(ext_data, path, 0, &mpoint) : LY_ENOTFOUND; |
| 258 | free(path); |
| 259 | if (r) { |
| 260 | /* missing mount-point, cannot be data for this extension (https://datatracker.ietf.org/doc/html/rfc8528#page-10) */ |
| 261 | return LY_ENOT; |
| 262 | } |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 263 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 264 | /* check config */ |
Michal Vasko | 9e12ffe | 2022-09-21 15:11:30 +0200 | [diff] [blame] | 265 | *config = 1; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 266 | if (!lyd_find_path(mpoint, "config", 0, &node) && !strcmp(lyd_get_value(node), "false")) { |
| 267 | *config = 0; |
Michal Vasko | 9e12ffe | 2022-09-21 15:11:30 +0200 | [diff] [blame] | 268 | } |
| 269 | assert((ext->parent_stmt == LY_STMT_CONTAINER) || (ext->parent_stmt == LY_STMT_LIST)); |
| 270 | if (((struct lysc_node *)ext->parent)->flags & LYS_CONFIG_R) { |
| 271 | *config = 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 274 | if (shared) { |
| 275 | /* check schema-ref */ |
| 276 | if (lyd_find_path(mpoint, "shared-schema", 0, NULL)) { |
| 277 | if (lyd_find_path(mpoint, "inline", 0, NULL)) { |
| 278 | EXT_LOGERR_INT_RET(NULL, ext); |
| 279 | } |
| 280 | *shared = 0; |
| 281 | } else { |
| 282 | *shared = 1; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 283 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return LY_SUCCESS; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @brief Create schema (context) based on retrieved extension data. |
| 291 | * |
| 292 | * @param[in] ext Compiled extension instance. |
| 293 | * @param[in] ext_data Extension data retrieved by the callback. |
| 294 | * @param[in] config Whether the whole schema should keep its config or be set to false. |
| 295 | * @param[out] ext_ctx Schema to use for parsing the data. |
| 296 | * @return LY_ERR value. |
| 297 | */ |
| 298 | static LY_ERR |
| 299 | schema_mount_create_ctx(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, |
| 300 | struct ly_ctx **ext_ctx) |
| 301 | { |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 302 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 303 | const char * const *searchdirs; |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 304 | char *sdirs = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 305 | const struct lys_module *mod; |
| 306 | struct lysc_node *root, *node; |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 307 | uint32_t i, idx = 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 308 | |
| 309 | /* get searchdirs from the current context */ |
| 310 | searchdirs = ly_ctx_get_searchdirs(ext->module->ctx); |
| 311 | |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 312 | if (searchdirs) { |
| 313 | /* append them all into a single string */ |
| 314 | for (i = 0; searchdirs[i]; ++i) { |
Jan Kundrát | 1fbc90d | 2022-06-07 21:27:53 +0200 | [diff] [blame] | 315 | if ((rc = ly_strcat(&sdirs, "%s" PATH_SEPARATOR, searchdirs[i]))) { |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 316 | goto cleanup; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 321 | /* create the context based on the data */ |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 322 | if ((rc = ly_ctx_new_yldata(sdirs, ext_data, ly_ctx_get_options(ext->module->ctx), ext_ctx))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 323 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Failed to create context for the schema-mount data."); |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 324 | goto cleanup; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (!config) { |
| 328 | /* manually change the config of all schema nodes in all the modules */ |
| 329 | while ((mod = ly_ctx_get_module_iter(*ext_ctx, &idx))) { |
| 330 | if (!mod->implemented) { |
| 331 | continue; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 332 | } |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 333 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 334 | LY_LIST_FOR(mod->compiled->data, root) { |
| 335 | LYSC_TREE_DFS_BEGIN(root, node) { |
| 336 | node->flags &= ~LYS_CONFIG_W; |
| 337 | node->flags |= LYS_CONFIG_R; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 338 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 339 | LYSC_TREE_DFS_END(root, node); |
| 340 | } |
| 341 | } |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Michal Vasko | 0fca45c | 2022-06-07 10:57:32 +0200 | [diff] [blame] | 345 | cleanup: |
| 346 | free(sdirs); |
| 347 | return rc; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
| 351 | * @brief Get schema (context) for a shared-schema mount point. |
| 352 | * |
| 353 | * @param[in] ext Compiled extension instance. |
| 354 | * @param[in] ext_data Extension data retrieved by the callback. |
| 355 | * @param[in] config Whether the whole schema should keep its config or be set to false. |
| 356 | * @param[out] ext_ctx Schema to use for parsing the data. |
| 357 | * @return LY_ERR value. |
| 358 | */ |
| 359 | static LY_ERR |
| 360 | schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, |
| 361 | const struct ly_ctx **ext_ctx) |
| 362 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 363 | struct lyplg_ext_sm *sm_data = ext->compiled; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 364 | LY_ERR ret = LY_SUCCESS, r; |
| 365 | struct lyd_node *node = NULL; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 366 | struct ly_ctx *new_ctx = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 367 | uint32_t i; |
| 368 | const char *content_id = NULL; |
| 369 | void *mem; |
| 370 | |
| 371 | assert(sm_data && sm_data->shared); |
| 372 | |
| 373 | /* get yang-library content-id or module-set-id */ |
| 374 | if (ext_data) { |
| 375 | lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, &node); |
| 376 | if (!node) { |
| 377 | lyd_find_path(ext_data, "/ietf-yang-library:modules-state/module-set-id", 0, &node); |
| 378 | } |
| 379 | if (node) { |
| 380 | content_id = lyd_get_value(node); |
| 381 | } |
| 382 | } |
| 383 | if (!content_id) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 384 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID, |
| 385 | "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data."); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 386 | return LY_EVALID; |
| 387 | } |
| 388 | |
| 389 | /* LOCK */ |
| 390 | if ((r = pthread_mutex_lock(&sm_data->shared->lock))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 391 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 392 | return LY_ESYS; |
| 393 | } |
| 394 | |
| 395 | /* try to find this mount point */ |
| 396 | for (i = 0; i < sm_data->shared->schema_count; ++i) { |
| 397 | if (ext->argument == sm_data->shared->schemas[i].mount_point) { |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (i < sm_data->shared->schema_count) { |
| 403 | /* schema exists already */ |
| 404 | if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 405 | lyplg_ext_compile_log_path("/ietf-yang-library:yang-library/content-id", ext, LY_LLERR, LY_EVALID, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 406 | "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.", |
| 407 | content_id, sm_data->shared->schemas[i].content_id); |
| 408 | ret = LY_EVALID; |
| 409 | goto cleanup; |
| 410 | } |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 411 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 412 | /* no schema found, create it */ |
| 413 | if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) { |
| 414 | ret = r; |
| 415 | goto cleanup; |
| 416 | } |
| 417 | |
| 418 | /* new entry */ |
| 419 | mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas); |
| 420 | if (!mem) { |
| 421 | ly_ctx_destroy(new_ctx); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 422 | EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 423 | } |
| 424 | sm_data->shared->schemas = mem; |
| 425 | ++sm_data->shared->schema_count; |
| 426 | |
| 427 | /* fill entry */ |
| 428 | sm_data->shared->schemas[i].ctx = new_ctx; |
| 429 | sm_data->shared->schemas[i].mount_point = ext->argument; |
| 430 | lydict_insert(ext->module->ctx, content_id, 0, &sm_data->shared->schemas[i].content_id); |
| 431 | } |
| 432 | |
| 433 | /* use the context */ |
| 434 | *ext_ctx = sm_data->shared->schemas[i].ctx; |
| 435 | |
| 436 | cleanup: |
| 437 | /* UNLOCK */ |
| 438 | pthread_mutex_unlock(&sm_data->shared->lock); |
| 439 | |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * @brief Get schema (context) for an inline mount point. |
| 445 | * |
| 446 | * @param[in] ext Compiled extension instance. |
| 447 | * @param[in] ext_data Extension data retrieved by the callback. |
| 448 | * @param[in] config Whether the whole schema should keep its config or be set to false. |
| 449 | * @param[out] ext_ctx Schema to use for parsing the data. |
| 450 | * @return LY_ERR value. |
| 451 | */ |
| 452 | static LY_ERR |
| 453 | schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, |
| 454 | const struct ly_ctx **ext_ctx) |
| 455 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 456 | struct lyplg_ext_sm *sm_data = ext->compiled; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 457 | LY_ERR r; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 458 | struct ly_ctx *new_ctx = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 459 | uint32_t i; |
| 460 | void *mem; |
| 461 | |
| 462 | assert(sm_data && sm_data->shared); |
| 463 | |
| 464 | i = sm_data->inln.schema_count; |
| 465 | |
| 466 | /* always new schema required, create context */ |
| 467 | if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) { |
| 468 | return r; |
| 469 | } |
| 470 | |
| 471 | /* new entry */ |
| 472 | mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas); |
| 473 | if (!mem) { |
| 474 | ly_ctx_destroy(new_ctx); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 475 | EXT_LOGERR_MEM_RET(NULL, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 476 | } |
| 477 | sm_data->inln.schemas = mem; |
| 478 | ++sm_data->inln.schema_count; |
| 479 | |
| 480 | /* fill entry */ |
| 481 | sm_data->inln.schemas[i].ctx = new_ctx; |
| 482 | |
| 483 | /* use the context */ |
| 484 | *ext_ctx = sm_data->inln.schemas[i].ctx; |
| 485 | return LY_SUCCESS; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * @brief Get schema (context) for a mount point. |
| 490 | * |
| 491 | * @param[in] ext Compiled extension instance. |
| 492 | * @param[out] ext_ctx Schema to use for parsing the data. |
| 493 | * @return LY_ERR value. |
| 494 | */ |
| 495 | static LY_ERR |
| 496 | schema_mount_get_ctx(struct lysc_ext_instance *ext, const struct ly_ctx **ext_ctx) |
| 497 | { |
| 498 | LY_ERR ret = LY_SUCCESS, r; |
| 499 | struct lyd_node *iter, *ext_data = NULL; |
| 500 | ly_bool ext_data_free = 0, config, shared; |
| 501 | |
| 502 | *ext_ctx = NULL; |
| 503 | |
| 504 | /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ |
| 505 | if ((r = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 506 | ret = r; |
| 507 | goto cleanup; |
| 508 | } |
| 509 | |
| 510 | LY_LIST_FOR(ext_data, iter) { |
| 511 | if (iter->flags & LYD_NEW) { |
| 512 | /* must be validated for the parent-reference prefix data to be stored */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 513 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated."); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 514 | ret = LY_EINVAL; |
| 515 | goto cleanup; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /* learn about this mount point */ |
| 520 | if ((r = schema_mount_get_smount(ext, ext_data, &config, &shared))) { |
| 521 | ret = r; |
| 522 | goto cleanup; |
| 523 | } |
| 524 | |
| 525 | /* create/get the context for parsing the data */ |
| 526 | if (shared) { |
| 527 | r = schema_mount_get_ctx_shared(ext, ext_data, config, ext_ctx); |
| 528 | } else { |
| 529 | r = schema_mount_get_ctx_inline(ext, ext_data, config, ext_ctx); |
| 530 | } |
| 531 | if (r) { |
| 532 | ret = r; |
| 533 | goto cleanup; |
| 534 | } |
| 535 | |
| 536 | cleanup: |
| 537 | if (ext_data_free) { |
| 538 | lyd_free_all(ext_data); |
| 539 | } |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /** |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 544 | * @brief Snode callback for schema mount. |
| 545 | * Check if data are valid for schema mount and returns their schema node. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 546 | */ |
| 547 | static LY_ERR |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 548 | schema_mount_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent, |
| 549 | const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len, |
| 550 | const struct lysc_node **snode) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 551 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 552 | LY_ERR r; |
| 553 | const struct lys_module *mod; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 554 | const struct ly_ctx *ext_ctx = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 555 | |
| 556 | /* get context based on ietf-yang-library data */ |
| 557 | if ((r = schema_mount_get_ctx(ext, &ext_ctx))) { |
| 558 | return r; |
| 559 | } |
| 560 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 561 | /* get the module */ |
| 562 | mod = lyplg_type_identity_module(ext_ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data); |
| 563 | if (!mod) { |
| 564 | return LY_ENOT; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 567 | /* get the top-level schema node */ |
| 568 | *snode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 569 | return *snode ? LY_SUCCESS : LY_ENOT; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 570 | } |
| 571 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 572 | static LY_ERR |
| 573 | schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, |
| 574 | struct ly_set **set) |
| 575 | { |
| 576 | LY_ERR ret = LY_SUCCESS; |
| 577 | char *path = NULL; |
| 578 | |
| 579 | /* get all parent references of this mount point */ |
| 580 | if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']" |
| 581 | "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 582 | EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 583 | } |
| 584 | if ((ret = lyd_find_xpath(ext_data, path, set))) { |
| 585 | goto cleanup; |
| 586 | } |
| 587 | |
| 588 | cleanup: |
| 589 | free(path); |
| 590 | return ret; |
| 591 | } |
| 592 | |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 593 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 594 | * @brief Duplicate all accessible parent references for a shared-schema mount point. |
| 595 | * |
| 596 | * @param[in] ext Compiled extension instance. |
| 597 | * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions. |
| 598 | * @param[in] ext_data Extension data retrieved by the callback. |
| 599 | * @param[in] trg_ctx Mounted data context to use for duplication. |
| 600 | * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty. |
| 601 | * @return LY_ERR value. |
| 602 | */ |
| 603 | static LY_ERR |
| 604 | schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node, |
| 605 | const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set) |
| 606 | { |
| 607 | LY_ERR ret = LY_SUCCESS; |
| 608 | char *path = NULL; |
| 609 | struct ly_set *set = NULL, *par_set = NULL; |
| 610 | struct lyd_node_term *term; |
| 611 | struct lyd_node *dup = NULL, *top_node, *first; |
| 612 | struct lyd_value_xpath10 *xp_val; |
| 613 | uint32_t i, j; |
| 614 | |
| 615 | *ref_set = NULL; |
| 616 | |
| 617 | if (!ext_data) { |
| 618 | /* we expect the same ext data as before and there must be some for data to be parsed */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 619 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "No ext data provided."); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 620 | ret = LY_EINVAL; |
| 621 | goto cleanup; |
| 622 | } |
| 623 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 624 | if ((ret = schema_mount_get_parent_ref(ext, ext_data, &set))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 625 | goto cleanup; |
| 626 | } |
| 627 | |
| 628 | /* prepare result set */ |
| 629 | if ((ret = ly_set_new(ref_set))) { |
| 630 | goto cleanup; |
| 631 | } |
| 632 | |
| 633 | first = NULL; |
| 634 | for (i = 0; i < set->count; ++i) { |
| 635 | term = set->objs[i]; |
| 636 | |
| 637 | /* get the referenced nodes (subtrees) */ |
| 638 | LYD_VALUE_GET(&term->value, xp_val); |
| 639 | if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data, |
| 640 | NULL, &par_set))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 641 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Parent reference \"%s\" evaluation failed.", |
| 642 | lyxp_get_expr(xp_val->exp)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 643 | goto cleanup; |
| 644 | } |
| 645 | |
| 646 | for (j = 0; j < par_set->count; ++j) { |
| 647 | /* duplicate with parents in the context of the mounted data */ |
| 648 | if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL, |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 649 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_EXT, &dup))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 650 | goto cleanup; |
| 651 | } |
| 652 | |
| 653 | /* go top-level */ |
| 654 | while (dup->parent) { |
| 655 | dup = lyd_parent(dup); |
| 656 | } |
| 657 | |
| 658 | /* check whether the top-level node exists */ |
| 659 | if (first) { |
| 660 | if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) { |
| 661 | goto cleanup; |
| 662 | } |
| 663 | } else { |
| 664 | top_node = NULL; |
| 665 | } |
| 666 | |
| 667 | if (top_node) { |
| 668 | /* merge */ |
| 669 | ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT); |
| 670 | dup = NULL; |
| 671 | if (ret) { |
| 672 | goto cleanup; |
| 673 | } |
| 674 | } else { |
| 675 | /* insert */ |
| 676 | if ((ret = lyd_insert_sibling(first, dup, &first))) { |
| 677 | goto cleanup; |
| 678 | } |
| 679 | |
| 680 | /* add into the result set because a new top-level node was added */ |
| 681 | if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) { |
| 682 | goto cleanup; |
| 683 | } |
| 684 | dup = NULL; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | cleanup: |
| 690 | free(path); |
| 691 | ly_set_free(set, NULL); |
| 692 | ly_set_free(par_set, NULL); |
| 693 | lyd_free_tree(dup); |
| 694 | if (ret && *ref_set) { |
| 695 | if ((*ref_set)->count) { |
| 696 | lyd_free_siblings((*ref_set)->dnodes[0]); |
| 697 | } |
| 698 | ly_set_free(*ref_set, NULL); |
| 699 | *ref_set = NULL; |
| 700 | } |
| 701 | return ret; |
| 702 | } |
| 703 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 704 | LY_ERR |
| 705 | lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs) |
| 706 | { |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 707 | LY_ERR rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 708 | struct ly_set *pref_set = NULL; |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 709 | struct ly_set *snode_set = NULL; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 710 | struct ly_set *results_set = NULL; |
| 711 | struct lyd_node *ext_data; |
| 712 | ly_bool ext_data_free; |
| 713 | |
| 714 | /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 715 | if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 716 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 717 | } |
| 718 | |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 719 | LY_CHECK_GOTO(rc = schema_mount_get_parent_ref(ext, ext_data, &pref_set), cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 720 | if (pref_set->count == 0) { |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 721 | goto cleanup; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 722 | } |
| 723 | |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 724 | LY_CHECK_GOTO(rc = ly_set_new(&results_set), cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 725 | |
| 726 | for (uint32_t i = 0; i < pref_set->count; ++i) { |
| 727 | struct lyd_node_term *term; |
| 728 | struct lyd_value_xpath10 *xp_val; |
| 729 | char *value; |
| 730 | struct ly_err_item *err; |
| 731 | |
| 732 | term = (struct lyd_node_term *)pref_set->dnodes[i]; |
| 733 | LYD_VALUE_GET(&term->value, xp_val); |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 734 | LY_CHECK_GOTO(rc = lyplg_type_print_xpath10_value(xp_val, LY_VALUE_JSON, NULL, &value, &err), cleanup); |
| 735 | LY_CHECK_ERR_GOTO(rc = lys_find_xpath(ext->module->ctx, NULL, value, 0, &snode_set), free(value), cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 736 | free(value); |
| 737 | for (uint32_t sn = 0; sn < snode_set->count; sn++) { |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 738 | LY_CHECK_GOTO(rc = ly_set_add(results_set, snode_set->snodes[sn], 0, NULL), cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 739 | } |
| 740 | ly_set_free(snode_set, NULL); |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 741 | snode_set = NULL; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | *refs = results_set; |
| 745 | |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 746 | cleanup: |
| 747 | if (rc) { |
| 748 | ly_set_free(results_set, NULL); |
| 749 | } |
| 750 | ly_set_free(snode_set, NULL); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 751 | if (ext_data_free) { |
| 752 | lyd_free_all(ext_data); |
| 753 | } |
| 754 | ly_set_free(pref_set, NULL); |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 755 | |
| 756 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 757 | } |
| 758 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 759 | /** |
| 760 | * @brief Validate callback for schema mount. |
| 761 | */ |
| 762 | static LY_ERR |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 763 | schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree, |
| 764 | enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 765 | { |
| 766 | LY_ERR ret = LY_SUCCESS; |
| 767 | uint32_t old_log_opts, i; |
| 768 | struct ly_err_item *err; |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 769 | struct lyd_node *iter, *ext_data = NULL, *ref_first = NULL, *orig_parent = lyd_parent(sibling), *op_tree; |
Michal Vasko | f4c6f00 | 2022-04-01 09:12:22 +0200 | [diff] [blame] | 770 | struct lyd_node *ext_diff = NULL, *diff_parent = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 771 | ly_bool ext_data_free = 0; |
| 772 | struct ly_set *ref_set = NULL; |
| 773 | |
| 774 | if (!sibling) { |
| 775 | /* some data had to be parsed for this callback to be called */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 776 | EXT_LOGERR_INT_RET(NULL, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ |
| 780 | if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 781 | goto cleanup; |
| 782 | } |
| 783 | |
| 784 | LY_LIST_FOR(ext_data, iter) { |
| 785 | if (iter->flags & LYD_NEW) { |
| 786 | /* must be validated for the parent-reference prefix data to be stored */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 787 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated."); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 788 | ret = LY_EINVAL; |
| 789 | goto cleanup; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /* duplicate the referenced parent nodes into ext context */ |
| 794 | if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) { |
| 795 | goto cleanup; |
| 796 | } |
| 797 | |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 798 | if (data_type != LYD_TYPE_DATA_YANG) { |
| 799 | /* remember the operation data tree, it may be moved */ |
| 800 | op_tree = sibling; |
| 801 | } |
| 802 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 803 | /* create accessible tree, remove LYD_EXT to not call this callback recursively */ |
| 804 | lyd_unlink_siblings(sibling); |
| 805 | LY_LIST_FOR(sibling, iter) { |
| 806 | iter->flags &= ~LYD_EXT; |
| 807 | } |
| 808 | if (ref_set->count) { |
| 809 | if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) { |
| 810 | goto cleanup; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* only store messages in the context, log as an extension */ |
| 815 | old_log_opts = ly_log_options(LY_LOSTORE_LAST); |
| 816 | |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 817 | if (data_type == LYD_TYPE_DATA_YANG) { |
| 818 | /* validate all the data */ |
| 819 | ret = lyd_validate_all(&sibling, NULL, val_opts, diff ? &ext_diff : NULL); |
| 820 | } else { |
| 821 | /* validate the operation */ |
| 822 | ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL); |
| 823 | } |
| 824 | |
| 825 | /* restore logging */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 826 | ly_log_options(old_log_opts); |
| 827 | |
| 828 | /* restore sibling tree */ |
| 829 | for (i = 0; i < ref_set->count; ++i) { |
| 830 | if (ref_set->dnodes[i] == sibling) { |
| 831 | sibling = sibling->next; |
| 832 | } |
| 833 | lyd_free_tree(ref_set->dnodes[i]); |
| 834 | } |
| 835 | LY_LIST_FOR(sibling, iter) { |
| 836 | iter->flags |= LYD_EXT; |
| 837 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 838 | lyplg_ext_insert(orig_parent, sibling); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 839 | |
| 840 | if (ret) { |
| 841 | /* log the error in the original context */ |
| 842 | err = ly_err_first(LYD_CTX(sibling)); |
| 843 | if (!err) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 844 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Unknown validation error (err code %d).", ret); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 845 | } else { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 846 | lyplg_ext_compile_log_path(err->path, ext, LY_LLERR, err->no, "%s", err->msg); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 847 | } |
| 848 | goto cleanup; |
| 849 | } |
| 850 | |
Michal Vasko | f4c6f00 | 2022-04-01 09:12:22 +0200 | [diff] [blame] | 851 | /* create proper diff */ |
| 852 | if (diff && ext_diff) { |
| 853 | /* diff nodes from an extension instance */ |
| 854 | LY_LIST_FOR(ext_diff, iter) { |
| 855 | iter->flags |= LYD_EXT; |
| 856 | } |
| 857 | |
| 858 | /* create the parent and insert the diff */ |
| 859 | if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) { |
| 860 | goto cleanup; |
| 861 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 862 | if ((ret = lyplg_ext_insert(diff_parent, ext_diff))) { |
Michal Vasko | f4c6f00 | 2022-04-01 09:12:22 +0200 | [diff] [blame] | 863 | goto cleanup; |
| 864 | } |
| 865 | ext_diff = NULL; |
| 866 | |
| 867 | /* go top-level and set the operation */ |
| 868 | while (lyd_parent(diff_parent)) { |
| 869 | diff_parent = lyd_parent(diff_parent); |
| 870 | } |
| 871 | if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) { |
| 872 | goto cleanup; |
| 873 | } |
| 874 | |
| 875 | /* finally merge into the global diff */ |
| 876 | if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) { |
| 877 | goto cleanup; |
| 878 | } |
| 879 | } |
| 880 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 881 | cleanup: |
| 882 | ly_set_free(ref_set, NULL); |
| 883 | lyd_free_siblings(ref_first); |
Michal Vasko | f4c6f00 | 2022-04-01 09:12:22 +0200 | [diff] [blame] | 884 | lyd_free_tree(ext_diff); |
| 885 | lyd_free_all(diff_parent); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 886 | if (ext_data_free) { |
| 887 | lyd_free_all(ext_data); |
| 888 | } |
| 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 893 | * @brief Schema mount compile free. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 894 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 895 | * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 896 | */ |
| 897 | static void |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 898 | schema_mount_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 899 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 900 | struct lyplg_ext_sm *sm_data = ext->compiled; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 901 | uint32_t i; |
| 902 | |
| 903 | if (!sm_data) { |
| 904 | return; |
| 905 | } |
| 906 | |
| 907 | if (!--sm_data->shared->ref_count) { |
| 908 | for (i = 0; i < sm_data->shared->schema_count; ++i) { |
| 909 | ly_ctx_destroy(sm_data->shared->schemas[i].ctx); |
| 910 | lydict_remove(ctx, sm_data->shared->schemas[i].content_id); |
| 911 | } |
| 912 | free(sm_data->shared->schemas); |
| 913 | pthread_mutex_destroy(&sm_data->shared->lock); |
| 914 | free(sm_data->shared); |
| 915 | } |
| 916 | |
| 917 | for (i = 0; i < sm_data->inln.schema_count; ++i) { |
| 918 | ly_ctx_destroy(sm_data->inln.schemas[i].ctx); |
| 919 | } |
| 920 | free(sm_data->inln.schemas); |
| 921 | free(sm_data); |
| 922 | } |
| 923 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 924 | LIBYANG_API_DEF LY_ERR |
| 925 | lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx) |
| 926 | { |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 927 | struct lyd_node *ext_data = NULL; |
| 928 | ly_bool ext_data_free = 0, config; |
| 929 | LY_ERR rc = LY_SUCCESS; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 930 | |
| 931 | if (!ext->module->ctx->ext_clb) { |
| 932 | return LY_EINVAL; |
| 933 | } |
| 934 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 935 | if (strcmp(ext->def->module->name, "ietf-yang-schema-mount") || strcmp(ext->def->name, "mount-point")) { |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 936 | return LY_EINVAL; |
| 937 | } |
| 938 | |
| 939 | /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 940 | if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 941 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* learn about this mount point */ |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 945 | if ((rc = schema_mount_get_smount(ext, ext_data, &config, NULL))) { |
| 946 | goto cleanup; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 947 | } |
| 948 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 949 | /* create the context */ |
| 950 | rc = schema_mount_create_ctx(ext, ext_data, config, ctx); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 951 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 952 | cleanup: |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 953 | if (ext_data_free) { |
| 954 | lyd_free_all(ext_data); |
| 955 | } |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 956 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 957 | } |
| 958 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 959 | static void |
| 960 | schema_mount_spriter_tree_free(void *priv) |
| 961 | { |
| 962 | struct sprinter_tree_priv *st_priv; |
| 963 | |
| 964 | st_priv = priv; |
| 965 | ly_set_free(st_priv->refs, NULL); |
| 966 | ly_ctx_destroy(st_priv->ext_ctx); |
| 967 | free(st_priv); |
| 968 | } |
| 969 | |
| 970 | static LY_ERR |
| 971 | schema_mount_sprinter_tree_cnode_override_mounted(const struct lysc_node *node, const void *UNUSED(plugin_priv), |
| 972 | ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts) |
| 973 | { |
| 974 | if (!node->parent) { |
| 975 | *add_opts = "/"; |
| 976 | } |
| 977 | |
| 978 | return LY_SUCCESS; |
| 979 | } |
| 980 | |
| 981 | static LY_ERR |
| 982 | schema_mount_sprinter_tree_pnode_override_mounted(const struct lysp_node *node, const void *UNUSED(plugin_priv), |
| 983 | ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts) |
| 984 | { |
| 985 | if (!node->parent) { |
| 986 | *add_opts = "/"; |
| 987 | } |
| 988 | |
| 989 | return LY_SUCCESS; |
| 990 | } |
| 991 | |
| 992 | static LY_ERR |
| 993 | schema_mount_sprinter_tree_node_override_parent_refs(const struct lysc_node *node, const void *plugin_priv, |
| 994 | ly_bool *skip, const char **UNUSED(flags), const char **add_opts) |
| 995 | { |
| 996 | uint32_t i; |
| 997 | const struct ly_set *refs; |
| 998 | const struct lysc_module *mod; |
| 999 | struct lysc_node *ref, *iter; |
| 1000 | |
| 1001 | refs = ((struct sprinter_tree_priv *)plugin_priv)->refs; |
| 1002 | mod = node->module->compiled; |
| 1003 | |
| 1004 | /* Assume the @p node will be skipped. */ |
| 1005 | *skip = 1; |
| 1006 | for (i = 0; (i < refs->count) && *skip; i++) { |
| 1007 | ref = refs->snodes[i]; |
| 1008 | if (ref->module->compiled != mod) { |
| 1009 | /* parent-reference points to different module */ |
| 1010 | continue; |
| 1011 | } |
| 1012 | |
| 1013 | for (iter = ref; iter; iter = iter->parent) { |
| 1014 | if (iter == node) { |
| 1015 | /* @p node is not skipped because it is parent-rererence node or his parent */ |
| 1016 | *skip = 0; |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | if (!*skip && !node->parent) { |
| 1023 | /* top-node has additional opts */ |
| 1024 | *add_opts = "@"; |
| 1025 | } |
| 1026 | |
| 1027 | return LY_SUCCESS; |
| 1028 | } |
| 1029 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1030 | /** |
| 1031 | * @brief Schema mount schema parsed tree printer. |
| 1032 | * |
| 1033 | * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. |
| 1034 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1035 | static LY_ERR |
| 1036 | schema_mount_sprinter_ptree(struct lysp_ext_instance *UNUSED(ext), const struct lyspr_tree_ctx *ctx, |
| 1037 | const char **flags, const char **UNUSED(add_opts)) |
| 1038 | { |
| 1039 | if (!ctx) { |
| 1040 | *flags = "mp"; |
| 1041 | } |
| 1042 | |
| 1043 | return LY_SUCCESS; |
| 1044 | } |
| 1045 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1046 | /** |
| 1047 | * @brief Schema mount schema compiled tree printer. |
| 1048 | * |
| 1049 | * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. |
| 1050 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1051 | static LY_ERR |
| 1052 | schema_mount_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 1053 | const char **flags, const char **UNUSED(add_opts)) |
| 1054 | { |
| 1055 | LY_ERR rc = LY_SUCCESS; |
| 1056 | struct ly_ctx *ext_ctx = NULL; |
| 1057 | const struct lys_module *mod; |
| 1058 | struct ly_set *refs = NULL; |
| 1059 | struct lysc_node *tree1, *tree2; |
| 1060 | uint32_t i, j; |
| 1061 | ly_bool from_parent_ref, is_first; |
| 1062 | struct sprinter_tree_priv *st_priv; |
| 1063 | |
| 1064 | if (!ctx) { |
| 1065 | *flags = "mp"; |
| 1066 | return LY_SUCCESS; |
| 1067 | } |
| 1068 | |
| 1069 | if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) { |
| 1070 | /* Void mount point */ |
| 1071 | return LY_SUCCESS; |
| 1072 | } |
| 1073 | |
| 1074 | rc = lyplg_ext_schema_mount_get_parent_ref(ext, &refs); |
| 1075 | LY_CHECK_GOTO(rc, cleanup); |
| 1076 | |
| 1077 | /* build new list of modules to print. This list will omit internal |
| 1078 | * modules, modules with no nodes (e.g., iana-if-types) and modules |
| 1079 | * that were loaded as the result of a parent-reference. |
| 1080 | */ |
| 1081 | i = ly_ctx_internal_modules_count(ext_ctx); |
| 1082 | while ((mod = ly_ctx_get_module_iter(ext_ctx, &i))) { |
| 1083 | from_parent_ref = 0; |
| 1084 | |
| 1085 | for (j = 0; refs && j < refs->count; j++) { |
| 1086 | if (!strcmp(mod->ns, refs->snodes[j]->module->ns)) { |
| 1087 | from_parent_ref = 1; |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | if (from_parent_ref) { |
| 1092 | /* Modules loaded as the result of a parent-reference are added later. */ |
| 1093 | continue; |
| 1094 | } |
| 1095 | |
| 1096 | /* Add data nodes, rpcs and notifications. */ |
| 1097 | if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) { |
| 1098 | /* For compiled module. */ |
| 1099 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, mod->compiled->data, |
| 1100 | schema_mount_sprinter_tree_cnode_override_mounted); |
| 1101 | LY_CHECK_GOTO(rc, cleanup); |
| 1102 | if (mod->compiled->rpcs) { |
| 1103 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->rpcs->node, |
| 1104 | schema_mount_sprinter_tree_cnode_override_mounted); |
| 1105 | } |
| 1106 | LY_CHECK_GOTO(rc, cleanup); |
| 1107 | if (mod->compiled->notifs) { |
| 1108 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->notifs->node, |
| 1109 | schema_mount_sprinter_tree_cnode_override_mounted); |
| 1110 | } |
| 1111 | LY_CHECK_GOTO(rc, cleanup); |
| 1112 | } else { |
| 1113 | /* For parsed module. */ |
| 1114 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, mod->parsed->data, |
| 1115 | schema_mount_sprinter_tree_pnode_override_mounted); |
| 1116 | LY_CHECK_GOTO(rc, cleanup); |
| 1117 | if (mod->parsed->rpcs) { |
| 1118 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->rpcs->node, |
| 1119 | schema_mount_sprinter_tree_pnode_override_mounted); |
| 1120 | } |
| 1121 | LY_CHECK_GOTO(rc, cleanup); |
| 1122 | if (mod->parsed->notifs) { |
| 1123 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->notifs->node, |
| 1124 | schema_mount_sprinter_tree_pnode_override_mounted); |
| 1125 | } |
| 1126 | LY_CHECK_GOTO(rc, cleanup); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* Add modules loaded as the result of a parent-reference. */ |
| 1131 | for (i = 0; refs && (i < refs->count); i++) { |
| 1132 | tree1 = refs->snodes[i]->module->compiled->data; |
| 1133 | |
| 1134 | /* Add data nodes from the module only once. */ |
| 1135 | is_first = 1; |
| 1136 | for (j = 0; j < i; j++) { |
| 1137 | tree2 = refs->snodes[j]->module->compiled->data; |
| 1138 | if (tree1 == tree2) { |
| 1139 | is_first = 0; |
| 1140 | break; |
| 1141 | } |
| 1142 | } |
| 1143 | if (is_first) { |
| 1144 | /* Add all data nodes but unavailable nodes are skipped in the callback. */ |
| 1145 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, tree1, schema_mount_sprinter_tree_node_override_parent_refs); |
| 1146 | LY_CHECK_GOTO(rc, cleanup); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* Add private plugin data. */ |
| 1151 | st_priv = calloc(1, sizeof(*st_priv)); |
| 1152 | st_priv->ext_ctx = ext_ctx; |
| 1153 | st_priv->refs = refs; |
| 1154 | rc = lyplg_ext_sprinter_tree_set_priv(ctx, st_priv, schema_mount_spriter_tree_free); |
| 1155 | |
| 1156 | cleanup: |
| 1157 | if (rc) { |
| 1158 | ly_set_free(refs, NULL); |
| 1159 | ly_ctx_destroy(ext_ctx); |
| 1160 | } |
| 1161 | |
| 1162 | return rc; |
| 1163 | } |
| 1164 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1165 | /** |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 1166 | * @brief Plugin descriptions for the Yang Schema Mount extension. |
| 1167 | * |
| 1168 | * Note that external plugins are supposed to use: |
| 1169 | * |
| 1170 | * LYPLG_EXTENSIONS = { |
| 1171 | */ |
| 1172 | const struct lyplg_ext_record plugins_schema_mount[] = { |
| 1173 | { |
| 1174 | .module = "ietf-yang-schema-mount", |
| 1175 | .revision = "2019-01-14", |
| 1176 | .name = "mount-point", |
| 1177 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1178 | .plugin.id = "ly2 schema mount v1", |
| 1179 | .plugin.parse = schema_mount_parse, |
| 1180 | .plugin.compile = schema_mount_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 1181 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1182 | .plugin.printer_ctree = schema_mount_sprinter_ctree, |
| 1183 | .plugin.printer_ptree = schema_mount_sprinter_ptree, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1184 | .plugin.node = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1185 | .plugin.snode = schema_mount_snode, |
| 1186 | .plugin.validate = schema_mount_validate, |
| 1187 | .plugin.pfree = NULL, |
| 1188 | .plugin.cfree = schema_mount_cfree |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 1189 | }, |
| 1190 | {0} /* terminating zeroed item */ |
| 1191 | }; |