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