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