blob: 7491926e648087a45f31637dc631804ccde6f969 [file] [log] [blame]
Radek Krejcid7e8a622018-10-29 15:54:55 +01001/**
Radek Krejci0935f412019-08-20 16:15:18 +02002 * @file plugins_exts.h
Radek Krejcid7e8a622018-10-29 15:54:55 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang support for YANG extensions implementation.
5 *
Radek Krejci0935f412019-08-20 16:15:18 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
Radek Krejcid7e8a622018-10-29 15:54:55 +01007 *
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 Krejci0935f412019-08-20 16:15:18 +020015#ifndef LY_PLUGINS_EXTS_H_
16#define LY_PLUGINS_EXTS_H_
Radek Krejcid7e8a622018-10-29 15:54:55 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include "log.h"
Radek Krejci0e59c312019-08-15 15:34:15 +020019#include "tree_schema.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020020
21struct ly_ctx;
22struct lyd_node;
Radek Krejci47fab892020-11-05 17:02:41 +010023struct lysc_ctx;
Radek Krejci0e59c312019-08-15 15:34:15 +020024
Radek Krejcid7e8a622018-10-29 15:54:55 +010025#ifdef __cplusplus
26extern "C" {
27#endif
28
29/**
30 * @defgroup extensions YANG Extensions
31 *
32 * @{
33 */
34
35/**
Radek Krejci0935f412019-08-20 16:15:18 +020036 * @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 Krejci1deb5be2020-08-26 16:43:36 +020044#define LYEXT_VERSION_CHECK uint32_t lyext_api_version = LYEXT_API_VERSION;
Radek Krejci0935f412019-08-20 16:15:18 +020045
46/**
Radek Krejci0e59c312019-08-15 15:34:15 +020047 * @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 Krejcid7e8a622018-10-29 15:54:55 +010057 */
Radek Krejcid7e8a622018-10-29 15:54:55 +010058
59/**
Radek Krejcid6b76452019-09-03 17:03:03 +020060 * @brief Possible cardinalities of the YANG statements.
61 *
62 * Used in extensions plugins to define cardinalities of the extension instance substatements.
63 */
64enum 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 Krejci8678fa42020-08-18 16:07:28 +020074 * 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 Krejcid6b76452019-09-03 17:03:03 +020076 */
77struct 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/**
85 * @brief Compile substatements of an extension instance.
Radek Krejci38d85362019-09-05 16:26:38 +020086 * TODO
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010087 * @return LY_ENOT if the extension is disabled and should be ignored.
Radek Krejcid6b76452019-09-03 17:03:03 +020088 */
89LY_ERR lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts);
90
91/**
Radek Krejci8678fa42020-08-18 16:07:28 +020092 * @brief Free the extension instance's data compiled with ::lys_compile_extension_instance().
Radek Krejci1b2eef82021-02-17 11:17:27 +010093 *
94 * @param[in] libyang context
95 * @param[in] substmts The sized array of extension instance's substatements. The whole array is freed except the storage
96 * places which are expected to be covered by the extension plugin.
Radek Krejci38d85362019-09-05 16:26:38 +020097 */
Radek Krejci1b2eef82021-02-17 11:17:27 +010098void lysc_extension_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts);
Radek Krejci38d85362019-09-05 16:26:38 +020099
100/**
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100101 * @brief Duplicate the compiled extension (definition) structure.
Radek Krejci8678fa42020-08-18 16:07:28 +0200102 * TODO should this be in API? currently required by nacm_compile()
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100103 * Instead of duplicating memory, the reference counter in the @p orig is increased.
104 *
105 * @param[in] orig The extension structure to duplicate.
106 * @return The duplicated structure to use.
107 */
108struct lysc_ext *lysc_ext_dup(struct lysc_ext *orig);
109
110/**
Radek Krejci1b2eef82021-02-17 11:17:27 +0100111 * @brief Get pointer to the storage of the specified substatement in the given extension instance.
112 *
113 * The function simplifies access into the ::lysc_ext_instance.substmts sized array.
114 *
115 * @param[in] ext Compiled extension instance to process.
116 * @param[in] substmt Extension substatement to search for.
117 * @param[out] instance_p Pointer where the storage of the @p substmt will be provided. The specific type returned depends
118 * on the @p substmt and can be found in the documentation of each ::ly_stmt value. Also note that some of the substatements
119 * (::lysc_node based or flags) can share the storage with other substatements. In case the pointer is NULL, still the return
120 * code can be used to at least know if the substatement is allowed for the extension.
121 * @param[out] cardinality_p Pointer to provide allowed cardinality of the substatements in the extension. Note that in some
122 * cases, the type of the storage depends also on the cardinality of the substatement.
123 * @return LY_SUCCESS if the @p substmt found.
124 * @return LY_ENOT in case the @p ext is not able to store (does not allow) the specified @p substmt.
125 */
126LY_ERR lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt,
127 void **instance_p, enum ly_stmt_cardinality *cardinality_p);
128
129/**
Radek Krejci0e59c312019-08-15 15:34:15 +0200130 * @brief Update path in the compile context, which is used for logging where the compilation failed.
131 *
132 * @param[in] ctx Compile context with the path.
Radek Krejci8678fa42020-08-18 16:07:28 +0200133 * @param[in] parent Parent of the current node to check difference with the currently processed module (taken from @p ctx).
Radek Krejci0e59c312019-08-15 15:34:15 +0200134 * @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
135 * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
136 */
137void lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name);
138
139/** @} extensionscompile */
140
141/**
142 * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared
143 * and only the extension specific data are supposed to be added (if any).
144 *
145 * @param[in] cctx Current compile context.
146 * @param[in] p_ext Parsed extension instance data.
147 * @param[in,out] c_ext Prepared compiled extension instance structure where an addition, extension-specific, data are supposed to be placed
148 * for later use (data validation or use of external tool).
149 * @return LY_SUCCESS in case of success.
150 * @return LY_EVALID in case of non-conforming parsed data.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100151 * @return LY_ENOT in case the extension instance is not supported and should be removed.
Radek Krejci0e59c312019-08-15 15:34:15 +0200152 */
153typedef LY_ERR (*lyext_clb_compile)(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext);
154
155/**
156 * @brief Callback to free the extension specific data created by the lyext_clb_compile callback of the same extension plugin.
157 *
Radek Krejci38d85362019-09-05 16:26:38 +0200158 * @param[in] ctx libyang context.
Radek Krejci0e59c312019-08-15 15:34:15 +0200159 * @param[in,out] ext Compiled extension structure where the data to free are placed.
160 */
Radek Krejci38d85362019-09-05 16:26:38 +0200161typedef void (*lyext_clb_free)(struct ly_ctx *ctx, struct lysc_ext_instance *ext);
Radek Krejci0e59c312019-08-15 15:34:15 +0200162
163/**
164 * @brief Callback to decide if data instance is valid according to the schema.
165 *
166 * The callback is used only for the extension instances placed in the following parent statements
Radek Krejci8678fa42020-08-18 16:07:28 +0200167 * (which is specified as ::lysc_ext_instance.parent_type):
Radek Krejci0e59c312019-08-15 15:34:15 +0200168 * - LYEXT_PAR_NODE - @p node is instance of the schema node where the extension instance was specified.
169 * - 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.
170 * - LYEXT_PAR_TYPE - @p node is instance of the schema node with the value of the type where the extension instance was specified.
171 * - LYEXT_PAR_TYPE_BIT - @p node is instance of the schema node with the value of the bit where the extension instance was specified.
172 * - LYEXT_PAR_TYPE_ENUM - @p node is instance of the schema node with the value of the enum where the extension instance was specified.
173 *
174 * @param[in] ext Extension instance to be checked.
175 * @param[in] node Data node, where the extension data are supposed to be placed.
176 *
177 * @return LY_SUCCESS on data validation success.
178 * @return LY_EVALID in case the validation fails.
179 */
180typedef LY_ERR (*lyext_clb_data_validation)(struct lysc_ext_instance *ext, struct lyd_node *node);
181
182/**
183 * @brief Extension plugin implementing various aspects of a YANG extension
184 */
185struct lyext_plugin {
186 const char *id; /**< Plugin identification (mainly for distinguish incompatible versions of the plugins for external tools) */
187 lyext_clb_compile compile; /**< Callback to compile extension instance from the parsed data */
188 lyext_clb_data_validation validate; /**< Callback to decide if data instance is valid according to the schema. */
189 /* TODO printers? (schema/data) */
Radek Krejci8678fa42020-08-18 16:07:28 +0200190 lyext_clb_free free; /**< Free the extension instance specific data created by ::lyext_plugin.compile callback */
Radek Krejci0e59c312019-08-15 15:34:15 +0200191};
192
Radek Krejci0935f412019-08-20 16:15:18 +0200193struct lyext_plugins_list {
194 const char *module; /**< name of the module where the extension is defined */
195 const char *revision; /**< optional module revision - if not specified, the plugin applies to any revision,
196 which is not an optimal approach due to a possible future revisions of the module.
197 Instead, there should be defined multiple items in the plugins list, each with the
198 different revision, but all with the same pointer to the plugin extension. The
199 only valid use case for the NULL revision is the case the module has no revision. */
200 const char *name; /**< name of the extension */
201 struct lyext_plugin *plugin; /**< plugin for the extension */
202};
203
Radek Krejci0935f412019-08-20 16:15:18 +0200204/**
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 */
213void lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...);
214
Radek Krejci0e59c312019-08-15 15:34:15 +0200215/** @} extensions */
Radek Krejcid7e8a622018-10-29 15:54:55 +0100216
217#ifdef __cplusplus
218}
219#endif
220
Radek Krejci0935f412019-08-20 16:15:18 +0200221#endif /* LY_PLUGINS_EXTS_H_ */