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