blob: a972d4cc956c0465e6b74eb639c2e334d7718642 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 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 <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <stdarg.h>
21#include <stdint.h>
22#include <string.h>
23#include <unistd.h>
24
25#include "log.h"
26#include "tree.h"
27#include "tree_data.h"
28#include "tree_data_internal.h"
29#include "tree_schema.h"
30
31static int
32cmp_str(const char *refstr, const char *str, size_t strlen)
33{
34
35 if (strlen) {
36 int r = strncmp(refstr, str, strlen);
37 if (!r && !refstr[strlen]) {
38 return 0;
39 } else {
40 return 1;
41 }
42 } else {
43 return strcmp(refstr, str);
44 }
45}
46
47API const struct lyd_node *
48lyd_search(const struct lyd_node *first, const struct lys_module *module,
49 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len)
50{
51 const struct lyd_node *node = NULL;
52 const struct lysc_node *snode;
53
54 LY_CHECK_ARG_RET(NULL, module, name, NULL);
55 if (!nodetype) {
56 nodetype = 0xffff;
57 }
58
59 LY_LIST_FOR(first, node) {
60 snode = node->schema;
61 if (!(snode->nodetype & nodetype)) {
62 continue;
63 }
64 if (snode->module != module) {
65 continue;
66 }
67
68 if (cmp_str(snode->name, name, name_len)) {
69 continue;
70 }
71
72 if (value) {
73 if (snode->nodetype == LYS_LIST) {
74 /* TODO handle value as keys of the list instance */
75 } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
76 if (cmp_str(((struct lyd_node_term*)node)->value.canonized, value, value_len)) {
77 continue;
78 }
79 } else {
80 continue;
81 }
82 }
83
84 /* all criteria passed */
85 return node;
86 }
87 return NULL;
88}
89
90static struct lyd_node *
91lyd_parse_mem_(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, va_list ap)
92{
93 LY_ERR ret = LY_SUCCESS;
94 struct lyd_node *result = NULL;
95 const struct lyd_node *rpc_act = NULL, *data_tree = NULL, *iter;
96 const char *yang_data_name = NULL;
97
98 if (lyd_parse_check_options(ctx, options, __func__)) {
99 return NULL;
100 }
101
102 if (options & LYD_OPT_RPCREPLY) {
103 rpc_act = va_arg(ap, const struct lyd_node *);
104 if (!rpc_act || rpc_act->parent || !(rpc_act->schema->nodetype & (LYS_ACTION | LYS_LIST | LYS_CONTAINER))) {
105 LOGERR(ctx, LY_EINVAL, "Data parser invalid variable parameter (const struct lyd_node *rpc_act).");
106 return NULL;
107 }
108 }
109 if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF | LYD_OPT_RPCREPLY)) {
110 data_tree = va_arg(ap, const struct lyd_node *);
111 if (data_tree) {
112 if (options & LYD_OPT_NOEXTDEPS) {
113 LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree and LYD_OPT_NOEXTDEPS set).",
114 __func__);
115 return NULL;
116 }
117
118 LY_LIST_FOR(data_tree, iter) {
119 if (iter->parent) {
120 /* a sibling is not top-level */
121 LOGERR(ctx, LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *data_tree).", __func__);
122 return NULL;
123 }
124 }
125
126 /* move it to the beginning */
127 for (; data_tree->prev->next; data_tree = data_tree->prev);
128
129 /* LYD_OPT_NOSIBLINGS cannot be set in this case */
130 if (options & LYD_OPT_NOSIBLINGS) {
131 LOGERR(ctx, LY_EINVAL, "%s: invalid parameter (variable arg const struct lyd_node *data_tree with LYD_OPT_NOSIBLINGS).", __func__);
132 return NULL;
133 }
134 }
135 }
136 if (options & LYD_OPT_DATA_TEMPLATE) {
137 yang_data_name = va_arg(ap, const char *);
138 }
139
140 if (!format) {
141 /* TODO try to detect format from the content */
142 }
143
144 switch (format) {
145 case LYD_XML:
146 ret = lyd_parse_xml(ctx, data, options, &result);
147 break;
148#if 0
149 case LYD_JSON:
150 ret = lyd_parse_json(ctx, data, options, rpc_act, data_tree, yang_data_name);
151 break;
152 case LYD_LYB:
153 ret = lyd_parse_lyb(ctx, data, options, data_tree, yang_data_name, NULL);
154 break;
155#endif
156 case LYD_UNKNOWN:
157 LOGINT(ctx);
158 break;
159 }
160
161 if (ret) {
162 lyd_free_all(result);
163 result = NULL;
164 }
165
166 return result;
167}
168
169API struct lyd_node *
170lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...)
171{
172 va_list ap;
173 struct lyd_node *result;
174
175 va_start(ap, options);
176 result = lyd_parse_mem_(ctx, data, format, options, ap);
177 va_end(ap);
178
179 return result;
180}
181
182static struct lyd_node *
183lyd_parse_fd_(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, va_list ap)
184{
185 struct lyd_node *result;
186 size_t length;
187 char *addr;
188
189 LY_CHECK_ARG_RET(ctx, ctx, NULL);
190 if (fd < 0) {
191 LOGARG(ctx, fd);
192 return NULL;
193 }
194
195 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
196 result = lyd_parse_mem_(ctx, addr ? addr : "", format, options, ap);
Radek Krejcidf3da792019-05-17 10:32:24 +0200197 if (addr) {
198 ly_munmap(addr, length);
199 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200200
201 return result;
202}
203
204API struct lyd_node *
205lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...)
206{
207 struct lyd_node *ret;
208 va_list ap;
209
210 va_start(ap, options);
211 ret = lyd_parse_fd_(ctx, fd, format, options, ap);
212 va_end(ap);
213
214 return ret;
215}
216
217API struct lyd_node *
218lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...)
219{
220 int fd;
221 struct lyd_node *result;
222 size_t len;
223 va_list ap;
224
225 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
226
227 fd = open(path, O_RDONLY);
228 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
229
230 if (!format) {
231 /* unknown format - try to detect it from filename's suffix */
232 len = strlen(path);
233
234 /* ignore trailing whitespaces */
235 for (; len > 0 && isspace(path[len - 1]); len--);
236
237 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
238 format = LYD_XML;
239#if 0
240 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
241 format = LYD_JSON;
242 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
243 format = LYD_LYB;
244#endif
245 } /* else still unknown, try later to detect it from the content */
246 }
247
248 va_start(ap, options);
249 result = lyd_parse_fd_(ctx, fd, format, options, ap);
250
251 va_end(ap);
252 close(fd);
253
254 return result;
255}
256
257
258
259