blob: b2b4cc258e7c4f0072151849b699b16e09400a2c [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 *
128 * Optional \<rpc\> envelope element is accepted if present. It is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and all
129 * its XML attributes are parsed. As a content of the envelope, an RPC data or \<action\> envelope element is expected. The \<action\> envelope element is
130 * also [checked](https://tools.ietf.org/html/rfc7950#section-7.15.2) and then an action data is expected as a content of this envelope.
131 *
132 * @param[in] ctx libyang context.
133 * @param[in] in Input structure.
134 * @param[out] tree_p Parsed full RPC/action tree.
135 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
136 * @return LY_ERR value.
137 */
138LY_ERR lyd_parse_xml_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
139
140/**
141 * @brief Parse XML string as YANG notification.
142 *
143 * Optional \<notification\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
144 * and parsed. Specifically, its namespace and the child \<eventTime\> element and its value.
145 *
146 * @param[in] ctx libyang context.
147 * @param[in] in Input structure.
148 * @param[out] tree_p Parsed full notification tree.
149 * @param[out] op_p Optional pointer to the actual notification. Useful mainly for nested notifications.
150 * @return LY_ERR value.
151 */
152LY_ERR lyd_parse_xml_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
153
154/**
155 * @brief Parse XML string as YANG RPC/action reply.
156 *
157 * Optional \<rpc-reply\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2)
158 * and all its XML attributes parsed.
159 *
160 * @param[in] request Data tree of the RPC/action request.
161 * @param[in] in Input structure.
162 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
163 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
164 * @return LY_ERR value.
165 */
166LY_ERR lyd_parse_xml_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
167
168/**
169 * @brief Parse JSON string as YANG data tree.
170 *
171 * @param[in] ctx libyang context
172 * @param[in] in Input structure.
173 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
174 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
175 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
176 * @param[out] lydctx_p Data parser context to finish validation.
177 * @return LY_ERR value.
178 */
179LY_ERR lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
180 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
181
182/**
183 * @brief Parse JSON string as YANG notification.
184 *
185 * Optional top-level "notification" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
186 * and parsed. Specifically the child "eventTime" member and its value.
187 *
188 * @param[in] ctx libyang context.
189 * @param[in] in Input structure.
190 * @param[out] tree_p Parsed full notification tree.
191 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
192 * @return LY_ERR value.
193 */
194LY_ERR lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
195
196/**
197 * @brief Parse JSON string as YANG RPC/action invocation.
198 *
199 * Optional top-level "rpc" envelope object, if present is is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and the parser
200 * goes inside for the content, which is an RPC data or "action" envelope objects. The "action" envelope object is
201 * also [checked](https://tools.ietf.org/html/rfc7950#section-7.15.2) and then an action data is expected as a content of this envelope.
202 *
203 * @param[in] ctx libyang context.
204 * @param[in] in Input structure.
205 * @param[out] tree_p Parsed full RPC/action tree.
206 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
207 * @return LY_ERR value.
208 */
209LY_ERR lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
210
211/**
212 * @brief Parse JSON string as YANG RPC/action reply.
213 *
214 * Optional "rpc-reply" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2).
215 *
216 * @param[in] request Data tree of the RPC/action request.
217 * @param[in] in Input structure.
218 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
219 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
220 * @return LY_ERR value.
221 */
222LY_ERR lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
223
224/**
225 * @brief Parse binary data as YANG data tree.
226 *
227 * @param[in] ctx libyang context
228 * @param[in] in Input structure.
229 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
230 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
231 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
232 * @param[out] lydctx_p Data parser context to finish validation.
233 * @return LY_ERR value.
234 */
235LY_ERR lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
236 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
237
238/**
239 * @brief Parse binary data as YANG RPC/action invocation.
240 *
241 * @param[in] ctx libyang context.
242 * @param[in] in Input structure.
243 * @param[out] tree_p Parsed full RPC/action tree.
244 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
245 * @return LY_ERR value.
246 */
247LY_ERR lyd_parse_lyb_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
248
249/**
250 * @brief Parse binary data as YANG notification.
251 *
252 * @param[in] ctx libyang context.
253 * @param[in] in Input structure.
254 * @param[out] tree_p Parsed full notification tree.
255 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
256 * @return LY_ERR value.
257 */
258LY_ERR lyd_parse_lyb_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
259
260/**
261 * @brief Parse binary data as YANG RPC/action reply.
262 *
263 * @param[in] request Data tree of the RPC/action request.
264 * @param[in] in Input structure.
265 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
266 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
267 * @return LY_ERR value.
268 */
269LY_ERR lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
270
271/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200272 * @brief Check that a data node representing the @p snode is suitable based on options.
273 *
274 * @param[in] lydctx Common data parsers context.
275 * @param[in] snode Schema node to check.
276 * @return LY_SUCCESS or LY_EVALID
277 */
278LY_ERR lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode);
279
280/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200281 * @brief Wrapper around ::lyd_create_term() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200282 *
283 * @param[in] lydctx Data parser context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200284 * @param[in] hints Data parser's hint for the value's type.
Radek Krejci1798aae2020-07-14 13:26:06 +0200285 */
286LY_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 +0200287 ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200288
289/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200290 * @brief Wrapper around ::lyd_create_meta() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200291 *
292 * @param[in] lydctx Data parser context.
Michal Vaskob68571a2020-11-06 17:18:41 +0100293 * @param[in] parent Parent of the created meta. Must be set if @p meta is NULL.
294 * @param[in,out] meta First existing meta to connect to or empty pointer to set. Must be set if @p parent is NULL.
295 * @param[in] mod Module of the created metadata.
296 * @param[in] name Metadata name.
297 * @param[in] name_len Length of @p name.
298 * @param[in] value Metadata value.
299 * @param[in] value_len Length of @p value.
300 * @param[in,out] dynamic Whether the @p value is dynamically allocated, is adjusted once the value is assigned.
301 * @param[in] format Prefix format.
302 * @param[in] prefix_data Prefix format data (see ::ly_resolve_prefix()).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200303 * @param[in] hints [Value hint](@ref lydvalhints) from the parser regarding the value type.
Michal Vaskob68571a2020-11-06 17:18:41 +0100304 * @return LY_ERR value.
Radek Krejci1798aae2020-07-14 13:26:06 +0200305 */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200306LY_ERR lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta,
Radek Krejci0f969882020-08-21 16:56:47 +0200307 const struct lys_module *mod, const char *name, size_t name_len, const char *value,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200308 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200309
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200310#endif /* LY_PARSER_INTERNAL_H_ */