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