blob: 3304bf34db1943d35369de9f6412246352711f3d [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,
Radek Krejci857189e2020-09-01 13:26:36 +020089 size_t *prefix_len_p, ly_bool *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;
Radek Krejci857189e2020-09-01 13:26:36 +020093 ly_bool 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,
Radek Krejci0f969882020-08-21 16:56:47 +0200135 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 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200154 onode = (struct lyd_node_opaq *)node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200155 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 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200162 node = (struct lyd_node *)node->parent;
Radek Krejci1798aae2020-07-14 13:26:06 +0200163 }
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
Radek Krejci857189e2020-09-01 13:26:36 +0200186lydjson_get_snode(const struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +0200187 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 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200192 uint32_t getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200193
194 /* init return value */
195 *snode_p = NULL;
196
197 /* get the element module */
Michal Vasko22df3f02020-08-24 13:29:22 +0200198 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200199 if (!prefix_len) {
200 /* opaq parent (envelope) - the second part of the condition */
Michal Vasko22df3f02020-08-24 13:29:22 +0200201 lydjson_get_node_prefix((struct lyd_node *)parent, NULL, 0, &prefix, &prefix_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200202 }
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
Michal Vasko22df3f02020-08-24 13:29:22 +0200223 if (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hint & LYD_NODE_OPAQ_ISENVELOPE)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200224 /* 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;
Radek Krejci1e008d22020-08-17 11:37:37 +0200303 ly_getutf8(&stop, &c, &bytes)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200304 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);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200320 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->id), error);
321 LY_CHECK_GOTO(ret = lydict_insert(ctx, start, len, &p->module_name), error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200322 } /* 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)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200365 ret = ly_set_add(&key_set, (void *)snode, LY_SET_OPT_USEASLIST, NULL);
366 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200367 }
368
369 if (status != LYJSON_OBJECT_EMPTY) {
370 status_count = jsonctx->status.count;
371
372 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
373 const char *name, *prefix;
374 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200375 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200376
377 /* match the key */
378 snode = NULL;
379 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
380
381 if (!is_attr && !prefix) {
382 for (i = 0; i < key_set.count; ++i) {
383 snode = (const struct lysc_node *)key_set.objs[i];
384 if (!ly_strncmp(snode->name, name, name_len)) {
385 break;
386 }
387 }
388 /* go into the item to a) process it as a key or b) start skipping it as another list child */
389 ret = lyjson_ctx_next(jsonctx, &status);
390 LY_CHECK_GOTO(ret, cleanup);
391
392 if (snode) {
393 /* we have the key, validate the value */
394 if (status < LYJSON_NUMBER) {
395 /* not a terminal */
396 ret = LY_ENOT;
397 goto cleanup;
398 }
399
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200400 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 LY_CHECK_GOTO(ret, cleanup);
402
403 /* key with a valid value, remove from the set */
404 ly_set_rm_index(&key_set, i, NULL);
405 }
406 } else {
407 /* start skipping the member we are not interested in */
408 ret = lyjson_ctx_next(jsonctx, &status);
409 LY_CHECK_GOTO(ret, cleanup);
410 }
411 /* move to the next child */
412 while (status_count < jsonctx->status.count) {
413 ret = lyjson_ctx_next(jsonctx, &status);
414 LY_CHECK_GOTO(ret, cleanup);
415 }
416 }
417 }
418
419 if (key_set.count) {
420 /* some keys are missing/did not validate */
421 ret = LY_ENOT;
422 }
423
424cleanup:
425 ly_set_erase(&key_set, NULL);
426 return ret;
427}
428
429/**
430 * @brief Get the hint for the data type parsers according to the current JSON parser context.
431 *
432 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
433 * @param[in,out] status Pointer to the current context status,
434 * in some circumstances the function manipulates with the context so the status is updated.
435 * @param[out] type_hint_p Pointer to the variable to store the result.
436 * @return LY_SUCCESS in case of success.
437 * @return LY_EINVAL in case of invalid context status not referring to a value.
438 */
439static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200440lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200441{
442 *type_hint_p = 0;
443
444 if (*status_p == LYJSON_ARRAY) {
445 /* only [null] */
446 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
447 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
448
449 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
450 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
451
452 *type_hint_p = LYD_NODE_OPAQ_ISEMPTY;
453 } else if (*status_p == LYJSON_STRING) {
454 *type_hint_p = LYD_NODE_OPAQ_ISSTRING;
455 } else if (*status_p == LYJSON_NUMBER) {
456 *type_hint_p = LYD_NODE_OPAQ_ISNUMBER;
457 } else if (*status_p == LYJSON_FALSE || *status_p == LYJSON_TRUE) {
458 *type_hint_p = LYD_NODE_OPAQ_ISBOOLEAN;
459 } else if (*status_p == LYJSON_NULL) {
460 *type_hint_p = 0;
461 } else {
462 return LY_EINVAL;
463 }
464
465 return LY_SUCCESS;
466}
467
468/**
469 * @brief Check in advance if the input data are parsable according to the provided @p snode.
470 *
471 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
472 * is naturally done when the data are really parsed.
473 *
474 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
475 * as before calling, despite it is necessary to process input data for checking.
476 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
477 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
478 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
479 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
480 * @return LY_EINVAL in case of invalid leaf JSON encoding
481 * and they are expected to be parsed as opaq nodes.
482 */
483static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200484lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200485{
486 LY_ERR ret = LY_SUCCESS;
487 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
488 enum LYJSON_PARSER_STATUS status;
489
490 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200491
Michal Vasko32ac9942020-08-12 14:35:12 +0200492 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
493 /* can always be parsed as a data node if we have the schema node */
494 return LY_SUCCESS;
495 }
496
497 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200498 /* backup parser */
499 lyjson_ctx_backup(jsonctx);
500 status = lyjson_ctx_status(jsonctx, 0);
501
Michal Vasko32ac9942020-08-12 14:35:12 +0200502 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
503 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200504 switch (snode->nodetype) {
505 case LYS_LEAFLIST:
506 case LYS_LEAF:
507 /* value may not be valid in which case we parse it as an opaque node */
508 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
509 if (ret) {
510 break;
511 }
512
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200513 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 ret = LY_ENOT;
515 }
516 break;
517 case LYS_LIST:
518 /* lists may not have all its keys */
519 if (lydjson_check_list(jsonctx, snode)) {
520 /* invalid list, parse as opaque if it missing/has invalid some keys */
521 ret = LY_ENOT;
522 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200523 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200524 }
525
526 /* restore parser */
527 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200528 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200529 status = lyjson_ctx_status(jsonctx, 0);
530 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
531 }
532
533 return ret;
534}
535
536/**
537 * @brief Join the forward-referencing metadata with their target data nodes.
538 *
539 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
540 *
541 * @param[in] lydctx JSON data parser context.
542 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
543 * as a starting point to search for the metadata's target data node
544 * @return LY_SUCCESS on success
545 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
546 */
547static LY_ERR
548lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
549{
550 LY_ERR ret = LY_SUCCESS;
551 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200552 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200553 const char *prev = NULL;
554
555 /* finish linking metadata */
556 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200557 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200558 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200559 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200560 const char *name, *prefix;
561 size_t name_len, prefix_len;
562 const struct lysc_node *snode;
563
564 if (attr->schema || meta_container->name[0] != '@') {
565 /* not an opaq metadata node */
566 continue;
567 }
568
569 if (prev != meta_container->name) {
570 /* metas' names are stored in dictionary, so checking pointers must works */
571 lydict_remove(lydctx->jsonctx->ctx, prev);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200572 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 instance = 1;
574 } else {
575 instance++;
576 }
577
578 /* find the correspnding data node */
579 LY_LIST_FOR(start, node) {
580 if (!node->schema) {
581 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vasko22df3f02020-08-24 13:29:22 +0200582 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq *)node)->name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200583 continue;
584 }
585
Michal Vasko22df3f02020-08-24 13:29:22 +0200586 if (((struct lyd_node_opaq *)node)->hint & LYD_NODE_OPAQ_ISLIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200587 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vasko22df3f02020-08-24 13:29:22 +0200588 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200589 ret = LY_EVALID;
590 goto cleanup;
591 }
592
593 /* match */
594 match++;
595 if (match != instance) {
596 continue;
597 }
598
599 LY_LIST_FOR(meta_container->child, meta_iter) {
600 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200601 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200602 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200603 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200604
605 /* get value prefixes */
606 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
607
608 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
609 &dynamic, meta->hint, LYD_JSON, val_prefs, meta->prefix.id, ly_strlen(meta->prefix.id),
610 meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
611 LY_CHECK_GOTO(ret, cleanup);
612 }
613
614 /* done */
615 break;
616 } else {
617 /* this is the second time we are resolving the schema node, so it must succeed,
618 * but remember that snode can be still NULL */
619 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
620 assert(is_attr);
621 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
622 assert(ret == LY_SUCCESS);
623
624 if (snode != node->schema) {
625 continue;
626 }
627
628 /* match */
629 match++;
630 if (match != instance) {
631 continue;
632 }
633
634 LY_LIST_FOR(meta_container->child, meta_iter) {
635 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200636 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200637 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200638 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200639
640 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
641 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200642 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Radek Krejci1798aae2020-07-14 13:26:06 +0200643 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200644 &dynamic, meta->hint, LY_PREF_JSON, NULL, snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200645 LY_CHECK_GOTO(ret, cleanup);
646 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
647 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
648 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
649 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
650 ret = LY_EVALID;
651 goto cleanup;
652 }
653 }
654 /* add/correct flags */
655 lyd_parse_set_data_flags(node, &lydctx->when_check, &node->meta, lydctx->parse_options);
656
657 /* done */
658 break;
659 }
660 }
661
662 if (match != instance) {
663 /* there is no corresponding data node for the metadata */
664 if (instance > 1) {
665 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
666 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
667 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
668 } else {
669 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
670 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
671 }
672 ret = LY_EVALID;
673 } else {
674 /* remove the opaq attr */
675 if (attr == (*first_p)) {
676 *first_p = attr->next;
677 }
678 lyd_free_tree(attr);
679 }
680 }
681
682cleanup:
683 lydict_remove(lydctx->jsonctx->ctx, prev);
684
685 return ret;
686}
687
688/**
689 * @brief Parse a metadata member.
690 *
691 * @param[in] lydctx JSON data parser context.
692 * @param[in] snode Schema node of the metadata parent.
693 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
694 * so the data node does not exists. In such a case the metadata is stored in the context for the later
695 * processing by lydjson_metadata_finish().
696 * @return LY_SUCCESS on success
697 * @return Various LY_ERR values in case of failure.
698 */
699static LY_ERR
700lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
701{
702 LY_ERR ret = LY_SUCCESS;
703 enum LYJSON_PARSER_STATUS status;
704 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200705 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200706 const char *name, *prefix = NULL;
707 size_t name_len, prefix_len = 0;
708 struct lys_module *mod;
709 struct lyd_meta *meta = NULL;
710 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200711 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200712 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200713 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 uint16_t nodetype;
715
716 assert(snode || node);
717
718 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
719
720 /* move to the second item in the name/X pair */
721 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
722 LY_CHECK_GOTO(ret, cleanup);
723
724 /* check attribute encoding */
725 switch (nodetype) {
726 case LYS_LEAFLIST:
727 expected = "@name/array of objects/nulls";
728
729 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
730
731next_entry:
732 instance++;
733
734 /* move into array / next entry */
735 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
736 LY_CHECK_GOTO(ret, cleanup);
737
738 if (status == LYJSON_ARRAY_CLOSED) {
739 /* we are done, move after the array */
740 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
741 goto cleanup;
742 }
743 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
744
745 if (!node || node->schema != prev->schema) {
746 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200747 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 instance, prev->schema->module->name, prev->schema->name);
749 ret = LY_EVALID;
750 goto cleanup;
751 }
752
753 if (status == LYJSON_NULL) {
754 /* continue with the next entry in the leaf-list array */
755 prev = node;
756 node = node->next;
757 goto next_entry;
758 }
759 break;
760 case LYS_LEAF:
761 case LYS_ANYXML:
762 expected = "@name/object";
763
764 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
765 break;
766 case LYS_CONTAINER:
767 case LYS_LIST:
768 case LYS_ANYDATA:
769 case LYS_NOTIF:
770 case LYS_ACTION:
771 case LYS_RPC:
772 in_parent = 1;
773 expected = "@/object";
774 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
775 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200776 default:
777 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 }
779
780 /* process all the members inside a single metadata object */
781 assert(status == LYJSON_OBJECT);
782
783 while (status != LYJSON_OBJECT_CLOSED) {
784 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
785 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200786 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Radek Krejci1798aae2020-07-14 13:26:06 +0200787 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
788 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
789 ret = LY_EVALID;
790 goto cleanup;
791 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200792 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Radek Krejci1798aae2020-07-14 13:26:06 +0200793 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
794 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
795 ret = LY_EVALID;
796 goto cleanup;
797 }
798
799 /* get the element module */
800 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
801 if (!mod) {
802 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200803 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Radek Krejci1798aae2020-07-14 13:26:06 +0200804 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
805 prefix_len, prefix, name_len, name);
806 ret = LY_EVALID;
807 goto cleanup;
808 }
809 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
810 /* skip element with children */
811 ret = lydjson_data_skip(lydctx->jsonctx);
812 LY_CHECK_GOTO(ret, cleanup);
813 status = lyjson_ctx_status(lydctx->jsonctx, 0);
814 /* end of the item */
815 continue;
816 }
817 }
818
819 /* get the value */
820 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
821 LY_CHECK_GOTO(ret, cleanup);
822
823 if (node->schema) {
824 /* create metadata */
825 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200826 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200827 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, 0, LY_PREF_JSON, NULL, snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200828 LY_CHECK_GOTO(ret, cleanup);
829
830 /* add/correct flags */
831 lyd_parse_set_data_flags(node, &lydctx->when_check, &meta, lydctx->parse_options);
832 } else {
833 /* create attribute */
834 struct ly_prefix *val_prefs = NULL;
835 const char *module_name;
836 size_t module_name_len;
837
838 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
839
840 /* get value prefixes */
841 LY_CHECK_GOTO(ret = lydjson_get_value_prefixes(lydctx->jsonctx->ctx, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &val_prefs), cleanup);
842
843 /* attr2 is always changed to the created attribute */
844 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len,
845 &lydctx->jsonctx->dynamic, 0, LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len);
846 LY_CHECK_GOTO(ret, cleanup);
847 }
848 /* next member */
849 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
850 LY_CHECK_GOTO(ret, cleanup);
851 }
852
853 if (nodetype == LYS_LEAFLIST) {
854 /* continue by processing another metadata object for the following
855 * leaf-list instance since they are allways instantiated in JSON array */
856 prev = node;
857 node = node->next;
858 goto next_entry;
859 }
860
861 /* move after the metadata */
862 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
863 LY_CHECK_GOTO(ret, cleanup);
864
865cleanup:
866 return ret;
867
868representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200869 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Radek Krejci1798aae2020-07-14 13:26:06 +0200870 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
Michal Vasko22df3f02020-08-24 13:29:22 +0200871 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name,
Radek Krejci1798aae2020-07-14 13:26:06 +0200872 expected, lyjson_token2str(status), in_parent ? "" : "name");
873
874 return LY_EVALID;
875}
876
877/**
878 * @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.
879 *
880 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
881 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
882 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
883 */
884static void
885lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
886{
887 if (*node_p) {
888 /* insert, keep first pointer correct */
889 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
890 if (first_p) {
891 if (parent) {
892 *first_p = parent->child;
893 } else {
894 while ((*first_p)->prev->next) {
895 *first_p = (*first_p)->prev;
896 }
897 }
898 }
899 *node_p = NULL;
900 }
901}
902
903static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
904
905/**
906 * @brief Parse opaq node from the input.
907 *
908 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
909 *
910 * @param[in] lydctx JSON data parser context.
911 * @param[in] name Name of the opaq node to create.
912 * @param[in] name_len Length of the @p name string.
913 * @param[in] prefix Prefix of the opaq node to create.
914 * @param[in] prefix_len Length of the @p prefx string.
915 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
916 * but must be set if @p first is not.
917 * @param[in,out] status_p Pointer to the current status of the parser context,
918 * since the function manipulates with the context and process the input, the status can be updated.
919 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
920 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
921 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
922 * @param[out] node_p Pointer to the created opaq node.
923 * @return LY_ERR value.
924 */
925static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200926lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200927 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
928 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200929{
930 LY_ERR ret = LY_SUCCESS;
931 const char *value = NULL, *module_name;
932 size_t value_len = 0, module_name_len = 0;
933 struct ly_prefix *val_prefs = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200934 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200935 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200936
937 if (*status_inner_p != LYJSON_OBJECT && *status_inner_p != LYJSON_OBJECT_EMPTY) {
938 /* prepare for creating opaq node with a value */
939 value = lydctx->jsonctx->value;
940 value_len = lydctx->jsonctx->value_len;
941 dynamic = lydctx->jsonctx->dynamic;
942 lydctx->jsonctx->dynamic = 0;
943
944 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
945
946 if (value) {
947 /* get value prefixes */
948 LY_CHECK_RET(lydjson_get_value_prefixes(lydctx->jsonctx->ctx, value, value_len, &val_prefs));
949 }
950 }
951
952 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200953 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200954 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, type_hint,
955 LYD_JSON, val_prefs, prefix, prefix_len, module_name, module_name_len, node_p);
956 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200957 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200958 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200959 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200960
961 if (*status_p == LYJSON_OBJECT || *status_p == LYJSON_OBJECT_EMPTY) {
962 /* process children */
963 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
964 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
965 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
966 }
967 } else if (*status_p == LYJSON_ARRAY || *status_p == LYJSON_ARRAY_EMPTY) {
968 /* process another instance of the same node */
969 /* but first mark the node to be expected a list or a leaf-list */
Michal Vasko22df3f02020-08-24 13:29:22 +0200970 ((struct lyd_node_opaq *)*node_p)->hint |= LYD_NODE_OPAQ_ISLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200971
972 if (*status_inner_p == LYJSON_OBJECT || *status_inner_p == LYJSON_OBJECT_EMPTY) {
973 /* but first process children of the object in the array */
974 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
975 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
976 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
977 }
978 }
979
980 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
981
982 /* continue with the next instance */
983 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
984 assert(node_p);
985 lydjson_maintain_children(parent, first_p, node_p);
986 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
987 }
988 }
989
990 /* finish linking metadata */
991 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
992
993 /* move after the item */
994 return lyjson_ctx_next(lydctx->jsonctx, status_p);
995}
996
997/**
998 * @brief Process the attribute container (starting by @)
999 *
1000 * @param[in] lydctx JSON data parser context.
1001 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1002 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1003 * @param[in] name Name of the opaq node to create.
1004 * @param[in] name_len Length of the @p name string.
1005 * @param[in] prefix Prefix of the opaq node to create.
1006 * @param[in] prefix_len Length of the @p prefx string.
1007 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1008 * but must be set if @p first is not.
1009 * @param[in,out] status_p Pointer to the current status of the parser context,
1010 * since the function manipulates with the context and process the input, the status can be updated.
1011 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1012 * @param[out] node_p Pointer to the created opaq node.
1013 * @return LY_ERR value.
1014 */
1015static LY_ERR
1016lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +02001017 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1018 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
1019 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001020{
1021 LY_ERR ret = LY_SUCCESS;
1022 enum LYJSON_PARSER_STATUS status_inner;
1023
1024 /* parse as an attribute to a node */
1025 if (!attr_node && snode) {
1026 /* try to find the instance */
1027 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1028 if (iter->schema == snode) {
1029 attr_node = iter;
1030 break;
1031 }
1032 }
1033 }
1034 if (!attr_node) {
1035 /* parse just as an opaq node with the name beginning with @,
1036 * later we have to check that it belongs to a standard node
1037 * and it is supposed to be converted to a metadata */
1038 uint32_t prev_opts;
1039
1040 /* move into metadata */
1041 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1042
1043 if (*status_p == LYJSON_ARRAY) {
1044 /* move into the array */
1045 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1046 } else {
1047 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1048 status_inner = LYJSON_ERROR;
1049 }
1050
1051 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
1052 prev_opts = lydctx->parse_options;
1053 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1054 lydctx->parse_options |= LYD_PARSE_OPAQ;
1055
1056 ret = lydjson_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
1057 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
1058
1059 /* restore the parser options */
1060 lydctx->parse_options = prev_opts;
1061 } else {
1062 ret = lydjson_metadata(lydctx, snode, attr_node);
1063 }
1064
1065 return ret;
1066}
1067
1068/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001069 * @brief Parse a single instance of a node.
1070 *
1071 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1072 * as before calling, despite it is necessary to process input data for checking.
1073 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1074 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1075 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1076 * @param[in] name Parsed JSON node name.
1077 * @param[in] name_len Lenght of @p name.
1078 * @param[in] prefix Parsed JSON node prefix.
1079 * @param[in] prefix_len Length of @p prefix.
1080 * @param[in,out] status JSON parser status, is updated.
1081 * @param[out] node Parsed data (or opaque) node.
1082 * @return LY_SUCCESS if a node was successfully parsed,
1083 * @return LY_EINVAL in case of invalid JSON encoding,
1084 * @return LY_ERR on other errors.
1085 */
1086static LY_ERR
1087lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001088 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1089 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001090{
1091 LY_ERR ret;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001092 uint32_t type_hint;
Michal Vasko32ac9942020-08-12 14:35:12 +02001093 uint32_t prev_opts;
1094 struct lyd_node *tree = NULL;
1095
1096 ret = lydjson_data_check_opaq(lydctx, snode, &type_hint);
1097 if (ret == LY_SUCCESS) {
1098 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1099 if (snode->nodetype & LYD_NODE_TERM) {
1100 /* create terminal node */
1101 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1102 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, type_hint,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001103 LY_PREF_JSON, NULL, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001104 LY_CHECK_RET(ret);
1105
1106 /* move JSON parser */
1107 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1108 LY_CHECK_RET(ret);
1109 } else if (snode->nodetype & LYD_NODE_INNER) {
1110 /* create inner node */
1111 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1112
1113 ret = lyd_create_inner(snode, node);
1114 LY_CHECK_RET(ret);
1115
1116 /* process children */
1117 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1118 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1119 LY_CHECK_RET(ret);
1120 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1121 }
1122
1123 /* finish linking metadata */
1124 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1125 LY_CHECK_RET(ret);
1126
1127 if (snode->nodetype == LYS_LIST) {
1128 /* check all keys exist */
1129 ret = lyd_parse_check_keys(*node);
1130 LY_CHECK_RET(ret);
1131 }
1132
1133 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1134 /* new node validation, autodelete CANNOT occur, all nodes are new */
1135 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1136 LY_CHECK_RET(ret);
1137
1138 /* add any missing default children */
1139 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->unres_node_type,
1140 &lydctx->when_check, (lydctx->validate_options & LYD_VALIDATE_NO_STATE)
1141 ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1142 LY_CHECK_RET(ret);
1143 }
1144
1145 /* move JSON parser */
1146 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1147 LY_CHECK_RET(ret);
1148 } else if (snode->nodetype & LYD_NODE_ANY) {
1149 /* create any node */
1150 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1151
1152 /* parse any data tree with correct options */
1153 /* first backup the current options and then make the parser to process data as opaq nodes */
1154 prev_opts = lydctx->parse_options;
1155 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1156 lydctx->parse_options |= LYD_PARSE_OPAQ;
1157
1158 /* process the anydata content */
1159 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1160 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1161 LY_CHECK_RET(ret);
1162 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1163 }
1164
1165 /* restore parser options */
1166 lydctx->parse_options = prev_opts;
1167
1168 /* finish linking metadata */
1169 ret = lydjson_metadata_finish(lydctx, &tree);
1170 LY_CHECK_RET(ret);
1171
1172 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1173 LY_CHECK_RET(ret);
1174 }
1175 } else if (ret == LY_ENOT) {
1176 /* parse it again as an opaq node */
1177 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
1178 status, status, first_p, node);
1179 LY_CHECK_RET(ret);
1180
1181 if (snode->nodetype & (LYS_LEAFLIST | LYS_LIST)) {
1182 ((struct lyd_node_opaq *)*node)->hint |= LYD_NODE_OPAQ_ISLIST;
1183 }
1184 }
1185
1186 return ret;
1187}
1188
1189/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001190 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001191 *
1192 * @param[in] lydctx JSON data parser context.
1193 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1194 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1195 * @return LY_ERR value.
1196 */
1197static LY_ERR
1198lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1199{
1200 LY_ERR ret = LY_SUCCESS;
1201 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1202 enum LYJSON_PARSER_STATUS status_inner = 0;
1203 const char *name, *prefix = NULL;
1204 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001205 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001206 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001207 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001208 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1209 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001210
1211 assert(parent || first_p);
1212 assert(status == LYJSON_OBJECT);
1213
1214 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001215 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 +02001216
Michal Vaskocabe0702020-08-12 10:14:36 +02001217 if (!is_meta || name_len || prefix_len) {
1218 /* get the schema node */
1219 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1220 if (ret == LY_ENOT) {
1221 /* skip element with children */
1222 ret = lydjson_data_skip(lydctx->jsonctx);
1223 LY_CHECK_GOTO(ret, cleanup);
1224 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1225 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001226 goto cleanup;
1227 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001228 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001229
Michal Vaskocabe0702020-08-12 10:14:36 +02001230 if (!snode) {
1231 /* we will not be parsing it as metadata */
1232 is_meta = 0;
1233 }
1234 }
1235
1236 if (is_meta) {
1237 /* parse as metadata */
1238 if (!name_len && !prefix_len) {
1239 /* parent's metadata without a name - use the schema from the parent */
1240 if (!parent) {
1241 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
1242 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1243 ret = LY_EVALID;
1244 goto cleanup;
1245 }
1246 attr_node = (struct lyd_node *)parent;
1247 snode = attr_node->schema;
1248 }
1249 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
1250 first_p, &node);
1251 LY_CHECK_GOTO(ret, cleanup);
1252 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001253 /* parse as an opaq node */
1254 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1255
1256 /* move to the second item in the name/X pair */
1257 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1258 LY_CHECK_GOTO(ret, cleanup);
1259
1260 if (status == LYJSON_ARRAY) {
1261 /* move into the array */
1262 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1263 LY_CHECK_GOTO(ret, cleanup);
1264 } else {
1265 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1266 status_inner = LYJSON_ERROR;
1267 }
1268
1269 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
1270 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
1271 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001272 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001273 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001274
1275 /* move to the second item in the name/X pair */
1276 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1277 LY_CHECK_GOTO(ret, cleanup);
1278
1279 /* first check the expected representation according to the nodetype and then continue with the content */
1280 switch (snode->nodetype) {
1281 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001282 case LYS_LIST:
1283 if (snode->nodetype == LYS_LEAFLIST) {
1284 expected = "name/array of values";
1285 } else {
1286 expected = "name/array of objects";
1287 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001288
1289 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1290
1291 /* move into array */
1292 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1293 LY_CHECK_GOTO(ret, cleanup);
1294
Michal Vasko32ac9942020-08-12 14:35:12 +02001295 /* process all the values/objects */
1296 do {
1297 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001298
Michal Vasko32ac9942020-08-12 14:35:12 +02001299 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
1300 &status, &node);
1301 if (ret == LY_EINVAL) {
1302 goto representation_error;
1303 } else if (ret) {
1304 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001305 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001306 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001307
Michal Vasko32ac9942020-08-12 14:35:12 +02001308 /* move after the array */
1309 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1310 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001311
1312 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001313 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001314 case LYS_CONTAINER:
1315 case LYS_NOTIF:
1316 case LYS_ACTION:
1317 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001318 case LYS_ANYDATA:
1319 case LYS_ANYXML:
1320 if (snode->nodetype == LYS_LEAF) {
1321 if (status == LYJSON_ARRAY) {
1322 expected = "name/[null]";
1323 } else {
1324 expected = "name/value";
1325 }
1326 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001327 expected = "name/object";
1328 }
1329
Michal Vasko32ac9942020-08-12 14:35:12 +02001330 /* process the value/object */
1331 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1332 if (ret == LY_EINVAL) {
1333 goto representation_error;
1334 } else if (ret) {
1335 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001336 }
1337
Michal Vasko32ac9942020-08-12 14:35:12 +02001338 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001339 /* rememeber the RPC/action/notification */
1340 lydctx->op_node = node;
1341 }
1342
1343 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001344 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001345 }
1346
Michal Vasko32ac9942020-08-12 14:35:12 +02001347 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001348 lydjson_maintain_children(parent, first_p, &node);
1349
1350cleanup:
1351 lyd_free_tree(node);
1352 return ret;
1353
1354representation_error:
1355 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
1356 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1357 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
1358
1359 ret = LY_EVALID;
1360 goto cleanup;
1361}
1362
1363/**
1364 * @brief Common start of JSON parser processing different types of the input data.
1365 *
1366 * @param[in] ctx libyang context
1367 * @param[in] in Input structure.
1368 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1369 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1370 * @param[out] lydctx_p Data parser context to finish validation.
1371 * @return LY_ERR value.
1372 */
1373static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001374lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001375 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001376{
1377 LY_ERR ret = LY_SUCCESS;
1378 struct lyd_json_ctx *lydctx;
1379 size_t i, line = 1;
1380
1381 /* starting top-level */
1382 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1383 if (in->current[i] == 0x0a) { /* new line */
1384 line++;
1385 };
1386 }
1387 if (in->current[i] == '\0') {
1388 /* empty data input */
1389 return LY_SUCCESS;
1390 }
1391 if (in->current[i] != '{') {
1392 LOGVAL(ctx, LY_VLOG_LINE, &line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(&in->current[i]), &in->current[i],
1393 "a top-level JSON object ('{') holding encoded YANG data");
1394 return LY_EVALID;
1395 }
1396
1397 /* init context */
1398 lydctx = calloc(1, sizeof *lydctx);
1399 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1400 lydctx->parse_options = parse_options;
1401 lydctx->validate_options = validate_options;
1402 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001403
1404 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1405
1406 *lydctx_p = lydctx;
1407 return LY_SUCCESS;
1408}
1409
1410LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001411lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001412 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001413{
1414 LY_ERR ret = LY_SUCCESS;
1415 struct lyd_json_ctx *lydctx = NULL;
1416 enum LYJSON_PARSER_STATUS status;
1417
1418 assert(tree_p);
1419 *tree_p = NULL;
1420
1421 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx);
1422 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1423
1424 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1425 assert(status == LYJSON_OBJECT);
1426
1427 /* read subtree(s) */
1428 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1429 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1430 LY_CHECK_GOTO(ret, cleanup);
1431
1432 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1433 }
1434
1435 /* finish linking metadata */
1436 ret = lydjson_metadata_finish(lydctx, tree_p);
1437 LY_CHECK_GOTO(ret, cleanup);
1438
1439cleanup:
1440 /* there should be no unresolved types stored */
1441 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count
Radek Krejci0f969882020-08-21 16:56:47 +02001442 && !lydctx->when_check.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001443
1444 if (ret) {
1445 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1446 lyd_free_all(*tree_p);
1447 *tree_p = NULL;
1448 } else {
1449 *lydctx_p = (struct lyd_ctx *)lydctx;
1450 }
1451
1452 return ret;
1453}
1454
1455/**
1456 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1457 *
1458 * @param[in] jsonctx JSON parser context
1459 * @param[out] envp_p Pointer to the created envelope opaq container.
1460 * @return LY_SUCCESS if the envelope present and successfully parsed.
1461 * @return LY_ENOT in case there is not the expected envelope.
1462 * @return LY_ERR in case of parsing failure.
1463 */
1464static LY_ERR
1465lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p)
1466{
1467 LY_ERR ret = LY_ENOT, r;
1468 const char *name, *prefix, *value = NULL;
1469 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001470 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001471 enum LYJSON_PARSER_STATUS status;
1472 struct lyd_node *et;
1473
1474 *envp_p = NULL;
1475
1476 /* backup the context */
1477 lyjson_ctx_backup(jsonctx);
1478
1479 /* "notification" envelope */
1480 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1481 LY_CHECK_GOTO(is_attr, cleanup);
1482 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1483 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1484
1485 r = lyjson_ctx_next(jsonctx, &status);
1486 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1487 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1488
1489 /* "eventTime" child */
1490 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1491 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1492 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1493
1494 /* go for the data */
1495 r = lyjson_ctx_next(jsonctx, &status);
1496 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1497 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1498
1499 value = jsonctx->value;
1500 value_len = jsonctx->value_len;
1501 dynamic = jsonctx->dynamic;
1502 jsonctx->dynamic = 0;
1503
1504 r = lyjson_ctx_next(jsonctx, &status);
1505 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1506 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1507 /* now the notificationContent is expected, which will be parsed by the caller */
1508
1509 /* create notification envelope */
1510 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LYD_NODE_OPAQ_ISENVELOPE,
1511 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, envp_p);
1512 LY_CHECK_GOTO(ret, cleanup);
1513 /* create notification envelope */
1514 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", 9, value, value_len, &dynamic, LYD_NODE_OPAQ_ISSTRING,
1515 LYD_JSON, NULL, NULL, 0, "ietf-restconf", 13, &et);
1516 LY_CHECK_GOTO(ret, cleanup);
1517 /* insert eventTime into notification */
1518 lyd_insert_node(*envp_p, NULL, et);
1519
1520 ret = LY_SUCCESS;
1521cleanup:
1522 if (ret) {
1523 /* restore the context */
1524 lyjson_ctx_restore(jsonctx);
1525 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001526 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001527 }
1528 }
1529 return ret;
1530}
1531
1532LY_ERR
1533lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1534{
1535 LY_ERR ret = LY_SUCCESS;
1536 struct lyd_json_ctx *lydctx = NULL;
1537 struct lyd_node *ntf_e = NULL;
1538 struct lyd_node *tree = NULL;
1539 enum LYJSON_PARSER_STATUS status;
1540
1541 /* init */
1542 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1543 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1544 lydctx->int_opts = LYD_INTOPT_NOTIF;
1545
1546 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1547 assert(status == LYJSON_OBJECT);
1548
1549 /* parse "notification" and "eventTime", if present */
1550 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e);
1551 if (ret == LY_ENOT) {
1552 ret = LY_SUCCESS;
1553 } else if (ret) {
1554 goto cleanup;
1555 }
1556
Michal Vaskocf770e22020-08-12 13:21:43 +02001557 /* read subtree */
1558 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1559 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001560
1561 /* finish linking metadata */
1562 ret = lydjson_metadata_finish(lydctx, &tree);
1563 LY_CHECK_GOTO(ret, cleanup);
1564
1565 /* make sure we have parsed some notification */
1566 if (!lydctx->op_node) {
1567 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1568 ret = LY_EVALID;
1569 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001570 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1571 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1572 tree->schema->name);
1573 ret = LY_EVALID;
1574 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001575 }
1576
1577 if (ntf_e) {
1578 /* finish notification envelope */
1579 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1580 LY_CHECK_GOTO(ret, cleanup);
1581 if (status == LYJSON_END) {
1582 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1583 ret = LY_EVALID;
1584 goto cleanup;
1585 } else if (status != LYJSON_OBJECT_CLOSED) {
1586 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
1587 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
1588 ret = LY_EVALID;
1589 goto cleanup;
1590 }
1591 }
1592
1593 if (ntf_p) {
1594 *ntf_p = lydctx->op_node;
1595 }
1596 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001597 if (ntf_e) {
1598 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001599 lyd_insert_node(ntf_e, NULL, tree);
1600 tree = ntf_e;
1601 }
1602 if (tree_p) {
1603 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001604 }
1605
1606cleanup:
1607 /* we have used parse_only flag */
1608 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1609
1610 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1611 if (ret) {
1612 lyd_free_all(tree);
1613 lyd_free_tree(ntf_e);
1614 }
1615 return ret;
1616}
1617
1618/**
1619 * @brief Parse optional JSON envelope around the processed content.
1620 *
1621 * @param[in] jsonctx JSON parser context
1622 * @param[in] parent Parent node (some other envelope).
1623 * @param[in] module_key Name of the module where the envelope element is expected.
1624 * @param[in] object_id Name of the envelope object.
1625 * @param[out] envp_p Pointer to the created envelope opaq container.
1626 * @return LY_SUCCESS if the envelope present and successfully parsed.
1627 * @return LY_ENOT in case there is not the expected envelope.
1628 * @return LY_ERR in case of parsing failure.
1629 */
1630static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001631lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejci0f969882020-08-21 16:56:47 +02001632 const char *object_id, struct lyd_node **envp_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001633{
1634 LY_ERR ret = LY_ENOT, r;
1635 const char *name, *prefix;
1636 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001637 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001638 enum LYJSON_PARSER_STATUS status;
1639
1640 *envp_p = NULL;
1641
1642 /* "id" envelope */
1643 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1644 LY_CHECK_GOTO(is_attr, cleanup);
1645 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1646 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1647 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1648
1649 r = lyjson_ctx_next(jsonctx, &status);
1650 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1651 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1652
1653 /* create the object envelope */
1654 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), "", 0, NULL,
1655 LYD_NODE_OPAQ_ISENVELOPE, LYD_JSON, NULL, NULL, 0,
1656 module_key, ly_strlen(module_key), envp_p);
1657 LY_CHECK_GOTO(ret, cleanup);
1658
1659 if (parent) {
1660 lyd_insert_node(parent, NULL, *envp_p);
1661 }
1662
1663 ret = LY_SUCCESS;
1664cleanup:
1665 return ret;
1666}
1667
1668static LY_ERR
1669lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id)
1670{
1671 enum LYJSON_PARSER_STATUS status;
1672
1673 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &status));
1674 if (status == LYJSON_END) {
1675 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1676 return LY_EVALID;
1677 } else if (status != LYJSON_OBJECT_CLOSED) {
1678 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
1679 jsonctx->value_len, jsonctx->value, object_id);
1680 return LY_EVALID;
1681 }
1682 return LY_SUCCESS;
1683}
1684
1685LY_ERR
1686lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1687{
1688 LY_ERR ret = LY_SUCCESS;
1689 struct lyd_json_ctx *lydctx = NULL;
1690 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1691 struct lyd_node *tree = NULL;
1692 enum LYJSON_PARSER_STATUS status;
1693
1694 /* init */
1695 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
1696 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1697 lydctx->int_opts = LYD_INTOPT_RPC;
1698
1699 /* process envelope(s), if present */
1700
1701 /* process rpc */
1702 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e);
1703 if (ret == LY_ENOT) {
1704 ret = LY_SUCCESS;
1705 goto parse_content;
1706 } else if (ret) {
1707 goto cleanup;
1708 }
1709 /* process action */
1710 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e);
1711 if (ret == LY_ENOT) {
1712 ret = LY_SUCCESS;
1713 goto parse_content;
1714 } else if (ret) {
1715 goto cleanup;
1716 }
1717
1718parse_content:
1719
1720 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1721 assert(status == LYJSON_OBJECT);
1722
1723 /* read subtree(s) */
Michal Vasko22df3f02020-08-24 13:29:22 +02001724 ret = lydjson_subtree_r(lydctx, act_e ? (struct lyd_node_inner *)act_e : (struct lyd_node_inner *)rpc_e, &tree);
Michal Vaskocf770e22020-08-12 13:21:43 +02001725 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001726
1727 /* finish linking metadata */
1728 ret = lydjson_metadata_finish(lydctx, &tree);
1729 LY_CHECK_GOTO(ret, cleanup);
1730
1731 /* make sure we have parsed some operation */
1732 if (!lydctx->op_node) {
1733 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
1734 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
1735 ret = LY_EVALID;
1736 goto cleanup;
Michal Vaskocf770e22020-08-12 13:21:43 +02001737 } else if (lydctx->jsonctx->in->current[0] && lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED) {
1738 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
1739 tree->schema->name);
1740 ret = LY_EVALID;
1741 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001742 }
1743
1744 if (act_e) {
1745 /* finish action envelope */
1746 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action");
1747 LY_CHECK_GOTO(ret, cleanup);
1748 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1749 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
1750 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1751 ret = LY_EVALID;
1752 goto cleanup;
1753 }
1754 }
1755 if (rpc_e) {
1756 /* finish rpc envelope */
1757 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc");
1758 LY_CHECK_GOTO(ret, cleanup);
1759 if (!act_e && lydctx->op_node->schema->nodetype != LYS_RPC) {
1760 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
1761 lys_nodetype2str(lydctx->op_node->schema->nodetype));
1762 ret = LY_EVALID;
1763 goto cleanup;
1764 }
1765 }
1766
1767 if (op_p) {
1768 *op_p = lydctx->op_node;
1769 }
1770 assert(tree);
1771 if (tree_p) {
1772 if (rpc_e) {
1773 *tree_p = rpc_e;
1774 } else if (act_e) {
1775 *tree_p = act_e;
1776 } else {
1777 *tree_p = tree;
1778 }
1779 }
1780
1781cleanup:
1782 /* we have used parse_only flag */
1783 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1784
1785 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1786 if (ret) {
1787 lyd_free_all(tree);
1788 lyd_free_tree(act_e);
1789 lyd_free_tree(rpc_e);
1790 }
1791 return ret;
1792}
1793
1794LY_ERR
1795lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1796{
1797 LY_ERR ret = LY_SUCCESS;
1798 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001799 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001800 enum LYJSON_PARSER_STATUS status;
1801
1802 /* init */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001803 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001804 LY_CHECK_GOTO(ret || !lydctx, cleanup);
1805 lydctx->int_opts = LYD_INTOPT_REPLY;
1806
1807 /* find request OP */
1808 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1809 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1810 break;
1811 }
1812 LYD_TREE_DFS_END(request, req_op);
1813 }
1814 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001815 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001816 ret = LY_EINVAL;
1817 goto cleanup;
1818 }
1819
1820 /* duplicate request OP with parents */
1821 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1822
1823 /* parse "rpc-reply", if any */
1824 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e);
1825 if (ret == LY_ENOT) {
1826 ret = LY_SUCCESS;
1827 } else if (ret) {
1828 goto cleanup;
1829 }
1830
1831 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1832 assert(status == LYJSON_OBJECT);
1833
1834 /* read subtree(s) */
1835 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001836 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op));
Radek Krejci1798aae2020-07-14 13:26:06 +02001837 LY_CHECK_GOTO(ret, cleanup);
1838
1839 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1840 }
1841
1842 /* finish linking metadata */
1843 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1844 LY_CHECK_GOTO(ret, cleanup);
1845
1846 if (rpcr_e) {
1847 /* finish rpc-reply envelope */
1848 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply");
1849 LY_CHECK_GOTO(ret, cleanup);
1850 }
1851
1852 if (op_p) {
1853 *op_p = rep_op;
1854 }
Radek Krejci1e008d22020-08-17 11:37:37 +02001855 for (tree = rep_op; tree->parent; tree = LYD_PARENT(tree)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001856 if (rpcr_e) {
1857 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001858 lyd_insert_node(rpcr_e, NULL, tree);
1859 tree = rpcr_e;
1860 }
1861 if (tree_p) {
1862 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001863 }
1864
1865cleanup:
1866 /* we have used parse_only flag */
1867 assert(!lydctx || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count && !lydctx->when_check.count));
1868
1869 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1870 if (ret) {
1871 lyd_free_all(rep_op);
1872 lyd_free_tree(rpcr_e);
1873 }
1874 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001875}