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