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> |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * @brief XML data parser for libyang |
| 6 | * |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 7 | * Copyright (c) 2019 - 2022 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include "common.h" |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 24 | #include "compat.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include "context.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 26 | #include "dict.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 27 | #include "in_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 29 | #include "parser_data.h" |
| 30 | #include "parser_internal.h" |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 31 | #include "plugins_exts.h" |
Michal Vasko | 51de7b7 | 2022-04-29 09:50:22 +0200 | [diff] [blame] | 32 | #include "plugins_internal.h" |
| 33 | #include "schema_compile_node.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "set.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 35 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 36 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree_data_internal.h" |
| 38 | #include "tree_schema.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 39 | #include "tree_schema_internal.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 40 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "xml.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 42 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 43 | static LY_ERR lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 44 | struct ly_set *parsed); |
| 45 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 46 | void |
| 47 | lyd_xml_ctx_free(struct lyd_ctx *lydctx) |
| 48 | { |
| 49 | struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx; |
| 50 | |
| 51 | lyd_ctx_free(lydctx); |
| 52 | lyxml_ctx_free(ctx->xmlctx); |
| 53 | free(ctx); |
| 54 | } |
| 55 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 56 | /** |
| 57 | * @brief Parse and create XML metadata. |
| 58 | * |
| 59 | * @param[in] lydctx XML data parser context. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 60 | * @param[in] sparent Schema node of the parent. |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 61 | * @param[out] meta List of created metadata instances. |
| 62 | * @return LY_ERR value. |
| 63 | */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 64 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 65 | lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 66 | { |
aPiecek | 1c4da36 | 2021-04-29 14:26:34 +0200 | [diff] [blame] | 67 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 68 | const struct lyxml_ns *ns; |
| 69 | struct lys_module *mod; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 70 | const char *name; |
| 71 | size_t name_len; |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 72 | LY_ARRAY_COUNT_TYPE u; |
| 73 | ly_bool filter_attrs = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 74 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 75 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 76 | *meta = NULL; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 77 | |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 78 | LOG_LOCSET(sparent, NULL, NULL, NULL); |
| 79 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 80 | /* check for NETCONF filter unqualified attributes */ |
Michal Vasko | 1b2a3f4 | 2022-12-20 09:38:28 +0100 | [diff] [blame] | 81 | if (!strcmp(sparent->module->name, "notifications")) { |
| 82 | /* ancient module that does not even use the extension */ |
| 83 | filter_attrs = 1; |
| 84 | } else { |
| 85 | LY_ARRAY_FOR(sparent->exts, u) { |
| 86 | if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") && |
| 87 | !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) { |
| 88 | filter_attrs = 1; |
| 89 | break; |
| 90 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 94 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 95 | if (!xmlctx->prefix_len) { |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 96 | /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */ |
| 97 | if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) || |
| 98 | !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) { |
| 99 | mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf"); |
| 100 | if (!mod) { |
| 101 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
| 102 | "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes."); |
| 103 | ret = LY_ENOTFOUND; |
| 104 | goto cleanup; |
| 105 | } |
| 106 | goto create_meta; |
| 107 | } |
| 108 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 109 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 110 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 111 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 112 | ret = LY_EVALID; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 113 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 114 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 115 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 116 | /* skip attr */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 117 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 118 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 119 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | |
| 123 | /* get namespace of the attribute to find its annotation definition */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 124 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 125 | if (!ns) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 126 | /* unknown namespace, XML error */ |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 127 | 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] | 128 | ret = LY_ENOTFOUND; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 129 | goto cleanup; |
| 130 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 131 | |
| 132 | /* get the module with metadata definition */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 133 | mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 134 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 135 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 136 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 137 | "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] | 138 | ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", |
| 139 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 140 | ret = LY_ENOTFOUND; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 141 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 142 | } |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 143 | |
| 144 | /* skip attr */ |
| 145 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 146 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 147 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 148 | continue; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Michal Vasko | 45791ad | 2021-06-17 08:45:03 +0200 | [diff] [blame] | 151 | create_meta: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 152 | /* remember meta name and get its content */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 153 | name = xmlctx->name; |
| 154 | name_len = xmlctx->name_len; |
| 155 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 156 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 157 | |
| 158 | /* create metadata */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 159 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 160 | xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 161 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 162 | |
| 163 | /* next attribute */ |
| 164 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 165 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 166 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 167 | cleanup: |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 168 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 169 | if (ret) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 170 | lyd_free_meta_siblings(*meta); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 171 | *meta = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 172 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 176 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 177 | lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 178 | { |
| 179 | LY_ERR ret = LY_SUCCESS; |
| 180 | const struct lyxml_ns *ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 181 | void *val_prefix_data; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 182 | LY_VALUE_FORMAT format; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 183 | struct lyd_attr *attr2; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 184 | const char *name, *prefix; |
| 185 | size_t name_len, prefix_len; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 186 | |
| 187 | assert(attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 188 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 189 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 190 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 191 | if (*attr) { |
| 192 | attr2 = *attr; |
| 193 | } else { |
| 194 | attr2 = NULL; |
| 195 | } |
| 196 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 197 | /* remember attr prefix, name, and get its content */ |
| 198 | prefix = xmlctx->prefix; |
| 199 | prefix_len = xmlctx->prefix_len; |
| 200 | name = xmlctx->name; |
| 201 | name_len = xmlctx->name_len; |
| 202 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 203 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 204 | |
Michal Vasko | e137fc4 | 2021-07-22 11:53:13 +0200 | [diff] [blame] | 205 | /* handle special "xml" attribute prefix */ |
| 206 | if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 207 | name = prefix; |
| 208 | name_len += 1 + prefix_len; |
| 209 | prefix = NULL; |
| 210 | prefix_len = 0; |
| 211 | } |
| 212 | |
| 213 | /* find namespace of the attribute, if any */ |
| 214 | ns = NULL; |
| 215 | if (prefix_len) { |
| 216 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 217 | if (!ns) { |
| 218 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix); |
| 219 | ret = LY_EVALID; |
| 220 | goto cleanup; |
| 221 | } |
| 222 | } |
| 223 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 224 | /* get value prefixes */ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 225 | val_prefix_data = NULL; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 226 | 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] | 227 | &xmlctx->ns, &format, &val_prefix_data), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 228 | |
| 229 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 230 | ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL, |
Michal Vasko | e5e49e9 | 2022-02-01 13:15:08 +0100 | [diff] [blame] | 231 | ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, |
| 232 | LYD_HINT_DATA); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 233 | LY_CHECK_GOTO(ret, cleanup); |
| 234 | |
| 235 | if (!*attr) { |
| 236 | *attr = attr2; |
| 237 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 238 | |
| 239 | /* next attribute */ |
| 240 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 244 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 245 | lyd_free_attr_siblings(xmlctx->ctx, *attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 246 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 247 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 251 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 252 | lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list) |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 253 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 254 | LY_ERR ret = LY_SUCCESS, r; |
| 255 | enum LYXML_PARSER_STATUS next; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 256 | struct ly_set key_set = {0}; |
| 257 | const struct lysc_node *snode; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 258 | uint32_t i, parents_count; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 259 | |
| 260 | assert(list && (list->nodetype == LYS_LIST)); |
| 261 | |
| 262 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 263 | snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 264 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 265 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 266 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Michal Vasko | 12d809c | 2021-03-03 16:34:32 +0100 | [diff] [blame] | 269 | /* remember parent count */ |
| 270 | parents_count = xmlctx->elements.count; |
| 271 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 272 | while (xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 273 | /* find key definition */ |
| 274 | for (i = 0; i < key_set.count; ++i) { |
| 275 | snode = (const struct lysc_node *)key_set.objs[i]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 276 | if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 280 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 281 | |
| 282 | /* skip attributes */ |
| 283 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 284 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 285 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 286 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 289 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 290 | if (i < key_set.count) { |
| 291 | /* validate the value */ |
Michal Vasko | 583b464 | 2023-05-25 10:39:34 +0200 | [diff] [blame] | 292 | r = ly_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 293 | if (!r) { |
| 294 | /* key with a valid value, remove from the set */ |
| 295 | ly_set_rm_index(&key_set, i, NULL); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 299 | /* parser next */ |
| 300 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 301 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 302 | /* skip any children, resursively */ |
Michal Vasko | 12d809c | 2021-03-03 16:34:32 +0100 | [diff] [blame] | 303 | while (xmlctx->status == LYXML_ELEMENT) { |
| 304 | while (parents_count < xmlctx->elements.count) { |
| 305 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 306 | } |
| 307 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 308 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 309 | } |
| 310 | |
| 311 | /* parser next, but do not parse closing element of the list because it would remove its namespaces */ |
| 312 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 313 | LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup); |
| 314 | if (next != LYXML_ELEM_CLOSE) { |
| 315 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 316 | } |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (key_set.count) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 320 | /* some keys are missing/did not validate */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 321 | ret = LY_ENOT; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | cleanup: |
| 325 | ly_set_erase(&key_set, NULL); |
| 326 | return ret; |
| 327 | } |
| 328 | |
Michal Vasko | 5c24ed1 | 2021-06-09 09:27:32 +0200 | [diff] [blame] | 329 | /** |
| 330 | * @brief Skip an element with all its descendants. |
| 331 | * |
| 332 | * @param[in] xmlctx XML parser context. |
| 333 | * @return LY_ERR value. |
| 334 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 335 | static LY_ERR |
| 336 | lydxml_data_skip(struct lyxml_ctx *xmlctx) |
| 337 | { |
| 338 | uint32_t parents_count; |
| 339 | |
| 340 | /* remember current number of parents */ |
| 341 | parents_count = xmlctx->elements.count; |
aPiecek | 9cdb9e6 | 2021-05-18 09:46:20 +0200 | [diff] [blame] | 342 | assert(parents_count); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 343 | |
| 344 | /* skip after the content */ |
| 345 | while (xmlctx->status != LYXML_ELEM_CONTENT) { |
| 346 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 347 | } |
| 348 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 349 | |
| 350 | /* skip all children elements, recursively, if any */ |
aPiecek | 9cdb9e6 | 2021-05-18 09:46:20 +0200 | [diff] [blame] | 351 | while (parents_count <= xmlctx->elements.count) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 352 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 353 | } |
| 354 | |
| 355 | /* close element */ |
| 356 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 357 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 358 | |
| 359 | return LY_SUCCESS; |
| 360 | } |
| 361 | |
Michal Vasko | 5c24ed1 | 2021-06-09 09:27:32 +0200 | [diff] [blame] | 362 | /** |
| 363 | * @brief Check that the current element can be parsed as a data node. |
| 364 | * |
| 365 | * @param[in] lydctx XML data parser context. |
| 366 | * @param[in,out] snode Found schema node, set to NULL if data node cannot be created. |
| 367 | * @return LY_ERR value. |
| 368 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 369 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 370 | 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] | 371 | { |
| 372 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 373 | struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 374 | |
Radek IÅ¡a | 3930fd7 | 2021-03-08 10:48:40 +0100 | [diff] [blame] | 375 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
| 376 | /* only checks specific to opaque nodes */ |
| 377 | return LY_SUCCESS; |
| 378 | } |
| 379 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 380 | if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) { |
| 381 | /* nothing to check */ |
| 382 | return LY_SUCCESS; |
| 383 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 384 | |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 385 | assert(xmlctx->elements.count); |
| 386 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 387 | /* backup parser */ |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 388 | LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx)); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 389 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 390 | /* skip attributes */ |
| 391 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 392 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 393 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 394 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 395 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 396 | if ((*snode)->nodetype & LYD_NODE_TERM) { |
| 397 | /* value may not be valid in which case we parse it as an opaque node */ |
Michal Vasko | 583b464 | 2023-05-25 10:39:34 +0200 | [diff] [blame] | 398 | if (ly_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA)) { |
Michal Vasko | d0237d4 | 2021-07-12 14:49:46 +0200 | [diff] [blame] | 399 | LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, xmlctx->value_len, |
| 400 | xmlctx->value); |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 401 | *snode = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 402 | } |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 403 | } else if ((*snode)->nodetype == LYS_LIST) { |
| 404 | /* skip content */ |
| 405 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 406 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 407 | if (lydxml_check_list(xmlctx, *snode)) { |
| 408 | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
Michal Vasko | d0237d4 | 2021-07-12 14:49:46 +0200 | [diff] [blame] | 409 | LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name); |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 410 | *snode = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 411 | } |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 412 | } else { |
Radek IÅ¡a | 3930fd7 | 2021-03-08 10:48:40 +0100 | [diff] [blame] | 413 | /* if there is a non-WS value, it cannot be parsed as an inner node */ |
| 414 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 415 | if (!xmlctx->ws_only) { |
| 416 | *snode = NULL; |
| 417 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Michal Vasko | 1385466 | 2021-06-09 09:27:50 +0200 | [diff] [blame] | 420 | restore: |
| 421 | /* restore parser */ |
Michal Vasko | da8fbbf | 2021-06-16 11:44:44 +0200 | [diff] [blame] | 422 | lyxml_ctx_restore(xmlctx, &pxmlctx); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 426 | /** |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 427 | * @brief Get sensible data hints for an opaque node. |
| 428 | * |
| 429 | * @param[in] name Node name. |
| 430 | * @param[in] name_len Length of @p name. |
| 431 | * @param[in] value Node value. |
| 432 | * @param[in] value_len Length of @p value. |
| 433 | * @param[in] first Node first sibling. |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 434 | * @param[in] ns Node module namespace, NULL for no namespace. |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 435 | * @param[out] hints Data hints to use. |
| 436 | * @param[out] anchor Anchor to insert after in case of a list. |
| 437 | */ |
| 438 | static void |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 439 | lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, const struct lyd_node *first, |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 440 | const char *ns, uint32_t *hints, struct lyd_node **anchor) |
| 441 | { |
| 442 | struct lyd_node_opaq *opaq; |
| 443 | char *ptr; |
| 444 | long num; |
| 445 | |
| 446 | *hints = 0; |
| 447 | *anchor = NULL; |
| 448 | |
| 449 | if (!value_len) { |
| 450 | /* no value */ |
| 451 | *hints |= LYD_VALHINT_EMPTY; |
| 452 | } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) { |
| 453 | /* boolean value */ |
| 454 | *hints |= LYD_VALHINT_BOOLEAN; |
| 455 | } else { |
| 456 | num = strtol(value, &ptr, 10); |
| 457 | if ((unsigned)(ptr - value) == value_len) { |
| 458 | /* number value */ |
| 459 | *hints |= LYD_VALHINT_DECNUM; |
| 460 | if ((num < INT32_MIN) || (num > UINT32_MAX)) { |
| 461 | /* large number */ |
| 462 | *hints |= LYD_VALHINT_NUM64; |
| 463 | } |
| 464 | } else { |
| 465 | /* string value */ |
| 466 | *hints |= LYD_VALHINT_STRING; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if (!first) { |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | /* search backwards to find the last instance */ |
| 475 | do { |
| 476 | first = first->prev; |
| 477 | if (first->schema) { |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | opaq = (struct lyd_node_opaq *)first; |
| 482 | assert(opaq->format == LY_VALUE_XML); |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 483 | if (!ly_strncmp(opaq->name.name, name, name_len) && |
| 484 | ((ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) { |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 485 | if (opaq->value && opaq->value[0]) { |
| 486 | /* leaf-list nodes */ |
| 487 | opaq->hints |= LYD_NODEHINT_LEAFLIST; |
| 488 | *hints |= LYD_NODEHINT_LEAFLIST; |
| 489 | } else { |
| 490 | /* list nodes */ |
| 491 | opaq->hints |= LYD_NODEHINT_LIST; |
| 492 | *hints |= LYD_NODEHINT_LIST; |
| 493 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 494 | *anchor = (struct lyd_node *)first; |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | } while (first->prev->next); |
| 498 | } |
| 499 | |
| 500 | /** |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 501 | * @brief Get schema node for the current element. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 502 | * |
| 503 | * @param[in] lydctx XML data parser context. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 504 | * @param[in] parent Parsed parent data node, if any. |
| 505 | * @param[in] prefix Element prefix, if any. |
| 506 | * @param[in] prefix_len Length of @p prefix. |
| 507 | * @param[in] name Element name. |
| 508 | * @param[in] name_len Length of @p name. |
| 509 | * @param[out] snode Found schema node, NULL if no suitable was found. |
| 510 | * @param[out] ext Extension instance that provided @p snode, if any. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 511 | * @return LY_SUCCESS on success; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 512 | * @return LY_ERR on error. |
| 513 | */ |
| 514 | static LY_ERR |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 515 | lydxml_subtree_get_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 516 | const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext) |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 517 | { |
| 518 | LY_ERR r; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 519 | struct lyxml_ctx *xmlctx; |
| 520 | const struct ly_ctx *ctx; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 521 | const struct lyxml_ns *ns; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 522 | struct lys_module *mod; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 523 | uint32_t getnext_opts; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 524 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 525 | xmlctx = lydctx->xmlctx; |
| 526 | ctx = xmlctx->ctx; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 527 | getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 528 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 529 | *snode = NULL; |
| 530 | *ext = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 531 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 532 | /* get current namespace */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 533 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 534 | if (!ns) { |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 535 | if (lydctx->int_opts & LYD_INTOPT_ANY) { |
| 536 | goto unknown_module; |
| 537 | } |
| 538 | |
| 539 | if (prefix_len) { |
| 540 | LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix); |
| 541 | } else { |
| 542 | LOGVAL(ctx, LYVE_REFERENCE, "Missing XML namespace."); |
| 543 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 544 | return LY_EVALID; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 545 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 546 | |
| 547 | /* get the element module, use parent context if possible because of extensions */ |
| 548 | mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 549 | if (!mod) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 550 | /* check for extension data */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 551 | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len, |
| 552 | snode, ext); |
| 553 | if (r != LY_ENOT) { |
| 554 | /* success or error */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 555 | return r; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 558 | unknown_module: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 559 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 560 | LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 561 | return LY_EVALID; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 562 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 563 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 566 | /* get the schema node */ |
Michal Vasko | 81008a5 | 2021-07-21 16:06:12 +0200 | [diff] [blame] | 567 | if (mod) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 568 | if (!parent && lydctx->ext) { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 569 | *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] | 570 | } else { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 571 | *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 572 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 573 | if (!*snode) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 574 | /* check for extension data */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 575 | r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, |
| 576 | name_len, snode, ext); |
| 577 | if (r != LY_ENOT) { |
| 578 | /* success or error */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 579 | return r; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 580 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 581 | |
| 582 | /* unknown data node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 583 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
| 584 | if (parent) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 585 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.", |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 586 | (int)name_len, name, LYD_NAME(parent)); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 587 | } else if (lydctx->ext) { |
| 588 | if (lydctx->ext->argument) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 589 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.", |
| 590 | (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 591 | } else { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 592 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", |
| 593 | (int)name_len, name, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 594 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 595 | } else { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 596 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
| 597 | (int)name_len, name, mod->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 598 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 599 | return LY_EVALID; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 600 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 601 | return LY_SUCCESS; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 602 | } else { |
| 603 | /* check that schema node is valid and can be used */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 604 | LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode)); |
| 605 | LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode)); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 609 | return LY_SUCCESS; |
| 610 | } |
| 611 | |
| 612 | /** |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 613 | * @brief Parse an XML opque node. |
| 614 | * |
| 615 | * @param[in] lydctx XML YANG data parser context. |
| 616 | * @param[in] sibling Existing sibling node, if any. |
| 617 | * @param[in] prefix Parsed node prefix. |
| 618 | * @param[in] prefix_len Length of @p prefix. |
| 619 | * @param[in] name Parsed node name. |
| 620 | * @param[in] name_len Length of @p name. |
| 621 | * @param[out] insert_anchor Optional anchor node for inserting this node. |
| 622 | * @param[out] node Created node. |
| 623 | * @return LY_ERR value. |
| 624 | */ |
| 625 | static LY_ERR |
| 626 | lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len, |
| 627 | const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node) |
| 628 | { |
| 629 | LY_ERR rc = LY_SUCCESS; |
| 630 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 631 | const char *ns_uri; |
| 632 | const struct lyxml_ns *ns; |
| 633 | uint32_t hints; |
| 634 | void *val_prefix_data = NULL; |
| 635 | LY_VALUE_FORMAT format; |
| 636 | |
| 637 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
| 638 | |
| 639 | *node = NULL; |
| 640 | |
| 641 | if (xmlctx->ws_only) { |
| 642 | /* ignore WS-only value */ |
| 643 | if (xmlctx->dynamic) { |
| 644 | free((char *)xmlctx->value); |
| 645 | } |
| 646 | xmlctx->dynamic = 0; |
| 647 | xmlctx->value = ""; |
| 648 | xmlctx->value_len = 0; |
| 649 | format = LY_VALUE_XML; |
| 650 | } else { |
| 651 | /* get value prefixes */ |
| 652 | rc = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, &format, |
| 653 | &val_prefix_data); |
| 654 | LY_CHECK_GOTO(rc, cleanup); |
| 655 | } |
| 656 | |
| 657 | /* get NS again, it may have been backed up and restored */ |
| 658 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 659 | ns_uri = ns ? ns->uri : NULL; |
| 660 | |
| 661 | /* get best-effort node hints */ |
| 662 | lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor); |
| 663 | |
| 664 | /* create node */ |
| 665 | rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, |
| 666 | xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, hints, node); |
| 667 | LY_CHECK_GOTO(rc, cleanup); |
| 668 | val_prefix_data = NULL; |
| 669 | |
| 670 | /* parser next */ |
| 671 | rc = lyxml_ctx_next(xmlctx); |
| 672 | LY_CHECK_GOTO(rc, cleanup); |
| 673 | |
| 674 | /* process children */ |
| 675 | while (xmlctx->status == LYXML_ELEMENT) { |
| 676 | rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
| 677 | LY_CHECK_GOTO(rc, cleanup); |
| 678 | } |
| 679 | |
| 680 | cleanup: |
| 681 | ly_free_prefix_data(format, val_prefix_data); |
| 682 | if (rc) { |
| 683 | lyd_free_tree(*node); |
| 684 | *node = NULL; |
| 685 | } |
| 686 | return rc; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * @brief Parse an XML leaf/leaf-list node. |
| 691 | * |
| 692 | * @param[in] lydctx XML YANG data parser context. |
| 693 | * @param[in] parent Parent node, if any. |
| 694 | * @param[in] snode Schema node of the new node. |
| 695 | * @param[out] node Created node. |
| 696 | * @return LY_ERR value. |
| 697 | */ |
| 698 | static LY_ERR |
| 699 | lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 700 | struct lyd_node **node) |
| 701 | { |
| 702 | LY_ERR r, rc = LY_SUCCESS; |
| 703 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 704 | struct lyd_node *anchor; |
| 705 | |
| 706 | *node = NULL; |
| 707 | |
| 708 | /* create node */ |
| 709 | r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, |
| 710 | LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node); |
| 711 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 712 | |
| 713 | if (*node) { |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 714 | LOG_LOCSET(NULL, *node, NULL, NULL); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 717 | if (*node && parent && (snode->flags & LYS_KEY)) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 718 | /* check the key order, the anchor must never be a key */ |
| 719 | anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node); |
| 720 | if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) { |
| 721 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
| 722 | LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name); |
| 723 | r = LY_EVALID; |
| 724 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 725 | } else { |
| 726 | LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name); |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* parser next */ |
| 732 | r = lyxml_ctx_next(xmlctx); |
| 733 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 734 | |
| 735 | /* no children expected */ |
| 736 | if (xmlctx->status == LYXML_ELEMENT) { |
| 737 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.", |
| 738 | (int)xmlctx->name_len, xmlctx->name, snode->name); |
| 739 | r = LY_EVALID; |
| 740 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 741 | } |
| 742 | |
| 743 | cleanup: |
| 744 | if (*node) { |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 745 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 746 | } |
| 747 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
| 748 | lyd_free_tree(*node); |
| 749 | *node = NULL; |
| 750 | } |
| 751 | return rc; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * @brief Parse an XML inner node. |
| 756 | * |
| 757 | * @param[in] lydctx XML YANG data parser context. |
| 758 | * @param[in] snode Schema node of the new node. |
| 759 | * @param[in] ext Extension instance of @p snode, if any. |
| 760 | * @param[out] node Created node. |
| 761 | * @return LY_ERR value. |
| 762 | */ |
| 763 | static LY_ERR |
| 764 | lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
| 765 | struct lyd_node **node) |
| 766 | { |
| 767 | LY_ERR r, rc = LY_SUCCESS; |
| 768 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 769 | uint32_t prev_parse_opts = lydctx->parse_opts; |
| 770 | |
| 771 | *node = NULL; |
| 772 | |
| 773 | if (!xmlctx->ws_only) { |
| 774 | /* value in inner node */ |
| 775 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.", |
| 776 | (int)xmlctx->value_len, xmlctx->value, snode->name); |
| 777 | r = LY_EVALID; |
| 778 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 779 | } |
| 780 | |
| 781 | /* create node */ |
| 782 | rc = lyd_create_inner(snode, node); |
| 783 | LY_CHECK_GOTO(rc, cleanup); |
| 784 | |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 785 | assert(*node); |
| 786 | LOG_LOCSET(NULL, *node, NULL, NULL); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 787 | |
| 788 | /* parser next */ |
| 789 | rc = lyxml_ctx_next(xmlctx); |
| 790 | LY_CHECK_GOTO(rc, cleanup); |
| 791 | |
| 792 | if (ext) { |
| 793 | /* only parse these extension data and validate afterwards */ |
| 794 | lydctx->parse_opts |= LYD_PARSE_ONLY; |
| 795 | } |
| 796 | |
| 797 | /* process children */ |
| 798 | while (xmlctx->status == LYXML_ELEMENT) { |
| 799 | r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
| 800 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 801 | } |
| 802 | |
| 803 | /* restore options */ |
| 804 | lydctx->parse_opts = prev_parse_opts; |
| 805 | |
| 806 | if (snode->nodetype == LYS_LIST) { |
| 807 | /* check all keys exist */ |
| 808 | r = lyd_parse_check_keys(*node); |
Michal Vasko | 202d816 | 2023-03-01 14:42:19 +0100 | [diff] [blame] | 809 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 813 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
| 814 | r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL); |
| 815 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 816 | |
| 817 | /* add any missing default children */ |
| 818 | r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types, |
| 819 | &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
| 820 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 821 | } |
| 822 | |
| 823 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 824 | /* rememeber the RPC/action/notification */ |
| 825 | lydctx->op_node = *node; |
| 826 | } |
| 827 | |
| 828 | cleanup: |
| 829 | if (*node) { |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 830 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 831 | } |
| 832 | lydctx->parse_opts = prev_parse_opts; |
Michal Vasko | 202d816 | 2023-03-01 14:42:19 +0100 | [diff] [blame] | 833 | if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
| 834 | /* list without keys is unusable or an error */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 835 | lyd_free_tree(*node); |
| 836 | *node = NULL; |
| 837 | } |
| 838 | return rc; |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * @brief Parse an XML anyxml/anydata node. |
| 843 | * |
| 844 | * @param[in] lydctx XML YANG data parser context. |
| 845 | * @param[in] snode Schema node of the new node. |
| 846 | * @param[in] ext Extension instance of @p snode, if any. |
| 847 | * @param[out] node Created node. |
| 848 | * @return LY_ERR value. |
| 849 | */ |
| 850 | static LY_ERR |
| 851 | lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
| 852 | struct lyd_node **node) |
| 853 | { |
| 854 | LY_ERR r, rc = LY_SUCCESS; |
| 855 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 856 | uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts; |
| 857 | struct lyd_node *child = NULL; |
| 858 | char *val = NULL; |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 859 | ly_bool log_node = 0; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 860 | |
| 861 | *node = NULL; |
| 862 | |
| 863 | if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) { |
| 864 | /* value in anydata node, we expect a tree */ |
| 865 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.", |
| 866 | (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name); |
| 867 | r = LY_EVALID; |
| 868 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 869 | } |
| 870 | |
| 871 | if (!xmlctx->ws_only) { |
| 872 | /* use an arbitrary text value for anyxml */ |
| 873 | val = strndup(xmlctx->value, xmlctx->value_len); |
| 874 | LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup); |
| 875 | |
| 876 | /* parser next */ |
| 877 | r = lyxml_ctx_next(xmlctx); |
| 878 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 879 | |
| 880 | /* create node */ |
| 881 | r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node); |
| 882 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 883 | val = NULL; |
| 884 | } else { |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 885 | /* create node */ |
| 886 | r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node); |
| 887 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 888 | |
| 889 | assert(*node); |
| 890 | LOG_LOCSET(NULL, *node, NULL, NULL); |
| 891 | log_node = 1; |
| 892 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 893 | /* parser next */ |
| 894 | r = lyxml_ctx_next(xmlctx); |
| 895 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 896 | |
| 897 | /* update options so that generic data can be parsed */ |
| 898 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 899 | lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0); |
| 900 | lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; |
| 901 | |
| 902 | /* parse any data tree */ |
| 903 | while (xmlctx->status == LYXML_ELEMENT) { |
| 904 | r = lydxml_subtree_r(lydctx, NULL, &child, NULL); |
| 905 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 906 | } |
| 907 | |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 908 | /* assign the data tree */ |
| 909 | ((struct lyd_node_any *)*node)->value.tree = child; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 910 | child = NULL; |
| 911 | } |
| 912 | |
| 913 | cleanup: |
Michal Vasko | 85be65e | 2023-06-13 09:44:17 +0200 | [diff] [blame] | 914 | if (log_node) { |
| 915 | LOG_LOCBACK(0, 1, 0, 0); |
| 916 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 917 | lydctx->parse_opts = prev_parse_opts; |
| 918 | lydctx->int_opts = prev_int_opts; |
| 919 | free(val); |
| 920 | lyd_free_tree(child); |
| 921 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
| 922 | lyd_free_tree(*node); |
| 923 | *node = NULL; |
| 924 | } |
| 925 | return rc; |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * @brief Parse an XML subtree, recursively. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 930 | * |
| 931 | * @param[in] lydctx XML YANG data parser context. |
| 932 | * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 933 | * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 934 | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
| 935 | * @return LY_ERR value. |
| 936 | */ |
| 937 | static LY_ERR |
| 938 | lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
| 939 | { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 940 | LY_ERR r, rc = LY_SUCCESS; |
| 941 | const char *prefix, *name; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 942 | size_t prefix_len, name_len; |
| 943 | struct lyxml_ctx *xmlctx; |
| 944 | const struct ly_ctx *ctx; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 945 | struct lyd_meta *meta = NULL; |
| 946 | struct lyd_attr *attr = NULL; |
| 947 | const struct lysc_node *snode; |
| 948 | struct lysc_ext_instance *ext; |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 949 | uint32_t orig_parse_opts; |
| 950 | struct lyd_node *node = NULL, *insert_anchor = NULL; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 951 | ly_bool parse_subtree; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 952 | |
| 953 | assert(parent || first_p); |
| 954 | |
| 955 | xmlctx = lydctx->xmlctx; |
| 956 | ctx = xmlctx->ctx; |
| 957 | |
| 958 | parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0; |
| 959 | /* all descendants should be parsed */ |
| 960 | lydctx->parse_opts &= ~LYD_PARSE_SUBTREE; |
| 961 | orig_parse_opts = lydctx->parse_opts; |
| 962 | |
| 963 | assert(xmlctx->status == LYXML_ELEMENT); |
| 964 | |
| 965 | /* remember element prefix and name */ |
| 966 | prefix = xmlctx->prefix; |
| 967 | prefix_len = xmlctx->prefix_len; |
| 968 | name = xmlctx->name; |
| 969 | name_len = xmlctx->name_len; |
| 970 | |
| 971 | /* parser next */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 972 | rc = lyxml_ctx_next(xmlctx); |
| 973 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 974 | |
| 975 | /* get the schema node */ |
Michal Vasko | 202d816 | 2023-03-01 14:42:19 +0100 | [diff] [blame] | 976 | r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext); |
| 977 | if (r) { |
| 978 | rc = r; |
| 979 | if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { |
| 980 | /* skip the invalid data */ |
| 981 | if ((r = lydxml_data_skip(xmlctx))) { |
| 982 | rc = r; |
| 983 | } |
| 984 | } |
| 985 | goto cleanup; |
| 986 | } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 987 | LOGVRB("Skipping parsing of unknown node \"%.*s\".", name_len, name); |
| 988 | |
| 989 | /* skip element with children */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 990 | rc = lydxml_data_skip(xmlctx); |
| 991 | goto cleanup; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 994 | /* create metadata/attributes */ |
| 995 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 996 | if (snode) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 997 | rc = lydxml_metadata(lydctx, snode, &meta); |
| 998 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 999 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1000 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1001 | rc = lydxml_attrs(xmlctx, &attr); |
| 1002 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 1007 | if (!snode) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1008 | /* opaque */ |
| 1009 | r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len, |
| 1010 | &insert_anchor, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1011 | } else if (snode->nodetype & LYD_NODE_TERM) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1012 | /* term */ |
| 1013 | r = lydxml_subtree_term(lydctx, parent, snode, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1014 | } else if (snode->nodetype & LYD_NODE_INNER) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1015 | /* inner */ |
| 1016 | r = lydxml_subtree_inner(lydctx, snode, ext, &node); |
Michal Vasko | 7c6f33f | 2023-02-10 09:40:11 +0100 | [diff] [blame] | 1017 | } else { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1018 | /* any */ |
Michal Vasko | 7c6f33f | 2023-02-10 09:40:11 +0100 | [diff] [blame] | 1019 | assert(snode->nodetype & LYD_NODE_ANY); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1020 | r = lydxml_subtree_any(lydctx, snode, ext, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1021 | } |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1022 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | 1524f1a | 2023-03-01 09:36:34 +0100 | [diff] [blame] | 1023 | |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 1024 | if (node && snode) { |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1025 | /* add/correct flags */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1026 | r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext); |
Michal Vasko | 567d703 | 2023-02-13 08:53:31 +0100 | [diff] [blame] | 1027 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1028 | |
Michal Vasko | eba2311 | 2022-08-26 08:35:41 +0200 | [diff] [blame] | 1029 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 1030 | /* store for ext instance node validation, if needed */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1031 | r = lyd_validate_node_ext(node, &lydctx->ext_node); |
| 1032 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | eba2311 | 2022-08-26 08:35:41 +0200 | [diff] [blame] | 1033 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /* parser next */ |
| 1037 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1038 | if (!parse_subtree) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1039 | r = lyxml_ctx_next(xmlctx); |
Michal Vasko | 567d703 | 2023-02-13 08:53:31 +0100 | [diff] [blame] | 1040 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1041 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1042 | |
Michal Vasko | 58a1a70 | 2023-03-01 13:45:38 +0100 | [diff] [blame] | 1043 | LY_CHECK_GOTO(!node, cleanup); |
| 1044 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1045 | /* add metadata/attributes */ |
| 1046 | if (snode) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1047 | lyd_insert_meta(node, meta, 0); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1048 | meta = NULL; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1049 | } else { |
| 1050 | lyd_insert_attr(node, attr); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1051 | attr = NULL; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* insert, keep first pointer correct */ |
Michal Vasko | cea5871 | 2022-04-01 14:37:08 +0200 | [diff] [blame] | 1055 | if (insert_anchor) { |
| 1056 | lyd_insert_after(insert_anchor, node); |
| 1057 | } else if (ext) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1058 | r = lyplg_ext_insert(parent, node); |
Michal Vasko | 567d703 | 2023-02-13 08:53:31 +0100 | [diff] [blame] | 1059 | LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1060 | } else { |
| 1061 | lyd_insert_node(parent, first_p, node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0); |
| 1062 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1063 | while (!parent && (*first_p)->prev->next) { |
| 1064 | *first_p = (*first_p)->prev; |
| 1065 | } |
| 1066 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1067 | /* rememeber a successfully parsed node */ |
| 1068 | if (parsed) { |
| 1069 | ly_set_add(parsed, node, 1, NULL); |
| 1070 | } |
| 1071 | |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1072 | cleanup: |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1073 | lydctx->parse_opts = orig_parse_opts; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1074 | lyd_free_meta_siblings(meta); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1075 | lyd_free_attr_siblings(ctx, attr); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1076 | return rc; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1079 | /** |
| 1080 | * @brief Parse a specific XML element into an opaque node. |
| 1081 | * |
| 1082 | * @param[in] xmlctx XML parser context. |
| 1083 | * @param[in] name Name of the element. |
| 1084 | * @param[in] uri URI of the element. |
| 1085 | * @param[in] value Whether a value is expected in the element. |
| 1086 | * @param[out] evnp Parsed envelope (opaque node). |
| 1087 | * @return LY_SUCCESS on success. |
| 1088 | * @return LY_ENOT if the specified element did not match. |
| 1089 | * @return LY_ERR value on error. |
| 1090 | */ |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1091 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1092 | 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] | 1093 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1094 | LY_ERR rc = LY_SUCCESS; |
| 1095 | const struct lyxml_ns *ns; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1096 | struct lyd_attr *attr = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1097 | const char *prefix; |
| 1098 | size_t prefix_len; |
| 1099 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 1100 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1101 | if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) { |
| 1102 | /* not the expected element */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1103 | return LY_ENOT; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | prefix = xmlctx->prefix; |
| 1107 | prefix_len = xmlctx->prefix_len; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1108 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1109 | if (!ns) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1110 | 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] | 1111 | return LY_EVALID; |
| 1112 | } else if (strcmp(ns->uri, uri)) { |
| 1113 | /* different namespace */ |
| 1114 | return LY_ENOT; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 1118 | |
| 1119 | /* create attributes */ |
| 1120 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 1121 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 1122 | } |
| 1123 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1124 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 1125 | if (!value && !xmlctx->ws_only) { |
| 1126 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1127 | (int)xmlctx->value_len, xmlctx->value, name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1128 | rc = LY_EVALID; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1129 | goto cleanup; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1130 | } |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1131 | |
| 1132 | /* create node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1133 | 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] | 1134 | 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] | 1135 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1136 | |
| 1137 | /* assign atributes */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1138 | ((struct lyd_node_opaq *)(*envp))->attr = attr; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1139 | attr = NULL; |
| 1140 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1141 | /* parser next element */ |
| 1142 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1143 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1144 | cleanup: |
| 1145 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
| 1146 | if (rc) { |
| 1147 | lyd_free_tree(*envp); |
| 1148 | *envp = NULL; |
| 1149 | } |
| 1150 | return rc; |
| 1151 | } |
| 1152 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1153 | LY_ERR |
| 1154 | lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1155 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts, |
| 1156 | struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p) |
| 1157 | { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1158 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1159 | struct lyd_xml_ctx *lydctx; |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1160 | ly_bool parsed_data_nodes = 0, close_elem = 0; |
| 1161 | struct lyd_node *act = NULL; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1162 | enum LYXML_PARSER_STATUS status; |
| 1163 | |
| 1164 | assert(ctx && in && lydctx_p); |
| 1165 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1166 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
| 1167 | |
| 1168 | /* init context */ |
| 1169 | lydctx = calloc(1, sizeof *lydctx); |
| 1170 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
| 1171 | LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
| 1172 | lydctx->parse_opts = parse_opts; |
| 1173 | lydctx->val_opts = val_opts; |
| 1174 | lydctx->int_opts = int_opts; |
| 1175 | lydctx->free = lyd_xml_ctx_free; |
| 1176 | lydctx->ext = ext; |
| 1177 | |
| 1178 | /* find the operation node if it exists already */ |
| 1179 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1180 | |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1181 | if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) { |
| 1182 | /* can be either, try to parse "action" */ |
| 1183 | if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) { |
| 1184 | close_elem = 1; |
| 1185 | int_opts &= ~LYD_INTOPT_RPC; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1189 | /* parse XML data */ |
| 1190 | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1191 | r = lydxml_subtree_r(lydctx, parent, first_p, parsed); |
| 1192 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
| 1193 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1194 | parsed_data_nodes = 1; |
| 1195 | |
| 1196 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
| 1200 | |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1201 | /* close an opened element */ |
| 1202 | if (close_elem) { |
| 1203 | if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1204 | assert(lydctx->xmlctx->status == LYXML_ELEMENT); |
| 1205 | LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", |
| 1206 | (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name); |
| 1207 | rc = LY_EVALID; |
| 1208 | goto cleanup; |
| 1209 | } |
| 1210 | |
| 1211 | LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup); |
| 1212 | } |
| 1213 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1214 | /* check final state */ |
| 1215 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) { |
| 1216 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1217 | r = LY_EVALID; |
| 1218 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1219 | } |
| 1220 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
| 1221 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1222 | r = LY_EVALID; |
| 1223 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | if (!parsed_data_nodes) { |
| 1227 | /* no data nodes were parsed */ |
| 1228 | lydctx->op_node = NULL; |
| 1229 | } |
| 1230 | |
| 1231 | if (parse_opts & LYD_PARSE_SUBTREE) { |
| 1232 | /* check for a sibling element */ |
| 1233 | assert(subtree_sibling); |
| 1234 | if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) { |
| 1235 | *subtree_sibling = 1; |
| 1236 | } else { |
| 1237 | *subtree_sibling = 0; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | cleanup: |
| 1242 | /* there should be no unres stored if validation should be skipped */ |
| 1243 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
| 1244 | !lydctx->node_when.count)); |
| 1245 | |
Michal Vasko | 6459269 | 2023-06-12 13:50:11 +0200 | [diff] [blame] | 1246 | lyd_free_tree(act); |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1247 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1248 | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
| 1249 | } else { |
| 1250 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 1251 | |
| 1252 | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
| 1253 | lyxml_ctx_free(lydctx->xmlctx); |
| 1254 | lydctx->xmlctx = NULL; |
| 1255 | } |
| 1256 | return rc; |
| 1257 | } |
| 1258 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1259 | /** |
| 1260 | * @brief Parse all expected non-data XML elements of a NETCONF rpc message. |
| 1261 | * |
| 1262 | * @param[in] xmlctx XML parser context. |
| 1263 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 1264 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 1265 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 1266 | * @return LY_SUCCESS on success. |
| 1267 | * @return LY_ERR value on error. |
| 1268 | */ |
| 1269 | static LY_ERR |
| 1270 | lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 1271 | { |
| 1272 | LY_ERR rc = LY_SUCCESS, r; |
| 1273 | struct lyd_node *child; |
| 1274 | |
| 1275 | assert(envp && !*envp); |
| 1276 | |
Michal Vasko | 5037527 | 2022-04-26 12:07:46 +0200 | [diff] [blame] | 1277 | if (xmlctx->status != LYXML_ELEMENT) { |
| 1278 | /* nothing to parse */ |
| 1279 | assert(xmlctx->status == LYXML_END); |
| 1280 | goto cleanup; |
| 1281 | } |
| 1282 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1283 | /* parse "rpc" */ |
| 1284 | 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] | 1285 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 1286 | |
| 1287 | /* parse "action", if any */ |
| 1288 | r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child); |
| 1289 | if (r == LY_SUCCESS) { |
| 1290 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1291 | lyd_insert_node(*envp, NULL, child, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1292 | |
| 1293 | /* NETCONF action */ |
| 1294 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION; |
| 1295 | *close_elem = 2; |
| 1296 | } else if (r == LY_ENOT) { |
| 1297 | /* NETCONF RPC */ |
| 1298 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC; |
| 1299 | *close_elem = 1; |
| 1300 | } else { |
| 1301 | rc = r; |
| 1302 | goto cleanup; |
| 1303 | } |
| 1304 | |
| 1305 | cleanup: |
| 1306 | if (rc) { |
| 1307 | lyd_free_tree(*envp); |
| 1308 | *envp = NULL; |
| 1309 | } |
| 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | /** |
| 1314 | * @brief Parse all expected non-data XML elements of a NETCONF notification message. |
| 1315 | * |
| 1316 | * @param[in] xmlctx XML parser context. |
| 1317 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 1318 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 1319 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 1320 | * @return LY_SUCCESS on success. |
| 1321 | * @return LY_ERR value on error. |
| 1322 | */ |
| 1323 | static LY_ERR |
| 1324 | lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 1325 | { |
| 1326 | LY_ERR rc = LY_SUCCESS, r; |
| 1327 | struct lyd_node *child; |
| 1328 | |
| 1329 | assert(envp && !*envp); |
| 1330 | |
Michal Vasko | 5037527 | 2022-04-26 12:07:46 +0200 | [diff] [blame] | 1331 | if (xmlctx->status != LYXML_ELEMENT) { |
| 1332 | /* nothing to parse */ |
| 1333 | assert(xmlctx->status == LYXML_END); |
| 1334 | goto cleanup; |
| 1335 | } |
| 1336 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1337 | /* parse "notification" */ |
| 1338 | 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] | 1339 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 1340 | |
| 1341 | /* parse "eventTime" */ |
| 1342 | r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child); |
| 1343 | if (r == LY_ENOT) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1344 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".", |
| 1345 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1346 | r = LY_EVALID; |
| 1347 | } |
| 1348 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 1349 | |
| 1350 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1351 | lyd_insert_node(*envp, NULL, child, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1352 | |
| 1353 | /* validate value */ |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 1354 | r = lyd_parser_notif_eventtime_validate(child); |
Michal Vasko | 51de7b7 | 2022-04-29 09:50:22 +0200 | [diff] [blame] | 1355 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1356 | |
| 1357 | /* finish child parsing */ |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1358 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1359 | assert(xmlctx->status == LYXML_ELEMENT); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1360 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1361 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1362 | rc = LY_EVALID; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1363 | goto cleanup; |
| 1364 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1365 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1366 | |
| 1367 | /* NETCONF notification */ |
| 1368 | *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF; |
| 1369 | *close_elem = 1; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1370 | |
| 1371 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1372 | if (rc) { |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1373 | lyd_free_tree(*envp); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1374 | *envp = NULL; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1375 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1376 | return rc; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1377 | } |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 1378 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1379 | /** |
| 1380 | * @brief Parse an XML element as an opaque node subtree. |
| 1381 | * |
| 1382 | * @param[in] xmlctx XML parser context. |
| 1383 | * @param[in] parent Parent to append nodes to. |
| 1384 | * @return LY_ERR value. |
| 1385 | */ |
| 1386 | static LY_ERR |
| 1387 | lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1388 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1389 | LY_ERR rc = LY_SUCCESS; |
| 1390 | const struct lyxml_ns *ns; |
| 1391 | struct lyd_attr *attr = NULL; |
| 1392 | struct lyd_node *child = NULL; |
| 1393 | const char *name, *prefix; |
| 1394 | size_t name_len, prefix_len; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1395 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1396 | assert(xmlctx->status == LYXML_ELEMENT); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1397 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1398 | name = xmlctx->name; |
| 1399 | name_len = xmlctx->name_len; |
| 1400 | prefix = xmlctx->prefix; |
| 1401 | prefix_len = xmlctx->prefix_len; |
| 1402 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
| 1403 | if (!ns) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1404 | 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] | 1405 | return LY_EVALID; |
| 1406 | } |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1407 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1408 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1409 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1410 | /* create attributes */ |
| 1411 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 1412 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 1413 | } |
| 1414 | |
| 1415 | /* create node */ |
| 1416 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 1417 | 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] | 1418 | 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] | 1419 | LY_CHECK_GOTO(rc, cleanup); |
| 1420 | |
| 1421 | /* assign atributes */ |
| 1422 | ((struct lyd_node_opaq *)child)->attr = attr; |
| 1423 | attr = NULL; |
| 1424 | |
| 1425 | /* parser next element */ |
| 1426 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1427 | |
| 1428 | /* parse all the descendants */ |
| 1429 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1430 | rc = lydxml_opaq_r(xmlctx, child); |
| 1431 | LY_CHECK_GOTO(rc, cleanup); |
| 1432 | } |
| 1433 | |
| 1434 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1435 | lyd_insert_node(parent, NULL, child, 1); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1436 | |
| 1437 | cleanup: |
| 1438 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
| 1439 | if (rc) { |
| 1440 | lyd_free_tree(child); |
| 1441 | } |
| 1442 | return rc; |
| 1443 | } |
| 1444 | |
| 1445 | /** |
| 1446 | * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message. |
| 1447 | * |
| 1448 | * @param[in] xmlctx XML parser context. |
| 1449 | * @param[in] parent Parent to append nodes to. |
| 1450 | * @return LY_ERR value. |
| 1451 | */ |
| 1452 | static LY_ERR |
| 1453 | lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
| 1454 | { |
| 1455 | LY_ERR r; |
| 1456 | struct lyd_node *child, *iter; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1457 | ly_bool no_dup; |
| 1458 | |
| 1459 | /* there must be some child */ |
| 1460 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1461 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\"."); |
| 1462 | return LY_EVALID; |
| 1463 | } |
| 1464 | |
| 1465 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1466 | child = NULL; |
| 1467 | |
| 1468 | /* |
| 1469 | * session-id |
| 1470 | */ |
| 1471 | r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1472 | if (r == LY_SUCCESS) { |
| 1473 | no_dup = 1; |
| 1474 | goto check_child; |
| 1475 | } else if (r != LY_ENOT) { |
| 1476 | goto error; |
| 1477 | } |
| 1478 | |
| 1479 | /* |
| 1480 | * bad-attribute |
| 1481 | */ |
| 1482 | r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1483 | if (r == LY_SUCCESS) { |
| 1484 | no_dup = 1; |
| 1485 | goto check_child; |
| 1486 | } else if (r != LY_ENOT) { |
| 1487 | goto error; |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * bad-element |
| 1492 | */ |
| 1493 | r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1494 | if (r == LY_SUCCESS) { |
| 1495 | no_dup = 1; |
| 1496 | goto check_child; |
| 1497 | } else if (r != LY_ENOT) { |
| 1498 | goto error; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * bad-namespace |
| 1503 | */ |
| 1504 | r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1505 | if (r == LY_SUCCESS) { |
| 1506 | no_dup = 1; |
| 1507 | goto check_child; |
| 1508 | } else if (r != LY_ENOT) { |
| 1509 | goto error; |
| 1510 | } |
| 1511 | |
| 1512 | if (r == LY_ENOT) { |
| 1513 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1514 | |
Michal Vasko | 845ba39 | 2022-06-16 07:52:13 +0200 | [diff] [blame] | 1515 | /* custom elements, parse all the siblings */ |
| 1516 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1517 | LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error); |
| 1518 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1519 | } |
Michal Vasko | 6398eaf | 2022-01-10 10:12:30 +0100 | [diff] [blame] | 1520 | continue; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | check_child: |
| 1524 | /* check for duplicates */ |
| 1525 | if (no_dup) { |
| 1526 | LY_LIST_FOR(lyd_child(parent), iter) { |
| 1527 | if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) && |
| 1528 | (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) { |
| 1529 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".", |
| 1530 | ((struct lyd_node_opaq *)child)->name.name); |
| 1531 | r = LY_EVALID; |
| 1532 | goto error; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | /* finish child parsing */ |
| 1538 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1539 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1540 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1541 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1542 | r = LY_EVALID; |
| 1543 | goto error; |
| 1544 | } |
| 1545 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1546 | |
| 1547 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1548 | lyd_insert_node(parent, NULL, child, 1); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | return LY_SUCCESS; |
| 1552 | |
| 1553 | error: |
| 1554 | lyd_free_tree(child); |
| 1555 | return r; |
| 1556 | } |
| 1557 | |
| 1558 | /** |
| 1559 | * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message. |
| 1560 | * |
| 1561 | * @param[in] xmlctx XML parser context. |
| 1562 | * @param[in] parent Parent to append nodes to. |
| 1563 | * @return LY_ERR value. |
| 1564 | */ |
| 1565 | static LY_ERR |
| 1566 | lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent) |
| 1567 | { |
| 1568 | LY_ERR r; |
| 1569 | struct lyd_node *child, *iter; |
| 1570 | const char *val; |
| 1571 | ly_bool no_dup; |
| 1572 | |
| 1573 | /* there must be some child */ |
| 1574 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1575 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\"."); |
| 1576 | return LY_EVALID; |
| 1577 | } |
| 1578 | |
| 1579 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1580 | child = NULL; |
| 1581 | |
| 1582 | /* |
| 1583 | * error-type |
| 1584 | */ |
| 1585 | r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1586 | if (r == LY_SUCCESS) { |
| 1587 | val = ((struct lyd_node_opaq *)child)->value; |
| 1588 | if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) { |
| 1589 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1590 | ((struct lyd_node_opaq *)child)->name.name); |
| 1591 | r = LY_EVALID; |
| 1592 | goto error; |
| 1593 | } |
| 1594 | |
| 1595 | no_dup = 1; |
| 1596 | goto check_child; |
| 1597 | } else if (r != LY_ENOT) { |
| 1598 | goto error; |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * error-tag |
| 1603 | */ |
| 1604 | r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1605 | if (r == LY_SUCCESS) { |
| 1606 | val = ((struct lyd_node_opaq *)child)->value; |
| 1607 | if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") && |
| 1608 | strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") && |
| 1609 | strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") && |
| 1610 | strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") && |
| 1611 | strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") && |
| 1612 | strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") && |
| 1613 | strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) { |
| 1614 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1615 | ((struct lyd_node_opaq *)child)->name.name); |
| 1616 | r = LY_EVALID; |
| 1617 | goto error; |
| 1618 | } |
| 1619 | |
| 1620 | no_dup = 1; |
| 1621 | goto check_child; |
| 1622 | } else if (r != LY_ENOT) { |
| 1623 | goto error; |
| 1624 | } |
| 1625 | |
| 1626 | /* |
| 1627 | * error-severity |
| 1628 | */ |
| 1629 | r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1630 | if (r == LY_SUCCESS) { |
| 1631 | val = ((struct lyd_node_opaq *)child)->value; |
| 1632 | if (strcmp(val, "error") && strcmp(val, "warning")) { |
| 1633 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val, |
| 1634 | ((struct lyd_node_opaq *)child)->name.name); |
| 1635 | r = LY_EVALID; |
| 1636 | goto error; |
| 1637 | } |
| 1638 | |
| 1639 | no_dup = 1; |
| 1640 | goto check_child; |
| 1641 | } else if (r != LY_ENOT) { |
| 1642 | goto error; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * error-app-tag |
| 1647 | */ |
| 1648 | r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1649 | if (r == LY_SUCCESS) { |
| 1650 | no_dup = 1; |
| 1651 | goto check_child; |
| 1652 | } else if (r != LY_ENOT) { |
| 1653 | goto error; |
| 1654 | } |
| 1655 | |
| 1656 | /* |
| 1657 | * error-path |
| 1658 | */ |
| 1659 | r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1660 | if (r == LY_SUCCESS) { |
| 1661 | no_dup = 1; |
| 1662 | goto check_child; |
| 1663 | } else if (r != LY_ENOT) { |
| 1664 | goto error; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * error-message |
| 1669 | */ |
| 1670 | r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child); |
| 1671 | if (r == LY_SUCCESS) { |
| 1672 | no_dup = 1; |
| 1673 | goto check_child; |
| 1674 | } else if (r != LY_ENOT) { |
| 1675 | goto error; |
| 1676 | } |
| 1677 | |
| 1678 | /* error-info */ |
| 1679 | r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1680 | if (r == LY_SUCCESS) { |
| 1681 | /* parse all the descendants */ |
| 1682 | LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error); |
| 1683 | |
| 1684 | no_dup = 0; |
| 1685 | goto check_child; |
| 1686 | } else if (r != LY_ENOT) { |
| 1687 | goto error; |
| 1688 | } |
| 1689 | |
| 1690 | if (r == LY_ENOT) { |
| 1691 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1692 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1693 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1694 | r = LY_EVALID; |
| 1695 | goto error; |
| 1696 | } |
| 1697 | |
| 1698 | check_child: |
| 1699 | /* check for duplicates */ |
| 1700 | if (no_dup) { |
| 1701 | LY_LIST_FOR(lyd_child(parent), iter) { |
| 1702 | if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) && |
| 1703 | (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) { |
| 1704 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".", |
| 1705 | ((struct lyd_node_opaq *)child)->name.name); |
| 1706 | r = LY_EVALID; |
| 1707 | goto error; |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | /* finish child parsing */ |
| 1713 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1714 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1715 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1716 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1717 | r = LY_EVALID; |
| 1718 | goto error; |
| 1719 | } |
| 1720 | LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error); |
| 1721 | |
| 1722 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1723 | lyd_insert_node(parent, NULL, child, 1); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | return LY_SUCCESS; |
| 1727 | |
| 1728 | error: |
| 1729 | lyd_free_tree(child); |
| 1730 | return r; |
| 1731 | } |
| 1732 | |
| 1733 | /** |
| 1734 | * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message. |
| 1735 | * |
| 1736 | * @param[in] xmlctx XML parser context. |
| 1737 | * @param[out] evnp Parsed envelope(s) (opaque node). |
| 1738 | * @param[out] int_opts Internal options for parsing the rest of YANG data. |
| 1739 | * @param[out] close_elem Number of parsed opened elements that need to be closed. |
| 1740 | * @return LY_SUCCESS on success. |
| 1741 | * @return LY_ERR value on error. |
| 1742 | */ |
| 1743 | static LY_ERR |
| 1744 | lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem) |
| 1745 | { |
| 1746 | LY_ERR rc = LY_SUCCESS, r; |
| 1747 | struct lyd_node *child = NULL; |
| 1748 | const char *parsed_elem = NULL; |
| 1749 | |
| 1750 | assert(envp && !*envp); |
| 1751 | |
Michal Vasko | 5037527 | 2022-04-26 12:07:46 +0200 | [diff] [blame] | 1752 | if (xmlctx->status != LYXML_ELEMENT) { |
| 1753 | /* nothing to parse */ |
| 1754 | assert(xmlctx->status == LYXML_END); |
| 1755 | goto cleanup; |
| 1756 | } |
| 1757 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1758 | /* parse "rpc-reply" */ |
| 1759 | 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] | 1760 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
| 1761 | |
| 1762 | /* there must be some child */ |
| 1763 | if (xmlctx->status == LYXML_ELEM_CLOSE) { |
| 1764 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\"."); |
| 1765 | rc = LY_EVALID; |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 1766 | goto cleanup; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1767 | } |
| 1768 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1769 | /* try to parse "ok" */ |
| 1770 | r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1771 | if (r == LY_SUCCESS) { |
| 1772 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1773 | lyd_insert_node(*envp, NULL, child, 1); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1774 | |
| 1775 | /* finish child parsing */ |
| 1776 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1777 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1778 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1779 | (int)xmlctx->name_len, xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1780 | rc = LY_EVALID; |
| 1781 | goto cleanup; |
| 1782 | } |
| 1783 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1784 | |
| 1785 | /* success */ |
| 1786 | parsed_elem = "ok"; |
| 1787 | goto finish; |
| 1788 | } else if (r != LY_ENOT) { |
| 1789 | rc = r; |
| 1790 | goto cleanup; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1791 | } |
| 1792 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1793 | /* try to parse all "rpc-error" elements */ |
| 1794 | while (xmlctx->status == LYXML_ELEMENT) { |
| 1795 | r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child); |
| 1796 | if (r == LY_ENOT) { |
| 1797 | break; |
| 1798 | } else if (r) { |
| 1799 | rc = r; |
| 1800 | goto cleanup; |
| 1801 | } |
| 1802 | |
| 1803 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1804 | lyd_insert_node(*envp, NULL, child, 1); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1805 | |
| 1806 | /* parse all children of "rpc-error" */ |
| 1807 | LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup); |
| 1808 | |
| 1809 | /* finish child parsing */ |
| 1810 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 1811 | LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup); |
| 1812 | |
| 1813 | parsed_elem = "rpc-error"; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1814 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1815 | |
| 1816 | finish: |
| 1817 | if (parsed_elem) { |
| 1818 | /* NETCONF rpc-reply with no data */ |
| 1819 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1820 | assert(xmlctx->status == LYXML_ELEMENT); |
| 1821 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1822 | (int)xmlctx->name_len, xmlctx->name, parsed_elem); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1823 | rc = LY_EVALID; |
| 1824 | goto cleanup; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | /* NETCONF rpc-reply */ |
Michal Vasko | 1d991fd | 2021-07-09 13:14:40 +0200 | [diff] [blame] | 1829 | *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1830 | *close_elem = 1; |
| 1831 | |
| 1832 | cleanup: |
| 1833 | if (rc) { |
| 1834 | lyd_free_tree(*envp); |
| 1835 | *envp = NULL; |
| 1836 | } |
| 1837 | return rc; |
| 1838 | } |
| 1839 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1840 | LY_ERR |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1841 | lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1842 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1843 | 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] | 1844 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1845 | LY_ERR rc = LY_SUCCESS; |
| 1846 | struct lyd_xml_ctx *lydctx; |
Michal Vasko | 2ca9f9e | 2021-07-02 09:21:36 +0200 | [diff] [blame] | 1847 | uint32_t i, int_opts = 0, close_elem = 0; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1848 | ly_bool parsed_data_nodes = 0; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1849 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1850 | assert(ctx && in && lydctx_p); |
| 1851 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1852 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1853 | assert(!(parse_opts & LYD_PARSE_SUBTREE)); |
| 1854 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1855 | /* init context */ |
| 1856 | lydctx = calloc(1, sizeof *lydctx); |
| 1857 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
| 1858 | LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
| 1859 | lydctx->parse_opts = parse_opts; |
| 1860 | lydctx->val_opts = val_opts; |
| 1861 | lydctx->free = lyd_xml_ctx_free; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1862 | lydctx->ext = ext; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1863 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1864 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1865 | case LYD_TYPE_RPC_NETCONF: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1866 | assert(!parent); |
Michal Vasko | d71c2d4 | 2022-04-29 09:50:44 +0200 | [diff] [blame] | 1867 | rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem); |
| 1868 | if (rc == LY_ENOT) { |
| 1869 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace."); |
| 1870 | } |
| 1871 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1872 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1873 | case LYD_TYPE_NOTIF_NETCONF: |
| 1874 | assert(!parent); |
Michal Vasko | d71c2d4 | 2022-04-29 09:50:44 +0200 | [diff] [blame] | 1875 | rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem); |
| 1876 | if (rc == LY_ENOT) { |
| 1877 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace."); |
| 1878 | } |
| 1879 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1880 | break; |
| 1881 | case LYD_TYPE_REPLY_NETCONF: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1882 | assert(parent); |
Michal Vasko | d71c2d4 | 2022-04-29 09:50:44 +0200 | [diff] [blame] | 1883 | rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem); |
| 1884 | if (rc == LY_ENOT) { |
| 1885 | LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace."); |
| 1886 | } |
| 1887 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1888 | break; |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 1889 | case LYD_TYPE_RPC_RESTCONF: |
| 1890 | assert(parent); |
| 1891 | |
| 1892 | /* parse "input" */ |
| 1893 | rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_owner_module(parent)->ns, 0, envp); |
| 1894 | if (rc == LY_ENOT) { |
| 1895 | LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace."); |
| 1896 | } |
| 1897 | LY_CHECK_GOTO(rc, cleanup); |
| 1898 | |
| 1899 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION; |
| 1900 | close_elem = 1; |
| 1901 | break; |
| 1902 | case LYD_TYPE_REPLY_RESTCONF: |
| 1903 | assert(parent); |
| 1904 | |
| 1905 | /* parse "output" */ |
| 1906 | rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_owner_module(parent)->ns, 0, envp); |
| 1907 | if (rc == LY_ENOT) { |
| 1908 | LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace."); |
| 1909 | } |
| 1910 | LY_CHECK_GOTO(rc, cleanup); |
| 1911 | |
| 1912 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; |
| 1913 | close_elem = 1; |
| 1914 | break; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1915 | default: |
| 1916 | LOGINT(ctx); |
| 1917 | rc = LY_EINT; |
| 1918 | goto cleanup; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1919 | } |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1920 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1921 | lydctx->int_opts = int_opts; |
| 1922 | |
| 1923 | /* find the operation node if it exists already */ |
| 1924 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1925 | |
| 1926 | /* parse XML data */ |
| 1927 | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
| 1928 | LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup); |
| 1929 | parsed_data_nodes = 1; |
| 1930 | |
| 1931 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1932 | break; |
| 1933 | } |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 1934 | } |
| 1935 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1936 | /* close all opened elements */ |
| 1937 | for (i = 0; i < close_elem; ++i) { |
| 1938 | if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) { |
| 1939 | assert(lydctx->xmlctx->status == LYXML_ELEMENT); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1940 | LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".", |
| 1941 | (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1942 | rc = LY_EVALID; |
| 1943 | goto cleanup; |
| 1944 | } |
| 1945 | |
| 1946 | LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup); |
Michal Vasko | 4189c0f | 2020-08-13 09:05:22 +0200 | [diff] [blame] | 1947 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1948 | |
| 1949 | /* check final state */ |
| 1950 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) { |
| 1951 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1952 | rc = LY_EVALID; |
| 1953 | goto cleanup; |
| 1954 | } |
Michal Vasko | c939fdd | 2022-01-03 11:35:13 +0100 | [diff] [blame] | 1955 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1956 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1957 | rc = LY_EVALID; |
| 1958 | goto cleanup; |
| 1959 | } |
| 1960 | |
| 1961 | if (!parsed_data_nodes) { |
| 1962 | /* no data nodes were parsed */ |
| 1963 | lydctx->op_node = NULL; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1967 | /* there should be no unres stored if validation should be skipped */ |
| 1968 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1969 | !lydctx->node_when.count)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1970 | |
| 1971 | if (rc) { |
| 1972 | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
| 1973 | } else { |
| 1974 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 1975 | |
| 1976 | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
| 1977 | lyxml_ctx_free(lydctx->xmlctx); |
| 1978 | lydctx->xmlctx = NULL; |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 1979 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1980 | return rc; |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 1981 | } |