blob: 3f426a49181c882ef40d224145525d275751a60d [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"
28#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 {
44 uint32_t parse_options; /**< various @ref dataparseroptions. */
45 uint32_t validate_options; /**< various @ref datavalidationoptions. */
46 uint32_t int_opts; /**< internal data parser options */
47 uint32_t path_len; /**< used bytes in the path buffer */
48 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vasko49c39d82020-11-06 17:20:27 +010049 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
50 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
51 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci1798aae2020-07-14 13:26:06 +020052 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
53
54 /* callbacks */
55 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020056
57 struct lyjson_ctx *jsonctx; /**< JSON context */
58};
59
60/**
61 * @brief Free the JSON data parser context.
62 *
63 * JSON implementation of lyd_ctx_free_clb().
64 */
65static void
66lyd_json_ctx_free(struct lyd_ctx *lydctx)
67{
68 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
69
70 if (lydctx) {
71 lyd_ctx_free(lydctx);
72 lyjson_ctx_free(ctx->jsonctx);
73 free(ctx);
74 }
75}
76
77/**
Radek Krejci1798aae2020-07-14 13:26:06 +020078 * @brief Parse JSON member-name as [\@][prefix:][name]
79 *
Michal Vaskocabe0702020-08-12 10:14:36 +020080 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020081 * prefix - name of the module of the data node
82 * name - name of the data node
83 *
84 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
85 *
86 * @param[in] value String to parse
87 * @param[in] value_len Length of the @p str.
88 * @param[out] name_p Pointer to the beginning of the parsed name.
89 * @param[out] name_len_p Pointer to the length of the parsed name.
90 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
91 * @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 +020092 * @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 +020093 */
94static void
Michal Vaskocabe0702020-08-12 10:14:36 +020095lydjson_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 +020096 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020097{
98 const char *name, *prefix = NULL;
99 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200100 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200101
102 name = memchr(value, ':', value_len);
103 if (name != NULL) {
104 prefix = value;
105 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200106 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200107 prefix++;
108 }
109 prefix_len = name - prefix;
110 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200111 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200112 } else {
113 name = value;
114 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200115 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 name++;
117 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200118 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100119 }
120
Radek Krejci1798aae2020-07-14 13:26:06 +0200121 *name_p = name;
122 *name_len_p = name_len;
123 *prefix_p = prefix;
124 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200125 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200126}
127
128/**
129 * @brief Get correct prefix (module_name) inside the @p node.
130 *
131 * @param[in] node Data node to get inherited prefix.
132 * @param[in] local_prefix Local prefix to replace the inherited prefix.
133 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
134 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
135 * and no context @p node to get inherited prefix.
136 * @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
137 * of no local prefix and no context @p node to get inherited prefix.
138 * @return LY_ERR value.
139 */
140static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200141lydjson_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 +0200142 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200143{
144 struct lyd_node_opaq *onode;
145 const char *module_name = NULL;
146
147 assert(prefix_p && prefix_len_p);
148
149 if (local_prefix_len) {
150 *prefix_p = local_prefix;
151 *prefix_len_p = local_prefix_len;
152 return LY_SUCCESS;
153 }
154
155 *prefix_p = NULL;
156 while (node) {
157 if (node->schema) {
158 *prefix_p = node->schema->module->name;
159 break;
160 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200161 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100162 if (onode->name.module_name) {
163 *prefix_p = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200164 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100165 } else if (onode->name.prefix) {
166 *prefix_p = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200167 break;
168 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200169 node = (struct lyd_node *)node->parent;
Radek Krejci1798aae2020-07-14 13:26:06 +0200170 }
171 *prefix_len_p = ly_strlen(module_name);
172
173 return LY_SUCCESS;
174}
175
176/**
177 * @brief Get schema node corresponding to the input parameters.
178 *
179 * @param[in] lydctx JSON data parser context.
180 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
181 * @param[in] prefix Requested node's prefix (module name).
182 * @param[in] prefix_len Length of the @p prefix.
183 * @param[in] name Requested node's name.
184 * @param[in] name_len Length of the @p name.
185 * @param[in] parent Parent of the node beeing processed, can be NULL in case of top-level. Also the opaq node with LYD_NODE_OPAQ_ISENVELOPE hint
186 * is accepted for searching top-level nodes.
187 * @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{
196 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100197 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200198
199 /* init return value */
200 *snode_p = NULL;
201
202 /* get the element module */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200203 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200204 if (!prefix_len) {
205 /* opaq parent (envelope) - the second part of the condition */
Michal Vasko22df3f02020-08-24 13:29:22 +0200206 lydjson_get_node_prefix((struct lyd_node *)parent, NULL, 0, &prefix, &prefix_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200207 }
208 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
209 } else if (parent) {
210 if (parent->schema) {
211 mod = parent->schema->module;
212 }
213 } else {
214 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 +0200215 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200216 return LY_EVALID;
217 }
218 if (!mod) {
219 if (lydctx->parse_options & LYD_PARSE_STRICT) {
220 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
221 return LY_EVALID;
222 }
223 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
224 return LY_ENOT;
225 }
226 }
227
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200228 if (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200229 /* ignore the envelope parent when searchinf for the schema node */
230 parent = NULL;
231 }
232
233 /* get the schema node */
234 if (mod && (!parent || parent->schema)) {
235 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
236 if (!*snode_p) {
237 if (lydctx->parse_options & LYD_PARSE_STRICT) {
238 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 +0200239 name_len, name, mod->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200240 return LY_EVALID;
241 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
242 /* skip element with children */
243 return LY_ENOT;
244 }
245 } else {
246 /* check that schema node is valid and can be used */
247 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
248 }
249 }
250
251 return LY_SUCCESS;
252}
253
254/**
255 * @brief Skip the currently open JSON object/array
256 * @param[in] jsonctx JSON context with the input data to skip.
257 * @return LY_ERR value.
258 */
259static LY_ERR
260lydjson_data_skip(struct lyjson_ctx *jsonctx)
261{
262 enum LYJSON_PARSER_STATUS status, current;
263 size_t sublevels = 1;
264
265 status = lyjson_ctx_status(jsonctx, 0);
266
267 /* skip after the content */
268 do {
269 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
270 if (current == status) {
271 sublevels++;
272 } else if (current == status + 1) {
273 sublevels--;
274 }
Radek Krejci79856c42020-11-19 11:44:08 +0100275 } while (current != status + 1 || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200276 /* open the next sibling */
277 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
278
279 return LY_SUCCESS;
280}
281
282/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200283 * @brief Check that the input data are parseable as the @p list.
284 *
285 * Checks for all the list's keys. Function does not revert the context state.
286 *
287 * @param[in] jsonctx JSON parser context.
288 * @param[in] list List schema node corresponding to the input data object.
289 * @return LY_SUCCESS in case the data are ok for the @p list
290 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
291 */
292static LY_ERR
293lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
294{
295 LY_ERR ret = LY_SUCCESS;
296 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
297 struct ly_set key_set = {0};
298 const struct lysc_node *snode;
299 uint32_t i, status_count;
300
301 assert(list && (list->nodetype == LYS_LIST));
302 assert(status == LYJSON_OBJECT);
303
304 /* get all keys into a set (keys do not have if-features or anything) */
305 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100306 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200307 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200308 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200309 }
310
311 if (status != LYJSON_OBJECT_EMPTY) {
312 status_count = jsonctx->status.count;
313
314 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
315 const char *name, *prefix;
316 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200317 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200318
319 /* match the key */
320 snode = NULL;
321 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
322
323 if (!is_attr && !prefix) {
324 for (i = 0; i < key_set.count; ++i) {
325 snode = (const struct lysc_node *)key_set.objs[i];
326 if (!ly_strncmp(snode->name, name, name_len)) {
327 break;
328 }
329 }
330 /* go into the item to a) process it as a key or b) start skipping it as another list child */
331 ret = lyjson_ctx_next(jsonctx, &status);
332 LY_CHECK_GOTO(ret, cleanup);
333
334 if (snode) {
335 /* we have the key, validate the value */
336 if (status < LYJSON_NUMBER) {
337 /* not a terminal */
338 ret = LY_ENOT;
339 goto cleanup;
340 }
341
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200342 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200343 LY_CHECK_GOTO(ret, cleanup);
344
345 /* key with a valid value, remove from the set */
346 ly_set_rm_index(&key_set, i, NULL);
347 }
348 } else {
349 /* start skipping the member we are not interested in */
350 ret = lyjson_ctx_next(jsonctx, &status);
351 LY_CHECK_GOTO(ret, cleanup);
352 }
353 /* move to the next child */
354 while (status_count < jsonctx->status.count) {
355 ret = lyjson_ctx_next(jsonctx, &status);
356 LY_CHECK_GOTO(ret, cleanup);
357 }
358 }
359 }
360
361 if (key_set.count) {
362 /* some keys are missing/did not validate */
363 ret = LY_ENOT;
364 }
365
366cleanup:
367 ly_set_erase(&key_set, NULL);
368 return ret;
369}
370
371/**
372 * @brief Get the hint for the data type parsers according to the current JSON parser context.
373 *
374 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
375 * @param[in,out] status Pointer to the current context status,
376 * in some circumstances the function manipulates with the context so the status is updated.
377 * @param[out] type_hint_p Pointer to the variable to store the result.
378 * @return LY_SUCCESS in case of success.
379 * @return LY_EINVAL in case of invalid context status not referring to a value.
380 */
381static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200382lydjson_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 +0200383{
384 *type_hint_p = 0;
385
386 if (*status_p == LYJSON_ARRAY) {
387 /* only [null] */
388 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
389 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
390
391 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
392 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
393
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200394 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200395 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200396 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200398 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200399 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200400 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 } else if (*status_p == LYJSON_NULL) {
402 *type_hint_p = 0;
403 } else {
404 return LY_EINVAL;
405 }
406
407 return LY_SUCCESS;
408}
409
410/**
411 * @brief Check in advance if the input data are parsable according to the provided @p snode.
412 *
413 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
414 * is naturally done when the data are really parsed.
415 *
416 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
417 * as before calling, despite it is necessary to process input data for checking.
418 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
419 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
420 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
421 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
422 * @return LY_EINVAL in case of invalid leaf JSON encoding
423 * and they are expected to be parsed as opaq nodes.
424 */
425static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200426lydjson_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 +0200427{
428 LY_ERR ret = LY_SUCCESS;
429 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
430 enum LYJSON_PARSER_STATUS status;
431
432 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200433
Michal Vasko32ac9942020-08-12 14:35:12 +0200434 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
435 /* can always be parsed as a data node if we have the schema node */
436 return LY_SUCCESS;
437 }
438
439 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200440 /* backup parser */
441 lyjson_ctx_backup(jsonctx);
442 status = lyjson_ctx_status(jsonctx, 0);
443
Michal Vasko32ac9942020-08-12 14:35:12 +0200444 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
445 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200446 switch (snode->nodetype) {
447 case LYS_LEAFLIST:
448 case LYS_LEAF:
449 /* value may not be valid in which case we parse it as an opaque node */
450 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
451 if (ret) {
452 break;
453 }
454
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200455 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200456 ret = LY_ENOT;
457 }
458 break;
459 case LYS_LIST:
460 /* lists may not have all its keys */
461 if (lydjson_check_list(jsonctx, snode)) {
462 /* invalid list, parse as opaque if it missing/has invalid some keys */
463 ret = LY_ENOT;
464 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200465 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200466 }
467
468 /* restore parser */
469 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200470 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200471 status = lyjson_ctx_status(jsonctx, 0);
472 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
473 }
474
475 return ret;
476}
477
478/**
479 * @brief Join the forward-referencing metadata with their target data nodes.
480 *
481 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
482 *
483 * @param[in] lydctx JSON data parser context.
484 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
485 * as a starting point to search for the metadata's target data node
486 * @return LY_SUCCESS on success
487 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
488 */
489static LY_ERR
490lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
491{
492 LY_ERR ret = LY_SUCCESS;
493 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200494 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200495 const char *prev = NULL;
496
497 /* finish linking metadata */
498 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200499 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200500 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200501 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200502 const char *name, *prefix;
503 size_t name_len, prefix_len;
504 const struct lysc_node *snode;
505
Michal Vaskoad92b672020-11-12 13:11:31 +0100506 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200507 /* not an opaq metadata node */
508 continue;
509 }
510
Michal Vaskoad92b672020-11-12 13:11:31 +0100511 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200512 /* metas' names are stored in dictionary, so checking pointers must works */
513 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100514 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 instance = 1;
516 } else {
517 instance++;
518 }
519
520 /* find the correspnding data node */
521 LY_LIST_FOR(start, node) {
522 if (!node->schema) {
523 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100524 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 continue;
526 }
527
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200528 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200529 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vaskoad92b672020-11-12 13:11:31 +0100530 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200531 ret = LY_EVALID;
532 goto cleanup;
533 }
534
535 /* match */
536 match++;
537 if (match != instance) {
538 continue;
539 }
540
541 LY_LIST_FOR(meta_container->child, meta_iter) {
542 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200543 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100544
Michal Vaskoad92b672020-11-12 13:11:31 +0100545 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
546 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
547 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_PREF_JSON,
548 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200549 LY_CHECK_GOTO(ret, cleanup);
550 }
551
552 /* done */
553 break;
554 } else {
555 /* this is the second time we are resolving the schema node, so it must succeed,
556 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100557 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
558 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200559 assert(is_attr);
560 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
561 assert(ret == LY_SUCCESS);
562
563 if (snode != node->schema) {
564 continue;
565 }
566
567 /* match */
568 match++;
569 if (match != instance) {
570 continue;
571 }
572
573 LY_LIST_FOR(meta_container->child, meta_iter) {
574 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200575 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200576 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200577 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200578
Michal Vaskoad92b672020-11-12 13:11:31 +0100579 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200580 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200581 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100582 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vasko69730152020-10-09 16:30:07 +0200583 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200584 LY_CHECK_GOTO(ret, cleanup);
585 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
586 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200587 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
Michal Vaskoad92b672020-11-12 13:11:31 +0100588 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", meta->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200589 ret = LY_EVALID;
590 goto cleanup;
591 }
592 }
593 /* add/correct flags */
Michal Vasko49c39d82020-11-06 17:20:27 +0100594 lyd_parse_set_data_flags(node, &lydctx->node_when, &node->meta, lydctx->parse_options);
Radek Krejci1798aae2020-07-14 13:26:06 +0200595
596 /* done */
597 break;
598 }
599 }
600
601 if (match != instance) {
602 /* there is no corresponding data node for the metadata */
603 if (instance > 1) {
604 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200605 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
Michal Vaskoad92b672020-11-12 13:11:31 +0100606 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200607 } else {
608 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vaskoad92b672020-11-12 13:11:31 +0100609 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200610 }
611 ret = LY_EVALID;
612 } else {
613 /* remove the opaq attr */
614 if (attr == (*first_p)) {
615 *first_p = attr->next;
616 }
617 lyd_free_tree(attr);
618 }
619 }
620
621cleanup:
622 lydict_remove(lydctx->jsonctx->ctx, prev);
623
624 return ret;
625}
626
627/**
628 * @brief Parse a metadata member.
629 *
630 * @param[in] lydctx JSON data parser context.
631 * @param[in] snode Schema node of the metadata parent.
632 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
633 * so the data node does not exists. In such a case the metadata is stored in the context for the later
634 * processing by lydjson_metadata_finish().
635 * @return LY_SUCCESS on success
636 * @return Various LY_ERR values in case of failure.
637 */
638static LY_ERR
639lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
640{
641 LY_ERR ret = LY_SUCCESS;
642 enum LYJSON_PARSER_STATUS status;
643 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200644 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200645 const char *name, *prefix = NULL;
646 size_t name_len, prefix_len = 0;
647 struct lys_module *mod;
648 struct lyd_meta *meta = NULL;
649 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200650 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200651 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200652 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200653 uint16_t nodetype;
654
655 assert(snode || node);
656
657 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
658
659 /* move to the second item in the name/X pair */
660 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
661 LY_CHECK_GOTO(ret, cleanup);
662
663 /* check attribute encoding */
664 switch (nodetype) {
665 case LYS_LEAFLIST:
666 expected = "@name/array of objects/nulls";
667
668 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
669
670next_entry:
671 instance++;
672
673 /* move into array / next entry */
674 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
675 LY_CHECK_GOTO(ret, cleanup);
676
677 if (status == LYJSON_ARRAY_CLOSED) {
678 /* we are done, move after the array */
679 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
680 goto cleanup;
681 }
682 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
683
Michal Vasko69730152020-10-09 16:30:07 +0200684 if (!node || (node->schema != prev->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200685 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200686 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
687 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200688 ret = LY_EVALID;
689 goto cleanup;
690 }
691
692 if (status == LYJSON_NULL) {
693 /* continue with the next entry in the leaf-list array */
694 prev = node;
695 node = node->next;
696 goto next_entry;
697 }
698 break;
699 case LYS_LEAF:
700 case LYS_ANYXML:
701 expected = "@name/object";
702
703 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
704 break;
705 case LYS_CONTAINER:
706 case LYS_LIST:
707 case LYS_ANYDATA:
708 case LYS_NOTIF:
709 case LYS_ACTION:
710 case LYS_RPC:
711 in_parent = 1;
712 expected = "@/object";
713 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
714 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200715 default:
716 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200717 }
718
719 /* process all the members inside a single metadata object */
720 assert(status == LYJSON_OBJECT);
721
722 while (status != LYJSON_OBJECT_CLOSED) {
723 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
724 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200725 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200726 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
727 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200728 ret = LY_EVALID;
729 goto cleanup;
730 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200731 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200732 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
733 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 ret = LY_EVALID;
735 goto cleanup;
736 }
737
738 /* get the element module */
739 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
740 if (!mod) {
741 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200742 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200743 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
744 prefix_len, prefix, name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200745 ret = LY_EVALID;
746 goto cleanup;
747 }
748 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
749 /* skip element with children */
750 ret = lydjson_data_skip(lydctx->jsonctx);
751 LY_CHECK_GOTO(ret, cleanup);
752 status = lyjson_ctx_status(lydctx->jsonctx, 0);
753 /* end of the item */
754 continue;
755 }
756 }
757
758 /* get the value */
759 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
760 LY_CHECK_GOTO(ret, cleanup);
761
762 if (node->schema) {
763 /* create metadata */
764 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200765 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 +0200766 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
767 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200768 LY_CHECK_GOTO(ret, cleanup);
769
770 /* add/correct flags */
Michal Vasko49c39d82020-11-06 17:20:27 +0100771 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_options);
Radek Krejci1798aae2020-07-14 13:26:06 +0200772 } else {
773 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200774 const char *module_name;
775 size_t module_name_len;
776
777 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
778
Radek Krejci1798aae2020-07-14 13:26:06 +0200779 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100780 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
781 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
782 LY_PREF_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200783 LY_CHECK_GOTO(ret, cleanup);
784 }
785 /* next member */
786 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
787 LY_CHECK_GOTO(ret, cleanup);
788 }
789
790 if (nodetype == LYS_LEAFLIST) {
791 /* continue by processing another metadata object for the following
792 * leaf-list instance since they are allways instantiated in JSON array */
793 prev = node;
794 node = node->next;
795 goto next_entry;
796 }
797
798 /* move after the metadata */
799 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
800 LY_CHECK_GOTO(ret, cleanup);
801
802cleanup:
803 return ret;
804
805representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200806 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200807 "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 +0100808 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name.name,
Michal Vasko69730152020-10-09 16:30:07 +0200809 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200810
811 return LY_EVALID;
812}
813
814/**
815 * @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.
816 *
817 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
818 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
819 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
820 */
821static void
822lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
823{
824 if (*node_p) {
825 /* insert, keep first pointer correct */
826 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
827 if (first_p) {
828 if (parent) {
829 *first_p = parent->child;
830 } else {
831 while ((*first_p)->prev->next) {
832 *first_p = (*first_p)->prev;
833 }
834 }
835 }
836 *node_p = NULL;
837 }
838}
839
840static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
841
842/**
843 * @brief Parse opaq node from the input.
844 *
845 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
846 *
847 * @param[in] lydctx JSON data parser context.
848 * @param[in] name Name of the opaq node to create.
849 * @param[in] name_len Length of the @p name string.
850 * @param[in] prefix Prefix of the opaq node to create.
851 * @param[in] prefix_len Length of the @p prefx string.
852 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
853 * but must be set if @p first is not.
854 * @param[in,out] status_p Pointer to the current status of the parser context,
855 * since the function manipulates with the context and process the input, the status can be updated.
856 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
857 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
858 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
859 * @param[out] node_p Pointer to the created opaq node.
860 * @return LY_ERR value.
861 */
862static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200863lydjson_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 +0200864 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
865 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200866{
867 LY_ERR ret = LY_SUCCESS;
868 const char *value = NULL, *module_name;
869 size_t value_len = 0, module_name_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200870 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200871 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200872
Michal Vasko69730152020-10-09 16:30:07 +0200873 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200874 /* prepare for creating opaq node with a value */
875 value = lydctx->jsonctx->value;
876 value_len = lydctx->jsonctx->value_len;
877 dynamic = lydctx->jsonctx->dynamic;
878 lydctx->jsonctx->dynamic = 0;
879
880 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
Radek Krejci1798aae2020-07-14 13:26:06 +0200881 }
882
883 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200884 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vasko501af032020-11-11 20:27:44 +0100885 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
886 value_len, &dynamic, LY_PREF_JSON, NULL, type_hint, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200887 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200888 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200889 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200890 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200891
Michal Vasko69730152020-10-09 16:30:07 +0200892 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200893 /* process children */
894 while (*status_p != LYJSON_OBJECT_CLOSED && *status_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_p = lyjson_ctx_status(lydctx->jsonctx, 0);
897 }
Michal Vasko69730152020-10-09 16:30:07 +0200898 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200899 /* process another instance of the same node */
900 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200901 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200902
Michal Vasko69730152020-10-09 16:30:07 +0200903 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200904 /* but first process children of the object in the array */
905 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
906 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
907 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
908 }
909 }
910
911 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
912
913 /* continue with the next instance */
914 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
915 assert(node_p);
916 lydjson_maintain_children(parent, first_p, node_p);
917 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
918 }
919 }
920
921 /* finish linking metadata */
922 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
923
924 /* move after the item */
925 return lyjson_ctx_next(lydctx->jsonctx, status_p);
926}
927
928/**
929 * @brief Process the attribute container (starting by @)
930 *
931 * @param[in] lydctx JSON data parser context.
932 * @param[in] attr_node The data node referenced by the attribute container, if already known.
933 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
934 * @param[in] name Name of the opaq node to create.
935 * @param[in] name_len Length of the @p name string.
936 * @param[in] prefix Prefix of the opaq node to create.
937 * @param[in] prefix_len Length of the @p prefx string.
938 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
939 * but must be set if @p first is not.
940 * @param[in,out] status_p Pointer to the current status of the parser context,
941 * since the function manipulates with the context and process the input, the status can be updated.
942 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
943 * @param[out] node_p Pointer to the created opaq node.
944 * @return LY_ERR value.
945 */
946static LY_ERR
947lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +0200948 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
949 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
950 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200951{
952 LY_ERR ret = LY_SUCCESS;
953 enum LYJSON_PARSER_STATUS status_inner;
954
955 /* parse as an attribute to a node */
956 if (!attr_node && snode) {
957 /* try to find the instance */
958 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
959 if (iter->schema == snode) {
960 attr_node = iter;
961 break;
962 }
963 }
964 }
965 if (!attr_node) {
966 /* parse just as an opaq node with the name beginning with @,
967 * later we have to check that it belongs to a standard node
968 * and it is supposed to be converted to a metadata */
969 uint32_t prev_opts;
970
971 /* move into metadata */
972 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
973
974 if (*status_p == LYJSON_ARRAY) {
975 /* move into the array */
976 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
977 } else {
978 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
979 status_inner = LYJSON_ERROR;
980 }
981
982 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
983 prev_opts = lydctx->parse_options;
984 lydctx->parse_options &= ~LYD_PARSE_STRICT;
985 lydctx->parse_options |= LYD_PARSE_OPAQ;
986
987 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 +0200988 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200989
990 /* restore the parser options */
991 lydctx->parse_options = prev_opts;
992 } else {
993 ret = lydjson_metadata(lydctx, snode, attr_node);
994 }
995
996 return ret;
997}
998
999/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001000 * @brief Parse a single instance of a node.
1001 *
1002 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1003 * as before calling, despite it is necessary to process input data for checking.
1004 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1005 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1006 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1007 * @param[in] name Parsed JSON node name.
1008 * @param[in] name_len Lenght of @p name.
1009 * @param[in] prefix Parsed JSON node prefix.
1010 * @param[in] prefix_len Length of @p prefix.
1011 * @param[in,out] status JSON parser status, is updated.
1012 * @param[out] node Parsed data (or opaque) node.
1013 * @return LY_SUCCESS if a node was successfully parsed,
1014 * @return LY_EINVAL in case of invalid JSON encoding,
1015 * @return LY_ERR on other errors.
1016 */
1017static LY_ERR
1018lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001019 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1020 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001021{
1022 LY_ERR ret;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001023 uint32_t type_hints = 0;
Michal Vasko32ac9942020-08-12 14:35:12 +02001024 uint32_t prev_opts;
1025 struct lyd_node *tree = NULL;
1026
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001027 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001028 if (ret == LY_SUCCESS) {
1029 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1030 if (snode->nodetype & LYD_NODE_TERM) {
1031 /* create terminal node */
1032 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001033 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1034 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001035 LY_CHECK_RET(ret);
1036
1037 /* move JSON parser */
1038 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1039 LY_CHECK_RET(ret);
1040 } else if (snode->nodetype & LYD_NODE_INNER) {
1041 /* create inner node */
1042 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1043
1044 ret = lyd_create_inner(snode, node);
1045 LY_CHECK_RET(ret);
1046
1047 /* process children */
1048 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1049 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1050 LY_CHECK_RET(ret);
1051 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1052 }
1053
1054 /* finish linking metadata */
1055 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1056 LY_CHECK_RET(ret);
1057
1058 if (snode->nodetype == LYS_LIST) {
1059 /* check all keys exist */
1060 ret = lyd_parse_check_keys(*node);
1061 LY_CHECK_RET(ret);
1062 }
1063
1064 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1065 /* new node validation, autodelete CANNOT occur, all nodes are new */
1066 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1067 LY_CHECK_RET(ret);
1068
1069 /* add any missing default children */
Michal Vasko49c39d82020-11-06 17:20:27 +01001070 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->node_types,
1071 &lydctx->node_when, (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ?
Michal Vasko69730152020-10-09 16:30:07 +02001072 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001073 LY_CHECK_RET(ret);
1074 }
1075
1076 /* move JSON parser */
1077 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1078 LY_CHECK_RET(ret);
1079 } else if (snode->nodetype & LYD_NODE_ANY) {
1080 /* create any node */
1081 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1082
1083 /* parse any data tree with correct options */
1084 /* first backup the current options and then make the parser to process data as opaq nodes */
1085 prev_opts = lydctx->parse_options;
1086 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1087 lydctx->parse_options |= LYD_PARSE_OPAQ;
1088
1089 /* process the anydata content */
1090 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1091 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1092 LY_CHECK_RET(ret);
1093 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1094 }
1095
1096 /* restore parser options */
1097 lydctx->parse_options = prev_opts;
1098
1099 /* finish linking metadata */
1100 ret = lydjson_metadata_finish(lydctx, &tree);
1101 LY_CHECK_RET(ret);
1102
1103 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1104 LY_CHECK_RET(ret);
1105 }
1106 } else if (ret == LY_ENOT) {
1107 /* parse it again as an opaq node */
1108 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001109 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001110 LY_CHECK_RET(ret);
1111
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001112 if (snode->nodetype == LYS_LIST) {
1113 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1114 } else if (snode->nodetype == LYS_LEAFLIST) {
1115 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001116 }
1117 }
1118
1119 return ret;
1120}
1121
1122/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001123 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001124 *
1125 * @param[in] lydctx JSON data parser context.
1126 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1127 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1128 * @return LY_ERR value.
1129 */
1130static LY_ERR
1131lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1132{
1133 LY_ERR ret = LY_SUCCESS;
1134 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1135 enum LYJSON_PARSER_STATUS status_inner = 0;
1136 const char *name, *prefix = NULL;
1137 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001138 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001139 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001140 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001141 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1142 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001143
1144 assert(parent || first_p);
1145 assert(status == LYJSON_OBJECT);
1146
1147 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001148 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 +02001149
Michal Vaskocabe0702020-08-12 10:14:36 +02001150 if (!is_meta || name_len || prefix_len) {
1151 /* get the schema node */
1152 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1153 if (ret == LY_ENOT) {
1154 /* skip element with children */
1155 ret = lydjson_data_skip(lydctx->jsonctx);
1156 LY_CHECK_GOTO(ret, cleanup);
1157 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1158 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001159 goto cleanup;
1160 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001161 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001162
Michal Vaskocabe0702020-08-12 10:14:36 +02001163 if (!snode) {
1164 /* we will not be parsing it as metadata */
1165 is_meta = 0;
1166 }
1167 }
1168
1169 if (is_meta) {
1170 /* parse as metadata */
1171 if (!name_len && !prefix_len) {
1172 /* parent's metadata without a name - use the schema from the parent */
1173 if (!parent) {
1174 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001175 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001176 ret = LY_EVALID;
1177 goto cleanup;
1178 }
1179 attr_node = (struct lyd_node *)parent;
1180 snode = attr_node->schema;
1181 }
1182 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001183 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001184 LY_CHECK_GOTO(ret, cleanup);
1185 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001186 /* parse as an opaq node */
1187 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1188
1189 /* move to the second item in the name/X pair */
1190 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1191 LY_CHECK_GOTO(ret, cleanup);
1192
1193 if (status == LYJSON_ARRAY) {
1194 /* move into the array */
1195 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1196 LY_CHECK_GOTO(ret, cleanup);
1197 } else {
1198 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1199 status_inner = LYJSON_ERROR;
1200 }
1201
1202 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001203 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001204 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001205 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001206 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001207
1208 /* move to the second item in the name/X pair */
1209 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1210 LY_CHECK_GOTO(ret, cleanup);
1211
1212 /* first check the expected representation according to the nodetype and then continue with the content */
1213 switch (snode->nodetype) {
1214 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001215 case LYS_LIST:
1216 if (snode->nodetype == LYS_LEAFLIST) {
1217 expected = "name/array of values";
1218 } else {
1219 expected = "name/array of objects";
1220 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001221
1222 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1223
1224 /* move into array */
1225 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1226 LY_CHECK_GOTO(ret, cleanup);
1227
Michal Vasko32ac9942020-08-12 14:35:12 +02001228 /* process all the values/objects */
1229 do {
1230 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001231
Michal Vasko32ac9942020-08-12 14:35:12 +02001232 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001233 &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001234 if (ret == LY_EINVAL) {
1235 goto representation_error;
1236 } else if (ret) {
1237 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001238 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001239 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001240
Michal Vasko32ac9942020-08-12 14:35:12 +02001241 /* move after the array */
1242 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1243 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001244
1245 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001246 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001247 case LYS_CONTAINER:
1248 case LYS_NOTIF:
1249 case LYS_ACTION:
1250 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001251 case LYS_ANYDATA:
1252 case LYS_ANYXML:
1253 if (snode->nodetype == LYS_LEAF) {
1254 if (status == LYJSON_ARRAY) {
1255 expected = "name/[null]";
1256 } else {
1257 expected = "name/value";
1258 }
1259 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001260 expected = "name/object";
1261 }
1262
Michal Vasko32ac9942020-08-12 14:35:12 +02001263 /* process the value/object */
1264 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1265 if (ret == LY_EINVAL) {
1266 goto representation_error;
1267 } else if (ret) {
1268 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001269 }
1270
Michal Vasko32ac9942020-08-12 14:35:12 +02001271 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001272 /* rememeber the RPC/action/notification */
1273 lydctx->op_node = node;
1274 }
1275
1276 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001277 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001278 }
1279
Michal Vasko32ac9942020-08-12 14:35:12 +02001280 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001281 lydjson_maintain_children(parent, first_p, &node);
1282
1283cleanup:
1284 lyd_free_tree(node);
1285 return ret;
1286
1287representation_error:
1288 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001289 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1290 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejci1798aae2020-07-14 13:26:06 +02001291
1292 ret = LY_EVALID;
1293 goto cleanup;
1294}
1295
1296/**
1297 * @brief Common start of JSON parser processing different types of the input data.
1298 *
1299 * @param[in] ctx libyang context
1300 * @param[in] in Input structure.
1301 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1302 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1303 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001304 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001305 * @return LY_ERR value.
1306 */
1307static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001308lyd_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 +01001309 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001310{
1311 LY_ERR ret = LY_SUCCESS;
1312 struct lyd_json_ctx *lydctx;
1313 size_t i, line = 1;
1314
Radek Krejcicd5f6222020-11-12 08:54:13 +01001315 assert(lydctx_p);
1316 assert(status);
1317
Radek Krejci1798aae2020-07-14 13:26:06 +02001318 /* init context */
1319 lydctx = calloc(1, sizeof *lydctx);
1320 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1321 lydctx->parse_options = parse_options;
1322 lydctx->validate_options = validate_options;
1323 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001324
Radek Krejci284f31f2020-09-18 15:40:29 +02001325 /* starting top-level */
1326 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001327 if (in->current[i] == '\n') {
1328 /* new line */
Radek Krejci284f31f2020-09-18 15:40:29 +02001329 line++;
Michal Vasko61d76362020-10-07 10:47:30 +02001330 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001331 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001332
Radek Krejci284f31f2020-09-18 15:40:29 +02001333 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001334 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejcibb27df32020-11-13 15:39:16 +01001335 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001336 *lydctx_p = lydctx;
1337 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001338 } else {
1339 /* expecting top-level object */
1340 LOGVAL(ctx, LY_VLOG_LINE, &line, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.",
1341 lyjson_token2str(*status));
1342 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001343 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001344 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001345 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001346}
1347
1348LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001349lyd_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 +02001350 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001351{
1352 LY_ERR ret = LY_SUCCESS;
1353 struct lyd_json_ctx *lydctx = NULL;
1354 enum LYJSON_PARSER_STATUS status;
1355
1356 assert(tree_p);
1357 *tree_p = NULL;
1358
Radek Krejcicd5f6222020-11-12 08:54:13 +01001359 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx, &status);
1360 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001361
Radek Krejcicd5f6222020-11-12 08:54:13 +01001362 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001363
1364 /* read subtree(s) */
1365 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1366 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1367 LY_CHECK_GOTO(ret, cleanup);
1368
1369 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1370 }
1371
1372 /* finish linking metadata */
1373 ret = lydjson_metadata_finish(lydctx, tree_p);
1374 LY_CHECK_GOTO(ret, cleanup);
1375
1376cleanup:
1377 /* there should be no unresolved types stored */
Michal Vasko49c39d82020-11-06 17:20:27 +01001378 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1379 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001380
1381 if (ret) {
1382 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1383 lyd_free_all(*tree_p);
1384 *tree_p = NULL;
1385 } else {
1386 *lydctx_p = (struct lyd_ctx *)lydctx;
1387 }
1388
1389 return ret;
1390}
1391
1392/**
1393 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1394 *
1395 * @param[in] jsonctx JSON parser context
1396 * @param[out] envp_p Pointer to the created envelope opaq container.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001397 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001398 * @return LY_SUCCESS if the envelope present and successfully parsed.
1399 * @return LY_ENOT in case there is not the expected envelope.
1400 * @return LY_ERR in case of parsing failure.
1401 */
1402static LY_ERR
Radek Krejcicd5f6222020-11-12 08:54:13 +01001403lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p, enum LYJSON_PARSER_STATUS *status_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001404{
1405 LY_ERR ret = LY_ENOT, r;
1406 const char *name, *prefix, *value = NULL;
1407 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001408 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001409 enum LYJSON_PARSER_STATUS status;
1410 struct lyd_node *et;
1411
1412 *envp_p = NULL;
1413
1414 /* backup the context */
1415 lyjson_ctx_backup(jsonctx);
1416
1417 /* "notification" envelope */
1418 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1419 LY_CHECK_GOTO(is_attr, cleanup);
1420 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1421 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1422
1423 r = lyjson_ctx_next(jsonctx, &status);
1424 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1425 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1426
1427 /* "eventTime" child */
1428 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1429 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1430 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1431
1432 /* go for the data */
1433 r = lyjson_ctx_next(jsonctx, &status);
1434 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1435 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1436
1437 value = jsonctx->value;
1438 value_len = jsonctx->value_len;
1439 dynamic = jsonctx->dynamic;
1440 jsonctx->dynamic = 0;
1441
1442 r = lyjson_ctx_next(jsonctx, &status);
1443 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1444 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1445 /* now the notificationContent is expected, which will be parsed by the caller */
1446
1447 /* create notification envelope */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001448 ret = lyd_create_opaq(jsonctx->ctx, "notification", ly_strlen_const("notification"), NULL, 0,
1449 "ietf-restconf", ly_strlen_const("ietf-restconf"), "", ly_strlen_const(""), NULL, LY_PREF_JSON, NULL,
1450 LYD_NODEHINT_ENVELOPE, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001451 LY_CHECK_GOTO(ret, cleanup);
1452 /* create notification envelope */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001453 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", ly_strlen_const("eventTime"), NULL, 0,
1454 "ietf-restconf", ly_strlen_const("ietf-restconf"), value, value_len, &dynamic, LY_PREF_JSON, NULL,
1455 LYD_VALHINT_STRING, &et);
Radek Krejci1798aae2020-07-14 13:26:06 +02001456 LY_CHECK_GOTO(ret, cleanup);
1457 /* insert eventTime into notification */
1458 lyd_insert_node(*envp_p, NULL, et);
1459
1460 ret = LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001461 *status_p = status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001462cleanup:
1463 if (ret) {
1464 /* restore the context */
1465 lyjson_ctx_restore(jsonctx);
1466 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001467 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001468 }
1469 }
1470 return ret;
1471}
1472
1473LY_ERR
1474lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1475{
1476 LY_ERR ret = LY_SUCCESS;
1477 struct lyd_json_ctx *lydctx = NULL;
1478 struct lyd_node *ntf_e = NULL;
1479 struct lyd_node *tree = NULL;
1480 enum LYJSON_PARSER_STATUS status;
1481
1482 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001483 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1484 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1485
Radek Krejci1798aae2020-07-14 13:26:06 +02001486 lydctx->int_opts = LYD_INTOPT_NOTIF;
1487
Radek Krejci1798aae2020-07-14 13:26:06 +02001488 /* parse "notification" and "eventTime", if present */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001489 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001490 if (ret == LY_ENOT) {
1491 ret = LY_SUCCESS;
1492 } else if (ret) {
1493 goto cleanup;
1494 }
1495
Radek Krejcicd5f6222020-11-12 08:54:13 +01001496 assert(status == LYJSON_OBJECT);
1497
Michal Vaskocf770e22020-08-12 13:21:43 +02001498 /* read subtree */
1499 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1500 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001501
1502 /* finish linking metadata */
1503 ret = lydjson_metadata_finish(lydctx, &tree);
1504 LY_CHECK_GOTO(ret, cleanup);
1505
1506 /* make sure we have parsed some notification */
1507 if (!lydctx->op_node) {
1508 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1509 ret = LY_EVALID;
1510 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001511 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001512 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001513 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001514 ret = LY_EVALID;
1515 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001516 }
1517
1518 if (ntf_e) {
1519 /* finish notification envelope */
1520 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1521 LY_CHECK_GOTO(ret, cleanup);
1522 if (status == LYJSON_END) {
1523 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1524 ret = LY_EVALID;
1525 goto cleanup;
1526 } else if (status != LYJSON_OBJECT_CLOSED) {
1527 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
Michal Vasko69730152020-10-09 16:30:07 +02001528 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001529 ret = LY_EVALID;
1530 goto cleanup;
1531 }
1532 }
1533
1534 if (ntf_p) {
1535 *ntf_p = lydctx->op_node;
1536 }
1537 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001538 if (ntf_e) {
1539 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001540 lyd_insert_node(ntf_e, NULL, tree);
1541 tree = ntf_e;
1542 }
1543 if (tree_p) {
1544 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001545 }
1546
1547cleanup:
1548 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001549 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001550
1551 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1552 if (ret) {
1553 lyd_free_all(tree);
1554 lyd_free_tree(ntf_e);
1555 }
1556 return ret;
1557}
1558
1559/**
1560 * @brief Parse optional JSON envelope around the processed content.
1561 *
1562 * @param[in] jsonctx JSON parser context
1563 * @param[in] parent Parent node (some other envelope).
1564 * @param[in] module_key Name of the module where the envelope element is expected.
1565 * @param[in] object_id Name of the envelope object.
1566 * @param[out] envp_p Pointer to the created envelope opaq container.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001567 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001568 * @return LY_SUCCESS if the envelope present and successfully parsed.
1569 * @return LY_ENOT in case there is not the expected envelope.
1570 * @return LY_ERR in case of parsing failure.
1571 */
1572static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001573lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001574 const char *object_id, struct lyd_node **envp_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001575{
1576 LY_ERR ret = LY_ENOT, r;
1577 const char *name, *prefix;
1578 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001579 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001580
1581 *envp_p = NULL;
1582
1583 /* "id" envelope */
1584 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1585 LY_CHECK_GOTO(is_attr, cleanup);
1586 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1587 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1588 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1589
Radek Krejcicd5f6222020-11-12 08:54:13 +01001590 r = lyjson_ctx_next(jsonctx, status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001591 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001592 LY_CHECK_GOTO(*status != LYJSON_OBJECT, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001593
1594 /* create the object envelope */
Michal Vasko501af032020-11-11 20:27:44 +01001595 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), NULL, 0, module_key, ly_strlen(module_key), "", 0,
1596 NULL, LY_PREF_JSON, NULL, LYD_NODEHINT_ENVELOPE, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001597 LY_CHECK_GOTO(ret, cleanup);
1598
1599 if (parent) {
1600 lyd_insert_node(parent, NULL, *envp_p);
1601 }
1602
1603 ret = LY_SUCCESS;
1604cleanup:
1605 return ret;
1606}
1607
1608static LY_ERR
Radek Krejcicd5f6222020-11-12 08:54:13 +01001609lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001610{
Radek Krejcicd5f6222020-11-12 08:54:13 +01001611 LY_CHECK_RET(lyjson_ctx_next(jsonctx, status));
1612 if (*status == LYJSON_END) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001613 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1614 return LY_EVALID;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001615 } else if (*status != LYJSON_OBJECT_CLOSED) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001616 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001617 jsonctx->value_len, jsonctx->value, object_id);
Radek Krejci1798aae2020-07-14 13:26:06 +02001618 return LY_EVALID;
1619 }
1620 return LY_SUCCESS;
1621}
1622
1623LY_ERR
1624lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1625{
1626 LY_ERR ret = LY_SUCCESS;
1627 struct lyd_json_ctx *lydctx = NULL;
1628 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1629 struct lyd_node *tree = NULL;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001630 enum LYJSON_PARSER_STATUS status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001631
1632 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001633 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1634 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1635
Radek Krejci1798aae2020-07-14 13:26:06 +02001636 lydctx->int_opts = LYD_INTOPT_RPC;
1637
1638 /* process envelope(s), if present */
1639
1640 /* process rpc */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001641 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001642 if (ret == LY_ENOT) {
1643 ret = LY_SUCCESS;
1644 goto parse_content;
1645 } else if (ret) {
1646 goto cleanup;
1647 }
1648 /* process action */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001649 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001650 if (ret == LY_ENOT) {
1651 ret = LY_SUCCESS;
1652 goto parse_content;
1653 } else if (ret) {
1654 goto cleanup;
1655 }
1656
1657parse_content:
Radek Krejcicd5f6222020-11-12 08:54:13 +01001658 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001659
1660 /* read subtree(s) */
Michal Vasko22df3f02020-08-24 13:29:22 +02001661 ret = lydjson_subtree_r(lydctx, act_e ? (struct lyd_node_inner *)act_e : (struct lyd_node_inner *)rpc_e, &tree);
Michal Vaskocf770e22020-08-12 13:21:43 +02001662 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001663
1664 /* finish linking metadata */
1665 ret = lydjson_metadata_finish(lydctx, &tree);
1666 LY_CHECK_GOTO(ret, cleanup);
1667
1668 /* make sure we have parsed some operation */
1669 if (!lydctx->op_node) {
1670 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
Michal Vasko69730152020-10-09 16:30:07 +02001671 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
Radek Krejci1798aae2020-07-14 13:26:06 +02001672 ret = LY_EVALID;
1673 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001674 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001675 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001676 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001677 ret = LY_EVALID;
1678 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001679 }
1680
1681 if (act_e) {
1682 /* finish action envelope */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001683 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action", &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001684 LY_CHECK_GOTO(ret, cleanup);
1685 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1686 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001687 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001688 ret = LY_EVALID;
1689 goto cleanup;
1690 }
1691 }
1692 if (rpc_e) {
1693 /* finish rpc envelope */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001694 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc", &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001695 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko69730152020-10-09 16:30:07 +02001696 if (!act_e && (lydctx->op_node->schema->nodetype != LYS_RPC)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001697 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001698 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001699 ret = LY_EVALID;
1700 goto cleanup;
1701 }
1702 }
1703
1704 if (op_p) {
1705 *op_p = lydctx->op_node;
1706 }
1707 assert(tree);
1708 if (tree_p) {
1709 if (rpc_e) {
1710 *tree_p = rpc_e;
1711 } else if (act_e) {
1712 *tree_p = act_e;
1713 } else {
1714 *tree_p = tree;
1715 }
1716 }
1717
1718cleanup:
1719 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001720 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001721
1722 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1723 if (ret) {
1724 lyd_free_all(tree);
1725 lyd_free_tree(act_e);
1726 lyd_free_tree(rpc_e);
1727 }
1728 return ret;
1729}
1730
1731LY_ERR
1732lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1733{
1734 LY_ERR ret = LY_SUCCESS;
1735 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001736 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001737 enum LYJSON_PARSER_STATUS status;
1738
1739 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001740 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1741 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1742
Radek Krejci1798aae2020-07-14 13:26:06 +02001743 lydctx->int_opts = LYD_INTOPT_REPLY;
1744
1745 /* find request OP */
1746 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1747 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1748 break;
1749 }
1750 LYD_TREE_DFS_END(request, req_op);
1751 }
1752 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001753 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001754 ret = LY_EINVAL;
1755 goto cleanup;
1756 }
1757
1758 /* duplicate request OP with parents */
1759 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1760
1761 /* parse "rpc-reply", if any */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001762 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001763 if (ret == LY_ENOT) {
1764 ret = LY_SUCCESS;
1765 } else if (ret) {
1766 goto cleanup;
1767 }
1768
Radek Krejci1798aae2020-07-14 13:26:06 +02001769 assert(status == LYJSON_OBJECT);
1770
1771 /* read subtree(s) */
1772 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001773 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op));
Radek Krejci1798aae2020-07-14 13:26:06 +02001774 LY_CHECK_GOTO(ret, cleanup);
1775
1776 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1777 }
1778
1779 /* finish linking metadata */
1780 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1781 LY_CHECK_GOTO(ret, cleanup);
1782
1783 if (rpcr_e) {
1784 /* finish rpc-reply envelope */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001785 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply", &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001786 LY_CHECK_GOTO(ret, cleanup);
1787 }
1788
1789 if (op_p) {
1790 *op_p = rep_op;
1791 }
Radek Krejcia1c1e542020-09-29 16:06:52 +02001792 for (tree = rep_op; tree->parent; tree = lyd_parent(tree)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001793 if (rpcr_e) {
1794 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001795 lyd_insert_node(rpcr_e, NULL, tree);
1796 tree = rpcr_e;
1797 }
1798 if (tree_p) {
1799 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001800 }
1801
1802cleanup:
1803 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001804 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001805
1806 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1807 if (ret) {
1808 lyd_free_all(rep_op);
1809 lyd_free_tree(rpcr_e);
1810 }
1811 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001812}