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