Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_data.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Data parsers for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015-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_DATA_H_ |
| 16 | #define LY_PARSER_DATA_H_ |
| 17 | |
| 18 | #include "tree_data.h" |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | struct ly_in; |
| 25 | |
| 26 | /** |
| 27 | * @addtogroup datatree |
| 28 | * @{ |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | * @defgroup dataparseroptions Data parser options |
| 33 | * |
| 34 | * Various options to change the data tree parsers behavior. |
| 35 | * |
| 36 | * Default parser behavior: |
| 37 | * - complete input file is always parsed. In case of XML, even not well-formed XML document (multiple top-level |
| 38 | * elements) is parsed in its entirety, |
| 39 | * - parser silently ignores data without matching schema node definition, |
| 40 | * - list instances are checked whether they have all the keys, error is raised if not. |
| 41 | * |
| 42 | * Default parser validation behavior: |
| 43 | * - the provided data are expected to provide complete datastore content (both the configuration and state data) |
| 44 | * and performs data validation according to all YANG rules, specifics follow, |
| 45 | * - list instances are expected to have all the keys (it is not checked), |
| 46 | * - instantiated (status) obsolete data print a warning, |
| 47 | * - all types are fully resolved (leafref/instance-identifier targets, unions) and must be valid (lists have |
| 48 | * all the keys, leaf(-lists) correct values), |
| 49 | * - when statements on existing nodes are evaluated, if not satisfied, a validation error is raised, |
| 50 | * - if-feature statements are evaluated, |
| 51 | * - invalid multiple data instances/data from several cases cause a validation error, |
| 52 | * - default values are added. |
| 53 | * @{ |
| 54 | */ |
| 55 | /* note: keep the lower 16bits free for use by LYD_VALIDATE_ flags. They are not supposed to be combined together, |
| 56 | * but since they are used (as a separate parameter) together in some functions, we want to keep them in a separated |
| 57 | * range to be able detect that the caller put wrong flags into the parser/validate options parameter. */ |
| 58 | #define LYD_PARSE_ONLY 0x010000 /**< Data will be only parsed and no validation will be performed. When statements |
| 59 | are kept unevaluated, union types may not be fully resolved, if-feature |
| 60 | statements are not checked, and default values are not added (only the ones |
| 61 | parsed are present). */ |
| 62 | #define LYD_PARSE_TRUSTED 0x020000 /**< Data are considered trusted so they will be parsed as validated. If the parsed |
| 63 | data are not valid, using this flag may lead to some unexpected behavior! |
| 64 | This flag can be used only with #LYD_OPT_PARSE_ONLY. */ |
| 65 | #define LYD_PARSE_STRICT 0x040000 /**< Instead of silently ignoring data without schema definition raise an error. |
| 66 | Do not combine with #LYD_OPT_OPAQ. */ |
| 67 | #define LYD_PARSE_OPAQ 0x080000 /**< Instead of silently ignoring data without definition, parse them into |
| 68 | an opaq node. Do not combine with #LYD_OPT_STRICT and use only for a generic |
| 69 | YANG data tree (opaq Notifications, RPCs or actions are not allowed). */ |
| 70 | #define LYD_PARSE_NO_STATE 0x100000 /**< Forbid state data in the parsed data. */ |
| 71 | |
| 72 | #define LYD_PARSE_LYB_MOD_UPDATE 0x200000 /**< Only for LYB format, allow parsing data printed using a specific module |
| 73 | revision to be loaded even with a module with the same name but newer |
| 74 | revision. */ |
| 75 | /** @} dataparseroptions */ |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * @defgroup datavalidationoptions Data validation options |
| 80 | * @ingroup datatree |
| 81 | * |
| 82 | * Various options to change data validation behaviour, both for the parser and separate validation. |
| 83 | * |
| 84 | * Default separate validation behavior: |
| 85 | * - the provided data are expected to provide complete datastore content (both the configuration and state data) |
| 86 | * and performs data validation according to all YANG rules, specifics follow, |
| 87 | * - instantiated (status) obsolete data print a warning, |
| 88 | * - all types are fully resolved (leafref/instance-identifier targets, unions) and must be valid (lists have |
| 89 | * all the keys, leaf(-lists) correct values), |
| 90 | * - when statements on existing nodes are evaluated. Depending on the previous when state (from previous validation |
| 91 | * or parsing), the node is silently auto-deleted if the state changed from true to false, otherwise a validation error |
| 92 | * is raised if it evaluates to false, |
| 93 | * - if-feature statements are evaluated, |
| 94 | * - data from several cases behave based on their previous state (from previous validation or parsing). If there existed |
| 95 | * already a case and another one was added, the previous one is silently auto-deleted. Otherwise (if data from 2 or |
| 96 | * more cases were created) a validation error is raised, |
| 97 | * - default values are added. |
| 98 | * |
| 99 | * @{ |
| 100 | */ |
| 101 | #define LYD_VALIDATE_NO_STATE 0x0001 /**< Consider state data not allowed and raise an error if they are found. */ |
| 102 | #define LYD_VALIDATE_PRESENT 0x0002 /**< Validate only modules whose data actually exist. */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 103 | |
| 104 | /** @} datavalidationoptions */ |
| 105 | |
| 106 | /** |
| 107 | * @defgroup datavalidateop Operation to validate |
| 108 | * @ingroup datatree |
| 109 | * |
| 110 | * Operation provided to lyd_validate_op() to validate. The operation cannot be determined automatically since RPC/action and a reply to it |
| 111 | * share the common top level node referencing the RPC/action schema node and may not have any input/output children to use for distinction. |
| 112 | */ |
| 113 | typedef enum { |
| 114 | LYD_VALIDATE_OP_RPC = 1, /**< Validate RPC/action request (input parameters). */ |
| 115 | LYD_VALIDATE_OP_REPLY, /**< Validate RPC/action reply (output parameters). */ |
| 116 | LYD_VALIDATE_OP_NOTIF /**< Validate Notification operation. */ |
| 117 | } LYD_VALIDATE_OP; |
| 118 | |
| 119 | /** @} datavalidateop */ |
| 120 | |
| 121 | /** |
| 122 | * @brief Parse (and validate) data from the input handler as a YANG data tree. |
| 123 | * |
| 124 | * @param[in] ctx Context to connect with the tree being built here. |
| 125 | * @param[in] in The input handle to provide the dumped data in the specified @p format to parse (and validate). |
| 126 | * @param[in] format Format of the input data to be parsed. Can be 0 to try to detect format from the input handler. |
| 127 | * @param[in] parse_options Options for parser, see @ref dataparseroptions. |
| 128 | * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions. |
| 129 | * @param[out] tree Resulting data tree built from the input data. Note that NULL can be a valid result as a representation of an empty YANG data tree. |
| 130 | * The returned data are expected to be freed using lyd_free_all(). |
| 131 | * @return LY_SUCCESS in case of successful parsing (and validation). |
Michal Vasko | 6d49766 | 2020-07-08 10:42:57 +0200 | [diff] [blame] | 132 | * @return LY_ERR value in case of error. Additional error information can be obtained from the context using ly_err* functions. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 133 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 134 | LY_ERR lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, |
| 135 | int validate_options, struct lyd_node **tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * @brief Parse (and validate) input data as a YANG data tree. |
| 139 | * |
| 140 | * Wrapper around lyd_parse_data() hiding work with the input handler. |
| 141 | * |
| 142 | * @param[in] ctx Context to connect with the tree being built here. |
| 143 | * @param[in] data The input data in the specified @p format to parse (and validate). |
| 144 | * @param[in] format Format of the input data to be parsed. Can be 0 to try to detect format from the input handler. |
| 145 | * @param[in] parse_options Options for parser, see @ref dataparseroptions. |
| 146 | * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions. |
| 147 | * @param[out] tree Resulting data tree built from the input data. Note that NULL can be a valid result as a representation of an empty YANG data tree. |
| 148 | * The returned data are expected to be freed using lyd_free_all(). |
| 149 | * @return LY_SUCCESS in case of successful parsing (and validation). |
Michal Vasko | 6d49766 | 2020-07-08 10:42:57 +0200 | [diff] [blame] | 150 | * @return LY_ERR value in case of error. Additional error information can be obtained from the context using ly_err* functions. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 151 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 152 | LY_ERR lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, |
| 153 | int validate_options, struct lyd_node **tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * @brief Parse (and validate) input data as a YANG data tree. |
| 157 | * |
| 158 | * Wrapper around lyd_parse_data() hiding work with the input handler. |
| 159 | * |
| 160 | * @param[in] ctx Context to connect with the tree being built here. |
| 161 | * @param[in] fd File descriptor of a regular file (e.g. sockets are not supported) containing the input data in the specified @p format to parse (and validate). |
| 162 | * @param[in] format Format of the input data to be parsed. Can be 0 to try to detect format from the input handler. |
| 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 Resulting data tree built from the input data. Note that NULL can be a valid result as a representation of an empty YANG data tree. |
| 166 | * The returned data are expected to be freed using lyd_free_all(). |
| 167 | * @return LY_SUCCESS in case of successful parsing (and validation). |
Michal Vasko | 6d49766 | 2020-07-08 10:42:57 +0200 | [diff] [blame] | 168 | * @return LY_ERR value in case of error. Additional error information can be obtained from the context using ly_err* functions. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 169 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 170 | LY_ERR lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options, |
| 171 | struct lyd_node **tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * @brief Parse (and validate) input data as a YANG data tree. |
| 175 | * |
| 176 | * Wrapper around lyd_parse_data() hiding work with the input handler. |
| 177 | * |
| 178 | * @param[in] ctx Context to connect with the tree being built here. |
| 179 | * @param[in] path Path to the file with the input data in the specified @p format to parse (and validate). |
| 180 | * @param[in] format Format of the input data to be parsed. Can be 0 to try to detect format from the input handler. |
| 181 | * @param[in] parse_options Options for parser, see @ref dataparseroptions. |
| 182 | * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions. |
| 183 | * @param[out] tree Resulting data tree built from the input data. Note that NULL can be a valid result as a representation of an empty YANG data tree. |
| 184 | * The returned data are expected to be freed using lyd_free_all(). |
| 185 | * @return LY_SUCCESS in case of successful parsing (and validation). |
Michal Vasko | 6d49766 | 2020-07-08 10:42:57 +0200 | [diff] [blame] | 186 | * @return LY_ERR value in case of error. Additional error information can be obtained from the context using ly_err* functions. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 187 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 188 | LY_ERR lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options, |
| 189 | int validate_options, struct lyd_node **tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * @brief Parse (and validate) data from the input handler as a YANG RPC/action invocation. |
| 193 | * |
| 194 | * In case o LYD_XML @p format, the \<rpc\> envelope element is accepted if present. It is [checked](https://tools.ietf.org/html/rfc6241#section-4.1), an opaq |
| 195 | * data node (lyd_node_opaq) is created and all its XML attributes are parsed and inserted into the node. As a content of the enveloper, an RPC data or |
| 196 | * \<action\> envelope element is expected. The \<action\> envelope element is also [checked](https://tools.ietf.org/html/rfc7950#section-7.15.2) and parsed as |
| 197 | * the \<rpc\> envelope. Inside the \<action\> envelope, only an action data are expected. |
| 198 | * |
| 199 | * @param[in] ctx Context to connect with the tree being built here. |
| 200 | * @param[in] in The input handle to provide the dumped data in the specified @p format to parse (and validate). |
| 201 | * @param[in] format Format of the input data to be parsed. |
| 202 | * @param[out] tree Resulting full RPC/action tree built from the input data. The returned data are expected to be freed using lyd_free_all(). |
| 203 | * In contrast to YANG data tree, result of parsing RPC/action cannot be NULL until an error occurs. |
| 204 | * @param[out] op Optional pointer to the actual operation node inside the full action @p tree, useful only for action. |
| 205 | * @return LY_SUCCESS in case of successful parsing (and validation). |
Michal Vasko | 6d49766 | 2020-07-08 10:42:57 +0200 | [diff] [blame] | 206 | * @return LY_ERR value in case of error. Additional error information can be obtained from the context using ly_err* functions. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 207 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 208 | LY_ERR lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, |
| 209 | struct lyd_node **op); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * @brief Parse (and validate) data from the input handler as a YANG RPC/action reply. |
| 213 | * |
| 214 | * In case o LYD_XML @p format, the \<rpc-reply\> envelope element is accepted if present. It is [checked](https://tools.ietf.org/html/rfc6241#section-4.2), an opaq |
| 215 | * data node (lyd_node_opaq) is created and all its XML attributes are parsed and inserted into the node. |
| 216 | * |
| 217 | * The reply data are strictly expected to be related to the provided RPC/action @p request. |
| 218 | * |
| 219 | * @param[in] request The RPC/action tree (result of lyd_parse_rpc()) of the request for the reply being parsed. |
| 220 | * @param[in] in The input handle to provide the dumped data in the specified @p format to parse (and validate). |
| 221 | * @param[in] format Format of the input data to be parsed. |
| 222 | * @param[out] tree Resulting full RPC/action reply tree built from the input data. The returned data are expected to be freed using lyd_free_all(). |
| 223 | * The reply tree always includes duplicated operation node (and its parents) of the @p request, so in contrast to YANG data tree, |
| 224 | * the result of parsing RPC/action reply cannot be NULL until an error occurs. |
| 225 | * @param[out] op Optional pointer to the actual operation node inside the full action reply @p tree, useful only for action. |
| 226 | * @return LY_SUCCESS in case of successful parsing (and validation). |
Michal Vasko | 6d49766 | 2020-07-08 10:42:57 +0200 | [diff] [blame] | 227 | * @return LY_ERR value in case of error. Additional error information can be obtained from the request's context using ly_err* functions. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 228 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 229 | LY_ERR lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, |
| 230 | struct lyd_node **op); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * @brief Parse XML string as YANG notification. |
| 234 | * |
| 235 | * In case o LYD_XML @p format, the \<notification\> envelope element in combination with the child \<eventTime\> element are accepted if present. They are |
| 236 | * [checked](https://tools.ietf.org/html/rfc5277#page-25), opaq data nodes (lyd_node_opaq) are created and all their XML attributes are parsed and inserted into the nodes. |
| 237 | * |
| 238 | * @param[in] ctx Context to connect with the tree being built here. |
| 239 | * @param[in] in The input handle to provide the dumped data in the specified @p format to parse (and validate). |
| 240 | * @param[in] format Format of the input data to be parsed. |
| 241 | * @param[out] tree Resulting full Notification tree built from the input data. The returned data are expected to be freed using lyd_free_all(). |
| 242 | * In contrast to YANG data tree, result of parsing Notification cannot be NULL until an error occurs. |
| 243 | * @param[out] ntf Optional pointer to the actual notification node inside the full Notification @p tree, useful for nested notifications. |
| 244 | * @return LY_SUCCESS in case of successful parsing (and validation). |
Michal Vasko | 6d49766 | 2020-07-08 10:42:57 +0200 | [diff] [blame] | 245 | * @return LY_ERR value in case of error. Additional error information can be obtained from the context using ly_err* functions. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 246 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 247 | LY_ERR lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, |
| 248 | struct lyd_node **ntf); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * @brief Fully validate a data tree. |
| 252 | * |
| 253 | * @param[in,out] tree Data tree to recursively validate. May be changed by validation. |
| 254 | * @param[in] ctx libyang context. Can be NULL if @p tree is set. |
| 255 | * @param[in] val_opts Validation options (@ref datavalidationoptions). |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 256 | * @param[out] diff Optional diff with any changes made by the validation. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 257 | * @return LY_SUCCESS on success. |
| 258 | * @return LY_ERR error on error. |
| 259 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 260 | LY_ERR lyd_validate_all(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts, struct lyd_node **diff); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 261 | |
| 262 | /** |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 263 | * @brief Fully validate a data tree of a module. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 264 | * |
| 265 | * @param[in,out] tree Data tree to recursively validate. May be changed by validation. |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 266 | * @param[in] module Module whose data (and schema restrictions) to validate. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 267 | * @param[in] val_opts Validation options (@ref datavalidationoptions). |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 268 | * @param[out] diff Optional diff with any changes made by the validation. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 269 | * @return LY_SUCCESS on success. |
| 270 | * @return LY_ERR error on error. |
| 271 | */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 272 | LY_ERR lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, int val_opts, struct lyd_node **diff); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * @brief Validate an RPC/action, notification, or RPC/action reply. |
| 276 | * |
| 277 | * @param[in,out] op_tree Operation tree with any parents. It can point to the operation itself or any of |
| 278 | * its parents, only the operation subtree is actually validated. |
| 279 | * @param[in] tree Tree to be used for validating references from the operation subtree. |
| 280 | * @param[in] op Operation to validate (@ref datavalidateop), the given @p op_tree must correspond to this value. Note that |
| 281 | * it isn't possible to detect the operation simply from the @p op_tree since RPC/action and their reply share the same |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 282 | * RPC/action data node and in case one of the input and output do not define any data node children, it is not possible |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 283 | * to get know what is here given for validation and if it is really valid. |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 284 | * @param[out] diff Optional diff with any changes made by the validation. |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 285 | * @return LY_SUCCESS on success. |
| 286 | * @return LY_ERR error on error. |
| 287 | */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 288 | LY_ERR lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, LYD_VALIDATE_OP op, struct lyd_node **diff); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 289 | |
| 290 | /** @} datatree */ |
| 291 | |
| 292 | #ifdef __cplusplus |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | #endif /* LY_PARSER_DATA_H_ */ |