blob: e94e4f550f12977adf573aa182e380557f3f569d [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief XML data parser for libyang
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
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
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <stdint.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include "context.h"
22#include "dict.h"
23#include "log.h"
24#include "plugins_types.h"
25#include "set.h"
26#include "tree_data.h"
27#include "tree_data_internal.h"
28#include "tree_schema.h"
29#include "xml.h"
30
31/**
32 * @brief internal context for XML YANG data parser.
33 *
34 * The leading part is compatible with the struct lyxml_context
35 */
36struct lyd_xml_ctx {
37 struct ly_ctx *ctx; /**< libyang context */
38 uint64_t line; /**< number of the line being currently processed */
39 enum LYXML_PARSER_STATUS status; /**< status providing information about the next expected object in input data */
40 struct ly_set elements; /**< list of not-yet-closed elements */
41 struct ly_set ns; /**< handled with LY_SET_OPT_USEASLIST */
42
43 uint16_t options; /**< various @ref dataparseroptions. */
44 uint16_t path_len; /**< used bytes in the path buffer */
45#define LYD_PARSER_BUFSIZE 4078
46 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Radek Krejcie553e6d2019-06-07 15:33:18 +020047 struct ly_set incomplete_type_validation; /**< set of nodes validated with LY_EINCOMPLETE result */
Radek Krejcie7b95092019-05-15 11:03:07 +020048};
49
50/**
Radek Krejciaca74032019-06-04 08:53:06 +020051 * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used in the values to the schema
52 * via XML namespaces.
53 */
54static const struct lys_module *
55lydxml_resolve_prefix(struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser)
56{
57 const struct lyxml_ns *ns;
58 struct lyxml_context *xmlctx = (struct lyxml_context*)parser;
59
60 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
61 if (!ns) {
62 return NULL;
63 }
64
65 return ly_ctx_get_module_implemented_ns(ctx, ns->uri);
66}
67
68/**
Radek Krejcie7b95092019-05-15 11:03:07 +020069 * @brief Parse XML attributes of the XML element of YANG data.
70 *
71 * @param[in] ctx XML YANG data parser context.
Radek Krejcie7b95092019-05-15 11:03:07 +020072 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
73 * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed
74 * as attributes, they are stored internally in the parser context.
75 * @reutn LY_ERR value.
76 */
77static LY_ERR
Radek Krejci17a78d82019-05-15 15:49:55 +020078lydxml_attributes(struct lyd_xml_ctx *ctx, const char **data, struct lyd_attr **attributes)
Radek Krejcie7b95092019-05-15 11:03:07 +020079{
80 LY_ERR ret = LY_SUCCESS;
81 unsigned int u;
82 const char *prefix, *name;
83 size_t prefix_len, name_len;
84 struct lyd_attr *attr = NULL, *last = NULL;
85 const struct lyxml_ns *ns;
86 struct ly_set attr_prefixes = {0};
87 struct attr_prefix_s {
88 const char *prefix;
89 size_t prefix_len;
90 } *attr_prefix;
91 struct lys_module *mod;
92
93 while(ctx->status == LYXML_ATTRIBUTE &&
94 lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) {
95 int dynamic = 0;
96 char *buffer = NULL, *value;
97 size_t buffer_size = 0, value_len;
98
Radek Krejci17a78d82019-05-15 15:49:55 +020099 if (!name) {
100 /* seems like all the attrributes were internally processed as namespace definitions */
101 continue;
Radek Krejcie7b95092019-05-15 11:03:07 +0200102 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200103
104 /* get attribute value */
105 ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
106 LY_CHECK_GOTO(ret, cleanup);
107
108 attr = calloc(1, sizeof *attr);
109 LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
110
111 attr->name = lydict_insert(ctx->ctx, name, name_len);
112 /* auxiliary store the prefix information and wait with resolving prefix to the time when all the namespaces,
113 * defined in this element, are parsed, so we will get the correct namespace for this prefix */
114 attr_prefix = malloc(sizeof *attr_prefix);
115 attr_prefix->prefix = prefix;
116 attr_prefix->prefix_len = prefix_len;
117 ly_set_add(&attr_prefixes, attr_prefix, LY_SET_OPT_USEASLIST);
118
119 /* TODO process value */
120
121 if (last) {
122 last->next = attr;
123 } else {
124 (*attributes) = attr;
125 }
126 last = attr;
Radek Krejcie7b95092019-05-15 11:03:07 +0200127 }
128
129 /* resolve annotation pointers in all the attributes */
130 for (last = *attributes, u = 0; u < attr_prefixes.count && last; u++, last = last->next) {
131 attr_prefix = (struct attr_prefix_s*)attr_prefixes.objs[u];
132 ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_prefix->prefix, attr_prefix->prefix_len);
133 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
134
135 /* TODO get annotation */
136 }
137
138cleanup:
139
140 ly_set_erase(&attr_prefixes, free);
141 return ret;
142}
143
144/**
145 * @brief Parse XML elements as children YANG data node of the specified parent node.
146 *
147 * @param[in] ctx XML YANG data parser context.
148 * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
149 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
150 * @param[out] node Resulting list of the parsed nodes.
151 * @reutn LY_ERR value.
152 */
153static LY_ERR
154lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node)
155{
156 LY_ERR ret = LY_SUCCESS;
157 const char *prefix, *name;
Radek Krejcie7b95092019-05-15 11:03:07 +0200158 size_t prefix_len, name_len;
159 struct lyd_attr *attributes = NULL;
160 const struct lyxml_ns *ns;
161 const struct lysc_node *snode;
162 struct lys_module *mod;
163 unsigned int parents_count = ctx->elements.count;
164 struct lyd_node *cur = NULL, *prev = NULL;
165
166 (*node) = NULL;
167
168 while(ctx->status == LYXML_ELEMENT) {
169 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len);
170 LY_CHECK_GOTO(ret, cleanup);
171 if (!name) {
172 /* closing previous element */
Radek Krejcie7b95092019-05-15 11:03:07 +0200173 if (ctx->elements.count < parents_count) {
174 /* all siblings parsed */
175 break;
176 } else {
177 continue;
178 }
179 }
180 attributes = NULL;
Radek Krejci17a78d82019-05-15 15:49:55 +0200181 LY_CHECK_GOTO(lydxml_attributes(ctx, data, &attributes), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200182 ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len);
183 if (!ns) {
184 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%*.s\".", prefix_len, prefix);
185 goto cleanup;
186 }
187 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
188 if (!mod) {
189 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
190 goto cleanup;
191 }
192 snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0);
193 if (!snode) {
194 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name);
195 goto cleanup;
196 }
197
198 /* allocate new node */
199 switch (snode->nodetype) {
200 case LYS_CONTAINER:
201 case LYS_LIST:
202 cur = calloc(1, sizeof(struct lyd_node_inner));
203 break;
204 case LYS_LEAF:
205 case LYS_LEAFLIST:
206 cur = calloc(1, sizeof(struct lyd_node_term));
207 break;
208 case LYS_ANYDATA:
209 case LYS_ANYXML:
210 cur = calloc(1, sizeof(struct lyd_node_any));
211 break;
212 /* TODO LYS_ACTION, LYS_NOTIF */
213 default:
214 LOGINT(ctx->ctx);
215 goto cleanup;
216 }
217 if (!(*node)) {
218 (*node) = cur;
219 }
220 cur->schema = snode;
221 cur->parent = parent;
Radek Krejcie92210c2019-05-17 15:53:35 +0200222 if (parent) {
223 parent->child->prev = cur;
224 } else if (prev) {
225 struct lyd_node *iter;
226 for (iter = prev; iter->prev->next; iter = iter->prev);
227 iter->prev = cur;
228 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200229 if (prev) {
230 cur->prev = prev;
231 prev->next = cur;
232 } else {
233 cur->prev = cur;
234 }
235 prev = cur;
236 cur->attr = attributes;
237 attributes = NULL;
238
239 if (snode->nodetype & LYD_NODE_TERM) {
240 int dynamic = 0;
241 char *buffer = NULL, *value;
242 size_t buffer_size = 0, value_len;
243
244 if (ctx->status == LYXML_ELEM_CONTENT) {
245 /* get the value */
Radek Krejci339e2de2019-05-17 14:28:24 +0200246 LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
247 if (r == LY_EINVAL) {
248 /* just indentation of a child element found */
249 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.", cur->schema->name);
250 goto cleanup;
251 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200252 } else {
253 /* no content - validate empty value */
254 value = "";
255 value_len = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200256 }
Radek Krejci3c9758d2019-07-11 16:49:10 +0200257 ret = lyd_value_parse((struct lyd_node_term*)cur, value, value_len, dynamic, 0, lydxml_resolve_prefix, ctx, LYD_XML, NULL);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200258 if (ret == LY_EINCOMPLETE) {
259 ly_set_add(&ctx->incomplete_type_validation, cur, LY_SET_OPT_USEASLIST);
260 } else if (ret) {
261 if (dynamic){
262 free(value);
263 }
264 goto cleanup;
265 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200266 } else if (snode->nodetype & LYD_NODE_INNER) {
267 int dynamic = 0;
268 char *buffer = NULL, *value;
269 size_t buffer_size = 0, value_len;
270
271 if (ctx->status == LYXML_ELEM_CONTENT) {
272 LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200273 if (r != LY_EINVAL && (r != LY_SUCCESS || value_len != 0)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200274 LOGINT(ctx->ctx);
275 goto cleanup;
276 }
277 }
Radek Krejcie553e6d2019-06-07 15:33:18 +0200278 if (ctx->status == LYXML_ELEMENT && parents_count != ctx->elements.count) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200279 ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur));
Radek Krejcie92210c2019-05-17 15:53:35 +0200280 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200281 }
282 }
283 /* TODO anyxml/anydata */
284 }
285
286cleanup:
Radek Krejcie7b95092019-05-15 11:03:07 +0200287 lyd_free_attr(ctx->ctx, attributes, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200288 return ret;
289}
290
291LY_ERR
292lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result)
293{
294 LY_ERR ret;
295 struct lyd_xml_ctx xmlctx = {0};
296
297 xmlctx.options = options;
298 xmlctx.ctx = ctx;
299 xmlctx.line = 1;
300
301 ret = lydxml_nodes(&xmlctx, NULL, &data, result);
Radek Krejcie92210c2019-05-17 15:53:35 +0200302 if (ret) {
303 lyd_free_all(*result);
304 *result = NULL;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200305 } else {
306 /* finish incompletely validated terminal values */
307 for (unsigned int u = 0; u < xmlctx.incomplete_type_validation.count; u++) {
308 struct lyd_node_term *node = (struct lyd_node_term*)xmlctx.incomplete_type_validation.objs[u];
Radek Krejcie72c0432019-06-10 10:17:03 +0200309 struct lyd_node **trees = NULL;
310
311 /* prepare sized array for validator */
312 if (*result) {
313 struct lyd_node **tree;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200314 LY_ARRAY_NEW_RET(ctx, trees, tree, LY_EMEM);
315 *tree = *result;
316 }
Radek Krejcie72c0432019-06-10 10:17:03 +0200317 /* validate and store the value of the node */
Radek Krejci3c9758d2019-07-11 16:49:10 +0200318 ret = lyd_value_parse(node, node->value.canonized, node->value.canonized ? strlen(node->value.canonized) : 0, 0, 1,
Radek Krejci084289f2019-07-09 17:35:30 +0200319 lydxml_resolve_prefix, ctx, LYD_XML, trees);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200320 LY_ARRAY_FREE(trees);
321 if (ret) {
322 lyd_free_all(*result);
323 *result = NULL;
324 break;
325 }
326 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200327 }
Radek Krejcie553e6d2019-06-07 15:33:18 +0200328
329 ly_set_erase(&xmlctx.incomplete_type_validation, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200330 lyxml_context_clear((struct lyxml_context*)&xmlctx);
331 return ret;
332}