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