blob: da43b40997da586810f4599e643e3a83542ae336 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko8cc3f662022-03-29 11:25:51 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02005 * @brief XML data parser for libyang
6 *
Michal Vasko8cc3f662022-03-29 11:25:51 +02007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Michal Vasko742a5b12022-02-24 16:07:27 +010016#define _GNU_SOURCE
17
Michal Vasko69730152020-10-09 16:30:07 +020018#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vasko742a5b12022-02-24 16:07:27 +010024#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020029#include "parser_data.h"
30#include "parser_internal.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010031#include "plugins_exts.h"
Michal Vasko51de7b72022-04-29 09:50:22 +020032#include "plugins_internal.h"
33#include "schema_compile_node.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "set.h"
Radek Krejci77114102021-03-10 15:21:57 +010035#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree_data_internal.h"
38#include "tree_schema.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_schema_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010040#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020042
Radek Krejci1798aae2020-07-14 13:26:06 +020043void
44lyd_xml_ctx_free(struct lyd_ctx *lydctx)
45{
46 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
47
48 lyd_ctx_free(lydctx);
49 lyxml_ctx_free(ctx->xmlctx);
50 free(ctx);
51}
52
Michal Vasko45791ad2021-06-17 08:45:03 +020053/**
54 * @brief Parse and create XML metadata.
55 *
56 * @param[in] lydctx XML data parser context.
Michal Vaskoddd76592022-01-17 13:34:48 +010057 * @param[in] sparent Schema node of the parent.
Michal Vasko45791ad2021-06-17 08:45:03 +020058 * @param[out] meta List of created metadata instances.
59 * @return LY_ERR value.
60 */
Radek Krejcie7b95092019-05-15 11:03:07 +020061static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +010062lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020063{
aPiecek1c4da362021-04-29 14:26:34 +020064 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020065 const struct lyxml_ns *ns;
66 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010067 const char *name;
68 size_t name_len;
Michal Vasko45791ad2021-06-17 08:45:03 +020069 LY_ARRAY_COUNT_TYPE u;
70 ly_bool filter_attrs = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020071 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020072
Michal Vaskob36053d2020-03-26 15:49:30 +010073 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +020074
Michal Vasko45791ad2021-06-17 08:45:03 +020075 /* check for NETCONF filter unqualified attributes */
Michal Vaskoddd76592022-01-17 13:34:48 +010076 LY_ARRAY_FOR(sparent->exts, u) {
77 if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") &&
78 !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) {
Michal Vasko45791ad2021-06-17 08:45:03 +020079 filter_attrs = 1;
80 break;
81 }
82 }
83
Michal Vaskob36053d2020-03-26 15:49:30 +010084 while (xmlctx->status == LYXML_ATTRIBUTE) {
85 if (!xmlctx->prefix_len) {
Michal Vasko45791ad2021-06-17 08:45:03 +020086 /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
87 if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
88 !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
89 mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
90 if (!mod) {
91 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
92 "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
93 ret = LY_ENOTFOUND;
94 goto cleanup;
95 }
96 goto create_meta;
97 }
98
Michal Vaskoe0665742021-02-11 11:08:44 +010099 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100100 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100101 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200102 ret = LY_EVALID;
Michal Vaskob36053d2020-03-26 15:49:30 +0100103 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200104 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100105
Michal Vasko45791ad2021-06-17 08:45:03 +0200106 /* skip attr */
Michal Vaskob36053d2020-03-26 15:49:30 +0100107 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
108 assert(xmlctx->status == LYXML_ATTR_CONTENT);
109 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200110 continue;
111 }
112
113 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200114 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200115 if (!ns) {
Michal Vasko52927e22020-03-16 17:26:14 +0100116 /* unknown namespace, XML error */
Radek Krejci422afb12021-03-04 16:38:16 +0100117 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko45791ad2021-06-17 08:45:03 +0200118 ret = LY_ENOTFOUND;
Radek Krejci28681fa2019-09-06 13:08:45 +0200119 goto cleanup;
120 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200121
122 /* get the module with metadata definition */
Michal Vasko52927e22020-03-16 17:26:14 +0100123 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200124 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100125 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100126 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200127 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100128 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
129 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200130 ret = LY_ENOTFOUND;
Michal Vaskob36053d2020-03-26 15:49:30 +0100131 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200132 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200133
134 /* skip attr */
135 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
136 assert(xmlctx->status == LYXML_ATTR_CONTENT);
137 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
138 continue;
Radek Krejci28681fa2019-09-06 13:08:45 +0200139 }
140
Michal Vasko45791ad2021-06-17 08:45:03 +0200141create_meta:
Michal Vasko60ea6352020-06-29 13:39:39 +0200142 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100143 name = xmlctx->name;
144 name_len = xmlctx->name_len;
145 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
146 assert(xmlctx->status == LYXML_ATTR_CONTENT);
147
148 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200149 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100150 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200151 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100152
153 /* next attribute */
154 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200155 }
Michal Vasko52927e22020-03-16 17:26:14 +0100156
Radek Krejcie7b95092019-05-15 11:03:07 +0200157cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100158 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200159 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100160 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200161 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200162 return ret;
163}
164
Michal Vasko52927e22020-03-16 17:26:14 +0100165static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200166lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100167{
168 LY_ERR ret = LY_SUCCESS;
169 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100170 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200171 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200172 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100173 const char *name, *prefix;
174 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100175
176 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100177 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100178
Michal Vaskob36053d2020-03-26 15:49:30 +0100179 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100180 if (*attr) {
181 attr2 = *attr;
182 } else {
183 attr2 = NULL;
184 }
185
Michal Vaskob36053d2020-03-26 15:49:30 +0100186 /* remember attr prefix, name, and get its content */
187 prefix = xmlctx->prefix;
188 prefix_len = xmlctx->prefix_len;
189 name = xmlctx->name;
190 name_len = xmlctx->name_len;
191 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
192 assert(xmlctx->status == LYXML_ATTR_CONTENT);
193
Michal Vaskoe137fc42021-07-22 11:53:13 +0200194 /* handle special "xml" attribute prefix */
195 if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
196 name = prefix;
197 name_len += 1 + prefix_len;
198 prefix = NULL;
199 prefix_len = 0;
200 }
201
202 /* find namespace of the attribute, if any */
203 ns = NULL;
204 if (prefix_len) {
205 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
206 if (!ns) {
207 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
208 ret = LY_EVALID;
209 goto cleanup;
210 }
211 }
212
Michal Vasko52927e22020-03-16 17:26:14 +0100213 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100214 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200215 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 +0100216 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100217
218 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100219 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
Michal Vaskoe5e49e92022-02-01 13:15:08 +0100220 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data,
221 LYD_HINT_DATA);
Michal Vasko52927e22020-03-16 17:26:14 +0100222 LY_CHECK_GOTO(ret, cleanup);
223
224 if (!*attr) {
225 *attr = attr2;
226 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100227
228 /* next attribute */
229 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100230 }
231
232cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100233 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200234 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100235 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100236 }
Michal Vasko52927e22020-03-16 17:26:14 +0100237 return ret;
238}
239
Michal Vasko44685da2020-03-17 15:38:06 +0100240static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100241lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100242{
Michal Vaskob36053d2020-03-26 15:49:30 +0100243 LY_ERR ret = LY_SUCCESS, r;
244 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100245 struct ly_set key_set = {0};
246 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100247 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100248
249 assert(list && (list->nodetype == LYS_LIST));
250
251 /* get all keys into a set (keys do not have if-features or anything) */
252 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100253 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200254 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200255 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100256 }
257
Michal Vasko12d809c2021-03-03 16:34:32 +0100258 /* remember parent count */
259 parents_count = xmlctx->elements.count;
260
Michal Vaskob36053d2020-03-26 15:49:30 +0100261 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100262 /* find key definition */
263 for (i = 0; i < key_set.count; ++i) {
264 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100265 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100266 break;
267 }
268 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100269 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100270
271 /* skip attributes */
272 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100273 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
274 assert(xmlctx->status == LYXML_ATTR_CONTENT);
275 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100276 }
277
Michal Vaskob36053d2020-03-26 15:49:30 +0100278 assert(xmlctx->status == LYXML_ELEM_CONTENT);
279 if (i < key_set.count) {
280 /* validate the value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200281 r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns);
Michal Vaskob36053d2020-03-26 15:49:30 +0100282 if (!r) {
283 /* key with a valid value, remove from the set */
284 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100285 }
286 }
287
Michal Vaskob36053d2020-03-26 15:49:30 +0100288 /* parser next */
289 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100290
Michal Vaskob36053d2020-03-26 15:49:30 +0100291 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100292 while (xmlctx->status == LYXML_ELEMENT) {
293 while (parents_count < xmlctx->elements.count) {
294 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
295 }
296 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100297 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
298 }
299
300 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
301 assert(xmlctx->status == LYXML_ELEM_CLOSE);
302 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
303 if (next != LYXML_ELEM_CLOSE) {
304 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
305 }
Michal Vasko44685da2020-03-17 15:38:06 +0100306 }
307
308 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100309 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100310 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100311 }
312
313cleanup:
314 ly_set_erase(&key_set, NULL);
315 return ret;
316}
317
Michal Vasko5c24ed12021-06-09 09:27:32 +0200318/**
319 * @brief Skip an element with all its descendants.
320 *
321 * @param[in] xmlctx XML parser context.
322 * @return LY_ERR value.
323 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100324static LY_ERR
325lydxml_data_skip(struct lyxml_ctx *xmlctx)
326{
327 uint32_t parents_count;
328
329 /* remember current number of parents */
330 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200331 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100332
333 /* skip after the content */
334 while (xmlctx->status != LYXML_ELEM_CONTENT) {
335 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
336 }
337 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
338
339 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200340 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100341 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
342 }
343
344 /* close element */
345 assert(xmlctx->status == LYXML_ELEM_CLOSE);
346 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
347
348 return LY_SUCCESS;
349}
350
Michal Vasko5c24ed12021-06-09 09:27:32 +0200351/**
352 * @brief Check that the current element can be parsed as a data node.
353 *
354 * @param[in] lydctx XML data parser context.
355 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
356 * @return LY_ERR value.
357 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100358static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200359lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100360{
361 LY_ERR ret = LY_SUCCESS;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200362 struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
Michal Vasko1bf09392020-03-27 12:38:10 +0100363
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100364 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
365 /* only checks specific to opaque nodes */
366 return LY_SUCCESS;
367 }
368
Michal Vasko13854662021-06-09 09:27:50 +0200369 if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
370 /* nothing to check */
371 return LY_SUCCESS;
372 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100373
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200374 assert(xmlctx->elements.count);
375
Michal Vasko13854662021-06-09 09:27:50 +0200376 /* backup parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200377 LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
Michal Vasko1bf09392020-03-27 12:38:10 +0100378
Michal Vasko13854662021-06-09 09:27:50 +0200379 /* skip attributes */
380 while (xmlctx->status == LYXML_ATTRIBUTE) {
381 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
382 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
383 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100384
Michal Vasko13854662021-06-09 09:27:50 +0200385 if ((*snode)->nodetype & LYD_NODE_TERM) {
386 /* value may not be valid in which case we parse it as an opaque node */
387 if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns)) {
Michal Vaskod0237d42021-07-12 14:49:46 +0200388 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, xmlctx->value_len,
389 xmlctx->value);
Michal Vasko13854662021-06-09 09:27:50 +0200390 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100391 }
Michal Vasko13854662021-06-09 09:27:50 +0200392 } else if ((*snode)->nodetype == LYS_LIST) {
393 /* skip content */
394 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
Michal Vasko1bf09392020-03-27 12:38:10 +0100395
Michal Vasko13854662021-06-09 09:27:50 +0200396 if (lydxml_check_list(xmlctx, *snode)) {
397 /* invalid list, parse as opaque if it missing/has invalid some keys */
Michal Vaskod0237d42021-07-12 14:49:46 +0200398 LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
Michal Vasko13854662021-06-09 09:27:50 +0200399 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100400 }
Michal Vasko13854662021-06-09 09:27:50 +0200401 } else {
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100402 /* if there is a non-WS value, it cannot be parsed as an inner node */
403 assert(xmlctx->status == LYXML_ELEM_CONTENT);
404 if (!xmlctx->ws_only) {
405 *snode = NULL;
406 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100407 }
408
Michal Vasko13854662021-06-09 09:27:50 +0200409restore:
410 /* restore parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200411 lyxml_ctx_restore(xmlctx, &pxmlctx);
Michal Vasko1bf09392020-03-27 12:38:10 +0100412 return ret;
413}
414
Radek Krejcie7b95092019-05-15 11:03:07 +0200415/**
Michal Vaskocea58712022-04-01 14:37:08 +0200416 * @brief Get sensible data hints for an opaque node.
417 *
418 * @param[in] name Node name.
419 * @param[in] name_len Length of @p name.
420 * @param[in] value Node value.
421 * @param[in] value_len Length of @p value.
422 * @param[in] first Node first sibling.
423 * @param[in] ns Node module namespace.
424 * @param[out] hints Data hints to use.
425 * @param[out] anchor Anchor to insert after in case of a list.
426 */
427static void
428lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, struct lyd_node *first,
429 const char *ns, uint32_t *hints, struct lyd_node **anchor)
430{
431 struct lyd_node_opaq *opaq;
432 char *ptr;
433 long num;
434
435 *hints = 0;
436 *anchor = NULL;
437
438 if (!value_len) {
439 /* no value */
440 *hints |= LYD_VALHINT_EMPTY;
441 } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
442 /* boolean value */
443 *hints |= LYD_VALHINT_BOOLEAN;
444 } else {
445 num = strtol(value, &ptr, 10);
446 if ((unsigned)(ptr - value) == value_len) {
447 /* number value */
448 *hints |= LYD_VALHINT_DECNUM;
449 if ((num < INT32_MIN) || (num > UINT32_MAX)) {
450 /* large number */
451 *hints |= LYD_VALHINT_NUM64;
452 }
453 } else {
454 /* string value */
455 *hints |= LYD_VALHINT_STRING;
456 }
457 }
458
459 if (!first) {
460 return;
461 }
462
463 /* search backwards to find the last instance */
464 do {
465 first = first->prev;
466 if (first->schema) {
467 continue;
468 }
469
470 opaq = (struct lyd_node_opaq *)first;
471 assert(opaq->format == LY_VALUE_XML);
472 if (!ly_strncmp(opaq->name.name, name, name_len) && !strcmp(opaq->name.module_ns, ns)) {
473 if (opaq->value && opaq->value[0]) {
474 /* leaf-list nodes */
475 opaq->hints |= LYD_NODEHINT_LEAFLIST;
476 *hints |= LYD_NODEHINT_LEAFLIST;
477 } else {
478 /* list nodes */
479 opaq->hints |= LYD_NODEHINT_LIST;
480 *hints |= LYD_NODEHINT_LIST;
481 }
482 *anchor = first;
483 break;
484 }
485 } while (first->prev->next);
486}
487
488/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200489 * @brief Get schema node for the current element.
Michal Vaskoddd76592022-01-17 13:34:48 +0100490 *
491 * @param[in] lydctx XML data parser context.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200492 * @param[in] parent Parsed parent data node, if any.
493 * @param[in] prefix Element prefix, if any.
494 * @param[in] prefix_len Length of @p prefix.
495 * @param[in] name Element name.
496 * @param[in] name_len Length of @p name.
497 * @param[out] snode Found schema node, NULL if no suitable was found.
498 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vaskoddd76592022-01-17 13:34:48 +0100499 * @return LY_SUCCESS on success;
Michal Vaskoddd76592022-01-17 13:34:48 +0100500 * @return LY_ERR on error.
501 */
502static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +0200503lydxml_subtree_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len,
504 const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100505{
506 LY_ERR r;
Michal Vaskob36053d2020-03-26 15:49:30 +0100507 struct lyxml_ctx *xmlctx;
508 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200509 const struct lyxml_ns *ns;
Radek Krejcie7b95092019-05-15 11:03:07 +0200510 struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200511 uint32_t getnext_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +0100512
Michal Vaskob36053d2020-03-26 15:49:30 +0100513 xmlctx = lydctx->xmlctx;
514 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100515 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100516
Michal Vasko8cc3f662022-03-29 11:25:51 +0200517 *snode = NULL;
518 *ext = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100519
Michal Vasko8cc3f662022-03-29 11:25:51 +0200520 /* get current namespace */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200521 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200522 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100523 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200524 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200525 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200526
527 /* get the element module, use parent context if possible because of extensions */
528 mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200529 if (!mod) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100530 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200531 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
532 snode, ext);
533 if (r != LY_ENOT) {
534 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100535 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100536 }
537
538 /* unknown module */
Michal Vaskoe0665742021-02-11 11:08:44 +0100539 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100540 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200541 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200542 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200543 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200544 }
545
Michal Vaskoa5da3292020-08-12 13:10:50 +0200546 /* get the schema node */
Michal Vasko81008a52021-07-21 16:06:12 +0200547 if (mod) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100548 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200549 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100550 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200551 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100552 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200553 if (!*snode) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100554 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200555 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
556 name_len, snode, ext);
557 if (r != LY_ENOT) {
558 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100559 return r;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100560 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100561
562 /* unknown data node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100563 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
564 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100565 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100566 (int)name_len, name, LYD_NAME(parent));
Radek Krejcif16e2542021-02-17 15:39:23 +0100567 } else if (lydctx->ext) {
568 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100569 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
570 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100571 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100572 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
573 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100574 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100575 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100576 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
577 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100578 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200579 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200580 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200581 return LY_SUCCESS;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200582 } else {
583 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200584 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
585 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200586 }
587 }
588
Michal Vasko8cc3f662022-03-29 11:25:51 +0200589 return LY_SUCCESS;
590}
591
592/**
593 * @brief Parse XML subtree.
594 *
595 * @param[in] lydctx XML YANG data parser context.
596 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
597 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings,
598 * this may point to a previously existing node.
599 * @param[in,out] parsed Optional set to add all the parsed siblings into.
600 * @return LY_ERR value.
601 */
602static LY_ERR
603lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
604{
605 LY_ERR ret = LY_SUCCESS;
606 const char *prefix, *name;
607 size_t prefix_len, name_len;
608 struct lyxml_ctx *xmlctx;
609 const struct ly_ctx *ctx;
610 const struct lyxml_ns *ns;
611 struct lyd_meta *meta = NULL;
612 struct lyd_attr *attr = NULL;
613 const struct lysc_node *snode;
614 struct lysc_ext_instance *ext;
Michal Vaskocea58712022-04-01 14:37:08 +0200615 uint32_t prev_parse_opts, orig_parse_opts, prev_int_opts, hints;
616 struct lyd_node *node = NULL, *anchor, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200617 void *val_prefix_data = NULL;
618 LY_VALUE_FORMAT format;
619 ly_bool parse_subtree;
620 char *val;
621
622 assert(parent || first_p);
623
624 xmlctx = lydctx->xmlctx;
625 ctx = xmlctx->ctx;
626
627 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
628 /* all descendants should be parsed */
629 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
630 orig_parse_opts = lydctx->parse_opts;
631
632 assert(xmlctx->status == LYXML_ELEMENT);
633
634 /* remember element prefix and name */
635 prefix = xmlctx->prefix;
636 prefix_len = xmlctx->prefix_len;
637 name = xmlctx->name;
638 name_len = xmlctx->name_len;
639
640 /* parser next */
641 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
642
643 /* get the schema node */
644 LY_CHECK_GOTO(ret = lydxml_subtree_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext), error);
645
646 if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
647 LOGVRB("Skipping parsing of unknown node \"%.*s\".", name_len, name);
648
649 /* skip element with children */
650 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
651 return LY_SUCCESS;
652 }
653
Michal Vaskoa5da3292020-08-12 13:10:50 +0200654 /* create metadata/attributes */
655 if (xmlctx->status == LYXML_ATTRIBUTE) {
656 if (snode) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100657 ret = lydxml_metadata(lydctx, snode, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200658 LY_CHECK_GOTO(ret, error);
659 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100660 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200661 ret = lydxml_attrs(xmlctx, &attr);
662 LY_CHECK_GOTO(ret, error);
663 }
664 }
665
666 assert(xmlctx->status == LYXML_ELEM_CONTENT);
667 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100668 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200669
670 if (xmlctx->ws_only) {
671 /* ignore WS-only value */
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100672 if (xmlctx->dynamic) {
Michal Vaskocea58712022-04-01 14:37:08 +0200673 free((char *)xmlctx->value);
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100674 }
675 xmlctx->dynamic = 0;
676 xmlctx->value = "";
Michal Vaskoa5da3292020-08-12 13:10:50 +0200677 xmlctx->value_len = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200678 format = LY_VALUE_XML;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200679 } else {
680 /* get value prefixes */
Radek Krejci8df109d2021-04-23 12:19:08 +0200681 ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100682 &xmlctx->ns, &format, &val_prefix_data);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200683 LY_CHECK_GOTO(ret, error);
684 }
685
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200686 /* get NS again, it may have been backed up and restored */
687 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
688 assert(ns);
689
Michal Vaskocea58712022-04-01 14:37:08 +0200690 /* get best-effort node hints */
691 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, parent ? lyd_child(parent) : *first_p,
692 ns->uri, &hints, &insert_anchor);
693
Michal Vaskoa5da3292020-08-12 13:10:50 +0200694 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100695 ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), xmlctx->value,
Michal Vaskocea58712022-04-01 14:37:08 +0200696 xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, hints, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200697 LY_CHECK_GOTO(ret, error);
698
699 /* parser next */
700 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
701
702 /* process children */
703 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100704 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200705 LY_CHECK_GOTO(ret, error);
706 }
707 } else if (snode->nodetype & LYD_NODE_TERM) {
708 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200709 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 +0200710 &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error);
Radek Krejciddace2c2021-01-08 11:30:56 +0100711 LOG_LOCSET(snode, node, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200712
713 if (parent && (node->schema->flags & LYS_KEY)) {
714 /* check the key order, the anchor must never be a key */
Michal Vaskoe0665742021-02-11 11:08:44 +0100715 anchor = lyd_insert_get_next_anchor(lyd_child(parent), node);
Michal Vasko441e0fa2022-03-03 13:45:00 +0100716 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100717 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100718 LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200719 ret = LY_EVALID;
720 goto error;
721 } else {
722 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name);
723 }
724 }
725 }
726
727 /* parser next */
728 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
729
730 /* no children expected */
731 if (xmlctx->status == LYXML_ELEMENT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100732 LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100733 (int)xmlctx->name_len, xmlctx->name, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200734 ret = LY_EVALID;
735 goto error;
736 }
737 } else if (snode->nodetype & LYD_NODE_INNER) {
738 if (!xmlctx->ws_only) {
739 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100740 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100741 (int)xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200742 ret = LY_EVALID;
743 goto error;
744 }
745
746 /* create node */
747 ret = lyd_create_inner(snode, &node);
748 LY_CHECK_GOTO(ret, error);
749
Radek Krejciddace2c2021-01-08 11:30:56 +0100750 LOG_LOCSET(snode, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100751
Michal Vaskoa5da3292020-08-12 13:10:50 +0200752 /* parser next */
753 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
754
Michal Vasko8cc3f662022-03-29 11:25:51 +0200755 prev_parse_opts = lydctx->parse_opts;
756 if (ext) {
757 /* only parse these extension data and validate afterwards */
758 lydctx->parse_opts |= LYD_PARSE_ONLY;
759 }
760
Michal Vaskoa5da3292020-08-12 13:10:50 +0200761 /* process children */
762 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100763 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200764 LY_CHECK_GOTO(ret, error);
765 }
766
Michal Vasko8cc3f662022-03-29 11:25:51 +0200767 /* restore options */
768 lydctx->parse_opts = prev_parse_opts;
769
Michal Vaskoa5da3292020-08-12 13:10:50 +0200770 if (snode->nodetype == LYS_LIST) {
771 /* check all keys exist */
772 LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error);
773 }
774
Michal Vaskoe0665742021-02-11 11:08:44 +0100775 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200776 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100777 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200778 LY_CHECK_GOTO(ret, error);
779
780 /* add any missing default children */
Michal Vaskoddd76592022-01-17 13:34:48 +0100781 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
782 (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200783 LY_CHECK_GOTO(ret, error);
784 }
785
786 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
787 /* rememeber the RPC/action/notification */
788 lydctx->op_node = node;
789 }
790 } else if (snode->nodetype & LYD_NODE_ANY) {
Michal Vasko27c4dce2021-03-04 15:50:50 +0100791 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
792 /* value in anydata node, we expect a tree */
793 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100794 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200795 ret = LY_EVALID;
796 goto error;
797 }
798
Michal Vasko27c4dce2021-03-04 15:50:50 +0100799 if (!xmlctx->ws_only) {
800 /* use an arbitrary text value for anyxml */
Michal Vasko742a5b12022-02-24 16:07:27 +0100801 val = strndup(xmlctx->value, xmlctx->value_len);
802 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); ret = LY_EMEM, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200803
Michal Vasko27c4dce2021-03-04 15:50:50 +0100804 /* parser next */
Michal Vaskoa04107f2022-03-29 12:09:50 +0200805 LY_CHECK_ERR_GOTO(ret = lyxml_ctx_next(xmlctx), free(val), error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100806
807 /* create node */
808 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, &node);
Michal Vasko742a5b12022-02-24 16:07:27 +0100809 LY_CHECK_ERR_GOTO(ret, free(val), error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100810 } else {
811 /* parser next */
812 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
813
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200814 /* update options so that generic data can be parsed */
815 prev_parse_opts = lydctx->parse_opts;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100816 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200817 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200818 prev_int_opts = lydctx->int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +0200819 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200820
821 /* parse any data tree */
Michal Vasko27c4dce2021-03-04 15:50:50 +0100822 anchor = NULL;
823 while (xmlctx->status == LYXML_ELEMENT) {
824 ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200825 if (ret) {
Michal Vaskocc53c582022-04-01 12:07:44 +0200826 lyd_free_siblings(anchor);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200827 break;
828 }
Michal Vasko27c4dce2021-03-04 15:50:50 +0100829 }
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200830
831 /* restore options */
832 lydctx->parse_opts = prev_parse_opts;
833 lydctx->int_opts = prev_int_opts;
834
835 LY_CHECK_GOTO(ret, error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100836
837 /* create node */
838 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
839 LY_CHECK_GOTO(ret, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200840 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200841 }
842 assert(node);
843
Michal Vaskoa5da3292020-08-12 13:10:50 +0200844 if (snode) {
Michal Vasko135719f2022-08-25 12:18:17 +0200845 /* add/correct flags */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200846 LY_CHECK_GOTO(ret = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext), error);
Michal Vasko135719f2022-08-25 12:18:17 +0200847
848 /* store for ext instance node validation, if needed */
849 LY_CHECK_GOTO(ret = lyd_validate_node_ext(node, &lydctx->ext_node), error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200850 }
851
852 /* parser next */
853 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +0100854 if (!parse_subtree) {
855 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
856 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200857
858 /* add metadata/attributes */
859 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +0100860 lyd_insert_meta(node, meta, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200861 } else {
862 lyd_insert_attr(node, attr);
863 }
864
865 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +0200866 if (insert_anchor) {
867 lyd_insert_after(insert_anchor, node);
868 } else if (ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200869 LY_CHECK_GOTO(ret = lyd_insert_ext(parent, node), error);
870 } else {
871 lyd_insert_node(parent, first_p, node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
872 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200873 while (!parent && (*first_p)->prev->next) {
874 *first_p = (*first_p)->prev;
875 }
876
Michal Vaskoe0665742021-02-11 11:08:44 +0100877 /* rememeber a successfully parsed node */
878 if (parsed) {
879 ly_set_add(parsed, node, 1, NULL);
880 }
881
Michal Vasko8cc3f662022-03-29 11:25:51 +0200882 lydctx->parse_opts = orig_parse_opts;
Radek Krejciddace2c2021-01-08 11:30:56 +0100883 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200884 return LY_SUCCESS;
885
886error:
Michal Vasko8cc3f662022-03-29 11:25:51 +0200887 lydctx->parse_opts = orig_parse_opts;
Radek Krejciddace2c2021-01-08 11:30:56 +0100888 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200889 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200890 lyd_free_attr_siblings(ctx, attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200891 lyd_free_tree(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200892 return ret;
893}
894
Michal Vaskoe0665742021-02-11 11:08:44 +0100895/**
896 * @brief Parse a specific XML element into an opaque node.
897 *
898 * @param[in] xmlctx XML parser context.
899 * @param[in] name Name of the element.
900 * @param[in] uri URI of the element.
901 * @param[in] value Whether a value is expected in the element.
902 * @param[out] evnp Parsed envelope (opaque node).
903 * @return LY_SUCCESS on success.
904 * @return LY_ENOT if the specified element did not match.
905 * @return LY_ERR value on error.
906 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100907static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100908lydxml_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 +0100909{
Michal Vaskoe0665742021-02-11 11:08:44 +0100910 LY_ERR rc = LY_SUCCESS;
911 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +0200912 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100913 const char *prefix;
914 size_t prefix_len;
915
Michal Vasko1bf09392020-03-27 12:38:10 +0100916 assert(xmlctx->status == LYXML_ELEMENT);
917 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
918 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +0100919 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100920 }
921
922 prefix = xmlctx->prefix;
923 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200924 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100925 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100926 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100927 return LY_EVALID;
928 } else if (strcmp(ns->uri, uri)) {
929 /* different namespace */
930 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100931 }
932
933 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
934
935 /* create attributes */
936 if (xmlctx->status == LYXML_ATTRIBUTE) {
937 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
938 }
939
Michal Vaskoe0665742021-02-11 11:08:44 +0100940 assert(xmlctx->status == LYXML_ELEM_CONTENT);
941 if (!value && !xmlctx->ws_only) {
942 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +0100943 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100944 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100945 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100946 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100947
948 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100949 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200950 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100951 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100952
953 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +0100954 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100955 attr = NULL;
956
Michal Vaskoe0665742021-02-11 11:08:44 +0100957 /* parser next element */
958 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100959
Michal Vaskoe0665742021-02-11 11:08:44 +0100960cleanup:
961 lyd_free_attr_siblings(xmlctx->ctx, attr);
962 if (rc) {
963 lyd_free_tree(*envp);
964 *envp = NULL;
965 }
966 return rc;
967}
968
969/**
970 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
971 *
972 * @param[in] xmlctx XML parser context.
973 * @param[out] evnp Parsed envelope(s) (opaque node).
974 * @param[out] int_opts Internal options for parsing the rest of YANG data.
975 * @param[out] close_elem Number of parsed opened elements that need to be closed.
976 * @return LY_SUCCESS on success.
977 * @return LY_ERR value on error.
978 */
979static LY_ERR
980lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
981{
982 LY_ERR rc = LY_SUCCESS, r;
983 struct lyd_node *child;
984
985 assert(envp && !*envp);
986
Michal Vasko50375272022-04-26 12:07:46 +0200987 if (xmlctx->status != LYXML_ELEMENT) {
988 /* nothing to parse */
989 assert(xmlctx->status == LYXML_END);
990 goto cleanup;
991 }
992
Michal Vaskoe0665742021-02-11 11:08:44 +0100993 /* parse "rpc" */
994 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100995 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
996
997 /* parse "action", if any */
998 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
999 if (r == LY_SUCCESS) {
1000 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001001 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001002
1003 /* NETCONF action */
1004 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1005 *close_elem = 2;
1006 } else if (r == LY_ENOT) {
1007 /* NETCONF RPC */
1008 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1009 *close_elem = 1;
1010 } else {
1011 rc = r;
1012 goto cleanup;
1013 }
1014
1015cleanup:
1016 if (rc) {
1017 lyd_free_tree(*envp);
1018 *envp = NULL;
1019 }
1020 return rc;
1021}
1022
1023/**
Michal Vasko51de7b72022-04-29 09:50:22 +02001024 * @brief Validate eventTime date-and-time value.
1025 *
1026 * @param[in] node Opaque eventTime node.
1027 * @return LY_SUCCESS on success.
1028 * @return LY_ERR value on error.
1029 */
1030static LY_ERR
1031lydxml_env_netconf_eventtime_validate(const struct lyd_node *node)
1032{
1033 LY_ERR rc = LY_SUCCESS;
1034 struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
1035 struct lysc_ctx cctx = {.ctx = ctx};
1036 const struct lys_module *mod;
1037 LY_ARRAY_COUNT_TYPE u;
1038 struct ly_err_item *err = NULL;
Michal Vaskoab36a7e2022-04-29 10:25:56 +02001039 struct lysp_type *type_p = NULL;
Michal Vasko51de7b72022-04-29 09:50:22 +02001040 struct lysc_pattern **patterns = NULL;
1041 const char *value;
1042
1043 /* get date-and-time parsed type */
1044 mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
Michal Vaskoab36a7e2022-04-29 10:25:56 +02001045 assert(mod);
Michal Vasko51de7b72022-04-29 09:50:22 +02001046 LY_ARRAY_FOR(mod->parsed->typedefs, u) {
1047 if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) {
1048 type_p = &mod->parsed->typedefs[u].type;
1049 break;
1050 }
1051 }
1052 assert(type_p);
1053
1054 /* compile patterns */
1055 assert(type_p->patterns);
1056 LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
1057
1058 /* validate */
1059 value = lyd_get_value(node);
1060 rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
1061
1062cleanup:
1063 FREE_ARRAY(ctx, patterns, lysc_pattern_free);
1064 if (rc && err) {
1065 LOGVAL_ERRITEM(ctx, err);
1066 ly_err_free(err);
1067 LOGVAL(ctx, LYVE_DATA, "Invalid \"eventTime\" in the notification.");
1068 }
1069 return rc;
1070}
1071
1072/**
Michal Vaskoe0665742021-02-11 11:08:44 +01001073 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
1074 *
1075 * @param[in] xmlctx XML parser context.
1076 * @param[out] evnp Parsed envelope(s) (opaque node).
1077 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1078 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1079 * @return LY_SUCCESS on success.
1080 * @return LY_ERR value on error.
1081 */
1082static LY_ERR
1083lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1084{
1085 LY_ERR rc = LY_SUCCESS, r;
1086 struct lyd_node *child;
1087
1088 assert(envp && !*envp);
1089
Michal Vasko50375272022-04-26 12:07:46 +02001090 if (xmlctx->status != LYXML_ELEMENT) {
1091 /* nothing to parse */
1092 assert(xmlctx->status == LYXML_END);
1093 goto cleanup;
1094 }
1095
Michal Vaskoe0665742021-02-11 11:08:44 +01001096 /* parse "notification" */
1097 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001098 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1099
1100 /* parse "eventTime" */
1101 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
1102 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +01001103 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
1104 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001105 r = LY_EVALID;
1106 }
1107 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1108
1109 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001110 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001111
1112 /* validate value */
Michal Vasko51de7b72022-04-29 09:50:22 +02001113 r = lydxml_env_netconf_eventtime_validate(child);
1114 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001115
1116 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +01001117 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1118 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001119 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001120 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001121 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001122 goto cleanup;
1123 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001124 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1125
1126 /* NETCONF notification */
1127 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
1128 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001129
1130cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001131 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +01001132 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001133 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001134 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001135 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001136}
Michal Vasko79135ae2020-12-16 10:08:35 +01001137
Michal Vaskoe0665742021-02-11 11:08:44 +01001138/**
1139 * @brief Parse an XML element as an opaque node subtree.
1140 *
1141 * @param[in] xmlctx XML parser context.
1142 * @param[in] parent Parent to append nodes to.
1143 * @return LY_ERR value.
1144 */
1145static LY_ERR
1146lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001147{
Michal Vaskoe0665742021-02-11 11:08:44 +01001148 LY_ERR rc = LY_SUCCESS;
1149 const struct lyxml_ns *ns;
1150 struct lyd_attr *attr = NULL;
1151 struct lyd_node *child = NULL;
1152 const char *name, *prefix;
1153 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001154
Michal Vaskoe0665742021-02-11 11:08:44 +01001155 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001156
Michal Vaskoe0665742021-02-11 11:08:44 +01001157 name = xmlctx->name;
1158 name_len = xmlctx->name_len;
1159 prefix = xmlctx->prefix;
1160 prefix_len = xmlctx->prefix_len;
1161 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1162 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001163 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001164 return LY_EVALID;
1165 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001166
Michal Vaskoe0665742021-02-11 11:08:44 +01001167 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001168
Michal Vaskoe0665742021-02-11 11:08:44 +01001169 /* create attributes */
1170 if (xmlctx->status == LYXML_ATTRIBUTE) {
1171 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1172 }
1173
1174 /* create node */
1175 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1176 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 +02001177 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +01001178 LY_CHECK_GOTO(rc, cleanup);
1179
1180 /* assign atributes */
1181 ((struct lyd_node_opaq *)child)->attr = attr;
1182 attr = NULL;
1183
1184 /* parser next element */
1185 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1186
1187 /* parse all the descendants */
1188 while (xmlctx->status == LYXML_ELEMENT) {
1189 rc = lydxml_opaq_r(xmlctx, child);
1190 LY_CHECK_GOTO(rc, cleanup);
1191 }
1192
1193 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001194 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001195
1196cleanup:
1197 lyd_free_attr_siblings(xmlctx->ctx, attr);
1198 if (rc) {
1199 lyd_free_tree(child);
1200 }
1201 return rc;
1202}
1203
1204/**
1205 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1206 *
1207 * @param[in] xmlctx XML parser context.
1208 * @param[in] parent Parent to append nodes to.
1209 * @return LY_ERR value.
1210 */
1211static LY_ERR
1212lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1213{
1214 LY_ERR r;
1215 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001216 ly_bool no_dup;
1217
1218 /* there must be some child */
1219 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1220 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1221 return LY_EVALID;
1222 }
1223
1224 while (xmlctx->status == LYXML_ELEMENT) {
1225 child = NULL;
1226
1227 /*
1228 * session-id
1229 */
1230 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1231 if (r == LY_SUCCESS) {
1232 no_dup = 1;
1233 goto check_child;
1234 } else if (r != LY_ENOT) {
1235 goto error;
1236 }
1237
1238 /*
1239 * bad-attribute
1240 */
1241 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1242 if (r == LY_SUCCESS) {
1243 no_dup = 1;
1244 goto check_child;
1245 } else if (r != LY_ENOT) {
1246 goto error;
1247 }
1248
1249 /*
1250 * bad-element
1251 */
1252 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1253 if (r == LY_SUCCESS) {
1254 no_dup = 1;
1255 goto check_child;
1256 } else if (r != LY_ENOT) {
1257 goto error;
1258 }
1259
1260 /*
1261 * bad-namespace
1262 */
1263 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1264 if (r == LY_SUCCESS) {
1265 no_dup = 1;
1266 goto check_child;
1267 } else if (r != LY_ENOT) {
1268 goto error;
1269 }
1270
1271 if (r == LY_ENOT) {
1272 assert(xmlctx->status == LYXML_ELEMENT);
1273
Michal Vasko845ba392022-06-16 07:52:13 +02001274 /* custom elements, parse all the siblings */
1275 while (xmlctx->status == LYXML_ELEMENT) {
1276 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1277 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1278 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001279 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001280 }
1281
1282check_child:
1283 /* check for duplicates */
1284 if (no_dup) {
1285 LY_LIST_FOR(lyd_child(parent), iter) {
1286 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1287 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1288 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1289 ((struct lyd_node_opaq *)child)->name.name);
1290 r = LY_EVALID;
1291 goto error;
1292 }
1293 }
1294 }
1295
1296 /* finish child parsing */
1297 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1298 assert(xmlctx->status == LYXML_ELEMENT);
1299 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001300 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001301 r = LY_EVALID;
1302 goto error;
1303 }
1304 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1305
1306 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001307 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001308 }
1309
1310 return LY_SUCCESS;
1311
1312error:
1313 lyd_free_tree(child);
1314 return r;
1315}
1316
1317/**
1318 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1319 *
1320 * @param[in] xmlctx XML parser context.
1321 * @param[in] parent Parent to append nodes to.
1322 * @return LY_ERR value.
1323 */
1324static LY_ERR
1325lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1326{
1327 LY_ERR r;
1328 struct lyd_node *child, *iter;
1329 const char *val;
1330 ly_bool no_dup;
1331
1332 /* there must be some child */
1333 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1334 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1335 return LY_EVALID;
1336 }
1337
1338 while (xmlctx->status == LYXML_ELEMENT) {
1339 child = NULL;
1340
1341 /*
1342 * error-type
1343 */
1344 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1345 if (r == LY_SUCCESS) {
1346 val = ((struct lyd_node_opaq *)child)->value;
1347 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1348 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1349 ((struct lyd_node_opaq *)child)->name.name);
1350 r = LY_EVALID;
1351 goto error;
1352 }
1353
1354 no_dup = 1;
1355 goto check_child;
1356 } else if (r != LY_ENOT) {
1357 goto error;
1358 }
1359
1360 /*
1361 * error-tag
1362 */
1363 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1364 if (r == LY_SUCCESS) {
1365 val = ((struct lyd_node_opaq *)child)->value;
1366 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1367 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1368 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1369 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1370 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1371 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1372 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1373 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1374 ((struct lyd_node_opaq *)child)->name.name);
1375 r = LY_EVALID;
1376 goto error;
1377 }
1378
1379 no_dup = 1;
1380 goto check_child;
1381 } else if (r != LY_ENOT) {
1382 goto error;
1383 }
1384
1385 /*
1386 * error-severity
1387 */
1388 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1389 if (r == LY_SUCCESS) {
1390 val = ((struct lyd_node_opaq *)child)->value;
1391 if (strcmp(val, "error") && strcmp(val, "warning")) {
1392 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1393 ((struct lyd_node_opaq *)child)->name.name);
1394 r = LY_EVALID;
1395 goto error;
1396 }
1397
1398 no_dup = 1;
1399 goto check_child;
1400 } else if (r != LY_ENOT) {
1401 goto error;
1402 }
1403
1404 /*
1405 * error-app-tag
1406 */
1407 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1408 if (r == LY_SUCCESS) {
1409 no_dup = 1;
1410 goto check_child;
1411 } else if (r != LY_ENOT) {
1412 goto error;
1413 }
1414
1415 /*
1416 * error-path
1417 */
1418 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1419 if (r == LY_SUCCESS) {
1420 no_dup = 1;
1421 goto check_child;
1422 } else if (r != LY_ENOT) {
1423 goto error;
1424 }
1425
1426 /*
1427 * error-message
1428 */
1429 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1430 if (r == LY_SUCCESS) {
1431 no_dup = 1;
1432 goto check_child;
1433 } else if (r != LY_ENOT) {
1434 goto error;
1435 }
1436
1437 /* error-info */
1438 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1439 if (r == LY_SUCCESS) {
1440 /* parse all the descendants */
1441 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1442
1443 no_dup = 0;
1444 goto check_child;
1445 } else if (r != LY_ENOT) {
1446 goto error;
1447 }
1448
1449 if (r == LY_ENOT) {
1450 assert(xmlctx->status == LYXML_ELEMENT);
1451 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001452 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001453 r = LY_EVALID;
1454 goto error;
1455 }
1456
1457check_child:
1458 /* check for duplicates */
1459 if (no_dup) {
1460 LY_LIST_FOR(lyd_child(parent), iter) {
1461 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1462 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1463 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1464 ((struct lyd_node_opaq *)child)->name.name);
1465 r = LY_EVALID;
1466 goto error;
1467 }
1468 }
1469 }
1470
1471 /* finish child parsing */
1472 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1473 assert(xmlctx->status == LYXML_ELEMENT);
1474 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001475 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001476 r = LY_EVALID;
1477 goto error;
1478 }
1479 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1480
1481 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001482 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001483 }
1484
1485 return LY_SUCCESS;
1486
1487error:
1488 lyd_free_tree(child);
1489 return r;
1490}
1491
1492/**
1493 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1494 *
1495 * @param[in] xmlctx XML parser context.
1496 * @param[out] evnp Parsed envelope(s) (opaque node).
1497 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1498 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1499 * @return LY_SUCCESS on success.
1500 * @return LY_ERR value on error.
1501 */
1502static LY_ERR
1503lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1504{
1505 LY_ERR rc = LY_SUCCESS, r;
1506 struct lyd_node *child = NULL;
1507 const char *parsed_elem = NULL;
1508
1509 assert(envp && !*envp);
1510
Michal Vasko50375272022-04-26 12:07:46 +02001511 if (xmlctx->status != LYXML_ELEMENT) {
1512 /* nothing to parse */
1513 assert(xmlctx->status == LYXML_END);
1514 goto cleanup;
1515 }
1516
Michal Vaskoe0665742021-02-11 11:08:44 +01001517 /* parse "rpc-reply" */
1518 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001519 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1520
1521 /* there must be some child */
1522 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1523 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1524 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001525 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001526 }
1527
Michal Vaskoe0665742021-02-11 11:08:44 +01001528 /* try to parse "ok" */
1529 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1530 if (r == LY_SUCCESS) {
1531 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001532 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001533
1534 /* finish child parsing */
1535 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1536 assert(xmlctx->status == LYXML_ELEMENT);
1537 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001538 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001539 rc = LY_EVALID;
1540 goto cleanup;
1541 }
1542 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1543
1544 /* success */
1545 parsed_elem = "ok";
1546 goto finish;
1547 } else if (r != LY_ENOT) {
1548 rc = r;
1549 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001550 }
1551
Michal Vaskoe0665742021-02-11 11:08:44 +01001552 /* try to parse all "rpc-error" elements */
1553 while (xmlctx->status == LYXML_ELEMENT) {
1554 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1555 if (r == LY_ENOT) {
1556 break;
1557 } else if (r) {
1558 rc = r;
1559 goto cleanup;
1560 }
1561
1562 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001563 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001564
1565 /* parse all children of "rpc-error" */
1566 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1567
1568 /* finish child parsing */
1569 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1570 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1571
1572 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001573 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001574
1575finish:
1576 if (parsed_elem) {
1577 /* NETCONF rpc-reply with no data */
1578 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1579 assert(xmlctx->status == LYXML_ELEMENT);
1580 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001581 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001582 rc = LY_EVALID;
1583 goto cleanup;
1584 }
1585 }
1586
1587 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001588 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001589 *close_elem = 1;
1590
1591cleanup:
1592 if (rc) {
1593 lyd_free_tree(*envp);
1594 *envp = NULL;
1595 }
1596 return rc;
1597}
1598
Michal Vasko2552ea32020-12-08 15:32:34 +01001599LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001600lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1601 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoddd76592022-01-17 13:34:48 +01001602 struct lyd_node **envp, struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001603{
Michal Vaskoe0665742021-02-11 11:08:44 +01001604 LY_ERR rc = LY_SUCCESS;
1605 struct lyd_xml_ctx *lydctx;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001606 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001607 ly_bool parsed_data_nodes = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01001608 enum LYXML_PARSER_STATUS status;
Michal Vasko2552ea32020-12-08 15:32:34 +01001609
Michal Vaskoe0665742021-02-11 11:08:44 +01001610 assert(ctx && in && lydctx_p);
1611 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1612 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001613
Michal Vaskoe0665742021-02-11 11:08:44 +01001614 /* init context */
1615 lydctx = calloc(1, sizeof *lydctx);
1616 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1617 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1618 lydctx->parse_opts = parse_opts;
1619 lydctx->val_opts = val_opts;
1620 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001621 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001622
Michal Vaskoe0665742021-02-11 11:08:44 +01001623 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001624 case LYD_TYPE_DATA_YANG:
Michal Vaskoddd76592022-01-17 13:34:48 +01001625 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
1626 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1627 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001628 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001629 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001630 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1631 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001632 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001633 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1634 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001635 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001636 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1637 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001638 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001639 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001640 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1641 if (rc == LY_ENOT) {
1642 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1643 }
1644 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001645 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001646 case LYD_TYPE_NOTIF_NETCONF:
1647 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001648 rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem);
1649 if (rc == LY_ENOT) {
1650 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1651 }
1652 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001653 break;
1654 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001655 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001656 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1657 if (rc == LY_ENOT) {
1658 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1659 }
1660 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001661 break;
1662 }
1663 lydctx->int_opts = int_opts;
1664
1665 /* find the operation node if it exists already */
1666 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1667
1668 /* parse XML data */
1669 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1670 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1671 parsed_data_nodes = 1;
1672
1673 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1674 break;
1675 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001676 }
1677
Michal Vaskoe0665742021-02-11 11:08:44 +01001678 /* close all opened elements */
1679 for (i = 0; i < close_elem; ++i) {
1680 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1681 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001682 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1683 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001684 rc = LY_EVALID;
1685 goto cleanup;
1686 }
1687
1688 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001689 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001690
1691 /* check final state */
1692 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1693 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1694 rc = LY_EVALID;
1695 goto cleanup;
1696 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01001697 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001698 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1699 rc = LY_EVALID;
1700 goto cleanup;
1701 }
1702
1703 if (!parsed_data_nodes) {
1704 /* no data nodes were parsed */
1705 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001706 }
1707
Michal Vaskoddd76592022-01-17 13:34:48 +01001708 if (parse_opts & LYD_PARSE_SUBTREE) {
1709 /* check for a sibling element */
1710 assert(subtree_sibling);
1711 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1712 *subtree_sibling = 1;
1713 } else {
1714 *subtree_sibling = 0;
1715 }
1716 }
1717
Michal Vaskoa8edff02020-03-27 14:47:01 +01001718cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001719 /* there should be no unres stored if validation should be skipped */
1720 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01001721 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001722
1723 if (rc) {
1724 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1725 } else {
1726 *lydctx_p = (struct lyd_ctx *)lydctx;
1727
1728 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1729 lyxml_ctx_free(lydctx->xmlctx);
1730 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001731 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001732 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001733}