Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file plugins_exts_compile.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang support for YANG extensions implementation - schema compilation related items. |
| 5 | * |
| 6 | * Copyright (c) 2015 - 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 | #ifndef LY_PLUGINS_EXTS_COMPILE_H_ |
| 16 | #define LY_PLUGINS_EXTS_COMPILE_H_ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | |
| 20 | #include "log.h" |
| 21 | #include "tree_schema.h" |
| 22 | |
| 23 | struct ly_ctx; |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 24 | struct lysc_ctx; |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | /** |
Radek Krejci | 7510412 | 2021-04-01 15:37:45 +0200 | [diff] [blame] | 31 | * @defgroup pluginsExtensionsCompile Plugins: Extensions compilation support |
| 32 | * @ingroup pluginsExtensions |
| 33 | * |
| 34 | * Helper functions to implement extension plugin's compile callback. |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 35 | * |
| 36 | * @{ |
| 37 | */ |
| 38 | |
| 39 | /** |
| 40 | * @defgroup scflags Schema compile flags |
| 41 | * |
| 42 | * Flags to modify schema compilation process and change the way how the particular statements are being compiled. * |
| 43 | * @{ |
| 44 | */ |
| 45 | #define LYS_COMPILE_GROUPING 0x01 /**< Compiling (validation) of a non-instantiated grouping. |
| 46 | In this case not all the restrictions are checked since they can |
Radek Krejci | 4e39123 | 2021-04-15 14:41:26 +0200 | [diff] [blame] | 47 | be valid only in the real placement of the grouping. This is |
| 48 | the case of any restriction that needs to look out of the statements |
| 49 | themselves, since the context is not known. */ |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 50 | #define LYS_COMPILE_DISABLED 0x02 /**< Compiling a disabled subtree (by its if-features). Meaning |
| 51 | it will be removed at the end of compilation and should not be |
| 52 | added to any unres sets. */ |
| 53 | #define LYS_COMPILE_NO_CONFIG 0x04 /**< ignore config statements, neither inherit config value */ |
| 54 | #define LYS_COMPILE_NO_DISABLED 0x08 /**< ignore if-feature statements */ |
| 55 | |
| 56 | #define LYS_COMPILE_RPC_INPUT (LYS_IS_INPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action input */ |
| 57 | #define LYS_COMPILE_RPC_OUTPUT (LYS_IS_OUTPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action output */ |
| 58 | #define LYS_COMPILE_NOTIFICATION (LYS_IS_NOTIF | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of Notification */ |
| 59 | |
| 60 | /** @} scflags */ |
| 61 | |
| 62 | /** |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 63 | * @brief YANG schema compilation context for use in ::lyplg_ext_compile_clb callback implementation. |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 64 | * |
| 65 | * The structure stores complex information connected with the schema compilation process. In the most simple case, |
| 66 | * the callback is just supposed to pass the provided callback to ::lys_compile_extension_instance() functions. |
| 67 | * |
| 68 | * To access various items from the context, use some of the following lysc_ctx_get_* getters. |
| 69 | */ |
| 70 | struct lysc_ctx; |
| 71 | |
| 72 | /** |
| 73 | * @brief YANG schema compilation context getter for libyang context. |
| 74 | * @param[in] ctx YANG schema compilation context. |
| 75 | * @return libyang context connected with the compilation context. |
| 76 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 77 | LIBYANG_API_DECL struct ly_ctx *lysc_ctx_get_ctx(const struct lysc_ctx *ctx); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief YANG schema compilation context getter for compilation options. |
| 81 | * @param[in] ctx YANG schema compilation context. |
Radek Krejci | f8d7f9a | 2021-03-10 14:32:36 +0100 | [diff] [blame] | 82 | * @return pointer to the compilation options to allow modifying them with @ref scflags values. |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 83 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 84 | LIBYANG_API_DECL uint32_t *lysc_ctx_get_options(const struct lysc_ctx *ctx); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * @brief YANG schema compilation context getter for path being currently processed. |
| 88 | * @param[in] ctx YANG schema compilation context. |
| 89 | * @return path identifying the place in schema being currently processed by the schema compiler. |
| 90 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 91 | LIBYANG_API_DECL const char *lysc_ctx_get_path(const struct lysc_ctx *ctx); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 92 | |
| 93 | /** |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 94 | * @brief YANG schema compilation context getter for current module. |
| 95 | * @param[in] ctx YANG schema compilation context. |
| 96 | * @return current module. |
| 97 | */ |
Michal Vasko | a9f7909 | 2022-02-11 10:52:12 +0100 | [diff] [blame] | 98 | LIBYANG_API_DECL const struct lys_module *lysc_ctx_get_cur_mod(const struct lysc_ctx *ctx); |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * @brief YANG schema compilation context getter for currently processed module. |
| 102 | * @param[in] ctx YANG schema compilation context. |
| 103 | * @return Currently processed module. |
| 104 | */ |
Michal Vasko | a9f7909 | 2022-02-11 10:52:12 +0100 | [diff] [blame] | 105 | LIBYANG_API_DECL struct lysp_module *lysc_ctx_get_pmod(const struct lysc_ctx *ctx); |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 106 | |
| 107 | /** |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 108 | * @brief Compile substatements of an extension instance. |
| 109 | * |
| 110 | * Uses standard libyang schema compiler to transform YANG statements into the compiled schema structures. The plugins are |
| 111 | * supposed to use this function when the extension instance's substatements are supposed to be compiled in a standard way |
| 112 | * (or if just the @ref scflags are enough to modify the compilation process). |
| 113 | * |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 114 | * @param[in] ctx Compile context. |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 115 | * @param[in] ext_p Parsed representation of the extension instance being processed. |
| 116 | * @param[in,out] ext Compiled extension instance with the prepared ::lysc_ext_instance.substmts array, which will be updated |
| 117 | * by storing the compiled data. |
| 118 | * @return LY_SUCCESS on success. |
| 119 | * @return LY_EVALID if compilation of the substatements fails. |
| 120 | * @return LY_ENOT if the extension is disabled (by if-feature) and should be ignored. |
| 121 | */ |
Michal Vasko | a9f7909 | 2022-02-11 10:52:12 +0100 | [diff] [blame] | 122 | LIBYANG_API_DECL LY_ERR lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, |
| 123 | struct lysc_ext_instance *ext); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 124 | |
| 125 | /** |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 126 | * @brief Compile substatements of an extension instance but append all schema data nodes as augments. |
| 127 | * |
| 128 | * Similar to ::lys_compile_extension_instance(). |
| 129 | * |
| 130 | * @param[in] ctx Compile context. |
| 131 | * @param[in] ext_p Parsed representation of the extension instance being processed. |
| 132 | * @param[in,out] ext Compiled extension instance with the prepared ::lysc_ext_instance.substmts array, which will be updated |
| 133 | * by storing the compiled data except for schema data nodes. |
| 134 | * @param[in] aug_target Augment target node to append schema data nodes. |
| 135 | * @return LY_SUCCESS on success. |
| 136 | * @return LY_EVALID if compilation of the substatements fails. |
| 137 | * @return LY_ENOT if the extension is disabled (by if-feature) and should be ignored. |
| 138 | */ |
| 139 | LIBYANG_API_DECL LY_ERR lys_compile_extension_instance_augment(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, |
| 140 | struct lysc_ext_instance *ext, struct lysc_node *aug_target); |
| 141 | |
| 142 | /** |
| 143 | * @brief Find augment target in an extension. |
| 144 | * |
| 145 | * @param[in] ctx Compile context. |
| 146 | * @param[in] path Absolute-schema-nodeid representing the augment target. The first segment is expected to identify |
| 147 | * the specific extension instance. |
| 148 | * @param[out] aug_ext Optional augment target extension. |
| 149 | * @param[out] aug_target Augment target compiled schema node. |
| 150 | * @return LY_ERR value. |
| 151 | */ |
| 152 | LIBYANG_API_DECL LY_ERR lys_compile_extension_instance_find_augment_target(struct lysc_ctx *ctx, const char *path, |
| 153 | struct lysc_ext_instance **aug_ext, struct lysc_node **aug_target); |
| 154 | |
| 155 | /** |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 156 | * @brief Update path in the compile context, which is used for logging where the compilation failed. |
| 157 | * |
| 158 | * @param[in] ctx Compile context with the path. |
| 159 | * @param[in] parent_module Module of the current node's parent to check difference with the currently processed module (taken from @p ctx). |
| 160 | * @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 |
| 161 | * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used). |
| 162 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 163 | LIBYANG_API_DECL void lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * @brief Duplicate the compiled extension (definition) structure. |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 167 | * |
| 168 | * @param[in] orig The extension structure to duplicate. |
| 169 | * @return The duplicated structure to use. |
| 170 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 171 | LIBYANG_API_DECL struct lysc_ext *lysc_ext_dup(struct lysc_ext *orig); |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 172 | |
| 173 | /** @} extensions */ |
| 174 | |
| 175 | #ifdef __cplusplus |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #endif /* LY_PLUGINS_EXTS_COMPILE_H_ */ |