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