Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 1 | /** |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2 | * @file plugins_exts.h |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang support for YANG extensions implementation. |
| 5 | * |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 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 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 15 | #ifndef LY_PLUGINS_EXTS_H_ |
| 16 | #define LY_PLUGINS_EXTS_H_ |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include "log.h" |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 19 | #include "tree_schema.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | |
| 21 | struct ly_ctx; |
| 22 | struct lyd_node; |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 23 | struct lysc_ctx; |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 24 | |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | /** |
| 30 | * @defgroup extensions YANG Extensions |
| 31 | * |
| 32 | * @{ |
| 33 | */ |
| 34 | |
| 35 | /** |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 36 | * @brief Extensions API version |
| 37 | */ |
| 38 | #define LYEXT_API_VERSION 1 |
| 39 | |
| 40 | /** |
| 41 | * @brief Macro to store version of extension plugins API in the plugins. |
| 42 | * It is matched when the plugin is being loaded by libyang. |
| 43 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 44 | #define LYEXT_VERSION_CHECK uint32_t lyext_api_version = LYEXT_API_VERSION; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 45 | |
| 46 | /** |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 47 | * @defgroup extensionscompile YANG Extensions - Compilation Helpers |
| 48 | * @ingroup extensions |
| 49 | * @brief Helper functions to compile (via lyext_clb_compile callback) statements inside the extension instance. |
| 50 | * |
| 51 | * NOTE: There is a lot of useful static functions in the tree_schema_compile.c which could be provided here. Since we don't want |
| 52 | * to have a large API with functions which will be never used, we provide here just the functions which are evidently needed. |
| 53 | * If you, as an extension plugin author, need to make some of the compile functions available, please contact libyang maintainers |
| 54 | * via the GITHUB issue tracker. |
| 55 | * |
| 56 | * @{ |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 57 | */ |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 58 | |
| 59 | /** |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 60 | * @brief Possible cardinalities of the YANG statements. |
| 61 | * |
| 62 | * Used in extensions plugins to define cardinalities of the extension instance substatements. |
| 63 | */ |
| 64 | enum ly_stmt_cardinality { |
| 65 | LY_STMT_CARD_OPT, /* 0..1 */ |
| 66 | LY_STMT_CARD_MAND, /* 1 */ |
| 67 | LY_STMT_CARD_SOME, /* 1..n */ |
| 68 | LY_STMT_CARD_ANY /* 0..n */ |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * @brief Description of the extension instance substatements. |
| 73 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 74 | * Provided by extensions plugins to libyang to be able to correctly compile the content of extension instances. |
| 75 | * Note that order of the defined records matters - just follow the values of ::ly_stmt and order the records from lower to higher values. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 76 | */ |
| 77 | struct lysc_ext_substmt { |
| 78 | enum ly_stmt stmt; /**< allowed substatement */ |
| 79 | enum ly_stmt_cardinality cardinality; /**< cardinality of the substatement */ |
| 80 | void *storage; /**< pointer to the storage of the compiled statement according to the specific |
| 81 | lysc_ext_substmt::stmt and lysc_ext_substmt::cardinality */ |
| 82 | }; |
| 83 | |
| 84 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 85 | * @brief Types of the YANG printers |
| 86 | */ |
| 87 | enum lys_ypr_schema_type { |
| 88 | LYS_YPR_PARSED, /**< YANG printer of the parsed schema */ |
| 89 | LYS_YPR_COMPILED /**< YANG printer of the compiled schema */ |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * @brief YANG printer context. |
| 94 | */ |
| 95 | struct lys_ypr_ctx { |
| 96 | struct ly_out *out; /**< output specification */ |
| 97 | uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
| 98 | uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */ |
| 99 | const struct lys_module *module; /**< schema to print */ |
| 100 | enum lys_ypr_schema_type schema; /**< type of the schema to print */ |
| 101 | }; |
| 102 | |
| 103 | /** |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 104 | * @brief Compile substatements of an extension instance. |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 105 | * TODO |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 106 | * @return LY_ENOT if the extension is disabled and should be ignored. |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 107 | */ |
Radek Krejci | 6b88a46 | 2021-02-17 12:39:34 +0100 | [diff] [blame] | 108 | LY_ERR lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 109 | |
| 110 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 111 | * @brief Print substatements of an extension instance |
| 112 | * |
| 113 | * Generic function to access YANG printer functions from the extension plugins (::lyext_clb_schema_printer). |
| 114 | * |
| 115 | * @param[in] ctx YANG printer context to provide output handler and other information for printing. |
| 116 | * @param[in] ext The compiled extension instance to access the extensions and substatements data. |
| 117 | * @param[in, out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed, |
| 118 | * 1 otherwise. |
| 119 | */ |
| 120 | void lysc_print_extension_instance(struct lys_ypr_ctx *ctx, const struct lysc_ext_instance *ext, ly_bool *flag); |
| 121 | |
| 122 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 123 | * @brief Free the extension instance's data compiled with ::lys_compile_extension_instance(). |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 124 | * |
| 125 | * @param[in] libyang context |
| 126 | * @param[in] substmts The sized array of extension instance's substatements. The whole array is freed except the storage |
| 127 | * places which are expected to be covered by the extension plugin. |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 128 | */ |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 129 | void lysc_extension_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 130 | |
| 131 | /** |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 132 | * @brief Duplicate the compiled extension (definition) structure. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 133 | * TODO should this be in API? currently required by nacm_compile() |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 134 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 135 | * |
| 136 | * @param[in] orig The extension structure to duplicate. |
| 137 | * @return The duplicated structure to use. |
| 138 | */ |
| 139 | struct lysc_ext *lysc_ext_dup(struct lysc_ext *orig); |
| 140 | |
| 141 | /** |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 142 | * @brief Get pointer to the storage of the specified substatement in the given extension instance. |
| 143 | * |
| 144 | * The function simplifies access into the ::lysc_ext_instance.substmts sized array. |
| 145 | * |
| 146 | * @param[in] ext Compiled extension instance to process. |
| 147 | * @param[in] substmt Extension substatement to search for. |
| 148 | * @param[out] instance_p Pointer where the storage of the @p substmt will be provided. The specific type returned depends |
| 149 | * on the @p substmt and can be found in the documentation of each ::ly_stmt value. Also note that some of the substatements |
| 150 | * (::lysc_node based or flags) can share the storage with other substatements. In case the pointer is NULL, still the return |
| 151 | * code can be used to at least know if the substatement is allowed for the extension. |
| 152 | * @param[out] cardinality_p Pointer to provide allowed cardinality of the substatements in the extension. Note that in some |
| 153 | * cases, the type of the storage depends also on the cardinality of the substatement. |
| 154 | * @return LY_SUCCESS if the @p substmt found. |
| 155 | * @return LY_ENOT in case the @p ext is not able to store (does not allow) the specified @p substmt. |
| 156 | */ |
| 157 | LY_ERR lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, |
| 158 | void **instance_p, enum ly_stmt_cardinality *cardinality_p); |
| 159 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 160 | const struct lysc_node *lys_find_ext_instance_node(const struct lysc_ext_instance *ext, const struct lys_module *module, |
| 161 | const char *name, size_t name_len, uint16_t nodetype, uint32_t options); |
| 162 | |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 163 | /** |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 164 | * @brief Update path in the compile context, which is used for logging where the compilation failed. |
| 165 | * |
| 166 | * @param[in] ctx Compile context with the path. |
Radek Krejci | a601699 | 2021-03-03 10:13:41 +0100 | [diff] [blame] | 167 | * @param[in] parent_module Module of the current node's parent to check difference with the currently processed module (taken from @p ctx). |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 168 | * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is `{keyword}`, the following |
| 169 | * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used). |
| 170 | */ |
Radek Krejci | a601699 | 2021-03-03 10:13:41 +0100 | [diff] [blame] | 171 | void lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name); |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 172 | |
| 173 | /** @} extensionscompile */ |
| 174 | |
| 175 | /** |
| 176 | * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared |
| 177 | * and only the extension specific data are supposed to be added (if any). |
| 178 | * |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 179 | * The parsed generic statements can be processed by the callback on its own or the ::lys_compile_extension_instance |
| 180 | * function can be used to let the compilation to libyang following the standard rules for processing the YANG statements. |
| 181 | * |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 182 | * @param[in] cctx Current compile context. |
| 183 | * @param[in] p_ext Parsed extension instance data. |
| 184 | * @param[in,out] c_ext Prepared compiled extension instance structure where an addition, extension-specific, data are supposed to be placed |
| 185 | * for later use (data validation or use of external tool). |
| 186 | * @return LY_SUCCESS in case of success. |
| 187 | * @return LY_EVALID in case of non-conforming parsed data. |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 188 | * @return LY_ENOT in case the extension instance is not supported and should be removed. |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 189 | */ |
| 190 | typedef LY_ERR (*lyext_clb_compile)(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext); |
| 191 | |
| 192 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 193 | * @brief Callback to free the extension specific data created by the ::lyext_clb_compile callback of the same extension plugin. |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 194 | * |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 195 | * @param[in] ctx libyang context. |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 196 | * @param[in,out] ext Compiled extension structure where the data to free are placed. |
| 197 | */ |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 198 | typedef void (*lyext_clb_free)(struct ly_ctx *ctx, struct lysc_ext_instance *ext); |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * @brief Callback to decide if data instance is valid according to the schema. |
| 202 | * |
| 203 | * The callback is used only for the extension instances placed in the following parent statements |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 204 | * (which is specified as ::lysc_ext_instance.parent_type): |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 205 | * - LYEXT_PAR_NODE - @p node is instance of the schema node where the extension instance was specified. |
| 206 | * - LYEXT_PAR_TPDF - @p node is instance of the schema node with the value of the typedef's type where the extension instance was specified. |
| 207 | * - LYEXT_PAR_TYPE - @p node is instance of the schema node with the value of the type where the extension instance was specified. |
| 208 | * - LYEXT_PAR_TYPE_BIT - @p node is instance of the schema node with the value of the bit where the extension instance was specified. |
| 209 | * - LYEXT_PAR_TYPE_ENUM - @p node is instance of the schema node with the value of the enum where the extension instance was specified. |
| 210 | * |
| 211 | * @param[in] ext Extension instance to be checked. |
| 212 | * @param[in] node Data node, where the extension data are supposed to be placed. |
| 213 | * |
| 214 | * @return LY_SUCCESS on data validation success. |
| 215 | * @return LY_EVALID in case the validation fails. |
| 216 | */ |
| 217 | typedef LY_ERR (*lyext_clb_data_validation)(struct lysc_ext_instance *ext, struct lyd_node *node); |
| 218 | |
| 219 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 220 | * @brief Callback to print the compiled extension instance's private data in the INFO format. |
| 221 | * |
| 222 | * @param[in] ctx YANG printer context to provide output handler and other information for printing. |
| 223 | * @param[in] ext The compiled extension instance, mainly to access the extensions. |
| 224 | * @param[in, out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed, |
| 225 | * 1 otherwise. |
| 226 | * |
| 227 | * @return LY_SUCCESS when everything was fine, other LY_ERR values in case of failure |
| 228 | */ |
| 229 | typedef LY_ERR (*lyext_clb_schema_printer)(struct lys_ypr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag); |
| 230 | |
| 231 | /** |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 232 | * @brief Extension plugin implementing various aspects of a YANG extension |
| 233 | */ |
| 234 | struct lyext_plugin { |
| 235 | const char *id; /**< Plugin identification (mainly for distinguish incompatible versions of the plugins for external tools) */ |
| 236 | lyext_clb_compile compile; /**< Callback to compile extension instance from the parsed data */ |
| 237 | lyext_clb_data_validation validate; /**< Callback to decide if data instance is valid according to the schema. */ |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 238 | lyext_clb_schema_printer sprinter; /**< Callback to print the compiled content (info format) of the extension instance */ |
| 239 | /* lyext_clb_data_printer dprinter; ? */ |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 240 | lyext_clb_free free; /**< Free the extension instance specific data created by ::lyext_plugin.compile callback */ |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 241 | }; |
| 242 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 243 | struct lyext_plugins_list { |
| 244 | const char *module; /**< name of the module where the extension is defined */ |
| 245 | const char *revision; /**< optional module revision - if not specified, the plugin applies to any revision, |
| 246 | which is not an optimal approach due to a possible future revisions of the module. |
| 247 | Instead, there should be defined multiple items in the plugins list, each with the |
| 248 | different revision, but all with the same pointer to the plugin extension. The |
| 249 | only valid use case for the NULL revision is the case the module has no revision. */ |
| 250 | const char *name; /**< name of the extension */ |
| 251 | struct lyext_plugin *plugin; /**< plugin for the extension */ |
| 252 | }; |
| 253 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 254 | /** |
| 255 | * @brief Provide a log message from an extension plugin. |
| 256 | * |
| 257 | * @param[in] ext Compiled extension structure providing generic information about the extension/plugin causing the message. |
| 258 | * @param[in] level Log message level (error, warning, etc.) |
| 259 | * @param[in] err_no Error type code. |
| 260 | * @param[in] path Path relevant to the message. |
| 261 | * @param[in] format Format string to print. |
| 262 | */ |
| 263 | void lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...); |
| 264 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 265 | /** @} extensions */ |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 266 | |
| 267 | #ifdef __cplusplus |
| 268 | } |
| 269 | #endif |
| 270 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 271 | #endif /* LY_PLUGINS_EXTS_H_ */ |