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