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