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