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