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