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