blob: 389a8dbc8efd53f7b4456fe164563c678df1509b [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/**
50 * @brief Parse XML attributes of the XML element of YANG data.
51 *
52 * @param[in] ctx XML YANG data parser context.
Radek Krejcie7b95092019-05-15 11:03:07 +020053 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
54 * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed
55 * as attributes, they are stored internally in the parser context.
56 * @reutn LY_ERR value.
57 */
58static LY_ERR
Radek Krejci17a78d82019-05-15 15:49:55 +020059lydxml_attributes(struct lyd_xml_ctx *ctx, const char **data, struct lyd_attr **attributes)
Radek Krejcie7b95092019-05-15 11:03:07 +020060{
61 LY_ERR ret = LY_SUCCESS;
62 unsigned int u;
63 const char *prefix, *name;
64 size_t prefix_len, name_len;
65 struct lyd_attr *attr = NULL, *last = NULL;
66 const struct lyxml_ns *ns;
67 struct ly_set attr_prefixes = {0};
68 struct attr_prefix_s {
69 const char *prefix;
70 size_t prefix_len;
71 } *attr_prefix;
72 struct lys_module *mod;
73
74 while(ctx->status == LYXML_ATTRIBUTE &&
75 lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) {
76 int dynamic = 0;
77 char *buffer = NULL, *value;
78 size_t buffer_size = 0, value_len;
79
Radek Krejci17a78d82019-05-15 15:49:55 +020080 if (!name) {
81 /* seems like all the attrributes were internally processed as namespace definitions */
82 continue;
Radek Krejcie7b95092019-05-15 11:03:07 +020083 }
Radek Krejci17a78d82019-05-15 15:49:55 +020084
85 /* get attribute value */
86 ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
87 LY_CHECK_GOTO(ret, cleanup);
88
89 attr = calloc(1, sizeof *attr);
90 LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
91
92 attr->name = lydict_insert(ctx->ctx, name, name_len);
93 /* auxiliary store the prefix information and wait with resolving prefix to the time when all the namespaces,
94 * defined in this element, are parsed, so we will get the correct namespace for this prefix */
95 attr_prefix = malloc(sizeof *attr_prefix);
96 attr_prefix->prefix = prefix;
97 attr_prefix->prefix_len = prefix_len;
98 ly_set_add(&attr_prefixes, attr_prefix, LY_SET_OPT_USEASLIST);
99
100 /* TODO process value */
101
102 if (last) {
103 last->next = attr;
104 } else {
105 (*attributes) = attr;
106 }
107 last = attr;
Radek Krejcie7b95092019-05-15 11:03:07 +0200108 }
109
110 /* resolve annotation pointers in all the attributes */
111 for (last = *attributes, u = 0; u < attr_prefixes.count && last; u++, last = last->next) {
112 attr_prefix = (struct attr_prefix_s*)attr_prefixes.objs[u];
113 ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_prefix->prefix, attr_prefix->prefix_len);
114 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
115
116 /* TODO get annotation */
117 }
118
119cleanup:
120
121 ly_set_erase(&attr_prefixes, free);
122 return ret;
123}
124
125/**
126 * @brief Parse XML elements as children YANG data node of the specified parent node.
127 *
128 * @param[in] ctx XML YANG data parser context.
129 * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
130 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
131 * @param[out] node Resulting list of the parsed nodes.
132 * @reutn LY_ERR value.
133 */
134static LY_ERR
135lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node)
136{
137 LY_ERR ret = LY_SUCCESS;
138 const char *prefix, *name;
Radek Krejcie7b95092019-05-15 11:03:07 +0200139 size_t prefix_len, name_len;
140 struct lyd_attr *attributes = NULL;
141 const struct lyxml_ns *ns;
142 const struct lysc_node *snode;
143 struct lys_module *mod;
144 unsigned int parents_count = ctx->elements.count;
145 struct lyd_node *cur = NULL, *prev = NULL;
146
147 (*node) = NULL;
148
149 while(ctx->status == LYXML_ELEMENT) {
150 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len);
151 LY_CHECK_GOTO(ret, cleanup);
152 if (!name) {
153 /* closing previous element */
Radek Krejcie7b95092019-05-15 11:03:07 +0200154 if (ctx->elements.count < parents_count) {
155 /* all siblings parsed */
156 break;
157 } else {
158 continue;
159 }
160 }
161 attributes = NULL;
Radek Krejci17a78d82019-05-15 15:49:55 +0200162 LY_CHECK_GOTO(lydxml_attributes(ctx, data, &attributes), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200163 ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len);
164 if (!ns) {
165 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%*.s\".", prefix_len, prefix);
166 goto cleanup;
167 }
168 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
169 if (!mod) {
170 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
171 goto cleanup;
172 }
173 snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0);
174 if (!snode) {
175 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name);
176 goto cleanup;
177 }
178
179 /* allocate new node */
180 switch (snode->nodetype) {
181 case LYS_CONTAINER:
182 case LYS_LIST:
183 cur = calloc(1, sizeof(struct lyd_node_inner));
184 break;
185 case LYS_LEAF:
186 case LYS_LEAFLIST:
187 cur = calloc(1, sizeof(struct lyd_node_term));
188 break;
189 case LYS_ANYDATA:
190 case LYS_ANYXML:
191 cur = calloc(1, sizeof(struct lyd_node_any));
192 break;
193 /* TODO LYS_ACTION, LYS_NOTIF */
194 default:
195 LOGINT(ctx->ctx);
196 goto cleanup;
197 }
198 if (!(*node)) {
199 (*node) = cur;
200 }
201 cur->schema = snode;
202 cur->parent = parent;
Radek Krejcie92210c2019-05-17 15:53:35 +0200203 if (parent) {
204 parent->child->prev = cur;
205 } else if (prev) {
206 struct lyd_node *iter;
207 for (iter = prev; iter->prev->next; iter = iter->prev);
208 iter->prev = cur;
209 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200210 if (prev) {
211 cur->prev = prev;
212 prev->next = cur;
213 } else {
214 cur->prev = cur;
215 }
216 prev = cur;
217 cur->attr = attributes;
218 attributes = NULL;
219
220 if (snode->nodetype & LYD_NODE_TERM) {
221 int dynamic = 0;
222 char *buffer = NULL, *value;
223 size_t buffer_size = 0, value_len;
224
225 if (ctx->status == LYXML_ELEM_CONTENT) {
226 /* get the value */
Radek Krejci339e2de2019-05-17 14:28:24 +0200227 LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
228 if (r == LY_EINVAL) {
229 /* just indentation of a child element found */
230 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.", cur->schema->name);
231 goto cleanup;
232 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200233 } else {
234 /* no content - validate empty value */
235 value = "";
236 value_len = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200237 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200238 ret = lyd_value_validate((struct lyd_node_term*)cur, value, value_len,
239 LY_TYPE_VALIDATE_CANONIZE | (dynamic ? LY_TYPE_VALIDATE_DYNAMIC : 0));
240 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200241 } else if (snode->nodetype & LYD_NODE_INNER) {
242 int dynamic = 0;
243 char *buffer = NULL, *value;
244 size_t buffer_size = 0, value_len;
245
246 if (ctx->status == LYXML_ELEM_CONTENT) {
247 LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
248 if (r != LY_EINVAL) {
249 LOGINT(ctx->ctx);
250 goto cleanup;
251 }
252 }
253 if (ctx->status == LYXML_ELEMENT) {
254 ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur));
Radek Krejcie92210c2019-05-17 15:53:35 +0200255 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200256 }
257 }
258 /* TODO anyxml/anydata */
259 }
260
261cleanup:
Radek Krejcie7b95092019-05-15 11:03:07 +0200262 lyd_free_attr(ctx->ctx, attributes, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200263 return ret;
264}
265
266LY_ERR
267lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result)
268{
269 LY_ERR ret;
270 struct lyd_xml_ctx xmlctx = {0};
271
272 xmlctx.options = options;
273 xmlctx.ctx = ctx;
274 xmlctx.line = 1;
275
276 ret = lydxml_nodes(&xmlctx, NULL, &data, result);
Radek Krejcie92210c2019-05-17 15:53:35 +0200277 if (ret) {
278 lyd_free_all(*result);
279 *result = NULL;
280 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200281 lyxml_context_clear((struct lyxml_context*)&xmlctx);
282 return ret;
283}