blob: d6115d631e2a0cf6d310617388d7c9d4be9ff5b0 [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"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "config.h"
Michal Vasko90932a92020-02-12 14:33:03 +010024#include "context.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 */
50 ly_resolve_prefix_clb resolve_prefix;
51
52 struct lyjson_ctx *jsonctx; /**< JSON context */
53};
54
55/**
56 * @brief Free the JSON data parser context.
57 *
58 * JSON implementation of lyd_ctx_free_clb().
59 */
60static void
61lyd_json_ctx_free(struct lyd_ctx *lydctx)
62{
63 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
64
65 if (lydctx) {
66 lyd_ctx_free(lydctx);
67 lyjson_ctx_free(ctx->jsonctx);
68 free(ctx);
69 }
70}
71
72/**
73 * @brief JSON implementation of ly_resolve_prefix_clb.
Michal Vasko90932a92020-02-12 14:33:03 +010074 */
75const struct lys_module *
Michal Vasko52927e22020-03-16 17:26:14 +010076lydjson_resolve_prefix(const struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *UNUSED(parser))
Michal Vasko90932a92020-02-12 14:33:03 +010077{
Radek Krejci1798aae2020-07-14 13:26:06 +020078 return ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
79}
Michal Vasko90932a92020-02-12 14:33:03 +010080
Radek Krejci1798aae2020-07-14 13:26:06 +020081/**
82 * @brief Parse JSON member-name as [\@][prefix:][name]
83 *
Michal Vaskocabe0702020-08-12 10:14:36 +020084 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020085 * prefix - name of the module of the data node
86 * name - name of the data node
87 *
88 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
89 *
90 * @param[in] value String to parse
91 * @param[in] value_len Length of the @p str.
92 * @param[out] name_p Pointer to the beginning of the parsed name.
93 * @param[out] name_len_p Pointer to the length of the parsed name.
94 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
95 * @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 +020096 * @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 +020097 */
98static void
Michal Vaskocabe0702020-08-12 10:14:36 +020099lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p,
100 size_t *prefix_len_p, int *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200101{
102 const char *name, *prefix = NULL;
103 size_t name_len, prefix_len = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +0200104 int is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200105
106 name = memchr(value, ':', value_len);
107 if (name != NULL) {
108 prefix = value;
109 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200110 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200111 prefix++;
112 }
113 prefix_len = name - prefix;
114 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200115 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 } else {
117 name = value;
118 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200119 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200120 name++;
121 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200122 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100123 }
124
Radek Krejci1798aae2020-07-14 13:26:06 +0200125 *name_p = name;
126 *name_len_p = name_len;
127 *prefix_p = prefix;
128 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200129 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200130}
131
132/**
133 * @brief Get correct prefix (module_name) inside the @p node.
134 *
135 * @param[in] node Data node to get inherited prefix.
136 * @param[in] local_prefix Local prefix to replace the inherited prefix.
137 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
138 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
139 * and no context @p node to get inherited prefix.
140 * @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
141 * of no local prefix and no context @p node to get inherited prefix.
142 * @return LY_ERR value.
143 */
144static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200145lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p,
146 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200147{
148 struct lyd_node_opaq *onode;
149 const char *module_name = NULL;
150
151 assert(prefix_p && prefix_len_p);
152
153 if (local_prefix_len) {
154 *prefix_p = local_prefix;
155 *prefix_len_p = local_prefix_len;
156 return LY_SUCCESS;
157 }
158
159 *prefix_p = NULL;
160 while (node) {
161 if (node->schema) {
162 *prefix_p = node->schema->module->name;
163 break;
164 }
165 onode = (struct lyd_node_opaq*)node;
166 if (onode->prefix.module_name) {
167 *prefix_p = onode->prefix.module_name;
168 break;
169 } else if (onode->prefix.id) {
170 *prefix_p = onode->prefix.id;
171 break;
172 }
173 node = (struct lyd_node*)node->parent;
174 }
175 *prefix_len_p = ly_strlen(module_name);
176
177 return LY_SUCCESS;
178}
179
180/**
181 * @brief Get schema node corresponding to the input parameters.
182 *
183 * @param[in] lydctx JSON data parser context.
184 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
185 * @param[in] prefix Requested node's prefix (module name).
186 * @param[in] prefix_len Length of the @p prefix.
187 * @param[in] name Requested node's name.
188 * @param[in] name_len Length of the @p name.
189 * @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
190 * is accepted for searching top-level nodes.
191 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
192 * @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.
193 * @return LY_EVALID on failure, error message is logged
194 * @return LY_ENOT in case the input data are expected to be skipped
195 */
196static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200197lydjson_get_snode(const struct lyd_json_ctx *lydctx, int is_attr, const char *prefix, size_t prefix_len, const char *name,
198 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200199{
200 struct lys_module *mod = NULL;
201
202 /* leave if-feature check for validation */
203 int getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
204
205 /* init return value */
206 *snode_p = NULL;
207
208 /* get the element module */
209 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq*)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE))) {
210 if (!prefix_len) {
211 /* opaq parent (envelope) - the second part of the condition */
212 lydjson_get_node_prefix((struct lyd_node*)parent, NULL, 0, &prefix, &prefix_len);
213 }
214 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
215 } else if (parent) {
216 if (parent->schema) {
217 mod = parent->schema->module;
218 }
219 } else {
220 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
221 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
222 return LY_EVALID;
223 }
224 if (!mod) {
225 if (lydctx->parse_options & LYD_PARSE_STRICT) {
226 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
227 return LY_EVALID;
228 }
229 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
230 return LY_ENOT;
231 }
232 }
233
234 if (parent && !parent->schema && (((struct lyd_node_opaq*)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE)) {
235 /* ignore the envelope parent when searchinf for the schema node */
236 parent = NULL;
237 }
238
239 /* get the schema node */
240 if (mod && (!parent || parent->schema)) {
241 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
242 if (!*snode_p) {
243 if (lydctx->parse_options & LYD_PARSE_STRICT) {
244 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
245 name_len, name, mod->name);
246 return LY_EVALID;
247 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
248 /* skip element with children */
249 return LY_ENOT;
250 }
251 } else {
252 /* check that schema node is valid and can be used */
253 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
254 }
255 }
256
257 return LY_SUCCESS;
258}
259
260/**
261 * @brief Skip the currently open JSON object/array
262 * @param[in] jsonctx JSON context with the input data to skip.
263 * @return LY_ERR value.
264 */
265static LY_ERR
266lydjson_data_skip(struct lyjson_ctx *jsonctx)
267{
268 enum LYJSON_PARSER_STATUS status, current;
269 size_t sublevels = 1;
270
271 status = lyjson_ctx_status(jsonctx, 0);
272
273 /* skip after the content */
274 do {
275 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
276 if (current == status) {
277 sublevels++;
278 } else if (current == status + 1) {
279 sublevels--;
280 }
281 } while (current != status + 1 && sublevels);
282 /* open the next sibling */
283 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
284
285 return LY_SUCCESS;
286}
287
288/**
289 * @brief Go through the @p value and find all possible prefixes and store them in @p val_prefs_p [sized array](@ref sizedarrays).
290 *
291 * @param[in] ctx libyang context
292 * @param[in] value Pointer to the beginning of the value to check.
293 * @param[in] value_len Length of the string to examine in @p value.
294 * @param[out] val_prefs_p Pointer to the resulting [sized array](@ref sizedarrays) of found prefixes. NULL in case there are no prefixes.
295 * @return LY_EMEM on memory allocation failure.
296 * @return LY_SUCCESS on success, empty @p val_prefs_p (NULL) is valid result if there are no possible prefixes
297 */
298static LY_ERR
299lydjson_get_value_prefixes(const struct ly_ctx *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs_p)
300{
301 LY_ERR ret;
302 LY_ARRAY_COUNT_TYPE u;
303 uint32_t c;
304 const char *start, *stop;
305 struct ly_prefix *prefixes = NULL;
306 size_t len;
307
308 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
309 size_t bytes;
310 ly_getutf8(&stop, &c, &bytes);
311 if (is_yangidentstartchar(c)) {
312 for (ly_getutf8(&stop, &c, &bytes);
313 is_yangidentchar(c) && (size_t)(stop - value) < value_len;
314 ly_getutf8(&stop, &c, &bytes));
315 stop = stop - bytes;
316 if (*stop == ':') {
317 /* we have a possible prefix */
318 struct ly_prefix *p = NULL;
319
320 len = stop - start;
321
322 /* check whether we do not already have this prefix stored */
323 LY_ARRAY_FOR(prefixes, u) {
324 if (!ly_strncmp(prefixes[u].id, start, len)) {
325 p = &prefixes[u];
326 break;
327 }
328 }
329 if (!p) {
330 LY_ARRAY_NEW_GOTO(ctx, prefixes, p, ret, error);
331 p->id = lydict_insert(ctx, start, len);
332 p->module_name = lydict_insert(ctx, start, len);
333 } /* else the prefix already present */
334 }
335 stop = stop + bytes;
336 }
337 }
338
339 *val_prefs_p = prefixes;
340 return LY_SUCCESS;
341
342error:
343 LY_ARRAY_FOR(prefixes, u) {
344 lydict_remove(ctx, prefixes[u].id);
345 lydict_remove(ctx, prefixes[u].module_name);
346 }
347 LY_ARRAY_FREE(prefixes);
348 return ret;
349}
350
351/**
352 * @brief Check that the input data are parseable as the @p list.
353 *
354 * Checks for all the list's keys. Function does not revert the context state.
355 *
356 * @param[in] jsonctx JSON parser context.
357 * @param[in] list List schema node corresponding to the input data object.
358 * @return LY_SUCCESS in case the data are ok for the @p list
359 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
360 */
361static LY_ERR
362lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
363{
364 LY_ERR ret = LY_SUCCESS;
365 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
366 struct ly_set key_set = {0};
367 const struct lysc_node *snode;
368 uint32_t i, status_count;
369
370 assert(list && (list->nodetype == LYS_LIST));
371 assert(status == LYJSON_OBJECT);
372
373 /* get all keys into a set (keys do not have if-features or anything) */
374 snode = NULL;
375 while ((snode = lys_getnext(snode, list, NULL, LYS_GETNEXT_NOSTATECHECK)) && (snode->flags & LYS_KEY)) {
376 ly_set_add(&key_set, (void *)snode, LY_SET_OPT_USEASLIST);
377 }
378
379 if (status != LYJSON_OBJECT_EMPTY) {
380 status_count = jsonctx->status.count;
381
382 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
383 const char *name, *prefix;
384 size_t name_len, prefix_len;
385 int is_attr;
386
387 /* match the key */
388 snode = NULL;
389 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
390
391 if (!is_attr && !prefix) {
392 for (i = 0; i < key_set.count; ++i) {
393 snode = (const struct lysc_node *)key_set.objs[i];
394 if (!ly_strncmp(snode->name, name, name_len)) {
395 break;
396 }
397 }
398 /* go into the item to a) process it as a key or b) start skipping it as another list child */
399 ret = lyjson_ctx_next(jsonctx, &status);
400 LY_CHECK_GOTO(ret, cleanup);
401
402 if (snode) {
403 /* we have the key, validate the value */
404 if (status < LYJSON_NUMBER) {
405 /* not a terminal */
406 ret = LY_ENOT;
407 goto cleanup;
408 }
409
410 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, lydjson_resolve_prefix, jsonctx, LYD_JSON);
411 LY_CHECK_GOTO(ret, cleanup);
412
413 /* key with a valid value, remove from the set */
414 ly_set_rm_index(&key_set, i, NULL);
415 }
416 } else {
417 /* start skipping the member we are not interested in */
418 ret = lyjson_ctx_next(jsonctx, &status);
419 LY_CHECK_GOTO(ret, cleanup);
420 }
421 /* move to the next child */
422 while (status_count < jsonctx->status.count) {
423 ret = lyjson_ctx_next(jsonctx, &status);
424 LY_CHECK_GOTO(ret, cleanup);
425 }
426 }
427 }
428
429 if (key_set.count) {
430 /* some keys are missing/did not validate */
431 ret = LY_ENOT;
432 }
433
434cleanup:
435 ly_set_erase(&key_set, NULL);
436 return ret;
437}
438
439/**
440 * @brief Get the hint for the data type parsers according to the current JSON parser context.
441 *
442 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
443 * @param[in,out] status Pointer to the current context status,
444 * in some circumstances the function manipulates with the context so the status is updated.
445 * @param[out] type_hint_p Pointer to the variable to store the result.
446 * @return LY_SUCCESS in case of success.
447 * @return LY_EINVAL in case of invalid context status not referring to a value.
448 */
449static LY_ERR
450lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, int *type_hint_p)
451{
452 *type_hint_p = 0;
453
454 if (*status_p == LYJSON_ARRAY) {
455 /* only [null] */
456 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
457 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
458
459 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
460 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
461
462 *type_hint_p = LYD_NODE_OPAQ_ISEMPTY;
463 } else if (*status_p == LYJSON_STRING) {
464 *type_hint_p = LYD_NODE_OPAQ_ISSTRING;
465 } else if (*status_p == LYJSON_NUMBER) {
466 *type_hint_p = LYD_NODE_OPAQ_ISNUMBER;
467 } else if (*status_p == LYJSON_FALSE || *status_p == LYJSON_TRUE) {
468 *type_hint_p = LYD_NODE_OPAQ_ISBOOLEAN;
469 } else if (*status_p == LYJSON_NULL) {
470 *type_hint_p = 0;
471 } else {
472 return LY_EINVAL;
473 }
474
475 return LY_SUCCESS;
476}
477
478/**
479 * @brief Check in advance if the input data are parsable according to the provided @p snode.
480 *
481 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
482 * is naturally done when the data are really parsed.
483 *
484 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
485 * as before calling, despite it is necessary to process input data for checking.
486 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
487 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
488 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
489 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
490 * @return LY_EINVAL in case of invalid leaf JSON encoding
491 * and they are expected to be parsed as opaq nodes.
492 */
493static LY_ERR
494lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, int *type_hint_p)
495{
496 LY_ERR ret = LY_SUCCESS;
497 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
498 enum LYJSON_PARSER_STATUS status;
499
500 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200501
Michal Vasko32ac9942020-08-12 14:35:12 +0200502 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
503 /* can always be parsed as a data node if we have the schema node */
504 return LY_SUCCESS;
505 }
506
507 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200508 /* backup parser */
509 lyjson_ctx_backup(jsonctx);
510 status = lyjson_ctx_status(jsonctx, 0);
511
Michal Vasko32ac9942020-08-12 14:35:12 +0200512 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
513 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 switch (snode->nodetype) {
515 case LYS_LEAFLIST:
516 case LYS_LEAF:
517 /* value may not be valid in which case we parse it as an opaque node */
518 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
519 if (ret) {
520 break;
521 }
522
Michal Vasko32ac9942020-08-12 14:35:12 +0200523 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, lydjson_resolve_prefix, jsonctx,
524 LYD_JSON)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 ret = LY_ENOT;
526 }
527 break;
528 case LYS_LIST:
529 /* lists may not have all its keys */
530 if (lydjson_check_list(jsonctx, snode)) {
531 /* invalid list, parse as opaque if it missing/has invalid some keys */
532 ret = LY_ENOT;
533 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200534 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200535 }
536
537 /* restore parser */
538 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200539 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200540 status = lyjson_ctx_status(jsonctx, 0);
541 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
542 }
543
544 return ret;
545}
546
547/**
548 * @brief Join the forward-referencing metadata with their target data nodes.
549 *
550 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
551 *
552 * @param[in] lydctx JSON data parser context.
553 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
554 * as a starting point to search for the metadata's target data node
555 * @return LY_SUCCESS on success
556 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
557 */
558static LY_ERR
559lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
560{
561 LY_ERR ret = LY_SUCCESS;
562 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
563 unsigned int instance = 0;
564 const char *prev = NULL;
565
566 /* finish linking metadata */
567 LY_LIST_FOR_SAFE(*first_p, next, attr) {
568 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq*)attr;
569 unsigned int match = 0;
570 int is_attr;
571 const char *name, *prefix;
572 size_t name_len, prefix_len;
573 const struct lysc_node *snode;
574
575 if (attr->schema || meta_container->name[0] != '@') {
576 /* not an opaq metadata node */
577 continue;
578 }
579
580 if (prev != meta_container->name) {
581 /* metas' names are stored in dictionary, so checking pointers must works */
582 lydict_remove(lydctx->jsonctx->ctx, prev);
583 prev = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0);
584 instance = 1;
585 } else {
586 instance++;
587 }
588
589 /* find the correspnding data node */
590 LY_LIST_FOR(start, node) {
591 if (!node->schema) {
592 /* opaq node - we are going to put into it just a generic attribute. */
593 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq*)node)->name)) {
594 continue;
595 }
596
597 if (((struct lyd_node_opaq*)node)->hint & LYD_NODE_OPAQ_ISLIST) {
598 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
599 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq*)node)->name);
600 ret = LY_EVALID;
601 goto cleanup;
602 }
603
604 /* match */
605 match++;
606 if (match != instance) {
607 continue;
608 }
609
610 LY_LIST_FOR(meta_container->child, meta_iter) {
611 /* convert opaq node to a attribute of the opaq node */
612 struct lyd_node_opaq *meta = (struct lyd_node_opaq*)meta_iter;
613 struct ly_prefix *val_prefs = NULL;
614 int dynamic = 0;
615
616 /* get value prefixes */
617 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
618
619 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
620 &dynamic, meta->hint, LYD_JSON, val_prefs, meta->prefix.id, ly_strlen(meta->prefix.id),
621 meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
622 LY_CHECK_GOTO(ret, cleanup);
623 }
624
625 /* done */
626 break;
627 } else {
628 /* this is the second time we are resolving the schema node, so it must succeed,
629 * but remember that snode can be still NULL */
630 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
631 assert(is_attr);
632 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
633 assert(ret == LY_SUCCESS);
634
635 if (snode != node->schema) {
636 continue;
637 }
638
639 /* match */
640 match++;
641 if (match != instance) {
642 continue;
643 }
644
645 LY_LIST_FOR(meta_container->child, meta_iter) {
646 /* convert opaq node to a metadata of the node */
647 struct lyd_node_opaq *meta = (struct lyd_node_opaq*)meta_iter;
648 struct lys_module *mod = NULL;
649 int dynamic = 0;
650
651 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
652 if (mod) {
653 ret = lyd_parser_create_meta((struct lyd_ctx*)lydctx, node, NULL, mod,
654 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
655 &dynamic, meta->hint, lydjson_resolve_prefix, NULL, LYD_JSON, snode);
656 LY_CHECK_GOTO(ret, cleanup);
657 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
658 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
659 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
660 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
661 ret = LY_EVALID;
662 goto cleanup;
663 }
664 }
665 /* add/correct flags */
666 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
667
668 /* done */
669 break;
670 }
671 }
672
673 if (match != instance) {
674 /* there is no corresponding data node for the metadata */
675 if (instance > 1) {
676 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
677 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
678 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
679 } else {
680 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
681 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
682 }
683 ret = LY_EVALID;
684 } else {
685 /* remove the opaq attr */
686 if (attr == (*first_p)) {
687 *first_p = attr->next;
688 }
689 lyd_free_tree(attr);
690 }
691 }
692
693cleanup:
694 lydict_remove(lydctx->jsonctx->ctx, prev);
695
696 return ret;
697}
698
699/**
700 * @brief Parse a metadata member.
701 *
702 * @param[in] lydctx JSON data parser context.
703 * @param[in] snode Schema node of the metadata parent.
704 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
705 * so the data node does not exists. In such a case the metadata is stored in the context for the later
706 * processing by lydjson_metadata_finish().
707 * @return LY_SUCCESS on success
708 * @return Various LY_ERR values in case of failure.
709 */
710static LY_ERR
711lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
712{
713 LY_ERR ret = LY_SUCCESS;
714 enum LYJSON_PARSER_STATUS status;
715 const char *expected;
716 int in_parent = 0;
717 const char *name, *prefix = NULL;
718 size_t name_len, prefix_len = 0;
719 struct lys_module *mod;
720 struct lyd_meta *meta = NULL;
721 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
722 int is_attr = 0;
723 struct lyd_node *prev = node;
724 int instance = 0;
725 uint16_t nodetype;
726
727 assert(snode || node);
728
729 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
730
731 /* move to the second item in the name/X pair */
732 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
733 LY_CHECK_GOTO(ret, cleanup);
734
735 /* check attribute encoding */
736 switch (nodetype) {
737 case LYS_LEAFLIST:
738 expected = "@name/array of objects/nulls";
739
740 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
741
742next_entry:
743 instance++;
744
745 /* move into array / next entry */
746 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
747 LY_CHECK_GOTO(ret, cleanup);
748
749 if (status == LYJSON_ARRAY_CLOSED) {
750 /* we are done, move after the array */
751 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
752 goto cleanup;
753 }
754 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
755
756 if (!node || node->schema != prev->schema) {
757 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
758 "Missing JSON data instance no. %d of %s:%s to be coupled with metadata.",
759 instance, prev->schema->module->name, prev->schema->name);
760 ret = LY_EVALID;
761 goto cleanup;
762 }
763
764 if (status == LYJSON_NULL) {
765 /* continue with the next entry in the leaf-list array */
766 prev = node;
767 node = node->next;
768 goto next_entry;
769 }
770 break;
771 case LYS_LEAF:
772 case LYS_ANYXML:
773 expected = "@name/object";
774
775 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
776 break;
777 case LYS_CONTAINER:
778 case LYS_LIST:
779 case LYS_ANYDATA:
780 case LYS_NOTIF:
781 case LYS_ACTION:
782 case LYS_RPC:
783 in_parent = 1;
784 expected = "@/object";
785 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
786 break;
787 }
788
789 /* process all the members inside a single metadata object */
790 assert(status == LYJSON_OBJECT);
791
792 while (status != LYJSON_OBJECT_CLOSED) {
793 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
794 if (!prefix) {
795 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
796 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
797 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
798 ret = LY_EVALID;
799 goto cleanup;
800 } else if (is_attr) {
801 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
802 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
803 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
804 ret = LY_EVALID;
805 goto cleanup;
806 }
807
808 /* get the element module */
809 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
810 if (!mod) {
811 if (lydctx->parse_options & LYD_PARSE_STRICT) {
812 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_REFERENCE,
813 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
814 prefix_len, prefix, name_len, name);
815 ret = LY_EVALID;
816 goto cleanup;
817 }
818 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
819 /* skip element with children */
820 ret = lydjson_data_skip(lydctx->jsonctx);
821 LY_CHECK_GOTO(ret, cleanup);
822 status = lyjson_ctx_status(lydctx->jsonctx, 0);
823 /* end of the item */
824 continue;
825 }
826 }
827
828 /* get the value */
829 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
830 LY_CHECK_GOTO(ret, cleanup);
831
832 if (node->schema) {
833 /* create metadata */
834 meta = NULL;
835 ret = lyd_parser_create_meta((struct lyd_ctx*)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
836 &lydctx->jsonctx->dynamic, 0, lydjson_resolve_prefix, lydctx->jsonctx, LYD_JSON, snode);
837 LY_CHECK_GOTO(ret, cleanup);
838
839 /* add/correct flags */
840 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
841 } else {
842 /* create attribute */
843 struct ly_prefix *val_prefs = NULL;
844 const char *module_name;
845 size_t module_name_len;
846
847 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
848
849 /* get value prefixes */
850 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
851
852 /* attr2 is always changed to the created attribute */
853 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
854 &lydctx->jsonctx->dynamic, 0, LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len);
855 LY_CHECK_GOTO(ret, cleanup);
856 }
857 /* next member */
858 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
859 LY_CHECK_GOTO(ret, cleanup);
860 }
861
862 if (nodetype == LYS_LEAFLIST) {
863 /* continue by processing another metadata object for the following
864 * leaf-list instance since they are allways instantiated in JSON array */
865 prev = node;
866 node = node->next;
867 goto next_entry;
868 }
869
870 /* move after the metadata */
871 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
872 LY_CHECK_GOTO(ret, cleanup);
873
874cleanup:
875 return ret;
876
877representation_error:
878 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
879 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
880 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq*)node)->name,
881 expected, lyjson_token2str(status), in_parent ? "" : "name");
882
883 return LY_EVALID;
884}
885
886/**
887 * @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.
888 *
889 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
890 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
891 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
892 */
893static void
894lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
895{
896 if (*node_p) {
897 /* insert, keep first pointer correct */
898 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
899 if (first_p) {
900 if (parent) {
901 *first_p = parent->child;
902 } else {
903 while ((*first_p)->prev->next) {
904 *first_p = (*first_p)->prev;
905 }
906 }
907 }
908 *node_p = NULL;
909 }
910}
911
912static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
913
914/**
915 * @brief Parse opaq node from the input.
916 *
917 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
918 *
919 * @param[in] lydctx JSON data parser context.
920 * @param[in] name Name of the opaq node to create.
921 * @param[in] name_len Length of the @p name string.
922 * @param[in] prefix Prefix of the opaq node to create.
923 * @param[in] prefix_len Length of the @p prefx string.
924 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
925 * but must be set if @p first is not.
926 * @param[in,out] status_p Pointer to the current status of the parser context,
927 * since the function manipulates with the context and process the input, the status can be updated.
928 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
929 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
930 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
931 * @param[out] node_p Pointer to the created opaq node.
932 * @return LY_ERR value.
933 */
934static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200935lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
936 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
937 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200938{
939 LY_ERR ret = LY_SUCCESS;
940 const char *value = NULL, *module_name;
941 size_t value_len = 0, module_name_len = 0;
942 struct ly_prefix *val_prefs = NULL;
943 int dynamic = 0;
944 int type_hint = 0;
945
946 if (*status_inner_p != LYJSON_OBJECT && *status_inner_p != LYJSON_OBJECT_EMPTY) {
947 /* prepare for creating opaq node with a value */
948 value = lydctx->jsonctx->value;
949 value_len = lydctx->jsonctx->value_len;
950 dynamic = lydctx->jsonctx->dynamic;
951 lydctx->jsonctx->dynamic = 0;
952
953 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
954
955 if (value) {
956 /* get value prefixes */
957 LY_CHECK_RET(lydjson_get_value_prefixes(lydctx->jsonctx->ctx, value, value_len, &val_prefs));
958 }
959 }
960
961 /* create node */
962 lydjson_get_node_prefix((struct lyd_node*)parent, prefix, prefix_len, &module_name, &module_name_len);
963 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, type_hint,
964 LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len, node_p);
965 if (dynamic) {
966 free((char*)value);
967 dynamic = 0;
968 }
969 value = NULL;
970 value_len = 0;
971 val_prefs = NULL;
972 LY_CHECK_RET(ret);
973
974 if (*status_p == LYJSON_OBJECT || *status_p == LYJSON_OBJECT_EMPTY) {
975 /* process children */
976 while (*status_p != LYJSON_OBJECT_CLOSED && *status_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_p = lyjson_ctx_status(lydctx->jsonctx, 0);
979 }
980 } else if (*status_p == LYJSON_ARRAY || *status_p == LYJSON_ARRAY_EMPTY) {
981 /* process another instance of the same node */
982 /* but first mark the node to be expected a list or a leaf-list */
983 ((struct lyd_node_opaq*)*node_p)->hint |= LYD_NODE_OPAQ_ISLIST;
984
985 if (*status_inner_p == LYJSON_OBJECT || *status_inner_p == LYJSON_OBJECT_EMPTY) {
986 /* but first process children of the object in the array */
987 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
988 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
989 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
990 }
991 }
992
993 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
994
995 /* continue with the next instance */
996 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
997 assert(node_p);
998 lydjson_maintain_children(parent, first_p, node_p);
999 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
1000 }
1001 }
1002
1003 /* finish linking metadata */
1004 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
1005
1006 /* move after the item */
1007 return lyjson_ctx_next(lydctx->jsonctx, status_p);
1008}
1009
1010/**
1011 * @brief Process the attribute container (starting by @)
1012 *
1013 * @param[in] lydctx JSON data parser context.
1014 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1015 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1016 * @param[in] name Name of the opaq node to create.
1017 * @param[in] name_len Length of the @p name string.
1018 * @param[in] prefix Prefix of the opaq node to create.
1019 * @param[in] prefix_len Length of the @p prefx string.
1020 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1021 * but must be set if @p first is not.
1022 * @param[in,out] status_p Pointer to the current status of the parser context,
1023 * since the function manipulates with the context and process the input, the status can be updated.
1024 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1025 * @param[out] node_p Pointer to the created opaq node.
1026 * @return LY_ERR value.
1027 */
1028static LY_ERR
1029lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
1030 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Michal Vaskoa5da3292020-08-12 13:10:50 +02001031 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1032 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001033{
1034 LY_ERR ret = LY_SUCCESS;
1035 enum LYJSON_PARSER_STATUS status_inner;
1036
1037 /* parse as an attribute to a node */
1038 if (!attr_node && snode) {
1039 /* try to find the instance */
1040 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1041 if (iter->schema == snode) {
1042 attr_node = iter;
1043 break;
1044 }
1045 }
1046 }
1047 if (!attr_node) {
1048 /* parse just as an opaq node with the name beginning with @,
1049 * later we have to check that it belongs to a standard node
1050 * and it is supposed to be converted to a metadata */
1051 uint32_t prev_opts;
1052
1053 /* move into metadata */
1054 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1055
1056 if (*status_p == LYJSON_ARRAY) {
1057 /* move into the array */
1058 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1059 } else {
1060 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1061 status_inner = LYJSON_ERROR;
1062 }
1063
1064 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
1065 prev_opts = lydctx->parse_options;
1066 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1067 lydctx->parse_options |= LYD_PARSE_OPAQ;
1068
1069 ret = lydjson_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
1070 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
1071
1072 /* restore the parser options */
1073 lydctx->parse_options = prev_opts;
1074 } else {
1075 ret = lydjson_metadata(lydctx, snode, attr_node);
1076 }
1077
1078 return ret;
1079}
1080
1081/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001082 * @brief Parse a single instance of a node.
1083 *
1084 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1085 * as before calling, despite it is necessary to process input data for checking.
1086 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1087 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1088 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1089 * @param[in] name Parsed JSON node name.
1090 * @param[in] name_len Lenght of @p name.
1091 * @param[in] prefix Parsed JSON node prefix.
1092 * @param[in] prefix_len Length of @p prefix.
1093 * @param[in,out] status JSON parser status, is updated.
1094 * @param[out] node Parsed data (or opaque) node.
1095 * @return LY_SUCCESS if a node was successfully parsed,
1096 * @return LY_EINVAL in case of invalid JSON encoding,
1097 * @return LY_ERR on other errors.
1098 */
1099static LY_ERR
1100lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
1101 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1102 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
1103{
1104 LY_ERR ret;
1105 int type_hint;
1106 uint32_t prev_opts;
1107 struct lyd_node *tree = NULL;
1108
1109 ret = lydjson_data_check_opaq(lydctx, snode, &type_hint);
1110 if (ret == LY_SUCCESS) {
1111 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1112 if (snode->nodetype & LYD_NODE_TERM) {
1113 /* create terminal node */
1114 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1115 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, type_hint,
1116 lydjson_resolve_prefix, lydctx->jsonctx, LYD_JSON, node);
1117 LY_CHECK_RET(ret);
1118
1119 /* move JSON parser */
1120 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1121 LY_CHECK_RET(ret);
1122 } else if (snode->nodetype & LYD_NODE_INNER) {
1123 /* create inner node */
1124 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1125
1126 ret = lyd_create_inner(snode, node);
1127 LY_CHECK_RET(ret);
1128
1129 /* process children */
1130 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1131 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1132 LY_CHECK_RET(ret);
1133 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1134 }
1135
1136 /* finish linking metadata */
1137 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1138 LY_CHECK_RET(ret);
1139
1140 if (snode->nodetype == LYS_LIST) {
1141 /* check all keys exist */
1142 ret = lyd_parse_check_keys(*node);
1143 LY_CHECK_RET(ret);
1144 }
1145
1146 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1147 /* new node validation, autodelete CANNOT occur, all nodes are new */
1148 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1149 LY_CHECK_RET(ret);
1150
1151 /* add any missing default children */
1152 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->unres_node_type,
1153 &lydctx->when_check, (lydctx->validate_options & LYD_VALIDATE_NO_STATE)
1154 ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1155 LY_CHECK_RET(ret);
1156 }
1157
1158 /* move JSON parser */
1159 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1160 LY_CHECK_RET(ret);
1161 } else if (snode->nodetype & LYD_NODE_ANY) {
1162 /* create any node */
1163 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1164
1165 /* parse any data tree with correct options */
1166 /* first backup the current options and then make the parser to process data as opaq nodes */
1167 prev_opts = lydctx->parse_options;
1168 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1169 lydctx->parse_options |= LYD_PARSE_OPAQ;
1170
1171 /* process the anydata content */
1172 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1173 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1174 LY_CHECK_RET(ret);
1175 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1176 }
1177
1178 /* restore parser options */
1179 lydctx->parse_options = prev_opts;
1180
1181 /* finish linking metadata */
1182 ret = lydjson_metadata_finish(lydctx, &tree);
1183 LY_CHECK_RET(ret);
1184
1185 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1186 LY_CHECK_RET(ret);
1187 }
1188 } else if (ret == LY_ENOT) {
1189 /* parse it again as an opaq node */
1190 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
1191 status, status, first_p, node);
1192 LY_CHECK_RET(ret);
1193
1194 if (snode->nodetype & (LYS_LEAFLIST | LYS_LIST)) {
1195 ((struct lyd_node_opaq *)*node)->hint |= LYD_NODE_OPAQ_ISLIST;
1196 }
1197 }
1198
1199 return ret;
1200}
1201
1202/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001203 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001204 *
1205 * @param[in] lydctx JSON data parser context.
1206 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1207 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1208 * @return LY_ERR value.
1209 */
1210static LY_ERR
1211lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1212{
1213 LY_ERR ret = LY_SUCCESS;
1214 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1215 enum LYJSON_PARSER_STATUS status_inner = 0;
1216 const char *name, *prefix = NULL;
1217 size_t name_len, prefix_len = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001218 int is_meta = 0;
1219 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001220 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001221 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1222 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001223
1224 assert(parent || first_p);
1225 assert(status == LYJSON_OBJECT);
1226
1227 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001228 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 +02001229
Michal Vaskocabe0702020-08-12 10:14:36 +02001230 if (!is_meta || name_len || prefix_len) {
1231 /* get the schema node */
1232 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1233 if (ret == LY_ENOT) {
1234 /* skip element with children */
1235 ret = lydjson_data_skip(lydctx->jsonctx);
1236 LY_CHECK_GOTO(ret, cleanup);
1237 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1238 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001239 goto cleanup;
1240 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001241 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001242
Michal Vaskocabe0702020-08-12 10:14:36 +02001243 if (!snode) {
1244 /* we will not be parsing it as metadata */
1245 is_meta = 0;
1246 }
1247 }
1248
1249 if (is_meta) {
1250 /* parse as metadata */
1251 if (!name_len && !prefix_len) {
1252 /* parent's metadata without a name - use the schema from the parent */
1253 if (!parent) {
1254 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
1255 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1256 ret = LY_EVALID;
1257 goto cleanup;
1258 }
1259 attr_node = (struct lyd_node *)parent;
1260 snode = attr_node->schema;
1261 }
1262 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
1263 first_p, &node);
1264 LY_CHECK_GOTO(ret, cleanup);
1265 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001266 /* parse as an opaq node */
1267 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1268
1269 /* move to the second item in the name/X pair */
1270 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1271 LY_CHECK_GOTO(ret, cleanup);
1272
1273 if (status == LYJSON_ARRAY) {
1274 /* move into the array */
1275 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1276 LY_CHECK_GOTO(ret, cleanup);
1277 } else {
1278 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1279 status_inner = LYJSON_ERROR;
1280 }
1281
1282 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
1283 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
1284 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001285 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001286 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001287
1288 /* move to the second item in the name/X pair */
1289 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1290 LY_CHECK_GOTO(ret, cleanup);
1291
1292 /* first check the expected representation according to the nodetype and then continue with the content */
1293 switch (snode->nodetype) {
1294 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001295 case LYS_LIST:
1296 if (snode->nodetype == LYS_LEAFLIST) {
1297 expected = "name/array of values";
1298 } else {
1299 expected = "name/array of objects";
1300 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001301
1302 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1303
1304 /* move into array */
1305 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1306 LY_CHECK_GOTO(ret, cleanup);
1307
Michal Vasko32ac9942020-08-12 14:35:12 +02001308 /* process all the values/objects */
1309 do {
1310 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001311
Michal Vasko32ac9942020-08-12 14:35:12 +02001312 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
1313 &status, &node);
1314 if (ret == LY_EINVAL) {
1315 goto representation_error;
1316 } else if (ret) {
1317 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001318 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001319 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001320
Michal Vasko32ac9942020-08-12 14:35:12 +02001321 /* move after the array */
1322 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1323 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001324
1325 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001326 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001327 case LYS_CONTAINER:
1328 case LYS_NOTIF:
1329 case LYS_ACTION:
1330 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001331 case LYS_ANYDATA:
1332 case LYS_ANYXML:
1333 if (snode->nodetype == LYS_LEAF) {
1334 if (status == LYJSON_ARRAY) {
1335 expected = "name/[null]";
1336 } else {
1337 expected = "name/value";
1338 }
1339 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001340 expected = "name/object";
1341 }
1342
Michal Vasko32ac9942020-08-12 14:35:12 +02001343 /* process the value/object */
1344 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1345 if (ret == LY_EINVAL) {
1346 goto representation_error;
1347 } else if (ret) {
1348 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001349 }
1350
Michal Vasko32ac9942020-08-12 14:35:12 +02001351 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001352 /* rememeber the RPC/action/notification */
1353 lydctx->op_node = node;
1354 }
1355
1356 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001357 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001358 }
1359
Michal Vasko32ac9942020-08-12 14:35:12 +02001360 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001361 lydjson_maintain_children(parent, first_p, &node);
1362
1363cleanup:
1364 lyd_free_tree(node);
1365 return ret;
1366
1367representation_error:
1368 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
1369 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1370 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
1371
1372 ret = LY_EVALID;
1373 goto cleanup;
1374}
1375
1376/**
1377 * @brief Common start of JSON parser processing different types of the input data.
1378 *
1379 * @param[in] ctx libyang context
1380 * @param[in] in Input structure.
1381 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1382 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1383 * @param[out] lydctx_p Data parser context to finish validation.
1384 * @return LY_ERR value.
1385 */
1386static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001387lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
1388 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001389{
1390 LY_ERR ret = LY_SUCCESS;
1391 struct lyd_json_ctx *lydctx;
1392 size_t i, line = 1;
1393
1394 /* starting top-level */
1395 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1396 if (in->current[i] == 0x0a) { /* new line */
1397 line++;
1398 };
1399 }
1400 if (in->current[i] == '\0') {
1401 /* empty data input */
1402 return LY_SUCCESS;
1403 }
1404 if (in->current[i] != '{') {
1405 LOGVAL(ctx, LY_VLOG_LINE, &line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(&in->current[i]), &in->current[i],
1406 "a top-level JSON object ('{') holding encoded YANG data");
1407 return LY_EVALID;
1408 }
1409
1410 /* init context */
1411 lydctx = calloc(1, sizeof *lydctx);
1412 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1413 lydctx->parse_options = parse_options;
1414 lydctx->validate_options = validate_options;
1415 lydctx->free = lyd_json_ctx_free;
1416 lydctx->resolve_prefix = lydjson_resolve_prefix;
1417
1418 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1419
1420 *lydctx_p = lydctx;
1421 return LY_SUCCESS;
1422}
1423
1424LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001425lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
1426 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001427{
1428 LY_ERR ret = LY_SUCCESS;
1429 struct lyd_json_ctx *lydctx = NULL;
1430 enum LYJSON_PARSER_STATUS status;
1431
1432 assert(tree_p);
1433 *tree_p = NULL;
1434
1435 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx);
1436 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1437
1438 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1439 assert(status == LYJSON_OBJECT);
1440
1441 /* read subtree(s) */
1442 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1443 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1444 LY_CHECK_GOTO(ret, cleanup);
1445
1446 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1447 }
1448
1449 /* finish linking metadata */
1450 ret = lydjson_metadata_finish(lydctx, tree_p);
1451 LY_CHECK_GOTO(ret, cleanup);
1452
1453cleanup:
1454 /* there should be no unresolved types stored */
1455 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count
1456 && !lydctx->when_check.count));
1457
1458 if (ret) {
1459 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1460 lyd_free_all(*tree_p);
1461 *tree_p = NULL;
1462 } else {
1463 *lydctx_p = (struct lyd_ctx *)lydctx;
1464 }
1465
1466 return ret;
1467}
1468
1469/**
1470 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1471 *
1472 * @param[in] jsonctx JSON parser context
1473 * @param[out] envp_p Pointer to the created envelope opaq container.
1474 * @return LY_SUCCESS if the envelope present and successfully parsed.
1475 * @return LY_ENOT in case there is not the expected envelope.
1476 * @return LY_ERR in case of parsing failure.
1477 */
1478static LY_ERR
1479lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p)
1480{
1481 LY_ERR ret = LY_ENOT, r;
1482 const char *name, *prefix, *value = NULL;
1483 size_t name_len, prefix_len, value_len;
1484 int is_attr, dynamic = 0;
1485 enum LYJSON_PARSER_STATUS status;
1486 struct lyd_node *et;
1487
1488 *envp_p = NULL;
1489
1490 /* backup the context */
1491 lyjson_ctx_backup(jsonctx);
1492
1493 /* "notification" envelope */
1494 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1495 LY_CHECK_GOTO(is_attr, cleanup);
1496 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1497 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1498
1499 r = lyjson_ctx_next(jsonctx, &status);
1500 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1501 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1502
1503 /* "eventTime" child */
1504 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1505 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1506 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1507
1508 /* go for the data */
1509 r = lyjson_ctx_next(jsonctx, &status);
1510 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1511 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1512
1513 value = jsonctx->value;
1514 value_len = jsonctx->value_len;
1515 dynamic = jsonctx->dynamic;
1516 jsonctx->dynamic = 0;
1517
1518 r = lyjson_ctx_next(jsonctx, &status);
1519 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1520 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1521 /* now the notificationContent is expected, which will be parsed by the caller */
1522
1523 /* create notification envelope */
1524 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LYD_NODE_OPAQ_ISENVELOPE,
1525 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, envp_p);
1526 LY_CHECK_GOTO(ret, cleanup);
1527 /* create notification envelope */
1528 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", 9, value, value_len, &dynamic, LYD_NODE_OPAQ_ISSTRING,
1529 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, &et);
1530 LY_CHECK_GOTO(ret, cleanup);
1531 /* insert eventTime into notification */
1532 lyd_insert_node(*envp_p, NULL, et);
1533
1534 ret = LY_SUCCESS;
1535cleanup:
1536 if (ret) {
1537 /* restore the context */
1538 lyjson_ctx_restore(jsonctx);
1539 if (dynamic) {
1540 free((char*)value);
1541 }
1542 }
1543 return ret;
1544}
1545
1546LY_ERR
1547lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1548{
1549 LY_ERR ret = LY_SUCCESS;
1550 struct lyd_json_ctx *lydctx = NULL;
1551 struct lyd_node *ntf_e = NULL;
1552 struct lyd_node *tree = NULL;
1553 enum LYJSON_PARSER_STATUS status;
1554
1555 /* init */
1556 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1557 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1558 lydctx->int_opts = LYD_INTOPT_NOTIF;
1559
1560 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1561 assert(status == LYJSON_OBJECT);
1562
1563 /* parse "notification" and "eventTime", if present */
1564 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e);
1565 if (ret == LY_ENOT) {
1566 ret = LY_SUCCESS;
1567 } else if (ret) {
1568 goto cleanup;
1569 }
1570
Michal Vaskocf770e22020-08-12 13:21:43 +02001571 /* read subtree */
1572 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1573 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001574
1575 /* finish linking metadata */
1576 ret = lydjson_metadata_finish(lydctx, &tree);
1577 LY_CHECK_GOTO(ret, cleanup);
1578
1579 /* make sure we have parsed some notification */
1580 if (!lydctx->op_node) {
1581 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1582 ret = LY_EVALID;
1583 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001584 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1585 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1586 tree->schema->name);
1587 ret = LY_EVALID;
1588 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001589 }
1590
1591 if (ntf_e) {
1592 /* finish notification envelope */
1593 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1594 LY_CHECK_GOTO(ret, cleanup);
1595 if (status == LYJSON_END) {
1596 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1597 ret = LY_EVALID;
1598 goto cleanup;
1599 } else if (status != LYJSON_OBJECT_CLOSED) {
1600 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
1601 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
1602 ret = LY_EVALID;
1603 goto cleanup;
1604 }
1605 }
1606
1607 if (ntf_p) {
1608 *ntf_p = lydctx->op_node;
1609 }
1610 assert(tree);
1611 if (tree_p) {
1612 *tree_p = tree;
1613 }
1614 if (ntf_e) {
1615 /* connect to the notification */
1616 lyd_insert_node(ntf_e, NULL, *tree_p);
1617 if (tree_p) {
1618 *tree_p = ntf_e;
1619 }
1620 }
1621
1622cleanup:
1623 /* we have used parse_only flag */
1624 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1625
1626 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1627 if (ret) {
1628 lyd_free_all(tree);
1629 lyd_free_tree(ntf_e);
1630 }
1631 return ret;
1632}
1633
1634/**
1635 * @brief Parse optional JSON envelope around the processed content.
1636 *
1637 * @param[in] jsonctx JSON parser context
1638 * @param[in] parent Parent node (some other envelope).
1639 * @param[in] module_key Name of the module where the envelope element is expected.
1640 * @param[in] object_id Name of the envelope object.
1641 * @param[out] envp_p Pointer to the created envelope opaq container.
1642 * @return LY_SUCCESS if the envelope present and successfully parsed.
1643 * @return LY_ENOT in case there is not the expected envelope.
1644 * @return LY_ERR in case of parsing failure.
1645 */
1646static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001647lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
1648 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001649{
1650 LY_ERR ret = LY_ENOT, r;
1651 const char *name, *prefix;
1652 size_t name_len, prefix_len;
1653 int is_attr;
1654 enum LYJSON_PARSER_STATUS status;
1655
1656 *envp_p = NULL;
1657
1658 /* "id" envelope */
1659 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1660 LY_CHECK_GOTO(is_attr, cleanup);
1661 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1662 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1663 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1664
1665 r = lyjson_ctx_next(jsonctx, &status);
1666 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1667 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1668
1669 /* create the object envelope */
1670 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), "", 0, NULL,
1671 LYD_NODE_OPAQ_ISENVELOPE, LYD_JSON, NULL, NULL, 0,
1672 module_key, ly_strlen(module_key), envp_p);
1673 LY_CHECK_GOTO(ret, cleanup);
1674
1675 if (parent) {
1676 lyd_insert_node(parent, NULL, *envp_p);
1677 }
1678
1679 ret = LY_SUCCESS;
1680cleanup:
1681 return ret;
1682}
1683
1684static LY_ERR
1685lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1686{
1687 enum LYJSON_PARSER_STATUS status;
1688
1689 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1690 if (status == LYJSON_END) {
1691 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1692 return LY_EVALID;
1693 } else if (status != LYJSON_OBJECT_CLOSED) {
1694 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
1695 jsonctx->value_len, jsonctx->value, object_id);
1696 return LY_EVALID;
1697 }
1698 return LY_SUCCESS;
1699}
1700
1701LY_ERR
1702lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1703{
1704 LY_ERR ret = LY_SUCCESS;
1705 struct lyd_json_ctx *lydctx = NULL;
1706 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1707 struct lyd_node *tree = NULL;
1708 enum LYJSON_PARSER_STATUS status;
1709
1710 /* init */
1711 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1712 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1713 lydctx->int_opts = LYD_INTOPT_RPC;
1714
1715 /* process envelope(s), if present */
1716
1717 /* process rpc */
1718 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1719 if (ret == LY_ENOT) {
1720 ret = LY_SUCCESS;
1721 goto parse_content;
1722 } else if (ret) {
1723 goto cleanup;
1724 }
1725 /* process action */
1726 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1727 if (ret == LY_ENOT) {
1728 ret = LY_SUCCESS;
1729 goto parse_content;
1730 } else if (ret) {
1731 goto cleanup;
1732 }
1733
1734parse_content:
1735
1736 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1737 assert(status == LYJSON_OBJECT);
1738
1739 /* read subtree(s) */
Michal Vaskocf770e22020-08-12 13:21:43 +02001740 ret = lydjson_subtree_r(lydctx, act_e ? (struct lyd_node_inner*)act_e : (struct lyd_node_inner*)rpc_e, &tree);
1741 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001742
1743 /* finish linking metadata */
1744 ret = lydjson_metadata_finish(lydctx, &tree);
1745 LY_CHECK_GOTO(ret, cleanup);
1746
1747 /* make sure we have parsed some operation */
1748 if (!lydctx->op_node) {
1749 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
1750 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
1751 ret = LY_EVALID;
1752 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001753 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1754 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1755 tree->schema->name);
1756 ret = LY_EVALID;
1757 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001758 }
1759
1760 if (act_e) {
1761 /* finish action envelope */
1762 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action");
1763 LY_CHECK_GOTO(ret, cleanup);
1764 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1765 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
1766 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1767 ret = LY_EVALID;
1768 goto cleanup;
1769 }
1770 }
1771 if (rpc_e) {
1772 /* finish rpc envelope */
1773 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc");
1774 LY_CHECK_GOTO(ret, cleanup);
1775 if (!act_e && lydctx->op_node->schema->nodetype != LYS_RPC) {
1776 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
1777 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1778 ret = LY_EVALID;
1779 goto cleanup;
1780 }
1781 }
1782
1783 if (op_p) {
1784 *op_p = lydctx->op_node;
1785 }
1786 assert(tree);
1787 if (tree_p) {
1788 if (rpc_e) {
1789 *tree_p = rpc_e;
1790 } else if (act_e) {
1791 *tree_p = act_e;
1792 } else {
1793 *tree_p = tree;
1794 }
1795 }
1796
1797cleanup:
1798 /* we have used parse_only flag */
1799 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1800
1801 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1802 if (ret) {
1803 lyd_free_all(tree);
1804 lyd_free_tree(act_e);
1805 lyd_free_tree(rpc_e);
1806 }
1807 return ret;
1808}
1809
1810LY_ERR
1811lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1812{
1813 LY_ERR ret = LY_SUCCESS;
1814 struct lyd_json_ctx *lydctx = NULL;
1815 struct lyd_node *rpcr_e = NULL, *iter, *req_op, *rep_op = NULL;
1816 enum LYJSON_PARSER_STATUS status;
1817
1818 /* init */
1819 ret = lyd_parse_json_init(LYD_NODE_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1820 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1821 lydctx->int_opts = LYD_INTOPT_REPLY;
1822
1823 /* find request OP */
1824 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1825 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1826 break;
1827 }
1828 LYD_TREE_DFS_END(request, req_op);
1829 }
1830 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1831 LOGERR(LYD_NODE_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
1832 ret = LY_EINVAL;
1833 goto cleanup;
1834 }
1835
1836 /* duplicate request OP with parents */
1837 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1838
1839 /* parse "rpc-reply", if any */
1840 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e);
1841 if (ret == LY_ENOT) {
1842 ret = LY_SUCCESS;
1843 } else if (ret) {
1844 goto cleanup;
1845 }
1846
1847 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1848 assert(status == LYJSON_OBJECT);
1849
1850 /* read subtree(s) */
1851 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1852 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner*)rep_op, lyd_node_children_p(rep_op));
1853 LY_CHECK_GOTO(ret, cleanup);
1854
1855 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1856 }
1857
1858 /* finish linking metadata */
1859 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1860 LY_CHECK_GOTO(ret, cleanup);
1861
1862 if (rpcr_e) {
1863 /* finish rpc-reply envelope */
1864 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply");
1865 LY_CHECK_GOTO(ret, cleanup);
1866 }
1867
1868 if (op_p) {
1869 *op_p = rep_op;
1870 }
1871 for (iter = rep_op; iter->parent; iter = (struct lyd_node *)iter->parent);
1872 if (tree_p) {
1873 *tree_p = iter;
1874 }
1875 if (rpcr_e) {
1876 /* connect to the operation */
1877 lyd_insert_node(rpcr_e, NULL, iter);
1878 if (tree_p) {
1879 *tree_p = rpcr_e;
1880 }
1881 }
1882
1883cleanup:
1884 /* we have used parse_only flag */
1885 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1886
1887 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1888 if (ret) {
1889 lyd_free_all(rep_op);
1890 lyd_free_tree(rpcr_e);
1891 }
1892 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001893}