blob: 6ebe123a1b9473936b9031276c045dcc012a00e9 [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 Krejci3e6632f2021-03-22 22:08:21 +010019#include "plugins.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010020#include "tree_edit.h"
Radek Krejci0e59c312019-08-15 15:34:15 +020021#include "tree_schema.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022
Radek Krejci5f9a3672021-03-05 21:35:22 +010023#include "plugins_exts_compile.h"
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010024#include "plugins_exts_print.h"
Radek Krejci5f9a3672021-03-05 21:35:22 +010025
Radek Krejci535ea9f2020-05-29 16:01:05 +020026struct ly_ctx;
27struct lyd_node;
Radek Krejci77114102021-03-10 15:21:57 +010028struct lysc_ctx;
29struct lyspr_ctx;
Radek Krejci0e59c312019-08-15 15:34:15 +020030
Radek Krejcid7e8a622018-10-29 15:54:55 +010031#ifdef __cplusplus
32extern "C" {
33#endif
34
35/**
36 * @defgroup extensions YANG Extensions
37 *
38 * @{
39 */
40
41/**
Radek Krejci0935f412019-08-20 16:15:18 +020042 * @brief Extensions API version
43 */
44#define LYEXT_API_VERSION 1
45
46/**
47 * @brief Macro to store version of extension plugins API in the plugins.
48 * It is matched when the plugin is being loaded by libyang.
49 */
Radek Krejci1deb5be2020-08-26 16:43:36 +020050#define LYEXT_VERSION_CHECK uint32_t lyext_api_version = LYEXT_API_VERSION;
Radek Krejci0935f412019-08-20 16:15:18 +020051
52/**
Radek Krejcid6b76452019-09-03 17:03:03 +020053 * @brief Possible cardinalities of the YANG statements.
54 *
55 * Used in extensions plugins to define cardinalities of the extension instance substatements.
56 */
57enum ly_stmt_cardinality {
58 LY_STMT_CARD_OPT, /* 0..1 */
59 LY_STMT_CARD_MAND, /* 1 */
60 LY_STMT_CARD_SOME, /* 1..n */
61 LY_STMT_CARD_ANY /* 0..n */
62};
63
64/**
65 * @brief Description of the extension instance substatements.
66 *
Radek Krejci8678fa42020-08-18 16:07:28 +020067 * Provided by extensions plugins to libyang to be able to correctly compile the content of extension instances.
68 * 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 +020069 */
70struct lysc_ext_substmt {
71 enum ly_stmt stmt; /**< allowed substatement */
72 enum ly_stmt_cardinality cardinality; /**< cardinality of the substatement */
73 void *storage; /**< pointer to the storage of the compiled statement according to the specific
74 lysc_ext_substmt::stmt and lysc_ext_substmt::cardinality */
75};
76
77/**
Radek Krejci8678fa42020-08-18 16:07:28 +020078 * @brief Free the extension instance's data compiled with ::lys_compile_extension_instance().
Radek Krejci1b2eef82021-02-17 11:17:27 +010079 *
80 * @param[in] libyang context
81 * @param[in] substmts The sized array of extension instance's substatements. The whole array is freed except the storage
82 * places which are expected to be covered by the extension plugin.
Radek Krejci38d85362019-09-05 16:26:38 +020083 */
Radek Krejci1b2eef82021-02-17 11:17:27 +010084void lysc_extension_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts);
Radek Krejci38d85362019-09-05 16:26:38 +020085
86/**
Radek Krejci1b2eef82021-02-17 11:17:27 +010087 * @brief Get pointer to the storage of the specified substatement in the given extension instance.
88 *
89 * The function simplifies access into the ::lysc_ext_instance.substmts sized array.
90 *
91 * @param[in] ext Compiled extension instance to process.
92 * @param[in] substmt Extension substatement to search for.
93 * @param[out] instance_p Pointer where the storage of the @p substmt will be provided. The specific type returned depends
94 * on the @p substmt and can be found in the documentation of each ::ly_stmt value. Also note that some of the substatements
95 * (::lysc_node based or flags) can share the storage with other substatements. In case the pointer is NULL, still the return
96 * code can be used to at least know if the substatement is allowed for the extension.
97 * @param[out] cardinality_p Pointer to provide allowed cardinality of the substatements in the extension. Note that in some
98 * cases, the type of the storage depends also on the cardinality of the substatement.
99 * @return LY_SUCCESS if the @p substmt found.
100 * @return LY_ENOT in case the @p ext is not able to store (does not allow) the specified @p substmt.
101 */
102LY_ERR lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt,
103 void **instance_p, enum ly_stmt_cardinality *cardinality_p);
104
105/**
Radek Krejci0e59c312019-08-15 15:34:15 +0200106 * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared
107 * and only the extension specific data are supposed to be added (if any).
108 *
Radek Krejciadcf63d2021-02-09 10:21:18 +0100109 * The parsed generic statements can be processed by the callback on its own or the ::lys_compile_extension_instance
110 * function can be used to let the compilation to libyang following the standard rules for processing the YANG statements.
111 *
Radek Krejci0e59c312019-08-15 15:34:15 +0200112 * @param[in] cctx Current compile context.
113 * @param[in] p_ext Parsed extension instance data.
114 * @param[in,out] c_ext Prepared compiled extension instance structure where an addition, extension-specific, data are supposed to be placed
115 * for later use (data validation or use of external tool).
116 * @return LY_SUCCESS in case of success.
117 * @return LY_EVALID in case of non-conforming parsed data.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100118 * @return LY_ENOT in case the extension instance is not supported and should be removed.
Radek Krejci0e59c312019-08-15 15:34:15 +0200119 */
120typedef LY_ERR (*lyext_clb_compile)(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext);
121
122/**
Radek Krejciadcf63d2021-02-09 10:21:18 +0100123 * @brief Callback to free the extension specific data created by the ::lyext_clb_compile callback of the same extension plugin.
Radek Krejci0e59c312019-08-15 15:34:15 +0200124 *
Radek Krejci38d85362019-09-05 16:26:38 +0200125 * @param[in] ctx libyang context.
Radek Krejci0e59c312019-08-15 15:34:15 +0200126 * @param[in,out] ext Compiled extension structure where the data to free are placed.
127 */
Radek Krejci38d85362019-09-05 16:26:38 +0200128typedef void (*lyext_clb_free)(struct ly_ctx *ctx, struct lysc_ext_instance *ext);
Radek Krejci0e59c312019-08-15 15:34:15 +0200129
130/**
131 * @brief Callback to decide if data instance is valid according to the schema.
132 *
133 * The callback is used only for the extension instances placed in the following parent statements
Radek Krejci8678fa42020-08-18 16:07:28 +0200134 * (which is specified as ::lysc_ext_instance.parent_type):
Radek Krejci0e59c312019-08-15 15:34:15 +0200135 * - LYEXT_PAR_NODE - @p node is instance of the schema node where the extension instance was specified.
136 * - 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.
137 * - LYEXT_PAR_TYPE - @p node is instance of the schema node with the value of the type where the extension instance was specified.
138 * - LYEXT_PAR_TYPE_BIT - @p node is instance of the schema node with the value of the bit where the extension instance was specified.
139 * - LYEXT_PAR_TYPE_ENUM - @p node is instance of the schema node with the value of the enum where the extension instance was specified.
140 *
141 * @param[in] ext Extension instance to be checked.
142 * @param[in] node Data node, where the extension data are supposed to be placed.
143 *
144 * @return LY_SUCCESS on data validation success.
145 * @return LY_EVALID in case the validation fails.
146 */
147typedef LY_ERR (*lyext_clb_data_validation)(struct lysc_ext_instance *ext, struct lyd_node *node);
148
149/**
Radek Krejciadcf63d2021-02-09 10:21:18 +0100150 * @brief Callback to print the compiled extension instance's private data in the INFO format.
151 *
152 * @param[in] ctx YANG printer context to provide output handler and other information for printing.
153 * @param[in] ext The compiled extension instance, mainly to access the extensions.
154 * @param[in, out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed,
155 * 1 otherwise.
156 *
157 * @return LY_SUCCESS when everything was fine, other LY_ERR values in case of failure
158 */
Radek Krejcif8d7f9a2021-03-10 14:32:36 +0100159typedef LY_ERR (*lyext_clb_schema_printer)(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag);
Radek Krejciadcf63d2021-02-09 10:21:18 +0100160
161/**
Radek Krejci0e59c312019-08-15 15:34:15 +0200162 * @brief Extension plugin implementing various aspects of a YANG extension
163 */
Radek Krejcicc9e30f2021-03-29 12:45:08 +0200164struct lyplg_ext {
Radek Krejci0e59c312019-08-15 15:34:15 +0200165 const char *id; /**< Plugin identification (mainly for distinguish incompatible versions of the plugins for external tools) */
166 lyext_clb_compile compile; /**< Callback to compile extension instance from the parsed data */
167 lyext_clb_data_validation validate; /**< Callback to decide if data instance is valid according to the schema. */
Radek Krejciadcf63d2021-02-09 10:21:18 +0100168 lyext_clb_schema_printer sprinter; /**< Callback to print the compiled content (info format) of the extension instance */
169 /* lyext_clb_data_printer dprinter; ? */
Radek Krejcicc9e30f2021-03-29 12:45:08 +0200170 lyext_clb_free free; /**< Free the extension instance specific data created by ::lyplg_ext.compile callback */
Radek Krejci0e59c312019-08-15 15:34:15 +0200171};
172
Radek Krejci3e6632f2021-03-22 22:08:21 +0100173struct lyplg_ext_record {
174 /* plugin identification */
175 const char *module; /**< name of the module where the extension is defined */
176 const char *revision; /**< optional module revision - if not specified, the plugin applies to any revision,
177 which is not an optimal approach due to a possible future revisions of the module.
178 Instead, there should be defined multiple items in the plugins list, each with the
179 different revision, but all with the same pointer to the plugin functions. The
180 only valid use case for the NULL revision is the case the module has no revision. */
181 const char *name; /**< name of the extension */
182
183 /* runtime data */
184 struct lyplg_ext plugin; /**< data to utilize plugin implementation */
185};
186
Radek Krejci0935f412019-08-20 16:15:18 +0200187/**
188 * @brief Provide a log message from an extension plugin.
189 *
190 * @param[in] ext Compiled extension structure providing generic information about the extension/plugin causing the message.
191 * @param[in] level Log message level (error, warning, etc.)
192 * @param[in] err_no Error type code.
193 * @param[in] path Path relevant to the message.
194 * @param[in] format Format string to print.
195 */
196void lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...);
197
Radek Krejci0e59c312019-08-15 15:34:15 +0200198/** @} extensions */
Radek Krejcid7e8a622018-10-29 15:54:55 +0100199
200#ifdef __cplusplus
201}
202#endif
203
Radek Krejci0935f412019-08-20 16:15:18 +0200204#endif /* LY_PLUGINS_EXTS_H_ */