Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_json.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief JSON data parser for libyang |
| 5 | * |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 7 | * |
| 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 Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 16 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 18 | #include <stdint.h> |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include "common.h" |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame^] | 23 | #include "compat.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 24 | #include "context.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | #include "dict.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 26 | #include "in_internal.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 27 | #include "json.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 28 | #include "log.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 29 | #include "parser_data.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 30 | #include "parser_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 31 | #include "set.h" |
| 32 | #include "tree.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 33 | #include "tree_data.h" |
| 34 | #include "tree_data_internal.h" |
| 35 | #include "tree_schema.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 36 | #include "tree_schema_internal.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 37 | #include "validation.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 40 | * @brief Internal context for JSON YANG data parser. |
| 41 | * |
| 42 | * Note that the structure maps to the lyd_ctx which is common for all the data parsers |
| 43 | */ |
| 44 | struct lyd_json_ctx { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 45 | const struct lysc_ext_instance *ext; /**< extension instance possibly changing document root context of the data being parsed */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 46 | uint32_t parse_opts; /**< various @ref dataparseroptions. */ |
| 47 | uint32_t val_opts; /**< various @ref datavalidationoptions. */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 48 | uint32_t int_opts; /**< internal data parser options */ |
| 49 | uint32_t path_len; /**< used bytes in the path buffer */ |
| 50 | char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */ |
Michal Vasko | c43c8ab | 2021-03-05 13:32:44 +0100 | [diff] [blame] | 51 | struct ly_set node_when; /**< set of nodes with "when" conditions */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 52 | struct ly_set node_exts; /**< set of nodes and extensions connected with a plugin providing own validation callback */ |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 53 | struct ly_set node_types; /**< set of nodes validated with LY_EINCOMPLETE result */ |
| 54 | struct ly_set meta_types; /**< set of metadata validated with LY_EINCOMPLETE result */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 55 | struct lyd_node *op_node; /**< if an RPC/action/notification is being parsed, store the pointer to it */ |
| 56 | |
| 57 | /* callbacks */ |
| 58 | lyd_ctx_free_clb free; /* destructor */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 59 | |
| 60 | struct lyjson_ctx *jsonctx; /**< JSON context */ |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * @brief Free the JSON data parser context. |
| 65 | * |
| 66 | * JSON implementation of lyd_ctx_free_clb(). |
| 67 | */ |
| 68 | static void |
| 69 | lyd_json_ctx_free(struct lyd_ctx *lydctx) |
| 70 | { |
| 71 | struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx; |
| 72 | |
| 73 | if (lydctx) { |
| 74 | lyd_ctx_free(lydctx); |
| 75 | lyjson_ctx_free(ctx->jsonctx); |
| 76 | free(ctx); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 81 | * @brief Parse JSON member-name as [\@][prefix:][name] |
| 82 | * |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 83 | * \@ - metadata flag, maps to 1 in @p is_meta_p |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 84 | * prefix - name of the module of the data node |
| 85 | * name - name of the data node |
| 86 | * |
| 87 | * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller. |
| 88 | * |
| 89 | * @param[in] value String to parse |
| 90 | * @param[in] value_len Length of the @p str. |
| 91 | * @param[out] name_p Pointer to the beginning of the parsed name. |
| 92 | * @param[out] name_len_p Pointer to the length of the parsed name. |
| 93 | * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL. |
| 94 | * @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 Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 95 | * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 96 | */ |
| 97 | static void |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 98 | lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 99 | size_t *prefix_len_p, ly_bool *is_meta_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 100 | { |
| 101 | const char *name, *prefix = NULL; |
| 102 | size_t name_len, prefix_len = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 103 | ly_bool is_meta = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 104 | |
| 105 | name = memchr(value, ':', value_len); |
| 106 | if (name != NULL) { |
| 107 | prefix = value; |
| 108 | if (*prefix == '@') { |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 109 | is_meta = 1; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 110 | prefix++; |
| 111 | } |
| 112 | prefix_len = name - prefix; |
| 113 | name++; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 114 | name_len = value_len - (prefix_len + 1) - is_meta; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 115 | } else { |
| 116 | name = value; |
| 117 | if (name[0] == '@') { |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 118 | is_meta = 1; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 119 | name++; |
| 120 | } |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 121 | name_len = value_len - is_meta; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 124 | *name_p = name; |
| 125 | *name_len_p = name_len; |
| 126 | *prefix_p = prefix; |
| 127 | *prefix_len_p = prefix_len; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 128 | *is_meta_p = is_meta; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @brief Get correct prefix (module_name) inside the @p node. |
| 133 | * |
| 134 | * @param[in] node Data node to get inherited prefix. |
| 135 | * @param[in] local_prefix Local prefix to replace the inherited prefix. |
| 136 | * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken. |
| 137 | * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix |
| 138 | * and no context @p node to get inherited prefix. |
| 139 | * @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 |
| 140 | * of no local prefix and no context @p node to get inherited prefix. |
| 141 | * @return LY_ERR value. |
| 142 | */ |
| 143 | static LY_ERR |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 144 | lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 145 | size_t *prefix_len_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 146 | { |
| 147 | struct lyd_node_opaq *onode; |
| 148 | const char *module_name = NULL; |
| 149 | |
| 150 | assert(prefix_p && prefix_len_p); |
| 151 | |
| 152 | if (local_prefix_len) { |
| 153 | *prefix_p = local_prefix; |
| 154 | *prefix_len_p = local_prefix_len; |
| 155 | return LY_SUCCESS; |
| 156 | } |
| 157 | |
| 158 | *prefix_p = NULL; |
| 159 | while (node) { |
| 160 | if (node->schema) { |
| 161 | *prefix_p = node->schema->module->name; |
| 162 | break; |
| 163 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 164 | onode = (struct lyd_node_opaq *)node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 165 | if (onode->name.module_name) { |
| 166 | *prefix_p = onode->name.module_name; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 167 | break; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 168 | } else if (onode->name.prefix) { |
| 169 | *prefix_p = onode->name.prefix; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 170 | break; |
| 171 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 172 | node = lyd_parent(node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 173 | } |
| 174 | *prefix_len_p = ly_strlen(module_name); |
| 175 | |
| 176 | return LY_SUCCESS; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @brief Get schema node corresponding to the input parameters. |
| 181 | * |
| 182 | * @param[in] lydctx JSON data parser context. |
| 183 | * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only. |
| 184 | * @param[in] prefix Requested node's prefix (module name). |
| 185 | * @param[in] prefix_len Length of the @p prefix. |
| 186 | * @param[in] name Requested node's name. |
| 187 | * @param[in] name_len Length of the @p name. |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 188 | * @param[in] parent Parent of the node being processed, can be NULL in case of top-level. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 189 | * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters. |
| 190 | * @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. |
| 191 | * @return LY_EVALID on failure, error message is logged |
| 192 | * @return LY_ENOT in case the input data are expected to be skipped |
| 193 | */ |
| 194 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 195 | lydjson_get_snode(const struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 196 | size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 197 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 198 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 199 | struct lys_module *mod = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 200 | uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 201 | |
| 202 | /* init return value */ |
| 203 | *snode_p = NULL; |
| 204 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 205 | LOG_LOCSET(NULL, &parent->node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 206 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 207 | /* get the element module */ |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 208 | if (prefix_len) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 209 | mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len); |
| 210 | } else if (parent) { |
| 211 | if (parent->schema) { |
| 212 | mod = parent->schema->module; |
| 213 | } |
| 214 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 215 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 216 | (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 217 | ret = LY_EVALID; |
| 218 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 219 | } |
| 220 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 221 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 222 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 223 | ret = LY_EVALID; |
| 224 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 225 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 226 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 227 | ret = LY_ENOT; |
| 228 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 232 | /* get the schema node */ |
| 233 | if (mod && (!parent || parent->schema)) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 234 | if (!parent && lydctx->ext) { |
Radek Krejci | ba05eab | 2021-03-10 13:19:29 +0100 | [diff] [blame] | 235 | *snode_p = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 236 | } else { |
| 237 | *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts); |
| 238 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 239 | if (!*snode_p) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 240 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 241 | if (lydctx->ext) { |
| 242 | if (lydctx->ext->argument) { |
| 243 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 244 | (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 245 | } else { |
| 246 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 247 | (int)name_len, name, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 248 | } |
| 249 | } else { |
| 250 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 251 | (int)name_len, name, mod->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 252 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 253 | ret = LY_EVALID; |
| 254 | goto cleanup; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 255 | } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 256 | /* skip element with children */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 257 | ret = LY_ENOT; |
| 258 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 259 | } |
| 260 | } else { |
| 261 | /* check that schema node is valid and can be used */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 262 | ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 266 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 267 | LOG_LOCBACK(0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 268 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * @brief Skip the currently open JSON object/array |
| 273 | * @param[in] jsonctx JSON context with the input data to skip. |
| 274 | * @return LY_ERR value. |
| 275 | */ |
| 276 | static LY_ERR |
| 277 | lydjson_data_skip(struct lyjson_ctx *jsonctx) |
| 278 | { |
| 279 | enum LYJSON_PARSER_STATUS status, current; |
| 280 | size_t sublevels = 1; |
| 281 | |
| 282 | status = lyjson_ctx_status(jsonctx, 0); |
| 283 | |
| 284 | /* skip after the content */ |
| 285 | do { |
| 286 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); |
| 287 | if (current == status) { |
| 288 | sublevels++; |
| 289 | } else if (current == status + 1) { |
| 290 | sublevels--; |
| 291 | } |
Radek Krejci | 79856c4 | 2020-11-19 11:44:08 +0100 | [diff] [blame] | 292 | } while (current != status + 1 || sublevels); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 293 | /* open the next sibling */ |
| 294 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL)); |
| 295 | |
| 296 | return LY_SUCCESS; |
| 297 | } |
| 298 | |
| 299 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 300 | * @brief Check that the input data are parseable as the @p list. |
| 301 | * |
| 302 | * Checks for all the list's keys. Function does not revert the context state. |
| 303 | * |
| 304 | * @param[in] jsonctx JSON parser context. |
| 305 | * @param[in] list List schema node corresponding to the input data object. |
| 306 | * @return LY_SUCCESS in case the data are ok for the @p list |
| 307 | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance. |
| 308 | */ |
| 309 | static LY_ERR |
| 310 | lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list) |
| 311 | { |
| 312 | LY_ERR ret = LY_SUCCESS; |
| 313 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0); |
| 314 | struct ly_set key_set = {0}; |
| 315 | const struct lysc_node *snode; |
| 316 | uint32_t i, status_count; |
| 317 | |
| 318 | assert(list && (list->nodetype == LYS_LIST)); |
| 319 | assert(status == LYJSON_OBJECT); |
| 320 | |
| 321 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 322 | snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 323 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 324 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 325 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (status != LYJSON_OBJECT_EMPTY) { |
| 329 | status_count = jsonctx->status.count; |
| 330 | |
| 331 | while (key_set.count && status != LYJSON_OBJECT_CLOSED) { |
| 332 | const char *name, *prefix; |
| 333 | size_t name_len, prefix_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 334 | ly_bool is_attr; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 335 | |
| 336 | /* match the key */ |
| 337 | snode = NULL; |
| 338 | lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
| 339 | |
| 340 | if (!is_attr && !prefix) { |
| 341 | for (i = 0; i < key_set.count; ++i) { |
| 342 | snode = (const struct lysc_node *)key_set.objs[i]; |
| 343 | if (!ly_strncmp(snode->name, name, name_len)) { |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | /* go into the item to a) process it as a key or b) start skipping it as another list child */ |
| 348 | ret = lyjson_ctx_next(jsonctx, &status); |
| 349 | LY_CHECK_GOTO(ret, cleanup); |
| 350 | |
| 351 | if (snode) { |
| 352 | /* we have the key, validate the value */ |
| 353 | if (status < LYJSON_NUMBER) { |
| 354 | /* not a terminal */ |
| 355 | ret = LY_ENOT; |
| 356 | goto cleanup; |
| 357 | } |
| 358 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 359 | ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 360 | LY_CHECK_GOTO(ret, cleanup); |
| 361 | |
| 362 | /* key with a valid value, remove from the set */ |
| 363 | ly_set_rm_index(&key_set, i, NULL); |
| 364 | } |
| 365 | } else { |
| 366 | /* start skipping the member we are not interested in */ |
| 367 | ret = lyjson_ctx_next(jsonctx, &status); |
| 368 | LY_CHECK_GOTO(ret, cleanup); |
| 369 | } |
| 370 | /* move to the next child */ |
| 371 | while (status_count < jsonctx->status.count) { |
| 372 | ret = lyjson_ctx_next(jsonctx, &status); |
| 373 | LY_CHECK_GOTO(ret, cleanup); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (key_set.count) { |
| 379 | /* some keys are missing/did not validate */ |
| 380 | ret = LY_ENOT; |
| 381 | } |
| 382 | |
| 383 | cleanup: |
| 384 | ly_set_erase(&key_set, NULL); |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * @brief Get the hint for the data type parsers according to the current JSON parser context. |
| 390 | * |
| 391 | * @param[in] lydctx JSON data parser context. The context is supposed to be on a value. |
| 392 | * @param[in,out] status Pointer to the current context status, |
| 393 | * in some circumstances the function manipulates with the context so the status is updated. |
| 394 | * @param[out] type_hint_p Pointer to the variable to store the result. |
| 395 | * @return LY_SUCCESS in case of success. |
| 396 | * @return LY_EINVAL in case of invalid context status not referring to a value. |
| 397 | */ |
| 398 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 399 | lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 400 | { |
| 401 | *type_hint_p = 0; |
| 402 | |
| 403 | if (*status_p == LYJSON_ARRAY) { |
| 404 | /* only [null] */ |
| 405 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); |
| 406 | LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL); |
| 407 | |
| 408 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL)); |
| 409 | LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL); |
| 410 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 411 | *type_hint_p = LYD_VALHINT_EMPTY; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 412 | } else if (*status_p == LYJSON_STRING) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 413 | *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 414 | } else if (*status_p == LYJSON_NUMBER) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 415 | *type_hint_p = LYD_VALHINT_DECNUM; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 416 | } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 417 | *type_hint_p = LYD_VALHINT_BOOLEAN; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 418 | } else if (*status_p == LYJSON_NULL) { |
| 419 | *type_hint_p = 0; |
| 420 | } else { |
| 421 | return LY_EINVAL; |
| 422 | } |
| 423 | |
| 424 | return LY_SUCCESS; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * @brief Check in advance if the input data are parsable according to the provided @p snode. |
| 429 | * |
| 430 | * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking |
| 431 | * is naturally done when the data are really parsed. |
| 432 | * |
| 433 | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
| 434 | * as before calling, despite it is necessary to process input data for checking. |
| 435 | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
| 436 | * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list. |
| 437 | * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled. |
| 438 | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance |
| 439 | * @return LY_EINVAL in case of invalid leaf JSON encoding |
| 440 | * and they are expected to be parsed as opaq nodes. |
| 441 | */ |
| 442 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 443 | lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 444 | { |
| 445 | LY_ERR ret = LY_SUCCESS; |
| 446 | struct lyjson_ctx *jsonctx = lydctx->jsonctx; |
| 447 | enum LYJSON_PARSER_STATUS status; |
| 448 | |
| 449 | assert(snode); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 450 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 451 | if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) { |
| 452 | /* can always be parsed as a data node if we have the schema node */ |
| 453 | return LY_SUCCESS; |
| 454 | } |
| 455 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 456 | if (lydctx->parse_opts & LYD_PARSE_OPAQ) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 457 | /* backup parser */ |
| 458 | lyjson_ctx_backup(jsonctx); |
| 459 | status = lyjson_ctx_status(jsonctx, 0); |
| 460 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 461 | /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed |
| 462 | * as an opaq node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 463 | switch (snode->nodetype) { |
| 464 | case LYS_LEAFLIST: |
| 465 | case LYS_LEAF: |
| 466 | /* value may not be valid in which case we parse it as an opaque node */ |
| 467 | ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); |
| 468 | if (ret) { |
| 469 | break; |
| 470 | } |
| 471 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 472 | if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 473 | ret = LY_ENOT; |
| 474 | } |
| 475 | break; |
| 476 | case LYS_LIST: |
| 477 | /* lists may not have all its keys */ |
| 478 | if (lydjson_check_list(jsonctx, snode)) { |
| 479 | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
| 480 | ret = LY_ENOT; |
| 481 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 482 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /* restore parser */ |
| 486 | lyjson_ctx_restore(jsonctx); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 487 | } else if (snode->nodetype & LYD_NODE_TERM) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 488 | status = lyjson_ctx_status(jsonctx, 0); |
| 489 | ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); |
| 490 | } |
| 491 | |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @brief Join the forward-referencing metadata with their target data nodes. |
| 497 | * |
| 498 | * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists. |
| 499 | * |
| 500 | * @param[in] lydctx JSON data parser context. |
| 501 | * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node) |
| 502 | * as a starting point to search for the metadata's target data node |
| 503 | * @return LY_SUCCESS on success |
| 504 | * @return LY_EVALID in case there are some metadata with unresolved target data node instance |
| 505 | */ |
| 506 | static LY_ERR |
| 507 | lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p) |
| 508 | { |
| 509 | LY_ERR ret = LY_SUCCESS; |
| 510 | struct lyd_node *node, *attr, *next, *start = *first_p, *meta_iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 511 | uint64_t instance = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 512 | const char *prev = NULL; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 513 | uint32_t log_location_items = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 514 | |
| 515 | /* finish linking metadata */ |
| 516 | LY_LIST_FOR_SAFE(*first_p, next, attr) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 517 | struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 518 | uint64_t match = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 519 | ly_bool is_attr; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 520 | const char *name, *prefix; |
| 521 | size_t name_len, prefix_len; |
| 522 | const struct lysc_node *snode; |
| 523 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 524 | if (attr->schema || (meta_container->name.name[0] != '@')) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 525 | /* not an opaq metadata node */ |
| 526 | continue; |
| 527 | } |
| 528 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 529 | LOG_LOCSET(NULL, attr, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 530 | log_location_items++; |
| 531 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 532 | if (prev != meta_container->name.name) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 533 | /* metas' names are stored in dictionary, so checking pointers must works */ |
| 534 | lydict_remove(lydctx->jsonctx->ctx, prev); |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 535 | LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 536 | instance = 1; |
| 537 | } else { |
| 538 | instance++; |
| 539 | } |
| 540 | |
| 541 | /* find the correspnding data node */ |
| 542 | LY_LIST_FOR(start, node) { |
| 543 | if (!node->schema) { |
| 544 | /* opaq node - we are going to put into it just a generic attribute. */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 545 | if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 546 | continue; |
| 547 | } |
| 548 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 549 | if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 550 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.", |
| 551 | ((struct lyd_node_opaq *)node)->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 552 | ret = LY_EVALID; |
| 553 | goto cleanup; |
| 554 | } |
| 555 | |
| 556 | /* match */ |
| 557 | match++; |
| 558 | if (match != instance) { |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | LY_LIST_FOR(meta_container->child, meta_iter) { |
| 563 | /* convert opaq node to a attribute of the opaq node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 564 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 565 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 566 | ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name), |
| 567 | meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 568 | ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 569 | NULL, meta->hints); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 570 | LY_CHECK_GOTO(ret, cleanup); |
| 571 | } |
| 572 | |
| 573 | /* done */ |
| 574 | break; |
| 575 | } else { |
| 576 | /* this is the second time we are resolving the schema node, so it must succeed, |
| 577 | * but remember that snode can be still NULL */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 578 | lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len, |
| 579 | &prefix, &prefix_len, &is_attr); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 580 | assert(is_attr); |
| 581 | ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode); |
| 582 | assert(ret == LY_SUCCESS); |
| 583 | |
| 584 | if (snode != node->schema) { |
| 585 | continue; |
| 586 | } |
| 587 | |
| 588 | /* match */ |
| 589 | match++; |
| 590 | if (match != instance) { |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | LY_LIST_FOR(meta_container->child, meta_iter) { |
| 595 | /* convert opaq node to a metadata of the node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 596 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 597 | struct lys_module *mod = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 598 | ly_bool dynamic = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 599 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 600 | mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 601 | if (mod) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 602 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 603 | meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value), |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 604 | &dynamic, LY_VALUE_JSON, NULL, meta->hints); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 605 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 606 | } else if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 607 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 608 | "Unknown (or not implemented) YANG module \"%s\" for metadata \"%s%s%s\".", |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 609 | meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", meta->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 610 | ret = LY_EVALID; |
| 611 | goto cleanup; |
| 612 | } |
| 613 | } |
| 614 | /* add/correct flags */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 615 | lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &node->meta, lydctx->parse_opts); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 616 | |
| 617 | /* done */ |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (match != instance) { |
| 623 | /* there is no corresponding data node for the metadata */ |
| 624 | if (instance > 1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 625 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing %d%s JSON data instance to be coupled with %s metadata.", |
| 626 | instance, instance == 2 ? "nd" : (instance == 3 ? "rd" : "th"), meta_container->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 627 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 628 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.", |
| 629 | meta_container->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 630 | } |
| 631 | ret = LY_EVALID; |
| 632 | } else { |
| 633 | /* remove the opaq attr */ |
| 634 | if (attr == (*first_p)) { |
| 635 | *first_p = attr->next; |
| 636 | } |
| 637 | lyd_free_tree(attr); |
| 638 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 639 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 640 | LOG_LOCBACK(0, log_location_items, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 641 | log_location_items = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | cleanup: |
| 645 | lydict_remove(lydctx->jsonctx->ctx, prev); |
| 646 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 647 | LOG_LOCBACK(0, log_location_items, 0, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @brief Parse a metadata member. |
| 653 | * |
| 654 | * @param[in] lydctx JSON data parser context. |
| 655 | * @param[in] snode Schema node of the metadata parent. |
| 656 | * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM) |
| 657 | * so the data node does not exists. In such a case the metadata is stored in the context for the later |
| 658 | * processing by lydjson_metadata_finish(). |
| 659 | * @return LY_SUCCESS on success |
| 660 | * @return Various LY_ERR values in case of failure. |
| 661 | */ |
| 662 | static LY_ERR |
| 663 | lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node) |
| 664 | { |
| 665 | LY_ERR ret = LY_SUCCESS; |
| 666 | enum LYJSON_PARSER_STATUS status; |
| 667 | const char *expected; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 668 | ly_bool in_parent = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 669 | const char *name, *prefix = NULL; |
| 670 | size_t name_len, prefix_len = 0; |
| 671 | struct lys_module *mod; |
| 672 | struct lyd_meta *meta = NULL; |
| 673 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 674 | ly_bool is_attr = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 675 | struct lyd_node *prev = node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 676 | uint32_t instance = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 677 | uint16_t nodetype; |
| 678 | |
| 679 | assert(snode || node); |
| 680 | |
| 681 | nodetype = snode ? snode->nodetype : LYS_CONTAINER; |
| 682 | |
| 683 | /* move to the second item in the name/X pair */ |
| 684 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 685 | LY_CHECK_GOTO(ret, cleanup); |
| 686 | |
| 687 | /* check attribute encoding */ |
| 688 | switch (nodetype) { |
| 689 | case LYS_LEAFLIST: |
| 690 | expected = "@name/array of objects/nulls"; |
| 691 | |
| 692 | LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); |
| 693 | |
| 694 | next_entry: |
| 695 | instance++; |
| 696 | |
| 697 | /* move into array / next entry */ |
| 698 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 699 | LY_CHECK_GOTO(ret, cleanup); |
| 700 | |
| 701 | if (status == LYJSON_ARRAY_CLOSED) { |
| 702 | /* we are done, move after the array */ |
| 703 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 704 | goto cleanup; |
| 705 | } |
| 706 | LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error); |
| 707 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 708 | if (!node || (node->schema != prev->schema)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 709 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance no. %u of %s:%s to be coupled with metadata.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 710 | instance, prev->schema->module->name, prev->schema->name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 711 | ret = LY_EVALID; |
| 712 | goto cleanup; |
| 713 | } |
| 714 | |
| 715 | if (status == LYJSON_NULL) { |
| 716 | /* continue with the next entry in the leaf-list array */ |
| 717 | prev = node; |
| 718 | node = node->next; |
| 719 | goto next_entry; |
| 720 | } |
| 721 | break; |
| 722 | case LYS_LEAF: |
| 723 | case LYS_ANYXML: |
| 724 | expected = "@name/object"; |
| 725 | |
| 726 | LY_CHECK_GOTO(status != LYJSON_OBJECT && (nodetype != LYS_LEAFLIST || status != LYJSON_NULL), representation_error); |
| 727 | break; |
| 728 | case LYS_CONTAINER: |
| 729 | case LYS_LIST: |
| 730 | case LYS_ANYDATA: |
| 731 | case LYS_NOTIF: |
| 732 | case LYS_ACTION: |
| 733 | case LYS_RPC: |
| 734 | in_parent = 1; |
| 735 | expected = "@/object"; |
| 736 | LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); |
| 737 | break; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 738 | default: |
| 739 | LOGINT_RET(ctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* process all the members inside a single metadata object */ |
| 743 | assert(status == LYJSON_OBJECT); |
| 744 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 745 | LOG_LOCSET(snode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 746 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 747 | while (status != LYJSON_OBJECT_CLOSED) { |
| 748 | lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
| 749 | if (!prefix) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 750 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 751 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 752 | ret = LY_EVALID; |
| 753 | goto cleanup; |
| 754 | } else if (is_attr) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 755 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 756 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 757 | ret = LY_EVALID; |
| 758 | goto cleanup; |
| 759 | } |
| 760 | |
| 761 | /* get the element module */ |
| 762 | mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len); |
| 763 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 764 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 765 | LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 766 | (int)prefix_len, prefix, (int)name_len, name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 767 | ret = LY_EVALID; |
| 768 | goto cleanup; |
| 769 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 770 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 771 | /* skip element with children */ |
| 772 | ret = lydjson_data_skip(lydctx->jsonctx); |
| 773 | LY_CHECK_GOTO(ret, cleanup); |
| 774 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 775 | /* end of the item */ |
| 776 | continue; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /* get the value */ |
| 781 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 782 | LY_CHECK_GOTO(ret, cleanup); |
| 783 | |
| 784 | if (node->schema) { |
| 785 | /* create metadata */ |
| 786 | meta = NULL; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 787 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 788 | lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 789 | LYD_HINT_DATA); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 790 | LY_CHECK_GOTO(ret, cleanup); |
| 791 | |
| 792 | /* add/correct flags */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 793 | lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 794 | } else { |
| 795 | /* create attribute */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 796 | const char *module_name; |
| 797 | size_t module_name_len; |
| 798 | |
| 799 | lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len); |
| 800 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 801 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 802 | ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, |
| 803 | module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 804 | LY_VALUE_JSON, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 805 | LY_CHECK_GOTO(ret, cleanup); |
| 806 | } |
| 807 | /* next member */ |
| 808 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 809 | LY_CHECK_GOTO(ret, cleanup); |
| 810 | } |
| 811 | |
| 812 | if (nodetype == LYS_LEAFLIST) { |
| 813 | /* continue by processing another metadata object for the following |
| 814 | * leaf-list instance since they are allways instantiated in JSON array */ |
| 815 | prev = node; |
| 816 | node = node->next; |
| 817 | goto next_entry; |
| 818 | } |
| 819 | |
| 820 | /* move after the metadata */ |
| 821 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 822 | LY_CHECK_GOTO(ret, cleanup); |
| 823 | |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 824 | /* success */ |
| 825 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 826 | |
| 827 | representation_error: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 828 | LOGVAL(ctx, LYVE_SYNTAX_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 829 | "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.", |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 830 | lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name.name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 831 | expected, lyjson_token2str(status), in_parent ? "" : "name"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 832 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 833 | ret = LY_EVALID; |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 834 | |
| 835 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 836 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 837 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /** |
| 841 | * @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. |
| 842 | * |
| 843 | * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p). |
| 844 | * @param[in, out] first_p Pointer to the first sibling node in case of top-level. |
| 845 | * @param[in, out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL. |
| 846 | */ |
| 847 | static void |
| 848 | lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p) |
| 849 | { |
| 850 | if (*node_p) { |
| 851 | /* insert, keep first pointer correct */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 852 | lyd_insert_node(&parent->node, first_p, *node_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 853 | if (first_p) { |
| 854 | if (parent) { |
| 855 | *first_p = parent->child; |
| 856 | } else { |
| 857 | while ((*first_p)->prev->next) { |
| 858 | *first_p = (*first_p)->prev; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | *node_p = NULL; |
| 863 | } |
| 864 | } |
| 865 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 866 | static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 867 | struct ly_set *parsed); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 868 | |
| 869 | /** |
| 870 | * @brief Parse opaq node from the input. |
| 871 | * |
| 872 | * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array. |
| 873 | * |
| 874 | * @param[in] lydctx JSON data parser context. |
| 875 | * @param[in] name Name of the opaq node to create. |
| 876 | * @param[in] name_len Length of the @p name string. |
| 877 | * @param[in] prefix Prefix of the opaq node to create. |
| 878 | * @param[in] prefix_len Length of the @p prefx string. |
| 879 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 880 | * but must be set if @p first is not. |
| 881 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 882 | * since the function manipulates with the context and process the input, the status can be updated. |
| 883 | * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone |
| 884 | * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. |
| 885 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 886 | * @param[out] node_p Pointer to the created opaq node. |
| 887 | * @return LY_ERR value. |
| 888 | */ |
| 889 | static LY_ERR |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 890 | lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 891 | struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, |
| 892 | enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 893 | { |
| 894 | LY_ERR ret = LY_SUCCESS; |
| 895 | const char *value = NULL, *module_name; |
| 896 | size_t value_len = 0, module_name_len = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 897 | ly_bool dynamic = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 898 | uint32_t type_hint = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 899 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 900 | if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 901 | /* prepare for creating opaq node with a value */ |
| 902 | value = lydctx->jsonctx->value; |
| 903 | value_len = lydctx->jsonctx->value_len; |
| 904 | dynamic = lydctx->jsonctx->dynamic; |
| 905 | lydctx->jsonctx->dynamic = 0; |
| 906 | |
| 907 | LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* create node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 911 | lydjson_get_node_prefix(&parent->node, prefix, prefix_len, &module_name, &module_name_len); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 912 | ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 913 | value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 914 | if (dynamic) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 915 | free((char *)value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 916 | } |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 917 | LY_CHECK_RET(ret); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 918 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 919 | if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 920 | /* process children */ |
| 921 | while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 922 | LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 923 | *status_p = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 924 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 925 | } else if ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 926 | /* process another instance of the same node */ |
| 927 | /* but first mark the node to be expected a list or a leaf-list */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 928 | ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 929 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 930 | if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 931 | /* but first process children of the object in the array */ |
| 932 | while (*status_inner_p != LYJSON_OBJECT_CLOSED && *status_inner_p != LYJSON_OBJECT_EMPTY) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 933 | LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 934 | *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p)); |
| 939 | |
| 940 | /* continue with the next instance */ |
| 941 | if (*status_inner_p != LYJSON_ARRAY_CLOSED) { |
| 942 | assert(node_p); |
| 943 | lydjson_maintain_children(parent, first_p, node_p); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 944 | return lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, status_inner_p, |
| 945 | first_p, node_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| 949 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 950 | LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p))); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 951 | |
| 952 | /* move after the item */ |
| 953 | return lyjson_ctx_next(lydctx->jsonctx, status_p); |
| 954 | } |
| 955 | |
| 956 | /** |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 957 | * @brief Move to the second item in the name/X pair and parse opaq node from the input. |
| 958 | * |
| 959 | * This function is basically the wrapper of the ::lydjson_parse_opaq(). |
| 960 | * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter |
| 961 | * for ::lydjson_parse_opaq(). |
| 962 | * |
| 963 | * @param[in] lydctx JSON data parser context. |
| 964 | * @param[in] name Name of the opaq node to create. |
| 965 | * @param[in] name_len Length of the @p name string. |
| 966 | * @param[in] prefix Prefix of the opaq node to create. |
| 967 | * @param[in] prefix_len Length of the @p prefx string. |
| 968 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 969 | * but must be set if @p first is not. |
| 970 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 971 | * since the function manipulates with the context and process the input, the status can be updated. |
| 972 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 973 | * @param[out] node_p Pointer to the created opaq node. |
| 974 | * @return LY_ERR value. |
| 975 | */ |
| 976 | static LY_ERR |
| 977 | lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, |
| 978 | const char *prefix, size_t prefix_len, struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, |
| 979 | struct lyd_node **first_p, struct lyd_node **node_p) |
| 980 | { |
| 981 | enum LYJSON_PARSER_STATUS status_inner = 0; |
| 982 | |
| 983 | /* move to the second item in the name/X pair */ |
| 984 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); |
| 985 | |
| 986 | if (*status_p == LYJSON_ARRAY) { |
| 987 | /* move into the array */ |
| 988 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner)); |
| 989 | } else { |
| 990 | /* just a flag to pass correct parameters into lydjson_parse_opaq() */ |
| 991 | status_inner = LYJSON_ERROR; |
| 992 | } |
| 993 | |
| 994 | if (status_inner == LYJSON_ERROR) { |
| 995 | status_inner = *status_p; |
| 996 | } |
| 997 | |
| 998 | /* parse opaq node from the input */ |
| 999 | LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner, first_p, node_p)); |
| 1000 | |
| 1001 | return LY_SUCCESS; |
| 1002 | } |
| 1003 | |
| 1004 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1005 | * @brief Process the attribute container (starting by @) |
| 1006 | * |
| 1007 | * @param[in] lydctx JSON data parser context. |
| 1008 | * @param[in] attr_node The data node referenced by the attribute container, if already known. |
| 1009 | * @param[in] snode The schema node of the data node referenced by the attribute container, if known. |
| 1010 | * @param[in] name Name of the opaq node to create. |
| 1011 | * @param[in] name_len Length of the @p name string. |
| 1012 | * @param[in] prefix Prefix of the opaq node to create. |
| 1013 | * @param[in] prefix_len Length of the @p prefx string. |
| 1014 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 1015 | * but must be set if @p first is not. |
| 1016 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 1017 | * since the function manipulates with the context and process the input, the status can be updated. |
| 1018 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 1019 | * @param[out] node_p Pointer to the created opaq node. |
| 1020 | * @return LY_ERR value. |
| 1021 | */ |
| 1022 | static LY_ERR |
| 1023 | lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode, |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1024 | const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent, |
| 1025 | enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1026 | { |
| 1027 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1028 | |
| 1029 | /* parse as an attribute to a node */ |
| 1030 | if (!attr_node && snode) { |
| 1031 | /* try to find the instance */ |
| 1032 | for (struct lyd_node *iter = *first_p; iter; iter = iter->next) { |
| 1033 | if (iter->schema == snode) { |
| 1034 | attr_node = iter; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | if (!attr_node) { |
| 1040 | /* parse just as an opaq node with the name beginning with @, |
| 1041 | * later we have to check that it belongs to a standard node |
| 1042 | * and it is supposed to be converted to a metadata */ |
| 1043 | uint32_t prev_opts; |
| 1044 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1045 | /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1046 | prev_opts = lydctx->parse_opts; |
| 1047 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 1048 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1049 | |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1050 | ret = lydjson_ctx_next_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1, |
| 1051 | NULL, 0, (struct lyd_node_inner *)parent, status_p, first_p, node_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1052 | |
| 1053 | /* restore the parser options */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1054 | lydctx->parse_opts = prev_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1055 | } else { |
| 1056 | ret = lydjson_metadata(lydctx, snode, attr_node); |
| 1057 | } |
| 1058 | |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1063 | * @brief Parse a single instance of a node. |
| 1064 | * |
| 1065 | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
| 1066 | * as before calling, despite it is necessary to process input data for checking. |
| 1067 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 1068 | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
| 1069 | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
| 1070 | * @param[in] name Parsed JSON node name. |
| 1071 | * @param[in] name_len Lenght of @p name. |
| 1072 | * @param[in] prefix Parsed JSON node prefix. |
| 1073 | * @param[in] prefix_len Length of @p prefix. |
| 1074 | * @param[in,out] status JSON parser status, is updated. |
| 1075 | * @param[out] node Parsed data (or opaque) node. |
| 1076 | * @return LY_SUCCESS if a node was successfully parsed, |
| 1077 | * @return LY_EINVAL in case of invalid JSON encoding, |
| 1078 | * @return LY_ERR on other errors. |
| 1079 | */ |
| 1080 | static LY_ERR |
| 1081 | lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1082 | const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len, |
| 1083 | enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1084 | { |
| 1085 | LY_ERR ret; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 1086 | uint32_t type_hints = 0; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1087 | uint32_t prev_opts; |
| 1088 | struct lyd_node *tree = NULL; |
| 1089 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1090 | ret = lydjson_data_check_opaq(lydctx, snode, &type_hints); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1091 | if (ret == LY_SUCCESS) { |
| 1092 | assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY)); |
| 1093 | if (snode->nodetype & LYD_NODE_TERM) { |
| 1094 | /* create terminal node */ |
| 1095 | ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1096 | lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1097 | type_hints, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1098 | LY_CHECK_RET(ret); |
| 1099 | |
| 1100 | /* move JSON parser */ |
| 1101 | ret = lyjson_ctx_next(lydctx->jsonctx, status); |
| 1102 | LY_CHECK_RET(ret); |
| 1103 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 1104 | /* create inner node */ |
| 1105 | LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL); |
| 1106 | |
| 1107 | ret = lyd_create_inner(snode, node); |
| 1108 | LY_CHECK_RET(ret); |
| 1109 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1110 | LOG_LOCSET(snode, *node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1111 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1112 | /* process children */ |
| 1113 | while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1114 | ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1115 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1116 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1117 | } |
| 1118 | |
| 1119 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1120 | ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node)); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1121 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1122 | |
| 1123 | if (snode->nodetype == LYS_LIST) { |
| 1124 | /* check all keys exist */ |
| 1125 | ret = lyd_parse_check_keys(*node); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1126 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1129 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1130 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1131 | ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1132 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1133 | |
| 1134 | /* add any missing default children */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 1135 | ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts, |
| 1136 | &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1137 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1140 | LOG_LOCBACK(1, 1, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1141 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1142 | /* move JSON parser */ |
| 1143 | ret = lyjson_ctx_next(lydctx->jsonctx, status); |
| 1144 | LY_CHECK_RET(ret); |
| 1145 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 1146 | /* create any node */ |
| 1147 | LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL); |
| 1148 | |
| 1149 | /* parse any data tree with correct options */ |
| 1150 | /* first backup the current options and then make the parser to process data as opaq nodes */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1151 | prev_opts = lydctx->parse_opts; |
| 1152 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 1153 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1154 | |
| 1155 | /* process the anydata content */ |
| 1156 | while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1157 | ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1158 | LY_CHECK_RET(ret); |
| 1159 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1160 | } |
| 1161 | |
| 1162 | /* restore parser options */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1163 | lydctx->parse_opts = prev_opts; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1164 | |
| 1165 | /* finish linking metadata */ |
| 1166 | ret = lydjson_metadata_finish(lydctx, &tree); |
| 1167 | LY_CHECK_RET(ret); |
| 1168 | |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 1169 | ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1170 | LY_CHECK_RET(ret); |
| 1171 | } |
| 1172 | } else if (ret == LY_ENOT) { |
| 1173 | /* parse it again as an opaq node */ |
| 1174 | ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1175 | status, status, first_p, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1176 | LY_CHECK_RET(ret); |
| 1177 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1178 | if (snode->nodetype == LYS_LIST) { |
| 1179 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST; |
| 1180 | } else if (snode->nodetype == LYS_LEAFLIST) { |
| 1181 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
| 1188 | /** |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1189 | * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1190 | * |
| 1191 | * @param[in] lydctx JSON data parser context. |
| 1192 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 1193 | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1194 | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1195 | * @return LY_ERR value. |
| 1196 | */ |
| 1197 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1198 | lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1199 | { |
| 1200 | LY_ERR ret = LY_SUCCESS; |
| 1201 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0); |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame^] | 1202 | const char *name, *prefix = NULL, *expected = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1203 | size_t name_len, prefix_len = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1204 | ly_bool is_meta = 0; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1205 | const struct lysc_node *snode = NULL; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1206 | struct lyd_node *node = NULL, *attr_node = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1207 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame^] | 1208 | char *value = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1209 | |
| 1210 | assert(parent || first_p); |
| 1211 | assert(status == LYJSON_OBJECT); |
| 1212 | |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame^] | 1213 | /* Duplicate ::lyjson_ctx.value because it can be dynamically allocated and later |
| 1214 | * ::lyjson_ctx_next() will release it although this string is needed for the ::lydjson_parse_opaq(). |
| 1215 | */ |
| 1216 | value = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); |
| 1217 | if (!value) { |
| 1218 | LOGMEM(lydctx->jsonctx->ctx); |
| 1219 | ret = LY_EMEM; |
| 1220 | goto cleanup; |
| 1221 | } |
| 1222 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1223 | /* process the node name */ |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame^] | 1224 | lydjson_parse_name(value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1225 | |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1226 | if (!is_meta || name_len || prefix_len) { |
| 1227 | /* get the schema node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1228 | ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, (struct lyd_node_inner *)parent, &snode); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1229 | if (ret == LY_ENOT) { |
| 1230 | /* skip element with children */ |
| 1231 | ret = lydjson_data_skip(lydctx->jsonctx); |
| 1232 | LY_CHECK_GOTO(ret, cleanup); |
| 1233 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1234 | /* nothing for now, continue with another call of lydjson_subtree_r() */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1235 | goto cleanup; |
| 1236 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1237 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1238 | |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1239 | if (!snode) { |
| 1240 | /* we will not be parsing it as metadata */ |
| 1241 | is_meta = 0; |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | if (is_meta) { |
| 1246 | /* parse as metadata */ |
| 1247 | if (!name_len && !prefix_len) { |
| 1248 | /* parent's metadata without a name - use the schema from the parent */ |
| 1249 | if (!parent) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1250 | LOGVAL(ctx, LYVE_SYNTAX_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1251 | "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries."); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1252 | ret = LY_EVALID; |
| 1253 | goto cleanup; |
| 1254 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1255 | attr_node = parent; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1256 | snode = attr_node->schema; |
| 1257 | } |
| 1258 | ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1259 | first_p, &node); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1260 | LY_CHECK_GOTO(ret, cleanup); |
| 1261 | } else if (!snode) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1262 | /* parse as an opaq node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1263 | assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1264 | |
aPiecek | b7b29e6 | 2021-05-11 10:02:43 +0200 | [diff] [blame] | 1265 | /* opaq node cannot have an empty string as the name. */ |
| 1266 | if (name_len == 0) { |
| 1267 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string."); |
| 1268 | ret = LY_EVALID; |
| 1269 | goto cleanup; |
| 1270 | } |
| 1271 | |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1272 | /* move to the second item in the name/X pair and parse opaq */ |
| 1273 | ret = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, |
| 1274 | (struct lyd_node_inner *)parent, &status, first_p, &node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1275 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1276 | } else { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1277 | /* parse as a standard lyd_node but it can still turn out to be an opaque node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1278 | |
| 1279 | /* move to the second item in the name/X pair */ |
| 1280 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 1281 | LY_CHECK_GOTO(ret, cleanup); |
| 1282 | |
| 1283 | /* first check the expected representation according to the nodetype and then continue with the content */ |
| 1284 | switch (snode->nodetype) { |
| 1285 | case LYS_LEAFLIST: |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1286 | case LYS_LIST: |
| 1287 | if (snode->nodetype == LYS_LEAFLIST) { |
| 1288 | expected = "name/array of values"; |
| 1289 | } else { |
| 1290 | expected = "name/array of objects"; |
| 1291 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1292 | |
| 1293 | LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); |
| 1294 | |
| 1295 | /* move into array */ |
| 1296 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 1297 | LY_CHECK_GOTO(ret, cleanup); |
| 1298 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1299 | /* process all the values/objects */ |
| 1300 | do { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1301 | lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1302 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1303 | ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len, |
| 1304 | prefix, prefix_len, &status, &node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1305 | if (ret == LY_EINVAL) { |
| 1306 | goto representation_error; |
| 1307 | } else if (ret) { |
| 1308 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1309 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1310 | } while (status != LYJSON_ARRAY_CLOSED); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1311 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1312 | /* move after the array */ |
| 1313 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 1314 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1315 | |
| 1316 | break; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1317 | case LYS_LEAF: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1318 | case LYS_CONTAINER: |
| 1319 | case LYS_NOTIF: |
| 1320 | case LYS_ACTION: |
| 1321 | case LYS_RPC: |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1322 | case LYS_ANYDATA: |
| 1323 | case LYS_ANYXML: |
| 1324 | if (snode->nodetype == LYS_LEAF) { |
| 1325 | if (status == LYJSON_ARRAY) { |
| 1326 | expected = "name/[null]"; |
| 1327 | } else { |
| 1328 | expected = "name/value"; |
| 1329 | } |
| 1330 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1331 | expected = "name/object"; |
| 1332 | } |
| 1333 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1334 | /* process the value/object */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1335 | ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len, |
| 1336 | prefix, prefix_len, &status, &node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1337 | if (ret == LY_EINVAL) { |
| 1338 | goto representation_error; |
| 1339 | } else if (ret) { |
| 1340 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1341 | } |
| 1342 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1343 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1344 | /* rememeber the RPC/action/notification */ |
| 1345 | lydctx->op_node = node; |
| 1346 | } |
| 1347 | |
| 1348 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1349 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1352 | /* finally connect the parsed node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1353 | lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node); |
| 1354 | |
| 1355 | /* rememeber a successfully parsed node */ |
| 1356 | if (parsed) { |
| 1357 | ly_set_add(parsed, node, 1, NULL); |
| 1358 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1359 | |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1360 | /* success */ |
| 1361 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1362 | |
| 1363 | representation_error: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1364 | LOG_LOCSET(NULL, parent, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1365 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1366 | lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status)); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1367 | LOG_LOCBACK(0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1368 | ret = LY_EVALID; |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1369 | |
| 1370 | cleanup: |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame^] | 1371 | free(value); |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1372 | lyd_free_tree(node); |
| 1373 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * @brief Common start of JSON parser processing different types of the input data. |
| 1378 | * |
| 1379 | * @param[in] ctx libyang context |
| 1380 | * @param[in] in Input structure. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1381 | * @param[in] parse_opts Options for parser, see @ref dataparseroptions. |
| 1382 | * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1383 | * @param[out] lydctx_p Data parser context to finish validation. |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1384 | * @param[out] status Storage for the current context's status |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1385 | * @return LY_ERR value. |
| 1386 | */ |
| 1387 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1388 | lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1389 | struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1390 | { |
| 1391 | LY_ERR ret = LY_SUCCESS; |
| 1392 | struct lyd_json_ctx *lydctx; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1393 | size_t i; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1394 | |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1395 | assert(lydctx_p); |
| 1396 | assert(status); |
| 1397 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1398 | /* init context */ |
| 1399 | lydctx = calloc(1, sizeof *lydctx); |
| 1400 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1401 | lydctx->parse_opts = parse_opts; |
| 1402 | lydctx->val_opts = val_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1403 | lydctx->free = lyd_json_ctx_free; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1404 | |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1405 | /* starting top-level */ |
| 1406 | for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1407 | if (in->current[i] == '\n') { |
| 1408 | /* new line */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1409 | LY_IN_NEW_LINE(in); |
Michal Vasko | 61d7636 | 2020-10-07 10:47:30 +0200 | [diff] [blame] | 1410 | } |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1411 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1412 | |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1413 | LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret); |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1414 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 1415 | if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) { |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1416 | *lydctx_p = lydctx; |
| 1417 | return LY_SUCCESS; |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1418 | } else { |
| 1419 | /* expecting top-level object */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1420 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1421 | lyjson_token2str(*status)); |
| 1422 | *lydctx_p = NULL; |
Michal Vasko | 9350901 | 2020-11-13 10:26:09 +0100 | [diff] [blame] | 1423 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1424 | return LY_EVALID; |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1425 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1429 | lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1430 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
| 1431 | struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1432 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1433 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1434 | struct lyd_json_ctx *lydctx = NULL; |
| 1435 | enum LYJSON_PARSER_STATUS status; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1436 | uint32_t int_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1437 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1438 | rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status); |
| 1439 | LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1440 | |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1441 | assert(status == LYJSON_OBJECT); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1442 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1443 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1444 | case LYD_TYPE_DATA_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1445 | int_opts = LYD_INTOPT_WITH_SIBLINGS; |
| 1446 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1447 | case LYD_TYPE_RPC_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1448 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS; |
| 1449 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1450 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1451 | int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS; |
| 1452 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1453 | case LYD_TYPE_REPLY_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1454 | int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS; |
| 1455 | break; |
| 1456 | default: |
| 1457 | LOGINT(ctx); |
| 1458 | rc = LY_EINT; |
| 1459 | goto cleanup; |
| 1460 | } |
| 1461 | lydctx->int_opts = int_opts; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1462 | lydctx->ext = ext; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1463 | |
| 1464 | /* find the operation node if it exists already */ |
| 1465 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1466 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1467 | /* read subtree(s) */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1468 | while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) { |
| 1469 | rc = lydjson_subtree_r(lydctx, parent, first_p, parsed); |
| 1470 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1471 | |
| 1472 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1473 | |
| 1474 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1475 | break; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && |
| 1480 | (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) { |
| 1481 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1482 | rc = LY_EVALID; |
| 1483 | goto cleanup; |
| 1484 | } |
| 1485 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
| 1486 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1487 | rc = LY_EVALID; |
| 1488 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1492 | rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); |
| 1493 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1494 | |
| 1495 | cleanup: |
| 1496 | /* there should be no unresolved types stored */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1497 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count && |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 1498 | !lydctx->node_when.count)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1499 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1500 | if (rc) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1501 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1502 | } else { |
| 1503 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 1504 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1505 | return rc; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1506 | } |