blob: 2ff090a7e35d3eee6b590cb8d768fa9344ddc2f2 [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
54lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
55 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,
71 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
72 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,
165 var->value, strlen(var->value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, predicates[u].key, NULL);
166 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 Vasko5b414dd2023-04-13 10:29:58 +0200302 return rc;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100303}
304
Michal Vasko59892dd2022-05-13 11:02:30 +0200305LY_ERR
306lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
307 struct lyd_node **node)
308{
Michal Vasko5b414dd2023-04-13 10:29:58 +0200309 LY_ERR rc = LY_SUCCESS, r;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100310 struct lyd_node *tree;
Michal Vaskobdeee992022-12-01 16:17:56 +0100311 struct lyd_node_any *any = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +0200312 union lyd_any_value any_val;
Michal Vasko5b414dd2023-04-13 10:29:58 +0200313 struct ly_in *in = NULL;
Michal Vasko59892dd2022-05-13 11:02:30 +0200314
315 assert(schema->nodetype & LYD_NODE_ANY);
316
317 any = calloc(1, sizeof *any);
318 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
319
320 any->schema = schema;
321 any->prev = &any->node;
322 any->flags = LYD_NEW;
323
Michal Vasko5b414dd2023-04-13 10:29:58 +0200324 if (schema->nodetype == LYS_ANYDATA) {
325 /* anydata */
326 if (value_type == LYD_ANYDATA_STRING) {
327 /* detect value type */
328 lyd_create_any_string_valtype(value, &value_type);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100329 }
Michal Vasko5b414dd2023-04-13 10:29:58 +0200330
331 if (value_type != LYD_ANYDATA_DATATREE) {
332 /* create input */
333 assert(value);
334 LY_CHECK_GOTO(rc = ly_in_new_memory(value, &in), cleanup);
335
336 /* parse as a data tree */
337 if ((r = lyd_create_any_datatree(schema->module->ctx, in, value_type, 1, &tree))) {
338 LOGERR(schema->module->ctx, rc, "Failed to parse any content into a data tree.");
339 rc = r;
340 goto cleanup;
341 }
342
343 /* use the parsed data tree */
344 if (use_value) {
345 free((void *)value);
346 }
347 use_value = 1;
348 value = tree;
349 value_type = LYD_ANYDATA_DATATREE;
350 }
351 } else {
352 /* anyxml */
353 switch (value_type) {
354 case LYD_ANYDATA_DATATREE:
355 /* fine, just use the value */
356 break;
357 case LYD_ANYDATA_STRING:
358 /* detect value type */
359 lyd_create_any_string_valtype(value, &value_type);
360 if ((value_type == LYD_ANYDATA_DATATREE) || (value_type == LYD_ANYDATA_STRING)) {
361 break;
362 }
363 /* fallthrough */
364 case LYD_ANYDATA_XML:
365 case LYD_ANYDATA_JSON:
366 case LYD_ANYDATA_LYB:
367 if (!value) {
368 /* nothing to parse */
369 break;
370 }
371
372 /* create input */
373 LY_CHECK_GOTO(rc = ly_in_new_memory(value, &in), cleanup);
374
375 /* try to parse as a data tree */
376 r = lyd_create_any_datatree(schema->module->ctx, in, value_type, 0, &tree);
377 if (!r) {
378 /* use the parsed data tree */
379 if (use_value) {
380 free((void *)value);
381 }
382 use_value = 1;
383 value = tree;
384 value_type = LYD_ANYDATA_DATATREE;
385 }
386 break;
387 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100388 }
389
Michal Vasko59892dd2022-05-13 11:02:30 +0200390 if (use_value) {
391 switch (value_type) {
392 case LYD_ANYDATA_DATATREE:
393 any->value.tree = (void *)value;
394 break;
395 case LYD_ANYDATA_STRING:
396 case LYD_ANYDATA_XML:
397 case LYD_ANYDATA_JSON:
Michal Vasko5b414dd2023-04-13 10:29:58 +0200398 LY_CHECK_GOTO(rc = lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200399 break;
400 case LYD_ANYDATA_LYB:
401 any->value.mem = (void *)value;
402 break;
403 }
404 any->value_type = value_type;
405 } else {
406 any_val.str = value;
Michal Vasko5b414dd2023-04-13 10:29:58 +0200407 LY_CHECK_GOTO(rc = lyd_any_copy_value(&any->node, &any_val, value_type), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +0200408 }
409 lyd_hash(&any->node);
410
Michal Vasko5b414dd2023-04-13 10:29:58 +0200411cleanup:
412 if (rc) {
413 lyd_free_tree(&any->node);
414 } else {
415 *node = &any->node;
416 }
417 ly_in_free(in, 0);
418 return rc;
Michal Vasko59892dd2022-05-13 11:02:30 +0200419}
420
421LY_ERR
422lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
423 const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
424 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
425{
426 LY_ERR ret = LY_SUCCESS;
427 struct lyd_node_opaq *opaq;
428
429 assert(ctx && name && name_len && format);
430
431 if (!value_len && (!dynamic || !*dynamic)) {
432 value = "";
433 }
434
435 opaq = calloc(1, sizeof *opaq);
436 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
437
438 opaq->prev = &opaq->node;
439 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
440
441 if (pref_len) {
442 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
443 }
444 if (module_key_len) {
445 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
446 }
447 if (dynamic && *dynamic) {
448 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
449 *dynamic = 0;
450 } else {
451 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
452 }
453
454 opaq->format = format;
455 opaq->val_prefix_data = val_prefix_data;
456 opaq->hints = hints;
457 opaq->ctx = ctx;
458
459finish:
460 if (ret) {
461 lyd_free_tree(&opaq->node);
462 ly_free_prefix_data(format, val_prefix_data);
463 } else {
464 *node = &opaq->node;
465 }
466 return ret;
467}
468
469LIBYANG_API_DEF LY_ERR
470lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
471 struct lyd_node **node)
472{
473 LY_ERR r;
474 struct lyd_node *ret = NULL;
475 const struct lysc_node *schema;
476 struct lysc_ext_instance *ext = NULL;
477 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
478
479 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
480 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
481
482 if (!module) {
483 module = parent->schema->module;
484 }
485
486 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
487 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
488 if (!schema && parent) {
489 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
490 strlen(name), &schema, &ext);
491 LY_CHECK_RET(r && (r != LY_ENOT), r);
492 }
493 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.",
494 name), LY_ENOTFOUND);
495
496 LY_CHECK_RET(lyd_create_inner(schema, &ret));
497 if (ext) {
498 ret->flags |= LYD_EXT;
499 }
500 if (parent) {
501 lyd_insert_node(parent, NULL, ret, 0);
502 }
503
504 if (node) {
505 *node = ret;
506 }
507 return LY_SUCCESS;
508}
509
510LIBYANG_API_DEF LY_ERR
511lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
512{
513 struct lyd_node *ret = NULL;
514 const struct lysc_node *schema;
515 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
516
517 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
518
519 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
520 if (!schema) {
521 if (ext->argument) {
522 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
523 name, ext->argument, ext->def->name);
524 } else {
525 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
526 name, ext->def->name);
527 }
528 return LY_ENOTFOUND;
529 }
530 LY_CHECK_RET(lyd_create_inner(schema, &ret));
531
532 *node = ret;
533
534 return LY_SUCCESS;
535}
536
537/**
538 * @brief Create a new list node in the data tree.
539 *
540 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
541 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
542 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
543 * @param[in] format Format of key values.
544 * @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
545 * taken into consideration. Otherwise, the output's data node is going to be created.
546 * @param[out] node Optional created node.
547 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
548 * be followed by the value length.
549 * @return LY_ERR value.
550 */
551static LY_ERR
552_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
553 ly_bool output, struct lyd_node **node, va_list ap)
554{
555 struct lyd_node *ret = NULL, *key;
556 const struct lysc_node *schema, *key_s;
557 struct lysc_ext_instance *ext = NULL;
558 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
559 const void *key_val;
560 uint32_t key_len;
561 LY_ERR r, rc = LY_SUCCESS;
562
563 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
564 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
565
566 if (!module) {
567 module = parent->schema->module;
568 }
569
570 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
571 if (!schema && parent) {
572 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
573 strlen(name), &schema, &ext);
574 LY_CHECK_RET(r && (r != LY_ENOT), r);
575 }
576 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
577
578 /* create list inner node */
579 LY_CHECK_RET(lyd_create_inner(schema, &ret));
580
581 /* create and insert all the keys */
582 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
583 if (format == LY_VALUE_LYB) {
584 key_val = va_arg(ap, const void *);
585 key_len = va_arg(ap, uint32_t);
586 } else {
587 key_val = va_arg(ap, const char *);
588 key_len = key_val ? strlen((char *)key_val) : 0;
589 }
590
591 rc = lyd_create_term(key_s, key_val, key_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
592 LY_CHECK_GOTO(rc, cleanup);
593 lyd_insert_node(ret, NULL, key, 1);
594 }
595
596 if (ext) {
597 ret->flags |= LYD_EXT;
598 }
599 if (parent) {
600 lyd_insert_node(parent, NULL, ret, 0);
601 }
602
603cleanup:
604 if (rc) {
605 lyd_free_tree(ret);
606 ret = NULL;
607 } else if (node) {
608 *node = ret;
609 }
610 return rc;
611}
612
613LIBYANG_API_DEF LY_ERR
614lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
615 struct lyd_node **node, ...)
616{
617 LY_ERR rc;
618 va_list ap;
619
620 va_start(ap, node);
621
622 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
623
624 va_end(ap);
625 return rc;
626}
627
628LIBYANG_API_DEF LY_ERR
629lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
630 struct lyd_node **node, ...)
631{
632 LY_ERR rc;
633 va_list ap;
634
635 va_start(ap, node);
636
637 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
638
639 va_end(ap);
640 return rc;
641}
642
643LIBYANG_API_DEF LY_ERR
644lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
645 struct lyd_node **node, ...)
646{
647 LY_ERR rc;
648 va_list ap;
649
650 va_start(ap, node);
651
652 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
653
654 va_end(ap);
655 return rc;
656}
657
658LIBYANG_API_DEF LY_ERR
659lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
660{
661 struct lyd_node *ret = NULL, *key;
662 const struct lysc_node *schema, *key_s;
663 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
664 va_list ap;
665 const char *key_val;
666 LY_ERR rc = LY_SUCCESS;
667
668 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
669
670 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
671 if (!schema) {
672 if (ext->argument) {
673 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
674 name, ext->argument, ext->def->name);
675 } else {
676 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
677 }
678 return LY_ENOTFOUND;
679 }
680 /* create list inner node */
681 LY_CHECK_RET(lyd_create_inner(schema, &ret));
682
683 va_start(ap, node);
684
685 /* create and insert all the keys */
686 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
687 key_val = va_arg(ap, const char *);
688
689 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
690 NULL, &key);
691 LY_CHECK_GOTO(rc, cleanup);
692 lyd_insert_node(ret, NULL, key, 1);
693 }
694
695cleanup:
696 va_end(ap);
697 if (rc) {
698 lyd_free_tree(ret);
699 ret = NULL;
700 }
701 *node = ret;
702 return rc;
703}
704
705LIBYANG_API_DEF LY_ERR
706lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
707 ly_bool output, struct lyd_node **node)
708{
709 LY_ERR r;
710 struct lyd_node *ret = NULL;
711 const struct lysc_node *schema;
712 struct lysc_ext_instance *ext = NULL;
713 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
714
715 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
716 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
717
718 if (!module) {
719 module = parent->schema->module;
720 }
721 if (!keys) {
722 keys = "";
723 }
724
725 /* find schema node */
726 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
727 if (!schema && parent) {
728 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name),
729 &schema, &ext);
730 LY_CHECK_RET(r && (r != LY_ENOT), r);
731 }
732 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
733
734 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
735 /* key-less list */
736 LY_CHECK_RET(lyd_create_inner(schema, &ret));
737 } else {
738 /* create the list node */
739 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
740 }
741 if (ext) {
742 ret->flags |= LYD_EXT;
743 }
744 if (parent) {
745 lyd_insert_node(parent, NULL, ret, 0);
746 }
747
748 if (node) {
749 *node = ret;
750 }
751 return LY_SUCCESS;
752}
753
754/**
755 * @brief Create a new term node in the data tree.
756 *
757 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
758 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
759 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
760 * @param[in] value Value of the node being created.
761 * @param[in] value_len Length of @p value.
762 * @param[in] format Format of @p value.
763 * @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
764 * taken into consideration. Otherwise, the output's data node is going to be created.
765 * @param[out] node Optional created node.
766 * @return LY_ERR value.
767 */
768static LY_ERR
769_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
770 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
771{
772 LY_ERR r;
773 struct lyd_node *ret = NULL;
774 const struct lysc_node *schema;
775 struct lysc_ext_instance *ext = NULL;
776 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
777
778 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
779 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
780
781 if (!module) {
782 module = parent->schema->module;
783 }
784
785 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
786 if (!schema && parent) {
787 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
788 strlen(name), &schema, &ext);
789 LY_CHECK_RET(r && (r != LY_ENOT), r);
790 }
791 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
792
793 LY_CHECK_RET(lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret));
794 if (ext) {
795 ret->flags |= LYD_EXT;
796 }
797 if (parent) {
798 lyd_insert_node(parent, NULL, ret, 0);
799 }
800
801 if (node) {
802 *node = ret;
803 }
804 return LY_SUCCESS;
805}
806
807LIBYANG_API_DEF LY_ERR
808lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
809 ly_bool output, struct lyd_node **node)
810{
811 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
812}
813
814LIBYANG_API_DEF LY_ERR
815lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
816 size_t value_len, ly_bool output, struct lyd_node **node)
817{
818 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
819}
820
821LIBYANG_API_DEF LY_ERR
822lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
823 ly_bool output, struct lyd_node **node)
824{
825 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
826}
827
828LIBYANG_API_DEF LY_ERR
829lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
830{
831 LY_ERR rc;
832 struct lyd_node *ret = NULL;
833 const struct lysc_node *schema;
834 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
835
836 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
837
838 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
839 if (!schema) {
840 if (ext->argument) {
841 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
842 name, ext->argument, ext->def->name);
843 } else {
844 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
845 }
846 return LY_ENOTFOUND;
847 }
848 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
849 LY_CHECK_RET(rc);
850
851 *node = ret;
852
853 return LY_SUCCESS;
854}
855
856LIBYANG_API_DEF LY_ERR
857lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
858 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
859{
860 LY_ERR r;
861 struct lyd_node *ret = NULL;
862 const struct lysc_node *schema;
863 struct lysc_ext_instance *ext = NULL;
864 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
865
866 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
867 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
868
869 if (!module) {
870 module = parent->schema->module;
871 }
872
873 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
874 if (!schema && parent) {
875 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
876 strlen(name), &schema, &ext);
877 LY_CHECK_RET(r && (r != LY_ENOT), r);
878 }
879 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
880
881 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
882 if (ext) {
883 ret->flags |= LYD_EXT;
884 }
885 if (parent) {
886 lyd_insert_node(parent, NULL, ret, 0);
887 }
888
889 if (node) {
890 *node = ret;
891 }
892 return LY_SUCCESS;
893}
894
895LIBYANG_API_DEF LY_ERR
896lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
897 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
898{
899 struct lyd_node *ret = NULL;
900 const struct lysc_node *schema;
901 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
902
903 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
904
905 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
906 if (!schema) {
907 if (ext->argument) {
908 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
909 name, ext->argument, ext->def->name);
910 } else {
911 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
912 }
913 return LY_ENOTFOUND;
914 }
915 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
916
917 *node = ret;
918
919 return LY_SUCCESS;
920}
921
922LIBYANG_API_DEF LY_ERR
923lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
924 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
925{
926 const char *prefix, *tmp;
927 size_t pref_len, name_len;
928
929 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
930 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
931 if (!ctx) {
Michal Vasko33c48972022-07-20 10:28:07 +0200932 ctx = module ? module->ctx : LYD_CTX(parent);
Michal Vasko59892dd2022-05-13 11:02:30 +0200933 }
934
935 if (parent && !parent->schema) {
936 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
937 return LY_EINVAL;
938 }
939 if (meta) {
940 *meta = NULL;
941 }
942
943 /* parse the name */
944 tmp = name;
945 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
946 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
947 return LY_EINVAL;
948 }
949
950 /* find the module */
951 if (prefix) {
952 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
953 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND);
954 }
955
956 /* set value if none */
957 if (!val_str) {
958 val_str = "";
959 }
960
961 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
962 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
963}
964
965LIBYANG_API_DEF LY_ERR
966lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
967 struct lyd_meta **meta)
968{
969 const struct lys_module *mod;
970
971 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
972 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
973
974 if (parent && !parent->schema) {
975 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
976 return LY_EINVAL;
977 }
978 if (meta) {
979 *meta = NULL;
980 }
981
982 switch (attr->format) {
983 case LY_VALUE_XML:
984 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
985 if (!mod) {
986 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
987 return LY_ENOTFOUND;
988 }
989 break;
990 case LY_VALUE_JSON:
991 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
992 if (!mod) {
993 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
994 return LY_ENOTFOUND;
995 }
996 break;
997 default:
998 LOGINT_RET(ctx);
999 }
1000
1001 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
1002 NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
1003}
1004
1005LIBYANG_API_DEF LY_ERR
1006lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
1007 const char *prefix, const char *module_name, struct lyd_node **node)
1008{
1009 struct lyd_node *ret = NULL;
1010
1011 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
1012 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1013
1014 if (!ctx) {
1015 ctx = LYD_CTX(parent);
1016 }
1017 if (!value) {
1018 value = "";
1019 }
1020
1021 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1022 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
1023 if (parent) {
1024 lyd_insert_node(parent, NULL, ret, 1);
1025 }
1026
1027 if (node) {
1028 *node = ret;
1029 }
1030 return LY_SUCCESS;
1031}
1032
1033LIBYANG_API_DEF LY_ERR
1034lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
1035 const char *prefix, const char *module_ns, struct lyd_node **node)
1036{
1037 struct lyd_node *ret = NULL;
1038
1039 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1040 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
1041
1042 if (!ctx) {
1043 ctx = LYD_CTX(parent);
1044 }
1045 if (!value) {
1046 value = "";
1047 }
1048
1049 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1050 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
1051 if (parent) {
1052 lyd_insert_node(parent, NULL, ret, 1);
1053 }
1054
1055 if (node) {
1056 *node = ret;
1057 }
1058 return LY_SUCCESS;
1059}
1060
1061LIBYANG_API_DEF LY_ERR
1062lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
1063 struct lyd_attr **attr)
1064{
1065 struct lyd_attr *ret = NULL;
1066 const struct ly_ctx *ctx;
1067 const char *prefix, *tmp;
1068 size_t pref_len, name_len, mod_len;
1069
1070 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1071
1072 ctx = LYD_CTX(parent);
1073
1074 /* parse the name */
1075 tmp = name;
1076 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1077 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1078 return LY_EVALID;
1079 }
1080
1081 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1082 /* not a prefix but special name */
1083 name = prefix;
1084 name_len += 1 + pref_len;
1085 prefix = NULL;
1086 pref_len = 0;
1087 }
1088
1089 /* get the module */
1090 if (module_name) {
1091 mod_len = strlen(module_name);
1092 } else {
1093 module_name = prefix;
1094 mod_len = pref_len;
1095 }
1096
1097 /* set value if none */
1098 if (!value) {
1099 value = "";
1100 }
1101
1102 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1103 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
1104
1105 if (attr) {
1106 *attr = ret;
1107 }
1108 return LY_SUCCESS;
1109}
1110
1111LIBYANG_API_DEF LY_ERR
1112lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1113 struct lyd_attr **attr)
1114{
1115 struct lyd_attr *ret = NULL;
1116 const struct ly_ctx *ctx;
1117 const char *prefix, *tmp;
1118 size_t pref_len, name_len;
1119
1120 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1121
1122 ctx = LYD_CTX(parent);
1123
1124 /* parse the name */
1125 tmp = name;
1126 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1127 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1128 return LY_EVALID;
1129 }
1130
1131 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1132 /* not a prefix but special name */
1133 name = prefix;
1134 name_len += 1 + pref_len;
1135 prefix = NULL;
1136 pref_len = 0;
1137 }
1138
1139 /* set value if none */
1140 if (!value) {
1141 value = "";
1142 }
Michal Vasko825718f2022-11-10 10:33:51 +01001143 if (strchr(value, ':')) {
1144 LOGWRN(ctx, "Value \"%s\" prefix will never be interpreted as an XML prefix.", value);
1145 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001146
1147 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1148 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
1149
1150 if (attr) {
1151 *attr = ret;
1152 }
1153 return LY_SUCCESS;
1154}
1155
1156/**
1157 * @brief Change the value of a term (leaf or leaf-list) node.
1158 *
1159 * Node changed this way is always considered explicitly set, meaning its default flag
1160 * is always cleared.
1161 *
1162 * @param[in] term Term node to change.
1163 * @param[in] value New value to set.
1164 * @param[in] value_len Length of @p value.
1165 * @param[in] format Format of @p value.
1166 * @return LY_SUCCESS if value was changed,
1167 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1168 * @return LY_ENOT if the values were equal and no change occured,
1169 * @return LY_ERR value on other errors.
1170 */
1171static LY_ERR
1172_lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format)
1173{
1174 LY_ERR ret = LY_SUCCESS;
1175 struct lysc_type *type;
1176 struct lyd_node_term *t;
1177 struct lyd_node *parent;
1178 struct lyd_value val;
1179 ly_bool dflt_change, val_change;
1180
1181 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
1182
1183 t = (struct lyd_node_term *)term;
1184 type = ((struct lysc_node_leaf *)term->schema)->type;
1185
1186 /* parse the new value */
1187 LOG_LOCSET(term->schema, term, NULL, NULL);
1188 ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, NULL, format, NULL, LYD_HINT_DATA, term->schema, NULL);
1189 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
1190 LY_CHECK_GOTO(ret, cleanup);
1191
1192 /* compare original and new value */
1193 if (type->plugin->compare(&t->value, &val)) {
1194 /* values differ, switch them */
1195 type->plugin->free(LYD_CTX(term), &t->value);
1196 t->value = val;
1197 val_change = 1;
1198 } else {
1199 /* same values, free the new stored one */
1200 type->plugin->free(LYD_CTX(term), &val);
1201 val_change = 0;
1202 }
1203
1204 /* always clear the default flag */
1205 if (term->flags & LYD_DEFAULT) {
1206 for (parent = term; parent; parent = lyd_parent(parent)) {
1207 parent->flags &= ~LYD_DEFAULT;
1208 }
1209 dflt_change = 1;
1210 } else {
1211 dflt_change = 0;
1212 }
1213
1214 if (val_change || dflt_change) {
1215 /* make the node non-validated */
1216 term->flags &= LYD_NEW;
1217 }
1218
1219 if (val_change) {
1220 if (term->schema->nodetype == LYS_LEAFLIST) {
1221 /* leaf-list needs to be hashed again and re-inserted into parent */
1222 lyd_unlink_hash(term);
1223 lyd_hash(term);
1224 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1225 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1226 /* list needs to be updated if its key was changed */
1227 assert(term->parent->schema->nodetype == LYS_LIST);
1228 lyd_unlink_hash(lyd_parent(term));
1229 lyd_hash(lyd_parent(term));
1230 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
1231 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1232 }
1233
1234 /* retrun value */
1235 if (!val_change) {
1236 if (dflt_change) {
1237 /* only default flag change */
1238 ret = LY_EEXIST;
1239 } else {
1240 /* no change */
1241 ret = LY_ENOT;
1242 }
1243 } /* else value changed, LY_SUCCESS */
1244
1245cleanup:
1246 return ret;
1247}
1248
1249LIBYANG_API_DEF LY_ERR
1250lyd_change_term(struct lyd_node *term, const char *val_str)
1251{
1252 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1253
1254 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1255}
1256
1257LIBYANG_API_DEF LY_ERR
1258lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1259{
1260 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1261
1262 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1263}
1264
1265LIBYANG_API_DEF LY_ERR
1266lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1267{
1268 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1269
1270 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1271}
1272
1273LIBYANG_API_DEF LY_ERR
1274lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1275{
1276 LY_ERR ret = LY_SUCCESS;
1277 struct lyd_meta *m2 = NULL;
1278 struct lyd_value val;
1279 ly_bool val_change;
1280
1281 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1282
1283 if (!val_str) {
1284 val_str = "";
1285 }
1286
1287 /* parse the new value into a new meta structure */
1288 ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
1289 NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
1290 LY_CHECK_GOTO(ret, cleanup);
1291
1292 /* compare original and new value */
1293 if (lyd_compare_meta(meta, m2)) {
1294 /* values differ, switch them */
1295 val = meta->value;
1296 meta->value = m2->value;
1297 m2->value = val;
1298 val_change = 1;
1299 } else {
1300 val_change = 0;
1301 }
1302
1303 /* retrun value */
1304 if (!val_change) {
1305 /* no change */
1306 ret = LY_ENOT;
1307 } /* else value changed, LY_SUCCESS */
1308
1309cleanup:
1310 lyd_free_meta_single(m2);
1311 return ret;
1312}
1313
1314/**
1315 * @brief Update node value.
1316 *
1317 * @param[in] node Node to update.
1318 * @param[in] value New value to set.
1319 * @param[in] value_len Length of @p value.
1320 * @param[in] value_type Type of @p value for anydata/anyxml node.
1321 * @param[in] format Format of @p value.
1322 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1323 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1324 * @return LY_ERR value.
1325 */
1326static LY_ERR
1327lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1328 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1329{
1330 LY_ERR ret = LY_SUCCESS;
1331 struct lyd_node *new_any;
1332
1333 switch (node->schema->nodetype) {
1334 case LYS_CONTAINER:
1335 case LYS_NOTIF:
1336 case LYS_RPC:
1337 case LYS_ACTION:
1338 case LYS_LIST:
1339 /* if it exists, there is nothing to update */
1340 *new_parent = NULL;
1341 *new_node = NULL;
1342 break;
1343 case LYS_LEAFLIST:
1344 if (!lysc_is_dup_inst_list(node->schema)) {
1345 /* if it exists, there is nothing to update */
1346 *new_parent = NULL;
1347 *new_node = NULL;
1348 break;
1349 }
1350 /* fallthrough */
1351 case LYS_LEAF:
1352 ret = _lyd_change_term(node, value, value_len, format);
1353 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1354 /* there was an actual change (at least of the default flag) */
1355 *new_parent = node;
1356 *new_node = node;
1357 ret = LY_SUCCESS;
1358 } else if (ret == LY_ENOT) {
1359 /* no change */
1360 *new_parent = NULL;
1361 *new_node = NULL;
1362 ret = LY_SUCCESS;
1363 } /* else error */
1364 break;
1365 case LYS_ANYDATA:
1366 case LYS_ANYXML:
1367 /* create a new any node */
1368 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1369
1370 /* compare with the existing one */
1371 if (lyd_compare_single(node, new_any, 0)) {
1372 /* not equal, switch values (so that we can use generic node free) */
1373 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1374 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1375 ((struct lyd_node_any *)node)->value.str = value;
1376 ((struct lyd_node_any *)node)->value_type = value_type;
1377
1378 *new_parent = node;
1379 *new_node = node;
1380 } else {
1381 /* they are equal */
1382 *new_parent = NULL;
1383 *new_node = NULL;
1384 }
1385 lyd_free_tree(new_any);
1386 break;
1387 default:
1388 LOGINT(LYD_CTX(node));
1389 ret = LY_EINT;
1390 break;
1391 }
1392
1393 return ret;
1394}
1395
1396static LY_ERR
1397lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1398 LY_VALUE_FORMAT format, uint32_t options)
1399{
1400 LY_ERR r;
1401 struct ly_path_predicate *pred;
1402 struct lyd_value val;
1403 const struct lysc_node *schema = NULL;
1404 LY_ARRAY_COUNT_TYPE u, new_count;
1405 int create = 0;
1406
1407 assert(path);
1408
1409 /* go through all the compiled nodes */
1410 LY_ARRAY_FOR(path, u) {
1411 schema = path[u].node;
1412
1413 if (lysc_is_dup_inst_list(schema)) {
Michal Vasko5de21542023-03-20 10:00:05 +01001414 if (!path[u].predicates ||
1415 ((schema->nodetype == LYS_LEAFLIST) && (path[u].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001416 /* creating a new key-less list or state leaf-list instance */
1417 create = 1;
1418 new_count = u;
Michal Vasko90189962023-02-28 12:10:34 +01001419 } else if (path[u].predicates[0].type != LY_PATH_PREDTYPE_POSITION) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001420 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vasko5de21542023-03-20 10:00:05 +01001421 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for state %s \"%s\" in path \"%s\".",
Michal Vasko59892dd2022-05-13 11:02:30 +02001422 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1423 LOG_LOCBACK(1, 0, 0, 0);
1424 return LY_EINVAL;
1425 }
Michal Vasko90189962023-02-28 12:10:34 +01001426 } else if ((schema->nodetype == LYS_LIST) &&
1427 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001428 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1429 LOG_LOCSET(schema, NULL, NULL, NULL);
1430 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1431 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1432 LOG_LOCBACK(1, 0, 0, 0);
1433 return LY_EINVAL;
1434 } /* else creating an opaque list */
Michal Vasko90189962023-02-28 12:10:34 +01001435 } else if ((schema->nodetype == LYS_LEAFLIST) &&
1436 (!path[u].predicates || (path[u].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001437 r = LY_SUCCESS;
1438 if (options & LYD_NEW_PATH_OPAQ) {
1439 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
1440 }
1441 if (!r) {
1442 /* try to store the value */
1443 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
1444 value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
1445 ++((struct lysc_type *)val.realtype)->refcount;
1446
1447 /* store the new predicate so that it is used when searching for this instance */
Michal Vasko59892dd2022-05-13 11:02:30 +02001448 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vasko90189962023-02-28 12:10:34 +01001449 pred->type = LY_PATH_PREDTYPE_LEAFLIST;
Michal Vasko59892dd2022-05-13 11:02:30 +02001450 pred->value = val;
1451 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1452 }
1453 }
1454
1455 if (create) {
1456 /* hide the nodes that should always be created so they are not found */
1457 while (new_count < LY_ARRAY_COUNT(path)) {
1458 LY_ARRAY_DECREMENT(path);
1459 }
1460 }
1461
1462 return LY_SUCCESS;
1463}
1464
1465/**
1466 * @brief Create a new node in the data tree based on a path. All node types can be created.
1467 *
1468 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1469 * Also, if a leaf-list is being created and both a predicate is defined in @p path
1470 * and @p value is set, the predicate is preferred.
1471 *
1472 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1473 * nodes, they are always created.
1474 *
1475 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1476 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1477 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1478 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1479 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1480 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1481 * searching for the appropriate module.
1482 * @param[in] path [Path](@ref howtoXPath) to create.
1483 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1484 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1485 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1486 * creating anyxml/anydata nodes.
1487 * @param[in] value_type Anyxml/anydata node @p value type.
1488 * @param[in] options Bitmask of options, see @ref pathoptions.
1489 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1490 * @param[out] new_node Optional last node created.
1491 * @return LY_ERR value.
1492 */
1493static LY_ERR
1494lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
1495 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
1496 struct lyd_node **new_parent, struct lyd_node **new_node)
1497{
1498 LY_ERR ret = LY_SUCCESS, r;
1499 struct lyxp_expr *exp = NULL;
1500 struct ly_path *p = NULL;
1501 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1502 const struct lysc_node *schema;
1503 const struct lyd_value *val = NULL;
1504 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
1505 LY_VALUE_FORMAT format;
1506
1507 assert(parent || ctx);
1508 assert(path && ((path[0] == '/') || parent));
1509 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
1510
1511 if (!ctx) {
1512 ctx = LYD_CTX(parent);
1513 }
1514 if (value && !value_len) {
1515 value_len = strlen(value);
1516 }
1517 if (options & LYD_NEW_PATH_BIN_VALUE) {
1518 format = LY_VALUE_LYB;
1519 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1520 format = LY_VALUE_CANON;
1521 } else {
1522 format = LY_VALUE_JSON;
1523 }
1524
1525 /* parse path */
1526 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1527 LY_PATH_PRED_SIMPLE, &exp), cleanup);
1528
1529 /* compile path */
1530 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1531 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
1532
1533 /* check the compiled path before searching existing nodes, it may be shortened */
1534 orig_count = LY_ARRAY_COUNT(p);
1535 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup);
1536
1537 /* try to find any existing nodes in the path */
1538 if (parent) {
Michal Vaskof2560512023-03-03 09:54:47 +01001539 r = ly_path_eval_partial(p, parent, NULL, &path_idx, &node);
1540 if (r == LY_SUCCESS) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001541 if (orig_count == LY_ARRAY_COUNT(p)) {
1542 /* the node exists, are we supposed to update it or is it just a default? */
1543 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1544 LOG_LOCSET(NULL, node, NULL, NULL);
Michal Vaskoa9764922023-02-09 13:59:07 +01001545 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists.", path);
Michal Vasko59892dd2022-05-13 11:02:30 +02001546 LOG_LOCBACK(0, 1, 0, 0);
1547 ret = LY_EEXIST;
1548 goto cleanup;
1549 }
1550
1551 /* update the existing node */
1552 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
1553 goto cleanup;
1554 } /* else we were not searching for the whole path */
Michal Vaskof2560512023-03-03 09:54:47 +01001555 } else if (r == LY_EINCOMPLETE) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001556 /* some nodes were found, adjust the iterator to the next segment */
1557 ++path_idx;
Michal Vaskof2560512023-03-03 09:54:47 +01001558 } else if (r == LY_ENOTFOUND) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001559 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
1560 if (lysc_data_parent(p[0].node)) {
1561 node = parent;
1562 }
1563 } else {
1564 /* error */
Michal Vaskof2560512023-03-03 09:54:47 +01001565 ret = r;
Michal Vasko59892dd2022-05-13 11:02:30 +02001566 goto cleanup;
1567 }
1568 }
1569
1570 /* restore the full path for creating new nodes */
1571 while (orig_count > LY_ARRAY_COUNT(p)) {
1572 LY_ARRAY_INCREMENT(p);
1573 }
1574
1575 /* create all the non-existing nodes in a loop */
1576 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
1577 cur_parent = node;
1578 schema = p[path_idx].node;
1579
1580 switch (schema->nodetype) {
1581 case LYS_LIST:
1582 if (lysc_is_dup_inst_list(schema)) {
1583 /* create key-less list instance */
1584 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
Michal Vasko90189962023-02-28 12:10:34 +01001585 } else if ((options & LYD_NEW_PATH_OPAQ) && !p[path_idx].predicates) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001586 /* creating opaque list without keys */
1587 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1588 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
1589 LYD_NODEHINT_LIST, &node), cleanup);
1590 } else {
1591 /* create standard list instance */
Michal Vasko90189962023-02-28 12:10:34 +01001592 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, NULL, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001593 }
1594 break;
1595 case LYS_CONTAINER:
1596 case LYS_NOTIF:
1597 case LYS_RPC:
1598 case LYS_ACTION:
1599 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1600 break;
1601 case LYS_LEAFLIST:
Michal Vasko90189962023-02-28 12:10:34 +01001602 if ((options & LYD_NEW_PATH_OPAQ) &&
1603 (!p[path_idx].predicates || (p[path_idx].predicates[0].type != LY_PATH_PREDTYPE_LEAFLIST))) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001604 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1605 r = LY_EVALID;
1606 if (lysc_is_dup_inst_list(schema)) {
1607 /* validate value */
1608 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1609 }
1610 if (r && (r != LY_EINCOMPLETE)) {
1611 /* creating opaque leaf-list */
1612 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1613 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1614 LYD_NODEHINT_LEAFLIST, &node), cleanup);
1615 break;
1616 }
1617 }
1618
1619 /* get value to set */
Michal Vasko90189962023-02-28 12:10:34 +01001620 if (p[path_idx].predicates && (p[path_idx].predicates[0].type == LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001621 val = &p[path_idx].predicates[0].value;
1622 }
1623
1624 /* create a leaf-list instance */
1625 if (val) {
Michal Vasko90189962023-02-28 12:10:34 +01001626 LY_CHECK_GOTO(ret = lyd_create_term2(schema, val, &node), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001627 } else {
1628 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1629 NULL, &node), cleanup);
1630 }
1631 break;
1632 case LYS_LEAF:
1633 if (lysc_is_key(schema) && cur_parent->schema) {
1634 /* it must have been already created or some error will occur later */
1635 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1636 assert(node);
1637 goto next_iter;
1638 }
1639
1640 if (options & LYD_NEW_PATH_OPAQ) {
1641 if (cur_parent && !cur_parent->schema) {
1642 /* always create opaque nodes for opaque parents */
1643 r = LY_ENOT;
1644 } else {
1645 /* validate value */
1646 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1647 }
1648 if (r && (r != LY_EINCOMPLETE)) {
1649 /* creating opaque leaf */
1650 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1651 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
1652 cleanup);
1653 break;
1654 }
1655 }
1656
1657 /* create a leaf instance */
1658 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
1659 &node), cleanup);
1660 break;
1661 case LYS_ANYDATA:
1662 case LYS_ANYXML:
1663 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
1664 break;
1665 default:
1666 LOGINT(ctx);
1667 ret = LY_EINT;
1668 goto cleanup;
1669 }
1670
1671 if (p[path_idx].ext) {
1672 node->flags |= LYD_EXT;
1673 }
1674 if (cur_parent) {
1675 /* connect to the parent */
1676 lyd_insert_node(cur_parent, NULL, node, 0);
1677 } else if (parent) {
1678 /* connect to top-level siblings */
1679 lyd_insert_node(NULL, &parent, node, 0);
1680 }
1681
1682next_iter:
1683 /* update remembered nodes */
1684 if (!nparent) {
1685 nparent = node;
1686 }
1687 nnode = node;
1688 }
1689
1690cleanup:
1691 lyxp_expr_free(ctx, exp);
1692 if (p) {
1693 while (orig_count > LY_ARRAY_COUNT(p)) {
1694 LY_ARRAY_INCREMENT(p);
1695 }
1696 }
1697 ly_path_free(ctx, p);
1698 if (!ret) {
1699 /* set out params only on success */
1700 if (new_parent) {
1701 *new_parent = nparent;
1702 }
1703 if (new_node) {
1704 *new_node = nnode;
1705 }
1706 } else {
1707 lyd_free_tree(nparent);
1708 }
1709 return ret;
1710}
1711
1712LIBYANG_API_DEF LY_ERR
1713lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1714 struct lyd_node **node)
1715{
1716 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1717 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1718 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1719
1720 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1721}
1722
1723LIBYANG_API_DEF LY_ERR
1724lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1725 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
1726 struct lyd_node **new_node)
1727{
1728 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1729 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1730 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1731
1732 return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node);
1733}
1734
1735LIBYANG_API_DEF LY_ERR
1736lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1737 uint32_t options, struct lyd_node **node)
1738{
1739 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1740
1741 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
1742 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1743 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
1744
1745 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
1746}
1747
1748LY_ERR
1749lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001750 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node,
1751 uint32_t impl_opts, struct lyd_node **diff)
Michal Vasko59892dd2022-05-13 11:02:30 +02001752{
1753 LY_ERR ret;
1754 const struct lysc_node *iter = NULL;
1755 struct lyd_node *node = NULL;
1756 struct lyd_value **dflts;
1757 LY_ARRAY_COUNT_TYPE u;
1758 uint32_t getnext_opts;
1759
1760 assert(first && (parent || sparent || mod));
1761
1762 if (!sparent && parent) {
1763 sparent = parent->schema;
1764 }
1765
1766 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1767 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1768 getnext_opts |= LYS_GETNEXT_OUTPUT;
1769 }
1770
1771 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
1772 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1773 continue;
1774 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1775 continue;
1776 }
1777
1778 switch (iter->nodetype) {
1779 case LYS_CHOICE:
1780 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1781 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
1782 /* create default case data */
1783 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001784 NULL, node_when, node_types, ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001785 } else if (node) {
1786 /* create any default data in the existing case */
1787 assert(node->schema->parent->nodetype == LYS_CASE);
1788 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001789 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001790 }
1791 break;
1792 case LYS_CONTAINER:
1793 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1794 /* create default NP container */
1795 LY_CHECK_RET(lyd_create_inner(iter, &node));
1796 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1797 lyd_insert_node(parent, first, node, 0);
1798
1799 if (lysc_has_when(iter) && node_when) {
1800 /* remember to resolve when */
1801 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1802 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001803 if (ext_node) {
1804 /* store for ext instance node validation, if needed */
1805 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1806 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001807 if (diff) {
1808 /* add into diff */
1809 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1810 }
1811
1812 /* create any default children */
1813 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 +02001814 ext_node, impl_opts, diff));
Michal Vasko59892dd2022-05-13 11:02:30 +02001815 }
1816 break;
1817 case LYS_LEAF:
1818 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1819 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1820 /* create default leaf */
1821 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1822 if (ret == LY_EINCOMPLETE) {
1823 if (node_types) {
1824 /* remember to resolve type */
1825 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
1826 }
1827 } else if (ret) {
1828 return ret;
1829 }
1830 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1831 lyd_insert_node(parent, first, node, 0);
1832
1833 if (lysc_has_when(iter) && node_when) {
1834 /* remember to resolve when */
1835 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1836 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001837 if (ext_node) {
1838 /* store for ext instance node validation, if needed */
1839 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1840 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001841 if (diff) {
1842 /* add into diff */
1843 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1844 }
1845 }
1846 break;
1847 case LYS_LEAFLIST:
1848 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1849 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1850 /* create all default leaf-lists */
1851 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1852 LY_ARRAY_FOR(dflts, u) {
1853 ret = lyd_create_term2(iter, dflts[u], &node);
1854 if (ret == LY_EINCOMPLETE) {
1855 if (node_types) {
1856 /* remember to resolve type */
1857 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
1858 }
1859 } else if (ret) {
1860 return ret;
1861 }
1862 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
1863 lyd_insert_node(parent, first, node, 0);
1864
1865 if (lysc_has_when(iter) && node_when) {
1866 /* remember to resolve when */
1867 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1868 }
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001869 if (ext_node) {
1870 /* store for ext instance node validation, if needed */
1871 LY_CHECK_RET(lyd_validate_node_ext(node, ext_node));
1872 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001873 if (diff) {
1874 /* add into diff */
1875 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1876 }
1877 }
1878 }
1879 break;
1880 default:
1881 /* without defaults */
1882 break;
1883 }
1884 }
1885
1886 return LY_SUCCESS;
1887}
1888
1889LIBYANG_API_DEF LY_ERR
1890lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
1891{
1892 LY_ERR ret = LY_SUCCESS;
1893 struct lyd_node *node;
1894 struct ly_set node_when = {0};
1895
1896 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1897 if (diff) {
1898 *diff = NULL;
1899 }
1900
1901 LYD_TREE_DFS_BEGIN(tree, node) {
1902 /* skip added default nodes */
1903 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1904 (node->schema->nodetype & LYD_NODE_INNER)) {
1905 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 +02001906 NULL, implicit_options, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001907 }
1908
1909 LYD_TREE_DFS_END(tree, node);
1910 }
1911
1912 /* resolve when and remove any invalid defaults */
Michal Vasko135719f2022-08-25 12:18:17 +02001913 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 +02001914 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001915
1916cleanup:
1917 ly_set_erase(&node_when, NULL);
1918 if (ret && diff) {
1919 lyd_free_all(*diff);
1920 *diff = NULL;
1921 }
1922 return ret;
1923}
1924
1925LIBYANG_API_DEF LY_ERR
1926lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
1927{
1928 const struct lys_module *mod;
1929 struct lyd_node *d = NULL;
1930 uint32_t i = 0;
1931 LY_ERR ret = LY_SUCCESS;
1932
1933 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1934 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
1935 if (diff) {
1936 *diff = NULL;
1937 }
1938 if (!ctx) {
1939 ctx = LYD_CTX(*tree);
1940 }
1941
1942 /* add nodes for each module one-by-one */
1943 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1944 if (!mod->implemented) {
1945 continue;
1946 }
1947
1948 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1949 if (d) {
1950 /* merge into one diff */
1951 lyd_insert_sibling(*diff, d, diff);
1952
1953 d = NULL;
1954 }
1955 }
1956
1957cleanup:
1958 if (ret && diff) {
1959 lyd_free_all(*diff);
1960 *diff = NULL;
1961 }
1962 return ret;
1963}
1964
1965LIBYANG_API_DEF LY_ERR
1966lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
1967 struct lyd_node **diff)
1968{
1969 LY_ERR ret = LY_SUCCESS;
1970 struct lyd_node *root, *d = NULL;
1971 struct ly_set node_when = {0};
1972
1973 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1974 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
1975 if (diff) {
1976 *diff = NULL;
1977 }
1978
1979 /* add all top-level defaults for this module */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001980 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, NULL, implicit_options, diff),
1981 cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001982
1983 /* resolve when and remove any invalid defaults */
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001984 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, 0, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, NULL,
1985 0, diff), cleanup);
Michal Vasko59892dd2022-05-13 11:02:30 +02001986
1987 /* process nested nodes */
1988 LY_LIST_FOR(*tree, root) {
1989 /* skip added default nodes */
1990 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1991 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1992
1993 if (d) {
1994 /* merge into one diff */
1995 lyd_insert_sibling(*diff, d, diff);
1996
1997 d = NULL;
1998 }
1999 }
2000 }
2001
2002cleanup:
2003 ly_set_erase(&node_when, NULL);
2004 if (ret && diff) {
2005 lyd_free_all(*diff);
2006 *diff = NULL;
2007 }
2008 return ret;
2009}