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 |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 119 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, struct lyd_node *parent, struct lyd_node *first_sibling, |
| 120 | struct lyd_node *prev, int options, struct unres_data *unres, struct lyd_node **result, |
| 121 | struct lyd_node **action) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 122 | { |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 123 | struct lyd_node *diter, *dlast; |
Michal Vasko | 5e523b6 | 2016-08-26 16:23:15 +0200 | [diff] [blame] | 124 | struct lys_node *schema = NULL, *target; |
| 125 | struct lys_node_augment *aug; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 126 | struct lyd_attr *dattr, *dattr_iter; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 127 | struct lyxml_attr *attr; |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 128 | struct lyxml_elem *child, *next; |
Michal Vasko | 5e523b6 | 2016-08-26 16:23:15 +0200 | [diff] [blame] | 129 | int i, j, havechildren, r, flag; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 130 | int ret = 0; |
Radek Krejci | abb7b58 | 2016-04-20 16:15:47 +0200 | [diff] [blame] | 131 | const char *str = NULL; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 132 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 133 | assert(xml); |
| 134 | assert(result); |
| 135 | *result = NULL; |
| 136 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 137 | if (!xml->ns || !xml->ns->value) { |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 138 | if (options & LYD_OPT_STRICT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 139 | LOGVAL(LYE_XML_MISS, LY_VLOG_XML, xml, "element's", "namespace"); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 140 | return -1; |
| 141 | } else { |
| 142 | return 0; |
| 143 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* find schema node */ |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 147 | 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 */ |
Radek Krejci | 919a924 | 2016-07-27 08:17:13 +0200 | [diff] [blame] | 153 | schema = xml_data_search_schemanode(xml, ctx->models.list[i]->data, options); |
Michal Vasko | 5e523b6 | 2016-08-26 16:23:15 +0200 | [diff] [blame] | 154 | if (!schema) { |
| 155 | /* it still can be the specific case of this module containing an augment of another module |
| 156 | * top-level choice or top-level choice's case, bleh */ |
| 157 | for (j = 0; j < ctx->models.list[i]->augment_size; ++j) { |
| 158 | aug = &ctx->models.list[i]->augment[j]; |
| 159 | target = aug->target; |
| 160 | if (target->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 161 | /* 1) okay, the target is choice or case */ |
| 162 | while (target && (target->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES))) { |
| 163 | target = lys_parent(target); |
| 164 | } |
| 165 | /* 2) now, the data node will be top-level, there are only non-data schema nodes */ |
| 166 | if (!target) { |
| 167 | while ((schema = (struct lys_node *)lys_getnext(schema, (struct lys_node *)aug, NULL, 0))) { |
| 168 | /* 3) alright, even the name matches, we found our schema node */ |
| 169 | if (ly_strequal(schema->name, xml->name, 1)) { |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (schema) { |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | } |
| 184 | } else { |
| 185 | /* parsing some internal node, we start with parent's schema pointer */ |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 186 | schema = xml_data_search_schemanode(xml, parent->schema->child, options); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 187 | } |
| 188 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 189 | 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] | 190 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, parent, xml->name); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 191 | return -1; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 192 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 193 | return 0; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 194 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 197 | /* create the element structure */ |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 198 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 199 | case LYS_CONTAINER: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 200 | case LYS_LIST: |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 201 | case LYS_NOTIF: |
| 202 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 203 | case LYS_ACTION: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 204 | *result = calloc(1, sizeof **result); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 205 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 206 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 207 | case LYS_LEAF: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 208 | case LYS_LEAFLIST: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 209 | *result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 210 | havechildren = 0; |
| 211 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 212 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 213 | case LYS_ANYDATA: |
| 214 | *result = calloc(1, sizeof(struct lyd_node_anydata)); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 215 | havechildren = 0; |
| 216 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 217 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 218 | LOGINT; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 219 | return -1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 220 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 221 | if (!(*result)) { |
| 222 | LOGMEM; |
| 223 | return -1; |
| 224 | } |
| 225 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 226 | (*result)->parent = parent; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 227 | if (parent && !parent->child) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 228 | parent->child = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 229 | } |
| 230 | if (prev) { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 231 | (*result)->prev = prev; |
| 232 | prev->next = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 233 | |
| 234 | /* fix the "last" pointer */ |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 235 | first_sibling->prev = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 236 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 237 | (*result)->prev = *result; |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 238 | first_sibling = *result; |
Radek Krejci | c41bf8a | 2015-08-21 10:28:48 +0200 | [diff] [blame] | 239 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 240 | (*result)->schema = schema; |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 241 | (*result)->validity = LYD_VAL_NOT; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 242 | if (resolve_applies_when(schema, 0, NULL)) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 243 | (*result)->when_status = LYD_WHEN; |
| 244 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 245 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 246 | /* check insert attribute and its values */ |
| 247 | if (options & LYD_OPT_EDIT) { |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 248 | /* 0x01 - insert attribute present |
| 249 | * 0x02 - insert is relative (before or after) |
| 250 | * 0x04 - value attribute present |
| 251 | * 0x08 - key attribute present |
| 252 | * 0x10 - operation not allowing insert attribute |
| 253 | */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 254 | i = 0; |
| 255 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 256 | if (attr->type != LYXML_ATTR_STD || !attr->ns) { |
| 257 | /* not interesting attribute or namespace declaration */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 258 | continue; |
| 259 | } |
| 260 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 261 | if (!strcmp(attr->name, "operation") && !strcmp(attr->ns->value, LY_NSNC)) { |
Radek Krejci | 72614bf | 2016-04-08 17:46:07 +0200 | [diff] [blame] | 262 | if (i & 0x10) { |
| 263 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "operation attributes", xml->name); |
| 264 | return -1; |
| 265 | } |
| 266 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 267 | if (!strcmp(attr->value, "delete") || !strcmp(attr->value, "remove")) { |
| 268 | i |= 0x10; |
| 269 | } else if (strcmp(attr->value, "create") && |
| 270 | strcmp(attr->value, "merge") && |
| 271 | strcmp(attr->value, "replace")) { |
| 272 | /* unknown operation */ |
| 273 | LOGVAL(LYE_INVALATTR, LY_VLOG_LYD, (*result), attr->value, attr->name); |
| 274 | return -1; |
| 275 | } |
| 276 | } else if (!strcmp(attr->name, "insert") && !strcmp(attr->ns->value, LY_NSYANG)) { |
| 277 | /* 'insert' attribute present */ |
| 278 | if (!(schema->flags & LYS_USERORDERED)) { |
| 279 | /* ... but it is not expected */ |
| 280 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), "insert", schema->name); |
| 281 | return -1; |
| 282 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 283 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 284 | if (i & 0x01) { |
| 285 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "insert attributes", xml->name); |
| 286 | return -1; |
| 287 | } |
| 288 | if (!strcmp(attr->value, "first") || !strcmp(attr->value, "last")) { |
| 289 | i |= 0x01; |
| 290 | } else if (!strcmp(attr->value, "before") || !strcmp(attr->value, "after")) { |
| 291 | i |= 0x01 | 0x02; |
| 292 | } else { |
| 293 | LOGVAL(LYE_INVALATTR, LY_VLOG_LYD, (*result), attr->value, attr->name); |
| 294 | return -1; |
| 295 | } |
| 296 | str = attr->name; |
| 297 | } else if (!strcmp(attr->name, "value") && !strcmp(attr->ns->value, LY_NSYANG)) { |
| 298 | if (i & 0x04) { |
| 299 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "value attributes", xml->name); |
| 300 | return -1; |
| 301 | } else if (schema->nodetype & LYS_LIST) { |
| 302 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, schema->name); |
| 303 | return -1; |
| 304 | } |
| 305 | i |= 0x04; |
| 306 | str = attr->name; |
| 307 | } else if (!strcmp(attr->name, "key") && !strcmp(attr->ns->value, LY_NSYANG)) { |
| 308 | if (i & 0x08) { |
| 309 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "key attributes", xml->name); |
| 310 | return -1; |
| 311 | } else if (schema->nodetype & LYS_LEAFLIST) { |
| 312 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, schema->name); |
| 313 | return -1; |
| 314 | } |
| 315 | i |= 0x08; |
| 316 | str = attr->name; |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 317 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 320 | /* report errors */ |
Radek Krejci | cced938 | 2016-04-13 13:15:03 +0200 | [diff] [blame] | 321 | if (i > 0x10 || (i && i < 0x10 && |
| 322 | (!(schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) || !(schema->flags & LYS_USERORDERED)))) { |
Radek Krejci | d0df690 | 2016-03-10 09:32:00 +0100 | [diff] [blame] | 323 | /* attributes in wrong elements */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 324 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), str, xml->name); |
Radek Krejci | d0df690 | 2016-03-10 09:32:00 +0100 | [diff] [blame] | 325 | return -1; |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 326 | } else if (i == 3) { |
| 327 | /* 0x01 | 0x02 - relative position, but value/key is missing */ |
| 328 | if (schema->nodetype & LYS_LIST) { |
| 329 | LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "key", xml->name); |
| 330 | } else { /* LYS_LEAFLIST */ |
| 331 | LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "value", xml->name); |
| 332 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 333 | return -1; |
Radek Krejci | 9bfcbde | 2016-04-07 16:30:15 +0200 | [diff] [blame] | 334 | } else if ((i & (0x04 | 0x08)) && !(i & 0x02)) { |
| 335 | /* key/value without relative position */ |
| 336 | 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] | 337 | return -1; |
| 338 | } |
| 339 | } |
| 340 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 341 | /* type specific processing */ |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 342 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 343 | /* type detection and assigning the value */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 344 | if (xml_get_value(*result, xml, options)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 345 | goto error; |
| 346 | } |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 347 | } else if (schema->nodetype & LYS_ANYDATA) { |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 348 | /* store children values */ |
| 349 | if (xml->child) { |
| 350 | child = xml->child; |
| 351 | /* manually unlink all siblings and correct namespaces */ |
| 352 | xml->child = NULL; |
| 353 | LY_TREE_FOR(child, next) { |
| 354 | next->parent = NULL; |
| 355 | lyxml_correct_elem_ns(ctx, next, 1, 1); |
| 356 | } |
| 357 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 358 | ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_XML; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 359 | ((struct lyd_node_anydata *)*result)->value.xml = child; |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 360 | } else { |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 361 | ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_CONSTSTRING; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 362 | ((struct lyd_node_anydata *)*result)->value.str = lydict_insert(ctx, xml->content, 0); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 363 | } |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 364 | } else if (schema->nodetype == LYS_ACTION) { |
| 365 | options &= ~LYS_ACTION; |
| 366 | options |= LYS_RPC; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Michal Vasko | 10e586f | 2016-05-18 13:25:30 +0200 | [diff] [blame] | 369 | /* first part of validation checks */ |
| 370 | if (!(options & LYD_OPT_TRUSTED) && lyv_data_context(*result, options, unres)) { |
| 371 | goto error; |
| 372 | } |
| 373 | |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 374 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 375 | flag = 0; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 376 | if (attr->type != LYXML_ATTR_STD) { |
| 377 | continue; |
| 378 | } else if (!attr->ns) { |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 379 | if ((*result)->schema->nodetype != LYS_ANYXML || |
| 380 | !ly_strequal((*result)->schema->name, "filter", 0) || |
| 381 | !ly_strequal((*result)->schema->module->name, "ietf-netconf", 0)) { |
Radek Krejci | 0f72123 | 2016-05-03 14:52:29 +0200 | [diff] [blame] | 382 | if (options & LYD_OPT_STRICT) { |
| 383 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, xml->name); |
| 384 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Attribute \"%s\" with no namespace (schema).", |
| 385 | attr->name); |
| 386 | goto error; |
| 387 | } else { |
| 388 | LOGWRN("Ignoring \"%s\" attribute in \"%s\" element.", attr->name, xml->name); |
| 389 | continue; |
| 390 | } |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 391 | } else { |
| 392 | /* exception for filter's attributes */ |
| 393 | flag = 1; |
| 394 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | dattr = malloc(sizeof *dattr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 398 | if (!dattr) { |
| 399 | goto error; |
| 400 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 401 | dattr->next = NULL; |
| 402 | dattr->name = attr->name; |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 403 | if (flag && ly_strequal(attr->name, "select", 0)) { |
| 404 | dattr->value = transform_xml2json(ctx, attr->value, xml, 1); |
| 405 | if (!dattr->value) { |
| 406 | free(dattr); |
| 407 | goto error; |
| 408 | } |
| 409 | lydict_remove(ctx, attr->value); |
| 410 | } else { |
| 411 | dattr->value = attr->value; |
| 412 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 413 | attr->name = NULL; |
| 414 | attr->value = NULL; |
| 415 | |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 416 | if (!attr->ns) { |
| 417 | /* filter's attributes, it actually has no namespace, but we need some for internal representation */ |
| 418 | dattr->module = (*result)->schema->module; |
| 419 | } else { |
| 420 | dattr->module = (struct lys_module *)ly_ctx_get_module_by_ns(ctx, attr->ns->value, NULL); |
| 421 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 422 | if (!dattr->module) { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 423 | free(dattr); |
Radek Krejci | 0f72123 | 2016-05-03 14:52:29 +0200 | [diff] [blame] | 424 | if (options & LYD_OPT_STRICT) { |
| 425 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), attr->name, xml->name); |
| 426 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Attribute \"%s\" from unknown schema (\"%s\").", |
| 427 | attr->name, attr->ns->value); |
| 428 | goto error; |
| 429 | } else { |
| 430 | LOGWRN("Attribute \"%s\" from unknown schema (\"%s\") - skipping.", attr->name, attr->ns->value); |
| 431 | continue; |
| 432 | } |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 435 | if (!(*result)->attr) { |
| 436 | (*result)->attr = dattr; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 437 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 438 | 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] | 439 | dattr_iter->next = dattr; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 440 | } |
| 441 | } |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 442 | |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 443 | if ((*result)->schema->nodetype == LYS_ACTION) { |
| 444 | if (!(options & LYD_OPT_ACTION) || *action) { |
Radek Krejci | b45b308 | 2016-09-09 16:08:51 +0200 | [diff] [blame] | 445 | LOGVAL(LYE_INACT, LY_VLOG_LYD, (*result), "action", (*result)->schema->name); |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 446 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, (*result), "Unexpected action node \"%s\".", (*result)->schema->name); |
| 447 | goto error; |
| 448 | } |
| 449 | *action = *result; |
| 450 | } |
| 451 | |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 452 | /* process children */ |
| 453 | if (havechildren && xml->child) { |
| 454 | diter = dlast = NULL; |
| 455 | LY_TREE_FOR_SAFE(xml->child, next, child) { |
| 456 | if (schema->nodetype & (LYS_RPC | LYS_NOTIF)) { |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 457 | r = xml_parse_data(ctx, child, *result, (*result)->child, dlast, 0, unres, &diter, action); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 458 | } else { |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 459 | r = xml_parse_data(ctx, child, *result, (*result)->child, dlast, options, unres, &diter, action); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 460 | } |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 461 | if (r) { |
| 462 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 463 | } else if (options & LYD_OPT_DESTRUCT) { |
| 464 | lyxml_free(ctx, child); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 465 | } |
| 466 | if (diter) { |
| 467 | dlast = diter; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 472 | /* if we have empty non-presence container, we can remove it */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 473 | if (schema->nodetype == LYS_CONTAINER && !(*result)->child && |
Radek Krejci | d3e7372 | 2016-05-23 12:24:55 +0200 | [diff] [blame] | 474 | !(*result)->attr && !((struct lys_node_container *)schema)->presence) { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 475 | goto clear; |
| 476 | } |
| 477 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 478 | /* rest of validation checks */ |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 479 | ly_errno = 0; |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 480 | if (!(options & LYD_OPT_TRUSTED) && |
| 481 | (lyv_data_content(*result, options, unres) || |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 482 | lyv_multicases(*result, NULL, first_sibling == *result ? NULL : &first_sibling, 0, NULL))) { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 483 | if (ly_errno) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 484 | goto error; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 485 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 486 | goto clear; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 487 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 490 | /* validation successful */ |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 491 | if ((*result)->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 492 | /* postpone checking when there will be all list/leaflist instances */ |
| 493 | (*result)->validity = LYD_VAL_UNIQUE; |
| 494 | } else { |
| 495 | (*result)->validity = LYD_VAL_OK; |
| 496 | } |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 497 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 498 | return ret; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 499 | |
| 500 | error: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 501 | ret--; |
| 502 | |
| 503 | clear: |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 504 | /* cleanup */ |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 505 | for (i = unres->count - 1; i >= 0; i--) { |
| 506 | /* remove unres items connected with the node being removed */ |
| 507 | if (unres->node[i] == *result) { |
| 508 | unres_data_del(unres, i); |
| 509 | } |
| 510 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 511 | lyd_free(*result); |
| 512 | *result = NULL; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 513 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 514 | return ret; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 517 | API struct lyd_node * |
| 518 | 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] | 519 | { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 520 | va_list ap; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 521 | int r, i; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 522 | struct unres_data *unres = NULL; |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame^] | 523 | const struct lys_node *rpc_act = NULL; |
| 524 | struct lyd_node *result = NULL, *iter, *last, *reply_parent = NULL, *action = NULL, *data_tree = NULL; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 525 | struct lyxml_elem *xmlstart, *xmlelem, *xmlaux; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 526 | struct ly_set *set; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 527 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 528 | ly_errno = LY_SUCCESS; |
| 529 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 530 | if (!ctx || !root) { |
| 531 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 532 | return NULL; |
| 533 | } |
| 534 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 535 | if (lyp_check_options(options)) { |
| 536 | LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__); |
| 537 | return NULL; |
| 538 | } |
| 539 | |
Radek Krejci | a6939c3 | 2016-03-24 15:19:09 +0100 | [diff] [blame] | 540 | if (!(*root)) { |
| 541 | /* empty tree - no work is needed */ |
| 542 | lyd_validate(&result, options, ctx); |
| 543 | return result; |
| 544 | } |
| 545 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 546 | unres = calloc(1, sizeof *unres); |
| 547 | if (!unres) { |
| 548 | LOGMEM; |
| 549 | return NULL; |
| 550 | } |
| 551 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 552 | va_start(ap, options); |
| 553 | if (options & LYD_OPT_RPCREPLY) { |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame^] | 554 | rpc_act = va_arg(ap, const struct lys_node *); |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 555 | if (!rpc_act || !(rpc_act->nodetype & (LYS_RPC | LYS_ACTION))) { |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame^] | 556 | LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lys_node *rpc_act).", __func__); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 557 | goto error; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 558 | } |
Radek Krejci | 47fd5cb | 2016-09-01 16:50:22 +0200 | [diff] [blame] | 559 | reply_parent = _lyd_new(NULL, rpc_act, 0); |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame^] | 560 | |
| 561 | data_tree = va_arg(ap, struct lyd_node *); |
| 562 | if (data_tree) { |
| 563 | LY_TREE_FOR(data_tree, iter) { |
| 564 | if (iter->parent) { |
| 565 | /* a sibling is not top-level */ |
| 566 | LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__); |
| 567 | goto error; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* move it to the beginning */ |
| 572 | for (; data_tree->prev->next; data_tree = data_tree->prev); |
| 573 | |
| 574 | /* LYD_OPT_NOSIBLINGS cannot be set in this case */ |
| 575 | if (options & LYD_OPT_NOSIBLINGS) { |
| 576 | LOGERR(LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__); |
| 577 | goto error; |
| 578 | } |
| 579 | } |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 582 | if (!(options & LYD_OPT_NOSIBLINGS)) { |
| 583 | /* locate the first root to process */ |
| 584 | if ((*root)->parent) { |
| 585 | xmlstart = (*root)->parent->child; |
| 586 | } else { |
| 587 | xmlstart = *root; |
| 588 | while(xmlstart->prev->next) { |
| 589 | xmlstart = xmlstart->prev; |
| 590 | } |
Radek Krejci | 04b97de | 2015-10-31 23:09:15 +0100 | [diff] [blame] | 591 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 592 | } else { |
| 593 | xmlstart = *root; |
| 594 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 595 | |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 596 | if ((options & LYD_OPT_RPC) |
| 597 | && !strcmp(xmlstart->name, "action") && !strcmp(xmlstart->ns->value, "urn:ietf:params:xml:ns:yang:1")) { |
| 598 | /* it's an action, not a simple RPC */ |
| 599 | xmlstart = xmlstart->child; |
| 600 | options &= ~LYD_OPT_RPC; |
| 601 | options |= LYD_OPT_ACTION; |
| 602 | } |
| 603 | |
| 604 | iter = last = NULL; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 605 | LY_TREE_FOR_SAFE(xmlstart, xmlaux, xmlelem) { |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 606 | r = xml_parse_data(ctx, xmlelem, reply_parent, result, last, options, unres, &iter, &action); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 607 | if (r) { |
Pavol Vican | 8a552f6 | 2016-09-05 11:20:57 +0200 | [diff] [blame] | 608 | if (reply_parent) { |
| 609 | result = reply_parent; |
| 610 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 611 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 612 | } else if (options & LYD_OPT_DESTRUCT) { |
| 613 | lyxml_free(ctx, xmlelem); |
| 614 | *root = xmlaux; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 615 | } |
| 616 | if (iter) { |
| 617 | last = iter; |
| 618 | } |
| 619 | if (!result) { |
| 620 | result = iter; |
| 621 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 622 | |
| 623 | if (options & LYD_OPT_NOSIBLINGS) { |
| 624 | /* stop after the first processed root */ |
| 625 | break; |
| 626 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 627 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 628 | |
Michal Vasko | e45aff5 | 2016-08-25 09:01:09 +0200 | [diff] [blame] | 629 | if (reply_parent) { |
| 630 | result = reply_parent; |
| 631 | } |
| 632 | |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame^] | 633 | if (options & LYD_OPT_ACTION) { |
| 634 | if (!action) { |
| 635 | ly_vecode = LYVE_INACT; |
| 636 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing action node."); |
| 637 | goto error; |
| 638 | } |
| 639 | options &= ~LYD_OPT_ACTION; |
| 640 | options |= LYD_OPT_RPC; |
Radek Krejci | b45b308 | 2016-09-09 16:08:51 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 643 | /* check for uniquness of top-level lists/leaflists because |
| 644 | * only the inner instances were tested in lyv_data_content() */ |
| 645 | set = ly_set_new(); |
| 646 | LY_TREE_FOR(result, iter) { |
| 647 | if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) { |
| 648 | continue; |
| 649 | } |
| 650 | |
| 651 | /* check each list/leaflist only once */ |
| 652 | i = set->number; |
| 653 | if (ly_set_add(set, iter->schema, 0) != i) { |
| 654 | /* already checked */ |
| 655 | continue; |
| 656 | } |
| 657 | |
| 658 | if (lyv_data_unique(iter, result)) { |
| 659 | ly_set_free(set); |
| 660 | goto error; |
| 661 | } |
| 662 | } |
| 663 | ly_set_free(set); |
| 664 | |
Radek Krejci | b45b308 | 2016-09-09 16:08:51 +0200 | [diff] [blame] | 665 | /* add default values, resolve unres and check for mandatory nodes in final tree */ |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame^] | 666 | if (lyd_defaults_add_unres(&result, options, ctx, data_tree, action, unres)) { |
| 667 | goto error; |
| 668 | } |
| 669 | if (!(options & LYD_OPT_TRUSTED)) { |
| 670 | if (lyd_check_mandatory_tree((action ? action : result), ctx, options)) { |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 671 | goto error; |
| 672 | } |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 675 | free(unres->node); |
| 676 | free(unres->type); |
| 677 | free(unres); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 678 | va_end(ap); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 679 | |
| 680 | return result; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 681 | |
| 682 | error: |
| 683 | lyd_free_withsiblings(result); |
| 684 | free(unres->node); |
| 685 | free(unres->type); |
| 686 | free(unres); |
| 687 | va_end(ap); |
| 688 | |
| 689 | return NULL; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 690 | } |