blob: ed3b2172a90853f7500bdf6e06393cf7e55eb616 [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
36/**
Michal Vaskob36053d2020-03-26 15:49:30 +010037 * @brief Internal context for XML YANG data parser.
Radek Krejci1798aae2020-07-14 13:26:06 +020038 *
Radek Krejci4f2e3e52021-03-30 14:20:28 +020039 * Note that the structure maps to the ::lyd_ctx which is common for all the data parsers
Radek Krejcie7b95092019-05-15 11:03:07 +020040 */
41struct lyd_xml_ctx {
Radek Krejcif16e2542021-02-17 15:39:23 +010042 const struct lysc_ext_instance *ext; /**< extension instance possibly changing document root context of the data being parsed */
Michal Vaskoe0665742021-02-11 11:08:44 +010043 uint32_t parse_opts; /**< various @ref dataparseroptions. */
44 uint32_t val_opts; /**< various @ref datavalidationoptions. */
Radek Krejci1798aae2020-07-14 13:26:06 +020045 uint32_t int_opts; /**< internal data parser options */
46 uint32_t path_len; /**< used bytes in the path buffer */
47 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +010048 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci4f2e3e52021-03-30 14:20:28 +020049 struct ly_set node_exts; /**< set of nodes and extensions connected with a plugin providing own validation callback */
Michal Vasko49c39d82020-11-06 17:20:27 +010050 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
51 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
Radek Krejci1798aae2020-07-14 13:26:06 +020052 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Radek Krejci1798aae2020-07-14 13:26:06 +020054 /* callbacks */
55 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020056
57 struct lyxml_ctx *xmlctx; /**< XML context */
Radek Krejcie7b95092019-05-15 11:03:07 +020058};
59
Radek Krejci1798aae2020-07-14 13:26:06 +020060void
61lyd_xml_ctx_free(struct lyd_ctx *lydctx)
62{
63 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
64
65 lyd_ctx_free(lydctx);
66 lyxml_ctx_free(ctx->xmlctx);
67 free(ctx);
68}
69
Radek Krejcie7b95092019-05-15 11:03:07 +020070static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020071lydxml_metadata(struct lyd_xml_ctx *lydctx, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020072{
aPiecek1c4da362021-04-29 14:26:34 +020073 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020074 const struct lyxml_ns *ns;
75 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010076 const char *name;
77 size_t name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +020078 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020079
Michal Vaskob36053d2020-03-26 15:49:30 +010080 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +020081
Michal Vaskob36053d2020-03-26 15:49:30 +010082 while (xmlctx->status == LYXML_ATTRIBUTE) {
83 if (!xmlctx->prefix_len) {
Radek Krejci28681fa2019-09-06 13:08:45 +020084 /* in XML, all attributes must be prefixed
85 * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
Michal Vaskoe0665742021-02-11 11:08:44 +010086 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
aPiecek1c4da362021-04-29 14:26:34 +020087 ret = LY_EVALID;
Radek Krejci2efc45b2020-12-22 16:25:44 +010088 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +010089 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +010090 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +020091 }
Michal Vaskob36053d2020-03-26 15:49:30 +010092
Radek Krejci28681fa2019-09-06 13:08:45 +020093skip_attr:
Michal Vaskob36053d2020-03-26 15:49:30 +010094 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
95 assert(xmlctx->status == LYXML_ATTR_CONTENT);
96 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +020097 continue;
98 }
99
100 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200101 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200102 if (!ns) {
aPiecek1c4da362021-04-29 14:26:34 +0200103 ret = LY_ENOTFOUND;
Michal Vasko52927e22020-03-16 17:26:14 +0100104 /* unknown namespace, XML error */
Radek Krejci422afb12021-03-04 16:38:16 +0100105 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Radek Krejci28681fa2019-09-06 13:08:45 +0200106 goto cleanup;
107 }
Michal Vasko52927e22020-03-16 17:26:14 +0100108 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200109 if (!mod) {
110 /* module is not implemented or not present in the schema */
Michal Vaskoe0665742021-02-11 11:08:44 +0100111 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
aPiecek1c4da362021-04-29 14:26:34 +0200112 ret = LY_ENOTFOUND;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100113 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200114 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100115 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
116 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100117 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200118 }
119 goto skip_attr;
120 }
121
Michal Vasko60ea6352020-06-29 13:39:39 +0200122 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100123 name = xmlctx->name;
124 name_len = xmlctx->name_len;
125 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
126 assert(xmlctx->status == LYXML_ATTR_CONTENT);
127
128 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200129 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200130 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200131 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100132
133 /* next attribute */
134 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200135 }
Michal Vasko52927e22020-03-16 17:26:14 +0100136
Radek Krejcie7b95092019-05-15 11:03:07 +0200137cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100138 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200139 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100140 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200141 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200142 return ret;
143}
144
Michal Vasko52927e22020-03-16 17:26:14 +0100145static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200146lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100147{
148 LY_ERR ret = LY_SUCCESS;
149 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100150 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200151 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200152 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100153 const char *name, *prefix;
154 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100155
156 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100157 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100158
Michal Vaskob36053d2020-03-26 15:49:30 +0100159 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100160 ns = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +0100161 if (xmlctx->prefix_len) {
Michal Vasko52927e22020-03-16 17:26:14 +0100162 /* get namespace of the attribute */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200163 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100164 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100165 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko52927e22020-03-16 17:26:14 +0100166 ret = LY_EVALID;
167 goto cleanup;
168 }
169 }
170
171 if (*attr) {
172 attr2 = *attr;
173 } else {
174 attr2 = NULL;
175 }
176
Michal Vaskob36053d2020-03-26 15:49:30 +0100177 /* remember attr prefix, name, and get its content */
178 prefix = xmlctx->prefix;
179 prefix_len = xmlctx->prefix_len;
180 name = xmlctx->name;
181 name_len = xmlctx->name_len;
182 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
183 assert(xmlctx->status == LYXML_ATTR_CONTENT);
184
Michal Vasko52927e22020-03-16 17:26:14 +0100185 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100186 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200187 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 +0100188 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100189
190 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100191 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
192 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100193 LY_CHECK_GOTO(ret, cleanup);
194
195 if (!*attr) {
196 *attr = attr2;
197 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100198
199 /* next attribute */
200 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100201 }
202
203cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100204 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200205 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100206 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100207 }
Michal Vasko52927e22020-03-16 17:26:14 +0100208 return ret;
209}
210
Michal Vasko44685da2020-03-17 15:38:06 +0100211static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100212lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100213{
Michal Vaskob36053d2020-03-26 15:49:30 +0100214 LY_ERR ret = LY_SUCCESS, r;
215 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100216 struct ly_set key_set = {0};
217 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100218 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100219
220 assert(list && (list->nodetype == LYS_LIST));
221
222 /* get all keys into a set (keys do not have if-features or anything) */
223 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100224 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200225 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200226 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100227 }
228
Michal Vasko12d809c2021-03-03 16:34:32 +0100229 /* remember parent count */
230 parents_count = xmlctx->elements.count;
231
Michal Vaskob36053d2020-03-26 15:49:30 +0100232 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100233 /* find key definition */
234 for (i = 0; i < key_set.count; ++i) {
235 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100236 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100237 break;
238 }
239 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100240 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100241
242 /* skip attributes */
243 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100244 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
245 assert(xmlctx->status == LYXML_ATTR_CONTENT);
246 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100247 }
248
Michal Vaskob36053d2020-03-26 15:49:30 +0100249 assert(xmlctx->status == LYXML_ELEM_CONTENT);
250 if (i < key_set.count) {
251 /* validate the value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200252 r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns);
Michal Vaskob36053d2020-03-26 15:49:30 +0100253 if (!r) {
254 /* key with a valid value, remove from the set */
255 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100256 }
257 }
258
Michal Vaskob36053d2020-03-26 15:49:30 +0100259 /* parser next */
260 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100261
Michal Vaskob36053d2020-03-26 15:49:30 +0100262 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100263 while (xmlctx->status == LYXML_ELEMENT) {
264 while (parents_count < xmlctx->elements.count) {
265 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
266 }
267 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100268 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
269 }
270
271 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
272 assert(xmlctx->status == LYXML_ELEM_CLOSE);
273 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
274 if (next != LYXML_ELEM_CLOSE) {
275 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
276 }
Michal Vasko44685da2020-03-17 15:38:06 +0100277 }
278
279 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100280 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100281 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100282 }
283
284cleanup:
285 ly_set_erase(&key_set, NULL);
286 return ret;
287}
288
Michal Vasko1bf09392020-03-27 12:38:10 +0100289static LY_ERR
290lydxml_data_skip(struct lyxml_ctx *xmlctx)
291{
292 uint32_t parents_count;
293
294 /* remember current number of parents */
295 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200296 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100297
298 /* skip after the content */
299 while (xmlctx->status != LYXML_ELEM_CONTENT) {
300 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
301 }
302 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
303
304 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200305 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100306 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
307 }
308
309 /* close element */
310 assert(xmlctx->status == LYXML_ELEM_CLOSE);
311 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
312
313 return LY_SUCCESS;
314}
315
316static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200317lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100318{
319 LY_ERR ret = LY_SUCCESS;
320 enum LYXML_PARSER_STATUS prev_status;
Michal Vasko63f3d842020-07-08 10:10:14 +0200321 const char *prev_current, *pname, *pprefix;
Michal Vasko1bf09392020-03-27 12:38:10 +0100322 size_t pprefix_len, pname_len;
323 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
324
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100325 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
326 /* only checks specific to opaque nodes */
327 return LY_SUCCESS;
328 }
329
330 if ((*snode)->nodetype & (LYD_NODE_TERM | LYS_LIST)) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100331 /* backup parser */
332 prev_status = xmlctx->status;
333 pprefix = xmlctx->prefix;
334 pprefix_len = xmlctx->prefix_len;
335 pname = xmlctx->name;
336 pname_len = xmlctx->name_len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200337 prev_current = xmlctx->in->current;
Michal Vasko1bf09392020-03-27 12:38:10 +0100338 if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) {
339 /* it was backed up, do not free */
340 xmlctx->dynamic = 0;
341 }
342
343 /* skip attributes */
344 while (xmlctx->status == LYXML_ATTRIBUTE) {
345 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
346 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
347 }
348
349 if ((*snode)->nodetype & LYD_NODE_TERM) {
350 /* value may not be valid in which case we parse it as an opaque node */
Radek Krejci8df109d2021-04-23 12:19:08 +0200351 if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns)) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100352 *snode = NULL;
353 }
354 } else {
355 /* skip content */
356 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
357
358 if (lydxml_check_list(xmlctx, *snode)) {
359 /* invalid list, parse as opaque if it missing/has invalid some keys */
360 *snode = NULL;
361 }
362 }
363
364restore:
365 /* restore parser */
366 if (xmlctx->dynamic) {
367 free((char *)xmlctx->value);
368 }
369 xmlctx->status = prev_status;
370 xmlctx->prefix = pprefix;
371 xmlctx->prefix_len = pprefix_len;
372 xmlctx->name = pname;
373 xmlctx->name_len = pname_len;
Michal Vasko63f3d842020-07-08 10:10:14 +0200374 xmlctx->in->current = prev_current;
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100375 } else if ((*snode)->nodetype & LYD_NODE_INNER) {
Michal Vasko90fa5682021-06-03 08:32:10 +0200376 /* skip attributes */
377 while (xmlctx->status == LYXML_ATTRIBUTE) {
378 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
379 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
380 }
381
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100382 /* if there is a non-WS value, it cannot be parsed as an inner node */
383 assert(xmlctx->status == LYXML_ELEM_CONTENT);
384 if (!xmlctx->ws_only) {
385 *snode = NULL;
386 }
387
Michal Vasko1bf09392020-03-27 12:38:10 +0100388 }
389
390 return ret;
391}
392
Radek Krejcie7b95092019-05-15 11:03:07 +0200393/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200394 * @brief Parse XML subtree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200395 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100396 * @param[in] lydctx XML YANG data parser context.
Michal Vaskoe0665742021-02-11 11:08:44 +0100397 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
398 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings,
399 * this may point to a previously existing node.
400 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Michal Vasko9b368d32020-02-14 13:53:31 +0100401 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200402 */
403static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100404lydxml_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 +0200405{
Michal Vaskob36053d2020-03-26 15:49:30 +0100406 LY_ERR ret = LY_SUCCESS;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100407 const char *prefix, *name, *val;
Michal Vasko1bf09392020-03-27 12:38:10 +0100408 size_t prefix_len, name_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100409 struct lyxml_ctx *xmlctx;
410 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200411 const struct lyxml_ns *ns;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200412 struct lyd_meta *meta = NULL;
413 struct lyd_attr *attr = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200414 const struct lysc_node *snode;
415 struct lys_module *mod;
Michal Vasko1bf09392020-03-27 12:38:10 +0100416 uint32_t prev_opts;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200417 struct lyd_node *node = NULL, *anchor;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100418 void *val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200419 LY_VALUE_FORMAT format;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200420 uint32_t getnext_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200421
Michal Vaskoe0665742021-02-11 11:08:44 +0100422 assert(parent || first_p);
423
Michal Vaskob36053d2020-03-26 15:49:30 +0100424 xmlctx = lydctx->xmlctx;
425 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100426 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100427
Michal Vaskoa5da3292020-08-12 13:10:50 +0200428 assert(xmlctx->status == LYXML_ELEMENT);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200429
Michal Vaskoa5da3292020-08-12 13:10:50 +0200430 /* remember element prefix and name */
431 prefix = xmlctx->prefix;
432 prefix_len = xmlctx->prefix_len;
433 name = xmlctx->name;
434 name_len = xmlctx->name_len;
435
436 /* get the element module */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200437 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200438 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100439 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200440 ret = LY_EVALID;
441 goto error;
442 }
443 mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
444 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100445 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100446 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko90932a92020-02-12 14:33:03 +0100447 ret = LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200448 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200449 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100450 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200451 /* skip element with children */
452 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
453 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200454 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200455 }
456
Michal Vaskoa5da3292020-08-12 13:10:50 +0200457 /* parser next */
458 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
Michal Vasko90932a92020-02-12 14:33:03 +0100459
Michal Vaskoa5da3292020-08-12 13:10:50 +0200460 /* get the schema node */
461 snode = NULL;
462 if (mod && (!parent || parent->schema)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100463 if (!parent && lydctx->ext) {
Radek Krejciba05eab2021-03-10 13:19:29 +0100464 snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100465 } else {
466 snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
467 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200468 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100469 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
470 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100471 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
472 (int)name_len, name, parent->schema->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100473 } else if (lydctx->ext) {
474 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100475 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
476 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100477 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100478 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
479 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100480 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100481 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100482 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
483 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100484 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200485 ret = LY_EVALID;
486 goto error;
Michal Vaskoe0665742021-02-11 11:08:44 +0100487 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200488 /* skip element with children */
489 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
490 return LY_SUCCESS;
491 }
492 } else {
493 /* check that schema node is valid and can be used */
494 LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), error);
495 LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), error);
496 }
497 }
498
499 /* create metadata/attributes */
500 if (xmlctx->status == LYXML_ATTRIBUTE) {
501 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200502 ret = lydxml_metadata(lydctx, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200503 LY_CHECK_GOTO(ret, error);
504 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100505 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200506 ret = lydxml_attrs(xmlctx, &attr);
507 LY_CHECK_GOTO(ret, error);
508 }
509 }
510
511 assert(xmlctx->status == LYXML_ELEM_CONTENT);
512 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100513 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200514
515 if (xmlctx->ws_only) {
516 /* ignore WS-only value */
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100517 if (xmlctx->dynamic) {
518 free((char *) xmlctx->value);
519 }
520 xmlctx->dynamic = 0;
521 xmlctx->value = "";
Michal Vaskoa5da3292020-08-12 13:10:50 +0200522 xmlctx->value_len = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200523 format = LY_VALUE_XML;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200524 } else {
525 /* get value prefixes */
Radek Krejci8df109d2021-04-23 12:19:08 +0200526 ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100527 &xmlctx->ns, &format, &val_prefix_data);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200528 LY_CHECK_GOTO(ret, error);
529 }
530
531 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100532 ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
533 xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, LYD_HINT_DATA, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200534 LY_CHECK_GOTO(ret, error);
535
536 /* parser next */
537 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
538
539 /* process children */
540 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100541 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200542 LY_CHECK_GOTO(ret, error);
543 }
544 } else if (snode->nodetype & LYD_NODE_TERM) {
545 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200546 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 +0200547 &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error);
Radek Krejciddace2c2021-01-08 11:30:56 +0100548 LOG_LOCSET(snode, node, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200549
550 if (parent && (node->schema->flags & LYS_KEY)) {
551 /* check the key order, the anchor must never be a key */
Michal Vaskoe0665742021-02-11 11:08:44 +0100552 anchor = lyd_insert_get_next_anchor(lyd_child(parent), node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200553 if (anchor && (anchor->schema->flags & LYS_KEY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100554 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100555 LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200556 ret = LY_EVALID;
557 goto error;
558 } else {
559 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name);
560 }
561 }
562 }
563
564 /* parser next */
565 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
566
567 /* no children expected */
568 if (xmlctx->status == LYXML_ELEMENT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100569 LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100570 (int)xmlctx->name_len, xmlctx->name, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200571 ret = LY_EVALID;
572 goto error;
573 }
574 } else if (snode->nodetype & LYD_NODE_INNER) {
575 if (!xmlctx->ws_only) {
576 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100577 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100578 (int)xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200579 ret = LY_EVALID;
580 goto error;
581 }
582
583 /* create node */
584 ret = lyd_create_inner(snode, &node);
585 LY_CHECK_GOTO(ret, error);
586
Radek Krejciddace2c2021-01-08 11:30:56 +0100587 LOG_LOCSET(snode, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100588
Michal Vaskoa5da3292020-08-12 13:10:50 +0200589 /* parser next */
590 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
591
592 /* process children */
593 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100594 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200595 LY_CHECK_GOTO(ret, error);
596 }
597
598 if (snode->nodetype == LYS_LIST) {
599 /* check all keys exist */
600 LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error);
601 }
602
Michal Vaskoe0665742021-02-11 11:08:44 +0100603 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200604 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100605 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200606 LY_CHECK_GOTO(ret, error);
607
608 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200609 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts,
610 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200611 LY_CHECK_GOTO(ret, error);
612 }
613
614 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
615 /* rememeber the RPC/action/notification */
616 lydctx->op_node = node;
617 }
618 } else if (snode->nodetype & LYD_NODE_ANY) {
Michal Vasko27c4dce2021-03-04 15:50:50 +0100619 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
620 /* value in anydata node, we expect a tree */
621 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100622 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200623 ret = LY_EVALID;
624 goto error;
625 }
626
Michal Vasko27c4dce2021-03-04 15:50:50 +0100627 if (!xmlctx->ws_only) {
628 /* use an arbitrary text value for anyxml */
629 lydict_insert(xmlctx->ctx, xmlctx->value, xmlctx->value_len, &val);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200630
Michal Vasko27c4dce2021-03-04 15:50:50 +0100631 /* parser next */
632 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
633
634 /* create node */
635 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, &node);
636 LY_CHECK_GOTO(ret, error);
637 } else {
638 /* parser next */
639 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
640
641 /* parse any data tree with correct options */
642 prev_opts = lydctx->parse_opts;
643 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
644 lydctx->parse_opts |= LYD_PARSE_OPAQ;
645 anchor = NULL;
646 while (xmlctx->status == LYXML_ELEMENT) {
647 ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL);
648 LY_CHECK_ERR_GOTO(ret, lydctx->parse_opts = prev_opts, error);
649 }
650 lydctx->parse_opts = prev_opts;
651
652 /* create node */
653 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
654 LY_CHECK_GOTO(ret, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200655 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200656 }
657 assert(node);
658
659 /* add/correct flags */
660 if (snode) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200661 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200662 }
663
664 /* parser next */
665 assert(xmlctx->status == LYXML_ELEM_CLOSE);
666 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
667
668 /* add metadata/attributes */
669 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +0100670 lyd_insert_meta(node, meta, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200671 } else {
672 lyd_insert_attr(node, attr);
673 }
674
675 /* insert, keep first pointer correct */
Michal Vaskoe0665742021-02-11 11:08:44 +0100676 lyd_insert_node(parent, first_p, node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200677 while (!parent && (*first_p)->prev->next) {
678 *first_p = (*first_p)->prev;
679 }
680
Michal Vaskoe0665742021-02-11 11:08:44 +0100681 /* rememeber a successfully parsed node */
682 if (parsed) {
683 ly_set_add(parsed, node, 1, NULL);
684 }
685
Radek Krejciddace2c2021-01-08 11:30:56 +0100686 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200687 return LY_SUCCESS;
688
689error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100690 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200691 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200692 lyd_free_attr_siblings(ctx, attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200693 lyd_free_tree(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200694 return ret;
695}
696
Michal Vaskoe0665742021-02-11 11:08:44 +0100697/**
698 * @brief Parse a specific XML element into an opaque node.
699 *
700 * @param[in] xmlctx XML parser context.
701 * @param[in] name Name of the element.
702 * @param[in] uri URI of the element.
703 * @param[in] value Whether a value is expected in the element.
704 * @param[out] evnp Parsed envelope (opaque node).
705 * @return LY_SUCCESS on success.
706 * @return LY_ENOT if the specified element did not match.
707 * @return LY_ERR value on error.
708 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100709static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100710lydxml_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 +0100711{
Michal Vaskoe0665742021-02-11 11:08:44 +0100712 LY_ERR rc = LY_SUCCESS;
713 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100715 const char *prefix;
716 size_t prefix_len;
717
Michal Vasko1bf09392020-03-27 12:38:10 +0100718 assert(xmlctx->status == LYXML_ELEMENT);
719 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
720 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +0100721 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100722 }
723
724 prefix = xmlctx->prefix;
725 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200726 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100727 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100728 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100729 return LY_EVALID;
730 } else if (strcmp(ns->uri, uri)) {
731 /* different namespace */
732 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100733 }
734
735 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
736
737 /* create attributes */
738 if (xmlctx->status == LYXML_ATTRIBUTE) {
739 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
740 }
741
Michal Vaskoe0665742021-02-11 11:08:44 +0100742 assert(xmlctx->status == LYXML_ELEM_CONTENT);
743 if (!value && !xmlctx->ws_only) {
744 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +0100745 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100746 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100747 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100748 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100749
750 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100751 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200752 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100753 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100754
755 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +0100756 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100757 attr = NULL;
758
Michal Vaskoe0665742021-02-11 11:08:44 +0100759 /* parser next element */
760 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100761
Michal Vaskoe0665742021-02-11 11:08:44 +0100762cleanup:
763 lyd_free_attr_siblings(xmlctx->ctx, attr);
764 if (rc) {
765 lyd_free_tree(*envp);
766 *envp = NULL;
767 }
768 return rc;
769}
770
771/**
772 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
773 *
774 * @param[in] xmlctx XML parser context.
775 * @param[out] evnp Parsed envelope(s) (opaque node).
776 * @param[out] int_opts Internal options for parsing the rest of YANG data.
777 * @param[out] close_elem Number of parsed opened elements that need to be closed.
778 * @return LY_SUCCESS on success.
779 * @return LY_ERR value on error.
780 */
781static LY_ERR
782lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
783{
784 LY_ERR rc = LY_SUCCESS, r;
785 struct lyd_node *child;
786
787 assert(envp && !*envp);
788
789 /* parse "rpc" */
790 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100791 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
792
793 /* parse "action", if any */
794 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
795 if (r == LY_SUCCESS) {
796 /* insert */
797 lyd_insert_node(*envp, NULL, child);
798
799 /* NETCONF action */
800 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
801 *close_elem = 2;
802 } else if (r == LY_ENOT) {
803 /* NETCONF RPC */
804 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
805 *close_elem = 1;
806 } else {
807 rc = r;
808 goto cleanup;
809 }
810
811cleanup:
812 if (rc) {
813 lyd_free_tree(*envp);
814 *envp = NULL;
815 }
816 return rc;
817}
818
819/**
820 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
821 *
822 * @param[in] xmlctx XML parser context.
823 * @param[out] evnp Parsed envelope(s) (opaque node).
824 * @param[out] int_opts Internal options for parsing the rest of YANG data.
825 * @param[out] close_elem Number of parsed opened elements that need to be closed.
826 * @return LY_SUCCESS on success.
827 * @return LY_ERR value on error.
828 */
829static LY_ERR
830lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
831{
832 LY_ERR rc = LY_SUCCESS, r;
833 struct lyd_node *child;
834
835 assert(envp && !*envp);
836
837 /* parse "notification" */
838 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100839 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
840
841 /* parse "eventTime" */
842 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
843 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +0100844 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
845 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100846 r = LY_EVALID;
847 }
848 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
849
850 /* insert */
851 lyd_insert_node(*envp, NULL, child);
852
853 /* validate value */
854 /* TODO validate child->value as yang:date-and-time */
855
856 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100857 if (xmlctx->status != LYXML_ELEM_CLOSE) {
858 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +0100859 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100860 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100861 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100862 goto cleanup;
863 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100864 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
865
866 /* NETCONF notification */
867 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
868 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100869
870cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100871 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +0100872 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100873 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100874 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100875 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100876}
Michal Vasko79135ae2020-12-16 10:08:35 +0100877
Michal Vaskoe0665742021-02-11 11:08:44 +0100878/**
879 * @brief Parse an XML element as an opaque node subtree.
880 *
881 * @param[in] xmlctx XML parser context.
882 * @param[in] parent Parent to append nodes to.
883 * @return LY_ERR value.
884 */
885static LY_ERR
886lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +0100887{
Michal Vaskoe0665742021-02-11 11:08:44 +0100888 LY_ERR rc = LY_SUCCESS;
889 const struct lyxml_ns *ns;
890 struct lyd_attr *attr = NULL;
891 struct lyd_node *child = NULL;
892 const char *name, *prefix;
893 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100894
Michal Vaskoe0665742021-02-11 11:08:44 +0100895 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100896
Michal Vaskoe0665742021-02-11 11:08:44 +0100897 name = xmlctx->name;
898 name_len = xmlctx->name_len;
899 prefix = xmlctx->prefix;
900 prefix_len = xmlctx->prefix_len;
901 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
902 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100903 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100904 return LY_EVALID;
905 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100906
Michal Vaskoe0665742021-02-11 11:08:44 +0100907 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +0100908
Michal Vaskoe0665742021-02-11 11:08:44 +0100909 /* create attributes */
910 if (xmlctx->status == LYXML_ATTRIBUTE) {
911 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
912 }
913
914 /* create node */
915 assert(xmlctx->status == LYXML_ELEM_CONTENT);
916 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 +0200917 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +0100918 LY_CHECK_GOTO(rc, cleanup);
919
920 /* assign atributes */
921 ((struct lyd_node_opaq *)child)->attr = attr;
922 attr = NULL;
923
924 /* parser next element */
925 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
926
927 /* parse all the descendants */
928 while (xmlctx->status == LYXML_ELEMENT) {
929 rc = lydxml_opaq_r(xmlctx, child);
930 LY_CHECK_GOTO(rc, cleanup);
931 }
932
933 /* insert */
934 lyd_insert_node(parent, NULL, child);
935
936cleanup:
937 lyd_free_attr_siblings(xmlctx->ctx, attr);
938 if (rc) {
939 lyd_free_tree(child);
940 }
941 return rc;
942}
943
944/**
945 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
946 *
947 * @param[in] xmlctx XML parser context.
948 * @param[in] parent Parent to append nodes to.
949 * @return LY_ERR value.
950 */
951static LY_ERR
952lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
953{
954 LY_ERR r;
955 struct lyd_node *child, *iter;
956 const struct lyxml_ns *ns;
957 ly_bool no_dup;
958
959 /* there must be some child */
960 if (xmlctx->status == LYXML_ELEM_CLOSE) {
961 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
962 return LY_EVALID;
963 }
964
965 while (xmlctx->status == LYXML_ELEMENT) {
966 child = NULL;
967
968 /*
969 * session-id
970 */
971 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
972 if (r == LY_SUCCESS) {
973 no_dup = 1;
974 goto check_child;
975 } else if (r != LY_ENOT) {
976 goto error;
977 }
978
979 /*
980 * bad-attribute
981 */
982 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
983 if (r == LY_SUCCESS) {
984 no_dup = 1;
985 goto check_child;
986 } else if (r != LY_ENOT) {
987 goto error;
988 }
989
990 /*
991 * bad-element
992 */
993 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
994 if (r == LY_SUCCESS) {
995 no_dup = 1;
996 goto check_child;
997 } else if (r != LY_ENOT) {
998 goto error;
999 }
1000
1001 /*
1002 * bad-namespace
1003 */
1004 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1005 if (r == LY_SUCCESS) {
1006 no_dup = 1;
1007 goto check_child;
1008 } else if (r != LY_ENOT) {
1009 goto error;
1010 }
1011
1012 if (r == LY_ENOT) {
1013 assert(xmlctx->status == LYXML_ELEMENT);
1014
1015 /* learn namespace */
1016 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
1017 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001018 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001019 r = LY_EVALID;
1020 goto error;
1021 } else if (!strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:base:1.0")) {
1022 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001023 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001024 r = LY_EVALID;
1025 goto error;
1026 }
1027
1028 /* custom elements */
1029 r = lydxml_opaq_r(xmlctx, parent);
1030 LY_CHECK_GOTO(r, error);
1031
1032 no_dup = 0;
1033 }
1034
1035check_child:
1036 /* check for duplicates */
1037 if (no_dup) {
1038 LY_LIST_FOR(lyd_child(parent), iter) {
1039 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1040 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1041 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1042 ((struct lyd_node_opaq *)child)->name.name);
1043 r = LY_EVALID;
1044 goto error;
1045 }
1046 }
1047 }
1048
1049 /* finish child parsing */
1050 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1051 assert(xmlctx->status == LYXML_ELEMENT);
1052 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001053 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001054 r = LY_EVALID;
1055 goto error;
1056 }
1057 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1058
1059 /* insert */
1060 lyd_insert_node(parent, NULL, child);
1061 }
1062
1063 return LY_SUCCESS;
1064
1065error:
1066 lyd_free_tree(child);
1067 return r;
1068}
1069
1070/**
1071 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1072 *
1073 * @param[in] xmlctx XML parser context.
1074 * @param[in] parent Parent to append nodes to.
1075 * @return LY_ERR value.
1076 */
1077static LY_ERR
1078lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1079{
1080 LY_ERR r;
1081 struct lyd_node *child, *iter;
1082 const char *val;
1083 ly_bool no_dup;
1084
1085 /* there must be some child */
1086 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1087 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1088 return LY_EVALID;
1089 }
1090
1091 while (xmlctx->status == LYXML_ELEMENT) {
1092 child = NULL;
1093
1094 /*
1095 * error-type
1096 */
1097 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1098 if (r == LY_SUCCESS) {
1099 val = ((struct lyd_node_opaq *)child)->value;
1100 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1101 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1102 ((struct lyd_node_opaq *)child)->name.name);
1103 r = LY_EVALID;
1104 goto error;
1105 }
1106
1107 no_dup = 1;
1108 goto check_child;
1109 } else if (r != LY_ENOT) {
1110 goto error;
1111 }
1112
1113 /*
1114 * error-tag
1115 */
1116 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1117 if (r == LY_SUCCESS) {
1118 val = ((struct lyd_node_opaq *)child)->value;
1119 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1120 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1121 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1122 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1123 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1124 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1125 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1126 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1127 ((struct lyd_node_opaq *)child)->name.name);
1128 r = LY_EVALID;
1129 goto error;
1130 }
1131
1132 no_dup = 1;
1133 goto check_child;
1134 } else if (r != LY_ENOT) {
1135 goto error;
1136 }
1137
1138 /*
1139 * error-severity
1140 */
1141 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1142 if (r == LY_SUCCESS) {
1143 val = ((struct lyd_node_opaq *)child)->value;
1144 if (strcmp(val, "error") && strcmp(val, "warning")) {
1145 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1146 ((struct lyd_node_opaq *)child)->name.name);
1147 r = LY_EVALID;
1148 goto error;
1149 }
1150
1151 no_dup = 1;
1152 goto check_child;
1153 } else if (r != LY_ENOT) {
1154 goto error;
1155 }
1156
1157 /*
1158 * error-app-tag
1159 */
1160 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1161 if (r == LY_SUCCESS) {
1162 no_dup = 1;
1163 goto check_child;
1164 } else if (r != LY_ENOT) {
1165 goto error;
1166 }
1167
1168 /*
1169 * error-path
1170 */
1171 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1172 if (r == LY_SUCCESS) {
1173 no_dup = 1;
1174 goto check_child;
1175 } else if (r != LY_ENOT) {
1176 goto error;
1177 }
1178
1179 /*
1180 * error-message
1181 */
1182 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1183 if (r == LY_SUCCESS) {
1184 no_dup = 1;
1185 goto check_child;
1186 } else if (r != LY_ENOT) {
1187 goto error;
1188 }
1189
1190 /* error-info */
1191 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1192 if (r == LY_SUCCESS) {
1193 /* parse all the descendants */
1194 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1195
1196 no_dup = 0;
1197 goto check_child;
1198 } else if (r != LY_ENOT) {
1199 goto error;
1200 }
1201
1202 if (r == LY_ENOT) {
1203 assert(xmlctx->status == LYXML_ELEMENT);
1204 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001205 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001206 r = LY_EVALID;
1207 goto error;
1208 }
1209
1210check_child:
1211 /* check for duplicates */
1212 if (no_dup) {
1213 LY_LIST_FOR(lyd_child(parent), iter) {
1214 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1215 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1216 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1217 ((struct lyd_node_opaq *)child)->name.name);
1218 r = LY_EVALID;
1219 goto error;
1220 }
1221 }
1222 }
1223
1224 /* finish child parsing */
1225 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1226 assert(xmlctx->status == LYXML_ELEMENT);
1227 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001228 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001229 r = LY_EVALID;
1230 goto error;
1231 }
1232 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1233
1234 /* insert */
1235 lyd_insert_node(parent, NULL, child);
1236 }
1237
1238 return LY_SUCCESS;
1239
1240error:
1241 lyd_free_tree(child);
1242 return r;
1243}
1244
1245/**
1246 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1247 *
1248 * @param[in] xmlctx XML parser context.
1249 * @param[out] evnp Parsed envelope(s) (opaque node).
1250 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1251 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1252 * @return LY_SUCCESS on success.
1253 * @return LY_ERR value on error.
1254 */
1255static LY_ERR
1256lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1257{
1258 LY_ERR rc = LY_SUCCESS, r;
1259 struct lyd_node *child = NULL;
1260 const char *parsed_elem = NULL;
1261
1262 assert(envp && !*envp);
1263
1264 /* parse "rpc-reply" */
1265 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001266 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1267
1268 /* there must be some child */
1269 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1270 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1271 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001272 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001273 }
1274
Michal Vaskoe0665742021-02-11 11:08:44 +01001275 /* try to parse "ok" */
1276 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1277 if (r == LY_SUCCESS) {
1278 /* insert */
1279 lyd_insert_node(*envp, NULL, child);
1280
1281 /* finish child parsing */
1282 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1283 assert(xmlctx->status == LYXML_ELEMENT);
1284 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001285 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001286 rc = LY_EVALID;
1287 goto cleanup;
1288 }
1289 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1290
1291 /* success */
1292 parsed_elem = "ok";
1293 goto finish;
1294 } else if (r != LY_ENOT) {
1295 rc = r;
1296 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001297 }
1298
Michal Vaskoe0665742021-02-11 11:08:44 +01001299 /* try to parse all "rpc-error" elements */
1300 while (xmlctx->status == LYXML_ELEMENT) {
1301 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1302 if (r == LY_ENOT) {
1303 break;
1304 } else if (r) {
1305 rc = r;
1306 goto cleanup;
1307 }
1308
1309 /* insert */
1310 lyd_insert_node(*envp, NULL, child);
1311
1312 /* parse all children of "rpc-error" */
1313 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1314
1315 /* finish child parsing */
1316 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1317 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1318
1319 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001320 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001321
1322finish:
1323 if (parsed_elem) {
1324 /* NETCONF rpc-reply with no data */
1325 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1326 assert(xmlctx->status == LYXML_ELEMENT);
1327 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001328 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001329 rc = LY_EVALID;
1330 goto cleanup;
1331 }
1332 }
1333
1334 /* NETCONF rpc-reply */
1335 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_REPLY;
1336 *close_elem = 1;
1337
1338cleanup:
1339 if (rc) {
1340 lyd_free_tree(*envp);
1341 *envp = NULL;
1342 }
1343 return rc;
1344}
1345
Michal Vasko2552ea32020-12-08 15:32:34 +01001346LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001347lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1348 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1349 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001350{
Michal Vaskoe0665742021-02-11 11:08:44 +01001351 LY_ERR rc = LY_SUCCESS;
1352 struct lyd_xml_ctx *lydctx;
1353 uint32_t i, int_opts, close_elem = 0;
1354 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001355
Michal Vaskoe0665742021-02-11 11:08:44 +01001356 assert(ctx && in && lydctx_p);
1357 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1358 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001359
Michal Vaskoe0665742021-02-11 11:08:44 +01001360 /* init context */
1361 lydctx = calloc(1, sizeof *lydctx);
1362 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1363 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1364 lydctx->parse_opts = parse_opts;
1365 lydctx->val_opts = val_opts;
1366 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001367 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001368
Michal Vaskoe0665742021-02-11 11:08:44 +01001369 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001370 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001371 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1372 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001373 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001374 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1375 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001376 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001377 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1378 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001379 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001380 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1381 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001382 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001383 assert(!parent);
1384 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1385 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001386 case LYD_TYPE_NOTIF_NETCONF:
1387 assert(!parent);
1388 LY_CHECK_GOTO(rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1389 break;
1390 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001391 assert(parent);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001392 LY_CHECK_GOTO(rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001393 break;
1394 }
1395 lydctx->int_opts = int_opts;
1396
1397 /* find the operation node if it exists already */
1398 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1399
1400 /* parse XML data */
1401 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1402 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1403 parsed_data_nodes = 1;
1404
1405 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1406 break;
1407 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001408 }
1409
Michal Vaskoe0665742021-02-11 11:08:44 +01001410 /* close all opened elements */
1411 for (i = 0; i < close_elem; ++i) {
1412 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1413 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001414 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1415 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001416 rc = LY_EVALID;
1417 goto cleanup;
1418 }
1419
1420 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001421 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001422
1423 /* check final state */
1424 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1425 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1426 rc = LY_EVALID;
1427 goto cleanup;
1428 }
1429 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1430 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1431 rc = LY_EVALID;
1432 goto cleanup;
1433 }
1434
1435 if (!parsed_data_nodes) {
1436 /* no data nodes were parsed */
1437 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001438 }
1439
1440cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001441 /* there should be no unres stored if validation should be skipped */
1442 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001443 !lydctx->node_when.count && !lydctx->node_exts.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001444
1445 if (rc) {
1446 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1447 } else {
1448 *lydctx_p = (struct lyd_ctx *)lydctx;
1449
1450 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1451 lyxml_ctx_free(lydctx->xmlctx);
1452 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001453 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001454 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001455}