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