blob: 5a929da34fe03c1e07fed6fd7bf35054640496ac [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 Vasko1b2a3f42022-12-20 09:38:28 +010076 if (!strcmp(sparent->module->name, "notifications")) {
77 /* ancient module that does not even use the extension */
78 filter_attrs = 1;
79 } else {
80 LY_ARRAY_FOR(sparent->exts, u) {
81 if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") &&
82 !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) {
83 filter_attrs = 1;
84 break;
85 }
Michal Vasko45791ad2021-06-17 08:45:03 +020086 }
87 }
88
Michal Vaskob36053d2020-03-26 15:49:30 +010089 while (xmlctx->status == LYXML_ATTRIBUTE) {
90 if (!xmlctx->prefix_len) {
Michal Vasko45791ad2021-06-17 08:45:03 +020091 /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
92 if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
93 !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
94 mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
95 if (!mod) {
96 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
97 "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
98 ret = LY_ENOTFOUND;
99 goto cleanup;
100 }
101 goto create_meta;
102 }
103
Michal Vaskoe0665742021-02-11 11:08:44 +0100104 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100105 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100106 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200107 ret = LY_EVALID;
Michal Vaskob36053d2020-03-26 15:49:30 +0100108 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200109 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100110
Michal Vasko45791ad2021-06-17 08:45:03 +0200111 /* skip attr */
Michal Vaskob36053d2020-03-26 15:49:30 +0100112 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
113 assert(xmlctx->status == LYXML_ATTR_CONTENT);
114 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200115 continue;
116 }
117
118 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200119 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200120 if (!ns) {
Michal Vasko52927e22020-03-16 17:26:14 +0100121 /* unknown namespace, XML error */
Radek Krejci422afb12021-03-04 16:38:16 +0100122 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko45791ad2021-06-17 08:45:03 +0200123 ret = LY_ENOTFOUND;
Radek Krejci28681fa2019-09-06 13:08:45 +0200124 goto cleanup;
125 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200126
127 /* get the module with metadata definition */
Michal Vasko52927e22020-03-16 17:26:14 +0100128 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200129 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100130 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100131 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200132 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100133 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
134 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200135 ret = LY_ENOTFOUND;
Michal Vaskob36053d2020-03-26 15:49:30 +0100136 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200137 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200138
139 /* skip attr */
140 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
141 assert(xmlctx->status == LYXML_ATTR_CONTENT);
142 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
143 continue;
Radek Krejci28681fa2019-09-06 13:08:45 +0200144 }
145
Michal Vasko45791ad2021-06-17 08:45:03 +0200146create_meta:
Michal Vasko60ea6352020-06-29 13:39:39 +0200147 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100148 name = xmlctx->name;
149 name_len = xmlctx->name_len;
150 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
151 assert(xmlctx->status == LYXML_ATTR_CONTENT);
152
153 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200154 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100155 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200156 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100157
158 /* next attribute */
159 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200160 }
Michal Vasko52927e22020-03-16 17:26:14 +0100161
Radek Krejcie7b95092019-05-15 11:03:07 +0200162cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100163 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200164 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200166 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200167 return ret;
168}
169
Michal Vasko52927e22020-03-16 17:26:14 +0100170static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200171lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100172{
173 LY_ERR ret = LY_SUCCESS;
174 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100175 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200176 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100178 const char *name, *prefix;
179 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100180
181 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100182 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100183
Michal Vaskob36053d2020-03-26 15:49:30 +0100184 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100185 if (*attr) {
186 attr2 = *attr;
187 } else {
188 attr2 = NULL;
189 }
190
Michal Vaskob36053d2020-03-26 15:49:30 +0100191 /* remember attr prefix, name, and get its content */
192 prefix = xmlctx->prefix;
193 prefix_len = xmlctx->prefix_len;
194 name = xmlctx->name;
195 name_len = xmlctx->name_len;
196 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
197 assert(xmlctx->status == LYXML_ATTR_CONTENT);
198
Michal Vaskoe137fc42021-07-22 11:53:13 +0200199 /* handle special "xml" attribute prefix */
200 if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
201 name = prefix;
202 name_len += 1 + prefix_len;
203 prefix = NULL;
204 prefix_len = 0;
205 }
206
207 /* find namespace of the attribute, if any */
208 ns = NULL;
209 if (prefix_len) {
210 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
211 if (!ns) {
212 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
213 ret = LY_EVALID;
214 goto cleanup;
215 }
216 }
217
Michal Vasko52927e22020-03-16 17:26:14 +0100218 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100219 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200220 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 +0100221 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100222
223 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100224 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 +0100225 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data,
226 LYD_HINT_DATA);
Michal Vasko52927e22020-03-16 17:26:14 +0100227 LY_CHECK_GOTO(ret, cleanup);
228
229 if (!*attr) {
230 *attr = attr2;
231 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100232
233 /* next attribute */
234 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100235 }
236
237cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100238 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200239 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100240 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100241 }
Michal Vasko52927e22020-03-16 17:26:14 +0100242 return ret;
243}
244
Michal Vasko44685da2020-03-17 15:38:06 +0100245static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100246lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100247{
Michal Vaskob36053d2020-03-26 15:49:30 +0100248 LY_ERR ret = LY_SUCCESS, r;
249 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100250 struct ly_set key_set = {0};
251 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100252 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100253
254 assert(list && (list->nodetype == LYS_LIST));
255
256 /* get all keys into a set (keys do not have if-features or anything) */
257 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100258 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200259 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200260 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100261 }
262
Michal Vasko12d809c2021-03-03 16:34:32 +0100263 /* remember parent count */
264 parents_count = xmlctx->elements.count;
265
Michal Vaskob36053d2020-03-26 15:49:30 +0100266 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100267 /* find key definition */
268 for (i = 0; i < key_set.count; ++i) {
269 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100270 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100271 break;
272 }
273 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100274 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100275
276 /* skip attributes */
277 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100278 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
279 assert(xmlctx->status == LYXML_ATTR_CONTENT);
280 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100281 }
282
Michal Vaskob36053d2020-03-26 15:49:30 +0100283 assert(xmlctx->status == LYXML_ELEM_CONTENT);
284 if (i < key_set.count) {
285 /* validate the value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200286 r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns);
Michal Vaskob36053d2020-03-26 15:49:30 +0100287 if (!r) {
288 /* key with a valid value, remove from the set */
289 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100290 }
291 }
292
Michal Vaskob36053d2020-03-26 15:49:30 +0100293 /* parser next */
294 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100295
Michal Vaskob36053d2020-03-26 15:49:30 +0100296 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100297 while (xmlctx->status == LYXML_ELEMENT) {
298 while (parents_count < xmlctx->elements.count) {
299 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
300 }
301 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100302 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
303 }
304
305 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
306 assert(xmlctx->status == LYXML_ELEM_CLOSE);
307 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
308 if (next != LYXML_ELEM_CLOSE) {
309 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
310 }
Michal Vasko44685da2020-03-17 15:38:06 +0100311 }
312
313 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100314 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100315 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100316 }
317
318cleanup:
319 ly_set_erase(&key_set, NULL);
320 return ret;
321}
322
Michal Vasko5c24ed12021-06-09 09:27:32 +0200323/**
324 * @brief Skip an element with all its descendants.
325 *
326 * @param[in] xmlctx XML parser context.
327 * @return LY_ERR value.
328 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100329static LY_ERR
330lydxml_data_skip(struct lyxml_ctx *xmlctx)
331{
332 uint32_t parents_count;
333
334 /* remember current number of parents */
335 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200336 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100337
338 /* skip after the content */
339 while (xmlctx->status != LYXML_ELEM_CONTENT) {
340 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
341 }
342 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
343
344 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200345 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100346 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
347 }
348
349 /* close element */
350 assert(xmlctx->status == LYXML_ELEM_CLOSE);
351 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
352
353 return LY_SUCCESS;
354}
355
Michal Vasko5c24ed12021-06-09 09:27:32 +0200356/**
357 * @brief Check that the current element can be parsed as a data node.
358 *
359 * @param[in] lydctx XML data parser context.
360 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
361 * @return LY_ERR value.
362 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100363static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200364lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100365{
366 LY_ERR ret = LY_SUCCESS;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200367 struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
Michal Vasko1bf09392020-03-27 12:38:10 +0100368
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100369 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
370 /* only checks specific to opaque nodes */
371 return LY_SUCCESS;
372 }
373
Michal Vasko13854662021-06-09 09:27:50 +0200374 if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
375 /* nothing to check */
376 return LY_SUCCESS;
377 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100378
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200379 assert(xmlctx->elements.count);
380
Michal Vasko13854662021-06-09 09:27:50 +0200381 /* backup parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200382 LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
Michal Vasko1bf09392020-03-27 12:38:10 +0100383
Michal Vasko13854662021-06-09 09:27:50 +0200384 /* skip attributes */
385 while (xmlctx->status == LYXML_ATTRIBUTE) {
386 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
387 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
388 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100389
Michal Vasko13854662021-06-09 09:27:50 +0200390 if ((*snode)->nodetype & LYD_NODE_TERM) {
391 /* value may not be valid in which case we parse it as an opaque node */
392 if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns)) {
Michal Vaskod0237d42021-07-12 14:49:46 +0200393 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, xmlctx->value_len,
394 xmlctx->value);
Michal Vasko13854662021-06-09 09:27:50 +0200395 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100396 }
Michal Vasko13854662021-06-09 09:27:50 +0200397 } else if ((*snode)->nodetype == LYS_LIST) {
398 /* skip content */
399 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
Michal Vasko1bf09392020-03-27 12:38:10 +0100400
Michal Vasko13854662021-06-09 09:27:50 +0200401 if (lydxml_check_list(xmlctx, *snode)) {
402 /* invalid list, parse as opaque if it missing/has invalid some keys */
Michal Vaskod0237d42021-07-12 14:49:46 +0200403 LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
Michal Vasko13854662021-06-09 09:27:50 +0200404 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100405 }
Michal Vasko13854662021-06-09 09:27:50 +0200406 } else {
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100407 /* if there is a non-WS value, it cannot be parsed as an inner node */
408 assert(xmlctx->status == LYXML_ELEM_CONTENT);
409 if (!xmlctx->ws_only) {
410 *snode = NULL;
411 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100412 }
413
Michal Vasko13854662021-06-09 09:27:50 +0200414restore:
415 /* restore parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200416 lyxml_ctx_restore(xmlctx, &pxmlctx);
Michal Vasko1bf09392020-03-27 12:38:10 +0100417 return ret;
418}
419
Radek Krejcie7b95092019-05-15 11:03:07 +0200420/**
Michal Vaskocea58712022-04-01 14:37:08 +0200421 * @brief Get sensible data hints for an opaque node.
422 *
423 * @param[in] name Node name.
424 * @param[in] name_len Length of @p name.
425 * @param[in] value Node value.
426 * @param[in] value_len Length of @p value.
427 * @param[in] first Node first sibling.
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100428 * @param[in] ns Node module namespace, NULL for no namespace.
Michal Vaskocea58712022-04-01 14:37:08 +0200429 * @param[out] hints Data hints to use.
430 * @param[out] anchor Anchor to insert after in case of a list.
431 */
432static void
433lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, struct lyd_node *first,
434 const char *ns, uint32_t *hints, struct lyd_node **anchor)
435{
436 struct lyd_node_opaq *opaq;
437 char *ptr;
438 long num;
439
440 *hints = 0;
441 *anchor = NULL;
442
443 if (!value_len) {
444 /* no value */
445 *hints |= LYD_VALHINT_EMPTY;
446 } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
447 /* boolean value */
448 *hints |= LYD_VALHINT_BOOLEAN;
449 } else {
450 num = strtol(value, &ptr, 10);
451 if ((unsigned)(ptr - value) == value_len) {
452 /* number value */
453 *hints |= LYD_VALHINT_DECNUM;
454 if ((num < INT32_MIN) || (num > UINT32_MAX)) {
455 /* large number */
456 *hints |= LYD_VALHINT_NUM64;
457 }
458 } else {
459 /* string value */
460 *hints |= LYD_VALHINT_STRING;
461 }
462 }
463
464 if (!first) {
465 return;
466 }
467
468 /* search backwards to find the last instance */
469 do {
470 first = first->prev;
471 if (first->schema) {
472 continue;
473 }
474
475 opaq = (struct lyd_node_opaq *)first;
476 assert(opaq->format == LY_VALUE_XML);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100477 if (!ly_strncmp(opaq->name.name, name, name_len) &&
478 ((ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) {
Michal Vaskocea58712022-04-01 14:37:08 +0200479 if (opaq->value && opaq->value[0]) {
480 /* leaf-list nodes */
481 opaq->hints |= LYD_NODEHINT_LEAFLIST;
482 *hints |= LYD_NODEHINT_LEAFLIST;
483 } else {
484 /* list nodes */
485 opaq->hints |= LYD_NODEHINT_LIST;
486 *hints |= LYD_NODEHINT_LIST;
487 }
488 *anchor = first;
489 break;
490 }
491 } while (first->prev->next);
492}
493
494/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200495 * @brief Get schema node for the current element.
Michal Vaskoddd76592022-01-17 13:34:48 +0100496 *
497 * @param[in] lydctx XML data parser context.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200498 * @param[in] parent Parsed parent data node, if any.
499 * @param[in] prefix Element prefix, if any.
500 * @param[in] prefix_len Length of @p prefix.
501 * @param[in] name Element name.
502 * @param[in] name_len Length of @p name.
503 * @param[out] snode Found schema node, NULL if no suitable was found.
504 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vaskoddd76592022-01-17 13:34:48 +0100505 * @return LY_SUCCESS on success;
Michal Vaskoddd76592022-01-17 13:34:48 +0100506 * @return LY_ERR on error.
507 */
508static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +0200509lydxml_subtree_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len,
510 const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100511{
512 LY_ERR r;
Michal Vaskob36053d2020-03-26 15:49:30 +0100513 struct lyxml_ctx *xmlctx;
514 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200515 const struct lyxml_ns *ns;
Radek Krejcie7b95092019-05-15 11:03:07 +0200516 struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200517 uint32_t getnext_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +0100518
Michal Vaskob36053d2020-03-26 15:49:30 +0100519 xmlctx = lydctx->xmlctx;
520 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100521 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100522
Michal Vasko8cc3f662022-03-29 11:25:51 +0200523 *snode = NULL;
524 *ext = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100525
Michal Vasko8cc3f662022-03-29 11:25:51 +0200526 /* get current namespace */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200527 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200528 if (!ns) {
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100529 if (lydctx->int_opts & LYD_INTOPT_ANY) {
530 goto unknown_module;
531 }
532
533 if (prefix_len) {
534 LOGVAL(ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
535 } else {
536 LOGVAL(ctx, LYVE_REFERENCE, "Missing XML namespace.");
537 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200538 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200539 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200540
541 /* get the element module, use parent context if possible because of extensions */
542 mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200543 if (!mod) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100544 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200545 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
546 snode, ext);
547 if (r != LY_ENOT) {
548 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100549 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100550 }
551
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100552unknown_module:
Michal Vaskoe0665742021-02-11 11:08:44 +0100553 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100554 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200555 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200556 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200557 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200558 }
559
Michal Vaskoa5da3292020-08-12 13:10:50 +0200560 /* get the schema node */
Michal Vasko81008a52021-07-21 16:06:12 +0200561 if (mod) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100562 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200563 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100564 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200565 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100566 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200567 if (!*snode) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100568 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200569 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
570 name_len, snode, ext);
571 if (r != LY_ENOT) {
572 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100573 return r;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100574 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100575
576 /* unknown data node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100577 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
578 if (parent) {
Radek Krejci422afb12021-03-04 16:38:16 +0100579 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100580 (int)name_len, name, LYD_NAME(parent));
Radek Krejcif16e2542021-02-17 15:39:23 +0100581 } else if (lydctx->ext) {
582 if (lydctx->ext->argument) {
Radek Krejci422afb12021-03-04 16:38:16 +0100583 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
584 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100585 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100586 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
587 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100588 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100589 } else {
Radek Krejci422afb12021-03-04 16:38:16 +0100590 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
591 (int)name_len, name, mod->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100592 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200593 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200594 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200595 return LY_SUCCESS;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200596 } else {
597 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200598 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
599 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200600 }
601 }
602
Michal Vasko8cc3f662022-03-29 11:25:51 +0200603 return LY_SUCCESS;
604}
605
606/**
607 * @brief Parse XML subtree.
608 *
609 * @param[in] lydctx XML YANG data parser context.
610 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
611 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child. In case there were already some siblings,
612 * this may point to a previously existing node.
613 * @param[in,out] parsed Optional set to add all the parsed siblings into.
614 * @return LY_ERR value.
615 */
616static LY_ERR
617lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
618{
619 LY_ERR ret = LY_SUCCESS;
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100620 const char *prefix, *name, *ns_uri;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200621 size_t prefix_len, name_len;
622 struct lyxml_ctx *xmlctx;
623 const struct ly_ctx *ctx;
624 const struct lyxml_ns *ns;
625 struct lyd_meta *meta = NULL;
626 struct lyd_attr *attr = NULL;
627 const struct lysc_node *snode;
628 struct lysc_ext_instance *ext;
Michal Vaskocea58712022-04-01 14:37:08 +0200629 uint32_t prev_parse_opts, orig_parse_opts, prev_int_opts, hints;
630 struct lyd_node *node = NULL, *anchor, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200631 void *val_prefix_data = NULL;
632 LY_VALUE_FORMAT format;
633 ly_bool parse_subtree;
634 char *val;
635
636 assert(parent || first_p);
637
638 xmlctx = lydctx->xmlctx;
639 ctx = xmlctx->ctx;
640
641 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
642 /* all descendants should be parsed */
643 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
644 orig_parse_opts = lydctx->parse_opts;
645
646 assert(xmlctx->status == LYXML_ELEMENT);
647
648 /* remember element prefix and name */
649 prefix = xmlctx->prefix;
650 prefix_len = xmlctx->prefix_len;
651 name = xmlctx->name;
652 name_len = xmlctx->name_len;
653
654 /* parser next */
655 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
656
657 /* get the schema node */
658 LY_CHECK_GOTO(ret = lydxml_subtree_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext), error);
659
660 if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
661 LOGVRB("Skipping parsing of unknown node \"%.*s\".", name_len, name);
662
663 /* skip element with children */
664 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), error);
665 return LY_SUCCESS;
666 }
667
Michal Vaskoa5da3292020-08-12 13:10:50 +0200668 /* create metadata/attributes */
669 if (xmlctx->status == LYXML_ATTRIBUTE) {
670 if (snode) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100671 ret = lydxml_metadata(lydctx, snode, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200672 LY_CHECK_GOTO(ret, error);
673 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +0100674 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200675 ret = lydxml_attrs(xmlctx, &attr);
676 LY_CHECK_GOTO(ret, error);
677 }
678 }
679
680 assert(xmlctx->status == LYXML_ELEM_CONTENT);
681 if (!snode) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100682 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200683
684 if (xmlctx->ws_only) {
685 /* ignore WS-only value */
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100686 if (xmlctx->dynamic) {
Michal Vaskocea58712022-04-01 14:37:08 +0200687 free((char *)xmlctx->value);
Radek IÅ¡a017270d2021-02-16 10:26:15 +0100688 }
689 xmlctx->dynamic = 0;
690 xmlctx->value = "";
Michal Vaskoa5da3292020-08-12 13:10:50 +0200691 xmlctx->value_len = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200692 format = LY_VALUE_XML;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200693 } else {
694 /* get value prefixes */
Radek Krejci8df109d2021-04-23 12:19:08 +0200695 ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100696 &xmlctx->ns, &format, &val_prefix_data);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200697 LY_CHECK_GOTO(ret, error);
698 }
699
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200700 /* get NS again, it may have been backed up and restored */
701 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100702 ns_uri = ns ? ns->uri : NULL;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200703
Michal Vaskocea58712022-04-01 14:37:08 +0200704 /* get best-effort node hints */
705 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, parent ? lyd_child(parent) : *first_p,
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100706 ns_uri, &hints, &insert_anchor);
Michal Vaskocea58712022-04-01 14:37:08 +0200707
Michal Vaskoa5da3292020-08-12 13:10:50 +0200708 /* create node */
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100709 ret = lyd_create_opaq(ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0,
710 xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data, hints, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200711 LY_CHECK_GOTO(ret, error);
712
713 /* parser next */
714 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
715
716 /* process children */
717 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100718 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200719 LY_CHECK_GOTO(ret, error);
720 }
721 } else if (snode->nodetype & LYD_NODE_TERM) {
722 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200723 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 +0200724 &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, &node), error);
Radek Krejciddace2c2021-01-08 11:30:56 +0100725 LOG_LOCSET(snode, node, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200726
727 if (parent && (node->schema->flags & LYS_KEY)) {
728 /* check the key order, the anchor must never be a key */
Michal Vaskoe0665742021-02-11 11:08:44 +0100729 anchor = lyd_insert_get_next_anchor(lyd_child(parent), node);
Michal Vasko441e0fa2022-03-03 13:45:00 +0100730 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100731 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100732 LOGVAL(ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", node->schema->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200733 ret = LY_EVALID;
734 goto error;
735 } else {
736 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", node->schema->name);
737 }
738 }
739 }
740
741 /* parser next */
742 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
743
744 /* no children expected */
745 if (xmlctx->status == LYXML_ELEMENT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100746 LOGVAL(ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100747 (int)xmlctx->name_len, xmlctx->name, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200748 ret = LY_EVALID;
749 goto error;
750 }
751 } else if (snode->nodetype & LYD_NODE_INNER) {
752 if (!xmlctx->ws_only) {
753 /* value in inner node */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100754 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100755 (int)xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200756 ret = LY_EVALID;
757 goto error;
758 }
759
760 /* create node */
761 ret = lyd_create_inner(snode, &node);
762 LY_CHECK_GOTO(ret, error);
763
Radek Krejciddace2c2021-01-08 11:30:56 +0100764 LOG_LOCSET(snode, node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100765
Michal Vaskoa5da3292020-08-12 13:10:50 +0200766 /* parser next */
767 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
768
Michal Vasko8cc3f662022-03-29 11:25:51 +0200769 prev_parse_opts = lydctx->parse_opts;
770 if (ext) {
771 /* only parse these extension data and validate afterwards */
772 lydctx->parse_opts |= LYD_PARSE_ONLY;
773 }
774
Michal Vaskoa5da3292020-08-12 13:10:50 +0200775 /* process children */
776 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100777 ret = lydxml_subtree_r(lydctx, node, lyd_node_child_p(node), NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200778 LY_CHECK_GOTO(ret, error);
779 }
780
Michal Vasko8cc3f662022-03-29 11:25:51 +0200781 /* restore options */
782 lydctx->parse_opts = prev_parse_opts;
783
Michal Vaskoa5da3292020-08-12 13:10:50 +0200784 if (snode->nodetype == LYS_LIST) {
785 /* check all keys exist */
786 LY_CHECK_GOTO(ret = lyd_parse_check_keys(node), error);
787 }
788
Michal Vaskoe0665742021-02-11 11:08:44 +0100789 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vaskoa5da3292020-08-12 13:10:50 +0200790 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100791 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200792 LY_CHECK_GOTO(ret, error);
793
794 /* add any missing default children */
Michal Vaskoddd76592022-01-17 13:34:48 +0100795 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +0200796 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200797 LY_CHECK_GOTO(ret, error);
798 }
799
800 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
801 /* rememeber the RPC/action/notification */
802 lydctx->op_node = node;
803 }
804 } else if (snode->nodetype & LYD_NODE_ANY) {
Michal Vasko27c4dce2021-03-04 15:50:50 +0100805 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
806 /* value in anydata node, we expect a tree */
807 LOGVAL(ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Radek Krejci422afb12021-03-04 16:38:16 +0100808 (int)xmlctx->value_len < 20 ? xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200809 ret = LY_EVALID;
810 goto error;
811 }
812
Michal Vasko27c4dce2021-03-04 15:50:50 +0100813 if (!xmlctx->ws_only) {
814 /* use an arbitrary text value for anyxml */
Michal Vasko742a5b12022-02-24 16:07:27 +0100815 val = strndup(xmlctx->value, xmlctx->value_len);
816 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); ret = LY_EMEM, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200817
Michal Vasko27c4dce2021-03-04 15:50:50 +0100818 /* parser next */
Michal Vaskoa04107f2022-03-29 12:09:50 +0200819 LY_CHECK_ERR_GOTO(ret = lyxml_ctx_next(xmlctx), free(val), error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100820
821 /* create node */
822 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, &node);
Michal Vasko742a5b12022-02-24 16:07:27 +0100823 LY_CHECK_ERR_GOTO(ret, free(val), error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100824 } else {
825 /* parser next */
826 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
827
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200828 /* update options so that generic data can be parsed */
829 prev_parse_opts = lydctx->parse_opts;
Michal Vasko27c4dce2021-03-04 15:50:50 +0100830 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200831 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200832 prev_int_opts = lydctx->int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +0200833 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200834
835 /* parse any data tree */
Michal Vasko27c4dce2021-03-04 15:50:50 +0100836 anchor = NULL;
837 while (xmlctx->status == LYXML_ELEMENT) {
838 ret = lydxml_subtree_r(lydctx, NULL, &anchor, NULL);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200839 if (ret) {
Michal Vaskocc53c582022-04-01 12:07:44 +0200840 lyd_free_siblings(anchor);
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200841 break;
842 }
Michal Vasko27c4dce2021-03-04 15:50:50 +0100843 }
Michal Vaskoa98e3ea2021-06-03 09:13:33 +0200844
845 /* restore options */
846 lydctx->parse_opts = prev_parse_opts;
847 lydctx->int_opts = prev_int_opts;
848
849 LY_CHECK_GOTO(ret, error);
Michal Vasko27c4dce2021-03-04 15:50:50 +0100850
851 /* create node */
852 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, 1, &node);
853 LY_CHECK_GOTO(ret, error);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200854 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200855 }
856 assert(node);
857
Michal Vaskoa5da3292020-08-12 13:10:50 +0200858 if (snode) {
Michal Vasko135719f2022-08-25 12:18:17 +0200859 /* add/correct flags */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200860 LY_CHECK_GOTO(ret = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext), error);
Michal Vasko135719f2022-08-25 12:18:17 +0200861
Michal Vaskoeba23112022-08-26 08:35:41 +0200862 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
863 /* store for ext instance node validation, if needed */
864 LY_CHECK_GOTO(ret = lyd_validate_node_ext(node, &lydctx->ext_node), error);
865 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200866 }
867
868 /* parser next */
869 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +0100870 if (!parse_subtree) {
871 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), error);
872 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200873
874 /* add metadata/attributes */
875 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +0100876 lyd_insert_meta(node, meta, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200877 } else {
878 lyd_insert_attr(node, attr);
879 }
880
881 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +0200882 if (insert_anchor) {
883 lyd_insert_after(insert_anchor, node);
884 } else if (ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200885 LY_CHECK_GOTO(ret = lyplg_ext_insert(parent, node), error);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200886 } else {
887 lyd_insert_node(parent, first_p, node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
888 }
Michal Vaskoa5da3292020-08-12 13:10:50 +0200889 while (!parent && (*first_p)->prev->next) {
890 *first_p = (*first_p)->prev;
891 }
892
Michal Vaskoe0665742021-02-11 11:08:44 +0100893 /* rememeber a successfully parsed node */
894 if (parsed) {
895 ly_set_add(parsed, node, 1, NULL);
896 }
897
Michal Vasko8cc3f662022-03-29 11:25:51 +0200898 lydctx->parse_opts = orig_parse_opts;
Radek Krejciddace2c2021-01-08 11:30:56 +0100899 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200900 return LY_SUCCESS;
901
902error:
Michal Vasko8cc3f662022-03-29 11:25:51 +0200903 lydctx->parse_opts = orig_parse_opts;
Radek Krejciddace2c2021-01-08 11:30:56 +0100904 LOG_LOCBACK(node ? 1 : 0, node ? 1 : 0, 0, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200905 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200906 lyd_free_attr_siblings(ctx, attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200907 lyd_free_tree(node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200908 return ret;
909}
910
Michal Vaskoe0665742021-02-11 11:08:44 +0100911/**
912 * @brief Parse a specific XML element into an opaque node.
913 *
914 * @param[in] xmlctx XML parser context.
915 * @param[in] name Name of the element.
916 * @param[in] uri URI of the element.
917 * @param[in] value Whether a value is expected in the element.
918 * @param[out] evnp Parsed envelope (opaque node).
919 * @return LY_SUCCESS on success.
920 * @return LY_ENOT if the specified element did not match.
921 * @return LY_ERR value on error.
922 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100923static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100924lydxml_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 +0100925{
Michal Vaskoe0665742021-02-11 11:08:44 +0100926 LY_ERR rc = LY_SUCCESS;
927 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +0200928 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100929 const char *prefix;
930 size_t prefix_len;
931
Michal Vasko1bf09392020-03-27 12:38:10 +0100932 assert(xmlctx->status == LYXML_ELEMENT);
933 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
934 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +0100935 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100936 }
937
938 prefix = xmlctx->prefix;
939 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200940 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100941 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +0100942 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +0100943 return LY_EVALID;
944 } else if (strcmp(ns->uri, uri)) {
945 /* different namespace */
946 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100947 }
948
949 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
950
951 /* create attributes */
952 if (xmlctx->status == LYXML_ATTRIBUTE) {
953 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
954 }
955
Michal Vaskoe0665742021-02-11 11:08:44 +0100956 assert(xmlctx->status == LYXML_ELEM_CONTENT);
957 if (!value && !xmlctx->ws_only) {
958 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +0100959 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100960 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100961 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100962 }
Michal Vaskoa8edff02020-03-27 14:47:01 +0100963
964 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +0100965 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200966 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +0100967 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100968
969 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +0100970 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100971 attr = NULL;
972
Michal Vaskoe0665742021-02-11 11:08:44 +0100973 /* parser next element */
974 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100975
Michal Vaskoe0665742021-02-11 11:08:44 +0100976cleanup:
977 lyd_free_attr_siblings(xmlctx->ctx, attr);
978 if (rc) {
979 lyd_free_tree(*envp);
980 *envp = NULL;
981 }
982 return rc;
983}
984
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100985LY_ERR
986lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
987 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
988 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
989{
990 LY_ERR rc = LY_SUCCESS;
991 struct lyd_xml_ctx *lydctx;
992 ly_bool parsed_data_nodes = 0;
993 enum LYXML_PARSER_STATUS status;
994
995 assert(ctx && in && lydctx_p);
996 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
997 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
998
999 /* init context */
1000 lydctx = calloc(1, sizeof *lydctx);
1001 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1002 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1003 lydctx->parse_opts = parse_opts;
1004 lydctx->val_opts = val_opts;
1005 lydctx->int_opts = int_opts;
1006 lydctx->free = lyd_xml_ctx_free;
1007 lydctx->ext = ext;
1008
1009 /* find the operation node if it exists already */
1010 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1011
1012 /* parse XML data */
1013 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1014 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1015 parsed_data_nodes = 1;
1016
1017 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1018 break;
1019 }
1020 }
1021
1022 /* check final state */
1023 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1024 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1025 rc = LY_EVALID;
1026 goto cleanup;
1027 }
1028 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1029 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1030 rc = LY_EVALID;
1031 goto cleanup;
1032 }
1033
1034 if (!parsed_data_nodes) {
1035 /* no data nodes were parsed */
1036 lydctx->op_node = NULL;
1037 }
1038
1039 if (parse_opts & LYD_PARSE_SUBTREE) {
1040 /* check for a sibling element */
1041 assert(subtree_sibling);
1042 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1043 *subtree_sibling = 1;
1044 } else {
1045 *subtree_sibling = 0;
1046 }
1047 }
1048
1049cleanup:
1050 /* there should be no unres stored if validation should be skipped */
1051 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1052 !lydctx->node_when.count));
1053
1054 if (rc) {
1055 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1056 } else {
1057 *lydctx_p = (struct lyd_ctx *)lydctx;
1058
1059 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1060 lyxml_ctx_free(lydctx->xmlctx);
1061 lydctx->xmlctx = NULL;
1062 }
1063 return rc;
1064}
1065
Michal Vaskoe0665742021-02-11 11:08:44 +01001066/**
1067 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1068 *
1069 * @param[in] xmlctx XML parser context.
1070 * @param[out] evnp Parsed envelope(s) (opaque node).
1071 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1072 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1073 * @return LY_SUCCESS on success.
1074 * @return LY_ERR value on error.
1075 */
1076static LY_ERR
1077lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1078{
1079 LY_ERR rc = LY_SUCCESS, r;
1080 struct lyd_node *child;
1081
1082 assert(envp && !*envp);
1083
Michal Vasko50375272022-04-26 12:07:46 +02001084 if (xmlctx->status != LYXML_ELEMENT) {
1085 /* nothing to parse */
1086 assert(xmlctx->status == LYXML_END);
1087 goto cleanup;
1088 }
1089
Michal Vaskoe0665742021-02-11 11:08:44 +01001090 /* parse "rpc" */
1091 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001092 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1093
1094 /* parse "action", if any */
1095 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1096 if (r == LY_SUCCESS) {
1097 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001098 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001099
1100 /* NETCONF action */
1101 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1102 *close_elem = 2;
1103 } else if (r == LY_ENOT) {
1104 /* NETCONF RPC */
1105 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1106 *close_elem = 1;
1107 } else {
1108 rc = r;
1109 goto cleanup;
1110 }
1111
1112cleanup:
1113 if (rc) {
1114 lyd_free_tree(*envp);
1115 *envp = NULL;
1116 }
1117 return rc;
1118}
1119
1120/**
Michal Vasko51de7b72022-04-29 09:50:22 +02001121 * @brief Validate eventTime date-and-time value.
1122 *
1123 * @param[in] node Opaque eventTime node.
1124 * @return LY_SUCCESS on success.
1125 * @return LY_ERR value on error.
1126 */
1127static LY_ERR
1128lydxml_env_netconf_eventtime_validate(const struct lyd_node *node)
1129{
1130 LY_ERR rc = LY_SUCCESS;
1131 struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001132 struct lysc_ctx cctx;
Michal Vasko51de7b72022-04-29 09:50:22 +02001133 const struct lys_module *mod;
1134 LY_ARRAY_COUNT_TYPE u;
1135 struct ly_err_item *err = NULL;
Michal Vaskoab36a7e2022-04-29 10:25:56 +02001136 struct lysp_type *type_p = NULL;
Michal Vasko51de7b72022-04-29 09:50:22 +02001137 struct lysc_pattern **patterns = NULL;
1138 const char *value;
1139
Michal Vaskoc636ea42022-09-16 10:20:31 +02001140 LYSC_CTX_INIT_CTX(cctx, ctx);
1141
Michal Vasko51de7b72022-04-29 09:50:22 +02001142 /* get date-and-time parsed type */
1143 mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
Michal Vaskoab36a7e2022-04-29 10:25:56 +02001144 assert(mod);
Michal Vasko51de7b72022-04-29 09:50:22 +02001145 LY_ARRAY_FOR(mod->parsed->typedefs, u) {
1146 if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) {
1147 type_p = &mod->parsed->typedefs[u].type;
1148 break;
1149 }
1150 }
1151 assert(type_p);
1152
1153 /* compile patterns */
1154 assert(type_p->patterns);
1155 LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
1156
1157 /* validate */
1158 value = lyd_get_value(node);
1159 rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
1160
1161cleanup:
Michal Vaskoc636ea42022-09-16 10:20:31 +02001162 FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free);
Michal Vasko51de7b72022-04-29 09:50:22 +02001163 if (rc && err) {
1164 LOGVAL_ERRITEM(ctx, err);
1165 ly_err_free(err);
1166 LOGVAL(ctx, LYVE_DATA, "Invalid \"eventTime\" in the notification.");
1167 }
1168 return rc;
1169}
1170
1171/**
Michal Vaskoe0665742021-02-11 11:08:44 +01001172 * @brief Parse all expected non-data XML elements of a NETCONF notification message.
1173 *
1174 * @param[in] xmlctx XML parser context.
1175 * @param[out] evnp Parsed envelope(s) (opaque node).
1176 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1177 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1178 * @return LY_SUCCESS on success.
1179 * @return LY_ERR value on error.
1180 */
1181static LY_ERR
1182lydxml_env_netconf_notif(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1183{
1184 LY_ERR rc = LY_SUCCESS, r;
1185 struct lyd_node *child;
1186
1187 assert(envp && !*envp);
1188
Michal Vasko50375272022-04-26 12:07:46 +02001189 if (xmlctx->status != LYXML_ELEMENT) {
1190 /* nothing to parse */
1191 assert(xmlctx->status == LYXML_END);
1192 goto cleanup;
1193 }
1194
Michal Vaskoe0665742021-02-11 11:08:44 +01001195 /* parse "notification" */
1196 r = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001197 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1198
1199 /* parse "eventTime" */
1200 r = lydxml_envelope(xmlctx, "eventTime", "urn:ietf:params:xml:ns:netconf:notification:1.0", 1, &child);
1201 if (r == LY_ENOT) {
Radek Krejci422afb12021-03-04 16:38:16 +01001202 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unexpected element \"%.*s\" instead of \"eventTime\".",
1203 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001204 r = LY_EVALID;
1205 }
1206 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1207
1208 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001209 lyd_insert_node(*envp, NULL, child, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001210
1211 /* validate value */
Michal Vasko51de7b72022-04-29 09:50:22 +02001212 r = lydxml_env_netconf_eventtime_validate(child);
1213 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001214
1215 /* finish child parsing */
Michal Vaskoa8edff02020-03-27 14:47:01 +01001216 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1217 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001218 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"eventTime\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001219 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001220 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001221 goto cleanup;
1222 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001223 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1224
1225 /* NETCONF notification */
1226 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
1227 *close_elem = 1;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001228
1229cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001230 if (rc) {
Michal Vaskoa8edff02020-03-27 14:47:01 +01001231 lyd_free_tree(*envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001232 *envp = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001233 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001234 return rc;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001235}
Michal Vasko79135ae2020-12-16 10:08:35 +01001236
Michal Vaskoe0665742021-02-11 11:08:44 +01001237/**
1238 * @brief Parse an XML element as an opaque node subtree.
1239 *
1240 * @param[in] xmlctx XML parser context.
1241 * @param[in] parent Parent to append nodes to.
1242 * @return LY_ERR value.
1243 */
1244static LY_ERR
1245lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001246{
Michal Vaskoe0665742021-02-11 11:08:44 +01001247 LY_ERR rc = LY_SUCCESS;
1248 const struct lyxml_ns *ns;
1249 struct lyd_attr *attr = NULL;
1250 struct lyd_node *child = NULL;
1251 const char *name, *prefix;
1252 size_t name_len, prefix_len;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001253
Michal Vaskoe0665742021-02-11 11:08:44 +01001254 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001255
Michal Vaskoe0665742021-02-11 11:08:44 +01001256 name = xmlctx->name;
1257 name_len = xmlctx->name_len;
1258 prefix = xmlctx->prefix;
1259 prefix_len = xmlctx->prefix_len;
1260 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1261 if (!ns) {
Radek Krejci422afb12021-03-04 16:38:16 +01001262 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", (int)prefix_len, prefix);
Michal Vaskoe0665742021-02-11 11:08:44 +01001263 return LY_EVALID;
1264 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001265
Michal Vaskoe0665742021-02-11 11:08:44 +01001266 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001267
Michal Vaskoe0665742021-02-11 11:08:44 +01001268 /* create attributes */
1269 if (xmlctx->status == LYXML_ATTRIBUTE) {
1270 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1271 }
1272
1273 /* create node */
1274 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1275 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 +02001276 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, &child);
Michal Vaskoe0665742021-02-11 11:08:44 +01001277 LY_CHECK_GOTO(rc, cleanup);
1278
1279 /* assign atributes */
1280 ((struct lyd_node_opaq *)child)->attr = attr;
1281 attr = NULL;
1282
1283 /* parser next element */
1284 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1285
1286 /* parse all the descendants */
1287 while (xmlctx->status == LYXML_ELEMENT) {
1288 rc = lydxml_opaq_r(xmlctx, child);
1289 LY_CHECK_GOTO(rc, cleanup);
1290 }
1291
1292 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001293 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001294
1295cleanup:
1296 lyd_free_attr_siblings(xmlctx->ctx, attr);
1297 if (rc) {
1298 lyd_free_tree(child);
1299 }
1300 return rc;
1301}
1302
1303/**
1304 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1305 *
1306 * @param[in] xmlctx XML parser context.
1307 * @param[in] parent Parent to append nodes to.
1308 * @return LY_ERR value.
1309 */
1310static LY_ERR
1311lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1312{
1313 LY_ERR r;
1314 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001315 ly_bool no_dup;
1316
1317 /* there must be some child */
1318 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1319 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1320 return LY_EVALID;
1321 }
1322
1323 while (xmlctx->status == LYXML_ELEMENT) {
1324 child = NULL;
1325
1326 /*
1327 * session-id
1328 */
1329 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1330 if (r == LY_SUCCESS) {
1331 no_dup = 1;
1332 goto check_child;
1333 } else if (r != LY_ENOT) {
1334 goto error;
1335 }
1336
1337 /*
1338 * bad-attribute
1339 */
1340 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1341 if (r == LY_SUCCESS) {
1342 no_dup = 1;
1343 goto check_child;
1344 } else if (r != LY_ENOT) {
1345 goto error;
1346 }
1347
1348 /*
1349 * bad-element
1350 */
1351 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1352 if (r == LY_SUCCESS) {
1353 no_dup = 1;
1354 goto check_child;
1355 } else if (r != LY_ENOT) {
1356 goto error;
1357 }
1358
1359 /*
1360 * bad-namespace
1361 */
1362 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1363 if (r == LY_SUCCESS) {
1364 no_dup = 1;
1365 goto check_child;
1366 } else if (r != LY_ENOT) {
1367 goto error;
1368 }
1369
1370 if (r == LY_ENOT) {
1371 assert(xmlctx->status == LYXML_ELEMENT);
1372
Michal Vasko845ba392022-06-16 07:52:13 +02001373 /* custom elements, parse all the siblings */
1374 while (xmlctx->status == LYXML_ELEMENT) {
1375 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1376 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1377 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001378 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001379 }
1380
1381check_child:
1382 /* check for duplicates */
1383 if (no_dup) {
1384 LY_LIST_FOR(lyd_child(parent), iter) {
1385 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1386 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1387 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1388 ((struct lyd_node_opaq *)child)->name.name);
1389 r = LY_EVALID;
1390 goto error;
1391 }
1392 }
1393 }
1394
1395 /* finish child parsing */
1396 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1397 assert(xmlctx->status == LYXML_ELEMENT);
1398 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001399 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001400 r = LY_EVALID;
1401 goto error;
1402 }
1403 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1404
1405 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001406 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001407 }
1408
1409 return LY_SUCCESS;
1410
1411error:
1412 lyd_free_tree(child);
1413 return r;
1414}
1415
1416/**
1417 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1418 *
1419 * @param[in] xmlctx XML parser context.
1420 * @param[in] parent Parent to append nodes to.
1421 * @return LY_ERR value.
1422 */
1423static LY_ERR
1424lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1425{
1426 LY_ERR r;
1427 struct lyd_node *child, *iter;
1428 const char *val;
1429 ly_bool no_dup;
1430
1431 /* there must be some child */
1432 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1433 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1434 return LY_EVALID;
1435 }
1436
1437 while (xmlctx->status == LYXML_ELEMENT) {
1438 child = NULL;
1439
1440 /*
1441 * error-type
1442 */
1443 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1444 if (r == LY_SUCCESS) {
1445 val = ((struct lyd_node_opaq *)child)->value;
1446 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1447 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1448 ((struct lyd_node_opaq *)child)->name.name);
1449 r = LY_EVALID;
1450 goto error;
1451 }
1452
1453 no_dup = 1;
1454 goto check_child;
1455 } else if (r != LY_ENOT) {
1456 goto error;
1457 }
1458
1459 /*
1460 * error-tag
1461 */
1462 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1463 if (r == LY_SUCCESS) {
1464 val = ((struct lyd_node_opaq *)child)->value;
1465 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1466 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1467 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1468 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1469 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1470 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1471 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1472 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1473 ((struct lyd_node_opaq *)child)->name.name);
1474 r = LY_EVALID;
1475 goto error;
1476 }
1477
1478 no_dup = 1;
1479 goto check_child;
1480 } else if (r != LY_ENOT) {
1481 goto error;
1482 }
1483
1484 /*
1485 * error-severity
1486 */
1487 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1488 if (r == LY_SUCCESS) {
1489 val = ((struct lyd_node_opaq *)child)->value;
1490 if (strcmp(val, "error") && strcmp(val, "warning")) {
1491 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1492 ((struct lyd_node_opaq *)child)->name.name);
1493 r = LY_EVALID;
1494 goto error;
1495 }
1496
1497 no_dup = 1;
1498 goto check_child;
1499 } else if (r != LY_ENOT) {
1500 goto error;
1501 }
1502
1503 /*
1504 * error-app-tag
1505 */
1506 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1507 if (r == LY_SUCCESS) {
1508 no_dup = 1;
1509 goto check_child;
1510 } else if (r != LY_ENOT) {
1511 goto error;
1512 }
1513
1514 /*
1515 * error-path
1516 */
1517 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1518 if (r == LY_SUCCESS) {
1519 no_dup = 1;
1520 goto check_child;
1521 } else if (r != LY_ENOT) {
1522 goto error;
1523 }
1524
1525 /*
1526 * error-message
1527 */
1528 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1529 if (r == LY_SUCCESS) {
1530 no_dup = 1;
1531 goto check_child;
1532 } else if (r != LY_ENOT) {
1533 goto error;
1534 }
1535
1536 /* error-info */
1537 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1538 if (r == LY_SUCCESS) {
1539 /* parse all the descendants */
1540 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1541
1542 no_dup = 0;
1543 goto check_child;
1544 } else if (r != LY_ENOT) {
1545 goto error;
1546 }
1547
1548 if (r == LY_ENOT) {
1549 assert(xmlctx->status == LYXML_ELEMENT);
1550 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001551 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001552 r = LY_EVALID;
1553 goto error;
1554 }
1555
1556check_child:
1557 /* check for duplicates */
1558 if (no_dup) {
1559 LY_LIST_FOR(lyd_child(parent), iter) {
1560 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1561 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1562 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1563 ((struct lyd_node_opaq *)child)->name.name);
1564 r = LY_EVALID;
1565 goto error;
1566 }
1567 }
1568 }
1569
1570 /* finish child parsing */
1571 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1572 assert(xmlctx->status == LYXML_ELEMENT);
1573 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001574 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001575 r = LY_EVALID;
1576 goto error;
1577 }
1578 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1579
1580 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001581 lyd_insert_node(parent, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001582 }
1583
1584 return LY_SUCCESS;
1585
1586error:
1587 lyd_free_tree(child);
1588 return r;
1589}
1590
1591/**
1592 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1593 *
1594 * @param[in] xmlctx XML parser context.
1595 * @param[out] evnp Parsed envelope(s) (opaque node).
1596 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1597 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1598 * @return LY_SUCCESS on success.
1599 * @return LY_ERR value on error.
1600 */
1601static LY_ERR
1602lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1603{
1604 LY_ERR rc = LY_SUCCESS, r;
1605 struct lyd_node *child = NULL;
1606 const char *parsed_elem = NULL;
1607
1608 assert(envp && !*envp);
1609
Michal Vasko50375272022-04-26 12:07:46 +02001610 if (xmlctx->status != LYXML_ELEMENT) {
1611 /* nothing to parse */
1612 assert(xmlctx->status == LYXML_END);
1613 goto cleanup;
1614 }
1615
Michal Vaskoe0665742021-02-11 11:08:44 +01001616 /* parse "rpc-reply" */
1617 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001618 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1619
1620 /* there must be some child */
1621 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1622 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1623 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001624 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001625 }
1626
Michal Vaskoe0665742021-02-11 11:08:44 +01001627 /* try to parse "ok" */
1628 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1629 if (r == LY_SUCCESS) {
1630 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001631 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001632
1633 /* finish child parsing */
1634 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1635 assert(xmlctx->status == LYXML_ELEMENT);
1636 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001637 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001638 rc = LY_EVALID;
1639 goto cleanup;
1640 }
1641 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1642
1643 /* success */
1644 parsed_elem = "ok";
1645 goto finish;
1646 } else if (r != LY_ENOT) {
1647 rc = r;
1648 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001649 }
1650
Michal Vaskoe0665742021-02-11 11:08:44 +01001651 /* try to parse all "rpc-error" elements */
1652 while (xmlctx->status == LYXML_ELEMENT) {
1653 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1654 if (r == LY_ENOT) {
1655 break;
1656 } else if (r) {
1657 rc = r;
1658 goto cleanup;
1659 }
1660
1661 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001662 lyd_insert_node(*envp, NULL, child, 1);
Michal Vaskoe0665742021-02-11 11:08:44 +01001663
1664 /* parse all children of "rpc-error" */
1665 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1666
1667 /* finish child parsing */
1668 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1669 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1670
1671 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001672 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001673
1674finish:
1675 if (parsed_elem) {
1676 /* NETCONF rpc-reply with no data */
1677 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1678 assert(xmlctx->status == LYXML_ELEMENT);
1679 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001680 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001681 rc = LY_EVALID;
1682 goto cleanup;
1683 }
1684 }
1685
1686 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001687 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001688 *close_elem = 1;
1689
1690cleanup:
1691 if (rc) {
1692 lyd_free_tree(*envp);
1693 *envp = NULL;
1694 }
1695 return rc;
1696}
1697
Michal Vasko2552ea32020-12-08 15:32:34 +01001698LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001699lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Radek Krejcif16e2542021-02-17 15:39:23 +01001700 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001701 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001702{
Michal Vaskoe0665742021-02-11 11:08:44 +01001703 LY_ERR rc = LY_SUCCESS;
1704 struct lyd_xml_ctx *lydctx;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001705 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001706 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001707
Michal Vaskoe0665742021-02-11 11:08:44 +01001708 assert(ctx && in && lydctx_p);
1709 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1710 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vasko2552ea32020-12-08 15:32:34 +01001711
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001712 assert((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF) ||
1713 (data_type == LYD_TYPE_REPLY_NETCONF));
1714 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1715
Michal Vaskoe0665742021-02-11 11:08:44 +01001716 /* init context */
1717 lydctx = calloc(1, sizeof *lydctx);
1718 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1719 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1720 lydctx->parse_opts = parse_opts;
1721 lydctx->val_opts = val_opts;
1722 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001723 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001724
Michal Vaskoe0665742021-02-11 11:08:44 +01001725 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001726 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001727 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001728 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1729 if (rc == LY_ENOT) {
1730 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1731 }
1732 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001733 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001734 case LYD_TYPE_NOTIF_NETCONF:
1735 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001736 rc = lydxml_env_netconf_notif(lydctx->xmlctx, envp, &int_opts, &close_elem);
1737 if (rc == LY_ENOT) {
1738 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1739 }
1740 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001741 break;
1742 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001743 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001744 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1745 if (rc == LY_ENOT) {
1746 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1747 }
1748 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001749 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001750 default:
1751 LOGINT(ctx);
1752 rc = LY_EINT;
1753 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001754 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001755
Michal Vaskoe0665742021-02-11 11:08:44 +01001756 lydctx->int_opts = int_opts;
1757
1758 /* find the operation node if it exists already */
1759 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1760
1761 /* parse XML data */
1762 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1763 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1764 parsed_data_nodes = 1;
1765
1766 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1767 break;
1768 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001769 }
1770
Michal Vaskoe0665742021-02-11 11:08:44 +01001771 /* close all opened elements */
1772 for (i = 0; i < close_elem; ++i) {
1773 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1774 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001775 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1776 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001777 rc = LY_EVALID;
1778 goto cleanup;
1779 }
1780
1781 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001782 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001783
1784 /* check final state */
1785 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1786 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1787 rc = LY_EVALID;
1788 goto cleanup;
1789 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01001790 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 +01001791 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1792 rc = LY_EVALID;
1793 goto cleanup;
1794 }
1795
1796 if (!parsed_data_nodes) {
1797 /* no data nodes were parsed */
1798 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001799 }
1800
1801cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001802 /* there should be no unres stored if validation should be skipped */
1803 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01001804 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01001805
1806 if (rc) {
1807 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1808 } else {
1809 *lydctx_p = (struct lyd_ctx *)lydctx;
1810
1811 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1812 lyxml_ctx_free(lydctx->xmlctx);
1813 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001814 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001815 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02001816}