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