blob: f3bd9e7f749438e5f9a6c56ebf1af88da6afe561 [file] [log] [blame]
Michal Vasko59892dd2022-05-13 11:02:30 +02001/**
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 Vaskoabd34fb2024-02-21 09:53:56 +01007 * Copyright (c) 2015 - 2024 CESNET, z.s.p.o.
Michal Vasko59892dd2022-05-13 11:02:30 +02008 *
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 Vasko59892dd2022-05-13 11:02:30 +020027#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 Vasko8f702ee2024-02-20 15:44:24 +010035#include "ly_common.h"
Michal Vasko59892dd2022-05-13 11:02:30 +020036#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"
aPiecek6cf1d162023-11-08 16:07:00 +010046#include "tree_data_sorted.h"
Michal Vasko59892dd2022-05-13 11:02:30 +020047#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
54LY_ERR
stewegd4cde642024-02-21 08:34:16 +010055lyd_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 Vasko59892dd2022-05-13 11:02:30 +020057{
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 Vasko7a266772024-01-23 11:02:38 +010070 LOG_LOCSET(schema, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +020071 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
stewegd4cde642024-02-21 08:34:16 +010072 value_len, is_utf8, store_only, dynamic, format, prefix_data, hints, schema, incomplete);
Michal Vasko7a266772024-01-23 11:02:38 +010073 LOG_LOCBACK(1, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +020074 LY_CHECK_ERR_RET(ret, free(term), ret);
75 lyd_hash(&term->node);
76
77 *node = &term->node;
78 return ret;
79}
80
81LY_ERR
82lyd_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
111LY_ERR
112lyd_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
137LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +0100138lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, const struct lyxp_var *vars,
stewegd4cde642024-02-21 08:34:16 +0100139 ly_bool store_only, struct lyd_node **node)
Michal Vasko59892dd2022-05-13 11:02:30 +0200140{
141 LY_ERR ret = LY_SUCCESS;
142 struct lyd_node *list = NULL, *key;
Michal Vasko90189962023-02-28 12:10:34 +0100143 const struct lyd_value *value;
144 struct lyd_value val = {0};
145 struct lyxp_var *var;
Michal Vasko59892dd2022-05-13 11:02:30 +0200146 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 Vasko7a266772024-01-23 11:02:38 +0100153 LOG_LOCSET(schema, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200154
155 /* create and insert all the keys */
156 LY_ARRAY_FOR(predicates, u) {
Michal Vasko90189962023-02-28 12:10:34 +0100157 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 Vasko7a266772024-01-23 11:02:38 +0100164 LOG_LOCSET(predicates[u].key, NULL);
Michal Vasko90189962023-02-28 12:10:34 +0100165 ret = lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaf *)predicates[u].key)->type,
stewegd4cde642024-02-21 08:34:16 +0100166 var->value, strlen(var->value), 0, store_only, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, predicates[u].key, NULL);
Michal Vasko7a266772024-01-23 11:02:38 +0100167 LOG_LOCBACK(1, 0);
Michal Vasko90189962023-02-28 12:10:34 +0100168 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);
aPiecek1462ab12024-02-07 09:13:29 +0100182 lyd_insert_node(list, NULL, key, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +0200183 }
184
185 /* hash having all the keys */
186 lyd_hash(list);
187
188 /* success */
189 *node = list;
190 list = NULL;
191
192cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +0100193 LOG_LOCBACK(1, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +0200194 lyd_free_tree(list);
195 return ret;
196}
197
198LY_ERR
stewegd4cde642024-02-21 08:34:16 +0100199lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, ly_bool store_only, struct lyd_node **node)
Michal Vasko59892dd2022-05-13 11:02:30 +0200200{
201 LY_ERR ret = LY_SUCCESS;
202 struct lyxp_expr *expr = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +0200203 uint32_t exp_idx = 0;
Michal Vasko59892dd2022-05-13 11:02:30 +0200204 struct ly_path_predicate *predicates = NULL;
205
Michal Vasko7a266772024-01-23 11:02:38 +0100206 LOG_LOCSET(schema, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200207
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 Vasko90189962023-02-28 12:10:34 +0100214 NULL, &predicates), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200215
216 /* create the list node */
stewegd4cde642024-02-21 08:34:16 +0100217 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, NULL, store_only, node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200218
219cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +0100220 LOG_LOCBACK(1, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +0200221 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90189962023-02-28 12:10:34 +0100222 ly_path_predicates_free(schema->module->ctx, predicates);
Michal Vasko59892dd2022-05-13 11:02:30 +0200223 return ret;
224}
225
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100226/**
Michal Vasko5b414dd2023-04-13 10:29:58 +0200227 * @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 */
232static void
233lyd_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 Vaskoe3ed7dc2022-11-30 11:39:44 +0100253 *
254 * @param[in] ctx libyang context.
Michal Vasko5b414dd2023-04-13 10:29:58 +0200255 * @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 Vaskoe3ed7dc2022-11-30 11:39:44 +0100258 * @param[out] tree Parsed data tree.
259 * @return LY_ERR value.
260 */
261static LY_ERR
Michal Vasko5b414dd2023-04-13 10:29:58 +0200262lyd_create_any_datatree(const struct ly_ctx *ctx, struct ly_in *value_in, LYD_ANYDATA_VALUETYPE value_type, ly_bool log,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100263 struct lyd_node **tree)
264{
Michal Vasko5b414dd2023-04-13 10:29:58 +0200265 LY_ERR rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100266 struct lyd_ctx *lydctx = NULL;
Michal Vasko17d9cea2024-02-09 13:48:40 +0100267 uint32_t parse_opts, int_opts, *prev_lo = NULL, temp_lo = 0;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100268
269 *tree = NULL;
270
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100271 /* set options */
272 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_OPAQ;
273 int_opts = LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
274
Michal Vasko5b414dd2023-04-13 10:29:58 +0200275 if (!log) {
276 /* no logging */
Michal Vasko59004e32024-01-30 16:09:54 +0100277 prev_lo = ly_temp_log_options(&temp_lo);
Michal Vasko5b414dd2023-04-13 10:29:58 +0200278 }
279
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100280 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 Vasko5b414dd2023-04-13 10:29:58 +0200286 rc = lyd_parse_xml(ctx, NULL, NULL, tree, value_in, parse_opts, 0, int_opts, NULL, NULL, &lydctx);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100287 break;
288 case LYD_ANYDATA_JSON:
Michal Vasko5b414dd2023-04-13 10:29:58 +0200289 rc = lyd_parse_json(ctx, NULL, NULL, tree, value_in, parse_opts, 0, int_opts, NULL, NULL, &lydctx);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100290 break;
291 case LYD_ANYDATA_LYB:
Michal Vasko5b414dd2023-04-13 10:29:58 +0200292 rc = lyd_parse_lyb(ctx, NULL, NULL, tree, value_in, parse_opts | LYD_PARSE_STRICT, 0, int_opts, NULL, NULL, &lydctx);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100293 break;
294 }
295 if (lydctx) {
296 lydctx->free(lydctx);
297 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100298
Michal Vasko5b414dd2023-04-13 10:29:58 +0200299 if (!log) {
300 /* restore logging */
Michal Vasko59004e32024-01-30 16:09:54 +0100301 ly_temp_log_options(prev_lo);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100302 }
Michal Vasko90fc1ae2023-04-27 12:22:19 +0200303 if (rc && *tree) {
304 lyd_free_siblings(*tree);
305 *tree = NULL;
306 }
Michal Vasko5b414dd2023-04-13 10:29:58 +0200307 return rc;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100308}
309
Michal Vasko59892dd2022-05-13 11:02:30 +0200310LY_ERR
311lyd_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 Vasko5b414dd2023-04-13 10:29:58 +0200314 LY_ERR rc = LY_SUCCESS, r;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100315 struct lyd_node *tree;
Michal Vaskobdeee992022-12-01 16:17:56 +0100316 struct lyd_node_any *any = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +0200317 union lyd_any_value any_val;
Michal Vasko5b414dd2023-04-13 10:29:58 +0200318 struct ly_in *in = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +0200319
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 Vasko5b414dd2023-04-13 10:29:58 +0200329 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 Vaskoe3ed7dc2022-11-30 11:39:44 +0100334 }
Michal Vasko5b414dd2023-04-13 10:29:58 +0200335
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 Vaskoe3ed7dc2022-11-30 11:39:44 +0100393 }
394
Michal Vasko59892dd2022-05-13 11:02:30 +0200395 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 Vasko5b414dd2023-04-13 10:29:58 +0200403 LY_CHECK_GOTO(rc = lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200404 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 Vasko5b414dd2023-04-13 10:29:58 +0200412 LY_CHECK_GOTO(rc = lyd_any_copy_value(&any->node, &any_val, value_type), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200413 }
414 lyd_hash(&any->node);
415
Michal Vasko5b414dd2023-04-13 10:29:58 +0200416cleanup:
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 Vasko59892dd2022-05-13 11:02:30 +0200424}
425
426LY_ERR
427lyd_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
464finish:
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
stewegd4cde642024-02-21 08:34:16 +0100474/**
475 * @brief Gets format from lyd_new_* options
476 *
477 * @param[in] options Bitmask of options, see @ref newvalueoptions.
Michal Vasko6a027db2024-02-21 09:55:34 +0100478 * @param[out] format Value format.
stewegd4cde642024-02-21 08:34:16 +0100479 * @return LY_ERR value.
480 */
481static LY_ERR
Michal Vasko6a027db2024-02-21 09:55:34 +0100482lyd_new_val_get_format(uint32_t options, LY_VALUE_FORMAT *format)
stewegd4cde642024-02-21 08:34:16 +0100483{
Michal Vasko6a027db2024-02-21 09:55:34 +0100484 LY_CHECK_ARG_RET(NULL, format, !((options & LYD_NEW_VAL_BIN) && (options & LYD_NEW_VAL_CANON)), LY_EVALID);
stewegd4cde642024-02-21 08:34:16 +0100485
Michal Vasko6a027db2024-02-21 09:55:34 +0100486 if (options & LYD_NEW_VAL_BIN) {
stewegd4cde642024-02-21 08:34:16 +0100487 *format = LY_VALUE_LYB;
Michal Vasko6a027db2024-02-21 09:55:34 +0100488 } else if (options & LYD_NEW_VAL_CANON) {
stewegd4cde642024-02-21 08:34:16 +0100489 *format = LY_VALUE_CANON;
490 } else {
491 *format = LY_VALUE_JSON;
492 }
493
494 return LY_SUCCESS;
495}
496
Michal Vasko59892dd2022-05-13 11:02:30 +0200497LIBYANG_API_DEF LY_ERR
498lyd_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) {
aPiecek1462ab12024-02-07 09:13:29 +0100529 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +0200530 }
531
532 if (node) {
533 *node = ret;
534 }
535 return LY_SUCCESS;
536}
537
538LIBYANG_API_DEF LY_ERR
539lyd_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 Vasko2c1e3272023-10-17 14:08:35 +0200566 * @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.
stewegd4cde642024-02-21 08:34:16 +0100572 * @param[in] options Bitmask of options, see @ref newvalueoptions.
Michal Vasko2c1e3272023-10-17 14:08:35 +0200573 * @param[out] node Created node.
574 * @return LY_ERR value.
575 */
576static LY_ERR
577_lyd_new_list_node(const struct ly_ctx *ctx, const struct lyd_node *parent, const struct lys_module *module,
stewegd4cde642024-02-21 08:34:16 +0100578 const char *name, uint32_t options, struct lyd_node **node)
Michal Vasko2c1e3272023-10-17 14:08:35 +0200579{
580 struct lyd_node *ret = NULL;
581 const struct lysc_node *schema;
582 struct lysc_ext_instance *ext = NULL;
stewegd4cde642024-02-21 08:34:16 +0100583 uint32_t getnext_opts = (options & LYD_NEW_VAL_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko2c1e3272023-10-17 14:08:35 +0200584 LY_ERR r;
585
586 if (!module) {
587 module = parent->schema->module;
588 }
589
stewegd4cde642024-02-21 08:34:16 +0100590 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, getnext_opts);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200591 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
stewegd4cde642024-02-21 08:34:16 +0100609LIBYANG_API_DEF LY_ERR
610lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, uint32_t options,
611 struct lyd_node **node, ...)
Michal Vasko59892dd2022-05-13 11:02:30 +0200612{
613 struct lyd_node *ret = NULL, *key;
Michal Vasko2c1e3272023-10-17 14:08:35 +0200614 const struct lysc_node *key_s;
Michal Vasko59892dd2022-05-13 11:02:30 +0200615 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
616 const void *key_val;
617 uint32_t key_len;
Michal Vasko2c1e3272023-10-17 14:08:35 +0200618 LY_ERR rc = LY_SUCCESS;
stewegd4cde642024-02-21 08:34:16 +0100619 ly_bool store_only = (options & LYD_NEW_VAL_STORE_ONLY) ? 1 : 0;
620 LY_VALUE_FORMAT format;
621 va_list ap;
Michal Vasko59892dd2022-05-13 11:02:30 +0200622
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 Vasko6a027db2024-02-21 09:55:34 +0100625 LY_CHECK_RET(lyd_new_val_get_format(options, &format));
stewegd4cde642024-02-21 08:34:16 +0100626 LY_CHECK_ARG_RET(ctx, !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)), LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200627
Michal Vasko2c1e3272023-10-17 14:08:35 +0200628 /* create the list node */
stewegd4cde642024-02-21 08:34:16 +0100629 LY_CHECK_RET(_lyd_new_list_node(ctx, parent, module, name, options, &ret));
Michal Vasko59892dd2022-05-13 11:02:30 +0200630
stewegd4cde642024-02-21 08:34:16 +0100631 va_start(ap, node);
Michal Vasko59892dd2022-05-13 11:02:30 +0200632 /* create and insert all the keys */
Michal Vasko2c1e3272023-10-17 14:08:35 +0200633 for (key_s = lysc_node_child(ret->schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
Michal Vasko59892dd2022-05-13 11:02:30 +0200634 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 }
stewegd4cde642024-02-21 08:34:16 +0100641 rc = lyd_create_term(key_s, key_val, key_len, 0, store_only, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
Michal Vasko59892dd2022-05-13 11:02:30 +0200642 LY_CHECK_GOTO(rc, cleanup);
aPiecek1462ab12024-02-07 09:13:29 +0100643 lyd_insert_node(ret, NULL, key, LYD_INSERT_NODE_LAST);
Michal Vasko59892dd2022-05-13 11:02:30 +0200644 }
645
Michal Vasko59892dd2022-05-13 11:02:30 +0200646 if (parent) {
aPiecek1462ab12024-02-07 09:13:29 +0100647 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +0200648 }
649
650cleanup:
stewegd4cde642024-02-21 08:34:16 +0100651 va_end(ap);
Michal Vasko59892dd2022-05-13 11:02:30 +0200652 if (rc) {
653 lyd_free_tree(ret);
654 ret = NULL;
655 } else if (node) {
656 *node = ret;
657 }
658 return rc;
659}
660
661LIBYANG_API_DEF LY_ERR
stewegd4cde642024-02-21 08:34:16 +0100662lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, uint32_t options, struct lyd_node **node, ...)
Michal Vasko59892dd2022-05-13 11:02:30 +0200663{
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;
stewegd4cde642024-02-21 08:34:16 +0100669 size_t key_len;
Michal Vasko59892dd2022-05-13 11:02:30 +0200670 LY_ERR rc = LY_SUCCESS;
stewegd4cde642024-02-21 08:34:16 +0100671 ly_bool store_only = (options & LYD_NEW_VAL_STORE_ONLY) ? 1 : 0;
672 LY_VALUE_FORMAT format;
Michal Vasko59892dd2022-05-13 11:02:30 +0200673
674 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
Michal Vasko6a027db2024-02-21 09:55:34 +0100675 LY_CHECK_RET(lyd_new_val_get_format(options, &format));
stewegd4cde642024-02-21 08:34:16 +0100676 LY_CHECK_ARG_RET(ctx, !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)), LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200677
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) {
stewegd4cde642024-02-21 08:34:16 +0100695 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 Vasko59892dd2022-05-13 11:02:30 +0200703 LY_CHECK_GOTO(rc, cleanup);
aPiecek1462ab12024-02-07 09:13:29 +0100704 lyd_insert_node(ret, NULL, key, LYD_INSERT_NODE_LAST);
Michal Vasko59892dd2022-05-13 11:02:30 +0200705 }
706
707cleanup:
708 va_end(ap);
709 if (rc) {
710 lyd_free_tree(ret);
711 ret = NULL;
712 }
713 *node = ret;
714 return rc;
715}
716
717LIBYANG_API_DEF LY_ERR
718lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
stewegd4cde642024-02-21 08:34:16 +0100719 uint32_t options, struct lyd_node **node)
Michal Vasko59892dd2022-05-13 11:02:30 +0200720{
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);
stewegd4cde642024-02-21 08:34:16 +0100726 uint32_t getnext_opts = (options & LYD_NEW_VAL_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko59892dd2022-05-13 11:02:30 +0200727
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 */
stewegd4cde642024-02-21 08:34:16 +0100739 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, getnext_opts);
Michal Vasko59892dd2022-05-13 11:02:30 +0200740 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 */
stewegd4cde642024-02-21 08:34:16 +0100752 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 Vasko59892dd2022-05-13 11:02:30 +0200755 }
756 if (ext) {
757 ret->flags |= LYD_EXT;
758 }
759 if (parent) {
aPiecek1462ab12024-02-07 09:13:29 +0100760 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +0200761 }
762
763 if (node) {
764 *node = ret;
765 }
766 return LY_SUCCESS;
767}
768
Michal Vaskof8c0b582024-02-21 09:55:53 +0100769LIBYANG_API_DEF LY_ERR
770lyd_new_list3(struct lyd_node *parent, const struct lys_module *module, const char *name, const char **key_values,
stewegd4cde642024-02-21 08:34:16 +0100771 uint32_t *value_lengths, uint32_t options, struct lyd_node **node)
Michal Vasko2c1e3272023-10-17 14:08:35 +0200772{
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;
stewegd4cde642024-02-21 08:34:16 +0100779 ly_bool store_only = (options & LYD_NEW_VAL_STORE_ONLY) ? 1 : 0;
780 LY_VALUE_FORMAT format;
Michal Vasko2c1e3272023-10-17 14:08:35 +0200781
Michal Vasko6a027db2024-02-21 09:55:34 +0100782 LY_CHECK_RET(lyd_new_val_get_format(options, &format));
Michal Vaskobeeb8b62024-01-30 16:10:17 +0100783 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, (format != LY_VALUE_LYB) || value_lengths, LY_EINVAL);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200784 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
stewegd4cde642024-02-21 08:34:16 +0100785 LY_CHECK_ARG_RET(ctx, !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)), LY_EINVAL);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200786
787 /* create the list node */
stewegd4cde642024-02-21 08:34:16 +0100788 LY_CHECK_RET(_lyd_new_list_node(ctx, parent, module, name, options, &ret));
Michal Vasko2c1e3272023-10-17 14:08:35 +0200789
Michal Vaskobeeb8b62024-01-30 16:10:17 +0100790 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 Vasko2c1e3272023-10-17 14:08:35 +0200796 /* 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 Vasko37f28c42024-01-08 09:43:01 +0100799 key_val = key_values[i] ? key_values[i] : "";
800 key_len = value_lengths ? value_lengths[i] : strlen(key_val);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200801
stewegd4cde642024-02-21 08:34:16 +0100802 rc = lyd_create_term(key_s, key_val, key_len, 0, store_only, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200803 LY_CHECK_GOTO(rc, cleanup);
aPiecek1462ab12024-02-07 09:13:29 +0100804 lyd_insert_node(ret, NULL, key, LYD_INSERT_NODE_LAST);
Amjad Daraisehf4ba2ff2024-03-22 10:38:50 +0300805 ++i;
Michal Vasko2c1e3272023-10-17 14:08:35 +0200806 }
807
808 if (parent) {
aPiecek1462ab12024-02-07 09:13:29 +0100809 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_DEFAULT);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200810 }
811
812cleanup:
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 Vasko2c1e3272023-10-17 14:08:35 +0200822/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200823 * @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.
stewegd4cde642024-02-21 08:34:16 +0100827 * @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 Vasko59892dd2022-05-13 11:02:30 +0200829 * @param[in] value_len Length of @p value.
stewegd4cde642024-02-21 08:34:16 +0100830 * @param[in] options Bitmask of options, see @ref newvalueoptions.
Michal Vasko59892dd2022-05-13 11:02:30 +0200831 * @param[out] node Optional created node.
832 * @return LY_ERR value.
833 */
834static LY_ERR
835_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
stewegd4cde642024-02-21 08:34:16 +0100836 size_t value_len, uint32_t options, struct lyd_node **node)
Michal Vasko59892dd2022-05-13 11:02:30 +0200837{
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);
stewegd4cde642024-02-21 08:34:16 +0100843 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 Vasko59892dd2022-05-13 11:02:30 +0200846
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 Vasko6a027db2024-02-21 09:55:34 +0100849 LY_CHECK_RET(lyd_new_val_get_format(options, &format));
stewegd4cde642024-02-21 08:34:16 +0100850 LY_CHECK_ARG_RET(ctx, !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)), LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200851
852 if (!module) {
853 module = parent->schema->module;
854 }
855
stewegd4cde642024-02-21 08:34:16 +0100856 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, getnext_opts);
Michal Vasko59892dd2022-05-13 11:02:30 +0200857 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
stewegd4cde642024-02-21 08:34:16 +0100864 LY_CHECK_RET(lyd_create_term(schema, value, value_len, 0, store_only, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret));
Michal Vasko59892dd2022-05-13 11:02:30 +0200865 if (ext) {
866 ret->flags |= LYD_EXT;
867 }
868 if (parent) {
aPiecek1462ab12024-02-07 09:13:29 +0100869 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +0200870 }
871
872 if (node) {
873 *node = ret;
874 }
875 return LY_SUCCESS;
876}
877
878LIBYANG_API_DEF LY_ERR
stewegd4cde642024-02-21 08:34:16 +0100879lyd_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 Vasko59892dd2022-05-13 11:02:30 +0200881{
stewegd4cde642024-02-21 08:34:16 +0100882 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
883
Michal Vasko6a027db2024-02-21 09:55:34 +0100884 LY_CHECK_ARG_RET(ctx, !(options & LYD_NEW_VAL_BIN), LY_EINVAL);
stewegd4cde642024-02-21 08:34:16 +0100885 return _lyd_new_term(parent, module, name, value, value ? strlen(value) : 0, options, node);
886}
887
888LIBYANG_API_DECL LY_ERR
889lyd_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 Vasko6a027db2024-02-21 09:55:34 +0100892 options |= LYD_NEW_VAL_BIN;
893
stewegd4cde642024-02-21 08:34:16 +0100894 return _lyd_new_term(parent, module, name, value, value_len, options, node);
Michal Vasko59892dd2022-05-13 11:02:30 +0200895}
896
897LIBYANG_API_DEF LY_ERR
stewegd4cde642024-02-21 08:34:16 +0100898lyd_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 Vasko59892dd2022-05-13 11:02:30 +0200900{
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;
stewegd4cde642024-02-21 08:34:16 +0100905 ly_bool store_only = (options & LYD_NEW_VAL_STORE_ONLY) ? 1 : 0;
906 LY_VALUE_FORMAT format;
Michal Vasko59892dd2022-05-13 11:02:30 +0200907
908 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
Michal Vasko6a027db2024-02-21 09:55:34 +0100909 LY_CHECK_RET(lyd_new_val_get_format(options, &format));
stewegd4cde642024-02-21 08:34:16 +0100910 LY_CHECK_ARG_RET(ctx, !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)), LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200911
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 }
stewegd4cde642024-02-21 08:34:16 +0100922 rc = lyd_create_term(schema, value, value_len, 0, store_only, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vasko59892dd2022-05-13 11:02:30 +0200923 LY_CHECK_RET(rc);
924
925 *node = ret;
926
927 return LY_SUCCESS;
928}
929
930LIBYANG_API_DEF LY_ERR
931lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko6a027db2024-02-21 09:55:34 +0100932 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **node)
Michal Vasko59892dd2022-05-13 11:02:30 +0200933{
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 Vasko16c25e02024-02-26 14:51:45 +0100939 uint32_t getnext_opts = (options & LYD_NEW_VAL_OUTPUT) ? LYS_GETNEXT_OUTPUT : 0;
stewegd4cde642024-02-21 08:34:16 +0100940 ly_bool use_value = (options & LYD_NEW_ANY_USE_VALUE) ? 1 : 0;
Michal Vasko59892dd2022-05-13 11:02:30 +0200941
Michal Vasko2ac91152023-11-21 10:29:18 +0100942 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 Vasko59892dd2022-05-13 11:02:30 +0200944 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
stewegd4cde642024-02-21 08:34:16 +0100950 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, getnext_opts);
Michal Vasko59892dd2022-05-13 11:02:30 +0200951 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) {
aPiecek1462ab12024-02-07 09:13:29 +0100963 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +0200964 }
965
966 if (node) {
967 *node = ret;
968 }
969 return LY_SUCCESS;
970}
971
972LIBYANG_API_DEF LY_ERR
Michal Vasko6a027db2024-02-21 09:55:34 +0100973lyd_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 Vasko59892dd2022-05-13 11:02:30 +0200975{
976 struct lyd_node *ret = NULL;
977 const struct lysc_node *schema;
978 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
stewegd4cde642024-02-21 08:34:16 +0100979 ly_bool use_value = (options & LYD_NEW_ANY_USE_VALUE) ? 1 : 0;
Michal Vasko59892dd2022-05-13 11:02:30 +0200980
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
1000LIBYANG_API_DEF LY_ERR
1001lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
stewegd4cde642024-02-21 08:34:16 +01001002 const char *val_str, uint32_t options, struct lyd_meta **meta)
Michal Vasko59892dd2022-05-13 11:02:30 +02001003{
1004 const char *prefix, *tmp;
1005 size_t pref_len, name_len;
stewegd4cde642024-02-21 08:34:16 +01001006 ly_bool clear_dflt = options & LYD_NEW_META_CLEAR_DFLT;
1007 ly_bool store_only = options & LYD_NEW_VAL_STORE_ONLY;
Michal Vasko59892dd2022-05-13 11:02:30 +02001008
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 Vasko33c48972022-07-20 10:28:07 +02001012 ctx = module ? module->ctx : LYD_CTX(parent);
Michal Vasko59892dd2022-05-13 11:02:30 +02001013 }
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
stewegd4cde642024-02-21 08:34:16 +01001041 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), 0, store_only, NULL, LY_VALUE_JSON,
Michal Vasko59892dd2022-05-13 11:02:30 +02001042 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
1043}
1044
1045LIBYANG_API_DEF LY_ERR
stewegd4cde642024-02-21 08:34:16 +01001046lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, uint32_t options, const struct lyd_attr *attr, struct lyd_meta **meta)
Michal Vasko59892dd2022-05-13 11:02:30 +02001047{
1048 const struct lys_module *mod;
stewegd4cde642024-02-21 08:34:16 +01001049 ly_bool clear_dflt = options & LYD_NEW_META_CLEAR_DFLT;
1050 ly_bool store_only = options & LYD_NEW_VAL_STORE_ONLY;
Michal Vasko59892dd2022-05-13 11:02:30 +02001051
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 Vasko7b3a00e2023-08-09 11:58:03 +02001056 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".",
1057 ((struct lyd_node_opaq *)parent)->name.name);
Michal Vasko59892dd2022-05-13 11:02:30 +02001058 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),
stewegd4cde642024-02-21 08:34:16 +01001084 0, store_only, NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001085}
1086
1087LIBYANG_API_DEF LY_ERR
1088lyd_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;
1092
1093 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
1094 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1095
1096 if (!ctx) {
1097 ctx = LYD_CTX(parent);
1098 }
1099 if (!value) {
1100 value = "";
1101 }
1102
1103 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1104 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
1105 if (parent) {
aPiecek1462ab12024-02-07 09:13:29 +01001106 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_LAST);
Michal Vasko59892dd2022-05-13 11:02:30 +02001107 }
1108
1109 if (node) {
1110 *node = ret;
1111 }
1112 return LY_SUCCESS;
1113}
1114
1115LIBYANG_API_DEF LY_ERR
1116lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
1117 const char *prefix, const char *module_ns, struct lyd_node **node)
1118{
1119 struct lyd_node *ret = NULL;
1120
1121 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1122 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1123
1124 if (!ctx) {
1125 ctx = LYD_CTX(parent);
1126 }
1127 if (!value) {
1128 value = "";
1129 }
1130
1131 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1132 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
1133 if (parent) {
aPiecek1462ab12024-02-07 09:13:29 +01001134 lyd_insert_node(parent, NULL, ret, LYD_INSERT_NODE_LAST);
Michal Vasko59892dd2022-05-13 11:02:30 +02001135 }
1136
1137 if (node) {
1138 *node = ret;
1139 }
1140 return LY_SUCCESS;
1141}
1142
1143LIBYANG_API_DEF LY_ERR
1144lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
1145 struct lyd_attr **attr)
1146{
1147 struct lyd_attr *ret = NULL;
1148 const struct ly_ctx *ctx;
1149 const char *prefix, *tmp;
1150 size_t pref_len, name_len, mod_len;
1151
1152 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1153
1154 ctx = LYD_CTX(parent);
1155
1156 /* parse the name */
1157 tmp = name;
1158 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1159 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1160 return LY_EVALID;
1161 }
1162
1163 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1164 /* not a prefix but special name */
1165 name = prefix;
1166 name_len += 1 + pref_len;
1167 prefix = NULL;
1168 pref_len = 0;
1169 }
1170
1171 /* get the module */
1172 if (module_name) {
1173 mod_len = strlen(module_name);
1174 } else {
1175 module_name = prefix;
1176 mod_len = pref_len;
1177 }
1178
1179 /* set value if none */
1180 if (!value) {
1181 value = "";
1182 }
1183
1184 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1185 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
1186
1187 if (attr) {
1188 *attr = ret;
1189 }
1190 return LY_SUCCESS;
1191}
1192
1193LIBYANG_API_DEF LY_ERR
1194lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1195 struct lyd_attr **attr)
1196{
1197 struct lyd_attr *ret = NULL;
1198 const struct ly_ctx *ctx;
1199 const char *prefix, *tmp;
1200 size_t pref_len, name_len;
1201
1202 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1203
1204 ctx = LYD_CTX(parent);
1205
1206 /* parse the name */
1207 tmp = name;
1208 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1209 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1210 return LY_EVALID;
1211 }
1212
1213 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1214 /* not a prefix but special name */
1215 name = prefix;
1216 name_len += 1 + pref_len;
1217 prefix = NULL;
1218 pref_len = 0;
1219 }
1220
1221 /* set value if none */
1222 if (!value) {
1223 value = "";
1224 }
Michal Vasko825718f2022-11-10 10:33:51 +01001225 if (strchr(value, ':')) {
1226 LOGWRN(ctx, "Value \"%s\" prefix will never be interpreted as an XML prefix.", value);
1227 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001228
1229 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1230 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
1231
1232 if (attr) {
1233 *attr = ret;
1234 }
1235 return LY_SUCCESS;
1236}
1237
1238/**
aPiecek6cf1d162023-11-08 16:07:00 +01001239 * @brief Change the value of @p term by @p val and reinsert the node if necessary.
aPiecek2e1f68f2023-11-08 11:27:33 +01001240 *
aPiecek6cf1d162023-11-08 16:07:00 +01001241 * Reinserting ensures that the node is in the correct position and the data instances remain properly ordered.
1242 *
1243 * @param[in] term Term node to change. If it is a key, the parental list is inserted again.
aPiecek2e1f68f2023-11-08 11:27:33 +01001244 * @param[in] val New value for @p term.
1245 * @return LY_SUCCESS on success.
1246 */
1247static LY_ERR
aPiecek6cf1d162023-11-08 16:07:00 +01001248lyd_change_node_value(struct lyd_node_term *term, struct lyd_value *val)
aPiecek2e1f68f2023-11-08 11:27:33 +01001249{
1250 LY_ERR ret = LY_SUCCESS;
aPiecek6cf1d162023-11-08 16:07:00 +01001251 struct lyd_node *target, *first;
aPiecek2e1f68f2023-11-08 11:27:33 +01001252
1253 if (term->schema->nodetype == LYS_LEAFLIST) {
1254 target = (struct lyd_node *)term;
1255 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1256 target = (struct lyd_node *)term->parent;
1257 } else {
1258 /* just change the value */
1259 term->value.realtype->plugin->free(LYD_CTX(term), &term->value);
1260 term->value = *val;
1261 /* leaf that is not a key, its value is not used for its hash so it does not change */
1262 return LY_SUCCESS;
1263 }
1264
aPiecek6cf1d162023-11-08 16:07:00 +01001265 if (!LYD_NODE_IS_ALONE(target) && lyds_is_supported(target)) {
1266 /* changing the value may cause a change in the order */
1267 first = lyd_first_sibling(target);
1268 first = first == target ? first->next : first;
1269 /* unlink hash and unlink the target node in the lyds tree */
1270 lyd_unlink_tree(target);
1271 /* change value */
1272 term->value.realtype->plugin->free(LYD_CTX(term), &term->value);
1273 term->value = *val;
1274 /* reinserting */
aPiecek1462ab12024-02-07 09:13:29 +01001275 lyd_insert_node(NULL, &first, target, LYD_INSERT_NODE_DEFAULT);
aPiecek6cf1d162023-11-08 16:07:00 +01001276 } else {
1277 /* unlink hash */
1278 lyd_unlink_hash(target);
1279 /* change value */
1280 term->value.realtype->plugin->free(LYD_CTX(term), &term->value);
1281 term->value = *val;
1282 }
aPiecek2e1f68f2023-11-08 11:27:33 +01001283 lyd_hash(target);
1284 ret = lyd_insert_hash(target);
1285
1286 return ret;
1287}
1288
1289/**
Michal Vasko59892dd2022-05-13 11:02:30 +02001290 * @brief Change the value of a term (leaf or leaf-list) node.
1291 *
1292 * Node changed this way is always considered explicitly set, meaning its default flag
1293 * is always cleared.
1294 *
1295 * @param[in] term Term node to change.
1296 * @param[in] value New value to set.
1297 * @param[in] value_len Length of @p value.
1298 * @param[in] format Format of @p value.
1299 * @return LY_SUCCESS if value was changed,
1300 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1301 * @return LY_ENOT if the values were equal and no change occured,
1302 * @return LY_ERR value on other errors.
1303 */
1304static LY_ERR
1305_lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format)
1306{
1307 LY_ERR ret = LY_SUCCESS;
1308 struct lysc_type *type;
1309 struct lyd_node_term *t;
1310 struct lyd_node *parent;
1311 struct lyd_value val;
1312 ly_bool dflt_change, val_change;
1313
1314 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
1315
1316 t = (struct lyd_node_term *)term;
1317 type = ((struct lysc_node_leaf *)term->schema)->type;
1318
1319 /* parse the new value */
Michal Vasko7a266772024-01-23 11:02:38 +01001320 LOG_LOCSET(term->schema, term);
stewegd4cde642024-02-21 08:34:16 +01001321 ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, 0, 0, NULL, format, NULL, LYD_HINT_DATA,
Michal Vasko989cdb42023-10-06 15:32:37 +02001322 term->schema, NULL);
Michal Vasko7a266772024-01-23 11:02:38 +01001323 LOG_LOCBACK(1, 1);
Michal Vasko59892dd2022-05-13 11:02:30 +02001324 LY_CHECK_GOTO(ret, cleanup);
1325
1326 /* compare original and new value */
aPiecek0a6705b2023-11-14 14:20:58 +01001327 if (type->plugin->compare(LYD_CTX(term), &t->value, &val)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001328 /* values differ, switch them */
aPiecek6cf1d162023-11-08 16:07:00 +01001329 lyd_change_node_value(t, &val);
aPiecek2e1f68f2023-11-08 11:27:33 +01001330 /* make the node non-validated */
1331 term->flags &= LYD_NEW;
Michal Vasko59892dd2022-05-13 11:02:30 +02001332 val_change = 1;
1333 } else {
1334 /* same values, free the new stored one */
1335 type->plugin->free(LYD_CTX(term), &val);
1336 val_change = 0;
1337 }
1338
stewegf9041a22024-01-18 13:29:12 +01001339 /* clear links to leafref nodes */
1340 if (ly_ctx_get_options(LYD_CTX(term)) & LY_CTX_LEAFREF_LINKING) {
1341 lyd_free_leafref_nodes(t);
1342 }
1343
Michal Vasko59892dd2022-05-13 11:02:30 +02001344 /* always clear the default flag */
1345 if (term->flags & LYD_DEFAULT) {
1346 for (parent = term; parent; parent = lyd_parent(parent)) {
1347 parent->flags &= ~LYD_DEFAULT;
1348 }
aPiecek2e1f68f2023-11-08 11:27:33 +01001349 /* make the node non-validated */
1350 term->flags &= LYD_NEW;
Michal Vasko59892dd2022-05-13 11:02:30 +02001351 dflt_change = 1;
1352 } else {
1353 dflt_change = 0;
1354 }
1355
aPiecek2e1f68f2023-11-08 11:27:33 +01001356 /* return value */
Michal Vasko59892dd2022-05-13 11:02:30 +02001357 if (!val_change) {
1358 if (dflt_change) {
1359 /* only default flag change */
1360 ret = LY_EEXIST;
1361 } else {
1362 /* no change */
1363 ret = LY_ENOT;
1364 }
1365 } /* else value changed, LY_SUCCESS */
1366
1367cleanup:
1368 return ret;
1369}
1370
1371LIBYANG_API_DEF LY_ERR
1372lyd_change_term(struct lyd_node *term, const char *val_str)
1373{
1374 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1375
1376 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1377}
1378
1379LIBYANG_API_DEF LY_ERR
1380lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1381{
1382 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1383
1384 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1385}
1386
1387LIBYANG_API_DEF LY_ERR
1388lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1389{
1390 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1391
1392 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1393}
1394
1395LIBYANG_API_DEF LY_ERR
1396lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1397{
1398 LY_ERR ret = LY_SUCCESS;
1399 struct lyd_meta *m2 = NULL;
1400 struct lyd_value val;
1401 ly_bool val_change;
1402
1403 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1404
1405 if (!val_str) {
1406 val_str = "";
1407 }
1408
1409 /* parse the new value into a new meta structure */
1410 ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
stewegd4cde642024-02-21 08:34:16 +01001411 0, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001412 LY_CHECK_GOTO(ret, cleanup);
1413
1414 /* compare original and new value */
1415 if (lyd_compare_meta(meta, m2)) {
1416 /* values differ, switch them */
1417 val = meta->value;
1418 meta->value = m2->value;
1419 m2->value = val;
1420 val_change = 1;
1421 } else {
1422 val_change = 0;
1423 }
1424
1425 /* retrun value */
1426 if (!val_change) {
1427 /* no change */
1428 ret = LY_ENOT;
1429 } /* else value changed, LY_SUCCESS */
1430
1431cleanup:
1432 lyd_free_meta_single(m2);
1433 return ret;
1434}
1435
1436/**
1437 * @brief Update node value.
1438 *
1439 * @param[in] node Node to update.
1440 * @param[in] value New value to set.
1441 * @param[in] value_len Length of @p value.
1442 * @param[in] value_type Type of @p value for anydata/anyxml node.
1443 * @param[in] format Format of @p value.
1444 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1445 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1446 * @return LY_ERR value.
1447 */
1448static LY_ERR
1449lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1450 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1451{
1452 LY_ERR ret = LY_SUCCESS;
1453 struct lyd_node *new_any;
1454
1455 switch (node->schema->nodetype) {
1456 case LYS_CONTAINER:
1457 case LYS_NOTIF:
1458 case LYS_RPC:
1459 case LYS_ACTION:
1460 case LYS_LIST:
1461 /* if it exists, there is nothing to update */
1462 *new_parent = NULL;
1463 *new_node = NULL;
1464 break;
1465 case LYS_LEAFLIST:
1466 if (!lysc_is_dup_inst_list(node->schema)) {
1467 /* if it exists, there is nothing to update */
1468 *new_parent = NULL;
1469 *new_node = NULL;
1470 break;
1471 }
1472 /* fallthrough */
1473 case LYS_LEAF:
1474 ret = _lyd_change_term(node, value, value_len, format);
1475 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1476 /* there was an actual change (at least of the default flag) */
1477 *new_parent = node;
1478 *new_node = node;
1479 ret = LY_SUCCESS;
1480 } else if (ret == LY_ENOT) {
1481 /* no change */
1482 *new_parent = NULL;
1483 *new_node = NULL;
1484 ret = LY_SUCCESS;
1485 } /* else error */
1486 break;
1487 case LYS_ANYDATA:
1488 case LYS_ANYXML:
1489 /* create a new any node */
1490 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1491
1492 /* compare with the existing one */
1493 if (lyd_compare_single(node, new_any, 0)) {
1494 /* not equal, switch values (so that we can use generic node free) */
1495 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1496 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1497 ((struct lyd_node_any *)node)->value.str = value;
1498 ((struct lyd_node_any *)node)->value_type = value_type;
1499
1500 *new_parent = node;
1501 *new_node = node;
1502 } else {
1503 /* they are equal */
1504 *new_parent = NULL;
1505 *new_node = NULL;
1506 }
1507 lyd_free_tree(new_any);
1508 break;
1509 default:
1510 LOGINT(LYD_CTX(node));
1511 ret = LY_EINT;
1512 break;
1513 }
1514
1515 return ret;
1516}
1517
1518static LY_ERR
1519lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1520 LY_VALUE_FORMAT format, uint32_t options)
1521{
1522 LY_ERR r;
1523 struct ly_path_predicate *pred;
1524 struct lyd_value val;
1525 const struct lysc_node *schema = NULL;
1526 LY_ARRAY_COUNT_TYPE u, new_count;
1527 int create = 0;
1528
1529 assert(path);
1530
1531 /* go through all the compiled nodes */
1532 LY_ARRAY_FOR(path, u) {
1533 schema = path[u].node;
1534
1535 if (lysc_is_dup_inst_list(schema)) {
Michal Vasko5de21542023-03-20 10:00:05 +01001536 if (!path[u].predicates ||
1537 ((schema->nodetype == LYS_LEAFLIST) && (path[u].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001538 /* creating a new key-less list or state leaf-list instance */
1539 create = 1;
1540 new_count = u;
Michal Vasko90189962023-02-28 12:10:34 +01001541 } else if (path[u].predicates[0].type != LY_PATH_PREDTYPE_POSITION) {
Michal Vasko7a266772024-01-23 11:02:38 +01001542 LOG_LOCSET(schema, NULL);
Michal Vasko5de21542023-03-20 10:00:05 +01001543 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for state %s \"%s\" in path \"%s\".",
Michal Vasko59892dd2022-05-13 11:02:30 +02001544 lys_nodetype2str(schema->nodetype), schema->name, str_path);
Michal Vasko7a266772024-01-23 11:02:38 +01001545 LOG_LOCBACK(1, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +02001546 return LY_EINVAL;
1547 }
Michal Vasko90189962023-02-28 12:10:34 +01001548 } else if ((schema->nodetype == LYS_LIST) &&
1549 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001550 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko7a266772024-01-23 11:02:38 +01001551 LOG_LOCSET(schema, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001552 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1553 lys_nodetype2str(schema->nodetype), schema->name, str_path);
Michal Vasko7a266772024-01-23 11:02:38 +01001554 LOG_LOCBACK(1, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +02001555 return LY_EINVAL;
1556 } /* else creating an opaque list */
Michal Vasko90189962023-02-28 12:10:34 +01001557 } else if ((schema->nodetype == LYS_LEAFLIST) &&
1558 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001559 r = LY_SUCCESS;
1560 if (options & LYD_NEW_PATH_OPAQ) {
1561 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
1562 }
1563 if (!r) {
1564 /* try to store the value */
1565 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
stewegd4cde642024-02-21 08:34:16 +01001566 value, value_len, 0, 0, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
Michal Vasko59892dd2022-05-13 11:02:30 +02001567 ++((struct lysc_type *)val.realtype)->refcount;
1568
1569 /* store the new predicate so that it is used when searching for this instance */
Michal Vasko59892dd2022-05-13 11:02:30 +02001570 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vasko90189962023-02-28 12:10:34 +01001571 pred->type = LY_PATH_PREDTYPE_LEAFLIST;
Michal Vasko59892dd2022-05-13 11:02:30 +02001572 pred->value = val;
1573 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1574 }
1575 }
1576
1577 if (create) {
1578 /* hide the nodes that should always be created so they are not found */
1579 while (new_count < LY_ARRAY_COUNT(path)) {
1580 LY_ARRAY_DECREMENT(path);
1581 }
1582 }
1583
1584 return LY_SUCCESS;
1585}
1586
1587/**
1588 * @brief Create a new node in the data tree based on a path. All node types can be created.
1589 *
1590 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1591 * Also, if a leaf-list is being created and both a predicate is defined in @p path
1592 * and @p value is set, the predicate is preferred.
1593 *
1594 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1595 * nodes, they are always created.
1596 *
1597 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1598 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1599 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1600 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1601 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1602 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1603 * searching for the appropriate module.
1604 * @param[in] path [Path](@ref howtoXPath) to create.
1605 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1606 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1607 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1608 * creating anyxml/anydata nodes.
1609 * @param[in] value_type Anyxml/anydata node @p value type.
1610 * @param[in] options Bitmask of options, see @ref pathoptions.
1611 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1612 * @param[out] new_node Optional last node created.
1613 * @return LY_ERR value.
1614 */
1615static LY_ERR
1616lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
1617 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
1618 struct lyd_node **new_parent, struct lyd_node **new_node)
1619{
1620 LY_ERR ret = LY_SUCCESS, r;
1621 struct lyxp_expr *exp = NULL;
1622 struct ly_path *p = NULL;
1623 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1624 const struct lysc_node *schema;
1625 const struct lyd_value *val = NULL;
stewegd4cde642024-02-21 08:34:16 +01001626 ly_bool store_only = (options & LYD_NEW_VAL_STORE_ONLY) ? 1 : 0;
Michal Vasko59892dd2022-05-13 11:02:30 +02001627 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
1628 LY_VALUE_FORMAT format;
1629
1630 assert(parent || ctx);
1631 assert(path && ((path[0] == '/') || parent));
Michal Vasko59892dd2022-05-13 11:02:30 +02001632
1633 if (!ctx) {
1634 ctx = LYD_CTX(parent);
1635 }
1636 if (value && !value_len) {
1637 value_len = strlen(value);
1638 }
Michal Vasko8760d962024-02-27 09:17:07 +01001639 LY_CHECK_GOTO(ret = lyd_new_val_get_format(options, &format), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001640
1641 /* parse path */
Michal Vasko074d9a12023-06-22 08:23:41 +02001642 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST,
Michal Vasko59892dd2022-05-13 11:02:30 +02001643 LY_PATH_PRED_SIMPLE, &exp), cleanup);
1644
1645 /* compile path */
stewegd4cde642024-02-21 08:34:16 +01001646 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_VAL_OUTPUT ?
Michal Vasko59892dd2022-05-13 11:02:30 +02001647 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
1648
1649 /* check the compiled path before searching existing nodes, it may be shortened */
1650 orig_count = LY_ARRAY_COUNT(p);
1651 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup);
1652
1653 /* try to find any existing nodes in the path */
1654 if (parent) {
Michal Vasko838829d2023-10-09 16:06:43 +02001655 r = ly_path_eval_partial(p, parent, NULL, options & LYD_NEW_PATH_WITH_OPAQ, &path_idx, &node);
Michal Vaskof2560512023-03-03 09:54:47 +01001656 if (r == LY_SUCCESS) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001657 if (orig_count == LY_ARRAY_COUNT(p)) {
1658 /* the node exists, are we supposed to update it or is it just a default? */
1659 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
Michal Vasko7a266772024-01-23 11:02:38 +01001660 LOG_LOCSET(NULL, node);
Michal Vaskoa9764922023-02-09 13:59:07 +01001661 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists.", path);
Michal Vasko7a266772024-01-23 11:02:38 +01001662 LOG_LOCBACK(0, 1);
Michal Vasko59892dd2022-05-13 11:02:30 +02001663 ret = LY_EEXIST;
1664 goto cleanup;
Michal Vaskofabe9b92023-10-23 13:45:27 +02001665 } else if ((options & LYD_NEW_PATH_UPDATE) && lysc_is_key(node->schema)) {
1666 /* fine, the key value must not be changed and has to be in the predicate to be found */
1667 goto cleanup;
Michal Vasko59892dd2022-05-13 11:02:30 +02001668 }
1669
1670 /* update the existing node */
1671 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
1672 goto cleanup;
1673 } /* else we were not searching for the whole path */
Michal Vaskof2560512023-03-03 09:54:47 +01001674 } else if (r == LY_EINCOMPLETE) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001675 /* some nodes were found, adjust the iterator to the next segment */
1676 ++path_idx;
Michal Vaskof2560512023-03-03 09:54:47 +01001677 } else if (r == LY_ENOTFOUND) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001678 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
1679 if (lysc_data_parent(p[0].node)) {
1680 node = parent;
1681 }
1682 } else {
1683 /* error */
Michal Vaskof2560512023-03-03 09:54:47 +01001684 ret = r;
Michal Vasko59892dd2022-05-13 11:02:30 +02001685 goto cleanup;
1686 }
1687 }
1688
1689 /* restore the full path for creating new nodes */
1690 while (orig_count > LY_ARRAY_COUNT(p)) {
1691 LY_ARRAY_INCREMENT(p);
1692 }
1693
1694 /* create all the non-existing nodes in a loop */
1695 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
1696 cur_parent = node;
1697 schema = p[path_idx].node;
1698
1699 switch (schema->nodetype) {
1700 case LYS_LIST:
1701 if (lysc_is_dup_inst_list(schema)) {
1702 /* create key-less list instance */
1703 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
Michal Vasko90189962023-02-28 12:10:34 +01001704 } else if ((options & LYD_NEW_PATH_OPAQ) && !p[path_idx].predicates) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001705 /* creating opaque list without keys */
1706 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1707 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
1708 LYD_NODEHINT_LIST, &node), cleanup);
1709 } else {
1710 /* create standard list instance */
stewegd4cde642024-02-21 08:34:16 +01001711 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, NULL, store_only, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001712 }
1713 break;
1714 case LYS_CONTAINER:
1715 case LYS_NOTIF:
1716 case LYS_RPC:
1717 case LYS_ACTION:
1718 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1719 break;
1720 case LYS_LEAFLIST:
Michal Vasko90189962023-02-28 12:10:34 +01001721 if ((options & LYD_NEW_PATH_OPAQ) &&
1722 (!p[path_idx].predicates || (p[path_idx].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001723 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1724 r = LY_EVALID;
1725 if (lysc_is_dup_inst_list(schema)) {
1726 /* validate value */
1727 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1728 }
1729 if (r && (r != LY_EINCOMPLETE)) {
1730 /* creating opaque leaf-list */
lucolivaeef8dae2024-03-04 11:12:26 +01001731 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1732 schema->module->name, strlen(schema->module->name), value, value_len, NULL, format, NULL,
Michal Vasko59892dd2022-05-13 11:02:30 +02001733 LYD_NODEHINT_LEAFLIST, &node), cleanup);
1734 break;
1735 }
1736 }
1737
1738 /* get value to set */
Michal Vasko90189962023-02-28 12:10:34 +01001739 if (p[path_idx].predicates && (p[path_idx].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001740 val = &p[path_idx].predicates[0].value;
1741 }
1742
1743 /* create a leaf-list instance */
1744 if (val) {
Michal Vasko90189962023-02-28 12:10:34 +01001745 LY_CHECK_GOTO(ret = lyd_create_term2(schema, val, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001746 } else {
stewegd4cde642024-02-21 08:34:16 +01001747 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, 0, store_only, NULL, format, NULL, LYD_HINT_DATA, NULL, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001748 }
1749 break;
1750 case LYS_LEAF:
1751 if (lysc_is_key(schema) && cur_parent->schema) {
1752 /* it must have been already created or some error will occur later */
1753 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1754 assert(node);
1755 goto next_iter;
1756 }
1757
1758 if (options & LYD_NEW_PATH_OPAQ) {
1759 if (cur_parent && !cur_parent->schema) {
1760 /* always create opaque nodes for opaque parents */
1761 r = LY_ENOT;
1762 } else {
1763 /* validate value */
1764 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1765 }
1766 if (r && (r != LY_EINCOMPLETE)) {
1767 /* creating opaque leaf */
lucolivaeef8dae2024-03-04 11:12:26 +01001768 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1769 schema->module->name, strlen(schema->module->name), value, value_len, NULL, format, NULL, 0, &node),
Michal Vasko59892dd2022-05-13 11:02:30 +02001770 cleanup);
1771 break;
1772 }
1773 }
1774
1775 /* create a leaf instance */
stewegd4cde642024-02-21 08:34:16 +01001776 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, 0, store_only, NULL, format, NULL, LYD_HINT_DATA, NULL, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001777 break;
1778 case LYS_ANYDATA:
1779 case LYS_ANYXML:
1780 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
1781 break;
1782 default:
1783 LOGINT(ctx);
1784 ret = LY_EINT;
1785 goto cleanup;
1786 }
1787
1788 if (p[path_idx].ext) {
1789 node->flags |= LYD_EXT;
1790 }
1791 if (cur_parent) {
1792 /* connect to the parent */
aPiecek1462ab12024-02-07 09:13:29 +01001793 lyd_insert_node(cur_parent, NULL, node, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +02001794 } else if (parent) {
1795 /* connect to top-level siblings */
aPiecek1462ab12024-02-07 09:13:29 +01001796 lyd_insert_node(NULL, &parent, node, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +02001797 }
1798
1799next_iter:
1800 /* update remembered nodes */
1801 if (!nparent) {
1802 nparent = node;
1803 }
1804 nnode = node;
1805 }
1806
1807cleanup:
1808 lyxp_expr_free(ctx, exp);
1809 if (p) {
1810 while (orig_count > LY_ARRAY_COUNT(p)) {
1811 LY_ARRAY_INCREMENT(p);
1812 }
1813 }
1814 ly_path_free(ctx, p);
1815 if (!ret) {
1816 /* set out params only on success */
1817 if (new_parent) {
1818 *new_parent = nparent;
1819 }
1820 if (new_node) {
1821 *new_node = nnode;
1822 }
1823 } else {
1824 lyd_free_tree(nparent);
1825 }
1826 return ret;
1827}
1828
1829LIBYANG_API_DEF LY_ERR
1830lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1831 struct lyd_node **node)
1832{
1833 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
Michal Vasko6a027db2024-02-21 09:55:34 +01001834 !(options & LYD_NEW_VAL_BIN) || !(options & LYD_NEW_VAL_CANON), LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001835 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1836
1837 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1838}
1839
1840LIBYANG_API_DEF LY_ERR
1841lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1842 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
1843 struct lyd_node **new_node)
1844{
1845 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
Michal Vasko6a027db2024-02-21 09:55:34 +01001846 !(options & LYD_NEW_VAL_BIN) || !(options & LYD_NEW_VAL_CANON), LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001847 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1848
1849 return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node);
1850}
1851
1852LIBYANG_API_DEF LY_ERR
1853lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1854 uint32_t options, struct lyd_node **node)
1855{
1856 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1857
1858 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
Michal Vasko6a027db2024-02-21 09:55:34 +01001859 !(options & LYD_NEW_VAL_BIN) || !(options & LYD_NEW_VAL_CANON), LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001860 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1861
1862 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1863}
1864
1865LY_ERR
1866lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001867 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node,
1868 uint32_t impl_opts, struct lyd_node **diff)
Michal Vasko59892dd2022-05-13 11:02:30 +02001869{
1870 LY_ERR ret;
1871 const struct lysc_node *iter = NULL;
1872 struct lyd_node *node = NULL;
1873 struct lyd_value **dflts;
1874 LY_ARRAY_COUNT_TYPE u;
1875 uint32_t getnext_opts;
1876
1877 assert(first && (parent || sparent || mod));
1878
1879 if (!sparent && parent) {
1880 sparent = parent->schema;
1881 }
1882
1883 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1884 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1885 getnext_opts |= LYS_GETNEXT_OUTPUT;
1886 }
1887
1888 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
1889 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1890 continue;
1891 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1892 continue;
1893 }
1894
1895 switch (iter->nodetype) {
1896 case LYS_CHOICE:
1897 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1898 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
1899 /* create default case data */
1900 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001901 NULL, node_when, node_types, ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001902 } else if (node) {
1903 /* create any default data in the existing case */
1904 assert(node->schema->parent->nodetype == LYS_CASE);
1905 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001906 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001907 }
1908 break;
1909 case LYS_CONTAINER:
1910 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1911 /* create default NP container */
1912 LY_CHECK_RET(lyd_create_inner(iter, &node));
1913 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
aPiecek1462ab12024-02-07 09:13:29 +01001914 lyd_insert_node(parent, first, node, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +02001915
1916 if (lysc_has_when(iter) && node_when) {
1917 /* remember to resolve when */
1918 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1919 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001920 if (ext_node) {
1921 /* store for ext instance node validation, if needed */
1922 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1923 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001924 if (diff) {
1925 /* add into diff */
1926 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1927 }
1928
1929 /* create any default children */
1930 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001931 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001932 }
1933 break;
1934 case LYS_LEAF:
1935 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1936 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1937 /* create default leaf */
1938 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1939 if (ret == LY_EINCOMPLETE) {
1940 if (node_types) {
1941 /* remember to resolve type */
1942 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
1943 }
1944 } else if (ret) {
1945 return ret;
1946 }
1947 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
aPiecek1462ab12024-02-07 09:13:29 +01001948 lyd_insert_node(parent, first, node, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +02001949
1950 if (lysc_has_when(iter) && node_when) {
1951 /* remember to resolve when */
1952 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1953 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001954 if (ext_node) {
1955 /* store for ext instance node validation, if needed */
1956 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1957 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001958 if (diff) {
1959 /* add into diff */
1960 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1961 }
1962 }
1963 break;
1964 case LYS_LEAFLIST:
1965 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1966 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1967 /* create all default leaf-lists */
1968 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1969 LY_ARRAY_FOR(dflts, u) {
1970 ret = lyd_create_term2(iter, dflts[u], &node);
1971 if (ret == LY_EINCOMPLETE) {
1972 if (node_types) {
1973 /* remember to resolve type */
1974 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
1975 }
1976 } else if (ret) {
1977 return ret;
1978 }
1979 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
aPiecek1462ab12024-02-07 09:13:29 +01001980 lyd_insert_node(parent, first, node, LYD_INSERT_NODE_DEFAULT);
Michal Vasko59892dd2022-05-13 11:02:30 +02001981
1982 if (lysc_has_when(iter) && node_when) {
1983 /* remember to resolve when */
1984 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1985 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001986 if (ext_node) {
1987 /* store for ext instance node validation, if needed */
1988 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1989 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001990 if (diff) {
1991 /* add into diff */
1992 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1993 }
1994 }
1995 }
1996 break;
1997 default:
1998 /* without defaults */
1999 break;
2000 }
2001 }
2002
2003 return LY_SUCCESS;
2004}
2005
2006LIBYANG_API_DEF LY_ERR
2007lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
2008{
2009 LY_ERR ret = LY_SUCCESS;
2010 struct lyd_node *node;
2011 struct ly_set node_when = {0};
2012
2013 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2014 if (diff) {
2015 *diff = NULL;
2016 }
2017
2018 LYD_TREE_DFS_BEGIN(tree, node) {
steweg0e1e5092024-02-12 09:06:04 +01002019 if (node->schema && (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02002020 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02002021 NULL, implicit_options, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002022 }
2023
2024 LYD_TREE_DFS_END(tree, node);
2025 }
2026
2027 /* resolve when and remove any invalid defaults */
Michal Vasko135719f2022-08-25 12:18:17 +02002028 ret = lyd_validate_unres(&tree, NULL, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL, 0, diff);
Michal Vaskofbbea932022-06-07 11:00:55 +02002029 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002030
2031cleanup:
2032 ly_set_erase(&node_when, NULL);
2033 if (ret && diff) {
2034 lyd_free_all(*diff);
2035 *diff = NULL;
2036 }
2037 return ret;
2038}
2039
2040LIBYANG_API_DEF LY_ERR
2041lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
2042{
2043 const struct lys_module *mod;
2044 struct lyd_node *d = NULL;
2045 uint32_t i = 0;
2046 LY_ERR ret = LY_SUCCESS;
2047
2048 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2049 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
2050 if (diff) {
2051 *diff = NULL;
2052 }
2053 if (!ctx) {
2054 ctx = LYD_CTX(*tree);
2055 }
2056
2057 /* add nodes for each module one-by-one */
2058 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2059 if (!mod->implemented) {
2060 continue;
2061 }
2062
2063 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2064 if (d) {
2065 /* merge into one diff */
2066 lyd_insert_sibling(*diff, d, diff);
2067
2068 d = NULL;
2069 }
2070 }
2071
2072cleanup:
2073 if (ret && diff) {
2074 lyd_free_all(*diff);
2075 *diff = NULL;
2076 }
2077 return ret;
2078}
2079
2080LIBYANG_API_DEF LY_ERR
2081lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
2082 struct lyd_node **diff)
2083{
2084 LY_ERR ret = LY_SUCCESS;
2085 struct lyd_node *root, *d = NULL;
2086 struct ly_set node_when = {0};
2087
2088 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2089 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
2090 if (diff) {
2091 *diff = NULL;
2092 }
2093
2094 /* add all top-level defaults for this module */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02002095 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, NULL, implicit_options, diff),
2096 cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002097
2098 /* resolve when and remove any invalid defaults */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02002099 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL,
2100 0, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002101
2102 /* process nested nodes */
2103 LY_LIST_FOR(*tree, root) {
Michal Vaskof87fbdf2023-07-11 10:20:02 +02002104 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002105
Michal Vaskof87fbdf2023-07-11 10:20:02 +02002106 if (d) {
2107 /* merge into one diff */
2108 lyd_insert_sibling(*diff, d, diff);
2109 d = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +02002110 }
2111 }
2112
2113cleanup:
2114 ly_set_erase(&node_when, NULL);
2115 if (ret && diff) {
2116 lyd_free_all(*diff);
2117 *diff = NULL;
2118 }
2119 return ret;
2120}