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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
| 22 | #include <assert.h> |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 24 | #include <errno.h> |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 25 | #include <stdarg.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 28 | #include <limits.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 29 | |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 30 | #include "libyang.h" |
| 31 | #include "common.h" |
| 32 | #include "context.h" |
Michal Vasko | b798232 | 2015-10-16 13:56:12 +0200 | [diff] [blame] | 33 | #include "parser.h" |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 34 | #include "tree_internal.h" |
| 35 | #include "validation.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 36 | #include "xml_internal.h" |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 37 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 38 | /* does not log */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 39 | static struct lys_node * |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 40 | 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] | 41 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 42 | struct lys_node *result, *aux; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 43 | |
| 44 | LY_TREE_FOR(start, result) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 45 | /* skip groupings ... */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 46 | if (result->nodetype == LYS_GROUPING) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 47 | continue; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 48 | /* ... and output in case of RPC ... */ |
| 49 | } else if (result->nodetype == LYS_OUTPUT && (options & LYD_OPT_RPC)) { |
| 50 | continue; |
| 51 | /* ... and input in case of RPC reply */ |
| 52 | } else if (result->nodetype == LYS_INPUT && (options & LYD_OPT_RPCREPLY)) { |
| 53 | continue; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Radek Krejci | fb54be4 | 2015-10-02 15:21:16 +0200 | [diff] [blame] | 56 | /* go into cases, choices, uses and in RPCs into input and output */ |
| 57 | 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] | 58 | aux = xml_data_search_schemanode(xml, result->child, options); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 59 | if (aux) { |
| 60 | /* we have matching result */ |
| 61 | return aux; |
| 62 | } |
| 63 | /* else, continue with next schema node */ |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | /* match data nodes */ |
| 68 | if (result->name == xml->name) { |
| 69 | /* names matches, what about namespaces? */ |
| 70 | if (result->module->ns == xml->ns->value) { |
| 71 | /* we have matching result */ |
| 72 | return result; |
| 73 | } |
| 74 | /* else, continue with next schema node */ |
| 75 | continue; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* no match */ |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 83 | /* logs directly */ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 84 | static int |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 85 | xml_get_value(struct lyd_node *node, struct lyxml_elem *xml, int options, struct unres_data *unres) |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 86 | { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 87 | struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 88 | int resolve; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 89 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 90 | assert(node && (node->schema->nodetype & (LYS_LEAFLIST | LYS_LEAF)) && xml && unres); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 91 | |
| 92 | leaf->value_str = xml->content; |
| 93 | xml->content = NULL; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 94 | |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 95 | /* will be changed in case of union */ |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 96 | leaf->value_type = ((struct lys_node_leaf *)node->schema)->type.base; |
Radek Krejci | e3c3314 | 2015-08-10 15:04:36 +0200 | [diff] [blame] | 97 | |
Radek Krejci | e2cf7c1 | 2015-08-12 10:24:05 +0200 | [diff] [blame] | 98 | if ((options & LYD_OPT_FILTER) && !leaf->value_str) { |
| 99 | /* no value in filter (selection) node -> nothing more is needed */ |
| 100 | return EXIT_SUCCESS; |
| 101 | } |
| 102 | |
Radek Krejci | 20cdf63 | 2015-11-09 14:44:58 +0100 | [diff] [blame] | 103 | if (options & (LYD_OPT_FILTER | LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) { |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 104 | resolve = 0; |
| 105 | } else { |
| 106 | resolve = 1; |
| 107 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 108 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 109 | 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] | 110 | /* convert the path from the XML form using XML namespaces into the JSON format |
| 111 | * using module names as namespaces |
| 112 | */ |
| 113 | xml->content = leaf->value_str; |
Michal Vasko | fba1526 | 2015-10-21 12:10:28 +0200 | [diff] [blame] | 114 | 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] | 115 | lydict_remove(leaf->schema->module->ctx, xml->content); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 116 | xml->content = NULL; |
| 117 | if (!leaf->value_str) { |
| 118 | return EXIT_FAILURE; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 119 | } |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 120 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 121 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 122 | if (lyp_parse_value(leaf, xml, resolve, unres, LOGLINE(xml))) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 123 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return EXIT_SUCCESS; |
| 127 | } |
| 128 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 129 | /* logs directly */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 130 | static int |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 131 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, const struct lys_node *schema_parent, struct lyd_node *parent, |
| 132 | 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] | 133 | { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 134 | struct lyd_node *diter, *dlast; |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 135 | struct lys_node *schema = NULL; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 136 | struct lyd_attr *dattr, *dattr_iter; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 137 | struct lyxml_attr *attr; |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 138 | struct lyxml_elem *tmp_xml, *child, *next; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 139 | int i, havechildren, r; |
| 140 | int ret = 0; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 141 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 142 | assert(xml); |
| 143 | assert(result); |
| 144 | *result = NULL; |
| 145 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 146 | if (!xml->ns || !xml->ns->value) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 147 | LOGVAL(LYE_XML_MISS, LOGLINE(xml), "element's", "namespace"); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 148 | return -1; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* find schema node */ |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 152 | if (schema_parent) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 153 | schema = xml_data_search_schemanode(xml, schema_parent->child, options); |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 154 | } else if (!parent) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 155 | /* starting in root */ |
| 156 | for (i = 0; i < ctx->models.used; i++) { |
| 157 | /* match data model based on namespace */ |
| 158 | if (ctx->models.list[i]->ns == xml->ns->value) { |
| 159 | /* get the proper schema node */ |
| 160 | LY_TREE_FOR(ctx->models.list[i]->data, schema) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 161 | /* skip nodes in module's data which are not expected here according to options' data type */ |
| 162 | if (options & LYD_OPT_RPC) { |
| 163 | if (schema->nodetype != LYS_RPC) { |
| 164 | continue; |
| 165 | } |
| 166 | } else if (options & LYD_OPT_NOTIF) { |
| 167 | if (schema->nodetype != LYS_NOTIF) { |
| 168 | continue; |
| 169 | } |
| 170 | } else if (!(options & LYD_OPT_RPCREPLY)) { |
| 171 | /* rest of the data types except RPCREPLY which cannot be here */ |
| 172 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
| 173 | continue; |
| 174 | } |
| 175 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 176 | if (schema->name == xml->name) { |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } else { |
| 184 | /* parsing some internal node, we start with parent's schema pointer */ |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 185 | schema = xml_data_search_schemanode(xml, parent->schema->child, options); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 186 | } |
| 187 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 188 | if ((options & LYD_OPT_STRICT) || ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL)) { |
| 189 | LOGVAL(LYE_INELEM, LOGLINE(xml), xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 190 | return -1; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 191 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 192 | return 0; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 193 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 196 | /* check insert attribute and its values */ |
| 197 | if (options & LYD_OPT_EDIT) { |
| 198 | i = 0; |
| 199 | for (attr = xml->attr; attr; attr = attr->next) { |
| 200 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 201 | strcmp(attr->name, "insert") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | /* insert attribute present */ |
| 206 | if (!(schema->flags & LYS_USERORDERED)) { |
| 207 | /* ... but it is not expected */ |
| 208 | LOGVAL(LYE_INATTR, LOGLINE(xml), "insert", schema->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 209 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (i) { |
| 213 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "insert attributes", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 214 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 215 | } |
| 216 | if (!strcmp(attr->value, "first") || !strcmp(attr->value, "last")) { |
| 217 | i = 1; |
| 218 | } else if (!strcmp(attr->value, "before") || !strcmp(attr->value, "after")) { |
| 219 | i = 2; |
| 220 | } else { |
| 221 | LOGVAL(LYE_INARG, LOGLINE(xml), attr->value, attr->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 222 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | for (attr = xml->attr; attr; attr = attr->next) { |
| 227 | if (attr->type != LYXML_ATTR_STD || !attr->ns || |
| 228 | strcmp(attr->name, "value") || strcmp(attr->ns->value, LY_NSYANG)) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | /* the value attribute is present */ |
| 233 | if (i < 2) { |
| 234 | /* but it shouldn't */ |
| 235 | LOGVAL(LYE_INATTR, LOGLINE(xml), "value", schema->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 236 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 237 | } |
| 238 | i++; |
| 239 | } |
| 240 | if (i == 2) { |
| 241 | /* missing value attribute for "before" or "after" */ |
| 242 | LOGVAL(LYE_MISSATTR, LOGLINE(xml), "value", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 243 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 244 | } else if (i > 3) { |
| 245 | /* more than one instance of the value attribute */ |
| 246 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), "value attributes", xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 247 | return -1; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 248 | } |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 251 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 252 | case LYS_CONTAINER: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 253 | case LYS_LIST: |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 254 | case LYS_NOTIF: |
| 255 | case LYS_RPC: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 256 | *result = calloc(1, sizeof **result); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 257 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 258 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 259 | case LYS_LEAF: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 260 | case LYS_LEAFLIST: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 261 | *result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 262 | havechildren = 0; |
| 263 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 264 | case LYS_ANYXML: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 265 | *result = calloc(1, sizeof(struct lyd_node_anyxml)); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 266 | havechildren = 0; |
| 267 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 268 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 269 | LOGINT; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 270 | return -1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 271 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 272 | if (!(*result)) { |
| 273 | LOGMEM; |
| 274 | return -1; |
| 275 | } |
| 276 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 277 | (*result)->parent = parent; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 278 | if (parent && !parent->child) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 279 | parent->child = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 280 | } |
| 281 | if (prev) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 282 | (*result)->prev = prev; |
| 283 | prev->next = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 284 | |
| 285 | /* fix the "last" pointer */ |
| 286 | for (diter = prev; diter->prev != prev; diter = diter->prev); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 287 | diter->prev = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 288 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 289 | (*result)->prev = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 290 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 291 | (*result)->schema = schema; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame^] | 292 | (*result)->validity = LYD_VAL_NOT; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 293 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 294 | if (lyv_data_context(*result, options, LOGLINE(xml), unres)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 295 | goto error; |
| 296 | } |
| 297 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 298 | /* type specific processing */ |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 299 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 300 | /* type detection and assigning the value */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 301 | if (xml_get_value(*result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 302 | goto error; |
| 303 | } |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 304 | } else if (schema->nodetype == LYS_ANYXML && !(options & LYD_OPT_FILTER)) { |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 305 | /* HACK unlink xml children and link them to a separate copy of xml */ |
| 306 | tmp_xml = calloc(1, sizeof *tmp_xml); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 307 | if (!tmp_xml) { |
| 308 | LOGMEM; |
| 309 | goto error; |
| 310 | } |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 311 | memcpy(tmp_xml, xml, sizeof *tmp_xml); |
| 312 | /* keep attributes in the original */ |
| 313 | tmp_xml->attr = NULL; |
| 314 | /* increase reference counters on strings */ |
| 315 | tmp_xml->name = lydict_insert(ctx, tmp_xml->name, 0); |
| 316 | tmp_xml->content = lydict_insert(ctx, tmp_xml->content, 0); |
| 317 | xml->child = NULL; |
| 318 | /* xml is correct now */ |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 319 | |
Michal Vasko | 17cc706 | 2015-12-10 14:31:48 +0100 | [diff] [blame] | 320 | LY_TREE_FOR(tmp_xml->child, child) { |
| 321 | child->parent = tmp_xml; |
| 322 | } |
| 323 | /* children are correct now */ |
| 324 | |
| 325 | tmp_xml->parent = NULL; |
| 326 | lyxml_unlink_elem(ctx, tmp_xml, 1); |
| 327 | /* tmp_xml is correct now */ |
| 328 | |
| 329 | ((struct lyd_node_anyxml *)*result)->value = tmp_xml; |
Michal Vasko | 60beecb | 2015-09-03 14:24:09 +0200 | [diff] [blame] | 330 | /* we can safely continue with xml, it's like it was, only without children */ |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 333 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 334 | if (attr->type != LYXML_ATTR_STD) { |
| 335 | continue; |
| 336 | } else if (!attr->ns) { |
| 337 | LOGWRN("Ignoring \"%s\" attribute in \"%s\" element.", attr->name, xml->name); |
| 338 | continue; |
| 339 | } |
| 340 | |
| 341 | dattr = malloc(sizeof *dattr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 342 | if (!dattr) { |
| 343 | goto error; |
| 344 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 345 | dattr->next = NULL; |
| 346 | dattr->name = attr->name; |
| 347 | dattr->value = attr->value; |
| 348 | attr->name = NULL; |
| 349 | attr->value = NULL; |
| 350 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 351 | dattr->module = (struct lys_module *)ly_ctx_get_module_by_ns(ctx, attr->ns->value, NULL); |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 352 | if (!dattr->module) { |
| 353 | LOGWRN("Attribute \"%s\" from unknown schema (\"%s\") - skipping.", attr->name, attr->ns->value); |
| 354 | free(dattr); |
| 355 | continue; |
| 356 | } |
| 357 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 358 | if (!(*result)->attr) { |
| 359 | (*result)->attr = dattr; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 360 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 361 | 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] | 362 | dattr_iter->next = dattr; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 363 | } |
| 364 | } |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 365 | |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 366 | /* process children */ |
| 367 | if (havechildren && xml->child) { |
| 368 | diter = dlast = NULL; |
| 369 | LY_TREE_FOR_SAFE(xml->child, next, child) { |
| 370 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 371 | r = xml_parse_data(ctx, child, NULL, *result, dlast, 0, unres, &diter); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 372 | } else { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 373 | r = xml_parse_data(ctx, child, NULL, *result, dlast, options, unres, &diter); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 374 | } |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 375 | if (r) { |
| 376 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 377 | } else if (options & LYD_OPT_DESTRUCT) { |
| 378 | lyxml_free(ctx, child); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 379 | } |
| 380 | if (diter) { |
| 381 | dlast = diter; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 386 | /* rest of validation checks */ |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 387 | ly_errno = 0; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 388 | if (lyv_data_content(*result, options, LOGLINE(xml), unres)) { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 389 | if (ly_errno) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 390 | goto error; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 391 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 392 | goto clear; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 393 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame^] | 396 | /* validation successful */ |
| 397 | (*result)->validity = LYD_VAL_OK; |
| 398 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 399 | return ret; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 400 | |
| 401 | error: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 402 | ret--; |
| 403 | |
| 404 | clear: |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 405 | /* cleanup */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 406 | lyd_free(*result); |
| 407 | *result = NULL; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 408 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 409 | return ret; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 412 | API struct lyd_node * |
| 413 | 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] | 414 | { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 415 | va_list ap; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 416 | int r; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 417 | struct unres_data *unres = NULL; |
| 418 | const struct lys_node *rpc = NULL; |
| 419 | struct lyd_node *result = NULL, *next, *iter, *last; |
| 420 | struct lyxml_elem *xmlstart, *xmlelem, *xmlaux; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 421 | |
| 422 | if (!ctx || !root) { |
| 423 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 424 | return NULL; |
| 425 | } |
| 426 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 427 | if (lyp_check_options(options)) { |
| 428 | LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__); |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | va_start(ap, options); |
| 433 | if (options & LYD_OPT_RPCREPLY) { |
| 434 | rpc = va_arg(ap, struct lys_node*); |
| 435 | if (!rpc || (rpc->nodetype != LYS_RPC)) { |
| 436 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 437 | goto cleanup; |
| 438 | } |
| 439 | } |
| 440 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 441 | unres = calloc(1, sizeof *unres); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 442 | if (!unres) { |
| 443 | LOGMEM; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 444 | goto cleanup; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 445 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 446 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 447 | if (!(options & LYD_OPT_NOSIBLINGS)) { |
| 448 | /* locate the first root to process */ |
| 449 | if ((*root)->parent) { |
| 450 | xmlstart = (*root)->parent->child; |
| 451 | } else { |
| 452 | xmlstart = *root; |
| 453 | while(xmlstart->prev->next) { |
| 454 | xmlstart = xmlstart->prev; |
| 455 | } |
Radek Krejci | 04b97de | 2015-10-31 23:09:15 +0100 | [diff] [blame] | 456 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 457 | } else { |
| 458 | xmlstart = *root; |
| 459 | } |
| 460 | iter = result = last = NULL; |
| 461 | |
| 462 | LY_TREE_FOR_SAFE(xmlstart, xmlaux, xmlelem) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 463 | r = xml_parse_data(ctx, xmlelem, rpc, NULL, last, options, unres, &iter); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 464 | if (r) { |
| 465 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 466 | lyd_free(iter); |
| 467 | } |
| 468 | result = NULL; |
| 469 | goto cleanup; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 470 | } else if (options & LYD_OPT_DESTRUCT) { |
| 471 | lyxml_free(ctx, xmlelem); |
| 472 | *root = xmlaux; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 473 | } |
| 474 | if (iter) { |
| 475 | last = iter; |
| 476 | } |
| 477 | if (!result) { |
| 478 | result = iter; |
| 479 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 480 | |
| 481 | if (options & LYD_OPT_NOSIBLINGS) { |
| 482 | /* stop after the first processed root */ |
| 483 | break; |
| 484 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 485 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 486 | |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 487 | if (!result) { |
| 488 | LOGERR(LY_EVALID, "Model for the data to be linked with not found."); |
| 489 | goto cleanup; |
| 490 | } |
| 491 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 492 | /* check leafrefs and/or instids if any */ |
| 493 | if (result && resolve_unres_data(unres)) { |
| 494 | /* leafref & instid checking failed */ |
| 495 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 496 | lyd_free(iter); |
| 497 | } |
| 498 | result = NULL; |
| 499 | } |
| 500 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 501 | cleanup: |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 502 | if (unres) { |
| 503 | free(unres->node); |
| 504 | free(unres->type); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 505 | #ifndef NDEBUG |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 506 | free(unres->line); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 507 | #endif |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 508 | free(unres); |
| 509 | } |
| 510 | va_end(ap); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 511 | |
| 512 | return result; |
| 513 | } |