blob: 8e9a5dc4784ba8cec0830a25018ed69b3e2521c5 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko8cc3f662022-03-29 11:25:51 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02005 * @brief XML data parser for libyang
6 *
Michal Vasko8cc3f662022-03-29 11:25:51 +02007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Michal Vasko742a5b12022-02-24 16:07:27 +010016#define _GNU_SOURCE
17
Michal Vasko69730152020-10-09 16:30:07 +020018#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
Michal Vasko742a5b12022-02-24 16:07:27 +010023#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010025#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010028#include "ly_common.h"
Radek Krejci7931b192020-06-25 17:05:03 +020029#include "parser_data.h"
30#include "parser_internal.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010031#include "plugins_exts.h"
Michal Vasko51de7b72022-04-29 09:50:22 +020032#include "plugins_internal.h"
33#include "schema_compile_node.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "set.h"
Radek Krejci77114102021-03-10 15:21:57 +010035#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree_data_internal.h"
38#include "tree_schema.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_schema_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010040#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020042
Michal Vaskod027f382023-02-10 09:13:25 +010043static LY_ERR lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
44 struct ly_set *parsed);
45
Radek Krejci1798aae2020-07-14 13:26:06 +020046void
47lyd_xml_ctx_free(struct lyd_ctx *lydctx)
48{
49 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
50
51 lyd_ctx_free(lydctx);
52 lyxml_ctx_free(ctx->xmlctx);
53 free(ctx);
54}
55
Michal Vasko45791ad2021-06-17 08:45:03 +020056/**
aPiecek77a4f502024-07-03 08:34:48 +020057 * @brief Log namespace error.
58 *
59 * @param[in] xmlctx XML context
60 * @param[in] prefix XML prefix.
61 * @param[in] prefix_len XML prefix length.
62 * @param[in] attr_name Current XML attribute name.
63 * @param[in] attr_len Current XML attribute name length.
64 */
65static void
66lydxml_log_namespace_err(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len,
67 const char *attr_name, size_t attr_len)
68{
69 if (prefix_len && attr_len) {
70 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\" at attribute \"%.*s\".",
71 (int)prefix_len, prefix, (int)attr_len, attr_name);
72 } else if (prefix_len) {
73 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
74 (int)prefix_len, prefix);
75 } else {
76 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing XML namespace.");
77 }
78}
79
80/**
Michal Vasko45791ad2021-06-17 08:45:03 +020081 * @brief Parse and create XML metadata.
82 *
83 * @param[in] lydctx XML data parser context.
Michal Vaskoddd76592022-01-17 13:34:48 +010084 * @param[in] sparent Schema node of the parent.
Michal Vasko45791ad2021-06-17 08:45:03 +020085 * @param[out] meta List of created metadata instances.
86 * @return LY_ERR value.
87 */
Radek Krejcie7b95092019-05-15 11:03:07 +020088static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +010089lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020090{
aPiecek1c4da362021-04-29 14:26:34 +020091 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020092 const struct lyxml_ns *ns;
93 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010094 const char *name;
95 size_t name_len;
Michal Vasko45791ad2021-06-17 08:45:03 +020096 LY_ARRAY_COUNT_TYPE u;
97 ly_bool filter_attrs = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020098 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020099
Michal Vaskob36053d2020-03-26 15:49:30 +0100100 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +0200101
Michal Vasko7a266772024-01-23 11:02:38 +0100102 LOG_LOCSET(sparent, NULL);
Michal Vasko85be65e2023-06-13 09:44:17 +0200103
Michal Vasko45791ad2021-06-17 08:45:03 +0200104 /* check for NETCONF filter unqualified attributes */
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100105 if (!strcmp(sparent->module->name, "notifications")) {
106 /* ancient module that does not even use the extension */
107 filter_attrs = 1;
108 } else {
109 LY_ARRAY_FOR(sparent->exts, u) {
110 if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") &&
111 !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) {
112 filter_attrs = 1;
113 break;
114 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200115 }
116 }
117
Michal Vaskob36053d2020-03-26 15:49:30 +0100118 while (xmlctx->status == LYXML_ATTRIBUTE) {
119 if (!xmlctx->prefix_len) {
Michal Vasko45791ad2021-06-17 08:45:03 +0200120 /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
121 if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
122 !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
123 mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
124 if (!mod) {
125 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
126 "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
127 ret = LY_ENOTFOUND;
128 goto cleanup;
129 }
130 goto create_meta;
131 }
132
Michal Vaskoe0665742021-02-11 11:08:44 +0100133 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100134 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100135 (int)xmlctx->name_len, xmlctx->name);
aPiecekf410c2e2024-06-20 13:09:07 +0200136 /* If LYD_VALIDATE_MULTI_ERROR is set, then continue parsing, because otherwise the parser context
137 * will remain in a bad state, which will cause termination on some assert or undefined behavior.
138 */
139 LY_DPARSER_ERR_GOTO(LY_EVALID, ret = LY_EVALID, lydctx, cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200140 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100141
Michal Vasko45791ad2021-06-17 08:45:03 +0200142 /* skip attr */
Michal Vaskob36053d2020-03-26 15:49:30 +0100143 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
144 assert(xmlctx->status == LYXML_ATTR_CONTENT);
145 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200146 continue;
147 }
148
149 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200150 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200151 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +0200152 lydxml_log_namespace_err(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
Michal Vasko45791ad2021-06-17 08:45:03 +0200153 ret = LY_ENOTFOUND;
Radek Krejci28681fa2019-09-06 13:08:45 +0200154 goto cleanup;
155 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200156
157 /* get the module with metadata definition */
Michal Vasko52927e22020-03-16 17:26:14 +0100158 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200159 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100160 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100161 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200162 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100163 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
164 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200165 ret = LY_ENOTFOUND;
Michal Vaskob36053d2020-03-26 15:49:30 +0100166 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200167 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200168
169 /* skip attr */
170 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
171 assert(xmlctx->status == LYXML_ATTR_CONTENT);
172 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
173 continue;
Radek Krejci28681fa2019-09-06 13:08:45 +0200174 }
175
Michal Vasko45791ad2021-06-17 08:45:03 +0200176create_meta:
Michal Vasko60ea6352020-06-29 13:39:39 +0200177 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100178 name = xmlctx->name;
179 name_len = xmlctx->name_len;
180 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
181 assert(xmlctx->status == LYXML_ATTR_CONTENT);
182
183 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200184 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100185 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200186 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100187
188 /* next attribute */
189 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200190 }
Michal Vasko52927e22020-03-16 17:26:14 +0100191
Radek Krejcie7b95092019-05-15 11:03:07 +0200192cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +0100193 LOG_LOCBACK(1, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100194 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200195 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100196 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200197 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200198 return ret;
199}
200
Michal Vasko52927e22020-03-16 17:26:14 +0100201static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200202lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100203{
204 LY_ERR ret = LY_SUCCESS;
205 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100206 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200207 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200208 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100209 const char *name, *prefix;
210 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100211
212 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100213 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100214
Michal Vaskob36053d2020-03-26 15:49:30 +0100215 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100216 if (*attr) {
217 attr2 = *attr;
218 } else {
219 attr2 = NULL;
220 }
221
Michal Vaskob36053d2020-03-26 15:49:30 +0100222 /* remember attr prefix, name, and get its content */
223 prefix = xmlctx->prefix;
224 prefix_len = xmlctx->prefix_len;
225 name = xmlctx->name;
226 name_len = xmlctx->name_len;
227 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
228 assert(xmlctx->status == LYXML_ATTR_CONTENT);
229
Michal Vaskoe137fc42021-07-22 11:53:13 +0200230 /* handle special "xml" attribute prefix */
231 if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
232 name = prefix;
233 name_len += 1 + prefix_len;
234 prefix = NULL;
235 prefix_len = 0;
236 }
237
238 /* find namespace of the attribute, if any */
239 ns = NULL;
240 if (prefix_len) {
241 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
242 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +0200243 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, name, name_len);
Michal Vaskoe137fc42021-07-22 11:53:13 +0200244 ret = LY_EVALID;
245 goto cleanup;
246 }
247 }
248
Michal Vasko52927e22020-03-16 17:26:14 +0100249 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100250 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200251 LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100252 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100253
254 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100255 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
Michal Vaskoe5e49e92022-02-01 13:15:08 +0100256 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data,
257 LYD_HINT_DATA);
Michal Vasko52927e22020-03-16 17:26:14 +0100258 LY_CHECK_GOTO(ret, cleanup);
259
260 if (!*attr) {
261 *attr = attr2;
262 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100263
264 /* next attribute */
265 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100266 }
267
268cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100269 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200270 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100271 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100272 }
Michal Vasko52927e22020-03-16 17:26:14 +0100273 return ret;
274}
275
Michal Vasko44685da2020-03-17 15:38:06 +0100276static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100277lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100278{
Michal Vaskob36053d2020-03-26 15:49:30 +0100279 LY_ERR ret = LY_SUCCESS, r;
280 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100281 struct ly_set key_set = {0};
282 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100283 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100284
285 assert(list && (list->nodetype == LYS_LIST));
286
287 /* get all keys into a set (keys do not have if-features or anything) */
288 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100289 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200290 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200291 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100292 }
293
Michal Vasko12d809c2021-03-03 16:34:32 +0100294 /* remember parent count */
295 parents_count = xmlctx->elements.count;
296
Michal Vaskob36053d2020-03-26 15:49:30 +0100297 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100298 /* find key definition */
299 for (i = 0; i < key_set.count; ++i) {
300 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100301 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100302 break;
303 }
304 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100305 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100306
307 /* skip attributes */
308 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100309 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
310 assert(xmlctx->status == LYXML_ATTR_CONTENT);
311 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100312 }
313
Michal Vaskob36053d2020-03-26 15:49:30 +0100314 assert(xmlctx->status == LYXML_ELEM_CONTENT);
315 if (i < key_set.count) {
316 /* validate the value */
Michal Vasko583b4642023-05-25 10:39:34 +0200317 r = ly_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
Michal Vaskob36053d2020-03-26 15:49:30 +0100318 if (!r) {
319 /* key with a valid value, remove from the set */
320 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100321 }
322 }
323
Michal Vaskob36053d2020-03-26 15:49:30 +0100324 /* parser next */
325 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100326
Michal Vaskob36053d2020-03-26 15:49:30 +0100327 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100328 while (xmlctx->status == LYXML_ELEMENT) {
329 while (parents_count < xmlctx->elements.count) {
330 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
331 }
332 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100333 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
334 }
335
336 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
337 assert(xmlctx->status == LYXML_ELEM_CLOSE);
338 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
339 if (next != LYXML_ELEM_CLOSE) {
340 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
341 }
Michal Vasko44685da2020-03-17 15:38:06 +0100342 }
343
344 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100345 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100346 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100347 }
348
349cleanup:
350 ly_set_erase(&key_set, NULL);
351 return ret;
352}
353
Michal Vasko5c24ed12021-06-09 09:27:32 +0200354/**
355 * @brief Skip an element with all its descendants.
356 *
357 * @param[in] xmlctx XML parser context.
358 * @return LY_ERR value.
359 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100360static LY_ERR
361lydxml_data_skip(struct lyxml_ctx *xmlctx)
362{
363 uint32_t parents_count;
364
365 /* remember current number of parents */
366 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200367 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100368
369 /* skip after the content */
370 while (xmlctx->status != LYXML_ELEM_CONTENT) {
371 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
372 }
373 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
374
375 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200376 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100377 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
378 }
379
380 /* close element */
381 assert(xmlctx->status == LYXML_ELEM_CLOSE);
382 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
383
384 return LY_SUCCESS;
385}
386
Michal Vasko5c24ed12021-06-09 09:27:32 +0200387/**
388 * @brief Check that the current element can be parsed as a data node.
389 *
390 * @param[in] lydctx XML data parser context.
391 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
392 * @return LY_ERR value.
393 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100394static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200395lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100396{
Michal Vasko8a8fde02024-10-21 15:17:46 +0200397 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200398 struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
Michal Vasko8a8fde02024-10-21 15:17:46 +0200399 uint32_t *prev_lo, temp_lo = 0;
Michal Vasko1bf09392020-03-27 12:38:10 +0100400
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100401 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
402 /* only checks specific to opaque nodes */
403 return LY_SUCCESS;
404 }
405
Michal Vasko13854662021-06-09 09:27:50 +0200406 if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
407 /* nothing to check */
408 return LY_SUCCESS;
409 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100410
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200411 assert(xmlctx->elements.count);
412
Michal Vasko13854662021-06-09 09:27:50 +0200413 /* backup parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200414 LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
Michal Vasko1bf09392020-03-27 12:38:10 +0100415
Michal Vasko13854662021-06-09 09:27:50 +0200416 /* skip attributes */
417 while (xmlctx->status == LYXML_ATTRIBUTE) {
418 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
419 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
420 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100421
Michal Vasko13854662021-06-09 09:27:50 +0200422 if ((*snode)->nodetype & LYD_NODE_TERM) {
423 /* value may not be valid in which case we parse it as an opaque node */
Michal Vasko8a8fde02024-10-21 15:17:46 +0200424 prev_lo = ly_temp_log_options(&temp_lo);
425 r = ly_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
426 ly_temp_log_options(prev_lo);
427 if (r) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200428 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, (int)xmlctx->value_len,
Michal Vaskod0237d42021-07-12 14:49:46 +0200429 xmlctx->value);
Michal Vasko13854662021-06-09 09:27:50 +0200430 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100431 }
Michal Vasko13854662021-06-09 09:27:50 +0200432 } else if ((*snode)->nodetype == LYS_LIST) {
433 /* skip content */
434 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
Michal Vasko1bf09392020-03-27 12:38:10 +0100435
Michal Vasko13854662021-06-09 09:27:50 +0200436 if (lydxml_check_list(xmlctx, *snode)) {
437 /* invalid list, parse as opaque if it missing/has invalid some keys */
Michal Vaskod0237d42021-07-12 14:49:46 +0200438 LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
Michal Vasko13854662021-06-09 09:27:50 +0200439 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100440 }
Michal Vasko13854662021-06-09 09:27:50 +0200441 } else {
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100442 /* if there is a non-WS value, it cannot be parsed as an inner node */
443 assert(xmlctx->status == LYXML_ELEM_CONTENT);
444 if (!xmlctx->ws_only) {
445 *snode = NULL;
446 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100447 }
448
Michal Vasko13854662021-06-09 09:27:50 +0200449restore:
450 /* restore parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200451 lyxml_ctx_restore(xmlctx, &pxmlctx);
Michal Vasko1bf09392020-03-27 12:38:10 +0100452 return ret;
453}
454
Radek Krejcie7b95092019-05-15 11:03:07 +0200455/**
Michal Vaskocea58712022-04-01 14:37:08 +0200456 * @brief Get sensible data hints for an opaque node.
457 *
458 * @param[in] name Node name.
459 * @param[in] name_len Length of @p name.
460 * @param[in] value Node value.
461 * @param[in] value_len Length of @p value.
462 * @param[in] first Node first sibling.
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100463 * @param[in] ns Node module namespace, NULL for no namespace.
Michal Vaskocea58712022-04-01 14:37:08 +0200464 * @param[out] hints Data hints to use.
465 * @param[out] anchor Anchor to insert after in case of a list.
466 */
467static void
Michal Vaskod027f382023-02-10 09:13:25 +0100468lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, const struct lyd_node *first,
Michal Vaskocea58712022-04-01 14:37:08 +0200469 const char *ns, uint32_t *hints, struct lyd_node **anchor)
470{
471 struct lyd_node_opaq *opaq;
472 char *ptr;
Jan Kundrát863b18b2024-07-06 20:55:23 +0200473 /* this needs to be at least 64bit, and it "should not" be an explicit int64_t
474 * because the code calls strtoll later on, which "might" return a bigger type */
475 long long num;
Michal Vaskocea58712022-04-01 14:37:08 +0200476
477 *hints = 0;
478 *anchor = NULL;
479
480 if (!value_len) {
Michal Vasko6235e152023-08-07 13:39:13 +0200481 /* no value but it may also be zero-length string */
482 *hints |= LYD_VALHINT_EMPTY | LYD_VALHINT_STRING;
Michal Vaskocea58712022-04-01 14:37:08 +0200483 } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
484 /* boolean value */
485 *hints |= LYD_VALHINT_BOOLEAN;
486 } else {
Jan Kundrát863b18b2024-07-06 20:55:23 +0200487 num = strtoll(value, &ptr, 10);
Michal Vaskocea58712022-04-01 14:37:08 +0200488 if ((unsigned)(ptr - value) == value_len) {
489 /* number value */
490 *hints |= LYD_VALHINT_DECNUM;
Jan Kundrát863b18b2024-07-06 20:55:23 +0200491 if ((num < INT32_MIN) || (num > UINT32_MAX)) {
Michal Vaskocea58712022-04-01 14:37:08 +0200492 /* large number */
493 *hints |= LYD_VALHINT_NUM64;
494 }
495 } else {
496 /* string value */
497 *hints |= LYD_VALHINT_STRING;
498 }
499 }
500
501 if (!first) {
502 return;
503 }
504
505 /* search backwards to find the last instance */
506 do {
507 first = first->prev;
508 if (first->schema) {
509 continue;
510 }
511
512 opaq = (struct lyd_node_opaq *)first;
513 assert(opaq->format == LY_VALUE_XML);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100514 if (!ly_strncmp(opaq->name.name, name, name_len) &&
zhangtaogb348a8b2024-02-21 15:43:30 +0800515 ((ns && opaq->name.module_ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) {
Michal Vaskocea58712022-04-01 14:37:08 +0200516 if (opaq->value && opaq->value[0]) {
517 /* leaf-list nodes */
518 opaq->hints |= LYD_NODEHINT_LEAFLIST;
519 *hints |= LYD_NODEHINT_LEAFLIST;
520 } else {
521 /* list nodes */
522 opaq->hints |= LYD_NODEHINT_LIST;
523 *hints |= LYD_NODEHINT_LIST;
524 }
Michal Vaskod027f382023-02-10 09:13:25 +0100525 *anchor = (struct lyd_node *)first;
Michal Vaskocea58712022-04-01 14:37:08 +0200526 break;
527 }
528 } while (first->prev->next);
529}
530
531/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200532 * @brief Get schema node for the current element.
Michal Vaskoddd76592022-01-17 13:34:48 +0100533 *
534 * @param[in] lydctx XML data parser context.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200535 * @param[in] parent Parsed parent data node, if any.
536 * @param[in] prefix Element prefix, if any.
537 * @param[in] prefix_len Length of @p prefix.
538 * @param[in] name Element name.
539 * @param[in] name_len Length of @p name.
540 * @param[out] snode Found schema node, NULL if no suitable was found.
541 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vaskoddd76592022-01-17 13:34:48 +0100542 * @return LY_SUCCESS on success;
Michal Vaskoddd76592022-01-17 13:34:48 +0100543 * @return LY_ERR on error.
544 */
545static LY_ERR
Michal Vaskod027f382023-02-10 09:13:25 +0100546lydxml_subtree_get_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200547 const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100548{
549 LY_ERR r;
Michal Vaskob36053d2020-03-26 15:49:30 +0100550 struct lyxml_ctx *xmlctx;
551 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200552 const struct lyxml_ns *ns;
Radek Krejcie7b95092019-05-15 11:03:07 +0200553 struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200554 uint32_t getnext_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +0100555
Michal Vaskob36053d2020-03-26 15:49:30 +0100556 xmlctx = lydctx->xmlctx;
557 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100558 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100559
Michal Vasko8cc3f662022-03-29 11:25:51 +0200560 *snode = NULL;
561 *ext = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100562
Michal Vasko8cc3f662022-03-29 11:25:51 +0200563 /* get current namespace */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200564 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200565 if (!ns) {
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100566 if (lydctx->int_opts & LYD_INTOPT_ANY) {
567 goto unknown_module;
568 }
aPiecek77a4f502024-07-03 08:34:48 +0200569 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200570 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200571 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200572
573 /* get the element module, use parent context if possible because of extensions */
574 mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200575 if (!mod) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100576 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200577 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
578 snode, ext);
579 if (r != LY_ENOT) {
580 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100581 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100582 }
583
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100584unknown_module:
Michal Vaskoe0665742021-02-11 11:08:44 +0100585 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200586 if (ns) {
587 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
588 } else if (prefix_len) {
589 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%.*s\" in the context.", (int)prefix_len, prefix);
590 } else {
591 LOGVAL(ctx, LYVE_REFERENCE, "No default namespace in the context.");
592 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200593 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200594 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200595 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200596 }
597
Michal Vaskoa5da3292020-08-12 13:10:50 +0200598 /* get the schema node */
Michal Vaskoa878a892023-08-18 12:22:07 +0200599 if (!parent && lydctx->ext) {
600 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
601 } else {
602 /* try to find parent schema node even if it is an opaque node (not connected to the parent) */
603 *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts);
604 }
605 if (!*snode) {
606 /* check for extension data */
607 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
608 name_len, snode, ext);
609 if (r != LY_ENOT) {
610 /* success or error */
611 return r;
Radek Krejcif16e2542021-02-17 15:39:23 +0100612 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100613
Michal Vaskoa878a892023-08-18 12:22:07 +0200614 /* unknown data node */
615 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
616 if (parent) {
617 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
618 (int)name_len, name, LYD_NAME(parent));
619 } else if (lydctx->ext) {
620 if (lydctx->ext->argument) {
621 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
622 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100623 } else {
Michal Vaskoa878a892023-08-18 12:22:07 +0200624 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
625 (int)name_len, name, lydctx->ext->def->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100626 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200627 } else {
628 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
629 (int)name_len, name, mod->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200630 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200631 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200632 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200633 return LY_SUCCESS;
634 } else {
635 /* check that schema node is valid and can be used */
636 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
637 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200638 }
639
Michal Vasko8cc3f662022-03-29 11:25:51 +0200640 return LY_SUCCESS;
641}
642
643/**
Michal Vaskod027f382023-02-10 09:13:25 +0100644 * @brief Parse an XML opque node.
645 *
646 * @param[in] lydctx XML YANG data parser context.
647 * @param[in] sibling Existing sibling node, if any.
648 * @param[in] prefix Parsed node prefix.
649 * @param[in] prefix_len Length of @p prefix.
650 * @param[in] name Parsed node name.
651 * @param[in] name_len Length of @p name.
652 * @param[out] insert_anchor Optional anchor node for inserting this node.
653 * @param[out] node Created node.
654 * @return LY_ERR value.
655 */
656static LY_ERR
657lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
658 const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
659{
660 LY_ERR rc = LY_SUCCESS;
661 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200662 struct lyd_node_opaq *opaq;
663 const char *ns_uri, *value = NULL;
664 size_t value_len;
665 ly_bool ws_only, dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100666 const struct lyxml_ns *ns;
667 uint32_t hints;
668 void *val_prefix_data = NULL;
669 LY_VALUE_FORMAT format;
670
671 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
672
673 *node = NULL;
674
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200675 /* remember the value */
676 value = xmlctx->value;
677 value_len = xmlctx->value_len;
678 ws_only = xmlctx->ws_only;
679 dynamic = xmlctx->dynamic;
680 if (dynamic) {
Michal Vaskod027f382023-02-10 09:13:25 +0100681 xmlctx->dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100682 }
683
Michal Vasko535d21c2023-08-09 10:41:44 +0200684 /* get value prefixes, if any */
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200685 rc = ly_store_prefix_data(xmlctx->ctx, value, value_len, LY_VALUE_XML, &xmlctx->ns, &format, &val_prefix_data);
Michal Vasko535d21c2023-08-09 10:41:44 +0200686 LY_CHECK_GOTO(rc, cleanup);
687
Michal Vaskod027f382023-02-10 09:13:25 +0100688 /* get NS again, it may have been backed up and restored */
689 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
690 ns_uri = ns ? ns->uri : NULL;
691
692 /* get best-effort node hints */
693 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
694
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200695 /* create the node without value */
696 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0,
697 NULL, format, NULL, hints, node);
Michal Vaskod027f382023-02-10 09:13:25 +0100698 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100699
Michal Vaskoa878a892023-08-18 12:22:07 +0200700 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100701 LOG_LOCSET(NULL, *node);
Michal Vaskoa878a892023-08-18 12:22:07 +0200702
Michal Vaskod027f382023-02-10 09:13:25 +0100703 /* parser next */
704 rc = lyxml_ctx_next(xmlctx);
705 LY_CHECK_GOTO(rc, cleanup);
706
707 /* process children */
708 while (xmlctx->status == LYXML_ELEMENT) {
709 rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
710 LY_CHECK_GOTO(rc, cleanup);
711 }
712
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200713 /* update the value */
714 opaq = (struct lyd_node_opaq *)*node;
715 if (opaq->child) {
716 if (!ws_only) {
717 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq));
718 rc = LY_EVALID;
719 goto cleanup;
720 }
721 } else if (value_len) {
722 lydict_remove(xmlctx->ctx, opaq->value);
723 if (dynamic) {
724 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
725 dynamic = 0;
726 } else {
727 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
728 }
729 }
730
731 /* always store val_prefix_data because the format requires them */
732 assert(!opaq->val_prefix_data);
733 opaq->val_prefix_data = val_prefix_data;
734 val_prefix_data = NULL;
735
Michal Vaskod027f382023-02-10 09:13:25 +0100736cleanup:
Michal Vaskoa878a892023-08-18 12:22:07 +0200737 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100738 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +0200739 }
Michal Vaskod027f382023-02-10 09:13:25 +0100740 ly_free_prefix_data(format, val_prefix_data);
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200741 if (dynamic) {
742 free((char *)value);
743 }
Michal Vaskod027f382023-02-10 09:13:25 +0100744 if (rc) {
745 lyd_free_tree(*node);
746 *node = NULL;
747 }
748 return rc;
749}
750
751/**
752 * @brief Parse an XML leaf/leaf-list node.
753 *
754 * @param[in] lydctx XML YANG data parser context.
755 * @param[in] parent Parent node, if any.
756 * @param[in] snode Schema node of the new node.
757 * @param[out] node Created node.
758 * @return LY_ERR value.
759 */
760static LY_ERR
761lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
762 struct lyd_node **node)
763{
764 LY_ERR r, rc = LY_SUCCESS;
765 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
766 struct lyd_node *anchor;
767
768 *node = NULL;
769
770 /* create node */
771 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
772 LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
773 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
774
775 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100776 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100777 }
778
Michal Vasko58a1a702023-03-01 13:45:38 +0100779 if (*node && parent && (snode->flags & LYS_KEY)) {
Michal Vaskod027f382023-02-10 09:13:25 +0100780 /* check the key order, the anchor must never be a key */
781 anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
782 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
783 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
784 LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
785 r = LY_EVALID;
786 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
787 } else {
788 LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
789 }
790 }
791 }
792
793 /* parser next */
794 r = lyxml_ctx_next(xmlctx);
795 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
796
797 /* no children expected */
798 if (xmlctx->status == LYXML_ELEMENT) {
799 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
800 (int)xmlctx->name_len, xmlctx->name, snode->name);
801 r = LY_EVALID;
802 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
803 }
804
805cleanup:
806 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100807 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100808 }
809 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
810 lyd_free_tree(*node);
811 *node = NULL;
812 }
813 return rc;
814}
815
816/**
817 * @brief Parse an XML inner node.
818 *
819 * @param[in] lydctx XML YANG data parser context.
820 * @param[in] snode Schema node of the new node.
821 * @param[in] ext Extension instance of @p snode, if any.
822 * @param[out] node Created node.
823 * @return LY_ERR value.
824 */
825static LY_ERR
826lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
827 struct lyd_node **node)
828{
829 LY_ERR r, rc = LY_SUCCESS;
830 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
831 uint32_t prev_parse_opts = lydctx->parse_opts;
832
833 *node = NULL;
834
835 if (!xmlctx->ws_only) {
836 /* value in inner node */
837 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
838 (int)xmlctx->value_len, xmlctx->value, snode->name);
839 r = LY_EVALID;
840 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
841 }
842
843 /* create node */
844 rc = lyd_create_inner(snode, node);
845 LY_CHECK_GOTO(rc, cleanup);
846
Michal Vasko58a1a702023-03-01 13:45:38 +0100847 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100848 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100849
850 /* parser next */
851 rc = lyxml_ctx_next(xmlctx);
852 LY_CHECK_GOTO(rc, cleanup);
853
854 if (ext) {
855 /* only parse these extension data and validate afterwards */
856 lydctx->parse_opts |= LYD_PARSE_ONLY;
857 }
858
859 /* process children */
860 while (xmlctx->status == LYXML_ELEMENT) {
861 r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
862 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
863 }
864
865 /* restore options */
866 lydctx->parse_opts = prev_parse_opts;
867
868 if (snode->nodetype == LYS_LIST) {
869 /* check all keys exist */
Michal Vasko0af83702024-10-08 13:42:19 +0200870 r = lyd_parser_check_keys(*node);
Michal Vasko202d8162023-03-01 14:42:19 +0100871 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100872 }
873
Michal Vasko40015732023-11-20 13:48:15 +0100874 if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) {
875 /* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +0100876 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
877 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
878
879 /* add any missing default children */
880 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
881 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
882 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
883 }
884
885 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
886 /* rememeber the RPC/action/notification */
887 lydctx->op_node = *node;
888 }
889
890cleanup:
891 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100892 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100893 }
894 lydctx->parse_opts = prev_parse_opts;
Michal Vasko202d8162023-03-01 14:42:19 +0100895 if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
896 /* list without keys is unusable or an error */
Michal Vaskod027f382023-02-10 09:13:25 +0100897 lyd_free_tree(*node);
898 *node = NULL;
899 }
900 return rc;
901}
902
903/**
904 * @brief Parse an XML anyxml/anydata node.
905 *
906 * @param[in] lydctx XML YANG data parser context.
907 * @param[in] snode Schema node of the new node.
908 * @param[in] ext Extension instance of @p snode, if any.
909 * @param[out] node Created node.
910 * @return LY_ERR value.
911 */
912static LY_ERR
913lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
914 struct lyd_node **node)
915{
916 LY_ERR r, rc = LY_SUCCESS;
917 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
918 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
919 struct lyd_node *child = NULL;
920 char *val = NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +0200921 ly_bool log_node = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100922
923 *node = NULL;
924
925 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
926 /* value in anydata node, we expect a tree */
927 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200928 xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskod027f382023-02-10 09:13:25 +0100929 r = LY_EVALID;
930 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
931 }
932
933 if (!xmlctx->ws_only) {
934 /* use an arbitrary text value for anyxml */
935 val = strndup(xmlctx->value, xmlctx->value_len);
936 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
937
938 /* parser next */
939 r = lyxml_ctx_next(xmlctx);
940 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
941
942 /* create node */
943 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
944 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
945 val = NULL;
946 } else {
Michal Vasko85be65e2023-06-13 09:44:17 +0200947 /* create node */
948 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
949 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
950
951 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100952 LOG_LOCSET(NULL, *node);
Michal Vasko85be65e2023-06-13 09:44:17 +0200953 log_node = 1;
954
Michal Vaskod027f382023-02-10 09:13:25 +0100955 /* parser next */
956 r = lyxml_ctx_next(xmlctx);
957 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
958
959 /* update options so that generic data can be parsed */
960 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
961 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
962 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
963
964 /* parse any data tree */
965 while (xmlctx->status == LYXML_ELEMENT) {
966 r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
967 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
968 }
969
Michal Vasko85be65e2023-06-13 09:44:17 +0200970 /* assign the data tree */
971 ((struct lyd_node_any *)*node)->value.tree = child;
Michal Vaskod027f382023-02-10 09:13:25 +0100972 child = NULL;
973 }
974
975cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +0200976 if (log_node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100977 LOG_LOCBACK(0, 1);
Michal Vasko85be65e2023-06-13 09:44:17 +0200978 }
Michal Vaskod027f382023-02-10 09:13:25 +0100979 lydctx->parse_opts = prev_parse_opts;
980 lydctx->int_opts = prev_int_opts;
981 free(val);
982 lyd_free_tree(child);
983 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
984 lyd_free_tree(*node);
985 *node = NULL;
986 }
987 return rc;
988}
989
990/**
991 * @brief Parse an XML subtree, recursively.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200992 *
993 * @param[in] lydctx XML YANG data parser context.
994 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
Michal Vaskod027f382023-02-10 09:13:25 +0100995 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200996 * @param[in,out] parsed Optional set to add all the parsed siblings into.
997 * @return LY_ERR value.
998 */
999static LY_ERR
1000lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1001{
Michal Vaskod027f382023-02-10 09:13:25 +01001002 LY_ERR r, rc = LY_SUCCESS;
1003 const char *prefix, *name;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001004 size_t prefix_len, name_len;
1005 struct lyxml_ctx *xmlctx;
1006 const struct ly_ctx *ctx;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001007 struct lyd_meta *meta = NULL;
1008 struct lyd_attr *attr = NULL;
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001009 const struct lysc_node *snode = NULL;
1010 struct lysc_ext_instance *ext = NULL;
Michal Vaskod027f382023-02-10 09:13:25 +01001011 uint32_t orig_parse_opts;
1012 struct lyd_node *node = NULL, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001013 ly_bool parse_subtree;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001014
1015 assert(parent || first_p);
1016
1017 xmlctx = lydctx->xmlctx;
1018 ctx = xmlctx->ctx;
1019
1020 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1021 /* all descendants should be parsed */
1022 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1023 orig_parse_opts = lydctx->parse_opts;
1024
1025 assert(xmlctx->status == LYXML_ELEMENT);
1026
1027 /* remember element prefix and name */
1028 prefix = xmlctx->prefix;
1029 prefix_len = xmlctx->prefix_len;
1030 name = xmlctx->name;
1031 name_len = xmlctx->name_len;
1032
1033 /* parser next */
Michal Vaskod027f382023-02-10 09:13:25 +01001034 rc = lyxml_ctx_next(xmlctx);
1035 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001036
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001037 if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && name_len && !prefix_len &&
1038 !ly_strncmp("eventTime", name, name_len)) {
1039 /* parse eventTime, create node */
1040 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1041 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len,
1042 "urn:ietf:params:xml:ns:netconf:notification:1.0", 47, xmlctx->value,
1043 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA, &node);
1044 LY_CHECK_GOTO(rc, cleanup);
1045
1046 /* validate the value */
1047 r = lyd_parser_notif_eventtime_validate(node);
1048 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1049
1050 /* parser next */
1051 r = lyxml_ctx_next(xmlctx);
1052 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1053 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1054 LOGVAL(ctx, LYVE_DATA, "Unexpected notification \"eventTime\" node children.");
1055 rc = LY_EVALID;
1056 lyd_free_tree(node);
1057 goto cleanup;
1058 }
1059
1060 goto node_parsed;
1061 }
1062
Michal Vasko8cc3f662022-03-29 11:25:51 +02001063 /* get the schema node */
Michal Vasko202d8162023-03-01 14:42:19 +01001064 r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
1065 if (r) {
1066 rc = r;
1067 if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1068 /* skip the invalid data */
1069 if ((r = lydxml_data_skip(xmlctx))) {
1070 rc = r;
1071 }
1072 }
1073 goto cleanup;
1074 } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001075 LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001076
1077 /* skip element with children */
Michal Vaskod027f382023-02-10 09:13:25 +01001078 rc = lydxml_data_skip(xmlctx);
1079 goto cleanup;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001080 }
1081
Michal Vaskoa5da3292020-08-12 13:10:50 +02001082 /* create metadata/attributes */
1083 if (xmlctx->status == LYXML_ATTRIBUTE) {
1084 if (snode) {
aPiecekf410c2e2024-06-20 13:09:07 +02001085 r = lydxml_metadata(lydctx, snode, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001086 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001087 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
aPiecekf410c2e2024-06-20 13:09:07 +02001088 r = lydxml_attrs(xmlctx, &attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001089 }
1090 }
aPiecekf410c2e2024-06-20 13:09:07 +02001091 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001092
1093 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1094 if (!snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001095 /* opaque */
1096 r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
1097 &insert_anchor, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001098 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskod027f382023-02-10 09:13:25 +01001099 /* term */
1100 r = lydxml_subtree_term(lydctx, parent, snode, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001101 } else if (snode->nodetype & LYD_NODE_INNER) {
Michal Vaskod027f382023-02-10 09:13:25 +01001102 /* inner */
1103 r = lydxml_subtree_inner(lydctx, snode, ext, &node);
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001104 } else {
Michal Vaskod027f382023-02-10 09:13:25 +01001105 /* any */
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001106 assert(snode->nodetype & LYD_NODE_ANY);
Michal Vaskod027f382023-02-10 09:13:25 +01001107 r = lydxml_subtree_any(lydctx, snode, ext, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001108 }
Michal Vaskod027f382023-02-10 09:13:25 +01001109 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko1524f1a2023-03-01 09:36:34 +01001110
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001111node_parsed:
Michal Vasko58a1a702023-03-01 13:45:38 +01001112 if (node && snode) {
Michal Vasko135719f2022-08-25 12:18:17 +02001113 /* add/correct flags */
Michal Vasko0af83702024-10-08 13:42:19 +02001114 r = lyd_parser_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko567d7032023-02-13 08:53:31 +01001115 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001116
Michal Vaskoeba23112022-08-26 08:35:41 +02001117 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1118 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001119 r = lyd_validate_node_ext(node, &lydctx->ext_node);
1120 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001121 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001122 }
1123
1124 /* parser next */
1125 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +01001126 if (!parse_subtree) {
Michal Vaskod027f382023-02-10 09:13:25 +01001127 r = lyxml_ctx_next(xmlctx);
Michal Vasko567d7032023-02-13 08:53:31 +01001128 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001129 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001130
Michal Vasko58a1a702023-03-01 13:45:38 +01001131 LY_CHECK_GOTO(!node, cleanup);
1132
Michal Vaskoa5da3292020-08-12 13:10:50 +02001133 /* add metadata/attributes */
1134 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +01001135 lyd_insert_meta(node, meta, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001136 meta = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001137 } else {
1138 lyd_insert_attr(node, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001139 attr = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001140 }
1141
1142 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +02001143 if (insert_anchor) {
1144 lyd_insert_after(insert_anchor, node);
1145 } else if (ext) {
Michal Vaskod027f382023-02-10 09:13:25 +01001146 r = lyplg_ext_insert(parent, node);
Michal Vasko567d7032023-02-13 08:53:31 +01001147 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001148 } else {
aPiecek1462ab12024-02-07 09:13:29 +01001149 lyd_insert_node(parent, first_p, node,
1150 lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001151 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001152 while (!parent && (*first_p)->prev->next) {
1153 *first_p = (*first_p)->prev;
1154 }
1155
Michal Vaskoe0665742021-02-11 11:08:44 +01001156 /* rememeber a successfully parsed node */
1157 if (parsed) {
1158 ly_set_add(parsed, node, 1, NULL);
1159 }
1160
Michal Vaskod027f382023-02-10 09:13:25 +01001161cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +02001162 lydctx->parse_opts = orig_parse_opts;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001163 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001164 lyd_free_attr_siblings(ctx, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001165 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +02001166}
1167
Michal Vaskoe0665742021-02-11 11:08:44 +01001168/**
1169 * @brief Parse a specific XML element into an opaque node.
1170 *
1171 * @param[in] xmlctx XML parser context.
1172 * @param[in] name Name of the element.
1173 * @param[in] uri URI of the element.
1174 * @param[in] value Whether a value is expected in the element.
1175 * @param[out] evnp Parsed envelope (opaque node).
1176 * @return LY_SUCCESS on success.
1177 * @return LY_ENOT if the specified element did not match.
1178 * @return LY_ERR value on error.
1179 */
Michal Vasko1bf09392020-03-27 12:38:10 +01001180static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001181lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, ly_bool value, struct lyd_node **envp)
Michal Vasko1bf09392020-03-27 12:38:10 +01001182{
Michal Vaskoe0665742021-02-11 11:08:44 +01001183 LY_ERR rc = LY_SUCCESS;
1184 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +02001185 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +01001186 const char *prefix;
1187 size_t prefix_len;
1188
Michal Vaskof048ba52023-06-13 10:40:43 +02001189 if (xmlctx->status != LYXML_ELEMENT) {
1190 /* nothing to parse */
1191 return LY_ENOT;
1192 }
1193
Michal Vasko1bf09392020-03-27 12:38:10 +01001194 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
1195 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +01001196 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001197 }
1198
1199 prefix = xmlctx->prefix;
1200 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001201 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001202 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +02001203 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001204 return LY_EVALID;
1205 } else if (strcmp(ns->uri, uri)) {
1206 /* different namespace */
1207 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001208 }
1209
1210 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
1211
1212 /* create attributes */
1213 if (xmlctx->status == LYXML_ATTRIBUTE) {
1214 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1215 }
1216
Michal Vaskoe0665742021-02-11 11:08:44 +01001217 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1218 if (!value && !xmlctx->ws_only) {
1219 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +01001220 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001221 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001222 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001223 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001224
1225 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001226 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001227 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001228 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001229
1230 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001231 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001232 attr = NULL;
1233
Michal Vaskoe0665742021-02-11 11:08:44 +01001234 /* parser next element */
1235 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001236
Michal Vaskoe0665742021-02-11 11:08:44 +01001237cleanup:
1238 lyd_free_attr_siblings(xmlctx->ctx, attr);
1239 if (rc) {
1240 lyd_free_tree(*envp);
1241 *envp = NULL;
1242 }
1243 return rc;
1244}
1245
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001246LY_ERR
1247lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1248 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
1249 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
1250{
Michal Vaskod027f382023-02-10 09:13:25 +01001251 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001252 struct lyd_xml_ctx *lydctx;
Michal Vasko64592692023-06-12 13:50:11 +02001253 ly_bool parsed_data_nodes = 0, close_elem = 0;
1254 struct lyd_node *act = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001255 enum LYXML_PARSER_STATUS status;
1256
1257 assert(ctx && in && lydctx_p);
1258 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1259 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1260
1261 /* init context */
1262 lydctx = calloc(1, sizeof *lydctx);
1263 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1264 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1265 lydctx->parse_opts = parse_opts;
1266 lydctx->val_opts = val_opts;
1267 lydctx->int_opts = int_opts;
1268 lydctx->free = lyd_xml_ctx_free;
1269 lydctx->ext = ext;
1270
1271 /* find the operation node if it exists already */
1272 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1273
Michal Vasko64592692023-06-12 13:50:11 +02001274 if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) {
1275 /* can be either, try to parse "action" */
1276 if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) {
1277 close_elem = 1;
1278 int_opts &= ~LYD_INTOPT_RPC;
1279 }
1280 }
1281
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001282 /* parse XML data */
1283 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
Michal Vaskod027f382023-02-10 09:13:25 +01001284 r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
1285 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1286
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001287 parsed_data_nodes = 1;
1288
1289 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1290 break;
1291 }
1292 }
1293
Michal Vasko64592692023-06-12 13:50:11 +02001294 /* close an opened element */
1295 if (close_elem) {
1296 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1297 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
1298 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1299 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
1300 rc = LY_EVALID;
1301 goto cleanup;
1302 }
1303
1304 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
1305 }
1306
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001307 /* check final state */
1308 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1309 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001310 r = LY_EVALID;
1311 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001312 }
1313 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1314 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001315 r = LY_EVALID;
1316 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001317 }
1318
1319 if (!parsed_data_nodes) {
1320 /* no data nodes were parsed */
1321 lydctx->op_node = NULL;
1322 }
1323
1324 if (parse_opts & LYD_PARSE_SUBTREE) {
1325 /* check for a sibling element */
1326 assert(subtree_sibling);
1327 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1328 *subtree_sibling = 1;
1329 } else {
1330 *subtree_sibling = 0;
1331 }
1332 }
1333
1334cleanup:
1335 /* there should be no unres stored if validation should be skipped */
1336 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1337 !lydctx->node_when.count));
1338
Michal Vasko64592692023-06-12 13:50:11 +02001339 lyd_free_tree(act);
Michal Vaskod027f382023-02-10 09:13:25 +01001340 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001341 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1342 } else {
1343 *lydctx_p = (struct lyd_ctx *)lydctx;
1344
1345 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1346 lyxml_ctx_free(lydctx->xmlctx);
1347 lydctx->xmlctx = NULL;
1348 }
1349 return rc;
1350}
1351
Michal Vaskoe0665742021-02-11 11:08:44 +01001352/**
1353 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1354 *
1355 * @param[in] xmlctx XML parser context.
1356 * @param[out] evnp Parsed envelope(s) (opaque node).
1357 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1358 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1359 * @return LY_SUCCESS on success.
1360 * @return LY_ERR value on error.
1361 */
1362static LY_ERR
1363lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1364{
1365 LY_ERR rc = LY_SUCCESS, r;
1366 struct lyd_node *child;
1367
1368 assert(envp && !*envp);
1369
1370 /* parse "rpc" */
1371 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001372 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1373
1374 /* parse "action", if any */
1375 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1376 if (r == LY_SUCCESS) {
1377 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001378 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001379
1380 /* NETCONF action */
1381 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1382 *close_elem = 2;
1383 } else if (r == LY_ENOT) {
1384 /* NETCONF RPC */
1385 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1386 *close_elem = 1;
1387 } else {
1388 rc = r;
1389 goto cleanup;
1390 }
1391
1392cleanup:
1393 if (rc) {
1394 lyd_free_tree(*envp);
1395 *envp = NULL;
1396 }
1397 return rc;
1398}
1399
1400/**
Michal Vaskoe0665742021-02-11 11:08:44 +01001401 * @brief Parse an XML element as an opaque node subtree.
1402 *
1403 * @param[in] xmlctx XML parser context.
1404 * @param[in] parent Parent to append nodes to.
1405 * @return LY_ERR value.
1406 */
1407static LY_ERR
1408lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001409{
Michal Vaskoe0665742021-02-11 11:08:44 +01001410 LY_ERR rc = LY_SUCCESS;
1411 const struct lyxml_ns *ns;
1412 struct lyd_attr *attr = NULL;
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001413 struct lyd_node *node = NULL;
1414 struct lyd_node_opaq *opaq;
1415 const char *name, *prefix, *value = NULL;
1416 size_t name_len, prefix_len, value_len;
1417 ly_bool ws_only, dynamic = 0;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001418
Michal Vaskoe0665742021-02-11 11:08:44 +01001419 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001420
Michal Vaskoe0665742021-02-11 11:08:44 +01001421 name = xmlctx->name;
1422 name_len = xmlctx->name_len;
1423 prefix = xmlctx->prefix;
1424 prefix_len = xmlctx->prefix_len;
1425 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1426 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +02001427 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001428 return LY_EVALID;
1429 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001430
Michal Vaskoe0665742021-02-11 11:08:44 +01001431 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001432
Michal Vaskoe0665742021-02-11 11:08:44 +01001433 /* create attributes */
1434 if (xmlctx->status == LYXML_ATTRIBUTE) {
1435 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1436 }
1437
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001438 /* remember the value */
Michal Vaskoe0665742021-02-11 11:08:44 +01001439 assert(xmlctx->status == LYXML_ELEM_CONTENT);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001440 value = xmlctx->value;
1441 value_len = xmlctx->value_len;
1442 ws_only = xmlctx->ws_only;
1443 dynamic = xmlctx->dynamic;
1444 if (dynamic) {
1445 xmlctx->dynamic = 0;
1446 }
1447
1448 /* create the node without value */
1449 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), NULL, 0, NULL,
1450 LY_VALUE_XML, NULL, 0, &node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001451 LY_CHECK_GOTO(rc, cleanup);
1452
1453 /* assign atributes */
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001454 ((struct lyd_node_opaq *)node)->attr = attr;
Michal Vaskoe0665742021-02-11 11:08:44 +01001455 attr = NULL;
1456
1457 /* parser next element */
1458 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1459
1460 /* parse all the descendants */
1461 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001462 rc = lydxml_opaq_r(xmlctx, node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001463 LY_CHECK_GOTO(rc, cleanup);
1464 }
1465
1466 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001467 lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_LAST);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001468
1469 /* update the value */
1470 opaq = (struct lyd_node_opaq *)node;
1471 if (opaq->child) {
1472 if (!ws_only) {
1473 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(node));
1474 rc = LY_EVALID;
1475 goto cleanup;
1476 }
1477 } else if (value_len) {
1478 lydict_remove(xmlctx->ctx, opaq->value);
1479 if (dynamic) {
1480 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
1481 dynamic = 0;
1482 } else {
1483 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
1484 }
1485 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001486
1487cleanup:
1488 lyd_free_attr_siblings(xmlctx->ctx, attr);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001489 if (dynamic) {
1490 free((char *)value);
1491 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001492 if (rc) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001493 lyd_free_tree(node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001494 }
1495 return rc;
1496}
1497
1498/**
1499 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1500 *
1501 * @param[in] xmlctx XML parser context.
1502 * @param[in] parent Parent to append nodes to.
1503 * @return LY_ERR value.
1504 */
1505static LY_ERR
1506lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1507{
1508 LY_ERR r;
1509 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001510 ly_bool no_dup;
1511
1512 /* there must be some child */
1513 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1514 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1515 return LY_EVALID;
1516 }
1517
1518 while (xmlctx->status == LYXML_ELEMENT) {
1519 child = NULL;
1520
1521 /*
1522 * session-id
1523 */
1524 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1525 if (r == LY_SUCCESS) {
1526 no_dup = 1;
1527 goto check_child;
1528 } else if (r != LY_ENOT) {
1529 goto error;
1530 }
1531
1532 /*
1533 * bad-attribute
1534 */
1535 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1536 if (r == LY_SUCCESS) {
1537 no_dup = 1;
1538 goto check_child;
1539 } else if (r != LY_ENOT) {
1540 goto error;
1541 }
1542
1543 /*
1544 * bad-element
1545 */
1546 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1547 if (r == LY_SUCCESS) {
1548 no_dup = 1;
1549 goto check_child;
1550 } else if (r != LY_ENOT) {
1551 goto error;
1552 }
1553
1554 /*
1555 * bad-namespace
1556 */
1557 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1558 if (r == LY_SUCCESS) {
1559 no_dup = 1;
1560 goto check_child;
1561 } else if (r != LY_ENOT) {
1562 goto error;
1563 }
1564
1565 if (r == LY_ENOT) {
1566 assert(xmlctx->status == LYXML_ELEMENT);
1567
Michal Vasko845ba392022-06-16 07:52:13 +02001568 /* custom elements, parse all the siblings */
1569 while (xmlctx->status == LYXML_ELEMENT) {
1570 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1571 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1572 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001573 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001574 }
1575
1576check_child:
1577 /* check for duplicates */
1578 if (no_dup) {
1579 LY_LIST_FOR(lyd_child(parent), iter) {
1580 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1581 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1582 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1583 ((struct lyd_node_opaq *)child)->name.name);
1584 r = LY_EVALID;
1585 goto error;
1586 }
1587 }
1588 }
1589
1590 /* finish child parsing */
1591 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1592 assert(xmlctx->status == LYXML_ELEMENT);
1593 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001594 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001595 r = LY_EVALID;
1596 goto error;
1597 }
1598 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1599
1600 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001601 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001602 }
1603
1604 return LY_SUCCESS;
1605
1606error:
1607 lyd_free_tree(child);
1608 return r;
1609}
1610
1611/**
1612 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1613 *
1614 * @param[in] xmlctx XML parser context.
1615 * @param[in] parent Parent to append nodes to.
1616 * @return LY_ERR value.
1617 */
1618static LY_ERR
1619lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1620{
1621 LY_ERR r;
1622 struct lyd_node *child, *iter;
1623 const char *val;
1624 ly_bool no_dup;
1625
1626 /* there must be some child */
1627 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1628 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1629 return LY_EVALID;
1630 }
1631
1632 while (xmlctx->status == LYXML_ELEMENT) {
1633 child = NULL;
1634
1635 /*
1636 * error-type
1637 */
1638 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1639 if (r == LY_SUCCESS) {
1640 val = ((struct lyd_node_opaq *)child)->value;
1641 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1642 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1643 ((struct lyd_node_opaq *)child)->name.name);
1644 r = LY_EVALID;
1645 goto error;
1646 }
1647
1648 no_dup = 1;
1649 goto check_child;
1650 } else if (r != LY_ENOT) {
1651 goto error;
1652 }
1653
1654 /*
1655 * error-tag
1656 */
1657 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1658 if (r == LY_SUCCESS) {
1659 val = ((struct lyd_node_opaq *)child)->value;
1660 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1661 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1662 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1663 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1664 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1665 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1666 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1667 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1668 ((struct lyd_node_opaq *)child)->name.name);
1669 r = LY_EVALID;
1670 goto error;
1671 }
1672
1673 no_dup = 1;
1674 goto check_child;
1675 } else if (r != LY_ENOT) {
1676 goto error;
1677 }
1678
1679 /*
1680 * error-severity
1681 */
1682 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1683 if (r == LY_SUCCESS) {
1684 val = ((struct lyd_node_opaq *)child)->value;
1685 if (strcmp(val, "error") && strcmp(val, "warning")) {
1686 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1687 ((struct lyd_node_opaq *)child)->name.name);
1688 r = LY_EVALID;
1689 goto error;
1690 }
1691
1692 no_dup = 1;
1693 goto check_child;
1694 } else if (r != LY_ENOT) {
1695 goto error;
1696 }
1697
1698 /*
1699 * error-app-tag
1700 */
1701 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1702 if (r == LY_SUCCESS) {
1703 no_dup = 1;
1704 goto check_child;
1705 } else if (r != LY_ENOT) {
1706 goto error;
1707 }
1708
1709 /*
1710 * error-path
1711 */
1712 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1713 if (r == LY_SUCCESS) {
1714 no_dup = 1;
1715 goto check_child;
1716 } else if (r != LY_ENOT) {
1717 goto error;
1718 }
1719
1720 /*
1721 * error-message
1722 */
1723 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1724 if (r == LY_SUCCESS) {
1725 no_dup = 1;
1726 goto check_child;
1727 } else if (r != LY_ENOT) {
1728 goto error;
1729 }
1730
1731 /* error-info */
1732 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1733 if (r == LY_SUCCESS) {
1734 /* parse all the descendants */
1735 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1736
1737 no_dup = 0;
1738 goto check_child;
1739 } else if (r != LY_ENOT) {
1740 goto error;
1741 }
1742
1743 if (r == LY_ENOT) {
1744 assert(xmlctx->status == LYXML_ELEMENT);
1745 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001746 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001747 r = LY_EVALID;
1748 goto error;
1749 }
1750
1751check_child:
1752 /* check for duplicates */
1753 if (no_dup) {
1754 LY_LIST_FOR(lyd_child(parent), iter) {
1755 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1756 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1757 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1758 ((struct lyd_node_opaq *)child)->name.name);
1759 r = LY_EVALID;
1760 goto error;
1761 }
1762 }
1763 }
1764
1765 /* finish child parsing */
1766 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1767 assert(xmlctx->status == LYXML_ELEMENT);
1768 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001769 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001770 r = LY_EVALID;
1771 goto error;
1772 }
1773 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1774
1775 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001776 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001777 }
1778
1779 return LY_SUCCESS;
1780
1781error:
1782 lyd_free_tree(child);
1783 return r;
1784}
1785
1786/**
1787 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1788 *
1789 * @param[in] xmlctx XML parser context.
1790 * @param[out] evnp Parsed envelope(s) (opaque node).
1791 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1792 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1793 * @return LY_SUCCESS on success.
1794 * @return LY_ERR value on error.
1795 */
1796static LY_ERR
1797lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1798{
1799 LY_ERR rc = LY_SUCCESS, r;
1800 struct lyd_node *child = NULL;
1801 const char *parsed_elem = NULL;
1802
1803 assert(envp && !*envp);
1804
1805 /* parse "rpc-reply" */
1806 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001807 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1808
1809 /* there must be some child */
1810 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1811 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1812 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001813 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001814 }
1815
Michal Vaskoe0665742021-02-11 11:08:44 +01001816 /* try to parse "ok" */
1817 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1818 if (r == LY_SUCCESS) {
1819 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001820 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001821
1822 /* finish child parsing */
1823 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1824 assert(xmlctx->status == LYXML_ELEMENT);
1825 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001826 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001827 rc = LY_EVALID;
1828 goto cleanup;
1829 }
1830 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1831
1832 /* success */
1833 parsed_elem = "ok";
1834 goto finish;
1835 } else if (r != LY_ENOT) {
1836 rc = r;
1837 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001838 }
1839
Michal Vaskoe0665742021-02-11 11:08:44 +01001840 /* try to parse all "rpc-error" elements */
1841 while (xmlctx->status == LYXML_ELEMENT) {
1842 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1843 if (r == LY_ENOT) {
1844 break;
1845 } else if (r) {
1846 rc = r;
1847 goto cleanup;
1848 }
1849
1850 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001851 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001852
1853 /* parse all children of "rpc-error" */
1854 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1855
1856 /* finish child parsing */
1857 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1858 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1859
1860 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001861 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001862
1863finish:
1864 if (parsed_elem) {
1865 /* NETCONF rpc-reply with no data */
1866 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1867 assert(xmlctx->status == LYXML_ELEMENT);
1868 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001869 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001870 rc = LY_EVALID;
1871 goto cleanup;
1872 }
1873 }
1874
1875 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001876 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001877 *close_elem = 1;
1878
1879cleanup:
1880 if (rc) {
1881 lyd_free_tree(*envp);
1882 *envp = NULL;
1883 }
1884 return rc;
1885}
1886
Michal Vasko2552ea32020-12-08 15:32:34 +01001887LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001888lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Radek Krejcif16e2542021-02-17 15:39:23 +01001889 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001890 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001891{
Michal Vaskoe0665742021-02-11 11:08:44 +01001892 LY_ERR rc = LY_SUCCESS;
1893 struct lyd_xml_ctx *lydctx;
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001894 struct lyd_node *node;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001895 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001896 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001897
Michal Vaskoe0665742021-02-11 11:08:44 +01001898 assert(ctx && in && lydctx_p);
1899 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1900 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001901 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1902
Michal Vaskoe0665742021-02-11 11:08:44 +01001903 /* init context */
1904 lydctx = calloc(1, sizeof *lydctx);
1905 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1906 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1907 lydctx->parse_opts = parse_opts;
1908 lydctx->val_opts = val_opts;
1909 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001910 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001911
Michal Vaskoe0665742021-02-11 11:08:44 +01001912 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001913 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001914 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001915 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1916 if (rc == LY_ENOT) {
1917 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1918 }
1919 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001920 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001921 case LYD_TYPE_NOTIF_NETCONF:
1922 assert(!parent);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001923
1924 /* parse "notification" */
1925 rc = lydxml_envelope(lydctx->xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001926 if (rc == LY_ENOT) {
1927 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1928 }
1929 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001930
1931 /* NETCONF notification */
1932 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;
1933 close_elem = 1;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001934 break;
1935 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001936 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001937 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1938 if (rc == LY_ENOT) {
1939 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1940 }
1941 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001942 break;
Michal Vasko820efe82023-05-12 15:47:43 +02001943 case LYD_TYPE_RPC_RESTCONF:
1944 assert(parent);
1945
1946 /* parse "input" */
Michal Vasko420cc252023-08-24 08:14:24 +02001947 rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001948 if (rc == LY_ENOT) {
1949 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace.");
1950 }
1951 LY_CHECK_GOTO(rc, cleanup);
1952
1953 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
1954 close_elem = 1;
1955 break;
1956 case LYD_TYPE_REPLY_RESTCONF:
1957 assert(parent);
1958
1959 /* parse "output" */
Michal Vasko420cc252023-08-24 08:14:24 +02001960 rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001961 if (rc == LY_ENOT) {
1962 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace.");
1963 }
1964 LY_CHECK_GOTO(rc, cleanup);
1965
1966 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
1967 close_elem = 1;
1968 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001969 default:
1970 LOGINT(ctx);
1971 rc = LY_EINT;
1972 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001973 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001974
Michal Vaskoe0665742021-02-11 11:08:44 +01001975 lydctx->int_opts = int_opts;
1976
1977 /* find the operation node if it exists already */
1978 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1979
1980 /* parse XML data */
1981 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1982 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1983 parsed_data_nodes = 1;
1984
1985 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1986 break;
1987 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001988 }
1989
Michal Vaskoe0665742021-02-11 11:08:44 +01001990 /* close all opened elements */
1991 for (i = 0; i < close_elem; ++i) {
1992 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1993 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001994 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1995 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001996 rc = LY_EVALID;
1997 goto cleanup;
1998 }
1999
2000 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02002001 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002002
2003 /* check final state */
2004 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
2005 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
2006 rc = LY_EVALID;
2007 goto cleanup;
2008 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01002009 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01002010 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
2011 rc = LY_EVALID;
2012 goto cleanup;
2013 }
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002014 if (int_opts & LYD_INTOPT_EVENTTIME) {
2015 /* parse as a child of the envelope */
2016 node = (*first_p)->prev;
2017 if (node->schema) {
2018 LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node.");
2019 rc = LY_EVALID;
2020 goto cleanup;
2021 } else {
2022 /* can be the only opaque node and an operation had to be parsed */
2023 assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
Michal Vasko2e784f82024-01-11 09:51:22 +01002024 lyd_unlink(node);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002025 assert(*envp);
2026 lyd_insert_child(*envp, node);
2027 }
2028 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002029
2030 if (!parsed_data_nodes) {
2031 /* no data nodes were parsed */
2032 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01002033 }
2034
2035cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01002036 /* there should be no unres stored if validation should be skipped */
2037 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01002038 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01002039
2040 if (rc) {
2041 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
2042 } else {
2043 *lydctx_p = (struct lyd_ctx *)lydctx;
2044
2045 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
2046 lyxml_ctx_free(lydctx->xmlctx);
2047 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002048 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002049 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002050}