blob: d6a0b1f1d52b607d9758af5078b08d69bcb52633 [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
27#include "common.h"
28#include "compat.h"
29#include "context.h"
30#include "dict.h"
31#include "diff.h"
32#include "hash_table.h"
33#include "in.h"
34#include "in_internal.h"
35#include "log.h"
36#include "parser_data.h"
37#include "parser_internal.h"
38#include "path.h"
39#include "plugins.h"
40#include "plugins_exts/metadata.h"
41#include "plugins_internal.h"
42#include "plugins_types.h"
43#include "set.h"
44#include "tree.h"
45#include "tree_data_internal.h"
46#include "tree_edit.h"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
49#include "validation.h"
50#include "xml.h"
51#include "xpath.h"
52
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
69 LOG_LOCSET(schema, NULL, NULL, NULL);
70 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Michal Vasko989cdb42023-10-06 15:32:37 +020071 value_len, is_utf8, dynamic, format, prefix_data, hints, schema, incomplete);
Michal Vasko59892dd2022-05-13 11:02:30 +020072 LOG_LOCBACK(1, 0, 0, 0);
73 LY_CHECK_ERR_RET(ret, free(term), ret);
74 lyd_hash(&term->node);
75
76 *node = &term->node;
77 return ret;
78}
79
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 Vasko6e075ab2022-12-21 15:48:14 +0100152 LOG_LOCSET(schema, NULL, NULL, 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 */
163 LOG_LOCSET(predicates[u].key, NULL, NULL, NULL);
164 ret = lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaf *)predicates[u].key)->type,
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 Vasko90189962023-02-28 12:10:34 +0100166 LOG_LOCBACK(1, 0, 0, 0);
167 LY_CHECK_GOTO(ret, cleanup);
168
169 value = &val;
170 } else {
171 assert(predicates[u].type == LY_PATH_PREDTYPE_LIST);
172 value = &predicates[u].value;
173 }
174
175 ret = lyd_create_term2(predicates[u].key, value, &key);
176 if (val.realtype) {
177 val.realtype->plugin->free(schema->module->ctx, &val);
178 memset(&val, 0, sizeof val);
179 }
180 LY_CHECK_GOTO(ret, cleanup);
Michal 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 Vasko6e075ab2022-12-21 15:48:14 +0100192 LOG_LOCBACK(1, 0, 0, 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
205 LOG_LOCSET(schema, NULL, NULL, NULL);
206
207 /* parse keys */
208 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
209 LY_PATH_PRED_KEYS, &expr), cleanup);
210
211 /* compile them */
212 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON,
Michal 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:
219 LOG_LOCBACK(1, 0, 0, 0);
220 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 Vasko5b414dd2023-04-13 10:29:58 +0200266 uint32_t parse_opts, int_opts, log_opts = 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 */
276 ly_temp_log_options(&log_opts);
277 }
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 */
300 ly_temp_log_options(NULL);
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/**
542 * @brief Create a new list node in the data tree.
543 *
544 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
545 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
546 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
547 * @param[in] format Format of key values.
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 Optional created node.
551 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
552 * be followed by the value length.
553 * @return LY_ERR value.
554 */
555static LY_ERR
556_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
557 ly_bool output, struct lyd_node **node, va_list ap)
558{
559 struct lyd_node *ret = NULL, *key;
560 const struct lysc_node *schema, *key_s;
561 struct lysc_ext_instance *ext = NULL;
562 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
563 const void *key_val;
564 uint32_t key_len;
565 LY_ERR r, rc = LY_SUCCESS;
566
567 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
568 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
569
570 if (!module) {
571 module = parent->schema->module;
572 }
573
574 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
575 if (!schema && parent) {
576 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
577 strlen(name), &schema, &ext);
578 LY_CHECK_RET(r && (r != LY_ENOT), r);
579 }
580 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
581
582 /* create list inner node */
583 LY_CHECK_RET(lyd_create_inner(schema, &ret));
584
585 /* create and insert all the keys */
586 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
587 if (format == LY_VALUE_LYB) {
588 key_val = va_arg(ap, const void *);
589 key_len = va_arg(ap, uint32_t);
590 } else {
591 key_val = va_arg(ap, const char *);
592 key_len = key_val ? strlen((char *)key_val) : 0;
593 }
594
Michal Vasko989cdb42023-10-06 15:32:37 +0200595 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 +0200596 LY_CHECK_GOTO(rc, cleanup);
597 lyd_insert_node(ret, NULL, key, 1);
598 }
599
600 if (ext) {
601 ret->flags |= LYD_EXT;
602 }
603 if (parent) {
604 lyd_insert_node(parent, NULL, ret, 0);
605 }
606
607cleanup:
608 if (rc) {
609 lyd_free_tree(ret);
610 ret = NULL;
611 } else if (node) {
612 *node = ret;
613 }
614 return rc;
615}
616
617LIBYANG_API_DEF LY_ERR
618lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
619 struct lyd_node **node, ...)
620{
621 LY_ERR rc;
622 va_list ap;
623
624 va_start(ap, node);
625
626 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
627
628 va_end(ap);
629 return rc;
630}
631
632LIBYANG_API_DEF LY_ERR
633lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
634 struct lyd_node **node, ...)
635{
636 LY_ERR rc;
637 va_list ap;
638
639 va_start(ap, node);
640
641 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
642
643 va_end(ap);
644 return rc;
645}
646
647LIBYANG_API_DEF LY_ERR
648lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
649 struct lyd_node **node, ...)
650{
651 LY_ERR rc;
652 va_list ap;
653
654 va_start(ap, node);
655
656 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
657
658 va_end(ap);
659 return rc;
660}
661
662LIBYANG_API_DEF LY_ERR
663lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
664{
665 struct lyd_node *ret = NULL, *key;
666 const struct lysc_node *schema, *key_s;
667 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
668 va_list ap;
669 const char *key_val;
670 LY_ERR rc = LY_SUCCESS;
671
672 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
673
674 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
675 if (!schema) {
676 if (ext->argument) {
677 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
678 name, ext->argument, ext->def->name);
679 } else {
680 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
681 }
682 return LY_ENOTFOUND;
683 }
684 /* create list inner node */
685 LY_CHECK_RET(lyd_create_inner(schema, &ret));
686
687 va_start(ap, node);
688
689 /* create and insert all the keys */
690 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
691 key_val = va_arg(ap, const char *);
692
Michal Vasko989cdb42023-10-06 15:32:37 +0200693 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 +0200694 NULL, &key);
695 LY_CHECK_GOTO(rc, cleanup);
696 lyd_insert_node(ret, NULL, key, 1);
697 }
698
699cleanup:
700 va_end(ap);
701 if (rc) {
702 lyd_free_tree(ret);
703 ret = NULL;
704 }
705 *node = ret;
706 return rc;
707}
708
709LIBYANG_API_DEF LY_ERR
710lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
711 ly_bool output, struct lyd_node **node)
712{
713 LY_ERR r;
714 struct lyd_node *ret = NULL;
715 const struct lysc_node *schema;
716 struct lysc_ext_instance *ext = NULL;
717 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
718
719 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
720 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
721
722 if (!module) {
723 module = parent->schema->module;
724 }
725 if (!keys) {
726 keys = "";
727 }
728
729 /* find schema node */
730 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
731 if (!schema && parent) {
732 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name),
733 &schema, &ext);
734 LY_CHECK_RET(r && (r != LY_ENOT), r);
735 }
736 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
737
738 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
739 /* key-less list */
740 LY_CHECK_RET(lyd_create_inner(schema, &ret));
741 } else {
742 /* create the list node */
743 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
744 }
745 if (ext) {
746 ret->flags |= LYD_EXT;
747 }
748 if (parent) {
749 lyd_insert_node(parent, NULL, ret, 0);
750 }
751
752 if (node) {
753 *node = ret;
754 }
755 return LY_SUCCESS;
756}
757
758/**
759 * @brief Create a new term node in the data tree.
760 *
761 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
762 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
763 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
764 * @param[in] value Value of the node being created.
765 * @param[in] value_len Length of @p value.
766 * @param[in] format Format of @p value.
767 * @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
768 * taken into consideration. Otherwise, the output's data node is going to be created.
769 * @param[out] node Optional created node.
770 * @return LY_ERR value.
771 */
772static LY_ERR
773_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
774 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
775{
776 LY_ERR r;
777 struct lyd_node *ret = NULL;
778 const struct lysc_node *schema;
779 struct lysc_ext_instance *ext = NULL;
780 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
781
782 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
783 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
784
785 if (!module) {
786 module = parent->schema->module;
787 }
788
789 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
790 if (!schema && parent) {
791 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
792 strlen(name), &schema, &ext);
793 LY_CHECK_RET(r && (r != LY_ENOT), r);
794 }
795 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
796
Michal Vasko989cdb42023-10-06 15:32:37 +0200797 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 +0200798 if (ext) {
799 ret->flags |= LYD_EXT;
800 }
801 if (parent) {
802 lyd_insert_node(parent, NULL, ret, 0);
803 }
804
805 if (node) {
806 *node = ret;
807 }
808 return LY_SUCCESS;
809}
810
811LIBYANG_API_DEF LY_ERR
812lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
813 ly_bool output, struct lyd_node **node)
814{
815 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
816}
817
818LIBYANG_API_DEF LY_ERR
819lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
820 size_t value_len, ly_bool output, struct lyd_node **node)
821{
822 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
823}
824
825LIBYANG_API_DEF LY_ERR
826lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
827 ly_bool output, struct lyd_node **node)
828{
829 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
830}
831
832LIBYANG_API_DEF LY_ERR
833lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
834{
835 LY_ERR rc;
836 struct lyd_node *ret = NULL;
837 const struct lysc_node *schema;
838 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
839
840 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
841
842 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
843 if (!schema) {
844 if (ext->argument) {
845 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
846 name, ext->argument, ext->def->name);
847 } else {
848 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
849 }
850 return LY_ENOTFOUND;
851 }
Michal Vasko989cdb42023-10-06 15:32:37 +0200852 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
853 NULL, &ret);
Michal Vasko59892dd2022-05-13 11:02:30 +0200854 LY_CHECK_RET(rc);
855
856 *node = ret;
857
858 return LY_SUCCESS;
859}
860
861LIBYANG_API_DEF LY_ERR
862lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
863 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
864{
865 LY_ERR r;
866 struct lyd_node *ret = NULL;
867 const struct lysc_node *schema;
868 struct lysc_ext_instance *ext = NULL;
869 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
870
871 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
872 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
873
874 if (!module) {
875 module = parent->schema->module;
876 }
877
878 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
879 if (!schema && parent) {
880 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
881 strlen(name), &schema, &ext);
882 LY_CHECK_RET(r && (r != LY_ENOT), r);
883 }
884 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
885
886 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
887 if (ext) {
888 ret->flags |= LYD_EXT;
889 }
890 if (parent) {
891 lyd_insert_node(parent, NULL, ret, 0);
892 }
893
894 if (node) {
895 *node = ret;
896 }
897 return LY_SUCCESS;
898}
899
900LIBYANG_API_DEF LY_ERR
901lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
902 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
903{
904 struct lyd_node *ret = NULL;
905 const struct lysc_node *schema;
906 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
907
908 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
909
910 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
911 if (!schema) {
912 if (ext->argument) {
913 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
914 name, ext->argument, ext->def->name);
915 } else {
916 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
917 }
918 return LY_ENOTFOUND;
919 }
920 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
921
922 *node = ret;
923
924 return LY_SUCCESS;
925}
926
927LIBYANG_API_DEF LY_ERR
928lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
929 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
930{
931 const char *prefix, *tmp;
932 size_t pref_len, name_len;
933
934 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
935 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
936 if (!ctx) {
Michal Vasko33c48972022-07-20 10:28:07 +0200937 ctx = module ? module->ctx : LYD_CTX(parent);
Michal Vasko59892dd2022-05-13 11:02:30 +0200938 }
939
940 if (parent && !parent->schema) {
941 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
942 return LY_EINVAL;
943 }
944 if (meta) {
945 *meta = NULL;
946 }
947
948 /* parse the name */
949 tmp = name;
950 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
951 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
952 return LY_EINVAL;
953 }
954
955 /* find the module */
956 if (prefix) {
957 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
958 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND);
959 }
960
961 /* set value if none */
962 if (!val_str) {
963 val_str = "";
964 }
965
Michal Vasko989cdb42023-10-06 15:32:37 +0200966 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 +0200967 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
968}
969
970LIBYANG_API_DEF LY_ERR
971lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
972 struct lyd_meta **meta)
973{
974 const struct lys_module *mod;
975
976 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
977 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
978
979 if (parent && !parent->schema) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200980 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".",
981 ((struct lyd_node_opaq *)parent)->name.name);
Michal Vasko59892dd2022-05-13 11:02:30 +0200982 return LY_EINVAL;
983 }
984 if (meta) {
985 *meta = NULL;
986 }
987
988 switch (attr->format) {
989 case LY_VALUE_XML:
990 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
991 if (!mod) {
992 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
993 return LY_ENOTFOUND;
994 }
995 break;
996 case LY_VALUE_JSON:
997 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
998 if (!mod) {
999 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
1000 return LY_ENOTFOUND;
1001 }
1002 break;
1003 default:
1004 LOGINT_RET(ctx);
1005 }
1006
1007 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 +02001008 0, NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001009}
1010
1011LIBYANG_API_DEF LY_ERR
1012lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
1013 const char *prefix, const char *module_name, struct lyd_node **node)
1014{
1015 struct lyd_node *ret = NULL;
1016
1017 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
1018 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1019
1020 if (!ctx) {
1021 ctx = LYD_CTX(parent);
1022 }
1023 if (!value) {
1024 value = "";
1025 }
1026
1027 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1028 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
1029 if (parent) {
1030 lyd_insert_node(parent, NULL, ret, 1);
1031 }
1032
1033 if (node) {
1034 *node = ret;
1035 }
1036 return LY_SUCCESS;
1037}
1038
1039LIBYANG_API_DEF LY_ERR
1040lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
1041 const char *prefix, const char *module_ns, struct lyd_node **node)
1042{
1043 struct lyd_node *ret = NULL;
1044
1045 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1046 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1047
1048 if (!ctx) {
1049 ctx = LYD_CTX(parent);
1050 }
1051 if (!value) {
1052 value = "";
1053 }
1054
1055 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1056 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
1057 if (parent) {
1058 lyd_insert_node(parent, NULL, ret, 1);
1059 }
1060
1061 if (node) {
1062 *node = ret;
1063 }
1064 return LY_SUCCESS;
1065}
1066
1067LIBYANG_API_DEF LY_ERR
1068lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
1069 struct lyd_attr **attr)
1070{
1071 struct lyd_attr *ret = NULL;
1072 const struct ly_ctx *ctx;
1073 const char *prefix, *tmp;
1074 size_t pref_len, name_len, mod_len;
1075
1076 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1077
1078 ctx = LYD_CTX(parent);
1079
1080 /* parse the name */
1081 tmp = name;
1082 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1083 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1084 return LY_EVALID;
1085 }
1086
1087 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1088 /* not a prefix but special name */
1089 name = prefix;
1090 name_len += 1 + pref_len;
1091 prefix = NULL;
1092 pref_len = 0;
1093 }
1094
1095 /* get the module */
1096 if (module_name) {
1097 mod_len = strlen(module_name);
1098 } else {
1099 module_name = prefix;
1100 mod_len = pref_len;
1101 }
1102
1103 /* set value if none */
1104 if (!value) {
1105 value = "";
1106 }
1107
1108 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1109 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
1110
1111 if (attr) {
1112 *attr = ret;
1113 }
1114 return LY_SUCCESS;
1115}
1116
1117LIBYANG_API_DEF LY_ERR
1118lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1119 struct lyd_attr **attr)
1120{
1121 struct lyd_attr *ret = NULL;
1122 const struct ly_ctx *ctx;
1123 const char *prefix, *tmp;
1124 size_t pref_len, name_len;
1125
1126 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1127
1128 ctx = LYD_CTX(parent);
1129
1130 /* parse the name */
1131 tmp = name;
1132 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1133 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1134 return LY_EVALID;
1135 }
1136
1137 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1138 /* not a prefix but special name */
1139 name = prefix;
1140 name_len += 1 + pref_len;
1141 prefix = NULL;
1142 pref_len = 0;
1143 }
1144
1145 /* set value if none */
1146 if (!value) {
1147 value = "";
1148 }
Michal Vasko825718f2022-11-10 10:33:51 +01001149 if (strchr(value, ':')) {
1150 LOGWRN(ctx, "Value \"%s\" prefix will never be interpreted as an XML prefix.", value);
1151 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001152
1153 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1154 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
1155
1156 if (attr) {
1157 *attr = ret;
1158 }
1159 return LY_SUCCESS;
1160}
1161
1162/**
1163 * @brief Change the value of a term (leaf or leaf-list) node.
1164 *
1165 * Node changed this way is always considered explicitly set, meaning its default flag
1166 * is always cleared.
1167 *
1168 * @param[in] term Term node to change.
1169 * @param[in] value New value to set.
1170 * @param[in] value_len Length of @p value.
1171 * @param[in] format Format of @p value.
1172 * @return LY_SUCCESS if value was changed,
1173 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1174 * @return LY_ENOT if the values were equal and no change occured,
1175 * @return LY_ERR value on other errors.
1176 */
1177static LY_ERR
1178_lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format)
1179{
1180 LY_ERR ret = LY_SUCCESS;
1181 struct lysc_type *type;
1182 struct lyd_node_term *t;
1183 struct lyd_node *parent;
1184 struct lyd_value val;
1185 ly_bool dflt_change, val_change;
1186
1187 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
1188
1189 t = (struct lyd_node_term *)term;
1190 type = ((struct lysc_node_leaf *)term->schema)->type;
1191
1192 /* parse the new value */
1193 LOG_LOCSET(term->schema, term, NULL, NULL);
Michal Vasko989cdb42023-10-06 15:32:37 +02001194 ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA,
1195 term->schema, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001196 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
1197 LY_CHECK_GOTO(ret, cleanup);
1198
1199 /* compare original and new value */
1200 if (type->plugin->compare(&t->value, &val)) {
1201 /* values differ, switch them */
1202 type->plugin->free(LYD_CTX(term), &t->value);
1203 t->value = val;
1204 val_change = 1;
1205 } else {
1206 /* same values, free the new stored one */
1207 type->plugin->free(LYD_CTX(term), &val);
1208 val_change = 0;
1209 }
1210
1211 /* always clear the default flag */
1212 if (term->flags & LYD_DEFAULT) {
1213 for (parent = term; parent; parent = lyd_parent(parent)) {
1214 parent->flags &= ~LYD_DEFAULT;
1215 }
1216 dflt_change = 1;
1217 } else {
1218 dflt_change = 0;
1219 }
1220
1221 if (val_change || dflt_change) {
1222 /* make the node non-validated */
1223 term->flags &= LYD_NEW;
1224 }
1225
1226 if (val_change) {
1227 if (term->schema->nodetype == LYS_LEAFLIST) {
1228 /* leaf-list needs to be hashed again and re-inserted into parent */
1229 lyd_unlink_hash(term);
1230 lyd_hash(term);
1231 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1232 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1233 /* list needs to be updated if its key was changed */
1234 assert(term->parent->schema->nodetype == LYS_LIST);
1235 lyd_unlink_hash(lyd_parent(term));
1236 lyd_hash(lyd_parent(term));
1237 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
1238 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1239 }
1240
1241 /* retrun value */
1242 if (!val_change) {
1243 if (dflt_change) {
1244 /* only default flag change */
1245 ret = LY_EEXIST;
1246 } else {
1247 /* no change */
1248 ret = LY_ENOT;
1249 }
1250 } /* else value changed, LY_SUCCESS */
1251
1252cleanup:
1253 return ret;
1254}
1255
1256LIBYANG_API_DEF LY_ERR
1257lyd_change_term(struct lyd_node *term, const char *val_str)
1258{
1259 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1260
1261 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1262}
1263
1264LIBYANG_API_DEF LY_ERR
1265lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1266{
1267 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1268
1269 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1270}
1271
1272LIBYANG_API_DEF LY_ERR
1273lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1274{
1275 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1276
1277 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1278}
1279
1280LIBYANG_API_DEF LY_ERR
1281lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1282{
1283 LY_ERR ret = LY_SUCCESS;
1284 struct lyd_meta *m2 = NULL;
1285 struct lyd_value val;
1286 ly_bool val_change;
1287
1288 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1289
1290 if (!val_str) {
1291 val_str = "";
1292 }
1293
1294 /* parse the new value into a new meta structure */
1295 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 +02001296 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
Michal Vasko59892dd2022-05-13 11:02:30 +02001297 LY_CHECK_GOTO(ret, cleanup);
1298
1299 /* compare original and new value */
1300 if (lyd_compare_meta(meta, m2)) {
1301 /* values differ, switch them */
1302 val = meta->value;
1303 meta->value = m2->value;
1304 m2->value = val;
1305 val_change = 1;
1306 } else {
1307 val_change = 0;
1308 }
1309
1310 /* retrun value */
1311 if (!val_change) {
1312 /* no change */
1313 ret = LY_ENOT;
1314 } /* else value changed, LY_SUCCESS */
1315
1316cleanup:
1317 lyd_free_meta_single(m2);
1318 return ret;
1319}
1320
1321/**
1322 * @brief Update node value.
1323 *
1324 * @param[in] node Node to update.
1325 * @param[in] value New value to set.
1326 * @param[in] value_len Length of @p value.
1327 * @param[in] value_type Type of @p value for anydata/anyxml node.
1328 * @param[in] format Format of @p value.
1329 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1330 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1331 * @return LY_ERR value.
1332 */
1333static LY_ERR
1334lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1335 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1336{
1337 LY_ERR ret = LY_SUCCESS;
1338 struct lyd_node *new_any;
1339
1340 switch (node->schema->nodetype) {
1341 case LYS_CONTAINER:
1342 case LYS_NOTIF:
1343 case LYS_RPC:
1344 case LYS_ACTION:
1345 case LYS_LIST:
1346 /* if it exists, there is nothing to update */
1347 *new_parent = NULL;
1348 *new_node = NULL;
1349 break;
1350 case LYS_LEAFLIST:
1351 if (!lysc_is_dup_inst_list(node->schema)) {
1352 /* if it exists, there is nothing to update */
1353 *new_parent = NULL;
1354 *new_node = NULL;
1355 break;
1356 }
1357 /* fallthrough */
1358 case LYS_LEAF:
1359 ret = _lyd_change_term(node, value, value_len, format);
1360 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1361 /* there was an actual change (at least of the default flag) */
1362 *new_parent = node;
1363 *new_node = node;
1364 ret = LY_SUCCESS;
1365 } else if (ret == LY_ENOT) {
1366 /* no change */
1367 *new_parent = NULL;
1368 *new_node = NULL;
1369 ret = LY_SUCCESS;
1370 } /* else error */
1371 break;
1372 case LYS_ANYDATA:
1373 case LYS_ANYXML:
1374 /* create a new any node */
1375 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1376
1377 /* compare with the existing one */
1378 if (lyd_compare_single(node, new_any, 0)) {
1379 /* not equal, switch values (so that we can use generic node free) */
1380 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1381 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1382 ((struct lyd_node_any *)node)->value.str = value;
1383 ((struct lyd_node_any *)node)->value_type = value_type;
1384
1385 *new_parent = node;
1386 *new_node = node;
1387 } else {
1388 /* they are equal */
1389 *new_parent = NULL;
1390 *new_node = NULL;
1391 }
1392 lyd_free_tree(new_any);
1393 break;
1394 default:
1395 LOGINT(LYD_CTX(node));
1396 ret = LY_EINT;
1397 break;
1398 }
1399
1400 return ret;
1401}
1402
1403static LY_ERR
1404lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1405 LY_VALUE_FORMAT format, uint32_t options)
1406{
1407 LY_ERR r;
1408 struct ly_path_predicate *pred;
1409 struct lyd_value val;
1410 const struct lysc_node *schema = NULL;
1411 LY_ARRAY_COUNT_TYPE u, new_count;
1412 int create = 0;
1413
1414 assert(path);
1415
1416 /* go through all the compiled nodes */
1417 LY_ARRAY_FOR(path, u) {
1418 schema = path[u].node;
1419
1420 if (lysc_is_dup_inst_list(schema)) {
Michal Vasko5de21542023-03-20 10:00:05 +01001421 if (!path[u].predicates ||
1422 ((schema->nodetype == LYS_LEAFLIST) && (path[u].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001423 /* creating a new key-less list or state leaf-list instance */
1424 create = 1;
1425 new_count = u;
Michal Vasko90189962023-02-28 12:10:34 +01001426 } else if (path[u].predicates[0].type != LY_PATH_PREDTYPE_POSITION) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001427 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vasko5de21542023-03-20 10:00:05 +01001428 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for state %s \"%s\" in path \"%s\".",
Michal Vasko59892dd2022-05-13 11:02:30 +02001429 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1430 LOG_LOCBACK(1, 0, 0, 0);
1431 return LY_EINVAL;
1432 }
Michal Vasko90189962023-02-28 12:10:34 +01001433 } else if ((schema->nodetype == LYS_LIST) &&
1434 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001435 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1436 LOG_LOCSET(schema, NULL, NULL, NULL);
1437 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1438 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1439 LOG_LOCBACK(1, 0, 0, 0);
1440 return LY_EINVAL;
1441 } /* else creating an opaque list */
Michal Vasko90189962023-02-28 12:10:34 +01001442 } else if ((schema->nodetype == LYS_LEAFLIST) &&
1443 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001444 r = LY_SUCCESS;
1445 if (options & LYD_NEW_PATH_OPAQ) {
1446 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
1447 }
1448 if (!r) {
1449 /* try to store the value */
1450 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
Michal Vasko989cdb42023-10-06 15:32:37 +02001451 value, value_len, 0, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
Michal Vasko59892dd2022-05-13 11:02:30 +02001452 ++((struct lysc_type *)val.realtype)->refcount;
1453
1454 /* store the new predicate so that it is used when searching for this instance */
Michal Vasko59892dd2022-05-13 11:02:30 +02001455 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vasko90189962023-02-28 12:10:34 +01001456 pred->type = LY_PATH_PREDTYPE_LEAFLIST;
Michal Vasko59892dd2022-05-13 11:02:30 +02001457 pred->value = val;
1458 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1459 }
1460 }
1461
1462 if (create) {
1463 /* hide the nodes that should always be created so they are not found */
1464 while (new_count < LY_ARRAY_COUNT(path)) {
1465 LY_ARRAY_DECREMENT(path);
1466 }
1467 }
1468
1469 return LY_SUCCESS;
1470}
1471
1472/**
1473 * @brief Create a new node in the data tree based on a path. All node types can be created.
1474 *
1475 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1476 * Also, if a leaf-list is being created and both a predicate is defined in @p path
1477 * and @p value is set, the predicate is preferred.
1478 *
1479 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1480 * nodes, they are always created.
1481 *
1482 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1483 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1484 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1485 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1486 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1487 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1488 * searching for the appropriate module.
1489 * @param[in] path [Path](@ref howtoXPath) to create.
1490 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1491 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1492 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1493 * creating anyxml/anydata nodes.
1494 * @param[in] value_type Anyxml/anydata node @p value type.
1495 * @param[in] options Bitmask of options, see @ref pathoptions.
1496 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1497 * @param[out] new_node Optional last node created.
1498 * @return LY_ERR value.
1499 */
1500static LY_ERR
1501lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
1502 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
1503 struct lyd_node **new_parent, struct lyd_node **new_node)
1504{
1505 LY_ERR ret = LY_SUCCESS, r;
1506 struct lyxp_expr *exp = NULL;
1507 struct ly_path *p = NULL;
1508 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1509 const struct lysc_node *schema;
1510 const struct lyd_value *val = NULL;
1511 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
1512 LY_VALUE_FORMAT format;
1513
1514 assert(parent || ctx);
1515 assert(path && ((path[0] == '/') || parent));
1516 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
1517
1518 if (!ctx) {
1519 ctx = LYD_CTX(parent);
1520 }
1521 if (value && !value_len) {
1522 value_len = strlen(value);
1523 }
1524 if (options & LYD_NEW_PATH_BIN_VALUE) {
1525 format = LY_VALUE_LYB;
1526 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1527 format = LY_VALUE_CANON;
1528 } else {
1529 format = LY_VALUE_JSON;
1530 }
1531
1532 /* parse path */
Michal Vasko074d9a12023-06-22 08:23:41 +02001533 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 +02001534 LY_PATH_PRED_SIMPLE, &exp), cleanup);
1535
1536 /* compile path */
1537 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1538 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
1539
1540 /* check the compiled path before searching existing nodes, it may be shortened */
1541 orig_count = LY_ARRAY_COUNT(p);
1542 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup);
1543
1544 /* try to find any existing nodes in the path */
1545 if (parent) {
Michal Vaskof2560512023-03-03 09:54:47 +01001546 r = ly_path_eval_partial(p, parent, NULL, &path_idx, &node);
1547 if (r == LY_SUCCESS) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001548 if (orig_count == LY_ARRAY_COUNT(p)) {
1549 /* the node exists, are we supposed to update it or is it just a default? */
1550 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1551 LOG_LOCSET(NULL, node, NULL, NULL);
Michal Vaskoa9764922023-02-09 13:59:07 +01001552 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists.", path);
Michal Vasko59892dd2022-05-13 11:02:30 +02001553 LOG_LOCBACK(0, 1, 0, 0);
1554 ret = LY_EEXIST;
1555 goto cleanup;
1556 }
1557
1558 /* update the existing node */
1559 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
1560 goto cleanup;
1561 } /* else we were not searching for the whole path */
Michal Vaskof2560512023-03-03 09:54:47 +01001562 } else if (r == LY_EINCOMPLETE) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001563 /* some nodes were found, adjust the iterator to the next segment */
1564 ++path_idx;
Michal Vaskof2560512023-03-03 09:54:47 +01001565 } else if (r == LY_ENOTFOUND) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001566 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
1567 if (lysc_data_parent(p[0].node)) {
1568 node = parent;
1569 }
1570 } else {
1571 /* error */
Michal Vaskof2560512023-03-03 09:54:47 +01001572 ret = r;
Michal Vasko59892dd2022-05-13 11:02:30 +02001573 goto cleanup;
1574 }
1575 }
1576
1577 /* restore the full path for creating new nodes */
1578 while (orig_count > LY_ARRAY_COUNT(p)) {
1579 LY_ARRAY_INCREMENT(p);
1580 }
1581
1582 /* create all the non-existing nodes in a loop */
1583 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
1584 cur_parent = node;
1585 schema = p[path_idx].node;
1586
1587 switch (schema->nodetype) {
1588 case LYS_LIST:
1589 if (lysc_is_dup_inst_list(schema)) {
1590 /* create key-less list instance */
1591 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
Michal Vasko90189962023-02-28 12:10:34 +01001592 } else if ((options & LYD_NEW_PATH_OPAQ) && !p[path_idx].predicates) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001593 /* creating opaque list without keys */
1594 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1595 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
1596 LYD_NODEHINT_LIST, &node), cleanup);
1597 } else {
1598 /* create standard list instance */
Michal Vasko90189962023-02-28 12:10:34 +01001599 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, NULL, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001600 }
1601 break;
1602 case LYS_CONTAINER:
1603 case LYS_NOTIF:
1604 case LYS_RPC:
1605 case LYS_ACTION:
1606 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1607 break;
1608 case LYS_LEAFLIST:
Michal Vasko90189962023-02-28 12:10:34 +01001609 if ((options & LYD_NEW_PATH_OPAQ) &&
1610 (!p[path_idx].predicates || (p[path_idx].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001611 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1612 r = LY_EVALID;
1613 if (lysc_is_dup_inst_list(schema)) {
1614 /* validate value */
1615 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1616 }
1617 if (r && (r != LY_EINCOMPLETE)) {
1618 /* creating opaque leaf-list */
1619 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1620 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1621 LYD_NODEHINT_LEAFLIST, &node), cleanup);
1622 break;
1623 }
1624 }
1625
1626 /* get value to set */
Michal Vasko90189962023-02-28 12:10:34 +01001627 if (p[path_idx].predicates && (p[path_idx].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001628 val = &p[path_idx].predicates[0].value;
1629 }
1630
1631 /* create a leaf-list instance */
1632 if (val) {
Michal Vasko90189962023-02-28 12:10:34 +01001633 LY_CHECK_GOTO(ret = lyd_create_term2(schema, val, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001634 } else {
Michal Vasko989cdb42023-10-06 15:32:37 +02001635 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 +02001636 NULL, &node), cleanup);
1637 }
1638 break;
1639 case LYS_LEAF:
1640 if (lysc_is_key(schema) && cur_parent->schema) {
1641 /* it must have been already created or some error will occur later */
1642 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1643 assert(node);
1644 goto next_iter;
1645 }
1646
1647 if (options & LYD_NEW_PATH_OPAQ) {
1648 if (cur_parent && !cur_parent->schema) {
1649 /* always create opaque nodes for opaque parents */
1650 r = LY_ENOT;
1651 } else {
1652 /* validate value */
1653 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1654 }
1655 if (r && (r != LY_EINCOMPLETE)) {
1656 /* creating opaque leaf */
1657 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1658 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
1659 cleanup);
1660 break;
1661 }
1662 }
1663
1664 /* create a leaf instance */
Michal Vasko989cdb42023-10-06 15:32:37 +02001665 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 +02001666 &node), cleanup);
1667 break;
1668 case LYS_ANYDATA:
1669 case LYS_ANYXML:
1670 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
1671 break;
1672 default:
1673 LOGINT(ctx);
1674 ret = LY_EINT;
1675 goto cleanup;
1676 }
1677
1678 if (p[path_idx].ext) {
1679 node->flags |= LYD_EXT;
1680 }
1681 if (cur_parent) {
1682 /* connect to the parent */
1683 lyd_insert_node(cur_parent, NULL, node, 0);
1684 } else if (parent) {
1685 /* connect to top-level siblings */
1686 lyd_insert_node(NULL, &parent, node, 0);
1687 }
1688
1689next_iter:
1690 /* update remembered nodes */
1691 if (!nparent) {
1692 nparent = node;
1693 }
1694 nnode = node;
1695 }
1696
1697cleanup:
1698 lyxp_expr_free(ctx, exp);
1699 if (p) {
1700 while (orig_count > LY_ARRAY_COUNT(p)) {
1701 LY_ARRAY_INCREMENT(p);
1702 }
1703 }
1704 ly_path_free(ctx, p);
1705 if (!ret) {
1706 /* set out params only on success */
1707 if (new_parent) {
1708 *new_parent = nparent;
1709 }
1710 if (new_node) {
1711 *new_node = nnode;
1712 }
1713 } else {
1714 lyd_free_tree(nparent);
1715 }
1716 return ret;
1717}
1718
1719LIBYANG_API_DEF LY_ERR
1720lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1721 struct lyd_node **node)
1722{
1723 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1724 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1725 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1726
1727 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1728}
1729
1730LIBYANG_API_DEF LY_ERR
1731lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1732 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
1733 struct lyd_node **new_node)
1734{
1735 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1736 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1737 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1738
1739 return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node);
1740}
1741
1742LIBYANG_API_DEF LY_ERR
1743lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1744 uint32_t options, struct lyd_node **node)
1745{
1746 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1747
1748 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
1749 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1750 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1751
1752 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1753}
1754
1755LY_ERR
1756lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001757 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node,
1758 uint32_t impl_opts, struct lyd_node **diff)
Michal Vasko59892dd2022-05-13 11:02:30 +02001759{
1760 LY_ERR ret;
1761 const struct lysc_node *iter = NULL;
1762 struct lyd_node *node = NULL;
1763 struct lyd_value **dflts;
1764 LY_ARRAY_COUNT_TYPE u;
1765 uint32_t getnext_opts;
1766
1767 assert(first && (parent || sparent || mod));
1768
1769 if (!sparent && parent) {
1770 sparent = parent->schema;
1771 }
1772
1773 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1774 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1775 getnext_opts |= LYS_GETNEXT_OUTPUT;
1776 }
1777
1778 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
1779 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1780 continue;
1781 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1782 continue;
1783 }
1784
1785 switch (iter->nodetype) {
1786 case LYS_CHOICE:
1787 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1788 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
1789 /* create default case data */
1790 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001791 NULL, node_when, node_types, ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001792 } else if (node) {
1793 /* create any default data in the existing case */
1794 assert(node->schema->parent->nodetype == LYS_CASE);
1795 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001796 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001797 }
1798 break;
1799 case LYS_CONTAINER:
1800 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1801 /* create default NP container */
1802 LY_CHECK_RET(lyd_create_inner(iter, &node));
1803 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1804 lyd_insert_node(parent, first, node, 0);
1805
1806 if (lysc_has_when(iter) && node_when) {
1807 /* remember to resolve when */
1808 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1809 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001810 if (ext_node) {
1811 /* store for ext instance node validation, if needed */
1812 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1813 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001814 if (diff) {
1815 /* add into diff */
1816 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1817 }
1818
1819 /* create any default children */
1820 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 +02001821 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001822 }
1823 break;
1824 case LYS_LEAF:
1825 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1826 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1827 /* create default leaf */
1828 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1829 if (ret == LY_EINCOMPLETE) {
1830 if (node_types) {
1831 /* remember to resolve type */
1832 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
1833 }
1834 } else if (ret) {
1835 return ret;
1836 }
1837 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1838 lyd_insert_node(parent, first, node, 0);
1839
1840 if (lysc_has_when(iter) && node_when) {
1841 /* remember to resolve when */
1842 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1843 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001844 if (ext_node) {
1845 /* store for ext instance node validation, if needed */
1846 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1847 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001848 if (diff) {
1849 /* add into diff */
1850 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1851 }
1852 }
1853 break;
1854 case LYS_LEAFLIST:
1855 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1856 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1857 /* create all default leaf-lists */
1858 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1859 LY_ARRAY_FOR(dflts, u) {
1860 ret = lyd_create_term2(iter, dflts[u], &node);
1861 if (ret == LY_EINCOMPLETE) {
1862 if (node_types) {
1863 /* remember to resolve type */
1864 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
1865 }
1866 } else if (ret) {
1867 return ret;
1868 }
1869 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1870 lyd_insert_node(parent, first, node, 0);
1871
1872 if (lysc_has_when(iter) && node_when) {
1873 /* remember to resolve when */
1874 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1875 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001876 if (ext_node) {
1877 /* store for ext instance node validation, if needed */
1878 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1879 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001880 if (diff) {
1881 /* add into diff */
1882 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1883 }
1884 }
1885 }
1886 break;
1887 default:
1888 /* without defaults */
1889 break;
1890 }
1891 }
1892
1893 return LY_SUCCESS;
1894}
1895
1896LIBYANG_API_DEF LY_ERR
1897lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
1898{
1899 LY_ERR ret = LY_SUCCESS;
1900 struct lyd_node *node;
1901 struct ly_set node_when = {0};
1902
1903 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1904 if (diff) {
1905 *diff = NULL;
1906 }
1907
1908 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskof87fbdf2023-07-11 10:20:02 +02001909 if (node->schema->nodetype & LYD_NODE_INNER) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001910 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 +02001911 NULL, implicit_options, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001912 }
1913
1914 LYD_TREE_DFS_END(tree, node);
1915 }
1916
1917 /* resolve when and remove any invalid defaults */
Michal Vasko135719f2022-08-25 12:18:17 +02001918 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 +02001919 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001920
1921cleanup:
1922 ly_set_erase(&node_when, NULL);
1923 if (ret && diff) {
1924 lyd_free_all(*diff);
1925 *diff = NULL;
1926 }
1927 return ret;
1928}
1929
1930LIBYANG_API_DEF LY_ERR
1931lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
1932{
1933 const struct lys_module *mod;
1934 struct lyd_node *d = NULL;
1935 uint32_t i = 0;
1936 LY_ERR ret = LY_SUCCESS;
1937
1938 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1939 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
1940 if (diff) {
1941 *diff = NULL;
1942 }
1943 if (!ctx) {
1944 ctx = LYD_CTX(*tree);
1945 }
1946
1947 /* add nodes for each module one-by-one */
1948 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1949 if (!mod->implemented) {
1950 continue;
1951 }
1952
1953 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1954 if (d) {
1955 /* merge into one diff */
1956 lyd_insert_sibling(*diff, d, diff);
1957
1958 d = NULL;
1959 }
1960 }
1961
1962cleanup:
1963 if (ret && diff) {
1964 lyd_free_all(*diff);
1965 *diff = NULL;
1966 }
1967 return ret;
1968}
1969
1970LIBYANG_API_DEF LY_ERR
1971lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
1972 struct lyd_node **diff)
1973{
1974 LY_ERR ret = LY_SUCCESS;
1975 struct lyd_node *root, *d = NULL;
1976 struct ly_set node_when = {0};
1977
1978 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1979 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
1980 if (diff) {
1981 *diff = NULL;
1982 }
1983
1984 /* add all top-level defaults for this module */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001985 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, NULL, implicit_options, diff),
1986 cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001987
1988 /* resolve when and remove any invalid defaults */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001989 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL,
1990 0, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001991
1992 /* process nested nodes */
1993 LY_LIST_FOR(*tree, root) {
Michal Vaskof87fbdf2023-07-11 10:20:02 +02001994 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001995
Michal Vaskof87fbdf2023-07-11 10:20:02 +02001996 if (d) {
1997 /* merge into one diff */
1998 lyd_insert_sibling(*diff, d, diff);
1999 d = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +02002000 }
2001 }
2002
2003cleanup:
2004 ly_set_erase(&node_when, NULL);
2005 if (ret && diff) {
2006 lyd_free_all(*diff);
2007 *diff = NULL;
2008 }
2009 return ret;
2010}