blob: f9cf274a47b129748fd7a7ca0efb3275d57f0708 [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"
Radek Krejci1798aae2020-07-14 13:26:06 +020024#include "json.h"
25#include "parser_internal.h"
26#include "tree_data.h"
27#include "tree_data_internal.h"
28#include "tree_schema.h"
29#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010030
31/**
Radek Krejci1798aae2020-07-14 13:26:06 +020032 * @brief Internal context for JSON YANG data parser.
33 *
34 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
35 */
36struct lyd_json_ctx {
37 uint32_t parse_options; /**< various @ref dataparseroptions. */
38 uint32_t validate_options; /**< various @ref datavalidationoptions. */
39 uint32_t int_opts; /**< internal data parser options */
40 uint32_t path_len; /**< used bytes in the path buffer */
41 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
42 struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
43 struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
44 struct ly_set when_check; /**< set of nodes with "when" conditions */
45 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
46
47 /* callbacks */
48 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020049
50 struct lyjson_ctx *jsonctx; /**< JSON context */
51};
52
53/**
54 * @brief Free the JSON data parser context.
55 *
56 * JSON implementation of lyd_ctx_free_clb().
57 */
58static void
59lyd_json_ctx_free(struct lyd_ctx *lydctx)
60{
61 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
62
63 if (lydctx) {
64 lyd_ctx_free(lydctx);
65 lyjson_ctx_free(ctx->jsonctx);
66 free(ctx);
67 }
68}
69
70/**
Radek Krejci1798aae2020-07-14 13:26:06 +020071 * @brief Parse JSON member-name as [\@][prefix:][name]
72 *
Michal Vaskocabe0702020-08-12 10:14:36 +020073 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020074 * prefix - name of the module of the data node
75 * name - name of the data node
76 *
77 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
78 *
79 * @param[in] value String to parse
80 * @param[in] value_len Length of the @p str.
81 * @param[out] name_p Pointer to the beginning of the parsed name.
82 * @param[out] name_len_p Pointer to the length of the parsed name.
83 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
84 * @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 +020085 * @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 +020086 */
87static void
Michal Vaskocabe0702020-08-12 10:14:36 +020088lydjson_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 +020089 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020090{
91 const char *name, *prefix = NULL;
92 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +020093 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020094
95 name = memchr(value, ':', value_len);
96 if (name != NULL) {
97 prefix = value;
98 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +020099 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200100 prefix++;
101 }
102 prefix_len = name - prefix;
103 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200104 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200105 } else {
106 name = value;
107 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200108 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200109 name++;
110 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200111 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100112 }
113
Radek Krejci1798aae2020-07-14 13:26:06 +0200114 *name_p = name;
115 *name_len_p = name_len;
116 *prefix_p = prefix;
117 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200118 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200119}
120
121/**
122 * @brief Get correct prefix (module_name) inside the @p node.
123 *
124 * @param[in] node Data node to get inherited prefix.
125 * @param[in] local_prefix Local prefix to replace the inherited prefix.
126 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
127 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
128 * and no context @p node to get inherited prefix.
129 * @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
130 * of no local prefix and no context @p node to get inherited prefix.
131 * @return LY_ERR value.
132 */
133static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200134lydjson_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 +0200135 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200136{
137 struct lyd_node_opaq *onode;
138 const char *module_name = NULL;
139
140 assert(prefix_p && prefix_len_p);
141
142 if (local_prefix_len) {
143 *prefix_p = local_prefix;
144 *prefix_len_p = local_prefix_len;
145 return LY_SUCCESS;
146 }
147
148 *prefix_p = NULL;
149 while (node) {
150 if (node->schema) {
151 *prefix_p = node->schema->module->name;
152 break;
153 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200154 onode = (struct lyd_node_opaq *)node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200155 if (onode->prefix.module_name) {
156 *prefix_p = onode->prefix.module_name;
157 break;
158 } else if (onode->prefix.id) {
159 *prefix_p = onode->prefix.id;
160 break;
161 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200162 node = (struct lyd_node *)node->parent;
Radek Krejci1798aae2020-07-14 13:26:06 +0200163 }
164 *prefix_len_p = ly_strlen(module_name);
165
166 return LY_SUCCESS;
167}
168
169/**
170 * @brief Get schema node corresponding to the input parameters.
171 *
172 * @param[in] lydctx JSON data parser context.
173 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
174 * @param[in] prefix Requested node's prefix (module name).
175 * @param[in] prefix_len Length of the @p prefix.
176 * @param[in] name Requested node's name.
177 * @param[in] name_len Length of the @p name.
178 * @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
179 * is accepted for searching top-level nodes.
180 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
181 * @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.
182 * @return LY_EVALID on failure, error message is logged
183 * @return LY_ENOT in case the input data are expected to be skipped
184 */
185static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200186lydjson_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 +0200187 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200188{
189 struct lys_module *mod = NULL;
190
191 /* leave if-feature check for validation */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200192 uint32_t getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200193
194 /* init return value */
195 *snode_p = NULL;
196
197 /* get the element module */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200198 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200199 if (!prefix_len) {
200 /* opaq parent (envelope) - the second part of the condition */
Michal Vasko22df3f02020-08-24 13:29:22 +0200201 lydjson_get_node_prefix((struct lyd_node *)parent, NULL, 0, &prefix, &prefix_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200202 }
203 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
204 } else if (parent) {
205 if (parent->schema) {
206 mod = parent->schema->module;
207 }
208 } else {
209 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 +0200210 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200211 return LY_EVALID;
212 }
213 if (!mod) {
214 if (lydctx->parse_options & LYD_PARSE_STRICT) {
215 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
216 return LY_EVALID;
217 }
218 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
219 return LY_ENOT;
220 }
221 }
222
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200223 if (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200224 /* ignore the envelope parent when searchinf for the schema node */
225 parent = NULL;
226 }
227
228 /* get the schema node */
229 if (mod && (!parent || parent->schema)) {
230 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
231 if (!*snode_p) {
232 if (lydctx->parse_options & LYD_PARSE_STRICT) {
233 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 +0200234 name_len, name, mod->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200235 return LY_EVALID;
236 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
237 /* skip element with children */
238 return LY_ENOT;
239 }
240 } else {
241 /* check that schema node is valid and can be used */
242 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
243 }
244 }
245
246 return LY_SUCCESS;
247}
248
249/**
250 * @brief Skip the currently open JSON object/array
251 * @param[in] jsonctx JSON context with the input data to skip.
252 * @return LY_ERR value.
253 */
254static LY_ERR
255lydjson_data_skip(struct lyjson_ctx *jsonctx)
256{
257 enum LYJSON_PARSER_STATUS status, current;
258 size_t sublevels = 1;
259
260 status = lyjson_ctx_status(jsonctx, 0);
261
262 /* skip after the content */
263 do {
264 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
265 if (current == status) {
266 sublevels++;
267 } else if (current == status + 1) {
268 sublevels--;
269 }
270 } while (current != status + 1 && sublevels);
271 /* open the next sibling */
272 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
273
274 return LY_SUCCESS;
275}
276
277/**
278 * @brief Go through the @p value and find all possible prefixes and store them in @p val_prefs_p [sized array](@ref sizedarrays).
279 *
280 * @param[in] ctx libyang context
281 * @param[in] value Pointer to the beginning of the value to check.
282 * @param[in] value_len Length of the string to examine in @p value.
283 * @param[out] val_prefs_p Pointer to the resulting [sized array](@ref sizedarrays) of found prefixes. NULL in case there are no prefixes.
284 * @return LY_EMEM on memory allocation failure.
285 * @return LY_SUCCESS on success, empty @p val_prefs_p (NULL) is valid result if there are no possible prefixes
286 */
287static LY_ERR
288lydjson_get_value_prefixes(const struct ly_ctx *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs_p)
289{
290 LY_ERR ret;
291 LY_ARRAY_COUNT_TYPE u;
292 uint32_t c;
293 const char *start, *stop;
294 struct ly_prefix *prefixes = NULL;
295 size_t len;
296
297 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
298 size_t bytes;
299 ly_getutf8(&stop, &c, &bytes);
300 if (is_yangidentstartchar(c)) {
301 for (ly_getutf8(&stop, &c, &bytes);
302 is_yangidentchar(c) && (size_t)(stop - value) < value_len;
Radek Krejci1e008d22020-08-17 11:37:37 +0200303 ly_getutf8(&stop, &c, &bytes)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200304 stop = stop - bytes;
305 if (*stop == ':') {
306 /* we have a possible prefix */
307 struct ly_prefix *p = NULL;
308
309 len = stop - start;
310
311 /* check whether we do not already have this prefix stored */
312 LY_ARRAY_FOR(prefixes, u) {
313 if (!ly_strncmp(prefixes[u].id, start, len)) {
314 p = &prefixes[u];
315 break;
316 }
317 }
318 if (!p) {
319 LY_ARRAY_NEW_GOTO(ctx, prefixes, p, ret, error);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200320 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->id), error);
321 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->module_name), error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200322 } /* else the prefix already present */
323 }
324 stop = stop + bytes;
325 }
326 }
327
328 *val_prefs_p = prefixes;
329 return LY_SUCCESS;
330
331error:
332 LY_ARRAY_FOR(prefixes, u) {
333 lydict_remove(ctx, prefixes[u].id);
334 lydict_remove(ctx, prefixes[u].module_name);
335 }
336 LY_ARRAY_FREE(prefixes);
337 return ret;
338}
339
340/**
341 * @brief Check that the input data are parseable as the @p list.
342 *
343 * Checks for all the list's keys. Function does not revert the context state.
344 *
345 * @param[in] jsonctx JSON parser context.
346 * @param[in] list List schema node corresponding to the input data object.
347 * @return LY_SUCCESS in case the data are ok for the @p list
348 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
349 */
350static LY_ERR
351lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
352{
353 LY_ERR ret = LY_SUCCESS;
354 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
355 struct ly_set key_set = {0};
356 const struct lysc_node *snode;
357 uint32_t i, status_count;
358
359 assert(list && (list->nodetype == LYS_LIST));
360 assert(status == LYJSON_OBJECT);
361
362 /* get all keys into a set (keys do not have if-features or anything) */
363 snode = NULL;
364 while ((snode = lys_getnext(snode, list, NULL, LYS_GETNEXT_NOSTATECHECK)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200365 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200366 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200367 }
368
369 if (status != LYJSON_OBJECT_EMPTY) {
370 status_count = jsonctx->status.count;
371
372 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
373 const char *name, *prefix;
374 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200375 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200376
377 /* match the key */
378 snode = NULL;
379 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
380
381 if (!is_attr && !prefix) {
382 for (i = 0; i < key_set.count; ++i) {
383 snode = (const struct lysc_node *)key_set.objs[i];
384 if (!ly_strncmp(snode->name, name, name_len)) {
385 break;
386 }
387 }
388 /* go into the item to a) process it as a key or b) start skipping it as another list child */
389 ret = lyjson_ctx_next(jsonctx, &status);
390 LY_CHECK_GOTO(ret, cleanup);
391
392 if (snode) {
393 /* we have the key, validate the value */
394 if (status < LYJSON_NUMBER) {
395 /* not a terminal */
396 ret = LY_ENOT;
397 goto cleanup;
398 }
399
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200400 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 LY_CHECK_GOTO(ret, cleanup);
402
403 /* key with a valid value, remove from the set */
404 ly_set_rm_index(&key_set, i, NULL);
405 }
406 } else {
407 /* start skipping the member we are not interested in */
408 ret = lyjson_ctx_next(jsonctx, &status);
409 LY_CHECK_GOTO(ret, cleanup);
410 }
411 /* move to the next child */
412 while (status_count < jsonctx->status.count) {
413 ret = lyjson_ctx_next(jsonctx, &status);
414 LY_CHECK_GOTO(ret, cleanup);
415 }
416 }
417 }
418
419 if (key_set.count) {
420 /* some keys are missing/did not validate */
421 ret = LY_ENOT;
422 }
423
424cleanup:
425 ly_set_erase(&key_set, NULL);
426 return ret;
427}
428
429/**
430 * @brief Get the hint for the data type parsers according to the current JSON parser context.
431 *
432 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
433 * @param[in,out] status Pointer to the current context status,
434 * in some circumstances the function manipulates with the context so the status is updated.
435 * @param[out] type_hint_p Pointer to the variable to store the result.
436 * @return LY_SUCCESS in case of success.
437 * @return LY_EINVAL in case of invalid context status not referring to a value.
438 */
439static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200440lydjson_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 +0200441{
442 *type_hint_p = 0;
443
444 if (*status_p == LYJSON_ARRAY) {
445 /* only [null] */
446 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
447 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
448
449 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
450 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
451
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200452 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200453 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200454 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200455 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200456 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200457 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200458 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200459 } else if (*status_p == LYJSON_NULL) {
460 *type_hint_p = 0;
461 } else {
462 return LY_EINVAL;
463 }
464
465 return LY_SUCCESS;
466}
467
468/**
469 * @brief Check in advance if the input data are parsable according to the provided @p snode.
470 *
471 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
472 * is naturally done when the data are really parsed.
473 *
474 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
475 * as before calling, despite it is necessary to process input data for checking.
476 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
477 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
478 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
479 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
480 * @return LY_EINVAL in case of invalid leaf JSON encoding
481 * and they are expected to be parsed as opaq nodes.
482 */
483static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200484lydjson_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 +0200485{
486 LY_ERR ret = LY_SUCCESS;
487 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
488 enum LYJSON_PARSER_STATUS status;
489
490 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200491
Michal Vasko32ac9942020-08-12 14:35:12 +0200492 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
493 /* can always be parsed as a data node if we have the schema node */
494 return LY_SUCCESS;
495 }
496
497 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200498 /* backup parser */
499 lyjson_ctx_backup(jsonctx);
500 status = lyjson_ctx_status(jsonctx, 0);
501
Michal Vasko32ac9942020-08-12 14:35:12 +0200502 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
503 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200504 switch (snode->nodetype) {
505 case LYS_LEAFLIST:
506 case LYS_LEAF:
507 /* value may not be valid in which case we parse it as an opaque node */
508 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
509 if (ret) {
510 break;
511 }
512
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200513 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 ret = LY_ENOT;
515 }
516 break;
517 case LYS_LIST:
518 /* lists may not have all its keys */
519 if (lydjson_check_list(jsonctx, snode)) {
520 /* invalid list, parse as opaque if it missing/has invalid some keys */
521 ret = LY_ENOT;
522 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200523 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200524 }
525
526 /* restore parser */
527 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200528 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200529 status = lyjson_ctx_status(jsonctx, 0);
530 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
531 }
532
533 return ret;
534}
535
536/**
537 * @brief Join the forward-referencing metadata with their target data nodes.
538 *
539 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
540 *
541 * @param[in] lydctx JSON data parser context.
542 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
543 * as a starting point to search for the metadata's target data node
544 * @return LY_SUCCESS on success
545 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
546 */
547static LY_ERR
548lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
549{
550 LY_ERR ret = LY_SUCCESS;
551 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200552 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200553 const char *prev = NULL;
554
555 /* finish linking metadata */
556 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200557 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200558 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200559 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200560 const char *name, *prefix;
561 size_t name_len, prefix_len;
562 const struct lysc_node *snode;
563
Michal Vasko69730152020-10-09 16:30:07 +0200564 if (attr->schema || (meta_container->name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200565 /* not an opaq metadata node */
566 continue;
567 }
568
569 if (prev != meta_container->name) {
570 /* metas' names are stored in dictionary, so checking pointers must works */
571 lydict_remove(lydctx->jsonctx->ctx, prev);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200572 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 instance = 1;
574 } else {
575 instance++;
576 }
577
578 /* find the correspnding data node */
579 LY_LIST_FOR(start, node) {
580 if (!node->schema) {
581 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vasko22df3f02020-08-24 13:29:22 +0200582 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq *)node)->name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200583 continue;
584 }
585
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200586 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200587 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vasko69730152020-10-09 16:30:07 +0200588 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200589 ret = LY_EVALID;
590 goto cleanup;
591 }
592
593 /* match */
594 match++;
595 if (match != instance) {
596 continue;
597 }
598
599 LY_LIST_FOR(meta_container->child, meta_iter) {
600 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200601 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200602 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200603 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200604
605 /* get value prefixes */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200606 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value,
607 lydctx->jsonctx->value_len, &val_prefs), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200608
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200609 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value,
610 ly_strlen(meta->value), &dynamic, LYD_JSON, meta->hints, val_prefs, meta->prefix.id,
611 ly_strlen(meta->prefix.id), meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
Radek Krejci1798aae2020-07-14 13:26:06 +0200612 LY_CHECK_GOTO(ret, cleanup);
613 }
614
615 /* done */
616 break;
617 } else {
618 /* this is the second time we are resolving the schema node, so it must succeed,
619 * but remember that snode can be still NULL */
620 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
621 assert(is_attr);
622 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
623 assert(ret == LY_SUCCESS);
624
625 if (snode != node->schema) {
626 continue;
627 }
628
629 /* match */
630 match++;
631 if (match != instance) {
632 continue;
633 }
634
635 LY_LIST_FOR(meta_container->child, meta_iter) {
636 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200637 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200638 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200639 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200640
641 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
642 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200643 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vasko69730152020-10-09 16:30:07 +0200644 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
645 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200646 LY_CHECK_GOTO(ret, cleanup);
647 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
648 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200649 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
650 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200651 ret = LY_EVALID;
652 goto cleanup;
653 }
654 }
655 /* add/correct flags */
656 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
657
658 /* done */
659 break;
660 }
661 }
662
663 if (match != instance) {
664 /* there is no corresponding data node for the metadata */
665 if (instance > 1) {
666 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200667 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
668 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200669 } else {
670 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200671 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200672 }
673 ret = LY_EVALID;
674 } else {
675 /* remove the opaq attr */
676 if (attr == (*first_p)) {
677 *first_p = attr->next;
678 }
679 lyd_free_tree(attr);
680 }
681 }
682
683cleanup:
684 lydict_remove(lydctx->jsonctx->ctx, prev);
685
686 return ret;
687}
688
689/**
690 * @brief Parse a metadata member.
691 *
692 * @param[in] lydctx JSON data parser context.
693 * @param[in] snode Schema node of the metadata parent.
694 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
695 * so the data node does not exists. In such a case the metadata is stored in the context for the later
696 * processing by lydjson_metadata_finish().
697 * @return LY_SUCCESS on success
698 * @return Various LY_ERR values in case of failure.
699 */
700static LY_ERR
701lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
702{
703 LY_ERR ret = LY_SUCCESS;
704 enum LYJSON_PARSER_STATUS status;
705 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200706 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200707 const char *name, *prefix = NULL;
708 size_t name_len, prefix_len = 0;
709 struct lys_module *mod;
710 struct lyd_meta *meta = NULL;
711 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200712 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200713 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200714 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200715 uint16_t nodetype;
716
717 assert(snode || node);
718
719 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
720
721 /* move to the second item in the name/X pair */
722 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
723 LY_CHECK_GOTO(ret, cleanup);
724
725 /* check attribute encoding */
726 switch (nodetype) {
727 case LYS_LEAFLIST:
728 expected = "@name/array of objects/nulls";
729
730 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
731
732next_entry:
733 instance++;
734
735 /* move into array / next entry */
736 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
737 LY_CHECK_GOTO(ret, cleanup);
738
739 if (status == LYJSON_ARRAY_CLOSED) {
740 /* we are done, move after the array */
741 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
742 goto cleanup;
743 }
744 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
745
Michal Vasko69730152020-10-09 16:30:07 +0200746 if (!node || (node->schema != prev->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200747 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200748 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
749 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 ret = LY_EVALID;
751 goto cleanup;
752 }
753
754 if (status == LYJSON_NULL) {
755 /* continue with the next entry in the leaf-list array */
756 prev = node;
757 node = node->next;
758 goto next_entry;
759 }
760 break;
761 case LYS_LEAF:
762 case LYS_ANYXML:
763 expected = "@name/object";
764
765 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
766 break;
767 case LYS_CONTAINER:
768 case LYS_LIST:
769 case LYS_ANYDATA:
770 case LYS_NOTIF:
771 case LYS_ACTION:
772 case LYS_RPC:
773 in_parent = 1;
774 expected = "@/object";
775 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
776 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200777 default:
778 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200779 }
780
781 /* process all the members inside a single metadata object */
782 assert(status == LYJSON_OBJECT);
783
784 while (status != LYJSON_OBJECT_CLOSED) {
785 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
786 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200787 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200788 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
789 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200790 ret = LY_EVALID;
791 goto cleanup;
792 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200793 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200794 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
795 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200796 ret = LY_EVALID;
797 goto cleanup;
798 }
799
800 /* get the element module */
801 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
802 if (!mod) {
803 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200804 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200805 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
806 prefix_len, prefix, name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200807 ret = LY_EVALID;
808 goto cleanup;
809 }
810 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
811 /* skip element with children */
812 ret = lydjson_data_skip(lydctx->jsonctx);
813 LY_CHECK_GOTO(ret, cleanup);
814 status = lyjson_ctx_status(lydctx->jsonctx, 0);
815 /* end of the item */
816 continue;
817 }
818 }
819
820 /* get the value */
821 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
822 LY_CHECK_GOTO(ret, cleanup);
823
824 if (node->schema) {
825 /* create metadata */
826 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200827 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 +0200828 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
829 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 LY_CHECK_GOTO(ret, cleanup);
831
832 /* add/correct flags */
833 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
834 } else {
835 /* create attribute */
836 struct ly_prefix *val_prefs = NULL;
837 const char *module_name;
838 size_t module_name_len;
839
840 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
841
842 /* get value prefixes */
843 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
844
845 /* attr2 is always changed to the created attribute */
846 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 +0200847 &lydctx->jsonctx->dynamic, LYD_JSON, 0, val_prefs, prefix, prefix_len, module_name, module_name_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200848 LY_CHECK_GOTO(ret, cleanup);
849 }
850 /* next member */
851 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
852 LY_CHECK_GOTO(ret, cleanup);
853 }
854
855 if (nodetype == LYS_LEAFLIST) {
856 /* continue by processing another metadata object for the following
857 * leaf-list instance since they are allways instantiated in JSON array */
858 prev = node;
859 node = node->next;
860 goto next_entry;
861 }
862
863 /* move after the metadata */
864 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
865 LY_CHECK_GOTO(ret, cleanup);
866
867cleanup:
868 return ret;
869
870representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200871 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200872 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
873 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name,
874 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200875
876 return LY_EVALID;
877}
878
879/**
880 * @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.
881 *
882 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
883 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
884 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
885 */
886static void
887lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
888{
889 if (*node_p) {
890 /* insert, keep first pointer correct */
891 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
892 if (first_p) {
893 if (parent) {
894 *first_p = parent->child;
895 } else {
896 while ((*first_p)->prev->next) {
897 *first_p = (*first_p)->prev;
898 }
899 }
900 }
901 *node_p = NULL;
902 }
903}
904
905static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
906
907/**
908 * @brief Parse opaq node from the input.
909 *
910 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
911 *
912 * @param[in] lydctx JSON data parser context.
913 * @param[in] name Name of the opaq node to create.
914 * @param[in] name_len Length of the @p name string.
915 * @param[in] prefix Prefix of the opaq node to create.
916 * @param[in] prefix_len Length of the @p prefx string.
917 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
918 * but must be set if @p first is not.
919 * @param[in,out] status_p Pointer to the current status of the parser context,
920 * since the function manipulates with the context and process the input, the status can be updated.
921 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
922 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
923 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
924 * @param[out] node_p Pointer to the created opaq node.
925 * @return LY_ERR value.
926 */
927static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200928lydjson_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 +0200929 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
930 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200931{
932 LY_ERR ret = LY_SUCCESS;
933 const char *value = NULL, *module_name;
934 size_t value_len = 0, module_name_len = 0;
935 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200936 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200937 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200938
Michal Vasko69730152020-10-09 16:30:07 +0200939 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200940 /* prepare for creating opaq node with a value */
941 value = lydctx->jsonctx->value;
942 value_len = lydctx->jsonctx->value_len;
943 dynamic = lydctx->jsonctx->dynamic;
944 lydctx->jsonctx->dynamic = 0;
945
946 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
947
948 if (value) {
949 /* get value prefixes */
950 LY_CHECK_RET(lydjson_get_value_prefixes(lydctx->jsonctx->ctx, value, value_len, &val_prefs));
951 }
952 }
953
954 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200955 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200956 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, LYD_JSON, type_hint,
957 val_prefs, prefix, prefix_len, module_name, module_name_len, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200958 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200959 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200960 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200961 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200962
Michal Vasko69730152020-10-09 16:30:07 +0200963 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200964 /* process children */
965 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
966 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
967 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
968 }
Michal Vasko69730152020-10-09 16:30:07 +0200969 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200970 /* process another instance of the same node */
971 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200972 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200973
Michal Vasko69730152020-10-09 16:30:07 +0200974 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200975 /* but first process children of the object in the array */
976 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
977 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
978 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
979 }
980 }
981
982 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
983
984 /* continue with the next instance */
985 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
986 assert(node_p);
987 lydjson_maintain_children(parent, first_p, node_p);
988 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
989 }
990 }
991
992 /* finish linking metadata */
993 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
994
995 /* move after the item */
996 return lyjson_ctx_next(lydctx->jsonctx, status_p);
997}
998
999/**
1000 * @brief Process the attribute container (starting by @)
1001 *
1002 * @param[in] lydctx JSON data parser context.
1003 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1004 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1005 * @param[in] name Name of the opaq node to create.
1006 * @param[in] name_len Length of the @p name string.
1007 * @param[in] prefix Prefix of the opaq node to create.
1008 * @param[in] prefix_len Length of the @p prefx string.
1009 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1010 * but must be set if @p first is not.
1011 * @param[in,out] status_p Pointer to the current status of the parser context,
1012 * since the function manipulates with the context and process the input, the status can be updated.
1013 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1014 * @param[out] node_p Pointer to the created opaq node.
1015 * @return LY_ERR value.
1016 */
1017static LY_ERR
1018lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +02001019 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1020 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1021 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001022{
1023 LY_ERR ret = LY_SUCCESS;
1024 enum LYJSON_PARSER_STATUS status_inner;
1025
1026 /* parse as an attribute to a node */
1027 if (!attr_node && snode) {
1028 /* try to find the instance */
1029 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1030 if (iter->schema == snode) {
1031 attr_node = iter;
1032 break;
1033 }
1034 }
1035 }
1036 if (!attr_node) {
1037 /* parse just as an opaq node with the name beginning with @,
1038 * later we have to check that it belongs to a standard node
1039 * and it is supposed to be converted to a metadata */
1040 uint32_t prev_opts;
1041
1042 /* move into metadata */
1043 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1044
1045 if (*status_p == LYJSON_ARRAY) {
1046 /* move into the array */
1047 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1048 } else {
1049 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1050 status_inner = LYJSON_ERROR;
1051 }
1052
1053 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
1054 prev_opts = lydctx->parse_options;
1055 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1056 lydctx->parse_options |= LYD_PARSE_OPAQ;
1057
1058 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 +02001059 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001060
1061 /* restore the parser options */
1062 lydctx->parse_options = prev_opts;
1063 } else {
1064 ret = lydjson_metadata(lydctx, snode, attr_node);
1065 }
1066
1067 return ret;
1068}
1069
1070/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001071 * @brief Parse a single instance of a node.
1072 *
1073 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1074 * as before calling, despite it is necessary to process input data for checking.
1075 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1076 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1077 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1078 * @param[in] name Parsed JSON node name.
1079 * @param[in] name_len Lenght of @p name.
1080 * @param[in] prefix Parsed JSON node prefix.
1081 * @param[in] prefix_len Length of @p prefix.
1082 * @param[in,out] status JSON parser status, is updated.
1083 * @param[out] node Parsed data (or opaque) node.
1084 * @return LY_SUCCESS if a node was successfully parsed,
1085 * @return LY_EINVAL in case of invalid JSON encoding,
1086 * @return LY_ERR on other errors.
1087 */
1088static LY_ERR
1089lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001090 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1091 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001092{
1093 LY_ERR ret;
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001094 uint32_t type_hints;
Michal Vasko32ac9942020-08-12 14:35:12 +02001095 uint32_t prev_opts;
1096 struct lyd_node *tree = NULL;
1097
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001098 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001099 if (ret == LY_SUCCESS) {
1100 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1101 if (snode->nodetype & LYD_NODE_TERM) {
1102 /* create terminal node */
1103 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001104 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1105 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001106 LY_CHECK_RET(ret);
1107
1108 /* move JSON parser */
1109 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1110 LY_CHECK_RET(ret);
1111 } else if (snode->nodetype & LYD_NODE_INNER) {
1112 /* create inner node */
1113 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1114
1115 ret = lyd_create_inner(snode, node);
1116 LY_CHECK_RET(ret);
1117
1118 /* process children */
1119 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1120 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1121 LY_CHECK_RET(ret);
1122 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1123 }
1124
1125 /* finish linking metadata */
1126 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1127 LY_CHECK_RET(ret);
1128
1129 if (snode->nodetype == LYS_LIST) {
1130 /* check all keys exist */
1131 ret = lyd_parse_check_keys(*node);
1132 LY_CHECK_RET(ret);
1133 }
1134
1135 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1136 /* new node validation, autodelete CANNOT occur, all nodes are new */
1137 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1138 LY_CHECK_RET(ret);
1139
1140 /* add any missing default children */
1141 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->unres_node_type,
Michal Vasko69730152020-10-09 16:30:07 +02001142 &lydctx->when_check, (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ?
1143 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001144 LY_CHECK_RET(ret);
1145 }
1146
1147 /* move JSON parser */
1148 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1149 LY_CHECK_RET(ret);
1150 } else if (snode->nodetype & LYD_NODE_ANY) {
1151 /* create any node */
1152 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1153
1154 /* parse any data tree with correct options */
1155 /* first backup the current options and then make the parser to process data as opaq nodes */
1156 prev_opts = lydctx->parse_options;
1157 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1158 lydctx->parse_options |= LYD_PARSE_OPAQ;
1159
1160 /* process the anydata content */
1161 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1162 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1163 LY_CHECK_RET(ret);
1164 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1165 }
1166
1167 /* restore parser options */
1168 lydctx->parse_options = prev_opts;
1169
1170 /* finish linking metadata */
1171 ret = lydjson_metadata_finish(lydctx, &tree);
1172 LY_CHECK_RET(ret);
1173
1174 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1175 LY_CHECK_RET(ret);
1176 }
1177 } else if (ret == LY_ENOT) {
1178 /* parse it again as an opaq node */
1179 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001180 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001181 LY_CHECK_RET(ret);
1182
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001183 if (snode->nodetype == LYS_LIST) {
1184 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1185 } else if (snode->nodetype == LYS_LEAFLIST) {
1186 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001187 }
1188 }
1189
1190 return ret;
1191}
1192
1193/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001194 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001195 *
1196 * @param[in] lydctx JSON data parser context.
1197 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1198 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1199 * @return LY_ERR value.
1200 */
1201static LY_ERR
1202lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1203{
1204 LY_ERR ret = LY_SUCCESS;
1205 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1206 enum LYJSON_PARSER_STATUS status_inner = 0;
1207 const char *name, *prefix = NULL;
1208 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001209 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001210 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001211 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001212 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1213 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001214
1215 assert(parent || first_p);
1216 assert(status == LYJSON_OBJECT);
1217
1218 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001219 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 +02001220
Michal Vaskocabe0702020-08-12 10:14:36 +02001221 if (!is_meta || name_len || prefix_len) {
1222 /* get the schema node */
1223 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1224 if (ret == LY_ENOT) {
1225 /* skip element with children */
1226 ret = lydjson_data_skip(lydctx->jsonctx);
1227 LY_CHECK_GOTO(ret, cleanup);
1228 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1229 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001230 goto cleanup;
1231 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001232 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001233
Michal Vaskocabe0702020-08-12 10:14:36 +02001234 if (!snode) {
1235 /* we will not be parsing it as metadata */
1236 is_meta = 0;
1237 }
1238 }
1239
1240 if (is_meta) {
1241 /* parse as metadata */
1242 if (!name_len && !prefix_len) {
1243 /* parent's metadata without a name - use the schema from the parent */
1244 if (!parent) {
1245 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001246 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001247 ret = LY_EVALID;
1248 goto cleanup;
1249 }
1250 attr_node = (struct lyd_node *)parent;
1251 snode = attr_node->schema;
1252 }
1253 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001254 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001255 LY_CHECK_GOTO(ret, cleanup);
1256 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001257 /* parse as an opaq node */
1258 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1259
1260 /* move to the second item in the name/X pair */
1261 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1262 LY_CHECK_GOTO(ret, cleanup);
1263
1264 if (status == LYJSON_ARRAY) {
1265 /* move into the array */
1266 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1267 LY_CHECK_GOTO(ret, cleanup);
1268 } else {
1269 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1270 status_inner = LYJSON_ERROR;
1271 }
1272
1273 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001274 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001275 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001276 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001277 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001278
1279 /* move to the second item in the name/X pair */
1280 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1281 LY_CHECK_GOTO(ret, cleanup);
1282
1283 /* first check the expected representation according to the nodetype and then continue with the content */
1284 switch (snode->nodetype) {
1285 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001286 case LYS_LIST:
1287 if (snode->nodetype == LYS_LEAFLIST) {
1288 expected = "name/array of values";
1289 } else {
1290 expected = "name/array of objects";
1291 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001292
1293 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1294
1295 /* move into array */
1296 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1297 LY_CHECK_GOTO(ret, cleanup);
1298
Michal Vasko32ac9942020-08-12 14:35:12 +02001299 /* process all the values/objects */
1300 do {
1301 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001302
Michal Vasko32ac9942020-08-12 14:35:12 +02001303 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001304 &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001305 if (ret == LY_EINVAL) {
1306 goto representation_error;
1307 } else if (ret) {
1308 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001309 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001310 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001311
Michal Vasko32ac9942020-08-12 14:35:12 +02001312 /* move after the array */
1313 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1314 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001315
1316 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001317 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001318 case LYS_CONTAINER:
1319 case LYS_NOTIF:
1320 case LYS_ACTION:
1321 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001322 case LYS_ANYDATA:
1323 case LYS_ANYXML:
1324 if (snode->nodetype == LYS_LEAF) {
1325 if (status == LYJSON_ARRAY) {
1326 expected = "name/[null]";
1327 } else {
1328 expected = "name/value";
1329 }
1330 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001331 expected = "name/object";
1332 }
1333
Michal Vasko32ac9942020-08-12 14:35:12 +02001334 /* process the value/object */
1335 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1336 if (ret == LY_EINVAL) {
1337 goto representation_error;
1338 } else if (ret) {
1339 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001340 }
1341
Michal Vasko32ac9942020-08-12 14:35:12 +02001342 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001343 /* rememeber the RPC/action/notification */
1344 lydctx->op_node = node;
1345 }
1346
1347 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001348 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001349 }
1350
Michal Vasko32ac9942020-08-12 14:35:12 +02001351 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001352 lydjson_maintain_children(parent, first_p, &node);
1353
1354cleanup:
1355 lyd_free_tree(node);
1356 return ret;
1357
1358representation_error:
1359 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001360 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1361 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejci1798aae2020-07-14 13:26:06 +02001362
1363 ret = LY_EVALID;
1364 goto cleanup;
1365}
1366
1367/**
1368 * @brief Common start of JSON parser processing different types of the input data.
1369 *
1370 * @param[in] ctx libyang context
1371 * @param[in] in Input structure.
1372 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1373 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1374 * @param[out] lydctx_p Data parser context to finish validation.
1375 * @return LY_ERR value.
1376 */
1377static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001378lyd_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 +02001379 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001380{
1381 LY_ERR ret = LY_SUCCESS;
1382 struct lyd_json_ctx *lydctx;
1383 size_t i, line = 1;
1384
Radek Krejci1798aae2020-07-14 13:26:06 +02001385 /* init context */
1386 lydctx = calloc(1, sizeof *lydctx);
1387 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1388 lydctx->parse_options = parse_options;
1389 lydctx->validate_options = validate_options;
1390 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001391
Radek Krejci284f31f2020-09-18 15:40:29 +02001392 /* starting top-level */
1393 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1394 if (in->current[i] == 0x0a) { /* new line */
1395 line++;
Michal Vasko61d76362020-10-07 10:47:30 +02001396 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001397 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001398
Radek Krejci284f31f2020-09-18 15:40:29 +02001399 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1400 if (lyjson_ctx_status(lydctx->jsonctx, 0) == LYJSON_END) {
1401 /* empty data input */
1402 *lydctx_p = lydctx;
1403 return LY_SUCCESS;
1404 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001405 *lydctx_p = lydctx;
1406 return LY_SUCCESS;
1407}
1408
1409LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001410lyd_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 +02001411 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001412{
1413 LY_ERR ret = LY_SUCCESS;
1414 struct lyd_json_ctx *lydctx = NULL;
1415 enum LYJSON_PARSER_STATUS status;
1416
1417 assert(tree_p);
1418 *tree_p = NULL;
1419
1420 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx);
Radek Krejci284f31f2020-09-18 15:40:29 +02001421 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001422
1423 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci284f31f2020-09-18 15:40:29 +02001424 LY_CHECK_GOTO(status == LYJSON_END, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001425 assert(status == LYJSON_OBJECT);
1426
1427 /* read subtree(s) */
1428 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1429 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1430 LY_CHECK_GOTO(ret, cleanup);
1431
1432 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1433 }
1434
1435 /* finish linking metadata */
1436 ret = lydjson_metadata_finish(lydctx, tree_p);
1437 LY_CHECK_GOTO(ret, cleanup);
1438
1439cleanup:
1440 /* there should be no unresolved types stored */
Michal Vasko69730152020-10-09 16:30:07 +02001441 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count &&
1442 !lydctx->when_check.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001443
1444 if (ret) {
1445 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1446 lyd_free_all(*tree_p);
1447 *tree_p = NULL;
1448 } else {
1449 *lydctx_p = (struct lyd_ctx *)lydctx;
1450 }
1451
1452 return ret;
1453}
1454
1455/**
1456 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1457 *
1458 * @param[in] jsonctx JSON parser context
1459 * @param[out] envp_p Pointer to the created envelope opaq container.
1460 * @return LY_SUCCESS if the envelope present and successfully parsed.
1461 * @return LY_ENOT in case there is not the expected envelope.
1462 * @return LY_ERR in case of parsing failure.
1463 */
1464static LY_ERR
1465lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p)
1466{
1467 LY_ERR ret = LY_ENOT, r;
1468 const char *name, *prefix, *value = NULL;
1469 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001470 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001471 enum LYJSON_PARSER_STATUS status;
1472 struct lyd_node *et;
1473
1474 *envp_p = NULL;
1475
1476 /* backup the context */
1477 lyjson_ctx_backup(jsonctx);
1478
1479 /* "notification" envelope */
1480 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1481 LY_CHECK_GOTO(is_attr, cleanup);
1482 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1483 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1484
1485 r = lyjson_ctx_next(jsonctx, &status);
1486 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1487 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1488
1489 /* "eventTime" child */
1490 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1491 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1492 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1493
1494 /* go for the data */
1495 r = lyjson_ctx_next(jsonctx, &status);
1496 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1497 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1498
1499 value = jsonctx->value;
1500 value_len = jsonctx->value_len;
1501 dynamic = jsonctx->dynamic;
1502 jsonctx->dynamic = 0;
1503
1504 r = lyjson_ctx_next(jsonctx, &status);
1505 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1506 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1507 /* now the notificationContent is expected, which will be parsed by the caller */
1508
1509 /* create notification envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001510 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LYD_JSON, LYD_NODEHINT_ENVELOPE, NULL, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001511 0, "ietf-restconf", 13, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001512 LY_CHECK_GOTO(ret, cleanup);
1513 /* create notification envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001514 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 +02001515 NULL, 0, "ietf-restconf", 13, &et);
Radek Krejci1798aae2020-07-14 13:26:06 +02001516 LY_CHECK_GOTO(ret, cleanup);
1517 /* insert eventTime into notification */
1518 lyd_insert_node(*envp_p, NULL, et);
1519
1520 ret = LY_SUCCESS;
1521cleanup:
1522 if (ret) {
1523 /* restore the context */
1524 lyjson_ctx_restore(jsonctx);
1525 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001526 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001527 }
1528 }
1529 return ret;
1530}
1531
1532LY_ERR
1533lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1534{
1535 LY_ERR ret = LY_SUCCESS;
1536 struct lyd_json_ctx *lydctx = NULL;
1537 struct lyd_node *ntf_e = NULL;
1538 struct lyd_node *tree = NULL;
1539 enum LYJSON_PARSER_STATUS status;
1540
1541 /* init */
1542 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1543 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1544 lydctx->int_opts = LYD_INTOPT_NOTIF;
1545
1546 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1547 assert(status == LYJSON_OBJECT);
1548
1549 /* parse "notification" and "eventTime", if present */
1550 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e);
1551 if (ret == LY_ENOT) {
1552 ret = LY_SUCCESS;
1553 } else if (ret) {
1554 goto cleanup;
1555 }
1556
Michal Vaskocf770e22020-08-12 13:21:43 +02001557 /* read subtree */
1558 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1559 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001560
1561 /* finish linking metadata */
1562 ret = lydjson_metadata_finish(lydctx, &tree);
1563 LY_CHECK_GOTO(ret, cleanup);
1564
1565 /* make sure we have parsed some notification */
1566 if (!lydctx->op_node) {
1567 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1568 ret = LY_EVALID;
1569 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001570 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001571 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001572 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001573 ret = LY_EVALID;
1574 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001575 }
1576
1577 if (ntf_e) {
1578 /* finish notification envelope */
1579 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1580 LY_CHECK_GOTO(ret, cleanup);
1581 if (status == LYJSON_END) {
1582 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1583 ret = LY_EVALID;
1584 goto cleanup;
1585 } else if (status != LYJSON_OBJECT_CLOSED) {
1586 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
Michal Vasko69730152020-10-09 16:30:07 +02001587 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001588 ret = LY_EVALID;
1589 goto cleanup;
1590 }
1591 }
1592
1593 if (ntf_p) {
1594 *ntf_p = lydctx->op_node;
1595 }
1596 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001597 if (ntf_e) {
1598 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001599 lyd_insert_node(ntf_e, NULL, tree);
1600 tree = ntf_e;
1601 }
1602 if (tree_p) {
1603 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001604 }
1605
1606cleanup:
1607 /* we have used parse_only flag */
1608 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1609
1610 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1611 if (ret) {
1612 lyd_free_all(tree);
1613 lyd_free_tree(ntf_e);
1614 }
1615 return ret;
1616}
1617
1618/**
1619 * @brief Parse optional JSON envelope around the processed content.
1620 *
1621 * @param[in] jsonctx JSON parser context
1622 * @param[in] parent Parent node (some other envelope).
1623 * @param[in] module_key Name of the module where the envelope element is expected.
1624 * @param[in] object_id Name of the envelope object.
1625 * @param[out] envp_p Pointer to the created envelope opaq container.
1626 * @return LY_SUCCESS if the envelope present and successfully parsed.
1627 * @return LY_ENOT in case there is not the expected envelope.
1628 * @return LY_ERR in case of parsing failure.
1629 */
1630static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001631lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejci0f969882020-08-21 16:56:47 +02001632 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001633{
1634 LY_ERR ret = LY_ENOT, r;
1635 const char *name, *prefix;
1636 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001637 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001638 enum LYJSON_PARSER_STATUS status;
1639
1640 *envp_p = NULL;
1641
1642 /* "id" envelope */
1643 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1644 LY_CHECK_GOTO(is_attr, cleanup);
1645 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1646 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1647 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1648
1649 r = lyjson_ctx_next(jsonctx, &status);
1650 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1651 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1652
1653 /* create the object envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001654 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 +02001655 NULL, NULL, 0, module_key, ly_strlen(module_key), envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001656 LY_CHECK_GOTO(ret, cleanup);
1657
1658 if (parent) {
1659 lyd_insert_node(parent, NULL, *envp_p);
1660 }
1661
1662 ret = LY_SUCCESS;
1663cleanup:
1664 return ret;
1665}
1666
1667static LY_ERR
1668lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1669{
1670 enum LYJSON_PARSER_STATUS status;
1671
1672 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1673 if (status == LYJSON_END) {
1674 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1675 return LY_EVALID;
1676 } else if (status != LYJSON_OBJECT_CLOSED) {
1677 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001678 jsonctx->value_len, jsonctx->value, object_id);
Radek Krejci1798aae2020-07-14 13:26:06 +02001679 return LY_EVALID;
1680 }
1681 return LY_SUCCESS;
1682}
1683
1684LY_ERR
1685lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1686{
1687 LY_ERR ret = LY_SUCCESS;
1688 struct lyd_json_ctx *lydctx = NULL;
1689 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1690 struct lyd_node *tree = NULL;
1691 enum LYJSON_PARSER_STATUS status;
1692
1693 /* init */
1694 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1695 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1696 lydctx->int_opts = LYD_INTOPT_RPC;
1697
1698 /* process envelope(s), if present */
1699
1700 /* process rpc */
1701 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1702 if (ret == LY_ENOT) {
1703 ret = LY_SUCCESS;
1704 goto parse_content;
1705 } else if (ret) {
1706 goto cleanup;
1707 }
1708 /* process action */
1709 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1710 if (ret == LY_ENOT) {
1711 ret = LY_SUCCESS;
1712 goto parse_content;
1713 } else if (ret) {
1714 goto cleanup;
1715 }
1716
1717parse_content:
1718
1719 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1720 assert(status == LYJSON_OBJECT);
1721
1722 /* read subtree(s) */
Michal Vasko22df3f02020-08-24 13:29:22 +02001723 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 +02001724 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001725
1726 /* finish linking metadata */
1727 ret = lydjson_metadata_finish(lydctx, &tree);
1728 LY_CHECK_GOTO(ret, cleanup);
1729
1730 /* make sure we have parsed some operation */
1731 if (!lydctx->op_node) {
1732 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
Michal Vasko69730152020-10-09 16:30:07 +02001733 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
Radek Krejci1798aae2020-07-14 13:26:06 +02001734 ret = LY_EVALID;
1735 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001736 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001737 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001738 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001739 ret = LY_EVALID;
1740 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001741 }
1742
1743 if (act_e) {
1744 /* finish action envelope */
1745 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action");
1746 LY_CHECK_GOTO(ret, cleanup);
1747 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1748 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001749 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001750 ret = LY_EVALID;
1751 goto cleanup;
1752 }
1753 }
1754 if (rpc_e) {
1755 /* finish rpc envelope */
1756 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc");
1757 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko69730152020-10-09 16:30:07 +02001758 if (!act_e && (lydctx->op_node->schema->nodetype != LYS_RPC)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001759 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001760 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001761 ret = LY_EVALID;
1762 goto cleanup;
1763 }
1764 }
1765
1766 if (op_p) {
1767 *op_p = lydctx->op_node;
1768 }
1769 assert(tree);
1770 if (tree_p) {
1771 if (rpc_e) {
1772 *tree_p = rpc_e;
1773 } else if (act_e) {
1774 *tree_p = act_e;
1775 } else {
1776 *tree_p = tree;
1777 }
1778 }
1779
1780cleanup:
1781 /* we have used parse_only flag */
1782 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1783
1784 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1785 if (ret) {
1786 lyd_free_all(tree);
1787 lyd_free_tree(act_e);
1788 lyd_free_tree(rpc_e);
1789 }
1790 return ret;
1791}
1792
1793LY_ERR
1794lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1795{
1796 LY_ERR ret = LY_SUCCESS;
1797 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001798 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001799 enum LYJSON_PARSER_STATUS status;
1800
1801 /* init */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001802 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001803 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1804 lydctx->int_opts = LYD_INTOPT_REPLY;
1805
1806 /* find request OP */
1807 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1808 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1809 break;
1810 }
1811 LYD_TREE_DFS_END(request, req_op);
1812 }
1813 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001814 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001815 ret = LY_EINVAL;
1816 goto cleanup;
1817 }
1818
1819 /* duplicate request OP with parents */
1820 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1821
1822 /* parse "rpc-reply", if any */
1823 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e);
1824 if (ret == LY_ENOT) {
1825 ret = LY_SUCCESS;
1826 } else if (ret) {
1827 goto cleanup;
1828 }
1829
1830 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1831 assert(status == LYJSON_OBJECT);
1832
1833 /* read subtree(s) */
1834 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001835 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op));
Radek Krejci1798aae2020-07-14 13:26:06 +02001836 LY_CHECK_GOTO(ret, cleanup);
1837
1838 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1839 }
1840
1841 /* finish linking metadata */
1842 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1843 LY_CHECK_GOTO(ret, cleanup);
1844
1845 if (rpcr_e) {
1846 /* finish rpc-reply envelope */
1847 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply");
1848 LY_CHECK_GOTO(ret, cleanup);
1849 }
1850
1851 if (op_p) {
1852 *op_p = rep_op;
1853 }
Radek Krejcia1c1e542020-09-29 16:06:52 +02001854 for (tree = rep_op; tree->parent; tree = lyd_parent(tree)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001855 if (rpcr_e) {
1856 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001857 lyd_insert_node(rpcr_e, NULL, tree);
1858 tree = rpcr_e;
1859 }
1860 if (tree_p) {
1861 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001862 }
1863
1864cleanup:
1865 /* we have used parse_only flag */
1866 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1867
1868 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1869 if (ret) {
1870 lyd_free_all(rep_op);
1871 lyd_free_tree(rpcr_e);
1872 }
1873 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001874}