blob: 408433d7fcfb0545dc7e0489740073a9484ec596 [file] [log] [blame]
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001/**
2 * @file parser_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Internal structures and functions for libyang parsers
5 *
6 * Copyright (c) 2020 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_PARSER_INTERNAL_H_
16#define LY_PARSER_INTERNAL_H_
17
Michal Vaskoc8a230d2020-08-14 12:17:10 +020018#include "plugins_types.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020019#include "tree_schema_internal.h"
20
Radek Krejci1798aae2020-07-14 13:26:06 +020021struct lyd_ctx;
Michal Vaskoafac7822020-10-20 14:22:26 +020022struct ly_in;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020023
Radek Krejci1798aae2020-07-14 13:26:06 +020024/**
25 * @brief Callback for lyd_ctx to free the structure
26 *
27 * @param[in] ctx Data parser context to free.
28 */
29typedef void (*lyd_ctx_free_clb)(struct lyd_ctx *ctx);
30
31/**
32 * @brief Internal (common) context for YANG data parsers.
33 */
34struct lyd_ctx {
35 uint32_t parse_options; /**< various @ref dataparseroptions. */
36 uint32_t validate_options; /**< various @ref datavalidationoptions. */
37 uint32_t int_opts; /**< internal data parser options */
38 uint32_t path_len; /**< used bytes in the path buffer */
39#define LYD_PARSER_BUFSIZE 4078
40 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vasko32711382020-12-03 14:14:31 +010041 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
42 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
43 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci1798aae2020-07-14 13:26:06 +020044 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
45
46 /* callbacks */
Radek Krejci0bff0b12020-08-15 18:37:50 +020047 lyd_ctx_free_clb free; /**< destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020048
49 struct {
Radek Krejci0bff0b12020-08-15 18:37:50 +020050 const struct ly_ctx *ctx; /**< libyang context */
51 uint64_t line; /**< current line */
52 struct ly_in *in; /**< input structure */
53 } *data_ctx; /**< generic pointer supposed to map to and access (common part of) XML/JSON/... parser contexts */
Radek Krejci1798aae2020-07-14 13:26:06 +020054};
55
Radek Krejci7931b192020-06-25 17:05:03 +020056/**
Radek Krejci1798aae2020-07-14 13:26:06 +020057 * @brief Common part of the lyd_ctx_free_t callbacks.
58 */
59void lyd_ctx_free(struct lyd_ctx *);
60
61/**
Radek Krejcif0e1ba52020-05-22 15:14:35 +020062 * @brief Parse submodule from YANG data.
63 * @param[in,out] ctx Parser context.
64 * @param[in] ly_ctx Context of YANG schemas.
65 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +020066 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +020067 * @param[out] submod Pointer to the parsed submodule structure.
68 * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID.
69 */
70LY_ERR yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +020071 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020072
73/**
74 * @brief Parse module from YANG data.
75 * @param[in] ctx Parser context.
Michal Vasko63f3d842020-07-08 10:10:14 +020076 * @param[in] in Input structure.
77 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
Radek Krejcif0e1ba52020-05-22 15:14:35 +020078 * module structure, will be filled in.
Michal Vasko405cc9e2020-12-01 12:01:27 +010079 * @param[in,out] unres Global unres structure.
80 * @return LY_ERR values.
Radek Krejcif0e1ba52020-05-22 15:14:35 +020081 */
Michal Vasko405cc9e2020-12-01 12:01:27 +010082LY_ERR yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod,
83 struct lys_glob_unres *unres);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020084
85/**
86 * @brief Parse module from YIN data.
87 *
88 * @param[in,out] yin_ctx Context created during parsing, is used to finalize lysp_model after it's completly parsed.
Michal Vasko63f3d842020-07-08 10:10:14 +020089 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +020090 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
91 * module structure, will be filled in.
Michal Vasko405cc9e2020-12-01 12:01:27 +010092 * @param[in,out] unres Global unres structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +020093 * @return LY_ERR values.
94 */
Michal Vasko405cc9e2020-12-01 12:01:27 +010095LY_ERR yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod,
96 struct lys_glob_unres *unres);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020097
98/**
99 * @brief Parse submodule from YIN data.
100 *
101 * @param[in,out] yin_ctx Context created during parsing, is used to finalize lysp_model after it's completly parsed.
102 * @param[in] ctx Libyang context.
103 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +0200104 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200105 * @param[in,out] submod Submodule structure where the parsed information, will be filled in.
106 * @return LY_ERR values.
107 */
108LY_ERR yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +0200109 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200110
Radek Krejci1798aae2020-07-14 13:26:06 +0200111/**
Michal Vaskoafac7822020-10-20 14:22:26 +0200112 * @brief Parse XML string as YANG data tree.
113 *
114 * @param[in] ctx libyang context
115 * @param[in] in Input structure.
116 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
117 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
118 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
119 * @param[out] lydctx_p Data parser context to finish validation.
120 * @return LY_ERR value.
121 */
122LY_ERR lyd_parse_xml_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
123 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
124
125/**
126 * @brief Parse XML string as YANG RPC/action invocation.
127 *
Michal Vaskoafac7822020-10-20 14:22:26 +0200128 * @param[in] ctx libyang context.
129 * @param[in] in Input structure.
130 * @param[out] tree_p Parsed full RPC/action tree.
131 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
132 * @return LY_ERR value.
133 */
134LY_ERR lyd_parse_xml_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
135
136/**
137 * @brief Parse XML string as YANG notification.
138 *
Michal Vaskoafac7822020-10-20 14:22:26 +0200139 * @param[in] ctx libyang context.
140 * @param[in] in Input structure.
141 * @param[out] tree_p Parsed full notification tree.
142 * @param[out] op_p Optional pointer to the actual notification. Useful mainly for nested notifications.
143 * @return LY_ERR value.
144 */
145LY_ERR lyd_parse_xml_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
146
147/**
148 * @brief Parse XML string as YANG RPC/action reply.
149 *
Michal Vasko2552ea32020-12-08 15:32:34 +0100150 * @param[in] ctx libyang context.
Michal Vaskoafac7822020-10-20 14:22:26 +0200151 * @param[in] in Input structure.
Michal Vasko2552ea32020-12-08 15:32:34 +0100152 * @param[out] tree_p Parsed full RPC/action reply tree.
Michal Vaskoafac7822020-10-20 14:22:26 +0200153 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
154 * @return LY_ERR value.
155 */
Michal Vasko2552ea32020-12-08 15:32:34 +0100156LY_ERR lyd_parse_xml_reply(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
Michal Vaskoafac7822020-10-20 14:22:26 +0200157
158/**
159 * @brief Parse JSON string as YANG data tree.
160 *
161 * @param[in] ctx libyang context
162 * @param[in] in Input structure.
163 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
164 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
165 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
166 * @param[out] lydctx_p Data parser context to finish validation.
167 * @return LY_ERR value.
168 */
169LY_ERR lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
170 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
171
172/**
173 * @brief Parse JSON string as YANG notification.
174 *
Michal Vaskoafac7822020-10-20 14:22:26 +0200175 * @param[in] ctx libyang context.
176 * @param[in] in Input structure.
177 * @param[out] tree_p Parsed full notification tree.
178 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
179 * @return LY_ERR value.
180 */
181LY_ERR lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
182
183/**
184 * @brief Parse JSON string as YANG RPC/action invocation.
185 *
Michal Vaskoafac7822020-10-20 14:22:26 +0200186 * @param[in] ctx libyang context.
187 * @param[in] in Input structure.
188 * @param[out] tree_p Parsed full RPC/action tree.
189 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
190 * @return LY_ERR value.
191 */
192LY_ERR lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
193
194/**
195 * @brief Parse JSON string as YANG RPC/action reply.
196 *
Michal Vasko2552ea32020-12-08 15:32:34 +0100197 * @param[in] ctx libyang context.
Michal Vaskoafac7822020-10-20 14:22:26 +0200198 * @param[in] in Input structure.
Michal Vasko2552ea32020-12-08 15:32:34 +0100199 * @param[out] tree_p Parsed full RPC/action reply tree.
Michal Vaskoafac7822020-10-20 14:22:26 +0200200 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
201 * @return LY_ERR value.
202 */
Michal Vasko2552ea32020-12-08 15:32:34 +0100203LY_ERR lyd_parse_json_reply(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
Michal Vaskoafac7822020-10-20 14:22:26 +0200204
205/**
206 * @brief Parse binary data as YANG data tree.
207 *
208 * @param[in] ctx libyang context
209 * @param[in] in Input structure.
210 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
211 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
212 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
213 * @param[out] lydctx_p Data parser context to finish validation.
214 * @return LY_ERR value.
215 */
216LY_ERR lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
217 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
218
219/**
220 * @brief Parse binary data as YANG RPC/action invocation.
221 *
222 * @param[in] ctx libyang context.
223 * @param[in] in Input structure.
224 * @param[out] tree_p Parsed full RPC/action tree.
225 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
226 * @return LY_ERR value.
227 */
228LY_ERR lyd_parse_lyb_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
229
230/**
231 * @brief Parse binary data as YANG notification.
232 *
233 * @param[in] ctx libyang context.
234 * @param[in] in Input structure.
235 * @param[out] tree_p Parsed full notification tree.
236 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
237 * @return LY_ERR value.
238 */
239LY_ERR lyd_parse_lyb_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
240
241/**
242 * @brief Parse binary data as YANG RPC/action reply.
243 *
Michal Vasko2552ea32020-12-08 15:32:34 +0100244 * @param[in] ctx libyang context.
Michal Vaskoafac7822020-10-20 14:22:26 +0200245 * @param[in] in Input structure.
Michal Vasko2552ea32020-12-08 15:32:34 +0100246 * @param[out] tree_p Parsed full RPC/action reply tree.
Michal Vaskoafac7822020-10-20 14:22:26 +0200247 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
248 * @return LY_ERR value.
249 */
Michal Vasko2552ea32020-12-08 15:32:34 +0100250LY_ERR lyd_parse_lyb_reply(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
Michal Vaskoafac7822020-10-20 14:22:26 +0200251
252/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200253 * @brief Check that a data node representing the @p snode is suitable based on options.
254 *
255 * @param[in] lydctx Common data parsers context.
256 * @param[in] snode Schema node to check.
257 * @return LY_SUCCESS or LY_EVALID
258 */
259LY_ERR lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode);
260
261/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200262 * @brief Wrapper around ::lyd_create_term() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200263 *
264 * @param[in] lydctx Data parser context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200265 * @param[in] hints Data parser's hint for the value's type.
Radek Krejci1798aae2020-07-14 13:26:06 +0200266 */
267LY_ERR lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200268 ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200269
270/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200271 * @brief Wrapper around ::lyd_create_meta() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200272 *
273 * @param[in] lydctx Data parser context.
Michal Vaskob68571a2020-11-06 17:18:41 +0100274 * @param[in] parent Parent of the created meta. Must be set if @p meta is NULL.
275 * @param[in,out] meta First existing meta to connect to or empty pointer to set. Must be set if @p parent is NULL.
276 * @param[in] mod Module of the created metadata.
277 * @param[in] name Metadata name.
278 * @param[in] name_len Length of @p name.
279 * @param[in] value Metadata value.
280 * @param[in] value_len Length of @p value.
281 * @param[in,out] dynamic Whether the @p value is dynamically allocated, is adjusted once the value is assigned.
282 * @param[in] format Prefix format.
283 * @param[in] prefix_data Prefix format data (see ::ly_resolve_prefix()).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200284 * @param[in] hints [Value hint](@ref lydvalhints) from the parser regarding the value type.
Michal Vaskob68571a2020-11-06 17:18:41 +0100285 * @return LY_ERR value.
Radek Krejci1798aae2020-07-14 13:26:06 +0200286 */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200287LY_ERR lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta,
Radek Krejci0f969882020-08-21 16:56:47 +0200288 const struct lys_module *mod, const char *name, size_t name_len, const char *value,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200289 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200290
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200291#endif /* LY_PARSER_INTERNAL_H_ */