blob: 8548da3f6db233c46fffb7f8280cd1269bd44eb7 [file] [log] [blame]
Radek Krejci1721c012015-07-08 12:52:33 +02001/**
Michal Vasko45a0ef32016-09-22 11:07:38 +02002 * @file parser_xml.c
Radek Krejci1721c012015-07-08 12:52:33 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief XML data parser for libyang
5 *
Michal Vasko7675c622017-03-02 10:50:07 +01006 * Copyright (c) 2015 - 2017 CESNET, z.s.p.o.
Radek Krejci1721c012015-07-08 12:52:33 +02007 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci1721c012015-07-08 12:52:33 +020013 */
14
15#include <assert.h>
Radek Krejci3e3affe2015-07-09 15:38:40 +020016#include <ctype.h>
Radek Krejci1721c012015-07-08 12:52:33 +020017#include <errno.h>
Radek Krejci4a49bdf2016-01-12 17:17:01 +010018#include <stdarg.h>
Radek Krejci1721c012015-07-08 12:52:33 +020019#include <stdlib.h>
20#include <string.h>
Michal Vasko8bcdf292015-08-19 14:04:43 +020021#include <limits.h>
Radek Krejci1721c012015-07-08 12:52:33 +020022
Radek Krejci998a0b82015-08-17 13:14:36 +020023#include "libyang.h"
24#include "common.h"
25#include "context.h"
Michal Vaskob7982322015-10-16 13:56:12 +020026#include "parser.h"
Radek Krejci998a0b82015-08-17 13:14:36 +020027#include "tree_internal.h"
28#include "validation.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020029#include "xml_internal.h"
Radek Krejci1721c012015-07-08 12:52:33 +020030
Michal Vasko0d343d12015-08-24 14:57:36 +020031/* does not log */
Radek Krejci76512572015-08-04 09:47:08 +020032static struct lys_node *
Radek Krejci4a49bdf2016-01-12 17:17:01 +010033xml_data_search_schemanode(struct lyxml_elem *xml, struct lys_node *start, int options)
Radek Krejci1721c012015-07-08 12:52:33 +020034{
Radek Krejci76512572015-08-04 09:47:08 +020035 struct lys_node *result, *aux;
Radek Krejci1721c012015-07-08 12:52:33 +020036
37 LY_TREE_FOR(start, result) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +010038 /* skip groupings ... */
Radek Krejci76512572015-08-04 09:47:08 +020039 if (result->nodetype == LYS_GROUPING) {
Radek Krejci1721c012015-07-08 12:52:33 +020040 continue;
Radek Krejci4a49bdf2016-01-12 17:17:01 +010041 /* ... and output in case of RPC ... */
42 } else if (result->nodetype == LYS_OUTPUT && (options & LYD_OPT_RPC)) {
43 continue;
44 /* ... and input in case of RPC reply */
45 } else if (result->nodetype == LYS_INPUT && (options & LYD_OPT_RPCREPLY)) {
46 continue;
Radek Krejci1721c012015-07-08 12:52:33 +020047 }
48
Radek Krejcifb54be42015-10-02 15:21:16 +020049 /* go into cases, choices, uses and in RPCs into input and output */
50 if (result->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES | LYS_INPUT | LYS_OUTPUT)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +010051 aux = xml_data_search_schemanode(xml, result->child, options);
Radek Krejci1721c012015-07-08 12:52:33 +020052 if (aux) {
53 /* we have matching result */
54 return aux;
55 }
56 /* else, continue with next schema node */
57 continue;
58 }
59
60 /* match data nodes */
Radek Krejci749190d2016-02-18 16:26:25 +010061 if (ly_strequal(result->name, xml->name, 1)) {
Radek Krejci1721c012015-07-08 12:52:33 +020062 /* names matches, what about namespaces? */
Radek Krejci1a6303d2016-11-04 12:42:46 +010063 if (ly_strequal(lys_main_module(result->module)->ns, xml->ns->value, 1)) {
Radek Krejci1721c012015-07-08 12:52:33 +020064 /* we have matching result */
65 return result;
66 }
67 /* else, continue with next schema node */
68 continue;
69 }
70 }
71
72 /* no match */
73 return NULL;
74}
75
Michal Vasko0d343d12015-08-24 14:57:36 +020076/* logs directly */
Radek Krejcie4748472015-07-08 18:00:22 +020077static int
Michal Vaskoe3886bb2017-01-02 11:33:28 +010078xml_get_value(struct lyd_node *node, struct lyxml_elem *xml, int editbits)
Michal Vasko07471a52015-07-16 11:18:48 +020079{
Michal Vasko4c183312015-09-25 10:41:47 +020080 struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node;
Michal Vasko23b61ec2015-08-19 11:19:50 +020081
Radek Krejci0b7704f2016-03-18 12:16:14 +010082 assert(node && (node->schema->nodetype & (LYS_LEAFLIST | LYS_LEAF)) && xml);
Radek Krejci5a988152015-07-15 11:16:26 +020083
Michal Vaskod27602c2016-12-20 11:11:17 +010084 leaf->value_str = lydict_insert(node->schema->module->ctx, xml->content, 0);
Radek Krejcie4748472015-07-08 18:00:22 +020085
Radek Krejci06b45272017-02-24 16:15:02 +010086 if ((editbits & 0x20) && (node->schema->nodetype & LYS_LEAF) && (!leaf->value_str || !leaf->value_str[0])) {
Radek Krejci1fe9ac02016-09-23 09:38:21 +020087 /* we have edit-config leaf/leaf-list with delete operation and no (empty) value,
88 * this is not a bug since the node is just used as a kind of selection node */
89 leaf->value_type = LY_TYPE_ERR;
90 return EXIT_SUCCESS;
91 }
92
Radek Krejci1899d6a2016-11-03 13:48:07 +010093 /* the value is here converted to a JSON format if needed in case of LY_TYPE_IDENT and LY_TYPE_INST or to a
94 * canonical form of the value */
Radek Krejcia571d942017-02-24 09:26:49 +010095 if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, xml, leaf, NULL, 1, 0)) {
Michal Vasko493bea72015-07-16 16:08:12 +020096 return EXIT_FAILURE;
Radek Krejcie4748472015-07-08 18:00:22 +020097 }
98
99 return EXIT_SUCCESS;
100}
101
Michal Vasko0d343d12015-08-24 14:57:36 +0200102/* logs directly */
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100103static int
Radek Krejcibd930122016-08-10 13:28:26 +0200104xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, struct lyd_node *parent, struct lyd_node *first_sibling,
105 struct lyd_node *prev, int options, struct unres_data *unres, struct lyd_node **result,
Michal Vaskob15cae22016-09-15 09:40:56 +0200106 struct lyd_node **act_notif)
Radek Krejci1721c012015-07-08 12:52:33 +0200107{
Michal Vaskof53187d2017-01-13 13:23:14 +0100108 const struct lys_module *mod = NULL;
Radek Krejcibd930122016-08-10 13:28:26 +0200109 struct lyd_node *diter, *dlast;
Michal Vasko5e523b62016-08-26 16:23:15 +0200110 struct lys_node *schema = NULL, *target;
111 struct lys_node_augment *aug;
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100112 struct lyd_attr *dattr, *dattr_iter;
Radek Krejcia5241e52015-08-19 15:09:31 +0200113 struct lyxml_attr *attr;
Michal Vaskof748dbc2016-04-05 11:27:47 +0200114 struct lyxml_elem *child, *next;
Michal Vasko7675c622017-03-02 10:50:07 +0100115 int i, j, havechildren, r, editbits = 0, pos, filterflag = 0, found;
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100116 int ret = 0;
Radek Krejciabb7b582016-04-20 16:15:47 +0200117 const char *str = NULL;
Radek Krejci1721c012015-07-08 12:52:33 +0200118
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100119 assert(xml);
120 assert(result);
121 *result = NULL;
122
Radek Krejcie1bacd72017-03-01 13:18:46 +0100123 if (xml->flags & LYXML_ELEM_MIXED) {
124 if (options & LYD_OPT_STRICT) {
125 LOGVAL(LYE_XML_INVAL, LY_VLOG_XML, xml, "XML element with mixed content");
126 return -1;
127 } else {
128 return 0;
129 }
130 }
131
Radek Krejci1721c012015-07-08 12:52:33 +0200132 if (!xml->ns || !xml->ns->value) {
Radek Krejci2342cf62016-01-29 16:48:23 +0100133 if (options & LYD_OPT_STRICT) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100134 LOGVAL(LYE_XML_MISS, LY_VLOG_XML, xml, "element's", "namespace");
Radek Krejci2342cf62016-01-29 16:48:23 +0100135 return -1;
136 } else {
137 return 0;
138 }
Radek Krejci1721c012015-07-08 12:52:33 +0200139 }
140
141 /* find schema node */
Michal Vaskob1b19442016-07-13 12:26:01 +0200142 if (!parent) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100143 mod = ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL);
144 if (ctx->data_clb) {
145 if (!mod) {
146 mod = ctx->data_clb(ctx, NULL, xml->ns->value, 0, ctx->data_clb_data);
147 } else if (!mod->implemented) {
148 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Radek Krejci27fe55e2016-09-13 17:13:35 +0200149 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100150 }
151
152 /* get the proper schema node */
153 if (mod && mod->implemented && !mod->disabled) {
154 schema = xml_data_search_schemanode(xml, mod->data, options);
155 if (!schema) {
156 /* it still can be the specific case of this module containing an augment of another module
157 * top-level choice or top-level choice's case, bleh */
158 for (j = 0; j < mod->augment_size; ++j) {
159 aug = &mod->augment[j];
160 target = aug->target;
161 if (target->nodetype & (LYS_CHOICE | LYS_CASE)) {
162 /* 1) okay, the target is choice or case */
163 while (target && (target->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES))) {
164 target = lys_parent(target);
165 }
166 /* 2) now, the data node will be top-level, there are only non-data schema nodes */
167 if (!target) {
168 while ((schema = (struct lys_node *)lys_getnext(schema, (struct lys_node *)aug, NULL, 0))) {
169 /* 3) alright, even the name matches, we found our schema node */
170 if (ly_strequal(schema->name, xml->name, 1)) {
171 break;
Michal Vasko5e523b62016-08-26 16:23:15 +0200172 }
173 }
174 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100175 }
Michal Vasko5e523b62016-08-26 16:23:15 +0200176
Michal Vaskof53187d2017-01-13 13:23:14 +0100177 if (schema) {
178 break;
Michal Vasko5e523b62016-08-26 16:23:15 +0200179 }
180 }
Radek Krejci1721c012015-07-08 12:52:33 +0200181 }
182 }
183 } else {
184 /* parsing some internal node, we start with parent's schema pointer */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100185 schema = xml_data_search_schemanode(xml, parent->schema->child, options);
Michal Vaskof53187d2017-01-13 13:23:14 +0100186
Michal Vaskoa358f942017-05-09 10:45:33 +0200187 if (ctx->data_clb) {
188 if (schema && !lys_node_module(schema)->implemented) {
Michal Vaskoad43c182017-01-23 09:55:28 +0100189 ctx->data_clb(ctx, lys_node_module(schema)->name, lys_node_module(schema)->ns,
190 LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Michal Vaskoa358f942017-05-09 10:45:33 +0200191 } else if (!schema) {
192 if (ctx->data_clb(ctx, NULL, xml->ns->value, 0, ctx->data_clb_data)) {
193 /* context was updated, so try to find the schema node again */
194 schema = xml_data_search_schemanode(xml, parent->schema->child, options);
195 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100196 }
Radek Krejci25b9fd32015-08-10 15:06:07 +0200197 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100198 }
199
200 mod = lys_node_module(schema);
201 if (!mod || !mod->implemented || mod->disabled) {
Radek Krejci27fe55e2016-09-13 17:13:35 +0200202 if (options & LYD_OPT_STRICT) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100203 LOGVAL(LYE_INELEM, (parent ? LY_VLOG_LYD : LY_VLOG_NONE), parent, xml->name);
Radek Krejci27fe55e2016-09-13 17:13:35 +0200204 return -1;
205 } else {
206 return 0;
207 }
Radek Krejci1721c012015-07-08 12:52:33 +0200208 }
209
Radek Krejciadb57612016-02-16 13:34:34 +0100210 /* create the element structure */
Radek Krejcib9930252015-07-08 15:47:45 +0200211 switch (schema->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +0200212 case LYS_CONTAINER:
Radek Krejci27aaa732015-09-04 15:24:04 +0200213 case LYS_LIST:
Michal Vasko2f30e572015-10-01 16:00:38 +0200214 case LYS_NOTIF:
215 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200216 case LYS_ACTION:
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100217 *result = calloc(1, sizeof **result);
Michal Vaskoab8e4402015-07-17 12:54:28 +0200218 havechildren = 1;
Radek Krejcib9930252015-07-08 15:47:45 +0200219 break;
Radek Krejci76512572015-08-04 09:47:08 +0200220 case LYS_LEAF:
Radek Krejci76512572015-08-04 09:47:08 +0200221 case LYS_LEAFLIST:
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100222 *result = calloc(1, sizeof(struct lyd_node_leaf_list));
Radek Krejcie4748472015-07-08 18:00:22 +0200223 havechildren = 0;
224 break;
Radek Krejci76512572015-08-04 09:47:08 +0200225 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200226 case LYS_ANYDATA:
227 *result = calloc(1, sizeof(struct lyd_node_anydata));
Michal Vaskoab8e4402015-07-17 12:54:28 +0200228 havechildren = 0;
229 break;
Radek Krejcib9930252015-07-08 15:47:45 +0200230 default:
Michal Vasko0c888fd2015-08-11 15:54:08 +0200231 LOGINT;
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100232 return -1;
Radek Krejcib9930252015-07-08 15:47:45 +0200233 }
Radek Krejcia8d111f2017-05-31 13:57:37 +0200234 LY_CHECK_ERR_RETURN(!(*result), LOGMEM, -1);
Michal Vasko253035f2015-12-17 16:58:13 +0100235
Radek Krejci61767ca2016-09-19 14:21:55 +0200236 (*result)->prev = *result;
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100237 (*result)->schema = schema;
Radek Krejci61767ca2016-09-19 14:21:55 +0200238 (*result)->parent = parent;
239 diter = NULL;
240 if (parent && parent->child && schema->nodetype == LYS_LEAF && parent->schema->nodetype == LYS_LIST &&
241 (pos = lys_is_key((struct lys_node_list *)parent->schema, (struct lys_node_leaf *)schema))) {
242 /* it is key and we need to insert it into a correct place */
243 for (i = 0, diter = parent->child;
244 diter && i < (pos - 1) && diter->schema->nodetype == LYS_LEAF &&
245 lys_is_key((struct lys_node_list *)parent->schema, (struct lys_node_leaf *)diter->schema);
246 i++, diter = diter->next);
247 if (diter) {
248 /* out of order insertion - insert list's key to the correct position, before the diter */
249 if (options & LYD_OPT_STRICT) {
250 LOGVAL(LYE_INORDER, LY_VLOG_LYD, *result, schema->name, diter->schema->name);
Michal Vasko51e5c582017-01-19 14:16:39 +0100251 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid position of the key \"%s\" in a list \"%s\".",
Radek Krejci61767ca2016-09-19 14:21:55 +0200252 schema->name, parent->schema->name);
253 free(*result);
254 *result = NULL;
255 return -1;
256 } else {
257 LOGWRN("Invalid position of the key \"%s\" in a list \"%s\".", schema->name, parent->schema->name)
258 }
259 if (parent->child == diter) {
260 parent->child = *result;
261 /* update first_sibling */
262 first_sibling = *result;
263 }
264 if (diter->prev->next) {
265 diter->prev->next = *result;
266 }
267 (*result)->prev = diter->prev;
268 diter->prev = *result;
269 (*result)->next = diter;
270 }
271 }
272 if (!diter) {
273 /* simplified (faster) insert as the last node */
274 if (parent && !parent->child) {
275 parent->child = *result;
276 }
277 if (prev) {
278 (*result)->prev = prev;
279 prev->next = *result;
280
281 /* fix the "last" pointer */
282 first_sibling->prev = *result;
283 } else {
284 (*result)->prev = *result;
285 first_sibling = *result;
286 }
287 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100288 (*result)->validity = ly_new_node_validity((*result)->schema);
Radek Krejci46165822016-08-26 14:06:27 +0200289 if (resolve_applies_when(schema, 0, NULL)) {
Radek Krejci0b7704f2016-03-18 12:16:14 +0100290 (*result)->when_status = LYD_WHEN;
291 }
Radek Krejci1721c012015-07-08 12:52:33 +0200292
Radek Krejci146e3fc2017-02-24 13:41:00 +0100293 /* process attributes */
Radek Krejci998a7502015-10-26 15:54:33 +0100294 for (attr = xml->attr; attr; attr = attr->next) {
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100295 if (attr->type != LYXML_ATTR_STD) {
296 continue;
297 } else if (!attr->ns) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100298 if ((*result)->schema->nodetype == LYS_ANYXML &&
299 ly_strequal((*result)->schema->name, "filter", 0) &&
Michal Vaskoaa98eb72017-03-24 11:23:34 +0100300 (ly_strequal((*result)->schema->module->name, "ietf-netconf", 0) ||
301 ly_strequal((*result)->schema->module->name, "notifications", 0))) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100302 /* NETCONF filter's attributes, which we implement as non-standard annotations,
303 * they are unqualified (no namespace), but we know that we have internally defined
Michal Vaskoaa98eb72017-03-24 11:23:34 +0100304 * them in the ietf-netconf module */
305 str = "urn:ietf:params:xml:ns:netconf:base:1.0";
Radek Krejci598dbcb2017-02-24 15:14:15 +0100306 filterflag = 1;
Radek Krejci9a5daea2016-03-02 16:49:40 +0100307 } else {
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100308 /* garbage */
309 goto attr_error;
Radek Krejci532e5e92017-02-22 12:59:24 +0100310 }
Michal Vasko7675c622017-03-02 10:50:07 +0100311 } else {
312 str = attr->ns->value;
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100313 }
314
Michal Vasko7675c622017-03-02 10:50:07 +0100315 r = lyp_fill_attr(ctx, *result, str, NULL, attr->name, attr->value, xml, &dattr);
316 if (r == -1) {
Michal Vasko253035f2015-12-17 16:58:13 +0100317 goto error;
Michal Vasko7675c622017-03-02 10:50:07 +0100318 } else if (r == 1) {
319attr_error:
320 if (options & LYD_OPT_STRICT) {
321 LOGVAL(LYE_INMETA, LY_VLOG_LYD, *result, (attr->ns ? attr->ns->prefix : "<none>"), attr->name, attr->value);
322 goto error;
323 }
324
325 LOGWRN("Unknown \"%s:%s\" metadata with value \"%s\", ignoring.",
326 (attr->ns ? attr->ns->prefix : "<none>"), attr->name, attr->value);
327 continue;
Michal Vasko253035f2015-12-17 16:58:13 +0100328 }
Radek Krejci532e5e92017-02-22 12:59:24 +0100329
Michal Vasko7675c622017-03-02 10:50:07 +0100330 /* special case of xpath in the value, we want to convert it to JSON */
331 if (filterflag && !strcmp(attr->name, "select")) {
Michal Vasko1b6ca962017-08-03 14:23:09 +0200332 dattr->value.string = transform_xml2json(ctx, dattr->value_str, xml, 0, 0, 1);
Radek Krejci598dbcb2017-02-24 15:14:15 +0100333 if (!dattr->value.string) {
334 /* problem with resolving value as xpath */
Michal Vasko7675c622017-03-02 10:50:07 +0100335 dattr->value.string = dattr->value_str;
Radek Krejci598dbcb2017-02-24 15:14:15 +0100336 goto error;
337 }
338 lydict_remove(ctx, dattr->value_str);
339 dattr->value_str = dattr->value.string;
Radek Krejcia571d942017-02-24 09:26:49 +0100340 }
341
Radek Krejci532e5e92017-02-22 12:59:24 +0100342 /* insert into the data node */
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100343 if (!(*result)->attr) {
344 (*result)->attr = dattr;
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100345 } else {
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100346 for (dattr_iter = (*result)->attr; dattr_iter->next; dattr_iter = dattr_iter->next);
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100347 dattr_iter->next = dattr;
Radek Krejci998a7502015-10-26 15:54:33 +0100348 }
Radek Krejci532e5e92017-02-22 12:59:24 +0100349 continue;
Radek Krejci998a7502015-10-26 15:54:33 +0100350 }
Michal Vasko4ff7b072015-08-21 09:05:03 +0200351
Radek Krejci146e3fc2017-02-24 13:41:00 +0100352 /* check insert attribute and its values */
353 if (options & LYD_OPT_EDIT) {
Michal Vasko7675c622017-03-02 10:50:07 +0100354 if (lyp_check_edit_attr(ctx, (*result)->attr, *result, &editbits)) {
355 goto error;
356 }
Radek Krejci146e3fc2017-02-24 13:41:00 +0100357
Michal Vasko7675c622017-03-02 10:50:07 +0100358 /* check correct filter extension attributes */
359 } else if (filterflag) {
360 found = 0; /* 0 - nothing, 1 - type subtree, 2 - type xpath, 3 - select, 4 - type xpath + select */
361 LY_TREE_FOR((*result)->attr, dattr_iter) {
362 if (!strcmp(dattr_iter->name, "type")) {
363 if ((found == 1) || (found == 2) || (found == 4)) {
364 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "type", xml->name);
Radek Krejcib3d905d2017-02-24 16:15:44 +0100365 goto error;
Radek Krejci146e3fc2017-02-24 13:41:00 +0100366 }
Michal Vasko7675c622017-03-02 10:50:07 +0100367 switch (dattr_iter->value.enm->value) {
368 case 0:
369 /* subtree */
370 if (found == 3) {
371 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), dattr_iter->name);
372 goto error;
373 }
Radek Krejci06b45272017-02-24 16:15:02 +0100374
Michal Vasko7675c622017-03-02 10:50:07 +0100375 assert(!found);
376 found = 1;
377 break;
378 case 1:
379 /* xpath */
380 if (found == 3) {
381 found = 4;
382 } else {
383 assert(!found);
384 found = 2;
385 }
386 break;
387 default:
388 LOGINT;
Radek Krejcib3d905d2017-02-24 16:15:44 +0100389 goto error;
Radek Krejci146e3fc2017-02-24 13:41:00 +0100390 }
Michal Vasko7675c622017-03-02 10:50:07 +0100391 } else if (!strcmp(dattr_iter->name, "select")) {
392 switch (found) {
393 case 0:
394 found = 3;
395 break;
396 case 1:
397 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), dattr_iter->name);
Radek Krejcib3d905d2017-02-24 16:15:44 +0100398 goto error;
Michal Vasko7675c622017-03-02 10:50:07 +0100399 case 2:
400 found = 4;
401 break;
402 case 3:
403 case 4:
404 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "select", xml->name);
405 goto error;
406 default:
407 LOGINT;
Radek Krejcib3d905d2017-02-24 16:15:44 +0100408 goto error;
Radek Krejci146e3fc2017-02-24 13:41:00 +0100409 }
Radek Krejci146e3fc2017-02-24 13:41:00 +0100410 }
411 }
412
Michal Vasko7675c622017-03-02 10:50:07 +0100413 /* check if what we found is correct */
414 switch (found) {
415 case 1:
416 case 4:
417 /* ok */
418 break;
419 case 2:
420 LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "select", xml->name);
Radek Krejcib3d905d2017-02-24 16:15:44 +0100421 goto error;
Michal Vasko7675c622017-03-02 10:50:07 +0100422 case 3:
423 LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "type", xml->name);
Radek Krejcib3d905d2017-02-24 16:15:44 +0100424 goto error;
Michal Vasko7675c622017-03-02 10:50:07 +0100425 default:
426 LOGINT;
Radek Krejcib3d905d2017-02-24 16:15:44 +0100427 goto error;
Radek Krejci146e3fc2017-02-24 13:41:00 +0100428 }
429 }
430
431 /* type specific processing */
432 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
433 /* type detection and assigning the value */
434 if (xml_get_value(*result, xml, editbits)) {
435 goto error;
436 }
437 } else if (schema->nodetype & LYS_ANYDATA) {
438 /* store children values */
439 if (xml->child) {
440 child = xml->child;
441 /* manually unlink all siblings and correct namespaces */
442 xml->child = NULL;
443 LY_TREE_FOR(child, next) {
444 next->parent = NULL;
445 lyxml_correct_elem_ns(ctx, next, 1, 1);
446 }
447
448 ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_XML;
449 ((struct lyd_node_anydata *)*result)->value.xml = child;
450 } else {
451 ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_CONSTSTRING;
452 ((struct lyd_node_anydata *)*result)->value.str = lydict_insert(ctx, xml->content, 0);
453 }
454 } else if (schema->nodetype & (LYS_RPC | LYS_ACTION)) {
455 if (!(options & LYD_OPT_RPC) || *act_notif) {
456 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*result), schema->name);
457 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected %s node \"%s\".",
458 (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name);
459 goto error;
460 }
461 *act_notif = *result;
462 } else if (schema->nodetype == LYS_NOTIF) {
463 if (!(options & LYD_OPT_NOTIF) || *act_notif) {
464 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*result), schema->name);
465 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected notification node \"%s\".", schema->name);
466 goto error;
467 }
468 *act_notif = *result;
469 }
470
471 /* first part of validation checks */
472 if (lyv_data_context(*result, options, unres)) {
473 goto error;
474 }
475
Radek Krejci14c00092015-11-01 11:03:24 +0100476 /* process children */
477 if (havechildren && xml->child) {
478 diter = dlast = NULL;
479 LY_TREE_FOR_SAFE(xml->child, next, child) {
Michal Vaskoa51c2002017-03-01 10:14:06 +0100480 r = xml_parse_data(ctx, child, *result, (*result)->child, dlast, options, unres, &diter, act_notif);
Radek Krejci14c00092015-11-01 11:03:24 +0100481 if (r) {
482 goto error;
Radek Krejci86538212015-12-17 15:59:01 +0100483 } else if (options & LYD_OPT_DESTRUCT) {
484 lyxml_free(ctx, child);
Radek Krejci14c00092015-11-01 11:03:24 +0100485 }
Radek Krejci61767ca2016-09-19 14:21:55 +0200486 if (diter && !diter->next) {
487 /* the child was parsed/created and it was placed as the last child. The child can be inserted
488 * out of order (not as the last one) in case it is a list's key present out of the correct order */
Radek Krejci14c00092015-11-01 11:03:24 +0100489 dlast = diter;
490 }
491 }
492 }
493
Radek Krejcifb7156e2016-10-27 13:39:56 +0200494 /* if we have empty non-presence container, we keep it, but mark it as default */
Radek Krejci2537fd32016-09-07 16:22:41 +0200495 if (schema->nodetype == LYS_CONTAINER && !(*result)->child &&
Radek Krejcid3e73722016-05-23 12:24:55 +0200496 !(*result)->attr && !((struct lys_node_container *)schema)->presence) {
Radek Krejcifb7156e2016-10-27 13:39:56 +0200497 (*result)->dflt = 1;
Radek Krejci0c0086a2016-03-24 15:20:28 +0100498 }
499
Radek Krejcicf509982015-12-15 09:22:44 +0100500 /* rest of validation checks */
Radek Krejci00a0e712016-10-26 10:24:46 +0200501 ly_err_clean(1);
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100502 if (lyv_data_content(*result, options, unres) ||
503 lyv_multicases(*result, NULL, prev ? &first_sibling : NULL, 0, NULL)) {
Radek Krejcieab784a2015-08-27 09:56:53 +0200504 if (ly_errno) {
Radek Krejcib1c12512015-08-11 11:22:04 +0200505 goto error;
Radek Krejci1b0d01a2015-08-19 17:00:35 +0200506 } else {
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100507 goto clear;
Radek Krejcida374342015-08-19 13:33:22 +0200508 }
Radek Krejci78ce8612015-08-18 14:31:05 +0200509 }
510
Radek Krejcica7efb72016-01-18 13:06:01 +0100511 /* validation successful */
Radek Krejci63b79c82016-08-10 10:09:33 +0200512 if ((*result)->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
513 /* postpone checking when there will be all list/leaflist instances */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100514 (*result)->validity |= LYD_VAL_UNIQUE;
Radek Krejci63b79c82016-08-10 10:09:33 +0200515 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100516
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100517 return ret;
Radek Krejci3e3affe2015-07-09 15:38:40 +0200518
519error:
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100520 ret--;
521
522clear:
Radek Krejcieab784a2015-08-27 09:56:53 +0200523 /* cleanup */
Radek Krejci0c0086a2016-03-24 15:20:28 +0100524 for (i = unres->count - 1; i >= 0; i--) {
525 /* remove unres items connected with the node being removed */
526 if (unres->node[i] == *result) {
527 unres_data_del(unres, i);
528 }
529 }
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100530 lyd_free(*result);
531 *result = NULL;
Radek Krejci1b0d01a2015-08-19 17:00:35 +0200532
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100533 return ret;
Radek Krejci1721c012015-07-08 12:52:33 +0200534}
535
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100536API struct lyd_node *
537lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options, ...)
Radek Krejcic6704c82015-10-06 11:12:45 +0200538{
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100539 va_list ap;
Radek Krejci63b79c82016-08-10 10:09:33 +0200540 int r, i;
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100541 struct unres_data *unres = NULL;
Michal Vasko945b96b2016-10-18 11:49:12 +0200542 const struct lyd_node *rpc_act = NULL, *data_tree = NULL;
543 struct lyd_node *result = NULL, *iter, *last, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL;
Michal Vaskoa8756562017-05-03 14:42:23 +0200544 struct lyxml_elem *xmlstart, *xmlelem, *xmlaux, *xmlfree = NULL;
Radek Krejci63b79c82016-08-10 10:09:33 +0200545 struct ly_set *set;
Radek Krejcic6704c82015-10-06 11:12:45 +0200546
Radek Krejci00a0e712016-10-26 10:24:46 +0200547 ly_err_clean(1);
Radek Krejci2342cf62016-01-29 16:48:23 +0100548
Radek Krejcic6704c82015-10-06 11:12:45 +0200549 if (!ctx || !root) {
550 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
551 return NULL;
552 }
553
Michal Vaskoe746a502017-08-11 10:56:40 +0200554 if (lyp_data_check_options(options, __func__)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100555 return NULL;
556 }
557
Radek Krejci19dbedb2017-08-08 13:54:31 +0200558 if (!(*root) && !(options & LYD_OPT_RPCREPLY)) {
559 /* empty tree */
560 if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
561 /* error, top level node identify RPC and Notification */
562 LOGERR(LY_EINVAL, "%s: *root identifies RPC/Notification so it cannot be NULL.", __func__);
563 return NULL;
564 } else if (!(options & LYD_OPT_RPCREPLY)) {
565 /* others - no work is needed, just check for missing mandatory nodes */
566 lyd_validate(&result, options, ctx);
567 return result;
568 }
569 /* continue with empty RPC reply, for which we need RPC */
Radek Krejcia6939c32016-03-24 15:19:09 +0100570 }
571
Michal Vasko24d982f2016-04-18 15:13:58 +0200572 unres = calloc(1, sizeof *unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200573 LY_CHECK_ERR_RETURN(!unres, LOGMEM, NULL);
Michal Vasko24d982f2016-04-18 15:13:58 +0200574
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100575 va_start(ap, options);
576 if (options & LYD_OPT_RPCREPLY) {
Michal Vasko945b96b2016-10-18 11:49:12 +0200577 rpc_act = va_arg(ap, const struct lyd_node *);
578 if (!rpc_act || rpc_act->parent || !(rpc_act->schema->nodetype & (LYS_RPC | LYS_LIST | LYS_CONTAINER))) {
579 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__);
Michal Vasko24d982f2016-04-18 15:13:58 +0200580 goto error;
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100581 }
Michal Vasko945b96b2016-10-18 11:49:12 +0200582 if (rpc_act->schema->nodetype == LYS_RPC) {
583 /* RPC request */
584 reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0);
585 } else {
586 /* action request */
587 reply_top = lyd_dup(rpc_act, 1);
588 LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) {
589 if (reply_parent->schema->nodetype == LYS_ACTION) {
590 break;
591 }
592 LY_TREE_DFS_END(reply_top, iter, reply_parent);
593 }
594 if (!reply_parent) {
595 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__);
596 lyd_free_withsiblings(reply_top);
597 goto error;
598 }
599 lyd_free_withsiblings(reply_parent->child);
600 }
Michal Vasko45e23652016-09-21 11:24:32 +0200601 }
602 if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF | LYD_OPT_RPCREPLY)) {
Michal Vasko945b96b2016-10-18 11:49:12 +0200603 data_tree = va_arg(ap, const struct lyd_node *);
Michal Vasko6b44d712016-09-12 16:25:46 +0200604 if (data_tree) {
Michal Vaskod6a48062017-08-11 14:28:56 +0200605 if (options & LYD_OPT_NOEXTDEPS) {
606 LOGERR(LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree and LYD_OPT_NOEXTDEPS set).",
607 __func__);
Michal Vaskodfbac232017-08-11 14:39:02 +0200608 goto error;
Michal Vaskod6a48062017-08-11 14:28:56 +0200609 }
610
Michal Vasko945b96b2016-10-18 11:49:12 +0200611 LY_TREE_FOR((struct lyd_node *)data_tree, iter) {
Michal Vasko6b44d712016-09-12 16:25:46 +0200612 if (iter->parent) {
613 /* a sibling is not top-level */
614 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__);
615 goto error;
616 }
617 }
618
619 /* move it to the beginning */
620 for (; data_tree->prev->next; data_tree = data_tree->prev);
621
622 /* LYD_OPT_NOSIBLINGS cannot be set in this case */
623 if (options & LYD_OPT_NOSIBLINGS) {
624 LOGERR(LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__);
625 goto error;
626 }
627 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100628 }
629
Radek Krejci19dbedb2017-08-08 13:54:31 +0200630 if ((*root) && !(options & LYD_OPT_NOSIBLINGS)) {
Radek Krejci86538212015-12-17 15:59:01 +0100631 /* locate the first root to process */
632 if ((*root)->parent) {
633 xmlstart = (*root)->parent->child;
634 } else {
635 xmlstart = *root;
636 while(xmlstart->prev->next) {
637 xmlstart = xmlstart->prev;
638 }
Radek Krejci04b97de2015-10-31 23:09:15 +0100639 }
Radek Krejci86538212015-12-17 15:59:01 +0100640 } else {
641 xmlstart = *root;
642 }
Radek Krejci86538212015-12-17 15:59:01 +0100643
Michal Vaskob1b19442016-07-13 12:26:01 +0200644 if ((options & LYD_OPT_RPC)
645 && !strcmp(xmlstart->name, "action") && !strcmp(xmlstart->ns->value, "urn:ietf:params:xml:ns:yang:1")) {
646 /* it's an action, not a simple RPC */
647 xmlstart = xmlstart->child;
Michal Vaskoa8756562017-05-03 14:42:23 +0200648 if (options & LYD_OPT_DESTRUCT) {
649 /* free it later */
650 xmlfree = xmlstart->parent;
651 }
Michal Vaskob1b19442016-07-13 12:26:01 +0200652 }
653
654 iter = last = NULL;
Radek Krejci86538212015-12-17 15:59:01 +0100655 LY_TREE_FOR_SAFE(xmlstart, xmlaux, xmlelem) {
Michal Vaskob15cae22016-09-15 09:40:56 +0200656 r = xml_parse_data(ctx, xmlelem, reply_parent, result, last, options, unres, &iter, &act_notif);
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100657 if (r) {
Michal Vasko945b96b2016-10-18 11:49:12 +0200658 if (reply_top) {
659 result = reply_top;
Pavol Vican8a552f62016-09-05 11:20:57 +0200660 }
Michal Vasko24d982f2016-04-18 15:13:58 +0200661 goto error;
Radek Krejci86538212015-12-17 15:59:01 +0100662 } else if (options & LYD_OPT_DESTRUCT) {
663 lyxml_free(ctx, xmlelem);
664 *root = xmlaux;
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100665 }
666 if (iter) {
667 last = iter;
Radek Krejci06f8bb92017-08-02 15:36:25 +0200668 if ((options & LYD_OPT_DATA_ADD_YANGLIB) && iter->schema->module == ctx->models.list[LY_INTERNAL_MODULE_COUNT - 1]) {
669 /* ietf-yang-library data present, so ignore the option to add them */
670 options &= ~LYD_OPT_DATA_ADD_YANGLIB;
671 }
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100672 }
673 if (!result) {
674 result = iter;
675 }
Radek Krejci86538212015-12-17 15:59:01 +0100676
677 if (options & LYD_OPT_NOSIBLINGS) {
678 /* stop after the first processed root */
679 break;
680 }
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100681 }
Radek Krejcic6704c82015-10-06 11:12:45 +0200682
Michal Vasko945b96b2016-10-18 11:49:12 +0200683 if (reply_top) {
684 result = reply_top;
Michal Vaskoe45aff52016-08-25 09:01:09 +0200685 }
686
Michal Vasko945b96b2016-10-18 11:49:12 +0200687 if ((options & LYD_OPT_RPCREPLY) && (rpc_act->schema->nodetype != LYS_RPC)) {
688 /* action reply */
689 act_notif = reply_parent;
Michal Vaskoafa7a642016-10-18 15:11:38 +0200690 } else if ((options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) && !act_notif) {
691 ly_vecode = LYVE_INELEM;
692 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing %s node.", (options & LYD_OPT_RPC ? "action" : "notification"));
693 goto error;
Radek Krejcib45b3082016-09-09 16:08:51 +0200694 }
695
Radek Krejci06f8bb92017-08-02 15:36:25 +0200696 /* add missing ietf-yang-library if requested */
697 if (options & LYD_OPT_DATA_ADD_YANGLIB) {
698 if (!result) {
699 result = ly_ctx_info(ctx);
700 } else if (lyd_merge(result, ly_ctx_info(ctx), LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
701 LOGERR(LY_EINT, "Adding ietf-yang-library data failed.");
702 goto error;
703 }
704 }
705
706 /* check for uniqueness of top-level lists/leaflists because
Radek Krejci63b79c82016-08-10 10:09:33 +0200707 * only the inner instances were tested in lyv_data_content() */
708 set = ly_set_new();
709 LY_TREE_FOR(result, iter) {
710 if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) {
711 continue;
712 }
713
714 /* check each list/leaflist only once */
715 i = set->number;
716 if (ly_set_add(set, iter->schema, 0) != i) {
717 /* already checked */
718 continue;
719 }
720
721 if (lyv_data_unique(iter, result)) {
722 ly_set_free(set);
723 goto error;
724 }
725 }
726 ly_set_free(set);
727
Radek Krejcib45b3082016-09-09 16:08:51 +0200728 /* add default values, resolve unres and check for mandatory nodes in final tree */
Michal Vaskob15cae22016-09-15 09:40:56 +0200729 if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) {
Michal Vasko6b44d712016-09-12 16:25:46 +0200730 goto error;
731 }
Michal Vaskoad2e44a2017-01-03 10:31:35 +0100732 if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER))
733 && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) {
Michal Vaskoafa7a642016-10-18 15:11:38 +0200734 goto error;
Radek Krejci46165822016-08-26 14:06:27 +0200735 }
736
Michal Vaskoa8756562017-05-03 14:42:23 +0200737 if (xmlfree) {
738 lyxml_free(ctx, xmlfree);
739 }
Michal Vasko24d982f2016-04-18 15:13:58 +0200740 free(unres->node);
741 free(unres->type);
742 free(unres);
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100743 va_end(ap);
Radek Krejcic6704c82015-10-06 11:12:45 +0200744
745 return result;
Michal Vasko24d982f2016-04-18 15:13:58 +0200746
747error:
748 lyd_free_withsiblings(result);
Michal Vaskoa8756562017-05-03 14:42:23 +0200749 if (xmlfree) {
750 lyxml_free(ctx, xmlfree);
751 }
Michal Vasko24d982f2016-04-18 15:13:58 +0200752 free(unres->node);
753 free(unres->type);
754 free(unres);
755 va_end(ap);
756
757 return NULL;
Radek Krejcic6704c82015-10-06 11:12:45 +0200758}