blob: 24f6544142cfc13e748354ed73a32fa58ae5c948 [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 Krejci77114102021-03-10 15:21:57 +010028#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020029#include "parser_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "set.h"
31#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020032#include "tree_data.h"
33#include "tree_data_internal.h"
34#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020036#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010037
38/**
Radek Krejci1798aae2020-07-14 13:26:06 +020039 * @brief Internal context for JSON YANG data parser.
40 *
41 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
42 */
43struct lyd_json_ctx {
Radek Krejcif16e2542021-02-17 15:39:23 +010044 const struct lysc_ext_instance *ext; /**< extension instance possibly changing document root context of the data being parsed */
Michal Vaskoe0665742021-02-11 11:08:44 +010045 uint32_t parse_opts; /**< various @ref dataparseroptions. */
46 uint32_t val_opts; /**< various @ref datavalidationoptions. */
Radek Krejci1798aae2020-07-14 13:26:06 +020047 uint32_t int_opts; /**< internal data parser options */
48 uint32_t path_len; /**< used bytes in the path buffer */
49 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +010050 struct ly_set node_when; /**< set of nodes with "when" conditions */
Michal Vasko49c39d82020-11-06 17:20:27 +010051 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
52 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
Radek Krejci1798aae2020-07-14 13:26:06 +020053 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
54
55 /* callbacks */
56 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020057
58 struct lyjson_ctx *jsonctx; /**< JSON context */
59};
60
61/**
62 * @brief Free the JSON data parser context.
63 *
64 * JSON implementation of lyd_ctx_free_clb().
65 */
66static void
67lyd_json_ctx_free(struct lyd_ctx *lydctx)
68{
69 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
70
71 if (lydctx) {
72 lyd_ctx_free(lydctx);
73 lyjson_ctx_free(ctx->jsonctx);
74 free(ctx);
75 }
76}
77
78/**
Radek Krejci1798aae2020-07-14 13:26:06 +020079 * @brief Parse JSON member-name as [\@][prefix:][name]
80 *
Michal Vaskocabe0702020-08-12 10:14:36 +020081 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020082 * prefix - name of the module of the data node
83 * name - name of the data node
84 *
85 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
86 *
87 * @param[in] value String to parse
88 * @param[in] value_len Length of the @p str.
89 * @param[out] name_p Pointer to the beginning of the parsed name.
90 * @param[out] name_len_p Pointer to the length of the parsed name.
91 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
92 * @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 +020093 * @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 +020094 */
95static void
Michal Vaskocabe0702020-08-12 10:14:36 +020096lydjson_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 +020097 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020098{
99 const char *name, *prefix = NULL;
100 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200101 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200102
103 name = memchr(value, ':', value_len);
104 if (name != NULL) {
105 prefix = value;
106 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200107 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200108 prefix++;
109 }
110 prefix_len = name - prefix;
111 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200112 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200113 } else {
114 name = value;
115 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200116 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200117 name++;
118 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200119 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100120 }
121
Radek Krejci1798aae2020-07-14 13:26:06 +0200122 *name_p = name;
123 *name_len_p = name_len;
124 *prefix_p = prefix;
125 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200126 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200127}
128
129/**
130 * @brief Get correct prefix (module_name) inside the @p node.
131 *
132 * @param[in] node Data node to get inherited prefix.
133 * @param[in] local_prefix Local prefix to replace the inherited prefix.
134 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
135 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
136 * and no context @p node to get inherited prefix.
137 * @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
138 * of no local prefix and no context @p node to get inherited prefix.
139 * @return LY_ERR value.
140 */
141static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200142lydjson_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 +0200143 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200144{
145 struct lyd_node_opaq *onode;
146 const char *module_name = NULL;
147
148 assert(prefix_p && prefix_len_p);
149
150 if (local_prefix_len) {
151 *prefix_p = local_prefix;
152 *prefix_len_p = local_prefix_len;
153 return LY_SUCCESS;
154 }
155
156 *prefix_p = NULL;
157 while (node) {
158 if (node->schema) {
159 *prefix_p = node->schema->module->name;
160 break;
161 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200162 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100163 if (onode->name.module_name) {
164 *prefix_p = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200165 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100166 } else if (onode->name.prefix) {
167 *prefix_p = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200168 break;
169 }
Michal Vasko9e685082021-01-29 14:49:09 +0100170 node = lyd_parent(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200171 }
172 *prefix_len_p = ly_strlen(module_name);
173
174 return LY_SUCCESS;
175}
176
177/**
178 * @brief Get schema node corresponding to the input parameters.
179 *
180 * @param[in] lydctx JSON data parser context.
181 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
182 * @param[in] prefix Requested node's prefix (module name).
183 * @param[in] prefix_len Length of the @p prefix.
184 * @param[in] name Requested node's name.
185 * @param[in] name_len Length of the @p name.
Michal Vasko2552ea32020-12-08 15:32:34 +0100186 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Radek Krejci1798aae2020-07-14 13:26:06 +0200187 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
188 * @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.
189 * @return LY_EVALID on failure, error message is logged
190 * @return LY_ENOT in case the input data are expected to be skipped
191 */
192static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200193lydjson_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 +0200194 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200195{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100196 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200197 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100198 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200199
200 /* init return value */
201 *snode_p = NULL;
202
Michal Vasko9e685082021-01-29 14:49:09 +0100203 LOG_LOCSET(NULL, &parent->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100204
Radek Krejci1798aae2020-07-14 13:26:06 +0200205 /* get the element module */
Michal Vasko2552ea32020-12-08 15:32:34 +0100206 if (prefix_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200207 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
208 } else if (parent) {
209 if (parent->schema) {
210 mod = parent->schema->module;
211 }
212 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100213 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
Radek Krejci422afb12021-03-04 16:38:16 +0100214 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100215 ret = LY_EVALID;
216 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200217 }
218 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100219 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci422afb12021-03-04 16:38:16 +0100220 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100221 ret = LY_EVALID;
222 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200223 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100224 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100225 ret = LY_ENOT;
226 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200227 }
228 }
229
Radek Krejci1798aae2020-07-14 13:26:06 +0200230 /* get the schema node */
231 if (mod && (!parent || parent->schema)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100232 if (!parent && lydctx->ext) {
Radek Krejciba05eab2021-03-10 13:19:29 +0100233 *snode_p = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100234 } else {
235 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
236 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200237 if (!*snode_p) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100238 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100239 if (lydctx->ext) {
240 if (lydctx->ext->argument) {
241 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
Radek Krejci422afb12021-03-04 16:38:16 +0100242 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100243 } else {
244 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
Radek Krejci422afb12021-03-04 16:38:16 +0100245 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100246 }
247 } else {
248 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
Radek Krejci422afb12021-03-04 16:38:16 +0100249 (int)name_len, name, mod->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100250 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100251 ret = LY_EVALID;
252 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100253 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200254 /* skip element with children */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100255 ret = LY_ENOT;
256 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200257 }
258 } else {
259 /* check that schema node is valid and can be used */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100260 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200261 }
262 }
263
Radek Krejci2efc45b2020-12-22 16:25:44 +0100264cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100265 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100266 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200267}
268
269/**
270 * @brief Skip the currently open JSON object/array
271 * @param[in] jsonctx JSON context with the input data to skip.
272 * @return LY_ERR value.
273 */
274static LY_ERR
275lydjson_data_skip(struct lyjson_ctx *jsonctx)
276{
277 enum LYJSON_PARSER_STATUS status, current;
278 size_t sublevels = 1;
279
280 status = lyjson_ctx_status(jsonctx, 0);
281
282 /* skip after the content */
283 do {
284 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
285 if (current == status) {
286 sublevels++;
287 } else if (current == status + 1) {
288 sublevels--;
289 }
Radek Krejci79856c42020-11-19 11:44:08 +0100290 } while (current != status + 1 || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200291 /* open the next sibling */
292 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
293
294 return LY_SUCCESS;
295}
296
297/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200298 * @brief Check that the input data are parseable as the @p list.
299 *
300 * Checks for all the list's keys. Function does not revert the context state.
301 *
302 * @param[in] jsonctx JSON parser context.
303 * @param[in] list List schema node corresponding to the input data object.
304 * @return LY_SUCCESS in case the data are ok for the @p list
305 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
306 */
307static LY_ERR
308lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
309{
310 LY_ERR ret = LY_SUCCESS;
311 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
312 struct ly_set key_set = {0};
313 const struct lysc_node *snode;
314 uint32_t i, status_count;
315
316 assert(list && (list->nodetype == LYS_LIST));
317 assert(status == LYJSON_OBJECT);
318
319 /* get all keys into a set (keys do not have if-features or anything) */
320 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100321 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200322 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200323 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200324 }
325
326 if (status != LYJSON_OBJECT_EMPTY) {
327 status_count = jsonctx->status.count;
328
329 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
330 const char *name, *prefix;
331 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200332 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200333
334 /* match the key */
335 snode = NULL;
336 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
337
338 if (!is_attr && !prefix) {
339 for (i = 0; i < key_set.count; ++i) {
340 snode = (const struct lysc_node *)key_set.objs[i];
341 if (!ly_strncmp(snode->name, name, name_len)) {
342 break;
343 }
344 }
345 /* go into the item to a) process it as a key or b) start skipping it as another list child */
346 ret = lyjson_ctx_next(jsonctx, &status);
347 LY_CHECK_GOTO(ret, cleanup);
348
349 if (snode) {
350 /* we have the key, validate the value */
351 if (status < LYJSON_NUMBER) {
352 /* not a terminal */
353 ret = LY_ENOT;
354 goto cleanup;
355 }
356
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200357 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200358 LY_CHECK_GOTO(ret, cleanup);
359
360 /* key with a valid value, remove from the set */
361 ly_set_rm_index(&key_set, i, NULL);
362 }
363 } else {
364 /* start skipping the member we are not interested in */
365 ret = lyjson_ctx_next(jsonctx, &status);
366 LY_CHECK_GOTO(ret, cleanup);
367 }
368 /* move to the next child */
369 while (status_count < jsonctx->status.count) {
370 ret = lyjson_ctx_next(jsonctx, &status);
371 LY_CHECK_GOTO(ret, cleanup);
372 }
373 }
374 }
375
376 if (key_set.count) {
377 /* some keys are missing/did not validate */
378 ret = LY_ENOT;
379 }
380
381cleanup:
382 ly_set_erase(&key_set, NULL);
383 return ret;
384}
385
386/**
387 * @brief Get the hint for the data type parsers according to the current JSON parser context.
388 *
389 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
390 * @param[in,out] status Pointer to the current context status,
391 * in some circumstances the function manipulates with the context so the status is updated.
392 * @param[out] type_hint_p Pointer to the variable to store the result.
393 * @return LY_SUCCESS in case of success.
394 * @return LY_EINVAL in case of invalid context status not referring to a value.
395 */
396static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200397lydjson_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 +0200398{
399 *type_hint_p = 0;
400
401 if (*status_p == LYJSON_ARRAY) {
402 /* only [null] */
403 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
404 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
405
406 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
407 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
408
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200409 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200410 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200411 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200412 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200413 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200414 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200415 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200416 } else if (*status_p == LYJSON_NULL) {
417 *type_hint_p = 0;
418 } else {
419 return LY_EINVAL;
420 }
421
422 return LY_SUCCESS;
423}
424
425/**
426 * @brief Check in advance if the input data are parsable according to the provided @p snode.
427 *
428 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
429 * is naturally done when the data are really parsed.
430 *
431 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
432 * as before calling, despite it is necessary to process input data for checking.
433 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
434 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
435 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
436 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
437 * @return LY_EINVAL in case of invalid leaf JSON encoding
438 * and they are expected to be parsed as opaq nodes.
439 */
440static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200441lydjson_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 +0200442{
443 LY_ERR ret = LY_SUCCESS;
444 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
445 enum LYJSON_PARSER_STATUS status;
446
447 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200448
Michal Vasko32ac9942020-08-12 14:35:12 +0200449 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
450 /* can always be parsed as a data node if we have the schema node */
451 return LY_SUCCESS;
452 }
453
Michal Vaskoe0665742021-02-11 11:08:44 +0100454 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200455 /* backup parser */
456 lyjson_ctx_backup(jsonctx);
457 status = lyjson_ctx_status(jsonctx, 0);
458
Michal Vasko32ac9942020-08-12 14:35:12 +0200459 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
460 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200461 switch (snode->nodetype) {
462 case LYS_LEAFLIST:
463 case LYS_LEAF:
464 /* value may not be valid in which case we parse it as an opaque node */
465 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
466 if (ret) {
467 break;
468 }
469
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200470 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200471 ret = LY_ENOT;
472 }
473 break;
474 case LYS_LIST:
475 /* lists may not have all its keys */
476 if (lydjson_check_list(jsonctx, snode)) {
477 /* invalid list, parse as opaque if it missing/has invalid some keys */
478 ret = LY_ENOT;
479 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200480 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200481 }
482
483 /* restore parser */
484 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200485 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200486 status = lyjson_ctx_status(jsonctx, 0);
487 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
488 }
489
490 return ret;
491}
492
493/**
494 * @brief Join the forward-referencing metadata with their target data nodes.
495 *
496 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
497 *
498 * @param[in] lydctx JSON data parser context.
499 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
500 * as a starting point to search for the metadata's target data node
501 * @return LY_SUCCESS on success
502 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
503 */
504static LY_ERR
505lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
506{
507 LY_ERR ret = LY_SUCCESS;
508 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200509 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200510 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100511 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200512
513 /* finish linking metadata */
514 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200515 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200516 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200517 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200518 const char *name, *prefix;
519 size_t name_len, prefix_len;
520 const struct lysc_node *snode;
521
Michal Vaskoad92b672020-11-12 13:11:31 +0100522 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200523 /* not an opaq metadata node */
524 continue;
525 }
526
Radek Krejciddace2c2021-01-08 11:30:56 +0100527 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100528 log_location_items++;
529
Michal Vaskoad92b672020-11-12 13:11:31 +0100530 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200531 /* metas' names are stored in dictionary, so checking pointers must works */
532 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100533 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200534 instance = 1;
535 } else {
536 instance++;
537 }
538
539 /* find the correspnding data node */
540 LY_LIST_FOR(start, node) {
541 if (!node->schema) {
542 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100543 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200544 continue;
545 }
546
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200547 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100548 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
549 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200550 ret = LY_EVALID;
551 goto cleanup;
552 }
553
554 /* match */
555 match++;
556 if (match != instance) {
557 continue;
558 }
559
560 LY_LIST_FOR(meta_container->child, meta_iter) {
561 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200562 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100563
Michal Vaskoad92b672020-11-12 13:11:31 +0100564 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
565 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
566 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_PREF_JSON,
567 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200568 LY_CHECK_GOTO(ret, cleanup);
569 }
570
571 /* done */
572 break;
573 } else {
574 /* this is the second time we are resolving the schema node, so it must succeed,
575 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100576 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
577 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 assert(is_attr);
579 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
580 assert(ret == LY_SUCCESS);
581
582 if (snode != node->schema) {
583 continue;
584 }
585
586 /* match */
587 match++;
588 if (match != instance) {
589 continue;
590 }
591
592 LY_LIST_FOR(meta_container->child, meta_iter) {
593 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200594 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200595 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200596 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200597
Michal Vaskoad92b672020-11-12 13:11:31 +0100598 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200599 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200600 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100601 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vasko69730152020-10-09 16:30:07 +0200602 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200603 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100604 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100605 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200606 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
Michal Vaskoad92b672020-11-12 13:11:31 +0100607 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", meta->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200608 ret = LY_EVALID;
609 goto cleanup;
610 }
611 }
612 /* add/correct flags */
Michal Vaskoe0665742021-02-11 11:08:44 +0100613 lyd_parse_set_data_flags(node, &lydctx->node_when, &node->meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200614
615 /* done */
616 break;
617 }
618 }
619
620 if (match != instance) {
621 /* there is no corresponding data node for the metadata */
622 if (instance > 1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing %d%s JSON data instance to be coupled with %s metadata.",
624 instance, instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200625 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100626 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
627 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200628 }
629 ret = LY_EVALID;
630 } else {
631 /* remove the opaq attr */
632 if (attr == (*first_p)) {
633 *first_p = attr->next;
634 }
635 lyd_free_tree(attr);
636 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100637
Radek Krejciddace2c2021-01-08 11:30:56 +0100638 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200640 }
641
642cleanup:
643 lydict_remove(lydctx->jsonctx->ctx, prev);
644
Radek Krejciddace2c2021-01-08 11:30:56 +0100645 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200646 return ret;
647}
648
649/**
650 * @brief Parse a metadata member.
651 *
652 * @param[in] lydctx JSON data parser context.
653 * @param[in] snode Schema node of the metadata parent.
654 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
655 * so the data node does not exists. In such a case the metadata is stored in the context for the later
656 * processing by lydjson_metadata_finish().
657 * @return LY_SUCCESS on success
658 * @return Various LY_ERR values in case of failure.
659 */
660static LY_ERR
661lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
662{
663 LY_ERR ret = LY_SUCCESS;
664 enum LYJSON_PARSER_STATUS status;
665 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200666 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200667 const char *name, *prefix = NULL;
668 size_t name_len, prefix_len = 0;
669 struct lys_module *mod;
670 struct lyd_meta *meta = NULL;
671 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200672 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200673 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200674 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200675 uint16_t nodetype;
676
677 assert(snode || node);
678
679 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
680
681 /* move to the second item in the name/X pair */
682 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
683 LY_CHECK_GOTO(ret, cleanup);
684
685 /* check attribute encoding */
686 switch (nodetype) {
687 case LYS_LEAFLIST:
688 expected = "@name/array of objects/nulls";
689
690 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
691
692next_entry:
693 instance++;
694
695 /* move into array / next entry */
696 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
697 LY_CHECK_GOTO(ret, cleanup);
698
699 if (status == LYJSON_ARRAY_CLOSED) {
700 /* we are done, move after the array */
701 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
702 goto cleanup;
703 }
704 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
705
Michal Vasko69730152020-10-09 16:30:07 +0200706 if (!node || (node->schema != prev->schema)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100707 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 +0200708 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200709 ret = LY_EVALID;
710 goto cleanup;
711 }
712
713 if (status == LYJSON_NULL) {
714 /* continue with the next entry in the leaf-list array */
715 prev = node;
716 node = node->next;
717 goto next_entry;
718 }
719 break;
720 case LYS_LEAF:
721 case LYS_ANYXML:
722 expected = "@name/object";
723
724 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
725 break;
726 case LYS_CONTAINER:
727 case LYS_LIST:
728 case LYS_ANYDATA:
729 case LYS_NOTIF:
730 case LYS_ACTION:
731 case LYS_RPC:
732 in_parent = 1;
733 expected = "@/object";
734 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
735 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200736 default:
737 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200738 }
739
740 /* process all the members inside a single metadata object */
741 assert(status == LYJSON_OBJECT);
742
Radek Krejciddace2c2021-01-08 11:30:56 +0100743 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100744
Radek Krejci1798aae2020-07-14 13:26:06 +0200745 while (status != LYJSON_OBJECT_CLOSED) {
746 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
747 if (!prefix) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100748 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100749 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 ret = LY_EVALID;
751 goto cleanup;
752 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100753 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100754 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200755 ret = LY_EVALID;
756 goto cleanup;
757 }
758
759 /* get the element module */
760 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
761 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100762 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100763 LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
Radek Krejci422afb12021-03-04 16:38:16 +0100764 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200765 ret = LY_EVALID;
766 goto cleanup;
767 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100768 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200769 /* skip element with children */
770 ret = lydjson_data_skip(lydctx->jsonctx);
771 LY_CHECK_GOTO(ret, cleanup);
772 status = lyjson_ctx_status(lydctx->jsonctx, 0);
773 /* end of the item */
774 continue;
775 }
776 }
777
778 /* get the value */
779 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
780 LY_CHECK_GOTO(ret, cleanup);
781
782 if (node->schema) {
783 /* create metadata */
784 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200785 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 +0200786 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
787 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200788 LY_CHECK_GOTO(ret, cleanup);
789
790 /* add/correct flags */
Michal Vaskoe0665742021-02-11 11:08:44 +0100791 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200792 } else {
793 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200794 const char *module_name;
795 size_t module_name_len;
796
797 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
798
Radek Krejci1798aae2020-07-14 13:26:06 +0200799 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100800 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
801 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
802 LY_PREF_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200803 LY_CHECK_GOTO(ret, cleanup);
804 }
805 /* next member */
806 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
807 LY_CHECK_GOTO(ret, cleanup);
808 }
809
810 if (nodetype == LYS_LEAFLIST) {
811 /* continue by processing another metadata object for the following
812 * leaf-list instance since they are allways instantiated in JSON array */
813 prev = node;
814 node = node->next;
815 goto next_entry;
816 }
817
818 /* move after the metadata */
819 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
820 LY_CHECK_GOTO(ret, cleanup);
821
Radek Krejci5813c362021-01-05 10:51:57 +0100822 /* success */
823 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200824
825representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100826 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200827 "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 +0100828 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name.name,
Michal Vasko69730152020-10-09 16:30:07 +0200829 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200830
Radek Krejci2efc45b2020-12-22 16:25:44 +0100831 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100832
833cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100834 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100835 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200836}
837
838/**
839 * @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.
840 *
841 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
842 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
843 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
844 */
845static void
846lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
847{
848 if (*node_p) {
849 /* insert, keep first pointer correct */
Michal Vasko9e685082021-01-29 14:49:09 +0100850 lyd_insert_node(&parent->node, first_p, *node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200851 if (first_p) {
852 if (parent) {
853 *first_p = parent->child;
854 } else {
855 while ((*first_p)->prev->next) {
856 *first_p = (*first_p)->prev;
857 }
858 }
859 }
860 *node_p = NULL;
861 }
862}
863
Michal Vaskoe0665742021-02-11 11:08:44 +0100864static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
865 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200866
867/**
868 * @brief Parse opaq node from the input.
869 *
870 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
871 *
872 * @param[in] lydctx JSON data parser context.
873 * @param[in] name Name of the opaq node to create.
874 * @param[in] name_len Length of the @p name string.
875 * @param[in] prefix Prefix of the opaq node to create.
876 * @param[in] prefix_len Length of the @p prefx string.
877 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
878 * but must be set if @p first is not.
879 * @param[in,out] status_p Pointer to the current status of the parser context,
880 * since the function manipulates with the context and process the input, the status can be updated.
881 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
882 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
883 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
884 * @param[out] node_p Pointer to the created opaq node.
885 * @return LY_ERR value.
886 */
887static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200888lydjson_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 +0200889 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
890 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200891{
892 LY_ERR ret = LY_SUCCESS;
893 const char *value = NULL, *module_name;
894 size_t value_len = 0, module_name_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200895 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200896 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200897
Michal Vasko69730152020-10-09 16:30:07 +0200898 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200899 /* prepare for creating opaq node with a value */
900 value = lydctx->jsonctx->value;
901 value_len = lydctx->jsonctx->value_len;
902 dynamic = lydctx->jsonctx->dynamic;
903 lydctx->jsonctx->dynamic = 0;
904
905 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
Radek Krejci1798aae2020-07-14 13:26:06 +0200906 }
907
908 /* create node */
Michal Vasko9e685082021-01-29 14:49:09 +0100909 lydjson_get_node_prefix(&parent->node, prefix, prefix_len, &module_name, &module_name_len);
Michal Vasko501af032020-11-11 20:27:44 +0100910 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
911 value_len, &dynamic, LY_PREF_JSON, NULL, type_hint, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200912 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200913 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200914 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200915 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200916
Michal Vasko69730152020-10-09 16:30:07 +0200917 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200918 /* process children */
919 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100920 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200921 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
922 }
Michal Vasko69730152020-10-09 16:30:07 +0200923 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200924 /* process another instance of the same node */
925 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200926 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200927
Michal Vasko69730152020-10-09 16:30:07 +0200928 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200929 /* but first process children of the object in the array */
930 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100931 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200932 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
933 }
934 }
935
936 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
937
938 /* continue with the next instance */
939 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
940 assert(node_p);
941 lydjson_maintain_children(parent, first_p, node_p);
Michal Vaskoe0665742021-02-11 11:08:44 +0100942 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p,
943 first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200944 }
945 }
946
947 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +0100948 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200949
950 /* move after the item */
951 return lyjson_ctx_next(lydctx->jsonctx, status_p);
952}
953
954/**
955 * @brief Process the attribute container (starting by @)
956 *
957 * @param[in] lydctx JSON data parser context.
958 * @param[in] attr_node The data node referenced by the attribute container, if already known.
959 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
960 * @param[in] name Name of the opaq node to create.
961 * @param[in] name_len Length of the @p name string.
962 * @param[in] prefix Prefix of the opaq node to create.
963 * @param[in] prefix_len Length of the @p prefx string.
964 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
965 * but must be set if @p first is not.
966 * @param[in,out] status_p Pointer to the current status of the parser context,
967 * since the function manipulates with the context and process the input, the status can be updated.
968 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
969 * @param[out] node_p Pointer to the created opaq node.
970 * @return LY_ERR value.
971 */
972static LY_ERR
973lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +0100974 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
975 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200976{
977 LY_ERR ret = LY_SUCCESS;
978 enum LYJSON_PARSER_STATUS status_inner;
979
980 /* parse as an attribute to a node */
981 if (!attr_node && snode) {
982 /* try to find the instance */
983 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
984 if (iter->schema == snode) {
985 attr_node = iter;
986 break;
987 }
988 }
989 }
990 if (!attr_node) {
991 /* parse just as an opaq node with the name beginning with @,
992 * later we have to check that it belongs to a standard node
993 * and it is supposed to be converted to a metadata */
994 uint32_t prev_opts;
995
996 /* move into metadata */
997 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
998
999 if (*status_p == LYJSON_ARRAY) {
1000 /* move into the array */
1001 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1002 } else {
1003 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1004 status_inner = LYJSON_ERROR;
1005 }
1006
1007 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001008 prev_opts = lydctx->parse_opts;
1009 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1010 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001011
1012 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 +01001013 NULL, 0, (struct lyd_node_inner *)parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner,
1014 first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001015
1016 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001017 lydctx->parse_opts = prev_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001018 } else {
1019 ret = lydjson_metadata(lydctx, snode, attr_node);
1020 }
1021
1022 return ret;
1023}
1024
1025/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001026 * @brief Parse a single instance of a node.
1027 *
1028 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1029 * as before calling, despite it is necessary to process input data for checking.
1030 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1031 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1032 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1033 * @param[in] name Parsed JSON node name.
1034 * @param[in] name_len Lenght of @p name.
1035 * @param[in] prefix Parsed JSON node prefix.
1036 * @param[in] prefix_len Length of @p prefix.
1037 * @param[in,out] status JSON parser status, is updated.
1038 * @param[out] node Parsed data (or opaque) node.
1039 * @return LY_SUCCESS if a node was successfully parsed,
1040 * @return LY_EINVAL in case of invalid JSON encoding,
1041 * @return LY_ERR on other errors.
1042 */
1043static LY_ERR
1044lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001045 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1046 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001047{
1048 LY_ERR ret;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001049 uint32_t type_hints = 0;
Michal Vasko32ac9942020-08-12 14:35:12 +02001050 uint32_t prev_opts;
1051 struct lyd_node *tree = NULL;
1052
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001053 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001054 if (ret == LY_SUCCESS) {
1055 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1056 if (snode->nodetype & LYD_NODE_TERM) {
1057 /* create terminal node */
1058 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001059 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1060 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001061 LY_CHECK_RET(ret);
1062
1063 /* move JSON parser */
1064 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1065 LY_CHECK_RET(ret);
1066 } else if (snode->nodetype & LYD_NODE_INNER) {
1067 /* create inner node */
1068 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1069
1070 ret = lyd_create_inner(snode, node);
1071 LY_CHECK_RET(ret);
1072
Radek Krejciddace2c2021-01-08 11:30:56 +01001073 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001074
Michal Vasko32ac9942020-08-12 14:35:12 +02001075 /* process children */
1076 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001077 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001078 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001079 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1080 }
1081
1082 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001083 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001084 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001085
1086 if (snode->nodetype == LYS_LIST) {
1087 /* check all keys exist */
1088 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001089 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001090 }
1091
Michal Vaskoe0665742021-02-11 11:08:44 +01001092 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001093 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001094 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001095 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001096
1097 /* add any missing default children */
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001098 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when,
1099 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ?
Michal Vasko69730152020-10-09 16:30:07 +02001100 LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001101 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001102 }
1103
Radek Krejciddace2c2021-01-08 11:30:56 +01001104 LOG_LOCBACK(1, 1, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001105
Michal Vasko32ac9942020-08-12 14:35:12 +02001106 /* move JSON parser */
1107 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1108 LY_CHECK_RET(ret);
1109 } else if (snode->nodetype & LYD_NODE_ANY) {
1110 /* create any node */
1111 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1112
1113 /* parse any data tree with correct options */
1114 /* first backup the current options and then make the parser to process data as opaq nodes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001115 prev_opts = lydctx->parse_opts;
1116 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1117 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vasko32ac9942020-08-12 14:35:12 +02001118
1119 /* process the anydata content */
1120 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001121 ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001122 LY_CHECK_RET(ret);
1123 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1124 }
1125
1126 /* restore parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001127 lydctx->parse_opts = prev_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001128
1129 /* finish linking metadata */
1130 ret = lydjson_metadata_finish(lydctx, &tree);
1131 LY_CHECK_RET(ret);
1132
Michal Vasko366a4a12020-12-04 16:23:57 +01001133 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001134 LY_CHECK_RET(ret);
1135 }
1136 } else if (ret == LY_ENOT) {
1137 /* parse it again as an opaq node */
1138 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001139 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001140 LY_CHECK_RET(ret);
1141
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001142 if (snode->nodetype == LYS_LIST) {
1143 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1144 } else if (snode->nodetype == LYS_LEAFLIST) {
1145 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001146 }
1147 }
1148
1149 return ret;
1150}
1151
1152/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001153 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001154 *
1155 * @param[in] lydctx JSON data parser context.
1156 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1157 * @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 +01001158 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001159 * @return LY_ERR value.
1160 */
1161static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001162lydjson_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 +02001163{
1164 LY_ERR ret = LY_SUCCESS;
1165 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1166 enum LYJSON_PARSER_STATUS status_inner = 0;
1167 const char *name, *prefix = NULL;
1168 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001169 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001170 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001171 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001172 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1173 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001174
1175 assert(parent || first_p);
1176 assert(status == LYJSON_OBJECT);
1177
1178 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001179 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 +02001180
Michal Vaskocabe0702020-08-12 10:14:36 +02001181 if (!is_meta || name_len || prefix_len) {
1182 /* get the schema node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001183 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 +02001184 if (ret == LY_ENOT) {
1185 /* skip element with children */
1186 ret = lydjson_data_skip(lydctx->jsonctx);
1187 LY_CHECK_GOTO(ret, cleanup);
1188 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1189 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001190 goto cleanup;
1191 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001192 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001193
Michal Vaskocabe0702020-08-12 10:14:36 +02001194 if (!snode) {
1195 /* we will not be parsing it as metadata */
1196 is_meta = 0;
1197 }
1198 }
1199
1200 if (is_meta) {
1201 /* parse as metadata */
1202 if (!name_len && !prefix_len) {
1203 /* parent's metadata without a name - use the schema from the parent */
1204 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001205 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001206 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001207 ret = LY_EVALID;
1208 goto cleanup;
1209 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001210 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001211 snode = attr_node->schema;
1212 }
1213 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001214 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001215 LY_CHECK_GOTO(ret, cleanup);
1216 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001217 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001218 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001219
1220 /* move to the second item in the name/X pair */
1221 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1222 LY_CHECK_GOTO(ret, cleanup);
1223
1224 if (status == LYJSON_ARRAY) {
1225 /* move into the array */
1226 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1227 LY_CHECK_GOTO(ret, cleanup);
1228 } else {
1229 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1230 status_inner = LYJSON_ERROR;
1231 }
1232
Michal Vaskoe0665742021-02-11 11:08:44 +01001233 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, (struct lyd_node_inner *)parent, &status,
1234 status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001235 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001236 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001237 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001238
1239 /* move to the second item in the name/X pair */
1240 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1241 LY_CHECK_GOTO(ret, cleanup);
1242
1243 /* first check the expected representation according to the nodetype and then continue with the content */
1244 switch (snode->nodetype) {
1245 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001246 case LYS_LIST:
1247 if (snode->nodetype == LYS_LEAFLIST) {
1248 expected = "name/array of values";
1249 } else {
1250 expected = "name/array of objects";
1251 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001252
1253 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1254
1255 /* move into array */
1256 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1257 LY_CHECK_GOTO(ret, cleanup);
1258
Michal Vasko32ac9942020-08-12 14:35:12 +02001259 /* process all the values/objects */
1260 do {
Michal Vaskoe0665742021-02-11 11:08:44 +01001261 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001262
Michal Vaskoe0665742021-02-11 11:08:44 +01001263 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1264 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001265 if (ret == LY_EINVAL) {
1266 goto representation_error;
1267 } else if (ret) {
1268 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001269 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001270 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001271
Michal Vasko32ac9942020-08-12 14:35:12 +02001272 /* move after the array */
1273 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1274 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001275
1276 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001277 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001278 case LYS_CONTAINER:
1279 case LYS_NOTIF:
1280 case LYS_ACTION:
1281 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001282 case LYS_ANYDATA:
1283 case LYS_ANYXML:
1284 if (snode->nodetype == LYS_LEAF) {
1285 if (status == LYJSON_ARRAY) {
1286 expected = "name/[null]";
1287 } else {
1288 expected = "name/value";
1289 }
1290 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001291 expected = "name/object";
1292 }
1293
Michal Vasko32ac9942020-08-12 14:35:12 +02001294 /* process the value/object */
Michal Vaskoe0665742021-02-11 11:08:44 +01001295 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1296 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001297 if (ret == LY_EINVAL) {
1298 goto representation_error;
1299 } else if (ret) {
1300 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001301 }
1302
Michal Vasko32ac9942020-08-12 14:35:12 +02001303 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001304 /* rememeber the RPC/action/notification */
1305 lydctx->op_node = node;
1306 }
1307
1308 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001309 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001310 }
1311
Michal Vasko32ac9942020-08-12 14:35:12 +02001312 /* finally connect the parsed node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001313 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node);
1314
1315 /* rememeber a successfully parsed node */
1316 if (parsed) {
1317 ly_set_add(parsed, node, 1, NULL);
1318 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001319
Radek Krejci5813c362021-01-05 10:51:57 +01001320 /* success */
1321 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001322
1323representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001324 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001325 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 +02001326 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001327 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001328 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001329
1330cleanup:
1331 lyd_free_tree(node);
1332 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001333}
1334
1335/**
1336 * @brief Common start of JSON parser processing different types of the input data.
1337 *
1338 * @param[in] ctx libyang context
1339 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001340 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1341 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001342 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001343 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001344 * @return LY_ERR value.
1345 */
1346static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001347lyd_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 +01001348 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001349{
1350 LY_ERR ret = LY_SUCCESS;
1351 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001352 size_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +02001353
Radek Krejcicd5f6222020-11-12 08:54:13 +01001354 assert(lydctx_p);
1355 assert(status);
1356
Radek Krejci1798aae2020-07-14 13:26:06 +02001357 /* init context */
1358 lydctx = calloc(1, sizeof *lydctx);
1359 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001360 lydctx->parse_opts = parse_opts;
1361 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001362 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001363
Radek Krejci284f31f2020-09-18 15:40:29 +02001364 /* starting top-level */
1365 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001366 if (in->current[i] == '\n') {
1367 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001368 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001369 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001370 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001371
Radek Krejci284f31f2020-09-18 15:40:29 +02001372 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001373 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejcibb27df32020-11-13 15:39:16 +01001374 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001375 *lydctx_p = lydctx;
1376 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001377 } else {
1378 /* expecting top-level object */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001379 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.",
Radek Krejcicd5f6222020-11-12 08:54:13 +01001380 lyjson_token2str(*status));
1381 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001382 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001383 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001384 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001385}
1386
1387LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001388lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1389 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1390 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001391{
Michal Vaskoe0665742021-02-11 11:08:44 +01001392 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001393 struct lyd_json_ctx *lydctx = NULL;
1394 enum LYJSON_PARSER_STATUS status;
Michal Vaskoe0665742021-02-11 11:08:44 +01001395 uint32_t int_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001396
Michal Vaskoe0665742021-02-11 11:08:44 +01001397 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1398 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001399
Radek Krejcicd5f6222020-11-12 08:54:13 +01001400 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001401
Michal Vaskoe0665742021-02-11 11:08:44 +01001402 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001403 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001404 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1405 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001406 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001407 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1408 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001409 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001410 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1411 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001412 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001413 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1414 break;
1415 default:
1416 LOGINT(ctx);
1417 rc = LY_EINT;
1418 goto cleanup;
1419 }
1420 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001421 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001422
1423 /* find the operation node if it exists already */
1424 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1425
Radek Krejci1798aae2020-07-14 13:26:06 +02001426 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001427 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1428 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1429 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001430
1431 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001432
1433 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1434 break;
1435 }
1436 }
1437
1438 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1439 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1440 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1441 rc = LY_EVALID;
1442 goto cleanup;
1443 }
1444 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1445 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1446 rc = LY_EVALID;
1447 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001448 }
1449
1450 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001451 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1452 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001453
1454cleanup:
1455 /* there should be no unresolved types stored */
Michal Vaskoe0665742021-02-11 11:08:44 +01001456 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001457 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001458
Michal Vaskoe0665742021-02-11 11:08:44 +01001459 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001460 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001461 } else {
1462 *lydctx_p = (struct lyd_ctx *)lydctx;
1463 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001464 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001465}