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