blob: da65f2f1ada22c2a64981287e9167a91ee851df1 [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 {
43 uint32_t parse_options; /**< various @ref dataparseroptions. */
44 uint32_t validate_options; /**< various @ref datavalidationoptions. */
45 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 Vasko22df3f02020-08-24 13:29:22 +0200168 node = (struct lyd_node *)node->parent;
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{
194 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100195 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200196
197 /* init return value */
198 *snode_p = NULL;
199
200 /* get the element module */
Michal Vasko2552ea32020-12-08 15:32:34 +0100201 if (prefix_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200202 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
203 } else if (parent) {
204 if (parent->schema) {
205 mod = parent->schema->module;
206 }
207 } else {
208 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
Michal Vasko69730152020-10-09 16:30:07 +0200209 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200210 return LY_EVALID;
211 }
212 if (!mod) {
213 if (lydctx->parse_options & LYD_PARSE_STRICT) {
214 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
215 return LY_EVALID;
216 }
217 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
218 return LY_ENOT;
219 }
220 }
221
Radek Krejci1798aae2020-07-14 13:26:06 +0200222 /* get the schema node */
223 if (mod && (!parent || parent->schema)) {
224 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
225 if (!*snode_p) {
226 if (lydctx->parse_options & LYD_PARSE_STRICT) {
227 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
Michal Vasko69730152020-10-09 16:30:07 +0200228 name_len, name, mod->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200229 return LY_EVALID;
230 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
231 /* skip element with children */
232 return LY_ENOT;
233 }
234 } else {
235 /* check that schema node is valid and can be used */
236 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
237 }
238 }
239
240 return LY_SUCCESS;
241}
242
243/**
244 * @brief Skip the currently open JSON object/array
245 * @param[in] jsonctx JSON context with the input data to skip.
246 * @return LY_ERR value.
247 */
248static LY_ERR
249lydjson_data_skip(struct lyjson_ctx *jsonctx)
250{
251 enum LYJSON_PARSER_STATUS status, current;
252 size_t sublevels = 1;
253
254 status = lyjson_ctx_status(jsonctx, 0);
255
256 /* skip after the content */
257 do {
258 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
259 if (current == status) {
260 sublevels++;
261 } else if (current == status + 1) {
262 sublevels--;
263 }
Radek Krejci79856c42020-11-19 11:44:08 +0100264 } while (current != status + 1 || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200265 /* open the next sibling */
266 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
267
268 return LY_SUCCESS;
269}
270
271/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200272 * @brief Check that the input data are parseable as the @p list.
273 *
274 * Checks for all the list's keys. Function does not revert the context state.
275 *
276 * @param[in] jsonctx JSON parser context.
277 * @param[in] list List schema node corresponding to the input data object.
278 * @return LY_SUCCESS in case the data are ok for the @p list
279 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
280 */
281static LY_ERR
282lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
283{
284 LY_ERR ret = LY_SUCCESS;
285 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
286 struct ly_set key_set = {0};
287 const struct lysc_node *snode;
288 uint32_t i, status_count;
289
290 assert(list && (list->nodetype == LYS_LIST));
291 assert(status == LYJSON_OBJECT);
292
293 /* get all keys into a set (keys do not have if-features or anything) */
294 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100295 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200296 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200297 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200298 }
299
300 if (status != LYJSON_OBJECT_EMPTY) {
301 status_count = jsonctx->status.count;
302
303 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
304 const char *name, *prefix;
305 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200306 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200307
308 /* match the key */
309 snode = NULL;
310 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
311
312 if (!is_attr && !prefix) {
313 for (i = 0; i < key_set.count; ++i) {
314 snode = (const struct lysc_node *)key_set.objs[i];
315 if (!ly_strncmp(snode->name, name, name_len)) {
316 break;
317 }
318 }
319 /* go into the item to a) process it as a key or b) start skipping it as another list child */
320 ret = lyjson_ctx_next(jsonctx, &status);
321 LY_CHECK_GOTO(ret, cleanup);
322
323 if (snode) {
324 /* we have the key, validate the value */
325 if (status < LYJSON_NUMBER) {
326 /* not a terminal */
327 ret = LY_ENOT;
328 goto cleanup;
329 }
330
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200331 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200332 LY_CHECK_GOTO(ret, cleanup);
333
334 /* key with a valid value, remove from the set */
335 ly_set_rm_index(&key_set, i, NULL);
336 }
337 } else {
338 /* start skipping the member we are not interested in */
339 ret = lyjson_ctx_next(jsonctx, &status);
340 LY_CHECK_GOTO(ret, cleanup);
341 }
342 /* move to the next child */
343 while (status_count < jsonctx->status.count) {
344 ret = lyjson_ctx_next(jsonctx, &status);
345 LY_CHECK_GOTO(ret, cleanup);
346 }
347 }
348 }
349
350 if (key_set.count) {
351 /* some keys are missing/did not validate */
352 ret = LY_ENOT;
353 }
354
355cleanup:
356 ly_set_erase(&key_set, NULL);
357 return ret;
358}
359
360/**
361 * @brief Get the hint for the data type parsers according to the current JSON parser context.
362 *
363 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
364 * @param[in,out] status Pointer to the current context status,
365 * in some circumstances the function manipulates with the context so the status is updated.
366 * @param[out] type_hint_p Pointer to the variable to store the result.
367 * @return LY_SUCCESS in case of success.
368 * @return LY_EINVAL in case of invalid context status not referring to a value.
369 */
370static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200371lydjson_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 +0200372{
373 *type_hint_p = 0;
374
375 if (*status_p == LYJSON_ARRAY) {
376 /* only [null] */
377 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
378 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
379
380 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
381 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
382
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200383 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200384 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200385 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200386 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200387 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200388 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200389 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200390 } else if (*status_p == LYJSON_NULL) {
391 *type_hint_p = 0;
392 } else {
393 return LY_EINVAL;
394 }
395
396 return LY_SUCCESS;
397}
398
399/**
400 * @brief Check in advance if the input data are parsable according to the provided @p snode.
401 *
402 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
403 * is naturally done when the data are really parsed.
404 *
405 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
406 * as before calling, despite it is necessary to process input data for checking.
407 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
408 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
409 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
410 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
411 * @return LY_EINVAL in case of invalid leaf JSON encoding
412 * and they are expected to be parsed as opaq nodes.
413 */
414static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200415lydjson_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 +0200416{
417 LY_ERR ret = LY_SUCCESS;
418 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
419 enum LYJSON_PARSER_STATUS status;
420
421 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200422
Michal Vasko32ac9942020-08-12 14:35:12 +0200423 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
424 /* can always be parsed as a data node if we have the schema node */
425 return LY_SUCCESS;
426 }
427
428 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200429 /* backup parser */
430 lyjson_ctx_backup(jsonctx);
431 status = lyjson_ctx_status(jsonctx, 0);
432
Michal Vasko32ac9942020-08-12 14:35:12 +0200433 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
434 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200435 switch (snode->nodetype) {
436 case LYS_LEAFLIST:
437 case LYS_LEAF:
438 /* value may not be valid in which case we parse it as an opaque node */
439 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
440 if (ret) {
441 break;
442 }
443
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200444 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200445 ret = LY_ENOT;
446 }
447 break;
448 case LYS_LIST:
449 /* lists may not have all its keys */
450 if (lydjson_check_list(jsonctx, snode)) {
451 /* invalid list, parse as opaque if it missing/has invalid some keys */
452 ret = LY_ENOT;
453 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200454 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200455 }
456
457 /* restore parser */
458 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200459 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200460 status = lyjson_ctx_status(jsonctx, 0);
461 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
462 }
463
464 return ret;
465}
466
467/**
468 * @brief Join the forward-referencing metadata with their target data nodes.
469 *
470 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
471 *
472 * @param[in] lydctx JSON data parser context.
473 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
474 * as a starting point to search for the metadata's target data node
475 * @return LY_SUCCESS on success
476 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
477 */
478static LY_ERR
479lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
480{
481 LY_ERR ret = LY_SUCCESS;
482 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200483 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200484 const char *prev = NULL;
485
486 /* finish linking metadata */
487 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200488 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200489 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200490 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200491 const char *name, *prefix;
492 size_t name_len, prefix_len;
493 const struct lysc_node *snode;
494
Michal Vaskoad92b672020-11-12 13:11:31 +0100495 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200496 /* not an opaq metadata node */
497 continue;
498 }
499
Michal Vaskoad92b672020-11-12 13:11:31 +0100500 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200501 /* metas' names are stored in dictionary, so checking pointers must works */
502 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100503 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200504 instance = 1;
505 } else {
506 instance++;
507 }
508
509 /* find the correspnding data node */
510 LY_LIST_FOR(start, node) {
511 if (!node->schema) {
512 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100513 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 continue;
515 }
516
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200517 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200518 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vaskoad92b672020-11-12 13:11:31 +0100519 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200520 ret = LY_EVALID;
521 goto cleanup;
522 }
523
524 /* match */
525 match++;
526 if (match != instance) {
527 continue;
528 }
529
530 LY_LIST_FOR(meta_container->child, meta_iter) {
531 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200532 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100533
Michal Vaskoad92b672020-11-12 13:11:31 +0100534 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
535 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
536 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_PREF_JSON,
537 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200538 LY_CHECK_GOTO(ret, cleanup);
539 }
540
541 /* done */
542 break;
543 } else {
544 /* this is the second time we are resolving the schema node, so it must succeed,
545 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100546 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
547 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200548 assert(is_attr);
549 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
550 assert(ret == LY_SUCCESS);
551
552 if (snode != node->schema) {
553 continue;
554 }
555
556 /* match */
557 match++;
558 if (match != instance) {
559 continue;
560 }
561
562 LY_LIST_FOR(meta_container->child, meta_iter) {
563 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200564 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200565 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200566 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200567
Michal Vaskoad92b672020-11-12 13:11:31 +0100568 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200569 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200570 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100571 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vasko69730152020-10-09 16:30:07 +0200572 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 LY_CHECK_GOTO(ret, cleanup);
574 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
575 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200576 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
Michal Vaskoad92b672020-11-12 13:11:31 +0100577 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", meta->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 ret = LY_EVALID;
579 goto cleanup;
580 }
581 }
582 /* add/correct flags */
Michal Vasko49c39d82020-11-06 17:20:27 +0100583 lyd_parse_set_data_flags(node, &lydctx->node_when, &node->meta, lydctx->parse_options);
Radek Krejci1798aae2020-07-14 13:26:06 +0200584
585 /* done */
586 break;
587 }
588 }
589
590 if (match != instance) {
591 /* there is no corresponding data node for the metadata */
592 if (instance > 1) {
593 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200594 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
Michal Vaskoad92b672020-11-12 13:11:31 +0100595 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200596 } else {
597 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vaskoad92b672020-11-12 13:11:31 +0100598 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200599 }
600 ret = LY_EVALID;
601 } else {
602 /* remove the opaq attr */
603 if (attr == (*first_p)) {
604 *first_p = attr->next;
605 }
606 lyd_free_tree(attr);
607 }
608 }
609
610cleanup:
611 lydict_remove(lydctx->jsonctx->ctx, prev);
612
613 return ret;
614}
615
616/**
617 * @brief Parse a metadata member.
618 *
619 * @param[in] lydctx JSON data parser context.
620 * @param[in] snode Schema node of the metadata parent.
621 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
622 * so the data node does not exists. In such a case the metadata is stored in the context for the later
623 * processing by lydjson_metadata_finish().
624 * @return LY_SUCCESS on success
625 * @return Various LY_ERR values in case of failure.
626 */
627static LY_ERR
628lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
629{
630 LY_ERR ret = LY_SUCCESS;
631 enum LYJSON_PARSER_STATUS status;
632 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200633 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200634 const char *name, *prefix = NULL;
635 size_t name_len, prefix_len = 0;
636 struct lys_module *mod;
637 struct lyd_meta *meta = NULL;
638 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200639 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200640 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200642 uint16_t nodetype;
643
644 assert(snode || node);
645
646 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
647
648 /* move to the second item in the name/X pair */
649 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
650 LY_CHECK_GOTO(ret, cleanup);
651
652 /* check attribute encoding */
653 switch (nodetype) {
654 case LYS_LEAFLIST:
655 expected = "@name/array of objects/nulls";
656
657 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
658
659next_entry:
660 instance++;
661
662 /* move into array / next entry */
663 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
664 LY_CHECK_GOTO(ret, cleanup);
665
666 if (status == LYJSON_ARRAY_CLOSED) {
667 /* we are done, move after the array */
668 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
669 goto cleanup;
670 }
671 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
672
Michal Vasko69730152020-10-09 16:30:07 +0200673 if (!node || (node->schema != prev->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200674 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200675 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
676 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200677 ret = LY_EVALID;
678 goto cleanup;
679 }
680
681 if (status == LYJSON_NULL) {
682 /* continue with the next entry in the leaf-list array */
683 prev = node;
684 node = node->next;
685 goto next_entry;
686 }
687 break;
688 case LYS_LEAF:
689 case LYS_ANYXML:
690 expected = "@name/object";
691
692 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
693 break;
694 case LYS_CONTAINER:
695 case LYS_LIST:
696 case LYS_ANYDATA:
697 case LYS_NOTIF:
698 case LYS_ACTION:
699 case LYS_RPC:
700 in_parent = 1;
701 expected = "@/object";
702 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
703 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200704 default:
705 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200706 }
707
708 /* process all the members inside a single metadata object */
709 assert(status == LYJSON_OBJECT);
710
711 while (status != LYJSON_OBJECT_CLOSED) {
712 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
713 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200714 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200715 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
716 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200717 ret = LY_EVALID;
718 goto cleanup;
719 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200720 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200721 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
722 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200723 ret = LY_EVALID;
724 goto cleanup;
725 }
726
727 /* get the element module */
728 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
729 if (!mod) {
730 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200731 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200732 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
733 prefix_len, prefix, name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 ret = LY_EVALID;
735 goto cleanup;
736 }
737 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
738 /* skip element with children */
739 ret = lydjson_data_skip(lydctx->jsonctx);
740 LY_CHECK_GOTO(ret, cleanup);
741 status = lyjson_ctx_status(lydctx->jsonctx, 0);
742 /* end of the item */
743 continue;
744 }
745 }
746
747 /* get the value */
748 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
749 LY_CHECK_GOTO(ret, cleanup);
750
751 if (node->schema) {
752 /* create metadata */
753 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200754 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 +0200755 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
756 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200757 LY_CHECK_GOTO(ret, cleanup);
758
759 /* add/correct flags */
Michal Vasko49c39d82020-11-06 17:20:27 +0100760 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_options);
Radek Krejci1798aae2020-07-14 13:26:06 +0200761 } else {
762 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200763 const char *module_name;
764 size_t module_name_len;
765
766 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
767
Radek Krejci1798aae2020-07-14 13:26:06 +0200768 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100769 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
770 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
771 LY_PREF_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200772 LY_CHECK_GOTO(ret, cleanup);
773 }
774 /* next member */
775 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
776 LY_CHECK_GOTO(ret, cleanup);
777 }
778
779 if (nodetype == LYS_LEAFLIST) {
780 /* continue by processing another metadata object for the following
781 * leaf-list instance since they are allways instantiated in JSON array */
782 prev = node;
783 node = node->next;
784 goto next_entry;
785 }
786
787 /* move after the metadata */
788 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
789 LY_CHECK_GOTO(ret, cleanup);
790
791cleanup:
792 return ret;
793
794representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200795 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200796 "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 +0100797 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name.name,
Michal Vasko69730152020-10-09 16:30:07 +0200798 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200799
800 return LY_EVALID;
801}
802
803/**
804 * @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.
805 *
806 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
807 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
808 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
809 */
810static void
811lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
812{
813 if (*node_p) {
814 /* insert, keep first pointer correct */
815 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
816 if (first_p) {
817 if (parent) {
818 *first_p = parent->child;
819 } else {
820 while ((*first_p)->prev->next) {
821 *first_p = (*first_p)->prev;
822 }
823 }
824 }
825 *node_p = NULL;
826 }
827}
828
829static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
830
831/**
832 * @brief Parse opaq node from the input.
833 *
834 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
835 *
836 * @param[in] lydctx JSON data parser context.
837 * @param[in] name Name of the opaq node to create.
838 * @param[in] name_len Length of the @p name string.
839 * @param[in] prefix Prefix of the opaq node to create.
840 * @param[in] prefix_len Length of the @p prefx string.
841 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
842 * but must be set if @p first is not.
843 * @param[in,out] status_p Pointer to the current status of the parser context,
844 * since the function manipulates with the context and process the input, the status can be updated.
845 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
846 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
847 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
848 * @param[out] node_p Pointer to the created opaq node.
849 * @return LY_ERR value.
850 */
851static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200852lydjson_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 +0200853 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
854 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200855{
856 LY_ERR ret = LY_SUCCESS;
857 const char *value = NULL, *module_name;
858 size_t value_len = 0, module_name_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200859 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200860 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200861
Michal Vasko69730152020-10-09 16:30:07 +0200862 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200863 /* prepare for creating opaq node with a value */
864 value = lydctx->jsonctx->value;
865 value_len = lydctx->jsonctx->value_len;
866 dynamic = lydctx->jsonctx->dynamic;
867 lydctx->jsonctx->dynamic = 0;
868
869 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
Radek Krejci1798aae2020-07-14 13:26:06 +0200870 }
871
872 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200873 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vasko501af032020-11-11 20:27:44 +0100874 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
875 value_len, &dynamic, LY_PREF_JSON, NULL, type_hint, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200876 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200877 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200878 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200879 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200880
Michal Vasko69730152020-10-09 16:30:07 +0200881 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200882 /* process children */
883 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
884 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
885 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
886 }
Michal Vasko69730152020-10-09 16:30:07 +0200887 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200888 /* process another instance of the same node */
889 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200890 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200891
Michal Vasko69730152020-10-09 16:30:07 +0200892 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200893 /* but first process children of the object in the array */
894 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
895 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
896 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
897 }
898 }
899
900 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
901
902 /* continue with the next instance */
903 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
904 assert(node_p);
905 lydjson_maintain_children(parent, first_p, node_p);
906 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
907 }
908 }
909
910 /* finish linking metadata */
911 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
912
913 /* move after the item */
914 return lyjson_ctx_next(lydctx->jsonctx, status_p);
915}
916
917/**
918 * @brief Process the attribute container (starting by @)
919 *
920 * @param[in] lydctx JSON data parser context.
921 * @param[in] attr_node The data node referenced by the attribute container, if already known.
922 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
923 * @param[in] name Name of the opaq node to create.
924 * @param[in] name_len Length of the @p name string.
925 * @param[in] prefix Prefix of the opaq node to create.
926 * @param[in] prefix_len Length of the @p prefx string.
927 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
928 * but must be set if @p first is not.
929 * @param[in,out] status_p Pointer to the current status of the parser context,
930 * since the function manipulates with the context and process the input, the status can be updated.
931 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
932 * @param[out] node_p Pointer to the created opaq node.
933 * @return LY_ERR value.
934 */
935static LY_ERR
936lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +0200937 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
938 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
939 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200940{
941 LY_ERR ret = LY_SUCCESS;
942 enum LYJSON_PARSER_STATUS status_inner;
943
944 /* parse as an attribute to a node */
945 if (!attr_node && snode) {
946 /* try to find the instance */
947 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
948 if (iter->schema == snode) {
949 attr_node = iter;
950 break;
951 }
952 }
953 }
954 if (!attr_node) {
955 /* parse just as an opaq node with the name beginning with @,
956 * later we have to check that it belongs to a standard node
957 * and it is supposed to be converted to a metadata */
958 uint32_t prev_opts;
959
960 /* move into metadata */
961 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
962
963 if (*status_p == LYJSON_ARRAY) {
964 /* move into the array */
965 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
966 } else {
967 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
968 status_inner = LYJSON_ERROR;
969 }
970
971 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
972 prev_opts = lydctx->parse_options;
973 lydctx->parse_options &= ~LYD_PARSE_STRICT;
974 lydctx->parse_options |= LYD_PARSE_OPAQ;
975
976 ret = lydjson_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
Michal Vasko69730152020-10-09 16:30:07 +0200977 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200978
979 /* restore the parser options */
980 lydctx->parse_options = prev_opts;
981 } else {
982 ret = lydjson_metadata(lydctx, snode, attr_node);
983 }
984
985 return ret;
986}
987
988/**
Michal Vasko32ac9942020-08-12 14:35:12 +0200989 * @brief Parse a single instance of a node.
990 *
991 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
992 * as before calling, despite it is necessary to process input data for checking.
993 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
994 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
995 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
996 * @param[in] name Parsed JSON node name.
997 * @param[in] name_len Lenght of @p name.
998 * @param[in] prefix Parsed JSON node prefix.
999 * @param[in] prefix_len Length of @p prefix.
1000 * @param[in,out] status JSON parser status, is updated.
1001 * @param[out] node Parsed data (or opaque) node.
1002 * @return LY_SUCCESS if a node was successfully parsed,
1003 * @return LY_EINVAL in case of invalid JSON encoding,
1004 * @return LY_ERR on other errors.
1005 */
1006static LY_ERR
1007lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001008 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1009 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001010{
1011 LY_ERR ret;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001012 uint32_t type_hints = 0;
Michal Vasko32ac9942020-08-12 14:35:12 +02001013 uint32_t prev_opts;
1014 struct lyd_node *tree = NULL;
1015
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001016 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001017 if (ret == LY_SUCCESS) {
1018 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1019 if (snode->nodetype & LYD_NODE_TERM) {
1020 /* create terminal node */
1021 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001022 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1023 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001024 LY_CHECK_RET(ret);
1025
1026 /* move JSON parser */
1027 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1028 LY_CHECK_RET(ret);
1029 } else if (snode->nodetype & LYD_NODE_INNER) {
1030 /* create inner node */
1031 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1032
1033 ret = lyd_create_inner(snode, node);
1034 LY_CHECK_RET(ret);
1035
1036 /* process children */
1037 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1038 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1039 LY_CHECK_RET(ret);
1040 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1041 }
1042
1043 /* finish linking metadata */
1044 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1045 LY_CHECK_RET(ret);
1046
1047 if (snode->nodetype == LYS_LIST) {
1048 /* check all keys exist */
1049 ret = lyd_parse_check_keys(*node);
1050 LY_CHECK_RET(ret);
1051 }
1052
1053 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1054 /* new node validation, autodelete CANNOT occur, all nodes are new */
1055 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1056 LY_CHECK_RET(ret);
1057
1058 /* add any missing default children */
Michal Vasko49c39d82020-11-06 17:20:27 +01001059 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->node_types,
1060 &lydctx->node_when, (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ?
Michal Vasko69730152020-10-09 16:30:07 +02001061 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001062 LY_CHECK_RET(ret);
1063 }
1064
1065 /* move JSON parser */
1066 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1067 LY_CHECK_RET(ret);
1068 } else if (snode->nodetype & LYD_NODE_ANY) {
1069 /* create any node */
1070 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1071
1072 /* parse any data tree with correct options */
1073 /* first backup the current options and then make the parser to process data as opaq nodes */
1074 prev_opts = lydctx->parse_options;
1075 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1076 lydctx->parse_options |= LYD_PARSE_OPAQ;
1077
1078 /* process the anydata content */
1079 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1080 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1081 LY_CHECK_RET(ret);
1082 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1083 }
1084
1085 /* restore parser options */
1086 lydctx->parse_options = prev_opts;
1087
1088 /* finish linking metadata */
1089 ret = lydjson_metadata_finish(lydctx, &tree);
1090 LY_CHECK_RET(ret);
1091
Michal Vasko366a4a12020-12-04 16:23:57 +01001092 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001093 LY_CHECK_RET(ret);
1094 }
1095 } else if (ret == LY_ENOT) {
1096 /* parse it again as an opaq node */
1097 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001098 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001099 LY_CHECK_RET(ret);
1100
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001101 if (snode->nodetype == LYS_LIST) {
1102 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1103 } else if (snode->nodetype == LYS_LEAFLIST) {
1104 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001105 }
1106 }
1107
1108 return ret;
1109}
1110
1111/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001112 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001113 *
1114 * @param[in] lydctx JSON data parser context.
1115 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1116 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1117 * @return LY_ERR value.
1118 */
1119static LY_ERR
1120lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1121{
1122 LY_ERR ret = LY_SUCCESS;
1123 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1124 enum LYJSON_PARSER_STATUS status_inner = 0;
1125 const char *name, *prefix = NULL;
1126 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001127 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001128 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001129 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001130 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1131 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001132
1133 assert(parent || first_p);
1134 assert(status == LYJSON_OBJECT);
1135
1136 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001137 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 +02001138
Michal Vaskocabe0702020-08-12 10:14:36 +02001139 if (!is_meta || name_len || prefix_len) {
1140 /* get the schema node */
1141 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1142 if (ret == LY_ENOT) {
1143 /* skip element with children */
1144 ret = lydjson_data_skip(lydctx->jsonctx);
1145 LY_CHECK_GOTO(ret, cleanup);
1146 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1147 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001148 goto cleanup;
1149 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001150 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001151
Michal Vaskocabe0702020-08-12 10:14:36 +02001152 if (!snode) {
1153 /* we will not be parsing it as metadata */
1154 is_meta = 0;
1155 }
1156 }
1157
1158 if (is_meta) {
1159 /* parse as metadata */
1160 if (!name_len && !prefix_len) {
1161 /* parent's metadata without a name - use the schema from the parent */
1162 if (!parent) {
1163 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001164 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001165 ret = LY_EVALID;
1166 goto cleanup;
1167 }
1168 attr_node = (struct lyd_node *)parent;
1169 snode = attr_node->schema;
1170 }
1171 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001172 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001173 LY_CHECK_GOTO(ret, cleanup);
1174 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001175 /* parse as an opaq node */
1176 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1177
1178 /* move to the second item in the name/X pair */
1179 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1180 LY_CHECK_GOTO(ret, cleanup);
1181
1182 if (status == LYJSON_ARRAY) {
1183 /* move into the array */
1184 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1185 LY_CHECK_GOTO(ret, cleanup);
1186 } else {
1187 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1188 status_inner = LYJSON_ERROR;
1189 }
1190
1191 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001192 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001193 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001194 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001195 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001196
1197 /* move to the second item in the name/X pair */
1198 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1199 LY_CHECK_GOTO(ret, cleanup);
1200
1201 /* first check the expected representation according to the nodetype and then continue with the content */
1202 switch (snode->nodetype) {
1203 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001204 case LYS_LIST:
1205 if (snode->nodetype == LYS_LEAFLIST) {
1206 expected = "name/array of values";
1207 } else {
1208 expected = "name/array of objects";
1209 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001210
1211 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1212
1213 /* move into array */
1214 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1215 LY_CHECK_GOTO(ret, cleanup);
1216
Michal Vasko32ac9942020-08-12 14:35:12 +02001217 /* process all the values/objects */
1218 do {
1219 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001220
Michal Vasko32ac9942020-08-12 14:35:12 +02001221 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001222 &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001223 if (ret == LY_EINVAL) {
1224 goto representation_error;
1225 } else if (ret) {
1226 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001227 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001228 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001229
Michal Vasko32ac9942020-08-12 14:35:12 +02001230 /* move after the array */
1231 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1232 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001233
1234 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001235 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001236 case LYS_CONTAINER:
1237 case LYS_NOTIF:
1238 case LYS_ACTION:
1239 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001240 case LYS_ANYDATA:
1241 case LYS_ANYXML:
1242 if (snode->nodetype == LYS_LEAF) {
1243 if (status == LYJSON_ARRAY) {
1244 expected = "name/[null]";
1245 } else {
1246 expected = "name/value";
1247 }
1248 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001249 expected = "name/object";
1250 }
1251
Michal Vasko32ac9942020-08-12 14:35:12 +02001252 /* process the value/object */
1253 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1254 if (ret == LY_EINVAL) {
1255 goto representation_error;
1256 } else if (ret) {
1257 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001258 }
1259
Michal Vasko32ac9942020-08-12 14:35:12 +02001260 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001261 /* rememeber the RPC/action/notification */
1262 lydctx->op_node = node;
1263 }
1264
1265 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001266 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001267 }
1268
Michal Vasko32ac9942020-08-12 14:35:12 +02001269 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001270 lydjson_maintain_children(parent, first_p, &node);
1271
1272cleanup:
1273 lyd_free_tree(node);
1274 return ret;
1275
1276representation_error:
1277 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001278 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1279 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejci1798aae2020-07-14 13:26:06 +02001280
1281 ret = LY_EVALID;
1282 goto cleanup;
1283}
1284
1285/**
1286 * @brief Common start of JSON parser processing different types of the input data.
1287 *
1288 * @param[in] ctx libyang context
1289 * @param[in] in Input structure.
1290 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1291 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1292 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001293 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001294 * @return LY_ERR value.
1295 */
1296static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001297lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001298 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001299{
1300 LY_ERR ret = LY_SUCCESS;
1301 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001302 size_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +02001303
Radek Krejcicd5f6222020-11-12 08:54:13 +01001304 assert(lydctx_p);
1305 assert(status);
1306
Radek Krejci1798aae2020-07-14 13:26:06 +02001307 /* init context */
1308 lydctx = calloc(1, sizeof *lydctx);
1309 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1310 lydctx->parse_options = parse_options;
1311 lydctx->validate_options = validate_options;
1312 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001313
Radek Krejci284f31f2020-09-18 15:40:29 +02001314 /* starting top-level */
1315 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001316 if (in->current[i] == '\n') {
1317 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001318 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001319 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001320 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001321
Radek Krejci284f31f2020-09-18 15:40:29 +02001322 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001323 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejcibb27df32020-11-13 15:39:16 +01001324 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001325 *lydctx_p = lydctx;
1326 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001327 } else {
1328 /* expecting top-level object */
Radek Krejcid54412f2020-12-17 20:25:35 +01001329 LOGVAL(ctx, LY_VLOG_LINE, &in->line, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.",
Radek Krejcicd5f6222020-11-12 08:54:13 +01001330 lyjson_token2str(*status));
1331 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001332 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001333 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001334 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001335}
1336
1337LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001338lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001339 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001340{
1341 LY_ERR ret = LY_SUCCESS;
1342 struct lyd_json_ctx *lydctx = NULL;
1343 enum LYJSON_PARSER_STATUS status;
1344
1345 assert(tree_p);
1346 *tree_p = NULL;
1347
Radek Krejcicd5f6222020-11-12 08:54:13 +01001348 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx, &status);
1349 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001350
Radek Krejcicd5f6222020-11-12 08:54:13 +01001351 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001352
1353 /* read subtree(s) */
1354 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1355 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1356 LY_CHECK_GOTO(ret, cleanup);
1357
1358 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1359 }
1360
1361 /* finish linking metadata */
1362 ret = lydjson_metadata_finish(lydctx, tree_p);
1363 LY_CHECK_GOTO(ret, cleanup);
1364
1365cleanup:
1366 /* there should be no unresolved types stored */
Michal Vasko49c39d82020-11-06 17:20:27 +01001367 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1368 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001369
1370 if (ret) {
1371 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1372 lyd_free_all(*tree_p);
1373 *tree_p = NULL;
1374 } else {
1375 *lydctx_p = (struct lyd_ctx *)lydctx;
1376 }
1377
1378 return ret;
1379}
1380
Michal Vasko2552ea32020-12-08 15:32:34 +01001381#if 0
Radek Krejci1798aae2020-07-14 13:26:06 +02001382/**
1383 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1384 *
1385 * @param[in] jsonctx JSON parser context
1386 * @param[out] envp_p Pointer to the created envelope opaq container.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001387 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001388 * @return LY_SUCCESS if the envelope present and successfully parsed.
1389 * @return LY_ENOT in case there is not the expected envelope.
1390 * @return LY_ERR in case of parsing failure.
1391 */
1392static LY_ERR
Radek Krejcicd5f6222020-11-12 08:54:13 +01001393lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p, enum LYJSON_PARSER_STATUS *status_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001394{
1395 LY_ERR ret = LY_ENOT, r;
1396 const char *name, *prefix, *value = NULL;
1397 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001398 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001399 enum LYJSON_PARSER_STATUS status;
1400 struct lyd_node *et;
1401
1402 *envp_p = NULL;
1403
1404 /* backup the context */
1405 lyjson_ctx_backup(jsonctx);
1406
1407 /* "notification" envelope */
1408 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1409 LY_CHECK_GOTO(is_attr, cleanup);
1410 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1411 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1412
1413 r = lyjson_ctx_next(jsonctx, &status);
1414 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1415 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1416
1417 /* "eventTime" child */
1418 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1419 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1420 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1421
1422 /* go for the data */
1423 r = lyjson_ctx_next(jsonctx, &status);
1424 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1425 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1426
1427 value = jsonctx->value;
1428 value_len = jsonctx->value_len;
1429 dynamic = jsonctx->dynamic;
1430 jsonctx->dynamic = 0;
1431
1432 r = lyjson_ctx_next(jsonctx, &status);
1433 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1434 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1435 /* now the notificationContent is expected, which will be parsed by the caller */
1436
1437 /* create notification envelope */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001438 ret = lyd_create_opaq(jsonctx->ctx, "notification", ly_strlen_const("notification"), NULL, 0,
1439 "ietf-restconf", ly_strlen_const("ietf-restconf"), "", ly_strlen_const(""), NULL, LY_PREF_JSON, NULL,
1440 LYD_NODEHINT_ENVELOPE, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001441 LY_CHECK_GOTO(ret, cleanup);
1442 /* create notification envelope */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001443 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", ly_strlen_const("eventTime"), NULL, 0,
1444 "ietf-restconf", ly_strlen_const("ietf-restconf"), value, value_len, &dynamic, LY_PREF_JSON, NULL,
1445 LYD_VALHINT_STRING, &et);
Radek Krejci1798aae2020-07-14 13:26:06 +02001446 LY_CHECK_GOTO(ret, cleanup);
1447 /* insert eventTime into notification */
1448 lyd_insert_node(*envp_p, NULL, et);
1449
1450 ret = LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001451 *status_p = status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001452cleanup:
1453 if (ret) {
1454 /* restore the context */
1455 lyjson_ctx_restore(jsonctx);
1456 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001457 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001458 }
1459 }
1460 return ret;
1461}
Michal Vasko79135ae2020-12-16 10:08:35 +01001462
Michal Vasko2552ea32020-12-08 15:32:34 +01001463#endif
Radek Krejci1798aae2020-07-14 13:26:06 +02001464
1465LY_ERR
1466lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1467{
1468 LY_ERR ret = LY_SUCCESS;
1469 struct lyd_json_ctx *lydctx = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001470 struct lyd_node *tree = NULL;
1471 enum LYJSON_PARSER_STATUS status;
1472
1473 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001474 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1475 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1476
Radek Krejci1798aae2020-07-14 13:26:06 +02001477 lydctx->int_opts = LYD_INTOPT_NOTIF;
1478
Michal Vasko2552ea32020-12-08 15:32:34 +01001479#if 0
Radek Krejci1798aae2020-07-14 13:26:06 +02001480 /* parse "notification" and "eventTime", if present */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001481 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001482 if (ret == LY_ENOT) {
1483 ret = LY_SUCCESS;
1484 } else if (ret) {
1485 goto cleanup;
1486 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001487#endif
Radek Krejci1798aae2020-07-14 13:26:06 +02001488
Radek Krejcicd5f6222020-11-12 08:54:13 +01001489 assert(status == LYJSON_OBJECT);
1490
Michal Vaskocf770e22020-08-12 13:21:43 +02001491 /* read subtree */
1492 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1493 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001494
1495 /* finish linking metadata */
1496 ret = lydjson_metadata_finish(lydctx, &tree);
1497 LY_CHECK_GOTO(ret, cleanup);
1498
1499 /* make sure we have parsed some notification */
1500 if (!lydctx->op_node) {
1501 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1502 ret = LY_EVALID;
1503 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001504 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Radek Krejcid54412f2020-12-17 20:25:35 +01001505 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->in->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001506 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001507 ret = LY_EVALID;
1508 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001509 }
1510
Radek Krejci1798aae2020-07-14 13:26:06 +02001511 if (ntf_p) {
1512 *ntf_p = lydctx->op_node;
1513 }
1514 assert(tree);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001515 if (tree_p) {
1516 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001517 }
1518
1519cleanup:
1520 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001521 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001522
1523 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1524 if (ret) {
1525 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001526 }
1527 return ret;
1528}
1529
Michal Vasko2552ea32020-12-08 15:32:34 +01001530#if 0
Radek Krejci1798aae2020-07-14 13:26:06 +02001531/**
1532 * @brief Parse optional JSON envelope around the processed content.
1533 *
1534 * @param[in] jsonctx JSON parser context
1535 * @param[in] parent Parent node (some other envelope).
1536 * @param[in] module_key Name of the module where the envelope element is expected.
1537 * @param[in] object_id Name of the envelope object.
1538 * @param[out] envp_p Pointer to the created envelope opaq container.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001539 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001540 * @return LY_SUCCESS if the envelope present and successfully parsed.
1541 * @return LY_ENOT in case there is not the expected envelope.
1542 * @return LY_ERR in case of parsing failure.
1543 */
1544static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001545lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001546 const char *object_id, struct lyd_node **envp_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001547{
1548 LY_ERR ret = LY_ENOT, r;
1549 const char *name, *prefix;
1550 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001551 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001552
1553 *envp_p = NULL;
1554
1555 /* "id" envelope */
1556 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1557 LY_CHECK_GOTO(is_attr, cleanup);
1558 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1559 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1560 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1561
Radek Krejcicd5f6222020-11-12 08:54:13 +01001562 r = lyjson_ctx_next(jsonctx, status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001563 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001564 LY_CHECK_GOTO(*status != LYJSON_OBJECT, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001565
1566 /* create the object envelope */
Michal Vasko501af032020-11-11 20:27:44 +01001567 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), NULL, 0, module_key, ly_strlen(module_key), "", 0,
1568 NULL, LY_PREF_JSON, NULL, LYD_NODEHINT_ENVELOPE, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001569 LY_CHECK_GOTO(ret, cleanup);
1570
1571 if (parent) {
1572 lyd_insert_node(parent, NULL, *envp_p);
1573 }
1574
1575 ret = LY_SUCCESS;
1576cleanup:
1577 return ret;
1578}
1579
1580static LY_ERR
Radek Krejcicd5f6222020-11-12 08:54:13 +01001581lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001582{
Radek Krejcicd5f6222020-11-12 08:54:13 +01001583 LY_CHECK_RET(lyjson_ctx_next(jsonctx, status));
1584 if (*status == LYJSON_END) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001585 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1586 return LY_EVALID;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001587 } else if (*status != LYJSON_OBJECT_CLOSED) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001588 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001589 jsonctx->value_len, jsonctx->value, object_id);
Radek Krejci1798aae2020-07-14 13:26:06 +02001590 return LY_EVALID;
1591 }
1592 return LY_SUCCESS;
1593}
Michal Vasko79135ae2020-12-16 10:08:35 +01001594
Michal Vasko2552ea32020-12-08 15:32:34 +01001595#endif
Radek Krejci1798aae2020-07-14 13:26:06 +02001596
1597LY_ERR
1598lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1599{
1600 LY_ERR ret = LY_SUCCESS;
1601 struct lyd_json_ctx *lydctx = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001602 struct lyd_node *tree = NULL;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001603 enum LYJSON_PARSER_STATUS status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001604
1605 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001606 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1607 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1608
Radek Krejci1798aae2020-07-14 13:26:06 +02001609 lydctx->int_opts = LYD_INTOPT_RPC;
1610
Michal Vasko2552ea32020-12-08 15:32:34 +01001611#if 0
Radek Krejci1798aae2020-07-14 13:26:06 +02001612 /* process envelope(s), if present */
1613
1614 /* process rpc */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001615 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001616 if (ret == LY_ENOT) {
1617 ret = LY_SUCCESS;
1618 goto parse_content;
1619 } else if (ret) {
1620 goto cleanup;
1621 }
1622 /* process action */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001623 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001624 if (ret == LY_ENOT) {
1625 ret = LY_SUCCESS;
1626 goto parse_content;
1627 } else if (ret) {
1628 goto cleanup;
1629 }
1630
1631parse_content:
Michal Vasko2552ea32020-12-08 15:32:34 +01001632#endif
Radek Krejcicd5f6222020-11-12 08:54:13 +01001633 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001634
1635 /* read subtree(s) */
Michal Vasko2552ea32020-12-08 15:32:34 +01001636 ret = lydjson_subtree_r(lydctx, NULL, &tree);
Michal Vaskocf770e22020-08-12 13:21:43 +02001637 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001638
1639 /* finish linking metadata */
1640 ret = lydjson_metadata_finish(lydctx, &tree);
1641 LY_CHECK_GOTO(ret, cleanup);
1642
1643 /* make sure we have parsed some operation */
1644 if (!lydctx->op_node) {
Michal Vasko2552ea32020-12-08 15:32:34 +01001645 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the rpc/action node.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001646 ret = LY_EVALID;
1647 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001648 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Radek Krejcid54412f2020-12-17 20:25:35 +01001649 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->in->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001650 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001651 ret = LY_EVALID;
1652 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001653 }
1654
Radek Krejci1798aae2020-07-14 13:26:06 +02001655 if (op_p) {
1656 *op_p = lydctx->op_node;
1657 }
1658 assert(tree);
1659 if (tree_p) {
Michal Vasko2552ea32020-12-08 15:32:34 +01001660 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001661 }
1662
1663cleanup:
1664 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001665 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001666
1667 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1668 if (ret) {
1669 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001670 }
1671 return ret;
1672}
1673
1674LY_ERR
Michal Vasko2552ea32020-12-08 15:32:34 +01001675lyd_parse_json_reply(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001676{
1677 LY_ERR ret = LY_SUCCESS;
1678 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko2552ea32020-12-08 15:32:34 +01001679 struct lyd_node *tree = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001680 enum LYJSON_PARSER_STATUS status;
1681
1682 /* init */
Michal Vasko2552ea32020-12-08 15:32:34 +01001683 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001684 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1685
Radek Krejci1798aae2020-07-14 13:26:06 +02001686 lydctx->int_opts = LYD_INTOPT_REPLY;
1687
Michal Vasko2552ea32020-12-08 15:32:34 +01001688#if 0
Radek Krejci1798aae2020-07-14 13:26:06 +02001689 /* parse "rpc-reply", if any */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001690 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001691 if (ret == LY_ENOT) {
1692 ret = LY_SUCCESS;
1693 } else if (ret) {
1694 goto cleanup;
1695 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001696#endif
Radek Krejci1798aae2020-07-14 13:26:06 +02001697
Radek Krejci1798aae2020-07-14 13:26:06 +02001698 assert(status == LYJSON_OBJECT);
1699
1700 /* read subtree(s) */
1701 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko2552ea32020-12-08 15:32:34 +01001702 ret = lydjson_subtree_r(lydctx, NULL, &tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001703 LY_CHECK_GOTO(ret, cleanup);
1704
1705 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1706 }
1707
1708 /* finish linking metadata */
Michal Vasko2552ea32020-12-08 15:32:34 +01001709 ret = lydjson_metadata_finish(lydctx, &tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001710 LY_CHECK_GOTO(ret, cleanup);
1711
Radek Krejci1798aae2020-07-14 13:26:06 +02001712 if (op_p) {
Michal Vasko79135ae2020-12-16 10:08:35 +01001713 *op_p = lydctx->op_node;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001714 }
1715 if (tree_p) {
1716 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001717 }
1718
1719cleanup:
1720 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001721 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001722
1723 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1724 if (ret) {
Michal Vasko2552ea32020-12-08 15:32:34 +01001725 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001726 }
1727 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001728}