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