Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML data parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2019 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 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include "context.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 22 | #include "dict.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 23 | #include "in_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 25 | #include "parser_data.h" |
| 26 | #include "parser_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include "set.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 28 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 29 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "tree_data_internal.h" |
| 31 | #include "tree_schema.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 32 | #include "tree_schema_internal.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 33 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 34 | #include "xml.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 35 | |
| 36 | /** |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 37 | * @brief Internal context for XML YANG data parser. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 38 | * |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 39 | * Note that the structure maps to the ::lyd_ctx which is common for all the data parsers |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | */ |
| 41 | struct lyd_xml_ctx { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 42 | const struct lysc_ext_instance *ext; /**< extension instance possibly changing document root context of the data being parsed */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 43 | uint32_t parse_opts; /**< various @ref dataparseroptions. */ |
| 44 | uint32_t val_opts; /**< various @ref datavalidationoptions. */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 45 | uint32_t int_opts; /**< internal data parser options */ |
| 46 | uint32_t path_len; /**< used bytes in the path buffer */ |
| 47 | char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */ |
Michal Vasko | c43c8ab | 2021-03-05 13:32:44 +0100 | [diff] [blame] | 48 | struct ly_set node_when; /**< set of nodes with "when" conditions */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 49 | struct ly_set node_exts; /**< set of nodes and extensions connected with a plugin providing own validation callback */ |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 50 | struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */ |
| 51 | struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 52 | struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 54 | /* callbacks */ |
| 55 | lyd_ctx_free_clb free; /* destructor */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 56 | |
| 57 | struct lyxml_ctx *xmlctx; /**< XML context */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 60 | void |
| 61 | lyd_xml_ctx_free(struct lyd_ctx *lydctx) |
| 62 | { |
| 63 | struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx; |
| 64 | |
| 65 | lyd_ctx_free(lydctx); |
| 66 | lyxml_ctx_free(ctx->xmlctx); |
| 67 | free(ctx); |
| 68 | } |
| 69 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 70 | /** |
| 71 | * @brief Parse and create XML metadata. |
| 72 | * |
| 73 | * @param[in] lydctx XML data parser context. |
| 74 | * @param[in] parent_exts Extension instances of the parent node. |
| 75 | * @param[out] meta List of created metadata instances. |
| 76 | * @return LY_ERR value. |
| 77 | */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 78 | static LY_ERR |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 79 | lydxml_metadata(struct lyd_xml_ctx *lydctx, struct lysc_ext_instance *parent_exts, struct lyd_meta **meta) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 80 | { |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 81 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 82 | const struct lyxml_ns *ns; |
| 83 | struct lys_module *mod; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 84 | const char *name; |
| 85 | size_t name_len; |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 86 | LY_ARRAY_COUNT_TYPE u; |
| 87 | ly_bool filter_attrs = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 88 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 89 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 90 | *meta = NULL; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 91 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 92 | /* check for NETCONF filter unqualified attributes */ |
| 93 | LY_ARRAY_FOR(parent_exts, u) { |
| 94 | if (!strcmp(parent_exts[u].def->name, "get-filter-element-attributes") && |
| 95 | !strcmp(parent_exts[u].def->module->name, "ietf-netconf")) { |
| 96 | filter_attrs = 1; |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 101 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 102 | if (!xmlctx->prefix_len) { |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 103 | /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */ |
| 104 | if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) || |
| 105 | !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) { |
| 106 | mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf"); |
| 107 | if (!mod) { |
| 108 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
| 109 | "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes."); |
| 110 | ret = LY_ENOTFOUND; |
| 111 | goto cleanup; |
| 112 | } |
| 113 | goto create_meta; |
| 114 | } |
| 115 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 116 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 117 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 118 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 119 | ret = LY_EVALID; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 120 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 121 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 122 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 123 | /* skip attr */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 124 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 125 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 126 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 127 | continue; |
| 128 | } |
| 129 | |
| 130 | /* get namespace of the attribute to find its annotation definition */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 131 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 132 | if (!ns) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 133 | /* unknown namespace, XML error */ |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 134 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix); |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 135 | ret = LY_ENOTFOUND; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 136 | goto cleanup; |
| 137 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 138 | |
| 139 | /* get the module with metadata definition */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 140 | mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 141 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 142 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 143 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 144 | "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 145 | ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", |
| 146 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 147 | ret = LY_ENOTFOUND; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 148 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 149 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 150 | |
| 151 | /* skip attr */ |
| 152 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 153 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 154 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 155 | continue; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 158 | create_meta: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 159 | /* remember meta name and get its content */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 160 | name = xmlctx->name; |
| 161 | name_len = xmlctx->name_len; |
| 162 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 163 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 164 | |
| 165 | /* create metadata */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 166 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 167 | xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 168 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 169 | |
| 170 | /* next attribute */ |
| 171 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 172 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 173 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 174 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 175 | if (ret) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 176 | lyd_free_meta_siblings(*meta); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 177 | *meta = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 178 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 179 | return ret; |
| 180 | } |
| 181 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 182 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 183 | lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 184 | { |
| 185 | LY_ERR ret = LY_SUCCESS; |
| 186 | const struct lyxml_ns *ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 187 | void *val_prefix_data; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 188 | LY_VALUE_FORMAT format; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 189 | struct lyd_attr *attr2; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 190 | const char *name, *prefix; |
| 191 | size_t name_len, prefix_len; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 192 | |
| 193 | assert(attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 194 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 195 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 196 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 197 | ns = NULL; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 198 | if (xmlctx->prefix_len) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 199 | /* get namespace of the attribute */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 200 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 201 | if (!ns) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 202 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 203 | ret = LY_EVALID; |
| 204 | goto cleanup; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (*attr) { |
| 209 | attr2 = *attr; |
| 210 | } else { |
| 211 | attr2 = NULL; |
| 212 | } |
| 213 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 214 | /* remember attr prefix, name, and get its content */ |
| 215 | prefix = xmlctx->prefix; |
| 216 | prefix_len = xmlctx->prefix_len; |
| 217 | name = xmlctx->name; |
| 218 | name_len = xmlctx->name_len; |
| 219 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 220 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 221 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 222 | /* get value prefixes */ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 223 | val_prefix_data = NULL; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 224 | LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 225 | &xmlctx->ns, &format, &val_prefix_data), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 226 | |
| 227 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 228 | ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL, |
| 229 | ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, 0); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 230 | LY_CHECK_GOTO(ret, cleanup); |
| 231 | |
| 232 | if (!*attr) { |
| 233 | *attr = attr2; |
| 234 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 235 | |
| 236 | /* next attribute */ |
| 237 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 241 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 242 | lyd_free_attr_siblings(xmlctx->ctx, *attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 243 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 244 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 245 | return ret; |
| 246 | } |
| 247 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 248 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 249 | lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list) |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 250 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 251 | LY_ERR ret = LY_SUCCESS, r; |
| 252 | enum LYXML_PARSER_STATUS next; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 253 | struct ly_set key_set = {0}; |
| 254 | const struct lysc_node *snode; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 255 | uint32_t i, parents_count; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 256 | |
| 257 | assert(list && (list->nodetype == LYS_LIST)); |
| 258 | |
| 259 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 260 | snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 261 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 262 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 263 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Michal Vasko | 12d809c | 2021-03-03 16:34:32 +0100 | [diff] [blame] | 266 | /* remember parent count */ |
| 267 | parents_count = xmlctx->elements.count; |
| 268 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 269 | while (xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 270 | /* find key definition */ |
| 271 | for (i = 0; i < key_set.count; ++i) { |
| 272 | snode = (const struct lysc_node *)key_set.objs[i]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 273 | if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 277 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 278 | |
| 279 | /* skip attributes */ |
| 280 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 281 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 282 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 283 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 286 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 287 | if (i < key_set.count) { |
| 288 | /* validate the value */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 289 | r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 290 | if (!r) { |
| 291 | /* key with a valid value, remove from the set */ |
| 292 | ly_set_rm_index(&key_set, i, NULL); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 296 | /* parser next */ |
| 297 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 298 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 299 | /* skip any children, resursively */ |
Michal Vasko | 12d809c | 2021-03-03 16:34:32 +0100 | [diff] [blame] | 300 | while (xmlctx->status == LYXML_ELEMENT) { |
| 301 | while (parents_count < xmlctx->elements.count) { |
| 302 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 303 | } |
| 304 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 305 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 306 | } |
| 307 | |
| 308 | /* parser next, but do not parse closing element of the list because it would remove its namespaces */ |
| 309 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 310 | LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup); |
| 311 | if (next != LYXML_ELEM_CLOSE) { |
| 312 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 313 | } |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | if (key_set.count) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 317 | /* some keys are missing/did not validate */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 318 | ret = LY_ENOT; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | cleanup: |
| 322 | ly_set_erase(&key_set, NULL); |
| 323 | return ret; |
| 324 | } |
| 325 | |
Michal Vasko | 5c24ed1 | 2021-06-09 09:27:32 +0200 | [diff] [blame] | 326 | /** |
| 327 | * @brief Skip an element with all its descendants. |
| 328 | * |
| 329 | * @param[in] xmlctx XML parser context. |
| 330 | * @return LY_ERR value. |
| 331 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 332 | static LY_ERR |
| 333 | lydxml_data_skip(struct lyxml_ctx *xmlctx) |
| 334 | { |
| 335 | uint32_t parents_count; |
| 336 | |
| 337 | /* remember current number of parents */ |
| 338 | parents_count = xmlctx->elements.count; |
aPiecek | 9cdb9e6 | 2021-05-18 09:46:20 +0200 | [diff] [blame] | 339 | assert(parents_count); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 340 | |
| 341 | /* skip after the content */ |
| 342 | while (xmlctx->status != LYXML_ELEM_CONTENT) { |
| 343 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 344 | } |
| 345 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 346 | |
| 347 | /* skip all children elements, recursively, if any */ |
aPiecek | 9cdb9e6 | 2021-05-18 09:46:20 +0200 | [diff] [blame] | 348 | while (parents_count <= xmlctx->elements.count) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 349 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 350 | } |
| 351 | |
| 352 | /* close element */ |
| 353 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 354 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 355 | |
| 356 | return LY_SUCCESS; |
| 357 | } |
| 358 | |
Michal Vasko | 5c24ed1 | 2021-06-09 09:27:32 +0200 | [diff] [blame] | 359 | /** |
| 360 | * @brief Check that the current element can be parsed as a data node. |
| 361 | * |
| 362 | * @param[in] lydctx XML data parser context. |
| 363 | * @param[in,out] snode Found schema node, set to NULL if data node cannot be created. |
| 364 | * @return LY_ERR value. |
| 365 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 366 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 367 | lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 368 | { |
| 369 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 370 | struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 371 | |
Radek IÅ¡a | 3930fd7 | 2021-03-08 10:48:40 +0100 | [diff] [blame] | 372 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
| 373 | /* only checks specific to opaque nodes */ |
| 374 | return LY_SUCCESS; |
| 375 | } |
| 376 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 377 | if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) { |
| 378 | /* nothing to check */ |
| 379 | return LY_SUCCESS; |
| 380 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 381 | |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 382 | assert(xmlctx->elements.count); |
| 383 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 384 | /* backup parser */ |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 385 | LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx)); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 386 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 387 | /* skip attributes */ |
| 388 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 389 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 390 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 391 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 392 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 393 | if ((*snode)->nodetype & LYD_NODE_TERM) { |
| 394 | /* value may not be valid in which case we parse it as an opaque node */ |
| 395 | if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns)) { |
| 396 | *snode = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 397 | } |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 398 | } else if ((*snode)->nodetype == LYS_LIST) { |
| 399 | /* skip content */ |
| 400 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 401 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 402 | if (lydxml_check_list(xmlctx, *snode)) { |
| 403 | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
| 404 | *snode = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 405 | } |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 406 | } else { |
Radek IÅ¡a | 3930fd7 | 2021-03-08 10:48:40 +0100 | [diff] [blame] | 407 | /* if there is a non-WS value, it cannot be parsed as an inner node */ |
| 408 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 409 | if (!xmlctx->ws_only) { |
| 410 | *snode = NULL; |
| 411 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 414 | restore: |
| 415 | /* restore parser */ |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 416 | lyxml_ctx_restore(xmlctx, &pxmlctx); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 417 | return ret; |
| 418 | } |
| 419 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 420 | /** |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 421 | * @brief Parse XML subtree. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 422 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 423 | * @param[in] lydctx XML YANG data parser context. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 424 | * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
| 425 | * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings, |
| 426 | * this may point to a previously existing node. |
| 427 | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 428 | * @return LY_ERR value. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 429 | */ |
| 430 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 431 | lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 432 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 433 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 434 | const char *prefix, *name, *val; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 435 | size_t prefix_len, name_len; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 436 | struct lyxml_ctx *xmlctx; |
| 437 | const struct ly_ctx *ctx; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 438 | const struct lyxml_ns *ns; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 439 | struct lyd_meta *meta = NULL; |
| 440 | struct lyd_attr *attr = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 441 | const struct lysc_node *snode; |
| 442 | struct lys_module *mod; |
Michal Vasko | a98e3ea | 2021-06-03 09:13:33 +0200 | [diff] [blame] | 443 | uint32_t prev_parse_opts, prev_int_opts; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 444 | struct lyd_node *node = NULL, *anchor; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 445 | void *val_prefix_data = NULL; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 446 | LY_VALUE_FORMAT format; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 447 | uint32_t getnext_opts; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 448 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 449 | assert(parent || first_p); |
| 450 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 451 | xmlctx = lydctx->xmlctx; |
| 452 | ctx = xmlctx->ctx; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 453 | getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 454 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 455 | assert(xmlctx->status == LYXML_ELEMENT); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 456 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 457 | /* remember element prefix and name */ |
| 458 | prefix = xmlctx->prefix; |
| 459 | prefix_len = xmlctx->prefix_len; |
| 460 | name = xmlctx->name; |
| 461 | name_len = xmlctx->name_len; |
| 462 | |
| 463 | /* get the element module */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 464 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 465 | if (!ns) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 466 | LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 467 | ret = LY_EVALID; |
| 468 | goto error; |
| 469 | } |
| 470 | mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri); |
| 471 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 472 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 473 | LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 474 | ret = LY_EVALID; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 475 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 476 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 477 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 478 | /* skip element with children */ |
| 479 | LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error); |
| 480 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 481 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 484 | /* parser next */ |
| 485 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 486 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 487 | /* get the schema node */ |
| 488 | snode = NULL; |
| 489 | if (mod && (!parent || parent->schema)) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 490 | if (!parent && lydctx->ext) { |
Radek Krejci | ba05eab | 2021-03-10 13:19:29 +0100 | [diff] [blame] | 491 | snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 492 | } else { |
| 493 | snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts); |
| 494 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 495 | if (!snode) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 496 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
| 497 | if (parent) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 498 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.", |
| 499 | (int)name_len, name, parent->schema->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 500 | } else if (lydctx->ext) { |
| 501 | if (lydctx->ext->argument) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 502 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.", |
| 503 | (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 504 | } else { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 505 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", |
| 506 | (int)name_len, name, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 507 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 508 | } else { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 509 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
| 510 | (int)name_len, name, mod->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 511 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 512 | ret = LY_EVALID; |
| 513 | goto error; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 514 | } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 515 | /* skip element with children */ |
| 516 | LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error); |
| 517 | return LY_SUCCESS; |
| 518 | } |
| 519 | } else { |
| 520 | /* check that schema node is valid and can be used */ |
| 521 | LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), error); |
| 522 | LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), error); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* create metadata/attributes */ |
| 527 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 528 | if (snode) { |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 529 | ret = lydxml_metadata(lydctx, snode->exts, &meta); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 530 | LY_CHECK_GOTO(ret, error); |
| 531 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 532 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 533 | ret = lydxml_attrs(xmlctx, &attr); |
| 534 | LY_CHECK_GOTO(ret, error); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 539 | if (!snode) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 540 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 541 | |
| 542 | if (xmlctx->ws_only) { |
| 543 | /* ignore WS-only value */ |
Radek IÅ¡a | 017270d | 2021-02-16 10:26:15 +0100 | [diff] [blame] | 544 | if (xmlctx->dynamic) { |
| 545 | free((char *) xmlctx->value); |
| 546 | } |
| 547 | xmlctx->dynamic = 0; |
| 548 | xmlctx->value = ""; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 549 | xmlctx->value_len = 0; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 550 | format = LY_VALUE_XML; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 551 | } else { |
| 552 | /* get value prefixes */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 553 | ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 554 | &xmlctx->ns, &format, &val_prefix_data); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 555 | LY_CHECK_GOTO(ret, error); |
| 556 | } |
| 557 | |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 558 | /* get NS again, it may have been backed up and restored */ |
| 559 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 560 | assert(ns); |
| 561 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 562 | /* create node */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 563 | ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value, |
| 564 | xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, LYD_HINT_DATA, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 565 | LY_CHECK_GOTO(ret, error); |
| 566 | |
| 567 | /* parser next */ |
| 568 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 569 | |
| 570 | /* process children */ |
| 571 | while (xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 572 | ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 573 | LY_CHECK_GOTO(ret, error); |
| 574 | } |
| 575 | } else if (snode->nodetype & LYD_NODE_TERM) { |
| 576 | /* create node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 577 | LY_CHECK_GOTO(ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 578 | &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 579 | LOG_LOCSET(snode, node, NULL, NULL); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 580 | |
| 581 | if (parent && (node->schema->flags & LYS_KEY)) { |
| 582 | /* check the key order, the anchor must never be a key */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 583 | anchor = lyd_insert_get_next_anchor(lyd_child(parent), node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 584 | if (anchor && (anchor->schema->flags & LYS_KEY)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 585 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 586 | LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 587 | ret = LY_EVALID; |
| 588 | goto error; |
| 589 | } else { |
| 590 | LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* parser next */ |
| 596 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 597 | |
| 598 | /* no children expected */ |
| 599 | if (xmlctx->status == LYXML_ELEMENT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 600 | LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 601 | (int)xmlctx->name_len, xmlctx->name, snode->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 602 | ret = LY_EVALID; |
| 603 | goto error; |
| 604 | } |
| 605 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 606 | if (!xmlctx->ws_only) { |
| 607 | /* value in inner node */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 608 | LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 609 | (int)xmlctx->value_len, xmlctx->value, snode->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 610 | ret = LY_EVALID; |
| 611 | goto error; |
| 612 | } |
| 613 | |
| 614 | /* create node */ |
| 615 | ret = lyd_create_inner(snode, &node); |
| 616 | LY_CHECK_GOTO(ret, error); |
| 617 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 618 | LOG_LOCSET(snode, node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 619 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 620 | /* parser next */ |
| 621 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 622 | |
| 623 | /* process children */ |
| 624 | while (xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 625 | ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 626 | LY_CHECK_GOTO(ret, error); |
| 627 | } |
| 628 | |
| 629 | if (snode->nodetype == LYS_LIST) { |
| 630 | /* check all keys exist */ |
| 631 | LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error); |
| 632 | } |
| 633 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 634 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 635 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 636 | ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 637 | LY_CHECK_GOTO(ret, error); |
| 638 | |
| 639 | /* add any missing default children */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 640 | ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts, |
| 641 | &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 642 | LY_CHECK_GOTO(ret, error); |
| 643 | } |
| 644 | |
| 645 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 646 | /* rememeber the RPC/action/notification */ |
| 647 | lydctx->op_node = node; |
| 648 | } |
| 649 | } else if (snode->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 650 | if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) { |
| 651 | /* value in anydata node, we expect a tree */ |
| 652 | LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 653 | (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 654 | ret = LY_EVALID; |
| 655 | goto error; |
| 656 | } |
| 657 | |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 658 | if (!xmlctx->ws_only) { |
| 659 | /* use an arbitrary text value for anyxml */ |
| 660 | lydict_insert(xmlctx->ctx, xmlctx->value, xmlctx->value_len, &val); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 661 | |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 662 | /* parser next */ |
| 663 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 664 | |
| 665 | /* create node */ |
| 666 | ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, &node); |
| 667 | LY_CHECK_GOTO(ret, error); |
| 668 | } else { |
| 669 | /* parser next */ |
| 670 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 671 | |
Michal Vasko | a98e3ea | 2021-06-03 09:13:33 +0200 | [diff] [blame] | 672 | /* update options so that generic data can be parsed */ |
| 673 | prev_parse_opts = lydctx->parse_opts; |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 674 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 675 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
Michal Vasko | a98e3ea | 2021-06-03 09:13:33 +0200 | [diff] [blame] | 676 | prev_int_opts = lydctx->int_opts; |
| 677 | lydctx->int_opts |= LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF; |
| 678 | |
| 679 | /* parse any data tree */ |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 680 | anchor = NULL; |
| 681 | while (xmlctx->status == LYXML_ELEMENT) { |
| 682 | ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL); |
Michal Vasko | a98e3ea | 2021-06-03 09:13:33 +0200 | [diff] [blame] | 683 | if (ret) { |
| 684 | break; |
| 685 | } |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 686 | } |
Michal Vasko | a98e3ea | 2021-06-03 09:13:33 +0200 | [diff] [blame] | 687 | |
| 688 | /* restore options */ |
| 689 | lydctx->parse_opts = prev_parse_opts; |
| 690 | lydctx->int_opts = prev_int_opts; |
| 691 | |
| 692 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 27c4dce | 2021-03-04 15:50:50 +0100 | [diff] [blame] | 693 | |
| 694 | /* create node */ |
| 695 | ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node); |
| 696 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 697 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 698 | } |
| 699 | assert(node); |
| 700 | |
| 701 | /* add/correct flags */ |
| 702 | if (snode) { |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 703 | lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* parser next */ |
| 707 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 708 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 709 | |
| 710 | /* add metadata/attributes */ |
| 711 | if (snode) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 712 | lyd_insert_meta(node, meta, 0); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 713 | } else { |
| 714 | lyd_insert_attr(node, attr); |
| 715 | } |
| 716 | |
| 717 | /* insert, keep first pointer correct */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 718 | lyd_insert_node(parent, first_p, node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 719 | while (!parent && (*first_p)->prev->next) { |
| 720 | *first_p = (*first_p)->prev; |
| 721 | } |
| 722 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 723 | /* rememeber a successfully parsed node */ |
| 724 | if (parsed) { |
| 725 | ly_set_add(parsed, node, 1, NULL); |
| 726 | } |
| 727 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 728 | LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 729 | return LY_SUCCESS; |
| 730 | |
| 731 | error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 732 | LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 733 | lyd_free_meta_siblings(meta); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 734 | lyd_free_attr_siblings(ctx, attr); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 735 | lyd_free_tree(node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 736 | return ret; |
| 737 | } |
| 738 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 739 | /** |
| 740 | * @brief Parse a specific XML element into an opaque node. |
| 741 | * |
| 742 | * @param[in] xmlctx XML parser context. |
| 743 | * @param[in] name Name of the element. |
| 744 | * @param[in] uri URI of the element. |
| 745 | * @param[in] value Whether a value is expected in the element. |
| 746 | * @param[out] evnp Parsed envelope (opaque node). |
| 747 | * @return LY_SUCCESS on success. |
| 748 | * @return LY_ENOT if the specified element did not match. |
| 749 | * @return LY_ERR value on error. |
| 750 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 751 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 752 | lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, ly_bool value, struct lyd_node **envp) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 753 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 754 | LY_ERR rc = LY_SUCCESS; |
| 755 | const struct lyxml_ns *ns; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 756 | struct lyd_attr *attr = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 757 | const char *prefix; |
| 758 | size_t prefix_len; |
| 759 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 760 | assert(xmlctx->status == LYXML_ELEMENT); |
| 761 | if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) { |
| 762 | /* not the expected element */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 763 | return LY_ENOT; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | prefix = xmlctx->prefix; |
| 767 | prefix_len = xmlctx->prefix_len; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 768 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 769 | if (!ns) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 770 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 771 | return LY_EVALID; |
| 772 | } else if (strcmp(ns->uri, uri)) { |
| 773 | /* different namespace */ |
| 774 | return LY_ENOT; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 778 | |
| 779 | /* create attributes */ |
| 780 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 781 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 782 | } |
| 783 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 784 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 785 | if (!value && !xmlctx->ws_only) { |
| 786 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 787 | (int)xmlctx->value_len, xmlctx->value, name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 788 | rc = LY_EVALID; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 789 | goto cleanup; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 790 | } |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 791 | |
| 792 | /* create node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 793 | rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 794 | xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 795 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 796 | |
| 797 | /* assign atributes */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 798 | ((struct lyd_node_opaq *)(*envp))->attr = attr; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 799 | attr = NULL; |
| 800 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 801 | /* parser next element */ |
| 802 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 803 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 804 | cleanup: |
| 805 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
| 806 | if (rc) { |
| 807 | lyd_free_tree(*envp); |
| 808 | *envp = NULL; |
| 809 | } |
| 810 | return rc; |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * @brief Parse all expected non-data XML elements of a NETCONF rpc message. |
| 815 | * |
| 816 | * @param[in] xmlctx XML parser context. |
| 817 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 818 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 819 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 820 | * @return LY_SUCCESS on success. |
| 821 | * @return LY_ERR value on error. |
| 822 | */ |
| 823 | static LY_ERR |
| 824 | lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 825 | { |
| 826 | LY_ERR rc = LY_SUCCESS, r; |
| 827 | struct lyd_node *child; |
| 828 | |
| 829 | assert(envp && !*envp); |
| 830 | |
| 831 | /* parse "rpc" */ |
| 832 | r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 833 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 834 | |
| 835 | /* parse "action", if any */ |
| 836 | r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child); |
| 837 | if (r == LY_SUCCESS) { |
| 838 | /* insert */ |
| 839 | lyd_insert_node(*envp, NULL, child); |
| 840 | |
| 841 | /* NETCONF action */ |
| 842 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION; |
| 843 | *close_elem = 2; |
| 844 | } else if (r == LY_ENOT) { |
| 845 | /* NETCONF RPC */ |
| 846 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC; |
| 847 | *close_elem = 1; |
| 848 | } else { |
| 849 | rc = r; |
| 850 | goto cleanup; |
| 851 | } |
| 852 | |
| 853 | cleanup: |
| 854 | if (rc) { |
| 855 | lyd_free_tree(*envp); |
| 856 | *envp = NULL; |
| 857 | } |
| 858 | return rc; |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * @brief Parse all expected non-data XML elements of a NETCONF notification message. |
| 863 | * |
| 864 | * @param[in] xmlctx XML parser context. |
| 865 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 866 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 867 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 868 | * @return LY_SUCCESS on success. |
| 869 | * @return LY_ERR value on error. |
| 870 | */ |
| 871 | static LY_ERR |
| 872 | lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 873 | { |
| 874 | LY_ERR rc = LY_SUCCESS, r; |
| 875 | struct lyd_node *child; |
| 876 | |
| 877 | assert(envp && !*envp); |
| 878 | |
| 879 | /* parse "notification" */ |
| 880 | r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 881 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 882 | |
| 883 | /* parse "eventTime" */ |
| 884 | r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child); |
| 885 | if (r == LY_ENOT) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 886 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".", |
| 887 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 888 | r = LY_EVALID; |
| 889 | } |
| 890 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 891 | |
| 892 | /* insert */ |
| 893 | lyd_insert_node(*envp, NULL, child); |
| 894 | |
| 895 | /* validate value */ |
| 896 | /* TODO validate child->value as yang:date-and-time */ |
| 897 | |
| 898 | /* finish child parsing */ |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 899 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 900 | assert(xmlctx->status == LYXML_ELEMENT); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 901 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 902 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 903 | rc = LY_EVALID; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 904 | goto cleanup; |
| 905 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 906 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 907 | |
| 908 | /* NETCONF notification */ |
| 909 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF; |
| 910 | *close_elem = 1; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 911 | |
| 912 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 913 | if (rc) { |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 914 | lyd_free_tree(*envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 915 | *envp = NULL; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 916 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 917 | return rc; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 918 | } |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 919 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 920 | /** |
| 921 | * @brief Parse an XML element as an opaque node subtree. |
| 922 | * |
| 923 | * @param[in] xmlctx XML parser context. |
| 924 | * @param[in] parent Parent to append nodes to. |
| 925 | * @return LY_ERR value. |
| 926 | */ |
| 927 | static LY_ERR |
| 928 | lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 929 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 930 | LY_ERR rc = LY_SUCCESS; |
| 931 | const struct lyxml_ns *ns; |
| 932 | struct lyd_attr *attr = NULL; |
| 933 | struct lyd_node *child = NULL; |
| 934 | const char *name, *prefix; |
| 935 | size_t name_len, prefix_len; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 936 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 937 | assert(xmlctx->status == LYXML_ELEMENT); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 938 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 939 | name = xmlctx->name; |
| 940 | name_len = xmlctx->name_len; |
| 941 | prefix = xmlctx->prefix; |
| 942 | prefix_len = xmlctx->prefix_len; |
| 943 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 944 | if (!ns) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 945 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 946 | return LY_EVALID; |
| 947 | } |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 948 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 949 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 950 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 951 | /* create attributes */ |
| 952 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 953 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 954 | } |
| 955 | |
| 956 | /* create node */ |
| 957 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 958 | rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 959 | xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 960 | LY_CHECK_GOTO(rc, cleanup); |
| 961 | |
| 962 | /* assign atributes */ |
| 963 | ((struct lyd_node_opaq *)child)->attr = attr; |
| 964 | attr = NULL; |
| 965 | |
| 966 | /* parser next element */ |
| 967 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 968 | |
| 969 | /* parse all the descendants */ |
| 970 | while (xmlctx->status == LYXML_ELEMENT) { |
| 971 | rc = lydxml_opaq_r(xmlctx, child); |
| 972 | LY_CHECK_GOTO(rc, cleanup); |
| 973 | } |
| 974 | |
| 975 | /* insert */ |
| 976 | lyd_insert_node(parent, NULL, child); |
| 977 | |
| 978 | cleanup: |
| 979 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
| 980 | if (rc) { |
| 981 | lyd_free_tree(child); |
| 982 | } |
| 983 | return rc; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message. |
| 988 | * |
| 989 | * @param[in] xmlctx XML parser context. |
| 990 | * @param[in] parent Parent to append nodes to. |
| 991 | * @return LY_ERR value. |
| 992 | */ |
| 993 | static LY_ERR |
| 994 | lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
| 995 | { |
| 996 | LY_ERR r; |
| 997 | struct lyd_node *child, *iter; |
| 998 | const struct lyxml_ns *ns; |
| 999 | ly_bool no_dup; |
| 1000 | |
| 1001 | /* there must be some child */ |
| 1002 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1003 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\"."); |
| 1004 | return LY_EVALID; |
| 1005 | } |
| 1006 | |
| 1007 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1008 | child = NULL; |
| 1009 | |
| 1010 | /* |
| 1011 | * session-id |
| 1012 | */ |
| 1013 | r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1014 | if (r == LY_SUCCESS) { |
| 1015 | no_dup = 1; |
| 1016 | goto check_child; |
| 1017 | } else if (r != LY_ENOT) { |
| 1018 | goto error; |
| 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * bad-attribute |
| 1023 | */ |
| 1024 | r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1025 | if (r == LY_SUCCESS) { |
| 1026 | no_dup = 1; |
| 1027 | goto check_child; |
| 1028 | } else if (r != LY_ENOT) { |
| 1029 | goto error; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * bad-element |
| 1034 | */ |
| 1035 | r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1036 | if (r == LY_SUCCESS) { |
| 1037 | no_dup = 1; |
| 1038 | goto check_child; |
| 1039 | } else if (r != LY_ENOT) { |
| 1040 | goto error; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * bad-namespace |
| 1045 | */ |
| 1046 | r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1047 | if (r == LY_SUCCESS) { |
| 1048 | no_dup = 1; |
| 1049 | goto check_child; |
| 1050 | } else if (r != LY_ENOT) { |
| 1051 | goto error; |
| 1052 | } |
| 1053 | |
| 1054 | if (r == LY_ENOT) { |
| 1055 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1056 | |
| 1057 | /* learn namespace */ |
| 1058 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
| 1059 | if (!ns) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1060 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1061 | r = LY_EVALID; |
| 1062 | goto error; |
| 1063 | } else if (!strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:base:1.0")) { |
| 1064 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1065 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1066 | r = LY_EVALID; |
| 1067 | goto error; |
| 1068 | } |
| 1069 | |
| 1070 | /* custom elements */ |
| 1071 | r = lydxml_opaq_r(xmlctx, parent); |
| 1072 | LY_CHECK_GOTO(r, error); |
| 1073 | |
| 1074 | no_dup = 0; |
| 1075 | } |
| 1076 | |
| 1077 | check_child: |
| 1078 | /* check for duplicates */ |
| 1079 | if (no_dup) { |
| 1080 | LY_LIST_FOR(lyd_child(parent), iter) { |
| 1081 | if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) && |
| 1082 | (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) { |
| 1083 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".", |
| 1084 | ((struct lyd_node_opaq *)child)->name.name); |
| 1085 | r = LY_EVALID; |
| 1086 | goto error; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /* finish child parsing */ |
| 1092 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1093 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1094 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1095 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1096 | r = LY_EVALID; |
| 1097 | goto error; |
| 1098 | } |
| 1099 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1100 | |
| 1101 | /* insert */ |
| 1102 | lyd_insert_node(parent, NULL, child); |
| 1103 | } |
| 1104 | |
| 1105 | return LY_SUCCESS; |
| 1106 | |
| 1107 | error: |
| 1108 | lyd_free_tree(child); |
| 1109 | return r; |
| 1110 | } |
| 1111 | |
| 1112 | /** |
| 1113 | * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message. |
| 1114 | * |
| 1115 | * @param[in] xmlctx XML parser context. |
| 1116 | * @param[in] parent Parent to append nodes to. |
| 1117 | * @return LY_ERR value. |
| 1118 | */ |
| 1119 | static LY_ERR |
| 1120 | lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
| 1121 | { |
| 1122 | LY_ERR r; |
| 1123 | struct lyd_node *child, *iter; |
| 1124 | const char *val; |
| 1125 | ly_bool no_dup; |
| 1126 | |
| 1127 | /* there must be some child */ |
| 1128 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1129 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\"."); |
| 1130 | return LY_EVALID; |
| 1131 | } |
| 1132 | |
| 1133 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1134 | child = NULL; |
| 1135 | |
| 1136 | /* |
| 1137 | * error-type |
| 1138 | */ |
| 1139 | r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1140 | if (r == LY_SUCCESS) { |
| 1141 | val = ((struct lyd_node_opaq *)child)->value; |
| 1142 | if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) { |
| 1143 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1144 | ((struct lyd_node_opaq *)child)->name.name); |
| 1145 | r = LY_EVALID; |
| 1146 | goto error; |
| 1147 | } |
| 1148 | |
| 1149 | no_dup = 1; |
| 1150 | goto check_child; |
| 1151 | } else if (r != LY_ENOT) { |
| 1152 | goto error; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * error-tag |
| 1157 | */ |
| 1158 | r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1159 | if (r == LY_SUCCESS) { |
| 1160 | val = ((struct lyd_node_opaq *)child)->value; |
| 1161 | if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") && |
| 1162 | strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") && |
| 1163 | strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") && |
| 1164 | strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") && |
| 1165 | strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") && |
| 1166 | strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") && |
| 1167 | strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) { |
| 1168 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1169 | ((struct lyd_node_opaq *)child)->name.name); |
| 1170 | r = LY_EVALID; |
| 1171 | goto error; |
| 1172 | } |
| 1173 | |
| 1174 | no_dup = 1; |
| 1175 | goto check_child; |
| 1176 | } else if (r != LY_ENOT) { |
| 1177 | goto error; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * error-severity |
| 1182 | */ |
| 1183 | r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1184 | if (r == LY_SUCCESS) { |
| 1185 | val = ((struct lyd_node_opaq *)child)->value; |
| 1186 | if (strcmp(val, "error") && strcmp(val, "warning")) { |
| 1187 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1188 | ((struct lyd_node_opaq *)child)->name.name); |
| 1189 | r = LY_EVALID; |
| 1190 | goto error; |
| 1191 | } |
| 1192 | |
| 1193 | no_dup = 1; |
| 1194 | goto check_child; |
| 1195 | } else if (r != LY_ENOT) { |
| 1196 | goto error; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * error-app-tag |
| 1201 | */ |
| 1202 | r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1203 | if (r == LY_SUCCESS) { |
| 1204 | no_dup = 1; |
| 1205 | goto check_child; |
| 1206 | } else if (r != LY_ENOT) { |
| 1207 | goto error; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * error-path |
| 1212 | */ |
| 1213 | r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1214 | if (r == LY_SUCCESS) { |
| 1215 | no_dup = 1; |
| 1216 | goto check_child; |
| 1217 | } else if (r != LY_ENOT) { |
| 1218 | goto error; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * error-message |
| 1223 | */ |
| 1224 | r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1225 | if (r == LY_SUCCESS) { |
| 1226 | no_dup = 1; |
| 1227 | goto check_child; |
| 1228 | } else if (r != LY_ENOT) { |
| 1229 | goto error; |
| 1230 | } |
| 1231 | |
| 1232 | /* error-info */ |
| 1233 | r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1234 | if (r == LY_SUCCESS) { |
| 1235 | /* parse all the descendants */ |
| 1236 | LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error); |
| 1237 | |
| 1238 | no_dup = 0; |
| 1239 | goto check_child; |
| 1240 | } else if (r != LY_ENOT) { |
| 1241 | goto error; |
| 1242 | } |
| 1243 | |
| 1244 | if (r == LY_ENOT) { |
| 1245 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1246 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1247 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1248 | r = LY_EVALID; |
| 1249 | goto error; |
| 1250 | } |
| 1251 | |
| 1252 | check_child: |
| 1253 | /* check for duplicates */ |
| 1254 | if (no_dup) { |
| 1255 | LY_LIST_FOR(lyd_child(parent), iter) { |
| 1256 | if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) && |
| 1257 | (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) { |
| 1258 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".", |
| 1259 | ((struct lyd_node_opaq *)child)->name.name); |
| 1260 | r = LY_EVALID; |
| 1261 | goto error; |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | /* finish child parsing */ |
| 1267 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1268 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1269 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1270 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1271 | r = LY_EVALID; |
| 1272 | goto error; |
| 1273 | } |
| 1274 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1275 | |
| 1276 | /* insert */ |
| 1277 | lyd_insert_node(parent, NULL, child); |
| 1278 | } |
| 1279 | |
| 1280 | return LY_SUCCESS; |
| 1281 | |
| 1282 | error: |
| 1283 | lyd_free_tree(child); |
| 1284 | return r; |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message. |
| 1289 | * |
| 1290 | * @param[in] xmlctx XML parser context. |
| 1291 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 1292 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 1293 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 1294 | * @return LY_SUCCESS on success. |
| 1295 | * @return LY_ERR value on error. |
| 1296 | */ |
| 1297 | static LY_ERR |
| 1298 | lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 1299 | { |
| 1300 | LY_ERR rc = LY_SUCCESS, r; |
| 1301 | struct lyd_node *child = NULL; |
| 1302 | const char *parsed_elem = NULL; |
| 1303 | |
| 1304 | assert(envp && !*envp); |
| 1305 | |
| 1306 | /* parse "rpc-reply" */ |
| 1307 | r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1308 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 1309 | |
| 1310 | /* there must be some child */ |
| 1311 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1312 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\"."); |
| 1313 | rc = LY_EVALID; |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 1314 | goto cleanup; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1317 | /* try to parse "ok" */ |
| 1318 | r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1319 | if (r == LY_SUCCESS) { |
| 1320 | /* insert */ |
| 1321 | lyd_insert_node(*envp, NULL, child); |
| 1322 | |
| 1323 | /* finish child parsing */ |
| 1324 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1325 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1326 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1327 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1328 | rc = LY_EVALID; |
| 1329 | goto cleanup; |
| 1330 | } |
| 1331 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1332 | |
| 1333 | /* success */ |
| 1334 | parsed_elem = "ok"; |
| 1335 | goto finish; |
| 1336 | } else if (r != LY_ENOT) { |
| 1337 | rc = r; |
| 1338 | goto cleanup; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1339 | } |
| 1340 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1341 | /* try to parse all "rpc-error" elements */ |
| 1342 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1343 | r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1344 | if (r == LY_ENOT) { |
| 1345 | break; |
| 1346 | } else if (r) { |
| 1347 | rc = r; |
| 1348 | goto cleanup; |
| 1349 | } |
| 1350 | |
| 1351 | /* insert */ |
| 1352 | lyd_insert_node(*envp, NULL, child); |
| 1353 | |
| 1354 | /* parse all children of "rpc-error" */ |
| 1355 | LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup); |
| 1356 | |
| 1357 | /* finish child parsing */ |
| 1358 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 1359 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1360 | |
| 1361 | parsed_elem = "rpc-error"; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1362 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1363 | |
| 1364 | finish: |
| 1365 | if (parsed_elem) { |
| 1366 | /* NETCONF rpc-reply with no data */ |
| 1367 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1368 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1369 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1370 | (int)xmlctx->name_len, xmlctx->name, parsed_elem); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1371 | rc = LY_EVALID; |
| 1372 | goto cleanup; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | /* NETCONF rpc-reply */ |
| 1377 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_REPLY; |
| 1378 | *close_elem = 1; |
| 1379 | |
| 1380 | cleanup: |
| 1381 | if (rc) { |
| 1382 | lyd_free_tree(*envp); |
| 1383 | *envp = NULL; |
| 1384 | } |
| 1385 | return rc; |
| 1386 | } |
| 1387 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1388 | LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1389 | lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1390 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
| 1391 | struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1392 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1393 | LY_ERR rc = LY_SUCCESS; |
| 1394 | struct lyd_xml_ctx *lydctx; |
Michal Vasko | 2ca9f9e | 2021-07-02 09:21:36 +0200 | [diff] [blame] | 1395 | uint32_t i, int_opts = 0, close_elem = 0; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1396 | ly_bool parsed_data_nodes = 0; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1397 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1398 | assert(ctx && in && lydctx_p); |
| 1399 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1400 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1401 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1402 | /* init context */ |
| 1403 | lydctx = calloc(1, sizeof *lydctx); |
| 1404 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
| 1405 | LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
| 1406 | lydctx->parse_opts = parse_opts; |
| 1407 | lydctx->val_opts = val_opts; |
| 1408 | lydctx->free = lyd_xml_ctx_free; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1409 | lydctx->ext = ext; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1410 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1411 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1412 | case LYD_TYPE_DATA_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1413 | int_opts = LYD_INTOPT_WITH_SIBLINGS; |
| 1414 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1415 | case LYD_TYPE_RPC_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1416 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS; |
| 1417 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1418 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1419 | int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS; |
| 1420 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1421 | case LYD_TYPE_REPLY_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1422 | int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS; |
| 1423 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1424 | case LYD_TYPE_RPC_NETCONF: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1425 | assert(!parent); |
| 1426 | LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup); |
| 1427 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1428 | case LYD_TYPE_NOTIF_NETCONF: |
| 1429 | assert(!parent); |
| 1430 | LY_CHECK_GOTO(rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup); |
| 1431 | break; |
| 1432 | case LYD_TYPE_REPLY_NETCONF: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1433 | assert(parent); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1434 | LY_CHECK_GOTO(rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1435 | break; |
| 1436 | } |
| 1437 | lydctx->int_opts = int_opts; |
| 1438 | |
| 1439 | /* find the operation node if it exists already */ |
| 1440 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1441 | |
| 1442 | /* parse XML data */ |
| 1443 | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
| 1444 | LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup); |
| 1445 | parsed_data_nodes = 1; |
| 1446 | |
| 1447 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1448 | break; |
| 1449 | } |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1452 | /* close all opened elements */ |
| 1453 | for (i = 0; i < close_elem; ++i) { |
| 1454 | if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1455 | assert(lydctx->xmlctx->status == LYXML_ELEMENT); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1456 | LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", |
| 1457 | (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1458 | rc = LY_EVALID; |
| 1459 | goto cleanup; |
| 1460 | } |
| 1461 | |
| 1462 | LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup); |
Michal Vasko | 4189c0f | 2020-08-13 09:05:22 +0200 | [diff] [blame] | 1463 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1464 | |
| 1465 | /* check final state */ |
| 1466 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) { |
| 1467 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1468 | rc = LY_EVALID; |
| 1469 | goto cleanup; |
| 1470 | } |
| 1471 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
| 1472 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1473 | rc = LY_EVALID; |
| 1474 | goto cleanup; |
| 1475 | } |
| 1476 | |
| 1477 | if (!parsed_data_nodes) { |
| 1478 | /* no data nodes were parsed */ |
| 1479 | lydctx->op_node = NULL; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1483 | /* there should be no unres stored if validation should be skipped */ |
| 1484 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 1485 | !lydctx->node_when.count && !lydctx->node_exts.count)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1486 | |
| 1487 | if (rc) { |
| 1488 | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
| 1489 | } else { |
| 1490 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 1491 | |
| 1492 | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
| 1493 | lyxml_ctx_free(lydctx->xmlctx); |
| 1494 | lydctx->xmlctx = NULL; |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 1495 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1496 | return rc; |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 1497 | } |