blob: 289d2dabf2ee314f50658c61cc54991d178e6bac [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 *
7 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#define _GNU_SOURCE
17
18#include <assert.h>
19#include <ctype.h>
20#include <inttypes.h>
21#include <stdarg.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
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
Michal Vasko989cdb42023-10-06 15:32:37 +020055lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool is_utf8, ly_bool *dynamic,
Michal Vasko59892dd2022-05-13 11:02:30 +020056 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
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 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,
Michal Vasko989cdb42023-10-06 15:32:37 +020072 value_len, is_utf8, 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,
139 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,
Michal Vasko989cdb42023-10-06 15:32:37 +0200166 var->value, strlen(var->value), 0, 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);
Michal Vasko59892dd2022-05-13 11:02:30 +0200182 lyd_insert_node(list, NULL, key, 0);
183 }
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
199lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
200{
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 */
Michal Vasko90189962023-02-28 12:10:34 +0100217 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, NULL, 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
474LIBYANG_API_DEF LY_ERR
475lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
476 struct lyd_node **node)
477{
478 LY_ERR r;
479 struct lyd_node *ret = NULL;
480 const struct lysc_node *schema;
481 struct lysc_ext_instance *ext = NULL;
482 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
483
484 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
485 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
486
487 if (!module) {
488 module = parent->schema->module;
489 }
490
491 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
492 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
493 if (!schema && parent) {
494 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
495 strlen(name), &schema, &ext);
496 LY_CHECK_RET(r && (r != LY_ENOT), r);
497 }
498 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.",
499 name), LY_ENOTFOUND);
500
501 LY_CHECK_RET(lyd_create_inner(schema, &ret));
502 if (ext) {
503 ret->flags |= LYD_EXT;
504 }
505 if (parent) {
506 lyd_insert_node(parent, NULL, ret, 0);
507 }
508
509 if (node) {
510 *node = ret;
511 }
512 return LY_SUCCESS;
513}
514
515LIBYANG_API_DEF LY_ERR
516lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
517{
518 struct lyd_node *ret = NULL;
519 const struct lysc_node *schema;
520 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
521
522 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
523
524 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
525 if (!schema) {
526 if (ext->argument) {
527 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
528 name, ext->argument, ext->def->name);
529 } else {
530 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
531 name, ext->def->name);
532 }
533 return LY_ENOTFOUND;
534 }
535 LY_CHECK_RET(lyd_create_inner(schema, &ret));
536
537 *node = ret;
538
539 return LY_SUCCESS;
540}
541
542/**
Michal Vasko2c1e3272023-10-17 14:08:35 +0200543 * @brief Create a new lits node instance without its keys.
544 *
545 * @param[in] ctx Context to use for logging.
546 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
547 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
548 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
549 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
550 * taken into consideration. Otherwise, the output's data node is going to be created.
551 * @param[out] node Created node.
552 * @return LY_ERR value.
553 */
554static LY_ERR
555_lyd_new_list_node(const struct ly_ctx *ctx, const struct lyd_node *parent, const struct lys_module *module,
556 const char *name, ly_bool output, struct lyd_node **node)
557{
558 struct lyd_node *ret = NULL;
559 const struct lysc_node *schema;
560 struct lysc_ext_instance *ext = NULL;
561 LY_ERR r;
562
563 if (!module) {
564 module = parent->schema->module;
565 }
566
567 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
568 if (!schema && parent) {
569 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
570 strlen(name), &schema, &ext);
571 LY_CHECK_RET(r && (r != LY_ENOT), r);
572 }
573 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
574
575 /* create list inner node */
576 LY_CHECK_RET(lyd_create_inner(schema, &ret));
577
578 if (ext) {
579 ret->flags |= LYD_EXT;
580 }
581
582 *node = ret;
583 return LY_SUCCESS;
584}
585
586/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200587 * @brief Create a new list node in the data tree.
588 *
589 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
590 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
591 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
592 * @param[in] format Format of key values.
593 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
594 * taken into consideration. Otherwise, the output's data node is going to be created.
595 * @param[out] node Optional created node.
596 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
597 * be followed by the value length.
598 * @return LY_ERR value.
599 */
600static LY_ERR
601_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
602 ly_bool output, struct lyd_node **node, va_list ap)
603{
604 struct lyd_node *ret = NULL, *key;
Michal Vasko2c1e3272023-10-17 14:08:35 +0200605 const struct lysc_node *key_s;
Michal Vasko59892dd2022-05-13 11:02:30 +0200606 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
607 const void *key_val;
608 uint32_t key_len;
Michal Vasko2c1e3272023-10-17 14:08:35 +0200609 LY_ERR rc = LY_SUCCESS;
Michal Vasko59892dd2022-05-13 11:02:30 +0200610
611 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
612 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
613
Michal Vasko2c1e3272023-10-17 14:08:35 +0200614 /* create the list node */
615 LY_CHECK_RET(_lyd_new_list_node(ctx, parent, module, name, output, &ret));
Michal Vasko59892dd2022-05-13 11:02:30 +0200616
617 /* create and insert all the keys */
Michal Vasko2c1e3272023-10-17 14:08:35 +0200618 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 +0200619 if (format == LY_VALUE_LYB) {
620 key_val = va_arg(ap, const void *);
621 key_len = va_arg(ap, uint32_t);
622 } else {
623 key_val = va_arg(ap, const char *);
624 key_len = key_val ? strlen((char *)key_val) : 0;
625 }
626
Michal Vasko989cdb42023-10-06 15:32:37 +0200627 rc = lyd_create_term(key_s, key_val, key_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
Michal Vasko59892dd2022-05-13 11:02:30 +0200628 LY_CHECK_GOTO(rc, cleanup);
629 lyd_insert_node(ret, NULL, key, 1);
630 }
631
Michal Vasko59892dd2022-05-13 11:02:30 +0200632 if (parent) {
633 lyd_insert_node(parent, NULL, ret, 0);
634 }
635
636cleanup:
637 if (rc) {
638 lyd_free_tree(ret);
639 ret = NULL;
640 } else if (node) {
641 *node = ret;
642 }
643 return rc;
644}
645
646LIBYANG_API_DEF LY_ERR
647lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
648 struct lyd_node **node, ...)
649{
650 LY_ERR rc;
651 va_list ap;
652
653 va_start(ap, node);
654
655 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
656
657 va_end(ap);
658 return rc;
659}
660
661LIBYANG_API_DEF LY_ERR
662lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
663 struct lyd_node **node, ...)
664{
665 LY_ERR rc;
666 va_list ap;
667
668 va_start(ap, node);
669
670 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
671
672 va_end(ap);
673 return rc;
674}
675
676LIBYANG_API_DEF LY_ERR
677lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
678 struct lyd_node **node, ...)
679{
680 LY_ERR rc;
681 va_list ap;
682
683 va_start(ap, node);
684
685 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
686
687 va_end(ap);
688 return rc;
689}
690
691LIBYANG_API_DEF LY_ERR
692lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
693{
694 struct lyd_node *ret = NULL, *key;
695 const struct lysc_node *schema, *key_s;
696 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
697 va_list ap;
698 const char *key_val;
699 LY_ERR rc = LY_SUCCESS;
700
701 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
702
703 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
704 if (!schema) {
705 if (ext->argument) {
706 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
707 name, ext->argument, ext->def->name);
708 } else {
709 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
710 }
711 return LY_ENOTFOUND;
712 }
713 /* create list inner node */
714 LY_CHECK_RET(lyd_create_inner(schema, &ret));
715
716 va_start(ap, node);
717
718 /* create and insert all the keys */
719 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
720 key_val = va_arg(ap, const char *);
721
Michal Vasko989cdb42023-10-06 15:32:37 +0200722 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
Michal Vasko59892dd2022-05-13 11:02:30 +0200723 NULL, &key);
724 LY_CHECK_GOTO(rc, cleanup);
725 lyd_insert_node(ret, NULL, key, 1);
726 }
727
728cleanup:
729 va_end(ap);
730 if (rc) {
731 lyd_free_tree(ret);
732 ret = NULL;
733 }
734 *node = ret;
735 return rc;
736}
737
738LIBYANG_API_DEF LY_ERR
739lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
740 ly_bool output, struct lyd_node **node)
741{
742 LY_ERR r;
743 struct lyd_node *ret = NULL;
744 const struct lysc_node *schema;
745 struct lysc_ext_instance *ext = NULL;
746 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
747
748 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
749 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
750
751 if (!module) {
752 module = parent->schema->module;
753 }
754 if (!keys) {
755 keys = "";
756 }
757
758 /* find schema node */
759 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
760 if (!schema && parent) {
761 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name),
762 &schema, &ext);
763 LY_CHECK_RET(r && (r != LY_ENOT), r);
764 }
765 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
766
767 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
768 /* key-less list */
769 LY_CHECK_RET(lyd_create_inner(schema, &ret));
770 } else {
771 /* create the list node */
772 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
773 }
774 if (ext) {
775 ret->flags |= LYD_EXT;
776 }
777 if (parent) {
778 lyd_insert_node(parent, NULL, ret, 0);
779 }
780
781 if (node) {
782 *node = ret;
783 }
784 return LY_SUCCESS;
785}
786
787/**
Michal Vasko2c1e3272023-10-17 14:08:35 +0200788 * @brief Create a new list node in the data tree.
789 *
790 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
791 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
792 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
793 * @param[in] format Format of key values.
Michal Vaskobeeb8b62024-01-30 16:10:17 +0100794 * @param[in] key_values Ordered key values of the new list instance, all must be set.
Michal Vasko2c1e3272023-10-17 14:08:35 +0200795 * @param[in] value_lengths Lengths of @p key_values, required for ::LY_VALUE_LYB, optional otherwise.
796 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
797 * taken into consideration. Otherwise, the output's data node is going to be created.
798 * @param[out] node Optional created node.
799 * @return LY_ERR value.
800 */
801static LY_ERR
802_lyd_new_list3(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
803 const void **key_values, uint32_t *value_lengths, ly_bool output, struct lyd_node **node)
804{
805 struct lyd_node *ret = NULL, *key;
806 const struct lysc_node *key_s;
807 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
808 const void *key_val;
809 uint32_t key_len, i;
810 LY_ERR rc = LY_SUCCESS;
811
Michal Vaskobeeb8b62024-01-30 16:10:17 +0100812 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 +0200813 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
814
815 /* create the list node */
816 LY_CHECK_RET(_lyd_new_list_node(ctx, parent, module, name, output, &ret));
817
Michal Vaskobeeb8b62024-01-30 16:10:17 +0100818 if (!(ret->schema->flags & LYS_KEYLESS) && !key_values) {
819 LOGERR(ctx, LY_EINVAL, "Missing list \"%s\" keys.", LYD_NAME(ret));
820 rc = LY_EINVAL;
821 goto cleanup;
822 }
823
Michal Vasko2c1e3272023-10-17 14:08:35 +0200824 /* create and insert all the keys */
825 i = 0;
826 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 +0100827 key_val = key_values[i] ? key_values[i] : "";
828 key_len = value_lengths ? value_lengths[i] : strlen(key_val);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200829
830 rc = lyd_create_term(key_s, key_val, key_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
831 LY_CHECK_GOTO(rc, cleanup);
832 lyd_insert_node(ret, NULL, key, 1);
833 }
834
835 if (parent) {
836 lyd_insert_node(parent, NULL, ret, 0);
837 }
838
839cleanup:
840 if (rc) {
841 lyd_free_tree(ret);
842 ret = NULL;
843 } else if (node) {
844 *node = ret;
845 }
846 return rc;
847}
848
849LIBYANG_API_DEF LY_ERR
850lyd_new_list3(struct lyd_node *parent, const struct lys_module *module, const char *name, const char **key_values,
851 uint32_t *value_lengths, ly_bool output, struct lyd_node **node)
852{
853 return _lyd_new_list3(parent, module, name, LY_VALUE_JSON, (const void **)key_values, value_lengths, output, node);
854}
855
856LIBYANG_API_DEF LY_ERR
857lyd_new_list3_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void **key_values,
858 uint32_t *value_lengths, ly_bool output, struct lyd_node **node)
859{
860 return _lyd_new_list3(parent, module, name, LY_VALUE_LYB, key_values, value_lengths, output, node);
861}
862
863LIBYANG_API_DEF LY_ERR
864lyd_new_list3_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char **key_values,
865 uint32_t *value_lengths, ly_bool output, struct lyd_node **node)
866{
867 return _lyd_new_list3(parent, module, name, LY_VALUE_CANON, (const void **)key_values, value_lengths, output, node);
868}
869
870/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200871 * @brief Create a new term node in the data tree.
872 *
873 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
874 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
875 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
876 * @param[in] value Value of the node being created.
877 * @param[in] value_len Length of @p value.
878 * @param[in] format Format of @p value.
879 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
880 * taken into consideration. Otherwise, the output's data node is going to be created.
881 * @param[out] node Optional created node.
882 * @return LY_ERR value.
883 */
884static LY_ERR
885_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
886 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
887{
888 LY_ERR r;
889 struct lyd_node *ret = NULL;
890 const struct lysc_node *schema;
891 struct lysc_ext_instance *ext = NULL;
892 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
893
894 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
895 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
896
897 if (!module) {
898 module = parent->schema->module;
899 }
900
901 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
902 if (!schema && parent) {
903 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
904 strlen(name), &schema, &ext);
905 LY_CHECK_RET(r && (r != LY_ENOT), r);
906 }
907 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
908
Michal Vasko989cdb42023-10-06 15:32:37 +0200909 LY_CHECK_RET(lyd_create_term(schema, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret));
Michal Vasko59892dd2022-05-13 11:02:30 +0200910 if (ext) {
911 ret->flags |= LYD_EXT;
912 }
913 if (parent) {
914 lyd_insert_node(parent, NULL, ret, 0);
915 }
916
917 if (node) {
918 *node = ret;
919 }
920 return LY_SUCCESS;
921}
922
923LIBYANG_API_DEF LY_ERR
924lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
925 ly_bool output, struct lyd_node **node)
926{
927 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
928}
929
930LIBYANG_API_DEF LY_ERR
931lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
932 size_t value_len, ly_bool output, struct lyd_node **node)
933{
934 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
935}
936
937LIBYANG_API_DEF LY_ERR
938lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
939 ly_bool output, struct lyd_node **node)
940{
941 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
942}
943
944LIBYANG_API_DEF LY_ERR
945lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
946{
947 LY_ERR rc;
948 struct lyd_node *ret = NULL;
949 const struct lysc_node *schema;
950 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
951
952 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
953
954 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
955 if (!schema) {
956 if (ext->argument) {
957 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
958 name, ext->argument, ext->def->name);
959 } else {
960 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
961 }
962 return LY_ENOTFOUND;
963 }
Michal Vasko989cdb42023-10-06 15:32:37 +0200964 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
965 NULL, &ret);
Michal Vasko59892dd2022-05-13 11:02:30 +0200966 LY_CHECK_RET(rc);
967
968 *node = ret;
969
970 return LY_SUCCESS;
971}
972
973LIBYANG_API_DEF LY_ERR
974lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
975 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
976{
977 LY_ERR r;
978 struct lyd_node *ret = NULL;
979 const struct lysc_node *schema;
980 struct lysc_ext_instance *ext = NULL;
981 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
982
Michal Vasko2ac91152023-11-21 10:29:18 +0100983 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name,
984 (value_type == LYD_ANYDATA_DATATREE) || (value_type == LYD_ANYDATA_STRING) || value, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200985 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
986
987 if (!module) {
988 module = parent->schema->module;
989 }
990
991 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
992 if (!schema && parent) {
993 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
994 strlen(name), &schema, &ext);
995 LY_CHECK_RET(r && (r != LY_ENOT), r);
996 }
997 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
998
999 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1000 if (ext) {
1001 ret->flags |= LYD_EXT;
1002 }
1003 if (parent) {
1004 lyd_insert_node(parent, NULL, ret, 0);
1005 }
1006
1007 if (node) {
1008 *node = ret;
1009 }
1010 return LY_SUCCESS;
1011}
1012
1013LIBYANG_API_DEF LY_ERR
1014lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1015 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1016{
1017 struct lyd_node *ret = NULL;
1018 const struct lysc_node *schema;
1019 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1020
1021 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1022
1023 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1024 if (!schema) {
1025 if (ext->argument) {
1026 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1027 name, ext->argument, ext->def->name);
1028 } else {
1029 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1030 }
1031 return LY_ENOTFOUND;
1032 }
1033 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1034
1035 *node = ret;
1036
1037 return LY_SUCCESS;
1038}
1039
1040LIBYANG_API_DEF LY_ERR
1041lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1042 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
1043{
1044 const char *prefix, *tmp;
1045 size_t pref_len, name_len;
1046
1047 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1048 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
1049 if (!ctx) {
Michal Vasko33c48972022-07-20 10:28:07 +02001050 ctx = module ? module->ctx : LYD_CTX(parent);
Michal Vasko59892dd2022-05-13 11:02:30 +02001051 }
1052
1053 if (parent && !parent->schema) {
1054 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
1055 return LY_EINVAL;
1056 }
1057 if (meta) {
1058 *meta = NULL;
1059 }
1060
1061 /* parse the name */
1062 tmp = name;
1063 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1064 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
1065 return LY_EINVAL;
1066 }
1067
1068 /* find the module */
1069 if (prefix) {
1070 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
1071 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND);
1072 }
1073
1074 /* set value if none */
1075 if (!val_str) {
1076 val_str = "";
1077 }
1078
Michal Vasko989cdb42023-10-06 15:32:37 +02001079 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), 0, NULL, LY_VALUE_JSON,
Michal Vasko59892dd2022-05-13 11:02:30 +02001080 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
1081}
1082
1083LIBYANG_API_DEF LY_ERR
1084lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1085 struct lyd_meta **meta)
1086{
1087 const struct lys_module *mod;
1088
1089 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1090 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1091
1092 if (parent && !parent->schema) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001093 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".",
1094 ((struct lyd_node_opaq *)parent)->name.name);
Michal Vasko59892dd2022-05-13 11:02:30 +02001095 return LY_EINVAL;
1096 }
1097 if (meta) {
1098 *meta = NULL;
1099 }
1100
1101 switch (attr->format) {
1102 case LY_VALUE_XML:
1103 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
1104 if (!mod) {
1105 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
1106 return LY_ENOTFOUND;
1107 }
1108 break;
1109 case LY_VALUE_JSON:
1110 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
1111 if (!mod) {
1112 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
1113 return LY_ENOTFOUND;
1114 }
1115 break;
1116 default:
1117 LOGINT_RET(ctx);
1118 }
1119
1120 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
Michal Vasko989cdb42023-10-06 15:32:37 +02001121 0, NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001122}
1123
1124LIBYANG_API_DEF LY_ERR
1125lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
1126 const char *prefix, const char *module_name, struct lyd_node **node)
1127{
1128 struct lyd_node *ret = NULL;
1129
1130 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
1131 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1132
1133 if (!ctx) {
1134 ctx = LYD_CTX(parent);
1135 }
1136 if (!value) {
1137 value = "";
1138 }
1139
1140 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1141 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
1142 if (parent) {
1143 lyd_insert_node(parent, NULL, ret, 1);
1144 }
1145
1146 if (node) {
1147 *node = ret;
1148 }
1149 return LY_SUCCESS;
1150}
1151
1152LIBYANG_API_DEF LY_ERR
1153lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
1154 const char *prefix, const char *module_ns, struct lyd_node **node)
1155{
1156 struct lyd_node *ret = NULL;
1157
1158 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1159 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1160
1161 if (!ctx) {
1162 ctx = LYD_CTX(parent);
1163 }
1164 if (!value) {
1165 value = "";
1166 }
1167
1168 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1169 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
1170 if (parent) {
1171 lyd_insert_node(parent, NULL, ret, 1);
1172 }
1173
1174 if (node) {
1175 *node = ret;
1176 }
1177 return LY_SUCCESS;
1178}
1179
1180LIBYANG_API_DEF LY_ERR
1181lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
1182 struct lyd_attr **attr)
1183{
1184 struct lyd_attr *ret = NULL;
1185 const struct ly_ctx *ctx;
1186 const char *prefix, *tmp;
1187 size_t pref_len, name_len, mod_len;
1188
1189 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1190
1191 ctx = LYD_CTX(parent);
1192
1193 /* parse the name */
1194 tmp = name;
1195 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1196 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1197 return LY_EVALID;
1198 }
1199
1200 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1201 /* not a prefix but special name */
1202 name = prefix;
1203 name_len += 1 + pref_len;
1204 prefix = NULL;
1205 pref_len = 0;
1206 }
1207
1208 /* get the module */
1209 if (module_name) {
1210 mod_len = strlen(module_name);
1211 } else {
1212 module_name = prefix;
1213 mod_len = pref_len;
1214 }
1215
1216 /* set value if none */
1217 if (!value) {
1218 value = "";
1219 }
1220
1221 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1222 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
1223
1224 if (attr) {
1225 *attr = ret;
1226 }
1227 return LY_SUCCESS;
1228}
1229
1230LIBYANG_API_DEF LY_ERR
1231lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1232 struct lyd_attr **attr)
1233{
1234 struct lyd_attr *ret = NULL;
1235 const struct ly_ctx *ctx;
1236 const char *prefix, *tmp;
1237 size_t pref_len, name_len;
1238
1239 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1240
1241 ctx = LYD_CTX(parent);
1242
1243 /* parse the name */
1244 tmp = name;
1245 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1246 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1247 return LY_EVALID;
1248 }
1249
1250 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1251 /* not a prefix but special name */
1252 name = prefix;
1253 name_len += 1 + pref_len;
1254 prefix = NULL;
1255 pref_len = 0;
1256 }
1257
1258 /* set value if none */
1259 if (!value) {
1260 value = "";
1261 }
Michal Vasko825718f2022-11-10 10:33:51 +01001262 if (strchr(value, ':')) {
1263 LOGWRN(ctx, "Value \"%s\" prefix will never be interpreted as an XML prefix.", value);
1264 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001265
1266 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1267 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
1268
1269 if (attr) {
1270 *attr = ret;
1271 }
1272 return LY_SUCCESS;
1273}
1274
1275/**
aPiecek6cf1d162023-11-08 16:07:00 +01001276 * @brief Change the value of @p term by @p val and reinsert the node if necessary.
aPiecek2e1f68f2023-11-08 11:27:33 +01001277 *
aPiecek6cf1d162023-11-08 16:07:00 +01001278 * Reinserting ensures that the node is in the correct position and the data instances remain properly ordered.
1279 *
1280 * @param[in] term Term node to change. If it is a key, the parental list is inserted again.
aPiecek2e1f68f2023-11-08 11:27:33 +01001281 * @param[in] val New value for @p term.
1282 * @return LY_SUCCESS on success.
1283 */
1284static LY_ERR
aPiecek6cf1d162023-11-08 16:07:00 +01001285lyd_change_node_value(struct lyd_node_term *term, struct lyd_value *val)
aPiecek2e1f68f2023-11-08 11:27:33 +01001286{
1287 LY_ERR ret = LY_SUCCESS;
aPiecek6cf1d162023-11-08 16:07:00 +01001288 struct lyd_node *target, *first;
aPiecek2e1f68f2023-11-08 11:27:33 +01001289
1290 if (term->schema->nodetype == LYS_LEAFLIST) {
1291 target = (struct lyd_node *)term;
1292 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1293 target = (struct lyd_node *)term->parent;
1294 } else {
1295 /* just change the value */
1296 term->value.realtype->plugin->free(LYD_CTX(term), &term->value);
1297 term->value = *val;
1298 /* leaf that is not a key, its value is not used for its hash so it does not change */
1299 return LY_SUCCESS;
1300 }
1301
aPiecek6cf1d162023-11-08 16:07:00 +01001302 if (!LYD_NODE_IS_ALONE(target) && lyds_is_supported(target)) {
1303 /* changing the value may cause a change in the order */
1304 first = lyd_first_sibling(target);
1305 first = first == target ? first->next : first;
1306 /* unlink hash and unlink the target node in the lyds tree */
1307 lyd_unlink_tree(target);
1308 /* change value */
1309 term->value.realtype->plugin->free(LYD_CTX(term), &term->value);
1310 term->value = *val;
1311 /* reinserting */
1312 lyd_insert_node(NULL, &first, target, 0);
1313 } else {
1314 /* unlink hash */
1315 lyd_unlink_hash(target);
1316 /* change value */
1317 term->value.realtype->plugin->free(LYD_CTX(term), &term->value);
1318 term->value = *val;
1319 }
aPiecek2e1f68f2023-11-08 11:27:33 +01001320 lyd_hash(target);
1321 ret = lyd_insert_hash(target);
1322
1323 return ret;
1324}
1325
1326/**
Michal Vasko59892dd2022-05-13 11:02:30 +02001327 * @brief Change the value of a term (leaf or leaf-list) node.
1328 *
1329 * Node changed this way is always considered explicitly set, meaning its default flag
1330 * is always cleared.
1331 *
1332 * @param[in] term Term node to change.
1333 * @param[in] value New value to set.
1334 * @param[in] value_len Length of @p value.
1335 * @param[in] format Format of @p value.
1336 * @return LY_SUCCESS if value was changed,
1337 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1338 * @return LY_ENOT if the values were equal and no change occured,
1339 * @return LY_ERR value on other errors.
1340 */
1341static LY_ERR
1342_lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format)
1343{
1344 LY_ERR ret = LY_SUCCESS;
1345 struct lysc_type *type;
1346 struct lyd_node_term *t;
1347 struct lyd_node *parent;
1348 struct lyd_value val;
1349 ly_bool dflt_change, val_change;
1350
1351 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
1352
1353 t = (struct lyd_node_term *)term;
1354 type = ((struct lysc_node_leaf *)term->schema)->type;
1355
1356 /* parse the new value */
Michal Vasko7a266772024-01-23 11:02:38 +01001357 LOG_LOCSET(term->schema, term);
Michal Vasko989cdb42023-10-06 15:32:37 +02001358 ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA,
1359 term->schema, NULL);
Michal Vasko7a266772024-01-23 11:02:38 +01001360 LOG_LOCBACK(1, 1);
Michal Vasko59892dd2022-05-13 11:02:30 +02001361 LY_CHECK_GOTO(ret, cleanup);
1362
1363 /* compare original and new value */
aPiecek0a6705b2023-11-14 14:20:58 +01001364 if (type->plugin->compare(LYD_CTX(term), &t->value, &val)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001365 /* values differ, switch them */
aPiecek6cf1d162023-11-08 16:07:00 +01001366 lyd_change_node_value(t, &val);
aPiecek2e1f68f2023-11-08 11:27:33 +01001367 /* make the node non-validated */
1368 term->flags &= LYD_NEW;
Michal Vasko59892dd2022-05-13 11:02:30 +02001369 val_change = 1;
1370 } else {
1371 /* same values, free the new stored one */
1372 type->plugin->free(LYD_CTX(term), &val);
1373 val_change = 0;
1374 }
1375
stewegf9041a22024-01-18 13:29:12 +01001376 /* clear links to leafref nodes */
1377 if (ly_ctx_get_options(LYD_CTX(term)) & LY_CTX_LEAFREF_LINKING) {
1378 lyd_free_leafref_nodes(t);
1379 }
1380
Michal Vasko59892dd2022-05-13 11:02:30 +02001381 /* always clear the default flag */
1382 if (term->flags & LYD_DEFAULT) {
1383 for (parent = term; parent; parent = lyd_parent(parent)) {
1384 parent->flags &= ~LYD_DEFAULT;
1385 }
aPiecek2e1f68f2023-11-08 11:27:33 +01001386 /* make the node non-validated */
1387 term->flags &= LYD_NEW;
Michal Vasko59892dd2022-05-13 11:02:30 +02001388 dflt_change = 1;
1389 } else {
1390 dflt_change = 0;
1391 }
1392
aPiecek2e1f68f2023-11-08 11:27:33 +01001393 /* return value */
Michal Vasko59892dd2022-05-13 11:02:30 +02001394 if (!val_change) {
1395 if (dflt_change) {
1396 /* only default flag change */
1397 ret = LY_EEXIST;
1398 } else {
1399 /* no change */
1400 ret = LY_ENOT;
1401 }
1402 } /* else value changed, LY_SUCCESS */
1403
1404cleanup:
1405 return ret;
1406}
1407
1408LIBYANG_API_DEF LY_ERR
1409lyd_change_term(struct lyd_node *term, const char *val_str)
1410{
1411 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1412
1413 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1414}
1415
1416LIBYANG_API_DEF LY_ERR
1417lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1418{
1419 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1420
1421 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1422}
1423
1424LIBYANG_API_DEF LY_ERR
1425lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1426{
1427 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1428
1429 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1430}
1431
1432LIBYANG_API_DEF LY_ERR
1433lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1434{
1435 LY_ERR ret = LY_SUCCESS;
1436 struct lyd_meta *m2 = NULL;
1437 struct lyd_value val;
1438 ly_bool val_change;
1439
1440 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1441
1442 if (!val_str) {
1443 val_str = "";
1444 }
1445
1446 /* parse the new value into a new meta structure */
1447 ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
Michal Vasko989cdb42023-10-06 15:32:37 +02001448 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001449 LY_CHECK_GOTO(ret, cleanup);
1450
1451 /* compare original and new value */
1452 if (lyd_compare_meta(meta, m2)) {
1453 /* values differ, switch them */
1454 val = meta->value;
1455 meta->value = m2->value;
1456 m2->value = val;
1457 val_change = 1;
1458 } else {
1459 val_change = 0;
1460 }
1461
1462 /* retrun value */
1463 if (!val_change) {
1464 /* no change */
1465 ret = LY_ENOT;
1466 } /* else value changed, LY_SUCCESS */
1467
1468cleanup:
1469 lyd_free_meta_single(m2);
1470 return ret;
1471}
1472
1473/**
1474 * @brief Update node value.
1475 *
1476 * @param[in] node Node to update.
1477 * @param[in] value New value to set.
1478 * @param[in] value_len Length of @p value.
1479 * @param[in] value_type Type of @p value for anydata/anyxml node.
1480 * @param[in] format Format of @p value.
1481 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1482 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1483 * @return LY_ERR value.
1484 */
1485static LY_ERR
1486lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1487 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1488{
1489 LY_ERR ret = LY_SUCCESS;
1490 struct lyd_node *new_any;
1491
1492 switch (node->schema->nodetype) {
1493 case LYS_CONTAINER:
1494 case LYS_NOTIF:
1495 case LYS_RPC:
1496 case LYS_ACTION:
1497 case LYS_LIST:
1498 /* if it exists, there is nothing to update */
1499 *new_parent = NULL;
1500 *new_node = NULL;
1501 break;
1502 case LYS_LEAFLIST:
1503 if (!lysc_is_dup_inst_list(node->schema)) {
1504 /* if it exists, there is nothing to update */
1505 *new_parent = NULL;
1506 *new_node = NULL;
1507 break;
1508 }
1509 /* fallthrough */
1510 case LYS_LEAF:
1511 ret = _lyd_change_term(node, value, value_len, format);
1512 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1513 /* there was an actual change (at least of the default flag) */
1514 *new_parent = node;
1515 *new_node = node;
1516 ret = LY_SUCCESS;
1517 } else if (ret == LY_ENOT) {
1518 /* no change */
1519 *new_parent = NULL;
1520 *new_node = NULL;
1521 ret = LY_SUCCESS;
1522 } /* else error */
1523 break;
1524 case LYS_ANYDATA:
1525 case LYS_ANYXML:
1526 /* create a new any node */
1527 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1528
1529 /* compare with the existing one */
1530 if (lyd_compare_single(node, new_any, 0)) {
1531 /* not equal, switch values (so that we can use generic node free) */
1532 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1533 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1534 ((struct lyd_node_any *)node)->value.str = value;
1535 ((struct lyd_node_any *)node)->value_type = value_type;
1536
1537 *new_parent = node;
1538 *new_node = node;
1539 } else {
1540 /* they are equal */
1541 *new_parent = NULL;
1542 *new_node = NULL;
1543 }
1544 lyd_free_tree(new_any);
1545 break;
1546 default:
1547 LOGINT(LYD_CTX(node));
1548 ret = LY_EINT;
1549 break;
1550 }
1551
1552 return ret;
1553}
1554
1555static LY_ERR
1556lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1557 LY_VALUE_FORMAT format, uint32_t options)
1558{
1559 LY_ERR r;
1560 struct ly_path_predicate *pred;
1561 struct lyd_value val;
1562 const struct lysc_node *schema = NULL;
1563 LY_ARRAY_COUNT_TYPE u, new_count;
1564 int create = 0;
1565
1566 assert(path);
1567
1568 /* go through all the compiled nodes */
1569 LY_ARRAY_FOR(path, u) {
1570 schema = path[u].node;
1571
1572 if (lysc_is_dup_inst_list(schema)) {
Michal Vasko5de21542023-03-20 10:00:05 +01001573 if (!path[u].predicates ||
1574 ((schema->nodetype == LYS_LEAFLIST) && (path[u].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001575 /* creating a new key-less list or state leaf-list instance */
1576 create = 1;
1577 new_count = u;
Michal Vasko90189962023-02-28 12:10:34 +01001578 } else if (path[u].predicates[0].type != LY_PATH_PREDTYPE_POSITION) {
Michal Vasko7a266772024-01-23 11:02:38 +01001579 LOG_LOCSET(schema, NULL);
Michal Vasko5de21542023-03-20 10:00:05 +01001580 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for state %s \"%s\" in path \"%s\".",
Michal Vasko59892dd2022-05-13 11:02:30 +02001581 lys_nodetype2str(schema->nodetype), schema->name, str_path);
Michal Vasko7a266772024-01-23 11:02:38 +01001582 LOG_LOCBACK(1, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +02001583 return LY_EINVAL;
1584 }
Michal Vasko90189962023-02-28 12:10:34 +01001585 } else if ((schema->nodetype == LYS_LIST) &&
1586 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001587 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko7a266772024-01-23 11:02:38 +01001588 LOG_LOCSET(schema, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001589 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1590 lys_nodetype2str(schema->nodetype), schema->name, str_path);
Michal Vasko7a266772024-01-23 11:02:38 +01001591 LOG_LOCBACK(1, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +02001592 return LY_EINVAL;
1593 } /* else creating an opaque list */
Michal Vasko90189962023-02-28 12:10:34 +01001594 } else if ((schema->nodetype == LYS_LEAFLIST) &&
1595 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001596 r = LY_SUCCESS;
1597 if (options & LYD_NEW_PATH_OPAQ) {
1598 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
1599 }
1600 if (!r) {
1601 /* try to store the value */
1602 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
Michal Vasko989cdb42023-10-06 15:32:37 +02001603 value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
Michal Vasko59892dd2022-05-13 11:02:30 +02001604 ++((struct lysc_type *)val.realtype)->refcount;
1605
1606 /* store the new predicate so that it is used when searching for this instance */
Michal Vasko59892dd2022-05-13 11:02:30 +02001607 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vasko90189962023-02-28 12:10:34 +01001608 pred->type = LY_PATH_PREDTYPE_LEAFLIST;
Michal Vasko59892dd2022-05-13 11:02:30 +02001609 pred->value = val;
1610 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1611 }
1612 }
1613
1614 if (create) {
1615 /* hide the nodes that should always be created so they are not found */
1616 while (new_count < LY_ARRAY_COUNT(path)) {
1617 LY_ARRAY_DECREMENT(path);
1618 }
1619 }
1620
1621 return LY_SUCCESS;
1622}
1623
1624/**
1625 * @brief Create a new node in the data tree based on a path. All node types can be created.
1626 *
1627 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1628 * Also, if a leaf-list is being created and both a predicate is defined in @p path
1629 * and @p value is set, the predicate is preferred.
1630 *
1631 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1632 * nodes, they are always created.
1633 *
1634 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1635 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1636 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1637 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1638 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1639 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1640 * searching for the appropriate module.
1641 * @param[in] path [Path](@ref howtoXPath) to create.
1642 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1643 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1644 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1645 * creating anyxml/anydata nodes.
1646 * @param[in] value_type Anyxml/anydata node @p value type.
1647 * @param[in] options Bitmask of options, see @ref pathoptions.
1648 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1649 * @param[out] new_node Optional last node created.
1650 * @return LY_ERR value.
1651 */
1652static LY_ERR
1653lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
1654 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
1655 struct lyd_node **new_parent, struct lyd_node **new_node)
1656{
1657 LY_ERR ret = LY_SUCCESS, r;
1658 struct lyxp_expr *exp = NULL;
1659 struct ly_path *p = NULL;
1660 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1661 const struct lysc_node *schema;
1662 const struct lyd_value *val = NULL;
1663 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
1664 LY_VALUE_FORMAT format;
1665
1666 assert(parent || ctx);
1667 assert(path && ((path[0] == '/') || parent));
1668 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
1669
1670 if (!ctx) {
1671 ctx = LYD_CTX(parent);
1672 }
1673 if (value && !value_len) {
1674 value_len = strlen(value);
1675 }
1676 if (options & LYD_NEW_PATH_BIN_VALUE) {
1677 format = LY_VALUE_LYB;
1678 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1679 format = LY_VALUE_CANON;
1680 } else {
1681 format = LY_VALUE_JSON;
1682 }
1683
1684 /* parse path */
Michal Vasko074d9a12023-06-22 08:23:41 +02001685 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 +02001686 LY_PATH_PRED_SIMPLE, &exp), cleanup);
1687
1688 /* compile path */
1689 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1690 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
1691
1692 /* check the compiled path before searching existing nodes, it may be shortened */
1693 orig_count = LY_ARRAY_COUNT(p);
1694 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup);
1695
1696 /* try to find any existing nodes in the path */
1697 if (parent) {
Michal Vasko838829d2023-10-09 16:06:43 +02001698 r = ly_path_eval_partial(p, parent, NULL, options & LYD_NEW_PATH_WITH_OPAQ, &path_idx, &node);
Michal Vaskof2560512023-03-03 09:54:47 +01001699 if (r == LY_SUCCESS) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001700 if (orig_count == LY_ARRAY_COUNT(p)) {
1701 /* the node exists, are we supposed to update it or is it just a default? */
1702 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
Michal Vasko7a266772024-01-23 11:02:38 +01001703 LOG_LOCSET(NULL, node);
Michal Vaskoa9764922023-02-09 13:59:07 +01001704 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists.", path);
Michal Vasko7a266772024-01-23 11:02:38 +01001705 LOG_LOCBACK(0, 1);
Michal Vasko59892dd2022-05-13 11:02:30 +02001706 ret = LY_EEXIST;
1707 goto cleanup;
Michal Vaskofabe9b92023-10-23 13:45:27 +02001708 } else if ((options & LYD_NEW_PATH_UPDATE) && lysc_is_key(node->schema)) {
1709 /* fine, the key value must not be changed and has to be in the predicate to be found */
1710 goto cleanup;
Michal Vasko59892dd2022-05-13 11:02:30 +02001711 }
1712
1713 /* update the existing node */
1714 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
1715 goto cleanup;
1716 } /* else we were not searching for the whole path */
Michal Vaskof2560512023-03-03 09:54:47 +01001717 } else if (r == LY_EINCOMPLETE) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001718 /* some nodes were found, adjust the iterator to the next segment */
1719 ++path_idx;
Michal Vaskof2560512023-03-03 09:54:47 +01001720 } else if (r == LY_ENOTFOUND) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001721 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
1722 if (lysc_data_parent(p[0].node)) {
1723 node = parent;
1724 }
1725 } else {
1726 /* error */
Michal Vaskof2560512023-03-03 09:54:47 +01001727 ret = r;
Michal Vasko59892dd2022-05-13 11:02:30 +02001728 goto cleanup;
1729 }
1730 }
1731
1732 /* restore the full path for creating new nodes */
1733 while (orig_count > LY_ARRAY_COUNT(p)) {
1734 LY_ARRAY_INCREMENT(p);
1735 }
1736
1737 /* create all the non-existing nodes in a loop */
1738 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
1739 cur_parent = node;
1740 schema = p[path_idx].node;
1741
1742 switch (schema->nodetype) {
1743 case LYS_LIST:
1744 if (lysc_is_dup_inst_list(schema)) {
1745 /* create key-less list instance */
1746 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
Michal Vasko90189962023-02-28 12:10:34 +01001747 } else if ((options & LYD_NEW_PATH_OPAQ) && !p[path_idx].predicates) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001748 /* creating opaque list without keys */
1749 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1750 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
1751 LYD_NODEHINT_LIST, &node), cleanup);
1752 } else {
1753 /* create standard list instance */
Michal Vasko90189962023-02-28 12:10:34 +01001754 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, NULL, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001755 }
1756 break;
1757 case LYS_CONTAINER:
1758 case LYS_NOTIF:
1759 case LYS_RPC:
1760 case LYS_ACTION:
1761 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1762 break;
1763 case LYS_LEAFLIST:
Michal Vasko90189962023-02-28 12:10:34 +01001764 if ((options & LYD_NEW_PATH_OPAQ) &&
1765 (!p[path_idx].predicates || (p[path_idx].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001766 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1767 r = LY_EVALID;
1768 if (lysc_is_dup_inst_list(schema)) {
1769 /* validate value */
1770 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1771 }
1772 if (r && (r != LY_EINCOMPLETE)) {
1773 /* creating opaque leaf-list */
1774 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1775 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1776 LYD_NODEHINT_LEAFLIST, &node), cleanup);
1777 break;
1778 }
1779 }
1780
1781 /* get value to set */
Michal Vasko90189962023-02-28 12:10:34 +01001782 if (p[path_idx].predicates && (p[path_idx].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001783 val = &p[path_idx].predicates[0].value;
1784 }
1785
1786 /* create a leaf-list instance */
1787 if (val) {
Michal Vasko90189962023-02-28 12:10:34 +01001788 LY_CHECK_GOTO(ret = lyd_create_term2(schema, val, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001789 } else {
Michal Vasko989cdb42023-10-06 15:32:37 +02001790 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA,
Michal Vasko59892dd2022-05-13 11:02:30 +02001791 NULL, &node), cleanup);
1792 }
1793 break;
1794 case LYS_LEAF:
1795 if (lysc_is_key(schema) && cur_parent->schema) {
1796 /* it must have been already created or some error will occur later */
1797 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1798 assert(node);
1799 goto next_iter;
1800 }
1801
1802 if (options & LYD_NEW_PATH_OPAQ) {
1803 if (cur_parent && !cur_parent->schema) {
1804 /* always create opaque nodes for opaque parents */
1805 r = LY_ENOT;
1806 } else {
1807 /* validate value */
1808 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1809 }
1810 if (r && (r != LY_EINCOMPLETE)) {
1811 /* creating opaque leaf */
1812 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1813 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
1814 cleanup);
1815 break;
1816 }
1817 }
1818
1819 /* create a leaf instance */
Michal Vasko989cdb42023-10-06 15:32:37 +02001820 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, NULL,
Michal Vasko59892dd2022-05-13 11:02:30 +02001821 &node), cleanup);
1822 break;
1823 case LYS_ANYDATA:
1824 case LYS_ANYXML:
1825 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
1826 break;
1827 default:
1828 LOGINT(ctx);
1829 ret = LY_EINT;
1830 goto cleanup;
1831 }
1832
1833 if (p[path_idx].ext) {
1834 node->flags |= LYD_EXT;
1835 }
1836 if (cur_parent) {
1837 /* connect to the parent */
1838 lyd_insert_node(cur_parent, NULL, node, 0);
1839 } else if (parent) {
1840 /* connect to top-level siblings */
1841 lyd_insert_node(NULL, &parent, node, 0);
1842 }
1843
1844next_iter:
1845 /* update remembered nodes */
1846 if (!nparent) {
1847 nparent = node;
1848 }
1849 nnode = node;
1850 }
1851
1852cleanup:
1853 lyxp_expr_free(ctx, exp);
1854 if (p) {
1855 while (orig_count > LY_ARRAY_COUNT(p)) {
1856 LY_ARRAY_INCREMENT(p);
1857 }
1858 }
1859 ly_path_free(ctx, p);
1860 if (!ret) {
1861 /* set out params only on success */
1862 if (new_parent) {
1863 *new_parent = nparent;
1864 }
1865 if (new_node) {
1866 *new_node = nnode;
1867 }
1868 } else {
1869 lyd_free_tree(nparent);
1870 }
1871 return ret;
1872}
1873
1874LIBYANG_API_DEF LY_ERR
1875lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1876 struct lyd_node **node)
1877{
1878 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1879 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1880 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1881
1882 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1883}
1884
1885LIBYANG_API_DEF LY_ERR
1886lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1887 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
1888 struct lyd_node **new_node)
1889{
1890 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1891 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1892 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1893
1894 return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node);
1895}
1896
1897LIBYANG_API_DEF LY_ERR
1898lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1899 uint32_t options, struct lyd_node **node)
1900{
1901 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1902
1903 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
1904 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1905 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1906
1907 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1908}
1909
1910LY_ERR
1911lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001912 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node,
1913 uint32_t impl_opts, struct lyd_node **diff)
Michal Vasko59892dd2022-05-13 11:02:30 +02001914{
1915 LY_ERR ret;
1916 const struct lysc_node *iter = NULL;
1917 struct lyd_node *node = NULL;
1918 struct lyd_value **dflts;
1919 LY_ARRAY_COUNT_TYPE u;
1920 uint32_t getnext_opts;
1921
1922 assert(first && (parent || sparent || mod));
1923
1924 if (!sparent && parent) {
1925 sparent = parent->schema;
1926 }
1927
1928 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1929 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1930 getnext_opts |= LYS_GETNEXT_OUTPUT;
1931 }
1932
1933 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
1934 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1935 continue;
1936 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1937 continue;
1938 }
1939
1940 switch (iter->nodetype) {
1941 case LYS_CHOICE:
1942 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1943 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
1944 /* create default case data */
1945 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001946 NULL, node_when, node_types, ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001947 } else if (node) {
1948 /* create any default data in the existing case */
1949 assert(node->schema->parent->nodetype == LYS_CASE);
1950 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001951 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001952 }
1953 break;
1954 case LYS_CONTAINER:
1955 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1956 /* create default NP container */
1957 LY_CHECK_RET(lyd_create_inner(iter, &node));
1958 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1959 lyd_insert_node(parent, first, node, 0);
1960
1961 if (lysc_has_when(iter) && node_when) {
1962 /* remember to resolve when */
1963 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1964 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001965 if (ext_node) {
1966 /* store for ext instance node validation, if needed */
1967 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1968 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001969 if (diff) {
1970 /* add into diff */
1971 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1972 }
1973
1974 /* create any default children */
1975 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 +02001976 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001977 }
1978 break;
1979 case LYS_LEAF:
1980 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1981 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1982 /* create default leaf */
1983 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1984 if (ret == LY_EINCOMPLETE) {
1985 if (node_types) {
1986 /* remember to resolve type */
1987 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
1988 }
1989 } else if (ret) {
1990 return ret;
1991 }
1992 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1993 lyd_insert_node(parent, first, node, 0);
1994
1995 if (lysc_has_when(iter) && node_when) {
1996 /* remember to resolve when */
1997 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1998 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001999 if (ext_node) {
2000 /* store for ext instance node validation, if needed */
2001 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
2002 }
Michal Vasko59892dd2022-05-13 11:02:30 +02002003 if (diff) {
2004 /* add into diff */
2005 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2006 }
2007 }
2008 break;
2009 case LYS_LEAFLIST:
2010 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2011 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2012 /* create all default leaf-lists */
2013 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2014 LY_ARRAY_FOR(dflts, u) {
2015 ret = lyd_create_term2(iter, dflts[u], &node);
2016 if (ret == LY_EINCOMPLETE) {
2017 if (node_types) {
2018 /* remember to resolve type */
2019 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
2020 }
2021 } else if (ret) {
2022 return ret;
2023 }
2024 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
2025 lyd_insert_node(parent, first, node, 0);
2026
2027 if (lysc_has_when(iter) && node_when) {
2028 /* remember to resolve when */
2029 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2030 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02002031 if (ext_node) {
2032 /* store for ext instance node validation, if needed */
2033 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
2034 }
Michal Vasko59892dd2022-05-13 11:02:30 +02002035 if (diff) {
2036 /* add into diff */
2037 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2038 }
2039 }
2040 }
2041 break;
2042 default:
2043 /* without defaults */
2044 break;
2045 }
2046 }
2047
2048 return LY_SUCCESS;
2049}
2050
2051LIBYANG_API_DEF LY_ERR
2052lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
2053{
2054 LY_ERR ret = LY_SUCCESS;
2055 struct lyd_node *node;
2056 struct ly_set node_when = {0};
2057
2058 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2059 if (diff) {
2060 *diff = NULL;
2061 }
2062
2063 LYD_TREE_DFS_BEGIN(tree, node) {
steweg0e1e5092024-02-12 09:06:04 +01002064 if (node->schema && (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02002065 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 +02002066 NULL, implicit_options, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002067 }
2068
2069 LYD_TREE_DFS_END(tree, node);
2070 }
2071
2072 /* resolve when and remove any invalid defaults */
Michal Vasko135719f2022-08-25 12:18:17 +02002073 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 +02002074 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002075
2076cleanup:
2077 ly_set_erase(&node_when, NULL);
2078 if (ret && diff) {
2079 lyd_free_all(*diff);
2080 *diff = NULL;
2081 }
2082 return ret;
2083}
2084
2085LIBYANG_API_DEF LY_ERR
2086lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
2087{
2088 const struct lys_module *mod;
2089 struct lyd_node *d = NULL;
2090 uint32_t i = 0;
2091 LY_ERR ret = LY_SUCCESS;
2092
2093 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2094 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
2095 if (diff) {
2096 *diff = NULL;
2097 }
2098 if (!ctx) {
2099 ctx = LYD_CTX(*tree);
2100 }
2101
2102 /* add nodes for each module one-by-one */
2103 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2104 if (!mod->implemented) {
2105 continue;
2106 }
2107
2108 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2109 if (d) {
2110 /* merge into one diff */
2111 lyd_insert_sibling(*diff, d, diff);
2112
2113 d = NULL;
2114 }
2115 }
2116
2117cleanup:
2118 if (ret && diff) {
2119 lyd_free_all(*diff);
2120 *diff = NULL;
2121 }
2122 return ret;
2123}
2124
2125LIBYANG_API_DEF LY_ERR
2126lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
2127 struct lyd_node **diff)
2128{
2129 LY_ERR ret = LY_SUCCESS;
2130 struct lyd_node *root, *d = NULL;
2131 struct ly_set node_when = {0};
2132
2133 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2134 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
2135 if (diff) {
2136 *diff = NULL;
2137 }
2138
2139 /* add all top-level defaults for this module */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02002140 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, NULL, implicit_options, diff),
2141 cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002142
2143 /* resolve when and remove any invalid defaults */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02002144 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL,
2145 0, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002146
2147 /* process nested nodes */
2148 LY_LIST_FOR(*tree, root) {
Michal Vaskof87fbdf2023-07-11 10:20:02 +02002149 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02002150
Michal Vaskof87fbdf2023-07-11 10:20:02 +02002151 if (d) {
2152 /* merge into one diff */
2153 lyd_insert_sibling(*diff, d, diff);
2154 d = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +02002155 }
2156 }
2157
2158cleanup:
2159 ly_set_erase(&node_when, NULL);
2160 if (ret && diff) {
2161 lyd_free_all(*diff);
2162 *diff = NULL;
2163 }
2164 return ret;
2165}