blob: 282938eaf750aea062553b9586ca8e4a668d5fba [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 Vaskoe0665742021-02-11 11:08:44 +010018#include "parser_data.h"
Michal Vaskoc8a230d2020-08-14 12:17:10 +020019#include "plugins_types.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020020#include "tree_schema_internal.h"
21
Radek Krejci1798aae2020-07-14 13:26:06 +020022struct lyd_ctx;
Michal Vaskoafac7822020-10-20 14:22:26 +020023struct ly_in;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020024
Radek Krejci1798aae2020-07-14 13:26:06 +020025/**
26 * @brief Callback for lyd_ctx to free the structure
27 *
28 * @param[in] ctx Data parser context to free.
29 */
30typedef void (*lyd_ctx_free_clb)(struct lyd_ctx *ctx);
31
32/**
Michal Vaskoe0665742021-02-11 11:08:44 +010033 * @brief Internal data parser flags.
34 */
35#define LYD_INTOPT_RPC 0x01 /**< RPC request is being parsed. */
36#define LYD_INTOPT_ACTION 0x02 /**< Action request is being parsed. */
37#define LYD_INTOPT_REPLY 0x04 /**< RPC/action reply is being parsed. */
38#define LYD_INTOPT_NOTIF 0x08 /**< Notification is being parsed. */
39#define LYD_INTOPT_WITH_SIBLINGS 0x10 /**< Parse the whole input with any siblings. */
40#define LYD_INTOPT_NO_SIBLINGS 0x20 /**< If there are any siblings, return an error. */
41
42/**
Radek Krejci1798aae2020-07-14 13:26:06 +020043 * @brief Internal (common) context for YANG data parsers.
44 */
45struct lyd_ctx {
Michal Vaskoe0665742021-02-11 11:08:44 +010046 uint32_t parse_opts; /**< various @ref dataparseroptions. */
47 uint32_t val_opts; /**< various @ref datavalidationoptions. */
48 uint32_t int_opts; /**< internal parser options */
Radek Krejci1798aae2020-07-14 13:26:06 +020049 uint32_t path_len; /**< used bytes in the path buffer */
50#define LYD_PARSER_BUFSIZE 4078
51 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vasko32711382020-12-03 14:14:31 +010052 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
53 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
54 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci1798aae2020-07-14 13:26:06 +020055 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
56
57 /* callbacks */
Radek Krejci0bff0b12020-08-15 18:37:50 +020058 lyd_ctx_free_clb free; /**< destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020059
60 struct {
Radek Krejci0bff0b12020-08-15 18:37:50 +020061 const struct ly_ctx *ctx; /**< libyang context */
62 uint64_t line; /**< current line */
63 struct ly_in *in; /**< input structure */
64 } *data_ctx; /**< generic pointer supposed to map to and access (common part of) XML/JSON/... parser contexts */
Radek Krejci1798aae2020-07-14 13:26:06 +020065};
66
Radek Krejci7931b192020-06-25 17:05:03 +020067/**
Radek Krejci1798aae2020-07-14 13:26:06 +020068 * @brief Common part of the lyd_ctx_free_t callbacks.
69 */
70void lyd_ctx_free(struct lyd_ctx *);
71
72/**
Radek Krejcif0e1ba52020-05-22 15:14:35 +020073 * @brief Parse submodule from YANG data.
74 * @param[in,out] ctx Parser context.
75 * @param[in] ly_ctx Context of YANG schemas.
76 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +020077 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +020078 * @param[out] submod Pointer to the parsed submodule structure.
79 * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID.
80 */
81LY_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 +020082 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020083
84/**
85 * @brief Parse module from YANG data.
86 * @param[in] ctx Parser context.
Michal Vasko63f3d842020-07-08 10:10:14 +020087 * @param[in] in Input structure.
88 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
Radek Krejcif0e1ba52020-05-22 15:14:35 +020089 * module structure, will be filled in.
Michal Vasko405cc9e2020-12-01 12:01:27 +010090 * @param[in,out] unres Global unres structure.
91 * @return LY_ERR values.
Radek Krejcif0e1ba52020-05-22 15:14:35 +020092 */
Michal Vasko405cc9e2020-12-01 12:01:27 +010093LY_ERR yang_parse_module(struct lys_yang_parser_ctx **context, struct ly_in *in, struct lys_module *mod,
94 struct lys_glob_unres *unres);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020095
96/**
97 * @brief Parse module from YIN data.
98 *
99 * @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 +0200100 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200101 * @param[in,out] mod Prepared module structure where the parsed information, including the parsed
102 * module structure, will be filled in.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100103 * @param[in,out] unres Global unres structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200104 * @return LY_ERR values.
105 */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100106LY_ERR yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod,
107 struct lys_glob_unres *unres);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200108
109/**
110 * @brief Parse submodule from YIN data.
111 *
112 * @param[in,out] yin_ctx Context created during parsing, is used to finalize lysp_model after it's completly parsed.
113 * @param[in] ctx Libyang context.
114 * @param[in] main_ctx Parser context of main module.
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 * @param[in] in Input structure.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200116 * @param[in,out] submod Submodule structure where the parsed information, will be filled in.
117 * @return LY_ERR values.
118 */
119LY_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 +0200120 struct ly_in *in, struct lysp_submodule **submod);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200121
Radek Krejci1798aae2020-07-14 13:26:06 +0200122/**
Michal Vaskoe0665742021-02-11 11:08:44 +0100123 * @brief Parse XML string as a YANG data tree.
Michal Vaskoafac7822020-10-20 14:22:26 +0200124 *
Michal Vaskoe0665742021-02-11 11:08:44 +0100125 * @param[in] ctx libyang context.
126 * @param[in] parent Parent to connect the parsed nodes to, if any.
127 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
Michal Vaskoafac7822020-10-20 14:22:26 +0200128 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +0100129 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
130 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
131 * @param[in] data_type Expected data type of the data.
Michal Vasko299d5d12021-02-16 16:36:37 +0100132 * @param[out] envp Individual parsed envelopes tree, returned only by specific @p data_type and possibly even if
133 * an error occurs later.
Michal Vaskoe0665742021-02-11 11:08:44 +0100134 * @param[out] parsed Set to add all the parsed siblings into.
Michal Vaskoafac7822020-10-20 14:22:26 +0200135 * @param[out] lydctx_p Data parser context to finish validation.
136 * @return LY_ERR value.
137 */
Michal Vaskoe0665742021-02-11 11:08:44 +0100138LY_ERR lyd_parse_xml(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_in *in,
139 uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, struct lyd_node **envp, struct ly_set *parsed,
140 struct lyd_ctx **lydctx_p);
Michal Vaskoafac7822020-10-20 14:22:26 +0200141
142/**
Michal Vaskoe0665742021-02-11 11:08:44 +0100143 * @brief Parse JSON string as a YANG data tree.
Michal Vaskoafac7822020-10-20 14:22:26 +0200144 *
Michal Vaskoafac7822020-10-20 14:22:26 +0200145 * @param[in] ctx libyang context.
Michal Vaskoe0665742021-02-11 11:08:44 +0100146 * @param[in] parent Parent to connect the parsed nodes to, if any.
147 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
Michal Vaskoafac7822020-10-20 14:22:26 +0200148 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +0100149 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
150 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
151 * @param[in] data_type Expected data type of the data.
152 * @param[out] parsed Set to add all the parsed siblings into.
Michal Vaskoafac7822020-10-20 14:22:26 +0200153 * @param[out] lydctx_p Data parser context to finish validation.
154 * @return LY_ERR value.
155 */
Michal Vaskoe0665742021-02-11 11:08:44 +0100156LY_ERR lyd_parse_json(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_in *in,
157 uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, struct ly_set *parsed, struct lyd_ctx **lydctx_p);
Michal Vaskoafac7822020-10-20 14:22:26 +0200158
159/**
Michal Vaskoe0665742021-02-11 11:08:44 +0100160 * @brief Parse binary LYB data as a YANG data tree.
Michal Vaskoafac7822020-10-20 14:22:26 +0200161 *
Michal Vaskoafac7822020-10-20 14:22:26 +0200162 * @param[in] ctx libyang context.
Michal Vaskoe0665742021-02-11 11:08:44 +0100163 * @param[in] parent Parent to connect the parsed nodes to, if any.
164 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
Michal Vaskoafac7822020-10-20 14:22:26 +0200165 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +0100166 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
167 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
168 * @param[in] data_type Expected data type of the data.
169 * @param[out] parsed Set to add all the parsed siblings into.
Michal Vaskoafac7822020-10-20 14:22:26 +0200170 * @param[out] lydctx_p Data parser context to finish validation.
171 * @return LY_ERR value.
172 */
Michal Vaskoe0665742021-02-11 11:08:44 +0100173LY_ERR lyd_parse_lyb(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_in *in,
174 uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, struct ly_set *parsed, struct lyd_ctx **lydctx_p);
Michal Vaskoafac7822020-10-20 14:22:26 +0200175
176/**
Michal Vaskoe0665742021-02-11 11:08:44 +0100177 * @brief Search all the parents for an operation node, check validity based on internal parser flags.
Michal Vaskoafac7822020-10-20 14:22:26 +0200178 *
Michal Vaskoe0665742021-02-11 11:08:44 +0100179 * @param[in] parent Parent to connect the parsed nodes to.
180 * @param[in] int_opt Internal parser options.
181 * @param[out] op Found operation, if any.
Michal Vaskoafac7822020-10-20 14:22:26 +0200182 * @return LY_ERR value.
183 */
Michal Vaskoe0665742021-02-11 11:08:44 +0100184LY_ERR lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op);
Michal Vaskoafac7822020-10-20 14:22:26 +0200185
186/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200187 * @brief Check that a data node representing the @p snode is suitable based on options.
188 *
189 * @param[in] lydctx Common data parsers context.
190 * @param[in] snode Schema node to check.
191 * @return LY_SUCCESS or LY_EVALID
192 */
193LY_ERR lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode);
194
195/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200196 * @brief Wrapper around ::lyd_create_term() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200197 *
198 * @param[in] lydctx Data parser context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200199 * @param[in] hints Data parser's hint for the value's type.
Radek Krejci1798aae2020-07-14 13:26:06 +0200200 */
201LY_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 +0200202 ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200203
204/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200205 * @brief Wrapper around ::lyd_create_meta() for data parsers.
Radek Krejci1798aae2020-07-14 13:26:06 +0200206 *
207 * @param[in] lydctx Data parser context.
Michal Vaskob68571a2020-11-06 17:18:41 +0100208 * @param[in] parent Parent of the created meta. Must be set if @p meta is NULL.
209 * @param[in,out] meta First existing meta to connect to or empty pointer to set. Must be set if @p parent is NULL.
210 * @param[in] mod Module of the created metadata.
211 * @param[in] name Metadata name.
212 * @param[in] name_len Length of @p name.
213 * @param[in] value Metadata value.
214 * @param[in] value_len Length of @p value.
215 * @param[in,out] dynamic Whether the @p value is dynamically allocated, is adjusted once the value is assigned.
216 * @param[in] format Prefix format.
217 * @param[in] prefix_data Prefix format data (see ::ly_resolve_prefix()).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200218 * @param[in] hints [Value hint](@ref lydvalhints) from the parser regarding the value type.
Michal Vaskob68571a2020-11-06 17:18:41 +0100219 * @return LY_ERR value.
Radek Krejci1798aae2020-07-14 13:26:06 +0200220 */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200221LY_ERR lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta,
Radek Krejci0f969882020-08-21 16:56:47 +0200222 const struct lys_module *mod, const char *name, size_t name_len, const char *value,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200223 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200224
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200225#endif /* LY_PARSER_INTERNAL_H_ */