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