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