blob: a7b7e54b0d60155bd29dd87528292b585576ae5f [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);
501 assert(snode->nodetype & (LYD_NODE_TERM | LYS_LIST));
502
503 if ((lydctx->parse_options & LYD_PARSE_OPAQ)) {
504 /* backup parser */
505 lyjson_ctx_backup(jsonctx);
506 status = lyjson_ctx_status(jsonctx, 0);
507
508 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed as an opaq node */
509 switch (snode->nodetype) {
510 case LYS_LEAFLIST:
511 case LYS_LEAF:
512 /* value may not be valid in which case we parse it as an opaque node */
513 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
514 if (ret) {
515 break;
516 }
517
518 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, lydjson_resolve_prefix, jsonctx, LYD_JSON)) {
519 ret = LY_ENOT;
520 }
521 break;
522 case LYS_LIST:
523 /* lists may not have all its keys */
524 if (lydjson_check_list(jsonctx, snode)) {
525 /* invalid list, parse as opaque if it missing/has invalid some keys */
526 ret = LY_ENOT;
527 }
528 }
529
530 /* restore parser */
531 lyjson_ctx_restore(jsonctx);
532 } else if (snode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
533 status = lyjson_ctx_status(jsonctx, 0);
534 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
535 }
536
537 return ret;
538}
539
540/**
541 * @brief Join the forward-referencing metadata with their target data nodes.
542 *
543 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
544 *
545 * @param[in] lydctx JSON data parser context.
546 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
547 * as a starting point to search for the metadata's target data node
548 * @return LY_SUCCESS on success
549 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
550 */
551static LY_ERR
552lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
553{
554 LY_ERR ret = LY_SUCCESS;
555 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
556 unsigned int instance = 0;
557 const char *prev = NULL;
558
559 /* finish linking metadata */
560 LY_LIST_FOR_SAFE(*first_p, next, attr) {
561 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq*)attr;
562 unsigned int match = 0;
563 int is_attr;
564 const char *name, *prefix;
565 size_t name_len, prefix_len;
566 const struct lysc_node *snode;
567
568 if (attr->schema || meta_container->name[0] != '@') {
569 /* not an opaq metadata node */
570 continue;
571 }
572
573 if (prev != meta_container->name) {
574 /* metas' names are stored in dictionary, so checking pointers must works */
575 lydict_remove(lydctx->jsonctx->ctx, prev);
576 prev = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0);
577 instance = 1;
578 } else {
579 instance++;
580 }
581
582 /* find the correspnding data node */
583 LY_LIST_FOR(start, node) {
584 if (!node->schema) {
585 /* opaq node - we are going to put into it just a generic attribute. */
586 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq*)node)->name)) {
587 continue;
588 }
589
590 if (((struct lyd_node_opaq*)node)->hint & LYD_NODE_OPAQ_ISLIST) {
591 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
592 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq*)node)->name);
593 ret = LY_EVALID;
594 goto cleanup;
595 }
596
597 /* match */
598 match++;
599 if (match != instance) {
600 continue;
601 }
602
603 LY_LIST_FOR(meta_container->child, meta_iter) {
604 /* convert opaq node to a attribute of the opaq node */
605 struct lyd_node_opaq *meta = (struct lyd_node_opaq*)meta_iter;
606 struct ly_prefix *val_prefs = NULL;
607 int dynamic = 0;
608
609 /* get value prefixes */
610 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
611
612 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
613 &dynamic, meta->hint, LYD_JSON, val_prefs, meta->prefix.id, ly_strlen(meta->prefix.id),
614 meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
615 LY_CHECK_GOTO(ret, cleanup);
616 }
617
618 /* done */
619 break;
620 } else {
621 /* this is the second time we are resolving the schema node, so it must succeed,
622 * but remember that snode can be still NULL */
623 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
624 assert(is_attr);
625 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
626 assert(ret == LY_SUCCESS);
627
628 if (snode != node->schema) {
629 continue;
630 }
631
632 /* match */
633 match++;
634 if (match != instance) {
635 continue;
636 }
637
638 LY_LIST_FOR(meta_container->child, meta_iter) {
639 /* convert opaq node to a metadata of the node */
640 struct lyd_node_opaq *meta = (struct lyd_node_opaq*)meta_iter;
641 struct lys_module *mod = NULL;
642 int dynamic = 0;
643
644 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
645 if (mod) {
646 ret = lyd_parser_create_meta((struct lyd_ctx*)lydctx, node, NULL, mod,
647 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
648 &dynamic, meta->hint, lydjson_resolve_prefix, NULL, LYD_JSON, snode);
649 LY_CHECK_GOTO(ret, cleanup);
650 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
651 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
652 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
653 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
654 ret = LY_EVALID;
655 goto cleanup;
656 }
657 }
658 /* add/correct flags */
659 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
660
661 /* done */
662 break;
663 }
664 }
665
666 if (match != instance) {
667 /* there is no corresponding data node for the metadata */
668 if (instance > 1) {
669 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
670 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
671 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
672 } else {
673 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
674 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
675 }
676 ret = LY_EVALID;
677 } else {
678 /* remove the opaq attr */
679 if (attr == (*first_p)) {
680 *first_p = attr->next;
681 }
682 lyd_free_tree(attr);
683 }
684 }
685
686cleanup:
687 lydict_remove(lydctx->jsonctx->ctx, prev);
688
689 return ret;
690}
691
692/**
693 * @brief Parse a metadata member.
694 *
695 * @param[in] lydctx JSON data parser context.
696 * @param[in] snode Schema node of the metadata parent.
697 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
698 * so the data node does not exists. In such a case the metadata is stored in the context for the later
699 * processing by lydjson_metadata_finish().
700 * @return LY_SUCCESS on success
701 * @return Various LY_ERR values in case of failure.
702 */
703static LY_ERR
704lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
705{
706 LY_ERR ret = LY_SUCCESS;
707 enum LYJSON_PARSER_STATUS status;
708 const char *expected;
709 int in_parent = 0;
710 const char *name, *prefix = NULL;
711 size_t name_len, prefix_len = 0;
712 struct lys_module *mod;
713 struct lyd_meta *meta = NULL;
714 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
715 int is_attr = 0;
716 struct lyd_node *prev = node;
717 int instance = 0;
718 uint16_t nodetype;
719
720 assert(snode || node);
721
722 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
723
724 /* move to the second item in the name/X pair */
725 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
726 LY_CHECK_GOTO(ret, cleanup);
727
728 /* check attribute encoding */
729 switch (nodetype) {
730 case LYS_LEAFLIST:
731 expected = "@name/array of objects/nulls";
732
733 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
734
735next_entry:
736 instance++;
737
738 /* move into array / next entry */
739 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
740 LY_CHECK_GOTO(ret, cleanup);
741
742 if (status == LYJSON_ARRAY_CLOSED) {
743 /* we are done, move after the array */
744 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
745 goto cleanup;
746 }
747 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
748
749 if (!node || node->schema != prev->schema) {
750 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
751 "Missing JSON data instance no. %d of %s:%s to be coupled with metadata.",
752 instance, prev->schema->module->name, prev->schema->name);
753 ret = LY_EVALID;
754 goto cleanup;
755 }
756
757 if (status == LYJSON_NULL) {
758 /* continue with the next entry in the leaf-list array */
759 prev = node;
760 node = node->next;
761 goto next_entry;
762 }
763 break;
764 case LYS_LEAF:
765 case LYS_ANYXML:
766 expected = "@name/object";
767
768 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
769 break;
770 case LYS_CONTAINER:
771 case LYS_LIST:
772 case LYS_ANYDATA:
773 case LYS_NOTIF:
774 case LYS_ACTION:
775 case LYS_RPC:
776 in_parent = 1;
777 expected = "@/object";
778 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
779 break;
780 }
781
782 /* process all the members inside a single metadata object */
783 assert(status == LYJSON_OBJECT);
784
785 while (status != LYJSON_OBJECT_CLOSED) {
786 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
787 if (!prefix) {
788 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
789 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
790 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
791 ret = LY_EVALID;
792 goto cleanup;
793 } else if (is_attr) {
794 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
795 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
796 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
797 ret = LY_EVALID;
798 goto cleanup;
799 }
800
801 /* get the element module */
802 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
803 if (!mod) {
804 if (lydctx->parse_options & LYD_PARSE_STRICT) {
805 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_REFERENCE,
806 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
807 prefix_len, prefix, name_len, name);
808 ret = LY_EVALID;
809 goto cleanup;
810 }
811 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
812 /* skip element with children */
813 ret = lydjson_data_skip(lydctx->jsonctx);
814 LY_CHECK_GOTO(ret, cleanup);
815 status = lyjson_ctx_status(lydctx->jsonctx, 0);
816 /* end of the item */
817 continue;
818 }
819 }
820
821 /* get the value */
822 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
823 LY_CHECK_GOTO(ret, cleanup);
824
825 if (node->schema) {
826 /* create metadata */
827 meta = NULL;
828 ret = lyd_parser_create_meta((struct lyd_ctx*)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
829 &lydctx->jsonctx->dynamic, 0, lydjson_resolve_prefix, lydctx->jsonctx, LYD_JSON, snode);
830 LY_CHECK_GOTO(ret, cleanup);
831
832 /* add/correct flags */
833 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
834 } else {
835 /* create attribute */
836 struct ly_prefix *val_prefs = NULL;
837 const char *module_name;
838 size_t module_name_len;
839
840 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
841
842 /* get value prefixes */
843 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
844
845 /* attr2 is always changed to the created attribute */
846 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
847 &lydctx->jsonctx->dynamic, 0, LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len);
848 LY_CHECK_GOTO(ret, cleanup);
849 }
850 /* next member */
851 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
852 LY_CHECK_GOTO(ret, cleanup);
853 }
854
855 if (nodetype == LYS_LEAFLIST) {
856 /* continue by processing another metadata object for the following
857 * leaf-list instance since they are allways instantiated in JSON array */
858 prev = node;
859 node = node->next;
860 goto next_entry;
861 }
862
863 /* move after the metadata */
864 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
865 LY_CHECK_GOTO(ret, cleanup);
866
867cleanup:
868 return ret;
869
870representation_error:
871 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
872 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
873 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq*)node)->name,
874 expected, lyjson_token2str(status), in_parent ? "" : "name");
875
876 return LY_EVALID;
877}
878
879/**
880 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing to the first child node.
881 *
882 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
883 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
884 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
885 */
886static void
887lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
888{
889 if (*node_p) {
890 /* insert, keep first pointer correct */
891 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
892 if (first_p) {
893 if (parent) {
894 *first_p = parent->child;
895 } else {
896 while ((*first_p)->prev->next) {
897 *first_p = (*first_p)->prev;
898 }
899 }
900 }
901 *node_p = NULL;
902 }
903}
904
905static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
906
907/**
908 * @brief Parse opaq node from the input.
909 *
910 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
911 *
912 * @param[in] lydctx JSON data parser context.
913 * @param[in] name Name of the opaq node to create.
914 * @param[in] name_len Length of the @p name string.
915 * @param[in] prefix Prefix of the opaq node to create.
916 * @param[in] prefix_len Length of the @p prefx string.
917 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
918 * but must be set if @p first is not.
919 * @param[in,out] status_p Pointer to the current status of the parser context,
920 * since the function manipulates with the context and process the input, the status can be updated.
921 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
922 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
923 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
924 * @param[out] node_p Pointer to the created opaq node.
925 * @return LY_ERR value.
926 */
927static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200928lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
929 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
930 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200931{
932 LY_ERR ret = LY_SUCCESS;
933 const char *value = NULL, *module_name;
934 size_t value_len = 0, module_name_len = 0;
935 struct ly_prefix *val_prefs = NULL;
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,
Michal Vaskoa5da3292020-08-12 13:10:50 +02001024 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1025 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001026{
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/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001075 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001076 *
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
Michal Vaskoa5da3292020-08-12 13:10:50 +02001370lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
1371 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001372{
1373 LY_ERR ret = LY_SUCCESS;
1374 struct lyd_json_ctx *lydctx;
1375 size_t i, line = 1;
1376
1377 /* starting top-level */
1378 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1379 if (in->current[i] == 0x0a) { /* new line */
1380 line++;
1381 };
1382 }
1383 if (in->current[i] == '\0') {
1384 /* empty data input */
1385 return LY_SUCCESS;
1386 }
1387 if (in->current[i] != '{') {
1388 LOGVAL(ctx, LY_VLOG_LINE, &line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(&in->current[i]), &in->current[i],
1389 "a top-level JSON object ('{') holding encoded YANG data");
1390 return LY_EVALID;
1391 }
1392
1393 /* init context */
1394 lydctx = calloc(1, sizeof *lydctx);
1395 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1396 lydctx->parse_options = parse_options;
1397 lydctx->validate_options = validate_options;
1398 lydctx->free = lyd_json_ctx_free;
1399 lydctx->resolve_prefix = lydjson_resolve_prefix;
1400
1401 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1402
1403 *lydctx_p = lydctx;
1404 return LY_SUCCESS;
1405}
1406
1407LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001408lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
1409 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001410{
1411 LY_ERR ret = LY_SUCCESS;
1412 struct lyd_json_ctx *lydctx = NULL;
1413 enum LYJSON_PARSER_STATUS status;
1414
1415 assert(tree_p);
1416 *tree_p = NULL;
1417
1418 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx);
1419 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1420
1421 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1422 assert(status == LYJSON_OBJECT);
1423
1424 /* read subtree(s) */
1425 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1426 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1427 LY_CHECK_GOTO(ret, cleanup);
1428
1429 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1430 }
1431
1432 /* finish linking metadata */
1433 ret = lydjson_metadata_finish(lydctx, tree_p);
1434 LY_CHECK_GOTO(ret, cleanup);
1435
1436cleanup:
1437 /* there should be no unresolved types stored */
1438 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count
1439 && !lydctx->when_check.count));
1440
1441 if (ret) {
1442 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1443 lyd_free_all(*tree_p);
1444 *tree_p = NULL;
1445 } else {
1446 *lydctx_p = (struct lyd_ctx *)lydctx;
1447 }
1448
1449 return ret;
1450}
1451
1452/**
1453 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1454 *
1455 * @param[in] jsonctx JSON parser context
1456 * @param[out] envp_p Pointer to the created envelope opaq container.
1457 * @return LY_SUCCESS if the envelope present and successfully parsed.
1458 * @return LY_ENOT in case there is not the expected envelope.
1459 * @return LY_ERR in case of parsing failure.
1460 */
1461static LY_ERR
1462lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p)
1463{
1464 LY_ERR ret = LY_ENOT, r;
1465 const char *name, *prefix, *value = NULL;
1466 size_t name_len, prefix_len, value_len;
1467 int is_attr, dynamic = 0;
1468 enum LYJSON_PARSER_STATUS status;
1469 struct lyd_node *et;
1470
1471 *envp_p = NULL;
1472
1473 /* backup the context */
1474 lyjson_ctx_backup(jsonctx);
1475
1476 /* "notification" envelope */
1477 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1478 LY_CHECK_GOTO(is_attr, cleanup);
1479 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1480 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1481
1482 r = lyjson_ctx_next(jsonctx, &status);
1483 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1484 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1485
1486 /* "eventTime" child */
1487 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1488 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1489 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1490
1491 /* go for the data */
1492 r = lyjson_ctx_next(jsonctx, &status);
1493 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1494 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1495
1496 value = jsonctx->value;
1497 value_len = jsonctx->value_len;
1498 dynamic = jsonctx->dynamic;
1499 jsonctx->dynamic = 0;
1500
1501 r = lyjson_ctx_next(jsonctx, &status);
1502 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1503 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1504 /* now the notificationContent is expected, which will be parsed by the caller */
1505
1506 /* create notification envelope */
1507 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LYD_NODE_OPAQ_ISENVELOPE,
1508 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, envp_p);
1509 LY_CHECK_GOTO(ret, cleanup);
1510 /* create notification envelope */
1511 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", 9, value, value_len, &dynamic, LYD_NODE_OPAQ_ISSTRING,
1512 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, &et);
1513 LY_CHECK_GOTO(ret, cleanup);
1514 /* insert eventTime into notification */
1515 lyd_insert_node(*envp_p, NULL, et);
1516
1517 ret = LY_SUCCESS;
1518cleanup:
1519 if (ret) {
1520 /* restore the context */
1521 lyjson_ctx_restore(jsonctx);
1522 if (dynamic) {
1523 free((char*)value);
1524 }
1525 }
1526 return ret;
1527}
1528
1529LY_ERR
1530lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1531{
1532 LY_ERR ret = LY_SUCCESS;
1533 struct lyd_json_ctx *lydctx = NULL;
1534 struct lyd_node *ntf_e = NULL;
1535 struct lyd_node *tree = NULL;
1536 enum LYJSON_PARSER_STATUS status;
1537
1538 /* init */
1539 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1540 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1541 lydctx->int_opts = LYD_INTOPT_NOTIF;
1542
1543 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1544 assert(status == LYJSON_OBJECT);
1545
1546 /* parse "notification" and "eventTime", if present */
1547 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e);
1548 if (ret == LY_ENOT) {
1549 ret = LY_SUCCESS;
1550 } else if (ret) {
1551 goto cleanup;
1552 }
1553
1554 /* read subtree(s) */
1555 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1556 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1557 LY_CHECK_GOTO(ret, cleanup);
1558
1559 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1560 }
1561
1562 /* finish linking metadata */
1563 ret = lydjson_metadata_finish(lydctx, &tree);
1564 LY_CHECK_GOTO(ret, cleanup);
1565
1566 /* make sure we have parsed some notification */
1567 if (!lydctx->op_node) {
1568 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1569 ret = LY_EVALID;
1570 goto cleanup;
1571 }
1572
1573 if (ntf_e) {
1574 /* finish notification envelope */
1575 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1576 LY_CHECK_GOTO(ret, cleanup);
1577 if (status == LYJSON_END) {
1578 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1579 ret = LY_EVALID;
1580 goto cleanup;
1581 } else if (status != LYJSON_OBJECT_CLOSED) {
1582 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
1583 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
1584 ret = LY_EVALID;
1585 goto cleanup;
1586 }
1587 }
1588
1589 if (ntf_p) {
1590 *ntf_p = lydctx->op_node;
1591 }
1592 assert(tree);
1593 if (tree_p) {
1594 *tree_p = tree;
1595 }
1596 if (ntf_e) {
1597 /* connect to the notification */
1598 lyd_insert_node(ntf_e, NULL, *tree_p);
1599 if (tree_p) {
1600 *tree_p = ntf_e;
1601 }
1602 }
1603
1604cleanup:
1605 /* we have used parse_only flag */
1606 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1607
1608 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1609 if (ret) {
1610 lyd_free_all(tree);
1611 lyd_free_tree(ntf_e);
1612 }
1613 return ret;
1614}
1615
1616/**
1617 * @brief Parse optional JSON envelope around the processed content.
1618 *
1619 * @param[in] jsonctx JSON parser context
1620 * @param[in] parent Parent node (some other envelope).
1621 * @param[in] module_key Name of the module where the envelope element is expected.
1622 * @param[in] object_id Name of the envelope object.
1623 * @param[out] envp_p Pointer to the created envelope opaq container.
1624 * @return LY_SUCCESS if the envelope present and successfully parsed.
1625 * @return LY_ENOT in case there is not the expected envelope.
1626 * @return LY_ERR in case of parsing failure.
1627 */
1628static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001629lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
1630 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001631{
1632 LY_ERR ret = LY_ENOT, r;
1633 const char *name, *prefix;
1634 size_t name_len, prefix_len;
1635 int is_attr;
1636 enum LYJSON_PARSER_STATUS status;
1637
1638 *envp_p = NULL;
1639
1640 /* "id" envelope */
1641 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1642 LY_CHECK_GOTO(is_attr, cleanup);
1643 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1644 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1645 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1646
1647 r = lyjson_ctx_next(jsonctx, &status);
1648 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1649 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1650
1651 /* create the object envelope */
1652 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), "", 0, NULL,
1653 LYD_NODE_OPAQ_ISENVELOPE, LYD_JSON, NULL, NULL, 0,
1654 module_key, ly_strlen(module_key), envp_p);
1655 LY_CHECK_GOTO(ret, cleanup);
1656
1657 if (parent) {
1658 lyd_insert_node(parent, NULL, *envp_p);
1659 }
1660
1661 ret = LY_SUCCESS;
1662cleanup:
1663 return ret;
1664}
1665
1666static LY_ERR
1667lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1668{
1669 enum LYJSON_PARSER_STATUS status;
1670
1671 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1672 if (status == LYJSON_END) {
1673 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1674 return LY_EVALID;
1675 } else if (status != LYJSON_OBJECT_CLOSED) {
1676 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
1677 jsonctx->value_len, jsonctx->value, object_id);
1678 return LY_EVALID;
1679 }
1680 return LY_SUCCESS;
1681}
1682
1683LY_ERR
1684lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1685{
1686 LY_ERR ret = LY_SUCCESS;
1687 struct lyd_json_ctx *lydctx = NULL;
1688 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1689 struct lyd_node *tree = NULL;
1690 enum LYJSON_PARSER_STATUS status;
1691
1692 /* init */
1693 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1694 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1695 lydctx->int_opts = LYD_INTOPT_RPC;
1696
1697 /* process envelope(s), if present */
1698
1699 /* process rpc */
1700 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1701 if (ret == LY_ENOT) {
1702 ret = LY_SUCCESS;
1703 goto parse_content;
1704 } else if (ret) {
1705 goto cleanup;
1706 }
1707 /* process action */
1708 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1709 if (ret == LY_ENOT) {
1710 ret = LY_SUCCESS;
1711 goto parse_content;
1712 } else if (ret) {
1713 goto cleanup;
1714 }
1715
1716parse_content:
1717
1718 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1719 assert(status == LYJSON_OBJECT);
1720
1721 /* read subtree(s) */
1722 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1723 ret = lydjson_subtree_r(lydctx, act_e ? (struct lyd_node_inner*)act_e : (struct lyd_node_inner*)rpc_e, &tree);
1724 LY_CHECK_GOTO(ret, cleanup);
1725
1726 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1727 }
1728
1729 /* finish linking metadata */
1730 ret = lydjson_metadata_finish(lydctx, &tree);
1731 LY_CHECK_GOTO(ret, cleanup);
1732
1733 /* make sure we have parsed some operation */
1734 if (!lydctx->op_node) {
1735 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
1736 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
1737 ret = LY_EVALID;
1738 goto cleanup;
1739 }
1740
1741 if (act_e) {
1742 /* finish action envelope */
1743 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action");
1744 LY_CHECK_GOTO(ret, cleanup);
1745 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1746 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
1747 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1748 ret = LY_EVALID;
1749 goto cleanup;
1750 }
1751 }
1752 if (rpc_e) {
1753 /* finish rpc envelope */
1754 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc");
1755 LY_CHECK_GOTO(ret, cleanup);
1756 if (!act_e && lydctx->op_node->schema->nodetype != LYS_RPC) {
1757 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
1758 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1759 ret = LY_EVALID;
1760 goto cleanup;
1761 }
1762 }
1763
1764 if (op_p) {
1765 *op_p = lydctx->op_node;
1766 }
1767 assert(tree);
1768 if (tree_p) {
1769 if (rpc_e) {
1770 *tree_p = rpc_e;
1771 } else if (act_e) {
1772 *tree_p = act_e;
1773 } else {
1774 *tree_p = tree;
1775 }
1776 }
1777
1778cleanup:
1779 /* we have used parse_only flag */
1780 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1781
1782 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1783 if (ret) {
1784 lyd_free_all(tree);
1785 lyd_free_tree(act_e);
1786 lyd_free_tree(rpc_e);
1787 }
1788 return ret;
1789}
1790
1791LY_ERR
1792lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1793{
1794 LY_ERR ret = LY_SUCCESS;
1795 struct lyd_json_ctx *lydctx = NULL;
1796 struct lyd_node *rpcr_e = NULL, *iter, *req_op, *rep_op = NULL;
1797 enum LYJSON_PARSER_STATUS status;
1798
1799 /* init */
1800 ret = lyd_parse_json_init(LYD_NODE_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1801 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1802 lydctx->int_opts = LYD_INTOPT_REPLY;
1803
1804 /* find request OP */
1805 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1806 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1807 break;
1808 }
1809 LYD_TREE_DFS_END(request, req_op);
1810 }
1811 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
1812 LOGERR(LYD_NODE_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
1813 ret = LY_EINVAL;
1814 goto cleanup;
1815 }
1816
1817 /* duplicate request OP with parents */
1818 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1819
1820 /* parse "rpc-reply", if any */
1821 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e);
1822 if (ret == LY_ENOT) {
1823 ret = LY_SUCCESS;
1824 } else if (ret) {
1825 goto cleanup;
1826 }
1827
1828 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1829 assert(status == LYJSON_OBJECT);
1830
1831 /* read subtree(s) */
1832 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1833 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner*)rep_op, lyd_node_children_p(rep_op));
1834 LY_CHECK_GOTO(ret, cleanup);
1835
1836 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1837 }
1838
1839 /* finish linking metadata */
1840 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1841 LY_CHECK_GOTO(ret, cleanup);
1842
1843 if (rpcr_e) {
1844 /* finish rpc-reply envelope */
1845 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply");
1846 LY_CHECK_GOTO(ret, cleanup);
1847 }
1848
1849 if (op_p) {
1850 *op_p = rep_op;
1851 }
1852 for (iter = rep_op; iter->parent; iter = (struct lyd_node *)iter->parent);
1853 if (tree_p) {
1854 *tree_p = iter;
1855 }
1856 if (rpcr_e) {
1857 /* connect to the operation */
1858 lyd_insert_node(rpcr_e, NULL, iter);
1859 if (tree_p) {
1860 *tree_p = rpcr_e;
1861 }
1862 }
1863
1864cleanup:
1865 /* we have used parse_only flag */
1866 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1867
1868 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1869 if (ret) {
1870 lyd_free_all(rep_op);
1871 lyd_free_tree(rpcr_e);
1872 }
1873 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001874}