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