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