blob: da255a0243b025d7c0b11072cfea8f62ac2a1dc6 [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON data parser for libyang
5 *
Radek Krejci1798aae2020-07-14 13:26:06 +02006 * Copyright (c) 2020 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include <assert.h>
Michal Vasko90932a92020-02-12 14:33:03 +010018#include <stdlib.h>
19#include <string.h>
20
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020022#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010023#include "context.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020024#include "json.h"
25#include "parser_internal.h"
26#include "tree_data.h"
27#include "tree_data_internal.h"
28#include "tree_schema.h"
29#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010030
31/**
Radek Krejci1798aae2020-07-14 13:26:06 +020032 * @brief Internal context for JSON YANG data parser.
33 *
34 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
35 */
36struct lyd_json_ctx {
37 uint32_t parse_options; /**< various @ref dataparseroptions. */
38 uint32_t validate_options; /**< various @ref datavalidationoptions. */
39 uint32_t int_opts; /**< internal data parser options */
40 uint32_t path_len; /**< used bytes in the path buffer */
41 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
42 struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
43 struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
44 struct ly_set when_check; /**< set of nodes with "when" conditions */
45 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
46
47 /* callbacks */
48 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020049
50 struct lyjson_ctx *jsonctx; /**< JSON context */
51};
52
53/**
54 * @brief Free the JSON data parser context.
55 *
56 * JSON implementation of lyd_ctx_free_clb().
57 */
58static void
59lyd_json_ctx_free(struct lyd_ctx *lydctx)
60{
61 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
62
63 if (lydctx) {
64 lyd_ctx_free(lydctx);
65 lyjson_ctx_free(ctx->jsonctx);
66 free(ctx);
67 }
68}
69
70/**
Radek Krejci1798aae2020-07-14 13:26:06 +020071 * @brief Parse JSON member-name as [\@][prefix:][name]
72 *
Michal Vaskocabe0702020-08-12 10:14:36 +020073 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020074 * prefix - name of the module of the data node
75 * name - name of the data node
76 *
77 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
78 *
79 * @param[in] value String to parse
80 * @param[in] value_len Length of the @p str.
81 * @param[out] name_p Pointer to the beginning of the parsed name.
82 * @param[out] name_len_p Pointer to the length of the parsed name.
83 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
84 * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0.
Michal Vaskocabe0702020-08-12 10:14:36 +020085 * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise.
Radek Krejci1798aae2020-07-14 13:26:06 +020086 */
87static void
Michal Vaskocabe0702020-08-12 10:14:36 +020088lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p,
Radek Krejci857189e2020-09-01 13:26:36 +020089 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020090{
91 const char *name, *prefix = NULL;
92 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +020093 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020094
95 name = memchr(value, ':', value_len);
96 if (name != NULL) {
97 prefix = value;
98 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +020099 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200100 prefix++;
101 }
102 prefix_len = name - prefix;
103 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200104 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200105 } else {
106 name = value;
107 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200108 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200109 name++;
110 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200111 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100112 }
113
Radek Krejci1798aae2020-07-14 13:26:06 +0200114 *name_p = name;
115 *name_len_p = name_len;
116 *prefix_p = prefix;
117 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200118 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200119}
120
121/**
122 * @brief Get correct prefix (module_name) inside the @p node.
123 *
124 * @param[in] node Data node to get inherited prefix.
125 * @param[in] local_prefix Local prefix to replace the inherited prefix.
126 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
127 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
128 * and no context @p node to get inherited prefix.
129 * @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
130 * of no local prefix and no context @p node to get inherited prefix.
131 * @return LY_ERR value.
132 */
133static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200134lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200135 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200136{
137 struct lyd_node_opaq *onode;
138 const char *module_name = NULL;
139
140 assert(prefix_p && prefix_len_p);
141
142 if (local_prefix_len) {
143 *prefix_p = local_prefix;
144 *prefix_len_p = local_prefix_len;
145 return LY_SUCCESS;
146 }
147
148 *prefix_p = NULL;
149 while (node) {
150 if (node->schema) {
151 *prefix_p = node->schema->module->name;
152 break;
153 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200154 onode = (struct lyd_node_opaq *)node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200155 if (onode->prefix.module_name) {
156 *prefix_p = onode->prefix.module_name;
157 break;
158 } else if (onode->prefix.id) {
159 *prefix_p = onode->prefix.id;
160 break;
161 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200162 node = (struct lyd_node *)node->parent;
Radek Krejci1798aae2020-07-14 13:26:06 +0200163 }
164 *prefix_len_p = ly_strlen(module_name);
165
166 return LY_SUCCESS;
167}
168
169/**
170 * @brief Get schema node corresponding to the input parameters.
171 *
172 * @param[in] lydctx JSON data parser context.
173 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
174 * @param[in] prefix Requested node's prefix (module name).
175 * @param[in] prefix_len Length of the @p prefix.
176 * @param[in] name Requested node's name.
177 * @param[in] name_len Length of the @p name.
178 * @param[in] parent Parent of the node beeing processed, can be NULL in case of top-level. Also the opaq node with LYD_NODE_OPAQ_ISENVELOPE hint
179 * is accepted for searching top-level nodes.
180 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
181 * @return LY_SUCCES on success, note that even in this case the returned value of @p snode_p can be NULL, so the data are expected to be parsed as opaq.
182 * @return LY_EVALID on failure, error message is logged
183 * @return LY_ENOT in case the input data are expected to be skipped
184 */
185static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200186lydjson_get_snode(const struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +0200187 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200188{
189 struct lys_module *mod = NULL;
190
191 /* leave if-feature check for validation */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200192 uint32_t getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200193
194 /* init return value */
195 *snode_p = NULL;
196
197 /* get the element module */
Michal Vasko22df3f02020-08-24 13:29:22 +0200198 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200199 if (!prefix_len) {
200 /* opaq parent (envelope) - the second part of the condition */
Michal Vasko22df3f02020-08-24 13:29:22 +0200201 lydjson_get_node_prefix((struct lyd_node *)parent, NULL, 0, &prefix, &prefix_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200202 }
203 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
204 } else if (parent) {
205 if (parent->schema) {
206 mod = parent->schema->module;
207 }
208 } else {
209 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
210 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
211 return LY_EVALID;
212 }
213 if (!mod) {
214 if (lydctx->parse_options & LYD_PARSE_STRICT) {
215 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
216 return LY_EVALID;
217 }
218 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
219 return LY_ENOT;
220 }
221 }
222
Michal Vasko22df3f02020-08-24 13:29:22 +0200223 if (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200224 /* ignore the envelope parent when searchinf for the schema node */
225 parent = NULL;
226 }
227
228 /* get the schema node */
229 if (mod && (!parent || parent->schema)) {
230 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
231 if (!*snode_p) {
232 if (lydctx->parse_options & LYD_PARSE_STRICT) {
233 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
234 name_len, name, mod->name);
235 return LY_EVALID;
236 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
237 /* skip element with children */
238 return LY_ENOT;
239 }
240 } else {
241 /* check that schema node is valid and can be used */
242 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
243 }
244 }
245
246 return LY_SUCCESS;
247}
248
249/**
250 * @brief Skip the currently open JSON object/array
251 * @param[in] jsonctx JSON context with the input data to skip.
252 * @return LY_ERR value.
253 */
254static LY_ERR
255lydjson_data_skip(struct lyjson_ctx *jsonctx)
256{
257 enum LYJSON_PARSER_STATUS status, current;
258 size_t sublevels = 1;
259
260 status = lyjson_ctx_status(jsonctx, 0);
261
262 /* skip after the content */
263 do {
264 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
265 if (current == status) {
266 sublevels++;
267 } else if (current == status + 1) {
268 sublevels--;
269 }
270 } while (current != status + 1 && sublevels);
271 /* open the next sibling */
272 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
273
274 return LY_SUCCESS;
275}
276
277/**
278 * @brief Go through the @p value and find all possible prefixes and store them in @p val_prefs_p [sized array](@ref sizedarrays).
279 *
280 * @param[in] ctx libyang context
281 * @param[in] value Pointer to the beginning of the value to check.
282 * @param[in] value_len Length of the string to examine in @p value.
283 * @param[out] val_prefs_p Pointer to the resulting [sized array](@ref sizedarrays) of found prefixes. NULL in case there are no prefixes.
284 * @return LY_EMEM on memory allocation failure.
285 * @return LY_SUCCESS on success, empty @p val_prefs_p (NULL) is valid result if there are no possible prefixes
286 */
287static LY_ERR
288lydjson_get_value_prefixes(const struct ly_ctx *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs_p)
289{
290 LY_ERR ret;
291 LY_ARRAY_COUNT_TYPE u;
292 uint32_t c;
293 const char *start, *stop;
294 struct ly_prefix *prefixes = NULL;
295 size_t len;
296
297 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
298 size_t bytes;
299 ly_getutf8(&stop, &c, &bytes);
300 if (is_yangidentstartchar(c)) {
301 for (ly_getutf8(&stop, &c, &bytes);
302 is_yangidentchar(c) && (size_t)(stop - value) < value_len;
Radek Krejci1e008d22020-08-17 11:37:37 +0200303 ly_getutf8(&stop, &c, &bytes)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200304 stop = stop - bytes;
305 if (*stop == ':') {
306 /* we have a possible prefix */
307 struct ly_prefix *p = NULL;
308
309 len = stop - start;
310
311 /* check whether we do not already have this prefix stored */
312 LY_ARRAY_FOR(prefixes, u) {
313 if (!ly_strncmp(prefixes[u].id, start, len)) {
314 p = &prefixes[u];
315 break;
316 }
317 }
318 if (!p) {
319 LY_ARRAY_NEW_GOTO(ctx, prefixes, p, ret, error);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200320 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->id), error);
321 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->module_name), error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200322 } /* else the prefix already present */
323 }
324 stop = stop + bytes;
325 }
326 }
327
328 *val_prefs_p = prefixes;
329 return LY_SUCCESS;
330
331error:
332 LY_ARRAY_FOR(prefixes, u) {
333 lydict_remove(ctx, prefixes[u].id);
334 lydict_remove(ctx, prefixes[u].module_name);
335 }
336 LY_ARRAY_FREE(prefixes);
337 return ret;
338}
339
340/**
341 * @brief Check that the input data are parseable as the @p list.
342 *
343 * Checks for all the list's keys. Function does not revert the context state.
344 *
345 * @param[in] jsonctx JSON parser context.
346 * @param[in] list List schema node corresponding to the input data object.
347 * @return LY_SUCCESS in case the data are ok for the @p list
348 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
349 */
350static LY_ERR
351lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
352{
353 LY_ERR ret = LY_SUCCESS;
354 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
355 struct ly_set key_set = {0};
356 const struct lysc_node *snode;
357 uint32_t i, status_count;
358
359 assert(list && (list->nodetype == LYS_LIST));
360 assert(status == LYJSON_OBJECT);
361
362 /* get all keys into a set (keys do not have if-features or anything) */
363 snode = NULL;
364 while ((snode = lys_getnext(snode, list, NULL, LYS_GETNEXT_NOSTATECHECK)) && (snode->flags & LYS_KEY)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200365 ret = ly_set_add(&key_set, (void *)snode, LY_SET_OPT_USEASLIST, NULL);
366 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200367 }
368
369 if (status != LYJSON_OBJECT_EMPTY) {
370 status_count = jsonctx->status.count;
371
372 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
373 const char *name, *prefix;
374 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200375 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200376
377 /* match the key */
378 snode = NULL;
379 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
380
381 if (!is_attr && !prefix) {
382 for (i = 0; i < key_set.count; ++i) {
383 snode = (const struct lysc_node *)key_set.objs[i];
384 if (!ly_strncmp(snode->name, name, name_len)) {
385 break;
386 }
387 }
388 /* go into the item to a) process it as a key or b) start skipping it as another list child */
389 ret = lyjson_ctx_next(jsonctx, &status);
390 LY_CHECK_GOTO(ret, cleanup);
391
392 if (snode) {
393 /* we have the key, validate the value */
394 if (status < LYJSON_NUMBER) {
395 /* not a terminal */
396 ret = LY_ENOT;
397 goto cleanup;
398 }
399
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200400 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 LY_CHECK_GOTO(ret, cleanup);
402
403 /* key with a valid value, remove from the set */
404 ly_set_rm_index(&key_set, i, NULL);
405 }
406 } else {
407 /* start skipping the member we are not interested in */
408 ret = lyjson_ctx_next(jsonctx, &status);
409 LY_CHECK_GOTO(ret, cleanup);
410 }
411 /* move to the next child */
412 while (status_count < jsonctx->status.count) {
413 ret = lyjson_ctx_next(jsonctx, &status);
414 LY_CHECK_GOTO(ret, cleanup);
415 }
416 }
417 }
418
419 if (key_set.count) {
420 /* some keys are missing/did not validate */
421 ret = LY_ENOT;
422 }
423
424cleanup:
425 ly_set_erase(&key_set, NULL);
426 return ret;
427}
428
429/**
430 * @brief Get the hint for the data type parsers according to the current JSON parser context.
431 *
432 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
433 * @param[in,out] status Pointer to the current context status,
434 * in some circumstances the function manipulates with the context so the status is updated.
435 * @param[out] type_hint_p Pointer to the variable to store the result.
436 * @return LY_SUCCESS in case of success.
437 * @return LY_EINVAL in case of invalid context status not referring to a value.
438 */
439static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200440lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200441{
442 *type_hint_p = 0;
443
444 if (*status_p == LYJSON_ARRAY) {
445 /* only [null] */
446 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
447 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
448
449 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
450 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
451
452 *type_hint_p = LYD_NODE_OPAQ_ISEMPTY;
453 } else if (*status_p == LYJSON_STRING) {
454 *type_hint_p = LYD_NODE_OPAQ_ISSTRING;
455 } else if (*status_p == LYJSON_NUMBER) {
456 *type_hint_p = LYD_NODE_OPAQ_ISNUMBER;
457 } else if (*status_p == LYJSON_FALSE || *status_p == LYJSON_TRUE) {
458 *type_hint_p = LYD_NODE_OPAQ_ISBOOLEAN;
459 } else if (*status_p == LYJSON_NULL) {
460 *type_hint_p = 0;
461 } else {
462 return LY_EINVAL;
463 }
464
465 return LY_SUCCESS;
466}
467
468/**
469 * @brief Check in advance if the input data are parsable according to the provided @p snode.
470 *
471 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
472 * is naturally done when the data are really parsed.
473 *
474 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
475 * as before calling, despite it is necessary to process input data for checking.
476 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
477 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
478 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
479 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
480 * @return LY_EINVAL in case of invalid leaf JSON encoding
481 * and they are expected to be parsed as opaq nodes.
482 */
483static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200484lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200485{
486 LY_ERR ret = LY_SUCCESS;
487 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
488 enum LYJSON_PARSER_STATUS status;
489
490 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200491
Michal Vasko32ac9942020-08-12 14:35:12 +0200492 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
493 /* can always be parsed as a data node if we have the schema node */
494 return LY_SUCCESS;
495 }
496
497 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200498 /* backup parser */
499 lyjson_ctx_backup(jsonctx);
500 status = lyjson_ctx_status(jsonctx, 0);
501
Michal Vasko32ac9942020-08-12 14:35:12 +0200502 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
503 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200504 switch (snode->nodetype) {
505 case LYS_LEAFLIST:
506 case LYS_LEAF:
507 /* value may not be valid in which case we parse it as an opaque node */
508 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
509 if (ret) {
510 break;
511 }
512
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200513 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 ret = LY_ENOT;
515 }
516 break;
517 case LYS_LIST:
518 /* lists may not have all its keys */
519 if (lydjson_check_list(jsonctx, snode)) {
520 /* invalid list, parse as opaque if it missing/has invalid some keys */
521 ret = LY_ENOT;
522 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200523 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200524 }
525
526 /* restore parser */
527 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200528 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200529 status = lyjson_ctx_status(jsonctx, 0);
530 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
531 }
532
533 return ret;
534}
535
536/**
537 * @brief Join the forward-referencing metadata with their target data nodes.
538 *
539 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
540 *
541 * @param[in] lydctx JSON data parser context.
542 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
543 * as a starting point to search for the metadata's target data node
544 * @return LY_SUCCESS on success
545 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
546 */
547static LY_ERR
548lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
549{
550 LY_ERR ret = LY_SUCCESS;
551 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200552 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200553 const char *prev = NULL;
554
555 /* finish linking metadata */
556 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200557 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200558 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200559 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200560 const char *name, *prefix;
561 size_t name_len, prefix_len;
562 const struct lysc_node *snode;
563
564 if (attr->schema || meta_container->name[0] != '@') {
565 /* not an opaq metadata node */
566 continue;
567 }
568
569 if (prev != meta_container->name) {
570 /* metas' names are stored in dictionary, so checking pointers must works */
571 lydict_remove(lydctx->jsonctx->ctx, prev);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200572 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 instance = 1;
574 } else {
575 instance++;
576 }
577
578 /* find the correspnding data node */
579 LY_LIST_FOR(start, node) {
580 if (!node->schema) {
581 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vasko22df3f02020-08-24 13:29:22 +0200582 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq *)node)->name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200583 continue;
584 }
585
Michal Vasko22df3f02020-08-24 13:29:22 +0200586 if (((struct lyd_node_opaq *)node)->hint & LYD_NODE_OPAQ_ISLIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200587 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vasko22df3f02020-08-24 13:29:22 +0200588 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200589 ret = LY_EVALID;
590 goto cleanup;
591 }
592
593 /* match */
594 match++;
595 if (match != instance) {
596 continue;
597 }
598
599 LY_LIST_FOR(meta_container->child, meta_iter) {
600 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200601 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200602 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200603 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200604
605 /* get value prefixes */
606 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
607
608 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
609 &dynamic, meta->hint, LYD_JSON, val_prefs, meta->prefix.id, ly_strlen(meta->prefix.id),
610 meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
611 LY_CHECK_GOTO(ret, cleanup);
612 }
613
614 /* done */
615 break;
616 } else {
617 /* this is the second time we are resolving the schema node, so it must succeed,
618 * but remember that snode can be still NULL */
619 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
620 assert(is_attr);
621 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
622 assert(ret == LY_SUCCESS);
623
624 if (snode != node->schema) {
625 continue;
626 }
627
628 /* match */
629 match++;
630 if (match != instance) {
631 continue;
632 }
633
634 LY_LIST_FOR(meta_container->child, meta_iter) {
635 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200636 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200637 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200638 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200639
640 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
641 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200642 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Radek Krejci1798aae2020-07-14 13:26:06 +0200643 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200644 &dynamic, meta->hint, LY_PREF_JSON, NULL, snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200645 LY_CHECK_GOTO(ret, cleanup);
646 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
647 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
648 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
649 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
650 ret = LY_EVALID;
651 goto cleanup;
652 }
653 }
654 /* add/correct flags */
655 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
656
657 /* done */
658 break;
659 }
660 }
661
662 if (match != instance) {
663 /* there is no corresponding data node for the metadata */
664 if (instance > 1) {
665 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
666 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
667 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
668 } else {
669 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
670 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
671 }
672 ret = LY_EVALID;
673 } else {
674 /* remove the opaq attr */
675 if (attr == (*first_p)) {
676 *first_p = attr->next;
677 }
678 lyd_free_tree(attr);
679 }
680 }
681
682cleanup:
683 lydict_remove(lydctx->jsonctx->ctx, prev);
684
685 return ret;
686}
687
688/**
689 * @brief Parse a metadata member.
690 *
691 * @param[in] lydctx JSON data parser context.
692 * @param[in] snode Schema node of the metadata parent.
693 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
694 * so the data node does not exists. In such a case the metadata is stored in the context for the later
695 * processing by lydjson_metadata_finish().
696 * @return LY_SUCCESS on success
697 * @return Various LY_ERR values in case of failure.
698 */
699static LY_ERR
700lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
701{
702 LY_ERR ret = LY_SUCCESS;
703 enum LYJSON_PARSER_STATUS status;
704 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200705 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200706 const char *name, *prefix = NULL;
707 size_t name_len, prefix_len = 0;
708 struct lys_module *mod;
709 struct lyd_meta *meta = NULL;
710 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200711 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200712 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200713 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 uint16_t nodetype;
715
716 assert(snode || node);
717
718 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
719
720 /* move to the second item in the name/X pair */
721 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
722 LY_CHECK_GOTO(ret, cleanup);
723
724 /* check attribute encoding */
725 switch (nodetype) {
726 case LYS_LEAFLIST:
727 expected = "@name/array of objects/nulls";
728
729 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
730
731next_entry:
732 instance++;
733
734 /* move into array / next entry */
735 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
736 LY_CHECK_GOTO(ret, cleanup);
737
738 if (status == LYJSON_ARRAY_CLOSED) {
739 /* we are done, move after the array */
740 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
741 goto cleanup;
742 }
743 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
744
745 if (!node || node->schema != prev->schema) {
746 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200747 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 instance, prev->schema->module->name, prev->schema->name);
749 ret = LY_EVALID;
750 goto cleanup;
751 }
752
753 if (status == LYJSON_NULL) {
754 /* continue with the next entry in the leaf-list array */
755 prev = node;
756 node = node->next;
757 goto next_entry;
758 }
759 break;
760 case LYS_LEAF:
761 case LYS_ANYXML:
762 expected = "@name/object";
763
764 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
765 break;
766 case LYS_CONTAINER:
767 case LYS_LIST:
768 case LYS_ANYDATA:
769 case LYS_NOTIF:
770 case LYS_ACTION:
771 case LYS_RPC:
772 in_parent = 1;
773 expected = "@/object";
774 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
775 break;
776 }
777
778 /* process all the members inside a single metadata object */
779 assert(status == LYJSON_OBJECT);
780
781 while (status != LYJSON_OBJECT_CLOSED) {
782 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
783 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200784 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Radek Krejci1798aae2020-07-14 13:26:06 +0200785 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
786 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
787 ret = LY_EVALID;
788 goto cleanup;
789 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200790 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Radek Krejci1798aae2020-07-14 13:26:06 +0200791 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
792 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
793 ret = LY_EVALID;
794 goto cleanup;
795 }
796
797 /* get the element module */
798 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
799 if (!mod) {
800 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200801 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Radek Krejci1798aae2020-07-14 13:26:06 +0200802 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
803 prefix_len, prefix, name_len, name);
804 ret = LY_EVALID;
805 goto cleanup;
806 }
807 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
808 /* skip element with children */
809 ret = lydjson_data_skip(lydctx->jsonctx);
810 LY_CHECK_GOTO(ret, cleanup);
811 status = lyjson_ctx_status(lydctx->jsonctx, 0);
812 /* end of the item */
813 continue;
814 }
815 }
816
817 /* get the value */
818 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
819 LY_CHECK_GOTO(ret, cleanup);
820
821 if (node->schema) {
822 /* create metadata */
823 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200824 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200825 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, 0, LY_PREF_JSON, NULL, snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200826 LY_CHECK_GOTO(ret, cleanup);
827
828 /* add/correct flags */
829 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
830 } else {
831 /* create attribute */
832 struct ly_prefix *val_prefs = NULL;
833 const char *module_name;
834 size_t module_name_len;
835
836 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
837
838 /* get value prefixes */
839 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
840
841 /* attr2 is always changed to the created attribute */
842 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
843 &lydctx->jsonctx->dynamic, 0, LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len);
844 LY_CHECK_GOTO(ret, cleanup);
845 }
846 /* next member */
847 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
848 LY_CHECK_GOTO(ret, cleanup);
849 }
850
851 if (nodetype == LYS_LEAFLIST) {
852 /* continue by processing another metadata object for the following
853 * leaf-list instance since they are allways instantiated in JSON array */
854 prev = node;
855 node = node->next;
856 goto next_entry;
857 }
858
859 /* move after the metadata */
860 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
861 LY_CHECK_GOTO(ret, cleanup);
862
863cleanup:
864 return ret;
865
866representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200867 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Radek Krejci1798aae2020-07-14 13:26:06 +0200868 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
Michal Vasko22df3f02020-08-24 13:29:22 +0200869 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name,
Radek Krejci1798aae2020-07-14 13:26:06 +0200870 expected, lyjson_token2str(status), in_parent ? "" : "name");
871
872 return LY_EVALID;
873}
874
875/**
876 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing to the first child node.
877 *
878 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
879 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
880 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
881 */
882static void
883lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
884{
885 if (*node_p) {
886 /* insert, keep first pointer correct */
887 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
888 if (first_p) {
889 if (parent) {
890 *first_p = parent->child;
891 } else {
892 while ((*first_p)->prev->next) {
893 *first_p = (*first_p)->prev;
894 }
895 }
896 }
897 *node_p = NULL;
898 }
899}
900
901static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
902
903/**
904 * @brief Parse opaq node from the input.
905 *
906 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
907 *
908 * @param[in] lydctx JSON data parser context.
909 * @param[in] name Name of the opaq node to create.
910 * @param[in] name_len Length of the @p name string.
911 * @param[in] prefix Prefix of the opaq node to create.
912 * @param[in] prefix_len Length of the @p prefx string.
913 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
914 * but must be set if @p first is not.
915 * @param[in,out] status_p Pointer to the current status of the parser context,
916 * since the function manipulates with the context and process the input, the status can be updated.
917 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
918 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
919 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
920 * @param[out] node_p Pointer to the created opaq node.
921 * @return LY_ERR value.
922 */
923static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200924lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200925 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
926 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200927{
928 LY_ERR ret = LY_SUCCESS;
929 const char *value = NULL, *module_name;
930 size_t value_len = 0, module_name_len = 0;
931 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200932 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200933 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200934
935 if (*status_inner_p != LYJSON_OBJECT && *status_inner_p != LYJSON_OBJECT_EMPTY) {
936 /* prepare for creating opaq node with a value */
937 value = lydctx->jsonctx->value;
938 value_len = lydctx->jsonctx->value_len;
939 dynamic = lydctx->jsonctx->dynamic;
940 lydctx->jsonctx->dynamic = 0;
941
942 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
943
944 if (value) {
945 /* get value prefixes */
946 LY_CHECK_RET(lydjson_get_value_prefixes(lydctx->jsonctx->ctx, value, value_len, &val_prefs));
947 }
948 }
949
950 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200951 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200952 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, type_hint,
953 LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len, node_p);
954 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200955 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200956 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200957 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200958
959 if (*status_p == LYJSON_OBJECT || *status_p == LYJSON_OBJECT_EMPTY) {
960 /* process children */
961 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
962 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
963 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
964 }
965 } else if (*status_p == LYJSON_ARRAY || *status_p == LYJSON_ARRAY_EMPTY) {
966 /* process another instance of the same node */
967 /* but first mark the node to be expected a list or a leaf-list */
Michal Vasko22df3f02020-08-24 13:29:22 +0200968 ((struct lyd_node_opaq *)*node_p)->hint |= LYD_NODE_OPAQ_ISLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200969
970 if (*status_inner_p == LYJSON_OBJECT || *status_inner_p == LYJSON_OBJECT_EMPTY) {
971 /* but first process children of the object in the array */
972 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
973 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
974 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
975 }
976 }
977
978 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
979
980 /* continue with the next instance */
981 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
982 assert(node_p);
983 lydjson_maintain_children(parent, first_p, node_p);
984 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
985 }
986 }
987
988 /* finish linking metadata */
989 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
990
991 /* move after the item */
992 return lyjson_ctx_next(lydctx->jsonctx, status_p);
993}
994
995/**
996 * @brief Process the attribute container (starting by @)
997 *
998 * @param[in] lydctx JSON data parser context.
999 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1000 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1001 * @param[in] name Name of the opaq node to create.
1002 * @param[in] name_len Length of the @p name string.
1003 * @param[in] prefix Prefix of the opaq node to create.
1004 * @param[in] prefix_len Length of the @p prefx string.
1005 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1006 * but must be set if @p first is not.
1007 * @param[in,out] status_p Pointer to the current status of the parser context,
1008 * since the function manipulates with the context and process the input, the status can be updated.
1009 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1010 * @param[out] node_p Pointer to the created opaq node.
1011 * @return LY_ERR value.
1012 */
1013static LY_ERR
1014lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +02001015 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1016 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1017 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001018{
1019 LY_ERR ret = LY_SUCCESS;
1020 enum LYJSON_PARSER_STATUS status_inner;
1021
1022 /* parse as an attribute to a node */
1023 if (!attr_node && snode) {
1024 /* try to find the instance */
1025 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1026 if (iter->schema == snode) {
1027 attr_node = iter;
1028 break;
1029 }
1030 }
1031 }
1032 if (!attr_node) {
1033 /* parse just as an opaq node with the name beginning with @,
1034 * later we have to check that it belongs to a standard node
1035 * and it is supposed to be converted to a metadata */
1036 uint32_t prev_opts;
1037
1038 /* move into metadata */
1039 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1040
1041 if (*status_p == LYJSON_ARRAY) {
1042 /* move into the array */
1043 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1044 } else {
1045 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1046 status_inner = LYJSON_ERROR;
1047 }
1048
1049 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
1050 prev_opts = lydctx->parse_options;
1051 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1052 lydctx->parse_options |= LYD_PARSE_OPAQ;
1053
1054 ret = lydjson_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
1055 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
1056
1057 /* restore the parser options */
1058 lydctx->parse_options = prev_opts;
1059 } else {
1060 ret = lydjson_metadata(lydctx, snode, attr_node);
1061 }
1062
1063 return ret;
1064}
1065
1066/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001067 * @brief Parse a single instance of a node.
1068 *
1069 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1070 * as before calling, despite it is necessary to process input data for checking.
1071 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1072 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1073 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1074 * @param[in] name Parsed JSON node name.
1075 * @param[in] name_len Lenght of @p name.
1076 * @param[in] prefix Parsed JSON node prefix.
1077 * @param[in] prefix_len Length of @p prefix.
1078 * @param[in,out] status JSON parser status, is updated.
1079 * @param[out] node Parsed data (or opaque) node.
1080 * @return LY_SUCCESS if a node was successfully parsed,
1081 * @return LY_EINVAL in case of invalid JSON encoding,
1082 * @return LY_ERR on other errors.
1083 */
1084static LY_ERR
1085lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001086 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1087 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001088{
1089 LY_ERR ret;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001090 uint32_t type_hint;
Michal Vasko32ac9942020-08-12 14:35:12 +02001091 uint32_t prev_opts;
1092 struct lyd_node *tree = NULL;
1093
1094 ret = lydjson_data_check_opaq(lydctx, snode, &type_hint);
1095 if (ret == LY_SUCCESS) {
1096 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1097 if (snode->nodetype & LYD_NODE_TERM) {
1098 /* create terminal node */
1099 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1100 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, type_hint,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001101 LY_PREF_JSON, NULL, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001102 LY_CHECK_RET(ret);
1103
1104 /* move JSON parser */
1105 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1106 LY_CHECK_RET(ret);
1107 } else if (snode->nodetype & LYD_NODE_INNER) {
1108 /* create inner node */
1109 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1110
1111 ret = lyd_create_inner(snode, node);
1112 LY_CHECK_RET(ret);
1113
1114 /* process children */
1115 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1116 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1117 LY_CHECK_RET(ret);
1118 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1119 }
1120
1121 /* finish linking metadata */
1122 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1123 LY_CHECK_RET(ret);
1124
1125 if (snode->nodetype == LYS_LIST) {
1126 /* check all keys exist */
1127 ret = lyd_parse_check_keys(*node);
1128 LY_CHECK_RET(ret);
1129 }
1130
1131 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1132 /* new node validation, autodelete CANNOT occur, all nodes are new */
1133 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1134 LY_CHECK_RET(ret);
1135
1136 /* add any missing default children */
1137 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->unres_node_type,
1138 &lydctx->when_check, (lydctx->validate_options & LYD_VALIDATE_NO_STATE)
1139 ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1140 LY_CHECK_RET(ret);
1141 }
1142
1143 /* move JSON parser */
1144 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1145 LY_CHECK_RET(ret);
1146 } else if (snode->nodetype & LYD_NODE_ANY) {
1147 /* create any node */
1148 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1149
1150 /* parse any data tree with correct options */
1151 /* first backup the current options and then make the parser to process data as opaq nodes */
1152 prev_opts = lydctx->parse_options;
1153 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1154 lydctx->parse_options |= LYD_PARSE_OPAQ;
1155
1156 /* process the anydata content */
1157 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1158 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1159 LY_CHECK_RET(ret);
1160 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1161 }
1162
1163 /* restore parser options */
1164 lydctx->parse_options = prev_opts;
1165
1166 /* finish linking metadata */
1167 ret = lydjson_metadata_finish(lydctx, &tree);
1168 LY_CHECK_RET(ret);
1169
1170 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1171 LY_CHECK_RET(ret);
1172 }
1173 } else if (ret == LY_ENOT) {
1174 /* parse it again as an opaq node */
1175 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
1176 status, status, first_p, node);
1177 LY_CHECK_RET(ret);
1178
1179 if (snode->nodetype & (LYS_LEAFLIST | LYS_LIST)) {
1180 ((struct lyd_node_opaq *)*node)->hint |= LYD_NODE_OPAQ_ISLIST;
1181 }
1182 }
1183
1184 return ret;
1185}
1186
1187/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001188 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001189 *
1190 * @param[in] lydctx JSON data parser context.
1191 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1192 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1193 * @return LY_ERR value.
1194 */
1195static LY_ERR
1196lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1197{
1198 LY_ERR ret = LY_SUCCESS;
1199 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1200 enum LYJSON_PARSER_STATUS status_inner = 0;
1201 const char *name, *prefix = NULL;
1202 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001203 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001204 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001205 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001206 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1207 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001208
1209 assert(parent || first_p);
1210 assert(status == LYJSON_OBJECT);
1211
1212 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001213 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
Radek Krejci1798aae2020-07-14 13:26:06 +02001214
Michal Vaskocabe0702020-08-12 10:14:36 +02001215 if (!is_meta || name_len || prefix_len) {
1216 /* get the schema node */
1217 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1218 if (ret == LY_ENOT) {
1219 /* skip element with children */
1220 ret = lydjson_data_skip(lydctx->jsonctx);
1221 LY_CHECK_GOTO(ret, cleanup);
1222 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1223 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001224 goto cleanup;
1225 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001226 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001227
Michal Vaskocabe0702020-08-12 10:14:36 +02001228 if (!snode) {
1229 /* we will not be parsing it as metadata */
1230 is_meta = 0;
1231 }
1232 }
1233
1234 if (is_meta) {
1235 /* parse as metadata */
1236 if (!name_len && !prefix_len) {
1237 /* parent's metadata without a name - use the schema from the parent */
1238 if (!parent) {
1239 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
1240 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1241 ret = LY_EVALID;
1242 goto cleanup;
1243 }
1244 attr_node = (struct lyd_node *)parent;
1245 snode = attr_node->schema;
1246 }
1247 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
1248 first_p, &node);
1249 LY_CHECK_GOTO(ret, cleanup);
1250 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001251 /* parse as an opaq node */
1252 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1253
1254 /* move to the second item in the name/X pair */
1255 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1256 LY_CHECK_GOTO(ret, cleanup);
1257
1258 if (status == LYJSON_ARRAY) {
1259 /* move into the array */
1260 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1261 LY_CHECK_GOTO(ret, cleanup);
1262 } else {
1263 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1264 status_inner = LYJSON_ERROR;
1265 }
1266
1267 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
1268 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
1269 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001270 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001271 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001272
1273 /* move to the second item in the name/X pair */
1274 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1275 LY_CHECK_GOTO(ret, cleanup);
1276
1277 /* first check the expected representation according to the nodetype and then continue with the content */
1278 switch (snode->nodetype) {
1279 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001280 case LYS_LIST:
1281 if (snode->nodetype == LYS_LEAFLIST) {
1282 expected = "name/array of values";
1283 } else {
1284 expected = "name/array of objects";
1285 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001286
1287 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1288
1289 /* move into array */
1290 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1291 LY_CHECK_GOTO(ret, cleanup);
1292
Michal Vasko32ac9942020-08-12 14:35:12 +02001293 /* process all the values/objects */
1294 do {
1295 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001296
Michal Vasko32ac9942020-08-12 14:35:12 +02001297 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
1298 &status, &node);
1299 if (ret == LY_EINVAL) {
1300 goto representation_error;
1301 } else if (ret) {
1302 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001303 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001304 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001305
Michal Vasko32ac9942020-08-12 14:35:12 +02001306 /* move after the array */
1307 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1308 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001309
1310 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001311 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001312 case LYS_CONTAINER:
1313 case LYS_NOTIF:
1314 case LYS_ACTION:
1315 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001316 case LYS_ANYDATA:
1317 case LYS_ANYXML:
1318 if (snode->nodetype == LYS_LEAF) {
1319 if (status == LYJSON_ARRAY) {
1320 expected = "name/[null]";
1321 } else {
1322 expected = "name/value";
1323 }
1324 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001325 expected = "name/object";
1326 }
1327
Michal Vasko32ac9942020-08-12 14:35:12 +02001328 /* process the value/object */
1329 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1330 if (ret == LY_EINVAL) {
1331 goto representation_error;
1332 } else if (ret) {
1333 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001334 }
1335
Michal Vasko32ac9942020-08-12 14:35:12 +02001336 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001337 /* rememeber the RPC/action/notification */
1338 lydctx->op_node = node;
1339 }
1340
1341 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001342 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001343 }
1344
Michal Vasko32ac9942020-08-12 14:35:12 +02001345 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001346 lydjson_maintain_children(parent, first_p, &node);
1347
1348cleanup:
1349 lyd_free_tree(node);
1350 return ret;
1351
1352representation_error:
1353 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
1354 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1355 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
1356
1357 ret = LY_EVALID;
1358 goto cleanup;
1359}
1360
1361/**
1362 * @brief Common start of JSON parser processing different types of the input data.
1363 *
1364 * @param[in] ctx libyang context
1365 * @param[in] in Input structure.
1366 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1367 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1368 * @param[out] lydctx_p Data parser context to finish validation.
1369 * @return LY_ERR value.
1370 */
1371static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001372lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001373 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001374{
1375 LY_ERR ret = LY_SUCCESS;
1376 struct lyd_json_ctx *lydctx;
1377 size_t i, line = 1;
1378
1379 /* starting top-level */
1380 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1381 if (in->current[i] == 0x0a) { /* new line */
1382 line++;
1383 };
1384 }
1385 if (in->current[i] == '\0') {
1386 /* empty data input */
1387 return LY_SUCCESS;
1388 }
1389 if (in->current[i] != '{') {
1390 LOGVAL(ctx, LY_VLOG_LINE, &line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(&in->current[i]), &in->current[i],
1391 "a top-level JSON object ('{') holding encoded YANG data");
1392 return LY_EVALID;
1393 }
1394
1395 /* init context */
1396 lydctx = calloc(1, sizeof *lydctx);
1397 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1398 lydctx->parse_options = parse_options;
1399 lydctx->validate_options = validate_options;
1400 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001401
1402 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1403
1404 *lydctx_p = lydctx;
1405 return LY_SUCCESS;
1406}
1407
1408LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001409lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001410 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001411{
1412 LY_ERR ret = LY_SUCCESS;
1413 struct lyd_json_ctx *lydctx = NULL;
1414 enum LYJSON_PARSER_STATUS status;
1415
1416 assert(tree_p);
1417 *tree_p = NULL;
1418
1419 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx);
1420 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1421
1422 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1423 assert(status == LYJSON_OBJECT);
1424
1425 /* read subtree(s) */
1426 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1427 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1428 LY_CHECK_GOTO(ret, cleanup);
1429
1430 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1431 }
1432
1433 /* finish linking metadata */
1434 ret = lydjson_metadata_finish(lydctx, tree_p);
1435 LY_CHECK_GOTO(ret, cleanup);
1436
1437cleanup:
1438 /* there should be no unresolved types stored */
1439 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count
Radek Krejci0f969882020-08-21 16:56:47 +02001440 && !lydctx->when_check.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001441
1442 if (ret) {
1443 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1444 lyd_free_all(*tree_p);
1445 *tree_p = NULL;
1446 } else {
1447 *lydctx_p = (struct lyd_ctx *)lydctx;
1448 }
1449
1450 return ret;
1451}
1452
1453/**
1454 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1455 *
1456 * @param[in] jsonctx JSON parser context
1457 * @param[out] envp_p Pointer to the created envelope opaq container.
1458 * @return LY_SUCCESS if the envelope present and successfully parsed.
1459 * @return LY_ENOT in case there is not the expected envelope.
1460 * @return LY_ERR in case of parsing failure.
1461 */
1462static LY_ERR
1463lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p)
1464{
1465 LY_ERR ret = LY_ENOT, r;
1466 const char *name, *prefix, *value = NULL;
1467 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001468 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001469 enum LYJSON_PARSER_STATUS status;
1470 struct lyd_node *et;
1471
1472 *envp_p = NULL;
1473
1474 /* backup the context */
1475 lyjson_ctx_backup(jsonctx);
1476
1477 /* "notification" envelope */
1478 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1479 LY_CHECK_GOTO(is_attr, cleanup);
1480 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1481 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1482
1483 r = lyjson_ctx_next(jsonctx, &status);
1484 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1485 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1486
1487 /* "eventTime" child */
1488 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1489 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1490 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1491
1492 /* go for the data */
1493 r = lyjson_ctx_next(jsonctx, &status);
1494 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1495 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1496
1497 value = jsonctx->value;
1498 value_len = jsonctx->value_len;
1499 dynamic = jsonctx->dynamic;
1500 jsonctx->dynamic = 0;
1501
1502 r = lyjson_ctx_next(jsonctx, &status);
1503 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1504 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1505 /* now the notificationContent is expected, which will be parsed by the caller */
1506
1507 /* create notification envelope */
1508 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LYD_NODE_OPAQ_ISENVELOPE,
1509 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, envp_p);
1510 LY_CHECK_GOTO(ret, cleanup);
1511 /* create notification envelope */
1512 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", 9, value, value_len, &dynamic, LYD_NODE_OPAQ_ISSTRING,
1513 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, &et);
1514 LY_CHECK_GOTO(ret, cleanup);
1515 /* insert eventTime into notification */
1516 lyd_insert_node(*envp_p, NULL, et);
1517
1518 ret = LY_SUCCESS;
1519cleanup:
1520 if (ret) {
1521 /* restore the context */
1522 lyjson_ctx_restore(jsonctx);
1523 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001524 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001525 }
1526 }
1527 return ret;
1528}
1529
1530LY_ERR
1531lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1532{
1533 LY_ERR ret = LY_SUCCESS;
1534 struct lyd_json_ctx *lydctx = NULL;
1535 struct lyd_node *ntf_e = NULL;
1536 struct lyd_node *tree = NULL;
1537 enum LYJSON_PARSER_STATUS status;
1538
1539 /* init */
1540 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1541 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1542 lydctx->int_opts = LYD_INTOPT_NOTIF;
1543
1544 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1545 assert(status == LYJSON_OBJECT);
1546
1547 /* parse "notification" and "eventTime", if present */
1548 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e);
1549 if (ret == LY_ENOT) {
1550 ret = LY_SUCCESS;
1551 } else if (ret) {
1552 goto cleanup;
1553 }
1554
Michal Vaskocf770e22020-08-12 13:21:43 +02001555 /* read subtree */
1556 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1557 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001558
1559 /* finish linking metadata */
1560 ret = lydjson_metadata_finish(lydctx, &tree);
1561 LY_CHECK_GOTO(ret, cleanup);
1562
1563 /* make sure we have parsed some notification */
1564 if (!lydctx->op_node) {
1565 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1566 ret = LY_EVALID;
1567 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001568 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1569 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1570 tree->schema->name);
1571 ret = LY_EVALID;
1572 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001573 }
1574
1575 if (ntf_e) {
1576 /* finish notification envelope */
1577 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1578 LY_CHECK_GOTO(ret, cleanup);
1579 if (status == LYJSON_END) {
1580 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1581 ret = LY_EVALID;
1582 goto cleanup;
1583 } else if (status != LYJSON_OBJECT_CLOSED) {
1584 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
1585 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
1586 ret = LY_EVALID;
1587 goto cleanup;
1588 }
1589 }
1590
1591 if (ntf_p) {
1592 *ntf_p = lydctx->op_node;
1593 }
1594 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001595 if (ntf_e) {
1596 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001597 lyd_insert_node(ntf_e, NULL, tree);
1598 tree = ntf_e;
1599 }
1600 if (tree_p) {
1601 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001602 }
1603
1604cleanup:
1605 /* we have used parse_only flag */
1606 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1607
1608 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1609 if (ret) {
1610 lyd_free_all(tree);
1611 lyd_free_tree(ntf_e);
1612 }
1613 return ret;
1614}
1615
1616/**
1617 * @brief Parse optional JSON envelope around the processed content.
1618 *
1619 * @param[in] jsonctx JSON parser context
1620 * @param[in] parent Parent node (some other envelope).
1621 * @param[in] module_key Name of the module where the envelope element is expected.
1622 * @param[in] object_id Name of the envelope object.
1623 * @param[out] envp_p Pointer to the created envelope opaq container.
1624 * @return LY_SUCCESS if the envelope present and successfully parsed.
1625 * @return LY_ENOT in case there is not the expected envelope.
1626 * @return LY_ERR in case of parsing failure.
1627 */
1628static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001629lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejci0f969882020-08-21 16:56:47 +02001630 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001631{
1632 LY_ERR ret = LY_ENOT, r;
1633 const char *name, *prefix;
1634 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001635 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001636 enum LYJSON_PARSER_STATUS status;
1637
1638 *envp_p = NULL;
1639
1640 /* "id" envelope */
1641 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1642 LY_CHECK_GOTO(is_attr, cleanup);
1643 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1644 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1645 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1646
1647 r = lyjson_ctx_next(jsonctx, &status);
1648 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1649 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1650
1651 /* create the object envelope */
1652 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), "", 0, NULL,
1653 LYD_NODE_OPAQ_ISENVELOPE, LYD_JSON, NULL, NULL, 0,
1654 module_key, ly_strlen(module_key), envp_p);
1655 LY_CHECK_GOTO(ret, cleanup);
1656
1657 if (parent) {
1658 lyd_insert_node(parent, NULL, *envp_p);
1659 }
1660
1661 ret = LY_SUCCESS;
1662cleanup:
1663 return ret;
1664}
1665
1666static LY_ERR
1667lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1668{
1669 enum LYJSON_PARSER_STATUS status;
1670
1671 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1672 if (status == LYJSON_END) {
1673 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1674 return LY_EVALID;
1675 } else if (status != LYJSON_OBJECT_CLOSED) {
1676 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
1677 jsonctx->value_len, jsonctx->value, object_id);
1678 return LY_EVALID;
1679 }
1680 return LY_SUCCESS;
1681}
1682
1683LY_ERR
1684lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1685{
1686 LY_ERR ret = LY_SUCCESS;
1687 struct lyd_json_ctx *lydctx = NULL;
1688 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1689 struct lyd_node *tree = NULL;
1690 enum LYJSON_PARSER_STATUS status;
1691
1692 /* init */
1693 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1694 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1695 lydctx->int_opts = LYD_INTOPT_RPC;
1696
1697 /* process envelope(s), if present */
1698
1699 /* process rpc */
1700 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1701 if (ret == LY_ENOT) {
1702 ret = LY_SUCCESS;
1703 goto parse_content;
1704 } else if (ret) {
1705 goto cleanup;
1706 }
1707 /* process action */
1708 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1709 if (ret == LY_ENOT) {
1710 ret = LY_SUCCESS;
1711 goto parse_content;
1712 } else if (ret) {
1713 goto cleanup;
1714 }
1715
1716parse_content:
1717
1718 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1719 assert(status == LYJSON_OBJECT);
1720
1721 /* read subtree(s) */
Michal Vasko22df3f02020-08-24 13:29:22 +02001722 ret = lydjson_subtree_r(lydctx, act_e ? (struct lyd_node_inner *)act_e : (struct lyd_node_inner *)rpc_e, &tree);
Michal Vaskocf770e22020-08-12 13:21:43 +02001723 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001724
1725 /* finish linking metadata */
1726 ret = lydjson_metadata_finish(lydctx, &tree);
1727 LY_CHECK_GOTO(ret, cleanup);
1728
1729 /* make sure we have parsed some operation */
1730 if (!lydctx->op_node) {
1731 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
1732 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
1733 ret = LY_EVALID;
1734 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001735 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1736 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1737 tree->schema->name);
1738 ret = LY_EVALID;
1739 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001740 }
1741
1742 if (act_e) {
1743 /* finish action envelope */
1744 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action");
1745 LY_CHECK_GOTO(ret, cleanup);
1746 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1747 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
1748 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1749 ret = LY_EVALID;
1750 goto cleanup;
1751 }
1752 }
1753 if (rpc_e) {
1754 /* finish rpc envelope */
1755 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc");
1756 LY_CHECK_GOTO(ret, cleanup);
1757 if (!act_e && lydctx->op_node->schema->nodetype != LYS_RPC) {
1758 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
1759 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1760 ret = LY_EVALID;
1761 goto cleanup;
1762 }
1763 }
1764
1765 if (op_p) {
1766 *op_p = lydctx->op_node;
1767 }
1768 assert(tree);
1769 if (tree_p) {
1770 if (rpc_e) {
1771 *tree_p = rpc_e;
1772 } else if (act_e) {
1773 *tree_p = act_e;
1774 } else {
1775 *tree_p = tree;
1776 }
1777 }
1778
1779cleanup:
1780 /* we have used parse_only flag */
1781 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1782
1783 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1784 if (ret) {
1785 lyd_free_all(tree);
1786 lyd_free_tree(act_e);
1787 lyd_free_tree(rpc_e);
1788 }
1789 return ret;
1790}
1791
1792LY_ERR
1793lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1794{
1795 LY_ERR ret = LY_SUCCESS;
1796 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001797 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001798 enum LYJSON_PARSER_STATUS status;
1799
1800 /* init */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001801 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001802 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1803 lydctx->int_opts = LYD_INTOPT_REPLY;
1804
1805 /* find request OP */
1806 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1807 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1808 break;
1809 }
1810 LYD_TREE_DFS_END(request, req_op);
1811 }
1812 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001813 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001814 ret = LY_EINVAL;
1815 goto cleanup;
1816 }
1817
1818 /* duplicate request OP with parents */
1819 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1820
1821 /* parse "rpc-reply", if any */
1822 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e);
1823 if (ret == LY_ENOT) {
1824 ret = LY_SUCCESS;
1825 } else if (ret) {
1826 goto cleanup;
1827 }
1828
1829 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1830 assert(status == LYJSON_OBJECT);
1831
1832 /* read subtree(s) */
1833 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001834 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op));
Radek Krejci1798aae2020-07-14 13:26:06 +02001835 LY_CHECK_GOTO(ret, cleanup);
1836
1837 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1838 }
1839
1840 /* finish linking metadata */
1841 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1842 LY_CHECK_GOTO(ret, cleanup);
1843
1844 if (rpcr_e) {
1845 /* finish rpc-reply envelope */
1846 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply");
1847 LY_CHECK_GOTO(ret, cleanup);
1848 }
1849
1850 if (op_p) {
1851 *op_p = rep_op;
1852 }
Radek Krejci1e008d22020-08-17 11:37:37 +02001853 for (tree = rep_op; tree->parent; tree = LYD_PARENT(tree)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001854 if (rpcr_e) {
1855 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001856 lyd_insert_node(rpcr_e, NULL, tree);
1857 tree = rpcr_e;
1858 }
1859 if (tree_p) {
1860 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001861 }
1862
1863cleanup:
1864 /* we have used parse_only flag */
1865 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1866
1867 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1868 if (ret) {
1869 lyd_free_all(rep_op);
1870 lyd_free_tree(rpcr_e);
1871 }
1872 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001873}