blob: 4c3e2aaf97846cd1a582a66bb4f31ef8be28ac8a [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;
191
192 /* leave if-feature check for validation */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200193 uint32_t getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200194
195 /* init return value */
196 *snode_p = NULL;
197
198 /* get the element module */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200199 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200200 if (!prefix_len) {
201 /* opaq parent (envelope) - the second part of the condition */
Michal Vasko22df3f02020-08-24 13:29:22 +0200202 lydjson_get_node_prefix((struct lyd_node *)parent, NULL, 0, &prefix, &prefix_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200203 }
204 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
205 } else if (parent) {
206 if (parent->schema) {
207 mod = parent->schema->module;
208 }
209 } else {
210 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 +0200211 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200212 return LY_EVALID;
213 }
214 if (!mod) {
215 if (lydctx->parse_options & LYD_PARSE_STRICT) {
216 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
217 return LY_EVALID;
218 }
219 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
220 return LY_ENOT;
221 }
222 }
223
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200224 if (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200225 /* ignore the envelope parent when searchinf for the schema node */
226 parent = NULL;
227 }
228
229 /* get the schema node */
230 if (mod && (!parent || parent->schema)) {
231 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
232 if (!*snode_p) {
233 if (lydctx->parse_options & LYD_PARSE_STRICT) {
234 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 +0200235 name_len, name, mod->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200236 return LY_EVALID;
237 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
238 /* skip element with children */
239 return LY_ENOT;
240 }
241 } else {
242 /* check that schema node is valid and can be used */
243 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
244 }
245 }
246
247 return LY_SUCCESS;
248}
249
250/**
251 * @brief Skip the currently open JSON object/array
252 * @param[in] jsonctx JSON context with the input data to skip.
253 * @return LY_ERR value.
254 */
255static LY_ERR
256lydjson_data_skip(struct lyjson_ctx *jsonctx)
257{
258 enum LYJSON_PARSER_STATUS status, current;
259 size_t sublevels = 1;
260
261 status = lyjson_ctx_status(jsonctx, 0);
262
263 /* skip after the content */
264 do {
265 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
266 if (current == status) {
267 sublevels++;
268 } else if (current == status + 1) {
269 sublevels--;
270 }
271 } while (current != status + 1 && sublevels);
272 /* open the next sibling */
273 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
274
275 return LY_SUCCESS;
276}
277
278/**
279 * @brief Go through the @p value and find all possible prefixes and store them in @p val_prefs_p [sized array](@ref sizedarrays).
280 *
281 * @param[in] ctx libyang context
282 * @param[in] value Pointer to the beginning of the value to check.
283 * @param[in] value_len Length of the string to examine in @p value.
284 * @param[out] val_prefs_p Pointer to the resulting [sized array](@ref sizedarrays) of found prefixes. NULL in case there are no prefixes.
285 * @return LY_EMEM on memory allocation failure.
286 * @return LY_SUCCESS on success, empty @p val_prefs_p (NULL) is valid result if there are no possible prefixes
287 */
288static LY_ERR
289lydjson_get_value_prefixes(const struct ly_ctx *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs_p)
290{
291 LY_ERR ret;
292 LY_ARRAY_COUNT_TYPE u;
293 uint32_t c;
294 const char *start, *stop;
295 struct ly_prefix *prefixes = NULL;
296 size_t len;
297
298 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
299 size_t bytes;
300 ly_getutf8(&stop, &c, &bytes);
301 if (is_yangidentstartchar(c)) {
302 for (ly_getutf8(&stop, &c, &bytes);
303 is_yangidentchar(c) && (size_t)(stop - value) < value_len;
Radek Krejci1e008d22020-08-17 11:37:37 +0200304 ly_getutf8(&stop, &c, &bytes)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200305 stop = stop - bytes;
306 if (*stop == ':') {
307 /* we have a possible prefix */
308 struct ly_prefix *p = NULL;
309
310 len = stop - start;
311
312 /* check whether we do not already have this prefix stored */
313 LY_ARRAY_FOR(prefixes, u) {
314 if (!ly_strncmp(prefixes[u].id, start, len)) {
315 p = &prefixes[u];
316 break;
317 }
318 }
319 if (!p) {
320 LY_ARRAY_NEW_GOTO(ctx, prefixes, p, ret, error);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200321 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->id), error);
322 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->module_name), error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200323 } /* else the prefix already present */
324 }
325 stop = stop + bytes;
326 }
327 }
328
329 *val_prefs_p = prefixes;
330 return LY_SUCCESS;
331
332error:
333 LY_ARRAY_FOR(prefixes, u) {
334 lydict_remove(ctx, prefixes[u].id);
335 lydict_remove(ctx, prefixes[u].module_name);
336 }
337 LY_ARRAY_FREE(prefixes);
338 return ret;
339}
340
341/**
342 * @brief Check that the input data are parseable as the @p list.
343 *
344 * Checks for all the list's keys. Function does not revert the context state.
345 *
346 * @param[in] jsonctx JSON parser context.
347 * @param[in] list List schema node corresponding to the input data object.
348 * @return LY_SUCCESS in case the data are ok for the @p list
349 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
350 */
351static LY_ERR
352lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
353{
354 LY_ERR ret = LY_SUCCESS;
355 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
356 struct ly_set key_set = {0};
357 const struct lysc_node *snode;
358 uint32_t i, status_count;
359
360 assert(list && (list->nodetype == LYS_LIST));
361 assert(status == LYJSON_OBJECT);
362
363 /* get all keys into a set (keys do not have if-features or anything) */
364 snode = NULL;
365 while ((snode = lys_getnext(snode, list, NULL, LYS_GETNEXT_NOSTATECHECK)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200366 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200367 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200368 }
369
370 if (status != LYJSON_OBJECT_EMPTY) {
371 status_count = jsonctx->status.count;
372
373 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
374 const char *name, *prefix;
375 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200376 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200377
378 /* match the key */
379 snode = NULL;
380 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
381
382 if (!is_attr && !prefix) {
383 for (i = 0; i < key_set.count; ++i) {
384 snode = (const struct lysc_node *)key_set.objs[i];
385 if (!ly_strncmp(snode->name, name, name_len)) {
386 break;
387 }
388 }
389 /* go into the item to a) process it as a key or b) start skipping it as another list child */
390 ret = lyjson_ctx_next(jsonctx, &status);
391 LY_CHECK_GOTO(ret, cleanup);
392
393 if (snode) {
394 /* we have the key, validate the value */
395 if (status < LYJSON_NUMBER) {
396 /* not a terminal */
397 ret = LY_ENOT;
398 goto cleanup;
399 }
400
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200401 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200402 LY_CHECK_GOTO(ret, cleanup);
403
404 /* key with a valid value, remove from the set */
405 ly_set_rm_index(&key_set, i, NULL);
406 }
407 } else {
408 /* start skipping the member we are not interested in */
409 ret = lyjson_ctx_next(jsonctx, &status);
410 LY_CHECK_GOTO(ret, cleanup);
411 }
412 /* move to the next child */
413 while (status_count < jsonctx->status.count) {
414 ret = lyjson_ctx_next(jsonctx, &status);
415 LY_CHECK_GOTO(ret, cleanup);
416 }
417 }
418 }
419
420 if (key_set.count) {
421 /* some keys are missing/did not validate */
422 ret = LY_ENOT;
423 }
424
425cleanup:
426 ly_set_erase(&key_set, NULL);
427 return ret;
428}
429
430/**
431 * @brief Get the hint for the data type parsers according to the current JSON parser context.
432 *
433 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
434 * @param[in,out] status Pointer to the current context status,
435 * in some circumstances the function manipulates with the context so the status is updated.
436 * @param[out] type_hint_p Pointer to the variable to store the result.
437 * @return LY_SUCCESS in case of success.
438 * @return LY_EINVAL in case of invalid context status not referring to a value.
439 */
440static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200441lydjson_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 +0200442{
443 *type_hint_p = 0;
444
445 if (*status_p == LYJSON_ARRAY) {
446 /* only [null] */
447 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
448 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
449
450 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
451 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
452
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200453 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200454 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200455 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200456 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200457 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200458 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200459 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200460 } else if (*status_p == LYJSON_NULL) {
461 *type_hint_p = 0;
462 } else {
463 return LY_EINVAL;
464 }
465
466 return LY_SUCCESS;
467}
468
469/**
470 * @brief Check in advance if the input data are parsable according to the provided @p snode.
471 *
472 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
473 * is naturally done when the data are really parsed.
474 *
475 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
476 * as before calling, despite it is necessary to process input data for checking.
477 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
478 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
479 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
480 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
481 * @return LY_EINVAL in case of invalid leaf JSON encoding
482 * and they are expected to be parsed as opaq nodes.
483 */
484static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200485lydjson_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 +0200486{
487 LY_ERR ret = LY_SUCCESS;
488 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
489 enum LYJSON_PARSER_STATUS status;
490
491 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200492
Michal Vasko32ac9942020-08-12 14:35:12 +0200493 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
494 /* can always be parsed as a data node if we have the schema node */
495 return LY_SUCCESS;
496 }
497
498 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200499 /* backup parser */
500 lyjson_ctx_backup(jsonctx);
501 status = lyjson_ctx_status(jsonctx, 0);
502
Michal Vasko32ac9942020-08-12 14:35:12 +0200503 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
504 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200505 switch (snode->nodetype) {
506 case LYS_LEAFLIST:
507 case LYS_LEAF:
508 /* value may not be valid in which case we parse it as an opaque node */
509 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
510 if (ret) {
511 break;
512 }
513
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200514 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 ret = LY_ENOT;
516 }
517 break;
518 case LYS_LIST:
519 /* lists may not have all its keys */
520 if (lydjson_check_list(jsonctx, snode)) {
521 /* invalid list, parse as opaque if it missing/has invalid some keys */
522 ret = LY_ENOT;
523 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200524 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 }
526
527 /* restore parser */
528 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200529 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200530 status = lyjson_ctx_status(jsonctx, 0);
531 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
532 }
533
534 return ret;
535}
536
537/**
538 * @brief Join the forward-referencing metadata with their target data nodes.
539 *
540 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
541 *
542 * @param[in] lydctx JSON data parser context.
543 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
544 * as a starting point to search for the metadata's target data node
545 * @return LY_SUCCESS on success
546 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
547 */
548static LY_ERR
549lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
550{
551 LY_ERR ret = LY_SUCCESS;
552 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200553 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200554 const char *prev = NULL;
555
556 /* finish linking metadata */
557 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200558 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200559 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200560 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200561 const char *name, *prefix;
562 size_t name_len, prefix_len;
563 const struct lysc_node *snode;
564
Michal Vasko69730152020-10-09 16:30:07 +0200565 if (attr->schema || (meta_container->name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200566 /* not an opaq metadata node */
567 continue;
568 }
569
570 if (prev != meta_container->name) {
571 /* metas' names are stored in dictionary, so checking pointers must works */
572 lydict_remove(lydctx->jsonctx->ctx, prev);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200573 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200574 instance = 1;
575 } else {
576 instance++;
577 }
578
579 /* find the correspnding data node */
580 LY_LIST_FOR(start, node) {
581 if (!node->schema) {
582 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vasko22df3f02020-08-24 13:29:22 +0200583 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq *)node)->name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200584 continue;
585 }
586
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200587 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200588 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vasko69730152020-10-09 16:30:07 +0200589 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200590 ret = LY_EVALID;
591 goto cleanup;
592 }
593
594 /* match */
595 match++;
596 if (match != instance) {
597 continue;
598 }
599
600 LY_LIST_FOR(meta_container->child, meta_iter) {
601 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200602 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200603 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200604 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200605
606 /* get value prefixes */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200607 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value,
608 lydctx->jsonctx->value_len, &val_prefs), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200609
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200610 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value,
611 ly_strlen(meta->value), &dynamic, LYD_JSON, meta->hints, val_prefs, meta->prefix.id,
612 ly_strlen(meta->prefix.id), meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
Radek Krejci1798aae2020-07-14 13:26:06 +0200613 LY_CHECK_GOTO(ret, cleanup);
614 }
615
616 /* done */
617 break;
618 } else {
619 /* this is the second time we are resolving the schema node, so it must succeed,
620 * but remember that snode can be still NULL */
621 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
622 assert(is_attr);
623 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
624 assert(ret == LY_SUCCESS);
625
626 if (snode != node->schema) {
627 continue;
628 }
629
630 /* match */
631 match++;
632 if (match != instance) {
633 continue;
634 }
635
636 LY_LIST_FOR(meta_container->child, meta_iter) {
637 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200638 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200639 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200640 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200641
642 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
643 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200644 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vasko69730152020-10-09 16:30:07 +0200645 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
646 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200647 LY_CHECK_GOTO(ret, cleanup);
648 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
649 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200650 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
651 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200652 ret = LY_EVALID;
653 goto cleanup;
654 }
655 }
656 /* add/correct flags */
657 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
658
659 /* done */
660 break;
661 }
662 }
663
664 if (match != instance) {
665 /* there is no corresponding data node for the metadata */
666 if (instance > 1) {
667 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200668 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
669 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200670 } else {
671 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200672 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200673 }
674 ret = LY_EVALID;
675 } else {
676 /* remove the opaq attr */
677 if (attr == (*first_p)) {
678 *first_p = attr->next;
679 }
680 lyd_free_tree(attr);
681 }
682 }
683
684cleanup:
685 lydict_remove(lydctx->jsonctx->ctx, prev);
686
687 return ret;
688}
689
690/**
691 * @brief Parse a metadata member.
692 *
693 * @param[in] lydctx JSON data parser context.
694 * @param[in] snode Schema node of the metadata parent.
695 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
696 * so the data node does not exists. In such a case the metadata is stored in the context for the later
697 * processing by lydjson_metadata_finish().
698 * @return LY_SUCCESS on success
699 * @return Various LY_ERR values in case of failure.
700 */
701static LY_ERR
702lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
703{
704 LY_ERR ret = LY_SUCCESS;
705 enum LYJSON_PARSER_STATUS status;
706 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200707 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200708 const char *name, *prefix = NULL;
709 size_t name_len, prefix_len = 0;
710 struct lys_module *mod;
711 struct lyd_meta *meta = NULL;
712 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200713 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200715 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200716 uint16_t nodetype;
717
718 assert(snode || node);
719
720 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
721
722 /* move to the second item in the name/X pair */
723 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
724 LY_CHECK_GOTO(ret, cleanup);
725
726 /* check attribute encoding */
727 switch (nodetype) {
728 case LYS_LEAFLIST:
729 expected = "@name/array of objects/nulls";
730
731 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
732
733next_entry:
734 instance++;
735
736 /* move into array / next entry */
737 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
738 LY_CHECK_GOTO(ret, cleanup);
739
740 if (status == LYJSON_ARRAY_CLOSED) {
741 /* we are done, move after the array */
742 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
743 goto cleanup;
744 }
745 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
746
Michal Vasko69730152020-10-09 16:30:07 +0200747 if (!node || (node->schema != prev->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200749 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
750 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200751 ret = LY_EVALID;
752 goto cleanup;
753 }
754
755 if (status == LYJSON_NULL) {
756 /* continue with the next entry in the leaf-list array */
757 prev = node;
758 node = node->next;
759 goto next_entry;
760 }
761 break;
762 case LYS_LEAF:
763 case LYS_ANYXML:
764 expected = "@name/object";
765
766 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
767 break;
768 case LYS_CONTAINER:
769 case LYS_LIST:
770 case LYS_ANYDATA:
771 case LYS_NOTIF:
772 case LYS_ACTION:
773 case LYS_RPC:
774 in_parent = 1;
775 expected = "@/object";
776 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
777 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200778 default:
779 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200780 }
781
782 /* process all the members inside a single metadata object */
783 assert(status == LYJSON_OBJECT);
784
785 while (status != LYJSON_OBJECT_CLOSED) {
786 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
787 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200788 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200789 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
790 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200791 ret = LY_EVALID;
792 goto cleanup;
793 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200794 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200795 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
796 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200797 ret = LY_EVALID;
798 goto cleanup;
799 }
800
801 /* get the element module */
802 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
803 if (!mod) {
804 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200805 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200806 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
807 prefix_len, prefix, name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200808 ret = LY_EVALID;
809 goto cleanup;
810 }
811 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
812 /* skip element with children */
813 ret = lydjson_data_skip(lydctx->jsonctx);
814 LY_CHECK_GOTO(ret, cleanup);
815 status = lyjson_ctx_status(lydctx->jsonctx, 0);
816 /* end of the item */
817 continue;
818 }
819 }
820
821 /* get the value */
822 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
823 LY_CHECK_GOTO(ret, cleanup);
824
825 if (node->schema) {
826 /* create metadata */
827 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200828 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 +0200829 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
830 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200831 LY_CHECK_GOTO(ret, cleanup);
832
833 /* add/correct flags */
834 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
835 } else {
836 /* create attribute */
837 struct ly_prefix *val_prefs = NULL;
838 const char *module_name;
839 size_t module_name_len;
840
841 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
842
843 /* get value prefixes */
844 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
845
846 /* attr2 is always changed to the created attribute */
847 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 +0200848 &lydctx->jsonctx->dynamic, LYD_JSON, 0, val_prefs, prefix, prefix_len, module_name, module_name_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200849 LY_CHECK_GOTO(ret, cleanup);
850 }
851 /* next member */
852 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
853 LY_CHECK_GOTO(ret, cleanup);
854 }
855
856 if (nodetype == LYS_LEAFLIST) {
857 /* continue by processing another metadata object for the following
858 * leaf-list instance since they are allways instantiated in JSON array */
859 prev = node;
860 node = node->next;
861 goto next_entry;
862 }
863
864 /* move after the metadata */
865 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
866 LY_CHECK_GOTO(ret, cleanup);
867
868cleanup:
869 return ret;
870
871representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200872 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200873 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
874 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name,
875 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200876
877 return LY_EVALID;
878}
879
880/**
881 * @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.
882 *
883 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
884 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
885 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
886 */
887static void
888lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
889{
890 if (*node_p) {
891 /* insert, keep first pointer correct */
892 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
893 if (first_p) {
894 if (parent) {
895 *first_p = parent->child;
896 } else {
897 while ((*first_p)->prev->next) {
898 *first_p = (*first_p)->prev;
899 }
900 }
901 }
902 *node_p = NULL;
903 }
904}
905
906static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
907
908/**
909 * @brief Parse opaq node from the input.
910 *
911 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
912 *
913 * @param[in] lydctx JSON data parser context.
914 * @param[in] name Name of the opaq node to create.
915 * @param[in] name_len Length of the @p name string.
916 * @param[in] prefix Prefix of the opaq node to create.
917 * @param[in] prefix_len Length of the @p prefx string.
918 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
919 * but must be set if @p first is not.
920 * @param[in,out] status_p Pointer to the current status of the parser context,
921 * since the function manipulates with the context and process the input, the status can be updated.
922 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
923 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
924 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
925 * @param[out] node_p Pointer to the created opaq node.
926 * @return LY_ERR value.
927 */
928static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200929lydjson_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 +0200930 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
931 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200932{
933 LY_ERR ret = LY_SUCCESS;
934 const char *value = NULL, *module_name;
935 size_t value_len = 0, module_name_len = 0;
936 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200937 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200938 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200939
Michal Vasko69730152020-10-09 16:30:07 +0200940 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200941 /* prepare for creating opaq node with a value */
942 value = lydctx->jsonctx->value;
943 value_len = lydctx->jsonctx->value_len;
944 dynamic = lydctx->jsonctx->dynamic;
945 lydctx->jsonctx->dynamic = 0;
946
947 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
948
949 if (value) {
950 /* get value prefixes */
951 LY_CHECK_RET(lydjson_get_value_prefixes(lydctx->jsonctx->ctx, value, value_len, &val_prefs));
952 }
953 }
954
955 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200956 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200957 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, LYD_JSON, type_hint,
958 val_prefs, prefix, prefix_len, module_name, module_name_len, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200959 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200960 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200961 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200962 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200963
Michal Vasko69730152020-10-09 16:30:07 +0200964 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200965 /* process children */
966 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
967 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
968 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
969 }
Michal Vasko69730152020-10-09 16:30:07 +0200970 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200971 /* process another instance of the same node */
972 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200973 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200974
Michal Vasko69730152020-10-09 16:30:07 +0200975 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200976 /* but first process children of the object in the array */
977 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
978 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
979 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
980 }
981 }
982
983 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
984
985 /* continue with the next instance */
986 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
987 assert(node_p);
988 lydjson_maintain_children(parent, first_p, node_p);
989 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
990 }
991 }
992
993 /* finish linking metadata */
994 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
995
996 /* move after the item */
997 return lyjson_ctx_next(lydctx->jsonctx, status_p);
998}
999
1000/**
1001 * @brief Process the attribute container (starting by @)
1002 *
1003 * @param[in] lydctx JSON data parser context.
1004 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1005 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1006 * @param[in] name Name of the opaq node to create.
1007 * @param[in] name_len Length of the @p name string.
1008 * @param[in] prefix Prefix of the opaq node to create.
1009 * @param[in] prefix_len Length of the @p prefx string.
1010 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1011 * but must be set if @p first is not.
1012 * @param[in,out] status_p Pointer to the current status of the parser context,
1013 * since the function manipulates with the context and process the input, the status can be updated.
1014 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1015 * @param[out] node_p Pointer to the created opaq node.
1016 * @return LY_ERR value.
1017 */
1018static LY_ERR
1019lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +02001020 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1021 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1022 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001023{
1024 LY_ERR ret = LY_SUCCESS;
1025 enum LYJSON_PARSER_STATUS status_inner;
1026
1027 /* parse as an attribute to a node */
1028 if (!attr_node && snode) {
1029 /* try to find the instance */
1030 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1031 if (iter->schema == snode) {
1032 attr_node = iter;
1033 break;
1034 }
1035 }
1036 }
1037 if (!attr_node) {
1038 /* parse just as an opaq node with the name beginning with @,
1039 * later we have to check that it belongs to a standard node
1040 * and it is supposed to be converted to a metadata */
1041 uint32_t prev_opts;
1042
1043 /* move into metadata */
1044 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1045
1046 if (*status_p == LYJSON_ARRAY) {
1047 /* move into the array */
1048 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1049 } else {
1050 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1051 status_inner = LYJSON_ERROR;
1052 }
1053
1054 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
1055 prev_opts = lydctx->parse_options;
1056 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1057 lydctx->parse_options |= LYD_PARSE_OPAQ;
1058
1059 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 +02001060 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001061
1062 /* restore the parser options */
1063 lydctx->parse_options = prev_opts;
1064 } else {
1065 ret = lydjson_metadata(lydctx, snode, attr_node);
1066 }
1067
1068 return ret;
1069}
1070
1071/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001072 * @brief Parse a single instance of a node.
1073 *
1074 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1075 * as before calling, despite it is necessary to process input data for checking.
1076 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1077 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1078 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1079 * @param[in] name Parsed JSON node name.
1080 * @param[in] name_len Lenght of @p name.
1081 * @param[in] prefix Parsed JSON node prefix.
1082 * @param[in] prefix_len Length of @p prefix.
1083 * @param[in,out] status JSON parser status, is updated.
1084 * @param[out] node Parsed data (or opaque) node.
1085 * @return LY_SUCCESS if a node was successfully parsed,
1086 * @return LY_EINVAL in case of invalid JSON encoding,
1087 * @return LY_ERR on other errors.
1088 */
1089static LY_ERR
1090lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001091 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1092 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001093{
1094 LY_ERR ret;
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001095 uint32_t type_hints;
Michal Vasko32ac9942020-08-12 14:35:12 +02001096 uint32_t prev_opts;
1097 struct lyd_node *tree = NULL;
1098
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001099 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001100 if (ret == LY_SUCCESS) {
1101 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1102 if (snode->nodetype & LYD_NODE_TERM) {
1103 /* create terminal node */
1104 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001105 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1106 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001107 LY_CHECK_RET(ret);
1108
1109 /* move JSON parser */
1110 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1111 LY_CHECK_RET(ret);
1112 } else if (snode->nodetype & LYD_NODE_INNER) {
1113 /* create inner node */
1114 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1115
1116 ret = lyd_create_inner(snode, node);
1117 LY_CHECK_RET(ret);
1118
1119 /* process children */
1120 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1121 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1122 LY_CHECK_RET(ret);
1123 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1124 }
1125
1126 /* finish linking metadata */
1127 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1128 LY_CHECK_RET(ret);
1129
1130 if (snode->nodetype == LYS_LIST) {
1131 /* check all keys exist */
1132 ret = lyd_parse_check_keys(*node);
1133 LY_CHECK_RET(ret);
1134 }
1135
1136 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1137 /* new node validation, autodelete CANNOT occur, all nodes are new */
1138 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1139 LY_CHECK_RET(ret);
1140
1141 /* add any missing default children */
1142 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->unres_node_type,
Michal Vasko69730152020-10-09 16:30:07 +02001143 &lydctx->when_check, (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ?
1144 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001145 LY_CHECK_RET(ret);
1146 }
1147
1148 /* move JSON parser */
1149 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1150 LY_CHECK_RET(ret);
1151 } else if (snode->nodetype & LYD_NODE_ANY) {
1152 /* create any node */
1153 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1154
1155 /* parse any data tree with correct options */
1156 /* first backup the current options and then make the parser to process data as opaq nodes */
1157 prev_opts = lydctx->parse_options;
1158 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1159 lydctx->parse_options |= LYD_PARSE_OPAQ;
1160
1161 /* process the anydata content */
1162 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1163 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1164 LY_CHECK_RET(ret);
1165 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1166 }
1167
1168 /* restore parser options */
1169 lydctx->parse_options = prev_opts;
1170
1171 /* finish linking metadata */
1172 ret = lydjson_metadata_finish(lydctx, &tree);
1173 LY_CHECK_RET(ret);
1174
1175 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1176 LY_CHECK_RET(ret);
1177 }
1178 } else if (ret == LY_ENOT) {
1179 /* parse it again as an opaq node */
1180 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001181 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001182 LY_CHECK_RET(ret);
1183
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001184 if (snode->nodetype == LYS_LIST) {
1185 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1186 } else if (snode->nodetype == LYS_LEAFLIST) {
1187 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001188 }
1189 }
1190
1191 return ret;
1192}
1193
1194/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001195 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001196 *
1197 * @param[in] lydctx JSON data parser context.
1198 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1199 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1200 * @return LY_ERR value.
1201 */
1202static LY_ERR
1203lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1204{
1205 LY_ERR ret = LY_SUCCESS;
1206 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1207 enum LYJSON_PARSER_STATUS status_inner = 0;
1208 const char *name, *prefix = NULL;
1209 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001210 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001211 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001212 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001213 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1214 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001215
1216 assert(parent || first_p);
1217 assert(status == LYJSON_OBJECT);
1218
1219 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001220 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 +02001221
Michal Vaskocabe0702020-08-12 10:14:36 +02001222 if (!is_meta || name_len || prefix_len) {
1223 /* get the schema node */
1224 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1225 if (ret == LY_ENOT) {
1226 /* skip element with children */
1227 ret = lydjson_data_skip(lydctx->jsonctx);
1228 LY_CHECK_GOTO(ret, cleanup);
1229 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1230 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001231 goto cleanup;
1232 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001233 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001234
Michal Vaskocabe0702020-08-12 10:14:36 +02001235 if (!snode) {
1236 /* we will not be parsing it as metadata */
1237 is_meta = 0;
1238 }
1239 }
1240
1241 if (is_meta) {
1242 /* parse as metadata */
1243 if (!name_len && !prefix_len) {
1244 /* parent's metadata without a name - use the schema from the parent */
1245 if (!parent) {
1246 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001247 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001248 ret = LY_EVALID;
1249 goto cleanup;
1250 }
1251 attr_node = (struct lyd_node *)parent;
1252 snode = attr_node->schema;
1253 }
1254 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001255 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001256 LY_CHECK_GOTO(ret, cleanup);
1257 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001258 /* parse as an opaq node */
1259 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1260
1261 /* move to the second item in the name/X pair */
1262 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1263 LY_CHECK_GOTO(ret, cleanup);
1264
1265 if (status == LYJSON_ARRAY) {
1266 /* move into the array */
1267 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1268 LY_CHECK_GOTO(ret, cleanup);
1269 } else {
1270 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1271 status_inner = LYJSON_ERROR;
1272 }
1273
1274 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001275 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001276 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001277 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001278 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001279
1280 /* move to the second item in the name/X pair */
1281 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1282 LY_CHECK_GOTO(ret, cleanup);
1283
1284 /* first check the expected representation according to the nodetype and then continue with the content */
1285 switch (snode->nodetype) {
1286 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001287 case LYS_LIST:
1288 if (snode->nodetype == LYS_LEAFLIST) {
1289 expected = "name/array of values";
1290 } else {
1291 expected = "name/array of objects";
1292 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001293
1294 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1295
1296 /* move into array */
1297 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1298 LY_CHECK_GOTO(ret, cleanup);
1299
Michal Vasko32ac9942020-08-12 14:35:12 +02001300 /* process all the values/objects */
1301 do {
1302 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001303
Michal Vasko32ac9942020-08-12 14:35:12 +02001304 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001305 &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001306 if (ret == LY_EINVAL) {
1307 goto representation_error;
1308 } else if (ret) {
1309 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001310 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001311 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001312
Michal Vasko32ac9942020-08-12 14:35:12 +02001313 /* move after the array */
1314 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1315 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001316
1317 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001318 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001319 case LYS_CONTAINER:
1320 case LYS_NOTIF:
1321 case LYS_ACTION:
1322 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001323 case LYS_ANYDATA:
1324 case LYS_ANYXML:
1325 if (snode->nodetype == LYS_LEAF) {
1326 if (status == LYJSON_ARRAY) {
1327 expected = "name/[null]";
1328 } else {
1329 expected = "name/value";
1330 }
1331 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001332 expected = "name/object";
1333 }
1334
Michal Vasko32ac9942020-08-12 14:35:12 +02001335 /* process the value/object */
1336 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1337 if (ret == LY_EINVAL) {
1338 goto representation_error;
1339 } else if (ret) {
1340 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001341 }
1342
Michal Vasko32ac9942020-08-12 14:35:12 +02001343 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001344 /* rememeber the RPC/action/notification */
1345 lydctx->op_node = node;
1346 }
1347
1348 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001349 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001350 }
1351
Michal Vasko32ac9942020-08-12 14:35:12 +02001352 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001353 lydjson_maintain_children(parent, first_p, &node);
1354
1355cleanup:
1356 lyd_free_tree(node);
1357 return ret;
1358
1359representation_error:
1360 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001361 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1362 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejci1798aae2020-07-14 13:26:06 +02001363
1364 ret = LY_EVALID;
1365 goto cleanup;
1366}
1367
1368/**
1369 * @brief Common start of JSON parser processing different types of the input data.
1370 *
1371 * @param[in] ctx libyang context
1372 * @param[in] in Input structure.
1373 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1374 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1375 * @param[out] lydctx_p Data parser context to finish validation.
1376 * @return LY_ERR value.
1377 */
1378static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001379lyd_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 +02001380 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001381{
1382 LY_ERR ret = LY_SUCCESS;
1383 struct lyd_json_ctx *lydctx;
1384 size_t i, line = 1;
1385
Radek Krejci1798aae2020-07-14 13:26:06 +02001386 /* init context */
1387 lydctx = calloc(1, sizeof *lydctx);
1388 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1389 lydctx->parse_options = parse_options;
1390 lydctx->validate_options = validate_options;
1391 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001392
Radek Krejci284f31f2020-09-18 15:40:29 +02001393 /* starting top-level */
1394 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1395 if (in->current[i] == 0x0a) { /* new line */
1396 line++;
Michal Vasko61d76362020-10-07 10:47:30 +02001397 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001398 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001399
Radek Krejci284f31f2020-09-18 15:40:29 +02001400 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1401 if (lyjson_ctx_status(lydctx->jsonctx, 0) == LYJSON_END) {
1402 /* empty data input */
1403 *lydctx_p = lydctx;
1404 return LY_SUCCESS;
1405 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001406 *lydctx_p = lydctx;
1407 return LY_SUCCESS;
1408}
1409
1410LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001411lyd_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 +02001412 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001413{
1414 LY_ERR ret = LY_SUCCESS;
1415 struct lyd_json_ctx *lydctx = NULL;
1416 enum LYJSON_PARSER_STATUS status;
1417
1418 assert(tree_p);
1419 *tree_p = NULL;
1420
1421 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx);
Radek Krejci284f31f2020-09-18 15:40:29 +02001422 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001423
1424 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci284f31f2020-09-18 15:40:29 +02001425 LY_CHECK_GOTO(status == LYJSON_END, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001426 assert(status == LYJSON_OBJECT);
1427
1428 /* read subtree(s) */
1429 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1430 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1431 LY_CHECK_GOTO(ret, cleanup);
1432
1433 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1434 }
1435
1436 /* finish linking metadata */
1437 ret = lydjson_metadata_finish(lydctx, tree_p);
1438 LY_CHECK_GOTO(ret, cleanup);
1439
1440cleanup:
1441 /* there should be no unresolved types stored */
Michal Vasko69730152020-10-09 16:30:07 +02001442 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count &&
1443 !lydctx->when_check.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001444
1445 if (ret) {
1446 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1447 lyd_free_all(*tree_p);
1448 *tree_p = NULL;
1449 } else {
1450 *lydctx_p = (struct lyd_ctx *)lydctx;
1451 }
1452
1453 return ret;
1454}
1455
1456/**
1457 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1458 *
1459 * @param[in] jsonctx JSON parser context
1460 * @param[out] envp_p Pointer to the created envelope opaq container.
1461 * @return LY_SUCCESS if the envelope present and successfully parsed.
1462 * @return LY_ENOT in case there is not the expected envelope.
1463 * @return LY_ERR in case of parsing failure.
1464 */
1465static LY_ERR
1466lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p)
1467{
1468 LY_ERR ret = LY_ENOT, r;
1469 const char *name, *prefix, *value = NULL;
1470 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001471 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001472 enum LYJSON_PARSER_STATUS status;
1473 struct lyd_node *et;
1474
1475 *envp_p = NULL;
1476
1477 /* backup the context */
1478 lyjson_ctx_backup(jsonctx);
1479
1480 /* "notification" envelope */
1481 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1482 LY_CHECK_GOTO(is_attr, cleanup);
1483 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1484 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1485
1486 r = lyjson_ctx_next(jsonctx, &status);
1487 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1488 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1489
1490 /* "eventTime" child */
1491 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1492 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1493 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1494
1495 /* go for the data */
1496 r = lyjson_ctx_next(jsonctx, &status);
1497 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1498 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1499
1500 value = jsonctx->value;
1501 value_len = jsonctx->value_len;
1502 dynamic = jsonctx->dynamic;
1503 jsonctx->dynamic = 0;
1504
1505 r = lyjson_ctx_next(jsonctx, &status);
1506 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1507 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1508 /* now the notificationContent is expected, which will be parsed by the caller */
1509
1510 /* create notification envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001511 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LYD_JSON, LYD_NODEHINT_ENVELOPE, NULL, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001512 0, "ietf-restconf", 13, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001513 LY_CHECK_GOTO(ret, cleanup);
1514 /* create notification envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001515 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 +02001516 NULL, 0, "ietf-restconf", 13, &et);
Radek Krejci1798aae2020-07-14 13:26:06 +02001517 LY_CHECK_GOTO(ret, cleanup);
1518 /* insert eventTime into notification */
1519 lyd_insert_node(*envp_p, NULL, et);
1520
1521 ret = LY_SUCCESS;
1522cleanup:
1523 if (ret) {
1524 /* restore the context */
1525 lyjson_ctx_restore(jsonctx);
1526 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001527 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001528 }
1529 }
1530 return ret;
1531}
1532
1533LY_ERR
1534lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1535{
1536 LY_ERR ret = LY_SUCCESS;
1537 struct lyd_json_ctx *lydctx = NULL;
1538 struct lyd_node *ntf_e = NULL;
1539 struct lyd_node *tree = NULL;
1540 enum LYJSON_PARSER_STATUS status;
1541
1542 /* init */
1543 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1544 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1545 lydctx->int_opts = LYD_INTOPT_NOTIF;
1546
1547 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1548 assert(status == LYJSON_OBJECT);
1549
1550 /* parse "notification" and "eventTime", if present */
1551 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e);
1552 if (ret == LY_ENOT) {
1553 ret = LY_SUCCESS;
1554 } else if (ret) {
1555 goto cleanup;
1556 }
1557
Michal Vaskocf770e22020-08-12 13:21:43 +02001558 /* read subtree */
1559 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1560 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001561
1562 /* finish linking metadata */
1563 ret = lydjson_metadata_finish(lydctx, &tree);
1564 LY_CHECK_GOTO(ret, cleanup);
1565
1566 /* make sure we have parsed some notification */
1567 if (!lydctx->op_node) {
1568 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1569 ret = LY_EVALID;
1570 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001571 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001572 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001573 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001574 ret = LY_EVALID;
1575 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001576 }
1577
1578 if (ntf_e) {
1579 /* finish notification envelope */
1580 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1581 LY_CHECK_GOTO(ret, cleanup);
1582 if (status == LYJSON_END) {
1583 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1584 ret = LY_EVALID;
1585 goto cleanup;
1586 } else if (status != LYJSON_OBJECT_CLOSED) {
1587 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
Michal Vasko69730152020-10-09 16:30:07 +02001588 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001589 ret = LY_EVALID;
1590 goto cleanup;
1591 }
1592 }
1593
1594 if (ntf_p) {
1595 *ntf_p = lydctx->op_node;
1596 }
1597 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001598 if (ntf_e) {
1599 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001600 lyd_insert_node(ntf_e, NULL, tree);
1601 tree = ntf_e;
1602 }
1603 if (tree_p) {
1604 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001605 }
1606
1607cleanup:
1608 /* we have used parse_only flag */
1609 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1610
1611 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1612 if (ret) {
1613 lyd_free_all(tree);
1614 lyd_free_tree(ntf_e);
1615 }
1616 return ret;
1617}
1618
1619/**
1620 * @brief Parse optional JSON envelope around the processed content.
1621 *
1622 * @param[in] jsonctx JSON parser context
1623 * @param[in] parent Parent node (some other envelope).
1624 * @param[in] module_key Name of the module where the envelope element is expected.
1625 * @param[in] object_id Name of the envelope object.
1626 * @param[out] envp_p Pointer to the created envelope opaq container.
1627 * @return LY_SUCCESS if the envelope present and successfully parsed.
1628 * @return LY_ENOT in case there is not the expected envelope.
1629 * @return LY_ERR in case of parsing failure.
1630 */
1631static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001632lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejci0f969882020-08-21 16:56:47 +02001633 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001634{
1635 LY_ERR ret = LY_ENOT, r;
1636 const char *name, *prefix;
1637 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001638 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001639 enum LYJSON_PARSER_STATUS status;
1640
1641 *envp_p = NULL;
1642
1643 /* "id" envelope */
1644 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1645 LY_CHECK_GOTO(is_attr, cleanup);
1646 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1647 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1648 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1649
1650 r = lyjson_ctx_next(jsonctx, &status);
1651 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1652 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1653
1654 /* create the object envelope */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001655 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 +02001656 NULL, NULL, 0, module_key, ly_strlen(module_key), envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001657 LY_CHECK_GOTO(ret, cleanup);
1658
1659 if (parent) {
1660 lyd_insert_node(parent, NULL, *envp_p);
1661 }
1662
1663 ret = LY_SUCCESS;
1664cleanup:
1665 return ret;
1666}
1667
1668static LY_ERR
1669lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1670{
1671 enum LYJSON_PARSER_STATUS status;
1672
1673 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1674 if (status == LYJSON_END) {
1675 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1676 return LY_EVALID;
1677 } else if (status != LYJSON_OBJECT_CLOSED) {
1678 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001679 jsonctx->value_len, jsonctx->value, object_id);
Radek Krejci1798aae2020-07-14 13:26:06 +02001680 return LY_EVALID;
1681 }
1682 return LY_SUCCESS;
1683}
1684
1685LY_ERR
1686lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1687{
1688 LY_ERR ret = LY_SUCCESS;
1689 struct lyd_json_ctx *lydctx = NULL;
1690 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1691 struct lyd_node *tree = NULL;
1692 enum LYJSON_PARSER_STATUS status;
1693
1694 /* init */
1695 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1696 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1697 lydctx->int_opts = LYD_INTOPT_RPC;
1698
1699 /* process envelope(s), if present */
1700
1701 /* process rpc */
1702 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1703 if (ret == LY_ENOT) {
1704 ret = LY_SUCCESS;
1705 goto parse_content;
1706 } else if (ret) {
1707 goto cleanup;
1708 }
1709 /* process action */
1710 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1711 if (ret == LY_ENOT) {
1712 ret = LY_SUCCESS;
1713 goto parse_content;
1714 } else if (ret) {
1715 goto cleanup;
1716 }
1717
1718parse_content:
1719
1720 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1721 assert(status == LYJSON_OBJECT);
1722
1723 /* read subtree(s) */
Michal Vasko22df3f02020-08-24 13:29:22 +02001724 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 +02001725 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001726
1727 /* finish linking metadata */
1728 ret = lydjson_metadata_finish(lydctx, &tree);
1729 LY_CHECK_GOTO(ret, cleanup);
1730
1731 /* make sure we have parsed some operation */
1732 if (!lydctx->op_node) {
1733 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
Michal Vasko69730152020-10-09 16:30:07 +02001734 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
Radek Krejci1798aae2020-07-14 13:26:06 +02001735 ret = LY_EVALID;
1736 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001737 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001738 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001739 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001740 ret = LY_EVALID;
1741 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001742 }
1743
1744 if (act_e) {
1745 /* finish action envelope */
1746 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action");
1747 LY_CHECK_GOTO(ret, cleanup);
1748 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1749 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001750 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001751 ret = LY_EVALID;
1752 goto cleanup;
1753 }
1754 }
1755 if (rpc_e) {
1756 /* finish rpc envelope */
1757 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc");
1758 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko69730152020-10-09 16:30:07 +02001759 if (!act_e && (lydctx->op_node->schema->nodetype != LYS_RPC)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001760 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001761 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001762 ret = LY_EVALID;
1763 goto cleanup;
1764 }
1765 }
1766
1767 if (op_p) {
1768 *op_p = lydctx->op_node;
1769 }
1770 assert(tree);
1771 if (tree_p) {
1772 if (rpc_e) {
1773 *tree_p = rpc_e;
1774 } else if (act_e) {
1775 *tree_p = act_e;
1776 } else {
1777 *tree_p = tree;
1778 }
1779 }
1780
1781cleanup:
1782 /* we have used parse_only flag */
1783 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1784
1785 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1786 if (ret) {
1787 lyd_free_all(tree);
1788 lyd_free_tree(act_e);
1789 lyd_free_tree(rpc_e);
1790 }
1791 return ret;
1792}
1793
1794LY_ERR
1795lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1796{
1797 LY_ERR ret = LY_SUCCESS;
1798 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001799 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001800 enum LYJSON_PARSER_STATUS status;
1801
1802 /* init */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001803 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001804 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1805 lydctx->int_opts = LYD_INTOPT_REPLY;
1806
1807 /* find request OP */
1808 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1809 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1810 break;
1811 }
1812 LYD_TREE_DFS_END(request, req_op);
1813 }
1814 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001815 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001816 ret = LY_EINVAL;
1817 goto cleanup;
1818 }
1819
1820 /* duplicate request OP with parents */
1821 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1822
1823 /* parse "rpc-reply", if any */
1824 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e);
1825 if (ret == LY_ENOT) {
1826 ret = LY_SUCCESS;
1827 } else if (ret) {
1828 goto cleanup;
1829 }
1830
1831 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1832 assert(status == LYJSON_OBJECT);
1833
1834 /* read subtree(s) */
1835 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001836 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op));
Radek Krejci1798aae2020-07-14 13:26:06 +02001837 LY_CHECK_GOTO(ret, cleanup);
1838
1839 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1840 }
1841
1842 /* finish linking metadata */
1843 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1844 LY_CHECK_GOTO(ret, cleanup);
1845
1846 if (rpcr_e) {
1847 /* finish rpc-reply envelope */
1848 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply");
1849 LY_CHECK_GOTO(ret, cleanup);
1850 }
1851
1852 if (op_p) {
1853 *op_p = rep_op;
1854 }
Radek Krejcia1c1e542020-09-29 16:06:52 +02001855 for (tree = rep_op; tree->parent; tree = lyd_parent(tree)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001856 if (rpcr_e) {
1857 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001858 lyd_insert_node(rpcr_e, NULL, tree);
1859 tree = rpcr_e;
1860 }
1861 if (tree_p) {
1862 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001863 }
1864
1865cleanup:
1866 /* we have used parse_only flag */
1867 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1868
1869 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1870 if (ret) {
1871 lyd_free_all(rep_op);
1872 lyd_free_tree(rpcr_e);
1873 }
1874 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001875}