Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @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 Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <assert.h> |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 16 | #include <ctype.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 17 | #include <errno.h> |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 18 | #include <stdarg.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 22 | |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 23 | #include "libyang.h" |
| 24 | #include "common.h" |
| 25 | #include "context.h" |
Michal Vasko | b798232 | 2015-10-16 13:56:12 +0200 | [diff] [blame] | 26 | #include "parser.h" |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 27 | #include "tree_internal.h" |
| 28 | #include "validation.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 29 | #include "xml_internal.h" |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 30 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 31 | /* does not log */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 32 | static struct lys_node * |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 33 | xml_data_search_schemanode(struct lyxml_elem *xml, struct lys_node *start, int options) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 34 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 35 | struct lys_node *result, *aux; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 36 | |
| 37 | LY_TREE_FOR(start, result) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 38 | /* skip groupings ... */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 39 | if (result->nodetype == LYS_GROUPING) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 40 | continue; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 41 | /* ... 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 Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 49 | /* 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 Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 51 | aux = xml_data_search_schemanode(xml, result->child, options); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 52 | 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 Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 61 | if (ly_strequal(result->name, xml->name, 1)) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 62 | /* names matches, what about namespaces? */ |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 63 | if (ly_strequal(result->module->ns, xml->ns->value, 1)) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 64 | /* 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 Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 76 | /* logs directly */ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 77 | static int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 78 | xml_get_value(struct lyd_node *node, struct lyxml_elem *xml, int options) |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 79 | { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 80 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 81 | int resolve; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 82 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 83 | assert(node && (node->schema->nodetype & (LYS_LEAFLIST | LYS_LEAF)) && xml); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 84 | |
| 85 | leaf->value_str = xml->content; |
| 86 | xml->content = NULL; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 87 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 88 | /* will be changed in case of union */ |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 89 | leaf->value_type = ((struct lys_node_leaf *)node->schema)->type.base; |
Radek Krejci | e3c3314 | 2015-08-10 15:04:36 +0200 | [diff] [blame] | 90 | |
Radek Krejci | 92ece00 | 2016-04-04 15:45:05 +0200 | [diff] [blame] | 91 | if (options & (LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 92 | resolve = 0; |
| 93 | } else { |
| 94 | resolve = 1; |
| 95 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 96 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 97 | if ((leaf->value_type == LY_TYPE_IDENT) || (leaf->value_type == LY_TYPE_INST)) { |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 98 | /* convert the path from the XML form using XML namespaces into the JSON format |
| 99 | * using module names as namespaces |
| 100 | */ |
| 101 | xml->content = leaf->value_str; |
Michal Vasko | fba1526 | 2015-10-21 12:10:28 +0200 | [diff] [blame] | 102 | leaf->value_str = transform_xml2json(leaf->schema->module->ctx, xml->content, xml, 1); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 103 | lydict_remove(leaf->schema->module->ctx, xml->content); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 104 | xml->content = NULL; |
| 105 | if (!leaf->value_str) { |
| 106 | return EXIT_FAILURE; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 107 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 108 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 109 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 110 | if (lyp_parse_value(leaf, xml, resolve)) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 111 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return EXIT_SUCCESS; |
| 115 | } |
| 116 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 117 | /* logs directly */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 118 | static int |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 119 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, const struct lys_node *schema_parent, struct lyd_node *parent, |
| 120 | struct lyd_node *prev, int options, struct unres_data *unres, struct lyd_node **result) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 121 | { |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 122 | struct lyd_node *diter, *dlast, *first_sibling; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 123 | struct lys_node *schema = NULL; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 124 | struct lyd_attr *dattr, *dattr_iter; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 125 | struct lyxml_attr *attr; |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 126 | struct lyxml_elem *child, *next; |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 127 | int i, havechildren, r, flag; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 128 | int ret = 0; |
Radek Krejci | abb7b58 | 2016-04-20 16:15:47 +0200 | [diff] [blame] | 129 | const char *str = NULL; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 130 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 131 | assert(xml); |
| 132 | assert(result); |
| 133 | *result = NULL; |
| 134 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 135 | if (!xml->ns || !xml->ns->value) { |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 136 | if (options & LYD_OPT_STRICT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 137 | LOGVAL(LYE_XML_MISS, LY_VLOG_XML, xml, "element's", "namespace"); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 138 | return -1; |
| 139 | } else { |
| 140 | return 0; |
| 141 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* find schema node */ |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 145 | if (schema_parent) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 146 | schema = xml_data_search_schemanode(xml, schema_parent->child, options); |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 147 | } else if (!parent) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 148 | /* starting in root */ |
| 149 | for (i = 0; i < ctx->models.used; i++) { |
| 150 | /* match data model based on namespace */ |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 151 | if (ly_strequal(ctx->models.list[i]->ns, xml->ns->value, 1)) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 152 | /* get the proper schema node */ |
| 153 | LY_TREE_FOR(ctx->models.list[i]->data, schema) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 154 | /* skip nodes in module's data which are not expected here according to options' data type */ |
| 155 | if (options & LYD_OPT_RPC) { |
| 156 | if (schema->nodetype != LYS_RPC) { |
| 157 | continue; |
| 158 | } |
| 159 | } else if (options & LYD_OPT_NOTIF) { |
| 160 | if (schema->nodetype != LYS_NOTIF) { |
| 161 | continue; |
| 162 | } |
| 163 | } else if (!(options & LYD_OPT_RPCREPLY)) { |
| 164 | /* rest of the data types except RPCREPLY which cannot be here */ |
| 165 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
| 166 | continue; |
| 167 | } |
| 168 | } |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 169 | if (ly_strequal(schema->name, xml->name, 1)) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | } |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | } else { |
| 177 | /* parsing some internal node, we start with parent's schema pointer */ |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 178 | schema = xml_data_search_schemanode(xml, parent->schema->child, options); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 179 | } |
| 180 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 181 | if ((options & LYD_OPT_STRICT) || ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 182 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, parent, xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 183 | return -1; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 184 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 185 | return 0; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 186 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 189 | /* create the element structure */ |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 190 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 191 | case LYS_CONTAINER: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 192 | case LYS_LIST: |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 193 | case LYS_NOTIF: |
| 194 | case LYS_RPC: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 195 | *result = calloc(1, sizeof **result); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 196 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 197 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 198 | case LYS_LEAF: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 199 | case LYS_LEAFLIST: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 200 | *result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 201 | havechildren = 0; |
| 202 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 203 | case LYS_ANYXML: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 204 | *result = calloc(1, sizeof(struct lyd_node_anyxml)); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 205 | havechildren = 0; |
| 206 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 207 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 208 | LOGINT; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 209 | return -1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 210 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 211 | if (!(*result)) { |
| 212 | LOGMEM; |
| 213 | return -1; |
| 214 | } |
| 215 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 216 | (*result)->parent = parent; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 217 | if (parent && !parent->child) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 218 | parent->child = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 219 | } |
| 220 | if (prev) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 221 | (*result)->prev = prev; |
| 222 | prev->next = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 223 | |
| 224 | /* fix the "last" pointer */ |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 225 | if (parent) { |
| 226 | diter = parent->child; |
| 227 | } else { |
| 228 | for (diter = prev; diter->prev != prev; diter = diter->prev); |
| 229 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 230 | diter->prev = *result; |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 231 | first_sibling = diter; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 232 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 233 | (*result)->prev = *result; |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 234 | first_sibling = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 235 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 236 | (*result)->schema = schema; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 237 | (*result)->validity = LYD_VAL_NOT; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 238 | if (resolve_applies_when(*result)) { |
| 239 | (*result)->when_status = LYD_WHEN; |
| 240 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 241 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 242 | /* check insert attribute and its values */ |
| 243 | if (options & LYD_OPT_EDIT) { |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 244 | /* 0x01 - insert attribute present |
| 245 | * 0x02 - insert is relative (before or after) |
| 246 | * 0x04 - value attribute present |
| 247 | * 0x08 - key attribute present |
| 248 | * 0x10 - operation not allowing insert attribute |
| 249 | */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 250 | i = 0; |
| 251 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 252 | if (attr->type != LYXML_ATTR_STD || !attr->ns) { |
| 253 | /* not interesting attribute or namespace declaration */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 254 | continue; |
| 255 | } |
| 256 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 257 | if (!strcmp(attr->name, "operation") && !strcmp(attr->ns->value, LY_NSNC)) { |
Radek Krejci | 72614bf | 2016-04-08 17:46:07 +0200 | [diff] [blame] | 258 | if (i & 0x10) { |
| 259 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "operation attributes", xml->name); |
| 260 | return -1; |
| 261 | } |
| 262 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 263 | if (!strcmp(attr->value, "delete") || !strcmp(attr->value, "remove")) { |
| 264 | i |= 0x10; |
| 265 | } else if (strcmp(attr->value, "create") && |
| 266 | strcmp(attr->value, "merge") && |
| 267 | strcmp(attr->value, "replace")) { |
| 268 | /* unknown operation */ |
| 269 | LOGVAL(LYE_INVALATTR, LY_VLOG_LYD, (*result), attr->value, attr->name); |
| 270 | return -1; |
| 271 | } |
| 272 | } else if (!strcmp(attr->name, "insert") && !strcmp(attr->ns->value, LY_NSYANG)) { |
| 273 | /* 'insert' attribute present */ |
| 274 | if (!(schema->flags & LYS_USERORDERED)) { |
| 275 | /* ... but it is not expected */ |
| 276 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), "insert", schema->name); |
| 277 | return -1; |
| 278 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 279 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 280 | if (i & 0x01) { |
| 281 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "insert attributes", xml->name); |
| 282 | return -1; |
| 283 | } |
| 284 | if (!strcmp(attr->value, "first") || !strcmp(attr->value, "last")) { |
| 285 | i |= 0x01; |
| 286 | } else if (!strcmp(attr->value, "before") || !strcmp(attr->value, "after")) { |
| 287 | i |= 0x01 | 0x02; |
| 288 | } else { |
| 289 | LOGVAL(LYE_INVALATTR, LY_VLOG_LYD, (*result), attr->value, attr->name); |
| 290 | return -1; |
| 291 | } |
| 292 | str = attr->name; |
| 293 | } else if (!strcmp(attr->name, "value") && !strcmp(attr->ns->value, LY_NSYANG)) { |
| 294 | if (i & 0x04) { |
| 295 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "value attributes", xml->name); |
| 296 | return -1; |
| 297 | } else if (schema->nodetype & LYS_LIST) { |
| 298 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, schema->name); |
| 299 | return -1; |
| 300 | } |
| 301 | i |= 0x04; |
| 302 | str = attr->name; |
| 303 | } else if (!strcmp(attr->name, "key") && !strcmp(attr->ns->value, LY_NSYANG)) { |
| 304 | if (i & 0x08) { |
| 305 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "key attributes", xml->name); |
| 306 | return -1; |
| 307 | } else if (schema->nodetype & LYS_LEAFLIST) { |
| 308 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, schema->name); |
| 309 | return -1; |
| 310 | } |
| 311 | i |= 0x08; |
| 312 | str = attr->name; |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 313 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 316 | /* report errors */ |
Radek Krejci | cced938 | 2016-04-13 13:15:03 +0200 | [diff] [blame] | 317 | if (i > 0x10 || (i && i < 0x10 && |
| 318 | (!(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_USERORDERED)))) { |
Radek Krejci | d0df690 | 2016-03-10 09:32:00 +0100 | [diff] [blame] | 319 | /* attributes in wrong elements */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 320 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), str, xml->name); |
Radek Krejci | d0df690 | 2016-03-10 09:32:00 +0100 | [diff] [blame] | 321 | return -1; |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 322 | } else if (i == 3) { |
| 323 | /* 0x01 | 0x02 - relative position, but value/key is missing */ |
| 324 | if (schema->nodetype & LYS_LIST) { |
| 325 | LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "key", xml->name); |
| 326 | } else { /* LYS_LEAFLIST */ |
| 327 | LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "value", xml->name); |
| 328 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 329 | return -1; |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 330 | } else if ((i & (0x04 | 0x08)) && !(i & 0x02)) { |
| 331 | /* key/value without relative position */ |
| 332 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), (i & 0x04) ? "value" : "key", schema->name); |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 333 | return -1; |
| 334 | } |
| 335 | } |
| 336 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 337 | /* type specific processing */ |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 338 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 339 | /* type detection and assigning the value */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 340 | if (xml_get_value(*result, xml, options)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 341 | goto error; |
| 342 | } |
Radek Krejci | 92ece00 | 2016-04-04 15:45:05 +0200 | [diff] [blame] | 343 | } else if (schema->nodetype == LYS_ANYXML) { |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 344 | /* store children values */ |
| 345 | if (xml->child) { |
| 346 | child = xml->child; |
| 347 | /* manually unlink all siblings and correct namespaces */ |
| 348 | xml->child = NULL; |
| 349 | LY_TREE_FOR(child, next) { |
| 350 | next->parent = NULL; |
| 351 | lyxml_correct_elem_ns(ctx, next, 1, 1); |
| 352 | } |
| 353 | |
| 354 | ((struct lyd_node_anyxml *)*result)->xml_struct = 1; |
| 355 | ((struct lyd_node_anyxml *)*result)->value.xml = child; |
| 356 | } else { |
| 357 | ((struct lyd_node_anyxml *)*result)->xml_struct = 0; |
| 358 | ((struct lyd_node_anyxml *)*result)->value.str = lydict_insert(ctx, xml->content, 0); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 359 | } |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Michal Vasko | 10e586f | 2016-05-18 13:25:30 +0200 | [diff] [blame] | 362 | /* first part of validation checks */ |
| 363 | if (!(options & LYD_OPT_TRUSTED) && lyv_data_context(*result, options, unres)) { |
| 364 | goto error; |
| 365 | } |
| 366 | |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 367 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 368 | flag = 0; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 369 | if (attr->type != LYXML_ATTR_STD) { |
| 370 | continue; |
| 371 | } else if (!attr->ns) { |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 372 | if ((*result)->schema->nodetype != LYS_ANYXML || |
| 373 | !ly_strequal((*result)->schema->name, "filter", 0) || |
| 374 | !ly_strequal((*result)->schema->module->name, "ietf-netconf", 0)) { |
Radek Krejci | 0f72123 | 2016-05-03 14:52:29 +0200 | [diff] [blame] | 375 | if (options & LYD_OPT_STRICT) { |
| 376 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, xml->name); |
| 377 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Attribute \"%s\" with no namespace (schema).", |
| 378 | attr->name); |
| 379 | goto error; |
| 380 | } else { |
| 381 | LOGWRN("Ignoring \"%s\" attribute in \"%s\" element.", attr->name, xml->name); |
| 382 | continue; |
| 383 | } |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 384 | } else { |
| 385 | /* exception for filter's attributes */ |
| 386 | flag = 1; |
| 387 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | dattr = malloc(sizeof *dattr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 391 | if (!dattr) { |
| 392 | goto error; |
| 393 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 394 | dattr->next = NULL; |
| 395 | dattr->name = attr->name; |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 396 | if (flag && ly_strequal(attr->name, "select", 0)) { |
| 397 | dattr->value = transform_xml2json(ctx, attr->value, xml, 1); |
| 398 | if (!dattr->value) { |
| 399 | free(dattr); |
| 400 | goto error; |
| 401 | } |
| 402 | lydict_remove(ctx, attr->value); |
| 403 | } else { |
| 404 | dattr->value = attr->value; |
| 405 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 406 | attr->name = NULL; |
| 407 | attr->value = NULL; |
| 408 | |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 409 | if (!attr->ns) { |
| 410 | /* filter's attributes, it actually has no namespace, but we need some for internal representation */ |
| 411 | dattr->module = (*result)->schema->module; |
| 412 | } else { |
| 413 | dattr->module = (struct lys_module *)ly_ctx_get_module_by_ns(ctx, attr->ns->value, NULL); |
| 414 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 415 | if (!dattr->module) { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 416 | free(dattr); |
Radek Krejci | 0f72123 | 2016-05-03 14:52:29 +0200 | [diff] [blame] | 417 | if (options & LYD_OPT_STRICT) { |
| 418 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, xml->name); |
| 419 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Attribute \"%s\" from unknown schema (\"%s\").", |
| 420 | attr->name, attr->ns->value); |
| 421 | goto error; |
| 422 | } else { |
| 423 | LOGWRN("Attribute \"%s\" from unknown schema (\"%s\") - skipping.", attr->name, attr->ns->value); |
| 424 | continue; |
| 425 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 428 | if (!(*result)->attr) { |
| 429 | (*result)->attr = dattr; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 430 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 431 | for (dattr_iter = (*result)->attr; dattr_iter->next; dattr_iter = dattr_iter->next); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 432 | dattr_iter->next = dattr; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 433 | } |
| 434 | } |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 435 | |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 436 | /* process children */ |
| 437 | if (havechildren && xml->child) { |
| 438 | diter = dlast = NULL; |
| 439 | LY_TREE_FOR_SAFE(xml->child, next, child) { |
| 440 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 441 | r = xml_parse_data(ctx, child, NULL, *result, dlast, 0, unres, &diter); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 442 | } else { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 443 | r = xml_parse_data(ctx, child, NULL, *result, dlast, options, unres, &diter); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 444 | } |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 445 | if (r) { |
| 446 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 447 | } else if (options & LYD_OPT_DESTRUCT) { |
| 448 | lyxml_free(ctx, child); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 449 | } |
| 450 | if (diter) { |
| 451 | dlast = diter; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 456 | /* if we have empty non-presence container, we can remove it */ |
| 457 | if (!(options & LYD_OPT_KEEPEMPTYCONT) && schema->nodetype == LYS_CONTAINER && !(*result)->child && |
Radek Krejci | d3e7372 | 2016-05-23 12:24:55 +0200 | [diff] [blame^] | 458 | !(*result)->attr && !((struct lys_node_container *)schema)->presence) { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 459 | goto clear; |
| 460 | } |
| 461 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 462 | /* rest of validation checks */ |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 463 | ly_errno = 0; |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 464 | if (!(options & LYD_OPT_TRUSTED) && |
| 465 | (lyv_data_content(*result, options, unres) || |
Radek Krejci | 76fed2b | 2016-04-11 14:55:23 +0200 | [diff] [blame] | 466 | lyv_multicases(*result, NULL, first_sibling == *result ? NULL : first_sibling, 0, NULL))) { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 467 | if (ly_errno) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 468 | goto error; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 469 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 470 | goto clear; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 471 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 474 | /* validation successful */ |
| 475 | (*result)->validity = LYD_VAL_OK; |
| 476 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 477 | return ret; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 478 | |
| 479 | error: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 480 | ret--; |
| 481 | |
| 482 | clear: |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 483 | /* cleanup */ |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 484 | for (i = unres->count - 1; i >= 0; i--) { |
| 485 | /* remove unres items connected with the node being removed */ |
| 486 | if (unres->node[i] == *result) { |
| 487 | unres_data_del(unres, i); |
| 488 | } |
| 489 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 490 | lyd_free(*result); |
| 491 | *result = NULL; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 492 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 493 | return ret; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 496 | API struct lyd_node * |
| 497 | lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options, ...) |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 498 | { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 499 | va_list ap; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 500 | int r; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 501 | struct unres_data *unres = NULL; |
Michal Vasko | 0a073da | 2016-05-10 15:48:40 +0200 | [diff] [blame] | 502 | struct lys_node *rpc = NULL; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 503 | struct lyd_node *result = NULL, *iter, *last; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 504 | struct lyxml_elem *xmlstart, *xmlelem, *xmlaux; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 505 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 506 | ly_errno = LY_SUCCESS; |
| 507 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 508 | if (!ctx || !root) { |
| 509 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 510 | return NULL; |
| 511 | } |
| 512 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 513 | if (lyp_check_options(options)) { |
| 514 | LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__); |
| 515 | return NULL; |
| 516 | } |
| 517 | |
Radek Krejci | a6939c3 | 2016-03-24 15:19:09 +0100 | [diff] [blame] | 518 | if (!(*root)) { |
| 519 | /* empty tree - no work is needed */ |
| 520 | lyd_validate(&result, options, ctx); |
| 521 | return result; |
| 522 | } |
| 523 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 524 | unres = calloc(1, sizeof *unres); |
| 525 | if (!unres) { |
| 526 | LOGMEM; |
| 527 | return NULL; |
| 528 | } |
| 529 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 530 | va_start(ap, options); |
| 531 | if (options & LYD_OPT_RPCREPLY) { |
| 532 | rpc = va_arg(ap, struct lys_node*); |
| 533 | if (!rpc || (rpc->nodetype != LYS_RPC)) { |
| 534 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 535 | goto error; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 539 | if (!(options & LYD_OPT_NOSIBLINGS)) { |
| 540 | /* locate the first root to process */ |
| 541 | if ((*root)->parent) { |
| 542 | xmlstart = (*root)->parent->child; |
| 543 | } else { |
| 544 | xmlstart = *root; |
| 545 | while(xmlstart->prev->next) { |
| 546 | xmlstart = xmlstart->prev; |
| 547 | } |
Radek Krejci | 04b97de | 2015-10-31 23:09:15 +0100 | [diff] [blame] | 548 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 549 | } else { |
| 550 | xmlstart = *root; |
| 551 | } |
| 552 | iter = result = last = NULL; |
| 553 | |
| 554 | LY_TREE_FOR_SAFE(xmlstart, xmlaux, xmlelem) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 555 | r = xml_parse_data(ctx, xmlelem, rpc, NULL, last, options, unres, &iter); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 556 | if (r) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 557 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 558 | } else if (options & LYD_OPT_DESTRUCT) { |
| 559 | lyxml_free(ctx, xmlelem); |
| 560 | *root = xmlaux; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 561 | } |
| 562 | if (iter) { |
| 563 | last = iter; |
| 564 | } |
| 565 | if (!result) { |
| 566 | result = iter; |
| 567 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 568 | |
| 569 | if (options & LYD_OPT_NOSIBLINGS) { |
| 570 | /* stop after the first processed root */ |
| 571 | break; |
| 572 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 573 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 574 | |
Michal Vasko | b8029b5 | 2016-05-11 16:26:16 +0200 | [diff] [blame] | 575 | if (options & LYD_OPT_RPCREPLY) { |
Michal Vasko | 79450fb | 2016-05-23 11:26:55 +0200 | [diff] [blame] | 576 | last = result; |
Michal Vasko | b8029b5 | 2016-05-11 16:26:16 +0200 | [diff] [blame] | 577 | result = lyd_new_output(NULL, lys_node_module(rpc), rpc->name); |
Michal Vasko | 79450fb | 2016-05-23 11:26:55 +0200 | [diff] [blame] | 578 | /* insert all the output parameters into RPC */ |
| 579 | do { |
| 580 | iter = last; |
| 581 | last = iter->next; |
| 582 | if (lyd_insert(result, iter)) { |
| 583 | LOGINT; |
| 584 | lyd_free_withsiblings(last); |
| 585 | goto error; |
| 586 | } |
| 587 | } while (last); |
Michal Vasko | b8029b5 | 2016-05-11 16:26:16 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 590 | /* check for missing top level mandatory nodes */ |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 591 | if (lyd_check_topmandatory(result, ctx, options)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 592 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 595 | /* add/validate default values, unres */ |
Michal Vasko | cd5dbe1 | 2016-05-12 10:56:38 +0200 | [diff] [blame] | 596 | if (lyd_defaults_add_unres(&result, options, ctx, unres)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 597 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 600 | free(unres->node); |
| 601 | free(unres->type); |
| 602 | free(unres); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 603 | va_end(ap); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 604 | |
| 605 | return result; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 606 | |
| 607 | error: |
| 608 | lyd_free_withsiblings(result); |
| 609 | free(unres->node); |
| 610 | free(unres->type); |
| 611 | free(unres); |
| 612 | va_end(ap); |
| 613 | |
| 614 | return NULL; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 615 | } |