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