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