Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /** |
| 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 Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 18 | #include "plugins_types.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 19 | #include "tree_schema_internal.h" |
| 20 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 21 | struct lyd_ctx; |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 22 | struct ly_in; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 23 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 24 | /** |
| 25 | * @brief Callback for lyd_ctx to free the structure |
| 26 | * |
| 27 | * @param[in] ctx Data parser context to free. |
| 28 | */ |
| 29 | typedef void (*lyd_ctx_free_clb)(struct lyd_ctx *ctx); |
| 30 | |
| 31 | /** |
| 32 | * @brief Internal (common) context for YANG data parsers. |
| 33 | */ |
| 34 | struct 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 Vasko | 3271138 | 2020-12-03 14:14:31 +0100 | [diff] [blame^] | 41 | 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 Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 44 | struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */ |
| 45 | |
| 46 | /* callbacks */ |
Radek Krejci | 0bff0b1 | 2020-08-15 18:37:50 +0200 | [diff] [blame] | 47 | lyd_ctx_free_clb free; /**< destructor */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 48 | |
| 49 | struct { |
Radek Krejci | 0bff0b1 | 2020-08-15 18:37:50 +0200 | [diff] [blame] | 50 | 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 Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 56 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 57 | * @brief Common part of the lyd_ctx_free_t callbacks. |
| 58 | */ |
| 59 | void lyd_ctx_free(struct lyd_ctx *); |
| 60 | |
| 61 | /** |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 62 | * @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 Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 66 | * @param[in] in Input structure. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 67 | * @param[out] submod Pointer to the parsed submodule structure. |
| 68 | * @return LY_ERR value - LY_SUCCESS, LY_EINVAL or LY_EVALID. |
| 69 | */ |
| 70 | LY_ERR yang_parse_submodule(struct lys_yang_parser_ctx **context, struct ly_ctx *ly_ctx, struct lys_parser_ctx *main_ctx, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 71 | struct ly_in *in, struct lysp_submodule **submod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * @brief Parse module from YANG data. |
| 75 | * @param[in] ctx Parser context. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 76 | * @param[in] in Input structure. |
| 77 | * @param[in,out] mod Prepared module structure where the parsed information, including the parsed |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 78 | * module structure, will be filled in. |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 79 | * @param[in,out] unres Global unres structure. |
| 80 | * @return LY_ERR values. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 81 | */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 82 | LY_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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 84 | |
| 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 Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 89 | * @param[in] in Input structure. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 90 | * @param[in,out] mod Prepared module structure where the parsed information, including the parsed |
| 91 | * module structure, will be filled in. |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 92 | * @param[in,out] unres Global unres structure. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 93 | * @return LY_ERR values. |
| 94 | */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 95 | LY_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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 97 | |
| 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 Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 104 | * @param[in] in Input structure. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 105 | * @param[in,out] submod Submodule structure where the parsed information, will be filled in. |
| 106 | * @return LY_ERR values. |
| 107 | */ |
| 108 | LY_ERR yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 109 | struct ly_in *in, struct lysp_submodule **submod); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 110 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 111 | /** |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 112 | * @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 | */ |
| 122 | LY_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 | */ |
| 138 | LY_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 | */ |
| 152 | LY_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 | */ |
| 166 | LY_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 | */ |
| 179 | LY_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 | */ |
| 194 | LY_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 | */ |
| 209 | LY_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 | */ |
| 222 | LY_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 | */ |
| 235 | LY_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 | */ |
| 247 | LY_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 | */ |
| 258 | LY_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 | */ |
| 269 | LY_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 Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 272 | * @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 | */ |
| 278 | LY_ERR lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode); |
| 279 | |
| 280 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 281 | * @brief Wrapper around ::lyd_create_term() for data parsers. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 282 | * |
| 283 | * @param[in] lydctx Data parser context. |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 284 | * @param[in] hints Data parser's hint for the value's type. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 285 | */ |
| 286 | LY_ERR lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const char *value, size_t value_len, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 287 | ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 288 | |
| 289 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 290 | * @brief Wrapper around ::lyd_create_meta() for data parsers. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 291 | * |
| 292 | * @param[in] lydctx Data parser context. |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 293 | * @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 Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 303 | * @param[in] hints [Value hint](@ref lydvalhints) from the parser regarding the value type. |
Michal Vasko | b68571a | 2020-11-06 17:18:41 +0100 | [diff] [blame] | 304 | * @return LY_ERR value. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 305 | */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 306 | LY_ERR lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 307 | const struct lys_module *mod, const char *name, size_t name_len, const char *value, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 308 | size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 309 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 310 | #endif /* LY_PARSER_INTERNAL_H_ */ |