blob: 92214e30d84cb6c3f61bf9d6db17a41b0bcb0fee [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief XML data parser for libyang
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
Michal Vasko9b368d32020-02-14 13:53:31 +010018#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "log.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "tree_data_internal.h"
26#include "tree_schema.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010027#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029
Michal Vasko1bf09392020-03-27 12:38:10 +010030#define LYD_INTOPT_RPC 0x01 /**< RPC/action invocation is being parsed */
31#define LYD_INTOPT_NOTIF 0x02 /**< notification is being parsed */
Michal Vasko1ce933a2020-03-30 12:38:22 +020032#define LYD_INTOPT_REPLY 0x04 /**< RPC/action reply is being parsed */
Michal Vasko1bf09392020-03-27 12:38:10 +010033
Radek Krejcie7b95092019-05-15 11:03:07 +020034/**
Michal Vaskob36053d2020-03-26 15:49:30 +010035 * @brief Internal context for XML YANG data parser.
Radek Krejcie7b95092019-05-15 11:03:07 +020036 */
37struct lyd_xml_ctx {
Michal Vaskob36053d2020-03-26 15:49:30 +010038 struct lyxml_ctx *xmlctx; /**< XML context */
Radek Krejcie7b95092019-05-15 11:03:07 +020039
Michal Vasko52927e22020-03-16 17:26:14 +010040 uint32_t options; /**< various @ref dataparseroptions. */
Michal Vasko1bf09392020-03-27 12:38:10 +010041 uint32_t int_opts; /**< internal data parser options */
Michal Vasko52927e22020-03-16 17:26:14 +010042 uint32_t path_len; /**< used bytes in the path buffer */
Radek Krejcie7b95092019-05-15 11:03:07 +020043#define LYD_PARSER_BUFSIZE 4078
44 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vasko52927e22020-03-16 17:26:14 +010045 struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
46 struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
Michal Vaskoecd62de2019-11-13 12:35:11 +010047 struct ly_set when_check; /**< set of nodes with "when" conditions */
Michal Vasko1bf09392020-03-27 12:38:10 +010048 struct lyd_node *op_ntf; /**< if an RPC/action/notification is being parsed, store the pointer to it */
Radek Krejcie7b95092019-05-15 11:03:07 +020049};
50
51/**
Michal Vaskob36053d2020-03-26 15:49:30 +010052 * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used
53 * in the values to the schema via XML namespaces.
Radek Krejciaca74032019-06-04 08:53:06 +020054 */
55static const struct lys_module *
Michal Vasko52927e22020-03-16 17:26:14 +010056lydxml_resolve_prefix(const struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser)
Radek Krejciaca74032019-06-04 08:53:06 +020057{
58 const struct lyxml_ns *ns;
Michal Vaskob36053d2020-03-26 15:49:30 +010059 struct lyxml_ctx *xmlctx = (struct lyxml_ctx *)parser;
Radek Krejciaca74032019-06-04 08:53:06 +020060
61 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
62 if (!ns) {
63 return NULL;
64 }
65
66 return ly_ctx_get_module_implemented_ns(ctx, ns->uri);
67}
68
Radek Krejcie7b95092019-05-15 11:03:07 +020069static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +010070lydxml_metadata(struct lyxml_ctx *xmlctx, const struct lysc_node *sparent, int strict, struct ly_set *type_meta_check,
71 struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020072{
Michal Vaskob36053d2020-03-26 15:49:30 +010073 LY_ERR ret = LY_EVALID;
Radek Krejci28681fa2019-09-06 13:08:45 +020074 const struct lyxml_ns *ns;
75 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010076 const char *name;
77 size_t name_len;
Radek Krejci28681fa2019-09-06 13:08:45 +020078
Michal Vaskob36053d2020-03-26 15:49:30 +010079 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +020080
Michal Vaskob36053d2020-03-26 15:49:30 +010081 while (xmlctx->status == LYXML_ATTRIBUTE) {
82 if (!xmlctx->prefix_len) {
Radek Krejci28681fa2019-09-06 13:08:45 +020083 /* in XML, all attributes must be prefixed
84 * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
Michal Vasko52927e22020-03-16 17:26:14 +010085 if (strict) {
86 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Michal Vaskob36053d2020-03-26 15:49:30 +010087 xmlctx->name_len, xmlctx->name);
88 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +020089 }
Michal Vaskob36053d2020-03-26 15:49:30 +010090
Radek Krejci28681fa2019-09-06 13:08:45 +020091skip_attr:
Michal Vaskob36053d2020-03-26 15:49:30 +010092 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
93 assert(xmlctx->status == LYXML_ATTR_CONTENT);
94 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +020095 continue;
96 }
97
98 /* get namespace of the attribute to find its annotation definition */
Michal Vaskob36053d2020-03-26 15:49:30 +010099 ns = lyxml_ns_get(xmlctx, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200100 if (!ns) {
Michal Vasko52927e22020-03-16 17:26:14 +0100101 /* unknown namespace, XML error */
102 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
Michal Vaskob36053d2020-03-26 15:49:30 +0100103 xmlctx->prefix_len, xmlctx->prefix);
Radek Krejci28681fa2019-09-06 13:08:45 +0200104 goto cleanup;
105 }
Michal Vasko52927e22020-03-16 17:26:14 +0100106 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200107 if (!mod) {
108 /* module is not implemented or not present in the schema */
Michal Vasko52927e22020-03-16 17:26:14 +0100109 if (strict) {
110 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE,
Michal Vasko9f96a052020-03-10 09:41:45 +0100111 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Michal Vaskob36053d2020-03-26 15:49:30 +0100112 ns->uri, xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", xmlctx->name_len,
113 xmlctx->name);
114 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200115 }
116 goto skip_attr;
117 }
118
Michal Vaskob36053d2020-03-26 15:49:30 +0100119 /* remember attr name and get its content */
120 name = xmlctx->name;
121 name_len = xmlctx->name_len;
122 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
123 assert(xmlctx->status == LYXML_ATTR_CONTENT);
124
125 /* create metadata */
126 ret = lyd_create_meta(NULL, meta, mod, name, name_len, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
127 lydxml_resolve_prefix, xmlctx, LYD_XML, sparent);
128 if (ret == LY_EINCOMPLETE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100129 if (type_meta_check) {
130 ly_set_add(type_meta_check, meta, LY_SET_OPT_USEASLIST);
131 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100132 } else if (ret) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200133 goto cleanup;
134 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100135
136 /* next attribute */
137 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200138 }
Michal Vasko52927e22020-03-16 17:26:14 +0100139
Radek Krejci28681fa2019-09-06 13:08:45 +0200140 ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200141
142cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100143 if (ret) {
144 lyd_free_meta(xmlctx->ctx, *meta, 1);
145 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200146 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200147 return ret;
148}
149
Michal Vasko52927e22020-03-16 17:26:14 +0100150static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100151lydxml_attrs(struct lyxml_ctx *xmlctx, struct ly_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100152{
153 LY_ERR ret = LY_SUCCESS;
154 const struct lyxml_ns *ns;
155 struct ly_prefix *val_prefs;
156 struct ly_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100157 const char *name, *prefix;
158 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100159
160 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100161 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100162
Michal Vaskob36053d2020-03-26 15:49:30 +0100163 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100164 ns = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 if (xmlctx->prefix_len) {
Michal Vasko52927e22020-03-16 17:26:14 +0100166 /* get namespace of the attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100167 ns = lyxml_ns_get(xmlctx, xmlctx->prefix, xmlctx->prefix_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100168 if (!ns) {
169 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
Michal Vaskob36053d2020-03-26 15:49:30 +0100170 xmlctx->prefix_len, xmlctx->prefix);
Michal Vasko52927e22020-03-16 17:26:14 +0100171 ret = LY_EVALID;
172 goto cleanup;
173 }
174 }
175
176 if (*attr) {
177 attr2 = *attr;
178 } else {
179 attr2 = NULL;
180 }
181
Michal Vaskob36053d2020-03-26 15:49:30 +0100182 /* remember attr prefix, name, and get its content */
183 prefix = xmlctx->prefix;
184 prefix_len = xmlctx->prefix_len;
185 name = xmlctx->name;
186 name_len = xmlctx->name_len;
187 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
188 assert(xmlctx->status == LYXML_ATTR_CONTENT);
189
Michal Vasko52927e22020-03-16 17:26:14 +0100190 /* get value prefixes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100191 LY_CHECK_GOTO(ret = lyxml_get_prefixes(xmlctx, xmlctx->value, xmlctx->value_len, &val_prefs), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100192
193 /* attr2 is always changed to the created attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100194 ret = ly_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, xmlctx->value, xmlctx->value_len,
195 &xmlctx->dynamic, LYD_XML, val_prefs, prefix, prefix_len, ns ? ns->uri : NULL);
Michal Vasko52927e22020-03-16 17:26:14 +0100196 LY_CHECK_GOTO(ret, cleanup);
197
198 if (!*attr) {
199 *attr = attr2;
200 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100201
202 /* next attribute */
203 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100204 }
205
206cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100207 if (ret) {
208 ly_free_attr(xmlctx->ctx, *attr, 1);
209 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100210 }
Michal Vasko52927e22020-03-16 17:26:14 +0100211 return ret;
212}
213
Michal Vasko44685da2020-03-17 15:38:06 +0100214static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100215lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100216{
Michal Vaskob36053d2020-03-26 15:49:30 +0100217 LY_ERR ret = LY_SUCCESS, r;
218 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100219 struct ly_set key_set = {0};
220 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100221 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100222
223 assert(list && (list->nodetype == LYS_LIST));
224
225 /* get all keys into a set (keys do not have if-features or anything) */
226 snode = NULL;
227 while ((snode = lys_getnext(snode, list, NULL, LYS_GETNEXT_NOSTATECHECK)) && (snode->flags & LYS_KEY)) {
228 ly_set_add(&key_set, (void *)snode, LY_SET_OPT_USEASLIST);
229 }
230
Michal Vaskob36053d2020-03-26 15:49:30 +0100231 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100232 /* find key definition */
233 for (i = 0; i < key_set.count; ++i) {
234 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100235 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100236 break;
237 }
238 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100239 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100240
241 /* skip attributes */
242 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100243 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
244 assert(xmlctx->status == LYXML_ATTR_CONTENT);
245 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100246 }
247
Michal Vaskob36053d2020-03-26 15:49:30 +0100248 assert(xmlctx->status == LYXML_ELEM_CONTENT);
249 if (i < key_set.count) {
250 /* validate the value */
251 r = lys_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, lydxml_resolve_prefix, xmlctx, LYD_XML);
252 if (!r) {
253 /* key with a valid value, remove from the set */
254 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100255 }
256 }
257
Michal Vaskob36053d2020-03-26 15:49:30 +0100258 /* parser next */
259 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100260
Michal Vaskob36053d2020-03-26 15:49:30 +0100261 /* skip any children, resursively */
262 parents_count = xmlctx->elements.count;
263 while ((parents_count < xmlctx->elements.count) || (xmlctx->status == LYXML_ELEMENT)) {
264 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
265 }
266
267 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
268 assert(xmlctx->status == LYXML_ELEM_CLOSE);
269 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
270 if (next != LYXML_ELEM_CLOSE) {
271 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
272 }
Michal Vasko44685da2020-03-17 15:38:06 +0100273 }
274
275 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100276 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100277 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100278 }
279
280cleanup:
281 ly_set_erase(&key_set, NULL);
282 return ret;
283}
284
Michal Vasko1bf09392020-03-27 12:38:10 +0100285static LY_ERR
286lydxml_data_skip(struct lyxml_ctx *xmlctx)
287{
288 uint32_t parents_count;
289
290 /* remember current number of parents */
291 parents_count = xmlctx->elements.count;
292
293 /* skip after the content */
294 while (xmlctx->status != LYXML_ELEM_CONTENT) {
295 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
296 }
297 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
298
299 /* skip all children elements, recursively, if any */
300 while (parents_count < xmlctx->elements.count) {
301 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
302 }
303
304 /* close element */
305 assert(xmlctx->status == LYXML_ELEM_CLOSE);
306 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
307
308 return LY_SUCCESS;
309}
310
311static LY_ERR
312lydxml_data_check_schema(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
313{
314 LY_ERR ret = LY_SUCCESS;
315 enum LYXML_PARSER_STATUS prev_status;
316 const char *prev_input, *pname, *pprefix;
317 size_t pprefix_len, pname_len;
318 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
319
320 if ((lydctx->options & LYD_OPT_NO_STATE) && ((*snode)->flags & LYS_CONFIG_R)) {
Michal Vaskocb7526d2020-03-30 15:08:26 +0200321 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INNODE, "state", (*snode)->name);
Michal Vasko1bf09392020-03-27 12:38:10 +0100322 return LY_EVALID;
323 }
324
325 if ((*snode)->nodetype & (LYS_RPC | LYS_ACTION)) {
326 if (lydctx->int_opts & LYD_INTOPT_RPC) {
327 if (lydctx->op_ntf) {
328 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
329 lys_nodetype2str((*snode)->nodetype), (*snode)->name,
330 lys_nodetype2str(lydctx->op_ntf->schema->nodetype), lydctx->op_ntf->schema->name);
331 return LY_EVALID;
332 }
333 } else {
334 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\".",
335 lys_nodetype2str((*snode)->nodetype), (*snode)->name);
336 return LY_EVALID;
337 }
338 } else if ((*snode)->nodetype == LYS_NOTIF) {
339 if (lydctx->int_opts & LYD_INTOPT_NOTIF) {
340 if (lydctx->op_ntf) {
341 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
342 lys_nodetype2str((*snode)->nodetype), (*snode)->name,
343 lys_nodetype2str(lydctx->op_ntf->schema->nodetype), lydctx->op_ntf->schema->name);
344 return LY_EVALID;
345 }
346 } else {
347 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\".",
348 lys_nodetype2str((*snode)->nodetype), (*snode)->name);
349 return LY_EVALID;
350 }
351 }
352
353 if ((lydctx->options & LYD_OPT_OPAQ) && ((*snode)->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
354 /* backup parser */
355 prev_status = xmlctx->status;
356 pprefix = xmlctx->prefix;
357 pprefix_len = xmlctx->prefix_len;
358 pname = xmlctx->name;
359 pname_len = xmlctx->name_len;
360 prev_input = xmlctx->input;
361 if ((xmlctx->status == LYXML_ELEM_CONTENT) && xmlctx->dynamic) {
362 /* it was backed up, do not free */
363 xmlctx->dynamic = 0;
364 }
365
366 /* skip attributes */
367 while (xmlctx->status == LYXML_ATTRIBUTE) {
368 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
369 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
370 }
371
372 if ((*snode)->nodetype & LYD_NODE_TERM) {
373 /* value may not be valid in which case we parse it as an opaque node */
374 if (lys_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, lydxml_resolve_prefix, xmlctx, LYD_XML)) {
375 *snode = NULL;
376 }
377 } else {
378 /* skip content */
379 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
380
381 if (lydxml_check_list(xmlctx, *snode)) {
382 /* invalid list, parse as opaque if it missing/has invalid some keys */
383 *snode = NULL;
384 }
385 }
386
387restore:
388 /* restore parser */
389 if (xmlctx->dynamic) {
390 free((char *)xmlctx->value);
391 }
392 xmlctx->status = prev_status;
393 xmlctx->prefix = pprefix;
394 xmlctx->prefix_len = pprefix_len;
395 xmlctx->name = pname;
396 xmlctx->name_len = pname_len;
397 xmlctx->input = prev_input;
398 }
399
400 return ret;
401}
402
403static void
404lydxml_data_flags(struct lyd_xml_ctx *lydctx, struct lyd_node *node, struct lyd_meta **meta)
405{
406 struct lyd_meta *meta2, *prev_meta = NULL;
407
408 if (!(node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && node->schema->when) {
409 if (lydctx->options & LYD_OPT_TRUSTED) {
410 /* just set it to true */
411 node->flags |= LYD_WHEN_TRUE;
412 } else {
413 /* remember we need to evaluate this node's when */
414 ly_set_add(&lydctx->when_check, node, LY_SET_OPT_USEASLIST);
415 }
416 }
417
418 if (lydctx->options & LYD_OPT_TRUSTED) {
419 /* node is valid */
420 node->flags &= ~LYD_NEW;
421 }
422
423 LY_LIST_FOR(*meta, meta2) {
424 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults")
425 && meta2->value.boolean) {
426 /* node is default according to the metadata */
427 node->flags |= LYD_DEFAULT;
428
429 /* delete the metadata */
430 if (prev_meta) {
431 prev_meta->next = meta2->next;
432 } else {
433 *meta = (*meta)->next;
434 }
435 lyd_free_meta(lydctx->xmlctx->ctx, meta2, 0);
436 break;
437 }
438
439 prev_meta = meta2;
440 }
441}
442
Radek Krejcie7b95092019-05-15 11:03:07 +0200443/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100444 * @brief Parse XML elements as YANG data node children the specified parent node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200445 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100446 * @param[in] lydctx XML YANG data parser context.
Radek Krejcie7b95092019-05-15 11:03:07 +0200447 * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
Radek Krejcie7b95092019-05-15 11:03:07 +0200448 * @param[out] node Resulting list of the parsed nodes.
Michal Vasko9b368d32020-02-14 13:53:31 +0100449 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200450 */
451static LY_ERR
Michal Vasko1bf09392020-03-27 12:38:10 +0100452lydxml_data_r(struct lyd_xml_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first)
Radek Krejcie7b95092019-05-15 11:03:07 +0200453{
Michal Vaskob36053d2020-03-26 15:49:30 +0100454 LY_ERR ret = LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +0100455 const char *prefix, *name;
456 size_t prefix_len, name_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100457 struct lyxml_ctx *xmlctx;
458 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200459 const struct lyxml_ns *ns;
Michal Vasko1bf09392020-03-27 12:38:10 +0100460 struct lyd_meta *meta = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100461 struct ly_attr *attr = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200462 const struct lysc_node *snode;
463 struct lys_module *mod;
Michal Vasko1bf09392020-03-27 12:38:10 +0100464 uint32_t prev_opts;
Michal Vasko52927e22020-03-16 17:26:14 +0100465 struct lyd_node *cur = NULL, *anchor;
466 struct ly_prefix *val_prefs;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200467 int getnext_opts;
Radek Krejcie7b95092019-05-15 11:03:07 +0200468
Michal Vaskob36053d2020-03-26 15:49:30 +0100469 xmlctx = lydctx->xmlctx;
470 ctx = xmlctx->ctx;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200471 /* leave if-feature check for validation */
472 getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100473
Michal Vaskob36053d2020-03-26 15:49:30 +0100474 while (xmlctx->status == LYXML_ELEMENT) {
475 /* remember element prefix and name */
476 prefix = xmlctx->prefix;
477 prefix_len = xmlctx->prefix_len;
478 name = xmlctx->name;
479 name_len = xmlctx->name_len;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200480
Michal Vaskob36053d2020-03-26 15:49:30 +0100481 /* get the element module */
482 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
Radek Krejcie7b95092019-05-15 11:03:07 +0200483 if (!ns) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100484 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
485 prefix_len, prefix);
Michal Vasko90932a92020-02-12 14:33:03 +0100486 ret = LY_EVALID;
487 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200488 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100489 mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
490 if (!mod && (lydctx->options & LYD_OPT_STRICT)) {
491 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko90932a92020-02-12 14:33:03 +0100492 ret = LY_EVALID;
493 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200494 }
Michal Vasko52927e22020-03-16 17:26:14 +0100495
Michal Vasko1bf09392020-03-27 12:38:10 +0100496 /* parser next */
497 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
498
Michal Vaskob36053d2020-03-26 15:49:30 +0100499 /* get the schema node */
Michal Vasko52927e22020-03-16 17:26:14 +0100500 snode = NULL;
501 if (mod && (!parent || parent->schema)) {
Michal Vasko1ce933a2020-03-30 12:38:22 +0200502 snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Michal Vaskob36053d2020-03-26 15:49:30 +0100503 if (!snode) {
504 if (lydctx->options & LYD_OPT_STRICT) {
505 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.",
506 name_len, name, mod->name);
507 ret = LY_EVALID;
508 goto cleanup;
509 } else if (!(lydctx->options & LYD_OPT_OPAQ)) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100510 /* skip element with children */
511 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100512 continue;
513 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100514 } else {
Michal Vasko1bf09392020-03-27 12:38:10 +0100515 /* check that schema node is valid and can be used */
516 LY_CHECK_GOTO(ret = lydxml_data_check_schema(lydctx, &snode), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100517 }
518 }
519
Michal Vaskob36053d2020-03-26 15:49:30 +0100520 /* create metadata/attributes */
521 if (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100522 if (snode) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100523 ret = lydxml_metadata(xmlctx, snode, lydctx->options & LYD_OPT_STRICT, &lydctx->unres_meta_type, &meta);
Michal Vasko52927e22020-03-16 17:26:14 +0100524 LY_CHECK_GOTO(ret, cleanup);
525 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100526 assert(lydctx->options & LYD_OPT_OPAQ);
527 ret = lydxml_attrs(xmlctx, &attr);
528 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100529 }
Michal Vasko8d544252020-03-02 10:19:52 +0100530 }
531
Michal Vaskob36053d2020-03-26 15:49:30 +0100532 assert(xmlctx->status == LYXML_ELEM_CONTENT);
Michal Vasko52927e22020-03-16 17:26:14 +0100533 if (!snode) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100534 assert(lydctx->options & LYD_OPT_OPAQ);
Michal Vasko52927e22020-03-16 17:26:14 +0100535
Michal Vaskob36053d2020-03-26 15:49:30 +0100536 if (xmlctx->ws_only) {
537 /* ignore WS-only value */
538 xmlctx->value_len = 0;
539 val_prefs = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100540 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100541 /* get value prefixes */
542 ret = lyxml_get_prefixes(xmlctx, xmlctx->value, xmlctx->value_len, &val_prefs);
Michal Vasko52927e22020-03-16 17:26:14 +0100543 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200544 }
Michal Vasko90932a92020-02-12 14:33:03 +0100545
546 /* create node */
Michal Vaskob36053d2020-03-26 15:49:30 +0100547 ret = lyd_create_opaq(ctx, name, name_len, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, LYD_XML,
548 val_prefs, prefix, prefix_len, ns->uri, &cur);
549 LY_CHECK_GOTO(ret, cleanup);
550
551 /* parser next */
552 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
553
554 /* process children */
555 if (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100556 ret = lydxml_data_r(lydctx, (struct lyd_node_inner *)cur, lyd_node_children_p(cur));
Michal Vaskob36053d2020-03-26 15:49:30 +0100557 LY_CHECK_GOTO(ret, cleanup);
558 }
559 } else if (snode->nodetype & LYD_NODE_TERM) {
560 /* create node */
561 ret = lyd_create_term(snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, lydxml_resolve_prefix,
562 xmlctx, LYD_XML, &cur);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200563 if (ret == LY_EINCOMPLETE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100564 if (!(lydctx->options & LYD_OPT_PARSE_ONLY)) {
565 ly_set_add(&lydctx->unres_node_type, cur, LY_SET_OPT_USEASLIST);
Michal Vasko9b368d32020-02-14 13:53:31 +0100566 }
Radek Krejcie553e6d2019-06-07 15:33:18 +0200567 } else if (ret) {
Radek Krejcie553e6d2019-06-07 15:33:18 +0200568 goto cleanup;
569 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100570
571 if (parent && (cur->schema->flags & LYS_KEY)) {
572 /* check the key order, the anchor must always be the last child */
Michal Vasko52927e22020-03-16 17:26:14 +0100573 anchor = lyd_get_prev_key_anchor(parent->child, cur->schema);
574 if ((!anchor && parent->child) || (anchor && anchor->next)) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100575 if (lydctx->options & LYD_OPT_STRICT) {
576 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Invalid position of the key \"%s\" in a list.",
Michal Vasko9b368d32020-02-14 13:53:31 +0100577 cur->schema->name);
578 ret = LY_EVALID;
579 goto cleanup;
580 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100581 LOGWRN(ctx, "Invalid position of the key \"%s\" in a list.", cur->schema->name);
Michal Vasko9b368d32020-02-14 13:53:31 +0100582 }
583 }
584 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100585
586 /* parser next */
587 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
588
589 /* no children expected */
590 if (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100591 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
592 xmlctx->name_len, xmlctx->name, snode->name);
Michal Vasko44685da2020-03-17 15:38:06 +0100593 ret = LY_EVALID;
594 goto cleanup;
Michal Vaskob36053d2020-03-26 15:49:30 +0100595 }
596 } else if (snode->nodetype & LYD_NODE_INNER) {
597 if (!xmlctx->ws_only) {
598 /* value in inner node */
Michal Vasko1bf09392020-03-27 12:38:10 +0100599 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
600 xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100601 ret = LY_EVALID;
602 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200603 }
Michal Vasko90932a92020-02-12 14:33:03 +0100604
605 /* create node */
606 ret = lyd_create_inner(snode, &cur);
607 LY_CHECK_GOTO(ret, cleanup);
608
Michal Vaskob36053d2020-03-26 15:49:30 +0100609 /* parser next */
610 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
611
Radek Krejciee4cab22019-07-17 17:07:47 +0200612 /* process children */
Michal Vaskob36053d2020-03-26 15:49:30 +0100613 if (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100614 ret = lydxml_data_r(lydctx, (struct lyd_node_inner *)cur, lyd_node_children_p(cur));
Michal Vasko9f96a052020-03-10 09:41:45 +0100615 LY_CHECK_GOTO(ret, cleanup);
616 }
617
618 if (snode->nodetype == LYS_LIST) {
619 /* check all keys exist */
Michal Vasko44685da2020-03-17 15:38:06 +0100620 LY_CHECK_GOTO(ret = lyd_parse_check_keys(cur), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200621 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100622
Michal Vaskob36053d2020-03-26 15:49:30 +0100623 if (!(lydctx->options & LYD_OPT_PARSE_ONLY)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100624 /* new node validation, autodelete CANNOT occur, all nodes are new */
625 ret = lyd_validate_new(lyd_node_children_p(cur), snode, NULL);
626 LY_CHECK_GOTO(ret, cleanup);
627
Michal Vasko9b368d32020-02-14 13:53:31 +0100628 /* add any missing default children */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100629 ret = lyd_validate_defaults_r((struct lyd_node_inner *)cur, lyd_node_children_p(cur), NULL, NULL,
Michal Vaskob36053d2020-03-26 15:49:30 +0100630 &lydctx->unres_node_type, &lydctx->when_check, lydctx->options);
Michal Vasko9b368d32020-02-14 13:53:31 +0100631 LY_CHECK_GOTO(ret, cleanup);
632 }
633
Michal Vasko9b368d32020-02-14 13:53:31 +0100634 if (snode->nodetype == LYS_LIST) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100635 /* hash now that all keys should be parsed, rehash for key-less list */
Michal Vasko9b368d32020-02-14 13:53:31 +0100636 lyd_hash(cur);
Michal Vasko1bf09392020-03-27 12:38:10 +0100637 } else if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
638 /* rememeber the RPC/action/notification */
639 lydctx->op_ntf = cur;
Michal Vasko9b368d32020-02-14 13:53:31 +0100640 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200641 } else if (snode->nodetype & LYD_NODE_ANY) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100642 if (!xmlctx->ws_only) {
643 /* value in inner node */
Michal Vasko1bf09392020-03-27 12:38:10 +0100644 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Text value \"%.*s\" inside an any node \"%s\" found.",
645 xmlctx->value_len, xmlctx->value, snode->name);
Michal Vaskob36053d2020-03-26 15:49:30 +0100646 ret = LY_EVALID;
647 goto cleanup;
Radek Krejciee4cab22019-07-17 17:07:47 +0200648 }
Michal Vasko90932a92020-02-12 14:33:03 +0100649
Michal Vaskob36053d2020-03-26 15:49:30 +0100650 /* parser next */
651 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
652
Michal Vasko52927e22020-03-16 17:26:14 +0100653 /* parse any data tree with correct options */
Michal Vaskob36053d2020-03-26 15:49:30 +0100654 prev_opts = lydctx->options;
655 lydctx->options &= ~LYD_OPT_STRICT;
656 lydctx->options |= LYD_OPT_OPAQ;
Michal Vasko52927e22020-03-16 17:26:14 +0100657 anchor = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100658 ret = lydxml_data_r(lydctx, NULL, &anchor);
Michal Vaskob36053d2020-03-26 15:49:30 +0100659 lydctx->options = prev_opts;
Michal Vasko52927e22020-03-16 17:26:14 +0100660 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100661
662 /* create node */
Michal Vasko52927e22020-03-16 17:26:14 +0100663 ret = lyd_create_any(snode, anchor, LYD_ANYDATA_DATATREE, &cur);
664 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200665 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100666 assert(cur);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200667
Michal Vasko1bf09392020-03-27 12:38:10 +0100668 /* add/correct flags */
Michal Vasko52927e22020-03-16 17:26:14 +0100669 if (snode) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100670 lydxml_data_flags(lydctx, cur, &meta);
Michal Vasko8d544252020-03-02 10:19:52 +0100671 }
672
Michal Vasko52927e22020-03-16 17:26:14 +0100673 /* add metadata/attributes */
674 if (snode) {
675 cur->meta = meta;
676 meta = NULL;
677 } else {
678 assert(!cur->schema);
679 ((struct lyd_node_opaq *)cur)->attr = attr;
680 attr = NULL;
681 }
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200682
Michal Vasko90932a92020-02-12 14:33:03 +0100683 /* insert */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100684 lyd_insert_node((struct lyd_node *)parent, first, cur);
Michal Vasko90932a92020-02-12 14:33:03 +0100685 cur = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +0100686
687 /* parser next */
688 assert(xmlctx->status == LYXML_ELEM_CLOSE);
689 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200690 }
691
Michal Vasko90932a92020-02-12 14:33:03 +0100692 /* success */
693 ret = LY_SUCCESS;
694
Radek Krejcie7b95092019-05-15 11:03:07 +0200695cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100696 lyd_free_meta(ctx, meta, 1);
697 ly_free_attr(ctx, attr, 1);
Michal Vasko90932a92020-02-12 14:33:03 +0100698 lyd_free_tree(cur);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100699 if (ret && *first) {
700 lyd_free_siblings(*first);
701 *first = NULL;
Michal Vasko90932a92020-02-12 14:33:03 +0100702 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200703 return ret;
704}
705
706LY_ERR
Michal Vasko1bf09392020-03-27 12:38:10 +0100707lyd_parse_xml_data(const struct ly_ctx *ctx, const char *data, int options, struct lyd_node **tree)
Radek Krejcie7b95092019-05-15 11:03:07 +0200708{
Radek Krejci18a57d92019-07-25 14:01:42 +0200709 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +0100710 struct lyd_xml_ctx lydctx = {0};
Michal Vaskob1b5c262020-03-05 14:29:47 +0100711 uint32_t i = 0;
712 const struct lys_module *mod;
713 struct lyd_node *first, *next, **first2;
Radek Krejcie7b95092019-05-15 11:03:07 +0200714
Michal Vaskob36053d2020-03-26 15:49:30 +0100715 /* init context and tree */
716 LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, data, &lydctx.xmlctx), cleanup);
717 lydctx.options = options;
Michal Vaskob1b5c262020-03-05 14:29:47 +0100718 *tree = NULL;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200719
Michal Vaskob1b5c262020-03-05 14:29:47 +0100720 /* parse XML data */
Michal Vasko1bf09392020-03-27 12:38:10 +0100721 LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, tree), cleanup);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100722
Michal Vasko9b368d32020-02-14 13:53:31 +0100723 if (!(options & LYD_OPT_PARSE_ONLY)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100724 next = *tree;
725 while (1) {
726 if (options & LYD_VALOPT_DATA_ONLY) {
727 mod = lyd_data_next_module(&next, &first);
728 } else {
729 mod = lyd_mod_next_module(next, NULL, 0, ctx, &i, &first);
730 }
731 if (!mod) {
732 break;
733 }
734 if (first == *tree) {
735 /* make sure first2 changes are carried to tree */
736 first2 = tree;
737 } else {
738 first2 = &first;
739 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100740
Michal Vaskob1b5c262020-03-05 14:29:47 +0100741 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
Michal Vasko1bf09392020-03-27 12:38:10 +0100742 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod), cleanup);
Michal Vasko9b368d32020-02-14 13:53:31 +0100743
Michal Vaskob1b5c262020-03-05 14:29:47 +0100744 /* add all top-level defaults for this module */
Michal Vaskob36053d2020-03-26 15:49:30 +0100745 ret = lyd_validate_defaults_r(NULL, first2, NULL, mod, &lydctx.unres_node_type, &lydctx.when_check,
Michal Vasko52927e22020-03-16 17:26:14 +0100746 options & LYD_VALOPT_MASK);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100747 LY_CHECK_GOTO(ret, cleanup);
748
749 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100750 ret = lyd_validate_unres(tree, &lydctx.when_check, &lydctx.unres_node_type, &lydctx.unres_meta_type, LYD_XML,
751 lydxml_resolve_prefix, lydctx.xmlctx);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100752 LY_CHECK_GOTO(ret, cleanup);
753
754 /* perform final validation that assumes the data tree is final */
Michal Vaskofea12c62020-03-30 11:00:15 +0200755 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, options & LYD_VALOPT_MASK), cleanup);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100756 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100757 }
758
Michal Vaskocde73ac2019-11-14 16:10:27 +0100759cleanup:
Michal Vasko9b368d32020-02-14 13:53:31 +0100760 /* there should be no unresolved types stored */
Michal Vaskob36053d2020-03-26 15:49:30 +0100761 assert(!(options & LYD_OPT_PARSE_ONLY) || (!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count
762 && !lydctx.when_check.count));
Michal Vasko9b368d32020-02-14 13:53:31 +0100763
Michal Vaskob36053d2020-03-26 15:49:30 +0100764 ly_set_erase(&lydctx.unres_node_type, NULL);
765 ly_set_erase(&lydctx.unres_meta_type, NULL);
766 ly_set_erase(&lydctx.when_check, NULL);
767 lyxml_ctx_free(lydctx.xmlctx);
Michal Vasko9f96a052020-03-10 09:41:45 +0100768 if (ret) {
769 lyd_free_all(*tree);
770 *tree = NULL;
771 }
772 return ret;
773}
Michal Vasko1bf09392020-03-27 12:38:10 +0100774
775static LY_ERR
776lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, struct lyd_node **envp)
777{
778 LY_ERR ret = LY_SUCCESS;
779 const struct lyxml_ns *ns = NULL;
780 struct ly_attr *attr = NULL;
781 const char *prefix;
782 size_t prefix_len;
783
784 *envp = NULL;
785
786 assert(xmlctx->status == LYXML_ELEMENT);
787 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
788 /* not the expected element */
789 return LY_SUCCESS;
790 }
791
792 prefix = xmlctx->prefix;
793 prefix_len = xmlctx->prefix_len;
794 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
795 if (!ns) {
796 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
797 prefix_len, prefix);
798 return LY_EVALID;
799 } else if (strcmp(ns->uri, uri)) {
800 /* different namespace */
801 return LY_SUCCESS;
802 }
803
804 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
805
806 /* create attributes */
807 if (xmlctx->status == LYXML_ATTRIBUTE) {
808 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
809 }
810
811 if (!xmlctx->ws_only) {
812 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
813 xmlctx->value_len, xmlctx->value, name);
814 ret = LY_EVALID;
815 goto cleanup;
816 }
817
818 /* parser next element */
819 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
820
821 /* create node */
822 ret = lyd_create_opaq(xmlctx->ctx, name, strlen(name), "", 0, NULL, LYD_XML, NULL, prefix, prefix_len, uri, envp);
823 LY_CHECK_GOTO(ret, cleanup);
824
825 /* assign atributes */
826 ((struct lyd_node_opaq *)(*envp))->attr = attr;
827 attr = NULL;
828
829cleanup:
830 ly_free_attr(xmlctx->ctx, attr, 1);
831 return ret;
832}
833
834LY_ERR
835lyd_parse_xml_rpc(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree, struct lyd_node **op)
836{
837 LY_ERR ret = LY_SUCCESS;
838 struct lyd_xml_ctx lydctx = {0};
839 struct lyd_node *rpc_e = NULL, *act_e = NULL;
840
841 /* init */
842 LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, data, &lydctx.xmlctx), cleanup);
843 lydctx.options = LYD_OPT_PARSE_ONLY | LYD_OPT_STRICT;
844 lydctx.int_opts = LYD_INTOPT_RPC;
845 *tree = NULL;
Michal Vaskocc048b22020-03-27 15:52:38 +0100846 if (op) {
847 *op = NULL;
848 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100849
850 /* parse "rpc", if any */
851 LY_CHECK_GOTO(ret = lydxml_envelope(lydctx.xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", &rpc_e), cleanup);
852
853 if (rpc_e) {
854 /* parse "action", if any */
855 LY_CHECK_GOTO(ret = lydxml_envelope(lydctx.xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", &act_e), cleanup);
856 }
857
858 /* parse the rest of data normally */
859 LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, tree), cleanup);
860
861 /* make sure we have parsed some operation */
862 if (!lydctx.op_ntf) {
863 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node.");
864 ret = LY_EVALID;
865 goto cleanup;
866 }
867
868 /* finish XML parsing and check operation type */
869 if (act_e) {
870 if (lydctx.xmlctx->status != LYXML_ELEM_CLOSE) {
871 assert(lydctx.xmlctx->status == LYXML_ELEMENT);
872 LOGVAL(ctx, LY_VLOG_LINE, &lydctx.xmlctx->line, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"action\".",
873 lydctx.xmlctx->name_len, lydctx.xmlctx->name);
874 ret = LY_EVALID;
875 goto cleanup;
876 } else if (lydctx.op_ntf->schema->nodetype != LYS_ACTION) {
877 LOGVAL(ctx, LY_VLOG_LYD, lydctx.op_ntf, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
878 lys_nodetype2str(lydctx.op_ntf->schema->nodetype));
879 ret = LY_EVALID;
880 goto cleanup;
881 }
882 LY_CHECK_GOTO(ret = lyxml_ctx_next(lydctx.xmlctx), cleanup);
883 }
884 if (rpc_e) {
885 if (lydctx.xmlctx->status != LYXML_ELEM_CLOSE) {
886 assert(lydctx.xmlctx->status == LYXML_ELEMENT);
887 LOGVAL(ctx, LY_VLOG_LINE, &lydctx.xmlctx->line, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"rpc\".",
888 lydctx.xmlctx->name_len, lydctx.xmlctx->name);
889 ret = LY_EVALID;
890 goto cleanup;
891 } else if (!act_e && (lydctx.op_ntf->schema->nodetype != LYS_RPC)) {
892 LOGVAL(ctx, LY_VLOG_LYD, lydctx.op_ntf, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
893 lys_nodetype2str(lydctx.op_ntf->schema->nodetype));
894 ret = LY_EVALID;
895 goto cleanup;
896 }
897 LY_CHECK_GOTO(ret = lyxml_ctx_next(lydctx.xmlctx), cleanup);
898 }
899
Michal Vaskocc048b22020-03-27 15:52:38 +0100900 if (op) {
901 *op = lydctx.op_ntf;
902 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100903 assert(*tree);
904 if (act_e) {
905 /* connect to the action */
906 lyd_insert_node(act_e, NULL, *tree);
907 *tree = act_e;
908 }
909 if (rpc_e) {
910 /* connect to the rpc */
911 lyd_insert_node(rpc_e, NULL, *tree);
912 *tree = rpc_e;
913 }
914
915cleanup:
916 /* we have used parse_only flag */
917 assert(!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count && !lydctx.when_check.count);
918 lyxml_ctx_free(lydctx.xmlctx);
919 if (ret) {
920 lyd_free_all(*tree);
921 lyd_free_tree(act_e);
922 lyd_free_tree(rpc_e);
923 *tree = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100924 }
925 return ret;
926}
Michal Vaskoa8edff02020-03-27 14:47:01 +0100927
928static LY_ERR
929lydxml_notif_envelope(struct lyxml_ctx *xmlctx, struct lyd_node **envp)
930{
931 LY_ERR ret = LY_SUCCESS;
932 const struct lyxml_ns *ns = NULL;
933 struct ly_attr *attr = NULL;
934 struct lyd_node *et;
935 const char *prefix;
936 size_t prefix_len;
937
938 *envp = NULL;
939
940 /* container envelope */
941 LY_CHECK_GOTO(ret = lydxml_envelope(xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0",
942 envp), cleanup);
943
944 /* no envelope, fine */
945 if (!*envp) {
946 goto cleanup;
947 }
948
949 /* child "eventTime" */
950 if ((xmlctx->status != LYXML_ELEMENT) || ly_strncmp("eventTime", xmlctx->name, xmlctx->name_len)) {
951 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Missing the \"eventTime\" element.");
952 ret = LY_EVALID;
953 goto cleanup;
954 }
955
956 prefix = xmlctx->prefix;
957 prefix_len = xmlctx->prefix_len;
958 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
959 if (!ns) {
960 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
961 prefix_len, prefix);
962 ret = LY_EVALID;
963 goto cleanup;
964 } else if (strcmp(ns->uri, "urn:ietf:params:xml:ns:netconf:notification:1.0")) {
965 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Invalid namespace \"%s\" of \"eventTime\".",
966 ns->uri);
967 ret = LY_EVALID;
968 goto cleanup;
969 }
970
971 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
972
973 /* create attributes */
974 if (xmlctx->status == LYXML_ATTRIBUTE) {
975 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
976 }
977
978 /* validate value */
979 /* TODO */
980 /*if (!xmlctx->ws_only) {
981 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
982 xmlctx->value_len, xmlctx->value, name);
983 ret = LY_EVALID;
984 goto cleanup;
985 }*/
986
987 /* create node */
988 ret = lyd_create_opaq(xmlctx->ctx, "eventTime", 9, xmlctx->value, xmlctx->value_len, NULL, LYD_XML, NULL,
989 prefix, prefix_len, ns->uri, &et);
990 LY_CHECK_GOTO(ret, cleanup);
991
992 /* assign atributes */
993 ((struct lyd_node_opaq *)et)->attr = attr;
994 attr = NULL;
995
996 /* insert */
997 lyd_insert_node(*envp, NULL, et);
998
999 /* finish parsing */
1000 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
1001 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1002 assert(xmlctx->status == LYXML_ELEMENT);
1003 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"eventTime\".",
1004 xmlctx->name_len, xmlctx->name);
1005 ret = LY_EVALID;
1006 goto cleanup;
1007 }
1008 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
1009
1010cleanup:
1011 if (ret) {
1012 lyd_free_tree(*envp);
1013 ly_free_attr(xmlctx->ctx, attr, 1);
1014 }
1015 return ret;
1016}
1017
1018LY_ERR
1019lyd_parse_xml_notif(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree, struct lyd_node **ntf)
1020{
1021 LY_ERR ret = LY_SUCCESS;
1022 struct lyd_xml_ctx lydctx = {0};
1023 struct lyd_node *ntf_e = NULL;
1024
1025 /* init */
1026 LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, data, &lydctx.xmlctx), cleanup);
1027 lydctx.options = LYD_OPT_PARSE_ONLY | LYD_OPT_STRICT;
1028 lydctx.int_opts = LYD_INTOPT_NOTIF;
1029 *tree = NULL;
Michal Vaskocc048b22020-03-27 15:52:38 +01001030 if (ntf) {
1031 *ntf = NULL;
1032 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001033
1034 /* parse "notification" and "eventTime", if present */
1035 LY_CHECK_GOTO(ret = lydxml_notif_envelope(lydctx.xmlctx, &ntf_e), cleanup);
1036
1037 /* parse the rest of data normally */
1038 LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, tree), cleanup);
1039
1040 /* make sure we have parsed some notification */
1041 if (!lydctx.op_ntf) {
1042 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1043 ret = LY_EVALID;
1044 goto cleanup;
1045 }
1046
1047 /* finish XML parsing */
1048 if (ntf_e) {
1049 if (lydctx.xmlctx->status != LYXML_ELEM_CLOSE) {
1050 assert(lydctx.xmlctx->status == LYXML_ELEMENT);
1051 LOGVAL(ctx, LY_VLOG_LINE, &lydctx.xmlctx->line, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"notification\".",
1052 lydctx.xmlctx->name_len, lydctx.xmlctx->name);
1053 ret = LY_EVALID;
1054 goto cleanup;
1055 }
1056 LY_CHECK_GOTO(ret = lyxml_ctx_next(lydctx.xmlctx), cleanup);
1057 }
1058
Michal Vaskocc048b22020-03-27 15:52:38 +01001059 if (ntf) {
1060 *ntf = lydctx.op_ntf;
1061 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001062 assert(*tree);
1063 if (ntf_e) {
1064 /* connect to the notification */
1065 lyd_insert_node(ntf_e, NULL, *tree);
1066 *tree = ntf_e;
1067 }
1068
1069cleanup:
1070 /* we have used parse_only flag */
1071 assert(!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count && !lydctx.when_check.count);
1072 lyxml_ctx_free(lydctx.xmlctx);
1073 if (ret) {
1074 lyd_free_all(*tree);
1075 lyd_free_tree(ntf_e);
1076 *tree = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001077 }
1078 return ret;
1079}
Michal Vasko1ce933a2020-03-30 12:38:22 +02001080
1081LY_ERR
1082lyd_parse_xml_reply(const struct lyd_node *request, const char *data, struct lyd_node **tree, struct lyd_node **op)
1083{
1084 LY_ERR ret = LY_SUCCESS;
1085 struct lyd_xml_ctx lydctx = {0};
1086 struct lyd_node *rpcr_e = NULL, *iter, *req_op, *rep_op;
1087
1088 /* init */
1089 LY_CHECK_GOTO(ret = lyxml_ctx_new(LYD_NODE_CTX(request), data, &lydctx.xmlctx), cleanup);
1090 lydctx.options = LYD_OPT_PARSE_ONLY | LYD_OPT_STRICT;
1091 lydctx.int_opts = LYD_INTOPT_REPLY;
1092 *tree = NULL;
1093 if (op) {
1094 *op = NULL;
1095 }
1096
1097 /* find request OP */
1098 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, iter, req_op) {
1099 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1100 break;
1101 }
1102 LYD_TREE_DFS_END(request, iter, req_op);
1103 }
1104 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1105 LOGERR(LYD_NODE_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
1106 ret = LY_EINVAL;
1107 goto cleanup;
1108 }
1109
1110 /* duplicate request OP with parents */
1111 rep_op = lyd_dup(req_op, NULL, LYD_DUP_WITH_PARENTS);
1112 LY_CHECK_ERR_GOTO(!rep_op, ret = LY_EMEM, cleanup);
1113
1114 /* parse "rpc-reply", if any */
1115 LY_CHECK_GOTO(ret = lydxml_envelope(lydctx.xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", &rpcr_e), cleanup);
1116
1117 /* parse the rest of data normally but connect them to the duplicated operation */
1118 LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op)), cleanup);
1119
1120 /* finish XML parsing and check operation type */
1121 if (rpcr_e) {
1122 if (lydctx.xmlctx->status != LYXML_ELEM_CLOSE) {
1123 assert(lydctx.xmlctx->status == LYXML_ELEMENT);
1124 LOGVAL(LYD_NODE_CTX(request), LY_VLOG_LINE, &lydctx.xmlctx->line, LYVE_SYNTAX,
1125 "Unexpected sibling element \"%.*s\" of \"rpc-reply\".", lydctx.xmlctx->name_len, lydctx.xmlctx->name);
1126 ret = LY_EVALID;
1127 goto cleanup;
1128 }
1129 LY_CHECK_GOTO(ret = lyxml_ctx_next(lydctx.xmlctx), cleanup);
1130 }
1131
1132 if (op) {
1133 *op = rep_op;
1134 }
1135 for (iter = rep_op; iter->parent; iter = (struct lyd_node *)iter->parent);
1136 *tree = iter;
1137 if (rpcr_e) {
1138 /* connect to the operation */
1139 lyd_insert_node(rpcr_e, NULL, *tree);
1140 *tree = rpcr_e;
1141 }
1142
1143cleanup:
1144 /* we have used parse_only flag */
1145 assert(!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count && !lydctx.when_check.count);
1146 lyxml_ctx_free(lydctx.xmlctx);
1147 if (ret) {
1148 lyd_free_all(rep_op);
1149 lyd_free_tree(rpcr_e);
1150 }
1151 return ret;
1152}