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 | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "set.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 27 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "tree_data_internal.h" |
| 29 | #include "tree_schema.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 30 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "xml.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 34 | * @brief Internal context for XML YANG data parser. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 35 | * |
| 36 | * 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] | 37 | */ |
| 38 | struct lyd_xml_ctx { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 39 | uint32_t parse_options; /**< various @ref dataparseroptions. */ |
| 40 | uint32_t validate_options; /**< various @ref datavalidationoptions. */ |
| 41 | uint32_t int_opts; /**< internal data parser options */ |
| 42 | uint32_t path_len; /**< used bytes in the path buffer */ |
| 43 | char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */ |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 44 | struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */ |
| 45 | struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */ |
| 46 | struct ly_set node_when; /**< set of nodes with "when" conditions */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 47 | 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] | 48 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 49 | /* callbacks */ |
| 50 | lyd_ctx_free_clb free; /* destructor */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 51 | |
| 52 | struct lyxml_ctx *xmlctx; /**< XML context */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 55 | void |
| 56 | lyd_xml_ctx_free(struct lyd_ctx *lydctx) |
| 57 | { |
| 58 | struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx; |
| 59 | |
| 60 | lyd_ctx_free(lydctx); |
| 61 | lyxml_ctx_free(ctx->xmlctx); |
| 62 | free(ctx); |
| 63 | } |
| 64 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 65 | static LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 66 | lydxml_metadata(struct lyd_xml_ctx *lydctx, struct lyd_meta **meta) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 67 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 68 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 69 | const struct lyxml_ns *ns; |
| 70 | struct lys_module *mod; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 71 | const char *name; |
| 72 | size_t name_len; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 73 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 74 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 75 | *meta = NULL; |
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 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 78 | if (!xmlctx->prefix_len) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 79 | /* in XML, all attributes must be prefixed |
| 80 | * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 81 | if (lydctx->parse_options & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 82 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 83 | xmlctx->name_len, xmlctx->name); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 84 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 85 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 86 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 87 | skip_attr: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 88 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 89 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 90 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 91 | continue; |
| 92 | } |
| 93 | |
| 94 | /* get namespace of the attribute to find its annotation definition */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 95 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 96 | if (!ns) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 97 | /* unknown namespace, XML error */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 98 | 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] | 99 | goto cleanup; |
| 100 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 101 | mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 102 | if (!mod) { |
| 103 | /* module is not implemented or not present in the schema */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 104 | if (lydctx->parse_options & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 105 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 106 | "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".", |
| 107 | ns->uri, xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", xmlctx->name_len, |
| 108 | xmlctx->name); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 109 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 110 | } |
| 111 | goto skip_attr; |
| 112 | } |
| 113 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 114 | /* remember meta name and get its content */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 115 | name = xmlctx->name; |
| 116 | name_len = xmlctx->name_len; |
| 117 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 118 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 119 | |
| 120 | /* create metadata */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 121 | 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] | 122 | 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] | 123 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 124 | |
| 125 | /* next attribute */ |
| 126 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 127 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 128 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 129 | ret = LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 130 | |
| 131 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 132 | if (ret) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 133 | lyd_free_meta_siblings(*meta); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 134 | *meta = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 135 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 136 | return ret; |
| 137 | } |
| 138 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 139 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 140 | lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 141 | { |
| 142 | LY_ERR ret = LY_SUCCESS; |
| 143 | const struct lyxml_ns *ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 144 | void *val_prefix_data; |
| 145 | LY_PREFIX_FORMAT format; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 146 | struct lyd_attr *attr2; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 147 | const char *name, *prefix; |
| 148 | size_t name_len, prefix_len; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 149 | |
| 150 | assert(attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 151 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 152 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 153 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 154 | ns = NULL; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 155 | if (xmlctx->prefix_len) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 156 | /* get namespace of the attribute */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 157 | ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 158 | if (!ns) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 159 | 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] | 160 | ret = LY_EVALID; |
| 161 | goto cleanup; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (*attr) { |
| 166 | attr2 = *attr; |
| 167 | } else { |
| 168 | attr2 = NULL; |
| 169 | } |
| 170 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 171 | /* remember attr prefix, name, and get its content */ |
| 172 | prefix = xmlctx->prefix; |
| 173 | prefix_len = xmlctx->prefix_len; |
| 174 | name = xmlctx->name; |
| 175 | name_len = xmlctx->name_len; |
| 176 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 177 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 178 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 179 | /* get value prefixes */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 180 | LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_PREF_XML, |
| 181 | &xmlctx->ns, &format, &val_prefix_data), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 182 | |
| 183 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 184 | ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL, |
| 185 | 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] | 186 | LY_CHECK_GOTO(ret, cleanup); |
| 187 | |
| 188 | if (!*attr) { |
| 189 | *attr = attr2; |
| 190 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 191 | |
| 192 | /* next attribute */ |
| 193 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 197 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 198 | lyd_free_attr_siblings(xmlctx->ctx, *attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 199 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 200 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 201 | return ret; |
| 202 | } |
| 203 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 204 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 205 | lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list) |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 206 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 207 | LY_ERR ret = LY_SUCCESS, r; |
| 208 | enum LYXML_PARSER_STATUS next; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 209 | struct ly_set key_set = {0}; |
| 210 | const struct lysc_node *snode; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 211 | uint32_t i, parents_count; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 212 | |
| 213 | assert(list && (list->nodetype == LYS_LIST)); |
| 214 | |
| 215 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 216 | snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 217 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 218 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 219 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 222 | while (xmlctx->status == LYXML_ELEMENT) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 223 | /* find key definition */ |
| 224 | for (i = 0; i < key_set.count; ++i) { |
| 225 | snode = (const struct lysc_node *)key_set.objs[i]; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 226 | if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 230 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 231 | |
| 232 | /* skip attributes */ |
| 233 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 234 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 235 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 236 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 239 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 240 | if (i < key_set.count) { |
| 241 | /* validate the value */ |
Michal Vasko | 713148b | 2020-11-12 13:10:13 +0100 | [diff] [blame] | 242 | 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] | 243 | if (!r) { |
| 244 | /* key with a valid value, remove from the set */ |
| 245 | ly_set_rm_index(&key_set, i, NULL); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 249 | /* parser next */ |
| 250 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 251 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 252 | /* skip any children, resursively */ |
| 253 | parents_count = xmlctx->elements.count; |
| 254 | while ((parents_count < xmlctx->elements.count) || (xmlctx->status == LYXML_ELEMENT)) { |
| 255 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 256 | } |
| 257 | |
| 258 | /* parser next, but do not parse closing element of the list because it would remove its namespaces */ |
| 259 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 260 | LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup); |
| 261 | if (next != LYXML_ELEM_CLOSE) { |
| 262 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 263 | } |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | if (key_set.count) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 267 | /* some keys are missing/did not validate */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 268 | ret = LY_ENOT; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | cleanup: |
| 272 | ly_set_erase(&key_set, NULL); |
| 273 | return ret; |
| 274 | } |
| 275 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 276 | static LY_ERR |
| 277 | lydxml_data_skip(struct lyxml_ctx *xmlctx) |
| 278 | { |
| 279 | uint32_t parents_count; |
| 280 | |
| 281 | /* remember current number of parents */ |
| 282 | parents_count = xmlctx->elements.count; |
| 283 | |
| 284 | /* skip after the content */ |
| 285 | while (xmlctx->status != LYXML_ELEM_CONTENT) { |
| 286 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 287 | } |
| 288 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 289 | |
| 290 | /* skip all children elements, recursively, if any */ |
| 291 | while (parents_count < xmlctx->elements.count) { |
| 292 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 293 | } |
| 294 | |
| 295 | /* close element */ |
| 296 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 297 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 298 | |
| 299 | return LY_SUCCESS; |
| 300 | } |
| 301 | |
| 302 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 303 | 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] | 304 | { |
| 305 | LY_ERR ret = LY_SUCCESS; |
| 306 | enum LYXML_PARSER_STATUS prev_status; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 307 | const char *prev_current, *pname, *pprefix; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 308 | size_t pprefix_len, pname_len; |
| 309 | struct lyxml_ctx *xmlctx = lydctx->xmlctx; |
| 310 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 311 | if ((lydctx->parse_options & LYD_PARSE_OPAQ) && ((*snode)->nodetype & (LYD_NODE_TERM | LYS_LIST))) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 312 | /* backup parser */ |
| 313 | prev_status = xmlctx->status; |
| 314 | pprefix = xmlctx->prefix; |
| 315 | pprefix_len = xmlctx->prefix_len; |
| 316 | pname = xmlctx->name; |
| 317 | pname_len = xmlctx->name_len; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 318 | prev_current = xmlctx->in->current; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 319 | if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) { |
| 320 | /* it was backed up, do not free */ |
| 321 | xmlctx->dynamic = 0; |
| 322 | } |
| 323 | |
| 324 | /* skip attributes */ |
| 325 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 326 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 327 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 328 | } |
| 329 | |
| 330 | if ((*snode)->nodetype & LYD_NODE_TERM) { |
| 331 | /* 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] | 332 | 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] | 333 | *snode = NULL; |
| 334 | } |
| 335 | } else { |
| 336 | /* skip content */ |
| 337 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore); |
| 338 | |
| 339 | if (lydxml_check_list(xmlctx, *snode)) { |
| 340 | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
| 341 | *snode = NULL; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | restore: |
| 346 | /* restore parser */ |
| 347 | if (xmlctx->dynamic) { |
| 348 | free((char *)xmlctx->value); |
| 349 | } |
| 350 | xmlctx->status = prev_status; |
| 351 | xmlctx->prefix = pprefix; |
| 352 | xmlctx->prefix_len = pprefix_len; |
| 353 | xmlctx->name = pname; |
| 354 | xmlctx->name_len = pname_len; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 355 | xmlctx->in->current = prev_current; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return ret; |
| 359 | } |
| 360 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 361 | /** |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 362 | * @brief Parse XML subtree. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 363 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 364 | * @param[in] lydctx XML YANG data parser context. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 365 | * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 366 | * @param[out] node Resulting list of the parsed nodes. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 367 | * @return LY_ERR value. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 368 | */ |
| 369 | static LY_ERR |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 370 | lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 371 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 372 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 373 | const char *prefix, *name; |
| 374 | size_t prefix_len, name_len; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 375 | struct lyxml_ctx *xmlctx; |
| 376 | const struct ly_ctx *ctx; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 377 | const struct lyxml_ns *ns; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 378 | struct lyd_meta *meta = NULL; |
| 379 | struct lyd_attr *attr = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 380 | const struct lysc_node *snode; |
| 381 | struct lys_module *mod; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 382 | uint32_t prev_opts; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 383 | struct lyd_node *node = NULL, *anchor; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 384 | void *val_prefix_data; |
| 385 | LY_PREFIX_FORMAT format; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 386 | uint32_t getnext_opts; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 387 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 388 | xmlctx = lydctx->xmlctx; |
| 389 | ctx = xmlctx->ctx; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 390 | getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 391 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 392 | assert(xmlctx->status == LYXML_ELEMENT); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 393 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 394 | /* remember element prefix and name */ |
| 395 | prefix = xmlctx->prefix; |
| 396 | prefix_len = xmlctx->prefix_len; |
| 397 | name = xmlctx->name; |
| 398 | name_len = xmlctx->name_len; |
| 399 | |
| 400 | /* get the element module */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 401 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 402 | if (!ns) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 403 | LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 404 | ret = LY_EVALID; |
| 405 | goto error; |
| 406 | } |
| 407 | mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri); |
| 408 | if (!mod) { |
| 409 | if (lydctx->parse_options & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 410 | 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] | 411 | ret = LY_EVALID; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 412 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 413 | } |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 414 | if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) { |
| 415 | /* skip element with children */ |
| 416 | LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error); |
| 417 | return LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 418 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 421 | /* parser next */ |
| 422 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 423 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 424 | /* get the schema node */ |
| 425 | snode = NULL; |
| 426 | if (mod && (!parent || parent->schema)) { |
| 427 | snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts); |
| 428 | if (!snode) { |
| 429 | if (lydctx->parse_options & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 430 | LOGVAL(ctx, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 431 | ret = LY_EVALID; |
| 432 | goto error; |
| 433 | } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) { |
| 434 | /* skip element with children */ |
| 435 | LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error); |
| 436 | return LY_SUCCESS; |
| 437 | } |
| 438 | } else { |
| 439 | /* check that schema node is valid and can be used */ |
| 440 | LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), error); |
| 441 | LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), error); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /* create metadata/attributes */ |
| 446 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 447 | if (snode) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 448 | ret = lydxml_metadata(lydctx, &meta); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 449 | LY_CHECK_GOTO(ret, error); |
| 450 | } else { |
| 451 | assert(lydctx->parse_options & LYD_PARSE_OPAQ); |
| 452 | ret = lydxml_attrs(xmlctx, &attr); |
| 453 | LY_CHECK_GOTO(ret, error); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
| 458 | if (!snode) { |
| 459 | assert(lydctx->parse_options & LYD_PARSE_OPAQ); |
| 460 | |
| 461 | if (xmlctx->ws_only) { |
| 462 | /* ignore WS-only value */ |
| 463 | xmlctx->value_len = 0; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 464 | val_prefix_data = NULL; |
| 465 | format = LY_PREF_XML; |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 466 | } else { |
| 467 | /* get value prefixes */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 468 | ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_PREF_XML, |
| 469 | &xmlctx->ns, &format, &val_prefix_data); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 470 | LY_CHECK_GOTO(ret, error); |
| 471 | } |
| 472 | |
| 473 | /* create node */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 474 | ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value, |
| 475 | 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] | 476 | LY_CHECK_GOTO(ret, error); |
| 477 | |
| 478 | /* parser next */ |
| 479 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 480 | |
| 481 | /* process children */ |
| 482 | while (xmlctx->status == LYXML_ELEMENT) { |
| 483 | ret = lydxml_subtree_r(lydctx, (struct lyd_node_inner *)node, lyd_node_children_p(node)); |
| 484 | LY_CHECK_GOTO(ret, error); |
| 485 | } |
| 486 | } else if (snode->nodetype & LYD_NODE_TERM) { |
| 487 | /* create node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 488 | 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] | 489 | &xmlctx->dynamic, LY_PREF_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 490 | LOG_LOCSET(snode, node, NULL, NULL); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 491 | |
| 492 | if (parent && (node->schema->flags & LYS_KEY)) { |
| 493 | /* check the key order, the anchor must never be a key */ |
| 494 | anchor = lyd_insert_get_next_anchor(parent->child, node); |
| 495 | if (anchor && (anchor->schema->flags & LYS_KEY)) { |
| 496 | if (lydctx->parse_options & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 497 | 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] | 498 | ret = LY_EVALID; |
| 499 | goto error; |
| 500 | } else { |
| 501 | LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* parser next */ |
| 507 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 508 | |
| 509 | /* no children expected */ |
| 510 | if (xmlctx->status == LYXML_ELEMENT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 511 | 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] | 512 | xmlctx->name_len, xmlctx->name, snode->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 513 | ret = LY_EVALID; |
| 514 | goto error; |
| 515 | } |
| 516 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 517 | if (!xmlctx->ws_only) { |
| 518 | /* value in inner node */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 519 | 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] | 520 | xmlctx->value_len, xmlctx->value, snode->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 521 | ret = LY_EVALID; |
| 522 | goto error; |
| 523 | } |
| 524 | |
| 525 | /* create node */ |
| 526 | ret = lyd_create_inner(snode, &node); |
| 527 | LY_CHECK_GOTO(ret, error); |
| 528 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 529 | LOG_LOCSET(snode, node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 530 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 531 | /* parser next */ |
| 532 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 533 | |
| 534 | /* process children */ |
| 535 | while (xmlctx->status == LYXML_ELEMENT) { |
| 536 | ret = lydxml_subtree_r(lydctx, (struct lyd_node_inner *)node, lyd_node_children_p(node)); |
| 537 | LY_CHECK_GOTO(ret, error); |
| 538 | } |
| 539 | |
| 540 | if (snode->nodetype == LYS_LIST) { |
| 541 | /* check all keys exist */ |
| 542 | LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error); |
| 543 | } |
| 544 | |
| 545 | if (!(lydctx->parse_options & LYD_PARSE_ONLY)) { |
| 546 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
| 547 | ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL, NULL); |
| 548 | LY_CHECK_GOTO(ret, error); |
| 549 | |
| 550 | /* add any missing default children */ |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 551 | ret = lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, &lydctx->node_types, &lydctx->node_when, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 552 | (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 553 | LY_CHECK_GOTO(ret, error); |
| 554 | } |
| 555 | |
| 556 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 557 | /* rememeber the RPC/action/notification */ |
| 558 | lydctx->op_node = node; |
| 559 | } |
| 560 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 561 | if (!xmlctx->ws_only) { |
| 562 | /* value in inner node */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 563 | LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an any node \"%s\" found.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 564 | xmlctx->value_len, xmlctx->value, snode->name); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 565 | ret = LY_EVALID; |
| 566 | goto error; |
| 567 | } |
| 568 | |
| 569 | /* parser next */ |
| 570 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 571 | |
| 572 | /* parse any data tree with correct options */ |
| 573 | prev_opts = lydctx->parse_options; |
| 574 | lydctx->parse_options &= ~LYD_PARSE_STRICT; |
| 575 | lydctx->parse_options |= LYD_PARSE_OPAQ; |
| 576 | anchor = NULL; |
| 577 | while (xmlctx->status == LYXML_ELEMENT) { |
| 578 | ret = lydxml_subtree_r(lydctx, NULL, &anchor); |
| 579 | LY_CHECK_ERR_GOTO(ret, lydctx->parse_options = prev_opts, error); |
| 580 | } |
| 581 | lydctx->parse_options = prev_opts; |
| 582 | |
| 583 | /* create node */ |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 584 | ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 585 | LY_CHECK_GOTO(ret, error); |
| 586 | } |
| 587 | assert(node); |
| 588 | |
| 589 | /* add/correct flags */ |
| 590 | if (snode) { |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 591 | lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_options); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* parser next */ |
| 595 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 596 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error); |
| 597 | |
| 598 | /* add metadata/attributes */ |
| 599 | if (snode) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 600 | lyd_insert_meta(node, meta, 0); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 601 | } else { |
| 602 | lyd_insert_attr(node, attr); |
| 603 | } |
| 604 | |
| 605 | /* insert, keep first pointer correct */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 606 | lyd_insert_node(&parent->node, first_p, node); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 607 | while (!parent && (*first_p)->prev->next) { |
| 608 | *first_p = (*first_p)->prev; |
| 609 | } |
| 610 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 611 | LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 612 | return LY_SUCCESS; |
| 613 | |
| 614 | error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 615 | LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 616 | lyd_free_meta_siblings(meta); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 617 | lyd_free_attr_siblings(ctx, attr); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 618 | lyd_free_tree(node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 623 | lyd_parse_xml_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 624 | struct lyd_node **tree_p, struct lyd_ctx **lydctx_p) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 625 | { |
Radek Krejci | 18a57d9 | 2019-07-25 14:01:42 +0200 | [diff] [blame] | 626 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 627 | struct lyd_xml_ctx *lydctx; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 628 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 629 | assert(!(parse_options & ~LYD_PARSE_OPTS_MASK)); |
| 630 | assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK)); |
| 631 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 632 | /* init context */ |
| 633 | lydctx = calloc(1, sizeof *lydctx); |
| 634 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
| 635 | LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup); |
| 636 | lydctx->parse_options = parse_options; |
| 637 | lydctx->validate_options = validate_options; |
| 638 | lydctx->free = lyd_xml_ctx_free; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 639 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 640 | /* parse XML data */ |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 641 | while (lydctx->xmlctx->status == LYXML_ELEMENT) { |
| 642 | LY_CHECK_GOTO(ret = lydxml_subtree_r(lydctx, NULL, tree_p), cleanup); |
| 643 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 644 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 645 | cleanup: |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 646 | /* there should be no unres stored if validation should be skipped */ |
| 647 | assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
| 648 | !lydctx->node_when.count)); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 649 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 650 | if (ret) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 651 | lyd_xml_ctx_free((struct lyd_ctx *)lydctx); |
| 652 | lyd_free_all(*tree_p); |
| 653 | *tree_p = NULL; |
| 654 | } else { |
| 655 | *lydctx_p = (struct lyd_ctx *)lydctx; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 656 | |
| 657 | /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
| 658 | lyxml_ctx_free(lydctx->xmlctx); |
| 659 | lydctx->xmlctx = NULL; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 660 | } |
| 661 | return ret; |
| 662 | } |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 663 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 664 | #if 0 |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 665 | static LY_ERR |
| 666 | lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, struct lyd_node **envp) |
| 667 | { |
| 668 | LY_ERR ret = LY_SUCCESS; |
| 669 | const struct lyxml_ns *ns = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 670 | struct lyd_attr *attr = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 671 | const char *prefix; |
| 672 | size_t prefix_len; |
| 673 | |
| 674 | *envp = NULL; |
| 675 | |
| 676 | assert(xmlctx->status == LYXML_ELEMENT); |
| 677 | if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) { |
| 678 | /* not the expected element */ |
| 679 | return LY_SUCCESS; |
| 680 | } |
| 681 | |
| 682 | prefix = xmlctx->prefix; |
| 683 | prefix_len = xmlctx->prefix_len; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 684 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 685 | if (!ns) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 686 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 687 | prefix_len, prefix); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 688 | return LY_EVALID; |
| 689 | } else if (strcmp(ns->uri, uri)) { |
| 690 | /* different namespace */ |
| 691 | return LY_SUCCESS; |
| 692 | } |
| 693 | |
| 694 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 695 | |
| 696 | /* create attributes */ |
| 697 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 698 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 699 | } |
| 700 | |
| 701 | if (!xmlctx->ws_only) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 702 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 703 | xmlctx->value_len, xmlctx->value, name); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 704 | ret = LY_EVALID; |
| 705 | goto cleanup; |
| 706 | } |
| 707 | |
| 708 | /* parser next element */ |
| 709 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 710 | |
| 711 | /* create node */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 712 | ret = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), "", 0, NULL, |
| 713 | LY_PREF_XML, NULL, LYD_NODEHINT_ENVELOPE, envp); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 714 | LY_CHECK_GOTO(ret, cleanup); |
| 715 | |
| 716 | /* assign atributes */ |
| 717 | ((struct lyd_node_opaq *)(*envp))->attr = attr; |
| 718 | attr = NULL; |
| 719 | |
| 720 | cleanup: |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 721 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 722 | return ret; |
| 723 | } |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 724 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 725 | #endif |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 726 | |
| 727 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 728 | lyd_parse_xml_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 729 | { |
| 730 | LY_ERR ret = LY_SUCCESS; |
| 731 | struct lyd_xml_ctx lydctx = {0}; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 732 | struct lyd_node *tree = NULL; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 733 | |
| 734 | /* init */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 735 | LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx.xmlctx), cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 736 | lydctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 737 | lydctx.int_opts = LYD_INTOPT_RPC; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 738 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 739 | #if 0 |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 740 | /* parse "rpc", if any */ |
| 741 | LY_CHECK_GOTO(ret = lydxml_envelope(lydctx.xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", &rpc_e), cleanup); |
| 742 | |
| 743 | if (rpc_e) { |
| 744 | /* parse "action", if any */ |
| 745 | LY_CHECK_GOTO(ret = lydxml_envelope(lydctx.xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", &act_e), cleanup); |
| 746 | } |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 747 | #endif |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 748 | |
| 749 | /* parse the rest of data normally */ |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 750 | LY_CHECK_GOTO(ret = lydxml_subtree_r(&lydctx, NULL, &tree), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 751 | |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 752 | /* make sure we have parsed some operation and it is the only subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 753 | if (!lydctx.op_node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 754 | LOGVAL(ctx, LYVE_DATA, "Missing the \"rpc\"/\"action\" node."); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 755 | ret = LY_EVALID; |
| 756 | goto cleanup; |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 757 | } else if (lydctx.xmlctx->status == LYXML_ELEMENT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 758 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 759 | tree->schema->name); |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 760 | ret = LY_EVALID; |
| 761 | goto cleanup; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 762 | } |
| 763 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 764 | if (op_p) { |
| 765 | *op_p = lydctx.op_node; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 766 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 767 | assert(tree); |
Michal Vasko | 4189c0f | 2020-08-13 09:05:22 +0200 | [diff] [blame] | 768 | if (tree_p) { |
| 769 | *tree_p = tree; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | cleanup: |
| 773 | /* we have used parse_only flag */ |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 774 | assert(!lydctx.node_types.count && !lydctx.meta_types.count && !lydctx.node_when.count); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 775 | lyxml_ctx_free(lydctx.xmlctx); |
| 776 | if (ret) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 777 | lyd_free_all(tree); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 778 | } |
| 779 | return ret; |
| 780 | } |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 781 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 782 | #if 0 |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 783 | static LY_ERR |
| 784 | lydxml_notif_envelope(struct lyxml_ctx *xmlctx, struct lyd_node **envp) |
| 785 | { |
| 786 | LY_ERR ret = LY_SUCCESS; |
| 787 | const struct lyxml_ns *ns = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 788 | struct lyd_attr *attr = NULL; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 789 | struct lyd_node *et; |
| 790 | const char *prefix; |
| 791 | size_t prefix_len; |
| 792 | |
| 793 | *envp = NULL; |
| 794 | |
| 795 | /* container envelope */ |
| 796 | LY_CHECK_GOTO(ret = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 797 | envp), cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 798 | |
| 799 | /* no envelope, fine */ |
| 800 | if (!*envp) { |
| 801 | goto cleanup; |
| 802 | } |
| 803 | |
| 804 | /* child "eventTime" */ |
| 805 | if ((xmlctx->status != LYXML_ELEMENT) || ly_strncmp("eventTime", xmlctx->name, xmlctx->name_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 806 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing the \"eventTime\" element."); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 807 | ret = LY_EVALID; |
| 808 | goto cleanup; |
| 809 | } |
| 810 | |
| 811 | prefix = xmlctx->prefix; |
| 812 | prefix_len = xmlctx->prefix_len; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 813 | ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 814 | if (!ns) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 815 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 816 | ret = LY_EVALID; |
| 817 | goto cleanup; |
| 818 | } else if (strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:notification:1.0")) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 819 | LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid namespace \"%s\" of \"eventTime\".", ns->uri); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 820 | ret = LY_EVALID; |
| 821 | goto cleanup; |
| 822 | } |
| 823 | |
| 824 | LY_CHECK_RET(lyxml_ctx_next(xmlctx)); |
| 825 | |
| 826 | /* create attributes */ |
| 827 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
| 828 | LY_CHECK_RET(lydxml_attrs(xmlctx, &attr)); |
| 829 | } |
| 830 | |
| 831 | /* validate value */ |
| 832 | /* TODO */ |
| 833 | /*if (!xmlctx->ws_only) { |
| 834 | LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.", |
| 835 | xmlctx->value_len, xmlctx->value, name); |
| 836 | ret = LY_EVALID; |
| 837 | goto cleanup; |
| 838 | }*/ |
| 839 | |
| 840 | /* create node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 841 | ret = lyd_create_opaq(xmlctx->ctx, "eventTime", ly_strlen_const("eventTime"), prefix, prefix_len, |
| 842 | ns->uri, strlen(ns->uri), xmlctx->value, xmlctx->value_len, NULL, LY_PREF_XML, NULL, LYD_NODEHINT_ENVELOPE, &et); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 843 | LY_CHECK_GOTO(ret, cleanup); |
| 844 | |
| 845 | /* assign atributes */ |
| 846 | ((struct lyd_node_opaq *)et)->attr = attr; |
| 847 | attr = NULL; |
| 848 | |
| 849 | /* insert */ |
| 850 | lyd_insert_node(*envp, NULL, et); |
| 851 | |
| 852 | /* finish parsing */ |
| 853 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 854 | if (xmlctx->status != LYXML_ELEM_CLOSE) { |
| 855 | assert(xmlctx->status == LYXML_ELEMENT); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 856 | LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"eventTime\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 857 | xmlctx->name_len, xmlctx->name); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 858 | ret = LY_EVALID; |
| 859 | goto cleanup; |
| 860 | } |
| 861 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 862 | |
| 863 | cleanup: |
| 864 | if (ret) { |
| 865 | lyd_free_tree(*envp); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 866 | lyd_free_attr_siblings(xmlctx->ctx, attr); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 867 | } |
| 868 | return ret; |
| 869 | } |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 870 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 871 | #endif |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 872 | |
| 873 | LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 874 | lyd_parse_xml_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p) |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 875 | { |
| 876 | LY_ERR ret = LY_SUCCESS; |
| 877 | struct lyd_xml_ctx lydctx = {0}; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 878 | struct lyd_node *tree = NULL; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 879 | |
| 880 | /* init */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 881 | LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx.xmlctx), cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 882 | lydctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 883 | lydctx.int_opts = LYD_INTOPT_NOTIF; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 884 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 885 | #if 0 |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 886 | /* parse "notification" and "eventTime", if present */ |
| 887 | LY_CHECK_GOTO(ret = lydxml_notif_envelope(lydctx.xmlctx, &ntf_e), cleanup); |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 888 | #endif |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 889 | |
| 890 | /* parse the rest of data normally */ |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 891 | LY_CHECK_GOTO(ret = lydxml_subtree_r(&lydctx, NULL, &tree), cleanup); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 892 | |
| 893 | /* make sure we have parsed some notification */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 894 | if (!lydctx.op_node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 895 | LOGVAL(ctx, LYVE_DATA, "Missing the \"notification\" node."); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 896 | ret = LY_EVALID; |
| 897 | goto cleanup; |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 898 | } else if (lydctx.xmlctx->status == LYXML_ELEMENT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 899 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 900 | tree->schema->name); |
Michal Vasko | cf770e2 | 2020-08-12 13:21:43 +0200 | [diff] [blame] | 901 | ret = LY_EVALID; |
| 902 | goto cleanup; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 905 | if (ntf_p) { |
| 906 | *ntf_p = lydctx.op_node; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 907 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 908 | assert(tree); |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 909 | if (tree_p) { |
| 910 | *tree_p = tree; |
| 911 | } |
| 912 | |
| 913 | cleanup: |
| 914 | /* we have used parse_only flag */ |
| 915 | assert(!lydctx.node_types.count && !lydctx.meta_types.count && !lydctx.node_when.count); |
| 916 | lyxml_ctx_free(lydctx.xmlctx); |
| 917 | if (ret) { |
| 918 | lyd_free_all(tree); |
| 919 | } |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | LY_ERR |
| 924 | lyd_parse_xml_reply(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p) |
| 925 | { |
| 926 | LY_ERR ret = LY_SUCCESS; |
| 927 | struct lyd_xml_ctx lydctx = {0}; |
| 928 | struct lyd_node *tree = NULL; |
| 929 | |
| 930 | /* init */ |
| 931 | LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx.xmlctx), cleanup); |
| 932 | lydctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT; |
| 933 | lydctx.int_opts = LYD_INTOPT_REPLY; |
| 934 | |
| 935 | #if 0 |
| 936 | /* parse "rpc-reply", if any */ |
| 937 | LY_CHECK_GOTO(ret = lydxml_envelope(lydctx.xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", &rpcr_e), |
| 938 | cleanup); |
| 939 | #endif |
| 940 | |
| 941 | /* parse the rest of data normally but connect them to the duplicated operation */ |
| 942 | while (lydctx.xmlctx->status == LYXML_ELEMENT) { |
| 943 | ret = lydxml_subtree_r(&lydctx, NULL, &tree); |
| 944 | LY_CHECK_GOTO(ret, cleanup); |
| 945 | } |
| 946 | |
| 947 | if (op_p) { |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 948 | *op_p = lydctx.op_node; |
Michal Vasko | 4189c0f | 2020-08-13 09:05:22 +0200 | [diff] [blame] | 949 | } |
| 950 | if (tree_p) { |
| 951 | *tree_p = tree; |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | cleanup: |
| 955 | /* we have used parse_only flag */ |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 956 | assert(!lydctx.node_types.count && !lydctx.meta_types.count && !lydctx.node_when.count); |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 957 | lyxml_ctx_free(lydctx.xmlctx); |
| 958 | if (ret) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 959 | lyd_free_all(tree); |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 960 | } |
| 961 | return ret; |
| 962 | } |