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 |
| 54 | lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic, |
| 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, |
| 71 | value_len, dynamic, format, prefix_data, hints, schema, incomplete); |
| 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 |
| 137 | lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node) |
| 138 | { |
| 139 | LY_ERR ret = LY_SUCCESS; |
| 140 | struct lyd_node *list = NULL, *key; |
| 141 | LY_ARRAY_COUNT_TYPE u; |
| 142 | |
| 143 | assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS)); |
| 144 | |
| 145 | /* create list */ |
| 146 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup); |
| 147 | |
| 148 | LOG_LOCSET(NULL, list, NULL, NULL); |
| 149 | |
| 150 | /* create and insert all the keys */ |
| 151 | LY_ARRAY_FOR(predicates, u) { |
| 152 | LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup); |
| 153 | lyd_insert_node(list, NULL, key, 0); |
| 154 | } |
| 155 | |
| 156 | /* hash having all the keys */ |
| 157 | lyd_hash(list); |
| 158 | |
| 159 | /* success */ |
| 160 | *node = list; |
| 161 | list = NULL; |
| 162 | |
| 163 | cleanup: |
| 164 | LOG_LOCBACK(0, 1, 0, 0); |
| 165 | lyd_free_tree(list); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | LY_ERR |
| 170 | lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node) |
| 171 | { |
| 172 | LY_ERR ret = LY_SUCCESS; |
| 173 | struct lyxp_expr *expr = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 174 | uint32_t exp_idx = 0; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 175 | enum ly_path_pred_type pred_type = 0; |
| 176 | struct ly_path_predicate *predicates = NULL; |
| 177 | |
| 178 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 179 | |
| 180 | /* parse keys */ |
| 181 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL, |
| 182 | LY_PATH_PRED_KEYS, &expr), cleanup); |
| 183 | |
| 184 | /* compile them */ |
| 185 | LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON, |
| 186 | NULL, &predicates, &pred_type), cleanup); |
| 187 | |
| 188 | /* create the list node */ |
| 189 | LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup); |
| 190 | |
| 191 | cleanup: |
| 192 | LOG_LOCBACK(1, 0, 0, 0); |
| 193 | lyxp_expr_free(schema->module->ctx, expr); |
| 194 | ly_path_predicates_free(schema->module->ctx, pred_type, predicates); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | LY_ERR |
| 199 | lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value, |
| 200 | struct lyd_node **node) |
| 201 | { |
| 202 | LY_ERR ret; |
| 203 | struct lyd_node_any *any; |
| 204 | union lyd_any_value any_val; |
| 205 | |
| 206 | assert(schema->nodetype & LYD_NODE_ANY); |
| 207 | |
| 208 | any = calloc(1, sizeof *any); |
| 209 | LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM); |
| 210 | |
| 211 | any->schema = schema; |
| 212 | any->prev = &any->node; |
| 213 | any->flags = LYD_NEW; |
| 214 | |
| 215 | if (use_value) { |
| 216 | switch (value_type) { |
| 217 | case LYD_ANYDATA_DATATREE: |
| 218 | any->value.tree = (void *)value; |
| 219 | break; |
| 220 | case LYD_ANYDATA_STRING: |
| 221 | case LYD_ANYDATA_XML: |
| 222 | case LYD_ANYDATA_JSON: |
| 223 | LY_CHECK_ERR_RET(lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), free(any), LY_EMEM); |
| 224 | break; |
| 225 | case LYD_ANYDATA_LYB: |
| 226 | any->value.mem = (void *)value; |
| 227 | break; |
| 228 | } |
| 229 | any->value_type = value_type; |
| 230 | } else { |
| 231 | any_val.str = value; |
| 232 | ret = lyd_any_copy_value(&any->node, &any_val, value_type); |
| 233 | LY_CHECK_ERR_RET(ret, free(any), ret); |
| 234 | } |
| 235 | lyd_hash(&any->node); |
| 236 | |
| 237 | *node = &any->node; |
| 238 | return LY_SUCCESS; |
| 239 | } |
| 240 | |
| 241 | LY_ERR |
| 242 | lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len, |
| 243 | const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic, |
| 244 | LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node) |
| 245 | { |
| 246 | LY_ERR ret = LY_SUCCESS; |
| 247 | struct lyd_node_opaq *opaq; |
| 248 | |
| 249 | assert(ctx && name && name_len && format); |
| 250 | |
| 251 | if (!value_len && (!dynamic || !*dynamic)) { |
| 252 | value = ""; |
| 253 | } |
| 254 | |
| 255 | opaq = calloc(1, sizeof *opaq); |
| 256 | LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish); |
| 257 | |
| 258 | opaq->prev = &opaq->node; |
| 259 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish); |
| 260 | |
| 261 | if (pref_len) { |
| 262 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish); |
| 263 | } |
| 264 | if (module_key_len) { |
| 265 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish); |
| 266 | } |
| 267 | if (dynamic && *dynamic) { |
| 268 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish); |
| 269 | *dynamic = 0; |
| 270 | } else { |
| 271 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish); |
| 272 | } |
| 273 | |
| 274 | opaq->format = format; |
| 275 | opaq->val_prefix_data = val_prefix_data; |
| 276 | opaq->hints = hints; |
| 277 | opaq->ctx = ctx; |
| 278 | |
| 279 | finish: |
| 280 | if (ret) { |
| 281 | lyd_free_tree(&opaq->node); |
| 282 | ly_free_prefix_data(format, val_prefix_data); |
| 283 | } else { |
| 284 | *node = &opaq->node; |
| 285 | } |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | LIBYANG_API_DEF LY_ERR |
| 290 | lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 291 | struct lyd_node **node) |
| 292 | { |
| 293 | LY_ERR r; |
| 294 | struct lyd_node *ret = NULL; |
| 295 | const struct lysc_node *schema; |
| 296 | struct lysc_ext_instance *ext = NULL; |
| 297 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 298 | |
| 299 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 300 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 301 | |
| 302 | if (!module) { |
| 303 | module = parent->schema->module; |
| 304 | } |
| 305 | |
| 306 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, |
| 307 | LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0); |
| 308 | if (!schema && parent) { |
| 309 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 310 | strlen(name), &schema, &ext); |
| 311 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 312 | } |
| 313 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.", |
| 314 | name), LY_ENOTFOUND); |
| 315 | |
| 316 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 317 | if (ext) { |
| 318 | ret->flags |= LYD_EXT; |
| 319 | } |
| 320 | if (parent) { |
| 321 | lyd_insert_node(parent, NULL, ret, 0); |
| 322 | } |
| 323 | |
| 324 | if (node) { |
| 325 | *node = ret; |
| 326 | } |
| 327 | return LY_SUCCESS; |
| 328 | } |
| 329 | |
| 330 | LIBYANG_API_DEF LY_ERR |
| 331 | lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node) |
| 332 | { |
| 333 | struct lyd_node *ret = NULL; |
| 334 | const struct lysc_node *schema; |
| 335 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 336 | |
| 337 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 338 | |
| 339 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0); |
| 340 | if (!schema) { |
| 341 | if (ext->argument) { |
| 342 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.", |
| 343 | name, ext->argument, ext->def->name); |
| 344 | } else { |
| 345 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.", |
| 346 | name, ext->def->name); |
| 347 | } |
| 348 | return LY_ENOTFOUND; |
| 349 | } |
| 350 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 351 | |
| 352 | *node = ret; |
| 353 | |
| 354 | return LY_SUCCESS; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * @brief Create a new list node in the data tree. |
| 359 | * |
| 360 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 361 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 362 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 363 | * @param[in] format Format of key values. |
| 364 | * @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 |
| 365 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 366 | * @param[out] node Optional created node. |
| 367 | * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must |
| 368 | * be followed by the value length. |
| 369 | * @return LY_ERR value. |
| 370 | */ |
| 371 | static LY_ERR |
| 372 | _lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format, |
| 373 | ly_bool output, struct lyd_node **node, va_list ap) |
| 374 | { |
| 375 | struct lyd_node *ret = NULL, *key; |
| 376 | const struct lysc_node *schema, *key_s; |
| 377 | struct lysc_ext_instance *ext = NULL; |
| 378 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 379 | const void *key_val; |
| 380 | uint32_t key_len; |
| 381 | LY_ERR r, rc = LY_SUCCESS; |
| 382 | |
| 383 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 384 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 385 | |
| 386 | if (!module) { |
| 387 | module = parent->schema->module; |
| 388 | } |
| 389 | |
| 390 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
| 391 | if (!schema && parent) { |
| 392 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 393 | strlen(name), &schema, &ext); |
| 394 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 395 | } |
| 396 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
| 397 | |
| 398 | /* create list inner node */ |
| 399 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 400 | |
| 401 | /* create and insert all the keys */ |
| 402 | for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 403 | if (format == LY_VALUE_LYB) { |
| 404 | key_val = va_arg(ap, const void *); |
| 405 | key_len = va_arg(ap, uint32_t); |
| 406 | } else { |
| 407 | key_val = va_arg(ap, const char *); |
| 408 | key_len = key_val ? strlen((char *)key_val) : 0; |
| 409 | } |
| 410 | |
| 411 | rc = lyd_create_term(key_s, key_val, key_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &key); |
| 412 | LY_CHECK_GOTO(rc, cleanup); |
| 413 | lyd_insert_node(ret, NULL, key, 1); |
| 414 | } |
| 415 | |
| 416 | if (ext) { |
| 417 | ret->flags |= LYD_EXT; |
| 418 | } |
| 419 | if (parent) { |
| 420 | lyd_insert_node(parent, NULL, ret, 0); |
| 421 | } |
| 422 | |
| 423 | cleanup: |
| 424 | if (rc) { |
| 425 | lyd_free_tree(ret); |
| 426 | ret = NULL; |
| 427 | } else if (node) { |
| 428 | *node = ret; |
| 429 | } |
| 430 | return rc; |
| 431 | } |
| 432 | |
| 433 | LIBYANG_API_DEF LY_ERR |
| 434 | lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 435 | struct lyd_node **node, ...) |
| 436 | { |
| 437 | LY_ERR rc; |
| 438 | va_list ap; |
| 439 | |
| 440 | va_start(ap, node); |
| 441 | |
| 442 | rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap); |
| 443 | |
| 444 | va_end(ap); |
| 445 | return rc; |
| 446 | } |
| 447 | |
| 448 | LIBYANG_API_DEF LY_ERR |
| 449 | lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 450 | struct lyd_node **node, ...) |
| 451 | { |
| 452 | LY_ERR rc; |
| 453 | va_list ap; |
| 454 | |
| 455 | va_start(ap, node); |
| 456 | |
| 457 | rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap); |
| 458 | |
| 459 | va_end(ap); |
| 460 | return rc; |
| 461 | } |
| 462 | |
| 463 | LIBYANG_API_DEF LY_ERR |
| 464 | lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 465 | struct lyd_node **node, ...) |
| 466 | { |
| 467 | LY_ERR rc; |
| 468 | va_list ap; |
| 469 | |
| 470 | va_start(ap, node); |
| 471 | |
| 472 | rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap); |
| 473 | |
| 474 | va_end(ap); |
| 475 | return rc; |
| 476 | } |
| 477 | |
| 478 | LIBYANG_API_DEF LY_ERR |
| 479 | lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...) |
| 480 | { |
| 481 | struct lyd_node *ret = NULL, *key; |
| 482 | const struct lysc_node *schema, *key_s; |
| 483 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 484 | va_list ap; |
| 485 | const char *key_val; |
| 486 | LY_ERR rc = LY_SUCCESS; |
| 487 | |
| 488 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 489 | |
| 490 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0); |
| 491 | if (!schema) { |
| 492 | if (ext->argument) { |
| 493 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.", |
| 494 | name, ext->argument, ext->def->name); |
| 495 | } else { |
| 496 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 497 | } |
| 498 | return LY_ENOTFOUND; |
| 499 | } |
| 500 | /* create list inner node */ |
| 501 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 502 | |
| 503 | va_start(ap, node); |
| 504 | |
| 505 | /* create and insert all the keys */ |
| 506 | for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 507 | key_val = va_arg(ap, const char *); |
| 508 | |
| 509 | rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, |
| 510 | NULL, &key); |
| 511 | LY_CHECK_GOTO(rc, cleanup); |
| 512 | lyd_insert_node(ret, NULL, key, 1); |
| 513 | } |
| 514 | |
| 515 | cleanup: |
| 516 | va_end(ap); |
| 517 | if (rc) { |
| 518 | lyd_free_tree(ret); |
| 519 | ret = NULL; |
| 520 | } |
| 521 | *node = ret; |
| 522 | return rc; |
| 523 | } |
| 524 | |
| 525 | LIBYANG_API_DEF LY_ERR |
| 526 | lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys, |
| 527 | ly_bool output, struct lyd_node **node) |
| 528 | { |
| 529 | LY_ERR r; |
| 530 | struct lyd_node *ret = NULL; |
| 531 | const struct lysc_node *schema; |
| 532 | struct lysc_ext_instance *ext = NULL; |
| 533 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 534 | |
| 535 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 536 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 537 | |
| 538 | if (!module) { |
| 539 | module = parent->schema->module; |
| 540 | } |
| 541 | if (!keys) { |
| 542 | keys = ""; |
| 543 | } |
| 544 | |
| 545 | /* find schema node */ |
| 546 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
| 547 | if (!schema && parent) { |
| 548 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name), |
| 549 | &schema, &ext); |
| 550 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 551 | } |
| 552 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
| 553 | |
| 554 | if ((schema->flags & LYS_KEYLESS) && !keys[0]) { |
| 555 | /* key-less list */ |
| 556 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 557 | } else { |
| 558 | /* create the list node */ |
| 559 | LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret)); |
| 560 | } |
| 561 | if (ext) { |
| 562 | ret->flags |= LYD_EXT; |
| 563 | } |
| 564 | if (parent) { |
| 565 | lyd_insert_node(parent, NULL, ret, 0); |
| 566 | } |
| 567 | |
| 568 | if (node) { |
| 569 | *node = ret; |
| 570 | } |
| 571 | return LY_SUCCESS; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * @brief Create a new term node in the data tree. |
| 576 | * |
| 577 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 578 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 579 | * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST. |
| 580 | * @param[in] value Value of the node being created. |
| 581 | * @param[in] value_len Length of @p value. |
| 582 | * @param[in] format Format of @p value. |
| 583 | * @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 |
| 584 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 585 | * @param[out] node Optional created node. |
| 586 | * @return LY_ERR value. |
| 587 | */ |
| 588 | static LY_ERR |
| 589 | _lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 590 | size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node) |
| 591 | { |
| 592 | LY_ERR r; |
| 593 | struct lyd_node *ret = NULL; |
| 594 | const struct lysc_node *schema; |
| 595 | struct lysc_ext_instance *ext = NULL; |
| 596 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 597 | |
| 598 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 599 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 600 | |
| 601 | if (!module) { |
| 602 | module = parent->schema->module; |
| 603 | } |
| 604 | |
| 605 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0); |
| 606 | if (!schema && parent) { |
| 607 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 608 | strlen(name), &schema, &ext); |
| 609 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 610 | } |
| 611 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND); |
| 612 | |
| 613 | LY_CHECK_RET(lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret)); |
| 614 | if (ext) { |
| 615 | ret->flags |= LYD_EXT; |
| 616 | } |
| 617 | if (parent) { |
| 618 | lyd_insert_node(parent, NULL, ret, 0); |
| 619 | } |
| 620 | |
| 621 | if (node) { |
| 622 | *node = ret; |
| 623 | } |
| 624 | return LY_SUCCESS; |
| 625 | } |
| 626 | |
| 627 | LIBYANG_API_DEF LY_ERR |
| 628 | lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 629 | ly_bool output, struct lyd_node **node) |
| 630 | { |
| 631 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node); |
| 632 | } |
| 633 | |
| 634 | LIBYANG_API_DEF LY_ERR |
| 635 | lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 636 | size_t value_len, ly_bool output, struct lyd_node **node) |
| 637 | { |
| 638 | return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node); |
| 639 | } |
| 640 | |
| 641 | LIBYANG_API_DEF LY_ERR |
| 642 | lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 643 | ly_bool output, struct lyd_node **node) |
| 644 | { |
| 645 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node); |
| 646 | } |
| 647 | |
| 648 | LIBYANG_API_DEF LY_ERR |
| 649 | lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node) |
| 650 | { |
| 651 | LY_ERR rc; |
| 652 | struct lyd_node *ret = NULL; |
| 653 | const struct lysc_node *schema; |
| 654 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 655 | |
| 656 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 657 | |
| 658 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0); |
| 659 | if (!schema) { |
| 660 | if (ext->argument) { |
| 661 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.", |
| 662 | name, ext->argument, ext->def->name); |
| 663 | } else { |
| 664 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 665 | } |
| 666 | return LY_ENOTFOUND; |
| 667 | } |
| 668 | rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret); |
| 669 | LY_CHECK_RET(rc); |
| 670 | |
| 671 | *node = ret; |
| 672 | |
| 673 | return LY_SUCCESS; |
| 674 | } |
| 675 | |
| 676 | LIBYANG_API_DEF LY_ERR |
| 677 | lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 678 | ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node) |
| 679 | { |
| 680 | LY_ERR r; |
| 681 | struct lyd_node *ret = NULL; |
| 682 | const struct lysc_node *schema; |
| 683 | struct lysc_ext_instance *ext = NULL; |
| 684 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
| 685 | |
| 686 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
| 687 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 688 | |
| 689 | if (!module) { |
| 690 | module = parent->schema->module; |
| 691 | } |
| 692 | |
| 693 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0); |
| 694 | if (!schema && parent) { |
| 695 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 696 | strlen(name), &schema, &ext); |
| 697 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 698 | } |
| 699 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND); |
| 700 | |
| 701 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
| 702 | if (ext) { |
| 703 | ret->flags |= LYD_EXT; |
| 704 | } |
| 705 | if (parent) { |
| 706 | lyd_insert_node(parent, NULL, ret, 0); |
| 707 | } |
| 708 | |
| 709 | if (node) { |
| 710 | *node = ret; |
| 711 | } |
| 712 | return LY_SUCCESS; |
| 713 | } |
| 714 | |
| 715 | LIBYANG_API_DEF LY_ERR |
| 716 | lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value, |
| 717 | LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node) |
| 718 | { |
| 719 | struct lyd_node *ret = NULL; |
| 720 | const struct lysc_node *schema; |
| 721 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 722 | |
| 723 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 724 | |
| 725 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0); |
| 726 | if (!schema) { |
| 727 | if (ext->argument) { |
| 728 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.", |
| 729 | name, ext->argument, ext->def->name); |
| 730 | } else { |
| 731 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 732 | } |
| 733 | return LY_ENOTFOUND; |
| 734 | } |
| 735 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
| 736 | |
| 737 | *node = ret; |
| 738 | |
| 739 | return LY_SUCCESS; |
| 740 | } |
| 741 | |
| 742 | LIBYANG_API_DEF LY_ERR |
| 743 | lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 744 | const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta) |
| 745 | { |
| 746 | const char *prefix, *tmp; |
| 747 | size_t pref_len, name_len; |
| 748 | |
| 749 | LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL); |
| 750 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 751 | if (!ctx) { |
Michal Vasko | 33c4897 | 2022-07-20 10:28:07 +0200 | [diff] [blame] | 752 | ctx = module ? module->ctx : LYD_CTX(parent); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | if (parent && !parent->schema) { |
| 756 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent)); |
| 757 | return LY_EINVAL; |
| 758 | } |
| 759 | if (meta) { |
| 760 | *meta = NULL; |
| 761 | } |
| 762 | |
| 763 | /* parse the name */ |
| 764 | tmp = name; |
| 765 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 766 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 767 | return LY_EINVAL; |
| 768 | } |
| 769 | |
| 770 | /* find the module */ |
| 771 | if (prefix) { |
| 772 | module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len); |
| 773 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND); |
| 774 | } |
| 775 | |
| 776 | /* set value if none */ |
| 777 | if (!val_str) { |
| 778 | val_str = ""; |
| 779 | } |
| 780 | |
| 781 | return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON, |
| 782 | NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL); |
| 783 | } |
| 784 | |
| 785 | LIBYANG_API_DEF LY_ERR |
| 786 | lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr, |
| 787 | struct lyd_meta **meta) |
| 788 | { |
| 789 | const struct lys_module *mod; |
| 790 | |
| 791 | LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL); |
| 792 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
| 793 | |
| 794 | if (parent && !parent->schema) { |
| 795 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name); |
| 796 | return LY_EINVAL; |
| 797 | } |
| 798 | if (meta) { |
| 799 | *meta = NULL; |
| 800 | } |
| 801 | |
| 802 | switch (attr->format) { |
| 803 | case LY_VALUE_XML: |
| 804 | mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns); |
| 805 | if (!mod) { |
| 806 | LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns); |
| 807 | return LY_ENOTFOUND; |
| 808 | } |
| 809 | break; |
| 810 | case LY_VALUE_JSON: |
| 811 | mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name); |
| 812 | if (!mod) { |
| 813 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name); |
| 814 | return LY_ENOTFOUND; |
| 815 | } |
| 816 | break; |
| 817 | default: |
| 818 | LOGINT_RET(ctx); |
| 819 | } |
| 820 | |
| 821 | return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value), |
| 822 | NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL); |
| 823 | } |
| 824 | |
| 825 | LIBYANG_API_DEF LY_ERR |
| 826 | lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
| 827 | const char *prefix, const char *module_name, struct lyd_node **node) |
| 828 | { |
| 829 | struct lyd_node *ret = NULL; |
| 830 | |
| 831 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL); |
| 832 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
| 833 | |
| 834 | if (!ctx) { |
| 835 | ctx = LYD_CTX(parent); |
| 836 | } |
| 837 | if (!value) { |
| 838 | value = ""; |
| 839 | } |
| 840 | |
| 841 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name, |
| 842 | strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret)); |
| 843 | if (parent) { |
| 844 | lyd_insert_node(parent, NULL, ret, 1); |
| 845 | } |
| 846 | |
| 847 | if (node) { |
| 848 | *node = ret; |
| 849 | } |
| 850 | return LY_SUCCESS; |
| 851 | } |
| 852 | |
| 853 | LIBYANG_API_DEF LY_ERR |
| 854 | lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
| 855 | const char *prefix, const char *module_ns, struct lyd_node **node) |
| 856 | { |
| 857 | struct lyd_node *ret = NULL; |
| 858 | |
| 859 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL); |
| 860 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
| 861 | |
| 862 | if (!ctx) { |
| 863 | ctx = LYD_CTX(parent); |
| 864 | } |
| 865 | if (!value) { |
| 866 | value = ""; |
| 867 | } |
| 868 | |
| 869 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns, |
| 870 | strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret)); |
| 871 | if (parent) { |
| 872 | lyd_insert_node(parent, NULL, ret, 1); |
| 873 | } |
| 874 | |
| 875 | if (node) { |
| 876 | *node = ret; |
| 877 | } |
| 878 | return LY_SUCCESS; |
| 879 | } |
| 880 | |
| 881 | LIBYANG_API_DEF LY_ERR |
| 882 | lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value, |
| 883 | struct lyd_attr **attr) |
| 884 | { |
| 885 | struct lyd_attr *ret = NULL; |
| 886 | const struct ly_ctx *ctx; |
| 887 | const char *prefix, *tmp; |
| 888 | size_t pref_len, name_len, mod_len; |
| 889 | |
| 890 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
| 891 | |
| 892 | ctx = LYD_CTX(parent); |
| 893 | |
| 894 | /* parse the name */ |
| 895 | tmp = name; |
| 896 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 897 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
| 898 | return LY_EVALID; |
| 899 | } |
| 900 | |
| 901 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 902 | /* not a prefix but special name */ |
| 903 | name = prefix; |
| 904 | name_len += 1 + pref_len; |
| 905 | prefix = NULL; |
| 906 | pref_len = 0; |
| 907 | } |
| 908 | |
| 909 | /* get the module */ |
| 910 | if (module_name) { |
| 911 | mod_len = strlen(module_name); |
| 912 | } else { |
| 913 | module_name = prefix; |
| 914 | mod_len = pref_len; |
| 915 | } |
| 916 | |
| 917 | /* set value if none */ |
| 918 | if (!value) { |
| 919 | value = ""; |
| 920 | } |
| 921 | |
| 922 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value, |
| 923 | strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA)); |
| 924 | |
| 925 | if (attr) { |
| 926 | *attr = ret; |
| 927 | } |
| 928 | return LY_SUCCESS; |
| 929 | } |
| 930 | |
| 931 | LIBYANG_API_DEF LY_ERR |
| 932 | lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value, |
| 933 | struct lyd_attr **attr) |
| 934 | { |
| 935 | struct lyd_attr *ret = NULL; |
| 936 | const struct ly_ctx *ctx; |
| 937 | const char *prefix, *tmp; |
| 938 | size_t pref_len, name_len; |
| 939 | |
| 940 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
| 941 | |
| 942 | ctx = LYD_CTX(parent); |
| 943 | |
| 944 | /* parse the name */ |
| 945 | tmp = name; |
| 946 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 947 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
| 948 | return LY_EVALID; |
| 949 | } |
| 950 | |
| 951 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 952 | /* not a prefix but special name */ |
| 953 | name = prefix; |
| 954 | name_len += 1 + pref_len; |
| 955 | prefix = NULL; |
| 956 | pref_len = 0; |
| 957 | } |
| 958 | |
| 959 | /* set value if none */ |
| 960 | if (!value) { |
| 961 | value = ""; |
| 962 | } |
| 963 | |
| 964 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns, |
| 965 | module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA)); |
| 966 | |
| 967 | if (attr) { |
| 968 | *attr = ret; |
| 969 | } |
| 970 | return LY_SUCCESS; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * @brief Change the value of a term (leaf or leaf-list) node. |
| 975 | * |
| 976 | * Node changed this way is always considered explicitly set, meaning its default flag |
| 977 | * is always cleared. |
| 978 | * |
| 979 | * @param[in] term Term node to change. |
| 980 | * @param[in] value New value to set. |
| 981 | * @param[in] value_len Length of @p value. |
| 982 | * @param[in] format Format of @p value. |
| 983 | * @return LY_SUCCESS if value was changed, |
| 984 | * @return LY_EEXIST if value was the same and only the default flag was cleared, |
| 985 | * @return LY_ENOT if the values were equal and no change occured, |
| 986 | * @return LY_ERR value on other errors. |
| 987 | */ |
| 988 | static LY_ERR |
| 989 | _lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format) |
| 990 | { |
| 991 | LY_ERR ret = LY_SUCCESS; |
| 992 | struct lysc_type *type; |
| 993 | struct lyd_node_term *t; |
| 994 | struct lyd_node *parent; |
| 995 | struct lyd_value val; |
| 996 | ly_bool dflt_change, val_change; |
| 997 | |
| 998 | assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM)); |
| 999 | |
| 1000 | t = (struct lyd_node_term *)term; |
| 1001 | type = ((struct lysc_node_leaf *)term->schema)->type; |
| 1002 | |
| 1003 | /* parse the new value */ |
| 1004 | LOG_LOCSET(term->schema, term, NULL, NULL); |
| 1005 | ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, NULL, format, NULL, LYD_HINT_DATA, term->schema, NULL); |
| 1006 | LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0); |
| 1007 | LY_CHECK_GOTO(ret, cleanup); |
| 1008 | |
| 1009 | /* compare original and new value */ |
| 1010 | if (type->plugin->compare(&t->value, &val)) { |
| 1011 | /* values differ, switch them */ |
| 1012 | type->plugin->free(LYD_CTX(term), &t->value); |
| 1013 | t->value = val; |
| 1014 | val_change = 1; |
| 1015 | } else { |
| 1016 | /* same values, free the new stored one */ |
| 1017 | type->plugin->free(LYD_CTX(term), &val); |
| 1018 | val_change = 0; |
| 1019 | } |
| 1020 | |
| 1021 | /* always clear the default flag */ |
| 1022 | if (term->flags & LYD_DEFAULT) { |
| 1023 | for (parent = term; parent; parent = lyd_parent(parent)) { |
| 1024 | parent->flags &= ~LYD_DEFAULT; |
| 1025 | } |
| 1026 | dflt_change = 1; |
| 1027 | } else { |
| 1028 | dflt_change = 0; |
| 1029 | } |
| 1030 | |
| 1031 | if (val_change || dflt_change) { |
| 1032 | /* make the node non-validated */ |
| 1033 | term->flags &= LYD_NEW; |
| 1034 | } |
| 1035 | |
| 1036 | if (val_change) { |
| 1037 | if (term->schema->nodetype == LYS_LEAFLIST) { |
| 1038 | /* leaf-list needs to be hashed again and re-inserted into parent */ |
| 1039 | lyd_unlink_hash(term); |
| 1040 | lyd_hash(term); |
| 1041 | LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup); |
| 1042 | } else if ((term->schema->flags & LYS_KEY) && term->parent) { |
| 1043 | /* list needs to be updated if its key was changed */ |
| 1044 | assert(term->parent->schema->nodetype == LYS_LIST); |
| 1045 | lyd_unlink_hash(lyd_parent(term)); |
| 1046 | lyd_hash(lyd_parent(term)); |
| 1047 | LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup); |
| 1048 | } /* else leaf that is not a key, its value is not used for its hash so it does not change */ |
| 1049 | } |
| 1050 | |
| 1051 | /* retrun value */ |
| 1052 | if (!val_change) { |
| 1053 | if (dflt_change) { |
| 1054 | /* only default flag change */ |
| 1055 | ret = LY_EEXIST; |
| 1056 | } else { |
| 1057 | /* no change */ |
| 1058 | ret = LY_ENOT; |
| 1059 | } |
| 1060 | } /* else value changed, LY_SUCCESS */ |
| 1061 | |
| 1062 | cleanup: |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | LIBYANG_API_DEF LY_ERR |
| 1067 | lyd_change_term(struct lyd_node *term, const char *val_str) |
| 1068 | { |
| 1069 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1070 | |
| 1071 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON); |
| 1072 | } |
| 1073 | |
| 1074 | LIBYANG_API_DEF LY_ERR |
| 1075 | lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len) |
| 1076 | { |
| 1077 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1078 | |
| 1079 | return _lyd_change_term(term, value, value_len, LY_VALUE_LYB); |
| 1080 | } |
| 1081 | |
| 1082 | LIBYANG_API_DEF LY_ERR |
| 1083 | lyd_change_term_canon(struct lyd_node *term, const char *val_str) |
| 1084 | { |
| 1085 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1086 | |
| 1087 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON); |
| 1088 | } |
| 1089 | |
| 1090 | LIBYANG_API_DEF LY_ERR |
| 1091 | lyd_change_meta(struct lyd_meta *meta, const char *val_str) |
| 1092 | { |
| 1093 | LY_ERR ret = LY_SUCCESS; |
| 1094 | struct lyd_meta *m2 = NULL; |
| 1095 | struct lyd_value val; |
| 1096 | ly_bool val_change; |
| 1097 | |
| 1098 | LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL); |
| 1099 | |
| 1100 | if (!val_str) { |
| 1101 | val_str = ""; |
| 1102 | } |
| 1103 | |
| 1104 | /* parse the new value into a new meta structure */ |
| 1105 | ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str), |
| 1106 | NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL); |
| 1107 | LY_CHECK_GOTO(ret, cleanup); |
| 1108 | |
| 1109 | /* compare original and new value */ |
| 1110 | if (lyd_compare_meta(meta, m2)) { |
| 1111 | /* values differ, switch them */ |
| 1112 | val = meta->value; |
| 1113 | meta->value = m2->value; |
| 1114 | m2->value = val; |
| 1115 | val_change = 1; |
| 1116 | } else { |
| 1117 | val_change = 0; |
| 1118 | } |
| 1119 | |
| 1120 | /* retrun value */ |
| 1121 | if (!val_change) { |
| 1122 | /* no change */ |
| 1123 | ret = LY_ENOT; |
| 1124 | } /* else value changed, LY_SUCCESS */ |
| 1125 | |
| 1126 | cleanup: |
| 1127 | lyd_free_meta_single(m2); |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * @brief Update node value. |
| 1133 | * |
| 1134 | * @param[in] node Node to update. |
| 1135 | * @param[in] value New value to set. |
| 1136 | * @param[in] value_len Length of @p value. |
| 1137 | * @param[in] value_type Type of @p value for anydata/anyxml node. |
| 1138 | * @param[in] format Format of @p value. |
| 1139 | * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL. |
| 1140 | * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL. |
| 1141 | * @return LY_ERR value. |
| 1142 | */ |
| 1143 | static LY_ERR |
| 1144 | lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, |
| 1145 | LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node) |
| 1146 | { |
| 1147 | LY_ERR ret = LY_SUCCESS; |
| 1148 | struct lyd_node *new_any; |
| 1149 | |
| 1150 | switch (node->schema->nodetype) { |
| 1151 | case LYS_CONTAINER: |
| 1152 | case LYS_NOTIF: |
| 1153 | case LYS_RPC: |
| 1154 | case LYS_ACTION: |
| 1155 | case LYS_LIST: |
| 1156 | /* if it exists, there is nothing to update */ |
| 1157 | *new_parent = NULL; |
| 1158 | *new_node = NULL; |
| 1159 | break; |
| 1160 | case LYS_LEAFLIST: |
| 1161 | if (!lysc_is_dup_inst_list(node->schema)) { |
| 1162 | /* if it exists, there is nothing to update */ |
| 1163 | *new_parent = NULL; |
| 1164 | *new_node = NULL; |
| 1165 | break; |
| 1166 | } |
| 1167 | /* fallthrough */ |
| 1168 | case LYS_LEAF: |
| 1169 | ret = _lyd_change_term(node, value, value_len, format); |
| 1170 | if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) { |
| 1171 | /* there was an actual change (at least of the default flag) */ |
| 1172 | *new_parent = node; |
| 1173 | *new_node = node; |
| 1174 | ret = LY_SUCCESS; |
| 1175 | } else if (ret == LY_ENOT) { |
| 1176 | /* no change */ |
| 1177 | *new_parent = NULL; |
| 1178 | *new_node = NULL; |
| 1179 | ret = LY_SUCCESS; |
| 1180 | } /* else error */ |
| 1181 | break; |
| 1182 | case LYS_ANYDATA: |
| 1183 | case LYS_ANYXML: |
| 1184 | /* create a new any node */ |
| 1185 | LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any)); |
| 1186 | |
| 1187 | /* compare with the existing one */ |
| 1188 | if (lyd_compare_single(node, new_any, 0)) { |
| 1189 | /* not equal, switch values (so that we can use generic node free) */ |
| 1190 | ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value; |
| 1191 | ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type; |
| 1192 | ((struct lyd_node_any *)node)->value.str = value; |
| 1193 | ((struct lyd_node_any *)node)->value_type = value_type; |
| 1194 | |
| 1195 | *new_parent = node; |
| 1196 | *new_node = node; |
| 1197 | } else { |
| 1198 | /* they are equal */ |
| 1199 | *new_parent = NULL; |
| 1200 | *new_node = NULL; |
| 1201 | } |
| 1202 | lyd_free_tree(new_any); |
| 1203 | break; |
| 1204 | default: |
| 1205 | LOGINT(LYD_CTX(node)); |
| 1206 | ret = LY_EINT; |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static LY_ERR |
| 1214 | lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len, |
| 1215 | LY_VALUE_FORMAT format, uint32_t options) |
| 1216 | { |
| 1217 | LY_ERR r; |
| 1218 | struct ly_path_predicate *pred; |
| 1219 | struct lyd_value val; |
| 1220 | const struct lysc_node *schema = NULL; |
| 1221 | LY_ARRAY_COUNT_TYPE u, new_count; |
| 1222 | int create = 0; |
| 1223 | |
| 1224 | assert(path); |
| 1225 | |
| 1226 | /* go through all the compiled nodes */ |
| 1227 | LY_ARRAY_FOR(path, u) { |
| 1228 | schema = path[u].node; |
| 1229 | |
| 1230 | if (lysc_is_dup_inst_list(schema)) { |
| 1231 | if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) { |
| 1232 | /* creating a new key-less list or state leaf-list instance */ |
| 1233 | create = 1; |
| 1234 | new_count = u; |
| 1235 | } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) { |
| 1236 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 1237 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".", |
| 1238 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1239 | LOG_LOCBACK(1, 0, 0, 0); |
| 1240 | return LY_EINVAL; |
| 1241 | } |
| 1242 | } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) { |
| 1243 | if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) { |
| 1244 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 1245 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".", |
| 1246 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1247 | LOG_LOCBACK(1, 0, 0, 0); |
| 1248 | return LY_EINVAL; |
| 1249 | } /* else creating an opaque list */ |
| 1250 | } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) { |
| 1251 | r = LY_SUCCESS; |
| 1252 | if (options & LYD_NEW_PATH_OPAQ) { |
| 1253 | r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL); |
| 1254 | } |
| 1255 | if (!r) { |
| 1256 | /* try to store the value */ |
| 1257 | LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type, |
| 1258 | value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL)); |
| 1259 | ++((struct lysc_type *)val.realtype)->refcount; |
| 1260 | |
| 1261 | /* store the new predicate so that it is used when searching for this instance */ |
| 1262 | path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST; |
| 1263 | LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM); |
| 1264 | pred->value = val; |
| 1265 | } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */ |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | if (create) { |
| 1270 | /* hide the nodes that should always be created so they are not found */ |
| 1271 | while (new_count < LY_ARRAY_COUNT(path)) { |
| 1272 | LY_ARRAY_DECREMENT(path); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | return LY_SUCCESS; |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * @brief Create a new node in the data tree based on a path. All node types can be created. |
| 1281 | * |
| 1282 | * If @p path points to a list key, the key value from the predicate is used and @p value is ignored. |
| 1283 | * Also, if a leaf-list is being created and both a predicate is defined in @p path |
| 1284 | * and @p value is set, the predicate is preferred. |
| 1285 | * |
| 1286 | * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these |
| 1287 | * nodes, they are always created. |
| 1288 | * |
| 1289 | * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used, |
| 1290 | * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted |
| 1291 | * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases. |
| 1292 | * @param[in] ctx libyang context, must be set if @p parent is NULL. |
| 1293 | * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute |
| 1294 | * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces |
| 1295 | * searching for the appropriate module. |
| 1296 | * @param[in] path [Path](@ref howtoXPath) to create. |
| 1297 | * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an |
| 1298 | * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL. |
| 1299 | * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when |
| 1300 | * creating anyxml/anydata nodes. |
| 1301 | * @param[in] value_type Anyxml/anydata node @p value type. |
| 1302 | * @param[in] options Bitmask of options, see @ref pathoptions. |
| 1303 | * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node. |
| 1304 | * @param[out] new_node Optional last node created. |
| 1305 | * @return LY_ERR value. |
| 1306 | */ |
| 1307 | static LY_ERR |
| 1308 | lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path, |
| 1309 | const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, |
| 1310 | struct lyd_node **new_parent, struct lyd_node **new_node) |
| 1311 | { |
| 1312 | LY_ERR ret = LY_SUCCESS, r; |
| 1313 | struct lyxp_expr *exp = NULL; |
| 1314 | struct ly_path *p = NULL; |
| 1315 | struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent; |
| 1316 | const struct lysc_node *schema; |
| 1317 | const struct lyd_value *val = NULL; |
| 1318 | LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0; |
| 1319 | LY_VALUE_FORMAT format; |
| 1320 | |
| 1321 | assert(parent || ctx); |
| 1322 | assert(path && ((path[0] == '/') || parent)); |
| 1323 | assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE)); |
| 1324 | |
| 1325 | if (!ctx) { |
| 1326 | ctx = LYD_CTX(parent); |
| 1327 | } |
| 1328 | if (value && !value_len) { |
| 1329 | value_len = strlen(value); |
| 1330 | } |
| 1331 | if (options & LYD_NEW_PATH_BIN_VALUE) { |
| 1332 | format = LY_VALUE_LYB; |
| 1333 | } else if (options & LYD_NEW_PATH_CANON_VALUE) { |
| 1334 | format = LY_VALUE_CANON; |
| 1335 | } else { |
| 1336 | format = LY_VALUE_JSON; |
| 1337 | } |
| 1338 | |
| 1339 | /* parse path */ |
| 1340 | LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL, |
| 1341 | LY_PATH_PRED_SIMPLE, &exp), cleanup); |
| 1342 | |
| 1343 | /* compile path */ |
| 1344 | LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ? |
| 1345 | LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup); |
| 1346 | |
| 1347 | /* check the compiled path before searching existing nodes, it may be shortened */ |
| 1348 | orig_count = LY_ARRAY_COUNT(p); |
| 1349 | LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup); |
| 1350 | |
| 1351 | /* try to find any existing nodes in the path */ |
| 1352 | if (parent) { |
| 1353 | ret = ly_path_eval_partial(p, parent, &path_idx, &node); |
| 1354 | if (ret == LY_SUCCESS) { |
| 1355 | if (orig_count == LY_ARRAY_COUNT(p)) { |
| 1356 | /* the node exists, are we supposed to update it or is it just a default? */ |
| 1357 | if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) { |
| 1358 | LOG_LOCSET(NULL, node, NULL, NULL); |
| 1359 | LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path); |
| 1360 | LOG_LOCBACK(0, 1, 0, 0); |
| 1361 | ret = LY_EEXIST; |
| 1362 | goto cleanup; |
| 1363 | } |
| 1364 | |
| 1365 | /* update the existing node */ |
| 1366 | ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode); |
| 1367 | goto cleanup; |
| 1368 | } /* else we were not searching for the whole path */ |
| 1369 | } else if (ret == LY_EINCOMPLETE) { |
| 1370 | /* some nodes were found, adjust the iterator to the next segment */ |
| 1371 | ++path_idx; |
| 1372 | } else if (ret == LY_ENOTFOUND) { |
| 1373 | /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */ |
| 1374 | if (lysc_data_parent(p[0].node)) { |
| 1375 | node = parent; |
| 1376 | } |
| 1377 | } else { |
| 1378 | /* error */ |
| 1379 | goto cleanup; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | /* restore the full path for creating new nodes */ |
| 1384 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 1385 | LY_ARRAY_INCREMENT(p); |
| 1386 | } |
| 1387 | |
| 1388 | /* create all the non-existing nodes in a loop */ |
| 1389 | for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) { |
| 1390 | cur_parent = node; |
| 1391 | schema = p[path_idx].node; |
| 1392 | |
| 1393 | switch (schema->nodetype) { |
| 1394 | case LYS_LIST: |
| 1395 | if (lysc_is_dup_inst_list(schema)) { |
| 1396 | /* create key-less list instance */ |
| 1397 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1398 | } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) { |
| 1399 | /* creating opaque list without keys */ |
| 1400 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, |
| 1401 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL, |
| 1402 | LYD_NODEHINT_LIST, &node), cleanup); |
| 1403 | } else { |
| 1404 | /* create standard list instance */ |
| 1405 | assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST); |
| 1406 | LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup); |
| 1407 | } |
| 1408 | break; |
| 1409 | case LYS_CONTAINER: |
| 1410 | case LYS_NOTIF: |
| 1411 | case LYS_RPC: |
| 1412 | case LYS_ACTION: |
| 1413 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1414 | break; |
| 1415 | case LYS_LEAFLIST: |
| 1416 | if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) { |
| 1417 | /* we have not checked this only for dup-inst lists, otherwise it must be opaque */ |
| 1418 | r = LY_EVALID; |
| 1419 | if (lysc_is_dup_inst_list(schema)) { |
| 1420 | /* validate value */ |
| 1421 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
| 1422 | } |
| 1423 | if (r && (r != LY_EINCOMPLETE)) { |
| 1424 | /* creating opaque leaf-list */ |
| 1425 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 1426 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, |
| 1427 | LYD_NODEHINT_LEAFLIST, &node), cleanup); |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | /* get value to set */ |
| 1433 | if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) { |
| 1434 | val = &p[path_idx].predicates[0].value; |
| 1435 | } |
| 1436 | |
| 1437 | /* create a leaf-list instance */ |
| 1438 | if (val) { |
| 1439 | LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup); |
| 1440 | } else { |
| 1441 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, |
| 1442 | NULL, &node), cleanup); |
| 1443 | } |
| 1444 | break; |
| 1445 | case LYS_LEAF: |
| 1446 | if (lysc_is_key(schema) && cur_parent->schema) { |
| 1447 | /* it must have been already created or some error will occur later */ |
| 1448 | lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node); |
| 1449 | assert(node); |
| 1450 | goto next_iter; |
| 1451 | } |
| 1452 | |
| 1453 | if (options & LYD_NEW_PATH_OPAQ) { |
| 1454 | if (cur_parent && !cur_parent->schema) { |
| 1455 | /* always create opaque nodes for opaque parents */ |
| 1456 | r = LY_ENOT; |
| 1457 | } else { |
| 1458 | /* validate value */ |
| 1459 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
| 1460 | } |
| 1461 | if (r && (r != LY_EINCOMPLETE)) { |
| 1462 | /* creating opaque leaf */ |
| 1463 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 1464 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node), |
| 1465 | cleanup); |
| 1466 | break; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /* create a leaf instance */ |
| 1471 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, |
| 1472 | &node), cleanup); |
| 1473 | break; |
| 1474 | case LYS_ANYDATA: |
| 1475 | case LYS_ANYXML: |
| 1476 | LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup); |
| 1477 | break; |
| 1478 | default: |
| 1479 | LOGINT(ctx); |
| 1480 | ret = LY_EINT; |
| 1481 | goto cleanup; |
| 1482 | } |
| 1483 | |
| 1484 | if (p[path_idx].ext) { |
| 1485 | node->flags |= LYD_EXT; |
| 1486 | } |
| 1487 | if (cur_parent) { |
| 1488 | /* connect to the parent */ |
| 1489 | lyd_insert_node(cur_parent, NULL, node, 0); |
| 1490 | } else if (parent) { |
| 1491 | /* connect to top-level siblings */ |
| 1492 | lyd_insert_node(NULL, &parent, node, 0); |
| 1493 | } |
| 1494 | |
| 1495 | next_iter: |
| 1496 | /* update remembered nodes */ |
| 1497 | if (!nparent) { |
| 1498 | nparent = node; |
| 1499 | } |
| 1500 | nnode = node; |
| 1501 | } |
| 1502 | |
| 1503 | cleanup: |
| 1504 | lyxp_expr_free(ctx, exp); |
| 1505 | if (p) { |
| 1506 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 1507 | LY_ARRAY_INCREMENT(p); |
| 1508 | } |
| 1509 | } |
| 1510 | ly_path_free(ctx, p); |
| 1511 | if (!ret) { |
| 1512 | /* set out params only on success */ |
| 1513 | if (new_parent) { |
| 1514 | *new_parent = nparent; |
| 1515 | } |
| 1516 | if (new_node) { |
| 1517 | *new_node = nnode; |
| 1518 | } |
| 1519 | } else { |
| 1520 | lyd_free_tree(nparent); |
| 1521 | } |
| 1522 | return ret; |
| 1523 | } |
| 1524 | |
| 1525 | LIBYANG_API_DEF LY_ERR |
| 1526 | lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options, |
| 1527 | struct lyd_node **node) |
| 1528 | { |
| 1529 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 1530 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
| 1531 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 1532 | |
| 1533 | return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
| 1534 | } |
| 1535 | |
| 1536 | LIBYANG_API_DEF LY_ERR |
| 1537 | lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value, |
| 1538 | size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, |
| 1539 | struct lyd_node **new_node) |
| 1540 | { |
| 1541 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 1542 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
| 1543 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 1544 | |
| 1545 | return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node); |
| 1546 | } |
| 1547 | |
| 1548 | LIBYANG_API_DEF LY_ERR |
| 1549 | lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value, |
| 1550 | uint32_t options, struct lyd_node **node) |
| 1551 | { |
| 1552 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1553 | |
| 1554 | LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent, |
| 1555 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
| 1556 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 1557 | |
| 1558 | return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
| 1559 | } |
| 1560 | |
| 1561 | LY_ERR |
| 1562 | lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent, |
| 1563 | const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts, |
| 1564 | struct lyd_node **diff) |
| 1565 | { |
| 1566 | LY_ERR ret; |
| 1567 | const struct lysc_node *iter = NULL; |
| 1568 | struct lyd_node *node = NULL; |
| 1569 | struct lyd_value **dflts; |
| 1570 | LY_ARRAY_COUNT_TYPE u; |
| 1571 | uint32_t getnext_opts; |
| 1572 | |
| 1573 | assert(first && (parent || sparent || mod)); |
| 1574 | |
| 1575 | if (!sparent && parent) { |
| 1576 | sparent = parent->schema; |
| 1577 | } |
| 1578 | |
| 1579 | getnext_opts = LYS_GETNEXT_WITHCHOICE; |
| 1580 | if (impl_opts & LYD_IMPLICIT_OUTPUT) { |
| 1581 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 1582 | } |
| 1583 | |
| 1584 | while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) { |
| 1585 | if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) { |
| 1586 | continue; |
| 1587 | } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) { |
| 1588 | continue; |
| 1589 | } |
| 1590 | |
| 1591 | switch (iter->nodetype) { |
| 1592 | case LYS_CHOICE: |
| 1593 | node = lys_getnext_data(NULL, *first, NULL, iter, NULL); |
| 1594 | if (!node && ((struct lysc_node_choice *)iter)->dflt) { |
| 1595 | /* create default case data */ |
| 1596 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node, |
| 1597 | NULL, node_when, node_types, impl_opts, diff)); |
| 1598 | } else if (node) { |
| 1599 | /* create any default data in the existing case */ |
| 1600 | assert(node->schema->parent->nodetype == LYS_CASE); |
| 1601 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types, |
| 1602 | impl_opts, diff)); |
| 1603 | } |
| 1604 | break; |
| 1605 | case LYS_CONTAINER: |
| 1606 | if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1607 | /* create default NP container */ |
| 1608 | LY_CHECK_RET(lyd_create_inner(iter, &node)); |
| 1609 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
| 1610 | lyd_insert_node(parent, first, node, 0); |
| 1611 | |
| 1612 | if (lysc_has_when(iter) && node_when) { |
| 1613 | /* remember to resolve when */ |
| 1614 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 1615 | } |
| 1616 | if (diff) { |
| 1617 | /* add into diff */ |
| 1618 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1619 | } |
| 1620 | |
| 1621 | /* create any default children */ |
| 1622 | LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types, |
| 1623 | impl_opts, diff)); |
| 1624 | } |
| 1625 | break; |
| 1626 | case LYS_LEAF: |
| 1627 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt && |
| 1628 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1629 | /* create default leaf */ |
| 1630 | ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node); |
| 1631 | if (ret == LY_EINCOMPLETE) { |
| 1632 | if (node_types) { |
| 1633 | /* remember to resolve type */ |
| 1634 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
| 1635 | } |
| 1636 | } else if (ret) { |
| 1637 | return ret; |
| 1638 | } |
| 1639 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
| 1640 | lyd_insert_node(parent, first, node, 0); |
| 1641 | |
| 1642 | if (lysc_has_when(iter) && node_when) { |
| 1643 | /* remember to resolve when */ |
| 1644 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 1645 | } |
| 1646 | if (diff) { |
| 1647 | /* add into diff */ |
| 1648 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1649 | } |
| 1650 | } |
| 1651 | break; |
| 1652 | case LYS_LEAFLIST: |
| 1653 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts && |
| 1654 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1655 | /* create all default leaf-lists */ |
| 1656 | dflts = ((struct lysc_node_leaflist *)iter)->dflts; |
| 1657 | LY_ARRAY_FOR(dflts, u) { |
| 1658 | ret = lyd_create_term2(iter, dflts[u], &node); |
| 1659 | if (ret == LY_EINCOMPLETE) { |
| 1660 | if (node_types) { |
| 1661 | /* remember to resolve type */ |
| 1662 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
| 1663 | } |
| 1664 | } else if (ret) { |
| 1665 | return ret; |
| 1666 | } |
| 1667 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
| 1668 | lyd_insert_node(parent, first, node, 0); |
| 1669 | |
| 1670 | if (lysc_has_when(iter) && node_when) { |
| 1671 | /* remember to resolve when */ |
| 1672 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 1673 | } |
| 1674 | if (diff) { |
| 1675 | /* add into diff */ |
| 1676 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | break; |
| 1681 | default: |
| 1682 | /* without defaults */ |
| 1683 | break; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | return LY_SUCCESS; |
| 1688 | } |
| 1689 | |
| 1690 | LIBYANG_API_DEF LY_ERR |
| 1691 | lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff) |
| 1692 | { |
| 1693 | LY_ERR ret = LY_SUCCESS; |
| 1694 | struct lyd_node *node; |
| 1695 | struct ly_set node_when = {0}; |
| 1696 | |
| 1697 | LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL); |
| 1698 | if (diff) { |
| 1699 | *diff = NULL; |
| 1700 | } |
| 1701 | |
| 1702 | LYD_TREE_DFS_BEGIN(tree, node) { |
| 1703 | /* skip added default nodes */ |
| 1704 | if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) && |
| 1705 | (node->schema->nodetype & LYD_NODE_INNER)) { |
| 1706 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL, |
| 1707 | implicit_options, diff), cleanup); |
| 1708 | } |
| 1709 | |
| 1710 | LYD_TREE_DFS_END(tree, node); |
| 1711 | } |
| 1712 | |
| 1713 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame^] | 1714 | 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] | 1715 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1716 | |
| 1717 | cleanup: |
| 1718 | ly_set_erase(&node_when, NULL); |
| 1719 | if (ret && diff) { |
| 1720 | lyd_free_all(*diff); |
| 1721 | *diff = NULL; |
| 1722 | } |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
| 1726 | LIBYANG_API_DEF LY_ERR |
| 1727 | lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff) |
| 1728 | { |
| 1729 | const struct lys_module *mod; |
| 1730 | struct lyd_node *d = NULL; |
| 1731 | uint32_t i = 0; |
| 1732 | LY_ERR ret = LY_SUCCESS; |
| 1733 | |
| 1734 | LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL); |
| 1735 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL); |
| 1736 | if (diff) { |
| 1737 | *diff = NULL; |
| 1738 | } |
| 1739 | if (!ctx) { |
| 1740 | ctx = LYD_CTX(*tree); |
| 1741 | } |
| 1742 | |
| 1743 | /* add nodes for each module one-by-one */ |
| 1744 | while ((mod = ly_ctx_get_module_iter(ctx, &i))) { |
| 1745 | if (!mod->implemented) { |
| 1746 | continue; |
| 1747 | } |
| 1748 | |
| 1749 | LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup); |
| 1750 | if (d) { |
| 1751 | /* merge into one diff */ |
| 1752 | lyd_insert_sibling(*diff, d, diff); |
| 1753 | |
| 1754 | d = NULL; |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | cleanup: |
| 1759 | if (ret && diff) { |
| 1760 | lyd_free_all(*diff); |
| 1761 | *diff = NULL; |
| 1762 | } |
| 1763 | return ret; |
| 1764 | } |
| 1765 | |
| 1766 | LIBYANG_API_DEF LY_ERR |
| 1767 | lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, |
| 1768 | struct lyd_node **diff) |
| 1769 | { |
| 1770 | LY_ERR ret = LY_SUCCESS; |
| 1771 | struct lyd_node *root, *d = NULL; |
| 1772 | struct ly_set node_when = {0}; |
| 1773 | |
| 1774 | LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL); |
| 1775 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
| 1776 | if (diff) { |
| 1777 | *diff = NULL; |
| 1778 | } |
| 1779 | |
| 1780 | /* add all top-level defaults for this module */ |
| 1781 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, implicit_options, diff), cleanup); |
| 1782 | |
| 1783 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame^] | 1784 | LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL, 0, diff), |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1785 | cleanup); |
| 1786 | |
| 1787 | /* process nested nodes */ |
| 1788 | LY_LIST_FOR(*tree, root) { |
| 1789 | /* skip added default nodes */ |
| 1790 | if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) { |
| 1791 | LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup); |
| 1792 | |
| 1793 | if (d) { |
| 1794 | /* merge into one diff */ |
| 1795 | lyd_insert_sibling(*diff, d, diff); |
| 1796 | |
| 1797 | d = NULL; |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | cleanup: |
| 1803 | ly_set_erase(&node_when, NULL); |
| 1804 | if (ret && diff) { |
| 1805 | lyd_free_all(*diff); |
| 1806 | *diff = NULL; |
| 1807 | } |
| 1808 | return ret; |
| 1809 | } |