blob: 37b7e6890a9992d7f5b51b3e2746f8f540faef59 [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>
Radek Krejci47fab892020-11-05 17:02:41 +010018#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010019#include <stdlib.h>
20#include <string.h>
21
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
Michal Vasko90932a92020-02-12 14:33:03 +010023#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020026#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "log.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020028#include "parser_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "set.h"
30#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020031#include "tree_data.h"
32#include "tree_data_internal.h"
33#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020035#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010036
37/**
Radek Krejci1798aae2020-07-14 13:26:06 +020038 * @brief Internal context for JSON YANG data parser.
39 *
40 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
41 */
42struct lyd_json_ctx {
Michal Vaskoe0665742021-02-11 11:08:44 +010043 uint32_t parse_opts; /**< various @ref dataparseroptions. */
44 uint32_t val_opts; /**< various @ref datavalidationoptions. */
Radek Krejci1798aae2020-07-14 13:26:06 +020045 uint32_t int_opts; /**< internal data parser options */
46 uint32_t path_len; /**< used bytes in the path buffer */
47 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vasko49c39d82020-11-06 17:20:27 +010048 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
49 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
50 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci1798aae2020-07-14 13:26:06 +020051 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
52
53 /* callbacks */
54 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020055
56 struct lyjson_ctx *jsonctx; /**< JSON context */
57};
58
59/**
60 * @brief Free the JSON data parser context.
61 *
62 * JSON implementation of lyd_ctx_free_clb().
63 */
64static void
65lyd_json_ctx_free(struct lyd_ctx *lydctx)
66{
67 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
68
69 if (lydctx) {
70 lyd_ctx_free(lydctx);
71 lyjson_ctx_free(ctx->jsonctx);
72 free(ctx);
73 }
74}
75
76/**
Radek Krejci1798aae2020-07-14 13:26:06 +020077 * @brief Parse JSON member-name as [\@][prefix:][name]
78 *
Michal Vaskocabe0702020-08-12 10:14:36 +020079 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020080 * prefix - name of the module of the data node
81 * name - name of the data node
82 *
83 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
84 *
85 * @param[in] value String to parse
86 * @param[in] value_len Length of the @p str.
87 * @param[out] name_p Pointer to the beginning of the parsed name.
88 * @param[out] name_len_p Pointer to the length of the parsed name.
89 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
90 * @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 +020091 * @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 +020092 */
93static void
Michal Vaskocabe0702020-08-12 10:14:36 +020094lydjson_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 +020095 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020096{
97 const char *name, *prefix = NULL;
98 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +020099 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200100
101 name = memchr(value, ':', value_len);
102 if (name != NULL) {
103 prefix = value;
104 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200105 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200106 prefix++;
107 }
108 prefix_len = name - prefix;
109 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200110 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200111 } else {
112 name = value;
113 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200114 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200115 name++;
116 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200117 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100118 }
119
Radek Krejci1798aae2020-07-14 13:26:06 +0200120 *name_p = name;
121 *name_len_p = name_len;
122 *prefix_p = prefix;
123 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200124 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200125}
126
127/**
128 * @brief Get correct prefix (module_name) inside the @p node.
129 *
130 * @param[in] node Data node to get inherited prefix.
131 * @param[in] local_prefix Local prefix to replace the inherited prefix.
132 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
133 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
134 * and no context @p node to get inherited prefix.
135 * @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
136 * of no local prefix and no context @p node to get inherited prefix.
137 * @return LY_ERR value.
138 */
139static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200140lydjson_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 +0200141 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200142{
143 struct lyd_node_opaq *onode;
144 const char *module_name = NULL;
145
146 assert(prefix_p && prefix_len_p);
147
148 if (local_prefix_len) {
149 *prefix_p = local_prefix;
150 *prefix_len_p = local_prefix_len;
151 return LY_SUCCESS;
152 }
153
154 *prefix_p = NULL;
155 while (node) {
156 if (node->schema) {
157 *prefix_p = node->schema->module->name;
158 break;
159 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200160 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100161 if (onode->name.module_name) {
162 *prefix_p = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200163 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100164 } else if (onode->name.prefix) {
165 *prefix_p = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200166 break;
167 }
Michal Vasko9e685082021-01-29 14:49:09 +0100168 node = lyd_parent(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200169 }
170 *prefix_len_p = ly_strlen(module_name);
171
172 return LY_SUCCESS;
173}
174
175/**
176 * @brief Get schema node corresponding to the input parameters.
177 *
178 * @param[in] lydctx JSON data parser context.
179 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
180 * @param[in] prefix Requested node's prefix (module name).
181 * @param[in] prefix_len Length of the @p prefix.
182 * @param[in] name Requested node's name.
183 * @param[in] name_len Length of the @p name.
Michal Vasko2552ea32020-12-08 15:32:34 +0100184 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Radek Krejci1798aae2020-07-14 13:26:06 +0200185 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
186 * @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.
187 * @return LY_EVALID on failure, error message is logged
188 * @return LY_ENOT in case the input data are expected to be skipped
189 */
190static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200191lydjson_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 +0200192 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200193{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100194 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200195 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100196 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200197
198 /* init return value */
199 *snode_p = NULL;
200
Michal Vasko9e685082021-01-29 14:49:09 +0100201 LOG_LOCSET(NULL, &parent->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100202
Radek Krejci1798aae2020-07-14 13:26:06 +0200203 /* get the element module */
Michal Vasko2552ea32020-12-08 15:32:34 +0100204 if (prefix_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200205 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
206 } else if (parent) {
207 if (parent->schema) {
208 mod = parent->schema->module;
209 }
210 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100211 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
Michal Vasko69730152020-10-09 16:30:07 +0200212 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100213 ret = LY_EVALID;
214 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200215 }
216 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100217 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100218 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
219 ret = LY_EVALID;
220 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200221 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100222 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100223 ret = LY_ENOT;
224 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200225 }
226 }
227
Radek Krejci1798aae2020-07-14 13:26:06 +0200228 /* 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) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100232 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100233 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
Michal Vasko69730152020-10-09 16:30:07 +0200234 name_len, name, mod->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100235 ret = LY_EVALID;
236 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100237 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200238 /* skip element with children */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100239 ret = LY_ENOT;
240 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200241 }
242 } else {
243 /* check that schema node is valid and can be used */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100244 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200245 }
246 }
247
Radek Krejci2efc45b2020-12-22 16:25:44 +0100248cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100249 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100250 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200251}
252
253/**
254 * @brief Skip the currently open JSON object/array
255 * @param[in] jsonctx JSON context with the input data to skip.
256 * @return LY_ERR value.
257 */
258static LY_ERR
259lydjson_data_skip(struct lyjson_ctx *jsonctx)
260{
261 enum LYJSON_PARSER_STATUS status, current;
262 size_t sublevels = 1;
263
264 status = lyjson_ctx_status(jsonctx, 0);
265
266 /* skip after the content */
267 do {
268 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
269 if (current == status) {
270 sublevels++;
271 } else if (current == status + 1) {
272 sublevels--;
273 }
Radek Krejci79856c42020-11-19 11:44:08 +0100274 } while (current != status + 1 || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200275 /* open the next sibling */
276 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
277
278 return LY_SUCCESS;
279}
280
281/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200282 * @brief Check that the input data are parseable as the @p list.
283 *
284 * Checks for all the list's keys. Function does not revert the context state.
285 *
286 * @param[in] jsonctx JSON parser context.
287 * @param[in] list List schema node corresponding to the input data object.
288 * @return LY_SUCCESS in case the data are ok for the @p list
289 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
290 */
291static LY_ERR
292lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
293{
294 LY_ERR ret = LY_SUCCESS;
295 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
296 struct ly_set key_set = {0};
297 const struct lysc_node *snode;
298 uint32_t i, status_count;
299
300 assert(list && (list->nodetype == LYS_LIST));
301 assert(status == LYJSON_OBJECT);
302
303 /* get all keys into a set (keys do not have if-features or anything) */
304 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100305 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200306 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200307 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200308 }
309
310 if (status != LYJSON_OBJECT_EMPTY) {
311 status_count = jsonctx->status.count;
312
313 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
314 const char *name, *prefix;
315 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200316 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200317
318 /* match the key */
319 snode = NULL;
320 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
321
322 if (!is_attr && !prefix) {
323 for (i = 0; i < key_set.count; ++i) {
324 snode = (const struct lysc_node *)key_set.objs[i];
325 if (!ly_strncmp(snode->name, name, name_len)) {
326 break;
327 }
328 }
329 /* go into the item to a) process it as a key or b) start skipping it as another list child */
330 ret = lyjson_ctx_next(jsonctx, &status);
331 LY_CHECK_GOTO(ret, cleanup);
332
333 if (snode) {
334 /* we have the key, validate the value */
335 if (status < LYJSON_NUMBER) {
336 /* not a terminal */
337 ret = LY_ENOT;
338 goto cleanup;
339 }
340
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200341 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200342 LY_CHECK_GOTO(ret, cleanup);
343
344 /* key with a valid value, remove from the set */
345 ly_set_rm_index(&key_set, i, NULL);
346 }
347 } else {
348 /* start skipping the member we are not interested in */
349 ret = lyjson_ctx_next(jsonctx, &status);
350 LY_CHECK_GOTO(ret, cleanup);
351 }
352 /* move to the next child */
353 while (status_count < jsonctx->status.count) {
354 ret = lyjson_ctx_next(jsonctx, &status);
355 LY_CHECK_GOTO(ret, cleanup);
356 }
357 }
358 }
359
360 if (key_set.count) {
361 /* some keys are missing/did not validate */
362 ret = LY_ENOT;
363 }
364
365cleanup:
366 ly_set_erase(&key_set, NULL);
367 return ret;
368}
369
370/**
371 * @brief Get the hint for the data type parsers according to the current JSON parser context.
372 *
373 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
374 * @param[in,out] status Pointer to the current context status,
375 * in some circumstances the function manipulates with the context so the status is updated.
376 * @param[out] type_hint_p Pointer to the variable to store the result.
377 * @return LY_SUCCESS in case of success.
378 * @return LY_EINVAL in case of invalid context status not referring to a value.
379 */
380static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200381lydjson_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 +0200382{
383 *type_hint_p = 0;
384
385 if (*status_p == LYJSON_ARRAY) {
386 /* only [null] */
387 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
388 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
389
390 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
391 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
392
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200393 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200394 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200395 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200396 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200397 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200398 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200399 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 } else if (*status_p == LYJSON_NULL) {
401 *type_hint_p = 0;
402 } else {
403 return LY_EINVAL;
404 }
405
406 return LY_SUCCESS;
407}
408
409/**
410 * @brief Check in advance if the input data are parsable according to the provided @p snode.
411 *
412 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
413 * is naturally done when the data are really parsed.
414 *
415 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
416 * as before calling, despite it is necessary to process input data for checking.
417 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
418 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
419 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
420 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
421 * @return LY_EINVAL in case of invalid leaf JSON encoding
422 * and they are expected to be parsed as opaq nodes.
423 */
424static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200425lydjson_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 +0200426{
427 LY_ERR ret = LY_SUCCESS;
428 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
429 enum LYJSON_PARSER_STATUS status;
430
431 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200432
Michal Vasko32ac9942020-08-12 14:35:12 +0200433 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
434 /* can always be parsed as a data node if we have the schema node */
435 return LY_SUCCESS;
436 }
437
Michal Vaskoe0665742021-02-11 11:08:44 +0100438 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200439 /* backup parser */
440 lyjson_ctx_backup(jsonctx);
441 status = lyjson_ctx_status(jsonctx, 0);
442
Michal Vasko32ac9942020-08-12 14:35:12 +0200443 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
444 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200445 switch (snode->nodetype) {
446 case LYS_LEAFLIST:
447 case LYS_LEAF:
448 /* value may not be valid in which case we parse it as an opaque node */
449 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
450 if (ret) {
451 break;
452 }
453
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200454 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200455 ret = LY_ENOT;
456 }
457 break;
458 case LYS_LIST:
459 /* lists may not have all its keys */
460 if (lydjson_check_list(jsonctx, snode)) {
461 /* invalid list, parse as opaque if it missing/has invalid some keys */
462 ret = LY_ENOT;
463 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200464 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 }
466
467 /* restore parser */
468 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200469 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200470 status = lyjson_ctx_status(jsonctx, 0);
471 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
472 }
473
474 return ret;
475}
476
477/**
478 * @brief Join the forward-referencing metadata with their target data nodes.
479 *
480 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
481 *
482 * @param[in] lydctx JSON data parser context.
483 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
484 * as a starting point to search for the metadata's target data node
485 * @return LY_SUCCESS on success
486 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
487 */
488static LY_ERR
489lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
490{
491 LY_ERR ret = LY_SUCCESS;
492 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200493 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200494 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100495 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200496
497 /* finish linking metadata */
498 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200499 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200500 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200501 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200502 const char *name, *prefix;
503 size_t name_len, prefix_len;
504 const struct lysc_node *snode;
505
Michal Vaskoad92b672020-11-12 13:11:31 +0100506 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200507 /* not an opaq metadata node */
508 continue;
509 }
510
Radek Krejciddace2c2021-01-08 11:30:56 +0100511 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100512 log_location_items++;
513
Michal Vaskoad92b672020-11-12 13:11:31 +0100514 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 /* metas' names are stored in dictionary, so checking pointers must works */
516 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100517 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200518 instance = 1;
519 } else {
520 instance++;
521 }
522
523 /* find the correspnding data node */
524 LY_LIST_FOR(start, node) {
525 if (!node->schema) {
526 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100527 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200528 continue;
529 }
530
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200531 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100532 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
533 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200534 ret = LY_EVALID;
535 goto cleanup;
536 }
537
538 /* match */
539 match++;
540 if (match != instance) {
541 continue;
542 }
543
544 LY_LIST_FOR(meta_container->child, meta_iter) {
545 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200546 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100547
Michal Vaskoad92b672020-11-12 13:11:31 +0100548 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
549 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
550 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_PREF_JSON,
551 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200552 LY_CHECK_GOTO(ret, cleanup);
553 }
554
555 /* done */
556 break;
557 } else {
558 /* this is the second time we are resolving the schema node, so it must succeed,
559 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100560 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
561 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200562 assert(is_attr);
563 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
564 assert(ret == LY_SUCCESS);
565
566 if (snode != node->schema) {
567 continue;
568 }
569
570 /* match */
571 match++;
572 if (match != instance) {
573 continue;
574 }
575
576 LY_LIST_FOR(meta_container->child, meta_iter) {
577 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200578 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200579 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200580 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200581
Michal Vaskoad92b672020-11-12 13:11:31 +0100582 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200583 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200584 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100585 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vasko69730152020-10-09 16:30:07 +0200586 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200587 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100588 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100589 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200590 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
Michal Vaskoad92b672020-11-12 13:11:31 +0100591 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", meta->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200592 ret = LY_EVALID;
593 goto cleanup;
594 }
595 }
596 /* add/correct flags */
Michal Vaskoe0665742021-02-11 11:08:44 +0100597 lyd_parse_set_data_flags(node, &lydctx->node_when, &node->meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200598
599 /* done */
600 break;
601 }
602 }
603
604 if (match != instance) {
605 /* there is no corresponding data node for the metadata */
606 if (instance > 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100607 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing %d%s JSON data instance to be coupled with %s metadata.",
608 instance, instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200609 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100610 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
611 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200612 }
613 ret = LY_EVALID;
614 } else {
615 /* remove the opaq attr */
616 if (attr == (*first_p)) {
617 *first_p = attr->next;
618 }
619 lyd_free_tree(attr);
620 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100621
Radek Krejciddace2c2021-01-08 11:30:56 +0100622 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200624 }
625
626cleanup:
627 lydict_remove(lydctx->jsonctx->ctx, prev);
628
Radek Krejciddace2c2021-01-08 11:30:56 +0100629 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200630 return ret;
631}
632
633/**
634 * @brief Parse a metadata member.
635 *
636 * @param[in] lydctx JSON data parser context.
637 * @param[in] snode Schema node of the metadata parent.
638 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
639 * so the data node does not exists. In such a case the metadata is stored in the context for the later
640 * processing by lydjson_metadata_finish().
641 * @return LY_SUCCESS on success
642 * @return Various LY_ERR values in case of failure.
643 */
644static LY_ERR
645lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
646{
647 LY_ERR ret = LY_SUCCESS;
648 enum LYJSON_PARSER_STATUS status;
649 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200650 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200651 const char *name, *prefix = NULL;
652 size_t name_len, prefix_len = 0;
653 struct lys_module *mod;
654 struct lyd_meta *meta = NULL;
655 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200656 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200657 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200658 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200659 uint16_t nodetype;
660
661 assert(snode || node);
662
663 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
664
665 /* move to the second item in the name/X pair */
666 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
667 LY_CHECK_GOTO(ret, cleanup);
668
669 /* check attribute encoding */
670 switch (nodetype) {
671 case LYS_LEAFLIST:
672 expected = "@name/array of objects/nulls";
673
674 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
675
676next_entry:
677 instance++;
678
679 /* move into array / next entry */
680 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
681 LY_CHECK_GOTO(ret, cleanup);
682
683 if (status == LYJSON_ARRAY_CLOSED) {
684 /* we are done, move after the array */
685 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
686 goto cleanup;
687 }
688 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
689
Michal Vasko69730152020-10-09 16:30:07 +0200690 if (!node || (node->schema != prev->schema)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100691 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
Michal Vasko69730152020-10-09 16:30:07 +0200692 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200693 ret = LY_EVALID;
694 goto cleanup;
695 }
696
697 if (status == LYJSON_NULL) {
698 /* continue with the next entry in the leaf-list array */
699 prev = node;
700 node = node->next;
701 goto next_entry;
702 }
703 break;
704 case LYS_LEAF:
705 case LYS_ANYXML:
706 expected = "@name/object";
707
708 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
709 break;
710 case LYS_CONTAINER:
711 case LYS_LIST:
712 case LYS_ANYDATA:
713 case LYS_NOTIF:
714 case LYS_ACTION:
715 case LYS_RPC:
716 in_parent = 1;
717 expected = "@/object";
718 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
719 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200720 default:
721 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200722 }
723
724 /* process all the members inside a single metadata object */
725 assert(status == LYJSON_OBJECT);
726
Radek Krejciddace2c2021-01-08 11:30:56 +0100727 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100728
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 while (status != LYJSON_OBJECT_CLOSED) {
730 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
731 if (!prefix) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100732 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200733 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 ret = LY_EVALID;
735 goto cleanup;
736 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100737 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Michal Vasko69730152020-10-09 16:30:07 +0200738 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200739 ret = LY_EVALID;
740 goto cleanup;
741 }
742
743 /* get the element module */
744 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
745 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100746 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100747 LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
Michal Vasko69730152020-10-09 16:30:07 +0200748 prefix_len, prefix, name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200749 ret = LY_EVALID;
750 goto cleanup;
751 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100752 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 /* skip element with children */
754 ret = lydjson_data_skip(lydctx->jsonctx);
755 LY_CHECK_GOTO(ret, cleanup);
756 status = lyjson_ctx_status(lydctx->jsonctx, 0);
757 /* end of the item */
758 continue;
759 }
760 }
761
762 /* get the value */
763 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
764 LY_CHECK_GOTO(ret, cleanup);
765
766 if (node->schema) {
767 /* create metadata */
768 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200769 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +0200770 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
771 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200772 LY_CHECK_GOTO(ret, cleanup);
773
774 /* add/correct flags */
Michal Vaskoe0665742021-02-11 11:08:44 +0100775 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200776 } else {
777 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 const char *module_name;
779 size_t module_name_len;
780
781 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
782
Radek Krejci1798aae2020-07-14 13:26:06 +0200783 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100784 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
785 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
786 LY_PREF_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200787 LY_CHECK_GOTO(ret, cleanup);
788 }
789 /* next member */
790 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
791 LY_CHECK_GOTO(ret, cleanup);
792 }
793
794 if (nodetype == LYS_LEAFLIST) {
795 /* continue by processing another metadata object for the following
796 * leaf-list instance since they are allways instantiated in JSON array */
797 prev = node;
798 node = node->next;
799 goto next_entry;
800 }
801
802 /* move after the metadata */
803 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
804 LY_CHECK_GOTO(ret, cleanup);
805
Radek Krejci5813c362021-01-05 10:51:57 +0100806 /* success */
807 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200808
809representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100810 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200811 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
Michal Vaskoad92b672020-11-12 13:11:31 +0100812 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name.name,
Michal Vasko69730152020-10-09 16:30:07 +0200813 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200814
Radek Krejci2efc45b2020-12-22 16:25:44 +0100815 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100816
817cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100818 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100819 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200820}
821
822/**
823 * @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.
824 *
825 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
826 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
827 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
828 */
829static void
830lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
831{
832 if (*node_p) {
833 /* insert, keep first pointer correct */
Michal Vasko9e685082021-01-29 14:49:09 +0100834 lyd_insert_node(&parent->node, first_p, *node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200835 if (first_p) {
836 if (parent) {
837 *first_p = parent->child;
838 } else {
839 while ((*first_p)->prev->next) {
840 *first_p = (*first_p)->prev;
841 }
842 }
843 }
844 *node_p = NULL;
845 }
846}
847
Michal Vaskoe0665742021-02-11 11:08:44 +0100848static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
849 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200850
851/**
852 * @brief Parse opaq node from the input.
853 *
854 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
855 *
856 * @param[in] lydctx JSON data parser context.
857 * @param[in] name Name of the opaq node to create.
858 * @param[in] name_len Length of the @p name string.
859 * @param[in] prefix Prefix of the opaq node to create.
860 * @param[in] prefix_len Length of the @p prefx string.
861 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
862 * but must be set if @p first is not.
863 * @param[in,out] status_p Pointer to the current status of the parser context,
864 * since the function manipulates with the context and process the input, the status can be updated.
865 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
866 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
867 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
868 * @param[out] node_p Pointer to the created opaq node.
869 * @return LY_ERR value.
870 */
871static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200872lydjson_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 +0200873 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
874 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200875{
876 LY_ERR ret = LY_SUCCESS;
877 const char *value = NULL, *module_name;
878 size_t value_len = 0, module_name_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200879 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200880 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200881
Michal Vasko69730152020-10-09 16:30:07 +0200882 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200883 /* prepare for creating opaq node with a value */
884 value = lydctx->jsonctx->value;
885 value_len = lydctx->jsonctx->value_len;
886 dynamic = lydctx->jsonctx->dynamic;
887 lydctx->jsonctx->dynamic = 0;
888
889 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
Radek Krejci1798aae2020-07-14 13:26:06 +0200890 }
891
892 /* create node */
Michal Vasko9e685082021-01-29 14:49:09 +0100893 lydjson_get_node_prefix(&parent->node, prefix, prefix_len, &module_name, &module_name_len);
Michal Vasko501af032020-11-11 20:27:44 +0100894 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
895 value_len, &dynamic, LY_PREF_JSON, NULL, type_hint, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200896 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200897 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200898 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200899 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200900
Michal Vasko69730152020-10-09 16:30:07 +0200901 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200902 /* process children */
903 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100904 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200905 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
906 }
Michal Vasko69730152020-10-09 16:30:07 +0200907 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200908 /* process another instance of the same node */
909 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200910 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200911
Michal Vasko69730152020-10-09 16:30:07 +0200912 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200913 /* but first process children of the object in the array */
914 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100915 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200916 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
917 }
918 }
919
920 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
921
922 /* continue with the next instance */
923 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
924 assert(node_p);
925 lydjson_maintain_children(parent, first_p, node_p);
Michal Vaskoe0665742021-02-11 11:08:44 +0100926 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p,
927 first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200928 }
929 }
930
931 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +0100932 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200933
934 /* move after the item */
935 return lyjson_ctx_next(lydctx->jsonctx, status_p);
936}
937
938/**
939 * @brief Process the attribute container (starting by @)
940 *
941 * @param[in] lydctx JSON data parser context.
942 * @param[in] attr_node The data node referenced by the attribute container, if already known.
943 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
944 * @param[in] name Name of the opaq node to create.
945 * @param[in] name_len Length of the @p name string.
946 * @param[in] prefix Prefix of the opaq node to create.
947 * @param[in] prefix_len Length of the @p prefx string.
948 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
949 * but must be set if @p first is not.
950 * @param[in,out] status_p Pointer to the current status of the parser context,
951 * since the function manipulates with the context and process the input, the status can be updated.
952 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
953 * @param[out] node_p Pointer to the created opaq node.
954 * @return LY_ERR value.
955 */
956static LY_ERR
957lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +0100958 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
959 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200960{
961 LY_ERR ret = LY_SUCCESS;
962 enum LYJSON_PARSER_STATUS status_inner;
963
964 /* parse as an attribute to a node */
965 if (!attr_node && snode) {
966 /* try to find the instance */
967 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
968 if (iter->schema == snode) {
969 attr_node = iter;
970 break;
971 }
972 }
973 }
974 if (!attr_node) {
975 /* parse just as an opaq node with the name beginning with @,
976 * later we have to check that it belongs to a standard node
977 * and it is supposed to be converted to a metadata */
978 uint32_t prev_opts;
979
980 /* move into metadata */
981 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
982
983 if (*status_p == LYJSON_ARRAY) {
984 /* move into the array */
985 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
986 } else {
987 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
988 status_inner = LYJSON_ERROR;
989 }
990
991 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +0100992 prev_opts = lydctx->parse_opts;
993 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
994 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +0200995
996 ret = lydjson_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
Michal Vaskoe0665742021-02-11 11:08:44 +0100997 NULL, 0, (struct lyd_node_inner *)parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner,
998 first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200999
1000 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001001 lydctx->parse_opts = prev_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001002 } else {
1003 ret = lydjson_metadata(lydctx, snode, attr_node);
1004 }
1005
1006 return ret;
1007}
1008
1009/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001010 * @brief Parse a single instance of a node.
1011 *
1012 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1013 * as before calling, despite it is necessary to process input data for checking.
1014 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1015 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1016 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1017 * @param[in] name Parsed JSON node name.
1018 * @param[in] name_len Lenght of @p name.
1019 * @param[in] prefix Parsed JSON node prefix.
1020 * @param[in] prefix_len Length of @p prefix.
1021 * @param[in,out] status JSON parser status, is updated.
1022 * @param[out] node Parsed data (or opaque) node.
1023 * @return LY_SUCCESS if a node was successfully parsed,
1024 * @return LY_EINVAL in case of invalid JSON encoding,
1025 * @return LY_ERR on other errors.
1026 */
1027static LY_ERR
1028lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001029 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1030 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001031{
1032 LY_ERR ret;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001033 uint32_t type_hints = 0;
Michal Vasko32ac9942020-08-12 14:35:12 +02001034 uint32_t prev_opts;
1035 struct lyd_node *tree = NULL;
1036
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001037 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001038 if (ret == LY_SUCCESS) {
1039 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1040 if (snode->nodetype & LYD_NODE_TERM) {
1041 /* create terminal node */
1042 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001043 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1044 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001045 LY_CHECK_RET(ret);
1046
1047 /* move JSON parser */
1048 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1049 LY_CHECK_RET(ret);
1050 } else if (snode->nodetype & LYD_NODE_INNER) {
1051 /* create inner node */
1052 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1053
1054 ret = lyd_create_inner(snode, node);
1055 LY_CHECK_RET(ret);
1056
Radek Krejciddace2c2021-01-08 11:30:56 +01001057 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001058
Michal Vasko32ac9942020-08-12 14:35:12 +02001059 /* process children */
1060 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001061 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001062 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001063 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1064 }
1065
1066 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001067 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001068 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001069
1070 if (snode->nodetype == LYS_LIST) {
1071 /* check all keys exist */
1072 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001073 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001074 }
1075
Michal Vaskoe0665742021-02-11 11:08:44 +01001076 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001077 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001078 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001079 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001080
1081 /* add any missing default children */
Michal Vaskoe0665742021-02-11 11:08:44 +01001082 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_types,
1083 &lydctx->node_when, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ?
Michal Vasko69730152020-10-09 16:30:07 +02001084 LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001085 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001086 }
1087
Radek Krejciddace2c2021-01-08 11:30:56 +01001088 LOG_LOCBACK(1, 1, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001089
Michal Vasko32ac9942020-08-12 14:35:12 +02001090 /* move JSON parser */
1091 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1092 LY_CHECK_RET(ret);
1093 } else if (snode->nodetype & LYD_NODE_ANY) {
1094 /* create any node */
1095 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1096
1097 /* parse any data tree with correct options */
1098 /* first backup the current options and then make the parser to process data as opaq nodes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001099 prev_opts = lydctx->parse_opts;
1100 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1101 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vasko32ac9942020-08-12 14:35:12 +02001102
1103 /* process the anydata content */
1104 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001105 ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001106 LY_CHECK_RET(ret);
1107 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1108 }
1109
1110 /* restore parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001111 lydctx->parse_opts = prev_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001112
1113 /* finish linking metadata */
1114 ret = lydjson_metadata_finish(lydctx, &tree);
1115 LY_CHECK_RET(ret);
1116
Michal Vasko366a4a12020-12-04 16:23:57 +01001117 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001118 LY_CHECK_RET(ret);
1119 }
1120 } else if (ret == LY_ENOT) {
1121 /* parse it again as an opaq node */
1122 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001123 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001124 LY_CHECK_RET(ret);
1125
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001126 if (snode->nodetype == LYS_LIST) {
1127 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1128 } else if (snode->nodetype == LYS_LEAFLIST) {
1129 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001130 }
1131 }
1132
1133 return ret;
1134}
1135
1136/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001137 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001138 *
1139 * @param[in] lydctx JSON data parser context.
1140 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1141 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
Michal Vaskoe0665742021-02-11 11:08:44 +01001142 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001143 * @return LY_ERR value.
1144 */
1145static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001146lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Radek Krejci1798aae2020-07-14 13:26:06 +02001147{
1148 LY_ERR ret = LY_SUCCESS;
1149 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1150 enum LYJSON_PARSER_STATUS status_inner = 0;
1151 const char *name, *prefix = NULL;
1152 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001153 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001154 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001155 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001156 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1157 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001158
1159 assert(parent || first_p);
1160 assert(status == LYJSON_OBJECT);
1161
1162 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001163 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 +02001164
Michal Vaskocabe0702020-08-12 10:14:36 +02001165 if (!is_meta || name_len || prefix_len) {
1166 /* get the schema node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001167 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, (struct lyd_node_inner *)parent, &snode);
Michal Vaskocabe0702020-08-12 10:14:36 +02001168 if (ret == LY_ENOT) {
1169 /* skip element with children */
1170 ret = lydjson_data_skip(lydctx->jsonctx);
1171 LY_CHECK_GOTO(ret, cleanup);
1172 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1173 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001174 goto cleanup;
1175 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001176 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001177
Michal Vaskocabe0702020-08-12 10:14:36 +02001178 if (!snode) {
1179 /* we will not be parsing it as metadata */
1180 is_meta = 0;
1181 }
1182 }
1183
1184 if (is_meta) {
1185 /* parse as metadata */
1186 if (!name_len && !prefix_len) {
1187 /* parent's metadata without a name - use the schema from the parent */
1188 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001189 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001190 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001191 ret = LY_EVALID;
1192 goto cleanup;
1193 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001194 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001195 snode = attr_node->schema;
1196 }
1197 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001198 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001199 LY_CHECK_GOTO(ret, cleanup);
1200 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001201 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001202 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001203
1204 /* move to the second item in the name/X pair */
1205 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1206 LY_CHECK_GOTO(ret, cleanup);
1207
1208 if (status == LYJSON_ARRAY) {
1209 /* move into the array */
1210 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1211 LY_CHECK_GOTO(ret, cleanup);
1212 } else {
1213 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1214 status_inner = LYJSON_ERROR;
1215 }
1216
Michal Vaskoe0665742021-02-11 11:08:44 +01001217 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, (struct lyd_node_inner *)parent, &status,
1218 status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001219 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001220 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001221 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001222
1223 /* move to the second item in the name/X pair */
1224 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1225 LY_CHECK_GOTO(ret, cleanup);
1226
1227 /* first check the expected representation according to the nodetype and then continue with the content */
1228 switch (snode->nodetype) {
1229 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001230 case LYS_LIST:
1231 if (snode->nodetype == LYS_LEAFLIST) {
1232 expected = "name/array of values";
1233 } else {
1234 expected = "name/array of objects";
1235 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001236
1237 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1238
1239 /* move into array */
1240 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1241 LY_CHECK_GOTO(ret, cleanup);
1242
Michal Vasko32ac9942020-08-12 14:35:12 +02001243 /* process all the values/objects */
1244 do {
Michal Vaskoe0665742021-02-11 11:08:44 +01001245 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001246
Michal Vaskoe0665742021-02-11 11:08:44 +01001247 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1248 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001249 if (ret == LY_EINVAL) {
1250 goto representation_error;
1251 } else if (ret) {
1252 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001253 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001254 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001255
Michal Vasko32ac9942020-08-12 14:35:12 +02001256 /* move after the array */
1257 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1258 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001259
1260 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001261 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001262 case LYS_CONTAINER:
1263 case LYS_NOTIF:
1264 case LYS_ACTION:
1265 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001266 case LYS_ANYDATA:
1267 case LYS_ANYXML:
1268 if (snode->nodetype == LYS_LEAF) {
1269 if (status == LYJSON_ARRAY) {
1270 expected = "name/[null]";
1271 } else {
1272 expected = "name/value";
1273 }
1274 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001275 expected = "name/object";
1276 }
1277
Michal Vasko32ac9942020-08-12 14:35:12 +02001278 /* process the value/object */
Michal Vaskoe0665742021-02-11 11:08:44 +01001279 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1280 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001281 if (ret == LY_EINVAL) {
1282 goto representation_error;
1283 } else if (ret) {
1284 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001285 }
1286
Michal Vasko32ac9942020-08-12 14:35:12 +02001287 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001288 /* rememeber the RPC/action/notification */
1289 lydctx->op_node = node;
1290 }
1291
1292 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001293 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001294 }
1295
Michal Vasko32ac9942020-08-12 14:35:12 +02001296 /* finally connect the parsed node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001297 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node);
1298
1299 /* rememeber a successfully parsed node */
1300 if (parsed) {
1301 ly_set_add(parsed, node, 1, NULL);
1302 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001303
Radek Krejci5813c362021-01-05 10:51:57 +01001304 /* success */
1305 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001306
1307representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001308 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001309 LOGVAL(ctx, LYVE_SYNTAX_JSON, "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
Michal Vasko69730152020-10-09 16:30:07 +02001310 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001311 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001312 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001313
1314cleanup:
1315 lyd_free_tree(node);
1316 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001317}
1318
1319/**
1320 * @brief Common start of JSON parser processing different types of the input data.
1321 *
1322 * @param[in] ctx libyang context
1323 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001324 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1325 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001326 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001327 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001328 * @return LY_ERR value.
1329 */
1330static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001331lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001332 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001333{
1334 LY_ERR ret = LY_SUCCESS;
1335 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001336 size_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +02001337
Radek Krejcicd5f6222020-11-12 08:54:13 +01001338 assert(lydctx_p);
1339 assert(status);
1340
Radek Krejci1798aae2020-07-14 13:26:06 +02001341 /* init context */
1342 lydctx = calloc(1, sizeof *lydctx);
1343 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001344 lydctx->parse_opts = parse_opts;
1345 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001346 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001347
Radek Krejci284f31f2020-09-18 15:40:29 +02001348 /* starting top-level */
1349 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001350 if (in->current[i] == '\n') {
1351 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001352 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001353 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001354 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001355
Radek Krejci284f31f2020-09-18 15:40:29 +02001356 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001357 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejcibb27df32020-11-13 15:39:16 +01001358 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001359 *lydctx_p = lydctx;
1360 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001361 } else {
1362 /* expecting top-level object */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001363 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.",
Radek Krejcicd5f6222020-11-12 08:54:13 +01001364 lyjson_token2str(*status));
1365 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001366 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001367 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001368 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001369}
1370
1371LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001372lyd_parse_json(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_in *in,
1373 uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001374{
Michal Vaskoe0665742021-02-11 11:08:44 +01001375 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001376 struct lyd_json_ctx *lydctx = NULL;
1377 enum LYJSON_PARSER_STATUS status;
Michal Vaskoe0665742021-02-11 11:08:44 +01001378 uint32_t int_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001379
Michal Vaskoe0665742021-02-11 11:08:44 +01001380 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1381 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001382
Radek Krejcicd5f6222020-11-12 08:54:13 +01001383 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001384
Michal Vaskoe0665742021-02-11 11:08:44 +01001385 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001386 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001387 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1388 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001389 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001390 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1391 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001392 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001393 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1394 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001395 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001396 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1397 break;
1398 default:
1399 LOGINT(ctx);
1400 rc = LY_EINT;
1401 goto cleanup;
1402 }
1403 lydctx->int_opts = int_opts;
1404
1405 /* find the operation node if it exists already */
1406 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1407
Radek Krejci1798aae2020-07-14 13:26:06 +02001408 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001409 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1410 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1411 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001412
1413 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001414
1415 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1416 break;
1417 }
1418 }
1419
1420 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1421 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1422 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1423 rc = LY_EVALID;
1424 goto cleanup;
1425 }
1426 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1427 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1428 rc = LY_EVALID;
1429 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001430 }
1431
1432 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001433 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1434 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001435
1436cleanup:
1437 /* there should be no unresolved types stored */
Michal Vaskoe0665742021-02-11 11:08:44 +01001438 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001439 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001440
Michal Vaskoe0665742021-02-11 11:08:44 +01001441 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001442 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001443 } else {
1444 *lydctx_p = (struct lyd_ctx *)lydctx;
1445 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001446 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001447}