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