blob: 86102b5b6b8a6f913f41792043e3f1d85cc49bb1 [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON data parser for libyang
5 *
Radek Krejci1798aae2020-07-14 13:26:06 +02006 * Copyright (c) 2020 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include <assert.h>
Michal Vasko90932a92020-02-12 14:33:03 +010018#include <stdlib.h>
19#include <string.h>
20
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020022#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010023#include "context.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020024#include "json.h"
25#include "parser_internal.h"
26#include "tree_data.h"
27#include "tree_data_internal.h"
28#include "tree_schema.h"
29#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010030
31/**
Radek Krejci1798aae2020-07-14 13:26:06 +020032 * @brief Internal context for JSON YANG data parser.
33 *
34 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
35 */
36struct lyd_json_ctx {
37 uint32_t parse_options; /**< various @ref dataparseroptions. */
38 uint32_t validate_options; /**< various @ref datavalidationoptions. */
39 uint32_t int_opts; /**< internal data parser options */
40 uint32_t path_len; /**< used bytes in the path buffer */
41 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
42 struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
43 struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
44 struct ly_set when_check; /**< set of nodes with "when" conditions */
45 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
46
47 /* callbacks */
48 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020049
50 struct lyjson_ctx *jsonctx; /**< JSON context */
51};
52
53/**
54 * @brief Free the JSON data parser context.
55 *
56 * JSON implementation of lyd_ctx_free_clb().
57 */
58static void
59lyd_json_ctx_free(struct lyd_ctx *lydctx)
60{
61 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
62
63 if (lydctx) {
64 lyd_ctx_free(lydctx);
65 lyjson_ctx_free(ctx->jsonctx);
66 free(ctx);
67 }
68}
69
70/**
Radek Krejci1798aae2020-07-14 13:26:06 +020071 * @brief Parse JSON member-name as [\@][prefix:][name]
72 *
Michal Vaskocabe0702020-08-12 10:14:36 +020073 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020074 * prefix - name of the module of the data node
75 * name - name of the data node
76 *
77 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
78 *
79 * @param[in] value String to parse
80 * @param[in] value_len Length of the @p str.
81 * @param[out] name_p Pointer to the beginning of the parsed name.
82 * @param[out] name_len_p Pointer to the length of the parsed name.
83 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
84 * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0.
Michal Vaskocabe0702020-08-12 10:14:36 +020085 * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise.
Radek Krejci1798aae2020-07-14 13:26:06 +020086 */
87static void
Michal Vaskocabe0702020-08-12 10:14:36 +020088lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p,
89 size_t *prefix_len_p, int *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020090{
91 const char *name, *prefix = NULL;
92 size_t name_len, prefix_len = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +020093 int is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020094
95 name = memchr(value, ':', value_len);
96 if (name != NULL) {
97 prefix = value;
98 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +020099 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200100 prefix++;
101 }
102 prefix_len = name - prefix;
103 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200104 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200105 } else {
106 name = value;
107 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200108 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200109 name++;
110 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200111 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100112 }
113
Radek Krejci1798aae2020-07-14 13:26:06 +0200114 *name_p = name;
115 *name_len_p = name_len;
116 *prefix_p = prefix;
117 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200118 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200119}
120
121/**
122 * @brief Get correct prefix (module_name) inside the @p node.
123 *
124 * @param[in] node Data node to get inherited prefix.
125 * @param[in] local_prefix Local prefix to replace the inherited prefix.
126 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
127 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
128 * and no context @p node to get inherited prefix.
129 * @param[out] prefix_len_p Pointer to the length of the resulting @p prefix_p string. Note that the result can be 0 in case
130 * of no local prefix and no context @p node to get inherited prefix.
131 * @return LY_ERR value.
132 */
133static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200134lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p,
135 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200136{
137 struct lyd_node_opaq *onode;
138 const char *module_name = NULL;
139
140 assert(prefix_p && prefix_len_p);
141
142 if (local_prefix_len) {
143 *prefix_p = local_prefix;
144 *prefix_len_p = local_prefix_len;
145 return LY_SUCCESS;
146 }
147
148 *prefix_p = NULL;
149 while (node) {
150 if (node->schema) {
151 *prefix_p = node->schema->module->name;
152 break;
153 }
154 onode = (struct lyd_node_opaq*)node;
155 if (onode->prefix.module_name) {
156 *prefix_p = onode->prefix.module_name;
157 break;
158 } else if (onode->prefix.id) {
159 *prefix_p = onode->prefix.id;
160 break;
161 }
162 node = (struct lyd_node*)node->parent;
163 }
164 *prefix_len_p = ly_strlen(module_name);
165
166 return LY_SUCCESS;
167}
168
169/**
170 * @brief Get schema node corresponding to the input parameters.
171 *
172 * @param[in] lydctx JSON data parser context.
173 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
174 * @param[in] prefix Requested node's prefix (module name).
175 * @param[in] prefix_len Length of the @p prefix.
176 * @param[in] name Requested node's name.
177 * @param[in] name_len Length of the @p name.
178 * @param[in] parent Parent of the node beeing processed, can be NULL in case of top-level. Also the opaq node with LYD_NODE_OPAQ_ISENVELOPE hint
179 * is accepted for searching top-level nodes.
180 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
181 * @return LY_SUCCES on success, note that even in this case the returned value of @p snode_p can be NULL, so the data are expected to be parsed as opaq.
182 * @return LY_EVALID on failure, error message is logged
183 * @return LY_ENOT in case the input data are expected to be skipped
184 */
185static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200186lydjson_get_snode(const struct lyd_json_ctx *lydctx, int is_attr, const char *prefix, size_t prefix_len, const char *name,
187 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200188{
189 struct lys_module *mod = NULL;
190
191 /* leave if-feature check for validation */
192 int getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
193
194 /* init return value */
195 *snode_p = NULL;
196
197 /* get the element module */
198 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq*)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE))) {
199 if (!prefix_len) {
200 /* opaq parent (envelope) - the second part of the condition */
201 lydjson_get_node_prefix((struct lyd_node*)parent, NULL, 0, &prefix, &prefix_len);
202 }
203 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
204 } else if (parent) {
205 if (parent->schema) {
206 mod = parent->schema->module;
207 }
208 } else {
209 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
210 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
211 return LY_EVALID;
212 }
213 if (!mod) {
214 if (lydctx->parse_options & LYD_PARSE_STRICT) {
215 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
216 return LY_EVALID;
217 }
218 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
219 return LY_ENOT;
220 }
221 }
222
223 if (parent && !parent->schema && (((struct lyd_node_opaq*)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE)) {
224 /* ignore the envelope parent when searchinf for the schema node */
225 parent = NULL;
226 }
227
228 /* get the schema node */
229 if (mod && (!parent || parent->schema)) {
230 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
231 if (!*snode_p) {
232 if (lydctx->parse_options & LYD_PARSE_STRICT) {
233 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
234 name_len, name, mod->name);
235 return LY_EVALID;
236 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
237 /* skip element with children */
238 return LY_ENOT;
239 }
240 } else {
241 /* check that schema node is valid and can be used */
242 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
243 }
244 }
245
246 return LY_SUCCESS;
247}
248
249/**
250 * @brief Skip the currently open JSON object/array
251 * @param[in] jsonctx JSON context with the input data to skip.
252 * @return LY_ERR value.
253 */
254static LY_ERR
255lydjson_data_skip(struct lyjson_ctx *jsonctx)
256{
257 enum LYJSON_PARSER_STATUS status, current;
258 size_t sublevels = 1;
259
260 status = lyjson_ctx_status(jsonctx, 0);
261
262 /* skip after the content */
263 do {
264 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
265 if (current == status) {
266 sublevels++;
267 } else if (current == status + 1) {
268 sublevels--;
269 }
270 } while (current != status + 1 && sublevels);
271 /* open the next sibling */
272 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
273
274 return LY_SUCCESS;
275}
276
277/**
278 * @brief Go through the @p value and find all possible prefixes and store them in @p val_prefs_p [sized array](@ref sizedarrays).
279 *
280 * @param[in] ctx libyang context
281 * @param[in] value Pointer to the beginning of the value to check.
282 * @param[in] value_len Length of the string to examine in @p value.
283 * @param[out] val_prefs_p Pointer to the resulting [sized array](@ref sizedarrays) of found prefixes. NULL in case there are no prefixes.
284 * @return LY_EMEM on memory allocation failure.
285 * @return LY_SUCCESS on success, empty @p val_prefs_p (NULL) is valid result if there are no possible prefixes
286 */
287static LY_ERR
288lydjson_get_value_prefixes(const struct ly_ctx *ctx, const char *value, size_t value_len, struct ly_prefix **val_prefs_p)
289{
290 LY_ERR ret;
291 LY_ARRAY_COUNT_TYPE u;
292 uint32_t c;
293 const char *start, *stop;
294 struct ly_prefix *prefixes = NULL;
295 size_t len;
296
297 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
298 size_t bytes;
299 ly_getutf8(&stop, &c, &bytes);
300 if (is_yangidentstartchar(c)) {
301 for (ly_getutf8(&stop, &c, &bytes);
302 is_yangidentchar(c) && (size_t)(stop - value) < value_len;
303 ly_getutf8(&stop, &c, &bytes));
304 stop = stop - bytes;
305 if (*stop == ':') {
306 /* we have a possible prefix */
307 struct ly_prefix *p = NULL;
308
309 len = stop - start;
310
311 /* check whether we do not already have this prefix stored */
312 LY_ARRAY_FOR(prefixes, u) {
313 if (!ly_strncmp(prefixes[u].id, start, len)) {
314 p = &prefixes[u];
315 break;
316 }
317 }
318 if (!p) {
319 LY_ARRAY_NEW_GOTO(ctx, prefixes, p, ret, error);
320 p->id = lydict_insert(ctx, start, len);
321 p->module_name = lydict_insert(ctx, start, len);
322 } /* else the prefix already present */
323 }
324 stop = stop + bytes;
325 }
326 }
327
328 *val_prefs_p = prefixes;
329 return LY_SUCCESS;
330
331error:
332 LY_ARRAY_FOR(prefixes, u) {
333 lydict_remove(ctx, prefixes[u].id);
334 lydict_remove(ctx, prefixes[u].module_name);
335 }
336 LY_ARRAY_FREE(prefixes);
337 return ret;
338}
339
340/**
341 * @brief Check that the input data are parseable as the @p list.
342 *
343 * Checks for all the list's keys. Function does not revert the context state.
344 *
345 * @param[in] jsonctx JSON parser context.
346 * @param[in] list List schema node corresponding to the input data object.
347 * @return LY_SUCCESS in case the data are ok for the @p list
348 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
349 */
350static LY_ERR
351lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
352{
353 LY_ERR ret = LY_SUCCESS;
354 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
355 struct ly_set key_set = {0};
356 const struct lysc_node *snode;
357 uint32_t i, status_count;
358
359 assert(list && (list->nodetype == LYS_LIST));
360 assert(status == LYJSON_OBJECT);
361
362 /* get all keys into a set (keys do not have if-features or anything) */
363 snode = NULL;
364 while ((snode = lys_getnext(snode, list, NULL, LYS_GETNEXT_NOSTATECHECK)) && (snode->flags & LYS_KEY)) {
365 ly_set_add(&key_set, (void *)snode, LY_SET_OPT_USEASLIST);
366 }
367
368 if (status != LYJSON_OBJECT_EMPTY) {
369 status_count = jsonctx->status.count;
370
371 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
372 const char *name, *prefix;
373 size_t name_len, prefix_len;
374 int is_attr;
375
376 /* match the key */
377 snode = NULL;
378 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
379
380 if (!is_attr && !prefix) {
381 for (i = 0; i < key_set.count; ++i) {
382 snode = (const struct lysc_node *)key_set.objs[i];
383 if (!ly_strncmp(snode->name, name, name_len)) {
384 break;
385 }
386 }
387 /* go into the item to a) process it as a key or b) start skipping it as another list child */
388 ret = lyjson_ctx_next(jsonctx, &status);
389 LY_CHECK_GOTO(ret, cleanup);
390
391 if (snode) {
392 /* we have the key, validate the value */
393 if (status < LYJSON_NUMBER) {
394 /* not a terminal */
395 ret = LY_ENOT;
396 goto cleanup;
397 }
398
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200399 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 LY_CHECK_GOTO(ret, cleanup);
401
402 /* key with a valid value, remove from the set */
403 ly_set_rm_index(&key_set, i, NULL);
404 }
405 } else {
406 /* start skipping the member we are not interested in */
407 ret = lyjson_ctx_next(jsonctx, &status);
408 LY_CHECK_GOTO(ret, cleanup);
409 }
410 /* move to the next child */
411 while (status_count < jsonctx->status.count) {
412 ret = lyjson_ctx_next(jsonctx, &status);
413 LY_CHECK_GOTO(ret, cleanup);
414 }
415 }
416 }
417
418 if (key_set.count) {
419 /* some keys are missing/did not validate */
420 ret = LY_ENOT;
421 }
422
423cleanup:
424 ly_set_erase(&key_set, NULL);
425 return ret;
426}
427
428/**
429 * @brief Get the hint for the data type parsers according to the current JSON parser context.
430 *
431 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
432 * @param[in,out] status Pointer to the current context status,
433 * in some circumstances the function manipulates with the context so the status is updated.
434 * @param[out] type_hint_p Pointer to the variable to store the result.
435 * @return LY_SUCCESS in case of success.
436 * @return LY_EINVAL in case of invalid context status not referring to a value.
437 */
438static LY_ERR
439lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, int *type_hint_p)
440{
441 *type_hint_p = 0;
442
443 if (*status_p == LYJSON_ARRAY) {
444 /* only [null] */
445 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
446 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
447
448 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
449 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
450
451 *type_hint_p = LYD_NODE_OPAQ_ISEMPTY;
452 } else if (*status_p == LYJSON_STRING) {
453 *type_hint_p = LYD_NODE_OPAQ_ISSTRING;
454 } else if (*status_p == LYJSON_NUMBER) {
455 *type_hint_p = LYD_NODE_OPAQ_ISNUMBER;
456 } else if (*status_p == LYJSON_FALSE || *status_p == LYJSON_TRUE) {
457 *type_hint_p = LYD_NODE_OPAQ_ISBOOLEAN;
458 } else if (*status_p == LYJSON_NULL) {
459 *type_hint_p = 0;
460 } else {
461 return LY_EINVAL;
462 }
463
464 return LY_SUCCESS;
465}
466
467/**
468 * @brief Check in advance if the input data are parsable according to the provided @p snode.
469 *
470 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
471 * is naturally done when the data are really parsed.
472 *
473 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
474 * as before calling, despite it is necessary to process input data for checking.
475 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
476 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
477 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
478 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
479 * @return LY_EINVAL in case of invalid leaf JSON encoding
480 * and they are expected to be parsed as opaq nodes.
481 */
482static LY_ERR
483lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, int *type_hint_p)
484{
485 LY_ERR ret = LY_SUCCESS;
486 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
487 enum LYJSON_PARSER_STATUS status;
488
489 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200490
Michal Vasko32ac9942020-08-12 14:35:12 +0200491 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
492 /* can always be parsed as a data node if we have the schema node */
493 return LY_SUCCESS;
494 }
495
496 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200497 /* backup parser */
498 lyjson_ctx_backup(jsonctx);
499 status = lyjson_ctx_status(jsonctx, 0);
500
Michal Vasko32ac9942020-08-12 14:35:12 +0200501 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
502 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200503 switch (snode->nodetype) {
504 case LYS_LEAFLIST:
505 case LYS_LEAF:
506 /* value may not be valid in which case we parse it as an opaque node */
507 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
508 if (ret) {
509 break;
510 }
511
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200512 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200513 ret = LY_ENOT;
514 }
515 break;
516 case LYS_LIST:
517 /* lists may not have all its keys */
518 if (lydjson_check_list(jsonctx, snode)) {
519 /* invalid list, parse as opaque if it missing/has invalid some keys */
520 ret = LY_ENOT;
521 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200522 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200523 }
524
525 /* restore parser */
526 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200527 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200528 status = lyjson_ctx_status(jsonctx, 0);
529 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
530 }
531
532 return ret;
533}
534
535/**
536 * @brief Join the forward-referencing metadata with their target data nodes.
537 *
538 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
539 *
540 * @param[in] lydctx JSON data parser context.
541 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
542 * as a starting point to search for the metadata's target data node
543 * @return LY_SUCCESS on success
544 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
545 */
546static LY_ERR
547lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
548{
549 LY_ERR ret = LY_SUCCESS;
550 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
551 unsigned int instance = 0;
552 const char *prev = NULL;
553
554 /* finish linking metadata */
555 LY_LIST_FOR_SAFE(*first_p, next, attr) {
556 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq*)attr;
557 unsigned int match = 0;
558 int is_attr;
559 const char *name, *prefix;
560 size_t name_len, prefix_len;
561 const struct lysc_node *snode;
562
563 if (attr->schema || meta_container->name[0] != '@') {
564 /* not an opaq metadata node */
565 continue;
566 }
567
568 if (prev != meta_container->name) {
569 /* metas' names are stored in dictionary, so checking pointers must works */
570 lydict_remove(lydctx->jsonctx->ctx, prev);
571 prev = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0);
572 instance = 1;
573 } else {
574 instance++;
575 }
576
577 /* find the correspnding data node */
578 LY_LIST_FOR(start, node) {
579 if (!node->schema) {
580 /* opaq node - we are going to put into it just a generic attribute. */
581 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq*)node)->name)) {
582 continue;
583 }
584
585 if (((struct lyd_node_opaq*)node)->hint & LYD_NODE_OPAQ_ISLIST) {
586 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
587 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq*)node)->name);
588 ret = LY_EVALID;
589 goto cleanup;
590 }
591
592 /* match */
593 match++;
594 if (match != instance) {
595 continue;
596 }
597
598 LY_LIST_FOR(meta_container->child, meta_iter) {
599 /* convert opaq node to a attribute of the opaq node */
600 struct lyd_node_opaq *meta = (struct lyd_node_opaq*)meta_iter;
601 struct ly_prefix *val_prefs = NULL;
602 int dynamic = 0;
603
604 /* get value prefixes */
605 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
606
607 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
608 &dynamic, meta->hint, LYD_JSON, val_prefs, meta->prefix.id, ly_strlen(meta->prefix.id),
609 meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
610 LY_CHECK_GOTO(ret, cleanup);
611 }
612
613 /* done */
614 break;
615 } else {
616 /* this is the second time we are resolving the schema node, so it must succeed,
617 * but remember that snode can be still NULL */
618 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
619 assert(is_attr);
620 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
621 assert(ret == LY_SUCCESS);
622
623 if (snode != node->schema) {
624 continue;
625 }
626
627 /* match */
628 match++;
629 if (match != instance) {
630 continue;
631 }
632
633 LY_LIST_FOR(meta_container->child, meta_iter) {
634 /* convert opaq node to a metadata of the node */
635 struct lyd_node_opaq *meta = (struct lyd_node_opaq*)meta_iter;
636 struct lys_module *mod = NULL;
637 int dynamic = 0;
638
639 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
640 if (mod) {
641 ret = lyd_parser_create_meta((struct lyd_ctx*)lydctx, node, NULL, mod,
642 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200643 &dynamic, meta->hint, LY_PREF_JSON, NULL, snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200644 LY_CHECK_GOTO(ret, cleanup);
645 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
646 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
647 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
648 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
649 ret = LY_EVALID;
650 goto cleanup;
651 }
652 }
653 /* add/correct flags */
654 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
655
656 /* done */
657 break;
658 }
659 }
660
661 if (match != instance) {
662 /* there is no corresponding data node for the metadata */
663 if (instance > 1) {
664 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
665 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
666 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
667 } else {
668 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
669 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
670 }
671 ret = LY_EVALID;
672 } else {
673 /* remove the opaq attr */
674 if (attr == (*first_p)) {
675 *first_p = attr->next;
676 }
677 lyd_free_tree(attr);
678 }
679 }
680
681cleanup:
682 lydict_remove(lydctx->jsonctx->ctx, prev);
683
684 return ret;
685}
686
687/**
688 * @brief Parse a metadata member.
689 *
690 * @param[in] lydctx JSON data parser context.
691 * @param[in] snode Schema node of the metadata parent.
692 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
693 * so the data node does not exists. In such a case the metadata is stored in the context for the later
694 * processing by lydjson_metadata_finish().
695 * @return LY_SUCCESS on success
696 * @return Various LY_ERR values in case of failure.
697 */
698static LY_ERR
699lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
700{
701 LY_ERR ret = LY_SUCCESS;
702 enum LYJSON_PARSER_STATUS status;
703 const char *expected;
704 int in_parent = 0;
705 const char *name, *prefix = NULL;
706 size_t name_len, prefix_len = 0;
707 struct lys_module *mod;
708 struct lyd_meta *meta = NULL;
709 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
710 int is_attr = 0;
711 struct lyd_node *prev = node;
712 int instance = 0;
713 uint16_t nodetype;
714
715 assert(snode || node);
716
717 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
718
719 /* move to the second item in the name/X pair */
720 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
721 LY_CHECK_GOTO(ret, cleanup);
722
723 /* check attribute encoding */
724 switch (nodetype) {
725 case LYS_LEAFLIST:
726 expected = "@name/array of objects/nulls";
727
728 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
729
730next_entry:
731 instance++;
732
733 /* move into array / next entry */
734 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
735 LY_CHECK_GOTO(ret, cleanup);
736
737 if (status == LYJSON_ARRAY_CLOSED) {
738 /* we are done, move after the array */
739 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
740 goto cleanup;
741 }
742 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
743
744 if (!node || node->schema != prev->schema) {
745 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
746 "Missing JSON data instance no. %d of %s:%s to be coupled with metadata.",
747 instance, prev->schema->module->name, prev->schema->name);
748 ret = LY_EVALID;
749 goto cleanup;
750 }
751
752 if (status == LYJSON_NULL) {
753 /* continue with the next entry in the leaf-list array */
754 prev = node;
755 node = node->next;
756 goto next_entry;
757 }
758 break;
759 case LYS_LEAF:
760 case LYS_ANYXML:
761 expected = "@name/object";
762
763 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
764 break;
765 case LYS_CONTAINER:
766 case LYS_LIST:
767 case LYS_ANYDATA:
768 case LYS_NOTIF:
769 case LYS_ACTION:
770 case LYS_RPC:
771 in_parent = 1;
772 expected = "@/object";
773 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
774 break;
775 }
776
777 /* process all the members inside a single metadata object */
778 assert(status == LYJSON_OBJECT);
779
780 while (status != LYJSON_OBJECT_CLOSED) {
781 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
782 if (!prefix) {
783 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
784 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
785 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
786 ret = LY_EVALID;
787 goto cleanup;
788 } else if (is_attr) {
789 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
790 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
791 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
792 ret = LY_EVALID;
793 goto cleanup;
794 }
795
796 /* get the element module */
797 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
798 if (!mod) {
799 if (lydctx->parse_options & LYD_PARSE_STRICT) {
800 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_REFERENCE,
801 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
802 prefix_len, prefix, name_len, name);
803 ret = LY_EVALID;
804 goto cleanup;
805 }
806 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
807 /* skip element with children */
808 ret = lydjson_data_skip(lydctx->jsonctx);
809 LY_CHECK_GOTO(ret, cleanup);
810 status = lyjson_ctx_status(lydctx->jsonctx, 0);
811 /* end of the item */
812 continue;
813 }
814 }
815
816 /* get the value */
817 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
818 LY_CHECK_GOTO(ret, cleanup);
819
820 if (node->schema) {
821 /* create metadata */
822 meta = NULL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200823 ret = lyd_parser_create_meta((struct lyd_ctx*)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
824 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, 0, LY_PREF_JSON, NULL, snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200825 LY_CHECK_GOTO(ret, cleanup);
826
827 /* add/correct flags */
828 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
829 } else {
830 /* create attribute */
831 struct ly_prefix *val_prefs = NULL;
832 const char *module_name;
833 size_t module_name_len;
834
835 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
836
837 /* get value prefixes */
838 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
839
840 /* attr2 is always changed to the created attribute */
841 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
842 &lydctx->jsonctx->dynamic, 0, LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len);
843 LY_CHECK_GOTO(ret, cleanup);
844 }
845 /* next member */
846 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
847 LY_CHECK_GOTO(ret, cleanup);
848 }
849
850 if (nodetype == LYS_LEAFLIST) {
851 /* continue by processing another metadata object for the following
852 * leaf-list instance since they are allways instantiated in JSON array */
853 prev = node;
854 node = node->next;
855 goto next_entry;
856 }
857
858 /* move after the metadata */
859 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
860 LY_CHECK_GOTO(ret, cleanup);
861
862cleanup:
863 return ret;
864
865representation_error:
866 LOGVAL(ctx, LY_VLOG_LYD, (void*)node, LYVE_SYNTAX_JSON,
867 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
868 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq*)node)->name,
869 expected, lyjson_token2str(status), in_parent ? "" : "name");
870
871 return LY_EVALID;
872}
873
874/**
875 * @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.
876 *
877 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
878 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
879 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
880 */
881static void
882lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
883{
884 if (*node_p) {
885 /* insert, keep first pointer correct */
886 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
887 if (first_p) {
888 if (parent) {
889 *first_p = parent->child;
890 } else {
891 while ((*first_p)->prev->next) {
892 *first_p = (*first_p)->prev;
893 }
894 }
895 }
896 *node_p = NULL;
897 }
898}
899
900static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
901
902/**
903 * @brief Parse opaq node from the input.
904 *
905 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
906 *
907 * @param[in] lydctx JSON data parser context.
908 * @param[in] name Name of the opaq node to create.
909 * @param[in] name_len Length of the @p name string.
910 * @param[in] prefix Prefix of the opaq node to create.
911 * @param[in] prefix_len Length of the @p prefx string.
912 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
913 * but must be set if @p first is not.
914 * @param[in,out] status_p Pointer to the current status of the parser context,
915 * since the function manipulates with the context and process the input, the status can be updated.
916 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
917 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
918 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
919 * @param[out] node_p Pointer to the created opaq node.
920 * @return LY_ERR value.
921 */
922static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200923lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
924 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
925 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200926{
927 LY_ERR ret = LY_SUCCESS;
928 const char *value = NULL, *module_name;
929 size_t value_len = 0, module_name_len = 0;
930 struct ly_prefix *val_prefs = NULL;
931 int dynamic = 0;
932 int type_hint = 0;
933
934 if (*status_inner_p != LYJSON_OBJECT && *status_inner_p != LYJSON_OBJECT_EMPTY) {
935 /* prepare for creating opaq node with a value */
936 value = lydctx->jsonctx->value;
937 value_len = lydctx->jsonctx->value_len;
938 dynamic = lydctx->jsonctx->dynamic;
939 lydctx->jsonctx->dynamic = 0;
940
941 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
942
943 if (value) {
944 /* get value prefixes */
945 LY_CHECK_RET(lydjson_get_value_prefixes(lydctx->jsonctx->ctx, value, value_len, &val_prefs));
946 }
947 }
948
949 /* create node */
950 lydjson_get_node_prefix((struct lyd_node*)parent, prefix, prefix_len, &module_name, &module_name_len);
951 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, type_hint,
952 LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len, node_p);
953 if (dynamic) {
954 free((char*)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200955 }
Radek Krejcic826b022020-08-13 11:00:59 +0200956 LY_CHECK_ERR_RET(ret, ly_free_val_prefs(lydctx->jsonctx->ctx, val_prefs), ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200957
958 if (*status_p == LYJSON_OBJECT || *status_p == LYJSON_OBJECT_EMPTY) {
959 /* process children */
960 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
961 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
962 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
963 }
964 } else if (*status_p == LYJSON_ARRAY || *status_p == LYJSON_ARRAY_EMPTY) {
965 /* process another instance of the same node */
966 /* but first mark the node to be expected a list or a leaf-list */
967 ((struct lyd_node_opaq*)*node_p)->hint |= LYD_NODE_OPAQ_ISLIST;
968
969 if (*status_inner_p == LYJSON_OBJECT || *status_inner_p == LYJSON_OBJECT_EMPTY) {
970 /* but first process children of the object in the array */
971 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
972 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
973 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
974 }
975 }
976
977 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
978
979 /* continue with the next instance */
980 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
981 assert(node_p);
982 lydjson_maintain_children(parent, first_p, node_p);
983 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
984 }
985 }
986
987 /* finish linking metadata */
988 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
989
990 /* move after the item */
991 return lyjson_ctx_next(lydctx->jsonctx, status_p);
992}
993
994/**
995 * @brief Process the attribute container (starting by @)
996 *
997 * @param[in] lydctx JSON data parser context.
998 * @param[in] attr_node The data node referenced by the attribute container, if already known.
999 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1000 * @param[in] name Name of the opaq node to create.
1001 * @param[in] name_len Length of the @p name string.
1002 * @param[in] prefix Prefix of the opaq node to create.
1003 * @param[in] prefix_len Length of the @p prefx string.
1004 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1005 * but must be set if @p first is not.
1006 * @param[in,out] status_p Pointer to the current status of the parser context,
1007 * since the function manipulates with the context and process the input, the status can be updated.
1008 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1009 * @param[out] node_p Pointer to the created opaq node.
1010 * @return LY_ERR value.
1011 */
1012static LY_ERR
1013lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
1014 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Michal Vaskoa5da3292020-08-12 13:10:50 +02001015 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1016 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001017{
1018 LY_ERR ret = LY_SUCCESS;
1019 enum LYJSON_PARSER_STATUS status_inner;
1020
1021 /* parse as an attribute to a node */
1022 if (!attr_node && snode) {
1023 /* try to find the instance */
1024 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1025 if (iter->schema == snode) {
1026 attr_node = iter;
1027 break;
1028 }
1029 }
1030 }
1031 if (!attr_node) {
1032 /* parse just as an opaq node with the name beginning with @,
1033 * later we have to check that it belongs to a standard node
1034 * and it is supposed to be converted to a metadata */
1035 uint32_t prev_opts;
1036
1037 /* move into metadata */
1038 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1039
1040 if (*status_p == LYJSON_ARRAY) {
1041 /* move into the array */
1042 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1043 } else {
1044 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1045 status_inner = LYJSON_ERROR;
1046 }
1047
1048 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
1049 prev_opts = lydctx->parse_options;
1050 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1051 lydctx->parse_options |= LYD_PARSE_OPAQ;
1052
1053 ret = lydjson_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
1054 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
1055
1056 /* restore the parser options */
1057 lydctx->parse_options = prev_opts;
1058 } else {
1059 ret = lydjson_metadata(lydctx, snode, attr_node);
1060 }
1061
1062 return ret;
1063}
1064
1065/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001066 * @brief Parse a single instance of a node.
1067 *
1068 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1069 * as before calling, despite it is necessary to process input data for checking.
1070 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1071 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1072 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1073 * @param[in] name Parsed JSON node name.
1074 * @param[in] name_len Lenght of @p name.
1075 * @param[in] prefix Parsed JSON node prefix.
1076 * @param[in] prefix_len Length of @p prefix.
1077 * @param[in,out] status JSON parser status, is updated.
1078 * @param[out] node Parsed data (or opaque) node.
1079 * @return LY_SUCCESS if a node was successfully parsed,
1080 * @return LY_EINVAL in case of invalid JSON encoding,
1081 * @return LY_ERR on other errors.
1082 */
1083static LY_ERR
1084lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
1085 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1086 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
1087{
1088 LY_ERR ret;
1089 int type_hint;
1090 uint32_t prev_opts;
1091 struct lyd_node *tree = NULL;
1092
1093 ret = lydjson_data_check_opaq(lydctx, snode, &type_hint);
1094 if (ret == LY_SUCCESS) {
1095 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1096 if (snode->nodetype & LYD_NODE_TERM) {
1097 /* create terminal node */
1098 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1099 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, type_hint,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001100 LY_PREF_JSON, NULL, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001101 LY_CHECK_RET(ret);
1102
1103 /* move JSON parser */
1104 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1105 LY_CHECK_RET(ret);
1106 } else if (snode->nodetype & LYD_NODE_INNER) {
1107 /* create inner node */
1108 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1109
1110 ret = lyd_create_inner(snode, node);
1111 LY_CHECK_RET(ret);
1112
1113 /* process children */
1114 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1115 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1116 LY_CHECK_RET(ret);
1117 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1118 }
1119
1120 /* finish linking metadata */
1121 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1122 LY_CHECK_RET(ret);
1123
1124 if (snode->nodetype == LYS_LIST) {
1125 /* check all keys exist */
1126 ret = lyd_parse_check_keys(*node);
1127 LY_CHECK_RET(ret);
1128 }
1129
1130 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1131 /* new node validation, autodelete CANNOT occur, all nodes are new */
1132 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1133 LY_CHECK_RET(ret);
1134
1135 /* add any missing default children */
1136 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->unres_node_type,
1137 &lydctx->when_check, (lydctx->validate_options & LYD_VALIDATE_NO_STATE)
1138 ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1139 LY_CHECK_RET(ret);
1140 }
1141
1142 /* move JSON parser */
1143 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1144 LY_CHECK_RET(ret);
1145 } else if (snode->nodetype & LYD_NODE_ANY) {
1146 /* create any node */
1147 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1148
1149 /* parse any data tree with correct options */
1150 /* first backup the current options and then make the parser to process data as opaq nodes */
1151 prev_opts = lydctx->parse_options;
1152 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1153 lydctx->parse_options |= LYD_PARSE_OPAQ;
1154
1155 /* process the anydata content */
1156 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1157 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1158 LY_CHECK_RET(ret);
1159 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1160 }
1161
1162 /* restore parser options */
1163 lydctx->parse_options = prev_opts;
1164
1165 /* finish linking metadata */
1166 ret = lydjson_metadata_finish(lydctx, &tree);
1167 LY_CHECK_RET(ret);
1168
1169 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1170 LY_CHECK_RET(ret);
1171 }
1172 } else if (ret == LY_ENOT) {
1173 /* parse it again as an opaq node */
1174 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
1175 status, status, first_p, node);
1176 LY_CHECK_RET(ret);
1177
1178 if (snode->nodetype & (LYS_LEAFLIST | LYS_LIST)) {
1179 ((struct lyd_node_opaq *)*node)->hint |= LYD_NODE_OPAQ_ISLIST;
1180 }
1181 }
1182
1183 return ret;
1184}
1185
1186/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001187 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001188 *
1189 * @param[in] lydctx JSON data parser context.
1190 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1191 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1192 * @return LY_ERR value.
1193 */
1194static LY_ERR
1195lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1196{
1197 LY_ERR ret = LY_SUCCESS;
1198 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1199 enum LYJSON_PARSER_STATUS status_inner = 0;
1200 const char *name, *prefix = NULL;
1201 size_t name_len, prefix_len = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001202 int is_meta = 0;
1203 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001204 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001205 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1206 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001207
1208 assert(parent || first_p);
1209 assert(status == LYJSON_OBJECT);
1210
1211 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001212 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 +02001213
Michal Vaskocabe0702020-08-12 10:14:36 +02001214 if (!is_meta || name_len || prefix_len) {
1215 /* get the schema node */
1216 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1217 if (ret == LY_ENOT) {
1218 /* skip element with children */
1219 ret = lydjson_data_skip(lydctx->jsonctx);
1220 LY_CHECK_GOTO(ret, cleanup);
1221 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1222 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001223 goto cleanup;
1224 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001225 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001226
Michal Vaskocabe0702020-08-12 10:14:36 +02001227 if (!snode) {
1228 /* we will not be parsing it as metadata */
1229 is_meta = 0;
1230 }
1231 }
1232
1233 if (is_meta) {
1234 /* parse as metadata */
1235 if (!name_len && !prefix_len) {
1236 /* parent's metadata without a name - use the schema from the parent */
1237 if (!parent) {
1238 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
1239 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1240 ret = LY_EVALID;
1241 goto cleanup;
1242 }
1243 attr_node = (struct lyd_node *)parent;
1244 snode = attr_node->schema;
1245 }
1246 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
1247 first_p, &node);
1248 LY_CHECK_GOTO(ret, cleanup);
1249 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001250 /* parse as an opaq node */
1251 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1252
1253 /* move to the second item in the name/X pair */
1254 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1255 LY_CHECK_GOTO(ret, cleanup);
1256
1257 if (status == LYJSON_ARRAY) {
1258 /* move into the array */
1259 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1260 LY_CHECK_GOTO(ret, cleanup);
1261 } else {
1262 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1263 status_inner = LYJSON_ERROR;
1264 }
1265
1266 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
1267 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
1268 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001269 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001270 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001271
1272 /* move to the second item in the name/X pair */
1273 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1274 LY_CHECK_GOTO(ret, cleanup);
1275
1276 /* first check the expected representation according to the nodetype and then continue with the content */
1277 switch (snode->nodetype) {
1278 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001279 case LYS_LIST:
1280 if (snode->nodetype == LYS_LEAFLIST) {
1281 expected = "name/array of values";
1282 } else {
1283 expected = "name/array of objects";
1284 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001285
1286 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1287
1288 /* move into array */
1289 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1290 LY_CHECK_GOTO(ret, cleanup);
1291
Michal Vasko32ac9942020-08-12 14:35:12 +02001292 /* process all the values/objects */
1293 do {
1294 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001295
Michal Vasko32ac9942020-08-12 14:35:12 +02001296 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
1297 &status, &node);
1298 if (ret == LY_EINVAL) {
1299 goto representation_error;
1300 } else if (ret) {
1301 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001302 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001303 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001304
Michal Vasko32ac9942020-08-12 14:35:12 +02001305 /* move after the array */
1306 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1307 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001308
1309 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001310 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001311 case LYS_CONTAINER:
1312 case LYS_NOTIF:
1313 case LYS_ACTION:
1314 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001315 case LYS_ANYDATA:
1316 case LYS_ANYXML:
1317 if (snode->nodetype == LYS_LEAF) {
1318 if (status == LYJSON_ARRAY) {
1319 expected = "name/[null]";
1320 } else {
1321 expected = "name/value";
1322 }
1323 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001324 expected = "name/object";
1325 }
1326
Michal Vasko32ac9942020-08-12 14:35:12 +02001327 /* process the value/object */
1328 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1329 if (ret == LY_EINVAL) {
1330 goto representation_error;
1331 } else if (ret) {
1332 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001333 }
1334
Michal Vasko32ac9942020-08-12 14:35:12 +02001335 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001336 /* rememeber the RPC/action/notification */
1337 lydctx->op_node = node;
1338 }
1339
1340 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001341 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001342 }
1343
Michal Vasko32ac9942020-08-12 14:35:12 +02001344 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001345 lydjson_maintain_children(parent, first_p, &node);
1346
1347cleanup:
1348 lyd_free_tree(node);
1349 return ret;
1350
1351representation_error:
1352 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
1353 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1354 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
1355
1356 ret = LY_EVALID;
1357 goto cleanup;
1358}
1359
1360/**
1361 * @brief Common start of JSON parser processing different types of the input data.
1362 *
1363 * @param[in] ctx libyang context
1364 * @param[in] in Input structure.
1365 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1366 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1367 * @param[out] lydctx_p Data parser context to finish validation.
1368 * @return LY_ERR value.
1369 */
1370static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001371lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
1372 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001373{
1374 LY_ERR ret = LY_SUCCESS;
1375 struct lyd_json_ctx *lydctx;
1376 size_t i, line = 1;
1377
1378 /* starting top-level */
1379 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1380 if (in->current[i] == 0x0a) { /* new line */
1381 line++;
1382 };
1383 }
1384 if (in->current[i] == '\0') {
1385 /* empty data input */
1386 return LY_SUCCESS;
1387 }
1388 if (in->current[i] != '{') {
1389 LOGVAL(ctx, LY_VLOG_LINE, &line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(&in->current[i]), &in->current[i],
1390 "a top-level JSON object ('{') holding encoded YANG data");
1391 return LY_EVALID;
1392 }
1393
1394 /* init context */
1395 lydctx = calloc(1, sizeof *lydctx);
1396 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1397 lydctx->parse_options = parse_options;
1398 lydctx->validate_options = validate_options;
1399 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001400
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
Michal Vaskocf770e22020-08-12 13:21:43 +02001554 /* read subtree */
1555 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1556 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001557
1558 /* finish linking metadata */
1559 ret = lydjson_metadata_finish(lydctx, &tree);
1560 LY_CHECK_GOTO(ret, cleanup);
1561
1562 /* make sure we have parsed some notification */
1563 if (!lydctx->op_node) {
1564 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1565 ret = LY_EVALID;
1566 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001567 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1568 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1569 tree->schema->name);
1570 ret = LY_EVALID;
1571 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001572 }
1573
1574 if (ntf_e) {
1575 /* finish notification envelope */
1576 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1577 LY_CHECK_GOTO(ret, cleanup);
1578 if (status == LYJSON_END) {
1579 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1580 ret = LY_EVALID;
1581 goto cleanup;
1582 } else if (status != LYJSON_OBJECT_CLOSED) {
1583 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
1584 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
1585 ret = LY_EVALID;
1586 goto cleanup;
1587 }
1588 }
1589
1590 if (ntf_p) {
1591 *ntf_p = lydctx->op_node;
1592 }
1593 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001594 if (ntf_e) {
1595 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001596 lyd_insert_node(ntf_e, NULL, tree);
1597 tree = ntf_e;
1598 }
1599 if (tree_p) {
1600 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001601 }
1602
1603cleanup:
1604 /* we have used parse_only flag */
1605 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1606
1607 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1608 if (ret) {
1609 lyd_free_all(tree);
1610 lyd_free_tree(ntf_e);
1611 }
1612 return ret;
1613}
1614
1615/**
1616 * @brief Parse optional JSON envelope around the processed content.
1617 *
1618 * @param[in] jsonctx JSON parser context
1619 * @param[in] parent Parent node (some other envelope).
1620 * @param[in] module_key Name of the module where the envelope element is expected.
1621 * @param[in] object_id Name of the envelope object.
1622 * @param[out] envp_p Pointer to the created envelope opaq container.
1623 * @return LY_SUCCESS if the envelope present and successfully parsed.
1624 * @return LY_ENOT in case there is not the expected envelope.
1625 * @return LY_ERR in case of parsing failure.
1626 */
1627static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001628lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
1629 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001630{
1631 LY_ERR ret = LY_ENOT, r;
1632 const char *name, *prefix;
1633 size_t name_len, prefix_len;
1634 int is_attr;
1635 enum LYJSON_PARSER_STATUS status;
1636
1637 *envp_p = NULL;
1638
1639 /* "id" envelope */
1640 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1641 LY_CHECK_GOTO(is_attr, cleanup);
1642 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1643 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1644 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1645
1646 r = lyjson_ctx_next(jsonctx, &status);
1647 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1648 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1649
1650 /* create the object envelope */
1651 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), "", 0, NULL,
1652 LYD_NODE_OPAQ_ISENVELOPE, LYD_JSON, NULL, NULL, 0,
1653 module_key, ly_strlen(module_key), envp_p);
1654 LY_CHECK_GOTO(ret, cleanup);
1655
1656 if (parent) {
1657 lyd_insert_node(parent, NULL, *envp_p);
1658 }
1659
1660 ret = LY_SUCCESS;
1661cleanup:
1662 return ret;
1663}
1664
1665static LY_ERR
1666lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1667{
1668 enum LYJSON_PARSER_STATUS status;
1669
1670 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1671 if (status == LYJSON_END) {
1672 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1673 return LY_EVALID;
1674 } else if (status != LYJSON_OBJECT_CLOSED) {
1675 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
1676 jsonctx->value_len, jsonctx->value, object_id);
1677 return LY_EVALID;
1678 }
1679 return LY_SUCCESS;
1680}
1681
1682LY_ERR
1683lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1684{
1685 LY_ERR ret = LY_SUCCESS;
1686 struct lyd_json_ctx *lydctx = NULL;
1687 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1688 struct lyd_node *tree = NULL;
1689 enum LYJSON_PARSER_STATUS status;
1690
1691 /* init */
1692 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1693 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1694 lydctx->int_opts = LYD_INTOPT_RPC;
1695
1696 /* process envelope(s), if present */
1697
1698 /* process rpc */
1699 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1700 if (ret == LY_ENOT) {
1701 ret = LY_SUCCESS;
1702 goto parse_content;
1703 } else if (ret) {
1704 goto cleanup;
1705 }
1706 /* process action */
1707 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1708 if (ret == LY_ENOT) {
1709 ret = LY_SUCCESS;
1710 goto parse_content;
1711 } else if (ret) {
1712 goto cleanup;
1713 }
1714
1715parse_content:
1716
1717 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1718 assert(status == LYJSON_OBJECT);
1719
1720 /* read subtree(s) */
Michal Vaskocf770e22020-08-12 13:21:43 +02001721 ret = lydjson_subtree_r(lydctx, act_e ? (struct lyd_node_inner*)act_e : (struct lyd_node_inner*)rpc_e, &tree);
1722 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001723
1724 /* finish linking metadata */
1725 ret = lydjson_metadata_finish(lydctx, &tree);
1726 LY_CHECK_GOTO(ret, cleanup);
1727
1728 /* make sure we have parsed some operation */
1729 if (!lydctx->op_node) {
1730 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
1731 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
1732 ret = LY_EVALID;
1733 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001734 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1735 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1736 tree->schema->name);
1737 ret = LY_EVALID;
1738 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001739 }
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;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001796 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001797 enum LYJSON_PARSER_STATUS status;
1798
1799 /* init */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001800 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001801 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))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001812 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001813 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 }
Michal Vasko4189c0f2020-08-13 09:05:22 +02001852 for (tree = rep_op; tree->parent; tree = LYD_PARENT(tree));
Radek Krejci1798aae2020-07-14 13:26:06 +02001853 if (rpcr_e) {
1854 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001855 lyd_insert_node(rpcr_e, NULL, tree);
1856 tree = rpcr_e;
1857 }
1858 if (tree_p) {
1859 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001860 }
1861
1862cleanup:
1863 /* we have used parse_only flag */
1864 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1865
1866 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1867 if (ret) {
1868 lyd_free_all(rep_op);
1869 lyd_free_tree(rpcr_e);
1870 }
1871 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001872}