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