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