blob: cf85cf77e31cd70a17d9f219b80e66bb2f60c84e [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 */
47};
48
49/**
Radek Krejciaca74032019-06-04 08:53:06 +020050 * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used in the values to the schema
51 * via XML namespaces.
52 */
53static const struct lys_module *
54lydxml_resolve_prefix(struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser)
55{
56 const struct lyxml_ns *ns;
57 struct lyxml_context *xmlctx = (struct lyxml_context*)parser;
58
59 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
60 if (!ns) {
61 return NULL;
62 }
63
64 return ly_ctx_get_module_implemented_ns(ctx, ns->uri);
65}
66
67/**
Radek Krejcie7b95092019-05-15 11:03:07 +020068 * @brief Parse XML attributes of the XML element of YANG data.
69 *
70 * @param[in] ctx XML YANG data parser context.
Radek Krejcie7b95092019-05-15 11:03:07 +020071 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
72 * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed
73 * as attributes, they are stored internally in the parser context.
74 * @reutn LY_ERR value.
75 */
76static LY_ERR
Radek Krejci17a78d82019-05-15 15:49:55 +020077lydxml_attributes(struct lyd_xml_ctx *ctx, const char **data, struct lyd_attr **attributes)
Radek Krejcie7b95092019-05-15 11:03:07 +020078{
79 LY_ERR ret = LY_SUCCESS;
80 unsigned int u;
81 const char *prefix, *name;
82 size_t prefix_len, name_len;
83 struct lyd_attr *attr = NULL, *last = NULL;
84 const struct lyxml_ns *ns;
85 struct ly_set attr_prefixes = {0};
86 struct attr_prefix_s {
87 const char *prefix;
88 size_t prefix_len;
89 } *attr_prefix;
90 struct lys_module *mod;
91
92 while(ctx->status == LYXML_ATTRIBUTE &&
93 lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) {
94 int dynamic = 0;
95 char *buffer = NULL, *value;
96 size_t buffer_size = 0, value_len;
97
Radek Krejci17a78d82019-05-15 15:49:55 +020098 if (!name) {
99 /* seems like all the attrributes were internally processed as namespace definitions */
100 continue;
Radek Krejcie7b95092019-05-15 11:03:07 +0200101 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200102
103 /* get attribute value */
104 ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
105 LY_CHECK_GOTO(ret, cleanup);
106
107 attr = calloc(1, sizeof *attr);
108 LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
109
110 attr->name = lydict_insert(ctx->ctx, name, name_len);
111 /* auxiliary store the prefix information and wait with resolving prefix to the time when all the namespaces,
112 * defined in this element, are parsed, so we will get the correct namespace for this prefix */
113 attr_prefix = malloc(sizeof *attr_prefix);
114 attr_prefix->prefix = prefix;
115 attr_prefix->prefix_len = prefix_len;
116 ly_set_add(&attr_prefixes, attr_prefix, LY_SET_OPT_USEASLIST);
117
118 /* TODO process value */
119
120 if (last) {
121 last->next = attr;
122 } else {
123 (*attributes) = attr;
124 }
125 last = attr;
Radek Krejcie7b95092019-05-15 11:03:07 +0200126 }
127
128 /* resolve annotation pointers in all the attributes */
129 for (last = *attributes, u = 0; u < attr_prefixes.count && last; u++, last = last->next) {
130 attr_prefix = (struct attr_prefix_s*)attr_prefixes.objs[u];
131 ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_prefix->prefix, attr_prefix->prefix_len);
132 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
133
134 /* TODO get annotation */
135 }
136
137cleanup:
138
139 ly_set_erase(&attr_prefixes, free);
140 return ret;
141}
142
143/**
144 * @brief Parse XML elements as children YANG data node of the specified parent node.
145 *
146 * @param[in] ctx XML YANG data parser context.
147 * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
148 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
149 * @param[out] node Resulting list of the parsed nodes.
150 * @reutn LY_ERR value.
151 */
152static LY_ERR
153lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node)
154{
155 LY_ERR ret = LY_SUCCESS;
156 const char *prefix, *name;
Radek Krejcie7b95092019-05-15 11:03:07 +0200157 size_t prefix_len, name_len;
158 struct lyd_attr *attributes = NULL;
159 const struct lyxml_ns *ns;
160 const struct lysc_node *snode;
161 struct lys_module *mod;
162 unsigned int parents_count = ctx->elements.count;
163 struct lyd_node *cur = NULL, *prev = NULL;
164
165 (*node) = NULL;
166
167 while(ctx->status == LYXML_ELEMENT) {
168 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len);
169 LY_CHECK_GOTO(ret, cleanup);
170 if (!name) {
171 /* closing previous element */
Radek Krejcie7b95092019-05-15 11:03:07 +0200172 if (ctx->elements.count < parents_count) {
173 /* all siblings parsed */
174 break;
175 } else {
176 continue;
177 }
178 }
179 attributes = NULL;
Radek Krejci17a78d82019-05-15 15:49:55 +0200180 LY_CHECK_GOTO(lydxml_attributes(ctx, data, &attributes), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200181 ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len);
182 if (!ns) {
183 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%*.s\".", prefix_len, prefix);
184 goto cleanup;
185 }
186 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
187 if (!mod) {
188 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
189 goto cleanup;
190 }
191 snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0);
192 if (!snode) {
193 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name);
194 goto cleanup;
195 }
196
197 /* allocate new node */
198 switch (snode->nodetype) {
199 case LYS_CONTAINER:
200 case LYS_LIST:
201 cur = calloc(1, sizeof(struct lyd_node_inner));
202 break;
203 case LYS_LEAF:
204 case LYS_LEAFLIST:
205 cur = calloc(1, sizeof(struct lyd_node_term));
206 break;
207 case LYS_ANYDATA:
208 case LYS_ANYXML:
209 cur = calloc(1, sizeof(struct lyd_node_any));
210 break;
211 /* TODO LYS_ACTION, LYS_NOTIF */
212 default:
213 LOGINT(ctx->ctx);
214 goto cleanup;
215 }
216 if (!(*node)) {
217 (*node) = cur;
218 }
219 cur->schema = snode;
220 cur->parent = parent;
Radek Krejcie92210c2019-05-17 15:53:35 +0200221 if (parent) {
222 parent->child->prev = cur;
223 } else if (prev) {
224 struct lyd_node *iter;
225 for (iter = prev; iter->prev->next; iter = iter->prev);
226 iter->prev = cur;
227 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200228 if (prev) {
229 cur->prev = prev;
230 prev->next = cur;
231 } else {
232 cur->prev = cur;
233 }
234 prev = cur;
235 cur->attr = attributes;
236 attributes = NULL;
237
238 if (snode->nodetype & LYD_NODE_TERM) {
239 int dynamic = 0;
240 char *buffer = NULL, *value;
241 size_t buffer_size = 0, value_len;
242
243 if (ctx->status == LYXML_ELEM_CONTENT) {
244 /* get the value */
Radek Krejci339e2de2019-05-17 14:28:24 +0200245 LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
246 if (r == LY_EINVAL) {
247 /* just indentation of a child element found */
248 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.", cur->schema->name);
249 goto cleanup;
250 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200251 } else {
252 /* no content - validate empty value */
253 value = "";
254 value_len = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200255 }
Radek Krejciaca74032019-06-04 08:53:06 +0200256 LY_CHECK_ERR_GOTO(ret = lyd_value_parse((struct lyd_node_term*)cur, value, value_len, dynamic, lydxml_resolve_prefix, ctx),
Radek Krejci5819f7c2019-05-31 14:53:29 +0200257 if (dynamic){free(value);}, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200258 } else if (snode->nodetype & LYD_NODE_INNER) {
259 int dynamic = 0;
260 char *buffer = NULL, *value;
261 size_t buffer_size = 0, value_len;
262
263 if (ctx->status == LYXML_ELEM_CONTENT) {
264 LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
265 if (r != LY_EINVAL) {
266 LOGINT(ctx->ctx);
267 goto cleanup;
268 }
269 }
270 if (ctx->status == LYXML_ELEMENT) {
271 ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur));
Radek Krejcie92210c2019-05-17 15:53:35 +0200272 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200273 }
274 }
275 /* TODO anyxml/anydata */
276 }
277
278cleanup:
Radek Krejcie7b95092019-05-15 11:03:07 +0200279 lyd_free_attr(ctx->ctx, attributes, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200280 return ret;
281}
282
283LY_ERR
284lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result)
285{
286 LY_ERR ret;
287 struct lyd_xml_ctx xmlctx = {0};
288
289 xmlctx.options = options;
290 xmlctx.ctx = ctx;
291 xmlctx.line = 1;
292
293 ret = lydxml_nodes(&xmlctx, NULL, &data, result);
Radek Krejcie92210c2019-05-17 15:53:35 +0200294 if (ret) {
295 lyd_free_all(*result);
296 *result = NULL;
297 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200298 lyxml_context_clear((struct lyxml_context*)&xmlctx);
299 return ret;
300}