Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_new.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief Data tree new functions |
| 6 | * |
| 7 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <ctype.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "common.h" |
| 28 | #include "compat.h" |
| 29 | #include "context.h" |
| 30 | #include "dict.h" |
| 31 | #include "diff.h" |
| 32 | #include "hash_table.h" |
| 33 | #include "in.h" |
| 34 | #include "in_internal.h" |
| 35 | #include "log.h" |
| 36 | #include "parser_data.h" |
| 37 | #include "parser_internal.h" |
| 38 | #include "path.h" |
| 39 | #include "plugins.h" |
| 40 | #include "plugins_exts/metadata.h" |
| 41 | #include "plugins_internal.h" |
| 42 | #include "plugins_types.h" |
| 43 | #include "set.h" |
| 44 | #include "tree.h" |
| 45 | #include "tree_data_internal.h" |
| 46 | #include "tree_edit.h" |
| 47 | #include "tree_schema.h" |
| 48 | #include "tree_schema_internal.h" |
| 49 | #include "validation.h" |
| 50 | #include "xml.h" |
| 51 | #include "xpath.h" |
| 52 | |
| 53 | LY_ERR |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 54 | lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool is_utf8, ly_bool *dynamic, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 55 | LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node) |
| 56 | { |
| 57 | LY_ERR ret; |
| 58 | struct lyd_node_term *term; |
| 59 | |
| 60 | assert(schema->nodetype & LYD_NODE_TERM); |
| 61 | |
| 62 | term = calloc(1, sizeof *term); |
| 63 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 64 | |
| 65 | term->schema = schema; |
| 66 | term->prev = &term->node; |
| 67 | term->flags = LYD_NEW; |
| 68 | |
| 69 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 70 | ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value, |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 71 | value_len, is_utf8, dynamic, format, prefix_data, hints, schema, incomplete); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 72 | LOG_LOCBACK(1, 0, 0, 0); |
| 73 | LY_CHECK_ERR_RET(ret, free(term), ret); |
| 74 | lyd_hash(&term->node); |
| 75 | |
| 76 | *node = &term->node; |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | LY_ERR |
| 81 | lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node) |
| 82 | { |
| 83 | LY_ERR ret; |
| 84 | struct lyd_node_term *term; |
| 85 | struct lysc_type *type; |
| 86 | |
| 87 | assert(schema->nodetype & LYD_NODE_TERM); |
| 88 | assert(val && val->realtype); |
| 89 | |
| 90 | term = calloc(1, sizeof *term); |
| 91 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 92 | |
| 93 | term->schema = schema; |
| 94 | term->prev = &term->node; |
| 95 | term->flags = LYD_NEW; |
| 96 | |
| 97 | type = ((struct lysc_node_leaf *)schema)->type; |
| 98 | ret = type->plugin->duplicate(schema->module->ctx, val, &term->value); |
| 99 | if (ret) { |
| 100 | LOGERR(schema->module->ctx, ret, "Value duplication failed."); |
| 101 | free(term); |
| 102 | return ret; |
| 103 | } |
| 104 | lyd_hash(&term->node); |
| 105 | |
| 106 | *node = &term->node; |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | LY_ERR |
| 111 | lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node) |
| 112 | { |
| 113 | struct lyd_node_inner *in; |
| 114 | |
| 115 | assert(schema->nodetype & LYD_NODE_INNER); |
| 116 | |
| 117 | in = calloc(1, sizeof *in); |
| 118 | LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM); |
| 119 | |
| 120 | in->schema = schema; |
| 121 | in->prev = &in->node; |
| 122 | in->flags = LYD_NEW; |
| 123 | if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) { |
| 124 | in->flags |= LYD_DEFAULT; |
| 125 | } |
| 126 | |
| 127 | /* do not hash list with keys, we need them for the hash */ |
| 128 | if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) { |
| 129 | lyd_hash(&in->node); |
| 130 | } |
| 131 | |
| 132 | *node = &in->node; |
| 133 | return LY_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 137 | lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, const struct lyxp_var *vars, |
| 138 | struct lyd_node **node) |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 139 | { |
| 140 | LY_ERR ret = LY_SUCCESS; |
| 141 | struct lyd_node *list = NULL, *key; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 142 | const struct lyd_value *value; |
| 143 | struct lyd_value val = {0}; |
| 144 | struct lyxp_var *var; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 145 | LY_ARRAY_COUNT_TYPE u; |
| 146 | |
| 147 | assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS)); |
| 148 | |
| 149 | /* create list */ |
| 150 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup); |
| 151 | |
Michal Vasko | 6e075ab | 2022-12-21 15:48:14 +0100 | [diff] [blame] | 152 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 153 | |
| 154 | /* create and insert all the keys */ |
| 155 | LY_ARRAY_FOR(predicates, u) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 156 | if (predicates[u].type == LY_PATH_PREDTYPE_LIST_VAR) { |
| 157 | /* find the var */ |
| 158 | if ((ret = lyxp_vars_find(schema->module->ctx, vars, predicates[u].variable, 0, &var))) { |
| 159 | goto cleanup; |
| 160 | } |
| 161 | |
| 162 | /* store the value */ |
| 163 | LOG_LOCSET(predicates[u].key, NULL, NULL, NULL); |
| 164 | ret = lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaf *)predicates[u].key)->type, |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 165 | var->value, strlen(var->value), 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, predicates[u].key, NULL); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 166 | LOG_LOCBACK(1, 0, 0, 0); |
| 167 | LY_CHECK_GOTO(ret, cleanup); |
| 168 | |
| 169 | value = &val; |
| 170 | } else { |
| 171 | assert(predicates[u].type == LY_PATH_PREDTYPE_LIST); |
| 172 | value = &predicates[u].value; |
| 173 | } |
| 174 | |
| 175 | ret = lyd_create_term2(predicates[u].key, value, &key); |
| 176 | if (val.realtype) { |
| 177 | val.realtype->plugin->free(schema->module->ctx, &val); |
| 178 | memset(&val, 0, sizeof val); |
| 179 | } |
| 180 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 181 | lyd_insert_node(list, NULL, key, 0); |
| 182 | } |
| 183 | |
| 184 | /* hash having all the keys */ |
| 185 | lyd_hash(list); |
| 186 | |
| 187 | /* success */ |
| 188 | *node = list; |
| 189 | list = NULL; |
| 190 | |
| 191 | cleanup: |
Michal Vasko | 6e075ab | 2022-12-21 15:48:14 +0100 | [diff] [blame] | 192 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 193 | lyd_free_tree(list); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | LY_ERR |
| 198 | lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node) |
| 199 | { |
| 200 | LY_ERR ret = LY_SUCCESS; |
| 201 | struct lyxp_expr *expr = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 202 | uint32_t exp_idx = 0; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 203 | struct ly_path_predicate *predicates = NULL; |
| 204 | |
| 205 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 206 | |
| 207 | /* parse keys */ |
| 208 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL, |
| 209 | LY_PATH_PRED_KEYS, &expr), cleanup); |
| 210 | |
| 211 | /* compile them */ |
| 212 | LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 213 | NULL, &predicates), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 214 | |
| 215 | /* create the list node */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 216 | LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, NULL, node), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 217 | |
| 218 | cleanup: |
| 219 | LOG_LOCBACK(1, 0, 0, 0); |
| 220 | lyxp_expr_free(schema->module->ctx, expr); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 221 | ly_path_predicates_free(schema->module->ctx, predicates); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 222 | return ret; |
| 223 | } |
| 224 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 225 | /** |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 226 | * @brief Learn actual any value type in case it is currently ::LYD_ANYDATA_STRING. |
| 227 | * |
| 228 | * @param[in] value Any value. |
| 229 | * @param[out] value_type Detected value type. |
| 230 | */ |
| 231 | static void |
| 232 | lyd_create_any_string_valtype(const void *value, LYD_ANYDATA_VALUETYPE *value_type) |
| 233 | { |
| 234 | /* detect format */ |
| 235 | if (!value) { |
| 236 | /* interpret it as an empty data tree */ |
| 237 | *value_type = LYD_ANYDATA_DATATREE; |
| 238 | } else if (((char *)value)[0] == '<') { |
| 239 | *value_type = LYD_ANYDATA_XML; |
| 240 | } else if (((char *)value)[0] == '{') { |
| 241 | *value_type = LYD_ANYDATA_JSON; |
| 242 | } else if (!strncmp(value, "lyb", 3)) { |
| 243 | *value_type = LYD_ANYDATA_LYB; |
| 244 | } else { |
| 245 | /* really just some string */ |
| 246 | *value_type = LYD_ANYDATA_STRING; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @brief Convert an any value into a datatree. |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 252 | * |
| 253 | * @param[in] ctx libyang context. |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 254 | * @param[in] value_in Any value as an input. |
| 255 | * @param[in] value_type Any @p value type. |
| 256 | * @param[in] log Whether parsing errors should be logged. |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 257 | * @param[out] tree Parsed data tree. |
| 258 | * @return LY_ERR value. |
| 259 | */ |
| 260 | static LY_ERR |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 261 | lyd_create_any_datatree(const struct ly_ctx *ctx, struct ly_in *value_in, LYD_ANYDATA_VALUETYPE value_type, ly_bool log, |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 262 | struct lyd_node **tree) |
| 263 | { |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 264 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 265 | struct lyd_ctx *lydctx = NULL; |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 266 | uint32_t parse_opts, int_opts, log_opts = 0; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 267 | |
| 268 | *tree = NULL; |
| 269 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 270 | /* set options */ |
| 271 | parse_opts = LYD_PARSE_ONLY | LYD_PARSE_OPAQ; |
| 272 | int_opts = LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; |
| 273 | |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 274 | if (!log) { |
| 275 | /* no logging */ |
| 276 | ly_temp_log_options(&log_opts); |
| 277 | } |
| 278 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 279 | switch (value_type) { |
| 280 | case LYD_ANYDATA_DATATREE: |
| 281 | case LYD_ANYDATA_STRING: |
| 282 | /* unreachable */ |
| 283 | LOGINT_RET(ctx); |
| 284 | case LYD_ANYDATA_XML: |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 285 | rc = lyd_parse_xml(ctx, NULL, NULL, tree, value_in, parse_opts, 0, int_opts, NULL, NULL, &lydctx); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 286 | break; |
| 287 | case LYD_ANYDATA_JSON: |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 288 | rc = lyd_parse_json(ctx, NULL, NULL, tree, value_in, parse_opts, 0, int_opts, NULL, NULL, &lydctx); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 289 | break; |
| 290 | case LYD_ANYDATA_LYB: |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 291 | rc = lyd_parse_lyb(ctx, NULL, NULL, tree, value_in, parse_opts | LYD_PARSE_STRICT, 0, int_opts, NULL, NULL, &lydctx); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 292 | break; |
| 293 | } |
| 294 | if (lydctx) { |
| 295 | lydctx->free(lydctx); |
| 296 | } |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 297 | |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 298 | if (!log) { |
| 299 | /* restore logging */ |
| 300 | ly_temp_log_options(NULL); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 301 | } |
Michal Vasko | 90fc1ae | 2023-04-27 12:22:19 +0200 | [diff] [blame] | 302 | if (rc && *tree) { |
| 303 | lyd_free_siblings(*tree); |
| 304 | *tree = NULL; |
| 305 | } |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 306 | return rc; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 309 | LY_ERR |
| 310 | lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value, |
| 311 | struct lyd_node **node) |
| 312 | { |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 313 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 314 | struct lyd_node *tree; |
Michal Vasko | bdeee99 | 2022-12-01 16:17:56 +0100 | [diff] [blame] | 315 | struct lyd_node_any *any = NULL; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 316 | union lyd_any_value any_val; |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 317 | struct ly_in *in = NULL; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 318 | |
| 319 | assert(schema->nodetype & LYD_NODE_ANY); |
| 320 | |
| 321 | any = calloc(1, sizeof *any); |
| 322 | LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM); |
| 323 | |
| 324 | any->schema = schema; |
| 325 | any->prev = &any->node; |
| 326 | any->flags = LYD_NEW; |
| 327 | |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 328 | if (schema->nodetype == LYS_ANYDATA) { |
| 329 | /* anydata */ |
| 330 | if (value_type == LYD_ANYDATA_STRING) { |
| 331 | /* detect value type */ |
| 332 | lyd_create_any_string_valtype(value, &value_type); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 333 | } |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 334 | |
| 335 | if (value_type != LYD_ANYDATA_DATATREE) { |
| 336 | /* create input */ |
| 337 | assert(value); |
| 338 | LY_CHECK_GOTO(rc = ly_in_new_memory(value, &in), cleanup); |
| 339 | |
| 340 | /* parse as a data tree */ |
| 341 | if ((r = lyd_create_any_datatree(schema->module->ctx, in, value_type, 1, &tree))) { |
| 342 | LOGERR(schema->module->ctx, rc, "Failed to parse any content into a data tree."); |
| 343 | rc = r; |
| 344 | goto cleanup; |
| 345 | } |
| 346 | |
| 347 | /* use the parsed data tree */ |
| 348 | if (use_value) { |
| 349 | free((void *)value); |
| 350 | } |
| 351 | use_value = 1; |
| 352 | value = tree; |
| 353 | value_type = LYD_ANYDATA_DATATREE; |
| 354 | } |
| 355 | } else { |
| 356 | /* anyxml */ |
| 357 | switch (value_type) { |
| 358 | case LYD_ANYDATA_DATATREE: |
| 359 | /* fine, just use the value */ |
| 360 | break; |
| 361 | case LYD_ANYDATA_STRING: |
| 362 | /* detect value type */ |
| 363 | lyd_create_any_string_valtype(value, &value_type); |
| 364 | if ((value_type == LYD_ANYDATA_DATATREE) || (value_type == LYD_ANYDATA_STRING)) { |
| 365 | break; |
| 366 | } |
| 367 | /* fallthrough */ |
| 368 | case LYD_ANYDATA_XML: |
| 369 | case LYD_ANYDATA_JSON: |
| 370 | case LYD_ANYDATA_LYB: |
| 371 | if (!value) { |
| 372 | /* nothing to parse */ |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | /* create input */ |
| 377 | LY_CHECK_GOTO(rc = ly_in_new_memory(value, &in), cleanup); |
| 378 | |
| 379 | /* try to parse as a data tree */ |
| 380 | r = lyd_create_any_datatree(schema->module->ctx, in, value_type, 0, &tree); |
| 381 | if (!r) { |
| 382 | /* use the parsed data tree */ |
| 383 | if (use_value) { |
| 384 | free((void *)value); |
| 385 | } |
| 386 | use_value = 1; |
| 387 | value = tree; |
| 388 | value_type = LYD_ANYDATA_DATATREE; |
| 389 | } |
| 390 | break; |
| 391 | } |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 394 | if (use_value) { |
| 395 | switch (value_type) { |
| 396 | case LYD_ANYDATA_DATATREE: |
| 397 | any->value.tree = (void *)value; |
| 398 | break; |
| 399 | case LYD_ANYDATA_STRING: |
| 400 | case LYD_ANYDATA_XML: |
| 401 | case LYD_ANYDATA_JSON: |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 402 | LY_CHECK_GOTO(rc = lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 403 | break; |
| 404 | case LYD_ANYDATA_LYB: |
| 405 | any->value.mem = (void *)value; |
| 406 | break; |
| 407 | } |
| 408 | any->value_type = value_type; |
| 409 | } else { |
| 410 | any_val.str = value; |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 411 | LY_CHECK_GOTO(rc = lyd_any_copy_value(&any->node, &any_val, value_type), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 412 | } |
| 413 | lyd_hash(&any->node); |
| 414 | |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 415 | cleanup: |
| 416 | if (rc) { |
| 417 | lyd_free_tree(&any->node); |
| 418 | } else { |
| 419 | *node = &any->node; |
| 420 | } |
| 421 | ly_in_free(in, 0); |
| 422 | return rc; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | LY_ERR |
| 426 | lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len, |
| 427 | const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic, |
| 428 | LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node) |
| 429 | { |
| 430 | LY_ERR ret = LY_SUCCESS; |
| 431 | struct lyd_node_opaq *opaq; |
| 432 | |
| 433 | assert(ctx && name && name_len && format); |
| 434 | |
| 435 | if (!value_len && (!dynamic || !*dynamic)) { |
| 436 | value = ""; |
| 437 | } |
| 438 | |
| 439 | opaq = calloc(1, sizeof *opaq); |
| 440 | LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish); |
| 441 | |
| 442 | opaq->prev = &opaq->node; |
| 443 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish); |
| 444 | |
| 445 | if (pref_len) { |
| 446 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish); |
| 447 | } |
| 448 | if (module_key_len) { |
| 449 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish); |
| 450 | } |
| 451 | if (dynamic && *dynamic) { |
| 452 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish); |
| 453 | *dynamic = 0; |
| 454 | } else { |
| 455 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish); |
| 456 | } |
| 457 | |
| 458 | opaq->format = format; |
| 459 | opaq->val_prefix_data = val_prefix_data; |
| 460 | opaq->hints = hints; |
| 461 | opaq->ctx = ctx; |
| 462 | |
| 463 | finish: |
| 464 | if (ret) { |
| 465 | lyd_free_tree(&opaq->node); |
| 466 | ly_free_prefix_data(format, val_prefix_data); |
| 467 | } else { |
| 468 | *node = &opaq->node; |
| 469 | } |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | LIBYANG_API_DEF LY_ERR |
| 474 | lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 475 | struct lyd_node **node) |
| 476 | { |
| 477 | LY_ERR r; |
| 478 | struct lyd_node *ret = NULL; |
| 479 | const struct lysc_node *schema; |
| 480 | struct lysc_ext_instance *ext = NULL; |
| 481 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 482 | |
| 483 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 484 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 485 | |
| 486 | if (!module) { |
| 487 | module = parent->schema->module; |
| 488 | } |
| 489 | |
| 490 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, |
| 491 | LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0); |
| 492 | if (!schema && parent) { |
| 493 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 494 | strlen(name), &schema, &ext); |
| 495 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 496 | } |
| 497 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.", |
| 498 | name), LY_ENOTFOUND); |
| 499 | |
| 500 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 501 | if (ext) { |
| 502 | ret->flags |= LYD_EXT; |
| 503 | } |
| 504 | if (parent) { |
| 505 | lyd_insert_node(parent, NULL, ret, 0); |
| 506 | } |
| 507 | |
| 508 | if (node) { |
| 509 | *node = ret; |
| 510 | } |
| 511 | return LY_SUCCESS; |
| 512 | } |
| 513 | |
| 514 | LIBYANG_API_DEF LY_ERR |
| 515 | lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node) |
| 516 | { |
| 517 | struct lyd_node *ret = NULL; |
| 518 | const struct lysc_node *schema; |
| 519 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 520 | |
| 521 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 522 | |
| 523 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0); |
| 524 | if (!schema) { |
| 525 | if (ext->argument) { |
| 526 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.", |
| 527 | name, ext->argument, ext->def->name); |
| 528 | } else { |
| 529 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.", |
| 530 | name, ext->def->name); |
| 531 | } |
| 532 | return LY_ENOTFOUND; |
| 533 | } |
| 534 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 535 | |
| 536 | *node = ret; |
| 537 | |
| 538 | return LY_SUCCESS; |
| 539 | } |
| 540 | |
| 541 | /** |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame^] | 542 | * @brief Create a new lits node instance without its keys. |
| 543 | * |
| 544 | * @param[in] ctx Context to use for logging. |
| 545 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 546 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 547 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 548 | * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are |
| 549 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 550 | * @param[out] node Created node. |
| 551 | * @return LY_ERR value. |
| 552 | */ |
| 553 | static LY_ERR |
| 554 | _lyd_new_list_node(const struct ly_ctx *ctx, const struct lyd_node *parent, const struct lys_module *module, |
| 555 | const char *name, ly_bool output, struct lyd_node **node) |
| 556 | { |
| 557 | struct lyd_node *ret = NULL; |
| 558 | const struct lysc_node *schema; |
| 559 | struct lysc_ext_instance *ext = NULL; |
| 560 | LY_ERR r; |
| 561 | |
| 562 | if (!module) { |
| 563 | module = parent->schema->module; |
| 564 | } |
| 565 | |
| 566 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
| 567 | if (!schema && parent) { |
| 568 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 569 | strlen(name), &schema, &ext); |
| 570 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 571 | } |
| 572 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
| 573 | |
| 574 | /* create list inner node */ |
| 575 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 576 | |
| 577 | if (ext) { |
| 578 | ret->flags |= LYD_EXT; |
| 579 | } |
| 580 | |
| 581 | *node = ret; |
| 582 | return LY_SUCCESS; |
| 583 | } |
| 584 | |
| 585 | /** |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 586 | * @brief Create a new list node in the data tree. |
| 587 | * |
| 588 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 589 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 590 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 591 | * @param[in] format Format of key values. |
| 592 | * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are |
| 593 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 594 | * @param[out] node Optional created node. |
| 595 | * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must |
| 596 | * be followed by the value length. |
| 597 | * @return LY_ERR value. |
| 598 | */ |
| 599 | static LY_ERR |
| 600 | _lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format, |
| 601 | ly_bool output, struct lyd_node **node, va_list ap) |
| 602 | { |
| 603 | struct lyd_node *ret = NULL, *key; |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame^] | 604 | const struct lysc_node *key_s; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 605 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 606 | const void *key_val; |
| 607 | uint32_t key_len; |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame^] | 608 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 609 | |
| 610 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 611 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 612 | |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame^] | 613 | /* create the list node */ |
| 614 | LY_CHECK_RET(_lyd_new_list_node(ctx, parent, module, name, output, &ret)); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 615 | |
| 616 | /* create and insert all the keys */ |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame^] | 617 | for (key_s = lysc_node_child(ret->schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 618 | if (format == LY_VALUE_LYB) { |
| 619 | key_val = va_arg(ap, const void *); |
| 620 | key_len = va_arg(ap, uint32_t); |
| 621 | } else { |
| 622 | key_val = va_arg(ap, const char *); |
| 623 | key_len = key_val ? strlen((char *)key_val) : 0; |
| 624 | } |
| 625 | |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 626 | rc = lyd_create_term(key_s, key_val, key_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL, &key); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 627 | LY_CHECK_GOTO(rc, cleanup); |
| 628 | lyd_insert_node(ret, NULL, key, 1); |
| 629 | } |
| 630 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 631 | if (parent) { |
| 632 | lyd_insert_node(parent, NULL, ret, 0); |
| 633 | } |
| 634 | |
| 635 | cleanup: |
| 636 | if (rc) { |
| 637 | lyd_free_tree(ret); |
| 638 | ret = NULL; |
| 639 | } else if (node) { |
| 640 | *node = ret; |
| 641 | } |
| 642 | return rc; |
| 643 | } |
| 644 | |
| 645 | LIBYANG_API_DEF LY_ERR |
| 646 | lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 647 | struct lyd_node **node, ...) |
| 648 | { |
| 649 | LY_ERR rc; |
| 650 | va_list ap; |
| 651 | |
| 652 | va_start(ap, node); |
| 653 | |
| 654 | rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap); |
| 655 | |
| 656 | va_end(ap); |
| 657 | return rc; |
| 658 | } |
| 659 | |
| 660 | LIBYANG_API_DEF LY_ERR |
| 661 | lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 662 | struct lyd_node **node, ...) |
| 663 | { |
| 664 | LY_ERR rc; |
| 665 | va_list ap; |
| 666 | |
| 667 | va_start(ap, node); |
| 668 | |
| 669 | rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap); |
| 670 | |
| 671 | va_end(ap); |
| 672 | return rc; |
| 673 | } |
| 674 | |
| 675 | LIBYANG_API_DEF LY_ERR |
| 676 | lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 677 | struct lyd_node **node, ...) |
| 678 | { |
| 679 | LY_ERR rc; |
| 680 | va_list ap; |
| 681 | |
| 682 | va_start(ap, node); |
| 683 | |
| 684 | rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap); |
| 685 | |
| 686 | va_end(ap); |
| 687 | return rc; |
| 688 | } |
| 689 | |
| 690 | LIBYANG_API_DEF LY_ERR |
| 691 | lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...) |
| 692 | { |
| 693 | struct lyd_node *ret = NULL, *key; |
| 694 | const struct lysc_node *schema, *key_s; |
| 695 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 696 | va_list ap; |
| 697 | const char *key_val; |
| 698 | LY_ERR rc = LY_SUCCESS; |
| 699 | |
| 700 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 701 | |
| 702 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0); |
| 703 | if (!schema) { |
| 704 | if (ext->argument) { |
| 705 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.", |
| 706 | name, ext->argument, ext->def->name); |
| 707 | } else { |
| 708 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 709 | } |
| 710 | return LY_ENOTFOUND; |
| 711 | } |
| 712 | /* create list inner node */ |
| 713 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 714 | |
| 715 | va_start(ap, node); |
| 716 | |
| 717 | /* create and insert all the keys */ |
| 718 | for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 719 | key_val = va_arg(ap, const char *); |
| 720 | |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 721 | rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 722 | NULL, &key); |
| 723 | LY_CHECK_GOTO(rc, cleanup); |
| 724 | lyd_insert_node(ret, NULL, key, 1); |
| 725 | } |
| 726 | |
| 727 | cleanup: |
| 728 | va_end(ap); |
| 729 | if (rc) { |
| 730 | lyd_free_tree(ret); |
| 731 | ret = NULL; |
| 732 | } |
| 733 | *node = ret; |
| 734 | return rc; |
| 735 | } |
| 736 | |
| 737 | LIBYANG_API_DEF LY_ERR |
| 738 | lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys, |
| 739 | ly_bool output, struct lyd_node **node) |
| 740 | { |
| 741 | LY_ERR r; |
| 742 | struct lyd_node *ret = NULL; |
| 743 | const struct lysc_node *schema; |
| 744 | struct lysc_ext_instance *ext = NULL; |
| 745 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 746 | |
| 747 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 748 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 749 | |
| 750 | if (!module) { |
| 751 | module = parent->schema->module; |
| 752 | } |
| 753 | if (!keys) { |
| 754 | keys = ""; |
| 755 | } |
| 756 | |
| 757 | /* find schema node */ |
| 758 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
| 759 | if (!schema && parent) { |
| 760 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name), |
| 761 | &schema, &ext); |
| 762 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 763 | } |
| 764 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
| 765 | |
| 766 | if ((schema->flags & LYS_KEYLESS) && !keys[0]) { |
| 767 | /* key-less list */ |
| 768 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 769 | } else { |
| 770 | /* create the list node */ |
| 771 | LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret)); |
| 772 | } |
| 773 | if (ext) { |
| 774 | ret->flags |= LYD_EXT; |
| 775 | } |
| 776 | if (parent) { |
| 777 | lyd_insert_node(parent, NULL, ret, 0); |
| 778 | } |
| 779 | |
| 780 | if (node) { |
| 781 | *node = ret; |
| 782 | } |
| 783 | return LY_SUCCESS; |
| 784 | } |
| 785 | |
| 786 | /** |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame^] | 787 | * @brief Create a new list node in the data tree. |
| 788 | * |
| 789 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 790 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 791 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 792 | * @param[in] format Format of key values. |
| 793 | * @param[in] key_values Ordered key values of the new list instance ended with NULL, all must be set. |
| 794 | * @param[in] value_lengths Lengths of @p key_values, required for ::LY_VALUE_LYB, optional otherwise. |
| 795 | * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are |
| 796 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 797 | * @param[out] node Optional created node. |
| 798 | * @return LY_ERR value. |
| 799 | */ |
| 800 | static LY_ERR |
| 801 | _lyd_new_list3(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format, |
| 802 | const void **key_values, uint32_t *value_lengths, ly_bool output, struct lyd_node **node) |
| 803 | { |
| 804 | struct lyd_node *ret = NULL, *key; |
| 805 | const struct lysc_node *key_s; |
| 806 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 807 | const void *key_val; |
| 808 | uint32_t key_len, i; |
| 809 | LY_ERR rc = LY_SUCCESS; |
| 810 | |
| 811 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, key_values, (format != LY_VALUE_LYB) || value_lengths, |
| 812 | LY_EINVAL); |
| 813 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 814 | |
| 815 | /* create the list node */ |
| 816 | LY_CHECK_RET(_lyd_new_list_node(ctx, parent, module, name, output, &ret)); |
| 817 | |
| 818 | /* create and insert all the keys */ |
| 819 | i = 0; |
| 820 | for (key_s = lysc_node_child(ret->schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 821 | key_val = key_values[i]; |
| 822 | key_len = value_lengths ? value_lengths[i] : 0; |
| 823 | |
| 824 | rc = lyd_create_term(key_s, key_val, key_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL, &key); |
| 825 | LY_CHECK_GOTO(rc, cleanup); |
| 826 | lyd_insert_node(ret, NULL, key, 1); |
| 827 | } |
| 828 | |
| 829 | if (parent) { |
| 830 | lyd_insert_node(parent, NULL, ret, 0); |
| 831 | } |
| 832 | |
| 833 | cleanup: |
| 834 | if (rc) { |
| 835 | lyd_free_tree(ret); |
| 836 | ret = NULL; |
| 837 | } else if (node) { |
| 838 | *node = ret; |
| 839 | } |
| 840 | return rc; |
| 841 | } |
| 842 | |
| 843 | LIBYANG_API_DEF LY_ERR |
| 844 | lyd_new_list3(struct lyd_node *parent, const struct lys_module *module, const char *name, const char **key_values, |
| 845 | uint32_t *value_lengths, ly_bool output, struct lyd_node **node) |
| 846 | { |
| 847 | return _lyd_new_list3(parent, module, name, LY_VALUE_JSON, (const void **)key_values, value_lengths, output, node); |
| 848 | } |
| 849 | |
| 850 | LIBYANG_API_DEF LY_ERR |
| 851 | lyd_new_list3_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void **key_values, |
| 852 | uint32_t *value_lengths, ly_bool output, struct lyd_node **node) |
| 853 | { |
| 854 | return _lyd_new_list3(parent, module, name, LY_VALUE_LYB, key_values, value_lengths, output, node); |
| 855 | } |
| 856 | |
| 857 | LIBYANG_API_DEF LY_ERR |
| 858 | lyd_new_list3_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char **key_values, |
| 859 | uint32_t *value_lengths, ly_bool output, struct lyd_node **node) |
| 860 | { |
| 861 | return _lyd_new_list3(parent, module, name, LY_VALUE_CANON, (const void **)key_values, value_lengths, output, node); |
| 862 | } |
| 863 | |
| 864 | /** |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 865 | * @brief Create a new term node in the data tree. |
| 866 | * |
| 867 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 868 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 869 | * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST. |
| 870 | * @param[in] value Value of the node being created. |
| 871 | * @param[in] value_len Length of @p value. |
| 872 | * @param[in] format Format of @p value. |
| 873 | * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are |
| 874 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 875 | * @param[out] node Optional created node. |
| 876 | * @return LY_ERR value. |
| 877 | */ |
| 878 | static LY_ERR |
| 879 | _lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 880 | size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node) |
| 881 | { |
| 882 | LY_ERR r; |
| 883 | struct lyd_node *ret = NULL; |
| 884 | const struct lysc_node *schema; |
| 885 | struct lysc_ext_instance *ext = NULL; |
| 886 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 887 | |
| 888 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 889 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 890 | |
| 891 | if (!module) { |
| 892 | module = parent->schema->module; |
| 893 | } |
| 894 | |
| 895 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0); |
| 896 | if (!schema && parent) { |
| 897 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 898 | strlen(name), &schema, &ext); |
| 899 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 900 | } |
| 901 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND); |
| 902 | |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 903 | LY_CHECK_RET(lyd_create_term(schema, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret)); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 904 | if (ext) { |
| 905 | ret->flags |= LYD_EXT; |
| 906 | } |
| 907 | if (parent) { |
| 908 | lyd_insert_node(parent, NULL, ret, 0); |
| 909 | } |
| 910 | |
| 911 | if (node) { |
| 912 | *node = ret; |
| 913 | } |
| 914 | return LY_SUCCESS; |
| 915 | } |
| 916 | |
| 917 | LIBYANG_API_DEF LY_ERR |
| 918 | lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 919 | ly_bool output, struct lyd_node **node) |
| 920 | { |
| 921 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node); |
| 922 | } |
| 923 | |
| 924 | LIBYANG_API_DEF LY_ERR |
| 925 | lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 926 | size_t value_len, ly_bool output, struct lyd_node **node) |
| 927 | { |
| 928 | return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node); |
| 929 | } |
| 930 | |
| 931 | LIBYANG_API_DEF LY_ERR |
| 932 | lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 933 | ly_bool output, struct lyd_node **node) |
| 934 | { |
| 935 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node); |
| 936 | } |
| 937 | |
| 938 | LIBYANG_API_DEF LY_ERR |
| 939 | lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node) |
| 940 | { |
| 941 | LY_ERR rc; |
| 942 | struct lyd_node *ret = NULL; |
| 943 | const struct lysc_node *schema; |
| 944 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 945 | |
| 946 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 947 | |
| 948 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0); |
| 949 | if (!schema) { |
| 950 | if (ext->argument) { |
| 951 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.", |
| 952 | name, ext->argument, ext->def->name); |
| 953 | } else { |
| 954 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 955 | } |
| 956 | return LY_ENOTFOUND; |
| 957 | } |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 958 | rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, |
| 959 | NULL, &ret); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 960 | LY_CHECK_RET(rc); |
| 961 | |
| 962 | *node = ret; |
| 963 | |
| 964 | return LY_SUCCESS; |
| 965 | } |
| 966 | |
| 967 | LIBYANG_API_DEF LY_ERR |
| 968 | lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 969 | ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node) |
| 970 | { |
| 971 | LY_ERR r; |
| 972 | struct lyd_node *ret = NULL; |
| 973 | const struct lysc_node *schema; |
| 974 | struct lysc_ext_instance *ext = NULL; |
| 975 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 976 | |
| 977 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 978 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 979 | |
| 980 | if (!module) { |
| 981 | module = parent->schema->module; |
| 982 | } |
| 983 | |
| 984 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0); |
| 985 | if (!schema && parent) { |
| 986 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 987 | strlen(name), &schema, &ext); |
| 988 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 989 | } |
| 990 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND); |
| 991 | |
| 992 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
| 993 | if (ext) { |
| 994 | ret->flags |= LYD_EXT; |
| 995 | } |
| 996 | if (parent) { |
| 997 | lyd_insert_node(parent, NULL, ret, 0); |
| 998 | } |
| 999 | |
| 1000 | if (node) { |
| 1001 | *node = ret; |
| 1002 | } |
| 1003 | return LY_SUCCESS; |
| 1004 | } |
| 1005 | |
| 1006 | LIBYANG_API_DEF LY_ERR |
| 1007 | lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value, |
| 1008 | LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node) |
| 1009 | { |
| 1010 | struct lyd_node *ret = NULL; |
| 1011 | const struct lysc_node *schema; |
| 1012 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1013 | |
| 1014 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 1015 | |
| 1016 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0); |
| 1017 | if (!schema) { |
| 1018 | if (ext->argument) { |
| 1019 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.", |
| 1020 | name, ext->argument, ext->def->name); |
| 1021 | } else { |
| 1022 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 1023 | } |
| 1024 | return LY_ENOTFOUND; |
| 1025 | } |
| 1026 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
| 1027 | |
| 1028 | *node = ret; |
| 1029 | |
| 1030 | return LY_SUCCESS; |
| 1031 | } |
| 1032 | |
| 1033 | LIBYANG_API_DEF LY_ERR |
| 1034 | lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 1035 | const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta) |
| 1036 | { |
| 1037 | const char *prefix, *tmp; |
| 1038 | size_t pref_len, name_len; |
| 1039 | |
| 1040 | LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL); |
| 1041 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 1042 | if (!ctx) { |
Michal Vasko | 33c4897 | 2022-07-20 10:28:07 +0200 | [diff] [blame] | 1043 | ctx = module ? module->ctx : LYD_CTX(parent); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | if (parent && !parent->schema) { |
| 1047 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent)); |
| 1048 | return LY_EINVAL; |
| 1049 | } |
| 1050 | if (meta) { |
| 1051 | *meta = NULL; |
| 1052 | } |
| 1053 | |
| 1054 | /* parse the name */ |
| 1055 | tmp = name; |
| 1056 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1057 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 1058 | return LY_EINVAL; |
| 1059 | } |
| 1060 | |
| 1061 | /* find the module */ |
| 1062 | if (prefix) { |
| 1063 | module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len); |
| 1064 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND); |
| 1065 | } |
| 1066 | |
| 1067 | /* set value if none */ |
| 1068 | if (!val_str) { |
| 1069 | val_str = ""; |
| 1070 | } |
| 1071 | |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 1072 | return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), 0, NULL, LY_VALUE_JSON, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1073 | NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL); |
| 1074 | } |
| 1075 | |
| 1076 | LIBYANG_API_DEF LY_ERR |
| 1077 | lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr, |
| 1078 | struct lyd_meta **meta) |
| 1079 | { |
| 1080 | const struct lys_module *mod; |
| 1081 | |
| 1082 | LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL); |
| 1083 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
| 1084 | |
| 1085 | if (parent && !parent->schema) { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 1086 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", |
| 1087 | ((struct lyd_node_opaq *)parent)->name.name); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1088 | return LY_EINVAL; |
| 1089 | } |
| 1090 | if (meta) { |
| 1091 | *meta = NULL; |
| 1092 | } |
| 1093 | |
| 1094 | switch (attr->format) { |
| 1095 | case LY_VALUE_XML: |
| 1096 | mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns); |
| 1097 | if (!mod) { |
| 1098 | LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns); |
| 1099 | return LY_ENOTFOUND; |
| 1100 | } |
| 1101 | break; |
| 1102 | case LY_VALUE_JSON: |
| 1103 | mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name); |
| 1104 | if (!mod) { |
| 1105 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name); |
| 1106 | return LY_ENOTFOUND; |
| 1107 | } |
| 1108 | break; |
| 1109 | default: |
| 1110 | LOGINT_RET(ctx); |
| 1111 | } |
| 1112 | |
| 1113 | return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value), |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 1114 | 0, NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | LIBYANG_API_DEF LY_ERR |
| 1118 | lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
| 1119 | const char *prefix, const char *module_name, struct lyd_node **node) |
| 1120 | { |
| 1121 | struct lyd_node *ret = NULL; |
| 1122 | |
| 1123 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL); |
| 1124 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
| 1125 | |
| 1126 | if (!ctx) { |
| 1127 | ctx = LYD_CTX(parent); |
| 1128 | } |
| 1129 | if (!value) { |
| 1130 | value = ""; |
| 1131 | } |
| 1132 | |
| 1133 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name, |
| 1134 | strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret)); |
| 1135 | if (parent) { |
| 1136 | lyd_insert_node(parent, NULL, ret, 1); |
| 1137 | } |
| 1138 | |
| 1139 | if (node) { |
| 1140 | *node = ret; |
| 1141 | } |
| 1142 | return LY_SUCCESS; |
| 1143 | } |
| 1144 | |
| 1145 | LIBYANG_API_DEF LY_ERR |
| 1146 | lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
| 1147 | const char *prefix, const char *module_ns, struct lyd_node **node) |
| 1148 | { |
| 1149 | struct lyd_node *ret = NULL; |
| 1150 | |
| 1151 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL); |
| 1152 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
| 1153 | |
| 1154 | if (!ctx) { |
| 1155 | ctx = LYD_CTX(parent); |
| 1156 | } |
| 1157 | if (!value) { |
| 1158 | value = ""; |
| 1159 | } |
| 1160 | |
| 1161 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns, |
| 1162 | strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret)); |
| 1163 | if (parent) { |
| 1164 | lyd_insert_node(parent, NULL, ret, 1); |
| 1165 | } |
| 1166 | |
| 1167 | if (node) { |
| 1168 | *node = ret; |
| 1169 | } |
| 1170 | return LY_SUCCESS; |
| 1171 | } |
| 1172 | |
| 1173 | LIBYANG_API_DEF LY_ERR |
| 1174 | lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value, |
| 1175 | struct lyd_attr **attr) |
| 1176 | { |
| 1177 | struct lyd_attr *ret = NULL; |
| 1178 | const struct ly_ctx *ctx; |
| 1179 | const char *prefix, *tmp; |
| 1180 | size_t pref_len, name_len, mod_len; |
| 1181 | |
| 1182 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
| 1183 | |
| 1184 | ctx = LYD_CTX(parent); |
| 1185 | |
| 1186 | /* parse the name */ |
| 1187 | tmp = name; |
| 1188 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1189 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
| 1190 | return LY_EVALID; |
| 1191 | } |
| 1192 | |
| 1193 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 1194 | /* not a prefix but special name */ |
| 1195 | name = prefix; |
| 1196 | name_len += 1 + pref_len; |
| 1197 | prefix = NULL; |
| 1198 | pref_len = 0; |
| 1199 | } |
| 1200 | |
| 1201 | /* get the module */ |
| 1202 | if (module_name) { |
| 1203 | mod_len = strlen(module_name); |
| 1204 | } else { |
| 1205 | module_name = prefix; |
| 1206 | mod_len = pref_len; |
| 1207 | } |
| 1208 | |
| 1209 | /* set value if none */ |
| 1210 | if (!value) { |
| 1211 | value = ""; |
| 1212 | } |
| 1213 | |
| 1214 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value, |
| 1215 | strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA)); |
| 1216 | |
| 1217 | if (attr) { |
| 1218 | *attr = ret; |
| 1219 | } |
| 1220 | return LY_SUCCESS; |
| 1221 | } |
| 1222 | |
| 1223 | LIBYANG_API_DEF LY_ERR |
| 1224 | lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value, |
| 1225 | struct lyd_attr **attr) |
| 1226 | { |
| 1227 | struct lyd_attr *ret = NULL; |
| 1228 | const struct ly_ctx *ctx; |
| 1229 | const char *prefix, *tmp; |
| 1230 | size_t pref_len, name_len; |
| 1231 | |
| 1232 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
| 1233 | |
| 1234 | ctx = LYD_CTX(parent); |
| 1235 | |
| 1236 | /* parse the name */ |
| 1237 | tmp = name; |
| 1238 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1239 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
| 1240 | return LY_EVALID; |
| 1241 | } |
| 1242 | |
| 1243 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 1244 | /* not a prefix but special name */ |
| 1245 | name = prefix; |
| 1246 | name_len += 1 + pref_len; |
| 1247 | prefix = NULL; |
| 1248 | pref_len = 0; |
| 1249 | } |
| 1250 | |
| 1251 | /* set value if none */ |
| 1252 | if (!value) { |
| 1253 | value = ""; |
| 1254 | } |
Michal Vasko | 825718f | 2022-11-10 10:33:51 +0100 | [diff] [blame] | 1255 | if (strchr(value, ':')) { |
| 1256 | LOGWRN(ctx, "Value \"%s\" prefix will never be interpreted as an XML prefix.", value); |
| 1257 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1258 | |
| 1259 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns, |
| 1260 | module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA)); |
| 1261 | |
| 1262 | if (attr) { |
| 1263 | *attr = ret; |
| 1264 | } |
| 1265 | return LY_SUCCESS; |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * @brief Change the value of a term (leaf or leaf-list) node. |
| 1270 | * |
| 1271 | * Node changed this way is always considered explicitly set, meaning its default flag |
| 1272 | * is always cleared. |
| 1273 | * |
| 1274 | * @param[in] term Term node to change. |
| 1275 | * @param[in] value New value to set. |
| 1276 | * @param[in] value_len Length of @p value. |
| 1277 | * @param[in] format Format of @p value. |
| 1278 | * @return LY_SUCCESS if value was changed, |
| 1279 | * @return LY_EEXIST if value was the same and only the default flag was cleared, |
| 1280 | * @return LY_ENOT if the values were equal and no change occured, |
| 1281 | * @return LY_ERR value on other errors. |
| 1282 | */ |
| 1283 | static LY_ERR |
| 1284 | _lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format) |
| 1285 | { |
| 1286 | LY_ERR ret = LY_SUCCESS; |
| 1287 | struct lysc_type *type; |
| 1288 | struct lyd_node_term *t; |
| 1289 | struct lyd_node *parent; |
| 1290 | struct lyd_value val; |
| 1291 | ly_bool dflt_change, val_change; |
| 1292 | |
| 1293 | assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM)); |
| 1294 | |
| 1295 | t = (struct lyd_node_term *)term; |
| 1296 | type = ((struct lysc_node_leaf *)term->schema)->type; |
| 1297 | |
| 1298 | /* parse the new value */ |
| 1299 | LOG_LOCSET(term->schema, term, NULL, NULL); |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 1300 | ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, |
| 1301 | term->schema, NULL); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1302 | LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0); |
| 1303 | LY_CHECK_GOTO(ret, cleanup); |
| 1304 | |
| 1305 | /* compare original and new value */ |
| 1306 | if (type->plugin->compare(&t->value, &val)) { |
| 1307 | /* values differ, switch them */ |
| 1308 | type->plugin->free(LYD_CTX(term), &t->value); |
| 1309 | t->value = val; |
| 1310 | val_change = 1; |
| 1311 | } else { |
| 1312 | /* same values, free the new stored one */ |
| 1313 | type->plugin->free(LYD_CTX(term), &val); |
| 1314 | val_change = 0; |
| 1315 | } |
| 1316 | |
| 1317 | /* always clear the default flag */ |
| 1318 | if (term->flags & LYD_DEFAULT) { |
| 1319 | for (parent = term; parent; parent = lyd_parent(parent)) { |
| 1320 | parent->flags &= ~LYD_DEFAULT; |
| 1321 | } |
| 1322 | dflt_change = 1; |
| 1323 | } else { |
| 1324 | dflt_change = 0; |
| 1325 | } |
| 1326 | |
| 1327 | if (val_change || dflt_change) { |
| 1328 | /* make the node non-validated */ |
| 1329 | term->flags &= LYD_NEW; |
| 1330 | } |
| 1331 | |
| 1332 | if (val_change) { |
| 1333 | if (term->schema->nodetype == LYS_LEAFLIST) { |
| 1334 | /* leaf-list needs to be hashed again and re-inserted into parent */ |
| 1335 | lyd_unlink_hash(term); |
| 1336 | lyd_hash(term); |
| 1337 | LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup); |
| 1338 | } else if ((term->schema->flags & LYS_KEY) && term->parent) { |
| 1339 | /* list needs to be updated if its key was changed */ |
| 1340 | assert(term->parent->schema->nodetype == LYS_LIST); |
| 1341 | lyd_unlink_hash(lyd_parent(term)); |
| 1342 | lyd_hash(lyd_parent(term)); |
| 1343 | LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup); |
| 1344 | } /* else leaf that is not a key, its value is not used for its hash so it does not change */ |
| 1345 | } |
| 1346 | |
| 1347 | /* retrun value */ |
| 1348 | if (!val_change) { |
| 1349 | if (dflt_change) { |
| 1350 | /* only default flag change */ |
| 1351 | ret = LY_EEXIST; |
| 1352 | } else { |
| 1353 | /* no change */ |
| 1354 | ret = LY_ENOT; |
| 1355 | } |
| 1356 | } /* else value changed, LY_SUCCESS */ |
| 1357 | |
| 1358 | cleanup: |
| 1359 | return ret; |
| 1360 | } |
| 1361 | |
| 1362 | LIBYANG_API_DEF LY_ERR |
| 1363 | lyd_change_term(struct lyd_node *term, const char *val_str) |
| 1364 | { |
| 1365 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1366 | |
| 1367 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON); |
| 1368 | } |
| 1369 | |
| 1370 | LIBYANG_API_DEF LY_ERR |
| 1371 | lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len) |
| 1372 | { |
| 1373 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1374 | |
| 1375 | return _lyd_change_term(term, value, value_len, LY_VALUE_LYB); |
| 1376 | } |
| 1377 | |
| 1378 | LIBYANG_API_DEF LY_ERR |
| 1379 | lyd_change_term_canon(struct lyd_node *term, const char *val_str) |
| 1380 | { |
| 1381 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1382 | |
| 1383 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON); |
| 1384 | } |
| 1385 | |
| 1386 | LIBYANG_API_DEF LY_ERR |
| 1387 | lyd_change_meta(struct lyd_meta *meta, const char *val_str) |
| 1388 | { |
| 1389 | LY_ERR ret = LY_SUCCESS; |
| 1390 | struct lyd_meta *m2 = NULL; |
| 1391 | struct lyd_value val; |
| 1392 | ly_bool val_change; |
| 1393 | |
| 1394 | LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL); |
| 1395 | |
| 1396 | if (!val_str) { |
| 1397 | val_str = ""; |
| 1398 | } |
| 1399 | |
| 1400 | /* parse the new value into a new meta structure */ |
| 1401 | ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str), |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 1402 | 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1403 | LY_CHECK_GOTO(ret, cleanup); |
| 1404 | |
| 1405 | /* compare original and new value */ |
| 1406 | if (lyd_compare_meta(meta, m2)) { |
| 1407 | /* values differ, switch them */ |
| 1408 | val = meta->value; |
| 1409 | meta->value = m2->value; |
| 1410 | m2->value = val; |
| 1411 | val_change = 1; |
| 1412 | } else { |
| 1413 | val_change = 0; |
| 1414 | } |
| 1415 | |
| 1416 | /* retrun value */ |
| 1417 | if (!val_change) { |
| 1418 | /* no change */ |
| 1419 | ret = LY_ENOT; |
| 1420 | } /* else value changed, LY_SUCCESS */ |
| 1421 | |
| 1422 | cleanup: |
| 1423 | lyd_free_meta_single(m2); |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * @brief Update node value. |
| 1429 | * |
| 1430 | * @param[in] node Node to update. |
| 1431 | * @param[in] value New value to set. |
| 1432 | * @param[in] value_len Length of @p value. |
| 1433 | * @param[in] value_type Type of @p value for anydata/anyxml node. |
| 1434 | * @param[in] format Format of @p value. |
| 1435 | * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL. |
| 1436 | * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL. |
| 1437 | * @return LY_ERR value. |
| 1438 | */ |
| 1439 | static LY_ERR |
| 1440 | lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, |
| 1441 | LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node) |
| 1442 | { |
| 1443 | LY_ERR ret = LY_SUCCESS; |
| 1444 | struct lyd_node *new_any; |
| 1445 | |
| 1446 | switch (node->schema->nodetype) { |
| 1447 | case LYS_CONTAINER: |
| 1448 | case LYS_NOTIF: |
| 1449 | case LYS_RPC: |
| 1450 | case LYS_ACTION: |
| 1451 | case LYS_LIST: |
| 1452 | /* if it exists, there is nothing to update */ |
| 1453 | *new_parent = NULL; |
| 1454 | *new_node = NULL; |
| 1455 | break; |
| 1456 | case LYS_LEAFLIST: |
| 1457 | if (!lysc_is_dup_inst_list(node->schema)) { |
| 1458 | /* if it exists, there is nothing to update */ |
| 1459 | *new_parent = NULL; |
| 1460 | *new_node = NULL; |
| 1461 | break; |
| 1462 | } |
| 1463 | /* fallthrough */ |
| 1464 | case LYS_LEAF: |
| 1465 | ret = _lyd_change_term(node, value, value_len, format); |
| 1466 | if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) { |
| 1467 | /* there was an actual change (at least of the default flag) */ |
| 1468 | *new_parent = node; |
| 1469 | *new_node = node; |
| 1470 | ret = LY_SUCCESS; |
| 1471 | } else if (ret == LY_ENOT) { |
| 1472 | /* no change */ |
| 1473 | *new_parent = NULL; |
| 1474 | *new_node = NULL; |
| 1475 | ret = LY_SUCCESS; |
| 1476 | } /* else error */ |
| 1477 | break; |
| 1478 | case LYS_ANYDATA: |
| 1479 | case LYS_ANYXML: |
| 1480 | /* create a new any node */ |
| 1481 | LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any)); |
| 1482 | |
| 1483 | /* compare with the existing one */ |
| 1484 | if (lyd_compare_single(node, new_any, 0)) { |
| 1485 | /* not equal, switch values (so that we can use generic node free) */ |
| 1486 | ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value; |
| 1487 | ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type; |
| 1488 | ((struct lyd_node_any *)node)->value.str = value; |
| 1489 | ((struct lyd_node_any *)node)->value_type = value_type; |
| 1490 | |
| 1491 | *new_parent = node; |
| 1492 | *new_node = node; |
| 1493 | } else { |
| 1494 | /* they are equal */ |
| 1495 | *new_parent = NULL; |
| 1496 | *new_node = NULL; |
| 1497 | } |
| 1498 | lyd_free_tree(new_any); |
| 1499 | break; |
| 1500 | default: |
| 1501 | LOGINT(LYD_CTX(node)); |
| 1502 | ret = LY_EINT; |
| 1503 | break; |
| 1504 | } |
| 1505 | |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | static LY_ERR |
| 1510 | lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len, |
| 1511 | LY_VALUE_FORMAT format, uint32_t options) |
| 1512 | { |
| 1513 | LY_ERR r; |
| 1514 | struct ly_path_predicate *pred; |
| 1515 | struct lyd_value val; |
| 1516 | const struct lysc_node *schema = NULL; |
| 1517 | LY_ARRAY_COUNT_TYPE u, new_count; |
| 1518 | int create = 0; |
| 1519 | |
| 1520 | assert(path); |
| 1521 | |
| 1522 | /* go through all the compiled nodes */ |
| 1523 | LY_ARRAY_FOR(path, u) { |
| 1524 | schema = path[u].node; |
| 1525 | |
| 1526 | if (lysc_is_dup_inst_list(schema)) { |
Michal Vasko | 5de2154 | 2023-03-20 10:00:05 +0100 | [diff] [blame] | 1527 | if (!path[u].predicates || |
| 1528 | ((schema->nodetype == LYS_LEAFLIST) && (path[u].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST))) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1529 | /* creating a new key-less list or state leaf-list instance */ |
| 1530 | create = 1; |
| 1531 | new_count = u; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1532 | } else if (path[u].predicates[0].type != LY_PATH_PREDTYPE_POSITION) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1533 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Michal Vasko | 5de2154 | 2023-03-20 10:00:05 +0100 | [diff] [blame] | 1534 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for state %s \"%s\" in path \"%s\".", |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1535 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1536 | LOG_LOCBACK(1, 0, 0, 0); |
| 1537 | return LY_EINVAL; |
| 1538 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1539 | } else if ((schema->nodetype == LYS_LIST) && |
| 1540 | (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LIST))) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1541 | if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) { |
| 1542 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 1543 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".", |
| 1544 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1545 | LOG_LOCBACK(1, 0, 0, 0); |
| 1546 | return LY_EINVAL; |
| 1547 | } /* else creating an opaque list */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1548 | } else if ((schema->nodetype == LYS_LEAFLIST) && |
| 1549 | (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1550 | r = LY_SUCCESS; |
| 1551 | if (options & LYD_NEW_PATH_OPAQ) { |
| 1552 | r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL); |
| 1553 | } |
| 1554 | if (!r) { |
| 1555 | /* try to store the value */ |
| 1556 | LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type, |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 1557 | value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, schema, NULL)); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1558 | ++((struct lysc_type *)val.realtype)->refcount; |
| 1559 | |
| 1560 | /* store the new predicate so that it is used when searching for this instance */ |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1561 | LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1562 | pred->type = LY_PATH_PREDTYPE_LEAFLIST; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1563 | pred->value = val; |
| 1564 | } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */ |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | if (create) { |
| 1569 | /* hide the nodes that should always be created so they are not found */ |
| 1570 | while (new_count < LY_ARRAY_COUNT(path)) { |
| 1571 | LY_ARRAY_DECREMENT(path); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | return LY_SUCCESS; |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * @brief Create a new node in the data tree based on a path. All node types can be created. |
| 1580 | * |
| 1581 | * If @p path points to a list key, the key value from the predicate is used and @p value is ignored. |
| 1582 | * Also, if a leaf-list is being created and both a predicate is defined in @p path |
| 1583 | * and @p value is set, the predicate is preferred. |
| 1584 | * |
| 1585 | * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these |
| 1586 | * nodes, they are always created. |
| 1587 | * |
| 1588 | * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used, |
| 1589 | * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted |
| 1590 | * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases. |
| 1591 | * @param[in] ctx libyang context, must be set if @p parent is NULL. |
| 1592 | * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute |
| 1593 | * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces |
| 1594 | * searching for the appropriate module. |
| 1595 | * @param[in] path [Path](@ref howtoXPath) to create. |
| 1596 | * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an |
| 1597 | * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL. |
| 1598 | * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when |
| 1599 | * creating anyxml/anydata nodes. |
| 1600 | * @param[in] value_type Anyxml/anydata node @p value type. |
| 1601 | * @param[in] options Bitmask of options, see @ref pathoptions. |
| 1602 | * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node. |
| 1603 | * @param[out] new_node Optional last node created. |
| 1604 | * @return LY_ERR value. |
| 1605 | */ |
| 1606 | static LY_ERR |
| 1607 | lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path, |
| 1608 | const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, |
| 1609 | struct lyd_node **new_parent, struct lyd_node **new_node) |
| 1610 | { |
| 1611 | LY_ERR ret = LY_SUCCESS, r; |
| 1612 | struct lyxp_expr *exp = NULL; |
| 1613 | struct ly_path *p = NULL; |
| 1614 | struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent; |
| 1615 | const struct lysc_node *schema; |
| 1616 | const struct lyd_value *val = NULL; |
| 1617 | LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0; |
| 1618 | LY_VALUE_FORMAT format; |
| 1619 | |
| 1620 | assert(parent || ctx); |
| 1621 | assert(path && ((path[0] == '/') || parent)); |
| 1622 | assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE)); |
| 1623 | |
| 1624 | if (!ctx) { |
| 1625 | ctx = LYD_CTX(parent); |
| 1626 | } |
| 1627 | if (value && !value_len) { |
| 1628 | value_len = strlen(value); |
| 1629 | } |
| 1630 | if (options & LYD_NEW_PATH_BIN_VALUE) { |
| 1631 | format = LY_VALUE_LYB; |
| 1632 | } else if (options & LYD_NEW_PATH_CANON_VALUE) { |
| 1633 | format = LY_VALUE_CANON; |
| 1634 | } else { |
| 1635 | format = LY_VALUE_JSON; |
| 1636 | } |
| 1637 | |
| 1638 | /* parse path */ |
Michal Vasko | 074d9a1 | 2023-06-22 08:23:41 +0200 | [diff] [blame] | 1639 | LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1640 | LY_PATH_PRED_SIMPLE, &exp), cleanup); |
| 1641 | |
| 1642 | /* compile path */ |
| 1643 | LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ? |
| 1644 | LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup); |
| 1645 | |
| 1646 | /* check the compiled path before searching existing nodes, it may be shortened */ |
| 1647 | orig_count = LY_ARRAY_COUNT(p); |
| 1648 | LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup); |
| 1649 | |
| 1650 | /* try to find any existing nodes in the path */ |
| 1651 | if (parent) { |
Michal Vasko | 838829d | 2023-10-09 16:06:43 +0200 | [diff] [blame] | 1652 | r = ly_path_eval_partial(p, parent, NULL, options & LYD_NEW_PATH_WITH_OPAQ, &path_idx, &node); |
Michal Vasko | f256051 | 2023-03-03 09:54:47 +0100 | [diff] [blame] | 1653 | if (r == LY_SUCCESS) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1654 | if (orig_count == LY_ARRAY_COUNT(p)) { |
| 1655 | /* the node exists, are we supposed to update it or is it just a default? */ |
| 1656 | if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) { |
| 1657 | LOG_LOCSET(NULL, node, NULL, NULL); |
Michal Vasko | a976492 | 2023-02-09 13:59:07 +0100 | [diff] [blame] | 1658 | LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists.", path); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1659 | LOG_LOCBACK(0, 1, 0, 0); |
| 1660 | ret = LY_EEXIST; |
| 1661 | goto cleanup; |
| 1662 | } |
| 1663 | |
| 1664 | /* update the existing node */ |
| 1665 | ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode); |
| 1666 | goto cleanup; |
| 1667 | } /* else we were not searching for the whole path */ |
Michal Vasko | f256051 | 2023-03-03 09:54:47 +0100 | [diff] [blame] | 1668 | } else if (r == LY_EINCOMPLETE) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1669 | /* some nodes were found, adjust the iterator to the next segment */ |
| 1670 | ++path_idx; |
Michal Vasko | f256051 | 2023-03-03 09:54:47 +0100 | [diff] [blame] | 1671 | } else if (r == LY_ENOTFOUND) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1672 | /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */ |
| 1673 | if (lysc_data_parent(p[0].node)) { |
| 1674 | node = parent; |
| 1675 | } |
| 1676 | } else { |
| 1677 | /* error */ |
Michal Vasko | f256051 | 2023-03-03 09:54:47 +0100 | [diff] [blame] | 1678 | ret = r; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1679 | goto cleanup; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | /* restore the full path for creating new nodes */ |
| 1684 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 1685 | LY_ARRAY_INCREMENT(p); |
| 1686 | } |
| 1687 | |
| 1688 | /* create all the non-existing nodes in a loop */ |
| 1689 | for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) { |
| 1690 | cur_parent = node; |
| 1691 | schema = p[path_idx].node; |
| 1692 | |
| 1693 | switch (schema->nodetype) { |
| 1694 | case LYS_LIST: |
| 1695 | if (lysc_is_dup_inst_list(schema)) { |
| 1696 | /* create key-less list instance */ |
| 1697 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1698 | } else if ((options & LYD_NEW_PATH_OPAQ) && !p[path_idx].predicates) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1699 | /* creating opaque list without keys */ |
| 1700 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, |
| 1701 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL, |
| 1702 | LYD_NODEHINT_LIST, &node), cleanup); |
| 1703 | } else { |
| 1704 | /* create standard list instance */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1705 | LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, NULL, &node), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1706 | } |
| 1707 | break; |
| 1708 | case LYS_CONTAINER: |
| 1709 | case LYS_NOTIF: |
| 1710 | case LYS_RPC: |
| 1711 | case LYS_ACTION: |
| 1712 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1713 | break; |
| 1714 | case LYS_LEAFLIST: |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1715 | if ((options & LYD_NEW_PATH_OPAQ) && |
| 1716 | (!p[path_idx].predicates || (p[path_idx].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1717 | /* we have not checked this only for dup-inst lists, otherwise it must be opaque */ |
| 1718 | r = LY_EVALID; |
| 1719 | if (lysc_is_dup_inst_list(schema)) { |
| 1720 | /* validate value */ |
| 1721 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
| 1722 | } |
| 1723 | if (r && (r != LY_EINCOMPLETE)) { |
| 1724 | /* creating opaque leaf-list */ |
| 1725 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 1726 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, |
| 1727 | LYD_NODEHINT_LEAFLIST, &node), cleanup); |
| 1728 | break; |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | /* get value to set */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1733 | if (p[path_idx].predicates && (p[path_idx].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST)) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1734 | val = &p[path_idx].predicates[0].value; |
| 1735 | } |
| 1736 | |
| 1737 | /* create a leaf-list instance */ |
| 1738 | if (val) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1739 | LY_CHECK_GOTO(ret = lyd_create_term2(schema, val, &node), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1740 | } else { |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 1741 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1742 | NULL, &node), cleanup); |
| 1743 | } |
| 1744 | break; |
| 1745 | case LYS_LEAF: |
| 1746 | if (lysc_is_key(schema) && cur_parent->schema) { |
| 1747 | /* it must have been already created or some error will occur later */ |
| 1748 | lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node); |
| 1749 | assert(node); |
| 1750 | goto next_iter; |
| 1751 | } |
| 1752 | |
| 1753 | if (options & LYD_NEW_PATH_OPAQ) { |
| 1754 | if (cur_parent && !cur_parent->schema) { |
| 1755 | /* always create opaque nodes for opaque parents */ |
| 1756 | r = LY_ENOT; |
| 1757 | } else { |
| 1758 | /* validate value */ |
| 1759 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
| 1760 | } |
| 1761 | if (r && (r != LY_EINCOMPLETE)) { |
| 1762 | /* creating opaque leaf */ |
| 1763 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 1764 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node), |
| 1765 | cleanup); |
| 1766 | break; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | /* create a leaf instance */ |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 1771 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL, |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1772 | &node), cleanup); |
| 1773 | break; |
| 1774 | case LYS_ANYDATA: |
| 1775 | case LYS_ANYXML: |
| 1776 | LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup); |
| 1777 | break; |
| 1778 | default: |
| 1779 | LOGINT(ctx); |
| 1780 | ret = LY_EINT; |
| 1781 | goto cleanup; |
| 1782 | } |
| 1783 | |
| 1784 | if (p[path_idx].ext) { |
| 1785 | node->flags |= LYD_EXT; |
| 1786 | } |
| 1787 | if (cur_parent) { |
| 1788 | /* connect to the parent */ |
| 1789 | lyd_insert_node(cur_parent, NULL, node, 0); |
| 1790 | } else if (parent) { |
| 1791 | /* connect to top-level siblings */ |
| 1792 | lyd_insert_node(NULL, &parent, node, 0); |
| 1793 | } |
| 1794 | |
| 1795 | next_iter: |
| 1796 | /* update remembered nodes */ |
| 1797 | if (!nparent) { |
| 1798 | nparent = node; |
| 1799 | } |
| 1800 | nnode = node; |
| 1801 | } |
| 1802 | |
| 1803 | cleanup: |
| 1804 | lyxp_expr_free(ctx, exp); |
| 1805 | if (p) { |
| 1806 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 1807 | LY_ARRAY_INCREMENT(p); |
| 1808 | } |
| 1809 | } |
| 1810 | ly_path_free(ctx, p); |
| 1811 | if (!ret) { |
| 1812 | /* set out params only on success */ |
| 1813 | if (new_parent) { |
| 1814 | *new_parent = nparent; |
| 1815 | } |
| 1816 | if (new_node) { |
| 1817 | *new_node = nnode; |
| 1818 | } |
| 1819 | } else { |
| 1820 | lyd_free_tree(nparent); |
| 1821 | } |
| 1822 | return ret; |
| 1823 | } |
| 1824 | |
| 1825 | LIBYANG_API_DEF LY_ERR |
| 1826 | lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options, |
| 1827 | struct lyd_node **node) |
| 1828 | { |
| 1829 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 1830 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
| 1831 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 1832 | |
| 1833 | return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
| 1834 | } |
| 1835 | |
| 1836 | LIBYANG_API_DEF LY_ERR |
| 1837 | lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value, |
| 1838 | size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, |
| 1839 | struct lyd_node **new_node) |
| 1840 | { |
| 1841 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 1842 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
| 1843 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 1844 | |
| 1845 | return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node); |
| 1846 | } |
| 1847 | |
| 1848 | LIBYANG_API_DEF LY_ERR |
| 1849 | lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value, |
| 1850 | uint32_t options, struct lyd_node **node) |
| 1851 | { |
| 1852 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1853 | |
| 1854 | LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent, |
| 1855 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
| 1856 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 1857 | |
| 1858 | return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
| 1859 | } |
| 1860 | |
| 1861 | LY_ERR |
| 1862 | lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent, |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1863 | const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node, |
| 1864 | uint32_t impl_opts, struct lyd_node **diff) |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1865 | { |
| 1866 | LY_ERR ret; |
| 1867 | const struct lysc_node *iter = NULL; |
| 1868 | struct lyd_node *node = NULL; |
| 1869 | struct lyd_value **dflts; |
| 1870 | LY_ARRAY_COUNT_TYPE u; |
| 1871 | uint32_t getnext_opts; |
| 1872 | |
| 1873 | assert(first && (parent || sparent || mod)); |
| 1874 | |
| 1875 | if (!sparent && parent) { |
| 1876 | sparent = parent->schema; |
| 1877 | } |
| 1878 | |
| 1879 | getnext_opts = LYS_GETNEXT_WITHCHOICE; |
| 1880 | if (impl_opts & LYD_IMPLICIT_OUTPUT) { |
| 1881 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 1882 | } |
| 1883 | |
| 1884 | while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) { |
| 1885 | if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) { |
| 1886 | continue; |
| 1887 | } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) { |
| 1888 | continue; |
| 1889 | } |
| 1890 | |
| 1891 | switch (iter->nodetype) { |
| 1892 | case LYS_CHOICE: |
| 1893 | node = lys_getnext_data(NULL, *first, NULL, iter, NULL); |
| 1894 | if (!node && ((struct lysc_node_choice *)iter)->dflt) { |
| 1895 | /* create default case data */ |
| 1896 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node, |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1897 | NULL, node_when, node_types, ext_node, impl_opts, diff)); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1898 | } else if (node) { |
| 1899 | /* create any default data in the existing case */ |
| 1900 | assert(node->schema->parent->nodetype == LYS_CASE); |
| 1901 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types, |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1902 | ext_node, impl_opts, diff)); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1903 | } |
| 1904 | break; |
| 1905 | case LYS_CONTAINER: |
| 1906 | if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1907 | /* create default NP container */ |
| 1908 | LY_CHECK_RET(lyd_create_inner(iter, &node)); |
| 1909 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
| 1910 | lyd_insert_node(parent, first, node, 0); |
| 1911 | |
| 1912 | if (lysc_has_when(iter) && node_when) { |
| 1913 | /* remember to resolve when */ |
| 1914 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 1915 | } |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1916 | if (ext_node) { |
| 1917 | /* store for ext instance node validation, if needed */ |
| 1918 | LY_CHECK_RET(lyd_validate_node_ext(node, ext_node)); |
| 1919 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1920 | if (diff) { |
| 1921 | /* add into diff */ |
| 1922 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1923 | } |
| 1924 | |
| 1925 | /* create any default children */ |
| 1926 | LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types, |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1927 | ext_node, impl_opts, diff)); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1928 | } |
| 1929 | break; |
| 1930 | case LYS_LEAF: |
| 1931 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt && |
| 1932 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1933 | /* create default leaf */ |
| 1934 | ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node); |
| 1935 | if (ret == LY_EINCOMPLETE) { |
| 1936 | if (node_types) { |
| 1937 | /* remember to resolve type */ |
| 1938 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
| 1939 | } |
| 1940 | } else if (ret) { |
| 1941 | return ret; |
| 1942 | } |
| 1943 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
| 1944 | lyd_insert_node(parent, first, node, 0); |
| 1945 | |
| 1946 | if (lysc_has_when(iter) && node_when) { |
| 1947 | /* remember to resolve when */ |
| 1948 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 1949 | } |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1950 | if (ext_node) { |
| 1951 | /* store for ext instance node validation, if needed */ |
| 1952 | LY_CHECK_RET(lyd_validate_node_ext(node, ext_node)); |
| 1953 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1954 | if (diff) { |
| 1955 | /* add into diff */ |
| 1956 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1957 | } |
| 1958 | } |
| 1959 | break; |
| 1960 | case LYS_LEAFLIST: |
| 1961 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts && |
| 1962 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1963 | /* create all default leaf-lists */ |
| 1964 | dflts = ((struct lysc_node_leaflist *)iter)->dflts; |
| 1965 | LY_ARRAY_FOR(dflts, u) { |
| 1966 | ret = lyd_create_term2(iter, dflts[u], &node); |
| 1967 | if (ret == LY_EINCOMPLETE) { |
| 1968 | if (node_types) { |
| 1969 | /* remember to resolve type */ |
| 1970 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
| 1971 | } |
| 1972 | } else if (ret) { |
| 1973 | return ret; |
| 1974 | } |
| 1975 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
| 1976 | lyd_insert_node(parent, first, node, 0); |
| 1977 | |
| 1978 | if (lysc_has_when(iter) && node_when) { |
| 1979 | /* remember to resolve when */ |
| 1980 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 1981 | } |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1982 | if (ext_node) { |
| 1983 | /* store for ext instance node validation, if needed */ |
| 1984 | LY_CHECK_RET(lyd_validate_node_ext(node, ext_node)); |
| 1985 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1986 | if (diff) { |
| 1987 | /* add into diff */ |
| 1988 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | break; |
| 1993 | default: |
| 1994 | /* without defaults */ |
| 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | return LY_SUCCESS; |
| 2000 | } |
| 2001 | |
| 2002 | LIBYANG_API_DEF LY_ERR |
| 2003 | lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff) |
| 2004 | { |
| 2005 | LY_ERR ret = LY_SUCCESS; |
| 2006 | struct lyd_node *node; |
| 2007 | struct ly_set node_when = {0}; |
| 2008 | |
| 2009 | LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL); |
| 2010 | if (diff) { |
| 2011 | *diff = NULL; |
| 2012 | } |
| 2013 | |
| 2014 | LYD_TREE_DFS_BEGIN(tree, node) { |
Michal Vasko | f87fbdf | 2023-07-11 10:20:02 +0200 | [diff] [blame] | 2015 | if (node->schema->nodetype & LYD_NODE_INNER) { |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2016 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL, |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 2017 | NULL, implicit_options, diff), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | LYD_TREE_DFS_END(tree, node); |
| 2021 | } |
| 2022 | |
| 2023 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 2024 | ret = lyd_validate_unres(&tree, NULL, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL, 0, diff); |
Michal Vasko | fbbea93 | 2022-06-07 11:00:55 +0200 | [diff] [blame] | 2025 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2026 | |
| 2027 | cleanup: |
| 2028 | ly_set_erase(&node_when, NULL); |
| 2029 | if (ret && diff) { |
| 2030 | lyd_free_all(*diff); |
| 2031 | *diff = NULL; |
| 2032 | } |
| 2033 | return ret; |
| 2034 | } |
| 2035 | |
| 2036 | LIBYANG_API_DEF LY_ERR |
| 2037 | lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff) |
| 2038 | { |
| 2039 | const struct lys_module *mod; |
| 2040 | struct lyd_node *d = NULL; |
| 2041 | uint32_t i = 0; |
| 2042 | LY_ERR ret = LY_SUCCESS; |
| 2043 | |
| 2044 | LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL); |
| 2045 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL); |
| 2046 | if (diff) { |
| 2047 | *diff = NULL; |
| 2048 | } |
| 2049 | if (!ctx) { |
| 2050 | ctx = LYD_CTX(*tree); |
| 2051 | } |
| 2052 | |
| 2053 | /* add nodes for each module one-by-one */ |
| 2054 | while ((mod = ly_ctx_get_module_iter(ctx, &i))) { |
| 2055 | if (!mod->implemented) { |
| 2056 | continue; |
| 2057 | } |
| 2058 | |
| 2059 | LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup); |
| 2060 | if (d) { |
| 2061 | /* merge into one diff */ |
| 2062 | lyd_insert_sibling(*diff, d, diff); |
| 2063 | |
| 2064 | d = NULL; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | cleanup: |
| 2069 | if (ret && diff) { |
| 2070 | lyd_free_all(*diff); |
| 2071 | *diff = NULL; |
| 2072 | } |
| 2073 | return ret; |
| 2074 | } |
| 2075 | |
| 2076 | LIBYANG_API_DEF LY_ERR |
| 2077 | lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, |
| 2078 | struct lyd_node **diff) |
| 2079 | { |
| 2080 | LY_ERR ret = LY_SUCCESS; |
| 2081 | struct lyd_node *root, *d = NULL; |
| 2082 | struct ly_set node_when = {0}; |
| 2083 | |
| 2084 | LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL); |
| 2085 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 2086 | if (diff) { |
| 2087 | *diff = NULL; |
| 2088 | } |
| 2089 | |
| 2090 | /* add all top-level defaults for this module */ |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 2091 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, NULL, implicit_options, diff), |
| 2092 | cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2093 | |
| 2094 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 2095 | LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL, |
| 2096 | 0, diff), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2097 | |
| 2098 | /* process nested nodes */ |
| 2099 | LY_LIST_FOR(*tree, root) { |
Michal Vasko | f87fbdf | 2023-07-11 10:20:02 +0200 | [diff] [blame] | 2100 | LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2101 | |
Michal Vasko | f87fbdf | 2023-07-11 10:20:02 +0200 | [diff] [blame] | 2102 | if (d) { |
| 2103 | /* merge into one diff */ |
| 2104 | lyd_insert_sibling(*diff, d, diff); |
| 2105 | d = NULL; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | cleanup: |
| 2110 | ly_set_erase(&node_when, NULL); |
| 2111 | if (ret && diff) { |
| 2112 | lyd_free_all(*diff); |
| 2113 | *diff = NULL; |
| 2114 | } |
| 2115 | return ret; |
| 2116 | } |