blob: 6684d6a46595a7f0d0a51bdde63ca6d40e70c0e0 [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 Krejci0e59c312019-08-15 15:34:15 +020018#include "set.h"
19#include "tree_schema.h"
20
Radek Krejcid7e8a622018-10-29 15:54:55 +010021#ifdef __cplusplus
22extern "C" {
23#endif
24
Radek Krejci0e59c312019-08-15 15:34:15 +020025
Radek Krejcid7e8a622018-10-29 15:54:55 +010026/**
27 * @defgroup extensions YANG Extensions
28 *
29 * @{
30 */
31
32/**
Radek Krejci0935f412019-08-20 16:15:18 +020033 * @brief Extensions API version
34 */
35#define LYEXT_API_VERSION 1
36
37/**
38 * @brief Macro to store version of extension plugins API in the plugins.
39 * It is matched when the plugin is being loaded by libyang.
40 */
41#define LYEXT_VERSION_CHECK int lyext_api_version = LYEXT_API_VERSION;
42
43/**
Radek Krejci0e59c312019-08-15 15:34:15 +020044 * @defgroup extensionscompile YANG Extensions - Compilation Helpers
45 * @ingroup extensions
46 * @brief Helper functions to compile (via lyext_clb_compile callback) statements inside the extension instance.
47 *
48 * 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
49 * to have a large API with functions which will be never used, we provide here just the functions which are evidently needed.
50 * If you, as an extension plugin author, need to make some of the compile functions available, please contact libyang maintainers
51 * via the GITHUB issue tracker.
52 *
53 * @{
Radek Krejcid7e8a622018-10-29 15:54:55 +010054 */
Radek Krejcid7e8a622018-10-29 15:54:55 +010055
56/**
Radek Krejci0e59c312019-08-15 15:34:15 +020057 * @brief internal context for compilation
Radek Krejcid7e8a622018-10-29 15:54:55 +010058 */
Radek Krejci0e59c312019-08-15 15:34:15 +020059struct lysc_ctx {
60 struct ly_ctx *ctx;
61 struct lys_module *mod;
62 struct lys_module *mod_def; /**< context module for the definitions of the nodes being currently
63 processed - groupings are supposed to be evaluated in place where
64 defined, but its content instances are supposed to be placed into
65 the target module (mod) */
66 struct ly_set groupings; /**< stack for groupings circular check */
67 struct ly_set unres; /**< to validate leafref's target and xpath of when/must */
68 struct ly_set dflts; /**< set of incomplete default values */
69 struct ly_set tpdf_chain;
70 uint16_t path_len;
71 int options; /**< various @ref scflags. */
72#define LYSC_CTX_BUFSIZE 4078
73 char path[LYSC_CTX_BUFSIZE];
74};
Radek Krejcid7e8a622018-10-29 15:54:55 +010075
Radek Krejci0e59c312019-08-15 15:34:15 +020076/**
77 * @brief Update path in the compile context, which is used for logging where the compilation failed.
78 *
79 * @param[in] ctx Compile context with the path.
80 * @param[in] parent Parent of the current node to check difference of the node's module. The current module is taken from lysc_ctx::mod.
81 * @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
82 * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
83 */
84void lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name);
85
86/** @} extensionscompile */
87
88/**
89 * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared
90 * and only the extension specific data are supposed to be added (if any).
91 *
92 * @param[in] cctx Current compile context.
93 * @param[in] p_ext Parsed extension instance data.
94 * @param[in,out] c_ext Prepared compiled extension instance structure where an addition, extension-specific, data are supposed to be placed
95 * for later use (data validation or use of external tool).
96 * @return LY_SUCCESS in case of success.
97 * @return LY_EVALID in case of non-conforming parsed data.
98 */
99typedef LY_ERR (*lyext_clb_compile)(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext);
100
101/**
102 * @brief Callback to free the extension specific data created by the lyext_clb_compile callback of the same extension plugin.
103 *
104 * @param[in,out] ext Compiled extension structure where the data to free are placed.
105 */
106typedef void (*lyext_clb_free)(struct lysc_ext_instance *ext);
107
108/**
109 * @brief Callback to decide if data instance is valid according to the schema.
110 *
111 * The callback is used only for the extension instances placed in the following parent statements
112 * (which is specified as lysc_ext_instance::parent_type):
113 * - LYEXT_PAR_NODE - @p node is instance of the schema node where the extension instance was specified.
114 * - 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.
115 * - LYEXT_PAR_TYPE - @p node is instance of the schema node with the value of the type where the extension instance was specified.
116 * - LYEXT_PAR_TYPE_BIT - @p node is instance of the schema node with the value of the bit where the extension instance was specified.
117 * - LYEXT_PAR_TYPE_ENUM - @p node is instance of the schema node with the value of the enum where the extension instance was specified.
118 *
119 * @param[in] ext Extension instance to be checked.
120 * @param[in] node Data node, where the extension data are supposed to be placed.
121 *
122 * @return LY_SUCCESS on data validation success.
123 * @return LY_EVALID in case the validation fails.
124 */
125typedef LY_ERR (*lyext_clb_data_validation)(struct lysc_ext_instance *ext, struct lyd_node *node);
126
127/**
128 * @brief Extension plugin implementing various aspects of a YANG extension
129 */
130struct lyext_plugin {
131 const char *id; /**< Plugin identification (mainly for distinguish incompatible versions of the plugins for external tools) */
132 lyext_clb_compile compile; /**< Callback to compile extension instance from the parsed data */
133 lyext_clb_data_validation validate; /**< Callback to decide if data instance is valid according to the schema. */
134 /* TODO printers? (schema/data) */
135 lyext_clb_free free; /**< Free the extension instance specific data created by lyext_plugin::compile callback */
136};
137
Radek Krejci0935f412019-08-20 16:15:18 +0200138struct lyext_plugins_list {
139 const char *module; /**< name of the module where the extension is defined */
140 const char *revision; /**< optional module revision - if not specified, the plugin applies to any revision,
141 which is not an optimal approach due to a possible future revisions of the module.
142 Instead, there should be defined multiple items in the plugins list, each with the
143 different revision, but all with the same pointer to the plugin extension. The
144 only valid use case for the NULL revision is the case the module has no revision. */
145 const char *name; /**< name of the extension */
146 struct lyext_plugin *plugin; /**< plugin for the extension */
147};
148
149
150/**
151 * @brief Provide a log message from an extension plugin.
152 *
153 * @param[in] ext Compiled extension structure providing generic information about the extension/plugin causing the message.
154 * @param[in] level Log message level (error, warning, etc.)
155 * @param[in] err_no Error type code.
156 * @param[in] path Path relevant to the message.
157 * @param[in] format Format string to print.
158 */
159void lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...);
160
Radek Krejci0e59c312019-08-15 15:34:15 +0200161/** @} extensions */
Radek Krejcid7e8a622018-10-29 15:54:55 +0100162
163#ifdef __cplusplus
164}
165#endif
166
Radek Krejci0935f412019-08-20 16:15:18 +0200167#endif /* LY_PLUGINS_EXTS_H_ */