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 | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 19 | #include "tree_edit.h" |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 20 | #include "tree_schema.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame^] | 22 | #include "plugins_exts_compile.h" |
| 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | struct ly_ctx; |
| 25 | struct lyd_node; |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 26 | |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
| 31 | /** |
| 32 | * @defgroup extensions YANG Extensions |
| 33 | * |
| 34 | * @{ |
| 35 | */ |
| 36 | |
| 37 | /** |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 38 | * @brief Extensions API version |
| 39 | */ |
| 40 | #define LYEXT_API_VERSION 1 |
| 41 | |
| 42 | /** |
| 43 | * @brief Macro to store version of extension plugins API in the plugins. |
| 44 | * It is matched when the plugin is being loaded by libyang. |
| 45 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 46 | #define LYEXT_VERSION_CHECK uint32_t lyext_api_version = LYEXT_API_VERSION; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 47 | |
| 48 | /** |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 49 | * @brief Possible cardinalities of the YANG statements. |
| 50 | * |
| 51 | * Used in extensions plugins to define cardinalities of the extension instance substatements. |
| 52 | */ |
| 53 | enum ly_stmt_cardinality { |
| 54 | LY_STMT_CARD_OPT, /* 0..1 */ |
| 55 | LY_STMT_CARD_MAND, /* 1 */ |
| 56 | LY_STMT_CARD_SOME, /* 1..n */ |
| 57 | LY_STMT_CARD_ANY /* 0..n */ |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * @brief Description of the extension instance substatements. |
| 62 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 63 | * Provided by extensions plugins to libyang to be able to correctly compile the content of extension instances. |
| 64 | * 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] | 65 | */ |
| 66 | struct lysc_ext_substmt { |
| 67 | enum ly_stmt stmt; /**< allowed substatement */ |
| 68 | enum ly_stmt_cardinality cardinality; /**< cardinality of the substatement */ |
| 69 | void *storage; /**< pointer to the storage of the compiled statement according to the specific |
| 70 | lysc_ext_substmt::stmt and lysc_ext_substmt::cardinality */ |
| 71 | }; |
| 72 | |
| 73 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 74 | * @brief Types of the YANG printers |
| 75 | */ |
| 76 | enum lys_ypr_schema_type { |
| 77 | LYS_YPR_PARSED, /**< YANG printer of the parsed schema */ |
| 78 | LYS_YPR_COMPILED /**< YANG printer of the compiled schema */ |
| 79 | }; |
| 80 | |
| 81 | /** |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame^] | 82 | * @brief Compiled YANG printer context for use in ::lyext_clb_schema_printer callback implementation. |
| 83 | * |
| 84 | * The structure provides basic information how the compiled schema is supposed to be printed and where. In the most simple |
| 85 | * case, the provided context is just passed into ::lysc_print_extension_instance() function which handles printing the |
| 86 | * extension's substatements in the standard way. |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 87 | */ |
| 88 | struct lys_ypr_ctx { |
| 89 | struct ly_out *out; /**< output specification */ |
| 90 | uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
| 91 | uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */ |
| 92 | const struct lys_module *module; /**< schema to print */ |
| 93 | enum lys_ypr_schema_type schema; /**< type of the schema to print */ |
| 94 | }; |
| 95 | |
| 96 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 97 | * @brief Print substatements of an extension instance |
| 98 | * |
| 99 | * Generic function to access YANG printer functions from the extension plugins (::lyext_clb_schema_printer). |
| 100 | * |
| 101 | * @param[in] ctx YANG printer context to provide output handler and other information for printing. |
| 102 | * @param[in] ext The compiled extension instance to access the extensions and substatements data. |
| 103 | * @param[in, out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed, |
| 104 | * 1 otherwise. |
| 105 | */ |
| 106 | void lysc_print_extension_instance(struct lys_ypr_ctx *ctx, const struct lysc_ext_instance *ext, ly_bool *flag); |
| 107 | |
| 108 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 109 | * @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] | 110 | * |
| 111 | * @param[in] libyang context |
| 112 | * @param[in] substmts The sized array of extension instance's substatements. The whole array is freed except the storage |
| 113 | * places which are expected to be covered by the extension plugin. |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 114 | */ |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 115 | 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] | 116 | |
| 117 | /** |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 118 | * @brief Get pointer to the storage of the specified substatement in the given extension instance. |
| 119 | * |
| 120 | * The function simplifies access into the ::lysc_ext_instance.substmts sized array. |
| 121 | * |
| 122 | * @param[in] ext Compiled extension instance to process. |
| 123 | * @param[in] substmt Extension substatement to search for. |
| 124 | * @param[out] instance_p Pointer where the storage of the @p substmt will be provided. The specific type returned depends |
| 125 | * on the @p substmt and can be found in the documentation of each ::ly_stmt value. Also note that some of the substatements |
| 126 | * (::lysc_node based or flags) can share the storage with other substatements. In case the pointer is NULL, still the return |
| 127 | * code can be used to at least know if the substatement is allowed for the extension. |
| 128 | * @param[out] cardinality_p Pointer to provide allowed cardinality of the substatements in the extension. Note that in some |
| 129 | * cases, the type of the storage depends also on the cardinality of the substatement. |
| 130 | * @return LY_SUCCESS if the @p substmt found. |
| 131 | * @return LY_ENOT in case the @p ext is not able to store (does not allow) the specified @p substmt. |
| 132 | */ |
| 133 | LY_ERR lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, |
| 134 | void **instance_p, enum ly_stmt_cardinality *cardinality_p); |
| 135 | |
| 136 | /** |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 137 | * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared |
| 138 | * and only the extension specific data are supposed to be added (if any). |
| 139 | * |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 140 | * The parsed generic statements can be processed by the callback on its own or the ::lys_compile_extension_instance |
| 141 | * function can be used to let the compilation to libyang following the standard rules for processing the YANG statements. |
| 142 | * |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 143 | * @param[in] cctx Current compile context. |
| 144 | * @param[in] p_ext Parsed extension instance data. |
| 145 | * @param[in,out] c_ext Prepared compiled extension instance structure where an addition, extension-specific, data are supposed to be placed |
| 146 | * for later use (data validation or use of external tool). |
| 147 | * @return LY_SUCCESS in case of success. |
| 148 | * @return LY_EVALID in case of non-conforming parsed data. |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 149 | * @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] | 150 | */ |
| 151 | typedef LY_ERR (*lyext_clb_compile)(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext); |
| 152 | |
| 153 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 154 | * @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] | 155 | * |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 156 | * @param[in] ctx libyang context. |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 157 | * @param[in,out] ext Compiled extension structure where the data to free are placed. |
| 158 | */ |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 159 | 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] | 160 | |
| 161 | /** |
| 162 | * @brief Callback to decide if data instance is valid according to the schema. |
| 163 | * |
| 164 | * 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] | 165 | * (which is specified as ::lysc_ext_instance.parent_type): |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 166 | * - LYEXT_PAR_NODE - @p node is instance of the schema node where the extension instance was specified. |
| 167 | * - 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. |
| 168 | * - LYEXT_PAR_TYPE - @p node is instance of the schema node with the value of the type where the extension instance was specified. |
| 169 | * - LYEXT_PAR_TYPE_BIT - @p node is instance of the schema node with the value of the bit where the extension instance was specified. |
| 170 | * - LYEXT_PAR_TYPE_ENUM - @p node is instance of the schema node with the value of the enum where the extension instance was specified. |
| 171 | * |
| 172 | * @param[in] ext Extension instance to be checked. |
| 173 | * @param[in] node Data node, where the extension data are supposed to be placed. |
| 174 | * |
| 175 | * @return LY_SUCCESS on data validation success. |
| 176 | * @return LY_EVALID in case the validation fails. |
| 177 | */ |
| 178 | typedef LY_ERR (*lyext_clb_data_validation)(struct lysc_ext_instance *ext, struct lyd_node *node); |
| 179 | |
| 180 | /** |
Radek Krejci | adcf63d | 2021-02-09 10:21:18 +0100 | [diff] [blame] | 181 | * @brief Callback to print the compiled extension instance's private data in the INFO format. |
| 182 | * |
| 183 | * @param[in] ctx YANG printer context to provide output handler and other information for printing. |
| 184 | * @param[in] ext The compiled extension instance, mainly to access the extensions. |
| 185 | * @param[in, out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed, |
| 186 | * 1 otherwise. |
| 187 | * |
| 188 | * @return LY_SUCCESS when everything was fine, other LY_ERR values in case of failure |
| 189 | */ |
| 190 | typedef LY_ERR (*lyext_clb_schema_printer)(struct lys_ypr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag); |
| 191 | |
| 192 | /** |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 193 | * @brief Extension plugin implementing various aspects of a YANG extension |
| 194 | */ |
| 195 | struct lyext_plugin { |
| 196 | const char *id; /**< Plugin identification (mainly for distinguish incompatible versions of the plugins for external tools) */ |
| 197 | lyext_clb_compile compile; /**< Callback to compile extension instance from the parsed data */ |
| 198 | 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] | 199 | lyext_clb_schema_printer sprinter; /**< Callback to print the compiled content (info format) of the extension instance */ |
| 200 | /* lyext_clb_data_printer dprinter; ? */ |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 201 | 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] | 202 | }; |
| 203 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 204 | /** |
| 205 | * @brief Provide a log message from an extension plugin. |
| 206 | * |
| 207 | * @param[in] ext Compiled extension structure providing generic information about the extension/plugin causing the message. |
| 208 | * @param[in] level Log message level (error, warning, etc.) |
| 209 | * @param[in] err_no Error type code. |
| 210 | * @param[in] path Path relevant to the message. |
| 211 | * @param[in] format Format string to print. |
| 212 | */ |
| 213 | void lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...); |
| 214 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 215 | /** @} extensions */ |
Radek Krejci | d7e8a62 | 2018-10-29 15:54:55 +0100 | [diff] [blame] | 216 | |
| 217 | #ifdef __cplusplus |
| 218 | } |
| 219 | #endif |
| 220 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 221 | #endif /* LY_PLUGINS_EXTS_H_ */ |