blob: 6b8076b4e7d011dcfd5c1aca81af513238f1ff51 [file] [log] [blame]
Radek Krejcid7e8a622018-10-29 15:54:55 +01001/**
2 * @file extesnions.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang support for YANG extensions implementation.
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_EXTENSIONS_H_
16#define LY_EXTENSIONS_H_
17
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 Krejci0e59c312019-08-15 15:34:15 +020033 * @defgroup extensionscompile YANG Extensions - Compilation Helpers
34 * @ingroup extensions
35 * @brief Helper functions to compile (via lyext_clb_compile callback) statements inside the extension instance.
36 *
37 * 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
38 * to have a large API with functions which will be never used, we provide here just the functions which are evidently needed.
39 * If you, as an extension plugin author, need to make some of the compile functions available, please contact libyang maintainers
40 * via the GITHUB issue tracker.
41 *
42 * @{
Radek Krejcid7e8a622018-10-29 15:54:55 +010043 */
Radek Krejcid7e8a622018-10-29 15:54:55 +010044
45/**
Radek Krejci0e59c312019-08-15 15:34:15 +020046 * @brief internal context for compilation
Radek Krejcid7e8a622018-10-29 15:54:55 +010047 */
Radek Krejci0e59c312019-08-15 15:34:15 +020048struct lysc_ctx {
49 struct ly_ctx *ctx;
50 struct lys_module *mod;
51 struct lys_module *mod_def; /**< context module for the definitions of the nodes being currently
52 processed - groupings are supposed to be evaluated in place where
53 defined, but its content instances are supposed to be placed into
54 the target module (mod) */
55 struct ly_set groupings; /**< stack for groupings circular check */
56 struct ly_set unres; /**< to validate leafref's target and xpath of when/must */
57 struct ly_set dflts; /**< set of incomplete default values */
58 struct ly_set tpdf_chain;
59 uint16_t path_len;
60 int options; /**< various @ref scflags. */
61#define LYSC_CTX_BUFSIZE 4078
62 char path[LYSC_CTX_BUFSIZE];
63};
Radek Krejcid7e8a622018-10-29 15:54:55 +010064
Radek Krejci0e59c312019-08-15 15:34:15 +020065/**
66 * @brief Update path in the compile context, which is used for logging where the compilation failed.
67 *
68 * @param[in] ctx Compile context with the path.
69 * @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.
70 * @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
71 * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
72 */
73void lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name);
74
75/** @} extensionscompile */
76
77/**
78 * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared
79 * and only the extension specific data are supposed to be added (if any).
80 *
81 * @param[in] cctx Current compile context.
82 * @param[in] p_ext Parsed extension instance data.
83 * @param[in,out] c_ext Prepared compiled extension instance structure where an addition, extension-specific, data are supposed to be placed
84 * for later use (data validation or use of external tool).
85 * @return LY_SUCCESS in case of success.
86 * @return LY_EVALID in case of non-conforming parsed data.
87 */
88typedef LY_ERR (*lyext_clb_compile)(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext);
89
90/**
91 * @brief Callback to free the extension specific data created by the lyext_clb_compile callback of the same extension plugin.
92 *
93 * @param[in,out] ext Compiled extension structure where the data to free are placed.
94 */
95typedef void (*lyext_clb_free)(struct lysc_ext_instance *ext);
96
97/**
98 * @brief Callback to decide if data instance is valid according to the schema.
99 *
100 * The callback is used only for the extension instances placed in the following parent statements
101 * (which is specified as lysc_ext_instance::parent_type):
102 * - LYEXT_PAR_NODE - @p node is instance of the schema node where the extension instance was specified.
103 * - 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.
104 * - LYEXT_PAR_TYPE - @p node is instance of the schema node with the value of the type where the extension instance was specified.
105 * - LYEXT_PAR_TYPE_BIT - @p node is instance of the schema node with the value of the bit where the extension instance was specified.
106 * - LYEXT_PAR_TYPE_ENUM - @p node is instance of the schema node with the value of the enum where the extension instance was specified.
107 *
108 * @param[in] ext Extension instance to be checked.
109 * @param[in] node Data node, where the extension data are supposed to be placed.
110 *
111 * @return LY_SUCCESS on data validation success.
112 * @return LY_EVALID in case the validation fails.
113 */
114typedef LY_ERR (*lyext_clb_data_validation)(struct lysc_ext_instance *ext, struct lyd_node *node);
115
116/**
117 * @brief Extension plugin implementing various aspects of a YANG extension
118 */
119struct lyext_plugin {
120 const char *id; /**< Plugin identification (mainly for distinguish incompatible versions of the plugins for external tools) */
121 lyext_clb_compile compile; /**< Callback to compile extension instance from the parsed data */
122 lyext_clb_data_validation validate; /**< Callback to decide if data instance is valid according to the schema. */
123 /* TODO printers? (schema/data) */
124 lyext_clb_free free; /**< Free the extension instance specific data created by lyext_plugin::compile callback */
125};
126
127/** @} extensions */
Radek Krejcid7e8a622018-10-29 15:54:55 +0100128
129#ifdef __cplusplus
130}
131#endif
132
133#endif /* LY_TREE_SCHEMA_H_ */