blob: b9f00e524ff125f3eae40b0ff693baeef1011e8e [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>
Michal Vaskofbbea932022-06-07 11:00:55 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcid7e8a622018-10-29 15:54:55 +01005 * @brief libyang support for YANG extensions implementation.
6 *
Michal Vaskofbbea932022-06-07 11:00:55 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcid7e8a622018-10-29 15:54:55 +01008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci0935f412019-08-20 16:15:18 +020016#ifndef LY_PLUGINS_EXTS_H_
17#define LY_PLUGINS_EXTS_H_
Radek Krejcid7e8a622018-10-29 15:54:55 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "log.h"
Michal Vaskofbbea932022-06-07 11:00:55 +020020#include "parser_data.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010021#include "plugins.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010022#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010023#include "tree_edit.h"
Radek Krejci0e59c312019-08-15 15:34:15 +020024#include "tree_schema.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025
26struct ly_ctx;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010027struct ly_in;
Radek Krejci535ea9f2020-05-29 16:01:05 +020028struct lyd_node;
Michal Vasko193dacd2022-10-13 08:43:05 +020029struct lysc_ctx;
Radek Krejci0aa1f702021-04-01 16:16:19 +020030struct lysc_ext_substmt;
Michal Vaskob4750962022-10-06 15:33:35 +020031struct lysp_ctx;
Michal Vasko193dacd2022-10-13 08:43:05 +020032struct lyspr_ctx;
Radek Krejci0e59c312019-08-15 15:34:15 +020033
Radek Krejcid7e8a622018-10-29 15:54:55 +010034#ifdef __cplusplus
35extern "C" {
36#endif
37
38/**
Radek Krejci75104122021-04-01 15:37:45 +020039 * @page howtoPluginsExtensions Extension Plugins
40 *
41 * Note that the part of the libyang API here is available only by including a separated `<libyang/plugins_exts.h>` header
42 * file. Also note that the extension plugins API is versioned separately from libyang itself, so backward incompatible
43 * changes can come even without changing libyang major version.
44 *
45 * YANG extensions are very complex. Usually only its description specifies how it is supposed to behave, what are the
46 * allowed substatements, their cardinality or if the standard YANG statements placed inside the extension differs somehow
aPiecekb0445f22021-06-24 11:34:07 +020047 * in their meaning or behavior. libyang provides the Extension plugins API to implement such extensions and add its support
Radek Krejci75104122021-04-01 15:37:45 +020048 * into libyang itself. However we tried our best, the API is not (and it cannot be) so universal and complete to cover all
49 * possibilities. There are definitely use cases which cannot be simply implemented only with this API.
50 *
51 * libyang implements 3 important extensions: [NACM](https://tools.ietf.org/html/rfc8341), [Metadata](@ref howtoDataMetadata)
52 * and [yang-data](@ref howtoDataYangdata). Despite the core implementation in all three cases is done via extension plugin
53 * API, also other parts of the libyang code had to be extended to cover complete scope of the extensions.
54 *
55 * We believe, that the API is capable to allow implementation of very wide range of YANG extensions. However, if you see
56 * limitations for the particular YANG extension, don't hesitate to contact the project developers to discuss all the
57 * options, including updating the API.
58 *
59 * The plugin's functionality is provided to libyang via a set of callbacks specified as an array of ::lyplg_ext_record
60 * structures using the ::LYPLG_EXTENSIONS macro.
61 *
62 * The most important ::lyplg_ext.compile callback is responsible for processing the parsed extension instance. In this
63 * phase, the callback must validate all the substatements, their values or placement of the extension instance itself.
64 * If needed, the processed data can be stored in some form into the compiled schema representation of the extension
65 * instance. To make the compilation process as easy as possible, libyang provides several
66 * [helper functions](@ref pluginsExtensionsCompile) to handle the schema compilation context and to compile standard YANG
67 * statements in the same way the libyang does it internally.
68 *
69 * The data validation callback ::lyplg_ext.validate is used for additional validation of a data nodes that contains the
70 * connected extension instance directly (as a substatement) or indirectly in case of terminal nodes via their type (no
71 * matter if the extension instance is placed directly in the leaf's/leaf-list's type or in the type of the referenced
72 * typedef).
73 *
74 * The ::lyplg_ext.sprinter callback implement printing the compiled extension instance data when the schema (module) is
75 * being printed in the ::LYS_OUT_YANG_COMPILED (info) format. As for compile callback, there are also
76 * [helper functions](@ref pluginsExtensionsPrint) to access printer's context and to print standard YANG statements
77 * placed in the extension instance by libyang itself.
78 *
79 * The last callback, ::lyplg_ext.free, is supposed to free all the data allocated by the ::lyplg_ext.compile callback.
80 * To free the data created by helper function ::lys_compile_extension_instance(), the plugin can used
81 * ::lyplg_ext_instance_substatements_free().
82 *
83 * The plugin information contains also the plugin identifier (::lyplg_type.id). This string can serve to identify the
84 * specific plugin responsible to storing data value. In case the user can recognize the id string, it can access the
85 * plugin specific data with the appropriate knowledge of its structure.
86 *
Michal Vasko193dacd2022-10-13 08:43:05 +020087 * Logging information from an extension plugin is possible via ::lyplg_extp_log() and ::yplg_extc_log() functions.
Radek Krejci75104122021-04-01 15:37:45 +020088 */
89
90/**
91 * @defgroup pluginsExtensions Plugins: Extensions
92 *
93 * Structures and functions to for libyang plugins implementing specific YANG extensions defined in YANG modules. For more
94 * information, see @ref howtoPluginsTypes.
95 *
96 * This part of libyang API is available by including `<libyang/plugins_ext.h>` header file.
Radek Krejcid7e8a622018-10-29 15:54:55 +010097 *
98 * @{
99 */
100
101/**
Radek Krejci0935f412019-08-20 16:15:18 +0200102 * @brief Extensions API version
103 */
Michal Vasko0b50f6b2022-10-05 15:07:55 +0200104#define LYPLG_EXT_API_VERSION 6
Radek Krejci0935f412019-08-20 16:15:18 +0200105
106/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200107 * @brief Mask for an operation statement.
Michal Vaskob4750962022-10-06 15:33:35 +0200108 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200109 * This mask matches action and RPC.
Michal Vaskob4750962022-10-06 15:33:35 +0200110 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200111#define LY_STMT_OP_MASK (LY_STMT_ACTION | LY_STMT_RPC)
Michal Vaskob4750962022-10-06 15:33:35 +0200112
113/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200114 * @brief Mask for a data node statement.
Michal Vaskob4750962022-10-06 15:33:35 +0200115 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200116 * This mask matches anydata, anyxml, case, choice, container, leaf, leaf-list, and list.
Michal Vaskob4750962022-10-06 15:33:35 +0200117 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200118#define LY_STMT_DATA_NODE_MASK (LY_STMT_ANYDATA | LY_STMT_ANYXML | LY_STMT_CASE | LY_STMT_CHOICE | LY_STMT_CONTAINER |\
119 LY_STMT_LEAF | LY_STMT_LEAF_LIST | LY_STMT_LIST)
Michal Vaskob4750962022-10-06 15:33:35 +0200120
121/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200122 * @brief Mask for a node statement.
Michal Vaskob4750962022-10-06 15:33:35 +0200123 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200124 * This mask matches notification, input, output, action, RPC, anydata, anyxml, augment, case, choice, container,
125 * grouping, leaf, leaf-list, list, and uses.
Michal Vaskob4750962022-10-06 15:33:35 +0200126 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200127#define LY_STMT_NODE_MASK 0xFFFF
Michal Vaskob4750962022-10-06 15:33:35 +0200128
129/**
130 * @brief List of YANG statements
Michal Vasko193dacd2022-10-13 08:43:05 +0200131 *
132 * Their description mentions what types are stored for each statement. Note that extension instance storage
133 * always stores a pointer to the type, not the type itself.
Michal Vaskob4750962022-10-06 15:33:35 +0200134 */
135enum ly_stmt {
136 LY_STMT_NONE = 0,
137
Michal Vasko193dacd2022-10-13 08:43:05 +0200138 LY_STMT_NOTIFICATION = 0x0001, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_notif *`
139 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_notif *` */
140 LY_STMT_INPUT = 0x0002, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action_inout *`
141 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action_inout *` */
142 LY_STMT_OUTPUT = 0x0004, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action_inout *`
143 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action_inout *` */
144 LY_STMT_ACTION = 0x0008, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action *`
145 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action *` */
146 LY_STMT_RPC = 0x0010, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_action *`
147 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_action *` */
148 LY_STMT_ANYDATA = 0x0020, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_anydata *`
149 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_anydata *` */
150 LY_STMT_ANYXML = 0x0040, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_anydata *`
151 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_anydata *` */
152 LY_STMT_AUGMENT = 0x0080, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_augment *`
153 ::lysc_ext_substmt.storage - not compiled
154 ::lysc_ext_instance.parent - `struct lysc_node *` */
155 LY_STMT_CASE = 0x0100, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_case *`
156 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_case *` */
157 LY_STMT_CHOICE = 0x0200, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_choice *`
158 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_choice *` */
159 LY_STMT_CONTAINER = 0x0400, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_container *`
160 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_container *` */
161 LY_STMT_GROUPING = 0x0800, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_grp *`
162 ::lysc_ext_substmt.storage - not compiled
163 ::lysc_ext_instance.parent - `struct lysc_node *` */
164 LY_STMT_LEAF = 0x1000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_leaf *`
165 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_leaf *` */
166 LY_STMT_LEAF_LIST = 0x2000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_leaflist *`
167 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_leaflist *` */
168 LY_STMT_LIST = 0x4000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_list *`
169 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node_list *` */
170 LY_STMT_USES = 0x8000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_node_uses *`
171 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_node *` */
Michal Vaskob4750962022-10-06 15:33:35 +0200172
Michal Vasko193dacd2022-10-13 08:43:05 +0200173 LY_STMT_ARGUMENT = 0x10000, /**< ::lysp_ext_substmt.storage - `const char *`
174 ::lysp_ext_instance.parent - `struct lysp_ext *`
175 ::lysc_ext_substmt.storage - `const char *`
176 ::lysc_ext_instance.parent - `struct lysc_ext *` */
177 LY_STMT_BASE = 0x20000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char **`[]
178 ::lysc_ext_substmt.storage - not compiled
179 ::lysc_ext_instance.parent - `struct lysc_ident *` */
180 LY_STMT_BELONGS_TO = 0x30000, /**< ::lysp_ext_substmt.storage - `const char *`
181 ::lysp_ext_instance.parent - `struct lysp_submodule *`
182 ::lysc_ext_substmt.storage - not compiled
183 ::lysc_ext_instance.parent - `struct lysc_module *` */
184 LY_STMT_BIT = 0x40000, /**< ::lysp_ext_substmt.storage - `struct lysp_type_enum *`[]
185 ::lysp_ext_instance.parent - `struct lysp_type_enum *`
186 ::lysc_ext_substmt.storage - `struct lysc_type_bitenum_item *`[]
187 ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */
188 LY_STMT_CONFIG = 0x50000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint16_t *`
189 ::lysc_ext_substmt.storage - `uint16_t *`
190 ::lysc_ext_instance.parent - `struct lysc_node *` */
191 LY_STMT_CONTACT = 0x60000, /**< ::lysp_ext_substmt.storage - `const char *`
192 ::lysp_ext_instance.parent - `struct lysp_(sub)module *`
193 ::lysc_ext_substmt.storage - `const char *`
194 ::lysc_ext_instance.parent - `struct lysc_module *` */
195 LY_STMT_DEFAULT = 0x70000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_qname *`
196 ::lysc_ext_substmt.storage - not compiled
197 ::lysc_ext_instance.parent - `struct lysc_node *`, `struct lysc_type *` (typedef) */
198 LY_STMT_DESCRIPTION = 0x80000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *`
199 ::lysc_ext_substmt.storage - `const char *`
200 ::lysc_ext_instance.parent - compiled parent statement */
201 LY_STMT_DEVIATE = 0x90000, /**< ::lysp_ext_substmt.storage - `struct lysp_deviate *`[]
202 ::lysp_ext_instance.parent - `struct lysp_deviate *`
203 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */
204 LY_STMT_DEVIATION = 0xA0000, /**< ::lysp_ext_substmt.storage - `struct lysp_deviation *`[]
205 ::lysp_ext_instance.parent - `struct lysp_deviation *`
206 ::lysc_ext_substmt.storage - not compiled
207 ::lysc_ext_instance.parent - `struct lysc_node *` */
208 LY_STMT_ENUM = 0xB0000, /**< ::lysp_ext_substmt.storage - `struct lysp_type_enum *`[]
209 ::lysp_ext_instance.parent - `struct lysp_type_enum *`
210 ::lysc_ext_substmt.storage - `struct lysc_type_bitenum_item *`[]
211 ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */
212 LY_STMT_ERROR_APP_TAG = 0xC0000, /**< ::lysp_ext_substmt.storage - `const char *`
213 ::lysp_ext_instance.parent - `struct lysp_restr *`
214 ::lysc_ext_substmt.storage - `const char *`
215 ::lysc_ext_instance.parent - compiled restriction structure */
216 LY_STMT_ERROR_MESSAGE = 0xD0000, /**< ::lysp_ext_substmt.storage - `const char *`
217 ::lysp_ext_instance.parent - `struct lysp_restr *`
218 ::lysc_ext_substmt.storage - `const char *`
219 ::lysc_ext_instance.parent - compiled restriction structure */
220 LY_STMT_EXTENSION = 0xE0000, /**< ::lysp_ext_substmt.storage - `struct lysp_ext *`[]
221 ::lysp_ext_instance.parent - `struct lysp_ext *`
222 ::lysc_ext_substmt.storage - not compiled explicitly
223 ::lysc_ext_instance.parent - `struct lysc_ext *` */
224 LY_STMT_EXTENSION_INSTANCE = 0xF0000, /**< ::lysp_ext_substmt.storage - `struct lysp_ext_instance *`[]
225 ::lysc_ext_substmt.storage - `struct lysc_ext_instance *`[] */
226 LY_STMT_FEATURE = 0x100000, /**< ::lysp_ext_substmt.storage - `struct lysp_feature *`[]
227 ::lysp_ext_instance.parent - `struct lysp_feature *`
228 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */
229 LY_STMT_FRACTION_DIGITS = 0x110000, /**< ::lysp_ext_substmt.storage - `uint8_t *`
230 ::lysp_ext_instance.parent - `struct lysp_type *`
231 ::lysc_ext_substmt.storage - `uint8_t *`
232 ::lysc_ext_instance.parent - `struct lysc_type *` */
233 LY_STMT_IDENTITY = 0x120000, /**< ::lysp_ext_substmt.storage - `struct lysp_ident *`[]
234 ::lysp_ext_instance.parent - `struct lysp_ident *`
235 ::lysc_ext_substmt.storage - `struct lysc_ident *`[]
236 ::lysc_ext_instance.parent - `struct lysc_ident *` */
237 LY_STMT_IF_FEATURE = 0x130000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_qname *`[]
238 ::lysc_ext_substmt.storage - no storage, evaluated when compiled
239 ::lysc_ext_instance.parent - compiled parent statement */
240 LY_STMT_IMPORT = 0x140000, /**< ::lysp_ext_substmt.storage - `struct lysp_import *`[]
241 ::lysp_ext_instance.parent - `struct lysp_import *`
242 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */
243 LY_STMT_INCLUDE = 0x150000, /**< ::lysp_ext_substmt.storage - `struct lysp_include *`[]
244 ::lysp_ext_instance.parent - `struct lysp_include *`
245 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */
246 LY_STMT_KEY = 0x160000, /**< ::lysp_ext_substmt.storage - `const char *`
247 ::lysp_ext_instance.parent - `struct lysp_node_list *`
248 ::lysc_ext_substmt.storage - `const char *`
249 ::lysc_ext_instance.parent - `struct lysc_node_list *` */
250 LY_STMT_LENGTH = 0x170000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_restr *`
251 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_range *` */
252 LY_STMT_MANDATORY = 0x180000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint16_t *`
253 ::lysc_ext_substmt.storage - `uint16_t *`
254 ::lysc_ext_instance.parent - `struct lysc_node *` */
255 LY_STMT_MAX_ELEMENTS = 0x190000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint32_t *`
256 ::lysc_ext_substmt.storage - `uint32_t *`
257 ::lysc_ext_instance.parent - `struct lysc_node_list *` */
258 LY_STMT_MIN_ELEMENTS = 0x1A0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint32_t *`
259 ::lysc_ext_substmt.storage - `uint32_t *`
260 ::lysc_ext_instance.parent - `struct lysc_node_list *` */
261 LY_STMT_MODIFIER = 0x1B0000, /**< ::lysp_ext_substmt.storage - `const char *`
262 ::lysp_ext_instance.parent - `struct lysp_restr *`
263 ::lysc_ext_substmt.storage - `const char *`
264 ::lysc_ext_instance.parent - `struct lysc_pattern *` */
265 LY_STMT_MODULE = 0x1C0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_module *`
266 ::lysc_ext_substmt.storage - not compiled
267 ::lysc_ext_instance.parent - `struct lysc_module *` */
268 LY_STMT_MUST = 0x1D0000, /**< ::lysp_ext_substmt.storage - `struct lysp_restr *`[]
269 ::lysp_ext_instance.parent - `struct lysp_restr *`
270 ::lysc_ext_substmt.storage - `struct lysc_must *`[]
271 ::lysc_ext_instance.parent - `struct lysc_must *` */
272 LY_STMT_NAMESPACE = 0x1E0000, /**< ::lysp_ext_substmt.storage - `const char *`
273 ::lysp_ext_instance.parent - `struct lysp_module *`
274 ::lysc_ext_substmt.storage - `const char *`
275 ::lysc_ext_instance.parent - `struct lysc_module *` */
276 LY_STMT_ORDERED_BY = 0x1F0000, /**< ::lysp_ext_substmt.storage - `uint16_t *`
277 ::lysp_ext_instance.parent - `struct lysp_node *`
278 ::lysc_ext_substmt.storage - `uint16_t *`
279 ::lysc_ext_instance.parent - `struct lysc_node *` */
280 LY_STMT_ORGANIZATION = 0x200000, /**< ::lysp_ext_substmt.storage - `const char *`
281 ::lysp_ext_instance.parent - `struct lysp_(sub)module *`
282 ::lysc_ext_substmt.storage - `const char *`
283 ::lysc_ext_instance.parent - `struct lysc_module *` */
284 LY_STMT_PATH = 0x210000, /**< ::lysp_ext_substmt.storage - `struct lyxp_expr *`
285 ::lysp_ext_instance.parent - `struct lysp_type *`
286 ::lysc_ext_substmt.storage - not compiled
287 ::lysc_ext_instance.parent - `struct lysc_type *` */
288 LY_STMT_PATTERN = 0x220000, /**< ::lysp_ext_substmt.storage - `struct lysp_restr *`[]
289 ::lysp_ext_instance.parent - `struct lysp_restr *`
290 ::lysc_ext_substmt.storage - `struct lysc_pattern **`[]
291 ::lysc_ext_instance.parent - `struct lysc_pattern *` */
292 LY_STMT_POSITION = 0x230000, /**< ::lysp_ext_substmt.storage - `int64_t *`
293 ::lysp_ext_instance.parent - `struct lysp_type_enum *`
294 ::lysc_ext_substmt.storage - `int64_t *`
295 ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */
296 LY_STMT_PREFIX = 0x240000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *`
297 ::lysc_ext_substmt.storage - not compiled
298 ::lysc_ext_instance.parent - `struct lysc_module *` */
299 LY_STMT_PRESENCE = 0x250000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *`
300 ::lysc_ext_substmt.storage - `const char *`
301 ::lysc_ext_instance.parent - `struct lysc_node_container *` */
302 LY_STMT_RANGE = 0x260000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_restr *`
303 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_range *` */
304 LY_STMT_REFERENCE = 0x270000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *`
305 ::lysc_ext_substmt.storage - `const char *`
306 ::lysc_ext_instance.parent - compiled parent statement */
307 LY_STMT_REFINE = 0x280000, /**< ::lysp_ext_substmt.storage - `struct lysp_refine *`[]
308 ::lysp_ext_instance.parent - `struct lysp_refine *`
309 ::lysc_ext_substmt.storage - not compiled
310 ::lysc_ext_instance.parent - `struct lysc_node *` */
311 LY_STMT_REQUIRE_INSTANCE = 0x290000, /**< ::lysp_ext_substmt.storage - `uint8_t *`
312 ::lysp_ext_instance.parent - `struct lysp_type *`
313 ::lysc_ext_substmt.storage - `uint8_t *`
314 ::lysc_ext_instance.parent - `struct lysc_type *` */
315 LY_STMT_REVISION = 0x2A0000, /**< ::lysp_ext_substmt.storage - `struct lysp_revision *`[]
316 ::lysp_ext_instance.parent - `struct lysp_revision *`
317 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */
318 LY_STMT_REVISION_DATE = 0x2B0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *`
319 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - not compiled */
320 LY_STMT_STATUS = 0x2C0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `uint16_t *`
321 ::lysc_ext_substmt.storage - `uint16_t *`
322 ::lysc_ext_instance.parent - compiled parent statement */
323 LY_STMT_SUBMODULE = 0x2D0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_submodule *`
324 ::lysc_ext_substmt.storage - not compiled
325 ::lysc_ext_instance.parent - `struct lysc_module *` */
326 LY_STMT_TYPE = 0x2E0000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_type *`
327 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_type *` */
328 LY_STMT_TYPEDEF = 0x2F0000, /**< ::lysp_ext_substmt.storage - `struct lysp_tpdf *`[]
329 ::lysp_ext_instance.parent - `struct lysp_tpdf *`
330 ::lysc_ext_substmt.storage - not compiled
331 ::lysc_ext_instance.parent - `struct lysc_type *` */
332 LY_STMT_UNIQUE = 0x300000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_qname *`[]
333 ::lysc_ext_substmt.storage - not compiled
334 ::lysc_ext_instance.parent - `struct lysc_node_list *` */
335 LY_STMT_UNITS = 0x310000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `const char *`
336 ::lysc_ext_substmt.storage - `const char *`
337 ::lysc_ext_instance.parent - `struct lysc_node *`, `struct lysc_type *` (typedef) */
338 LY_STMT_VALUE = 0x320000, /**< ::lysp_ext_substmt.storage - `int64_t *`
339 ::lysp_ext_instance.parent - `struct lysp_type_enum *`
340 ::lysc_ext_substmt.storage - `int64_t *`
341 ::lysc_ext_instance.parent - `struct lysc_type_bitenum_item *` */
342 LY_STMT_WHEN = 0x330000, /**< ::lysp_ext_substmt.storage and ::lysp_ext_instance.parent - `struct lysp_when *`
343 ::lysc_ext_substmt.storage and ::lysc_ext_instance.parent - `struct lysc_when *` */
344 LY_STMT_YANG_VERSION = 0x340000, /**< ::lysp_ext_substmt.storage - `uint8_t *`
345 ::lysp_ext_instance.parent - `struct lysp_(sub)module *`
346 ::lysc_ext_substmt.storage - not compiled
347 ::lysc_ext_instance.parent - `struct lysc_module *` */
348 LY_STMT_YIN_ELEMENT = 0x350000, /**< ::lysp_ext_substmt.storage - `uint16_t *`
349 ::lysp_ext_instance.parent - `struct lysp_ext *`
350 ::lysc_ext_substmt.storage - not compiled
351 ::lysc_ext_instance.parent - `struct lysc_ext *` */
Michal Vaskob4750962022-10-06 15:33:35 +0200352
353 /* separated from the list of statements
354 * the following tokens are part of the syntax and parsers have to work
355 * with them, but they are not a standard YANG statements
356 */
357 LY_STMT_SYNTAX_SEMICOLON,
358 LY_STMT_SYNTAX_LEFT_BRACE,
359 LY_STMT_SYNTAX_RIGHT_BRACE,
360
361 /*
362 * YIN-specific tokens, still they are part of the syntax, but not the standard statements
363 */
364 LY_STMT_ARG_TEXT,
365 LY_STMT_ARG_VALUE
366};
367
368/**
Michal Vasko9c3556a2022-10-06 16:08:47 +0200369 * @brief Structure representing a generic parsed YANG substatement in an extension instance.
Michal Vaskob4750962022-10-06 15:33:35 +0200370 */
371struct lysp_stmt {
372 const char *stmt; /**< identifier of the statement */
373 const char *arg; /**< statement's argument */
374 LY_VALUE_FORMAT format; /**< prefix format of the identifier/argument (::LY_VALUE_XML is YIN format) */
375 void *prefix_data; /**< Format-specific data for prefix resolution (see ly_resolve_prefix()) */
376
377 struct lysp_stmt *next; /**< link to the next statement */
378 struct lysp_stmt *child; /**< list of the statement's substatements (linked list) */
379 uint16_t flags; /**< statement flags, can be set to LYS_YIN_ATTR */
380 enum ly_stmt kw; /**< numeric respresentation of the stmt value */
381};
382
383/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200384 * @brief Structure representing a parsed known YANG substatement in an extension instance.
385 */
386struct lysp_ext_substmt {
387 enum ly_stmt stmt; /**< parsed substatement */
388 void *storage; /**< pointer to the parsed storage of the statement according to the specific
389 lys_ext_substmt::stmt */
390};
391
392/**
393 * @brief YANG extension parsed instance.
Michal Vaskob4750962022-10-06 15:33:35 +0200394 */
395struct lysp_ext_instance {
396 const char *name; /**< extension identifier, including possible prefix */
397 const char *argument; /**< optional value of the extension's argument */
398 LY_VALUE_FORMAT format; /**< prefix format of the extension name/argument (::LY_VALUE_XML is YIN format) */
Michal Vasko193dacd2022-10-13 08:43:05 +0200399 void *prefix_data; /**< format-specific data for prefix resolution (see ly_resolve_prefix()) */
400 struct lysp_ext *def; /**< pointer to the extension definition */
Michal Vaskob4750962022-10-06 15:33:35 +0200401
Michal Vasko193dacd2022-10-13 08:43:05 +0200402 void *parent; /**< pointer to the parent statement holding the extension instance(s), use
403 ::lysp_ext_instance#parent_stmt to access the value/structure */
404 enum ly_stmt parent_stmt; /**< type of the parent statement */
405 LY_ARRAY_COUNT_TYPE parent_stmt_index; /**< index of the stamenet in case the parent does not point to the parent
406 statement directly and it is an array */
407 uint16_t flags; /**< ::LYS_INTERNAL value (@ref snodeflags) */
Michal Vaskob4750962022-10-06 15:33:35 +0200408
Michal Vasko193dacd2022-10-13 08:43:05 +0200409 const struct lyplg_ext_record *record; /**< extension definition plugin record, if any */
410 struct lysp_ext_substmt *substmts; /**< list of supported known YANG statements with the pointer to their
411 parsed data ([sized array](@ref sizedarrays)) */
412 void *parsed; /**< private plugin parsed data */
413 struct lysp_stmt *child; /**< list of generic (unknown) YANG statements */
Michal Vaskob4750962022-10-06 15:33:35 +0200414};
415
416/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200417 * @brief Structure representing a compiled known YANG substatement in an extension instance.
Michal Vaskob4750962022-10-06 15:33:35 +0200418 */
419struct lysc_ext_substmt {
Michal Vasko193dacd2022-10-13 08:43:05 +0200420 enum ly_stmt stmt; /**< compiled substatement */
421 void *storage; /**< pointer to the compiled storage of the statement according to the specific
422 lys_ext_substmt::stmt */
Michal Vaskob4750962022-10-06 15:33:35 +0200423};
424
425/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200426 * @brief YANG extension compiled instance.
Michal Vaskob4750962022-10-06 15:33:35 +0200427 */
428struct lysc_ext_instance {
Michal Vasko193dacd2022-10-13 08:43:05 +0200429 struct lysc_ext *def; /**< pointer to the extension definition */
430 const char *argument; /**< optional value of the extension's argument */
431 struct lys_module *module; /**< module where the extension instantiated is defined */
432 struct lysc_ext_instance *exts; /**< list of the extension instances ([sized array](@ref sizedarrays)) */
Michal Vaskob4750962022-10-06 15:33:35 +0200433
Michal Vasko193dacd2022-10-13 08:43:05 +0200434 void *parent; /**< pointer to the parent element holding the extension instance(s), use
435 ::lysc_ext_instance#parent_stmt to access the value/structure */
436 enum ly_stmt parent_stmt; /**< type of the parent statement */
437 LY_ARRAY_COUNT_TYPE parent_stmt_index; /**< index of the stamenet in case the parent does not point to the parent
438 statement directly and it is an array */
439
440 struct lysc_ext_substmt *substmts; /**< list of supported known YANG statements with the pointer to their
441 compiled data ([sized array](@ref sizedarrays)) */
442 void *compiled; /**< private plugin compiled data */
Michal Vaskob4750962022-10-06 15:33:35 +0200443};
444
445/**
Radek Krejcia6f61e72021-03-24 21:00:19 +0100446 * @brief Macro to define plugin information in external plugins
447 *
448 * Use as follows:
449 * LYPLG_EXTENSIONS = {{<filled information of ::lyplg_ext_record>}, ..., {0}};
Radek Krejci0935f412019-08-20 16:15:18 +0200450 */
Radek Krejcia6f61e72021-03-24 21:00:19 +0100451#define LYPLG_EXTENSIONS \
452 uint32_t plugins_extensions_apiver__ = LYPLG_EXT_API_VERSION; \
453 const struct lyplg_ext_record plugins_extensions__[]
Radek Krejci0935f412019-08-20 16:15:18 +0200454
Michal Vasko193dacd2022-10-13 08:43:05 +0200455/*
456 * parse
457 */
458
459/**
460 * @brief Callback for parsing extension instance substatements.
461 *
462 * All known YANG substatements can easily be parsed using ::lys_parse_extension_instance.
463 *
464 * @param[in] pctx Parse context.
465 * @param[in,out] ext Parsed extension instance data.
466 * @return LY_SUCCESS on success.
467 * @return LY_ENOT if the extension instance is not supported and should be removed.
468 * @return LY_ERR error on error.
469 */
470typedef LY_ERR (*lyplg_ext_parse_clb)(struct lysp_ctx *pctx, struct lysp_ext_instance *ext);
471
472/**
473 * @brief Log a message from an extension plugin using the parsed extension instance.
474 *
475 * @param[in] pctx Parse context to use.
476 * @param[in] ext Parsed extensiopn instance.
477 * @param[in] level Log message level (error, warning, etc.)
478 * @param[in] err_no Error type code.
479 * @param[in] format Format string to print.
480 * @param[in] ... Format variable parameters.
481 */
482LIBYANG_API_DECL void lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext,
483 LY_LOG_LEVEL level, LY_ERR err_no, const char *format, ...);
484
485/**
486 * @brief Get current parsed module from a parse context.
487 *
488 * @param[in] pctx Parse context.
489 * @return Current (local) parse mod.
490 */
491LIBYANG_API_DECL const struct lysp_module *lyplg_ext_parse_get_cur_pmod(const struct lysp_ctx *pctx);
492
493/**
494 * @brief Parse substatements of an extension instance.
495 *
496 * Uses standard libyang schema compiler to transform YANG statements into the parsed schema structures. The plugins are
497 * supposed to use this function when the extension instance's substatements can be parsed in a standard way.
498 *
499 * @param[in] pctx Parse context.
500 * @param[in,out] ext Parsed extension instance with the prepared ::lysp_ext_instance.substmts array, which will be
501 * updated by storing the parsed data.
502 * @return LY_SUCCESS on success.
503 * @return LY_ERR error on error.
504 */
505LIBYANG_API_DECL LY_ERR lyplg_ext_parse_extension_instance(struct lysp_ctx *pctx, struct lysp_ext_instance *ext);
506
507/*
508 * compile
509 */
510
511/**
512 * @defgroup scflags Schema compile flags
513 *
514 * Flags to modify schema compilation process and change the way how the particular statements are being compiled. *
515 * @{
516 */
517#define LYS_COMPILE_GROUPING 0x01 /**< Compiling (validation) of a non-instantiated grouping.
518 In this case not all the restrictions are checked since they can
519 be valid only in the real placement of the grouping. This is
520 the case of any restriction that needs to look out of the statements
521 themselves, since the context is not known. */
522#define LYS_COMPILE_DISABLED 0x02 /**< Compiling a disabled subtree (by its if-features). Meaning
523 it will be removed at the end of compilation and should not be
524 added to any unres sets. */
525#define LYS_COMPILE_NO_CONFIG 0x04 /**< ignore config statements, neither inherit config value */
526#define LYS_COMPILE_NO_DISABLED 0x08 /**< ignore if-feature statements */
527
528#define LYS_COMPILE_RPC_INPUT (LYS_IS_INPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action input */
529#define LYS_COMPILE_RPC_OUTPUT (LYS_IS_OUTPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action output */
530#define LYS_COMPILE_NOTIFICATION (LYS_IS_NOTIF | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of Notification */
531
532/** @} scflags */
Radek Krejci38d85362019-09-05 16:26:38 +0200533
534/**
Radek Krejci0e59c312019-08-15 15:34:15 +0200535 * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared
536 * and only the extension specific data are supposed to be added (if any).
537 *
Radek Krejciadcf63d2021-02-09 10:21:18 +0100538 * The parsed generic statements can be processed by the callback on its own or the ::lys_compile_extension_instance
539 * function can be used to let the compilation to libyang following the standard rules for processing the YANG statements.
540 *
Radek Krejci0e59c312019-08-15 15:34:15 +0200541 * @param[in] cctx Current compile context.
Michal Vasko193dacd2022-10-13 08:43:05 +0200542 * @param[in] extp Parsed extension instance data.
543 * @param[in,out] ext Prepared compiled extension instance structure where an addition, extension-specific, data are
Michal Vaskoddd76592022-01-17 13:34:48 +0100544 * supposed to be placed for later use (data validation or use of external tool).
Radek Krejci0e59c312019-08-15 15:34:15 +0200545 * @return LY_SUCCESS in case of success.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100546 * @return LY_ENOT in case the extension instance is not supported and should be removed.
Michal Vasko193dacd2022-10-13 08:43:05 +0200547 * @return LY_ERR error on error.
Radek Krejci0e59c312019-08-15 15:34:15 +0200548 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200549typedef LY_ERR (*lyplg_ext_compile_clb)(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp,
550 struct lysc_ext_instance *ext);
551
552/**
553 * @brief Log a message from an extension plugin using the compiled extension instance.
554 *
555 * @param[in] cctx Optional compile context to generate the path from.
556 * @param[in] ext Compiled extension instance.
557 * @param[in] level Log message level (error, warning, etc.)
558 * @param[in] err_no Error type code.
559 * @param[in] format Format string to print.
560 */
561LIBYANG_API_DECL void lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext,
562 LY_LOG_LEVEL level, LY_ERR err_no, const char *format, ...);
563
564/**
565 * @brief Log a message from an extension plugin using the compiled extension instance with an explicit error path.
566 *
567 * @param[in] path Log error path to use.
568 * @param[in] ext Compiled extension instance.
569 * @param[in] level Log message level (error, warning, etc.)
570 * @param[in] err_no Error type code.
571 * @param[in] format Format string to print.
572 */
573LIBYANG_API_DECL void lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext,
574 LY_LOG_LEVEL level, LY_ERR err_no, const char *format, ...);
575
576/**
577 * @brief YANG schema compilation context getter for libyang context.
578 *
579 * @param[in] ctx YANG schema compilation context.
580 * @return libyang context connected with the compilation context.
581 */
582LIBYANG_API_DECL struct ly_ctx *lyplg_ext_compile_get_ctx(const struct lysc_ctx *ctx);
583
584/**
585 * @brief YANG schema compilation context getter for compilation options.
586 *
587 * @param[in] ctx YANG schema compilation context.
588 * @return pointer to the compilation options to allow modifying them with @ref scflags values.
589 */
590LIBYANG_API_DECL uint32_t *lyplg_ext_compile_get_options(const struct lysc_ctx *ctx);
591
592/**
593 * @brief YANG schema compilation context getter for current module.
594 *
595 * @param[in] ctx YANG schema compilation context.
596 * @return current module.
597 */
598LIBYANG_API_DECL const struct lys_module *lyplg_ext_compile_get_cur_mod(const struct lysc_ctx *ctx);
599
600/**
601 * @brief YANG schema compilation context getter for currently processed module.
602 *
603 * @param[in] ctx YANG schema compilation context.
604 * @return Currently processed module.
605 */
606LIBYANG_API_DECL struct lysp_module *lyplg_ext_compile_get_pmod(const struct lysc_ctx *ctx);
607
608/**
609 * @brief Compile substatements of an extension instance.
610 *
611 * Uses standard libyang schema compiler to transform YANG statements into the compiled schema structures. The plugins are
612 * supposed to use this function when the extension instance's substatements are supposed to be compiled in a standard way
613 * (or if just the @ref scflags are enough to modify the compilation process).
614 *
615 * @param[in] ctx Compile context.
616 * @param[in] extp Parsed representation of the extension instance being processed.
617 * @param[in,out] ext Compiled extension instance with the prepared ::lysc_ext_instance.substmts array, which will be updated
618 * by storing the compiled data.
619 * @return LY_SUCCESS on success.
620 * @return LY_EVALID if compilation of the substatements fails.
621 * @return LY_ENOT if the extension is disabled (by if-feature) and should be ignored.
622 */
623LIBYANG_API_DECL LY_ERR lyplg_ext_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *extp,
624 struct lysc_ext_instance *ext);
625
626/**
627 * @brief Compile substatements of an extension instance but append all schema data nodes as augments.
628 *
629 * Similar to ::lys_compile_extension_instance().
630 *
631 * @param[in] ctx Compile context.
632 * @param[in] extp Parsed representation of the extension instance being processed.
633 * @param[in,out] ext Compiled extension instance with the prepared ::lysc_ext_instance.substmts array, which will be updated
634 * by storing the compiled data except for schema data nodes.
635 * @param[in] aug_target Augment target node to append schema data nodes.
636 * @return LY_SUCCESS on success.
637 * @return LY_EVALID if compilation of the substatements fails.
638 * @return LY_ENOT if the extension is disabled (by if-feature) and should be ignored.
639 */
640LIBYANG_API_DECL LY_ERR lyplg_ext_compile_extension_instance_augment(struct lysc_ctx *ctx,
641 const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext, struct lysc_node *aug_target);
642
643/**
644 * @brief Find augment target in an extension.
645 *
646 * @param[in] ctx Compile context.
647 * @param[in] path Absolute-schema-nodeid representing the augment target. The first segment is expected to identify
648 * the specific extension instance.
649 * @param[out] aug_ext Optional augment target extension.
650 * @param[out] aug_target Augment target compiled schema node.
651 * @return LY_ERR value.
652 */
653LIBYANG_API_DECL LY_ERR lys_compile_extension_instance_find_augment_target(struct lysc_ctx *ctx, const char *path,
654 struct lysc_ext_instance **aug_ext, struct lysc_node **aug_target);
655
656/**
657 * @brief Update path in the compile context, which is used for logging where the compilation failed.
658 *
659 * @param[in] ctx Compile context with the path.
660 * @param[in] parent_module Module of the current node's parent to check difference with the currently processed module (taken from @p ctx).
661 * @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
662 * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
663 */
664LIBYANG_API_DECL void lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name);
665
666/**
667 * @brief Duplicate the compiled extension (definition) structure.
668 *
669 * @param[in] orig The extension structure to duplicate.
670 * @return The duplicated structure to use.
671 */
672LIBYANG_API_DECL struct lysc_ext *lysc_ext_dup(struct lysc_ext *orig);
673
674/*
Michal Vasko941e0562022-10-18 10:35:00 +0200675 * sprinter info
Michal Vasko193dacd2022-10-13 08:43:05 +0200676 */
Radek Krejci0e59c312019-08-15 15:34:15 +0200677
678/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100679 * @brief Callback to print the compiled extension instance's private data in the INFO format.
680 *
681 * @param[in] ctx YANG printer context to provide output handler and other information for printing.
682 * @param[in] ext The compiled extension instance, mainly to access the extensions.
683 * @param[in,out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed,
684 * 1 otherwise.
685 * @return LY_SUCCESS when everything was fine, other LY_ERR values in case of failure
686 */
Michal Vasko941e0562022-10-18 10:35:00 +0200687typedef LY_ERR (*lyplg_ext_sprinter_info_clb)(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag);
Michal Vaskoddd76592022-01-17 13:34:48 +0100688
689/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200690 * @brief YANG printer context getter for output handler.
Radek Krejci0e59c312019-08-15 15:34:15 +0200691 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200692 * @param[in] ctx YANG printer context.
693 * @return Output handler where the data are being printed. Note that the address of the handler pointer in the context is
694 * returned to allow to modify the handler.
Radek Krejci0e59c312019-08-15 15:34:15 +0200695 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200696LIBYANG_API_DECL struct ly_out **lyplg_ext_print_get_out(const struct lyspr_ctx *ctx);
697
698/**
699 * @brief YANG printer context getter for printer options.
700 *
701 * @param[in] ctx YANG printer context.
702 * @return pointer to the printer options to allow modifying them with @ref schemaprinterflags values.
703 */
704LIBYANG_API_DECL uint32_t *lyplg_ext_print_get_options(const struct lyspr_ctx *ctx);
705
706/**
707 * @brief YANG printer context getter for printer indentation level.
708 *
709 * @param[in] ctx YANG printer context.
710 * @return pointer to the printer's indentation level to allow modifying its value.
711 */
712LIBYANG_API_DECL uint16_t *lyplg_ext_print_get_level(const struct lyspr_ctx *ctx);
713
714/**
Michal Vasko941e0562022-10-18 10:35:00 +0200715 * @brief Print substatements of an extension instance in info format (compiled YANG).
Michal Vasko193dacd2022-10-13 08:43:05 +0200716 *
Michal Vasko941e0562022-10-18 10:35:00 +0200717 * Generic function to access YANG printer functions from the extension plugins (::lyplg_ext_sprinter_info_clb).
Michal Vasko193dacd2022-10-13 08:43:05 +0200718 *
719 * @param[in] ctx YANG printer context to provide output handler and other information for printing.
720 * @param[in] ext The compiled extension instance to access the extensions and substatements data.
721 * @param[in,out] flag Flag to be shared with the caller regarding the opening brackets - 0 if the '{' not yet printed,
722 * 1 otherwise.
723 */
Michal Vasko941e0562022-10-18 10:35:00 +0200724LIBYANG_API_DECL void lyplg_ext_print_info_extension_instance(struct lyspr_ctx *ctx, const struct lysc_ext_instance *ext,
Michal Vasko193dacd2022-10-13 08:43:05 +0200725 ly_bool *flag);
726
727/*
728 * data node
729 */
Radek Krejci0e59c312019-08-15 15:34:15 +0200730
731/**
Michal Vasko135719f2022-08-25 12:18:17 +0200732 * @brief Callback called for all data nodes connected to the extension instance.
733 *
734 * Can be used for additional data node validation. Is called only after the whole data tree is created and standard
Michal Vaskoeba23112022-08-26 08:35:41 +0200735 * validation succeeds. Not called when parsing data and ::LYD_PARSE_ONLY is used.
Michal Vasko135719f2022-08-25 12:18:17 +0200736 *
737 * @param[in] ext Compiled extension instance.
738 * @param[in] node Data node to process.
Michal Vaskoeba23112022-08-26 08:35:41 +0200739 * @param[in] validate_options Options used for the validation phase, see @ref datavalidationoptions.
Michal Vasko135719f2022-08-25 12:18:17 +0200740 * @return LY_SUCCESS on success.
741 * @return LY_ERR on error.
742 */
Michal Vaskoeba23112022-08-26 08:35:41 +0200743typedef LY_ERR (*lyplg_ext_data_node_clb)(struct lysc_ext_instance *ext, struct lyd_node *node, uint32_t validate_options);
Michal Vasko135719f2022-08-25 12:18:17 +0200744
Michal Vasko193dacd2022-10-13 08:43:05 +0200745/*
746 * snode
747 */
748
Michal Vasko135719f2022-08-25 12:18:17 +0200749/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200750 * @brief Callback for getting a schema node for a new YANG instance data described by an extension instance.
751 * Needed only if the extension instance supports some nested standard YANG data.
Radek Krejci0e59c312019-08-15 15:34:15 +0200752 *
Michal Vaskoddd76592022-01-17 13:34:48 +0100753 * @param[in] ext Compiled extension instance.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200754 * @param[in] parent Parsed parent data node. Set if @p sparent is NULL.
755 * @param[in] sparent Schema parent node. Set if @p parent is NULL.
756 * @param[in] prefix Element prefix, if any.
757 * @param[in] prefix_len Length of @p prefix.
758 * @param[in] format Format of @p prefix.
759 * @param[in] prefix_data Format-specific prefix data.
760 * @param[in] name Element name.
761 * @param[in] name_len Length of @p name.
762 * @param[out] snode Schema node to use for parsing the node.
Michal Vaskoddd76592022-01-17 13:34:48 +0100763 * @return LY_SUCCESS on success.
764 * @return LY_ENOT if the data are not described by @p ext.
765 * @return LY_ERR on error.
Radek Krejci0e59c312019-08-15 15:34:15 +0200766 */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200767typedef LY_ERR (*lyplg_ext_data_snode_clb)(struct lysc_ext_instance *ext, const struct lyd_node *parent,
768 const struct lysc_node *sparent, const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data,
769 const char *name, size_t name_len, const struct lysc_node **snode);
Radek Krejci0e59c312019-08-15 15:34:15 +0200770
Michal Vasko193dacd2022-10-13 08:43:05 +0200771/*
772 * validate
773 */
774
Radek Krejci0e59c312019-08-15 15:34:15 +0200775/**
Michal Vaskoddd76592022-01-17 13:34:48 +0100776 * @brief Callback for validating parsed YANG instance data described by an extension instance.
Radek Krejciadcf63d2021-02-09 10:21:18 +0100777 *
Michal Vaskoddd76592022-01-17 13:34:48 +0100778 * This callback is used only for nested data definition (with a standard YANG schema parent).
Radek Krejciadcf63d2021-02-09 10:21:18 +0100779 *
Michal Vaskoddd76592022-01-17 13:34:48 +0100780 * @param[in] ext Compiled extension instance.
Michal Vasko9af7cd42022-04-05 12:26:09 +0200781 * @param[in] sibling First sibling with schema node returned by ::lyplg_ext_data_snode_clb.
Michal Vaskofbbea932022-06-07 11:00:55 +0200782 * @param[in] dep_tree Tree to be used for validating references from the operation subtree, if operation.
783 * @param[in] data_type Validated data type, can be ::LYD_TYPE_DATA_YANG, ::LYD_TYPE_RPC_YANG, ::LYD_TYPE_NOTIF_YANG,
784 * or ::LYD_TYPE_REPLY_YANG.
Michal Vaskoddd76592022-01-17 13:34:48 +0100785 * @param[in] val_opts Validation options, see @ref datavalidationoptions.
Michal Vaskof4c6f002022-04-01 09:12:22 +0200786 * @param[out] diff Optional diff with any changes made by the validation.
Michal Vaskoddd76592022-01-17 13:34:48 +0100787 * @return LY_SUCCESS on success.
788 * @return LY_ERR on error.
Radek Krejciadcf63d2021-02-09 10:21:18 +0100789 */
Michal Vaskofbbea932022-06-07 11:00:55 +0200790typedef LY_ERR (*lyplg_ext_data_validate_clb)(struct lysc_ext_instance *ext, struct lyd_node *sibling,
791 const struct lyd_node *dep_tree, enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff);
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100792
Michal Vasko193dacd2022-10-13 08:43:05 +0200793/*
794 * parse free
795 */
796
797/**
798 * @brief Callback to free the extension-specific data created by its parsing.
799 *
800 * @param[in] ctx libyang context.
801 * @param[in,out] ext Parsed extension structure to free.
802 */
803typedef void (*lyplg_ext_parse_free_clb)(const struct ly_ctx *ctx, struct lysp_ext_instance *ext);
804
805/**
806 * @brief Free the extension instance's data parsed with ::lys_parse_extension_instance().
807 *
808 * @param[in] ctx libyang context
809 * @param[in] substmts Extension instance substatements to free.
810 */
811LIBYANG_API_DECL void lyplg_ext_pfree_instance_substatements(const struct ly_ctx *ctx, struct lysp_ext_substmt *substmts);
812
813/*
814 * compile free
815 */
816
817/**
818 * @brief Callback to free the extension-specific data created by its compilation.
819 *
820 * @param[in] ctx libyang context.
821 * @param[in,out] ext Compiled extension structure to free.
822 */
823typedef void (*lyplg_ext_compile_free_clb)(const struct ly_ctx *ctx, struct lysc_ext_instance *ext);
824
825/**
826 * @brief Free the extension instance's data compiled with ::lys_compile_extension_instance().
827 *
828 * @param[in] ctx libyang context
829 * @param[in] substmts Extension instance substatements to free.
830 */
831LIBYANG_API_DECL void lyplg_ext_cfree_instance_substatements(const struct ly_ctx *ctx, struct lysc_ext_substmt *substmts);
832
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100833/**
Radek Krejci0e59c312019-08-15 15:34:15 +0200834 * @brief Extension plugin implementing various aspects of a YANG extension
835 */
Radek Krejcicc9e30f2021-03-29 12:45:08 +0200836struct lyplg_ext {
Michal Vaskoddd76592022-01-17 13:34:48 +0100837 const char *id; /**< plugin identification (mainly for distinguish incompatible versions
838 of the plugins for external tools) */
Michal Vasko9c3556a2022-10-06 16:08:47 +0200839 lyplg_ext_parse_clb parse; /**< callback to parse the extension instance substatements */
Michal Vaskoddd76592022-01-17 13:34:48 +0100840 lyplg_ext_compile_clb compile; /**< callback to compile extension instance from the parsed data */
Michal Vasko941e0562022-10-18 10:35:00 +0200841 lyplg_ext_sprinter_info_clb printer_info; /**< callback to print the compiled content (info format) of the extension
842 instance */
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100843
Michal Vasko135719f2022-08-25 12:18:17 +0200844 lyplg_ext_data_node_clb node; /**< callback to validate most relevant data instance for the extension
845 instance */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200846 lyplg_ext_data_snode_clb snode; /**< callback to get schema node for nested YANG data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100847 lyplg_ext_data_validate_clb validate; /**< callback to validate parsed data instances according to the extension
848 definition */
Michal Vasko193dacd2022-10-13 08:43:05 +0200849
850 lyplg_ext_parse_free_clb pfree; /**< free the extension-specific data created by its parsing */
851 lyplg_ext_compile_free_clb cfree; /**< free the extension-specific data created by its compilation */
Radek Krejci0e59c312019-08-15 15:34:15 +0200852};
853
Radek Krejci3e6632f2021-03-22 22:08:21 +0100854struct lyplg_ext_record {
855 /* plugin identification */
856 const char *module; /**< name of the module where the extension is defined */
857 const char *revision; /**< optional module revision - if not specified, the plugin applies to any revision,
858 which is not an optimal approach due to a possible future revisions of the module.
859 Instead, there should be defined multiple items in the plugins list, each with the
860 different revision, but all with the same pointer to the plugin functions. The
861 only valid use case for the NULL revision is the case the module has no revision. */
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100862 const char *name; /**< YANG name of the extension */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100863
864 /* runtime data */
865 struct lyplg_ext plugin; /**< data to utilize plugin implementation */
866};
867
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100868/**
Michal Vaskob4750962022-10-06 15:33:35 +0200869 * @brief Stringify statement identifier.
870 *
871 * @param[in] stmt The statement identifier to stringify.
872 * @return Constant string representation of the given @p stmt.
873 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200874LIBYANG_API_DECL const char *lyplg_ext_stmt2str(enum ly_stmt stmt);
Michal Vaskob4750962022-10-06 15:33:35 +0200875
876/**
Michal Vaskob4750962022-10-06 15:33:35 +0200877 * @brief Convert nodetype to statement identifier
878 *
879 * @param[in] nodetype Nodetype to convert.
880 * @return Statement identifier representing the given @p nodetype.
881 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200882LIBYANG_API_DECL enum ly_stmt lyplg_ext_nodetype2stmt(uint16_t nodetype);
Michal Vaskob4750962022-10-06 15:33:35 +0200883
884/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200885 * @brief Get compiled ext instance storage for a specific statement.
Michal Vaskob4750962022-10-06 15:33:35 +0200886 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200887 * @param[in] ext Compiled ext instance.
888 * @param[in] stmt Compiled statement. Can be a mask when the first match is returned, it is expected the storage is
889 * the same for all the masked statements.
890 * @param[out] storage Compiled ext instance substatement storage, NULL if was not compiled.
891 * @return LY_SUCCESS on success.
892 * @return LY_ENOT if the substatement is not supported.
Michal Vaskob4750962022-10-06 15:33:35 +0200893 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200894LIBYANG_API_DECL LY_ERR lyplg_ext_get_storage(const struct lysc_ext_instance *ext, int stmt, const void **storage);
895
896/**
897 * @brief Get parsed ext instance storage for a specific statement.
898 *
899 * @param[in] ext Compiled ext instance.
900 * @param[in] stmt Parsed statement. Can be a mask when the first match is returned, it is expected the storage is
901 * the same for all the masked statements.
902 * @param[out] storage Parsed ext instance substatement storage, NULL if was not parsed.
903 * @return LY_SUCCESS on success.
904 * @return LY_ENOT if the substatement is not supported.
905 */
906LIBYANG_API_DECL LY_ERR lyplg_ext_parsed_get_storage(const struct lysc_ext_instance *ext, int stmt, const void **storage);
Michal Vaskob4750962022-10-06 15:33:35 +0200907
908/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100909 * @brief Get specific run-time extension instance data from a callback set by ::ly_ctx_set_ext_data_clb().
910 *
911 * @param[in] ctx Context with the callback.
912 * @param[in] ext Compiled extension instance.
913 * @param[out] ext_data Provided extension instance data.
914 * @param[out] ext_data_free Whether the extension instance should free @p ext_data or not.
915 * @return LY_SUCCESS on success.
916 * @return LY_ERR on error.
917 */
Michal Vaskoddd76592022-01-17 13:34:48 +0100918LIBYANG_API_DECL LY_ERR lyplg_ext_get_data(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, void **ext_data,
919 ly_bool *ext_data_free);
920
921/**
922 * @brief Insert extension instance data into a parent.
923 *
924 * @param[in] parent Parent node to insert into.
925 * @param[in] first First top-level sibling node to insert.
926 * @return LY_SUCCESS on success.
927 * @return LY_ERR error on error.
928 */
Michal Vasko193dacd2022-10-13 08:43:05 +0200929LIBYANG_API_DECL LY_ERR lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first);
Radek Krejci0935f412019-08-20 16:15:18 +0200930
ekinzie0ab8b302022-10-10 03:03:57 -0400931/**
932 * @brief Expand parent-reference xpath expressions
933 *
Michal Vasko193dacd2022-10-13 08:43:05 +0200934 * @param[in] ext Context allocated for extension.
935 * @param[out] refs Set of schema node matching parent-reference XPaths.
ekinzie0ab8b302022-10-10 03:03:57 -0400936 * @return LY_ERR value.
937 */
938LIBYANG_API_DECL LY_ERR lyplg_ext_schema_mount_get_parent_ref(const struct lysc_ext_instance *ext, struct ly_set **refs);
939
940/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200941 * @brief Allocate a new context for a particular instance of the yangmnt:mount-point extension.
942 * Caller is responsible for destroying the resulting context.
ekinzie0ab8b302022-10-10 03:03:57 -0400943 *
944 * @param[in] ext Compiled extension instance.
945 * @param[out] ctx A context with modules loaded from the list found in
946 * the extension data.
947 * @return LY_ERR value.
948 */
949LIBYANG_API_DECL LY_ERR lyplg_ext_schema_mount_create_context(const struct lysc_ext_instance *ext, struct ly_ctx **ctx);
950
Radek Krejci75104122021-04-01 15:37:45 +0200951/** @} pluginsExtensions */
Radek Krejcid7e8a622018-10-29 15:54:55 +0100952
953#ifdef __cplusplus
954}
955#endif
956
Radek Krejci0935f412019-08-20 16:15:18 +0200957#endif /* LY_PLUGINS_EXTS_H_ */