blob: c9b61451014e425f6c3f25cf6fac3ed5e4a0ef1f [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
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 Vasko69730152020-10-09 16:30:07 +020015#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <stdint.h>
17#include <stdlib.h>
18#include <string.h>
19
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010022#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
26#include "parser_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "set.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "tree_data_internal.h"
31#include "tree_schema.h"
Radek Krejci77114102021-03-10 15:21:57 +010032#include "tree_schema_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010033#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035
Radek Krejci1798aae2020-07-14 13:26:06 +020036void
37lyd_xml_ctx_free(struct lyd_ctx *lydctx)
38{
39 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
40
41 lyd_ctx_free(lydctx);
42 lyxml_ctx_free(ctx->xmlctx);
43 free(ctx);
44}
45
Michal Vasko45791ad2021-06-17 08:45:03 +020046/**
47 * @brief Parse and create XML metadata.
48 *
49 * @param[in] lydctx XML data parser context.
50 * @param[in] parent_exts Extension instances of the parent node.
51 * @param[out] meta List of created metadata instances.
52 * @return LY_ERR value.
53 */
Radek Krejcie7b95092019-05-15 11:03:07 +020054static LY_ERR
Michal Vasko45791ad2021-06-17 08:45:03 +020055lydxml_metadata(struct lyd_xml_ctx *lydctx, struct lysc_ext_instance *parent_exts, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020056{
aPiecek1c4da362021-04-29 14:26:34 +020057 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020058 const struct lyxml_ns *ns;
59 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010060 const char *name;
61 size_t name_len;
Michal Vasko45791ad2021-06-17 08:45:03 +020062 LY_ARRAY_COUNT_TYPE u;
63 ly_bool filter_attrs = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020064 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020065
Michal Vaskob36053d2020-03-26 15:49:30 +010066 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +020067
Michal Vasko45791ad2021-06-17 08:45:03 +020068 /* check for NETCONF filter unqualified attributes */
69 LY_ARRAY_FOR(parent_exts, u) {
70 if (!strcmp(parent_exts[u].def->name, "get-filter-element-attributes") &&
71 !strcmp(parent_exts[u].def->module->name, "ietf-netconf")) {
72 filter_attrs = 1;
73 break;
74 }
75 }
76
Michal Vaskob36053d2020-03-26 15:49:30 +010077 while (xmlctx->status == LYXML_ATTRIBUTE) {
78 if (!xmlctx->prefix_len) {
Michal Vasko45791ad2021-06-17 08:45:03 +020079 /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
80 if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
81 !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
82 mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
83 if (!mod) {
84 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
85 "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
86 ret = LY_ENOTFOUND;
87 goto cleanup;
88 }
89 goto create_meta;
90 }
91
Michal Vaskoe0665742021-02-11 11:08:44 +010092 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010093 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +010094 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +020095 ret = LY_EVALID;
Michal Vaskob36053d2020-03-26 15:49:30 +010096 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +020097 }
Michal Vaskob36053d2020-03-26 15:49:30 +010098
Michal Vasko45791ad2021-06-17 08:45:03 +020099 /* skip attr */
Michal Vaskob36053d2020-03-26 15:49:30 +0100100 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
101 assert(xmlctx->status == LYXML_ATTR_CONTENT);
102 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200103 continue;
104 }
105
106 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200107 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200108 if (!ns) {
Michal Vasko52927e22020-03-16 17:26:14 +0100109 /* unknown namespace, XML error */
Radek Krejci422afb12021-03-04 16:38:16 +0100110 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko45791ad2021-06-17 08:45:03 +0200111 ret = LY_ENOTFOUND;
Radek Krejci28681fa2019-09-06 13:08:45 +0200112 goto cleanup;
113 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200114
115 /* get the module with metadata definition */
Michal Vasko52927e22020-03-16 17:26:14 +0100116 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200117 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100118 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100119 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200120 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100121 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
122 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200123 ret = LY_ENOTFOUND;
Michal Vaskob36053d2020-03-26 15:49:30 +0100124 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200125 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200126
127 /* skip attr */
128 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
129 assert(xmlctx->status == LYXML_ATTR_CONTENT);
130 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
131 continue;
Radek Krejci28681fa2019-09-06 13:08:45 +0200132 }
133
Michal Vasko45791ad2021-06-17 08:45:03 +0200134create_meta:
Michal Vasko60ea6352020-06-29 13:39:39 +0200135 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100136 name = xmlctx->name;
137 name_len = xmlctx->name_len;
138 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
139 assert(xmlctx->status == LYXML_ATTR_CONTENT);
140
141 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200142 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200143 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200144 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100145
146 /* next attribute */
147 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200148 }
Michal Vasko52927e22020-03-16 17:26:14 +0100149
Radek Krejcie7b95092019-05-15 11:03:07 +0200150cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100151 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200152 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100153 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200154 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200155 return ret;
156}
157
Michal Vasko52927e22020-03-16 17:26:14 +0100158static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200159lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100160{
161 LY_ERR ret = LY_SUCCESS;
162 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100163 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200164 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200165 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100166 const char *name, *prefix;
167 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100168
169 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100170 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100171
Michal Vaskob36053d2020-03-26 15:49:30 +0100172 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100173 if (*attr) {
174 attr2 = *attr;
175 } else {
176 attr2 = NULL;
177 }
178
Michal Vaskob36053d2020-03-26 15:49:30 +0100179 /* remember attr prefix, name, and get its content */
180 prefix = xmlctx->prefix;
181 prefix_len = xmlctx->prefix_len;
182 name = xmlctx->name;
183 name_len = xmlctx->name_len;
184 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
185 assert(xmlctx->status == LYXML_ATTR_CONTENT);
186
Michal Vaskoe137fc42021-07-22 11:53:13 +0200187 /* handle special "xml" attribute prefix */
188 if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
189 name = prefix;
190 name_len += 1 + prefix_len;
191 prefix = NULL;
192 prefix_len = 0;
193 }
194
195 /* find namespace of the attribute, if any */
196 ns = NULL;
197 if (prefix_len) {
198 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
199 if (!ns) {
200 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
201 ret = LY_EVALID;
202 goto cleanup;
203 }
204 }
205
Michal Vasko52927e22020-03-16 17:26:14 +0100206 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100207 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200208 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 +0100209 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100210
211 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100212 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
213 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100214 LY_CHECK_GOTO(ret, cleanup);
215
216 if (!*attr) {
217 *attr = attr2;
218 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100219
220 /* next attribute */
221 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100222 }
223
224cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100225 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200226 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100227 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100228 }
Michal Vasko52927e22020-03-16 17:26:14 +0100229 return ret;
230}
231
Michal Vasko44685da2020-03-17 15:38:06 +0100232static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100233lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100234{
Michal Vaskob36053d2020-03-26 15:49:30 +0100235 LY_ERR ret = LY_SUCCESS, r;
236 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100237 struct ly_set key_set = {0};
238 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100239 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100240
241 assert(list && (list->nodetype == LYS_LIST));
242
243 /* get all keys into a set (keys do not have if-features or anything) */
244 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100245 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200246 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200247 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100248 }
249
Michal Vasko12d809c2021-03-03 16:34:32 +0100250 /* remember parent count */
251 parents_count = xmlctx->elements.count;
252
Michal Vaskob36053d2020-03-26 15:49:30 +0100253 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100254 /* find key definition */
255 for (i = 0; i < key_set.count; ++i) {
256 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100257 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100258 break;
259 }
260 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100261 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100262
263 /* skip attributes */
264 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100265 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
266 assert(xmlctx->status == LYXML_ATTR_CONTENT);
267 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100268 }
269
Michal Vaskob36053d2020-03-26 15:49:30 +0100270 assert(xmlctx->status == LYXML_ELEM_CONTENT);
271 if (i < key_set.count) {
272 /* validate the value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200273 r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns);
Michal Vaskob36053d2020-03-26 15:49:30 +0100274 if (!r) {
275 /* key with a valid value, remove from the set */
276 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100277 }
278 }
279
Michal Vaskob36053d2020-03-26 15:49:30 +0100280 /* parser next */
281 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100282
Michal Vaskob36053d2020-03-26 15:49:30 +0100283 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100284 while (xmlctx->status == LYXML_ELEMENT) {
285 while (parents_count < xmlctx->elements.count) {
286 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
287 }
288 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100289 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
290 }
291
292 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
293 assert(xmlctx->status == LYXML_ELEM_CLOSE);
294 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
295 if (next != LYXML_ELEM_CLOSE) {
296 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
297 }
Michal Vasko44685da2020-03-17 15:38:06 +0100298 }
299
300 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100301 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100302 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100303 }
304
305cleanup:
306 ly_set_erase(&key_set, NULL);
307 return ret;
308}
309
Michal Vasko5c24ed12021-06-09 09:27:32 +0200310/**
311 * @brief Skip an element with all its descendants.
312 *
313 * @param[in] xmlctx XML parser context.
314 * @return LY_ERR value.
315 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100316static LY_ERR
317lydxml_data_skip(struct lyxml_ctx *xmlctx)
318{
319 uint32_t parents_count;
320
321 /* remember current number of parents */
322 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200323 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100324
325 /* skip after the content */
326 while (xmlctx->status != LYXML_ELEM_CONTENT) {
327 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
328 }
329 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
330
331 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200332 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100333 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
334 }
335
336 /* close element */
337 assert(xmlctx->status == LYXML_ELEM_CLOSE);
338 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
339
340 return LY_SUCCESS;
341}
342
Michal Vasko5c24ed12021-06-09 09:27:32 +0200343/**
344 * @brief Check that the current element can be parsed as a data node.
345 *
346 * @param[in] lydctx XML data parser context.
347 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
348 * @return LY_ERR value.
349 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100350static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200351lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100352{
353 LY_ERR ret = LY_SUCCESS;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200354 struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
Michal Vasko1bf09392020-03-27 12:38:10 +0100355
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100356 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
357 /* only checks specific to opaque nodes */
358 return LY_SUCCESS;
359 }
360
Michal Vasko13854662021-06-09 09:27:50 +0200361 if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
362 /* nothing to check */
363 return LY_SUCCESS;
364 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100365
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200366 assert(xmlctx->elements.count);
367
Michal Vasko13854662021-06-09 09:27:50 +0200368 /* backup parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200369 LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
Michal Vasko1bf09392020-03-27 12:38:10 +0100370
Michal Vasko13854662021-06-09 09:27:50 +0200371 /* skip attributes */
372 while (xmlctx->status == LYXML_ATTRIBUTE) {
373 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
374 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
375 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100376
Michal Vasko13854662021-06-09 09:27:50 +0200377 if ((*snode)->nodetype & LYD_NODE_TERM) {
378 /* value may not be valid in which case we parse it as an opaque node */
379 if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns)) {
Michal Vaskod0237d42021-07-12 14:49:46 +0200380 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, xmlctx->value_len,
381 xmlctx->value);
Michal Vasko13854662021-06-09 09:27:50 +0200382 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100383 }
Michal Vasko13854662021-06-09 09:27:50 +0200384 } else if ((*snode)->nodetype == LYS_LIST) {
385 /* skip content */
386 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
Michal Vasko1bf09392020-03-27 12:38:10 +0100387
Michal Vasko13854662021-06-09 09:27:50 +0200388 if (lydxml_check_list(xmlctx, *snode)) {
389 /* invalid list, parse as opaque if it missing/has invalid some keys */
Michal Vaskod0237d42021-07-12 14:49:46 +0200390 LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
Michal Vasko13854662021-06-09 09:27:50 +0200391 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100392 }
Michal Vasko13854662021-06-09 09:27:50 +0200393 } else {
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100394 /* if there is a non-WS value, it cannot be parsed as an inner node */
395 assert(xmlctx->status == LYXML_ELEM_CONTENT);
396 if (!xmlctx->ws_only) {
397 *snode = NULL;
398 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100399 }
400
Michal Vasko13854662021-06-09 09:27:50 +0200401restore:
402 /* restore parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200403 lyxml_ctx_restore(xmlctx, &pxmlctx);
Michal Vasko1bf09392020-03-27 12:38:10 +0100404 return ret;
405}
406
Radek Krejcie7b95092019-05-15 11:03:07 +0200407/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200408 * @brief Parse XML subtree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200409 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100410 * @param[in] lydctx XML YANG data parser context.
Michal Vaskoe0665742021-02-11 11:08:44 +0100411 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
412 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings,
413 * this may point to a previously existing node.
414 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Michal Vasko9b368d32020-02-14 13:53:31 +0100415 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200416 */
417static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100418lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Radek Krejcie7b95092019-05-15 11:03:07 +0200419{
Michal Vaskob36053d2020-03-26 15:49:30 +0100420 LY_ERR ret = LY_SUCCESS;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100421 const char *prefix, *name, *val;
Michal Vasko1bf09392020-03-27 12:38:10 +0100422 size_t prefix_len, name_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100423 struct lyxml_ctx *xmlctx;
424 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200425 const struct lyxml_ns *ns;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200426 struct lyd_meta *meta = NULL;
427 struct lyd_attr *attr = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200428 const struct lysc_node *snode;
429 struct lys_module *mod;
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200430 uint32_t prev_parse_opts, prev_int_opts;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200431 struct lyd_node *node = NULL, *anchor;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100432 void *val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200433 LY_VALUE_FORMAT format;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200434 uint32_t getnext_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200435
Michal Vaskoe0665742021-02-11 11:08:44 +0100436 assert(parent || first_p);
437
Michal Vaskob36053d2020-03-26 15:49:30 +0100438 xmlctx = lydctx->xmlctx;
439 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100440 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100441
Michal Vaskoa5da3292020-08-12 13:10:50 +0200442 assert(xmlctx->status == LYXML_ELEMENT);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200443
Michal Vaskoa5da3292020-08-12 13:10:50 +0200444 /* remember element prefix and name */
445 prefix = xmlctx->prefix;
446 prefix_len = xmlctx->prefix_len;
447 name = xmlctx->name;
448 name_len = xmlctx->name_len;
449
450 /* get the element module */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200451 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200452 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100453 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200454 ret = LY_EVALID;
455 goto error;
456 }
457 mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
458 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100459 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100460 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko90932a92020-02-12 14:33:03 +0100461 ret = LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200462 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200463 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100464 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200465 /* skip element with children */
466 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
467 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200468 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200469 }
470
Michal Vaskoa5da3292020-08-12 13:10:50 +0200471 /* parser next */
472 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
Michal Vasko90932a92020-02-12 14:33:03 +0100473
Michal Vaskoa5da3292020-08-12 13:10:50 +0200474 /* get the schema node */
475 snode = NULL;
Michal Vasko81008a52021-07-21 16:06:12 +0200476 if (mod) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100477 if (!parent && lydctx->ext) {
Radek Krejciba05eab2021-03-10 13:19:29 +0100478 snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100479 } else {
480 snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
481 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200482 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100483 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
484 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100485 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
486 (int)name_len, name, parent->schema->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100487 } else if (lydctx->ext) {
488 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100489 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
490 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100491 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100492 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
493 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100494 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100495 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100496 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
497 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100498 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200499 ret = LY_EVALID;
500 goto error;
Michal Vaskoe0665742021-02-11 11:08:44 +0100501 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskod68f8a22022-01-05 12:06:51 +0100502 LOGVRB("Skipping parsing of unknown node \"%.*s\".", name_len, name);
Michal Vaskod0237d42021-07-12 14:49:46 +0200503
Michal Vaskoa5da3292020-08-12 13:10:50 +0200504 /* skip element with children */
505 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
506 return LY_SUCCESS;
507 }
508 } else {
509 /* check that schema node is valid and can be used */
510 LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), error);
511 LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), error);
512 }
513 }
514
515 /* create metadata/attributes */
516 if (xmlctx->status == LYXML_ATTRIBUTE) {
517 if (snode) {
Michal Vasko45791ad2021-06-17 08:45:03 +0200518 ret = lydxml_metadata(lydctx, snode->exts, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200519 LY_CHECK_GOTO(ret, error);
520 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100521 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200522 ret = lydxml_attrs(xmlctx, &attr);
523 LY_CHECK_GOTO(ret, error);
524 }
525 }
526
527 assert(xmlctx->status == LYXML_ELEM_CONTENT);
528 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100529 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200530
531 if (xmlctx->ws_only) {
532 /* ignore WS-only value */
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100533 if (xmlctx->dynamic) {
534 free((char *) xmlctx->value);
535 }
536 xmlctx->dynamic = 0;
537 xmlctx->value = "";
Michal Vaskoa5da3292020-08-12 13:10:50 +0200538 xmlctx->value_len = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200539 format = LY_VALUE_XML;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200540 } else {
541 /* get value prefixes */
Radek Krejci8df109d2021-04-23 12:19:08 +0200542 ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100543 &xmlctx->ns, &format, &val_prefix_data);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200544 LY_CHECK_GOTO(ret, error);
545 }
546
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200547 /* get NS again, it may have been backed up and restored */
548 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
549 assert(ns);
550
Michal Vaskoa5da3292020-08-12 13:10:50 +0200551 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100552 ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
553 xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, LYD_HINT_DATA, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200554 LY_CHECK_GOTO(ret, error);
555
556 /* parser next */
557 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
558
559 /* process children */
560 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100561 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200562 LY_CHECK_GOTO(ret, error);
563 }
564 } else if (snode->nodetype & LYD_NODE_TERM) {
565 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200566 LY_CHECK_GOTO(ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200567 &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error);
Radek Krejciddace2c2021-01-08 11:30:56 +0100568 LOG_LOCSET(snode, node, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200569
570 if (parent && (node->schema->flags & LYS_KEY)) {
571 /* check the key order, the anchor must never be a key */
Michal Vaskoe0665742021-02-11 11:08:44 +0100572 anchor = lyd_insert_get_next_anchor(lyd_child(parent), node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200573 if (anchor && (anchor->schema->flags & LYS_KEY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100574 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100575 LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200576 ret = LY_EVALID;
577 goto error;
578 } else {
579 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name);
580 }
581 }
582 }
583
584 /* parser next */
585 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
586
587 /* no children expected */
588 if (xmlctx->status == LYXML_ELEMENT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100589 LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100590 (int)xmlctx->name_len, xmlctx->name, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200591 ret = LY_EVALID;
592 goto error;
593 }
594 } else if (snode->nodetype & LYD_NODE_INNER) {
595 if (!xmlctx->ws_only) {
596 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100597 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100598 (int)xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200599 ret = LY_EVALID;
600 goto error;
601 }
602
603 /* create node */
604 ret = lyd_create_inner(snode, &node);
605 LY_CHECK_GOTO(ret, error);
606
Radek Krejciddace2c2021-01-08 11:30:56 +0100607 LOG_LOCSET(snode, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100608
Michal Vaskoa5da3292020-08-12 13:10:50 +0200609 /* parser next */
610 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
611
612 /* process children */
613 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100614 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200615 LY_CHECK_GOTO(ret, error);
616 }
617
618 if (snode->nodetype == LYS_LIST) {
619 /* check all keys exist */
620 LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error);
621 }
622
Michal Vaskoe0665742021-02-11 11:08:44 +0100623 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200624 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100625 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200626 LY_CHECK_GOTO(ret, error);
627
628 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200629 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts,
630 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200631 LY_CHECK_GOTO(ret, error);
632 }
633
634 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
635 /* rememeber the RPC/action/notification */
636 lydctx->op_node = node;
637 }
638 } else if (snode->nodetype & LYD_NODE_ANY) {
Michal Vasko27c4dce2021-03-04 15:50:50 +0100639 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
640 /* value in anydata node, we expect a tree */
641 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100642 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200643 ret = LY_EVALID;
644 goto error;
645 }
646
Michal Vasko27c4dce2021-03-04 15:50:50 +0100647 if (!xmlctx->ws_only) {
648 /* use an arbitrary text value for anyxml */
649 lydict_insert(xmlctx->ctx, xmlctx->value, xmlctx->value_len, &val);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200650
Michal Vasko27c4dce2021-03-04 15:50:50 +0100651 /* parser next */
652 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
653
654 /* create node */
655 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, &node);
656 LY_CHECK_GOTO(ret, error);
657 } else {
658 /* parser next */
659 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
660
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200661 /* update options so that generic data can be parsed */
662 prev_parse_opts = lydctx->parse_opts;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100663 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
664 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200665 prev_int_opts = lydctx->int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +0200666 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200667
668 /* parse any data tree */
Michal Vasko27c4dce2021-03-04 15:50:50 +0100669 anchor = NULL;
670 while (xmlctx->status == LYXML_ELEMENT) {
671 ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200672 if (ret) {
673 break;
674 }
Michal Vasko27c4dce2021-03-04 15:50:50 +0100675 }
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200676
677 /* restore options */
678 lydctx->parse_opts = prev_parse_opts;
679 lydctx->int_opts = prev_int_opts;
680
681 LY_CHECK_GOTO(ret, error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100682
683 /* create node */
684 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
685 LY_CHECK_GOTO(ret, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200686 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200687 }
688 assert(node);
689
690 /* add/correct flags */
691 if (snode) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200692 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200693 }
694
695 /* parser next */
696 assert(xmlctx->status == LYXML_ELEM_CLOSE);
697 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
698
699 /* add metadata/attributes */
700 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +0100701 lyd_insert_meta(node, meta, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200702 } else {
703 lyd_insert_attr(node, attr);
704 }
705
706 /* insert, keep first pointer correct */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200707 lyd_insert_node(parent, first_p, node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200708 while (!parent && (*first_p)->prev->next) {
709 *first_p = (*first_p)->prev;
710 }
711
Michal Vaskoe0665742021-02-11 11:08:44 +0100712 /* rememeber a successfully parsed node */
713 if (parsed) {
714 ly_set_add(parsed, node, 1, NULL);
715 }
716
Radek Krejciddace2c2021-01-08 11:30:56 +0100717 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200718 return LY_SUCCESS;
719
720error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100721 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200722 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200723 lyd_free_attr_siblings(ctx, attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200724 lyd_free_tree(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200725 return ret;
726}
727
Michal Vaskoe0665742021-02-11 11:08:44 +0100728/**
729 * @brief Parse a specific XML element into an opaque node.
730 *
731 * @param[in] xmlctx XML parser context.
732 * @param[in] name Name of the element.
733 * @param[in] uri URI of the element.
734 * @param[in] value Whether a value is expected in the element.
735 * @param[out] evnp Parsed envelope (opaque node).
736 * @return LY_SUCCESS on success.
737 * @return LY_ENOT if the specified element did not match.
738 * @return LY_ERR value on error.
739 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100740static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100741lydxml_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 +0100742{
Michal Vaskoe0665742021-02-11 11:08:44 +0100743 LY_ERR rc = LY_SUCCESS;
744 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +0200745 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100746 const char *prefix;
747 size_t prefix_len;
748
Michal Vasko1bf09392020-03-27 12:38:10 +0100749 assert(xmlctx->status == LYXML_ELEMENT);
750 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
751 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +0100752 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100753 }
754
755 prefix = xmlctx->prefix;
756 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200757 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100758 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100759 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100760 return LY_EVALID;
761 } else if (strcmp(ns->uri, uri)) {
762 /* different namespace */
763 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100764 }
765
766 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
767
768 /* create attributes */
769 if (xmlctx->status == LYXML_ATTRIBUTE) {
770 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
771 }
772
Michal Vaskoe0665742021-02-11 11:08:44 +0100773 assert(xmlctx->status == LYXML_ELEM_CONTENT);
774 if (!value && !xmlctx->ws_only) {
775 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +0100776 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100777 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100778 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100779 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100780
781 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100782 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200783 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100784 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100785
786 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +0100787 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100788 attr = NULL;
789
Michal Vaskoe0665742021-02-11 11:08:44 +0100790 /* parser next element */
791 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100792
Michal Vaskoe0665742021-02-11 11:08:44 +0100793cleanup:
794 lyd_free_attr_siblings(xmlctx->ctx, attr);
795 if (rc) {
796 lyd_free_tree(*envp);
797 *envp = NULL;
798 }
799 return rc;
800}
801
802/**
803 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
804 *
805 * @param[in] xmlctx XML parser context.
806 * @param[out] evnp Parsed envelope(s) (opaque node).
807 * @param[out] int_opts Internal options for parsing the rest of YANG data.
808 * @param[out] close_elem Number of parsed opened elements that need to be closed.
809 * @return LY_SUCCESS on success.
810 * @return LY_ERR value on error.
811 */
812static LY_ERR
813lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
814{
815 LY_ERR rc = LY_SUCCESS, r;
816 struct lyd_node *child;
817
818 assert(envp && !*envp);
819
820 /* parse "rpc" */
821 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100822 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
823
824 /* parse "action", if any */
825 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
826 if (r == LY_SUCCESS) {
827 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200828 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +0100829
830 /* NETCONF action */
831 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
832 *close_elem = 2;
833 } else if (r == LY_ENOT) {
834 /* NETCONF RPC */
835 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
836 *close_elem = 1;
837 } else {
838 rc = r;
839 goto cleanup;
840 }
841
842cleanup:
843 if (rc) {
844 lyd_free_tree(*envp);
845 *envp = NULL;
846 }
847 return rc;
848}
849
850/**
851 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
852 *
853 * @param[in] xmlctx XML parser context.
854 * @param[out] evnp Parsed envelope(s) (opaque node).
855 * @param[out] int_opts Internal options for parsing the rest of YANG data.
856 * @param[out] close_elem Number of parsed opened elements that need to be closed.
857 * @return LY_SUCCESS on success.
858 * @return LY_ERR value on error.
859 */
860static LY_ERR
861lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
862{
863 LY_ERR rc = LY_SUCCESS, r;
864 struct lyd_node *child;
865
866 assert(envp && !*envp);
867
868 /* parse "notification" */
869 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100870 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
871
872 /* parse "eventTime" */
873 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
874 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +0100875 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
876 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100877 r = LY_EVALID;
878 }
879 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
880
881 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200882 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +0100883
884 /* validate value */
885 /* TODO validate child->value as yang:date-and-time */
886
887 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100888 if (xmlctx->status != LYXML_ELEM_CLOSE) {
889 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +0100890 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100891 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100892 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100893 goto cleanup;
894 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100895 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
896
897 /* NETCONF notification */
898 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
899 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100900
901cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100902 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +0100903 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100904 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100905 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100906 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100907}
Michal Vasko79135ae2020-12-16 10:08:35 +0100908
Michal Vaskoe0665742021-02-11 11:08:44 +0100909/**
910 * @brief Parse an XML element as an opaque node subtree.
911 *
912 * @param[in] xmlctx XML parser context.
913 * @param[in] parent Parent to append nodes to.
914 * @return LY_ERR value.
915 */
916static LY_ERR
917lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +0100918{
Michal Vaskoe0665742021-02-11 11:08:44 +0100919 LY_ERR rc = LY_SUCCESS;
920 const struct lyxml_ns *ns;
921 struct lyd_attr *attr = NULL;
922 struct lyd_node *child = NULL;
923 const char *name, *prefix;
924 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100925
Michal Vaskoe0665742021-02-11 11:08:44 +0100926 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100927
Michal Vaskoe0665742021-02-11 11:08:44 +0100928 name = xmlctx->name;
929 name_len = xmlctx->name_len;
930 prefix = xmlctx->prefix;
931 prefix_len = xmlctx->prefix_len;
932 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
933 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100934 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100935 return LY_EVALID;
936 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100937
Michal Vaskoe0665742021-02-11 11:08:44 +0100938 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +0100939
Michal Vaskoe0665742021-02-11 11:08:44 +0100940 /* create attributes */
941 if (xmlctx->status == LYXML_ATTRIBUTE) {
942 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
943 }
944
945 /* create node */
946 assert(xmlctx->status == LYXML_ELEM_CONTENT);
947 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200948 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +0100949 LY_CHECK_GOTO(rc, cleanup);
950
951 /* assign atributes */
952 ((struct lyd_node_opaq *)child)->attr = attr;
953 attr = NULL;
954
955 /* parser next element */
956 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
957
958 /* parse all the descendants */
959 while (xmlctx->status == LYXML_ELEMENT) {
960 rc = lydxml_opaq_r(xmlctx, child);
961 LY_CHECK_GOTO(rc, cleanup);
962 }
963
964 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200965 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +0100966
967cleanup:
968 lyd_free_attr_siblings(xmlctx->ctx, attr);
969 if (rc) {
970 lyd_free_tree(child);
971 }
972 return rc;
973}
974
975/**
976 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
977 *
978 * @param[in] xmlctx XML parser context.
979 * @param[in] parent Parent to append nodes to.
980 * @return LY_ERR value.
981 */
982static LY_ERR
983lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
984{
985 LY_ERR r;
986 struct lyd_node *child, *iter;
987 const struct lyxml_ns *ns;
988 ly_bool no_dup;
989
990 /* there must be some child */
991 if (xmlctx->status == LYXML_ELEM_CLOSE) {
992 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
993 return LY_EVALID;
994 }
995
996 while (xmlctx->status == LYXML_ELEMENT) {
997 child = NULL;
998
999 /*
1000 * session-id
1001 */
1002 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1003 if (r == LY_SUCCESS) {
1004 no_dup = 1;
1005 goto check_child;
1006 } else if (r != LY_ENOT) {
1007 goto error;
1008 }
1009
1010 /*
1011 * bad-attribute
1012 */
1013 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1014 if (r == LY_SUCCESS) {
1015 no_dup = 1;
1016 goto check_child;
1017 } else if (r != LY_ENOT) {
1018 goto error;
1019 }
1020
1021 /*
1022 * bad-element
1023 */
1024 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1025 if (r == LY_SUCCESS) {
1026 no_dup = 1;
1027 goto check_child;
1028 } else if (r != LY_ENOT) {
1029 goto error;
1030 }
1031
1032 /*
1033 * bad-namespace
1034 */
1035 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1036 if (r == LY_SUCCESS) {
1037 no_dup = 1;
1038 goto check_child;
1039 } else if (r != LY_ENOT) {
1040 goto error;
1041 }
1042
1043 if (r == LY_ENOT) {
1044 assert(xmlctx->status == LYXML_ELEMENT);
1045
1046 /* learn namespace */
1047 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
1048 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001049 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001050 r = LY_EVALID;
1051 goto error;
1052 } else if (!strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:base:1.0")) {
1053 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001054 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001055 r = LY_EVALID;
1056 goto error;
1057 }
1058
1059 /* custom elements */
1060 r = lydxml_opaq_r(xmlctx, parent);
1061 LY_CHECK_GOTO(r, error);
1062
1063 no_dup = 0;
1064 }
1065
1066check_child:
1067 /* check for duplicates */
1068 if (no_dup) {
1069 LY_LIST_FOR(lyd_child(parent), iter) {
1070 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1071 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1072 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1073 ((struct lyd_node_opaq *)child)->name.name);
1074 r = LY_EVALID;
1075 goto error;
1076 }
1077 }
1078 }
1079
1080 /* finish child parsing */
1081 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1082 assert(xmlctx->status == LYXML_ELEMENT);
1083 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001084 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001085 r = LY_EVALID;
1086 goto error;
1087 }
1088 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1089
1090 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001091 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001092 }
1093
1094 return LY_SUCCESS;
1095
1096error:
1097 lyd_free_tree(child);
1098 return r;
1099}
1100
1101/**
1102 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1103 *
1104 * @param[in] xmlctx XML parser context.
1105 * @param[in] parent Parent to append nodes to.
1106 * @return LY_ERR value.
1107 */
1108static LY_ERR
1109lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1110{
1111 LY_ERR r;
1112 struct lyd_node *child, *iter;
1113 const char *val;
1114 ly_bool no_dup;
1115
1116 /* there must be some child */
1117 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1118 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1119 return LY_EVALID;
1120 }
1121
1122 while (xmlctx->status == LYXML_ELEMENT) {
1123 child = NULL;
1124
1125 /*
1126 * error-type
1127 */
1128 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1129 if (r == LY_SUCCESS) {
1130 val = ((struct lyd_node_opaq *)child)->value;
1131 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1132 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1133 ((struct lyd_node_opaq *)child)->name.name);
1134 r = LY_EVALID;
1135 goto error;
1136 }
1137
1138 no_dup = 1;
1139 goto check_child;
1140 } else if (r != LY_ENOT) {
1141 goto error;
1142 }
1143
1144 /*
1145 * error-tag
1146 */
1147 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1148 if (r == LY_SUCCESS) {
1149 val = ((struct lyd_node_opaq *)child)->value;
1150 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1151 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1152 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1153 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1154 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1155 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1156 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1157 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1158 ((struct lyd_node_opaq *)child)->name.name);
1159 r = LY_EVALID;
1160 goto error;
1161 }
1162
1163 no_dup = 1;
1164 goto check_child;
1165 } else if (r != LY_ENOT) {
1166 goto error;
1167 }
1168
1169 /*
1170 * error-severity
1171 */
1172 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1173 if (r == LY_SUCCESS) {
1174 val = ((struct lyd_node_opaq *)child)->value;
1175 if (strcmp(val, "error") && strcmp(val, "warning")) {
1176 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1177 ((struct lyd_node_opaq *)child)->name.name);
1178 r = LY_EVALID;
1179 goto error;
1180 }
1181
1182 no_dup = 1;
1183 goto check_child;
1184 } else if (r != LY_ENOT) {
1185 goto error;
1186 }
1187
1188 /*
1189 * error-app-tag
1190 */
1191 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1192 if (r == LY_SUCCESS) {
1193 no_dup = 1;
1194 goto check_child;
1195 } else if (r != LY_ENOT) {
1196 goto error;
1197 }
1198
1199 /*
1200 * error-path
1201 */
1202 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1203 if (r == LY_SUCCESS) {
1204 no_dup = 1;
1205 goto check_child;
1206 } else if (r != LY_ENOT) {
1207 goto error;
1208 }
1209
1210 /*
1211 * error-message
1212 */
1213 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1214 if (r == LY_SUCCESS) {
1215 no_dup = 1;
1216 goto check_child;
1217 } else if (r != LY_ENOT) {
1218 goto error;
1219 }
1220
1221 /* error-info */
1222 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1223 if (r == LY_SUCCESS) {
1224 /* parse all the descendants */
1225 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1226
1227 no_dup = 0;
1228 goto check_child;
1229 } else if (r != LY_ENOT) {
1230 goto error;
1231 }
1232
1233 if (r == LY_ENOT) {
1234 assert(xmlctx->status == LYXML_ELEMENT);
1235 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001236 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001237 r = LY_EVALID;
1238 goto error;
1239 }
1240
1241check_child:
1242 /* check for duplicates */
1243 if (no_dup) {
1244 LY_LIST_FOR(lyd_child(parent), iter) {
1245 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1246 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1247 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1248 ((struct lyd_node_opaq *)child)->name.name);
1249 r = LY_EVALID;
1250 goto error;
1251 }
1252 }
1253 }
1254
1255 /* finish child parsing */
1256 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1257 assert(xmlctx->status == LYXML_ELEMENT);
1258 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001259 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001260 r = LY_EVALID;
1261 goto error;
1262 }
1263 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1264
1265 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001266 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001267 }
1268
1269 return LY_SUCCESS;
1270
1271error:
1272 lyd_free_tree(child);
1273 return r;
1274}
1275
1276/**
1277 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1278 *
1279 * @param[in] xmlctx XML parser context.
1280 * @param[out] evnp Parsed envelope(s) (opaque node).
1281 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1282 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1283 * @return LY_SUCCESS on success.
1284 * @return LY_ERR value on error.
1285 */
1286static LY_ERR
1287lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1288{
1289 LY_ERR rc = LY_SUCCESS, r;
1290 struct lyd_node *child = NULL;
1291 const char *parsed_elem = NULL;
1292
1293 assert(envp && !*envp);
1294
1295 /* parse "rpc-reply" */
1296 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001297 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1298
1299 /* there must be some child */
1300 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1301 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1302 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001303 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001304 }
1305
Michal Vaskoe0665742021-02-11 11:08:44 +01001306 /* try to parse "ok" */
1307 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1308 if (r == LY_SUCCESS) {
1309 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001310 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001311
1312 /* finish child parsing */
1313 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1314 assert(xmlctx->status == LYXML_ELEMENT);
1315 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001316 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001317 rc = LY_EVALID;
1318 goto cleanup;
1319 }
1320 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1321
1322 /* success */
1323 parsed_elem = "ok";
1324 goto finish;
1325 } else if (r != LY_ENOT) {
1326 rc = r;
1327 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001328 }
1329
Michal Vaskoe0665742021-02-11 11:08:44 +01001330 /* try to parse all "rpc-error" elements */
1331 while (xmlctx->status == LYXML_ELEMENT) {
1332 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1333 if (r == LY_ENOT) {
1334 break;
1335 } else if (r) {
1336 rc = r;
1337 goto cleanup;
1338 }
1339
1340 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001341 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001342
1343 /* parse all children of "rpc-error" */
1344 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1345
1346 /* finish child parsing */
1347 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1348 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1349
1350 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001351 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001352
1353finish:
1354 if (parsed_elem) {
1355 /* NETCONF rpc-reply with no data */
1356 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1357 assert(xmlctx->status == LYXML_ELEMENT);
1358 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001359 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001360 rc = LY_EVALID;
1361 goto cleanup;
1362 }
1363 }
1364
1365 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001366 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001367 *close_elem = 1;
1368
1369cleanup:
1370 if (rc) {
1371 lyd_free_tree(*envp);
1372 *envp = NULL;
1373 }
1374 return rc;
1375}
1376
Michal Vasko2552ea32020-12-08 15:32:34 +01001377LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001378lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1379 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1380 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001381{
Michal Vaskoe0665742021-02-11 11:08:44 +01001382 LY_ERR rc = LY_SUCCESS;
1383 struct lyd_xml_ctx *lydctx;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001384 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001385 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001386
Michal Vaskoe0665742021-02-11 11:08:44 +01001387 assert(ctx && in && lydctx_p);
1388 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1389 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001390
Michal Vaskoe0665742021-02-11 11:08:44 +01001391 /* init context */
1392 lydctx = calloc(1, sizeof *lydctx);
1393 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1394 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1395 lydctx->parse_opts = parse_opts;
1396 lydctx->val_opts = val_opts;
1397 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001398 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001399
Michal Vaskoe0665742021-02-11 11:08:44 +01001400 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001401 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001402 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1403 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001404 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001405 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1406 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001407 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001408 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1409 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001410 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001411 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1412 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001413 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001414 assert(!parent);
1415 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1416 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001417 case LYD_TYPE_NOTIF_NETCONF:
1418 assert(!parent);
1419 LY_CHECK_GOTO(rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1420 break;
1421 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001422 assert(parent);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001423 LY_CHECK_GOTO(rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001424 break;
1425 }
1426 lydctx->int_opts = int_opts;
1427
1428 /* find the operation node if it exists already */
1429 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1430
1431 /* parse XML data */
1432 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1433 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1434 parsed_data_nodes = 1;
1435
1436 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1437 break;
1438 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001439 }
1440
Michal Vaskoe0665742021-02-11 11:08:44 +01001441 /* close all opened elements */
1442 for (i = 0; i < close_elem; ++i) {
1443 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1444 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001445 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1446 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001447 rc = LY_EVALID;
1448 goto cleanup;
1449 }
1450
1451 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001452 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001453
1454 /* check final state */
1455 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1456 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1457 rc = LY_EVALID;
1458 goto cleanup;
1459 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01001460 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 +01001461 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1462 rc = LY_EVALID;
1463 goto cleanup;
1464 }
1465
1466 if (!parsed_data_nodes) {
1467 /* no data nodes were parsed */
1468 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001469 }
1470
1471cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001472 /* there should be no unres stored if validation should be skipped */
1473 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001474 !lydctx->node_when.count && !lydctx->node_exts.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001475
1476 if (rc) {
1477 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1478 } else {
1479 *lydctx_p = (struct lyd_ctx *)lydctx;
1480
1481 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1482 lyxml_ctx_free(lydctx->xmlctx);
1483 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001484 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001485 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001486}