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