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