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