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 { |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 38 | pthread_mutex_t lock; /**< lock for accessing this shared structure */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 39 | |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 40 | struct lyplg_ext_sm_shared { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 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 { |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 54 | struct ly_ctx *ctx; /**< context created for inline schema data, may be reused if possible */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 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 | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 210 | pthread_mutex_init(&sm_data->lock, NULL); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 211 | ext->compiled = sm_data; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 212 | |
Michal Vasko | db64179 | 2022-03-21 10:04:54 +0100 | [diff] [blame] | 213 | /* find the owner module */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 214 | node = ext->parent; |
Michal Vasko | db64179 | 2022-03-21 10:04:54 +0100 | [diff] [blame] | 215 | while (node->parent) { |
| 216 | node = node->parent; |
| 217 | } |
| 218 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 219 | /* reuse/init shared schema */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 220 | sm_data->shared = schema_mount_compile_find_shared(node->module, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 221 | if (sm_data->shared) { |
| 222 | ++sm_data->shared->ref_count; |
| 223 | } else { |
| 224 | sm_data->shared = calloc(1, sizeof *sm_data->shared); |
| 225 | if (!sm_data->shared) { |
| 226 | free(sm_data); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 227 | EXT_LOGERR_MEM_RET(cctx, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 228 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 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 | /** |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 351 | * @brief Get ietf-yang-library context-id from its data. |
| 352 | * |
| 353 | * @param[in] ext Compiled extension instance for logging. |
| 354 | * @param[in] ext_data Extension data retrieved by the callback with the yang-library data. |
| 355 | * @param[out] content_id Content ID in @p ext_data. |
| 356 | * @return LY_ERR value. |
| 357 | */ |
| 358 | static LY_ERR |
| 359 | schema_mount_get_content_id(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, const char **content_id) |
| 360 | { |
| 361 | struct lyd_node *node = NULL; |
| 362 | |
| 363 | *content_id = NULL; |
| 364 | |
| 365 | /* get yang-library content-id or module-set-id */ |
| 366 | if (ext_data) { |
| 367 | lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, &node); |
| 368 | if (!node) { |
| 369 | lyd_find_path(ext_data, "/ietf-yang-library:modules-state/module-set-id", 0, &node); |
| 370 | } |
| 371 | if (node) { |
| 372 | *content_id = lyd_get_value(node); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (!*content_id) { |
| 377 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID, |
| 378 | "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data."); |
| 379 | return LY_EVALID; |
| 380 | } |
| 381 | return LY_SUCCESS; |
| 382 | } |
| 383 | |
| 384 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 385 | * @brief Get schema (context) for a shared-schema mount point. |
| 386 | * |
| 387 | * @param[in] ext Compiled extension instance. |
| 388 | * @param[in] ext_data Extension data retrieved by the callback. |
| 389 | * @param[in] config Whether the whole schema should keep its config or be set to false. |
| 390 | * @param[out] ext_ctx Schema to use for parsing the data. |
| 391 | * @return LY_ERR value. |
| 392 | */ |
| 393 | static LY_ERR |
| 394 | schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, |
| 395 | const struct ly_ctx **ext_ctx) |
| 396 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 397 | struct lyplg_ext_sm *sm_data = ext->compiled; |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 398 | LY_ERR rc = LY_SUCCESS, r; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 399 | struct ly_ctx *new_ctx = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 400 | uint32_t i; |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 401 | const char *content_id; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 402 | void *mem; |
| 403 | |
| 404 | assert(sm_data && sm_data->shared); |
| 405 | |
| 406 | /* get yang-library content-id or module-set-id */ |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 407 | if ((r = schema_mount_get_content_id(ext, ext_data, &content_id))) { |
| 408 | return r; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* LOCK */ |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 412 | if ((r = pthread_mutex_lock(&sm_data->lock))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 413 | 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] | 414 | return LY_ESYS; |
| 415 | } |
| 416 | |
| 417 | /* try to find this mount point */ |
| 418 | for (i = 0; i < sm_data->shared->schema_count; ++i) { |
| 419 | if (ext->argument == sm_data->shared->schemas[i].mount_point) { |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (i < sm_data->shared->schema_count) { |
| 425 | /* schema exists already */ |
| 426 | if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 427 | 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] | 428 | "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.", |
| 429 | content_id, sm_data->shared->schemas[i].content_id); |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 430 | rc = LY_EVALID; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 431 | goto cleanup; |
| 432 | } |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 433 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 434 | /* no schema found, create it */ |
| 435 | if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) { |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 436 | rc = r; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 437 | goto cleanup; |
| 438 | } |
| 439 | |
| 440 | /* new entry */ |
| 441 | mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas); |
| 442 | if (!mem) { |
| 443 | ly_ctx_destroy(new_ctx); |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 444 | EXT_LOGERR_MEM_GOTO(NULL, ext, rc, cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 445 | } |
| 446 | sm_data->shared->schemas = mem; |
| 447 | ++sm_data->shared->schema_count; |
| 448 | |
| 449 | /* fill entry */ |
| 450 | sm_data->shared->schemas[i].ctx = new_ctx; |
| 451 | sm_data->shared->schemas[i].mount_point = ext->argument; |
| 452 | lydict_insert(ext->module->ctx, content_id, 0, &sm_data->shared->schemas[i].content_id); |
| 453 | } |
| 454 | |
| 455 | /* use the context */ |
| 456 | *ext_ctx = sm_data->shared->schemas[i].ctx; |
| 457 | |
| 458 | cleanup: |
| 459 | /* UNLOCK */ |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 460 | pthread_mutex_unlock(&sm_data->lock); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 461 | |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 462 | return rc; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * @brief Check whether ietf-yang-library data describe an existing context meaning whether it includes |
| 467 | * at least exactly all the mentioned modules. |
| 468 | * |
| 469 | * @param[in] ext Compiled extension instance for logging. |
| 470 | * @param[in] ext_data Extension data retrieved by the callback with the yang-library data. |
| 471 | * @param[in] ctx Context to consider. |
| 472 | * @return LY_SUCCESS if the context matches. |
| 473 | * @return LY_ENOT if the context differs. |
| 474 | * @return LY_ERR on error. |
| 475 | */ |
| 476 | static LY_ERR |
| 477 | schema_mount_ctx_match(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, const struct ly_ctx *ctx) |
| 478 | { |
| 479 | struct ly_set *impl_mods = NULL, *imp_mods = NULL; |
| 480 | struct lyd_node *node; |
| 481 | const struct lys_module *mod; |
| 482 | const char *name, *revision; |
| 483 | LY_ERR rc = LY_ENOT, r; |
| 484 | uint32_t i; |
| 485 | |
| 486 | /* collect all the implemented and imported modules, we do not really care about content-id */ |
| 487 | if (!lyd_find_path(ext_data, "/ietf-yang-library:yang-library/content-id", 0, NULL)) { |
| 488 | if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:yang-library/module-set[1]/module", &impl_mods))) { |
| 489 | rc = r; |
| 490 | goto cleanup; |
| 491 | } |
| 492 | if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:yang-library/module-set[1]/import-only-module", &imp_mods))) { |
| 493 | rc = r; |
| 494 | goto cleanup; |
| 495 | } |
| 496 | } else { |
| 497 | if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:modules-state/module[conformance-type='implement']", &impl_mods))) { |
| 498 | rc = r; |
| 499 | goto cleanup; |
| 500 | } |
| 501 | if ((r = lyd_find_xpath(ext_data, "/ietf-yang-library:modules-state/module[conformance-type='import']", &imp_mods))) { |
| 502 | rc = r; |
| 503 | goto cleanup; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (!impl_mods->count) { |
| 508 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID, "No implemented modules included in ietf-yang-library data."); |
| 509 | rc = LY_EVALID; |
| 510 | goto cleanup; |
| 511 | } |
| 512 | |
| 513 | /* check all the implemented modules */ |
| 514 | for (i = 0; i < impl_mods->count; ++i) { |
| 515 | lyd_find_path(impl_mods->dnodes[i], "name", 0, &node); |
| 516 | name = lyd_get_value(node); |
| 517 | |
| 518 | lyd_find_path(impl_mods->dnodes[i], "revision", 0, &node); |
| 519 | if (node && strlen(lyd_get_value(node))) { |
| 520 | revision = lyd_get_value(node); |
| 521 | } else { |
| 522 | revision = NULL; |
| 523 | } |
| 524 | |
| 525 | if (!(mod = ly_ctx_get_module(ctx, name, revision)) || !mod->implemented) { |
| 526 | /* unsuitable module */ |
| 527 | goto cleanup; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* check all the imported modules */ |
| 532 | for (i = 0; i < imp_mods->count; ++i) { |
| 533 | lyd_find_path(imp_mods->dnodes[i], "name", 0, &node); |
| 534 | name = lyd_get_value(node); |
| 535 | |
| 536 | lyd_find_path(imp_mods->dnodes[i], "revision", 0, &node); |
| 537 | if (node && strlen(lyd_get_value(node))) { |
| 538 | revision = lyd_get_value(node); |
| 539 | } else { |
| 540 | revision = NULL; |
| 541 | } |
| 542 | |
| 543 | if (!ly_ctx_get_module(ctx, name, revision)) { |
| 544 | /* unsuitable module */ |
| 545 | goto cleanup; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /* context matches and can be reused */ |
| 550 | rc = LY_SUCCESS; |
| 551 | |
| 552 | cleanup: |
| 553 | ly_set_free(impl_mods, NULL); |
| 554 | ly_set_free(imp_mods, NULL); |
| 555 | return rc; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @brief Get schema (context) for an inline mount point. |
| 560 | * |
| 561 | * @param[in] ext Compiled extension instance. |
| 562 | * @param[in] ext_data Extension data retrieved by the callback. |
| 563 | * @param[in] config Whether the whole schema should keep its config or be set to false. |
| 564 | * @param[out] ext_ctx Schema to use for parsing the data. |
| 565 | * @return LY_ERR value. |
| 566 | */ |
| 567 | static LY_ERR |
| 568 | schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config, |
| 569 | const struct ly_ctx **ext_ctx) |
| 570 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 571 | struct lyplg_ext_sm *sm_data = ext->compiled; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 572 | struct ly_ctx *new_ctx = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 573 | uint32_t i; |
| 574 | void *mem; |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 575 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 576 | |
| 577 | assert(sm_data && sm_data->shared); |
| 578 | |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 579 | /* LOCK */ |
| 580 | if ((r = pthread_mutex_lock(&sm_data->lock))) { |
| 581 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r)); |
| 582 | return LY_ESYS; |
| 583 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 584 | |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 585 | /* try to find a context we can reuse */ |
| 586 | for (i = 0; i < sm_data->inln.schema_count; ++i) { |
| 587 | r = schema_mount_ctx_match(ext, ext_data, sm_data->inln.schemas[i].ctx); |
| 588 | if (!r) { |
| 589 | /* match */ |
| 590 | *ext_ctx = sm_data->inln.schemas[i].ctx; |
| 591 | goto cleanup; |
| 592 | } else if (r != LY_ENOT) { |
| 593 | /* error */ |
| 594 | rc = r; |
| 595 | goto cleanup; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* new schema required, create context */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 600 | if ((r = schema_mount_create_ctx(ext, ext_data, config, &new_ctx))) { |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 601 | rc = r; |
| 602 | goto cleanup; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* new entry */ |
| 606 | mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas); |
| 607 | if (!mem) { |
| 608 | ly_ctx_destroy(new_ctx); |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 609 | EXT_LOGERR_MEM_GOTO(NULL, ext, rc, cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 610 | } |
| 611 | sm_data->inln.schemas = mem; |
| 612 | ++sm_data->inln.schema_count; |
| 613 | |
| 614 | /* fill entry */ |
| 615 | sm_data->inln.schemas[i].ctx = new_ctx; |
| 616 | |
| 617 | /* use the context */ |
| 618 | *ext_ctx = sm_data->inln.schemas[i].ctx; |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 619 | |
| 620 | cleanup: |
| 621 | /* UNLOCK */ |
| 622 | pthread_mutex_unlock(&sm_data->lock); |
| 623 | |
| 624 | return rc; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
| 628 | * @brief Get schema (context) for a mount point. |
| 629 | * |
| 630 | * @param[in] ext Compiled extension instance. |
| 631 | * @param[out] ext_ctx Schema to use for parsing the data. |
| 632 | * @return LY_ERR value. |
| 633 | */ |
| 634 | static LY_ERR |
| 635 | schema_mount_get_ctx(struct lysc_ext_instance *ext, const struct ly_ctx **ext_ctx) |
| 636 | { |
| 637 | LY_ERR ret = LY_SUCCESS, r; |
| 638 | struct lyd_node *iter, *ext_data = NULL; |
| 639 | ly_bool ext_data_free = 0, config, shared; |
| 640 | |
| 641 | *ext_ctx = NULL; |
| 642 | |
| 643 | /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ |
| 644 | if ((r = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 645 | ret = r; |
| 646 | goto cleanup; |
| 647 | } |
| 648 | |
| 649 | LY_LIST_FOR(ext_data, iter) { |
| 650 | if (iter->flags & LYD_NEW) { |
| 651 | /* must be validated for the parent-reference prefix data to be stored */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 652 | 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] | 653 | ret = LY_EINVAL; |
| 654 | goto cleanup; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /* learn about this mount point */ |
| 659 | if ((r = schema_mount_get_smount(ext, ext_data, &config, &shared))) { |
| 660 | ret = r; |
| 661 | goto cleanup; |
| 662 | } |
| 663 | |
| 664 | /* create/get the context for parsing the data */ |
| 665 | if (shared) { |
| 666 | r = schema_mount_get_ctx_shared(ext, ext_data, config, ext_ctx); |
| 667 | } else { |
| 668 | r = schema_mount_get_ctx_inline(ext, ext_data, config, ext_ctx); |
| 669 | } |
| 670 | if (r) { |
| 671 | ret = r; |
| 672 | goto cleanup; |
| 673 | } |
| 674 | |
| 675 | cleanup: |
| 676 | if (ext_data_free) { |
| 677 | lyd_free_all(ext_data); |
| 678 | } |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | /** |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 683 | * @brief Snode callback for schema mount. |
| 684 | * 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] | 685 | */ |
| 686 | static LY_ERR |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 687 | schema_mount_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent, |
| 688 | const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len, |
| 689 | const struct lysc_node **snode) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 690 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 691 | LY_ERR r; |
| 692 | const struct lys_module *mod; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 693 | const struct ly_ctx *ext_ctx = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 694 | |
| 695 | /* get context based on ietf-yang-library data */ |
| 696 | if ((r = schema_mount_get_ctx(ext, &ext_ctx))) { |
| 697 | return r; |
| 698 | } |
| 699 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 700 | /* get the module */ |
| 701 | mod = lyplg_type_identity_module(ext_ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data); |
| 702 | if (!mod) { |
| 703 | return LY_ENOT; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 706 | /* get the top-level schema node */ |
| 707 | *snode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 708 | return *snode ? LY_SUCCESS : LY_ENOT; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 709 | } |
| 710 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 711 | static LY_ERR |
| 712 | schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ext_data, |
| 713 | struct ly_set **set) |
| 714 | { |
| 715 | LY_ERR ret = LY_SUCCESS; |
| 716 | char *path = NULL; |
| 717 | |
| 718 | /* get all parent references of this mount point */ |
| 719 | if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']" |
| 720 | "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 721 | EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 722 | } |
| 723 | if ((ret = lyd_find_xpath(ext_data, path, set))) { |
| 724 | goto cleanup; |
| 725 | } |
| 726 | |
| 727 | cleanup: |
| 728 | free(path); |
| 729 | return ret; |
| 730 | } |
| 731 | |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 732 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 733 | * @brief Duplicate all accessible parent references for a shared-schema mount point. |
| 734 | * |
| 735 | * @param[in] ext Compiled extension instance. |
| 736 | * @param[in] ctx_node Context node for evaluating the parent-reference XPath expressions. |
| 737 | * @param[in] ext_data Extension data retrieved by the callback. |
| 738 | * @param[in] trg_ctx Mounted data context to use for duplication. |
| 739 | * @param[out] ref_set Set of all top-level parent-ref subtrees connected to each other, may be empty. |
| 740 | * @return LY_ERR value. |
| 741 | */ |
| 742 | static LY_ERR |
| 743 | schema_mount_dup_parent_ref(const struct lysc_ext_instance *ext, const struct lyd_node *ctx_node, |
| 744 | const struct lyd_node *ext_data, const struct ly_ctx *trg_ctx, struct ly_set **ref_set) |
| 745 | { |
| 746 | LY_ERR ret = LY_SUCCESS; |
| 747 | char *path = NULL; |
| 748 | struct ly_set *set = NULL, *par_set = NULL; |
| 749 | struct lyd_node_term *term; |
| 750 | struct lyd_node *dup = NULL, *top_node, *first; |
| 751 | struct lyd_value_xpath10 *xp_val; |
| 752 | uint32_t i, j; |
| 753 | |
| 754 | *ref_set = NULL; |
| 755 | |
| 756 | if (!ext_data) { |
| 757 | /* we expect the same ext data as before and there must be some for data to be parsed */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 758 | 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] | 759 | ret = LY_EINVAL; |
| 760 | goto cleanup; |
| 761 | } |
| 762 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 763 | if ((ret = schema_mount_get_parent_ref(ext, ext_data, &set))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 764 | goto cleanup; |
| 765 | } |
| 766 | |
| 767 | /* prepare result set */ |
| 768 | if ((ret = ly_set_new(ref_set))) { |
| 769 | goto cleanup; |
| 770 | } |
| 771 | |
| 772 | first = NULL; |
| 773 | for (i = 0; i < set->count; ++i) { |
| 774 | term = set->objs[i]; |
| 775 | |
| 776 | /* get the referenced nodes (subtrees) */ |
| 777 | LYD_VALUE_GET(&term->value, xp_val); |
| 778 | if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data, |
| 779 | NULL, &par_set))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 780 | lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Parent reference \"%s\" evaluation failed.", |
| 781 | lyxp_get_expr(xp_val->exp)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 782 | goto cleanup; |
| 783 | } |
| 784 | |
| 785 | for (j = 0; j < par_set->count; ++j) { |
| 786 | /* duplicate with parents in the context of the mounted data */ |
| 787 | if ((ret = lyd_dup_single_to_ctx(par_set->dnodes[j], trg_ctx, NULL, |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 788 | 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] | 789 | goto cleanup; |
| 790 | } |
| 791 | |
| 792 | /* go top-level */ |
| 793 | while (dup->parent) { |
| 794 | dup = lyd_parent(dup); |
| 795 | } |
| 796 | |
| 797 | /* check whether the top-level node exists */ |
| 798 | if (first) { |
| 799 | if ((ret = lyd_find_sibling_first(first, dup, &top_node)) && (ret != LY_ENOTFOUND)) { |
| 800 | goto cleanup; |
| 801 | } |
| 802 | } else { |
| 803 | top_node = NULL; |
| 804 | } |
| 805 | |
| 806 | if (top_node) { |
| 807 | /* merge */ |
| 808 | ret = lyd_merge_tree(&first, dup, LYD_MERGE_DESTRUCT); |
| 809 | dup = NULL; |
| 810 | if (ret) { |
| 811 | goto cleanup; |
| 812 | } |
| 813 | } else { |
| 814 | /* insert */ |
| 815 | if ((ret = lyd_insert_sibling(first, dup, &first))) { |
| 816 | goto cleanup; |
| 817 | } |
| 818 | |
| 819 | /* add into the result set because a new top-level node was added */ |
| 820 | if ((ret = ly_set_add(*ref_set, dup, 1, NULL))) { |
| 821 | goto cleanup; |
| 822 | } |
| 823 | dup = NULL; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | cleanup: |
| 829 | free(path); |
| 830 | ly_set_free(set, NULL); |
| 831 | ly_set_free(par_set, NULL); |
| 832 | lyd_free_tree(dup); |
| 833 | if (ret && *ref_set) { |
| 834 | if ((*ref_set)->count) { |
| 835 | lyd_free_siblings((*ref_set)->dnodes[0]); |
| 836 | } |
| 837 | ly_set_free(*ref_set, NULL); |
| 838 | *ref_set = NULL; |
| 839 | } |
| 840 | return ret; |
| 841 | } |
| 842 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 843 | LY_ERR |
| 844 | lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs) |
| 845 | { |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 846 | LY_ERR rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 847 | struct ly_set *pref_set = NULL; |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 848 | struct ly_set *snode_set = NULL; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 849 | struct ly_set *results_set = NULL; |
| 850 | struct lyd_node *ext_data; |
| 851 | ly_bool ext_data_free; |
| 852 | |
| 853 | /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 854 | if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 855 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 856 | } |
| 857 | |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 858 | 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] | 859 | if (pref_set->count == 0) { |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 860 | goto cleanup; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 861 | } |
| 862 | |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 863 | LY_CHECK_GOTO(rc = ly_set_new(&results_set), cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 864 | |
| 865 | for (uint32_t i = 0; i < pref_set->count; ++i) { |
| 866 | struct lyd_node_term *term; |
| 867 | struct lyd_value_xpath10 *xp_val; |
| 868 | char *value; |
| 869 | struct ly_err_item *err; |
| 870 | |
| 871 | term = (struct lyd_node_term *)pref_set->dnodes[i]; |
| 872 | LYD_VALUE_GET(&term->value, xp_val); |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 873 | LY_CHECK_GOTO(rc = lyplg_type_print_xpath10_value(xp_val, LY_VALUE_JSON, NULL, &value, &err), cleanup); |
| 874 | LY_CHECK_ERR_GOTO(rc = lys_find_xpath(ext->module->ctx, NULL, value, 0, &snode_set), free(value), cleanup); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 875 | free(value); |
| 876 | for (uint32_t sn = 0; sn < snode_set->count; sn++) { |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 877 | 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] | 878 | } |
| 879 | ly_set_free(snode_set, NULL); |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 880 | snode_set = NULL; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | *refs = results_set; |
| 884 | |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 885 | cleanup: |
| 886 | if (rc) { |
| 887 | ly_set_free(results_set, NULL); |
| 888 | } |
| 889 | ly_set_free(snode_set, NULL); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 890 | if (ext_data_free) { |
| 891 | lyd_free_all(ext_data); |
| 892 | } |
| 893 | ly_set_free(pref_set, NULL); |
aPiecek | b72a6df | 2022-10-10 10:02:38 +0200 | [diff] [blame] | 894 | |
| 895 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 896 | } |
| 897 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 898 | /** |
| 899 | * @brief Validate callback for schema mount. |
| 900 | */ |
| 901 | static LY_ERR |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 902 | schema_mount_validate(struct lysc_ext_instance *ext, struct lyd_node *sibling, const struct lyd_node *dep_tree, |
| 903 | enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 904 | { |
| 905 | LY_ERR ret = LY_SUCCESS; |
| 906 | uint32_t old_log_opts, i; |
| 907 | struct ly_err_item *err; |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 908 | 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] | 909 | struct lyd_node *ext_diff = NULL, *diff_parent = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 910 | ly_bool ext_data_free = 0; |
| 911 | struct ly_set *ref_set = NULL; |
| 912 | |
| 913 | if (!sibling) { |
| 914 | /* some data had to be parsed for this callback to be called */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 915 | EXT_LOGERR_INT_RET(NULL, ext); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */ |
| 919 | if ((ret = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 920 | goto cleanup; |
| 921 | } |
| 922 | |
| 923 | LY_LIST_FOR(ext_data, iter) { |
| 924 | if (iter->flags & LYD_NEW) { |
| 925 | /* must be validated for the parent-reference prefix data to be stored */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 926 | 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] | 927 | ret = LY_EINVAL; |
| 928 | goto cleanup; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /* duplicate the referenced parent nodes into ext context */ |
| 933 | if ((ret = schema_mount_dup_parent_ref(ext, orig_parent, ext_data, LYD_CTX(sibling), &ref_set))) { |
| 934 | goto cleanup; |
| 935 | } |
| 936 | |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 937 | if (data_type != LYD_TYPE_DATA_YANG) { |
| 938 | /* remember the operation data tree, it may be moved */ |
| 939 | op_tree = sibling; |
| 940 | } |
| 941 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 942 | /* create accessible tree, remove LYD_EXT to not call this callback recursively */ |
| 943 | lyd_unlink_siblings(sibling); |
| 944 | LY_LIST_FOR(sibling, iter) { |
| 945 | iter->flags &= ~LYD_EXT; |
| 946 | } |
| 947 | if (ref_set->count) { |
| 948 | if ((ret = lyd_insert_sibling(sibling, ref_set->dnodes[0], &sibling))) { |
| 949 | goto cleanup; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* only store messages in the context, log as an extension */ |
| 954 | old_log_opts = ly_log_options(LY_LOSTORE_LAST); |
| 955 | |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 956 | if (data_type == LYD_TYPE_DATA_YANG) { |
| 957 | /* validate all the data */ |
| 958 | ret = lyd_validate_all(&sibling, NULL, val_opts, diff ? &ext_diff : NULL); |
| 959 | } else { |
| 960 | /* validate the operation */ |
| 961 | ret = lyd_validate_op(op_tree, dep_tree, data_type, diff ? &ext_diff : NULL); |
| 962 | } |
| 963 | |
| 964 | /* restore logging */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 965 | ly_log_options(old_log_opts); |
| 966 | |
| 967 | /* restore sibling tree */ |
| 968 | for (i = 0; i < ref_set->count; ++i) { |
| 969 | if (ref_set->dnodes[i] == sibling) { |
| 970 | sibling = sibling->next; |
| 971 | } |
| 972 | lyd_free_tree(ref_set->dnodes[i]); |
| 973 | } |
| 974 | LY_LIST_FOR(sibling, iter) { |
| 975 | iter->flags |= LYD_EXT; |
| 976 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 977 | lyplg_ext_insert(orig_parent, sibling); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 978 | |
| 979 | if (ret) { |
| 980 | /* log the error in the original context */ |
| 981 | err = ly_err_first(LYD_CTX(sibling)); |
| 982 | if (!err) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 983 | 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] | 984 | } else { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 985 | 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] | 986 | } |
| 987 | goto cleanup; |
| 988 | } |
| 989 | |
Michal Vasko | f4c6f00 | 2022-04-01 09:12:22 +0200 | [diff] [blame] | 990 | /* create proper diff */ |
| 991 | if (diff && ext_diff) { |
| 992 | /* diff nodes from an extension instance */ |
| 993 | LY_LIST_FOR(ext_diff, iter) { |
| 994 | iter->flags |= LYD_EXT; |
| 995 | } |
| 996 | |
| 997 | /* create the parent and insert the diff */ |
| 998 | if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) { |
| 999 | goto cleanup; |
| 1000 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1001 | if ((ret = lyplg_ext_insert(diff_parent, ext_diff))) { |
Michal Vasko | f4c6f00 | 2022-04-01 09:12:22 +0200 | [diff] [blame] | 1002 | goto cleanup; |
| 1003 | } |
| 1004 | ext_diff = NULL; |
| 1005 | |
| 1006 | /* go top-level and set the operation */ |
| 1007 | while (lyd_parent(diff_parent)) { |
| 1008 | diff_parent = lyd_parent(diff_parent); |
| 1009 | } |
| 1010 | if ((ret = lyd_new_meta(LYD_CTX(diff_parent), diff_parent, NULL, "yang:operation", "none", 0, NULL))) { |
| 1011 | goto cleanup; |
| 1012 | } |
| 1013 | |
| 1014 | /* finally merge into the global diff */ |
| 1015 | if ((ret = lyd_diff_merge_all(diff, diff_parent, LYD_DIFF_MERGE_DEFAULTS))) { |
| 1016 | goto cleanup; |
| 1017 | } |
| 1018 | } |
| 1019 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1020 | cleanup: |
| 1021 | ly_set_free(ref_set, NULL); |
| 1022 | lyd_free_siblings(ref_first); |
Michal Vasko | f4c6f00 | 2022-04-01 09:12:22 +0200 | [diff] [blame] | 1023 | lyd_free_tree(ext_diff); |
| 1024 | lyd_free_all(diff_parent); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1025 | if (ext_data_free) { |
| 1026 | lyd_free_all(ext_data); |
| 1027 | } |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | /** |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1032 | * @brief Schema mount compile free. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1033 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1034 | * 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] | 1035 | */ |
| 1036 | static void |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1037 | 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] | 1038 | { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1039 | struct lyplg_ext_sm *sm_data = ext->compiled; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1040 | uint32_t i; |
| 1041 | |
| 1042 | if (!sm_data) { |
| 1043 | return; |
| 1044 | } |
| 1045 | |
| 1046 | if (!--sm_data->shared->ref_count) { |
| 1047 | for (i = 0; i < sm_data->shared->schema_count; ++i) { |
| 1048 | ly_ctx_destroy(sm_data->shared->schemas[i].ctx); |
| 1049 | lydict_remove(ctx, sm_data->shared->schemas[i].content_id); |
| 1050 | } |
| 1051 | free(sm_data->shared->schemas); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1052 | free(sm_data->shared); |
| 1053 | } |
| 1054 | |
| 1055 | for (i = 0; i < sm_data->inln.schema_count; ++i) { |
| 1056 | ly_ctx_destroy(sm_data->inln.schemas[i].ctx); |
| 1057 | } |
| 1058 | free(sm_data->inln.schemas); |
Michal Vasko | 2765316 | 2022-11-21 13:58:44 +0100 | [diff] [blame^] | 1059 | |
| 1060 | pthread_mutex_destroy(&sm_data->lock); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1061 | free(sm_data); |
| 1062 | } |
| 1063 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 1064 | LIBYANG_API_DEF LY_ERR |
| 1065 | lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx) |
| 1066 | { |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1067 | struct lyd_node *ext_data = NULL; |
| 1068 | ly_bool ext_data_free = 0, config; |
| 1069 | LY_ERR rc = LY_SUCCESS; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 1070 | |
| 1071 | if (!ext->module->ctx->ext_clb) { |
| 1072 | return LY_EINVAL; |
| 1073 | } |
| 1074 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1075 | 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] | 1076 | return LY_EINVAL; |
| 1077 | } |
| 1078 | |
| 1079 | /* 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] | 1080 | if ((rc = lyplg_ext_get_data(ext->module->ctx, ext, (void **)&ext_data, &ext_data_free))) { |
| 1081 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* learn about this mount point */ |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1085 | if ((rc = schema_mount_get_smount(ext, ext_data, &config, NULL))) { |
| 1086 | goto cleanup; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 1087 | } |
| 1088 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1089 | /* create the context */ |
| 1090 | rc = schema_mount_create_ctx(ext, ext_data, config, ctx); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 1091 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1092 | cleanup: |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 1093 | if (ext_data_free) { |
| 1094 | lyd_free_all(ext_data); |
| 1095 | } |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1096 | return rc; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1099 | static void |
| 1100 | schema_mount_spriter_tree_free(void *priv) |
| 1101 | { |
| 1102 | struct sprinter_tree_priv *st_priv; |
| 1103 | |
| 1104 | st_priv = priv; |
| 1105 | ly_set_free(st_priv->refs, NULL); |
| 1106 | ly_ctx_destroy(st_priv->ext_ctx); |
| 1107 | free(st_priv); |
| 1108 | } |
| 1109 | |
| 1110 | static LY_ERR |
| 1111 | schema_mount_sprinter_tree_cnode_override_mounted(const struct lysc_node *node, const void *UNUSED(plugin_priv), |
| 1112 | ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts) |
| 1113 | { |
| 1114 | if (!node->parent) { |
| 1115 | *add_opts = "/"; |
| 1116 | } |
| 1117 | |
| 1118 | return LY_SUCCESS; |
| 1119 | } |
| 1120 | |
| 1121 | static LY_ERR |
| 1122 | schema_mount_sprinter_tree_pnode_override_mounted(const struct lysp_node *node, const void *UNUSED(plugin_priv), |
| 1123 | ly_bool *UNUSED(skip), const char **UNUSED(flags), const char **add_opts) |
| 1124 | { |
| 1125 | if (!node->parent) { |
| 1126 | *add_opts = "/"; |
| 1127 | } |
| 1128 | |
| 1129 | return LY_SUCCESS; |
| 1130 | } |
| 1131 | |
| 1132 | static LY_ERR |
| 1133 | schema_mount_sprinter_tree_node_override_parent_refs(const struct lysc_node *node, const void *plugin_priv, |
| 1134 | ly_bool *skip, const char **UNUSED(flags), const char **add_opts) |
| 1135 | { |
| 1136 | uint32_t i; |
| 1137 | const struct ly_set *refs; |
| 1138 | const struct lysc_module *mod; |
| 1139 | struct lysc_node *ref, *iter; |
| 1140 | |
| 1141 | refs = ((struct sprinter_tree_priv *)plugin_priv)->refs; |
| 1142 | mod = node->module->compiled; |
| 1143 | |
| 1144 | /* Assume the @p node will be skipped. */ |
| 1145 | *skip = 1; |
| 1146 | for (i = 0; (i < refs->count) && *skip; i++) { |
| 1147 | ref = refs->snodes[i]; |
| 1148 | if (ref->module->compiled != mod) { |
| 1149 | /* parent-reference points to different module */ |
| 1150 | continue; |
| 1151 | } |
| 1152 | |
| 1153 | for (iter = ref; iter; iter = iter->parent) { |
| 1154 | if (iter == node) { |
| 1155 | /* @p node is not skipped because it is parent-rererence node or his parent */ |
| 1156 | *skip = 0; |
| 1157 | break; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | if (!*skip && !node->parent) { |
| 1163 | /* top-node has additional opts */ |
| 1164 | *add_opts = "@"; |
| 1165 | } |
| 1166 | |
| 1167 | return LY_SUCCESS; |
| 1168 | } |
| 1169 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1170 | /** |
| 1171 | * @brief Schema mount schema parsed tree printer. |
| 1172 | * |
| 1173 | * Implementation of ::lyplg_ext_sprinter_ptree_clb callback set as lyext_plugin::printer_ptree. |
| 1174 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1175 | static LY_ERR |
| 1176 | schema_mount_sprinter_ptree(struct lysp_ext_instance *UNUSED(ext), const struct lyspr_tree_ctx *ctx, |
| 1177 | const char **flags, const char **UNUSED(add_opts)) |
| 1178 | { |
| 1179 | if (!ctx) { |
| 1180 | *flags = "mp"; |
| 1181 | } |
| 1182 | |
| 1183 | return LY_SUCCESS; |
| 1184 | } |
| 1185 | |
Michal Vasko | 266308d | 2022-11-08 08:45:31 +0100 | [diff] [blame] | 1186 | /** |
| 1187 | * @brief Schema mount schema compiled tree printer. |
| 1188 | * |
| 1189 | * Implementation of ::lyplg_ext_sprinter_ctree_clb callback set as lyext_plugin::printer_ctree. |
| 1190 | */ |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1191 | static LY_ERR |
| 1192 | schema_mount_sprinter_ctree(struct lysc_ext_instance *ext, const struct lyspr_tree_ctx *ctx, |
| 1193 | const char **flags, const char **UNUSED(add_opts)) |
| 1194 | { |
| 1195 | LY_ERR rc = LY_SUCCESS; |
| 1196 | struct ly_ctx *ext_ctx = NULL; |
| 1197 | const struct lys_module *mod; |
| 1198 | struct ly_set *refs = NULL; |
| 1199 | struct lysc_node *tree1, *tree2; |
| 1200 | uint32_t i, j; |
| 1201 | ly_bool from_parent_ref, is_first; |
| 1202 | struct sprinter_tree_priv *st_priv; |
| 1203 | |
| 1204 | if (!ctx) { |
| 1205 | *flags = "mp"; |
| 1206 | return LY_SUCCESS; |
| 1207 | } |
| 1208 | |
| 1209 | if (lyplg_ext_schema_mount_create_context(ext, &ext_ctx)) { |
| 1210 | /* Void mount point */ |
| 1211 | return LY_SUCCESS; |
| 1212 | } |
| 1213 | |
| 1214 | rc = lyplg_ext_schema_mount_get_parent_ref(ext, &refs); |
| 1215 | LY_CHECK_GOTO(rc, cleanup); |
| 1216 | |
| 1217 | /* build new list of modules to print. This list will omit internal |
| 1218 | * modules, modules with no nodes (e.g., iana-if-types) and modules |
| 1219 | * that were loaded as the result of a parent-reference. |
| 1220 | */ |
| 1221 | i = ly_ctx_internal_modules_count(ext_ctx); |
| 1222 | while ((mod = ly_ctx_get_module_iter(ext_ctx, &i))) { |
| 1223 | from_parent_ref = 0; |
| 1224 | |
| 1225 | for (j = 0; refs && j < refs->count; j++) { |
| 1226 | if (!strcmp(mod->ns, refs->snodes[j]->module->ns)) { |
| 1227 | from_parent_ref = 1; |
| 1228 | break; |
| 1229 | } |
| 1230 | } |
| 1231 | if (from_parent_ref) { |
| 1232 | /* Modules loaded as the result of a parent-reference are added later. */ |
| 1233 | continue; |
| 1234 | } |
| 1235 | |
| 1236 | /* Add data nodes, rpcs and notifications. */ |
| 1237 | if ((ext_ctx->flags & LY_CTX_SET_PRIV_PARSED) && mod->compiled) { |
| 1238 | /* For compiled module. */ |
| 1239 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, mod->compiled->data, |
| 1240 | schema_mount_sprinter_tree_cnode_override_mounted); |
| 1241 | LY_CHECK_GOTO(rc, cleanup); |
| 1242 | if (mod->compiled->rpcs) { |
| 1243 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->rpcs->node, |
| 1244 | schema_mount_sprinter_tree_cnode_override_mounted); |
| 1245 | } |
| 1246 | LY_CHECK_GOTO(rc, cleanup); |
| 1247 | if (mod->compiled->notifs) { |
| 1248 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, &mod->compiled->notifs->node, |
| 1249 | schema_mount_sprinter_tree_cnode_override_mounted); |
| 1250 | } |
| 1251 | LY_CHECK_GOTO(rc, cleanup); |
| 1252 | } else { |
| 1253 | /* For parsed module. */ |
| 1254 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, mod->parsed->data, |
| 1255 | schema_mount_sprinter_tree_pnode_override_mounted); |
| 1256 | LY_CHECK_GOTO(rc, cleanup); |
| 1257 | if (mod->parsed->rpcs) { |
| 1258 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->rpcs->node, |
| 1259 | schema_mount_sprinter_tree_pnode_override_mounted); |
| 1260 | } |
| 1261 | LY_CHECK_GOTO(rc, cleanup); |
| 1262 | if (mod->parsed->notifs) { |
| 1263 | rc = lyplg_ext_sprinter_ptree_add_nodes(ctx, &mod->parsed->notifs->node, |
| 1264 | schema_mount_sprinter_tree_pnode_override_mounted); |
| 1265 | } |
| 1266 | LY_CHECK_GOTO(rc, cleanup); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /* Add modules loaded as the result of a parent-reference. */ |
| 1271 | for (i = 0; refs && (i < refs->count); i++) { |
| 1272 | tree1 = refs->snodes[i]->module->compiled->data; |
| 1273 | |
| 1274 | /* Add data nodes from the module only once. */ |
| 1275 | is_first = 1; |
| 1276 | for (j = 0; j < i; j++) { |
| 1277 | tree2 = refs->snodes[j]->module->compiled->data; |
| 1278 | if (tree1 == tree2) { |
| 1279 | is_first = 0; |
| 1280 | break; |
| 1281 | } |
| 1282 | } |
| 1283 | if (is_first) { |
| 1284 | /* Add all data nodes but unavailable nodes are skipped in the callback. */ |
| 1285 | rc = lyplg_ext_sprinter_ctree_add_nodes(ctx, tree1, schema_mount_sprinter_tree_node_override_parent_refs); |
| 1286 | LY_CHECK_GOTO(rc, cleanup); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | /* Add private plugin data. */ |
| 1291 | st_priv = calloc(1, sizeof(*st_priv)); |
| 1292 | st_priv->ext_ctx = ext_ctx; |
| 1293 | st_priv->refs = refs; |
| 1294 | rc = lyplg_ext_sprinter_tree_set_priv(ctx, st_priv, schema_mount_spriter_tree_free); |
| 1295 | |
| 1296 | cleanup: |
| 1297 | if (rc) { |
| 1298 | ly_set_free(refs, NULL); |
| 1299 | ly_ctx_destroy(ext_ctx); |
| 1300 | } |
| 1301 | |
| 1302 | return rc; |
| 1303 | } |
| 1304 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1305 | /** |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 1306 | * @brief Plugin descriptions for the Yang Schema Mount extension. |
| 1307 | * |
| 1308 | * Note that external plugins are supposed to use: |
| 1309 | * |
| 1310 | * LYPLG_EXTENSIONS = { |
| 1311 | */ |
| 1312 | const struct lyplg_ext_record plugins_schema_mount[] = { |
| 1313 | { |
| 1314 | .module = "ietf-yang-schema-mount", |
| 1315 | .revision = "2019-01-14", |
| 1316 | .name = "mount-point", |
| 1317 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1318 | .plugin.id = "ly2 schema mount v1", |
| 1319 | .plugin.parse = schema_mount_parse, |
| 1320 | .plugin.compile = schema_mount_compile, |
Michal Vasko | 941e056 | 2022-10-18 10:35:00 +0200 | [diff] [blame] | 1321 | .plugin.printer_info = NULL, |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 1322 | .plugin.printer_ctree = schema_mount_sprinter_ctree, |
| 1323 | .plugin.printer_ptree = schema_mount_sprinter_ptree, |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1324 | .plugin.node = NULL, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1325 | .plugin.snode = schema_mount_snode, |
| 1326 | .plugin.validate = schema_mount_validate, |
| 1327 | .plugin.pfree = NULL, |
| 1328 | .plugin.cfree = schema_mount_cfree |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 1329 | }, |
| 1330 | {0} /* terminating zeroed item */ |
| 1331 | }; |