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