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