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