blob: 7ccf92bb7f908248b0e1c5c61e185fb01a7f0db7 [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>
Radek Krejci47fab892020-11-05 17:02:41 +010018#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010019#include <stdlib.h>
20#include <string.h>
21
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
Michal Vasko90932a92020-02-12 14:33:03 +010023#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020026#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "log.h"
28#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020029#include "parser_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "set.h"
31#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020032#include "tree_data.h"
33#include "tree_data_internal.h"
34#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020036#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010037
38/**
Radek Krejci1798aae2020-07-14 13:26:06 +020039 * @brief Internal context for JSON YANG data parser.
40 *
41 * Note that the structure maps to the lyd_ctx which is common for all the data parsers
42 */
43struct lyd_json_ctx {
44 uint32_t parse_options; /**< various @ref dataparseroptions. */
45 uint32_t validate_options; /**< various @ref datavalidationoptions. */
46 uint32_t int_opts; /**< internal data parser options */
47 uint32_t path_len; /**< used bytes in the path buffer */
48 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Michal Vasko49c39d82020-11-06 17:20:27 +010049 struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */
50 struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */
51 struct ly_set node_when; /**< set of nodes with "when" conditions */
Radek Krejci1798aae2020-07-14 13:26:06 +020052 struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */
53
54 /* callbacks */
55 lyd_ctx_free_clb free; /* destructor */
Radek Krejci1798aae2020-07-14 13:26:06 +020056
57 struct lyjson_ctx *jsonctx; /**< JSON context */
58};
59
60/**
61 * @brief Free the JSON data parser context.
62 *
63 * JSON implementation of lyd_ctx_free_clb().
64 */
65static void
66lyd_json_ctx_free(struct lyd_ctx *lydctx)
67{
68 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
69
70 if (lydctx) {
71 lyd_ctx_free(lydctx);
72 lyjson_ctx_free(ctx->jsonctx);
73 free(ctx);
74 }
75}
76
77/**
Radek Krejci1798aae2020-07-14 13:26:06 +020078 * @brief Parse JSON member-name as [\@][prefix:][name]
79 *
Michal Vaskocabe0702020-08-12 10:14:36 +020080 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020081 * prefix - name of the module of the data node
82 * name - name of the data node
83 *
84 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
85 *
86 * @param[in] value String to parse
87 * @param[in] value_len Length of the @p str.
88 * @param[out] name_p Pointer to the beginning of the parsed name.
89 * @param[out] name_len_p Pointer to the length of the parsed name.
90 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
91 * @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 +020092 * @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 +020093 */
94static void
Michal Vaskocabe0702020-08-12 10:14:36 +020095lydjson_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 +020096 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +020097{
98 const char *name, *prefix = NULL;
99 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200100 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200101
102 name = memchr(value, ':', value_len);
103 if (name != NULL) {
104 prefix = value;
105 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200106 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200107 prefix++;
108 }
109 prefix_len = name - prefix;
110 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200111 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200112 } else {
113 name = value;
114 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200115 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 name++;
117 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200118 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100119 }
120
Radek Krejci1798aae2020-07-14 13:26:06 +0200121 *name_p = name;
122 *name_len_p = name_len;
123 *prefix_p = prefix;
124 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200125 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200126}
127
128/**
129 * @brief Get correct prefix (module_name) inside the @p node.
130 *
131 * @param[in] node Data node to get inherited prefix.
132 * @param[in] local_prefix Local prefix to replace the inherited prefix.
133 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
134 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
135 * and no context @p node to get inherited prefix.
136 * @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
137 * of no local prefix and no context @p node to get inherited prefix.
138 * @return LY_ERR value.
139 */
140static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200141lydjson_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 +0200142 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200143{
144 struct lyd_node_opaq *onode;
145 const char *module_name = NULL;
146
147 assert(prefix_p && prefix_len_p);
148
149 if (local_prefix_len) {
150 *prefix_p = local_prefix;
151 *prefix_len_p = local_prefix_len;
152 return LY_SUCCESS;
153 }
154
155 *prefix_p = NULL;
156 while (node) {
157 if (node->schema) {
158 *prefix_p = node->schema->module->name;
159 break;
160 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200161 onode = (struct lyd_node_opaq *)node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200162 if (onode->prefix.module_name) {
163 *prefix_p = onode->prefix.module_name;
164 break;
165 } else if (onode->prefix.id) {
166 *prefix_p = onode->prefix.id;
167 break;
168 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200169 node = (struct lyd_node *)node->parent;
Radek Krejci1798aae2020-07-14 13:26:06 +0200170 }
171 *prefix_len_p = ly_strlen(module_name);
172
173 return LY_SUCCESS;
174}
175
176/**
177 * @brief Get schema node corresponding to the input parameters.
178 *
179 * @param[in] lydctx JSON data parser context.
180 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
181 * @param[in] prefix Requested node's prefix (module name).
182 * @param[in] prefix_len Length of the @p prefix.
183 * @param[in] name Requested node's name.
184 * @param[in] name_len Length of the @p name.
185 * @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
186 * is accepted for searching top-level nodes.
187 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
188 * @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.
189 * @return LY_EVALID on failure, error message is logged
190 * @return LY_ENOT in case the input data are expected to be skipped
191 */
192static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200193lydjson_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 +0200194 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200195{
196 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100197 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200198
199 /* init return value */
200 *snode_p = NULL;
201
202 /* get the element module */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200203 if (prefix_len || (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200204 if (!prefix_len) {
205 /* opaq parent (envelope) - the second part of the condition */
Michal Vasko22df3f02020-08-24 13:29:22 +0200206 lydjson_get_node_prefix((struct lyd_node *)parent, NULL, 0, &prefix, &prefix_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200207 }
208 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
209 } else if (parent) {
210 if (parent->schema) {
211 mod = parent->schema->module;
212 }
213 } else {
214 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
Michal Vasko69730152020-10-09 16:30:07 +0200215 is_attr ? name_len + 1 : name_len, is_attr ? name - 1 : name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200216 return LY_EVALID;
217 }
218 if (!mod) {
219 if (lydctx->parse_options & LYD_PARSE_STRICT) {
220 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", prefix_len, prefix);
221 return LY_EVALID;
222 }
223 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
224 return LY_ENOT;
225 }
226 }
227
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200228 if (parent && !parent->schema && (((struct lyd_node_opaq *)parent)->hints & LYD_NODEHINT_ENVELOPE)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200229 /* ignore the envelope parent when searchinf for the schema node */
230 parent = NULL;
231 }
232
233 /* get the schema node */
234 if (mod && (!parent || parent->schema)) {
235 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
236 if (!*snode_p) {
237 if (lydctx->parse_options & LYD_PARSE_STRICT) {
238 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, parent, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
Michal Vasko69730152020-10-09 16:30:07 +0200239 name_len, name, mod->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200240 return LY_EVALID;
241 } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
242 /* skip element with children */
243 return LY_ENOT;
244 }
245 } else {
246 /* check that schema node is valid and can be used */
247 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p));
248 }
249 }
250
251 return LY_SUCCESS;
252}
253
254/**
255 * @brief Skip the currently open JSON object/array
256 * @param[in] jsonctx JSON context with the input data to skip.
257 * @return LY_ERR value.
258 */
259static LY_ERR
260lydjson_data_skip(struct lyjson_ctx *jsonctx)
261{
262 enum LYJSON_PARSER_STATUS status, current;
263 size_t sublevels = 1;
264
265 status = lyjson_ctx_status(jsonctx, 0);
266
267 /* skip after the content */
268 do {
269 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
270 if (current == status) {
271 sublevels++;
272 } else if (current == status + 1) {
273 sublevels--;
274 }
275 } while (current != status + 1 && sublevels);
276 /* open the next sibling */
277 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
278
279 return LY_SUCCESS;
280}
281
282/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200283 * @brief Check that the input data are parseable as the @p list.
284 *
285 * Checks for all the list's keys. Function does not revert the context state.
286 *
287 * @param[in] jsonctx JSON parser context.
288 * @param[in] list List schema node corresponding to the input data object.
289 * @return LY_SUCCESS in case the data are ok for the @p list
290 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
291 */
292static LY_ERR
293lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
294{
295 LY_ERR ret = LY_SUCCESS;
296 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
297 struct ly_set key_set = {0};
298 const struct lysc_node *snode;
299 uint32_t i, status_count;
300
301 assert(list && (list->nodetype == LYS_LIST));
302 assert(status == LYJSON_OBJECT);
303
304 /* get all keys into a set (keys do not have if-features or anything) */
305 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100306 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200307 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200308 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200309 }
310
311 if (status != LYJSON_OBJECT_EMPTY) {
312 status_count = jsonctx->status.count;
313
314 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
315 const char *name, *prefix;
316 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200317 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200318
319 /* match the key */
320 snode = NULL;
321 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
322
323 if (!is_attr && !prefix) {
324 for (i = 0; i < key_set.count; ++i) {
325 snode = (const struct lysc_node *)key_set.objs[i];
326 if (!ly_strncmp(snode->name, name, name_len)) {
327 break;
328 }
329 }
330 /* go into the item to a) process it as a key or b) start skipping it as another list child */
331 ret = lyjson_ctx_next(jsonctx, &status);
332 LY_CHECK_GOTO(ret, cleanup);
333
334 if (snode) {
335 /* we have the key, validate the value */
336 if (status < LYJSON_NUMBER) {
337 /* not a terminal */
338 ret = LY_ENOT;
339 goto cleanup;
340 }
341
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200342 ret = _lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200343 LY_CHECK_GOTO(ret, cleanup);
344
345 /* key with a valid value, remove from the set */
346 ly_set_rm_index(&key_set, i, NULL);
347 }
348 } else {
349 /* start skipping the member we are not interested in */
350 ret = lyjson_ctx_next(jsonctx, &status);
351 LY_CHECK_GOTO(ret, cleanup);
352 }
353 /* move to the next child */
354 while (status_count < jsonctx->status.count) {
355 ret = lyjson_ctx_next(jsonctx, &status);
356 LY_CHECK_GOTO(ret, cleanup);
357 }
358 }
359 }
360
361 if (key_set.count) {
362 /* some keys are missing/did not validate */
363 ret = LY_ENOT;
364 }
365
366cleanup:
367 ly_set_erase(&key_set, NULL);
368 return ret;
369}
370
371/**
372 * @brief Get the hint for the data type parsers according to the current JSON parser context.
373 *
374 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
375 * @param[in,out] status Pointer to the current context status,
376 * in some circumstances the function manipulates with the context so the status is updated.
377 * @param[out] type_hint_p Pointer to the variable to store the result.
378 * @return LY_SUCCESS in case of success.
379 * @return LY_EINVAL in case of invalid context status not referring to a value.
380 */
381static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200382lydjson_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 +0200383{
384 *type_hint_p = 0;
385
386 if (*status_p == LYJSON_ARRAY) {
387 /* only [null] */
388 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
389 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
390
391 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
392 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
393
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200394 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200395 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200396 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200398 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200399 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200400 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 } else if (*status_p == LYJSON_NULL) {
402 *type_hint_p = 0;
403 } else {
404 return LY_EINVAL;
405 }
406
407 return LY_SUCCESS;
408}
409
410/**
411 * @brief Check in advance if the input data are parsable according to the provided @p snode.
412 *
413 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
414 * is naturally done when the data are really parsed.
415 *
416 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
417 * as before calling, despite it is necessary to process input data for checking.
418 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
419 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
420 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
421 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
422 * @return LY_EINVAL in case of invalid leaf JSON encoding
423 * and they are expected to be parsed as opaq nodes.
424 */
425static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200426lydjson_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 +0200427{
428 LY_ERR ret = LY_SUCCESS;
429 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
430 enum LYJSON_PARSER_STATUS status;
431
432 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200433
Michal Vasko32ac9942020-08-12 14:35:12 +0200434 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
435 /* can always be parsed as a data node if we have the schema node */
436 return LY_SUCCESS;
437 }
438
439 if (lydctx->parse_options & LYD_PARSE_OPAQ) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200440 /* backup parser */
441 lyjson_ctx_backup(jsonctx);
442 status = lyjson_ctx_status(jsonctx, 0);
443
Michal Vasko32ac9942020-08-12 14:35:12 +0200444 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
445 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200446 switch (snode->nodetype) {
447 case LYS_LEAFLIST:
448 case LYS_LEAF:
449 /* value may not be valid in which case we parse it as an opaque node */
450 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
451 if (ret) {
452 break;
453 }
454
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200455 if (_lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_PREF_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200456 ret = LY_ENOT;
457 }
458 break;
459 case LYS_LIST:
460 /* lists may not have all its keys */
461 if (lydjson_check_list(jsonctx, snode)) {
462 /* invalid list, parse as opaque if it missing/has invalid some keys */
463 ret = LY_ENOT;
464 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200465 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200466 }
467
468 /* restore parser */
469 lyjson_ctx_restore(jsonctx);
Michal Vasko32ac9942020-08-12 14:35:12 +0200470 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200471 status = lyjson_ctx_status(jsonctx, 0);
472 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
473 }
474
475 return ret;
476}
477
478/**
479 * @brief Join the forward-referencing metadata with their target data nodes.
480 *
481 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
482 *
483 * @param[in] lydctx JSON data parser context.
484 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
485 * as a starting point to search for the metadata's target data node
486 * @return LY_SUCCESS on success
487 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
488 */
489static LY_ERR
490lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
491{
492 LY_ERR ret = LY_SUCCESS;
493 struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200494 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200495 const char *prev = NULL;
496
497 /* finish linking metadata */
498 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200499 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200500 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200501 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200502 const char *name, *prefix;
503 size_t name_len, prefix_len;
504 const struct lysc_node *snode;
505
Michal Vasko69730152020-10-09 16:30:07 +0200506 if (attr->schema || (meta_container->name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200507 /* not an opaq metadata node */
508 continue;
509 }
510
511 if (prev != meta_container->name) {
512 /* metas' names are stored in dictionary, so checking pointers must works */
513 lydict_remove(lydctx->jsonctx->ctx, prev);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200514 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 instance = 1;
516 } else {
517 instance++;
518 }
519
520 /* find the correspnding data node */
521 LY_LIST_FOR(start, node) {
522 if (!node->schema) {
523 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vasko22df3f02020-08-24 13:29:22 +0200524 if (strcmp(&meta_container->name[1], ((struct lyd_node_opaq *)node)->name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 continue;
526 }
527
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200528 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200529 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_SYNTAX,
Michal Vasko69730152020-10-09 16:30:07 +0200530 "Metadata container references a sibling list node %s.", ((struct lyd_node_opaq *)node)->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200531 ret = LY_EVALID;
532 goto cleanup;
533 }
534
535 /* match */
536 match++;
537 if (match != instance) {
538 continue;
539 }
540
541 LY_LIST_FOR(meta_container->child, meta_iter) {
542 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200543 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200544 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name, strlen(meta->name), meta->value,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100545 ly_strlen(meta->value), NULL, LY_PREF_JSON, meta->hints, NULL, meta->prefix.id,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200546 ly_strlen(meta->prefix.id), meta->prefix.module_name, ly_strlen(meta->prefix.module_name));
Radek Krejci1798aae2020-07-14 13:26:06 +0200547 LY_CHECK_GOTO(ret, cleanup);
548 }
549
550 /* done */
551 break;
552 } else {
553 /* this is the second time we are resolving the schema node, so it must succeed,
554 * but remember that snode can be still NULL */
555 lydjson_parse_name(meta_container->name, strlen(meta_container->name), &name, &name_len, &prefix, &prefix_len, &is_attr);
556 assert(is_attr);
557 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
558 assert(ret == LY_SUCCESS);
559
560 if (snode != node->schema) {
561 continue;
562 }
563
564 /* match */
565 match++;
566 if (match != instance) {
567 continue;
568 }
569
570 LY_LIST_FOR(meta_container->child, meta_iter) {
571 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200572 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 struct lys_module *mod = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200574 ly_bool dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200575
576 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->prefix.id);
577 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200578 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vasko69730152020-10-09 16:30:07 +0200579 meta->name, strlen(meta->name), meta->value, ly_strlen(meta->value),
580 &dynamic, LY_PREF_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200581 LY_CHECK_GOTO(ret, cleanup);
582 } else if (lydctx->parse_options & LYD_PARSE_STRICT) {
583 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200584 "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".",
585 meta->prefix.id, meta->prefix.id, ly_strlen(meta->prefix.id) ? ":" : "", meta->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200586 ret = LY_EVALID;
587 goto cleanup;
588 }
589 }
590 /* add/correct flags */
Michal Vasko49c39d82020-11-06 17:20:27 +0100591 lyd_parse_set_data_flags(node, &lydctx->node_when, &node->meta, lydctx->parse_options);
Radek Krejci1798aae2020-07-14 13:26:06 +0200592
593 /* done */
594 break;
595 }
596 }
597
598 if (match != instance) {
599 /* there is no corresponding data node for the metadata */
600 if (instance > 1) {
601 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200602 "Missing %d%s JSON data instance to be coupled with %s metadata.", instance,
603 instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200604 } else {
605 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, *first_p ? (*first_p)->parent : NULL, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200606 "Missing JSON data instance to be coupled with %s metadata.", meta_container->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200607 }
608 ret = LY_EVALID;
609 } else {
610 /* remove the opaq attr */
611 if (attr == (*first_p)) {
612 *first_p = attr->next;
613 }
614 lyd_free_tree(attr);
615 }
616 }
617
618cleanup:
619 lydict_remove(lydctx->jsonctx->ctx, prev);
620
621 return ret;
622}
623
624/**
625 * @brief Parse a metadata member.
626 *
627 * @param[in] lydctx JSON data parser context.
628 * @param[in] snode Schema node of the metadata parent.
629 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
630 * so the data node does not exists. In such a case the metadata is stored in the context for the later
631 * processing by lydjson_metadata_finish().
632 * @return LY_SUCCESS on success
633 * @return Various LY_ERR values in case of failure.
634 */
635static LY_ERR
636lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
637{
638 LY_ERR ret = LY_SUCCESS;
639 enum LYJSON_PARSER_STATUS status;
640 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200641 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200642 const char *name, *prefix = NULL;
643 size_t name_len, prefix_len = 0;
644 struct lys_module *mod;
645 struct lyd_meta *meta = NULL;
646 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200647 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200648 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200649 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200650 uint16_t nodetype;
651
652 assert(snode || node);
653
654 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
655
656 /* move to the second item in the name/X pair */
657 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
658 LY_CHECK_GOTO(ret, cleanup);
659
660 /* check attribute encoding */
661 switch (nodetype) {
662 case LYS_LEAFLIST:
663 expected = "@name/array of objects/nulls";
664
665 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
666
667next_entry:
668 instance++;
669
670 /* move into array / next entry */
671 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
672 LY_CHECK_GOTO(ret, cleanup);
673
674 if (status == LYJSON_ARRAY_CLOSED) {
675 /* we are done, move after the array */
676 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
677 goto cleanup;
678 }
679 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
680
Michal Vasko69730152020-10-09 16:30:07 +0200681 if (!node || (node->schema != prev->schema)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200682 LOGVAL(lydctx->jsonctx->ctx, LY_VLOG_LYD, prev->parent, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200683 "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.",
684 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200685 ret = LY_EVALID;
686 goto cleanup;
687 }
688
689 if (status == LYJSON_NULL) {
690 /* continue with the next entry in the leaf-list array */
691 prev = node;
692 node = node->next;
693 goto next_entry;
694 }
695 break;
696 case LYS_LEAF:
697 case LYS_ANYXML:
698 expected = "@name/object";
699
700 LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error);
701 break;
702 case LYS_CONTAINER:
703 case LYS_LIST:
704 case LYS_ANYDATA:
705 case LYS_NOTIF:
706 case LYS_ACTION:
707 case LYS_RPC:
708 in_parent = 1;
709 expected = "@/object";
710 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
711 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200712 default:
713 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 }
715
716 /* process all the members inside a single metadata object */
717 assert(status == LYJSON_OBJECT);
718
719 while (status != LYJSON_OBJECT_CLOSED) {
720 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
721 if (!prefix) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200722 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200723 "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
724 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200725 ret = LY_EVALID;
726 goto cleanup;
727 } else if (is_attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200728 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200729 "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
730 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200731 ret = LY_EVALID;
732 goto cleanup;
733 }
734
735 /* get the element module */
736 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
737 if (!mod) {
738 if (lydctx->parse_options & LYD_PARSE_STRICT) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200739 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200740 "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
741 prefix_len, prefix, name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200742 ret = LY_EVALID;
743 goto cleanup;
744 }
745 if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
746 /* skip element with children */
747 ret = lydjson_data_skip(lydctx->jsonctx);
748 LY_CHECK_GOTO(ret, cleanup);
749 status = lyjson_ctx_status(lydctx->jsonctx, 0);
750 /* end of the item */
751 continue;
752 }
753 }
754
755 /* get the value */
756 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
757 LY_CHECK_GOTO(ret, cleanup);
758
759 if (node->schema) {
760 /* create metadata */
761 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200762 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +0200763 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
764 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200765 LY_CHECK_GOTO(ret, cleanup);
766
767 /* add/correct flags */
Michal Vasko49c39d82020-11-06 17:20:27 +0100768 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_options);
Radek Krejci1798aae2020-07-14 13:26:06 +0200769 } else {
770 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200771 const char *module_name;
772 size_t module_name_len;
773
774 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
775
Radek Krejci1798aae2020-07-14 13:26:06 +0200776 /* attr2 is always changed to the created attribute */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100777 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, lydctx->jsonctx->value,
778 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, 0, NULL, prefix, prefix_len,
779 module_name, module_name_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200780 LY_CHECK_GOTO(ret, cleanup);
781 }
782 /* next member */
783 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
784 LY_CHECK_GOTO(ret, cleanup);
785 }
786
787 if (nodetype == LYS_LEAFLIST) {
788 /* continue by processing another metadata object for the following
789 * leaf-list instance since they are allways instantiated in JSON array */
790 prev = node;
791 node = node->next;
792 goto next_entry;
793 }
794
795 /* move after the metadata */
796 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
797 LY_CHECK_GOTO(ret, cleanup);
798
799cleanup:
800 return ret;
801
802representation_error:
Michal Vasko22df3f02020-08-24 13:29:22 +0200803 LOGVAL(ctx, LY_VLOG_LYD, (void *)node, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200804 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
805 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name,
806 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200807
808 return LY_EVALID;
809}
810
811/**
812 * @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.
813 *
814 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
815 * @param[in, out] first_p Pointer to the first sibling node in case of top-level.
816 * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
817 */
818static void
819lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p)
820{
821 if (*node_p) {
822 /* insert, keep first pointer correct */
823 lyd_insert_node((struct lyd_node *)parent, first_p, *node_p);
824 if (first_p) {
825 if (parent) {
826 *first_p = parent->child;
827 } else {
828 while ((*first_p)->prev->next) {
829 *first_p = (*first_p)->prev;
830 }
831 }
832 }
833 *node_p = NULL;
834 }
835}
836
837static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p);
838
839/**
840 * @brief Parse opaq node from the input.
841 *
842 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
843 *
844 * @param[in] lydctx JSON data parser context.
845 * @param[in] name Name of the opaq node to create.
846 * @param[in] name_len Length of the @p name string.
847 * @param[in] prefix Prefix of the opaq node to create.
848 * @param[in] prefix_len Length of the @p prefx string.
849 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
850 * but must be set if @p first is not.
851 * @param[in,out] status_p Pointer to the current status of the parser context,
852 * since the function manipulates with the context and process the input, the status can be updated.
853 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
854 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
855 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
856 * @param[out] node_p Pointer to the created opaq node.
857 * @return LY_ERR value.
858 */
859static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200860lydjson_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 +0200861 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
862 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200863{
864 LY_ERR ret = LY_SUCCESS;
865 const char *value = NULL, *module_name;
866 size_t value_len = 0, module_name_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200867 ly_bool dynamic = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200868 uint32_t type_hint = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200869
Michal Vasko69730152020-10-09 16:30:07 +0200870 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200871 /* prepare for creating opaq node with a value */
872 value = lydctx->jsonctx->value;
873 value_len = lydctx->jsonctx->value_len;
874 dynamic = lydctx->jsonctx->dynamic;
875 lydctx->jsonctx->dynamic = 0;
876
877 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
Radek Krejci1798aae2020-07-14 13:26:06 +0200878 }
879
880 /* create node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200881 lydjson_get_node_prefix((struct lyd_node *)parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100882 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, value, value_len, &dynamic, LY_PREF_JSON, type_hint,
883 NULL, prefix, prefix_len, module_name, module_name_len, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200884 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200885 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200886 }
Radek Krejcid46e46a2020-09-15 14:22:42 +0200887 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200888
Michal Vasko69730152020-10-09 16:30:07 +0200889 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200890 /* process children */
891 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
892 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
893 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
894 }
Michal Vasko69730152020-10-09 16:30:07 +0200895 } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200896 /* process another instance of the same node */
897 /* but first mark the node to be expected a list or a leaf-list */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200898 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +0200899
Michal Vasko69730152020-10-09 16:30:07 +0200900 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200901 /* but first process children of the object in the array */
902 while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) {
903 LY_CHECK_RET(lydjson_subtree_r(lydctx, (struct lyd_node_inner *)(*node_p), lyd_node_children_p(*node_p)));
904 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
905 }
906 }
907
908 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
909
910 /* continue with the next instance */
911 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
912 assert(node_p);
913 lydjson_maintain_children(parent, first_p, node_p);
914 return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, first_p, node_p);
915 }
916 }
917
918 /* finish linking metadata */
919 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_children_p(*node_p)));
920
921 /* move after the item */
922 return lyjson_ctx_next(lydctx->jsonctx, status_p);
923}
924
925/**
926 * @brief Process the attribute container (starting by @)
927 *
928 * @param[in] lydctx JSON data parser context.
929 * @param[in] attr_node The data node referenced by the attribute container, if already known.
930 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
931 * @param[in] name Name of the opaq node to create.
932 * @param[in] name_len Length of the @p name string.
933 * @param[in] prefix Prefix of the opaq node to create.
934 * @param[in] prefix_len Length of the @p prefx string.
935 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
936 * but must be set if @p first is not.
937 * @param[in,out] status_p Pointer to the current status of the parser context,
938 * since the function manipulates with the context and process the input, the status can be updated.
939 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
940 * @param[out] node_p Pointer to the created opaq node.
941 * @return LY_ERR value.
942 */
943static LY_ERR
944lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Radek Krejci0f969882020-08-21 16:56:47 +0200945 const char *name, size_t name_len, const char *prefix, size_t prefix_len,
946 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p,
947 struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200948{
949 LY_ERR ret = LY_SUCCESS;
950 enum LYJSON_PARSER_STATUS status_inner;
951
952 /* parse as an attribute to a node */
953 if (!attr_node && snode) {
954 /* try to find the instance */
955 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
956 if (iter->schema == snode) {
957 attr_node = iter;
958 break;
959 }
960 }
961 }
962 if (!attr_node) {
963 /* parse just as an opaq node with the name beginning with @,
964 * later we have to check that it belongs to a standard node
965 * and it is supposed to be converted to a metadata */
966 uint32_t prev_opts;
967
968 /* move into metadata */
969 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
970
971 if (*status_p == LYJSON_ARRAY) {
972 /* move into the array */
973 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
974 } else {
975 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
976 status_inner = LYJSON_ERROR;
977 }
978
979 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
980 prev_opts = lydctx->parse_options;
981 lydctx->parse_options &= ~LYD_PARSE_STRICT;
982 lydctx->parse_options |= LYD_PARSE_OPAQ;
983
984 ret = lydjson_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
Michal Vasko69730152020-10-09 16:30:07 +0200985 NULL, 0, parent, status_p, status_inner == LYJSON_ERROR ? status_p : &status_inner, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200986
987 /* restore the parser options */
988 lydctx->parse_options = prev_opts;
989 } else {
990 ret = lydjson_metadata(lydctx, snode, attr_node);
991 }
992
993 return ret;
994}
995
996/**
Michal Vasko32ac9942020-08-12 14:35:12 +0200997 * @brief Parse a single instance of a node.
998 *
999 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1000 * as before calling, despite it is necessary to process input data for checking.
1001 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1002 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1003 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1004 * @param[in] name Parsed JSON node name.
1005 * @param[in] name_len Lenght of @p name.
1006 * @param[in] prefix Parsed JSON node prefix.
1007 * @param[in] prefix_len Length of @p prefix.
1008 * @param[in,out] status JSON parser status, is updated.
1009 * @param[out] node Parsed data (or opaque) node.
1010 * @return LY_SUCCESS if a node was successfully parsed,
1011 * @return LY_EINVAL in case of invalid JSON encoding,
1012 * @return LY_ERR on other errors.
1013 */
1014static LY_ERR
1015lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001016 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1017 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001018{
1019 LY_ERR ret;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001020 uint32_t type_hints = 0;
Michal Vasko32ac9942020-08-12 14:35:12 +02001021 uint32_t prev_opts;
1022 struct lyd_node *tree = NULL;
1023
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001024 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001025 if (ret == LY_SUCCESS) {
1026 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1027 if (snode->nodetype & LYD_NODE_TERM) {
1028 /* create terminal node */
1029 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Michal Vasko69730152020-10-09 16:30:07 +02001030 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_PREF_JSON, NULL,
1031 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001032 LY_CHECK_RET(ret);
1033
1034 /* move JSON parser */
1035 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1036 LY_CHECK_RET(ret);
1037 } else if (snode->nodetype & LYD_NODE_INNER) {
1038 /* create inner node */
1039 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1040
1041 ret = lyd_create_inner(snode, node);
1042 LY_CHECK_RET(ret);
1043
1044 /* process children */
1045 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1046 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)*node, lyd_node_children_p(*node));
1047 LY_CHECK_RET(ret);
1048 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1049 }
1050
1051 /* finish linking metadata */
1052 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(*node));
1053 LY_CHECK_RET(ret);
1054
1055 if (snode->nodetype == LYS_LIST) {
1056 /* check all keys exist */
1057 ret = lyd_parse_check_keys(*node);
1058 LY_CHECK_RET(ret);
1059 }
1060
1061 if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
1062 /* new node validation, autodelete CANNOT occur, all nodes are new */
1063 ret = lyd_validate_new(lyd_node_children_p(*node), snode, NULL, NULL);
1064 LY_CHECK_RET(ret);
1065
1066 /* add any missing default children */
Michal Vasko49c39d82020-11-06 17:20:27 +01001067 ret = lyd_new_implicit_r(*node, lyd_node_children_p(*node), NULL, NULL, &lydctx->node_types,
1068 &lydctx->node_when, (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ?
Michal Vasko69730152020-10-09 16:30:07 +02001069 LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001070 LY_CHECK_RET(ret);
1071 }
1072
1073 /* move JSON parser */
1074 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1075 LY_CHECK_RET(ret);
1076 } else if (snode->nodetype & LYD_NODE_ANY) {
1077 /* create any node */
1078 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1079
1080 /* parse any data tree with correct options */
1081 /* first backup the current options and then make the parser to process data as opaq nodes */
1082 prev_opts = lydctx->parse_options;
1083 lydctx->parse_options &= ~LYD_PARSE_STRICT;
1084 lydctx->parse_options |= LYD_PARSE_OPAQ;
1085
1086 /* process the anydata content */
1087 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
1088 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1089 LY_CHECK_RET(ret);
1090 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1091 }
1092
1093 /* restore parser options */
1094 lydctx->parse_options = prev_opts;
1095
1096 /* finish linking metadata */
1097 ret = lydjson_metadata_finish(lydctx, &tree);
1098 LY_CHECK_RET(ret);
1099
1100 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, node);
1101 LY_CHECK_RET(ret);
1102 }
1103 } else if (ret == LY_ENOT) {
1104 /* parse it again as an opaq node */
1105 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001106 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001107 LY_CHECK_RET(ret);
1108
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001109 if (snode->nodetype == LYS_LIST) {
1110 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1111 } else if (snode->nodetype == LYS_LEAFLIST) {
1112 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001113 }
1114 }
1115
1116 return ret;
1117}
1118
1119/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001120 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001121 *
1122 * @param[in] lydctx JSON data parser context.
1123 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1124 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1125 * @return LY_ERR value.
1126 */
1127static LY_ERR
1128lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p)
1129{
1130 LY_ERR ret = LY_SUCCESS;
1131 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
1132 enum LYJSON_PARSER_STATUS status_inner = 0;
1133 const char *name, *prefix = NULL;
1134 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001135 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001136 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001137 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001138 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
1139 const char *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001140
1141 assert(parent || first_p);
1142 assert(status == LYJSON_OBJECT);
1143
1144 /* process the node name */
Michal Vaskocabe0702020-08-12 10:14:36 +02001145 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 +02001146
Michal Vaskocabe0702020-08-12 10:14:36 +02001147 if (!is_meta || name_len || prefix_len) {
1148 /* get the schema node */
1149 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1150 if (ret == LY_ENOT) {
1151 /* skip element with children */
1152 ret = lydjson_data_skip(lydctx->jsonctx);
1153 LY_CHECK_GOTO(ret, cleanup);
1154 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1155 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001156 goto cleanup;
1157 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001158 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001159
Michal Vaskocabe0702020-08-12 10:14:36 +02001160 if (!snode) {
1161 /* we will not be parsing it as metadata */
1162 is_meta = 0;
1163 }
1164 }
1165
1166 if (is_meta) {
1167 /* parse as metadata */
1168 if (!name_len && !prefix_len) {
1169 /* parent's metadata without a name - use the schema from the parent */
1170 if (!parent) {
1171 LOGVAL(ctx, LY_VLOG_LYD, NULL, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001172 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001173 ret = LY_EVALID;
1174 goto cleanup;
1175 }
1176 attr_node = (struct lyd_node *)parent;
1177 snode = attr_node->schema;
1178 }
1179 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001180 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001181 LY_CHECK_GOTO(ret, cleanup);
1182 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001183 /* parse as an opaq node */
1184 assert((lydctx->parse_options & LYD_PARSE_OPAQ) || (lydctx->int_opts));
1185
1186 /* move to the second item in the name/X pair */
1187 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1188 LY_CHECK_GOTO(ret, cleanup);
1189
1190 if (status == LYJSON_ARRAY) {
1191 /* move into the array */
1192 ret = lyjson_ctx_next(lydctx->jsonctx, &status_inner);
1193 LY_CHECK_GOTO(ret, cleanup);
1194 } else {
1195 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1196 status_inner = LYJSON_ERROR;
1197 }
1198
1199 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001200 parent, &status, status_inner == LYJSON_ERROR ? &status : &status_inner, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001201 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001202 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001203 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001204
1205 /* move to the second item in the name/X pair */
1206 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1207 LY_CHECK_GOTO(ret, cleanup);
1208
1209 /* first check the expected representation according to the nodetype and then continue with the content */
1210 switch (snode->nodetype) {
1211 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001212 case LYS_LIST:
1213 if (snode->nodetype == LYS_LEAFLIST) {
1214 expected = "name/array of values";
1215 } else {
1216 expected = "name/array of objects";
1217 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001218
1219 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1220
1221 /* move into array */
1222 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1223 LY_CHECK_GOTO(ret, cleanup);
1224
Michal Vasko32ac9942020-08-12 14:35:12 +02001225 /* process all the values/objects */
1226 do {
1227 lydjson_maintain_children(parent, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001228
Michal Vasko32ac9942020-08-12 14:35:12 +02001229 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
Michal Vasko69730152020-10-09 16:30:07 +02001230 &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001231 if (ret == LY_EINVAL) {
1232 goto representation_error;
1233 } else if (ret) {
1234 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001235 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001236 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001237
Michal Vasko32ac9942020-08-12 14:35:12 +02001238 /* move after the array */
1239 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1240 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001241
1242 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001243 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001244 case LYS_CONTAINER:
1245 case LYS_NOTIF:
1246 case LYS_ACTION:
1247 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001248 case LYS_ANYDATA:
1249 case LYS_ANYXML:
1250 if (snode->nodetype == LYS_LEAF) {
1251 if (status == LYJSON_ARRAY) {
1252 expected = "name/[null]";
1253 } else {
1254 expected = "name/value";
1255 }
1256 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001257 expected = "name/object";
1258 }
1259
Michal Vasko32ac9942020-08-12 14:35:12 +02001260 /* process the value/object */
1261 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1262 if (ret == LY_EINVAL) {
1263 goto representation_error;
1264 } else if (ret) {
1265 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001266 }
1267
Michal Vasko32ac9942020-08-12 14:35:12 +02001268 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001269 /* rememeber the RPC/action/notification */
1270 lydctx->op_node = node;
1271 }
1272
1273 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001274 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001275 }
1276
Michal Vasko32ac9942020-08-12 14:35:12 +02001277 /* finally connect the parsed node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001278 lydjson_maintain_children(parent, first_p, &node);
1279
1280cleanup:
1281 lyd_free_tree(node);
1282 return ret;
1283
1284representation_error:
1285 LOGVAL(ctx, LY_VLOG_LYD, parent, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001286 "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
1287 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejci1798aae2020-07-14 13:26:06 +02001288
1289 ret = LY_EVALID;
1290 goto cleanup;
1291}
1292
1293/**
1294 * @brief Common start of JSON parser processing different types of the input data.
1295 *
1296 * @param[in] ctx libyang context
1297 * @param[in] in Input structure.
1298 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
1299 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
1300 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001301 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001302 * @return LY_ERR value.
1303 */
1304static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001305lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001306 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001307{
1308 LY_ERR ret = LY_SUCCESS;
1309 struct lyd_json_ctx *lydctx;
1310 size_t i, line = 1;
1311
Radek Krejcicd5f6222020-11-12 08:54:13 +01001312 assert(lydctx_p);
1313 assert(status);
1314
Radek Krejci1798aae2020-07-14 13:26:06 +02001315 /* init context */
1316 lydctx = calloc(1, sizeof *lydctx);
1317 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1318 lydctx->parse_options = parse_options;
1319 lydctx->validate_options = validate_options;
1320 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001321
Radek Krejci284f31f2020-09-18 15:40:29 +02001322 /* starting top-level */
1323 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
1324 if (in->current[i] == 0x0a) { /* new line */
1325 line++;
Michal Vasko61d76362020-10-07 10:47:30 +02001326 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001327 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001328
Radek Krejci284f31f2020-09-18 15:40:29 +02001329 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001330 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1331 if (*status == LYJSON_END || *status == LYJSON_OBJECT_EMPTY || *status == LYJSON_OBJECT) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001332 *lydctx_p = lydctx;
1333 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001334 } else {
1335 /* expecting top-level object */
1336 LOGVAL(ctx, LY_VLOG_LINE, &line, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.",
1337 lyjson_token2str(*status));
1338 *lydctx_p = NULL;
1339 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001340 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001341}
1342
1343LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001344lyd_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 +02001345 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001346{
1347 LY_ERR ret = LY_SUCCESS;
1348 struct lyd_json_ctx *lydctx = NULL;
1349 enum LYJSON_PARSER_STATUS status;
1350
1351 assert(tree_p);
1352 *tree_p = NULL;
1353
Radek Krejcicd5f6222020-11-12 08:54:13 +01001354 ret = lyd_parse_json_init(ctx, in, parse_options, validate_options, &lydctx, &status);
1355 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001356
Radek Krejcicd5f6222020-11-12 08:54:13 +01001357 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001358
1359 /* read subtree(s) */
1360 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
1361 ret = lydjson_subtree_r(lydctx, NULL, tree_p);
1362 LY_CHECK_GOTO(ret, cleanup);
1363
1364 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1365 }
1366
1367 /* finish linking metadata */
1368 ret = lydjson_metadata_finish(lydctx, tree_p);
1369 LY_CHECK_GOTO(ret, cleanup);
1370
1371cleanup:
1372 /* there should be no unresolved types stored */
Michal Vasko49c39d82020-11-06 17:20:27 +01001373 assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1374 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001375
1376 if (ret) {
1377 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1378 lyd_free_all(*tree_p);
1379 *tree_p = NULL;
1380 } else {
1381 *lydctx_p = (struct lyd_ctx *)lydctx;
1382 }
1383
1384 return ret;
1385}
1386
1387/**
1388 * @brief Parse optional JSON envelope around the Notification data, including the eventTime data.
1389 *
1390 * @param[in] jsonctx JSON parser context
1391 * @param[out] envp_p Pointer to the created envelope opaq container.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001392 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001393 * @return LY_SUCCESS if the envelope present and successfully parsed.
1394 * @return LY_ENOT in case there is not the expected envelope.
1395 * @return LY_ERR in case of parsing failure.
1396 */
1397static LY_ERR
Radek Krejcicd5f6222020-11-12 08:54:13 +01001398lydjson_notif_envelope(struct lyjson_ctx *jsonctx, struct lyd_node **envp_p, enum LYJSON_PARSER_STATUS *status_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001399{
1400 LY_ERR ret = LY_ENOT, r;
1401 const char *name, *prefix, *value = NULL;
1402 size_t name_len, prefix_len, value_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001403 ly_bool is_attr, dynamic = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001404 enum LYJSON_PARSER_STATUS status;
1405 struct lyd_node *et;
1406
1407 *envp_p = NULL;
1408
1409 /* backup the context */
1410 lyjson_ctx_backup(jsonctx);
1411
1412 /* "notification" envelope */
1413 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1414 LY_CHECK_GOTO(is_attr, cleanup);
1415 LY_CHECK_GOTO(prefix_len != 13 || strncmp(prefix, "ietf-restconf", 13), cleanup);
1416 LY_CHECK_GOTO(name_len != 12 || strncmp(name, "notification", name_len), cleanup);
1417
1418 r = lyjson_ctx_next(jsonctx, &status);
1419 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1420 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1421
1422 /* "eventTime" child */
1423 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1424 LY_CHECK_GOTO(prefix_len || is_attr, cleanup);
1425 LY_CHECK_GOTO(name_len != 9 || strncmp(name, "eventTime", name_len), cleanup);
1426
1427 /* go for the data */
1428 r = lyjson_ctx_next(jsonctx, &status);
1429 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1430 LY_CHECK_GOTO(status != LYJSON_STRING, cleanup);
1431
1432 value = jsonctx->value;
1433 value_len = jsonctx->value_len;
1434 dynamic = jsonctx->dynamic;
1435 jsonctx->dynamic = 0;
1436
1437 r = lyjson_ctx_next(jsonctx, &status);
1438 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
1439 LY_CHECK_GOTO(status != LYJSON_OBJECT, cleanup);
1440 /* now the notificationContent is expected, which will be parsed by the caller */
1441
1442 /* create notification envelope */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001443 ret = lyd_create_opaq(jsonctx->ctx, "notification", 12, "", 0, NULL, LY_PREF_JSON, LYD_NODEHINT_ENVELOPE, NULL, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001444 0, "ietf-restconf", 13, envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001445 LY_CHECK_GOTO(ret, cleanup);
1446 /* create notification envelope */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001447 ret = lyd_create_opaq(jsonctx->ctx, "eventTime", 9, value, value_len, &dynamic, LY_PREF_JSON, LYD_VALHINT_STRING, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001448 NULL, 0, "ietf-restconf", 13, &et);
Radek Krejci1798aae2020-07-14 13:26:06 +02001449 LY_CHECK_GOTO(ret, cleanup);
1450 /* insert eventTime into notification */
1451 lyd_insert_node(*envp_p, NULL, et);
1452
1453 ret = LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001454 *status_p = status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001455cleanup:
1456 if (ret) {
1457 /* restore the context */
1458 lyjson_ctx_restore(jsonctx);
1459 if (dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001460 free((char *)value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001461 }
1462 }
1463 return ret;
1464}
1465
1466LY_ERR
1467lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
1468{
1469 LY_ERR ret = LY_SUCCESS;
1470 struct lyd_json_ctx *lydctx = NULL;
1471 struct lyd_node *ntf_e = NULL;
1472 struct lyd_node *tree = NULL;
1473 enum LYJSON_PARSER_STATUS status;
1474
1475 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001476 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1477 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1478
Radek Krejci1798aae2020-07-14 13:26:06 +02001479 lydctx->int_opts = LYD_INTOPT_NOTIF;
1480
Radek Krejci1798aae2020-07-14 13:26:06 +02001481 /* parse "notification" and "eventTime", if present */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001482 ret = lydjson_notif_envelope(lydctx->jsonctx, &ntf_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001483 if (ret == LY_ENOT) {
1484 ret = LY_SUCCESS;
1485 } else if (ret) {
1486 goto cleanup;
1487 }
1488
Radek Krejcicd5f6222020-11-12 08:54:13 +01001489 assert(status == LYJSON_OBJECT);
1490
Michal Vaskocf770e22020-08-12 13:21:43 +02001491 /* read subtree */
1492 ret = lydjson_subtree_r(lydctx, NULL, &tree);
1493 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001494
1495 /* finish linking metadata */
1496 ret = lydjson_metadata_finish(lydctx, &tree);
1497 LY_CHECK_GOTO(ret, cleanup);
1498
1499 /* make sure we have parsed some notification */
1500 if (!lydctx->op_node) {
1501 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
1502 ret = LY_EVALID;
1503 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001504 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001505 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001506 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001507 ret = LY_EVALID;
1508 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001509 }
1510
1511 if (ntf_e) {
1512 /* finish notification envelope */
1513 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1514 LY_CHECK_GOTO(ret, cleanup);
1515 if (status == LYJSON_END) {
1516 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LY_VCODE_EOF);
1517 ret = LY_EVALID;
1518 goto cleanup;
1519 } else if (status != LYJSON_OBJECT_CLOSED) {
1520 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"notification\".",
Michal Vasko69730152020-10-09 16:30:07 +02001521 lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001522 ret = LY_EVALID;
1523 goto cleanup;
1524 }
1525 }
1526
1527 if (ntf_p) {
1528 *ntf_p = lydctx->op_node;
1529 }
1530 assert(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001531 if (ntf_e) {
1532 /* connect to the notification */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001533 lyd_insert_node(ntf_e, NULL, tree);
1534 tree = ntf_e;
1535 }
1536 if (tree_p) {
1537 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001538 }
1539
1540cleanup:
1541 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001542 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001543
1544 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1545 if (ret) {
1546 lyd_free_all(tree);
1547 lyd_free_tree(ntf_e);
1548 }
1549 return ret;
1550}
1551
1552/**
1553 * @brief Parse optional JSON envelope around the processed content.
1554 *
1555 * @param[in] jsonctx JSON parser context
1556 * @param[in] parent Parent node (some other envelope).
1557 * @param[in] module_key Name of the module where the envelope element is expected.
1558 * @param[in] object_id Name of the envelope object.
1559 * @param[out] envp_p Pointer to the created envelope opaq container.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001560 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001561 * @return LY_SUCCESS if the envelope present and successfully parsed.
1562 * @return LY_ENOT in case there is not the expected envelope.
1563 * @return LY_ERR in case of parsing failure.
1564 */
1565static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001566lydjson_object_envelope(struct lyjson_ctx *jsonctx, struct lyd_node *parent, const char *module_key,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001567 const char *object_id, struct lyd_node **envp_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001568{
1569 LY_ERR ret = LY_ENOT, r;
1570 const char *name, *prefix;
1571 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001572 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +02001573
1574 *envp_p = NULL;
1575
1576 /* "id" envelope */
1577 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
1578 LY_CHECK_GOTO(is_attr, cleanup);
1579 LY_CHECK_GOTO(lydjson_get_node_prefix(parent, prefix, prefix_len, &prefix, &prefix_len), cleanup);
1580 LY_CHECK_GOTO(prefix_len != strlen(module_key) || strncmp(prefix, module_key, prefix_len), cleanup);
1581 LY_CHECK_GOTO(name_len != strlen(object_id) || strncmp(name, object_id, name_len), cleanup);
1582
Radek Krejcicd5f6222020-11-12 08:54:13 +01001583 r = lyjson_ctx_next(jsonctx, status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001584 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001585 LY_CHECK_GOTO(*status != LYJSON_OBJECT, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001586
1587 /* create the object envelope */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001588 ret = lyd_create_opaq(jsonctx->ctx, object_id, strlen(object_id), "", 0, NULL, LY_PREF_JSON, LYD_NODEHINT_ENVELOPE,
Michal Vasko69730152020-10-09 16:30:07 +02001589 NULL, NULL, 0, module_key, ly_strlen(module_key), envp_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001590 LY_CHECK_GOTO(ret, cleanup);
1591
1592 if (parent) {
1593 lyd_insert_node(parent, NULL, *envp_p);
1594 }
1595
1596 ret = LY_SUCCESS;
1597cleanup:
1598 return ret;
1599}
1600
1601static LY_ERR
Radek Krejcicd5f6222020-11-12 08:54:13 +01001602lydjson_object_envelope_close(struct lyjson_ctx *jsonctx, const char *object_id, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001603{
Radek Krejcicd5f6222020-11-12 08:54:13 +01001604 LY_CHECK_RET(lyjson_ctx_next(jsonctx, status));
1605 if (*status == LYJSON_END) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001606 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LY_VCODE_EOF);
1607 return LY_EVALID;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001608 } else if (*status != LYJSON_OBJECT_CLOSED) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001609 LOGVAL(jsonctx->ctx, LY_VLOG_LINE, &jsonctx->line, LYVE_SYNTAX, "Unexpected sibling member \"%.*s\" of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001610 jsonctx->value_len, jsonctx->value, object_id);
Radek Krejci1798aae2020-07-14 13:26:06 +02001611 return LY_EVALID;
1612 }
1613 return LY_SUCCESS;
1614}
1615
1616LY_ERR
1617lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1618{
1619 LY_ERR ret = LY_SUCCESS;
1620 struct lyd_json_ctx *lydctx = NULL;
1621 struct lyd_node *rpc_e = NULL, *act_e = NULL;
1622 struct lyd_node *tree = NULL;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001623 enum LYJSON_PARSER_STATUS status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001624
1625 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001626 ret = lyd_parse_json_init(ctx, in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1627 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1628
Radek Krejci1798aae2020-07-14 13:26:06 +02001629 lydctx->int_opts = LYD_INTOPT_RPC;
1630
1631 /* process envelope(s), if present */
1632
1633 /* process rpc */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001634 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc", &rpc_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001635 if (ret == LY_ENOT) {
1636 ret = LY_SUCCESS;
1637 goto parse_content;
1638 } else if (ret) {
1639 goto cleanup;
1640 }
1641 /* process action */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001642 ret = lydjson_object_envelope(lydctx->jsonctx, rpc_e, "yang", "action", &act_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001643 if (ret == LY_ENOT) {
1644 ret = LY_SUCCESS;
1645 goto parse_content;
1646 } else if (ret) {
1647 goto cleanup;
1648 }
1649
1650parse_content:
Radek Krejcicd5f6222020-11-12 08:54:13 +01001651 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001652
1653 /* read subtree(s) */
Michal Vasko22df3f02020-08-24 13:29:22 +02001654 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 +02001655 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001656
1657 /* finish linking metadata */
1658 ret = lydjson_metadata_finish(lydctx, &tree);
1659 LY_CHECK_GOTO(ret, cleanup);
1660
1661 /* make sure we have parsed some operation */
1662 if (!lydctx->op_node) {
1663 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the %s node.",
Michal Vasko69730152020-10-09 16:30:07 +02001664 act_e ? "action" : (rpc_e ? "rpc" : "rpc/action"));
Radek Krejci1798aae2020-07-14 13:26:06 +02001665 ret = LY_EVALID;
1666 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02001667 } else if (lydctx->jsonctx->in->current[0] && (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskocf770e22020-08-12 13:21:43 +02001668 LOGVAL(ctx, LY_VLOG_LINE, &lydctx->jsonctx->line, LYVE_SYNTAX, "Unexpected sibling element of \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02001669 tree->schema->name);
Michal Vaskocf770e22020-08-12 13:21:43 +02001670 ret = LY_EVALID;
1671 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001672 }
1673
1674 if (act_e) {
1675 /* finish action envelope */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001676 ret = lydjson_object_envelope_close(lydctx->jsonctx, "action", &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001677 LY_CHECK_GOTO(ret, cleanup);
1678 if (lydctx->op_node->schema->nodetype != LYS_ACTION) {
1679 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001680 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001681 ret = LY_EVALID;
1682 goto cleanup;
1683 }
1684 }
1685 if (rpc_e) {
1686 /* finish rpc envelope */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001687 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc", &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001688 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko69730152020-10-09 16:30:07 +02001689 if (!act_e && (lydctx->op_node->schema->nodetype != LYS_RPC)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001690 LOGVAL(ctx, LY_VLOG_LYD, lydctx->op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
Michal Vasko69730152020-10-09 16:30:07 +02001691 lys_nodetype2str(lydctx->op_node->schema->nodetype));
Radek Krejci1798aae2020-07-14 13:26:06 +02001692 ret = LY_EVALID;
1693 goto cleanup;
1694 }
1695 }
1696
1697 if (op_p) {
1698 *op_p = lydctx->op_node;
1699 }
1700 assert(tree);
1701 if (tree_p) {
1702 if (rpc_e) {
1703 *tree_p = rpc_e;
1704 } else if (act_e) {
1705 *tree_p = act_e;
1706 } else {
1707 *tree_p = tree;
1708 }
1709 }
1710
1711cleanup:
1712 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001713 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001714
1715 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1716 if (ret) {
1717 lyd_free_all(tree);
1718 lyd_free_tree(act_e);
1719 lyd_free_tree(rpc_e);
1720 }
1721 return ret;
1722}
1723
1724LY_ERR
1725lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
1726{
1727 LY_ERR ret = LY_SUCCESS;
1728 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4189c0f2020-08-13 09:05:22 +02001729 struct lyd_node *rpcr_e = NULL, *tree, *req_op, *rep_op = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001730 enum LYJSON_PARSER_STATUS status;
1731
1732 /* init */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001733 ret = lyd_parse_json_init(LYD_CTX(request), in, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &lydctx, &status);
1734 LY_CHECK_GOTO(ret || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
1735
Radek Krejci1798aae2020-07-14 13:26:06 +02001736 lydctx->int_opts = LYD_INTOPT_REPLY;
1737
1738 /* find request OP */
1739 LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
1740 if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1741 break;
1742 }
1743 LYD_TREE_DFS_END(request, req_op);
1744 }
1745 if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001746 LOGERR(LYD_CTX(request), LY_EINVAL, "No RPC/action in the request found.");
Radek Krejci1798aae2020-07-14 13:26:06 +02001747 ret = LY_EINVAL;
1748 goto cleanup;
1749 }
1750
1751 /* duplicate request OP with parents */
1752 LY_CHECK_GOTO(ret = lyd_dup_single(req_op, NULL, LYD_DUP_WITH_PARENTS, &rep_op), cleanup);
1753
1754 /* parse "rpc-reply", if any */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001755 ret = lydjson_object_envelope(lydctx->jsonctx, NULL, "ietf-netconf", "rpc-reply", &rpcr_e, &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001756 if (ret == LY_ENOT) {
1757 ret = LY_SUCCESS;
1758 } else if (ret) {
1759 goto cleanup;
1760 }
1761
Radek Krejci1798aae2020-07-14 13:26:06 +02001762 assert(status == LYJSON_OBJECT);
1763
1764 /* read subtree(s) */
1765 while (lydctx->jsonctx->in->current[0] && status != LYJSON_OBJECT_CLOSED) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001766 ret = lydjson_subtree_r(lydctx, (struct lyd_node_inner *)rep_op, lyd_node_children_p(rep_op));
Radek Krejci1798aae2020-07-14 13:26:06 +02001767 LY_CHECK_GOTO(ret, cleanup);
1768
1769 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1770 }
1771
1772 /* finish linking metadata */
1773 ret = lydjson_metadata_finish(lydctx, lyd_node_children_p(rep_op));
1774 LY_CHECK_GOTO(ret, cleanup);
1775
1776 if (rpcr_e) {
1777 /* finish rpc-reply envelope */
Radek Krejcicd5f6222020-11-12 08:54:13 +01001778 ret = lydjson_object_envelope_close(lydctx->jsonctx, "rpc-reply", &status);
Radek Krejci1798aae2020-07-14 13:26:06 +02001779 LY_CHECK_GOTO(ret, cleanup);
1780 }
1781
1782 if (op_p) {
1783 *op_p = rep_op;
1784 }
Radek Krejcia1c1e542020-09-29 16:06:52 +02001785 for (tree = rep_op; tree->parent; tree = lyd_parent(tree)) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001786 if (rpcr_e) {
1787 /* connect to the operation */
Michal Vasko4189c0f2020-08-13 09:05:22 +02001788 lyd_insert_node(rpcr_e, NULL, tree);
1789 tree = rpcr_e;
1790 }
1791 if (tree_p) {
1792 *tree_p = tree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001793 }
1794
1795cleanup:
1796 /* we have used parse_only flag */
Michal Vasko49c39d82020-11-06 17:20:27 +01001797 assert(!lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001798
1799 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
1800 if (ret) {
1801 lyd_free_all(rep_op);
1802 lyd_free_tree(rpcr_e);
1803 }
1804 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001805}