Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 45a0ef3 | 2016-09-22 11:07:38 +0200 | [diff] [blame] | 2 | * @file parser_xml.c |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML data parser for libyang |
| 5 | * |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2017 CESNET, z.s.p.o. |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 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 | 1a6303d | 2016-11-04 12:42:46 +0100 | [diff] [blame] | 63 | if (ly_strequal(lys_main_module(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 |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 78 | xml_get_value(struct lyd_node *node, struct lyxml_elem *xml, int editbits) |
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; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 81 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 82 | assert(node && (node->schema->nodetype & (LYS_LEAFLIST | LYS_LEAF)) && xml); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 83 | |
Michal Vasko | d27602c | 2016-12-20 11:11:17 +0100 | [diff] [blame] | 84 | leaf->value_str = lydict_insert(node->schema->module->ctx, xml->content, 0); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 85 | |
Radek Krejci | 06b4527 | 2017-02-24 16:15:02 +0100 | [diff] [blame] | 86 | if ((editbits & 0x20) && (node->schema->nodetype & LYS_LEAF) && (!leaf->value_str || !leaf->value_str[0])) { |
Radek Krejci | 1fe9ac0 | 2016-09-23 09:38:21 +0200 | [diff] [blame] | 87 | /* we have edit-config leaf/leaf-list with delete operation and no (empty) value, |
| 88 | * this is not a bug since the node is just used as a kind of selection node */ |
| 89 | leaf->value_type = LY_TYPE_ERR; |
| 90 | return EXIT_SUCCESS; |
| 91 | } |
| 92 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 93 | /* the value is here converted to a JSON format if needed in case of LY_TYPE_IDENT and LY_TYPE_INST or to a |
| 94 | * canonical form of the value */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 95 | if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, xml, leaf, NULL, 1, 0)) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 96 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return EXIT_SUCCESS; |
| 100 | } |
| 101 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 102 | /* logs directly */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 103 | static int |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 104 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, struct lyd_node *parent, struct lyd_node *first_sibling, |
| 105 | struct lyd_node *prev, int options, struct unres_data *unres, struct lyd_node **result, |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 106 | struct lyd_node **act_notif) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 107 | { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 108 | const struct lys_module *mod = NULL; |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 109 | struct lyd_node *diter, *dlast; |
Michal Vasko | 5e523b6 | 2016-08-26 16:23:15 +0200 | [diff] [blame] | 110 | struct lys_node *schema = NULL, *target; |
| 111 | struct lys_node_augment *aug; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 112 | struct lyd_attr *dattr, *dattr_iter; |
Radek Krejci | a5241e5 | 2015-08-19 15:09:31 +0200 | [diff] [blame] | 113 | struct lyxml_attr *attr; |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 114 | struct lyxml_elem *child, *next; |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 115 | int i, j, havechildren, r, editbits = 0, pos, filterflag = 0, found; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 116 | int ret = 0; |
Radek Krejci | abb7b58 | 2016-04-20 16:15:47 +0200 | [diff] [blame] | 117 | const char *str = NULL; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 118 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 119 | assert(xml); |
| 120 | assert(result); |
| 121 | *result = NULL; |
| 122 | |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 123 | if (xml->flags & LYXML_ELEM_MIXED) { |
| 124 | if (options & LYD_OPT_STRICT) { |
| 125 | LOGVAL(LYE_XML_INVAL, LY_VLOG_XML, xml, "XML element with mixed content"); |
| 126 | return -1; |
| 127 | } else { |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 132 | if (!xml->ns || !xml->ns->value) { |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 133 | if (options & LYD_OPT_STRICT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 134 | LOGVAL(LYE_XML_MISS, LY_VLOG_XML, xml, "element's", "namespace"); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 135 | return -1; |
| 136 | } else { |
| 137 | return 0; |
| 138 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* find schema node */ |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 142 | if (!parent) { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 143 | mod = ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL); |
| 144 | if (ctx->data_clb) { |
| 145 | if (!mod) { |
| 146 | mod = ctx->data_clb(ctx, NULL, xml->ns->value, 0, ctx->data_clb_data); |
| 147 | } else if (!mod->implemented) { |
| 148 | mod = ctx->data_clb(ctx, mod->name, mod->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 149 | } |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* get the proper schema node */ |
| 153 | if (mod && mod->implemented && !mod->disabled) { |
| 154 | schema = xml_data_search_schemanode(xml, mod->data, options); |
| 155 | if (!schema) { |
| 156 | /* it still can be the specific case of this module containing an augment of another module |
| 157 | * top-level choice or top-level choice's case, bleh */ |
| 158 | for (j = 0; j < mod->augment_size; ++j) { |
| 159 | aug = &mod->augment[j]; |
| 160 | target = aug->target; |
| 161 | if (target->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 162 | /* 1) okay, the target is choice or case */ |
| 163 | while (target && (target->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES))) { |
| 164 | target = lys_parent(target); |
| 165 | } |
| 166 | /* 2) now, the data node will be top-level, there are only non-data schema nodes */ |
| 167 | if (!target) { |
| 168 | while ((schema = (struct lys_node *)lys_getnext(schema, (struct lys_node *)aug, NULL, 0))) { |
| 169 | /* 3) alright, even the name matches, we found our schema node */ |
| 170 | if (ly_strequal(schema->name, xml->name, 1)) { |
| 171 | break; |
Michal Vasko | 5e523b6 | 2016-08-26 16:23:15 +0200 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 175 | } |
Michal Vasko | 5e523b6 | 2016-08-26 16:23:15 +0200 | [diff] [blame] | 176 | |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 177 | if (schema) { |
| 178 | break; |
Michal Vasko | 5e523b6 | 2016-08-26 16:23:15 +0200 | [diff] [blame] | 179 | } |
| 180 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 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); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 186 | |
| 187 | if (schema) { |
| 188 | if (!lys_node_module(schema)->implemented && ctx->data_clb) { |
Michal Vasko | ad43c18 | 2017-01-23 09:55:28 +0100 | [diff] [blame] | 189 | ctx->data_clb(ctx, lys_node_module(schema)->name, lys_node_module(schema)->ns, |
| 190 | LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 191 | } |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 192 | } |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | mod = lys_node_module(schema); |
| 196 | if (!mod || !mod->implemented || mod->disabled) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 197 | if (options & LYD_OPT_STRICT) { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 198 | LOGVAL(LYE_INELEM, (parent ? LY_VLOG_LYD : LY_VLOG_NONE), parent, xml->name); |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 199 | return -1; |
| 200 | } else { |
| 201 | return 0; |
| 202 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 205 | /* create the element structure */ |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 206 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 207 | case LYS_CONTAINER: |
Radek Krejci | 27aaa73 | 2015-09-04 15:24:04 +0200 | [diff] [blame] | 208 | case LYS_LIST: |
Michal Vasko | 2f30e57 | 2015-10-01 16:00:38 +0200 | [diff] [blame] | 209 | case LYS_NOTIF: |
| 210 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 211 | case LYS_ACTION: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 212 | *result = calloc(1, sizeof **result); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 213 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 214 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 215 | case LYS_LEAF: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 216 | case LYS_LEAFLIST: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 217 | *result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 218 | havechildren = 0; |
| 219 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 220 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 221 | case LYS_ANYDATA: |
| 222 | *result = calloc(1, sizeof(struct lyd_node_anydata)); |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 223 | havechildren = 0; |
| 224 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 225 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 226 | LOGINT; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 227 | return -1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 228 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 229 | if (!(*result)) { |
| 230 | LOGMEM; |
| 231 | return -1; |
| 232 | } |
| 233 | |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 234 | (*result)->prev = *result; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 235 | (*result)->schema = schema; |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 236 | (*result)->parent = parent; |
| 237 | diter = NULL; |
| 238 | if (parent && parent->child && schema->nodetype == LYS_LEAF && parent->schema->nodetype == LYS_LIST && |
| 239 | (pos = lys_is_key((struct lys_node_list *)parent->schema, (struct lys_node_leaf *)schema))) { |
| 240 | /* it is key and we need to insert it into a correct place */ |
| 241 | for (i = 0, diter = parent->child; |
| 242 | diter && i < (pos - 1) && diter->schema->nodetype == LYS_LEAF && |
| 243 | lys_is_key((struct lys_node_list *)parent->schema, (struct lys_node_leaf *)diter->schema); |
| 244 | i++, diter = diter->next); |
| 245 | if (diter) { |
| 246 | /* out of order insertion - insert list's key to the correct position, before the diter */ |
| 247 | if (options & LYD_OPT_STRICT) { |
| 248 | LOGVAL(LYE_INORDER, LY_VLOG_LYD, *result, schema->name, diter->schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 249 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Invalid position of the key \"%s\" in a list \"%s\".", |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 250 | schema->name, parent->schema->name); |
| 251 | free(*result); |
| 252 | *result = NULL; |
| 253 | return -1; |
| 254 | } else { |
| 255 | LOGWRN("Invalid position of the key \"%s\" in a list \"%s\".", schema->name, parent->schema->name) |
| 256 | } |
| 257 | if (parent->child == diter) { |
| 258 | parent->child = *result; |
| 259 | /* update first_sibling */ |
| 260 | first_sibling = *result; |
| 261 | } |
| 262 | if (diter->prev->next) { |
| 263 | diter->prev->next = *result; |
| 264 | } |
| 265 | (*result)->prev = diter->prev; |
| 266 | diter->prev = *result; |
| 267 | (*result)->next = diter; |
| 268 | } |
| 269 | } |
| 270 | if (!diter) { |
| 271 | /* simplified (faster) insert as the last node */ |
| 272 | if (parent && !parent->child) { |
| 273 | parent->child = *result; |
| 274 | } |
| 275 | if (prev) { |
| 276 | (*result)->prev = prev; |
| 277 | prev->next = *result; |
| 278 | |
| 279 | /* fix the "last" pointer */ |
| 280 | first_sibling->prev = *result; |
| 281 | } else { |
| 282 | (*result)->prev = *result; |
| 283 | first_sibling = *result; |
| 284 | } |
| 285 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 286 | (*result)->validity = ly_new_node_validity((*result)->schema); |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 287 | if (resolve_applies_when(schema, 0, NULL)) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 288 | (*result)->when_status = LYD_WHEN; |
| 289 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 290 | |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 291 | /* process attributes */ |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 292 | for (attr = xml->attr; attr; attr = attr->next) { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 293 | if (attr->type != LYXML_ATTR_STD) { |
| 294 | continue; |
| 295 | } else if (!attr->ns) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 296 | if ((*result)->schema->nodetype == LYS_ANYXML && |
| 297 | ly_strequal((*result)->schema->name, "filter", 0) && |
Michal Vasko | aa98eb7 | 2017-03-24 11:23:34 +0100 | [diff] [blame] | 298 | (ly_strequal((*result)->schema->module->name, "ietf-netconf", 0) || |
| 299 | ly_strequal((*result)->schema->module->name, "notifications", 0))) { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 300 | /* NETCONF filter's attributes, which we implement as non-standard annotations, |
| 301 | * they are unqualified (no namespace), but we know that we have internally defined |
Michal Vasko | aa98eb7 | 2017-03-24 11:23:34 +0100 | [diff] [blame] | 302 | * them in the ietf-netconf module */ |
| 303 | str = "urn:ietf:params:xml:ns:netconf:base:1.0"; |
Radek Krejci | 598dbcb | 2017-02-24 15:14:15 +0100 | [diff] [blame] | 304 | filterflag = 1; |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 305 | } else { |
Radek Krejci | a68ddeb | 2017-02-24 12:49:44 +0100 | [diff] [blame] | 306 | /* garbage */ |
| 307 | goto attr_error; |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 308 | } |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 309 | } else { |
| 310 | str = attr->ns->value; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 313 | r = lyp_fill_attr(ctx, *result, str, NULL, attr->name, attr->value, xml, &dattr); |
| 314 | if (r == -1) { |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 315 | goto error; |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 316 | } else if (r == 1) { |
| 317 | attr_error: |
| 318 | if (options & LYD_OPT_STRICT) { |
| 319 | LOGVAL(LYE_INMETA, LY_VLOG_LYD, *result, (attr->ns ? attr->ns->prefix : "<none>"), attr->name, attr->value); |
| 320 | goto error; |
| 321 | } |
| 322 | |
| 323 | LOGWRN("Unknown \"%s:%s\" metadata with value \"%s\", ignoring.", |
| 324 | (attr->ns ? attr->ns->prefix : "<none>"), attr->name, attr->value); |
| 325 | continue; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 326 | } |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 327 | |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 328 | /* special case of xpath in the value, we want to convert it to JSON */ |
| 329 | if (filterflag && !strcmp(attr->name, "select")) { |
Radek Krejci | 598dbcb | 2017-02-24 15:14:15 +0100 | [diff] [blame] | 330 | dattr->value.string = transform_xml2json(ctx, dattr->value_str, xml, 0, 1); |
| 331 | if (!dattr->value.string) { |
| 332 | /* problem with resolving value as xpath */ |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 333 | dattr->value.string = dattr->value_str; |
Radek Krejci | 598dbcb | 2017-02-24 15:14:15 +0100 | [diff] [blame] | 334 | goto error; |
| 335 | } |
| 336 | lydict_remove(ctx, dattr->value_str); |
| 337 | dattr->value_str = dattr->value.string; |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 340 | /* insert into the data node */ |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 341 | if (!(*result)->attr) { |
| 342 | (*result)->attr = dattr; |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 343 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 344 | 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] | 345 | dattr_iter->next = dattr; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 346 | } |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 347 | continue; |
Radek Krejci | 998a750 | 2015-10-26 15:54:33 +0100 | [diff] [blame] | 348 | } |
Michal Vasko | 4ff7b07 | 2015-08-21 09:05:03 +0200 | [diff] [blame] | 349 | |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 350 | /* check insert attribute and its values */ |
| 351 | if (options & LYD_OPT_EDIT) { |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 352 | if (lyp_check_edit_attr(ctx, (*result)->attr, *result, &editbits)) { |
| 353 | goto error; |
| 354 | } |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 355 | |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 356 | /* check correct filter extension attributes */ |
| 357 | } else if (filterflag) { |
| 358 | found = 0; /* 0 - nothing, 1 - type subtree, 2 - type xpath, 3 - select, 4 - type xpath + select */ |
| 359 | LY_TREE_FOR((*result)->attr, dattr_iter) { |
| 360 | if (!strcmp(dattr_iter->name, "type")) { |
| 361 | if ((found == 1) || (found == 2) || (found == 4)) { |
| 362 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "type", xml->name); |
Radek Krejci | b3d905d | 2017-02-24 16:15:44 +0100 | [diff] [blame] | 363 | goto error; |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 364 | } |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 365 | switch (dattr_iter->value.enm->value) { |
| 366 | case 0: |
| 367 | /* subtree */ |
| 368 | if (found == 3) { |
| 369 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), dattr_iter->name); |
| 370 | goto error; |
| 371 | } |
Radek Krejci | 06b4527 | 2017-02-24 16:15:02 +0100 | [diff] [blame] | 372 | |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 373 | assert(!found); |
| 374 | found = 1; |
| 375 | break; |
| 376 | case 1: |
| 377 | /* xpath */ |
| 378 | if (found == 3) { |
| 379 | found = 4; |
| 380 | } else { |
| 381 | assert(!found); |
| 382 | found = 2; |
| 383 | } |
| 384 | break; |
| 385 | default: |
| 386 | LOGINT; |
Radek Krejci | b3d905d | 2017-02-24 16:15:44 +0100 | [diff] [blame] | 387 | goto error; |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 388 | } |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 389 | } else if (!strcmp(dattr_iter->name, "select")) { |
| 390 | switch (found) { |
| 391 | case 0: |
| 392 | found = 3; |
| 393 | break; |
| 394 | case 1: |
| 395 | LOGVAL(LYE_INATTR, LY_VLOG_LYD, (*result), dattr_iter->name); |
Radek Krejci | b3d905d | 2017-02-24 16:15:44 +0100 | [diff] [blame] | 396 | goto error; |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 397 | case 2: |
| 398 | found = 4; |
| 399 | break; |
| 400 | case 3: |
| 401 | case 4: |
| 402 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, (*result), "select", xml->name); |
| 403 | goto error; |
| 404 | default: |
| 405 | LOGINT; |
Radek Krejci | b3d905d | 2017-02-24 16:15:44 +0100 | [diff] [blame] | 406 | goto error; |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 407 | } |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 411 | /* check if what we found is correct */ |
| 412 | switch (found) { |
| 413 | case 1: |
| 414 | case 4: |
| 415 | /* ok */ |
| 416 | break; |
| 417 | case 2: |
| 418 | LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "select", xml->name); |
Radek Krejci | b3d905d | 2017-02-24 16:15:44 +0100 | [diff] [blame] | 419 | goto error; |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 420 | case 3: |
| 421 | LOGVAL(LYE_MISSATTR, LY_VLOG_LYD, (*result), "type", xml->name); |
Radek Krejci | b3d905d | 2017-02-24 16:15:44 +0100 | [diff] [blame] | 422 | goto error; |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 423 | default: |
| 424 | LOGINT; |
Radek Krejci | b3d905d | 2017-02-24 16:15:44 +0100 | [diff] [blame] | 425 | goto error; |
Radek Krejci | 146e3fc | 2017-02-24 13:41:00 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | /* type specific processing */ |
| 430 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 431 | /* type detection and assigning the value */ |
| 432 | if (xml_get_value(*result, xml, editbits)) { |
| 433 | goto error; |
| 434 | } |
| 435 | } else if (schema->nodetype & LYS_ANYDATA) { |
| 436 | /* store children values */ |
| 437 | if (xml->child) { |
| 438 | child = xml->child; |
| 439 | /* manually unlink all siblings and correct namespaces */ |
| 440 | xml->child = NULL; |
| 441 | LY_TREE_FOR(child, next) { |
| 442 | next->parent = NULL; |
| 443 | lyxml_correct_elem_ns(ctx, next, 1, 1); |
| 444 | } |
| 445 | |
| 446 | ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_XML; |
| 447 | ((struct lyd_node_anydata *)*result)->value.xml = child; |
| 448 | } else { |
| 449 | ((struct lyd_node_anydata *)*result)->value_type = LYD_ANYDATA_CONSTSTRING; |
| 450 | ((struct lyd_node_anydata *)*result)->value.str = lydict_insert(ctx, xml->content, 0); |
| 451 | } |
| 452 | } else if (schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 453 | if (!(options & LYD_OPT_RPC) || *act_notif) { |
| 454 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*result), schema->name); |
| 455 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected %s node \"%s\".", |
| 456 | (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name); |
| 457 | goto error; |
| 458 | } |
| 459 | *act_notif = *result; |
| 460 | } else if (schema->nodetype == LYS_NOTIF) { |
| 461 | if (!(options & LYD_OPT_NOTIF) || *act_notif) { |
| 462 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*result), schema->name); |
| 463 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected notification node \"%s\".", schema->name); |
| 464 | goto error; |
| 465 | } |
| 466 | *act_notif = *result; |
| 467 | } |
| 468 | |
| 469 | /* first part of validation checks */ |
| 470 | if (lyv_data_context(*result, options, unres)) { |
| 471 | goto error; |
| 472 | } |
| 473 | |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 474 | /* process children */ |
| 475 | if (havechildren && xml->child) { |
| 476 | diter = dlast = NULL; |
| 477 | LY_TREE_FOR_SAFE(xml->child, next, child) { |
Michal Vasko | a51c200 | 2017-03-01 10:14:06 +0100 | [diff] [blame] | 478 | r = xml_parse_data(ctx, child, *result, (*result)->child, dlast, options, unres, &diter, act_notif); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 479 | if (r) { |
| 480 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 481 | } else if (options & LYD_OPT_DESTRUCT) { |
| 482 | lyxml_free(ctx, child); |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 483 | } |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 484 | if (diter && !diter->next) { |
| 485 | /* the child was parsed/created and it was placed as the last child. The child can be inserted |
| 486 | * out of order (not as the last one) in case it is a list's key present out of the correct order */ |
Radek Krejci | 14c0009 | 2015-11-01 11:03:24 +0100 | [diff] [blame] | 487 | dlast = diter; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 492 | /* if we have empty non-presence container, we keep it, but mark it as default */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 493 | if (schema->nodetype == LYS_CONTAINER && !(*result)->child && |
Radek Krejci | d3e7372 | 2016-05-23 12:24:55 +0200 | [diff] [blame] | 494 | !(*result)->attr && !((struct lys_node_container *)schema)->presence) { |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 495 | (*result)->dflt = 1; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 498 | /* rest of validation checks */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 499 | ly_err_clean(1); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 500 | if (lyv_data_content(*result, options, unres) || |
| 501 | lyv_multicases(*result, NULL, prev ? &first_sibling : NULL, 0, NULL)) { |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 502 | if (ly_errno) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 503 | goto error; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 504 | } else { |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 505 | goto clear; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 506 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 509 | /* validation successful */ |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 510 | if ((*result)->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 511 | /* postpone checking when there will be all list/leaflist instances */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 512 | (*result)->validity |= LYD_VAL_UNIQUE; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 513 | } |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 514 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 515 | return ret; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 516 | |
| 517 | error: |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 518 | ret--; |
| 519 | |
| 520 | clear: |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 521 | /* cleanup */ |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 522 | for (i = unres->count - 1; i >= 0; i--) { |
| 523 | /* remove unres items connected with the node being removed */ |
| 524 | if (unres->node[i] == *result) { |
| 525 | unres_data_del(unres, i); |
| 526 | } |
| 527 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 528 | lyd_free(*result); |
| 529 | *result = NULL; |
Radek Krejci | 1b0d01a | 2015-08-19 17:00:35 +0200 | [diff] [blame] | 530 | |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 531 | return ret; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 534 | API struct lyd_node * |
| 535 | 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] | 536 | { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 537 | va_list ap; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 538 | int r, i; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 539 | struct unres_data *unres = NULL; |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 540 | const struct lyd_node *rpc_act = NULL, *data_tree = NULL; |
| 541 | struct lyd_node *result = NULL, *iter, *last, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL; |
Michal Vasko | a875656 | 2017-05-03 14:42:23 +0200 | [diff] [blame^] | 542 | struct lyxml_elem *xmlstart, *xmlelem, *xmlaux, *xmlfree = NULL; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 543 | struct ly_set *set; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 544 | |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 545 | ly_err_clean(1); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 546 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 547 | if (!ctx || !root) { |
| 548 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 549 | return NULL; |
| 550 | } |
| 551 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 552 | if (lyp_check_options(options)) { |
| 553 | LOGERR(LY_EINVAL, "%s: Invalid options (multiple data type flags set).", __func__); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
Radek Krejci | a6939c3 | 2016-03-24 15:19:09 +0100 | [diff] [blame] | 557 | if (!(*root)) { |
| 558 | /* empty tree - no work is needed */ |
| 559 | lyd_validate(&result, options, ctx); |
| 560 | return result; |
| 561 | } |
| 562 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 563 | unres = calloc(1, sizeof *unres); |
| 564 | if (!unres) { |
| 565 | LOGMEM; |
| 566 | return NULL; |
| 567 | } |
| 568 | |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 569 | va_start(ap, options); |
| 570 | if (options & LYD_OPT_RPCREPLY) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 571 | rpc_act = va_arg(ap, const struct lyd_node *); |
| 572 | if (!rpc_act || rpc_act->parent || !(rpc_act->schema->nodetype & (LYS_RPC | LYS_LIST | LYS_CONTAINER))) { |
| 573 | LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 574 | goto error; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 575 | } |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 576 | if (rpc_act->schema->nodetype == LYS_RPC) { |
| 577 | /* RPC request */ |
| 578 | reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0); |
| 579 | } else { |
| 580 | /* action request */ |
| 581 | reply_top = lyd_dup(rpc_act, 1); |
| 582 | LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) { |
| 583 | if (reply_parent->schema->nodetype == LYS_ACTION) { |
| 584 | break; |
| 585 | } |
| 586 | LY_TREE_DFS_END(reply_top, iter, reply_parent); |
| 587 | } |
| 588 | if (!reply_parent) { |
| 589 | LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__); |
| 590 | lyd_free_withsiblings(reply_top); |
| 591 | goto error; |
| 592 | } |
| 593 | lyd_free_withsiblings(reply_parent->child); |
| 594 | } |
Michal Vasko | 45e2365 | 2016-09-21 11:24:32 +0200 | [diff] [blame] | 595 | } |
| 596 | if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF | LYD_OPT_RPCREPLY)) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 597 | data_tree = va_arg(ap, const struct lyd_node *); |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 598 | if (data_tree) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 599 | LY_TREE_FOR((struct lyd_node *)data_tree, iter) { |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 600 | if (iter->parent) { |
| 601 | /* a sibling is not top-level */ |
| 602 | LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__); |
| 603 | goto error; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /* move it to the beginning */ |
| 608 | for (; data_tree->prev->next; data_tree = data_tree->prev); |
| 609 | |
| 610 | /* LYD_OPT_NOSIBLINGS cannot be set in this case */ |
| 611 | if (options & LYD_OPT_NOSIBLINGS) { |
| 612 | LOGERR(LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__); |
| 613 | goto error; |
| 614 | } |
| 615 | } |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 618 | if (!(options & LYD_OPT_NOSIBLINGS)) { |
| 619 | /* locate the first root to process */ |
| 620 | if ((*root)->parent) { |
| 621 | xmlstart = (*root)->parent->child; |
| 622 | } else { |
| 623 | xmlstart = *root; |
| 624 | while(xmlstart->prev->next) { |
| 625 | xmlstart = xmlstart->prev; |
| 626 | } |
Radek Krejci | 04b97de | 2015-10-31 23:09:15 +0100 | [diff] [blame] | 627 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 628 | } else { |
| 629 | xmlstart = *root; |
| 630 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 631 | |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 632 | if ((options & LYD_OPT_RPC) |
| 633 | && !strcmp(xmlstart->name, "action") && !strcmp(xmlstart->ns->value, "urn:ietf:params:xml:ns:yang:1")) { |
| 634 | /* it's an action, not a simple RPC */ |
| 635 | xmlstart = xmlstart->child; |
Michal Vasko | a875656 | 2017-05-03 14:42:23 +0200 | [diff] [blame^] | 636 | if (options & LYD_OPT_DESTRUCT) { |
| 637 | /* free it later */ |
| 638 | xmlfree = xmlstart->parent; |
| 639 | } |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | iter = last = NULL; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 643 | LY_TREE_FOR_SAFE(xmlstart, xmlaux, xmlelem) { |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 644 | r = xml_parse_data(ctx, xmlelem, reply_parent, result, last, options, unres, &iter, &act_notif); |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 645 | if (r) { |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 646 | if (reply_top) { |
| 647 | result = reply_top; |
Pavol Vican | 8a552f6 | 2016-09-05 11:20:57 +0200 | [diff] [blame] | 648 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 649 | goto error; |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 650 | } else if (options & LYD_OPT_DESTRUCT) { |
| 651 | lyxml_free(ctx, xmlelem); |
| 652 | *root = xmlaux; |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 653 | } |
| 654 | if (iter) { |
| 655 | last = iter; |
| 656 | } |
| 657 | if (!result) { |
| 658 | result = iter; |
| 659 | } |
Radek Krejci | 8653821 | 2015-12-17 15:59:01 +0100 | [diff] [blame] | 660 | |
| 661 | if (options & LYD_OPT_NOSIBLINGS) { |
| 662 | /* stop after the first processed root */ |
| 663 | break; |
| 664 | } |
Radek Krejci | 3b41a6c | 2015-10-31 23:06:12 +0100 | [diff] [blame] | 665 | } |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 666 | |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 667 | if (reply_top) { |
| 668 | result = reply_top; |
Michal Vasko | e45aff5 | 2016-08-25 09:01:09 +0200 | [diff] [blame] | 669 | } |
| 670 | |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 671 | if ((options & LYD_OPT_RPCREPLY) && (rpc_act->schema->nodetype != LYS_RPC)) { |
| 672 | /* action reply */ |
| 673 | act_notif = reply_parent; |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 674 | } else if ((options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) && !act_notif) { |
| 675 | ly_vecode = LYVE_INELEM; |
| 676 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing %s node.", (options & LYD_OPT_RPC ? "action" : "notification")); |
| 677 | goto error; |
Radek Krejci | b45b308 | 2016-09-09 16:08:51 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 680 | /* check for uniquness of top-level lists/leaflists because |
| 681 | * only the inner instances were tested in lyv_data_content() */ |
| 682 | set = ly_set_new(); |
| 683 | LY_TREE_FOR(result, iter) { |
| 684 | if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) { |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | /* check each list/leaflist only once */ |
| 689 | i = set->number; |
| 690 | if (ly_set_add(set, iter->schema, 0) != i) { |
| 691 | /* already checked */ |
| 692 | continue; |
| 693 | } |
| 694 | |
| 695 | if (lyv_data_unique(iter, result)) { |
| 696 | ly_set_free(set); |
| 697 | goto error; |
| 698 | } |
| 699 | } |
| 700 | ly_set_free(set); |
| 701 | |
Radek Krejci | b45b308 | 2016-09-09 16:08:51 +0200 | [diff] [blame] | 702 | /* add default values, resolve unres and check for mandatory nodes in final tree */ |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 703 | if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) { |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 704 | goto error; |
| 705 | } |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 706 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER)) |
| 707 | && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 708 | goto error; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Michal Vasko | a875656 | 2017-05-03 14:42:23 +0200 | [diff] [blame^] | 711 | if (xmlfree) { |
| 712 | lyxml_free(ctx, xmlfree); |
| 713 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 714 | free(unres->node); |
| 715 | free(unres->type); |
| 716 | free(unres); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 717 | va_end(ap); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 718 | |
| 719 | return result; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 720 | |
| 721 | error: |
| 722 | lyd_free_withsiblings(result); |
Michal Vasko | a875656 | 2017-05-03 14:42:23 +0200 | [diff] [blame^] | 723 | if (xmlfree) { |
| 724 | lyxml_free(ctx, xmlfree); |
| 725 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 726 | free(unres->node); |
| 727 | free(unres->type); |
| 728 | free(unres); |
| 729 | va_end(ap); |
| 730 | |
| 731 | return NULL; |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 732 | } |