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