blob: 657ae108aef68900ff596a995b21a2d70c5c8b24 [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 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
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 Krejci1fe9ac02016-09-23 09:38:21 +020086 if ((editbits & 0x10) && (node->schema->nodetype & LYS_LEAF) && (!leaf->value_str || !leaf->value_str[0])) {
87 /* 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 */
Michal Vaskoe3886bb2017-01-02 11:33:28 +010095 if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, xml, leaf, 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;
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200115 int i, j, havechildren, r, flag, pos, editbits = 0;
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 Krejci1721c012015-07-08 12:52:33 +0200123 if (!xml->ns || !xml->ns->value) {
Radek Krejci2342cf62016-01-29 16:48:23 +0100124 if (options & LYD_OPT_STRICT) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100125 LOGVAL(LYE_XML_MISS, LY_VLOG_XML, xml, "element's", "namespace");
Radek Krejci2342cf62016-01-29 16:48:23 +0100126 return -1;
127 } else {
128 return 0;
129 }
Radek Krejci1721c012015-07-08 12:52:33 +0200130 }
131
132 /* find schema node */
Michal Vaskob1b19442016-07-13 12:26:01 +0200133 if (!parent) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100134 mod = ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL);
135 if (ctx->data_clb) {
136 if (!mod) {
137 mod = ctx->data_clb(ctx, NULL, xml->ns->value, 0, ctx->data_clb_data);
138 } else if (!mod->implemented) {
139 mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Radek Krejci27fe55e2016-09-13 17:13:35 +0200140 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100141 }
142
143 /* get the proper schema node */
144 if (mod && mod->implemented && !mod->disabled) {
145 schema = xml_data_search_schemanode(xml, mod->data, options);
146 if (!schema) {
147 /* it still can be the specific case of this module containing an augment of another module
148 * top-level choice or top-level choice's case, bleh */
149 for (j = 0; j < mod->augment_size; ++j) {
150 aug = &mod->augment[j];
151 target = aug->target;
152 if (target->nodetype & (LYS_CHOICE | LYS_CASE)) {
153 /* 1) okay, the target is choice or case */
154 while (target && (target->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES))) {
155 target = lys_parent(target);
156 }
157 /* 2) now, the data node will be top-level, there are only non-data schema nodes */
158 if (!target) {
159 while ((schema = (struct lys_node *)lys_getnext(schema, (struct lys_node *)aug, NULL, 0))) {
160 /* 3) alright, even the name matches, we found our schema node */
161 if (ly_strequal(schema->name, xml->name, 1)) {
162 break;
Michal Vasko5e523b62016-08-26 16:23:15 +0200163 }
164 }
165 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100166 }
Michal Vasko5e523b62016-08-26 16:23:15 +0200167
Michal Vaskof53187d2017-01-13 13:23:14 +0100168 if (schema) {
169 break;
Michal Vasko5e523b62016-08-26 16:23:15 +0200170 }
171 }
Radek Krejci1721c012015-07-08 12:52:33 +0200172 }
173 }
174 } else {
175 /* parsing some internal node, we start with parent's schema pointer */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100176 schema = xml_data_search_schemanode(xml, parent->schema->child, options);
Michal Vaskof53187d2017-01-13 13:23:14 +0100177
178 if (schema) {
179 if (!lys_node_module(schema)->implemented && ctx->data_clb) {
180 mod = ctx->data_clb(ctx, lys_node_module(schema)->name, lys_node_module(schema)->ns,
181 LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
182 }
Radek Krejci25b9fd32015-08-10 15:06:07 +0200183 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100184 }
185
186 mod = lys_node_module(schema);
187 if (!mod || !mod->implemented || mod->disabled) {
Radek Krejci27fe55e2016-09-13 17:13:35 +0200188 if (options & LYD_OPT_STRICT) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100189 LOGVAL(LYE_INELEM, (parent ? LY_VLOG_LYD : LY_VLOG_NONE), parent, xml->name);
Radek Krejci27fe55e2016-09-13 17:13:35 +0200190 return -1;
191 } else {
192 return 0;
193 }
Radek Krejci1721c012015-07-08 12:52:33 +0200194 }
195
Radek Krejciadb57612016-02-16 13:34:34 +0100196 /* create the element structure */
Radek Krejcib9930252015-07-08 15:47:45 +0200197 switch (schema->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +0200198 case LYS_CONTAINER:
Radek Krejci27aaa732015-09-04 15:24:04 +0200199 case LYS_LIST:
Michal Vasko2f30e572015-10-01 16:00:38 +0200200 case LYS_NOTIF:
201 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200202 case LYS_ACTION:
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100203 *result = calloc(1, sizeof **result);
Michal Vaskoab8e4402015-07-17 12:54:28 +0200204 havechildren = 1;
Radek Krejcib9930252015-07-08 15:47:45 +0200205 break;
Radek Krejci76512572015-08-04 09:47:08 +0200206 case LYS_LEAF:
Radek Krejci76512572015-08-04 09:47:08 +0200207 case LYS_LEAFLIST:
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100208 *result = calloc(1, sizeof(struct lyd_node_leaf_list));
Radek Krejcie4748472015-07-08 18:00:22 +0200209 havechildren = 0;
210 break;
Radek Krejci76512572015-08-04 09:47:08 +0200211 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200212 case LYS_ANYDATA:
213 *result = calloc(1, sizeof(struct lyd_node_anydata));
Michal Vaskoab8e4402015-07-17 12:54:28 +0200214 havechildren = 0;
215 break;
Radek Krejcib9930252015-07-08 15:47:45 +0200216 default:
Michal Vasko0c888fd2015-08-11 15:54:08 +0200217 LOGINT;
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100218 return -1;
Radek Krejcib9930252015-07-08 15:47:45 +0200219 }
Michal Vasko253035f2015-12-17 16:58:13 +0100220 if (!(*result)) {
221 LOGMEM;
222 return -1;
223 }
224
Radek Krejci61767ca2016-09-19 14:21:55 +0200225 (*result)->prev = *result;
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100226 (*result)->schema = schema;
Radek Krejci61767ca2016-09-19 14:21:55 +0200227 (*result)->parent = parent;
228 diter = NULL;
229 if (parent && parent->child && schema->nodetype == LYS_LEAF && parent->schema->nodetype == LYS_LIST &&
230 (pos = lys_is_key((struct lys_node_list *)parent->schema, (struct lys_node_leaf *)schema))) {
231 /* it is key and we need to insert it into a correct place */
232 for (i = 0, diter = parent->child;
233 diter && i < (pos - 1) && diter->schema->nodetype == LYS_LEAF &&
234 lys_is_key((struct lys_node_list *)parent->schema, (struct lys_node_leaf *)diter->schema);
235 i++, diter = diter->next);
236 if (diter) {
237 /* out of order insertion - insert list's key to the correct position, before the diter */
238 if (options & LYD_OPT_STRICT) {
239 LOGVAL(LYE_INORDER, LY_VLOG_LYD, *result, schema->name, diter->schema->name);
240 LOGVAL(LYE_SPEC, LY_VLOG_LYD, *result, "Invalid position of the key \"%s\" in a list \"%s\".",
241 schema->name, parent->schema->name);
242 free(*result);
243 *result = NULL;
244 return -1;
245 } else {
246 LOGWRN("Invalid position of the key \"%s\" in a list \"%s\".", schema->name, parent->schema->name)
247 }
248 if (parent->child == diter) {
249 parent->child = *result;
250 /* update first_sibling */
251 first_sibling = *result;
252 }
253 if (diter->prev->next) {
254 diter->prev->next = *result;
255 }
256 (*result)->prev = diter->prev;
257 diter->prev = *result;
258 (*result)->next = diter;
259 }
260 }
261 if (!diter) {
262 /* simplified (faster) insert as the last node */
263 if (parent && !parent->child) {
264 parent->child = *result;
265 }
266 if (prev) {
267 (*result)->prev = prev;
268 prev->next = *result;
269
270 /* fix the "last" pointer */
271 first_sibling->prev = *result;
272 } else {
273 (*result)->prev = *result;
274 first_sibling = *result;
275 }
276 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100277 (*result)->validity = ly_new_node_validity((*result)->schema);
Radek Krejci46165822016-08-26 14:06:27 +0200278 if (resolve_applies_when(schema, 0, NULL)) {
Radek Krejci0b7704f2016-03-18 12:16:14 +0100279 (*result)->when_status = LYD_WHEN;
280 }
Radek Krejci1721c012015-07-08 12:52:33 +0200281
Radek Krejciadb57612016-02-16 13:34:34 +0100282 /* check insert attribute and its values */
283 if (options & LYD_OPT_EDIT) {
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200284 /* 0x01 - insert attribute present
285 * 0x02 - insert is relative (before or after)
286 * 0x04 - value attribute present
287 * 0x08 - key attribute present
288 * 0x10 - operation not allowing insert attribute
289 */
Radek Krejciadb57612016-02-16 13:34:34 +0100290 for (attr = xml->attr; attr; attr = attr->next) {
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200291 if (attr->type != LYXML_ATTR_STD || !attr->ns) {
292 /* not interesting attribute or namespace declaration */
Radek Krejciadb57612016-02-16 13:34:34 +0100293 continue;
294 }
295
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200296 if (!strcmp(attr->name, "operation") && !strcmp(attr->ns->value, LY_NSNC)) {
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200297 if (editbits & 0x10) {
Radek Krejci72614bf2016-04-08 17:46:07 +0200298 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "operation attributes", xml->name);
299 return -1;
300 }
301
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200302 if (!strcmp(attr->value, "delete") || !strcmp(attr->value, "remove")) {
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200303 editbits |= 0x10;
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200304 } else if (strcmp(attr->value, "create") &&
305 strcmp(attr->value, "merge") &&
306 strcmp(attr->value, "replace")) {
307 /* unknown operation */
308 LOGVAL(LYE_INVALATTR, LY_VLOG_LYD, (*result), attr->value, attr->name);
309 return -1;
310 }
311 } else if (!strcmp(attr->name, "insert") && !strcmp(attr->ns->value, LY_NSYANG)) {
312 /* 'insert' attribute present */
313 if (!(schema->flags & LYS_USERORDERED)) {
314 /* ... but it is not expected */
315 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), "insert", schema->name);
316 return -1;
317 }
Radek Krejciadb57612016-02-16 13:34:34 +0100318
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200319 if (editbits & 0x01) {
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200320 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "insert attributes", xml->name);
321 return -1;
322 }
323 if (!strcmp(attr->value, "first") || !strcmp(attr->value, "last")) {
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200324 editbits |= 0x01;
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200325 } else if (!strcmp(attr->value, "before") || !strcmp(attr->value, "after")) {
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200326 editbits |= 0x01 | 0x02;
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200327 } else {
328 LOGVAL(LYE_INVALATTR, LY_VLOG_LYD, (*result), attr->value, attr->name);
329 return -1;
330 }
331 str = attr->name;
332 } else if (!strcmp(attr->name, "value") && !strcmp(attr->ns->value, LY_NSYANG)) {
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200333 if (editbits & 0x04) {
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200334 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "value attributes", xml->name);
335 return -1;
336 } else if (schema->nodetype & LYS_LIST) {
337 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, schema->name);
338 return -1;
339 }
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200340 editbits |= 0x04;
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200341 str = attr->name;
342 } else if (!strcmp(attr->name, "key") && !strcmp(attr->ns->value, LY_NSYANG)) {
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200343 if (editbits & 0x08) {
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200344 LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "key attributes", xml->name);
345 return -1;
346 } else if (schema->nodetype & LYS_LEAFLIST) {
347 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, schema->name);
348 return -1;
349 }
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200350 editbits |= 0x08;
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200351 str = attr->name;
Radek Krejciadb57612016-02-16 13:34:34 +0100352 }
Radek Krejciadb57612016-02-16 13:34:34 +0100353 }
354
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200355 /* report errors */
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200356 if (editbits > 0x10 || (editbits && editbits < 0x10 &&
Radek Krejcicced9382016-04-13 13:15:03 +0200357 (!(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_USERORDERED)))) {
Radek Krejcid0df6902016-03-10 09:32:00 +0100358 /* attributes in wrong elements */
Radek Krejci48464ed2016-03-17 15:44:09 +0100359 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), str, xml->name);
Radek Krejcid0df6902016-03-10 09:32:00 +0100360 return -1;
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200361 } else if (editbits == 3) {
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200362 /* 0x01 | 0x02 - relative position, but value/key is missing */
363 if (schema->nodetype & LYS_LIST) {
364 LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "key", xml->name);
365 } else { /* LYS_LEAFLIST */
366 LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "value", xml->name);
367 }
Radek Krejciadb57612016-02-16 13:34:34 +0100368 return -1;
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200369 } else if ((editbits & (0x04 | 0x08)) && !(editbits & 0x02)) {
Radek Krejci9bfcbde2016-04-07 16:30:15 +0200370 /* key/value without relative position */
Radek Krejci1fe9ac02016-09-23 09:38:21 +0200371 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), (editbits & 0x04) ? "value" : "key", schema->name);
Radek Krejciadb57612016-02-16 13:34:34 +0100372 return -1;
373 }
374 }
375
Radek Krejcib9930252015-07-08 15:47:45 +0200376 /* type specific processing */
Radek Krejci27aaa732015-09-04 15:24:04 +0200377 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcie4748472015-07-08 18:00:22 +0200378 /* type detection and assigning the value */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100379 if (xml_get_value(*result, xml, editbits)) {
Radek Krejci3e3affe2015-07-09 15:38:40 +0200380 goto error;
381 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200382 } else if (schema->nodetype & LYS_ANYDATA) {
Michal Vaskof748dbc2016-04-05 11:27:47 +0200383 /* store children values */
384 if (xml->child) {
385 child = xml->child;
386 /* manually unlink all siblings and correct namespaces */
387 xml->child = NULL;
388 LY_TREE_FOR(child, next) {
389 next->parent = NULL;
390 lyxml_correct_elem_ns(ctx, next, 1, 1);
391 }
392
Radek Krejci45826012016-08-24 15:07:57 +0200393 ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_XML;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200394 ((struct lyd_node_anydata *)*result)->value.xml = child;
Michal Vaskof748dbc2016-04-05 11:27:47 +0200395 } else {
Radek Krejci45826012016-08-24 15:07:57 +0200396 ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_CONSTSTRING;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200397 ((struct lyd_node_anydata *)*result)->value.str = lydict_insert(ctx, xml->content, 0);
Michal Vasko253035f2015-12-17 16:58:13 +0100398 }
Michal Vaskoafa7a642016-10-18 15:11:38 +0200399 } else if (schema->nodetype & (LYS_RPC | LYS_ACTION)) {
400 if (!(options & LYD_OPT_RPC) || *act_notif) {
401 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*result), schema->name);
402 LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Unexpected %s node \"%s\".",
403 (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name);
404 goto error;
405 }
406 *act_notif = *result;
407 } else if (schema->nodetype == LYS_NOTIF) {
408 if (!(options & LYD_OPT_NOTIF) || *act_notif) {
409 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*result), schema->name);
410 LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Unexpected notification node \"%s\".", schema->name);
411 goto error;
412 }
413 *act_notif = *result;
Radek Krejcib9930252015-07-08 15:47:45 +0200414 }
415
Michal Vasko10e586f2016-05-18 13:25:30 +0200416 /* first part of validation checks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100417 if (lyv_data_context(*result, options, unres)) {
Michal Vasko10e586f2016-05-18 13:25:30 +0200418 goto error;
419 }
420
Radek Krejci998a7502015-10-26 15:54:33 +0100421 for (attr = xml->attr; attr; attr = attr->next) {
Radek Krejci9a5daea2016-03-02 16:49:40 +0100422 flag = 0;
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100423 if (attr->type != LYXML_ATTR_STD) {
424 continue;
425 } else if (!attr->ns) {
Radek Krejci9a5daea2016-03-02 16:49:40 +0100426 if ((*result)->schema->nodetype != LYS_ANYXML ||
427 !ly_strequal((*result)->schema->name, "filter", 0) ||
428 !ly_strequal((*result)->schema->module->name, "ietf-netconf", 0)) {
Radek Krejci0f721232016-05-03 14:52:29 +0200429 if (options & LYD_OPT_STRICT) {
430 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, xml->name);
431 LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Attribute \"%s\" with no namespace (schema).",
432 attr->name);
433 goto error;
434 } else {
435 LOGWRN("Ignoring \"%s\" attribute in \"%s\" element.", attr->name, xml->name);
436 continue;
437 }
Radek Krejci9a5daea2016-03-02 16:49:40 +0100438 } else {
439 /* exception for filter's attributes */
440 flag = 1;
441 }
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100442 }
443
444 dattr = malloc(sizeof *dattr);
Michal Vasko253035f2015-12-17 16:58:13 +0100445 if (!dattr) {
446 goto error;
447 }
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100448 dattr->next = NULL;
449 dattr->name = attr->name;
Radek Krejci9a5daea2016-03-02 16:49:40 +0100450 if (flag && ly_strequal(attr->name, "select", 0)) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100451 dattr->value = transform_xml2json(ctx, attr->value, xml, 0, 1);
Radek Krejci9a5daea2016-03-02 16:49:40 +0100452 if (!dattr->value) {
453 free(dattr);
454 goto error;
455 }
456 lydict_remove(ctx, attr->value);
457 } else {
458 dattr->value = attr->value;
459 }
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100460 attr->name = NULL;
461 attr->value = NULL;
462
Radek Krejci9a5daea2016-03-02 16:49:40 +0100463 if (!attr->ns) {
464 /* filter's attributes, it actually has no namespace, but we need some for internal representation */
465 dattr->module = (*result)->schema->module;
466 } else {
467 dattr->module = (struct lys_module *)ly_ctx_get_module_by_ns(ctx, attr->ns->value, NULL);
468 }
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100469 if (!dattr->module) {
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100470 free(dattr);
Radek Krejci0f721232016-05-03 14:52:29 +0200471 if (options & LYD_OPT_STRICT) {
472 LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, xml->name);
473 LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Attribute \"%s\" from unknown schema (\"%s\").",
474 attr->name, attr->ns->value);
475 goto error;
476 } else {
477 LOGWRN("Attribute \"%s\" from unknown schema (\"%s\") - skipping.", attr->name, attr->ns->value);
478 continue;
479 }
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100480 }
481
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100482 if (!(*result)->attr) {
483 (*result)->attr = dattr;
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100484 } else {
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100485 for (dattr_iter = (*result)->attr; dattr_iter->next; dattr_iter = dattr_iter->next);
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100486 dattr_iter->next = dattr;
Radek Krejci998a7502015-10-26 15:54:33 +0100487 }
488 }
Michal Vasko4ff7b072015-08-21 09:05:03 +0200489
Radek Krejci14c00092015-11-01 11:03:24 +0100490 /* process children */
491 if (havechildren && xml->child) {
492 diter = dlast = NULL;
493 LY_TREE_FOR_SAFE(xml->child, next, child) {
494 if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) {
Michal Vaskob15cae22016-09-15 09:40:56 +0200495 r = xml_parse_data(ctx, child, *result, (*result)->child, dlast, 0, unres, &diter, act_notif);
Radek Krejci14c00092015-11-01 11:03:24 +0100496 } else {
Michal Vaskob15cae22016-09-15 09:40:56 +0200497 r = xml_parse_data(ctx, child, *result, (*result)->child, dlast, options, unres, &diter, act_notif);
Radek Krejci14c00092015-11-01 11:03:24 +0100498 }
Radek Krejci14c00092015-11-01 11:03:24 +0100499 if (r) {
500 goto error;
Radek Krejci86538212015-12-17 15:59:01 +0100501 } else if (options & LYD_OPT_DESTRUCT) {
502 lyxml_free(ctx, child);
Radek Krejci14c00092015-11-01 11:03:24 +0100503 }
Radek Krejci61767ca2016-09-19 14:21:55 +0200504 if (diter && !diter->next) {
505 /* the child was parsed/created and it was placed as the last child. The child can be inserted
506 * 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 +0100507 dlast = diter;
508 }
509 }
510 }
511
Radek Krejcifb7156e2016-10-27 13:39:56 +0200512 /* if we have empty non-presence container, we keep it, but mark it as default */
Radek Krejci2537fd32016-09-07 16:22:41 +0200513 if (schema->nodetype == LYS_CONTAINER && !(*result)->child &&
Radek Krejcid3e73722016-05-23 12:24:55 +0200514 !(*result)->attr && !((struct lys_node_container *)schema)->presence) {
Radek Krejcifb7156e2016-10-27 13:39:56 +0200515 (*result)->dflt = 1;
Radek Krejci0c0086a2016-03-24 15:20:28 +0100516 }
517
Radek Krejcicf509982015-12-15 09:22:44 +0100518 /* rest of validation checks */
Radek Krejci00a0e712016-10-26 10:24:46 +0200519 ly_err_clean(1);
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100520 if (lyv_data_content(*result, options, unres) ||
521 lyv_multicases(*result, NULL, prev ? &first_sibling : NULL, 0, NULL)) {
Radek Krejcieab784a2015-08-27 09:56:53 +0200522 if (ly_errno) {
Radek Krejcib1c12512015-08-11 11:22:04 +0200523 goto error;
Radek Krejci1b0d01a2015-08-19 17:00:35 +0200524 } else {
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100525 goto clear;
Radek Krejcida374342015-08-19 13:33:22 +0200526 }
Radek Krejci78ce8612015-08-18 14:31:05 +0200527 }
528
Radek Krejcica7efb72016-01-18 13:06:01 +0100529 /* validation successful */
Radek Krejci63b79c82016-08-10 10:09:33 +0200530 if ((*result)->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
531 /* postpone checking when there will be all list/leaflist instances */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100532 (*result)->validity |= LYD_VAL_UNIQUE;
Radek Krejci63b79c82016-08-10 10:09:33 +0200533 }
Radek Krejcica7efb72016-01-18 13:06:01 +0100534
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100535 return ret;
Radek Krejci3e3affe2015-07-09 15:38:40 +0200536
537error:
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100538 ret--;
539
540clear:
Radek Krejcieab784a2015-08-27 09:56:53 +0200541 /* cleanup */
Radek Krejci0c0086a2016-03-24 15:20:28 +0100542 for (i = unres->count - 1; i >= 0; i--) {
543 /* remove unres items connected with the node being removed */
544 if (unres->node[i] == *result) {
545 unres_data_del(unres, i);
546 }
547 }
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100548 lyd_free(*result);
549 *result = NULL;
Radek Krejci1b0d01a2015-08-19 17:00:35 +0200550
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100551 return ret;
Radek Krejci1721c012015-07-08 12:52:33 +0200552}
553
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100554API struct lyd_node *
555lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options, ...)
Radek Krejcic6704c82015-10-06 11:12:45 +0200556{
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100557 va_list ap;
Radek Krejci63b79c82016-08-10 10:09:33 +0200558 int r, i;
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100559 struct unres_data *unres = NULL;
Michal Vasko945b96b2016-10-18 11:49:12 +0200560 const struct lyd_node *rpc_act = NULL, *data_tree = NULL;
561 struct lyd_node *result = NULL, *iter, *last, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL;
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100562 struct lyxml_elem *xmlstart, *xmlelem, *xmlaux;
Radek Krejci63b79c82016-08-10 10:09:33 +0200563 struct ly_set *set;
Radek Krejcic6704c82015-10-06 11:12:45 +0200564
Radek Krejci00a0e712016-10-26 10:24:46 +0200565 ly_err_clean(1);
Radek Krejci2342cf62016-01-29 16:48:23 +0100566
Radek Krejcic6704c82015-10-06 11:12:45 +0200567 if (!ctx || !root) {
568 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
569 return NULL;
570 }
571
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100572 if (lyp_check_options(options)) {
573 LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__);
574 return NULL;
575 }
576
Radek Krejcia6939c32016-03-24 15:19:09 +0100577 if (!(*root)) {
578 /* empty tree - no work is needed */
579 lyd_validate(&result, options, ctx);
580 return result;
581 }
582
Michal Vasko24d982f2016-04-18 15:13:58 +0200583 unres = calloc(1, sizeof *unres);
584 if (!unres) {
585 LOGMEM;
586 return NULL;
587 }
588
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100589 va_start(ap, options);
590 if (options & LYD_OPT_RPCREPLY) {
Michal Vasko945b96b2016-10-18 11:49:12 +0200591 rpc_act = va_arg(ap, const struct lyd_node *);
592 if (!rpc_act || rpc_act->parent || !(rpc_act->schema->nodetype & (LYS_RPC | LYS_LIST | LYS_CONTAINER))) {
593 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__);
Michal Vasko24d982f2016-04-18 15:13:58 +0200594 goto error;
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100595 }
Michal Vasko945b96b2016-10-18 11:49:12 +0200596 if (rpc_act->schema->nodetype == LYS_RPC) {
597 /* RPC request */
598 reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0);
599 } else {
600 /* action request */
601 reply_top = lyd_dup(rpc_act, 1);
602 LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) {
603 if (reply_parent->schema->nodetype == LYS_ACTION) {
604 break;
605 }
606 LY_TREE_DFS_END(reply_top, iter, reply_parent);
607 }
608 if (!reply_parent) {
609 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__);
610 lyd_free_withsiblings(reply_top);
611 goto error;
612 }
613 lyd_free_withsiblings(reply_parent->child);
614 }
Michal Vasko45e23652016-09-21 11:24:32 +0200615 }
616 if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF | LYD_OPT_RPCREPLY)) {
Michal Vasko945b96b2016-10-18 11:49:12 +0200617 data_tree = va_arg(ap, const struct lyd_node *);
Michal Vasko6b44d712016-09-12 16:25:46 +0200618 if (data_tree) {
Michal Vasko945b96b2016-10-18 11:49:12 +0200619 LY_TREE_FOR((struct lyd_node *)data_tree, iter) {
Michal Vasko6b44d712016-09-12 16:25:46 +0200620 if (iter->parent) {
621 /* a sibling is not top-level */
622 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__);
623 goto error;
624 }
625 }
626
627 /* move it to the beginning */
628 for (; data_tree->prev->next; data_tree = data_tree->prev);
629
630 /* LYD_OPT_NOSIBLINGS cannot be set in this case */
631 if (options & LYD_OPT_NOSIBLINGS) {
632 LOGERR(LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__);
633 goto error;
634 }
635 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100636 }
637
Radek Krejci86538212015-12-17 15:59:01 +0100638 if (!(options & LYD_OPT_NOSIBLINGS)) {
639 /* locate the first root to process */
640 if ((*root)->parent) {
641 xmlstart = (*root)->parent->child;
642 } else {
643 xmlstart = *root;
644 while(xmlstart->prev->next) {
645 xmlstart = xmlstart->prev;
646 }
Radek Krejci04b97de2015-10-31 23:09:15 +0100647 }
Radek Krejci86538212015-12-17 15:59:01 +0100648 } else {
649 xmlstart = *root;
650 }
Radek Krejci86538212015-12-17 15:59:01 +0100651
Michal Vaskob1b19442016-07-13 12:26:01 +0200652 if ((options & LYD_OPT_RPC)
653 && !strcmp(xmlstart->name, "action") && !strcmp(xmlstart->ns->value, "urn:ietf:params:xml:ns:yang:1")) {
654 /* it's an action, not a simple RPC */
655 xmlstart = xmlstart->child;
Michal Vaskob1b19442016-07-13 12:26:01 +0200656 }
657
658 iter = last = NULL;
Radek Krejci86538212015-12-17 15:59:01 +0100659 LY_TREE_FOR_SAFE(xmlstart, xmlaux, xmlelem) {
Michal Vaskob15cae22016-09-15 09:40:56 +0200660 r = xml_parse_data(ctx, xmlelem, reply_parent, result, last, options, unres, &iter, &act_notif);
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100661 if (r) {
Michal Vasko945b96b2016-10-18 11:49:12 +0200662 if (reply_top) {
663 result = reply_top;
Pavol Vican8a552f62016-09-05 11:20:57 +0200664 }
Michal Vasko24d982f2016-04-18 15:13:58 +0200665 goto error;
Radek Krejci86538212015-12-17 15:59:01 +0100666 } else if (options & LYD_OPT_DESTRUCT) {
667 lyxml_free(ctx, xmlelem);
668 *root = xmlaux;
Radek Krejci3b41a6c2015-10-31 23:06:12 +0100669 }
670 if (iter) {
671 last = iter;
672 }
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 Krejci63b79c82016-08-10 10:09:33 +0200696 /* check for uniquness of top-level lists/leaflists because
697 * only the inner instances were tested in lyv_data_content() */
698 set = ly_set_new();
699 LY_TREE_FOR(result, iter) {
700 if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) {
701 continue;
702 }
703
704 /* check each list/leaflist only once */
705 i = set->number;
706 if (ly_set_add(set, iter->schema, 0) != i) {
707 /* already checked */
708 continue;
709 }
710
711 if (lyv_data_unique(iter, result)) {
712 ly_set_free(set);
713 goto error;
714 }
715 }
716 ly_set_free(set);
717
Radek Krejcib45b3082016-09-09 16:08:51 +0200718 /* add default values, resolve unres and check for mandatory nodes in final tree */
Michal Vaskob15cae22016-09-15 09:40:56 +0200719 if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) {
Michal Vasko6b44d712016-09-12 16:25:46 +0200720 goto error;
721 }
Michal Vaskoad2e44a2017-01-03 10:31:35 +0100722 if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER))
723 && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) {
Michal Vaskoafa7a642016-10-18 15:11:38 +0200724 goto error;
Radek Krejci46165822016-08-26 14:06:27 +0200725 }
726
Michal Vasko24d982f2016-04-18 15:13:58 +0200727 free(unres->node);
728 free(unres->type);
729 free(unres);
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100730 va_end(ap);
Radek Krejcic6704c82015-10-06 11:12:45 +0200731
732 return result;
Michal Vasko24d982f2016-04-18 15:13:58 +0200733
734error:
735 lyd_free_withsiblings(result);
736 free(unres->node);
737 free(unres->type);
738 free(unres);
739 va_end(ap);
740
741 return NULL;
Radek Krejcic6704c82015-10-06 11:12:45 +0200742}