blob: 9fa5334b95afae8358c559ea253883141b9f28c0 [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON data parser for libyang
5 *
Radek Krejci1798aae2020-07-14 13:26:06 +02006 * Copyright (c) 2020 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include <assert.h>
Michal Vasko90932a92020-02-12 14:33:03 +010018#include <stdlib.h>
19#include <string.h>
20
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020022#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010023#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020024#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020025#include "json.h"
26#include "parser_internal.h"
27#include "tree_data.h"
28#include "tree_data_internal.h"
29#include "tree_schema.h"
30#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010031
32/**
Radek Krejci1798aae2020-07-14 13:26:06 +020033 * @brief Internal context for JSON YANG data parser.
34 *
35 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
36 */
37struct lyd_json_ctx {
38 uint32_t parse_options; /**< various @ref dataparseroptions. */
39 uint32_t validate_options; /**< various @ref datavalidationoptions. */
40 uint32_t int_opts; /**< internal data parser options */
41 uint32_t path_len; /**< used bytes in the path buffer */
42 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
43 struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
44 struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
45 struct ly_set when_check; /**< set of nodes with "when" conditions */
46 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
47
48 /* callbacks */
49 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020050
51 struct lyjson_ctx *jsonctx; /**< JSON context */
52};
53
54/**
55 * @brief Free the JSON data parser context.
56 *
57 * JSON implementation of lyd_ctx_free_clb().
58 */
59static void
60lyd_json_ctx_free(struct lyd_ctx *lydctx)
61{
62 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
63
64 if (lydctx) {
65 lyd_ctx_free(lydctx);
66 lyjson_ctx_free(ctx->jsonctx);
67 free(ctx);
68 }
69}
70
71/**
Radek Krejci1798aae2020-07-14 13:26:06 +020072 * @brief Parse JSON member-name as [\@][prefix:][name]
73 *
Michal Vaskocabe0702020-08-12 10:14:36 +020074 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020075 * prefix - name of the module of the data node
76 * name - name of the data node
77 *
78 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
79 *
80 * @param[in] value String to parse
81 * @param[in] value_len Length of the @p str.
82 * @param[out] name_p Pointer to the beginning of the parsed name.
83 * @param[out] name_len_p Pointer to the length of the parsed name.
84 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
85 * @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 +020086 * @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 +020087 */
88static void
Michal Vaskocabe0702020-08-12 10:14:36 +020089lydjson_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 +020090 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020091{
92 const char *name, *prefix = NULL;
93 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +020094 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020095
96 name = memchr(value, ':', value_len);
97 if (name != NULL) {
98 prefix = value;
99 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200100 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200101 prefix++;
102 }
103 prefix_len = name - prefix;
104 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200105 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200106 } else {
107 name = value;
108 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200109 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200110 name++;
111 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200112 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100113 }
114
Radek Krejci1798aae2020-07-14 13:26:06 +0200115 *name_p = name;
116 *name_len_p = name_len;
117 *prefix_p = prefix;
118 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200119 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200120}
121
122/**
123 * @brief Get correct prefix (module_name) inside the @p node.
124 *
125 * @param[in] node Data node to get inherited prefix.
126 * @param[in] local_prefix Local prefix to replace the inherited prefix.
127 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
128 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
129 * and no context @p node to get inherited prefix.
130 * @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
131 * of no local prefix and no context @p node to get inherited prefix.
132 * @return LY_ERR value.
133 */
134static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200135lydjson_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 +0200136 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200137{
138 struct lyd_node_opaq *onode;
139 const char *module_name = NULL;
140
141 assert(prefix_p && prefix_len_p);
142
143 if (local_prefix_len) {
144 *prefix_p = local_prefix;
145 *prefix_len_p = local_prefix_len;
146 return LY_SUCCESS;
147 }
148
149 *prefix_p = NULL;
150 while (node) {
151 if (node->schema) {
152 *prefix_p = node->schema->module->name;
153 break;
154 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200155 onode = (struct lyd_node_opaq *)node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200156 if (onode->prefix.module_name) {
157 *prefix_p = onode->prefix.module_name;
158 break;
159 } else if (onode->prefix.id) {
160 *prefix_p = onode->prefix.id;
161 break;
162 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200163 node = (struct lyd_node *)node->parent;
Radek Krejci1798aae2020-07-14 13:26:06 +0200164 }
165 *prefix_len_p = ly_strlen(module_name);
166
167 return LY_SUCCESS;
168}
169
170/**
171 * @brief Get schema node corresponding to the input parameters.
172 *
173 * @param[in] lydctx JSON data parser context.
174 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
175 * @param[in] prefix Requested node's prefix (module name).
176 * @param[in] prefix_len Length of the @p prefix.
177 * @param[in] name Requested node's name.
178 * @param[in] name_len Length of the @p name.
179 * @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
180 * is accepted for searching top-level nodes.
181 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
182 * @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.
183 * @return LY_EVALID on failure, error message is logged
184 * @return LY_ENOT in case the input data are expected to be skipped
185 */
186static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200187lydjson_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 +0200188 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200189{
190 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100191 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200192
193 /* init return value */
194 *snode_p = NULL;
195
196 /* get the element module */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200197 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200198 if (!prefix_len) {
199 /* opaq parent (envelope) - the second part of the condition */
Michal Vasko22df3f02020-08-24 13:29:22 +0200200 lydjson_get_node_prefix((struct lyd_node *)parent, NULL, 0, &prefix, &prefix_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200201 }
202 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
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200222 if (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200223 /* ignore the envelope parent when searchinf for the schema node */
224 parent = NULL;
225 }
226
227 /* get the schema node */
228 if (mod && (!parent || parent->schema)) {
229 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
230 if (!*snode_p) {
231 if (lydctx->parse_options & LYD_PARSE_STRICT) {
232 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 +0200233 name_len, name, mod->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200234 return LY_EVALID;
235 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
236 /* skip element with children */
237 return LY_ENOT;
238 }
239 } else {
240 /* check that schema node is valid and can be used */
241 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
242 }
243 }
244
245 return LY_SUCCESS;
246}
247
248/**
249 * @brief Skip the currently open JSON object/array
250 * @param[in] jsonctx JSON context with the input data to skip.
251 * @return LY_ERR value.
252 */
253static LY_ERR
254lydjson_data_skip(struct lyjson_ctx *jsonctx)
255{
256 enum LYJSON_PARSER_STATUS status, current;
257 size_t sublevels = 1;
258
259 status = lyjson_ctx_status(jsonctx, 0);
260
261 /* skip after the content */
262 do {
263 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
264 if (current == status) {
265 sublevels++;
266 } else if (current == status + 1) {
267 sublevels--;
268 }
269 } while (current != status + 1 && sublevels);
270 /* open the next sibling */
271 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
272
273 return LY_SUCCESS;
274}
275
276/**
277 * @brief Go through the @p value and find all possible prefixes and store them in @p val_prefs_p [sized array](@ref sizedarrays).
278 *
279 * @param[in] ctx libyang context
280 * @param[in] value Pointer to the beginning of the value to check.
281 * @param[in] value_len Length of the string to examine in @p value.
282 * @param[out] val_prefs_p Pointer to the resulting [sized array](@ref sizedarrays) of found prefixes. NULL in case there are no prefixes.
283 * @return LY_EMEM on memory allocation failure.
284 * @return LY_SUCCESS on success, empty @p val_prefs_p (NULL) is valid result if there are no possible prefixes
285 */
286static LY_ERR
287lydjson_get_value_prefixes(const struct ly_ctx *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs_p)
288{
289 LY_ERR ret;
290 LY_ARRAY_COUNT_TYPE u;
291 uint32_t c;
292 const char *start, *stop;
293 struct ly_prefix *prefixes = NULL;
294 size_t len;
295
296 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
297 size_t bytes;
298 ly_getutf8(&stop, &c, &bytes);
299 if (is_yangidentstartchar(c)) {
300 for (ly_getutf8(&stop, &c, &bytes);
301 is_yangidentchar(c) && (size_t)(stop - value) < value_len;
Radek Krejci1e008d22020-08-17 11:37:37 +0200302 ly_getutf8(&stop, &c, &bytes)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200303 stop = stop - bytes;
304 if (*stop == ':') {
305 /* we have a possible prefix */
306 struct ly_prefix *p = NULL;
307
308 len = stop - start;
309
310 /* check whether we do not already have this prefix stored */
311 LY_ARRAY_FOR(prefixes, u) {
312 if (!ly_strncmp(prefixes[u].id, start, len)) {
313 p = &prefixes[u];
314 break;
315 }
316 }
317 if (!p) {
318 LY_ARRAY_NEW_GOTO(ctx, prefixes, p, ret, error);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200319 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->id), error);
320 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->module_name), error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200321 } /* else the prefix already present */
322 }
323 stop = stop + bytes;
324 }
325 }
326
327 *val_prefs_p = prefixes;
328 return LY_SUCCESS;
329
330error:
331 LY_ARRAY_FOR(prefixes, u) {
332 lydict_remove(ctx, prefixes[u].id);
333 lydict_remove(ctx, prefixes[u].module_name);
334 }
335 LY_ARRAY_FREE(prefixes);
336 return ret;
337}
338
339/**
340 * @brief Check that the input data are parseable as the @p list.
341 *
342 * Checks for all the list's keys. Function does not revert the context state.
343 *
344 * @param[in] jsonctx JSON parser context.
345 * @param[in] list List schema node corresponding to the input data object.
346 * @return LY_SUCCESS in case the data are ok for the @p list
347 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
348 */
349static LY_ERR
350lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
351{
352 LY_ERR ret = LY_SUCCESS;
353 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
354 struct ly_set key_set = {0};
355 const struct lysc_node *snode;
356 uint32_t i, status_count;
357
358 assert(list && (list->nodetype == LYS_LIST));
359 assert(status == LYJSON_OBJECT);
360
361 /* get all keys into a set (keys do not have if-features or anything) */
362 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100363 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200364 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200365 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200366 }
367
368 if (status != LYJSON_OBJECT_EMPTY) {
369 status_count = jsonctx->status.count;
370
371 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
372 const char *name, *prefix;
373 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200374 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200375
376 /* match the key */
377 snode = NULL;
378 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
379
380 if (!is_attr && !prefix) {
381 for (i = 0; i < key_set.count; ++i) {
382 snode = (const struct lysc_node *)key_set.objs[i];
383 if (!ly_strncmp(snode->name, name, name_len)) {
384 break;
385 }
386 }
387 /* go into the item to a) process it as a key or b) start skipping it as another list child */
388 ret = lyjson_ctx_next(jsonctx, &status);
389 LY_CHECK_GOTO(ret, cleanup);
390
391 if (snode) {
392 /* we have the key, validate the value */
393 if (status < LYJSON_NUMBER) {
394 /* not a terminal */
395 ret = LY_ENOT;
396 goto cleanup;
397 }
398
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200399 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 LY_CHECK_GOTO(ret, cleanup);
401
402 /* key with a valid value, remove from the set */
403 ly_set_rm_index(&key_set, i, NULL);
404 }
405 } else {
406 /* start skipping the member we are not interested in */
407 ret = lyjson_ctx_next(jsonctx, &status);
408 LY_CHECK_GOTO(ret, cleanup);
409 }
410 /* move to the next child */
411 while (status_count < jsonctx->status.count) {
412 ret = lyjson_ctx_next(jsonctx, &status);
413 LY_CHECK_GOTO(ret, cleanup);
414 }
415 }
416 }
417
418 if (key_set.count) {
419 /* some keys are missing/did not validate */
420 ret = LY_ENOT;
421 }
422
423cleanup:
424 ly_set_erase(&key_set, NULL);
425 return ret;
426}
427
428/**
429 * @brief Get the hint for the data type parsers according to the current JSON parser context.
430 *
431 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
432 * @param[in,out] status Pointer to the current context status,
433 * in some circumstances the function manipulates with the context so the status is updated.
434 * @param[out] type_hint_p Pointer to the variable to store the result.
435 * @return LY_SUCCESS in case of success.
436 * @return LY_EINVAL in case of invalid context status not referring to a value.
437 */
438static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200439lydjson_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 +0200440{
441 *type_hint_p = 0;
442
443 if (*status_p == LYJSON_ARRAY) {
444 /* only [null] */
445 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
446 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
447
448 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
449 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
450
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200451 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200452 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200453 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200454 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200455 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200456 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200457 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200458 } else if (*status_p == LYJSON_NULL) {
459 *type_hint_p = 0;
460 } else {
461 return LY_EINVAL;
462 }
463
464 return LY_SUCCESS;
465}
466
467/**
468 * @brief Check in advance if the input data are parsable according to the provided @p snode.
469 *
470 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
471 * is naturally done when the data are really parsed.
472 *
473 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
474 * as before calling, despite it is necessary to process input data for checking.
475 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
476 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
477 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
478 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
479 * @return LY_EINVAL in case of invalid leaf JSON encoding
480 * and they are expected to be parsed as opaq nodes.
481 */
482static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200483lydjson_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 +0200484{
485 LY_ERR ret = LY_SUCCESS;
486 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
487 enum LYJSON_PARSER_STATUS status;
488
489 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200490
Michal Vasko32ac9942020-08-12 14:35:12 +0200491 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
492 /* can always be parsed as a data node if we have the schema node */
493 return LY_SUCCESS;
494 }
495
496 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200497 /* backup parser */
498 lyjson_ctx_backup(jsonctx);
499 status = lyjson_ctx_status(jsonctx, 0);
500
Michal Vasko32ac9942020-08-12 14:35:12 +0200501 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
502 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200503 switch (snode->nodetype) {
504 case LYS_LEAFLIST:
505 case LYS_LEAF:
506 /* value may not be valid in which case we parse it as an opaque node */
507 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
508 if (ret) {
509 break;
510 }
511
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200512 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200513 ret = LY_ENOT;
514 }
515 break;
516 case LYS_LIST:
517 /* lists may not have all its keys */
518 if (lydjson_check_list(jsonctx, snode)) {
519 /* invalid list, parse as opaque if it missing/has invalid some keys */
520 ret = LY_ENOT;
521 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200522 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200523 }
524
525 /* restore parser */
526 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200527 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200528 status = lyjson_ctx_status(jsonctx, 0);
529 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
530 }
531
532 return ret;
533}
534
535/**
536 * @brief Join the forward-referencing metadata with their target data nodes.
537 *
538 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
539 *
540 * @param[in] lydctx JSON data parser context.
541 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
542 * as a starting point to search for the metadata's target data node
543 * @return LY_SUCCESS on success
544 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
545 */
546static LY_ERR
547lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
548{
549 LY_ERR ret = LY_SUCCESS;
550 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200551 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200552 const char *prev = NULL;
553
554 /* finish linking metadata */
555 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200556 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200557 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200558 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200559 const char *name, *prefix;
560 size_t name_len, prefix_len;
561 const struct lysc_node *snode;
562
Michal Vasko69730152020-10-09 16:30:07 +0200563 if (attr->schema || (meta_container->name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200564 /* not an opaq metadata node */
565 continue;
566 }
567
568 if (prev != meta_container->name) {
569 /* metas' names are stored in dictionary, so checking pointers must works */
570 lydict_remove(lydctx->jsonctx->ctx, prev);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200571 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200572 instance = 1;
573 } else {
574 instance++;
575 }
576
577 /* find the correspnding data node */
578 LY_LIST_FOR(start, node) {
579 if (!node->schema) {
580 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vasko22df3f02020-08-24 13:29:22 +0200581 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq *)node)->name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200582 continue;
583 }
584
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200585 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200586 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vasko69730152020-10-09 16:30:07 +0200587 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200588 ret = LY_EVALID;
589 goto cleanup;
590 }
591
592 /* match */
593 match++;
594 if (match != instance) {
595 continue;
596 }
597
598 LY_LIST_FOR(meta_container->child, meta_iter) {
599 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200600 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200601 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200602 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200603
604 /* get value prefixes */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200605 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value,
606 lydctx->jsonctx->value_len, &val_prefs), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200607
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200608 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value,
609 ly_strlen(meta->value), &dynamic, LYD_JSON, meta->hints, val_prefs, meta->prefix.id,
610 ly_strlen(meta->prefix.id), meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
Radek Krejci1798aae2020-07-14 13:26:06 +0200611 LY_CHECK_GOTO(ret, cleanup);
612 }
613
614 /* done */
615 break;
616 } else {
617 /* this is the second time we are resolving the schema node, so it must succeed,
618 * but remember that snode can be still NULL */
619 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
620 assert(is_attr);
621 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
622 assert(ret == LY_SUCCESS);
623
624 if (snode != node->schema) {
625 continue;
626 }
627
628 /* match */
629 match++;
630 if (match != instance) {
631 continue;
632 }
633
634 LY_LIST_FOR(meta_container->child, meta_iter) {
635 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200636 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200637 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200638 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200639
640 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
641 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200642 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vasko69730152020-10-09 16:30:07 +0200643 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
644 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200645 LY_CHECK_GOTO(ret, cleanup);
646 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
647 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200648 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
649 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200650 ret = LY_EVALID;
651 goto cleanup;
652 }
653 }
654 /* add/correct flags */
655 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
656
657 /* done */
658 break;
659 }
660 }
661
662 if (match != instance) {
663 /* there is no corresponding data node for the metadata */
664 if (instance > 1) {
665 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200666 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
667 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200668 } else {
669 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200670 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200671 }
672 ret = LY_EVALID;
673 } else {
674 /* remove the opaq attr */
675 if (attr == (*first_p)) {
676 *first_p = attr->next;
677 }
678 lyd_free_tree(attr);
679 }
680 }
681
682cleanup:
683 lydict_remove(lydctx->jsonctx->ctx, prev);
684
685 return ret;
686}
687
688/**
689 * @brief Parse a metadata member.
690 *
691 * @param[in] lydctx JSON data parser context.
692 * @param[in] snode Schema node of the metadata parent.
693 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
694 * so the data node does not exists. In such a case the metadata is stored in the context for the later
695 * processing by lydjson_metadata_finish().
696 * @return LY_SUCCESS on success
697 * @return Various LY_ERR values in case of failure.
698 */
699static LY_ERR
700lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
701{
702 LY_ERR ret = LY_SUCCESS;
703 enum LYJSON_PARSER_STATUS status;
704 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200705 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200706 const char *name, *prefix = NULL;
707 size_t name_len, prefix_len = 0;
708 struct lys_module *mod;
709 struct lyd_meta *meta = NULL;
710 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200711 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200712 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200713 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 uint16_t nodetype;
715
716 assert(snode || node);
717
718 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
719
720 /* move to the second item in the name/X pair */
721 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
722 LY_CHECK_GOTO(ret, cleanup);
723
724 /* check attribute encoding */
725 switch (nodetype) {
726 case LYS_LEAFLIST:
727 expected = "@name/array of objects/nulls";
728
729 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
730
731next_entry:
732 instance++;
733
734 /* move into array / next entry */
735 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
736 LY_CHECK_GOTO(ret, cleanup);
737
738 if (status == LYJSON_ARRAY_CLOSED) {
739 /* we are done, move after the array */
740 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
741 goto cleanup;
742 }
743 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
744
Michal Vasko69730152020-10-09 16:30:07 +0200745 if (!node || (node->schema != prev->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200746 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200747 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
748 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200749 ret = LY_EVALID;
750 goto cleanup;
751 }
752
753 if (status == LYJSON_NULL) {
754 /* continue with the next entry in the leaf-list array */
755 prev = node;
756 node = node->next;
757 goto next_entry;
758 }
759 break;
760 case LYS_LEAF:
761 case LYS_ANYXML:
762 expected = "@name/object";
763
764 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
765 break;
766 case LYS_CONTAINER:
767 case LYS_LIST:
768 case LYS_ANYDATA:
769 case LYS_NOTIF:
770 case LYS_ACTION:
771 case LYS_RPC:
772 in_parent = 1;
773 expected = "@/object";
774 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
775 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200776 default:
777 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 }
779
780 /* process all the members inside a single metadata object */
781 assert(status == LYJSON_OBJECT);
782
783 while (status != LYJSON_OBJECT_CLOSED) {
784 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
785 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200786 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200787 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
788 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200789 ret = LY_EVALID;
790 goto cleanup;
791 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200792 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200793 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
794 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200795 ret = LY_EVALID;
796 goto cleanup;
797 }
798
799 /* get the element module */
800 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
801 if (!mod) {
802 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200803 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200804 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
805 prefix_len, prefix, name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200806 ret = LY_EVALID;
807 goto cleanup;
808 }
809 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
810 /* skip element with children */
811 ret = lydjson_data_skip(lydctx->jsonctx);
812 LY_CHECK_GOTO(ret, cleanup);
813 status = lyjson_ctx_status(lydctx->jsonctx, 0);
814 /* end of the item */
815 continue;
816 }
817 }
818
819 /* get the value */
820 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
821 LY_CHECK_GOTO(ret, cleanup);
822
823 if (node->schema) {
824 /* create metadata */
825 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200826 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 +0200827 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
828 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200829 LY_CHECK_GOTO(ret, cleanup);
830
831 /* add/correct flags */
832 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
833 } else {
834 /* create attribute */
835 struct ly_prefix *val_prefs = NULL;
836 const char *module_name;
837 size_t module_name_len;
838
839 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
840
841 /* get value prefixes */
842 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
843
844 /* attr2 is always changed to the created attribute */
845 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
Michal Vasko69730152020-10-09 16:30:07 +0200846 &lydctx->jsonctx->dynamic, LYD_JSON, 0, val_prefs, prefix, prefix_len, module_name, module_name_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200847 LY_CHECK_GOTO(ret, cleanup);
848 }
849 /* next member */
850 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
851 LY_CHECK_GOTO(ret, cleanup);
852 }
853
854 if (nodetype == LYS_LEAFLIST) {
855 /* continue by processing another metadata object for the following
856 * leaf-list instance since they are allways instantiated in JSON array */
857 prev = node;
858 node = node->next;
859 goto next_entry;
860 }
861
862 /* move after the metadata */
863 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
864 LY_CHECK_GOTO(ret, cleanup);
865
866cleanup:
867 return ret;
868
869representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200870 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200871 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
872 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name,
873 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200874
875 return LY_EVALID;
876}
877
878/**
879 * @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.
880 *
881 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
882 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
883 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
884 */
885static void
886lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
887{
888 if (*node_p) {
889 /* insert, keep first pointer correct */
890 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
891 if (first_p) {
892 if (parent) {
893 *first_p = parent->child;
894 } else {
895 while ((*first_p)->prev->next) {
896 *first_p = (*first_p)->prev;
897 }
898 }
899 }
900 *node_p = NULL;
901 }
902}
903
904static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
905
906/**
907 * @brief Parse opaq node from the input.
908 *
909 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
910 *
911 * @param[in] lydctx JSON data parser context.
912 * @param[in] name Name of the opaq node to create.
913 * @param[in] name_len Length of the @p name string.
914 * @param[in] prefix Prefix of the opaq node to create.
915 * @param[in] prefix_len Length of the @p prefx string.
916 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
917 * but must be set if @p first is not.
918 * @param[in,out] status_p Pointer to the current status of the parser context,
919 * since the function manipulates with the context and process the input, the status can be updated.
920 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
921 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
922 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
923 * @param[out] node_p Pointer to the created opaq node.
924 * @return LY_ERR value.
925 */
926static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200927lydjson_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 +0200928 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
929 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200930{
931 LY_ERR ret = LY_SUCCESS;
932 const char *value = NULL, *module_name;
933 size_t value_len = 0, module_name_len = 0;
934 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200935 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200936 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200937
Michal Vasko69730152020-10-09 16:30:07 +0200938 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200939 /* prepare for creating opaq node with a value */
940 value = lydctx->jsonctx->value;
941 value_len = lydctx->jsonctx->value_len;
942 dynamic = lydctx->jsonctx->dynamic;
943 lydctx->jsonctx->dynamic = 0;
944
945 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
946
947 if (value) {
948 /* get value prefixes */
949 LY_CHECK_RET(lydjson_get_value_prefixes(lydctx->jsonctx->ctx, value, value_len, &val_prefs));
950 }
951 }
952
953 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200954 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200955 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, LYD_JSON, type_hint,
956 val_prefs, prefix, prefix_len, module_name, module_name_len, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200957 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200958 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200959 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200960 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200961
Michal Vasko69730152020-10-09 16:30:07 +0200962 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200963 /* process children */
964 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
965 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
966 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
967 }
Michal Vasko69730152020-10-09 16:30:07 +0200968 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200969 /* process another instance of the same node */
970 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200971 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200972
Michal Vasko69730152020-10-09 16:30:07 +0200973 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200974 /* but first process children of the object in the array */
975 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
976 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
977 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
978 }
979 }
980
981 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
982
983 /* continue with the next instance */
984 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
985 assert(node_p);
986 lydjson_maintain_children(parent, first_p, node_p);
987 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
988 }
989 }
990
991 /* finish linking metadata */
992 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
993
994 /* move after the item */
995 return lyjson_ctx_next(lydctx->jsonctx, status_p);
996}
997
998/**
999 * @brief Process the attribute container (starting by @)
1000 *
1001 * @param[in] lydctx JSON data parser context.
1002 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1003 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1004 * @param[in] name Name of the opaq node to create.
1005 * @param[in] name_len Length of the @p name string.
1006 * @param[in] prefix Prefix of the opaq node to create.
1007 * @param[in] prefix_len Length of the @p prefx string.
1008 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1009 * but must be set if @p first is not.
1010 * @param[in,out] status_p Pointer to the current status of the parser context,
1011 * since the function manipulates with the context and process the input, the status can be updated.
1012 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1013 * @param[out] node_p Pointer to the created opaq node.
1014 * @return LY_ERR value.
1015 */
1016static LY_ERR
1017lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +02001018 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1019 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1020 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001021{
1022 LY_ERR ret = LY_SUCCESS;
1023 enum LYJSON_PARSER_STATUS status_inner;
1024
1025 /* parse as an attribute to a node */
1026 if (!attr_node && snode) {
1027 /* try to find the instance */
1028 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1029 if (iter->schema == snode) {
1030 attr_node = iter;
1031 break;
1032 }
1033 }
1034 }
1035 if (!attr_node) {
1036 /* parse just as an opaq node with the name beginning with @,
1037 * later we have to check that it belongs to a standard node
1038 * and it is supposed to be converted to a metadata */
1039 uint32_t prev_opts;
1040
1041 /* move into metadata */
1042 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1043
1044 if (*status_p == LYJSON_ARRAY) {
1045 /* move into the array */
1046 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1047 } else {
1048 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1049 status_inner = LYJSON_ERROR;
1050 }
1051
1052 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
1053 prev_opts = lydctx->parse_options;
1054 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1055 lydctx->parse_options |= LYD_PARSE_OPAQ;
1056
1057 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 +02001058 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001059
1060 /* restore the parser options */
1061 lydctx->parse_options = prev_opts;
1062 } else {
1063 ret = lydjson_metadata(lydctx, snode, attr_node);
1064 }
1065
1066 return ret;
1067}
1068
1069/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001070 * @brief Parse a single instance of a node.
1071 *
1072 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1073 * as before calling, despite it is necessary to process input data for checking.
1074 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1075 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1076 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1077 * @param[in] name Parsed JSON node name.
1078 * @param[in] name_len Lenght of @p name.
1079 * @param[in] prefix Parsed JSON node prefix.
1080 * @param[in] prefix_len Length of @p prefix.
1081 * @param[in,out] status JSON parser status, is updated.
1082 * @param[out] node Parsed data (or opaque) node.
1083 * @return LY_SUCCESS if a node was successfully parsed,
1084 * @return LY_EINVAL in case of invalid JSON encoding,
1085 * @return LY_ERR on other errors.
1086 */
1087static LY_ERR
1088lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001089 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1090 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001091{
1092 LY_ERR ret;
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001093 uint32_t type_hints;
Michal Vasko32ac9942020-08-12 14:35:12 +02001094 uint32_t prev_opts;
1095 struct lyd_node *tree = NULL;
1096
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001097 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001098 if (ret == LY_SUCCESS) {
1099 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1100 if (snode->nodetype & LYD_NODE_TERM) {
1101 /* create terminal node */
1102 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001103 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1104 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001105 LY_CHECK_RET(ret);
1106
1107 /* move JSON parser */
1108 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1109 LY_CHECK_RET(ret);
1110 } else if (snode->nodetype & LYD_NODE_INNER) {
1111 /* create inner node */
1112 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1113
1114 ret = lyd_create_inner(snode, node);
1115 LY_CHECK_RET(ret);
1116
1117 /* process children */
1118 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1119 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1120 LY_CHECK_RET(ret);
1121 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1122 }
1123
1124 /* finish linking metadata */
1125 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1126 LY_CHECK_RET(ret);
1127
1128 if (snode->nodetype == LYS_LIST) {
1129 /* check all keys exist */
1130 ret = lyd_parse_check_keys(*node);
1131 LY_CHECK_RET(ret);
1132 }
1133
1134 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1135 /* new node validation, autodelete CANNOT occur, all nodes are new */
1136 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1137 LY_CHECK_RET(ret);
1138
1139 /* add any missing default children */
1140 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->unres_node_type,
Michal Vasko69730152020-10-09 16:30:07 +02001141 &lydctx->when_check, (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ?
1142 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001143 LY_CHECK_RET(ret);
1144 }
1145
1146 /* move JSON parser */
1147 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1148 LY_CHECK_RET(ret);
1149 } else if (snode->nodetype & LYD_NODE_ANY) {
1150 /* create any node */
1151 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1152
1153 /* parse any data tree with correct options */
1154 /* first backup the current options and then make the parser to process data as opaq nodes */
1155 prev_opts = lydctx->parse_options;
1156 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1157 lydctx->parse_options |= LYD_PARSE_OPAQ;
1158
1159 /* process the anydata content */
1160 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1161 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1162 LY_CHECK_RET(ret);
1163 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1164 }
1165
1166 /* restore parser options */
1167 lydctx->parse_options = prev_opts;
1168
1169 /* finish linking metadata */
1170 ret = lydjson_metadata_finish(lydctx, &tree);
1171 LY_CHECK_RET(ret);
1172
1173 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1174 LY_CHECK_RET(ret);
1175 }
1176 } else if (ret == LY_ENOT) {
1177 /* parse it again as an opaq node */
1178 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001179 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001180 LY_CHECK_RET(ret);
1181
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001182 if (snode->nodetype == LYS_LIST) {
1183 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1184 } else if (snode->nodetype == LYS_LEAFLIST) {
1185 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001186 }
1187 }
1188
1189 return ret;
1190}
1191
1192/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001193 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001194 *
1195 * @param[in] lydctx JSON data parser context.
1196 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1197 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1198 * @return LY_ERR value.
1199 */
1200static LY_ERR
1201lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1202{
1203 LY_ERR ret = LY_SUCCESS;
1204 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1205 enum LYJSON_PARSER_STATUS status_inner = 0;
1206 const char *name, *prefix = NULL;
1207 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001208 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001209 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001210 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001211 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1212 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001213
1214 assert(parent || first_p);
1215 assert(status == LYJSON_OBJECT);
1216
1217 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001218 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 +02001219
Michal Vaskocabe0702020-08-12 10:14:36 +02001220 if (!is_meta || name_len || prefix_len) {
1221 /* get the schema node */
1222 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1223 if (ret == LY_ENOT) {
1224 /* skip element with children */
1225 ret = lydjson_data_skip(lydctx->jsonctx);
1226 LY_CHECK_GOTO(ret, cleanup);
1227 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1228 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001229 goto cleanup;
1230 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001231 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001232
Michal Vaskocabe0702020-08-12 10:14:36 +02001233 if (!snode) {
1234 /* we will not be parsing it as metadata */
1235 is_meta = 0;
1236 }
1237 }
1238
1239 if (is_meta) {
1240 /* parse as metadata */
1241 if (!name_len && !prefix_len) {
1242 /* parent's metadata without a name - use the schema from the parent */
1243 if (!parent) {
1244 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001245 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001246 ret = LY_EVALID;
1247 goto cleanup;
1248 }
1249 attr_node = (struct lyd_node *)parent;
1250 snode = attr_node->schema;
1251 }
1252 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001253 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001254 LY_CHECK_GOTO(ret, cleanup);
1255 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001256 /* parse as an opaq node */
1257 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1258
1259 /* move to the second item in the name/X pair */
1260 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1261 LY_CHECK_GOTO(ret, cleanup);
1262
1263 if (status == LYJSON_ARRAY) {
1264 /* move into the array */
1265 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1266 LY_CHECK_GOTO(ret, cleanup);
1267 } else {
1268 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1269 status_inner = LYJSON_ERROR;
1270 }
1271
1272 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001273 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001274 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001275 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001276 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001277
1278 /* move to the second item in the name/X pair */
1279 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1280 LY_CHECK_GOTO(ret, cleanup);
1281
1282 /* first check the expected representation according to the nodetype and then continue with the content */
1283 switch (snode->nodetype) {
1284 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001285 case LYS_LIST:
1286 if (snode->nodetype == LYS_LEAFLIST) {
1287 expected = "name/array of values";
1288 } else {
1289 expected = "name/array of objects";
1290 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001291
1292 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1293
1294 /* move into array */
1295 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1296 LY_CHECK_GOTO(ret, cleanup);
1297
Michal Vasko32ac9942020-08-12 14:35:12 +02001298 /* process all the values/objects */
1299 do {
1300 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001301
Michal Vasko32ac9942020-08-12 14:35:12 +02001302 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001303 &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001304 if (ret == LY_EINVAL) {
1305 goto representation_error;
1306 } else if (ret) {
1307 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001308 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001309 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001310
Michal Vasko32ac9942020-08-12 14:35:12 +02001311 /* move after the array */
1312 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1313 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001314
1315 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001316 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001317 case LYS_CONTAINER:
1318 case LYS_NOTIF:
1319 case LYS_ACTION:
1320 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001321 case LYS_ANYDATA:
1322 case LYS_ANYXML:
1323 if (snode->nodetype == LYS_LEAF) {
1324 if (status == LYJSON_ARRAY) {
1325 expected = "name/[null]";
1326 } else {
1327 expected = "name/value";
1328 }
1329 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001330 expected = "name/object";
1331 }
1332
Michal Vasko32ac9942020-08-12 14:35:12 +02001333 /* process the value/object */
1334 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1335 if (ret == LY_EINVAL) {
1336 goto representation_error;
1337 } else if (ret) {
1338 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001339 }
1340
Michal Vasko32ac9942020-08-12 14:35:12 +02001341 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001342 /* rememeber the RPC/action/notification */
1343 lydctx->op_node = node;
1344 }
1345
1346 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001347 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001348 }
1349
Michal Vasko32ac9942020-08-12 14:35:12 +02001350 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001351 lydjson_maintain_children(parent, first_p, &node);
1352
1353cleanup:
1354 lyd_free_tree(node);
1355 return ret;
1356
1357representation_error:
1358 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001359 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1360 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejci1798aae2020-07-14 13:26:06 +02001361
1362 ret = LY_EVALID;
1363 goto cleanup;
1364}
1365
1366/**
1367 * @brief Common start of JSON parser processing different types of the input data.
1368 *
1369 * @param[in] ctx libyang context
1370 * @param[in] in Input structure.
1371 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1372 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1373 * @param[out] lydctx_p Data parser context to finish validation.
1374 * @return LY_ERR value.
1375 */
1376static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001377lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001378 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001379{
1380 LY_ERR ret = LY_SUCCESS;
1381 struct lyd_json_ctx *lydctx;
1382 size_t i, line = 1;
1383
Radek Krejci1798aae2020-07-14 13:26:06 +02001384 /* init context */
1385 lydctx = calloc(1, sizeof *lydctx);
1386 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1387 lydctx->parse_options = parse_options;
1388 lydctx->validate_options = validate_options;
1389 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001390
Radek Krejci284f31f2020-09-18 15:40:29 +02001391 /* starting top-level */
1392 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1393 if (in->current[i] == 0x0a) { /* new line */
1394 line++;
Michal Vasko61d76362020-10-07 10:47:30 +02001395 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001396 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001397
Radek Krejci284f31f2020-09-18 15:40:29 +02001398 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1399 if (lyjson_ctx_status(lydctx->jsonctx, 0) == LYJSON_END) {
1400 /* empty data input */
1401 *lydctx_p = lydctx;
1402 return LY_SUCCESS;
1403 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001404 *lydctx_p = lydctx;
1405 return LY_SUCCESS;
1406}
1407
1408LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001409lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001410 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001411{
1412 LY_ERR ret = LY_SUCCESS;
1413 struct lyd_json_ctx *lydctx = NULL;
1414 enum LYJSON_PARSER_STATUS status;
1415
1416 assert(tree_p);
1417 *tree_p = NULL;
1418
1419 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx);
Radek Krejci284f31f2020-09-18 15:40:29 +02001420 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001421
1422 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vasko48cec8c2020-10-20 16:38:07 +02001423 LY_CHECK_GOTO(status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1424 assert((status == LYJSON_OBJECT));
Radek Krejci1798aae2020-07-14 13:26:06 +02001425
1426 /* read subtree(s) */
1427 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1428 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1429 LY_CHECK_GOTO(ret, cleanup);
1430
1431 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1432 }
1433
1434 /* finish linking metadata */
1435 ret = lydjson_metadata_finish(lydctx, tree_p);
1436 LY_CHECK_GOTO(ret, cleanup);
1437
1438cleanup:
1439 /* there should be no unresolved types stored */
Michal Vasko69730152020-10-09 16:30:07 +02001440 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count &&
1441 !lydctx->when_check.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001442
1443 if (ret) {
1444 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1445 lyd_free_all(*tree_p);
1446 *tree_p = NULL;
1447 } else {
1448 *lydctx_p = (struct lyd_ctx *)lydctx;
1449 }
1450
1451 return ret;
1452}
1453
1454/**
1455 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1456 *
1457 * @param[in] jsonctx JSON parser context
1458 * @param[out] envp_p Pointer to the created envelope opaq container.
1459 * @return LY_SUCCESS if the envelope present and successfully parsed.
1460 * @return LY_ENOT in case there is not the expected envelope.
1461 * @return LY_ERR in case of parsing failure.
1462 */
1463static LY_ERR
1464lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p)
1465{
1466 LY_ERR ret = LY_ENOT, r;
1467 const char *name, *prefix, *value = NULL;
1468 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001469 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001470 enum LYJSON_PARSER_STATUS status;
1471 struct lyd_node *et;
1472
1473 *envp_p = NULL;
1474
1475 /* backup the context */
1476 lyjson_ctx_backup(jsonctx);
1477
1478 /* "notification" envelope */
1479 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1480 LY_CHECK_GOTO(is_attr, cleanup);
1481 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1482 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1483
1484 r = lyjson_ctx_next(jsonctx, &status);
1485 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1486 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1487
1488 /* "eventTime" child */
1489 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1490 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1491 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1492
1493 /* go for the data */
1494 r = lyjson_ctx_next(jsonctx, &status);
1495 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1496 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1497
1498 value = jsonctx->value;
1499 value_len = jsonctx->value_len;
1500 dynamic = jsonctx->dynamic;
1501 jsonctx->dynamic = 0;
1502
1503 r = lyjson_ctx_next(jsonctx, &status);
1504 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1505 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1506 /* now the notificationContent is expected, which will be parsed by the caller */
1507
1508 /* create notification envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001509 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LYD_JSON, LYD_NODEHINT_ENVELOPE, NULL, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001510 0, "ietf-restconf", 13, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001511 LY_CHECK_GOTO(ret, cleanup);
1512 /* create notification envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001513 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", 9, value, value_len, &dynamic, LYD_JSON, LYD_VALHINT_STRING, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001514 NULL, 0, "ietf-restconf", 13, &et);
Radek Krejci1798aae2020-07-14 13:26:06 +02001515 LY_CHECK_GOTO(ret, cleanup);
1516 /* insert eventTime into notification */
1517 lyd_insert_node(*envp_p, NULL, et);
1518
1519 ret = LY_SUCCESS;
1520cleanup:
1521 if (ret) {
1522 /* restore the context */
1523 lyjson_ctx_restore(jsonctx);
1524 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001525 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001526 }
1527 }
1528 return ret;
1529}
1530
1531LY_ERR
1532lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1533{
1534 LY_ERR ret = LY_SUCCESS;
1535 struct lyd_json_ctx *lydctx = NULL;
1536 struct lyd_node *ntf_e = NULL;
1537 struct lyd_node *tree = NULL;
1538 enum LYJSON_PARSER_STATUS status;
1539
1540 /* init */
1541 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1542 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1543 lydctx->int_opts = LYD_INTOPT_NOTIF;
1544
1545 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1546 assert(status == LYJSON_OBJECT);
1547
1548 /* parse "notification" and "eventTime", if present */
1549 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e);
1550 if (ret == LY_ENOT) {
1551 ret = LY_SUCCESS;
1552 } else if (ret) {
1553 goto cleanup;
1554 }
1555
Michal Vaskocf770e22020-08-12 13:21:43 +02001556 /* read subtree */
1557 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1558 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001559
1560 /* finish linking metadata */
1561 ret = lydjson_metadata_finish(lydctx, &tree);
1562 LY_CHECK_GOTO(ret, cleanup);
1563
1564 /* make sure we have parsed some notification */
1565 if (!lydctx->op_node) {
1566 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1567 ret = LY_EVALID;
1568 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001569 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001570 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001571 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001572 ret = LY_EVALID;
1573 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001574 }
1575
1576 if (ntf_e) {
1577 /* finish notification envelope */
1578 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1579 LY_CHECK_GOTO(ret, cleanup);
1580 if (status == LYJSON_END) {
1581 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1582 ret = LY_EVALID;
1583 goto cleanup;
1584 } else if (status != LYJSON_OBJECT_CLOSED) {
1585 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
Michal Vasko69730152020-10-09 16:30:07 +02001586 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001587 ret = LY_EVALID;
1588 goto cleanup;
1589 }
1590 }
1591
1592 if (ntf_p) {
1593 *ntf_p = lydctx->op_node;
1594 }
1595 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001596 if (ntf_e) {
1597 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001598 lyd_insert_node(ntf_e, NULL, tree);
1599 tree = ntf_e;
1600 }
1601 if (tree_p) {
1602 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001603 }
1604
1605cleanup:
1606 /* we have used parse_only flag */
1607 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1608
1609 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1610 if (ret) {
1611 lyd_free_all(tree);
1612 lyd_free_tree(ntf_e);
1613 }
1614 return ret;
1615}
1616
1617/**
1618 * @brief Parse optional JSON envelope around the processed content.
1619 *
1620 * @param[in] jsonctx JSON parser context
1621 * @param[in] parent Parent node (some other envelope).
1622 * @param[in] module_key Name of the module where the envelope element is expected.
1623 * @param[in] object_id Name of the envelope object.
1624 * @param[out] envp_p Pointer to the created envelope opaq container.
1625 * @return LY_SUCCESS if the envelope present and successfully parsed.
1626 * @return LY_ENOT in case there is not the expected envelope.
1627 * @return LY_ERR in case of parsing failure.
1628 */
1629static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001630lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejci0f969882020-08-21 16:56:47 +02001631 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001632{
1633 LY_ERR ret = LY_ENOT, r;
1634 const char *name, *prefix;
1635 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001636 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001637 enum LYJSON_PARSER_STATUS status;
1638
1639 *envp_p = NULL;
1640
1641 /* "id" envelope */
1642 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1643 LY_CHECK_GOTO(is_attr, cleanup);
1644 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1645 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1646 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1647
1648 r = lyjson_ctx_next(jsonctx, &status);
1649 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1650 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1651
1652 /* create the object envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001653 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), "", 0, NULL, LYD_JSON, LYD_NODEHINT_ENVELOPE,
Michal Vasko69730152020-10-09 16:30:07 +02001654 NULL, NULL, 0, module_key, ly_strlen(module_key), envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001655 LY_CHECK_GOTO(ret, cleanup);
1656
1657 if (parent) {
1658 lyd_insert_node(parent, NULL, *envp_p);
1659 }
1660
1661 ret = LY_SUCCESS;
1662cleanup:
1663 return ret;
1664}
1665
1666static LY_ERR
1667lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1668{
1669 enum LYJSON_PARSER_STATUS status;
1670
1671 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1672 if (status == LYJSON_END) {
1673 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1674 return LY_EVALID;
1675 } else if (status != LYJSON_OBJECT_CLOSED) {
1676 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001677 jsonctx->value_len, jsonctx->value, object_id);
Radek Krejci1798aae2020-07-14 13:26:06 +02001678 return LY_EVALID;
1679 }
1680 return LY_SUCCESS;
1681}
1682
1683LY_ERR
1684lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1685{
1686 LY_ERR ret = LY_SUCCESS;
1687 struct lyd_json_ctx *lydctx = NULL;
1688 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1689 struct lyd_node *tree = NULL;
1690 enum LYJSON_PARSER_STATUS status;
1691
1692 /* init */
1693 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1694 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1695 lydctx->int_opts = LYD_INTOPT_RPC;
1696
1697 /* process envelope(s), if present */
1698
1699 /* process rpc */
1700 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1701 if (ret == LY_ENOT) {
1702 ret = LY_SUCCESS;
1703 goto parse_content;
1704 } else if (ret) {
1705 goto cleanup;
1706 }
1707 /* process action */
1708 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1709 if (ret == LY_ENOT) {
1710 ret = LY_SUCCESS;
1711 goto parse_content;
1712 } else if (ret) {
1713 goto cleanup;
1714 }
1715
1716parse_content:
1717
1718 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1719 assert(status == LYJSON_OBJECT);
1720
1721 /* read subtree(s) */
Michal Vasko22df3f02020-08-24 13:29:22 +02001722 ret = lydjson_subtree_r(lydctx, act_e ? (struct lyd_node_inner *)act_e : (struct lyd_node_inner *)rpc_e, &tree);
Michal Vaskocf770e22020-08-12 13:21:43 +02001723 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001724
1725 /* finish linking metadata */
1726 ret = lydjson_metadata_finish(lydctx, &tree);
1727 LY_CHECK_GOTO(ret, cleanup);
1728
1729 /* make sure we have parsed some operation */
1730 if (!lydctx->op_node) {
1731 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
Michal Vasko69730152020-10-09 16:30:07 +02001732 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
Radek Krejci1798aae2020-07-14 13:26:06 +02001733 ret = LY_EVALID;
1734 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001735 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001736 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001737 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001738 ret = LY_EVALID;
1739 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001740 }
1741
1742 if (act_e) {
1743 /* finish action envelope */
1744 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action");
1745 LY_CHECK_GOTO(ret, cleanup);
1746 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1747 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001748 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001749 ret = LY_EVALID;
1750 goto cleanup;
1751 }
1752 }
1753 if (rpc_e) {
1754 /* finish rpc envelope */
1755 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc");
1756 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko69730152020-10-09 16:30:07 +02001757 if (!act_e && (lydctx->op_node->schema->nodetype != LYS_RPC)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001758 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001759 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001760 ret = LY_EVALID;
1761 goto cleanup;
1762 }
1763 }
1764
1765 if (op_p) {
1766 *op_p = lydctx->op_node;
1767 }
1768 assert(tree);
1769 if (tree_p) {
1770 if (rpc_e) {
1771 *tree_p = rpc_e;
1772 } else if (act_e) {
1773 *tree_p = act_e;
1774 } else {
1775 *tree_p = tree;
1776 }
1777 }
1778
1779cleanup:
1780 /* we have used parse_only flag */
1781 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1782
1783 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1784 if (ret) {
1785 lyd_free_all(tree);
1786 lyd_free_tree(act_e);
1787 lyd_free_tree(rpc_e);
1788 }
1789 return ret;
1790}
1791
1792LY_ERR
1793lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1794{
1795 LY_ERR ret = LY_SUCCESS;
1796 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001797 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001798 enum LYJSON_PARSER_STATUS status;
1799
1800 /* init */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001801 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001802 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1803 lydctx->int_opts = LYD_INTOPT_REPLY;
1804
1805 /* find request OP */
1806 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1807 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1808 break;
1809 }
1810 LYD_TREE_DFS_END(request, req_op);
1811 }
1812 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001813 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001814 ret = LY_EINVAL;
1815 goto cleanup;
1816 }
1817
1818 /* duplicate request OP with parents */
1819 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1820
1821 /* parse "rpc-reply", if any */
1822 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e);
1823 if (ret == LY_ENOT) {
1824 ret = LY_SUCCESS;
1825 } else if (ret) {
1826 goto cleanup;
1827 }
1828
1829 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1830 assert(status == LYJSON_OBJECT);
1831
1832 /* read subtree(s) */
1833 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001834 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op));
Radek Krejci1798aae2020-07-14 13:26:06 +02001835 LY_CHECK_GOTO(ret, cleanup);
1836
1837 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1838 }
1839
1840 /* finish linking metadata */
1841 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1842 LY_CHECK_GOTO(ret, cleanup);
1843
1844 if (rpcr_e) {
1845 /* finish rpc-reply envelope */
1846 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply");
1847 LY_CHECK_GOTO(ret, cleanup);
1848 }
1849
1850 if (op_p) {
1851 *op_p = rep_op;
1852 }
Radek Krejcia1c1e542020-09-29 16:06:52 +02001853 for (tree = rep_op; tree->parent; tree = lyd_parent(tree)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001854 if (rpcr_e) {
1855 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001856 lyd_insert_node(rpcr_e, NULL, tree);
1857 tree = rpcr_e;
1858 }
1859 if (tree_p) {
1860 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001861 }
1862
1863cleanup:
1864 /* we have used parse_only flag */
1865 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1866
1867 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1868 if (ret) {
1869 lyd_free_all(rep_op);
1870 lyd_free_tree(rpcr_e);
1871 }
1872 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001873}