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