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 | |
| 15 | #include "common.h" |
| 16 | |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 20 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | |
| 22 | #include "context.h" |
| 23 | #include "dict.h" |
| 24 | #include "log.h" |
| 25 | #include "plugins_types.h" |
| 26 | #include "set.h" |
| 27 | #include "tree_data.h" |
| 28 | #include "tree_data_internal.h" |
| 29 | #include "tree_schema.h" |
| 30 | #include "xml.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 31 | #include "validation.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 | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 35 | */ |
| 36 | struct lyd_xml_ctx { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 37 | struct lyxml_ctx *xmlctx; /**< XML context */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 38 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 39 | uint32_t options; /**< various @ref dataparseroptions. */ |
| 40 | uint32_t path_len; /**< used bytes in the path buffer */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 41 | #define LYD_PARSER_BUFSIZE 4078 |
| 42 | char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 43 | struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */ |
| 44 | struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 45 | struct ly_set when_check; /**< set of nodes with "when" conditions */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /** |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 49 | * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used |
| 50 | * in the values to the schema via XML namespaces. |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 51 | */ |
| 52 | static const struct lys_module * |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 53 | lydxml_resolve_prefix(const struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser) |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 54 | { |
| 55 | const struct lyxml_ns *ns; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 56 | struct lyxml_ctx *xmlctx = (struct lyxml_ctx *)parser; |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 57 | |
| 58 | ns = lyxml_ns_get(xmlctx, prefix, prefix_len); |
| 59 | if (!ns) { |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | return ly_ctx_get_module_implemented_ns(ctx, ns->uri); |
| 64 | } |
| 65 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 66 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 67 | lydxml_metadata(struct lyxml_ctx *xmlctx, const struct lysc_node *sparent, int strict, struct ly_set *type_meta_check, |
| 68 | 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 | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 75 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 76 | *meta = NULL; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 77 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 78 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 79 | if (!xmlctx->prefix_len) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 80 | /* in XML, all attributes must be prefixed |
| 81 | * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 82 | if (strict) { |
| 83 | LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".", |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 84 | xmlctx->name_len, xmlctx->name); |
| 85 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 86 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 87 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 88 | skip_attr: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 89 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 90 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 91 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 92 | continue; |
| 93 | } |
| 94 | |
| 95 | /* get namespace of the attribute to find its annotation definition */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 96 | ns = lyxml_ns_get(xmlctx, xmlctx->prefix, xmlctx->prefix_len); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 97 | if (!ns) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 98 | /* unknown namespace, XML error */ |
| 99 | LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 100 | 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 | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 106 | if (strict) { |
| 107 | LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 108 | "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".", |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 109 | ns->uri, xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", xmlctx->name_len, |
| 110 | xmlctx->name); |
| 111 | goto cleanup; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 112 | } |
| 113 | goto skip_attr; |
| 114 | } |
| 115 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 116 | /* remember attr name and get its content */ |
| 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 */ |
| 123 | ret = lyd_create_meta(NULL, meta, mod, name, name_len, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, |
| 124 | lydxml_resolve_prefix, xmlctx, LYD_XML, sparent); |
| 125 | if (ret == LY_EINCOMPLETE) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 126 | if (type_meta_check) { |
| 127 | ly_set_add(type_meta_check, meta, LY_SET_OPT_USEASLIST); |
| 128 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 129 | } else if (ret) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 130 | goto cleanup; |
| 131 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 132 | |
| 133 | /* next attribute */ |
| 134 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 135 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 136 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 137 | ret = LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 138 | |
| 139 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 140 | if (ret) { |
| 141 | lyd_free_meta(xmlctx->ctx, *meta, 1); |
| 142 | *meta = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 143 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 147 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 148 | lydxml_attrs(struct lyxml_ctx *xmlctx, struct ly_attr **attr) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 149 | { |
| 150 | LY_ERR ret = LY_SUCCESS; |
| 151 | const struct lyxml_ns *ns; |
| 152 | struct ly_prefix *val_prefs; |
| 153 | struct ly_attr *attr2; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 154 | const char *name, *prefix; |
| 155 | size_t name_len, prefix_len; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 156 | |
| 157 | assert(attr); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 158 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 159 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 160 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 161 | ns = NULL; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 162 | if (xmlctx->prefix_len) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 163 | /* get namespace of the attribute */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 164 | ns = lyxml_ns_get(xmlctx, xmlctx->prefix, xmlctx->prefix_len); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 165 | if (!ns) { |
| 166 | LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 167 | xmlctx->prefix_len, xmlctx->prefix); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 168 | ret = LY_EVALID; |
| 169 | goto cleanup; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (*attr) { |
| 174 | attr2 = *attr; |
| 175 | } else { |
| 176 | attr2 = NULL; |
| 177 | } |
| 178 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 179 | /* remember attr prefix, name, and get its content */ |
| 180 | prefix = xmlctx->prefix; |
| 181 | prefix_len = xmlctx->prefix_len; |
| 182 | name = xmlctx->name; |
| 183 | name_len = xmlctx->name_len; |
| 184 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 185 | assert(xmlctx->status == LYXML_ATTR_CONTENT); |
| 186 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 187 | /* get value prefixes */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 188 | LY_CHECK_GOTO(ret = lyxml_get_prefixes(xmlctx, xmlctx->value, xmlctx->value_len, &val_prefs), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 189 | |
| 190 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 191 | ret = ly_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, xmlctx->value, xmlctx->value_len, |
| 192 | &xmlctx->dynamic, LYD_XML, val_prefs, prefix, prefix_len, ns ? ns->uri : NULL); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 193 | LY_CHECK_GOTO(ret, cleanup); |
| 194 | |
| 195 | if (!*attr) { |
| 196 | *attr = attr2; |
| 197 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 198 | |
| 199 | /* next attribute */ |
| 200 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 204 | if (ret) { |
| 205 | ly_free_attr(xmlctx->ctx, *attr, 1); |
| 206 | *attr = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 207 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 211 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 212 | lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list) |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 213 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 214 | LY_ERR ret = LY_SUCCESS, r; |
| 215 | enum LYXML_PARSER_STATUS next; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 216 | struct ly_set key_set = {0}; |
| 217 | const struct lysc_node *snode; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 218 | uint32_t i, parents_count; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 219 | |
| 220 | assert(list && (list->nodetype == LYS_LIST)); |
| 221 | |
| 222 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 223 | snode = NULL; |
| 224 | while ((snode = lys_getnext(snode, list, NULL, LYS_GETNEXT_NOSTATECHECK)) && (snode->flags & LYS_KEY)) { |
| 225 | ly_set_add(&key_set, (void *)snode, LY_SET_OPT_USEASLIST); |
| 226 | } |
| 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 */ |
| 248 | r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, lydxml_resolve_prefix, xmlctx, LYD_XML); |
| 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 */ |
| 259 | parents_count = xmlctx->elements.count; |
| 260 | while ((parents_count < xmlctx->elements.count) || (xmlctx->status == LYXML_ELEMENT)) { |
| 261 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 262 | } |
| 263 | |
| 264 | /* parser next, but do not parse closing element of the list because it would remove its namespaces */ |
| 265 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 266 | LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup); |
| 267 | if (next != LYXML_ELEM_CLOSE) { |
| 268 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 269 | } |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (key_set.count) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 273 | /* some keys are missing/did not validate */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 274 | ret = LY_ENOT; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | cleanup: |
| 278 | ly_set_erase(&key_set, NULL); |
| 279 | return ret; |
| 280 | } |
| 281 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 282 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 283 | * @brief Parse XML elements as YANG data node children the specified parent node. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 284 | * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 285 | * @param[in] lydctx XML YANG data parser context. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 286 | * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
| 287 | * @param[in,out] data Pointer to the XML string representation of the YANG data to parse. |
| 288 | * @param[out] node Resulting list of the parsed nodes. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 289 | * @return LY_ERR value. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 290 | */ |
| 291 | static LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 292 | lydxml_data_r(struct lyd_xml_ctx *lydctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **first) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 293 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 294 | LY_ERR ret = LY_SUCCESS; |
| 295 | enum LYXML_PARSER_STATUS prev_status; |
| 296 | const char *prefix, *name, *prev_input, *pname, *pprefix; |
| 297 | size_t prefix_len, name_len, pprefix_len, pname_len; |
| 298 | struct lyxml_ctx *xmlctx; |
| 299 | const struct ly_ctx *ctx; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 300 | const struct lyxml_ns *ns; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 301 | struct lyd_meta *meta = NULL, *meta2, *prev_meta; |
| 302 | struct ly_attr *attr = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 303 | const struct lysc_node *snode; |
| 304 | struct lys_module *mod; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 305 | uint32_t prev_opts, parents_count; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 306 | struct lyd_node *cur = NULL, *anchor; |
| 307 | struct ly_prefix *val_prefs; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 308 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 309 | xmlctx = lydctx->xmlctx; |
| 310 | ctx = xmlctx->ctx; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 311 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 312 | while (xmlctx->status == LYXML_ELEMENT) { |
| 313 | /* remember element prefix and name */ |
| 314 | prefix = xmlctx->prefix; |
| 315 | prefix_len = xmlctx->prefix_len; |
| 316 | name = xmlctx->name; |
| 317 | name_len = xmlctx->name_len; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 318 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 319 | /* get the element module */ |
| 320 | ns = lyxml_ns_get(xmlctx, prefix, prefix_len); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 321 | if (!ns) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 322 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", |
| 323 | prefix_len, prefix); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 324 | ret = LY_EVALID; |
| 325 | goto cleanup; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 326 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 327 | mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri); |
| 328 | if (!mod && (lydctx->options & LYD_OPT_STRICT)) { |
| 329 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 330 | ret = LY_EVALID; |
| 331 | goto cleanup; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 332 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 333 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 334 | /* get the schema node */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 335 | snode = NULL; |
| 336 | if (mod && (!parent || parent->schema)) { |
| 337 | /* leave if-feature check for validation */ |
| 338 | snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 339 | if (!snode) { |
| 340 | if (lydctx->options & LYD_OPT_STRICT) { |
| 341 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", |
| 342 | name_len, name, mod->name); |
| 343 | ret = LY_EVALID; |
| 344 | goto cleanup; |
| 345 | } else if (!(lydctx->options & LYD_OPT_OPAQ)) { |
| 346 | /* remember current number of parents */ |
| 347 | parents_count = xmlctx->elements.count; |
| 348 | |
| 349 | /* skip after the content */ |
| 350 | while (xmlctx->status != LYXML_ELEM_CONTENT) { |
| 351 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 352 | } |
| 353 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 354 | |
| 355 | /* skip all children elements, recursively, if any */ |
| 356 | while (parents_count < xmlctx->elements.count) { |
| 357 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 358 | } |
| 359 | |
| 360 | /* close element */ |
| 361 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 362 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 363 | continue; |
| 364 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 365 | } |
| 366 | if (snode) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 367 | if ((lydctx->options & LYD_OPT_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
| 368 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTATE, snode->name); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 369 | ret = LY_EVALID; |
| 370 | goto cleanup; |
| 371 | } |
| 372 | if (snode->nodetype & (LYS_ACTION | LYS_NOTIF)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 373 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%.*s\".", |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 374 | snode->nodetype == LYS_ACTION ? "RPC/action" : "notification", name_len, name); |
| 375 | ret = LY_EVALID; |
| 376 | goto cleanup; |
| 377 | } |
| 378 | } |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 379 | } |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 380 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 381 | /* parser next */ |
| 382 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 383 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 384 | if (snode && (lydctx->options & LYD_OPT_OPAQ) && (snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) { |
| 385 | /* backup parser */ |
| 386 | prev_status = xmlctx->status; |
| 387 | pprefix = xmlctx->prefix; |
| 388 | pprefix_len = xmlctx->prefix_len; |
| 389 | pname = xmlctx->name; |
| 390 | pname_len = xmlctx->name_len; |
| 391 | prev_input = xmlctx->input; |
| 392 | if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) { |
| 393 | /* it was backed up, do not free */ |
| 394 | xmlctx->dynamic = 0; |
| 395 | } |
| 396 | |
| 397 | /* skip attributes */ |
| 398 | while (xmlctx->status == LYXML_ATTRIBUTE) { |
| 399 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 400 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 401 | } |
| 402 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 403 | if (snode->nodetype & LYD_NODE_TERM) { |
| 404 | /* value may not be valid in which case we parse it as an opaque node */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 405 | if (lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, lydxml_resolve_prefix, xmlctx, LYD_XML)) { |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 406 | snode = NULL; |
| 407 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 408 | } else { |
| 409 | /* skip content */ |
| 410 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 411 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 412 | if (lydxml_check_list(xmlctx, snode)) { |
| 413 | /* invalid list, parse as opaque if it does */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 414 | snode = NULL; |
| 415 | } |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 416 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 417 | |
| 418 | /* restore parser */ |
| 419 | if (xmlctx->dynamic) { |
| 420 | free((char *)xmlctx->value); |
| 421 | } |
| 422 | xmlctx->status = prev_status; |
| 423 | xmlctx->prefix = pprefix; |
| 424 | xmlctx->prefix_len = pprefix_len; |
| 425 | xmlctx->name = pname; |
| 426 | xmlctx->name_len = pname_len; |
| 427 | xmlctx->input = prev_input; |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 430 | /* create metadata/attributes */ |
| 431 | if (xmlctx->status == LYXML_ATTRIBUTE) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 432 | if (snode) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 433 | ret = lydxml_metadata(xmlctx, snode, lydctx->options & LYD_OPT_STRICT, &lydctx->unres_meta_type, &meta); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 434 | LY_CHECK_GOTO(ret, cleanup); |
| 435 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 436 | assert(lydctx->options & LYD_OPT_OPAQ); |
| 437 | ret = lydxml_attrs(xmlctx, &attr); |
| 438 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 439 | } |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 442 | assert(xmlctx->status == LYXML_ELEM_CONTENT); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 443 | if (!snode) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 444 | assert(lydctx->options & LYD_OPT_OPAQ); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 445 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 446 | if (xmlctx->ws_only) { |
| 447 | /* ignore WS-only value */ |
| 448 | xmlctx->value_len = 0; |
| 449 | val_prefs = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 450 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 451 | /* get value prefixes */ |
| 452 | ret = lyxml_get_prefixes(xmlctx, xmlctx->value, xmlctx->value_len, &val_prefs); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 453 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 454 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 455 | |
| 456 | /* create node */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 457 | ret = lyd_create_opaq(ctx, name, name_len, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, LYD_XML, |
| 458 | val_prefs, prefix, prefix_len, ns->uri, &cur); |
| 459 | LY_CHECK_GOTO(ret, cleanup); |
| 460 | |
| 461 | /* parser next */ |
| 462 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 463 | |
| 464 | /* process children */ |
| 465 | if (xmlctx->status == LYXML_ELEMENT) { |
| 466 | ret = lydxml_data_r(lydctx, (struct lyd_node_inner *)cur, data, lyd_node_children_p(cur)); |
| 467 | LY_CHECK_GOTO(ret, cleanup); |
| 468 | } |
| 469 | } else if (snode->nodetype & LYD_NODE_TERM) { |
| 470 | /* create node */ |
| 471 | ret = lyd_create_term(snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, lydxml_resolve_prefix, |
| 472 | xmlctx, LYD_XML, &cur); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 473 | if (ret == LY_EINCOMPLETE) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 474 | if (!(lydctx->options & LYD_OPT_PARSE_ONLY)) { |
| 475 | ly_set_add(&lydctx->unres_node_type, cur, LY_SET_OPT_USEASLIST); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 476 | } |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 477 | } else if (ret) { |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 478 | goto cleanup; |
| 479 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 480 | |
| 481 | if (parent && (cur->schema->flags & LYS_KEY)) { |
| 482 | /* check the key order, the anchor must always be the last child */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 483 | anchor = lyd_get_prev_key_anchor(parent->child, cur->schema); |
| 484 | if ((!anchor && parent->child) || (anchor && anchor->next)) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 485 | if (lydctx->options & LYD_OPT_STRICT) { |
| 486 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 487 | cur->schema->name); |
| 488 | ret = LY_EVALID; |
| 489 | goto cleanup; |
| 490 | } else { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 491 | LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", cur->schema->name); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 495 | |
| 496 | /* parser next */ |
| 497 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 498 | |
| 499 | /* no children expected */ |
| 500 | if (xmlctx->status == LYXML_ELEMENT) { |
| 501 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Child element inside a terminal node \"%s\" found.", |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 502 | snode->name); |
| 503 | ret = LY_EVALID; |
| 504 | goto cleanup; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 505 | } |
| 506 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 507 | if (!xmlctx->ws_only) { |
| 508 | /* value in inner node */ |
| 509 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Text value inside an inner node \"%s\" found.", |
| 510 | snode->name); |
| 511 | ret = LY_EVALID; |
| 512 | goto cleanup; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 513 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 514 | |
| 515 | /* create node */ |
| 516 | ret = lyd_create_inner(snode, &cur); |
| 517 | LY_CHECK_GOTO(ret, cleanup); |
| 518 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 519 | /* parser next */ |
| 520 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 521 | |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 522 | /* process children */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 523 | if (xmlctx->status == LYXML_ELEMENT) { |
| 524 | ret = lydxml_data_r(lydctx, (struct lyd_node_inner *)cur, data, lyd_node_children_p(cur)); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 525 | LY_CHECK_GOTO(ret, cleanup); |
| 526 | } |
| 527 | |
| 528 | if (snode->nodetype == LYS_LIST) { |
| 529 | /* check all keys exist */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 530 | LY_CHECK_GOTO(ret = lyd_parse_check_keys(cur), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 531 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 532 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 533 | if (!(lydctx->options & LYD_OPT_PARSE_ONLY)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 534 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
| 535 | ret = lyd_validate_new(lyd_node_children_p(cur), snode, NULL); |
| 536 | LY_CHECK_GOTO(ret, cleanup); |
| 537 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 538 | /* add any missing default children */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 539 | ret = lyd_validate_defaults_r((struct lyd_node_inner *)cur, lyd_node_children_p(cur), NULL, NULL, |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 540 | &lydctx->unres_node_type, &lydctx->when_check, lydctx->options); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 541 | LY_CHECK_GOTO(ret, cleanup); |
| 542 | } |
| 543 | |
| 544 | /* hash now that all keys should be parsed, rehash for key-less list */ |
| 545 | if (snode->nodetype == LYS_LIST) { |
| 546 | lyd_hash(cur); |
| 547 | } |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 548 | } else if (snode->nodetype & LYD_NODE_ANY) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 549 | if (!xmlctx->ws_only) { |
| 550 | /* value in inner node */ |
| 551 | LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Text value inside an any node \"%s\" found.", |
| 552 | snode->name); |
| 553 | ret = LY_EVALID; |
| 554 | goto cleanup; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 555 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 556 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 557 | /* parser next */ |
| 558 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
| 559 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 560 | /* parse any data tree with correct options */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 561 | prev_opts = lydctx->options; |
| 562 | lydctx->options &= ~LYD_OPT_STRICT; |
| 563 | lydctx->options |= LYD_OPT_OPAQ; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 564 | anchor = NULL; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 565 | ret = lydxml_data_r(lydctx, NULL, data, &anchor); |
| 566 | lydctx->options = prev_opts; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 567 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 568 | |
| 569 | /* create node */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 570 | ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, &cur); |
| 571 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 572 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 573 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 574 | /* correct flags */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 575 | if (snode) { |
| 576 | if (!(snode->nodetype & (LYS_ACTION | LYS_NOTIF)) && snode->when) { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 577 | if (lydctx->options & LYD_OPT_TRUSTED) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 578 | /* just set it to true */ |
| 579 | cur->flags |= LYD_WHEN_TRUE; |
| 580 | } else { |
| 581 | /* remember we need to evaluate this node's when */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 582 | ly_set_add(&lydctx->when_check, cur, LY_SET_OPT_USEASLIST); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 583 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 584 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 585 | if (lydctx->options & LYD_OPT_TRUSTED) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 586 | /* node is valid */ |
| 587 | cur->flags &= ~LYD_NEW; |
| 588 | } |
| 589 | prev_meta = NULL; |
| 590 | LY_LIST_FOR(meta, meta2) { |
| 591 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") |
| 592 | && meta2->value.boolean) { |
| 593 | /* node is default according to the metadata */ |
| 594 | cur->flags |= LYD_DEFAULT; |
| 595 | |
| 596 | /* delete the metadata */ |
| 597 | if (prev_meta) { |
| 598 | prev_meta->next = meta2->next; |
| 599 | } else { |
| 600 | meta = meta->next; |
| 601 | } |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 602 | lyd_free_meta(ctx, meta2, 0); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 603 | break; |
| 604 | } |
| 605 | |
| 606 | prev_meta = meta2; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 610 | /* add metadata/attributes */ |
| 611 | if (snode) { |
| 612 | cur->meta = meta; |
| 613 | meta = NULL; |
| 614 | } else { |
| 615 | assert(!cur->schema); |
| 616 | ((struct lyd_node_opaq *)cur)->attr = attr; |
| 617 | attr = NULL; |
| 618 | } |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 619 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 620 | /* insert */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 621 | lyd_insert_node((struct lyd_node *)parent, first, cur); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 622 | cur = NULL; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 623 | |
| 624 | /* parser next */ |
| 625 | assert(xmlctx->status == LYXML_ELEM_CLOSE); |
| 626 | LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 629 | /* success */ |
| 630 | ret = LY_SUCCESS; |
| 631 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 632 | cleanup: |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 633 | lyd_free_meta(ctx, meta, 1); |
| 634 | ly_free_attr(ctx, attr, 1); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 635 | lyd_free_tree(cur); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 636 | if (ret && *first) { |
| 637 | lyd_free_siblings(*first); |
| 638 | *first = NULL; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 639 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 644 | lyd_parse_xml_data(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **tree) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 645 | { |
Radek Krejci | 18a57d9 | 2019-07-25 14:01:42 +0200 | [diff] [blame] | 646 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 647 | struct lyd_xml_ctx lydctx = {0}; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 648 | uint32_t i = 0; |
| 649 | const struct lys_module *mod; |
| 650 | struct lyd_node *first, *next, **first2; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 651 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 652 | /* init context and tree */ |
| 653 | LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, data, &lydctx.xmlctx), cleanup); |
| 654 | lydctx.options = options; |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 655 | *tree = NULL; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 656 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 657 | /* parse XML data */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 658 | ret = lydxml_data_r(&lydctx, NULL, &data, tree); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 659 | LY_CHECK_GOTO(ret, cleanup); |
| 660 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 661 | if (!(options & LYD_OPT_PARSE_ONLY)) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 662 | next = *tree; |
| 663 | while (1) { |
| 664 | if (options & LYD_VALOPT_DATA_ONLY) { |
| 665 | mod = lyd_data_next_module(&next, &first); |
| 666 | } else { |
| 667 | mod = lyd_mod_next_module(next, NULL, 0, ctx, &i, &first); |
| 668 | } |
| 669 | if (!mod) { |
| 670 | break; |
| 671 | } |
| 672 | if (first == *tree) { |
| 673 | /* make sure first2 changes are carried to tree */ |
| 674 | first2 = tree; |
| 675 | } else { |
| 676 | first2 = &first; |
| 677 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 678 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 679 | /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */ |
| 680 | ret = lyd_validate_new(first2, NULL, mod); |
| 681 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 682 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 683 | /* add all top-level defaults for this module */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 684 | ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &lydctx.unres_node_type, &lydctx.when_check, |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 685 | options & LYD_VALOPT_MASK); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 686 | LY_CHECK_GOTO(ret, cleanup); |
| 687 | |
| 688 | /* finish incompletely validated terminal values/attributes and when conditions */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 689 | ret = lyd_validate_unres(tree, &lydctx.when_check, &lydctx.unres_node_type, &lydctx.unres_meta_type, LYD_XML, |
| 690 | lydxml_resolve_prefix, lydctx.xmlctx); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 691 | LY_CHECK_GOTO(ret, cleanup); |
| 692 | |
| 693 | /* perform final validation that assumes the data tree is final */ |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 694 | ret = lyd_validate_siblings_r(*first2, NULL, mod, options & LYD_VALOPT_MASK); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 695 | LY_CHECK_GOTO(ret, cleanup); |
| 696 | } |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 699 | cleanup: |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 700 | /* there should be no unresolved types stored */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 701 | assert(!(options & LYD_OPT_PARSE_ONLY) || (!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count |
| 702 | && !lydctx.when_check.count)); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 703 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame^] | 704 | ly_set_erase(&lydctx.unres_node_type, NULL); |
| 705 | ly_set_erase(&lydctx.unres_meta_type, NULL); |
| 706 | ly_set_erase(&lydctx.when_check, NULL); |
| 707 | lyxml_ctx_free(lydctx.xmlctx); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 708 | if (ret) { |
| 709 | lyd_free_all(*tree); |
| 710 | *tree = NULL; |
| 711 | } |
| 712 | return ret; |
| 713 | } |