blob: 40193e80d19f94dd69e4657b88c2736fdc76149c [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) {
376 /* if there is a non-WS value, it cannot be parsed as an inner node */
377 assert(xmlctx->status == LYXML_ELEM_CONTENT);
378 if (!xmlctx->ws_only) {
379 *snode = NULL;
380 }
381
Michal Vasko1bf09392020-03-27 12:38:10 +0100382 }
383
384 return ret;
385}
386
Radek Krejcie7b95092019-05-15 11:03:07 +0200387/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200388 * @brief Parse XML subtree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200389 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100390 * @param[in] lydctx XML YANG data parser context.
Michal Vaskoe0665742021-02-11 11:08:44 +0100391 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
392 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings,
393 * this may point to a previously existing node.
394 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Michal Vasko9b368d32020-02-14 13:53:31 +0100395 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200396 */
397static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100398lydxml_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 +0200399{
Michal Vaskob36053d2020-03-26 15:49:30 +0100400 LY_ERR ret = LY_SUCCESS;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100401 const char *prefix, *name, *val;
Michal Vasko1bf09392020-03-27 12:38:10 +0100402 size_t prefix_len, name_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100403 struct lyxml_ctx *xmlctx;
404 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200405 const struct lyxml_ns *ns;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200406 struct lyd_meta *meta = NULL;
407 struct lyd_attr *attr = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200408 const struct lysc_node *snode;
409 struct lys_module *mod;
Michal Vasko1bf09392020-03-27 12:38:10 +0100410 uint32_t prev_opts;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200411 struct lyd_node *node = NULL, *anchor;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100412 void *val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200413 LY_VALUE_FORMAT format;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200414 uint32_t getnext_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200415
Michal Vaskoe0665742021-02-11 11:08:44 +0100416 assert(parent || first_p);
417
Michal Vaskob36053d2020-03-26 15:49:30 +0100418 xmlctx = lydctx->xmlctx;
419 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100420 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100421
Michal Vaskoa5da3292020-08-12 13:10:50 +0200422 assert(xmlctx->status == LYXML_ELEMENT);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200423
Michal Vaskoa5da3292020-08-12 13:10:50 +0200424 /* remember element prefix and name */
425 prefix = xmlctx->prefix;
426 prefix_len = xmlctx->prefix_len;
427 name = xmlctx->name;
428 name_len = xmlctx->name_len;
429
430 /* get the element module */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200431 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200432 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100433 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200434 ret = LY_EVALID;
435 goto error;
436 }
437 mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
438 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100439 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100440 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko90932a92020-02-12 14:33:03 +0100441 ret = LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200442 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200443 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100444 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200445 /* skip element with children */
446 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
447 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200448 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200449 }
450
Michal Vaskoa5da3292020-08-12 13:10:50 +0200451 /* parser next */
452 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
Michal Vasko90932a92020-02-12 14:33:03 +0100453
Michal Vaskoa5da3292020-08-12 13:10:50 +0200454 /* get the schema node */
455 snode = NULL;
456 if (mod && (!parent || parent->schema)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100457 if (!parent && lydctx->ext) {
Radek Krejciba05eab2021-03-10 13:19:29 +0100458 snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100459 } else {
460 snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
461 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200462 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100463 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
464 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100465 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
466 (int)name_len, name, parent->schema->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100467 } else if (lydctx->ext) {
468 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100469 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
470 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100471 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100472 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
473 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100474 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100475 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100476 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
477 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100478 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200479 ret = LY_EVALID;
480 goto error;
Michal Vaskoe0665742021-02-11 11:08:44 +0100481 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200482 /* skip element with children */
483 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
484 return LY_SUCCESS;
485 }
486 } else {
487 /* check that schema node is valid and can be used */
488 LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), error);
489 LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), error);
490 }
491 }
492
493 /* create metadata/attributes */
494 if (xmlctx->status == LYXML_ATTRIBUTE) {
495 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200496 ret = lydxml_metadata(lydctx, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200497 LY_CHECK_GOTO(ret, error);
498 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100499 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200500 ret = lydxml_attrs(xmlctx, &attr);
501 LY_CHECK_GOTO(ret, error);
502 }
503 }
504
505 assert(xmlctx->status == LYXML_ELEM_CONTENT);
506 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100507 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200508
509 if (xmlctx->ws_only) {
510 /* ignore WS-only value */
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100511 if (xmlctx->dynamic) {
512 free((char *) xmlctx->value);
513 }
514 xmlctx->dynamic = 0;
515 xmlctx->value = "";
Michal Vaskoa5da3292020-08-12 13:10:50 +0200516 xmlctx->value_len = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200517 format = LY_VALUE_XML;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200518 } else {
519 /* get value prefixes */
Radek Krejci8df109d2021-04-23 12:19:08 +0200520 ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100521 &xmlctx->ns, &format, &val_prefix_data);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200522 LY_CHECK_GOTO(ret, error);
523 }
524
525 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100526 ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
527 xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, LYD_HINT_DATA, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200528 LY_CHECK_GOTO(ret, error);
529
530 /* parser next */
531 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
532
533 /* process children */
534 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100535 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200536 LY_CHECK_GOTO(ret, error);
537 }
538 } else if (snode->nodetype & LYD_NODE_TERM) {
539 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200540 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 +0200541 &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error);
Radek Krejciddace2c2021-01-08 11:30:56 +0100542 LOG_LOCSET(snode, node, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200543
544 if (parent && (node->schema->flags & LYS_KEY)) {
545 /* check the key order, the anchor must never be a key */
Michal Vaskoe0665742021-02-11 11:08:44 +0100546 anchor = lyd_insert_get_next_anchor(lyd_child(parent), node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200547 if (anchor && (anchor->schema->flags & LYS_KEY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100548 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100549 LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200550 ret = LY_EVALID;
551 goto error;
552 } else {
553 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name);
554 }
555 }
556 }
557
558 /* parser next */
559 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
560
561 /* no children expected */
562 if (xmlctx->status == LYXML_ELEMENT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100563 LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100564 (int)xmlctx->name_len, xmlctx->name, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200565 ret = LY_EVALID;
566 goto error;
567 }
568 } else if (snode->nodetype & LYD_NODE_INNER) {
569 if (!xmlctx->ws_only) {
570 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100571 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100572 (int)xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200573 ret = LY_EVALID;
574 goto error;
575 }
576
577 /* create node */
578 ret = lyd_create_inner(snode, &node);
579 LY_CHECK_GOTO(ret, error);
580
Radek Krejciddace2c2021-01-08 11:30:56 +0100581 LOG_LOCSET(snode, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100582
Michal Vaskoa5da3292020-08-12 13:10:50 +0200583 /* parser next */
584 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
585
586 /* process children */
587 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100588 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200589 LY_CHECK_GOTO(ret, error);
590 }
591
592 if (snode->nodetype == LYS_LIST) {
593 /* check all keys exist */
594 LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error);
595 }
596
Michal Vaskoe0665742021-02-11 11:08:44 +0100597 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200598 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100599 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200600 LY_CHECK_GOTO(ret, error);
601
602 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200603 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts,
604 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200605 LY_CHECK_GOTO(ret, error);
606 }
607
608 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
609 /* rememeber the RPC/action/notification */
610 lydctx->op_node = node;
611 }
612 } else if (snode->nodetype & LYD_NODE_ANY) {
Michal Vasko27c4dce2021-03-04 15:50:50 +0100613 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
614 /* value in anydata node, we expect a tree */
615 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100616 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200617 ret = LY_EVALID;
618 goto error;
619 }
620
Michal Vasko27c4dce2021-03-04 15:50:50 +0100621 if (!xmlctx->ws_only) {
622 /* use an arbitrary text value for anyxml */
623 lydict_insert(xmlctx->ctx, xmlctx->value, xmlctx->value_len, &val);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200624
Michal Vasko27c4dce2021-03-04 15:50:50 +0100625 /* parser next */
626 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
627
628 /* create node */
629 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, &node);
630 LY_CHECK_GOTO(ret, error);
631 } else {
632 /* parser next */
633 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
634
635 /* parse any data tree with correct options */
636 prev_opts = lydctx->parse_opts;
637 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
638 lydctx->parse_opts |= LYD_PARSE_OPAQ;
639 anchor = NULL;
640 while (xmlctx->status == LYXML_ELEMENT) {
641 ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL);
642 LY_CHECK_ERR_GOTO(ret, lydctx->parse_opts = prev_opts, error);
643 }
644 lydctx->parse_opts = prev_opts;
645
646 /* create node */
647 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
648 LY_CHECK_GOTO(ret, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200649 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200650 }
651 assert(node);
652
653 /* add/correct flags */
654 if (snode) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200655 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200656 }
657
658 /* parser next */
659 assert(xmlctx->status == LYXML_ELEM_CLOSE);
660 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
661
662 /* add metadata/attributes */
663 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +0100664 lyd_insert_meta(node, meta, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200665 } else {
666 lyd_insert_attr(node, attr);
667 }
668
669 /* insert, keep first pointer correct */
Michal Vaskoe0665742021-02-11 11:08:44 +0100670 lyd_insert_node(parent, first_p, node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200671 while (!parent && (*first_p)->prev->next) {
672 *first_p = (*first_p)->prev;
673 }
674
Michal Vaskoe0665742021-02-11 11:08:44 +0100675 /* rememeber a successfully parsed node */
676 if (parsed) {
677 ly_set_add(parsed, node, 1, NULL);
678 }
679
Radek Krejciddace2c2021-01-08 11:30:56 +0100680 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200681 return LY_SUCCESS;
682
683error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100684 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200685 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200686 lyd_free_attr_siblings(ctx, attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200687 lyd_free_tree(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200688 return ret;
689}
690
Michal Vaskoe0665742021-02-11 11:08:44 +0100691/**
692 * @brief Parse a specific XML element into an opaque node.
693 *
694 * @param[in] xmlctx XML parser context.
695 * @param[in] name Name of the element.
696 * @param[in] uri URI of the element.
697 * @param[in] value Whether a value is expected in the element.
698 * @param[out] evnp Parsed envelope (opaque node).
699 * @return LY_SUCCESS on success.
700 * @return LY_ENOT if the specified element did not match.
701 * @return LY_ERR value on error.
702 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100703static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100704lydxml_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 +0100705{
Michal Vaskoe0665742021-02-11 11:08:44 +0100706 LY_ERR rc = LY_SUCCESS;
707 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +0200708 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100709 const char *prefix;
710 size_t prefix_len;
711
Michal Vasko1bf09392020-03-27 12:38:10 +0100712 assert(xmlctx->status == LYXML_ELEMENT);
713 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
714 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +0100715 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100716 }
717
718 prefix = xmlctx->prefix;
719 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200720 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100721 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100722 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100723 return LY_EVALID;
724 } else if (strcmp(ns->uri, uri)) {
725 /* different namespace */
726 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100727 }
728
729 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
730
731 /* create attributes */
732 if (xmlctx->status == LYXML_ATTRIBUTE) {
733 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
734 }
735
Michal Vaskoe0665742021-02-11 11:08:44 +0100736 assert(xmlctx->status == LYXML_ELEM_CONTENT);
737 if (!value && !xmlctx->ws_only) {
738 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +0100739 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100740 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100741 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100742 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100743
744 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100745 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200746 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100747 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100748
749 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +0100750 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100751 attr = NULL;
752
Michal Vaskoe0665742021-02-11 11:08:44 +0100753 /* parser next element */
754 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100755
Michal Vaskoe0665742021-02-11 11:08:44 +0100756cleanup:
757 lyd_free_attr_siblings(xmlctx->ctx, attr);
758 if (rc) {
759 lyd_free_tree(*envp);
760 *envp = NULL;
761 }
762 return rc;
763}
764
765/**
766 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
767 *
768 * @param[in] xmlctx XML parser context.
769 * @param[out] evnp Parsed envelope(s) (opaque node).
770 * @param[out] int_opts Internal options for parsing the rest of YANG data.
771 * @param[out] close_elem Number of parsed opened elements that need to be closed.
772 * @return LY_SUCCESS on success.
773 * @return LY_ERR value on error.
774 */
775static LY_ERR
776lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
777{
778 LY_ERR rc = LY_SUCCESS, r;
779 struct lyd_node *child;
780
781 assert(envp && !*envp);
782
783 /* parse "rpc" */
784 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100785 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
786
787 /* parse "action", if any */
788 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
789 if (r == LY_SUCCESS) {
790 /* insert */
791 lyd_insert_node(*envp, NULL, child);
792
793 /* NETCONF action */
794 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
795 *close_elem = 2;
796 } else if (r == LY_ENOT) {
797 /* NETCONF RPC */
798 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
799 *close_elem = 1;
800 } else {
801 rc = r;
802 goto cleanup;
803 }
804
805cleanup:
806 if (rc) {
807 lyd_free_tree(*envp);
808 *envp = NULL;
809 }
810 return rc;
811}
812
813/**
814 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
815 *
816 * @param[in] xmlctx XML parser context.
817 * @param[out] evnp Parsed envelope(s) (opaque node).
818 * @param[out] int_opts Internal options for parsing the rest of YANG data.
819 * @param[out] close_elem Number of parsed opened elements that need to be closed.
820 * @return LY_SUCCESS on success.
821 * @return LY_ERR value on error.
822 */
823static LY_ERR
824lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
825{
826 LY_ERR rc = LY_SUCCESS, r;
827 struct lyd_node *child;
828
829 assert(envp && !*envp);
830
831 /* parse "notification" */
832 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100833 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
834
835 /* parse "eventTime" */
836 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
837 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +0100838 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
839 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100840 r = LY_EVALID;
841 }
842 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
843
844 /* insert */
845 lyd_insert_node(*envp, NULL, child);
846
847 /* validate value */
848 /* TODO validate child->value as yang:date-and-time */
849
850 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100851 if (xmlctx->status != LYXML_ELEM_CLOSE) {
852 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +0100853 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100854 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100855 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100856 goto cleanup;
857 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100858 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
859
860 /* NETCONF notification */
861 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
862 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100863
864cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100865 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +0100866 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100867 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100868 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100869 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100870}
Michal Vasko79135ae2020-12-16 10:08:35 +0100871
Michal Vaskoe0665742021-02-11 11:08:44 +0100872/**
873 * @brief Parse an XML element as an opaque node subtree.
874 *
875 * @param[in] xmlctx XML parser context.
876 * @param[in] parent Parent to append nodes to.
877 * @return LY_ERR value.
878 */
879static LY_ERR
880lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +0100881{
Michal Vaskoe0665742021-02-11 11:08:44 +0100882 LY_ERR rc = LY_SUCCESS;
883 const struct lyxml_ns *ns;
884 struct lyd_attr *attr = NULL;
885 struct lyd_node *child = NULL;
886 const char *name, *prefix;
887 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100888
Michal Vaskoe0665742021-02-11 11:08:44 +0100889 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100890
Michal Vaskoe0665742021-02-11 11:08:44 +0100891 name = xmlctx->name;
892 name_len = xmlctx->name_len;
893 prefix = xmlctx->prefix;
894 prefix_len = xmlctx->prefix_len;
895 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
896 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100897 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100898 return LY_EVALID;
899 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100900
Michal Vaskoe0665742021-02-11 11:08:44 +0100901 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +0100902
Michal Vaskoe0665742021-02-11 11:08:44 +0100903 /* create attributes */
904 if (xmlctx->status == LYXML_ATTRIBUTE) {
905 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
906 }
907
908 /* create node */
909 assert(xmlctx->status == LYXML_ELEM_CONTENT);
910 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 +0200911 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +0100912 LY_CHECK_GOTO(rc, cleanup);
913
914 /* assign atributes */
915 ((struct lyd_node_opaq *)child)->attr = attr;
916 attr = NULL;
917
918 /* parser next element */
919 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
920
921 /* parse all the descendants */
922 while (xmlctx->status == LYXML_ELEMENT) {
923 rc = lydxml_opaq_r(xmlctx, child);
924 LY_CHECK_GOTO(rc, cleanup);
925 }
926
927 /* insert */
928 lyd_insert_node(parent, NULL, child);
929
930cleanup:
931 lyd_free_attr_siblings(xmlctx->ctx, attr);
932 if (rc) {
933 lyd_free_tree(child);
934 }
935 return rc;
936}
937
938/**
939 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
940 *
941 * @param[in] xmlctx XML parser context.
942 * @param[in] parent Parent to append nodes to.
943 * @return LY_ERR value.
944 */
945static LY_ERR
946lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
947{
948 LY_ERR r;
949 struct lyd_node *child, *iter;
950 const struct lyxml_ns *ns;
951 ly_bool no_dup;
952
953 /* there must be some child */
954 if (xmlctx->status == LYXML_ELEM_CLOSE) {
955 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
956 return LY_EVALID;
957 }
958
959 while (xmlctx->status == LYXML_ELEMENT) {
960 child = NULL;
961
962 /*
963 * session-id
964 */
965 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
966 if (r == LY_SUCCESS) {
967 no_dup = 1;
968 goto check_child;
969 } else if (r != LY_ENOT) {
970 goto error;
971 }
972
973 /*
974 * bad-attribute
975 */
976 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
977 if (r == LY_SUCCESS) {
978 no_dup = 1;
979 goto check_child;
980 } else if (r != LY_ENOT) {
981 goto error;
982 }
983
984 /*
985 * bad-element
986 */
987 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
988 if (r == LY_SUCCESS) {
989 no_dup = 1;
990 goto check_child;
991 } else if (r != LY_ENOT) {
992 goto error;
993 }
994
995 /*
996 * bad-namespace
997 */
998 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
999 if (r == LY_SUCCESS) {
1000 no_dup = 1;
1001 goto check_child;
1002 } else if (r != LY_ENOT) {
1003 goto error;
1004 }
1005
1006 if (r == LY_ENOT) {
1007 assert(xmlctx->status == LYXML_ELEMENT);
1008
1009 /* learn namespace */
1010 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
1011 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001012 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001013 r = LY_EVALID;
1014 goto error;
1015 } else if (!strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:base:1.0")) {
1016 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001017 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001018 r = LY_EVALID;
1019 goto error;
1020 }
1021
1022 /* custom elements */
1023 r = lydxml_opaq_r(xmlctx, parent);
1024 LY_CHECK_GOTO(r, error);
1025
1026 no_dup = 0;
1027 }
1028
1029check_child:
1030 /* check for duplicates */
1031 if (no_dup) {
1032 LY_LIST_FOR(lyd_child(parent), iter) {
1033 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1034 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1035 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1036 ((struct lyd_node_opaq *)child)->name.name);
1037 r = LY_EVALID;
1038 goto error;
1039 }
1040 }
1041 }
1042
1043 /* finish child parsing */
1044 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1045 assert(xmlctx->status == LYXML_ELEMENT);
1046 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001047 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001048 r = LY_EVALID;
1049 goto error;
1050 }
1051 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1052
1053 /* insert */
1054 lyd_insert_node(parent, NULL, child);
1055 }
1056
1057 return LY_SUCCESS;
1058
1059error:
1060 lyd_free_tree(child);
1061 return r;
1062}
1063
1064/**
1065 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1066 *
1067 * @param[in] xmlctx XML parser context.
1068 * @param[in] parent Parent to append nodes to.
1069 * @return LY_ERR value.
1070 */
1071static LY_ERR
1072lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1073{
1074 LY_ERR r;
1075 struct lyd_node *child, *iter;
1076 const char *val;
1077 ly_bool no_dup;
1078
1079 /* there must be some child */
1080 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1081 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1082 return LY_EVALID;
1083 }
1084
1085 while (xmlctx->status == LYXML_ELEMENT) {
1086 child = NULL;
1087
1088 /*
1089 * error-type
1090 */
1091 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1092 if (r == LY_SUCCESS) {
1093 val = ((struct lyd_node_opaq *)child)->value;
1094 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1095 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1096 ((struct lyd_node_opaq *)child)->name.name);
1097 r = LY_EVALID;
1098 goto error;
1099 }
1100
1101 no_dup = 1;
1102 goto check_child;
1103 } else if (r != LY_ENOT) {
1104 goto error;
1105 }
1106
1107 /*
1108 * error-tag
1109 */
1110 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1111 if (r == LY_SUCCESS) {
1112 val = ((struct lyd_node_opaq *)child)->value;
1113 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1114 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1115 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1116 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1117 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1118 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1119 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1120 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1121 ((struct lyd_node_opaq *)child)->name.name);
1122 r = LY_EVALID;
1123 goto error;
1124 }
1125
1126 no_dup = 1;
1127 goto check_child;
1128 } else if (r != LY_ENOT) {
1129 goto error;
1130 }
1131
1132 /*
1133 * error-severity
1134 */
1135 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1136 if (r == LY_SUCCESS) {
1137 val = ((struct lyd_node_opaq *)child)->value;
1138 if (strcmp(val, "error") && strcmp(val, "warning")) {
1139 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1140 ((struct lyd_node_opaq *)child)->name.name);
1141 r = LY_EVALID;
1142 goto error;
1143 }
1144
1145 no_dup = 1;
1146 goto check_child;
1147 } else if (r != LY_ENOT) {
1148 goto error;
1149 }
1150
1151 /*
1152 * error-app-tag
1153 */
1154 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1155 if (r == LY_SUCCESS) {
1156 no_dup = 1;
1157 goto check_child;
1158 } else if (r != LY_ENOT) {
1159 goto error;
1160 }
1161
1162 /*
1163 * error-path
1164 */
1165 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1166 if (r == LY_SUCCESS) {
1167 no_dup = 1;
1168 goto check_child;
1169 } else if (r != LY_ENOT) {
1170 goto error;
1171 }
1172
1173 /*
1174 * error-message
1175 */
1176 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1177 if (r == LY_SUCCESS) {
1178 no_dup = 1;
1179 goto check_child;
1180 } else if (r != LY_ENOT) {
1181 goto error;
1182 }
1183
1184 /* error-info */
1185 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1186 if (r == LY_SUCCESS) {
1187 /* parse all the descendants */
1188 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1189
1190 no_dup = 0;
1191 goto check_child;
1192 } else if (r != LY_ENOT) {
1193 goto error;
1194 }
1195
1196 if (r == LY_ENOT) {
1197 assert(xmlctx->status == LYXML_ELEMENT);
1198 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001199 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001200 r = LY_EVALID;
1201 goto error;
1202 }
1203
1204check_child:
1205 /* check for duplicates */
1206 if (no_dup) {
1207 LY_LIST_FOR(lyd_child(parent), iter) {
1208 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1209 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1210 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1211 ((struct lyd_node_opaq *)child)->name.name);
1212 r = LY_EVALID;
1213 goto error;
1214 }
1215 }
1216 }
1217
1218 /* finish child parsing */
1219 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1220 assert(xmlctx->status == LYXML_ELEMENT);
1221 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001222 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001223 r = LY_EVALID;
1224 goto error;
1225 }
1226 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1227
1228 /* insert */
1229 lyd_insert_node(parent, NULL, child);
1230 }
1231
1232 return LY_SUCCESS;
1233
1234error:
1235 lyd_free_tree(child);
1236 return r;
1237}
1238
1239/**
1240 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1241 *
1242 * @param[in] xmlctx XML parser context.
1243 * @param[out] evnp Parsed envelope(s) (opaque node).
1244 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1245 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1246 * @return LY_SUCCESS on success.
1247 * @return LY_ERR value on error.
1248 */
1249static LY_ERR
1250lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1251{
1252 LY_ERR rc = LY_SUCCESS, r;
1253 struct lyd_node *child = NULL;
1254 const char *parsed_elem = NULL;
1255
1256 assert(envp && !*envp);
1257
1258 /* parse "rpc-reply" */
1259 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001260 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1261
1262 /* there must be some child */
1263 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1264 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1265 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001266 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001267 }
1268
Michal Vaskoe0665742021-02-11 11:08:44 +01001269 /* try to parse "ok" */
1270 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1271 if (r == LY_SUCCESS) {
1272 /* insert */
1273 lyd_insert_node(*envp, NULL, child);
1274
1275 /* finish child parsing */
1276 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1277 assert(xmlctx->status == LYXML_ELEMENT);
1278 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001279 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001280 rc = LY_EVALID;
1281 goto cleanup;
1282 }
1283 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1284
1285 /* success */
1286 parsed_elem = "ok";
1287 goto finish;
1288 } else if (r != LY_ENOT) {
1289 rc = r;
1290 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001291 }
1292
Michal Vaskoe0665742021-02-11 11:08:44 +01001293 /* try to parse all "rpc-error" elements */
1294 while (xmlctx->status == LYXML_ELEMENT) {
1295 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1296 if (r == LY_ENOT) {
1297 break;
1298 } else if (r) {
1299 rc = r;
1300 goto cleanup;
1301 }
1302
1303 /* insert */
1304 lyd_insert_node(*envp, NULL, child);
1305
1306 /* parse all children of "rpc-error" */
1307 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1308
1309 /* finish child parsing */
1310 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1311 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1312
1313 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001314 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001315
1316finish:
1317 if (parsed_elem) {
1318 /* NETCONF rpc-reply with no data */
1319 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1320 assert(xmlctx->status == LYXML_ELEMENT);
1321 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001322 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001323 rc = LY_EVALID;
1324 goto cleanup;
1325 }
1326 }
1327
1328 /* NETCONF rpc-reply */
1329 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_REPLY;
1330 *close_elem = 1;
1331
1332cleanup:
1333 if (rc) {
1334 lyd_free_tree(*envp);
1335 *envp = NULL;
1336 }
1337 return rc;
1338}
1339
Michal Vasko2552ea32020-12-08 15:32:34 +01001340LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001341lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1342 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1343 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001344{
Michal Vaskoe0665742021-02-11 11:08:44 +01001345 LY_ERR rc = LY_SUCCESS;
1346 struct lyd_xml_ctx *lydctx;
1347 uint32_t i, int_opts, close_elem = 0;
1348 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001349
Michal Vaskoe0665742021-02-11 11:08:44 +01001350 assert(ctx && in && lydctx_p);
1351 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1352 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001353
Michal Vaskoe0665742021-02-11 11:08:44 +01001354 /* init context */
1355 lydctx = calloc(1, sizeof *lydctx);
1356 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1357 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1358 lydctx->parse_opts = parse_opts;
1359 lydctx->val_opts = val_opts;
1360 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001361 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001362
Michal Vaskoe0665742021-02-11 11:08:44 +01001363 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001364 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001365 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1366 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001367 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001368 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1369 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001370 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001371 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1372 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001373 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001374 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1375 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001376 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001377 assert(!parent);
1378 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1379 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001380 case LYD_TYPE_NOTIF_NETCONF:
1381 assert(!parent);
1382 LY_CHECK_GOTO(rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
1383 break;
1384 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001385 assert(parent);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001386 LY_CHECK_GOTO(rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem), cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001387 break;
1388 }
1389 lydctx->int_opts = int_opts;
1390
1391 /* find the operation node if it exists already */
1392 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1393
1394 /* parse XML data */
1395 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1396 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1397 parsed_data_nodes = 1;
1398
1399 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1400 break;
1401 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001402 }
1403
Michal Vaskoe0665742021-02-11 11:08:44 +01001404 /* close all opened elements */
1405 for (i = 0; i < close_elem; ++i) {
1406 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1407 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001408 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1409 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001410 rc = LY_EVALID;
1411 goto cleanup;
1412 }
1413
1414 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001415 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001416
1417 /* check final state */
1418 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1419 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1420 rc = LY_EVALID;
1421 goto cleanup;
1422 }
1423 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1424 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1425 rc = LY_EVALID;
1426 goto cleanup;
1427 }
1428
1429 if (!parsed_data_nodes) {
1430 /* no data nodes were parsed */
1431 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001432 }
1433
1434cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001435 /* there should be no unres stored if validation should be skipped */
1436 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001437 !lydctx->node_when.count && !lydctx->node_exts.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001438
1439 if (rc) {
1440 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1441 } else {
1442 *lydctx_p = (struct lyd_ctx *)lydctx;
1443
1444 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1445 lyxml_ctx_free(lydctx->xmlctx);
1446 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001447 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001448 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001449}