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