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