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