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