/src/libyang/src/parser_json.c
Line | Count | Source |
1 | | /** |
2 | | * @file parser_json.c |
3 | | * @author Radek Krejci <rkrejci@cesnet.cz> |
4 | | * @author Michal Vasko <mvasko@cesnet.cz> |
5 | | * @brief JSON data parser for libyang |
6 | | * |
7 | | * Copyright (c) 2020 - 2026 CESNET, z.s.p.o. |
8 | | * |
9 | | * This source code is licensed under BSD 3-Clause License (the "License"). |
10 | | * You may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * https://opensource.org/licenses/BSD-3-Clause |
14 | | */ |
15 | | |
16 | | #define _GNU_SOURCE |
17 | | |
18 | | #include <assert.h> |
19 | | #include <stdint.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | |
23 | | #include "compat.h" |
24 | | #include "context.h" |
25 | | #include "dict.h" |
26 | | #include "in_internal.h" |
27 | | #include "json.h" |
28 | | #include "log.h" |
29 | | #include "ly_common.h" |
30 | | #include "parser_data.h" |
31 | | #include "parser_internal.h" |
32 | | #include "plugins_exts.h" |
33 | | #include "set.h" |
34 | | #include "tree.h" |
35 | | #include "tree_data.h" |
36 | | #include "tree_data_internal.h" |
37 | | #include "tree_schema.h" |
38 | | #include "tree_schema_internal.h" |
39 | | #include "validation.h" |
40 | | |
41 | | static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, |
42 | | struct ly_set *parsed); |
43 | | |
44 | | /** |
45 | | * @brief Free the JSON data parser context. |
46 | | * |
47 | | * JSON implementation of lyd_ctx_free_clb(). |
48 | | */ |
49 | | static void |
50 | | lyd_json_ctx_free(struct lyd_ctx *lydctx) |
51 | 0 | { |
52 | 0 | struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx; |
53 | |
|
54 | 0 | if (lydctx) { |
55 | 0 | lyd_ctx_free(lydctx); |
56 | 0 | lyjson_ctx_free(ctx->jsonctx); |
57 | 0 | free(ctx); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | /** |
62 | | * @brief Pass the responsibility for releasing the dynamic values to @p dst. |
63 | | * |
64 | | * @param[in] jsonctx JSON context which contains the dynamic value. |
65 | | * @param[in,out] dst Pointer to which the responsibility will be submited. |
66 | | * If the pointer is already pointing to some allocated memory, it is released beforehand. |
67 | | */ |
68 | | static void |
69 | | lyjson_ctx_give_dynamic_value(struct lyjson_ctx *jsonctx, char **dst) |
70 | 0 | { |
71 | 0 | assert(jsonctx && dst); |
72 | | |
73 | 0 | if (!jsonctx->dynamic) { |
74 | 0 | return; |
75 | 0 | } |
76 | | |
77 | 0 | if (dst) { |
78 | 0 | free(*dst); |
79 | 0 | } |
80 | 0 | *dst = NULL; |
81 | | |
82 | | /* give the dynamic value */ |
83 | 0 | *dst = (char *)jsonctx->value; |
84 | | |
85 | | /* responsibility for the release is now passed to dst */ |
86 | 0 | jsonctx->dynamic = 0; |
87 | 0 | } |
88 | | |
89 | | /** |
90 | | * @brief Parse JSON member-name as [\@][prefix:][name] |
91 | | * |
92 | | * \@ - metadata flag, maps to 1 in @p is_meta_p |
93 | | * prefix - name of the module of the data node |
94 | | * name - name of the data node |
95 | | * |
96 | | * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller. |
97 | | * |
98 | | * @param[in] value String to parse |
99 | | * @param[in] value_len Length of the @p str. |
100 | | * @param[out] name_p Pointer to the beginning of the parsed name. |
101 | | * @param[out] name_len_p Pointer to the length of the parsed name. |
102 | | * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL. |
103 | | * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0. |
104 | | * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise. |
105 | | */ |
106 | | static void |
107 | | lydjson_parse_name(const char *value, uint32_t value_len, const char **name_p, uint32_t *name_len_p, const char **prefix_p, |
108 | | uint32_t *prefix_len_p, ly_bool *is_meta_p) |
109 | 0 | { |
110 | 0 | const char *name, *prefix = NULL; |
111 | 0 | uint32_t name_len, prefix_len = 0; |
112 | 0 | ly_bool is_meta = 0; |
113 | |
|
114 | 0 | name = memchr(value, ':', value_len); |
115 | 0 | if (name != NULL) { |
116 | 0 | prefix = value; |
117 | 0 | if (*prefix == '@') { |
118 | 0 | is_meta = 1; |
119 | 0 | prefix++; |
120 | 0 | } |
121 | 0 | prefix_len = name - prefix; |
122 | 0 | name++; |
123 | 0 | name_len = value_len - (prefix_len + 1) - is_meta; |
124 | 0 | } else { |
125 | 0 | name = value; |
126 | 0 | if (name[0] == '@') { |
127 | 0 | is_meta = 1; |
128 | 0 | name++; |
129 | 0 | } |
130 | 0 | name_len = value_len - is_meta; |
131 | 0 | } |
132 | |
|
133 | 0 | *name_p = name; |
134 | 0 | *name_len_p = name_len; |
135 | 0 | *prefix_p = prefix; |
136 | 0 | *prefix_len_p = prefix_len; |
137 | 0 | *is_meta_p = is_meta; |
138 | 0 | } |
139 | | |
140 | | /** |
141 | | * @brief Get correct prefix (module_name) inside the @p node. |
142 | | * |
143 | | * @param[in] node Data node to get inherited prefix. |
144 | | * @param[in] local_prefix Local prefix to replace the inherited prefix. |
145 | | * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken. |
146 | | * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix |
147 | | * and no context @p node to get inherited prefix. |
148 | | * @param[out] prefix_len_p Pointer to the length of the resulting @p prefix_p string. Note that the result can be 0 in case |
149 | | * of no local prefix and no context @p node to get inherited prefix. |
150 | | * @return LY_ERR value. |
151 | | */ |
152 | | static LY_ERR |
153 | | lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, uint32_t local_prefix_len, const char **prefix_p, |
154 | | uint32_t *prefix_len_p) |
155 | 0 | { |
156 | 0 | struct lyd_node_opaq *onode; |
157 | 0 | const char *module_name = NULL; |
158 | |
|
159 | 0 | assert(prefix_p && prefix_len_p); |
160 | | |
161 | 0 | if (local_prefix_len) { |
162 | 0 | *prefix_p = local_prefix; |
163 | 0 | *prefix_len_p = local_prefix_len; |
164 | 0 | return LY_SUCCESS; |
165 | 0 | } |
166 | | |
167 | 0 | while (node) { |
168 | 0 | if (node->schema) { |
169 | 0 | module_name = node->schema->module->name; |
170 | 0 | break; |
171 | 0 | } |
172 | 0 | onode = (struct lyd_node_opaq *)node; |
173 | 0 | if (onode->name.module_name) { |
174 | 0 | module_name = onode->name.module_name; |
175 | 0 | break; |
176 | 0 | } else if (onode->name.prefix) { |
177 | 0 | module_name = onode->name.prefix; |
178 | 0 | break; |
179 | 0 | } |
180 | 0 | node = node->parent; |
181 | 0 | } |
182 | |
|
183 | 0 | *prefix_p = module_name; |
184 | 0 | *prefix_len_p = ly_strlen(module_name); |
185 | 0 | return LY_SUCCESS; |
186 | 0 | } |
187 | | |
188 | | /** |
189 | | * @brief Skip the current JSON item (based on status). |
190 | | * |
191 | | * The JSON context is moved to the last "status" of the JSON item so to completely |
192 | | * finish the skip, one more JSON context move is required. |
193 | | * |
194 | | * @param[in] jsonctx JSON context with the input data to skip. |
195 | | * @return LY_ERR value. |
196 | | */ |
197 | | static LY_ERR |
198 | | lydjson_data_skip(struct lyjson_ctx *jsonctx) |
199 | 0 | { |
200 | 0 | enum LYJSON_PARSER_STATUS status, current; |
201 | 0 | uint32_t depth; |
202 | |
|
203 | 0 | status = lyjson_ctx_status(jsonctx); |
204 | 0 | depth = lyjson_ctx_depth(jsonctx); |
205 | |
|
206 | 0 | switch (status) { |
207 | 0 | case LYJSON_OBJECT: |
208 | 0 | ++depth; |
209 | | |
210 | | /* skip until object closes */ |
211 | 0 | do { |
212 | 0 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); |
213 | 0 | } while ((current != LYJSON_OBJECT_CLOSED) || (depth != lyjson_ctx_depth(jsonctx))); |
214 | 0 | break; |
215 | 0 | case LYJSON_ARRAY: |
216 | 0 | ++depth; |
217 | | |
218 | | /* skip until array closes */ |
219 | 0 | do { |
220 | 0 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); |
221 | 0 | } while ((current != LYJSON_ARRAY_CLOSED) || (depth != lyjson_ctx_depth(jsonctx))); |
222 | 0 | break; |
223 | 0 | case LYJSON_OBJECT_NAME: |
224 | | /* just get to the value */ |
225 | 0 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); |
226 | 0 | if ((current == LYJSON_OBJECT) || (current == LYJSON_ARRAY)) { |
227 | 0 | LY_CHECK_RET(lydjson_data_skip(jsonctx)); |
228 | 0 | } |
229 | 0 | break; |
230 | 0 | default: |
231 | | /* no other status really expected, just go to next */ |
232 | 0 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); |
233 | 0 | break; |
234 | 0 | } |
235 | | |
236 | 0 | return LY_SUCCESS; |
237 | 0 | } |
238 | | |
239 | | /** |
240 | | * @brief Get schema node corresponding to the input parameters. |
241 | | * |
242 | | * @param[in] lydctx JSON data parser context. |
243 | | * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only. |
244 | | * @param[in] prefix Requested node's prefix (module name). |
245 | | * @param[in] prefix_len Length of the @p prefix. |
246 | | * @param[in] name Requested node's name. |
247 | | * @param[in] name_len Length of the @p name. |
248 | | * @param[in] parent Parent of the node being processed, can be NULL in case of top-level. |
249 | | * @param[out] snode Found schema node corresponding to the input parameters. If NULL, parse as an opaque node. |
250 | | * @param[out] ext Extension instance that provided @p snode, if any. |
251 | | * @return LY_SUCCES on success. |
252 | | * @return LY_ENOT if the whole object was parsed (skipped or as an extension). |
253 | | * @return LY_ERR on error. |
254 | | */ |
255 | | static LY_ERR |
256 | | lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, uint32_t prefix_len, const char *name, |
257 | | uint32_t name_len, struct lyd_node *parent, const struct lysc_node **snode, struct lysc_ext_instance **ext) |
258 | 0 | { |
259 | 0 | LY_ERR r; |
260 | 0 | struct lys_module *mod = NULL; |
261 | 0 | const struct lysc_node *sparent; |
262 | 0 | uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
263 | |
|
264 | 0 | *snode = NULL; |
265 | 0 | if (ext) { |
266 | 0 | *ext = NULL; |
267 | 0 | } |
268 | | |
269 | | /* try to find parent schema node */ |
270 | 0 | if (parent && parent->schema && !(parent->schema->nodetype & LYD_NODE_ANY)) { |
271 | | /* use only a schema parent */ |
272 | 0 | sparent = parent->schema; |
273 | 0 | } else { |
274 | 0 | sparent = NULL; |
275 | 0 | } |
276 | 0 | if (!prefix_len) { |
277 | 0 | prefix = NULL; |
278 | 0 | } |
279 | 0 | r = lys_find_child_node(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, sparent, NULL, prefix, prefix_len, |
280 | 0 | LY_VALUE_JSON, NULL, name, name_len, getnext_opts, snode, ext); |
281 | 0 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
282 | |
|
283 | 0 | if (!r) { |
284 | | /* check that schema node is valid and can be used */ |
285 | 0 | LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode)); |
286 | 0 | return LY_SUCCESS; |
287 | 0 | } |
288 | | |
289 | | /* generate error, find the module */ |
290 | 0 | if (prefix_len) { |
291 | 0 | mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len); |
292 | 0 | } else if (parent) { |
293 | 0 | if (parent->schema) { |
294 | 0 | mod = parent->schema->module; |
295 | 0 | } |
296 | 0 | } else if (!(lydctx->int_opts & LYD_INTOPT_ANY)) { |
297 | 0 | LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_SYNTAX_JSON, |
298 | 0 | "Top-level JSON object member \"%.*s\" must be namespace-qualified.", |
299 | 0 | (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name); |
300 | 0 | return LY_EVALID; |
301 | 0 | } |
302 | 0 | if (!(lydctx->parse_opts & LYD_PARSE_STRICT)) { |
303 | 0 | return LY_SUCCESS; |
304 | 0 | } |
305 | | |
306 | 0 | if (!mod) { |
307 | | /* unknown module */ |
308 | 0 | LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", |
309 | 0 | (int)prefix_len, prefix); |
310 | 0 | return LY_EVALID; |
311 | 0 | } |
312 | | |
313 | | /* unknown node name */ |
314 | 0 | if (sparent) { |
315 | 0 | LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.", |
316 | 0 | (int)name_len, name, sparent->name); |
317 | 0 | } else { |
318 | 0 | LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
319 | 0 | (int)name_len, name, mod->name); |
320 | 0 | } |
321 | 0 | return LY_EVALID; |
322 | 0 | } |
323 | | |
324 | | /** |
325 | | * @brief Get the hint for the data type parsers according to the current JSON parser context. |
326 | | * |
327 | | * @param[in] lydctx JSON data parser context. |
328 | | * @param[in] snode Schema node for logging. |
329 | | * @param[in,out] status Pointer to the current context status, |
330 | | * in some circumstances the function manipulates with the context so the status is updated. |
331 | | * @param[out] type_hint_p Pointer to the variable to store the result. |
332 | | * @return LY_SUCCESS in case of success. |
333 | | * @return LY_EINVAL in case of invalid context status not referring to a value. |
334 | | */ |
335 | | static LY_ERR |
336 | | lydjson_value_type_hint(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, enum LYJSON_PARSER_STATUS *status_p, |
337 | | uint32_t *type_hint_p) |
338 | 0 | { |
339 | 0 | struct lyjson_ctx *jsonctx = lydctx->jsonctx; |
340 | |
|
341 | 0 | *type_hint_p = 0; |
342 | |
|
343 | 0 | if (*status_p == LYJSON_ARRAY) { |
344 | | /* only [null] */ |
345 | 0 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, status_p)); |
346 | 0 | if (*status_p != LYJSON_NULL) { |
347 | 0 | if (snode) { |
348 | 0 | LOG_LOCSET(snode); |
349 | 0 | } |
350 | 0 | LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX_JSON, |
351 | 0 | "Expected JSON name/value or special name/[null], but input data contains name/[%s].", |
352 | 0 | lyjson_token2str(*status_p)); |
353 | 0 | if (snode) { |
354 | 0 | LOG_LOCBACK(1); |
355 | 0 | } |
356 | 0 | return LY_EINVAL; |
357 | 0 | } |
358 | | |
359 | 0 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL)); |
360 | 0 | if (lyjson_ctx_status(jsonctx) != LYJSON_ARRAY_CLOSED) { |
361 | 0 | if (snode) { |
362 | 0 | LOG_LOCSET(snode); |
363 | 0 | } |
364 | 0 | LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.", |
365 | 0 | lyjson_token2str(*status_p)); |
366 | 0 | if (snode) { |
367 | 0 | LOG_LOCBACK(1); |
368 | 0 | } |
369 | 0 | return LY_EINVAL; |
370 | 0 | } |
371 | | |
372 | 0 | *type_hint_p = LYD_VALHINT_EMPTY; |
373 | 0 | } else if (*status_p == LYJSON_STRING) { |
374 | 0 | *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64; |
375 | 0 | } else if (*status_p == LYJSON_NUMBER) { |
376 | 0 | *type_hint_p = LYD_VALHINT_DECNUM; |
377 | 0 | } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) { |
378 | 0 | *type_hint_p = LYD_VALHINT_BOOLEAN; |
379 | 0 | } else if (*status_p == LYJSON_NULL) { |
380 | 0 | *type_hint_p = 0; |
381 | 0 | } else { |
382 | 0 | if (snode) { |
383 | 0 | LOG_LOCSET(snode); |
384 | 0 | } |
385 | 0 | LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p)); |
386 | 0 | if (snode) { |
387 | 0 | LOG_LOCBACK(1); |
388 | 0 | } |
389 | 0 | return LY_EINVAL; |
390 | 0 | } |
391 | | |
392 | 0 | if (lydctx->parse_opts & LYD_PARSE_JSON_STRING_DATATYPES) { |
393 | 0 | *type_hint_p |= LYD_VALHINT_STRING_DATATYPES; |
394 | 0 | } |
395 | |
|
396 | 0 | return LY_SUCCESS; |
397 | 0 | } |
398 | | |
399 | | /** |
400 | | * @brief Check that the input data are parseable as the @p list. |
401 | | * |
402 | | * Checks for all the list's keys. Function does not revert the context state. |
403 | | * |
404 | | * @param[in] lydctx JSON data parser context. |
405 | | * @param[in] list List schema node corresponding to the input data object. |
406 | | * @return LY_SUCCESS in case the data are ok for the @p list |
407 | | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance. |
408 | | */ |
409 | | static LY_ERR |
410 | | lydjson_check_list(struct lyd_json_ctx *lydctx, const struct lysc_node *list) |
411 | 0 | { |
412 | 0 | LY_ERR rc = LY_SUCCESS; |
413 | 0 | struct lyjson_ctx *jsonctx = lydctx->jsonctx; |
414 | 0 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx); |
415 | 0 | struct ly_set key_set = {0}; |
416 | 0 | const struct lysc_node *snode; |
417 | 0 | uint32_t i, hints; |
418 | |
|
419 | 0 | assert(list && (list->nodetype == LYS_LIST)); |
420 | | |
421 | | /* get all keys into a set (keys do not have if-features or anything) */ |
422 | 0 | snode = NULL; |
423 | 0 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
424 | 0 | rc = ly_set_add(&key_set, (void *)snode, 1, NULL); |
425 | 0 | LY_CHECK_GOTO(rc, cleanup); |
426 | 0 | } |
427 | 0 | if (!key_set.count) { |
428 | | /* no keys */ |
429 | 0 | goto cleanup; |
430 | 0 | } |
431 | | |
432 | 0 | if (status == LYJSON_OBJECT) { |
433 | 0 | do { |
434 | 0 | const char *name, *prefix; |
435 | 0 | uint32_t name_len, prefix_len; |
436 | 0 | ly_bool is_attr; |
437 | | |
438 | | /* match the key */ |
439 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); |
440 | 0 | if (status != LYJSON_OBJECT_NAME) { |
441 | 0 | break; |
442 | 0 | } |
443 | 0 | snode = NULL; |
444 | 0 | lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
445 | |
|
446 | 0 | if (!is_attr && !prefix) { |
447 | 0 | for (i = 0; i < key_set.count; ++i) { |
448 | 0 | if (!ly_strncmp(key_set.snodes[i]->name, name, name_len)) { |
449 | 0 | snode = key_set.snodes[i]; |
450 | 0 | break; |
451 | 0 | } |
452 | 0 | } |
453 | | |
454 | | /* get the value */ |
455 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); |
456 | |
|
457 | 0 | if (snode) { |
458 | | /* we have the key, validate the value */ |
459 | 0 | if ((status < LYJSON_NUMBER) || (status > LYJSON_NULL)) { |
460 | | /* not a terminal */ |
461 | 0 | rc = LY_ENOT; |
462 | 0 | goto cleanup; |
463 | 0 | } |
464 | | |
465 | 0 | rc = lydjson_value_type_hint(lydctx, snode, &status, &hints); |
466 | 0 | LY_CHECK_GOTO(rc, cleanup); |
467 | 0 | rc = ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len * 8, LY_VALUE_JSON, NULL, |
468 | 0 | hints); |
469 | 0 | LY_CHECK_GOTO(rc, cleanup); |
470 | | |
471 | | /* key with a valid value, remove from the set */ |
472 | 0 | ly_set_rm_index(&key_set, i, NULL); |
473 | 0 | } |
474 | | |
475 | | /* next object */ |
476 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); |
477 | 0 | } else { |
478 | | /* skip the uninteresting object */ |
479 | 0 | LY_CHECK_GOTO(rc = lydjson_data_skip(jsonctx), cleanup); |
480 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); |
481 | 0 | } |
482 | 0 | } while (key_set.count && (status == LYJSON_OBJECT_NEXT)); |
483 | 0 | } |
484 | | |
485 | 0 | if (key_set.count) { |
486 | | /* some keys are missing/did not validate */ |
487 | 0 | rc = LY_ENOT; |
488 | 0 | } |
489 | |
|
490 | 0 | cleanup: |
491 | 0 | ly_set_erase(&key_set, NULL); |
492 | 0 | return rc; |
493 | 0 | } |
494 | | |
495 | | /** |
496 | | * @brief Check in advance if the input data are parsable according to the provided @p snode. |
497 | | * |
498 | | * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking |
499 | | * is naturally done when the data are really parsed. |
500 | | * |
501 | | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
502 | | * as before calling, despite it is necessary to process input data for checking. |
503 | | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
504 | | * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list. |
505 | | * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled. |
506 | | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance |
507 | | * @return LY_EINVAL in case of invalid leaf JSON encoding |
508 | | * and they are expected to be parsed as opaq nodes. |
509 | | */ |
510 | | static LY_ERR |
511 | | lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p) |
512 | 0 | { |
513 | 0 | LY_ERR ret = LY_SUCCESS; |
514 | 0 | struct lyjson_ctx *jsonctx = lydctx->jsonctx; |
515 | 0 | enum LYJSON_PARSER_STATUS status; |
516 | 0 | uint32_t *prev_lo, temp_lo = 0; |
517 | |
|
518 | 0 | assert(snode); |
519 | | |
520 | | /* backup parser */ |
521 | 0 | lyjson_ctx_backup(jsonctx); |
522 | 0 | status = lyjson_ctx_status(jsonctx); |
523 | |
|
524 | 0 | if (lydctx->parse_opts & LYD_PARSE_OPAQ) { |
525 | | /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed |
526 | | * as an opaq node */ |
527 | 0 | switch (snode->nodetype) { |
528 | 0 | case LYS_LEAFLIST: |
529 | 0 | case LYS_LEAF: |
530 | 0 | prev_lo = ly_temp_log_options(&temp_lo); |
531 | | |
532 | | /* value may not be valid in which case we parse it as an opaque node */ |
533 | 0 | if (lydjson_value_type_hint(lydctx, snode, &status, type_hint_p)) { |
534 | 0 | ret = LY_ENOT; |
535 | 0 | } |
536 | |
|
537 | 0 | if (!ret && ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len * 8, LY_VALUE_JSON, NULL, |
538 | 0 | *type_hint_p)) { |
539 | 0 | ret = LY_ENOT; |
540 | 0 | } |
541 | |
|
542 | 0 | ly_temp_log_options(prev_lo); |
543 | 0 | break; |
544 | 0 | case LYS_LIST: |
545 | | /* lists may not have all its keys */ |
546 | 0 | if (lydjson_check_list(lydctx, snode)) { |
547 | | /* invalid list, parse as opaque if it misses/has invalid some keys */ |
548 | 0 | ret = LY_ENOT; |
549 | 0 | } |
550 | 0 | break; |
551 | 0 | case LYS_CONTAINER: |
552 | 0 | case LYS_RPC: |
553 | 0 | case LYS_ACTION: |
554 | 0 | case LYS_NOTIF: |
555 | | /* check the status only */ |
556 | 0 | if (status != LYJSON_OBJECT) { |
557 | 0 | ret = LY_ENOT; |
558 | 0 | } |
559 | 0 | break; |
560 | 0 | } |
561 | 0 | } else if (snode->nodetype & LYD_NODE_TERM) { |
562 | 0 | ret = lydjson_value_type_hint(lydctx, snode, &status, type_hint_p); |
563 | 0 | } |
564 | | |
565 | | /* restore parser */ |
566 | 0 | lyjson_ctx_restore(jsonctx); |
567 | |
|
568 | 0 | return ret; |
569 | 0 | } |
570 | | |
571 | | /** |
572 | | * @brief Join the forward-referencing metadata with their target data nodes. |
573 | | * |
574 | | * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists. |
575 | | * |
576 | | * @param[in] lydctx JSON data parser context. |
577 | | * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node) |
578 | | * as a starting point to search for the metadata's target data node |
579 | | * @return LY_SUCCESS on success |
580 | | * @return LY_EVALID in case there are some metadata with unresolved target data node instance |
581 | | */ |
582 | | static LY_ERR |
583 | | lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p) |
584 | 0 | { |
585 | 0 | LY_ERR ret = LY_SUCCESS; |
586 | 0 | struct lyd_node *node, *attr, *next, *meta_iter; |
587 | 0 | uint64_t instance = 0; |
588 | 0 | const char *prev = NULL; |
589 | | |
590 | | /* finish linking metadata */ |
591 | 0 | LY_LIST_FOR_SAFE(*first_p, next, attr) { |
592 | 0 | struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr; |
593 | 0 | uint64_t match = 0; |
594 | 0 | ly_bool is_attr; |
595 | 0 | const char *name, *prefix; |
596 | 0 | uint32_t name_len, prefix_len; |
597 | 0 | const struct lysc_node *snode; |
598 | |
|
599 | 0 | if (attr->schema || (meta_container->name.name[0] != '@')) { |
600 | | /* not an opaq metadata node */ |
601 | 0 | continue; |
602 | 0 | } |
603 | | |
604 | 0 | if (prev != meta_container->name.name) { |
605 | | /* metas' names are stored in dictionary, so checking pointers must works */ |
606 | 0 | lydict_remove(lydctx->jsonctx->ctx, prev); |
607 | 0 | LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup); |
608 | 0 | instance = 1; |
609 | 0 | } else { |
610 | 0 | instance++; |
611 | 0 | } |
612 | | |
613 | | /* find the corresponding data node */ |
614 | 0 | LY_LIST_FOR(*first_p, node) { |
615 | 0 | if (!node->schema) { |
616 | | /* opaq node - we are going to put into it just a generic attribute. */ |
617 | 0 | if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) { |
618 | 0 | continue; |
619 | 0 | } |
620 | | |
621 | 0 | if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) { |
622 | 0 | LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_SYNTAX, "Metadata container references a sibling list node %s.", |
623 | 0 | ((struct lyd_node_opaq *)node)->name.name); |
624 | 0 | ret = LY_EVALID; |
625 | 0 | goto cleanup; |
626 | 0 | } |
627 | | |
628 | | /* match */ |
629 | 0 | match++; |
630 | 0 | if (match != instance) { |
631 | 0 | continue; |
632 | 0 | } |
633 | | |
634 | 0 | LY_LIST_FOR(meta_container->child, meta_iter) { |
635 | | /* convert opaq node to a attribute of the opaq node */ |
636 | 0 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
637 | |
|
638 | 0 | ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name), |
639 | 0 | meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name, |
640 | 0 | ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON, |
641 | 0 | NULL, meta->hints); |
642 | 0 | LY_CHECK_GOTO(ret, cleanup); |
643 | 0 | } |
644 | | |
645 | | /* done */ |
646 | 0 | break; |
647 | 0 | } else { |
648 | | /* this is the second time we are resolving the schema node, so it must succeed, |
649 | | * but remember that snode can be still NULL */ |
650 | 0 | lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len, |
651 | 0 | &prefix, &prefix_len, &is_attr); |
652 | 0 | assert(is_attr); |
653 | 0 | lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode, NULL); |
654 | |
|
655 | 0 | if (snode != node->schema) { |
656 | 0 | continue; |
657 | 0 | } |
658 | | |
659 | | /* match */ |
660 | 0 | match++; |
661 | 0 | if (match != instance) { |
662 | 0 | continue; |
663 | 0 | } |
664 | | |
665 | 0 | LY_LIST_FOR(meta_container->child, meta_iter) { |
666 | | /* convert opaq node to a metadata of the node */ |
667 | 0 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
668 | 0 | struct lys_module *mod = NULL; |
669 | |
|
670 | 0 | mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix); |
671 | 0 | if (mod) { |
672 | 0 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, meta->name.name, |
673 | 0 | strlen(meta->name.name), meta->value, ly_strlen(meta->value) * 8, NULL, LY_VALUE_JSON, |
674 | 0 | NULL, meta->hints, node->schema, node); |
675 | 0 | LY_CHECK_GOTO(ret, cleanup); |
676 | 0 | } else if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
677 | 0 | if (meta->name.prefix) { |
678 | 0 | LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE, |
679 | 0 | "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".", |
680 | 0 | meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", |
681 | 0 | meta->name.name); |
682 | 0 | } else { |
683 | 0 | LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".", |
684 | 0 | meta->name.name); |
685 | 0 | } |
686 | 0 | ret = LY_EVALID; |
687 | 0 | goto cleanup; |
688 | 0 | } |
689 | 0 | } |
690 | | |
691 | | /* add/correct flags */ |
692 | 0 | ret = lyd_parser_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL); |
693 | 0 | LY_CHECK_GOTO(ret, cleanup); |
694 | 0 | break; |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | 0 | if (match != instance) { |
699 | | /* there is no corresponding data node for the metadata */ |
700 | 0 | if (instance > 1) { |
701 | 0 | LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE, |
702 | 0 | "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.", |
703 | 0 | instance, meta_container->name.name); |
704 | 0 | } else { |
705 | 0 | LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.", |
706 | 0 | meta_container->name.name); |
707 | 0 | } |
708 | 0 | ret = LY_EVALID; |
709 | 0 | } else { |
710 | | /* remove the opaq attr */ |
711 | 0 | if (attr == (*first_p)) { |
712 | 0 | *first_p = attr->next; |
713 | 0 | } |
714 | 0 | lyd_free_tree(attr); |
715 | 0 | } |
716 | 0 | } |
717 | | |
718 | 0 | cleanup: |
719 | 0 | lydict_remove(lydctx->jsonctx->ctx, prev); |
720 | 0 | return ret; |
721 | 0 | } |
722 | | |
723 | | /** |
724 | | * @brief Parse a metadata member/attribute. |
725 | | * |
726 | | * @param[in] lydctx JSON data parser context. |
727 | | * @param[in] node Parent node. |
728 | | * @return LY_SUCCESS on success |
729 | | * @return Various LY_ERR values in case of failure. |
730 | | */ |
731 | | static LY_ERR |
732 | | lydjson_meta_attr(struct lyd_json_ctx *lydctx, struct lyd_node *node) |
733 | 0 | { |
734 | 0 | LY_ERR rc = LY_SUCCESS, r; |
735 | 0 | enum LYJSON_PARSER_STATUS status; |
736 | 0 | const char *expected; |
737 | 0 | ly_bool in_parent = 0; |
738 | 0 | const char *name, *prefix = NULL; |
739 | 0 | char *dynamic_prefname = NULL; |
740 | 0 | uint32_t name_len, prefix_len = 0; |
741 | 0 | struct lys_module *mod; |
742 | 0 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
743 | 0 | ly_bool is_attr = 0; |
744 | 0 | struct lyd_node *prev = node; |
745 | 0 | uint32_t instance = 0, val_hints; |
746 | 0 | uint16_t nodetype; |
747 | |
|
748 | 0 | nodetype = node->schema ? node->schema->nodetype : LYS_CONTAINER; |
749 | | |
750 | | /* move to the second item in the name/X pair */ |
751 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
752 | | |
753 | | /* check attribute encoding */ |
754 | 0 | switch (nodetype) { |
755 | 0 | case LYS_LEAFLIST: |
756 | 0 | expected = "@name/array of objects/nulls"; |
757 | 0 | LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); |
758 | |
|
759 | 0 | next_entry: |
760 | 0 | if (status == LYJSON_ARRAY_CLOSED) { |
761 | | /* no more metadata */ |
762 | 0 | goto cleanup; |
763 | 0 | } |
764 | | |
765 | | /* move into the array/next item */ |
766 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
767 | 0 | instance++; |
768 | 0 | LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_NULL), representation_error); |
769 | |
|
770 | 0 | if (!node || (node->schema != prev->schema)) { |
771 | 0 | LOG_LOCSET(prev->schema); |
772 | 0 | LOGVAL(lydctx->jsonctx->ctx, NULL, LYVE_REFERENCE, "Missing JSON data instance #%" PRIu32 |
773 | 0 | " of %s:%s to be coupled with metadata.", instance, prev->schema->module->name, prev->schema->name); |
774 | 0 | LOG_LOCBACK(1); |
775 | 0 | rc = LY_EVALID; |
776 | 0 | goto cleanup; |
777 | 0 | } |
778 | | |
779 | 0 | if (status == LYJSON_NULL) { |
780 | | /* continue with the next entry in the leaf-list array */ |
781 | 0 | prev = node; |
782 | 0 | node = node->next; |
783 | |
|
784 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
785 | 0 | goto next_entry; |
786 | 0 | } |
787 | 0 | break; |
788 | 0 | case LYS_LEAF: |
789 | 0 | case LYS_ANYXML: |
790 | 0 | expected = "@name/object"; |
791 | |
|
792 | 0 | LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); |
793 | 0 | break; |
794 | 0 | case LYS_CONTAINER: |
795 | 0 | case LYS_LIST: |
796 | 0 | case LYS_ANYDATA: |
797 | 0 | case LYS_NOTIF: |
798 | 0 | case LYS_ACTION: |
799 | 0 | case LYS_RPC: |
800 | 0 | in_parent = 1; |
801 | 0 | expected = "@/object"; |
802 | 0 | LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); |
803 | 0 | break; |
804 | 0 | default: |
805 | 0 | LOGINT(ctx); |
806 | 0 | rc = LY_EINT; |
807 | 0 | goto cleanup; |
808 | 0 | } |
809 | | |
810 | | /* process all the members inside a single metadata object */ |
811 | 0 | assert(status == LYJSON_OBJECT); |
812 | 0 | do { |
813 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
814 | 0 | LY_CHECK_GOTO(status != LYJSON_OBJECT_NAME, representation_error); |
815 | |
|
816 | 0 | lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
817 | 0 | lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname); |
818 | |
|
819 | 0 | if (!name_len) { |
820 | 0 | LOGVAL(ctx, prev, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name); |
821 | 0 | rc = LY_EVALID; |
822 | 0 | goto cleanup; |
823 | 0 | } else if (!prefix_len) { |
824 | 0 | LOGVAL(ctx, prev, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".", |
825 | 0 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
826 | 0 | rc = LY_EVALID; |
827 | 0 | goto cleanup; |
828 | 0 | } else if (is_attr) { |
829 | 0 | LOGVAL(ctx, prev, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"", |
830 | 0 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
831 | 0 | rc = LY_EVALID; |
832 | 0 | goto cleanup; |
833 | 0 | } |
834 | | |
835 | | /* get the element module */ |
836 | 0 | mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len); |
837 | 0 | if (!mod) { |
838 | 0 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
839 | 0 | LOGVAL(ctx, prev, LYVE_REFERENCE, |
840 | 0 | "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.", |
841 | 0 | (int)prefix_len, prefix, (int)name_len, name); |
842 | 0 | rc = LY_EVALID; |
843 | 0 | goto cleanup; |
844 | 0 | } |
845 | 0 | if (node->schema) { |
846 | | /* skip element with children */ |
847 | 0 | LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup); |
848 | 0 | status = lyjson_ctx_status(lydctx->jsonctx); |
849 | | /* end of the item */ |
850 | 0 | continue; |
851 | 0 | } |
852 | 0 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
853 | 0 | } |
854 | | |
855 | | /* get the value */ |
856 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
857 | | |
858 | | /* get value hints */ |
859 | 0 | LY_CHECK_GOTO(rc = lydjson_value_type_hint(lydctx, node->schema, &status, &val_hints), cleanup); |
860 | |
|
861 | 0 | if (node->schema) { |
862 | | /* create metadata */ |
863 | 0 | rc = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, name, name_len, |
864 | 0 | lydctx->jsonctx->value, lydctx->jsonctx->value_len * 8, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, |
865 | 0 | NULL, val_hints, node->schema, node); |
866 | 0 | LY_CHECK_GOTO(rc, cleanup); |
867 | | |
868 | | /* add/correct flags */ |
869 | 0 | rc = lyd_parser_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL); |
870 | 0 | LY_CHECK_GOTO(rc, cleanup); |
871 | 0 | } else { |
872 | | /* create attribute */ |
873 | 0 | const char *module_name; |
874 | 0 | uint32_t module_name_len; |
875 | |
|
876 | 0 | lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len); |
877 | | |
878 | | /* attr2 is always changed to the created attribute */ |
879 | 0 | rc = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, |
880 | 0 | module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, |
881 | 0 | LY_VALUE_JSON, NULL, val_hints); |
882 | 0 | LY_CHECK_GOTO(rc, cleanup); |
883 | 0 | } |
884 | | |
885 | | /* next member */ |
886 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
887 | 0 | } while (status == LYJSON_OBJECT_NEXT); |
888 | 0 | LY_CHECK_GOTO(status != LYJSON_OBJECT_CLOSED, representation_error); |
889 | |
|
890 | 0 | if (nodetype == LYS_LEAFLIST) { |
891 | | /* continue by processing another metadata object for the following |
892 | | * leaf-list instance since they are always instantiated in JSON array */ |
893 | 0 | prev = node; |
894 | 0 | node = node->next; |
895 | |
|
896 | 0 | LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); |
897 | 0 | goto next_entry; |
898 | 0 | } |
899 | | |
900 | | /* success */ |
901 | 0 | goto cleanup; |
902 | | |
903 | 0 | representation_error: |
904 | 0 | LOGVAL(ctx, prev, LYVE_SYNTAX_JSON, |
905 | 0 | "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.", |
906 | 0 | lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status), |
907 | 0 | in_parent ? "" : "name"); |
908 | 0 | rc = LY_EVALID; |
909 | |
|
910 | 0 | cleanup: |
911 | 0 | if ((rc == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { |
912 | | /* try to skip the invalid data */ |
913 | 0 | if ((r = lydjson_data_skip(lydctx->jsonctx))) { |
914 | 0 | rc = r; |
915 | 0 | } |
916 | 0 | } |
917 | 0 | free(dynamic_prefname); |
918 | 0 | return rc; |
919 | 0 | } |
920 | | |
921 | | /** |
922 | | * @brief Wrapper for ::lyd_create_opaq(). |
923 | | * |
924 | | * @param[in] lydctx JSON data parser context. |
925 | | * @param[in] name Name of the opaq node to create. |
926 | | * @param[in] name_len Length of the @p name string. |
927 | | * @param[in] prefix Prefix of the opaq node to create. |
928 | | * @param[in] prefix_len Length of the @p prefx string. |
929 | | * @param[in] parent Data parent of the opaq node, to inherit module name from. |
930 | | * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone |
931 | | * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. |
932 | | * @param[out] node_p Pointer to the created opaq node. |
933 | | * @return LY_ERR value. |
934 | | */ |
935 | | static LY_ERR |
936 | | lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, uint32_t name_len, const char *prefix, uint32_t prefix_len, |
937 | | struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p) |
938 | 0 | { |
939 | 0 | LY_ERR ret = LY_SUCCESS; |
940 | 0 | const char *value = NULL, *module_name; |
941 | 0 | uint32_t value_len = 0, module_name_len = 0; |
942 | 0 | ly_bool dynamic = 0; |
943 | 0 | uint32_t type_hint = 0; |
944 | |
|
945 | 0 | if (*status_inner_p != LYJSON_OBJECT) { |
946 | | /* prepare for creating opaq node with a value */ |
947 | 0 | value = lydctx->jsonctx->value; |
948 | 0 | value_len = lydctx->jsonctx->value_len; |
949 | 0 | dynamic = lydctx->jsonctx->dynamic; |
950 | 0 | lydctx->jsonctx->dynamic = 0; |
951 | |
|
952 | 0 | LY_CHECK_RET(lydjson_value_type_hint(lydctx, NULL, status_inner_p, &type_hint)); |
953 | 0 | } |
954 | | |
955 | | /* get the module name */ |
956 | 0 | lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len); |
957 | 0 | if (!module_name && !parent && lydctx->any_schema) { |
958 | | /* in an anyxml/anydata tree, parsing first node, use the previous any schema node */ |
959 | 0 | module_name = lydctx->any_schema->module->name; |
960 | 0 | module_name_len = strlen(module_name); |
961 | 0 | } |
962 | | |
963 | | /* create node */ |
964 | 0 | ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value, |
965 | 0 | value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p); |
966 | 0 | if (dynamic) { |
967 | 0 | free((char *)value); |
968 | 0 | } |
969 | |
|
970 | 0 | return ret; |
971 | 0 | } |
972 | | |
973 | | /** |
974 | | * @brief Parse opaq node from the input. |
975 | | * |
976 | | * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array. |
977 | | * |
978 | | * @param[in] lydctx JSON data parser context. |
979 | | * @param[in] name Name of the opaq node to create. |
980 | | * @param[in] name_len Length of the @p name string. |
981 | | * @param[in] prefix Prefix of the opaq node to create. |
982 | | * @param[in] prefix_len Length of the @p prefx string. |
983 | | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
984 | | * but must be set if @p first is not. |
985 | | * @param[in,out] status_p Pointer to the current status of the parser context, |
986 | | * since the function manipulates with the context and process the input, the status can be updated. |
987 | | * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone |
988 | | * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. |
989 | | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
990 | | * @param[out] node Pointer to the created opaq node. |
991 | | * @return LY_ERR value. |
992 | | */ |
993 | | static LY_ERR |
994 | | lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, uint32_t name_len, const char *prefix, uint32_t prefix_len, |
995 | | struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p, |
996 | | struct lyd_node **first_p, struct lyd_node **node) |
997 | 0 | { |
998 | 0 | LY_ERR ret = LY_SUCCESS; |
999 | |
|
1000 | 0 | LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node), cleanup); |
1001 | | |
1002 | | /* insert */ |
1003 | 0 | ret = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node); |
1004 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1005 | |
|
1006 | 0 | if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) { |
1007 | | /* special array null value */ |
1008 | 0 | ((struct lyd_node_opaq *)*node)->hints |= LYD_VALHINT_EMPTY; |
1009 | | |
1010 | | /* must be the only item */ |
1011 | 0 | LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup); |
1012 | 0 | if (*status_inner_p != LYJSON_ARRAY_CLOSED) { |
1013 | 0 | LOGVAL(lydctx->jsonctx->ctx, *node, LYVE_SYNTAX, "Array \"null\" member with another member."); |
1014 | 0 | ret = LY_EVALID; |
1015 | 0 | goto cleanup; |
1016 | 0 | } |
1017 | | |
1018 | 0 | goto finish; |
1019 | 0 | } |
1020 | | |
1021 | 0 | while (*status_p == LYJSON_ARRAY) { |
1022 | | /* process another instance of the same node */ |
1023 | 0 | if (*status_inner_p == LYJSON_OBJECT) { |
1024 | | /* array with objects, list */ |
1025 | 0 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST; |
1026 | | |
1027 | | /* but first process children of the object in the array */ |
1028 | 0 | do { |
1029 | 0 | LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL), cleanup); |
1030 | 0 | *status_inner_p = lyjson_ctx_status(lydctx->jsonctx); |
1031 | 0 | } while (*status_inner_p == LYJSON_OBJECT_NEXT); |
1032 | 0 | } else { |
1033 | | /* array with values, leaf-list */ |
1034 | 0 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST; |
1035 | 0 | } |
1036 | | |
1037 | 0 | LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup); |
1038 | 0 | if (*status_inner_p == LYJSON_ARRAY_CLOSED) { |
1039 | 0 | goto finish; |
1040 | 0 | } |
1041 | 0 | assert(*status_inner_p == LYJSON_ARRAY_NEXT); |
1042 | | |
1043 | | /* continue with the next instance */ |
1044 | 0 | LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup); |
1045 | 0 | LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node), |
1046 | 0 | cleanup); |
1047 | | |
1048 | | /* insert */ |
1049 | 0 | ret = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node); |
1050 | 0 | LY_CHECK_GOTO(ret, cleanup); |
1051 | 0 | } |
1052 | | |
1053 | 0 | if (*status_p == LYJSON_OBJECT) { |
1054 | 0 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_CONTAINER; |
1055 | | /* process children */ |
1056 | 0 | do { |
1057 | 0 | LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL), cleanup); |
1058 | 0 | *status_p = lyjson_ctx_status(lydctx->jsonctx); |
1059 | 0 | } while (*status_p == LYJSON_OBJECT_NEXT); |
1060 | 0 | } |
1061 | | |
1062 | 0 | finish: |
1063 | | /* finish linking metadata */ |
1064 | 0 | ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node)); |
1065 | |
|
1066 | 0 | cleanup: |
1067 | 0 | return ret; |
1068 | 0 | } |
1069 | | |
1070 | | /** |
1071 | | * @brief Move to the second item in the name/X pair and parse opaq node from the input. |
1072 | | * |
1073 | | * This function is basically the wrapper of the ::lydjson_parse_opaq(). |
1074 | | * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter |
1075 | | * for ::lydjson_parse_opaq(). |
1076 | | * |
1077 | | * @param[in] lydctx JSON data parser context. |
1078 | | * @param[in] name Name of the opaq node to create. |
1079 | | * @param[in] name_len Length of the @p name string. |
1080 | | * @param[in] prefix Prefix of the opaq node to create. |
1081 | | * @param[in] prefix_len Length of the @p prefx string. |
1082 | | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
1083 | | * but must be set if @p first is not. |
1084 | | * @param[in,out] status_p Pointer to the current status of the parser context, |
1085 | | * since the function manipulates with the context and process the input, the status can be updated. |
1086 | | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
1087 | | * @param[out] node Pointer to the created opaq node. |
1088 | | * @return LY_ERR value. |
1089 | | */ |
1090 | | static LY_ERR |
1091 | | lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, uint32_t name_len, const char *prefix, |
1092 | | uint32_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, |
1093 | | struct lyd_node **first_p, struct lyd_node **node) |
1094 | 0 | { |
1095 | 0 | enum LYJSON_PARSER_STATUS status_inner = 0; |
1096 | | |
1097 | | /* move to the second item in the name/X pair */ |
1098 | 0 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); |
1099 | |
|
1100 | 0 | if (*status_p == LYJSON_ARRAY) { |
1101 | | /* move into the array */ |
1102 | 0 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner)); |
1103 | 0 | } else { |
1104 | | /* just a flag to pass correct parameters into lydjson_parse_opaq() */ |
1105 | 0 | status_inner = LYJSON_ERROR; |
1106 | 0 | } |
1107 | | |
1108 | 0 | if (status_inner == LYJSON_ERROR) { |
1109 | 0 | status_inner = *status_p; |
1110 | 0 | } |
1111 | | |
1112 | | /* parse opaq node from the input */ |
1113 | 0 | LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner, |
1114 | 0 | first_p, node)); |
1115 | |
|
1116 | 0 | return LY_SUCCESS; |
1117 | 0 | } |
1118 | | |
1119 | | /** |
1120 | | * @brief Process the attribute container (starting by @) |
1121 | | * |
1122 | | * @param[in] lydctx JSON data parser context. |
1123 | | * @param[in] attr_node The data node referenced by the attribute container, if already known. |
1124 | | * @param[in] snode The schema node of the data node referenced by the attribute container, if known. |
1125 | | * @param[in] name Name of the opaq node to create. |
1126 | | * @param[in] name_len Length of the @p name string. |
1127 | | * @param[in] prefix Prefix of the opaq node to create. |
1128 | | * @param[in] prefix_len Length of the @p prefx string. |
1129 | | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
1130 | | * but must be set if @p first is not. |
1131 | | * @param[in,out] status_p Pointer to the current status of the parser context, |
1132 | | * since the function manipulates with the context and process the input, the status can be updated. |
1133 | | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
1134 | | * @param[out] node_p Pointer to the created opaq node. |
1135 | | * @return LY_ERR value. |
1136 | | */ |
1137 | | static LY_ERR |
1138 | | lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode, |
1139 | | const char *name, uint32_t name_len, const char *prefix, uint32_t prefix_len, struct lyd_node *parent, |
1140 | | enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p) |
1141 | 0 | { |
1142 | 0 | LY_ERR r; |
1143 | 0 | const char *opaq_name, *mod_name, *attr_mod = NULL; |
1144 | 0 | uint32_t opaq_name_len, attr_mod_len = 0; |
1145 | |
|
1146 | 0 | if (!attr_node) { |
1147 | | /* learn the attribute module name */ |
1148 | 0 | if (!snode) { |
1149 | 0 | if (!prefix) { |
1150 | 0 | lydjson_get_node_prefix(parent, NULL, 0, &attr_mod, &attr_mod_len); |
1151 | 0 | } else { |
1152 | 0 | attr_mod = prefix; |
1153 | 0 | attr_mod_len = prefix_len; |
1154 | 0 | } |
1155 | 0 | } |
1156 | | |
1157 | | /* try to find the instance */ |
1158 | 0 | LY_LIST_FOR(parent ? lyd_child(parent) : *first_p, attr_node) { |
1159 | 0 | if (snode) { |
1160 | 0 | if (attr_node->schema) { |
1161 | 0 | if (attr_node->schema == snode) { |
1162 | 0 | break; |
1163 | 0 | } |
1164 | 0 | } else { |
1165 | 0 | mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name; |
1166 | 0 | if (!strcmp(LYD_NAME(attr_node), snode->name) && mod_name && !strcmp(mod_name, snode->module->name)) { |
1167 | 0 | break; |
1168 | 0 | } |
1169 | 0 | } |
1170 | 0 | } else { |
1171 | | /* get the node module name */ |
1172 | 0 | if (attr_node->schema) { |
1173 | 0 | mod_name = attr_node->schema->module->name; |
1174 | 0 | } else { |
1175 | 0 | mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name; |
1176 | 0 | } |
1177 | | |
1178 | | /* check the attribute belongs to this node */ |
1179 | 0 | if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) && mod_name && attr_mod && |
1180 | 0 | !ly_strncmp(mod_name, attr_mod, attr_mod_len)) { |
1181 | 0 | break; |
1182 | 0 | } |
1183 | 0 | } |
1184 | 0 | } |
1185 | 0 | } |
1186 | |
|
1187 | 0 | if (!attr_node) { |
1188 | | /* parse just as an opaq node with the name beginning with @, |
1189 | | * later we have to check that it belongs to a standard node |
1190 | | * and it is supposed to be converted to a metadata */ |
1191 | 0 | uint32_t prev_opts; |
1192 | | |
1193 | | /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */ |
1194 | 0 | prev_opts = lydctx->parse_opts; |
1195 | 0 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
1196 | 0 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
1197 | |
|
1198 | 0 | opaq_name = prefix ? prefix - 1 : name - 1; |
1199 | 0 | opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1; |
1200 | 0 | r = lydjson_ctx_next_parse_opaq(lydctx, opaq_name, opaq_name_len, NULL, 0, parent, status_p, first_p, node_p); |
1201 | | |
1202 | | /* restore the parser options */ |
1203 | 0 | lydctx->parse_opts = prev_opts; |
1204 | 0 | LY_CHECK_RET(r); |
1205 | 0 | } else { |
1206 | 0 | LY_CHECK_RET(lydjson_meta_attr(lydctx, attr_node)); |
1207 | 0 | } |
1208 | | |
1209 | 0 | return LY_SUCCESS; |
1210 | 0 | } |
1211 | | |
1212 | | /** |
1213 | | * @brief Parse a single anydata/anyxml node. |
1214 | | * |
1215 | | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
1216 | | * as before calling, despite it is necessary to process input data for checking. |
1217 | | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
1218 | | * @param[in] ext Extension instance of @p snode, if any. |
1219 | | * @param[in,out] parent Parent node, if any. |
1220 | | * @param[in,out] first_p First top-level node, is updated. |
1221 | | * @param[in,out] status JSON parser status, is updated. |
1222 | | * @param[out] node Parsed data (or opaque) node. |
1223 | | * @return LY_SUCCESS if a node was successfully parsed, |
1224 | | * @return LY_ENOT in case of invalid JSON encoding, |
1225 | | * @return LY_ERR on other errors. |
1226 | | */ |
1227 | | static LY_ERR |
1228 | | lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
1229 | | struct lyd_node *parent, struct lyd_node **first_p, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) |
1230 | 0 | { |
1231 | 0 | LY_ERR r, rc = LY_SUCCESS; |
1232 | 0 | uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts; |
1233 | 0 | struct ly_in in_start; |
1234 | 0 | char *val = NULL; |
1235 | 0 | const char *end; |
1236 | |
|
1237 | 0 | assert(snode->nodetype & LYD_NODE_ANY); |
1238 | | |
1239 | 0 | *node = NULL; |
1240 | | |
1241 | | /* status check according to allowed JSON types */ |
1242 | 0 | if (snode->nodetype == LYS_ANYXML) { |
1243 | 0 | LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && |
1244 | 0 | (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && |
1245 | 0 | (*status != LYJSON_NULL), LY_ENOT); |
1246 | 0 | } else { |
1247 | 0 | LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT); |
1248 | 0 | } |
1249 | | |
1250 | | /* create any node */ |
1251 | 0 | switch (*status) { |
1252 | 0 | case LYJSON_OBJECT: |
1253 | | /* create empty node */ |
1254 | 0 | r = lyd_create_any(snode, NULL, NULL, 0, 1, 0, node); |
1255 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1256 | 0 | break; |
1257 | 0 | case LYJSON_ARRAY: |
1258 | | /* skip until the array end */ |
1259 | 0 | in_start = *lydctx->jsonctx->in; |
1260 | 0 | LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup); |
1261 | | |
1262 | | /* return back by all the WS */ |
1263 | 0 | end = lydctx->jsonctx->in->current; |
1264 | 0 | while (is_jsonws(end[-1])) { |
1265 | 0 | --end; |
1266 | 0 | } |
1267 | | |
1268 | | /* make a copy of the whole array and store it */ |
1269 | 0 | if (asprintf(&val, "[%.*s", (int)(end - in_start.current), in_start.current) == -1) { |
1270 | 0 | LOGMEM(lydctx->jsonctx->ctx); |
1271 | 0 | rc = LY_EMEM; |
1272 | 0 | goto cleanup; |
1273 | 0 | } |
1274 | 0 | r = lyd_create_any(snode, NULL, val, LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST, 1, 0, node); |
1275 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1276 | 0 | val = NULL; |
1277 | 0 | break; |
1278 | 0 | case LYJSON_STRING: |
1279 | | /* string value */ |
1280 | 0 | if (lydctx->jsonctx->dynamic) { |
1281 | 0 | LY_CHECK_GOTO(rc = lyd_create_any(snode, NULL, lydctx->jsonctx->value, |
1282 | 0 | LYD_VALHINT_STRING | LYD_VALHINT_NUM64, 1, 0, node), cleanup); |
1283 | 0 | lydctx->jsonctx->dynamic = 0; |
1284 | 0 | } else { |
1285 | 0 | val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); |
1286 | 0 | LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup); |
1287 | |
|
1288 | 0 | r = lyd_create_any(snode, NULL, val, LYD_VALHINT_STRING | LYD_VALHINT_NUM64, 1, 0, node); |
1289 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1290 | 0 | val = NULL; |
1291 | 0 | } |
1292 | 0 | break; |
1293 | 0 | case LYJSON_NUMBER: |
1294 | 0 | case LYJSON_FALSE: |
1295 | 0 | case LYJSON_TRUE: |
1296 | | /* JSON value */ |
1297 | 0 | assert(!lydctx->jsonctx->dynamic); |
1298 | 0 | val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); |
1299 | 0 | LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup); |
1300 | |
|
1301 | 0 | r = lyd_create_any(snode, NULL, val, (*status == LYJSON_NUMBER) ? LYD_VALHINT_DECNUM : LYD_VALHINT_BOOLEAN, 1, 0, node); |
1302 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1303 | 0 | val = NULL; |
1304 | 0 | break; |
1305 | 0 | case LYJSON_NULL: |
1306 | | /* no value */ |
1307 | 0 | r = lyd_create_any(snode, NULL, NULL, LYD_VALHINT_EMPTY, 1, 0, node); |
1308 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1309 | 0 | break; |
1310 | 0 | default: |
1311 | 0 | LOGINT(lydctx->jsonctx->ctx); |
1312 | 0 | rc = LY_EINT; |
1313 | 0 | goto cleanup; |
1314 | 0 | } |
1315 | | |
1316 | | /* insert, needs LYD_EXT flag */ |
1317 | 0 | if (ext) { |
1318 | 0 | (*node)->flags |= LYD_EXT; |
1319 | 0 | } |
1320 | 0 | r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node); |
1321 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1322 | | |
1323 | | /* parse descendants */ |
1324 | 0 | if (*status == LYJSON_OBJECT) { |
1325 | 0 | if (lydctx->parse_opts & LYD_PARSE_ANYDATA_STRICT) { |
1326 | | /* explicit strict data parsing */ |
1327 | 0 | lydctx->parse_opts |= LYD_PARSE_STRICT; |
1328 | 0 | } else { |
1329 | | /* make the parser process data as opaq nodes */ |
1330 | 0 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
1331 | 0 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
1332 | 0 | } |
1333 | 0 | lydctx->parse_opts |= (ext ? LYD_PARSE_ONLY : 0); |
1334 | 0 | lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; |
1335 | 0 | lydctx->any_schema = snode; |
1336 | | |
1337 | | /* process the anydata content */ |
1338 | 0 | do { |
1339 | 0 | r = lydjson_subtree_r(lydctx, *node, &((struct lyd_node_any *)*node)->child, NULL); |
1340 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1341 | |
|
1342 | 0 | *status = lyjson_ctx_status(lydctx->jsonctx); |
1343 | 0 | } while (*status == LYJSON_OBJECT_NEXT); |
1344 | | |
1345 | | /* finish linking metadata */ |
1346 | 0 | r = lydjson_metadata_finish(lydctx, &((struct lyd_node_any *)*node)->child); |
1347 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1348 | 0 | } |
1349 | | |
1350 | 0 | cleanup: |
1351 | 0 | lydctx->parse_opts = prev_parse_opts; |
1352 | 0 | lydctx->int_opts = prev_int_opts; |
1353 | 0 | lydctx->any_schema = NULL; |
1354 | 0 | free(val); |
1355 | 0 | return rc; |
1356 | 0 | } |
1357 | | |
1358 | | /** |
1359 | | * @brief Parse a single instance of an inner node. |
1360 | | * |
1361 | | * @param[in] lydctx JSON data parser context. |
1362 | | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
1363 | | * @param[in] ext Extension instance of @p snode, if any. |
1364 | | * @param[in,out] parent Parent node, if any. |
1365 | | * @param[in,out] first_p First top-level node, is updated. |
1366 | | * @param[in,out] status JSON parser status, is updated. |
1367 | | * @param[out] node Parsed data (or opaque) node. |
1368 | | * @return LY_SUCCESS if a node was successfully parsed, |
1369 | | * @return LY_ENOT in case of invalid JSON encoding, |
1370 | | * @return LY_ERR on other errors. |
1371 | | */ |
1372 | | static LY_ERR |
1373 | | lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
1374 | | struct lyd_node *parent, struct lyd_node **first_p, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) |
1375 | 0 | { |
1376 | 0 | LY_ERR r, rc = LY_SUCCESS; |
1377 | 0 | uint32_t prev_parse_opts = lydctx->parse_opts; |
1378 | |
|
1379 | 0 | LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT); |
1380 | | |
1381 | | /* create inner node */ |
1382 | 0 | LY_CHECK_RET(lyd_create_inner(snode, node)); |
1383 | | |
1384 | | /* insert, needs LYD_EXT flag */ |
1385 | 0 | if (ext) { |
1386 | 0 | (*node)->flags |= LYD_EXT; |
1387 | 0 | } |
1388 | 0 | r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node); |
1389 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1390 | |
|
1391 | 0 | if (ext) { |
1392 | | /* only parse these extension data and validate afterwards */ |
1393 | 0 | lydctx->parse_opts |= LYD_PARSE_ONLY; |
1394 | 0 | } |
1395 | | |
1396 | | /* process children */ |
1397 | 0 | do { |
1398 | 0 | r = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
1399 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1400 | | |
1401 | | /* insert again, node may be a list that had its keys missing */ |
1402 | 0 | r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node); |
1403 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1404 | |
|
1405 | 0 | *status = lyjson_ctx_status(lydctx->jsonctx); |
1406 | 0 | } while (*status == LYJSON_OBJECT_NEXT); |
1407 | | |
1408 | | /* finish linking metadata */ |
1409 | 0 | r = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node)); |
1410 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1411 | |
|
1412 | 0 | if (snode->nodetype == LYS_LIST) { |
1413 | | /* check all keys exist */ |
1414 | 0 | r = lyd_parser_check_keys(*node); |
1415 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1416 | 0 | } |
1417 | | |
1418 | 0 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) { |
1419 | | /* new node validation */ |
1420 | 0 | r = lyd_parser_validate_new_implicit((struct lyd_ctx *)lydctx, *node); |
1421 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1422 | 0 | } |
1423 | | |
1424 | 0 | cleanup: |
1425 | 0 | lydctx->parse_opts = prev_parse_opts; |
1426 | 0 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
1427 | 0 | lyd_parser_node_free(first_p, node); |
1428 | 0 | } |
1429 | 0 | return rc; |
1430 | 0 | } |
1431 | | |
1432 | | /** |
1433 | | * @brief Parse a single instance of a term node. |
1434 | | * |
1435 | | * @param[in] lydctx JSON data parser context. |
1436 | | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
1437 | | * @param[in] ext Extension instance of @p snode, if any. |
1438 | | * @param[in] type_hints Type hints to use. |
1439 | | * @param[in,out] parent Parent node, if any. |
1440 | | * @param[in,out] first_p First top-level node, is updated. |
1441 | | * @param[in,out] status JSON parser status, is updated. |
1442 | | * @param[out] node Parsed data (or opaque) node. |
1443 | | * @return LY_SUCCESS if a node was successfully parsed, |
1444 | | * @return LY_ENOT in case of invalid JSON encoding, |
1445 | | * @return LY_ERR on other errors. |
1446 | | */ |
1447 | | static LY_ERR |
1448 | | lydjson_parse_instance_term(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext, |
1449 | | uint32_t type_hints, struct lyd_node *parent, struct lyd_node **first_p, enum LYJSON_PARSER_STATUS *status, |
1450 | | struct lyd_node **node) |
1451 | 0 | { |
1452 | 0 | LY_ERR r, rc = LY_SUCCESS; |
1453 | |
|
1454 | 0 | assert(snode->nodetype & LYD_NODE_TERM); |
1455 | | |
1456 | 0 | if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && |
1457 | 0 | (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) { |
1458 | 0 | rc = LY_ENOT; |
1459 | 0 | goto cleanup; |
1460 | 0 | } |
1461 | | |
1462 | | /* create terminal node */ |
1463 | 0 | r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, parent, lydctx->jsonctx->value, |
1464 | 0 | (uint64_t)lydctx->jsonctx->value_len * 8, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, |
1465 | 0 | type_hints, node); |
1466 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1467 | | |
1468 | | /* insert, needs LYD_EXT flag */ |
1469 | 0 | if (ext) { |
1470 | 0 | (*node)->flags |= LYD_EXT; |
1471 | 0 | } |
1472 | 0 | r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node); |
1473 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1474 | | |
1475 | | /* move JSON parser */ |
1476 | 0 | if (*status == LYJSON_ARRAY) { |
1477 | | /* only [null], 2 more moves are needed */ |
1478 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, status); |
1479 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1480 | 0 | assert(*status == LYJSON_NULL); |
1481 | | |
1482 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, status); |
1483 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1484 | 0 | assert(*status == LYJSON_ARRAY_CLOSED); |
1485 | 0 | } |
1486 | | |
1487 | 0 | cleanup: |
1488 | 0 | return rc; |
1489 | 0 | } |
1490 | | |
1491 | | /** |
1492 | | * @brief Parse a single instance of a node. |
1493 | | * |
1494 | | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
1495 | | * as before calling, despite it is necessary to process input data for checking. |
1496 | | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
1497 | | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
1498 | | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
1499 | | * @param[in] ext Extension instance of @p snode, if any. |
1500 | | * @param[in] name Parsed JSON node name. |
1501 | | * @param[in] name_len Lenght of @p name. |
1502 | | * @param[in] prefix Parsed JSON node prefix. |
1503 | | * @param[in] prefix_len Length of @p prefix. |
1504 | | * @param[in,out] status JSON parser status, is updated. |
1505 | | * @param[out] node Parsed data (or opaque) node. |
1506 | | * @return LY_SUCCESS if a node was successfully parsed, |
1507 | | * @return LY_ENOT in case of invalid JSON encoding, |
1508 | | * @return LY_ERR on other errors. |
1509 | | */ |
1510 | | static LY_ERR |
1511 | | lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, |
1512 | | const struct lysc_node *snode, const struct lysc_ext_instance *ext, const char *name, uint32_t name_len, |
1513 | | const char *prefix, uint32_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) |
1514 | 0 | { |
1515 | 0 | LY_ERR r, rc = LY_SUCCESS; |
1516 | 0 | uint32_t type_hints = 0; |
1517 | |
|
1518 | 0 | r = lydjson_data_check_opaq(lydctx, snode, &type_hints); |
1519 | 0 | if (r == LY_SUCCESS) { |
1520 | 0 | assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY)); |
1521 | 0 | if ((lydctx->parse_opts & LYD_PARSE_JSON_NULL) && (*status == LYJSON_NULL)) { |
1522 | | /* do not do anything if value is JSON 'null' */ |
1523 | 0 | goto cleanup; |
1524 | 0 | } else if (snode->nodetype & LYD_NODE_TERM) { |
1525 | | /* create term node */ |
1526 | 0 | r = lydjson_parse_instance_term(lydctx, snode, ext, type_hints, parent, first_p, status, node); |
1527 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1528 | 0 | } else if (snode->nodetype & LYD_NODE_INNER) { |
1529 | | /* create inner node */ |
1530 | 0 | r = lydjson_parse_instance_inner(lydctx, snode, ext, parent, first_p, status, node); |
1531 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1532 | 0 | } else { |
1533 | | /* create any node */ |
1534 | 0 | r = lydjson_parse_any(lydctx, snode, ext, parent, first_p, status, node); |
1535 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1536 | 0 | } |
1537 | 0 | LY_CHECK_GOTO(!*node, cleanup); |
1538 | | |
1539 | | /* add/correct flags */ |
1540 | 0 | r = lyd_parser_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext); |
1541 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1542 | 0 | } else if (r == LY_ENOT) { |
1543 | | /* parse it again as an opaq node */ |
1544 | 0 | r = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node); |
1545 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1546 | |
|
1547 | 0 | if (snode->nodetype == LYS_LIST) { |
1548 | 0 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST; |
1549 | 0 | } else if (snode->nodetype == LYS_LEAFLIST) { |
1550 | 0 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST; |
1551 | 0 | } |
1552 | 0 | } else { |
1553 | | /* error */ |
1554 | 0 | rc = r; |
1555 | 0 | goto cleanup; |
1556 | 0 | } |
1557 | | |
1558 | 0 | cleanup: |
1559 | 0 | return rc; |
1560 | 0 | } |
1561 | | |
1562 | | /** |
1563 | | * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree. |
1564 | | * |
1565 | | * @param[in] lydctx JSON data parser context. |
1566 | | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
1567 | | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
1568 | | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
1569 | | * @return LY_ERR value. |
1570 | | */ |
1571 | | static LY_ERR |
1572 | | lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
1573 | 0 | { |
1574 | 0 | LY_ERR r, rc = LY_SUCCESS; |
1575 | 0 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx); |
1576 | 0 | const char *name, *prefix = NULL, *expected = NULL; |
1577 | 0 | uint32_t name_len, prefix_len = 0; |
1578 | 0 | ly_bool is_meta = 0; |
1579 | 0 | const struct lysc_node *snode = NULL; |
1580 | 0 | struct lysc_ext_instance *ext = NULL; |
1581 | 0 | struct lyd_node *node = NULL, *attr_node = NULL; |
1582 | 0 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
1583 | 0 | char *value = NULL; |
1584 | |
|
1585 | 0 | assert(parent || first_p); |
1586 | 0 | assert((status == LYJSON_OBJECT) || (status == LYJSON_OBJECT_NEXT)); |
1587 | | |
1588 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, &status); |
1589 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1590 | 0 | if (status == LYJSON_OBJECT_CLOSED) { |
1591 | | /* empty object, fine... */ |
1592 | 0 | goto cleanup; |
1593 | 0 | } |
1594 | | |
1595 | | /* process the node name */ |
1596 | 0 | assert(status == LYJSON_OBJECT_NAME); |
1597 | 0 | lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta); |
1598 | 0 | lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value); |
1599 | |
|
1600 | 0 | if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && !is_meta && name_len && !prefix_len && |
1601 | 0 | !ly_strncmp("eventTime", name, name_len)) { |
1602 | | /* parse eventTime */ |
1603 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, &status); |
1604 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1605 | |
|
1606 | 0 | if (status != LYJSON_STRING) { |
1607 | 0 | LOGVAL(lydctx->jsonctx->ctx, NULL, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", |
1608 | 0 | lyjson_token2str(LYJSON_STRING), lyjson_token2str(status)); |
1609 | 0 | rc = LY_EVALID; |
1610 | 0 | goto cleanup; |
1611 | 0 | } |
1612 | | |
1613 | | /* create node */ |
1614 | 0 | r = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, prefix, prefix_len, |
1615 | 0 | lydctx->jsonctx->value, lydctx->jsonctx->value_len, NULL, LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, &node); |
1616 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1617 | | |
1618 | | /* insert */ |
1619 | 0 | r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, node); |
1620 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1621 | | |
1622 | | /* validate the value */ |
1623 | 0 | r = lyd_parser_notif_eventtime_validate(node); |
1624 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1625 | |
|
1626 | 0 | goto node_parsed; |
1627 | 0 | } else if (!is_meta || name_len || prefix_len) { |
1628 | | /* get the schema node */ |
1629 | 0 | r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext); |
1630 | 0 | if (r == LY_ENOT) { |
1631 | | /* data parsed */ |
1632 | 0 | goto cleanup; |
1633 | 0 | } else if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { |
1634 | 0 | rc = r; |
1635 | | |
1636 | | /* skip the invalid data */ |
1637 | 0 | if ((r = lydjson_data_skip(lydctx->jsonctx))) { |
1638 | 0 | rc = r; |
1639 | 0 | } |
1640 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, &status); |
1641 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1642 | 0 | goto cleanup; |
1643 | 0 | } else if (r) { |
1644 | | /* error */ |
1645 | 0 | rc = r; |
1646 | 0 | goto cleanup; |
1647 | 0 | } |
1648 | 0 | } |
1649 | | |
1650 | 0 | if (is_meta) { |
1651 | | /* parse as metadata */ |
1652 | 0 | if (!name_len && !prefix_len && !parent) { |
1653 | 0 | LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON, |
1654 | 0 | "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries."); |
1655 | 0 | r = LY_EVALID; |
1656 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1657 | 0 | } else if (!name_len && !prefix_len) { |
1658 | | /* parent's metadata without a name - use the schema from the parent */ |
1659 | 0 | attr_node = parent; |
1660 | 0 | snode = attr_node->schema; |
1661 | 0 | } |
1662 | 0 | r = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status, |
1663 | 0 | first_p, &node); |
1664 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1665 | 0 | } else if (!snode) { |
1666 | 0 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
1667 | | /* skip element with children */ |
1668 | 0 | r = lydjson_data_skip(lydctx->jsonctx); |
1669 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1670 | 0 | } else { |
1671 | | /* parse as an opaq node */ |
1672 | | |
1673 | | /* opaq node cannot have an empty string as the name. */ |
1674 | 0 | if (name_len == 0) { |
1675 | 0 | LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_SYNTAX_JSON, "JSON object member name cannot be a zero-length string."); |
1676 | 0 | r = LY_EVALID; |
1677 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1678 | 0 | } |
1679 | | |
1680 | | /* move to the second item in the name/X pair and parse opaq */ |
1681 | 0 | r = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node); |
1682 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1683 | 0 | } |
1684 | 0 | } else { |
1685 | | /* parse as a standard lyd_node but it can still turn out to be an opaque node */ |
1686 | | |
1687 | | /* move to the second item in the name/X pair */ |
1688 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, &status); |
1689 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1690 | | |
1691 | | /* set expected representation */ |
1692 | 0 | switch (snode->nodetype) { |
1693 | 0 | case LYS_LEAFLIST: |
1694 | 0 | expected = "name/array of values"; |
1695 | 0 | break; |
1696 | 0 | case LYS_LIST: |
1697 | 0 | expected = "name/array of objects"; |
1698 | 0 | break; |
1699 | 0 | case LYS_LEAF: |
1700 | 0 | if (status == LYJSON_ARRAY) { |
1701 | 0 | expected = "name/[null]"; |
1702 | 0 | } else { |
1703 | 0 | expected = "name/value"; |
1704 | 0 | } |
1705 | 0 | break; |
1706 | 0 | case LYS_CONTAINER: |
1707 | 0 | case LYS_NOTIF: |
1708 | 0 | case LYS_ACTION: |
1709 | 0 | case LYS_RPC: |
1710 | 0 | case LYS_ANYDATA: |
1711 | 0 | expected = "name/object"; |
1712 | 0 | break; |
1713 | 0 | case LYS_ANYXML: |
1714 | 0 | if (status == LYJSON_ARRAY) { |
1715 | 0 | expected = "name/array"; |
1716 | 0 | } else { |
1717 | 0 | expected = "name/value"; |
1718 | 0 | } |
1719 | 0 | break; |
1720 | 0 | } |
1721 | | |
1722 | | /* check the representation according to the nodetype and then continue with the content */ |
1723 | 0 | switch (snode->nodetype) { |
1724 | 0 | case LYS_LEAFLIST: |
1725 | 0 | case LYS_LIST: |
1726 | 0 | if (status == LYJSON_ARRAY) { |
1727 | | /* process all the values/objects */ |
1728 | 0 | do { |
1729 | | /* move into array/next value */ |
1730 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, &status); |
1731 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1732 | 0 | if (status == LYJSON_ARRAY_CLOSED) { |
1733 | | /* empty array, fine... */ |
1734 | 0 | break; |
1735 | 0 | } |
1736 | | |
1737 | 0 | r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, |
1738 | 0 | &status, &node); |
1739 | 0 | if (r == LY_ENOT) { |
1740 | 0 | goto representation_error; |
1741 | 0 | } |
1742 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1743 | | |
1744 | | /* move after the item(s) */ |
1745 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, &status); |
1746 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1747 | 0 | } while (status == LYJSON_ARRAY_NEXT); |
1748 | 0 | } else if (lydctx->parse_opts & LYD_PARSE_OPAQ) { |
1749 | | /* may still be an opaque node */ |
1750 | 0 | r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, |
1751 | 0 | &status, &node); |
1752 | 0 | if (r == LY_ENOT) { |
1753 | 0 | goto representation_error; |
1754 | 0 | } |
1755 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1756 | 0 | } else { |
1757 | 0 | goto representation_error; |
1758 | 0 | } |
1759 | 0 | break; |
1760 | 0 | case LYS_LEAF: |
1761 | 0 | case LYS_CONTAINER: |
1762 | 0 | case LYS_NOTIF: |
1763 | 0 | case LYS_ACTION: |
1764 | 0 | case LYS_RPC: |
1765 | 0 | case LYS_ANYDATA: |
1766 | 0 | case LYS_ANYXML: |
1767 | | /* process the value/object */ |
1768 | 0 | r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, |
1769 | 0 | &status, &node); |
1770 | 0 | if (r == LY_ENOT) { |
1771 | 0 | goto representation_error; |
1772 | 0 | } |
1773 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1774 | |
|
1775 | 0 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
1776 | | /* rememeber the RPC/action/notification */ |
1777 | 0 | lydctx->op_node = node; |
1778 | 0 | } |
1779 | 0 | break; |
1780 | 0 | } |
1781 | 0 | } |
1782 | | |
1783 | 0 | node_parsed: |
1784 | | /* rememeber a successfully parsed node */ |
1785 | 0 | if (parsed && node) { |
1786 | 0 | ly_set_add(parsed, node, 1, NULL); |
1787 | 0 | } |
1788 | | |
1789 | | /* move after the item(s) */ |
1790 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, &status); |
1791 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1792 | | |
1793 | | /* success */ |
1794 | 0 | goto cleanup; |
1795 | | |
1796 | 0 | representation_error: |
1797 | 0 | LOGVAL(ctx, parent, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as name/%s.", |
1798 | 0 | expected, lys_nodetype2str(snode->nodetype), snode->name, lyjson_token2str(status)); |
1799 | 0 | rc = LY_EVALID; |
1800 | 0 | if (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) { |
1801 | | /* try to skip the invalid data */ |
1802 | 0 | if ((r = lydjson_data_skip(lydctx->jsonctx))) { |
1803 | 0 | rc = r; |
1804 | 0 | } |
1805 | 0 | } |
1806 | |
|
1807 | 0 | cleanup: |
1808 | 0 | free(value); |
1809 | 0 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
1810 | 0 | lyd_parser_node_free(first_p, &node); |
1811 | 0 | } |
1812 | 0 | return rc; |
1813 | 0 | } |
1814 | | |
1815 | | /** |
1816 | | * @brief Common start of JSON parser processing different types of the input data. |
1817 | | * |
1818 | | * @param[in] ctx libyang context |
1819 | | * @param[in] schema Schema node of the potential bare value to check. |
1820 | | * @param[in] in Input structure. |
1821 | | * @param[in] parse_opts Options for parser, see @ref dataparseroptions. |
1822 | | * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions. |
1823 | | * @param[out] status_p Initial status of the input structure. |
1824 | | * @param[out] lydctx_p Data parser context to finish validation. |
1825 | | * @return LY_ERR value. |
1826 | | */ |
1827 | | static LY_ERR |
1828 | | lyd_parse_json_init(const struct ly_ctx *ctx, const struct lysc_node *schema, struct ly_in *in, uint32_t parse_opts, |
1829 | | uint32_t val_opts, enum LYJSON_PARSER_STATUS *status_p, struct lyd_json_ctx **lydctx_p) |
1830 | 0 | { |
1831 | 0 | LY_ERR ret = LY_SUCCESS; |
1832 | 0 | struct lyd_json_ctx *lydctx; |
1833 | 0 | enum LYJSON_PARSER_STATUS status; |
1834 | |
|
1835 | 0 | assert(lydctx_p); |
1836 | | |
1837 | | /* init context */ |
1838 | 0 | lydctx = calloc(1, sizeof *lydctx); |
1839 | 0 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
1840 | 0 | lydctx->parse_opts = parse_opts; |
1841 | 0 | lydctx->val_opts = val_opts; |
1842 | 0 | lydctx->free = lyd_json_ctx_free; |
1843 | |
|
1844 | 0 | LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret); |
1845 | 0 | status = lyjson_ctx_status(lydctx->jsonctx); |
1846 | | |
1847 | | /* parse_opts & LYD_PARSE_SUBTREE not implemented */ |
1848 | | /* there are two options: either we want to parse a bare JSON value or a JSON object |
1849 | | * node of the bare JSON value has to have a schema, otherwise we do not know where to put the value |
1850 | | * the only types of nodes that can take on a value are a leaf (number, string or bool) and a leaf-list (array) */ |
1851 | 0 | if (schema && |
1852 | 0 | (((status == LYJSON_ARRAY) && (schema->nodetype & LYS_LEAFLIST)) || |
1853 | 0 | (((status == LYJSON_NUMBER) || (status == LYJSON_STRING) || (status == LYJSON_FALSE) || |
1854 | 0 | (status == LYJSON_TRUE) || (status == LYJSON_NULL) || (status == LYJSON_ARRAY)) && (schema->nodetype & LYS_LEAF)))) { |
1855 | | /* bare value (bare anydata 'value = object' is not supported) */ |
1856 | 0 | } else if (status == LYJSON_OBJECT) { |
1857 | | /* JSON object */ |
1858 | 0 | } else { |
1859 | | /* expecting top-level object or bare value */ |
1860 | 0 | LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON, "Expected top-level JSON object or correct bare value, but %s found.", |
1861 | 0 | lyjson_token2str(status)); |
1862 | 0 | *lydctx_p = NULL; |
1863 | 0 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
1864 | 0 | return LY_EVALID; |
1865 | 0 | } |
1866 | | |
1867 | 0 | *lydctx_p = lydctx; |
1868 | 0 | if (status_p) { |
1869 | 0 | *status_p = status; |
1870 | 0 | } |
1871 | 0 | return LY_SUCCESS; |
1872 | 0 | } |
1873 | | |
1874 | | /** |
1875 | | * @brief Parse a bare JSON value. |
1876 | | * |
1877 | | * @param[in] lydctx Data parser context. |
1878 | | * @param[in,out] status Current status of the data parser context. |
1879 | | * @param[in] parent Parent to connect the parsed nodes to, if any. |
1880 | | * @param[in] schema Optional schema node of the parsed node (mandatory when parsing JSON value fragment). |
1881 | | * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL. |
1882 | | * @return LY_ERR value. |
1883 | | */ |
1884 | | static LY_ERR |
1885 | | lyd_parse_json_bare_value(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status, struct lyd_node *parent, |
1886 | | const struct lysc_node *schema, struct lyd_node **first_p) |
1887 | 0 | { |
1888 | 0 | LY_ERR r, rc = LY_SUCCESS; |
1889 | 0 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
1890 | 0 | struct lyd_node *node = NULL; |
1891 | 0 | const char *expected = NULL; |
1892 | |
|
1893 | 0 | assert(schema); |
1894 | | |
1895 | | /* this branch partly copies the behavior of lydjson_subtree_r() */ |
1896 | | |
1897 | | /* set expected representation */ |
1898 | 0 | switch (schema->nodetype) { |
1899 | 0 | case LYS_LEAFLIST: |
1900 | 0 | expected = "array of values"; |
1901 | 0 | break; |
1902 | 0 | case LYS_LEAF: |
1903 | 0 | if (*status == LYJSON_ARRAY) { |
1904 | 0 | expected = "[null]"; |
1905 | 0 | } else { |
1906 | 0 | expected = "value"; |
1907 | 0 | } |
1908 | 0 | break; |
1909 | 0 | } |
1910 | | |
1911 | | /* check the representation according to the nodetype and then continue with the content */ |
1912 | | /* for now object values are not supported (anydata) */ |
1913 | | /* for now extensions not supported */ |
1914 | 0 | switch (schema->nodetype) { |
1915 | 0 | case LYS_LEAFLIST: |
1916 | 0 | LY_CHECK_GOTO(*status != LYJSON_ARRAY, representation_error); |
1917 | | |
1918 | | /* process all the values/objects */ |
1919 | 0 | do { |
1920 | | /* move into array/next value */ |
1921 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, status); |
1922 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1923 | 0 | if (*status == LYJSON_ARRAY_CLOSED) { |
1924 | | /* empty array, fine... */ |
1925 | 0 | break; |
1926 | 0 | } |
1927 | | |
1928 | 0 | r = lydjson_parse_instance(lydctx, parent, first_p, schema, NULL, schema->name, strlen(schema->name), |
1929 | 0 | NULL, 0, status, &node); |
1930 | 0 | if (r == LY_ENOT) { |
1931 | 0 | goto representation_error; |
1932 | 0 | } |
1933 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1934 | | |
1935 | | /* move after the item(s) */ |
1936 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, status); |
1937 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1938 | 0 | } while (*status == LYJSON_ARRAY_NEXT); |
1939 | | |
1940 | 0 | break; |
1941 | 0 | case LYS_LEAF: |
1942 | | /* process the value/object */ |
1943 | 0 | r = lydjson_parse_instance(lydctx, parent, first_p, schema, NULL, schema->name, strlen(schema->name), |
1944 | 0 | NULL, 0, status, &node); |
1945 | 0 | if (r == LY_ENOT) { |
1946 | 0 | goto representation_error; |
1947 | 0 | } |
1948 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1949 | |
|
1950 | 0 | break; |
1951 | 0 | } |
1952 | | |
1953 | 0 | goto cleanup; |
1954 | | |
1955 | 0 | representation_error: |
1956 | 0 | LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as %s.", |
1957 | 0 | expected, lys_nodetype2str(schema->nodetype), schema->name, lyjson_token2str(*status)); |
1958 | 0 | rc = LY_EVALID; |
1959 | |
|
1960 | 0 | cleanup: |
1961 | 0 | if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
1962 | 0 | lyd_parser_node_free(first_p, &node); |
1963 | 0 | } |
1964 | 0 | return rc; |
1965 | 0 | } |
1966 | | |
1967 | | LY_ERR |
1968 | | lyd_parse_json(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lysc_node *schema, |
1969 | | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts, |
1970 | | struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
1971 | 0 | { |
1972 | 0 | LY_ERR r, rc = LY_SUCCESS; |
1973 | 0 | struct lyd_json_ctx *lydctx = NULL; |
1974 | 0 | enum LYJSON_PARSER_STATUS status = LYJSON_ERROR; |
1975 | |
|
1976 | 0 | rc = lyd_parse_json_init(ctx, schema, in, parse_opts, val_opts, &status, &lydctx); |
1977 | 0 | LY_CHECK_GOTO(rc, cleanup); |
1978 | |
|
1979 | 0 | lydctx->int_opts = int_opts; |
1980 | | |
1981 | | /* find the operation node if it exists already */ |
1982 | 0 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
1983 | |
|
1984 | 0 | if (status != LYJSON_OBJECT) { |
1985 | | /* parse bare JSON value */ |
1986 | 0 | r = lyd_parse_json_bare_value(lydctx, &status, parent, schema, first_p); |
1987 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
1988 | 0 | } else { |
1989 | | /* parse JSON object */ |
1990 | | |
1991 | | /* read subtree(s) */ |
1992 | 0 | do { |
1993 | 0 | r = lydjson_subtree_r(lydctx, parent, first_p, parsed); |
1994 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
1995 | |
|
1996 | 0 | status = lyjson_ctx_status(lydctx->jsonctx); |
1997 | |
|
1998 | 0 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
1999 | 0 | break; |
2000 | 0 | } |
2001 | 0 | } while (status == LYJSON_OBJECT_NEXT); |
2002 | 0 | } |
2003 | | |
2004 | 0 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) { |
2005 | 0 | LOGVAL(ctx, NULL, LYVE_SYNTAX, "Unexpected sibling node."); |
2006 | 0 | r = LY_EVALID; |
2007 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
2008 | 0 | } |
2009 | 0 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
2010 | 0 | LOGVAL(ctx, NULL, LYVE_DATA, "Missing the operation node."); |
2011 | 0 | r = LY_EVALID; |
2012 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
2013 | 0 | } |
2014 | | |
2015 | | /* finish linking metadata */ |
2016 | 0 | r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); |
2017 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
2018 | |
|
2019 | 0 | cleanup: |
2020 | | /* there should be no unresolved types stored */ |
2021 | 0 | assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && |
2022 | 0 | !lydctx->node_when.count)); |
2023 | | |
2024 | 0 | if (rc && (!lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { |
2025 | 0 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
2026 | 0 | } else { |
2027 | | /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
2028 | 0 | lyjson_ctx_free(lydctx->jsonctx); |
2029 | 0 | lydctx->jsonctx = NULL; |
2030 | | |
2031 | | /* set optional lydctx pointer, otherwise free */ |
2032 | 0 | if (lydctx_p) { |
2033 | 0 | *lydctx_p = (struct lyd_ctx *)lydctx; |
2034 | 0 | } else { |
2035 | 0 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
2036 | 0 | } |
2037 | 0 | } |
2038 | 0 | return rc; |
2039 | 0 | } |
2040 | | |
2041 | | /** |
2042 | | * @brief Parse a specific JSON object into an opaque node. |
2043 | | * |
2044 | | * @param[in] jsonctx JSON parser context. |
2045 | | * @param[in] name Name of the object. |
2046 | | * @param[in] module Module name of the object, NULL if none expected. |
2047 | | * @param[out] evnp Parsed envelope (opaque node). |
2048 | | * @return LY_SUCCESS on success. |
2049 | | * @return LY_ENOT if the specified object did not match. |
2050 | | * @return LY_ERR value on error. |
2051 | | */ |
2052 | | static LY_ERR |
2053 | | lydjson_envelope(struct lyjson_ctx *jsonctx, const char *name, const char *module, struct lyd_node **envp) |
2054 | 0 | { |
2055 | 0 | LY_ERR rc = LY_SUCCESS, r; |
2056 | 0 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx); |
2057 | 0 | const char *nam, *prefix; |
2058 | 0 | uint32_t nam_len, prefix_len; |
2059 | 0 | ly_bool is_meta; |
2060 | |
|
2061 | 0 | assert(status == LYJSON_OBJECT); |
2062 | | |
2063 | 0 | *envp = NULL; |
2064 | |
|
2065 | 0 | r = lyjson_ctx_next(jsonctx, &status); |
2066 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
2067 | 0 | if (status == LYJSON_OBJECT_CLOSED) { |
2068 | 0 | LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX, "Empty JSON object."); |
2069 | 0 | rc = LY_EVALID; |
2070 | 0 | goto cleanup; |
2071 | 0 | } |
2072 | | |
2073 | | /* process the name */ |
2074 | 0 | assert(status == LYJSON_OBJECT_NAME); |
2075 | 0 | lydjson_parse_name(jsonctx->value, jsonctx->value_len, &nam, &nam_len, &prefix, &prefix_len, &is_meta); |
2076 | 0 | if (is_meta) { |
2077 | 0 | LOGVAL(jsonctx->ctx, NULL, LYVE_DATA, "Unexpected metadata."); |
2078 | 0 | rc = LY_EVALID; |
2079 | 0 | goto cleanup; |
2080 | 0 | } else if (module && ly_strncmp(module, prefix, prefix_len)) { |
2081 | 0 | LOGVAL(jsonctx->ctx, NULL, LYVE_DATA, "Unexpected module \"%.*s\" instead of \"%s\".", (int)prefix_len, prefix, |
2082 | 0 | module); |
2083 | 0 | rc = LY_EVALID; |
2084 | 0 | goto cleanup; |
2085 | 0 | } else if (ly_strncmp(name, nam, nam_len)) { |
2086 | 0 | LOGVAL(jsonctx->ctx, NULL, LYVE_DATA, "Unexpected object \"%.*s\" instead of \"%s\".", (int)nam_len, nam, name); |
2087 | 0 | rc = LY_EVALID; |
2088 | 0 | goto cleanup; |
2089 | 0 | } |
2090 | | |
2091 | 0 | r = lyjson_ctx_next(jsonctx, &status); |
2092 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
2093 | | |
2094 | | /* create node */ |
2095 | 0 | rc = lyd_create_opaq(jsonctx->ctx, name, strlen(name), prefix, prefix_len, prefix, prefix_len, NULL, 0, NULL, |
2096 | 0 | LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, envp); |
2097 | 0 | LY_CHECK_GOTO(rc, cleanup); |
2098 | |
|
2099 | 0 | cleanup: |
2100 | 0 | if (rc) { |
2101 | 0 | lyd_free_tree(*envp); |
2102 | 0 | *envp = NULL; |
2103 | 0 | } |
2104 | 0 | return rc; |
2105 | 0 | } |
2106 | | |
2107 | | LY_ERR |
2108 | | lyd_parse_json_restconf(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_in *in, |
2109 | | uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, struct lyd_node **envp, struct ly_set *parsed, |
2110 | | struct lyd_ctx **lydctx_p) |
2111 | 0 | { |
2112 | 0 | LY_ERR rc = LY_SUCCESS, r; |
2113 | 0 | struct lyd_json_ctx *lydctx = NULL; |
2114 | 0 | struct lyd_node *node; |
2115 | 0 | uint32_t i, int_opts = 0, close_elem = 0; |
2116 | |
|
2117 | 0 | assert(ctx && in && lydctx_p); |
2118 | 0 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
2119 | 0 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
2120 | | |
2121 | 0 | assert((data_type == LYD_TYPE_RPC_RESTCONF) || (data_type == LYD_TYPE_NOTIF_RESTCONF) || |
2122 | 0 | (data_type == LYD_TYPE_REPLY_RESTCONF)); |
2123 | | |
2124 | | /* init context */ |
2125 | 0 | rc = lyd_parse_json_init(ctx, NULL, in, parse_opts, val_opts, NULL, &lydctx); |
2126 | 0 | LY_CHECK_GOTO(rc, cleanup); |
2127 | |
|
2128 | 0 | switch (data_type) { |
2129 | 0 | case LYD_TYPE_RPC_RESTCONF: |
2130 | 0 | assert(parent); |
2131 | | |
2132 | | /* parse "input" */ |
2133 | 0 | rc = lydjson_envelope(lydctx->jsonctx, "input", lyd_node_module(parent)->name, envp); |
2134 | 0 | LY_CHECK_GOTO(rc, cleanup); |
2135 | |
|
2136 | 0 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION; |
2137 | 0 | close_elem = 1; |
2138 | 0 | break; |
2139 | 0 | case LYD_TYPE_NOTIF_RESTCONF: |
2140 | 0 | assert(!parent); |
2141 | | |
2142 | | /* parse "notification" */ |
2143 | 0 | rc = lydjson_envelope(lydctx->jsonctx, "notification", "ietf-restconf", envp); |
2144 | 0 | LY_CHECK_GOTO(rc, cleanup); |
2145 | | |
2146 | | /* RESTCONF notification and eventTime */ |
2147 | 0 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME; |
2148 | 0 | close_elem = 1; |
2149 | 0 | break; |
2150 | 0 | case LYD_TYPE_REPLY_RESTCONF: |
2151 | 0 | assert(parent); |
2152 | | |
2153 | | /* parse "output" */ |
2154 | 0 | rc = lydjson_envelope(lydctx->jsonctx, "output", lyd_node_module(parent)->name, envp); |
2155 | 0 | LY_CHECK_GOTO(rc, cleanup); |
2156 | |
|
2157 | 0 | int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; |
2158 | 0 | close_elem = 1; |
2159 | 0 | break; |
2160 | 0 | default: |
2161 | 0 | LOGINT(ctx); |
2162 | 0 | rc = LY_EINT; |
2163 | 0 | goto cleanup; |
2164 | 0 | } |
2165 | | |
2166 | 0 | lydctx->int_opts = int_opts; |
2167 | | |
2168 | | /* find the operation node if it exists already */ |
2169 | 0 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
2170 | | |
2171 | | /* read subtree(s) */ |
2172 | 0 | do { |
2173 | 0 | r = lydjson_subtree_r(lydctx, parent, first_p, parsed); |
2174 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
2175 | 0 | } while (lyjson_ctx_status(lydctx->jsonctx) == LYJSON_OBJECT_NEXT); |
2176 | | |
2177 | | /* close all opened elements */ |
2178 | 0 | for (i = 0; i < close_elem; ++i) { |
2179 | 0 | if (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_OBJECT_CLOSED) { |
2180 | 0 | LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_OBJECT_CLOSED), |
2181 | 0 | lyjson_token2str(lyjson_ctx_status(lydctx->jsonctx))); |
2182 | 0 | rc = LY_EVALID; |
2183 | 0 | goto cleanup; |
2184 | 0 | } |
2185 | | |
2186 | 0 | r = lyjson_ctx_next(lydctx->jsonctx, NULL); |
2187 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
2188 | 0 | } |
2189 | | |
2190 | 0 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
2191 | 0 | LOGVAL(ctx, NULL, LYVE_DATA, "Missing the operation node."); |
2192 | 0 | r = LY_EVALID; |
2193 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
2194 | 0 | } |
2195 | 0 | if (int_opts & LYD_INTOPT_EVENTTIME) { |
2196 | | /* parse as a child of the envelope */ |
2197 | 0 | node = (*first_p)->prev; |
2198 | 0 | if (node->schema) { |
2199 | 0 | LOGVAL(ctx, NULL, LYVE_DATA, "Missing notification \"eventTime\" node."); |
2200 | 0 | r = LY_EVALID; |
2201 | 0 | LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); |
2202 | 0 | } else { |
2203 | | /* can be the only opaque node and an operation had to be parsed */ |
2204 | 0 | assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next); |
2205 | 0 | lyd_unlink(node); |
2206 | 0 | assert(*envp); |
2207 | 0 | lyd_insert_child(*envp, node); |
2208 | 0 | } |
2209 | 0 | } |
2210 | | |
2211 | | /* finish linking metadata */ |
2212 | 0 | r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); |
2213 | 0 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
2214 | |
|
2215 | 0 | cleanup: |
2216 | | /* there should be no unres stored if validation should be skipped */ |
2217 | 0 | assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && |
2218 | 0 | !lydctx->node_when.count)); |
2219 | | |
2220 | 0 | if (rc) { |
2221 | 0 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
2222 | 0 | } else { |
2223 | 0 | *lydctx_p = (struct lyd_ctx *)lydctx; |
2224 | | |
2225 | | /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */ |
2226 | 0 | lyjson_ctx_free(lydctx->jsonctx); |
2227 | | lydctx->jsonctx = NULL; |
2228 | 0 | } |
2229 | 0 | return rc; |
2230 | 0 | } |