Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2 | * @file tree_data.c |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 4 | * @brief Data tree functions |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 21 | #include <inttypes.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
| 23 | #include <stdint.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <string.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 29 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "context.h" |
| 31 | #include "dict.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 32 | #include "diff.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 33 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 34 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 35 | #include "in_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 37 | #include "parser_data.h" |
| 38 | #include "parser_internal.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 39 | #include "path.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 40 | #include "plugins.h" |
Radek Krejci | f1ca0ac | 2021-04-12 16:00:06 +0200 | [diff] [blame] | 41 | #include "plugins_exts/metadata.h" |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 42 | #include "plugins_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 43 | #include "plugins_types.h" |
| 44 | #include "set.h" |
| 45 | #include "tree.h" |
| 46 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 47 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 48 | #include "tree_schema.h" |
| 49 | #include "tree_schema_internal.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 50 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 51 | #include "xml.h" |
| 52 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 54 | static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 55 | struct lyd_node **match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 56 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 57 | LY_ERR |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 58 | lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 59 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 60 | const struct lysc_node *ctx_node, ly_bool *incomplete) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 61 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 62 | LY_ERR ret; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 63 | struct ly_err_item *err = NULL; |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 64 | uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 65 | |
Michal Vasko | 62ed865 | 2021-04-28 09:38:37 +0200 | [diff] [blame] | 66 | if (!value) { |
| 67 | value = ""; |
| 68 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 69 | if (incomplete) { |
| 70 | *incomplete = 0; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 73 | ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err); |
Radek Iša | 96b2de1 | 2021-02-16 10:32:57 +0100 | [diff] [blame] | 74 | if (dynamic) { |
| 75 | *dynamic = 0; |
| 76 | } |
| 77 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 78 | if (ret == LY_EINCOMPLETE) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 79 | if (incomplete) { |
| 80 | *incomplete = 1; |
| 81 | } |
| 82 | } else if (ret) { |
| 83 | if (err) { |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 84 | LOGVAL_ERRITEM(ctx, err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 85 | ly_err_free(err); |
| 86 | } else { |
Michal Vasko | 509d721 | 2022-02-21 08:47:23 +0100 | [diff] [blame] | 87 | LOGVAL(ctx, LYVE_OTHER, "Storing value failed."); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 88 | } |
| 89 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 92 | return LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 95 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 96 | lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 97 | const struct lyd_node *ctx_node, const struct lyd_node *tree) |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 98 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 99 | LY_ERR ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 100 | struct ly_err_item *err = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 101 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 102 | assert(type->plugin->validate); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 103 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 104 | ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err); |
| 105 | if (ret) { |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 106 | if (err) { |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 107 | LOGVAL_ERRITEM(ctx, err); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 108 | ly_err_free(err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 109 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 110 | LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON, |
| 111 | NULL, NULL, NULL)); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 112 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 113 | return ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 116 | return LY_SUCCESS; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 119 | LY_ERR |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 120 | lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 121 | LY_VALUE_FORMAT format, void *prefix_data) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 122 | { |
| 123 | LY_ERR rc = LY_SUCCESS; |
| 124 | struct ly_err_item *err = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 125 | struct lyd_value storage; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 126 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 127 | |
| 128 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 129 | |
| 130 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 131 | LOGARG(ctx, node); |
| 132 | return LY_EINVAL; |
| 133 | } |
| 134 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 135 | type = ((struct lysc_node_leaf *)node)->type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 136 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 137 | LYD_HINT_SCHEMA, node, &storage, NULL, &err); |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 138 | if (rc == LY_EINCOMPLETE) { |
| 139 | /* actually success since we do not provide the context tree and call validation with |
| 140 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 141 | rc = LY_SUCCESS; |
| 142 | } else if (rc && err) { |
| 143 | if (ctx) { |
| 144 | /* log only in case the ctx was provided as input parameter */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 145 | LOG_LOCSET(NULL, NULL, err->path, NULL); |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 146 | LOGVAL_ERRITEM(ctx, err); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 147 | LOG_LOCBACK(0, 0, 1, 0); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 148 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 149 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 152 | if (!rc) { |
| 153 | type->plugin->free(ctx ? ctx : node->module->ctx, &storage); |
| 154 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 155 | return rc; |
| 156 | } |
| 157 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 158 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 159 | lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len, |
| 160 | const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 161 | { |
| 162 | LY_ERR rc; |
| 163 | struct ly_err_item *err = NULL; |
| 164 | struct lysc_type *type; |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 165 | struct lyd_value val = {0}; |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 166 | ly_bool stored = 0, log = 1; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 167 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 168 | LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 169 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 170 | if (!ctx) { |
| 171 | ctx = schema->module->ctx; |
| 172 | log = 0; |
| 173 | } |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 174 | type = ((struct lysc_node_leaf *)schema)->type; |
| 175 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 176 | /* store */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 177 | rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL, |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 178 | LYD_HINT_DATA, schema, &val, NULL, &err); |
| 179 | if (!rc || (rc == LY_EINCOMPLETE)) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 180 | stored = 1; |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 181 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 182 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 183 | if (ctx_node && (rc == LY_EINCOMPLETE)) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 184 | /* resolve */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 185 | rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 188 | if (rc && (rc != LY_EINCOMPLETE) && err) { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 189 | if (log) { |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 190 | /* log error */ |
| 191 | if (err->path) { |
| 192 | LOG_LOCSET(NULL, NULL, err->path, NULL); |
| 193 | } else if (ctx_node) { |
| 194 | LOG_LOCSET(NULL, ctx_node, NULL, NULL); |
| 195 | } else { |
| 196 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 197 | } |
aPiecek | 4204b61 | 2021-05-03 09:06:16 +0200 | [diff] [blame] | 198 | LOGVAL_ERRITEM(ctx, err); |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 199 | if (err->path) { |
| 200 | LOG_LOCBACK(0, 0, 1, 0); |
| 201 | } else if (ctx_node) { |
| 202 | LOG_LOCBACK(0, 1, 0, 0); |
| 203 | } else { |
| 204 | LOG_LOCBACK(1, 0, 0, 0); |
| 205 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 206 | } |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 207 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 210 | if (!rc || (rc == LY_EINCOMPLETE)) { |
| 211 | if (realtype) { |
| 212 | /* return realtype */ |
| 213 | if (val.realtype->basetype == LY_TYPE_UNION) { |
| 214 | *realtype = val.subvalue->value.realtype; |
| 215 | } else { |
| 216 | *realtype = val.realtype; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (canonical) { |
| 221 | /* return canonical value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 222 | lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical); |
Michal Vasko | 29134f8 | 2020-11-13 18:03:20 +0100 | [diff] [blame] | 223 | } |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 226 | if (stored) { |
| 227 | /* free value */ |
| 228 | type->plugin->free(ctx ? ctx : schema->module->ctx, &val); |
| 229 | } |
| 230 | return rc; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 233 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 234 | lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 235 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 236 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 237 | struct ly_ctx *ctx; |
| 238 | struct lysc_type *type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 239 | struct lyd_value val = {0}; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 240 | |
| 241 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL); |
| 242 | |
| 243 | ctx = node->schema->module->ctx; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 244 | type = ((struct lysc_node_leaf *)node->schema)->type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 245 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 246 | /* store the value */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 247 | LOG_LOCSET(node->schema, &node->node, NULL, NULL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 248 | ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 249 | LOG_LOCBACK(1, 1, 0, 0); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 250 | LY_CHECK_RET(ret); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 251 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 252 | /* compare values */ |
| 253 | ret = type->plugin->compare(&node->value, &val); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 254 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 255 | type->plugin->free(ctx, &val); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 259 | LIBYANG_API_DEF ly_bool |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 260 | lyd_is_default(const struct lyd_node *node) |
| 261 | { |
| 262 | const struct lysc_node_leaf *leaf; |
| 263 | const struct lysc_node_leaflist *llist; |
| 264 | const struct lyd_node_term *term; |
| 265 | LY_ARRAY_COUNT_TYPE u; |
| 266 | |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 267 | if (!(node->schema->nodetype & LYD_NODE_TERM)) { |
| 268 | return 0; |
| 269 | } |
| 270 | |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 271 | term = (const struct lyd_node_term *)node; |
| 272 | |
| 273 | if (node->schema->nodetype == LYS_LEAF) { |
| 274 | leaf = (const struct lysc_node_leaf *)node->schema; |
| 275 | if (!leaf->dflt) { |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* compare with the default value */ |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 280 | if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) { |
| 281 | return 1; |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 282 | } |
| 283 | } else { |
| 284 | llist = (const struct lysc_node_leaflist *)node->schema; |
| 285 | if (!llist->dflts) { |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | LY_ARRAY_FOR(llist->dflts, u) { |
| 290 | /* compare with each possible default value */ |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 291 | if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) { |
| 292 | return 1; |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
Michal Vasko | 39eb688 | 2021-09-16 08:53:13 +0200 | [diff] [blame] | 297 | return 0; |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 300 | static LYD_FORMAT |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 301 | lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 302 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 303 | if (!format && (in->type == LY_IN_FILEPATH)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 304 | /* unknown format - try to detect it from filename's suffix */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 305 | const char *path = in->method.fpath.filepath; |
| 306 | size_t len = strlen(path); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 307 | |
| 308 | /* ignore trailing whitespaces */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 309 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 310 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 311 | if ((len >= LY_XML_SUFFIX_LEN + 1) && |
| 312 | !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 313 | format = LYD_XML; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 314 | } else if ((len >= LY_JSON_SUFFIX_LEN + 1) && |
| 315 | !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 316 | format = LYD_JSON; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 317 | } else if ((len >= LY_LYB_SUFFIX_LEN + 1) && |
| 318 | !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 319 | format = LYD_LYB; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 320 | } /* else still unknown */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 323 | return format; |
| 324 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 325 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 326 | /** |
| 327 | * @brief Parse YANG data into a data tree. |
| 328 | * |
| 329 | * @param[in] ctx libyang context. |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 330 | * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 331 | * @param[in] parent Parent to connect the parsed nodes to, if any. |
| 332 | * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL. |
| 333 | * @param[in] in Input handle to read the input from. |
| 334 | * @param[in] format Expected format of the data in @p in. |
| 335 | * @param[in] parse_opts Options for parser. |
| 336 | * @param[in] val_opts Options for validation. |
| 337 | * @param[out] op Optional pointer to the parsed operation, if any. |
| 338 | * @return LY_ERR value. |
| 339 | */ |
| 340 | static LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 341 | lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p, |
| 342 | struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 343 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 344 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 345 | struct lyd_ctx *lydctx = NULL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 346 | struct ly_set parsed = {0}; |
| 347 | struct lyd_node *first; |
| 348 | uint32_t i; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 349 | ly_bool subtree_sibling = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 350 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 351 | assert(ctx && (parent || first_p)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 352 | |
| 353 | format = lyd_parse_get_format(in, format); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 354 | if (first_p) { |
| 355 | *first_p = NULL; |
| 356 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 357 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 358 | /* remember input position */ |
| 359 | in->func_start = in->current; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 360 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 361 | /* parse the data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 362 | switch (format) { |
| 363 | case LYD_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 364 | rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed, |
| 365 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 366 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 367 | case LYD_JSON: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 368 | rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, |
| 369 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 370 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 371 | case LYD_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 372 | rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, |
| 373 | &subtree_sibling, &lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 374 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 375 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 376 | LOGARG(ctx, format); |
| 377 | rc = LY_EINVAL; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | LY_CHECK_GOTO(rc, cleanup); |
| 381 | |
| 382 | if (parent) { |
| 383 | /* get first top-level sibling */ |
| 384 | for (first = parent; first->parent; first = lyd_parent(first)) {} |
| 385 | first = lyd_first_sibling(first); |
| 386 | first_p = &first; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 389 | if (!(parse_opts & LYD_PARSE_ONLY)) { |
| 390 | /* validate data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 391 | rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types, |
| 392 | &lydctx->ext_val, NULL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 393 | LY_CHECK_GOTO(rc, cleanup); |
| 394 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 395 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 396 | /* set the operation node */ |
| 397 | if (op) { |
| 398 | *op = lydctx->op_node; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 402 | if (lydctx) { |
| 403 | lydctx->free(lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 404 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 405 | if (rc) { |
| 406 | if (parent) { |
| 407 | /* free all the parsed subtrees */ |
| 408 | for (i = 0; i < parsed.count; ++i) { |
| 409 | lyd_free_tree(parsed.dnodes[i]); |
| 410 | } |
| 411 | } else { |
| 412 | /* free everything */ |
| 413 | lyd_free_all(*first_p); |
| 414 | *first_p = NULL; |
| 415 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 416 | } else if (subtree_sibling) { |
| 417 | rc = LY_ENOT; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 418 | } |
| 419 | ly_set_erase(&parsed, NULL); |
| 420 | return rc; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 423 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 424 | lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 425 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 426 | { |
| 427 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 428 | |
| 429 | LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL); |
| 430 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 431 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 432 | |
| 433 | return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL); |
| 434 | } |
| 435 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 436 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 437 | lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 438 | uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree) |
| 439 | { |
| 440 | LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL); |
| 441 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 442 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 443 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 444 | return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 447 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 448 | lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options, |
| 449 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 450 | { |
| 451 | LY_ERR ret; |
| 452 | struct ly_in *in; |
| 453 | |
| 454 | LY_CHECK_RET(ly_in_new_memory(data, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 455 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 456 | |
| 457 | ly_in_free(in, 0); |
| 458 | return ret; |
| 459 | } |
| 460 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 461 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 462 | lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 463 | struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 464 | { |
| 465 | LY_ERR ret; |
| 466 | struct ly_in *in; |
| 467 | |
| 468 | LY_CHECK_RET(ly_in_new_fd(fd, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 469 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 470 | |
| 471 | ly_in_free(in, 0); |
| 472 | return ret; |
| 473 | } |
| 474 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 475 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 476 | lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options, |
| 477 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 478 | { |
| 479 | LY_ERR ret; |
| 480 | struct ly_in *in; |
| 481 | |
| 482 | LY_CHECK_RET(ly_in_new_filepath(path, 0, &in)); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 483 | ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 484 | |
| 485 | ly_in_free(in, 0); |
| 486 | return ret; |
| 487 | } |
| 488 | |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 489 | /** |
| 490 | * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching |
| 491 | * for schema nodes locked inside the extension instance. |
| 492 | * |
| 493 | * At least one of @p parent, @p tree, or @p op must always be set. |
| 494 | * |
| 495 | * Specific @p data_type values have different parameter meaning as follows: |
| 496 | * - ::LYD_TYPE_RPC_NETCONF: |
| 497 | * - @p parent - must be NULL, the whole RPC is expected; |
| 498 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 499 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 500 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 501 | * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation; |
| 502 | * |
| 503 | * - ::LYD_TYPE_NOTIF_NETCONF: |
| 504 | * - @p parent - must be NULL, the whole notification is expected; |
| 505 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 506 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 507 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 508 | * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation; |
| 509 | * |
| 510 | * - ::LYD_TYPE_REPLY_NETCONF: |
| 511 | * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node; |
| 512 | * - @p format - must be ::LYD_XML, NETCONF supports only this format; |
| 513 | * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as |
| 514 | * a separate opaque data tree, even if the function fails, this may be returned; |
| 515 | * - @p op - must be NULL, the reply is appended to the RPC; |
| 516 | * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC. |
| 517 | * |
| 518 | * @param[in] ctx libyang context. |
| 519 | * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed. |
| 520 | * @param[in] parent Optional parent to connect the parsed nodes to. |
| 521 | * @param[in] in Input handle to read the input from. |
| 522 | * @param[in] format Expected format of the data in @p in. |
| 523 | * @param[in] data_type Expected operation to parse (@ref datatype). |
| 524 | * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL. |
| 525 | * @param[out] op Optional parsed operation node. |
| 526 | * @return LY_ERR value. |
| 527 | * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one. |
| 528 | */ |
| 529 | static LY_ERR |
| 530 | lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 531 | struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 532 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 533 | LY_ERR rc = LY_SUCCESS; |
| 534 | struct lyd_ctx *lydctx = NULL; |
| 535 | struct ly_set parsed = {0}; |
| 536 | struct lyd_node *first = NULL, *envp = NULL; |
| 537 | uint32_t i, parse_opts, val_opts; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 538 | |
Michal Vasko | 27fb026 | 2021-02-23 09:42:01 +0100 | [diff] [blame] | 539 | if (!ctx) { |
| 540 | ctx = LYD_CTX(parent); |
| 541 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 542 | if (tree) { |
| 543 | *tree = NULL; |
| 544 | } |
| 545 | if (op) { |
| 546 | *op = NULL; |
| 547 | } |
| 548 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 549 | format = lyd_parse_get_format(in, format); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 550 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 551 | /* remember input position */ |
| 552 | in->func_start = in->current; |
| 553 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 554 | /* check params based on the data type */ |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 555 | if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 556 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 557 | } else if (data_type == LYD_TYPE_REPLY_NETCONF) { |
| 558 | LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, |
| 559 | LY_EINVAL); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 560 | } |
Michal Vasko | 9da78cf | 2021-07-21 13:45:10 +0200 | [diff] [blame] | 561 | parse_opts = LYD_PARSE_ONLY | LYD_PARSE_OPAQ; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 562 | val_opts = 0; |
| 563 | |
| 564 | /* parse the data */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 565 | switch (format) { |
| 566 | case LYD_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 567 | rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, NULL, &lydctx); |
Michal Vasko | 299d5d1 | 2021-02-16 16:36:37 +0100 | [diff] [blame] | 568 | if (rc && envp) { |
| 569 | /* special situation when the envelopes were parsed successfully */ |
| 570 | if (tree) { |
| 571 | *tree = envp; |
| 572 | } |
| 573 | ly_set_erase(&parsed, NULL); |
| 574 | return rc; |
| 575 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 576 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 577 | case LYD_JSON: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 578 | rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 579 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 580 | case LYD_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 581 | rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 582 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 583 | case LYD_UNKNOWN: |
Michal Vasko | d74978f | 2021-02-12 11:59:36 +0100 | [diff] [blame] | 584 | LOGARG(ctx, format); |
| 585 | rc = LY_EINVAL; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 586 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 587 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 588 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 589 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 590 | /* set out params correctly */ |
| 591 | if (tree) { |
| 592 | if (envp) { |
| 593 | /* special out param meaning */ |
| 594 | *tree = envp; |
| 595 | } else { |
| 596 | *tree = parent ? NULL : first; |
| 597 | } |
| 598 | } |
| 599 | if (op) { |
| 600 | *op = lydctx->op_node; |
| 601 | } |
| 602 | |
| 603 | cleanup: |
| 604 | if (lydctx) { |
| 605 | lydctx->free(lydctx); |
| 606 | } |
| 607 | if (rc) { |
| 608 | if (parent) { |
| 609 | /* free all the parsed subtrees */ |
| 610 | for (i = 0; i < parsed.count; ++i) { |
| 611 | lyd_free_tree(parsed.dnodes[i]); |
| 612 | } |
| 613 | } else { |
| 614 | /* free everything (cannot occur in the current code, a safety) */ |
| 615 | lyd_free_all(first); |
| 616 | if (tree) { |
| 617 | *tree = NULL; |
| 618 | } |
| 619 | if (op) { |
| 620 | *op = NULL; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | ly_set_erase(&parsed, NULL); |
| 625 | return rc; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 626 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 627 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 628 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 629 | lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 630 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 631 | { |
| 632 | LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL); |
| 633 | |
| 634 | return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op); |
| 635 | } |
| 636 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 637 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 638 | lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format, |
| 639 | enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op) |
| 640 | { |
| 641 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 642 | |
| 643 | LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL); |
| 644 | |
| 645 | return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op); |
| 646 | } |
| 647 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 648 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 649 | lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 650 | LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 651 | { |
| 652 | LY_ERR ret; |
| 653 | struct lyd_node_term *term; |
| 654 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 655 | assert(schema->nodetype & LYD_NODE_TERM); |
| 656 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 657 | term = calloc(1, sizeof *term); |
| 658 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 659 | |
| 660 | term->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 661 | term->prev = &term->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 662 | term->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 663 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 664 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 665 | ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 666 | value_len, dynamic, format, prefix_data, hints, schema, incomplete); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 667 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 668 | LY_CHECK_ERR_RET(ret, free(term), ret); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 669 | lyd_hash(&term->node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 670 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 671 | *node = &term->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | LY_ERR |
| 676 | lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node) |
| 677 | { |
| 678 | LY_ERR ret; |
| 679 | struct lyd_node_term *term; |
| 680 | struct lysc_type *type; |
| 681 | |
| 682 | assert(schema->nodetype & LYD_NODE_TERM); |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 683 | assert(val && val->realtype); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 684 | |
| 685 | term = calloc(1, sizeof *term); |
| 686 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 687 | |
| 688 | term->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 689 | term->prev = &term->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 690 | term->flags = LYD_NEW; |
| 691 | |
| 692 | type = ((struct lysc_node_leaf *)schema)->type; |
| 693 | ret = type->plugin->duplicate(schema->module->ctx, val, &term->value); |
| 694 | if (ret) { |
| 695 | LOGERR(schema->module->ctx, ret, "Value duplication failed."); |
| 696 | free(term); |
| 697 | return ret; |
| 698 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 699 | lyd_hash(&term->node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 700 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 701 | *node = &term->node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | LY_ERR |
| 706 | lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node) |
| 707 | { |
| 708 | struct lyd_node_inner *in; |
| 709 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 710 | assert(schema->nodetype & LYD_NODE_INNER); |
| 711 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 712 | in = calloc(1, sizeof *in); |
| 713 | LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM); |
| 714 | |
| 715 | in->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 716 | in->prev = &in->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 717 | in->flags = LYD_NEW; |
Michal Vasko | 3383be7 | 2020-11-03 17:15:31 +0100 | [diff] [blame] | 718 | if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) { |
| 719 | in->flags |= LYD_DEFAULT; |
| 720 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 721 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 722 | /* do not hash list with keys, we need them for the hash */ |
| 723 | if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 724 | lyd_hash(&in->node); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 725 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 726 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 727 | *node = &in->node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 728 | return LY_SUCCESS; |
| 729 | } |
| 730 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 731 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 732 | lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 733 | { |
| 734 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 735 | struct lyd_node *list = NULL, *key; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 736 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 737 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 738 | assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 739 | |
| 740 | /* create list */ |
| 741 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup); |
| 742 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 743 | LOG_LOCSET(NULL, list, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 744 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 745 | /* create and insert all the keys */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 746 | LY_ARRAY_FOR(predicates, u) { |
| 747 | LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 748 | lyd_insert_node(list, NULL, key, 0); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 751 | /* hash having all the keys */ |
| 752 | lyd_hash(list); |
| 753 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 754 | /* success */ |
| 755 | *node = list; |
| 756 | list = NULL; |
| 757 | |
| 758 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 759 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 760 | lyd_free_tree(list); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | static LY_ERR |
| 765 | lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node) |
| 766 | { |
| 767 | LY_ERR ret = LY_SUCCESS; |
| 768 | struct lyxp_expr *expr = NULL; |
| 769 | uint16_t exp_idx = 0; |
| 770 | enum ly_path_pred_type pred_type = 0; |
| 771 | struct ly_path_predicate *predicates = NULL; |
| 772 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 773 | LOG_LOCSET(schema, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 774 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 775 | /* parse keys */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 776 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 777 | LY_PATH_PRED_KEYS, &expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 778 | |
| 779 | /* compile them */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 780 | LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 781 | NULL, &predicates, &pred_type), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 782 | |
| 783 | /* create the list node */ |
| 784 | LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup); |
| 785 | |
| 786 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 787 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 788 | lyxp_expr_free(schema->module->ctx, expr); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 789 | ly_path_predicates_free(schema->module->ctx, pred_type, predicates); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | LY_ERR |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 794 | lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value, |
| 795 | struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 796 | { |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 797 | LY_ERR ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 798 | struct lyd_node_any *any; |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 799 | union lyd_any_value any_val; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 800 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 801 | assert(schema->nodetype & LYD_NODE_ANY); |
| 802 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 803 | any = calloc(1, sizeof *any); |
| 804 | LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM); |
| 805 | |
| 806 | any->schema = schema; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 807 | any->prev = &any->node; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 808 | any->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 809 | |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 810 | if (use_value) { |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 811 | switch (value_type) { |
| 812 | case LYD_ANYDATA_DATATREE: |
| 813 | any->value.tree = (void *)value; |
| 814 | break; |
| 815 | case LYD_ANYDATA_STRING: |
| 816 | case LYD_ANYDATA_XML: |
| 817 | case LYD_ANYDATA_JSON: |
Michal Vasko | 0d00e46 | 2022-02-24 16:24:40 +0100 | [diff] [blame] | 818 | LY_CHECK_ERR_RET(lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), free(any), LY_EMEM); |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 819 | break; |
| 820 | case LYD_ANYDATA_LYB: |
| 821 | any->value.mem = (void *)value; |
| 822 | break; |
| 823 | } |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 824 | any->value_type = value_type; |
| 825 | } else { |
| 826 | any_val.str = value; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 827 | ret = lyd_any_copy_value(&any->node, &any_val, value_type); |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 828 | LY_CHECK_ERR_RET(ret, free(any), ret); |
| 829 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 830 | lyd_hash(&any->node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 831 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 832 | *node = &any->node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 833 | return LY_SUCCESS; |
| 834 | } |
| 835 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 836 | LY_ERR |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 837 | lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len, |
| 838 | const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 839 | LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 840 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 841 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 842 | struct lyd_node_opaq *opaq; |
| 843 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 844 | assert(ctx && name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 845 | |
Michal Vasko | 4cd0a60 | 2021-05-31 11:11:58 +0200 | [diff] [blame] | 846 | if (!value_len && (!dynamic || !*dynamic)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 847 | value = ""; |
| 848 | } |
| 849 | |
| 850 | opaq = calloc(1, sizeof *opaq); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 851 | LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 852 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 853 | opaq->prev = &opaq->node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 854 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 855 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 856 | if (pref_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 857 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 858 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 859 | if (module_key_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 860 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 861 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 862 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 863 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 864 | *dynamic = 0; |
| 865 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 866 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 867 | } |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 868 | |
| 869 | opaq->format = format; |
| 870 | opaq->val_prefix_data = val_prefix_data; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 871 | opaq->hints = hints; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 872 | opaq->ctx = ctx; |
| 873 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 874 | finish: |
| 875 | if (ret) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 876 | lyd_free_tree(&opaq->node); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 877 | ly_free_prefix_data(format, val_prefix_data); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 878 | } else { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 879 | *node = &opaq->node; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 880 | } |
| 881 | return ret; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 884 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6524389 | 2020-12-04 16:26:21 +0100 | [diff] [blame] | 885 | lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 886 | struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 887 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 888 | LY_ERR r; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 889 | struct lyd_node *ret = NULL; |
| 890 | const struct lysc_node *schema; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 891 | struct lysc_ext_instance *ext = NULL; |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 892 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 893 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 894 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 895 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 896 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 897 | if (!module) { |
| 898 | module = parent->schema->module; |
| 899 | } |
| 900 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 901 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 902 | LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 903 | if (!schema && parent) { |
| 904 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 905 | strlen(name), &schema, &ext); |
| 906 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 907 | } |
| 908 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.", |
| 909 | name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 910 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 911 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 912 | if (ext) { |
| 913 | ret->flags |= LYD_EXT; |
| 914 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 915 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 916 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 917 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 918 | |
| 919 | if (node) { |
| 920 | *node = ret; |
| 921 | } |
| 922 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 923 | } |
| 924 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 925 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | dd2a766 | 2021-03-12 11:26:34 +0100 | [diff] [blame] | 926 | lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node) |
| 927 | { |
| 928 | struct lyd_node *ret = NULL; |
| 929 | const struct lysc_node *schema; |
| 930 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 931 | |
| 932 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 933 | |
| 934 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0); |
| 935 | if (!schema) { |
| 936 | if (ext->argument) { |
| 937 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.", |
| 938 | name, ext->argument, ext->def->name); |
| 939 | } else { |
| 940 | LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.", |
| 941 | name, ext->def->name); |
| 942 | } |
| 943 | return LY_ENOTFOUND; |
| 944 | } |
| 945 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 946 | |
| 947 | *node = ret; |
| 948 | |
| 949 | return LY_SUCCESS; |
| 950 | } |
| 951 | |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 952 | /** |
| 953 | * @brief Create a new list node in the data tree. |
| 954 | * |
| 955 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 956 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 957 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 958 | * @param[in] format Format of key values. |
| 959 | * @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 |
| 960 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 961 | * @param[out] node Optional created node. |
| 962 | * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must |
| 963 | * be followed by the value length. |
| 964 | * @return LY_ERR value. |
| 965 | */ |
| 966 | static LY_ERR |
| 967 | _lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format, |
| 968 | ly_bool output, struct lyd_node **node, va_list ap) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 969 | { |
| 970 | struct lyd_node *ret = NULL, *key; |
| 971 | const struct lysc_node *schema, *key_s; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 972 | struct lysc_ext_instance *ext = NULL; |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 973 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 974 | const void *key_val; |
| 975 | uint32_t key_len; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 976 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 977 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 978 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 979 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 980 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 981 | if (!module) { |
| 982 | module = parent->schema->module; |
| 983 | } |
| 984 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 985 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 986 | if (!schema && parent) { |
| 987 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 988 | strlen(name), &schema, &ext); |
| 989 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 990 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 991 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 992 | |
| 993 | /* create list inner node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 994 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 995 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 996 | /* create and insert all the keys */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 997 | for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 998 | if (format == LY_VALUE_LYB) { |
| 999 | key_val = va_arg(ap, const void *); |
| 1000 | key_len = va_arg(ap, uint32_t); |
| 1001 | } else { |
| 1002 | key_val = va_arg(ap, const char *); |
| 1003 | key_len = key_val ? strlen((char *)key_val) : 0; |
| 1004 | } |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1005 | |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 1006 | rc = lyd_create_term(key_s, key_val, key_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &key); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1007 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1008 | lyd_insert_node(ret, NULL, key, 1); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1011 | if (ext) { |
| 1012 | ret->flags |= LYD_EXT; |
| 1013 | } |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1014 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1015 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | cleanup: |
| 1019 | if (rc) { |
| 1020 | lyd_free_tree(ret); |
| 1021 | ret = NULL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1022 | } else if (node) { |
| 1023 | *node = ret; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1024 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1025 | return rc; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1028 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 1029 | lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 1030 | struct lyd_node **node, ...) |
| 1031 | { |
| 1032 | LY_ERR rc; |
| 1033 | va_list ap; |
| 1034 | |
| 1035 | va_start(ap, node); |
| 1036 | |
| 1037 | rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap); |
| 1038 | |
| 1039 | va_end(ap); |
| 1040 | return rc; |
| 1041 | } |
| 1042 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1043 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 1044 | lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 1045 | struct lyd_node **node, ...) |
| 1046 | { |
| 1047 | LY_ERR rc; |
| 1048 | va_list ap; |
| 1049 | |
| 1050 | va_start(ap, node); |
| 1051 | |
| 1052 | rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap); |
| 1053 | |
| 1054 | va_end(ap); |
| 1055 | return rc; |
| 1056 | } |
| 1057 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1058 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 208e6d6 | 2021-06-28 11:23:50 +0200 | [diff] [blame] | 1059 | lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, |
| 1060 | struct lyd_node **node, ...) |
| 1061 | { |
| 1062 | LY_ERR rc; |
| 1063 | va_list ap; |
| 1064 | |
| 1065 | va_start(ap, node); |
| 1066 | |
| 1067 | rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap); |
| 1068 | |
| 1069 | va_end(ap); |
| 1070 | return rc; |
| 1071 | } |
| 1072 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1073 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1074 | lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...) |
| 1075 | { |
| 1076 | struct lyd_node *ret = NULL, *key; |
| 1077 | const struct lysc_node *schema, *key_s; |
| 1078 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1079 | va_list ap; |
| 1080 | const char *key_val; |
| 1081 | LY_ERR rc = LY_SUCCESS; |
| 1082 | |
| 1083 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 1084 | |
| 1085 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0); |
| 1086 | if (!schema) { |
| 1087 | if (ext->argument) { |
| 1088 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.", |
| 1089 | name, ext->argument, ext->def->name); |
| 1090 | } else { |
| 1091 | LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 1092 | } |
| 1093 | return LY_ENOTFOUND; |
| 1094 | } |
| 1095 | /* create list inner node */ |
| 1096 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 1097 | |
| 1098 | va_start(ap, node); |
| 1099 | |
| 1100 | /* create and insert all the keys */ |
| 1101 | for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 1102 | key_val = va_arg(ap, const char *); |
| 1103 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1104 | rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1105 | NULL, &key); |
| 1106 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1107 | lyd_insert_node(ret, NULL, key, 1); |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | cleanup: |
| 1111 | va_end(ap); |
| 1112 | if (rc) { |
| 1113 | lyd_free_tree(ret); |
| 1114 | ret = NULL; |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1115 | } |
Radek Krejci | 8919fbd | 2021-03-15 09:35:25 +0100 | [diff] [blame] | 1116 | *node = ret; |
Radek Krejci | 8247bae | 2021-03-12 11:47:02 +0100 | [diff] [blame] | 1117 | return rc; |
| 1118 | } |
| 1119 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1120 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1121 | lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1122 | ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1123 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1124 | LY_ERR r; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1125 | struct lyd_node *ret = NULL; |
| 1126 | const struct lysc_node *schema; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1127 | struct lysc_ext_instance *ext = NULL; |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1128 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1129 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 1130 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1131 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1132 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1133 | if (!module) { |
| 1134 | module = parent->schema->module; |
| 1135 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1136 | if (!keys) { |
| 1137 | keys = ""; |
| 1138 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1139 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1140 | /* find schema node */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1141 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1142 | if (!schema && parent) { |
| 1143 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name), |
| 1144 | &schema, &ext); |
| 1145 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 1146 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1147 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1148 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1149 | if ((schema->flags & LYS_KEYLESS) && !keys[0]) { |
| 1150 | /* key-less list */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1151 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1152 | } else { |
| 1153 | /* create the list node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1154 | LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1155 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1156 | if (ext) { |
| 1157 | ret->flags |= LYD_EXT; |
| 1158 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1159 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1160 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1161 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1162 | |
| 1163 | if (node) { |
| 1164 | *node = ret; |
| 1165 | } |
| 1166 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1169 | /** |
| 1170 | * @brief Create a new term node in the data tree. |
| 1171 | * |
| 1172 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
| 1173 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
| 1174 | * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST. |
| 1175 | * @param[in] value Value of the node being created. |
| 1176 | * @param[in] value_len Length of @p value. |
| 1177 | * @param[in] format Format of @p value. |
| 1178 | * @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 |
| 1179 | * taken into consideration. Otherwise, the output's data node is going to be created. |
| 1180 | * @param[out] node Optional created node. |
| 1181 | * @return LY_ERR value. |
| 1182 | */ |
| 1183 | static LY_ERR |
| 1184 | _lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 1185 | size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1186 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1187 | LY_ERR r; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1188 | struct lyd_node *ret = NULL; |
| 1189 | const struct lysc_node *schema; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1190 | struct lysc_ext_instance *ext = NULL; |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1191 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1192 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 1193 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1194 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1195 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1196 | if (!module) { |
| 1197 | module = parent->schema->module; |
| 1198 | } |
| 1199 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1200 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1201 | if (!schema && parent) { |
| 1202 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 1203 | strlen(name), &schema, &ext); |
| 1204 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 1205 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1206 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1207 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1208 | LY_CHECK_RET(lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret)); |
| 1209 | if (ext) { |
| 1210 | ret->flags |= LYD_EXT; |
| 1211 | } |
Michal Vasko | cbff3e9 | 2020-05-27 12:56:41 +0200 | [diff] [blame] | 1212 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1213 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1214 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1215 | |
| 1216 | if (node) { |
| 1217 | *node = ret; |
| 1218 | } |
| 1219 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1222 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1223 | lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 1224 | ly_bool output, struct lyd_node **node) |
| 1225 | { |
| 1226 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node); |
| 1227 | } |
| 1228 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1229 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1230 | lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 1231 | size_t value_len, ly_bool output, struct lyd_node **node) |
| 1232 | { |
| 1233 | return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node); |
| 1234 | } |
| 1235 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1236 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1237 | lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 1238 | ly_bool output, struct lyd_node **node) |
| 1239 | { |
| 1240 | return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node); |
| 1241 | } |
| 1242 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1243 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 8a5afc2 | 2021-03-12 11:10:47 +0100 | [diff] [blame] | 1244 | lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node) |
| 1245 | { |
| 1246 | LY_ERR rc; |
| 1247 | struct lyd_node *ret = NULL; |
| 1248 | const struct lysc_node *schema; |
| 1249 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1250 | |
| 1251 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 1252 | |
| 1253 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0); |
| 1254 | if (!schema) { |
| 1255 | if (ext->argument) { |
| 1256 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.", |
| 1257 | name, ext->argument, ext->def->name); |
| 1258 | } else { |
| 1259 | LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 1260 | } |
| 1261 | return LY_ENOTFOUND; |
| 1262 | } |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1263 | rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret); |
Radek Krejci | 8a5afc2 | 2021-03-12 11:10:47 +0100 | [diff] [blame] | 1264 | LY_CHECK_RET(rc); |
| 1265 | |
| 1266 | *node = ret; |
| 1267 | |
| 1268 | return LY_SUCCESS; |
| 1269 | } |
| 1270 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1271 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2a4ab2b | 2021-03-04 15:52:40 +0100 | [diff] [blame] | 1272 | lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 1273 | ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1274 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1275 | LY_ERR r; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1276 | struct lyd_node *ret = NULL; |
| 1277 | const struct lysc_node *schema; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1278 | struct lysc_ext_instance *ext = NULL; |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1279 | const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1280 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 1281 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1282 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1283 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1284 | if (!module) { |
| 1285 | module = parent->schema->module; |
| 1286 | } |
| 1287 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 1288 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1289 | if (!schema && parent) { |
| 1290 | r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, |
| 1291 | strlen(name), &schema, &ext); |
| 1292 | LY_CHECK_RET(r && (r != LY_ENOT), r); |
| 1293 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1294 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1295 | |
Michal Vasko | 2a4ab2b | 2021-03-04 15:52:40 +0100 | [diff] [blame] | 1296 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1297 | if (ext) { |
| 1298 | ret->flags |= LYD_EXT; |
| 1299 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1300 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1301 | lyd_insert_node(parent, NULL, ret, 0); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1302 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1303 | |
| 1304 | if (node) { |
| 1305 | *node = ret; |
| 1306 | } |
| 1307 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 1308 | } |
| 1309 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1310 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 0b963da | 2021-03-12 13:23:44 +0100 | [diff] [blame] | 1311 | lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value, |
| 1312 | LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node) |
| 1313 | { |
| 1314 | struct lyd_node *ret = NULL; |
| 1315 | const struct lysc_node *schema; |
| 1316 | struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 1317 | |
| 1318 | LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL); |
| 1319 | |
| 1320 | schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0); |
| 1321 | if (!schema) { |
| 1322 | if (ext->argument) { |
| 1323 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.", |
| 1324 | name, ext->argument, ext->def->name); |
| 1325 | } else { |
| 1326 | LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name); |
| 1327 | } |
| 1328 | return LY_ENOTFOUND; |
| 1329 | } |
| 1330 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret)); |
| 1331 | |
| 1332 | *node = ret; |
| 1333 | |
| 1334 | return LY_SUCCESS; |
| 1335 | } |
| 1336 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1337 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1338 | lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 1339 | const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta) |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1340 | { |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1341 | const char *prefix, *tmp; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1342 | size_t pref_len, name_len; |
| 1343 | |
Michal Vasko | 2b5344c | 2021-02-26 10:12:05 +0100 | [diff] [blame] | 1344 | LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1345 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | 2b5344c | 2021-02-26 10:12:05 +0100 | [diff] [blame] | 1346 | if (!ctx) { |
| 1347 | ctx = LYD_CTX(parent); |
| 1348 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1349 | |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1350 | if (parent && !parent->schema) { |
Michal Vasko | 33b4fab | 2021-03-04 15:56:12 +0100 | [diff] [blame] | 1351 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent)); |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1352 | return LY_EINVAL; |
| 1353 | } |
Michal Vasko | 610553d | 2020-11-18 18:15:05 +0100 | [diff] [blame] | 1354 | if (meta) { |
| 1355 | *meta = NULL; |
| 1356 | } |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1357 | |
| 1358 | /* parse the name */ |
| 1359 | tmp = name; |
| 1360 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1361 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1362 | return LY_EINVAL; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* find the module */ |
| 1366 | if (prefix) { |
Radek Krejci | 0ad51f1 | 2020-07-16 12:08:12 +0200 | [diff] [blame] | 1367 | module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 1368 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND); |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /* set value if none */ |
| 1372 | if (!val_str) { |
| 1373 | val_str = ""; |
| 1374 | } |
| 1375 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1376 | return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1377 | NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL); |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1378 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1379 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1380 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1381 | lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr, |
| 1382 | struct lyd_meta **meta) |
| 1383 | { |
| 1384 | const struct lys_module *mod; |
| 1385 | |
| 1386 | LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1387 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1388 | |
| 1389 | if (parent && !parent->schema) { |
| 1390 | LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name); |
| 1391 | return LY_EINVAL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1392 | } |
Michal Vasko | 610553d | 2020-11-18 18:15:05 +0100 | [diff] [blame] | 1393 | if (meta) { |
| 1394 | *meta = NULL; |
| 1395 | } |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1396 | |
| 1397 | switch (attr->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1398 | case LY_VALUE_XML: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1399 | mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1400 | if (!mod) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1401 | LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1402 | return LY_ENOTFOUND; |
| 1403 | } |
| 1404 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1405 | case LY_VALUE_JSON: |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1406 | mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1407 | if (!mod) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1408 | LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name); |
Michal Vasko | ba69670 | 2020-11-11 19:12:45 +0100 | [diff] [blame] | 1409 | return LY_ENOTFOUND; |
| 1410 | } |
| 1411 | break; |
| 1412 | default: |
| 1413 | LOGINT_RET(ctx); |
| 1414 | } |
| 1415 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 1416 | return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value), |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1417 | NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL); |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1420 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1421 | lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1422 | const char *prefix, const char *module_name, struct lyd_node **node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1423 | { |
| 1424 | struct lyd_node *ret = NULL; |
Radek Krejci | 07a5596 | 2021-03-02 20:16:43 +0100 | [diff] [blame] | 1425 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1426 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1427 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1428 | |
| 1429 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1430 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1431 | } |
| 1432 | if (!value) { |
| 1433 | value = ""; |
| 1434 | } |
| 1435 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1436 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1437 | strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1438 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1439 | lyd_insert_node(parent, NULL, ret, 1); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1440 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1441 | |
| 1442 | if (node) { |
| 1443 | *node = ret; |
| 1444 | } |
| 1445 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1446 | } |
| 1447 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1448 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1449 | lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1450 | const char *prefix, const char *module_ns, struct lyd_node **node) |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1451 | { |
| 1452 | struct lyd_node *ret = NULL; |
| 1453 | |
| 1454 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 1455 | LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1456 | |
| 1457 | if (!ctx) { |
| 1458 | ctx = LYD_CTX(parent); |
| 1459 | } |
| 1460 | if (!value) { |
| 1461 | value = ""; |
| 1462 | } |
| 1463 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 1464 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1465 | strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret)); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1466 | if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1467 | lyd_insert_node(parent, NULL, ret, 1); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | if (node) { |
| 1471 | *node = ret; |
| 1472 | } |
| 1473 | return LY_SUCCESS; |
| 1474 | } |
| 1475 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1476 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1477 | lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1478 | struct lyd_attr **attr) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1479 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1480 | struct lyd_attr *ret = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1481 | const struct ly_ctx *ctx; |
| 1482 | const char *prefix, *tmp; |
Michal Vasko | 51d21b8 | 2020-11-13 18:03:54 +0100 | [diff] [blame] | 1483 | size_t pref_len, name_len, mod_len; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1484 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1485 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1486 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1487 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1488 | |
| 1489 | /* parse the name */ |
| 1490 | tmp = name; |
| 1491 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
Michal Vasko | b69b68c | 2021-02-17 11:18:16 +0100 | [diff] [blame] | 1492 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1493 | return LY_EVALID; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Michal Vasko | e137fc4 | 2021-07-22 11:53:13 +0200 | [diff] [blame] | 1496 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 1497 | /* not a prefix but special name */ |
| 1498 | name = prefix; |
| 1499 | name_len += 1 + pref_len; |
| 1500 | prefix = NULL; |
| 1501 | pref_len = 0; |
| 1502 | } |
| 1503 | |
Michal Vasko | 51d21b8 | 2020-11-13 18:03:54 +0100 | [diff] [blame] | 1504 | /* get the module */ |
| 1505 | if (module_name) { |
| 1506 | mod_len = strlen(module_name); |
| 1507 | } else { |
| 1508 | module_name = prefix; |
| 1509 | mod_len = pref_len; |
| 1510 | } |
| 1511 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1512 | /* set value if none */ |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1513 | if (!value) { |
| 1514 | value = ""; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1515 | } |
| 1516 | |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1517 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1518 | strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA)); |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1519 | |
| 1520 | if (attr) { |
| 1521 | *attr = ret; |
| 1522 | } |
| 1523 | return LY_SUCCESS; |
| 1524 | } |
| 1525 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1526 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1527 | lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value, |
| 1528 | struct lyd_attr **attr) |
| 1529 | { |
| 1530 | struct lyd_attr *ret = NULL; |
| 1531 | const struct ly_ctx *ctx; |
| 1532 | const char *prefix, *tmp; |
| 1533 | size_t pref_len, name_len; |
| 1534 | |
| 1535 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
| 1536 | |
| 1537 | ctx = LYD_CTX(parent); |
| 1538 | |
| 1539 | /* parse the name */ |
| 1540 | tmp = name; |
| 1541 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1542 | LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name); |
| 1543 | return LY_EVALID; |
| 1544 | } |
| 1545 | |
Michal Vasko | e137fc4 | 2021-07-22 11:53:13 +0200 | [diff] [blame] | 1546 | if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) { |
| 1547 | /* not a prefix but special name */ |
| 1548 | name = prefix; |
| 1549 | name_len += 1 + pref_len; |
| 1550 | prefix = NULL; |
| 1551 | pref_len = 0; |
| 1552 | } |
| 1553 | |
Michal Vasko | 8d65f85 | 2021-02-17 11:28:13 +0100 | [diff] [blame] | 1554 | /* set value if none */ |
| 1555 | if (!value) { |
| 1556 | value = ""; |
| 1557 | } |
| 1558 | |
| 1559 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1560 | module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1561 | |
| 1562 | if (attr) { |
| 1563 | *attr = ret; |
| 1564 | } |
| 1565 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1568 | /** |
| 1569 | * @brief Change the value of a term (leaf or leaf-list) node. |
| 1570 | * |
| 1571 | * Node changed this way is always considered explicitly set, meaning its default flag |
| 1572 | * is always cleared. |
| 1573 | * |
| 1574 | * @param[in] term Term node to change. |
| 1575 | * @param[in] value New value to set. |
| 1576 | * @param[in] value_len Length of @p value. |
| 1577 | * @param[in] format Format of @p value. |
| 1578 | * @return LY_SUCCESS if value was changed, |
| 1579 | * @return LY_EEXIST if value was the same and only the default flag was cleared, |
| 1580 | * @return LY_ENOT if the values were equal and no change occured, |
| 1581 | * @return LY_ERR value on other errors. |
| 1582 | */ |
| 1583 | static LY_ERR |
| 1584 | _lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1585 | { |
| 1586 | LY_ERR ret = LY_SUCCESS; |
| 1587 | struct lysc_type *type; |
| 1588 | struct lyd_node_term *t; |
| 1589 | struct lyd_node *parent; |
| 1590 | struct lyd_value val = {0}; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1591 | ly_bool dflt_change, val_change; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1592 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1593 | assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1594 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1595 | t = (struct lyd_node_term *)term; |
| 1596 | type = ((struct lysc_node_leaf *)term->schema)->type; |
| 1597 | |
| 1598 | /* parse the new value */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1599 | LOG_LOCSET(term->schema, term, NULL, NULL); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1600 | ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, NULL, format, NULL, LYD_HINT_DATA, term->schema, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1601 | LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1602 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1603 | |
| 1604 | /* compare original and new value */ |
| 1605 | if (type->plugin->compare(&t->value, &val)) { |
| 1606 | /* values differ, switch them */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1607 | type->plugin->free(LYD_CTX(term), &t->value); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1608 | t->value = val; |
| 1609 | memset(&val, 0, sizeof val); |
| 1610 | val_change = 1; |
| 1611 | } else { |
| 1612 | val_change = 0; |
| 1613 | } |
| 1614 | |
| 1615 | /* always clear the default flag */ |
| 1616 | if (term->flags & LYD_DEFAULT) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1617 | for (parent = term; parent; parent = lyd_parent(parent)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1618 | parent->flags &= ~LYD_DEFAULT; |
| 1619 | } |
| 1620 | dflt_change = 1; |
| 1621 | } else { |
| 1622 | dflt_change = 0; |
| 1623 | } |
| 1624 | |
| 1625 | if (val_change || dflt_change) { |
| 1626 | /* make the node non-validated */ |
| 1627 | term->flags &= LYD_NEW; |
| 1628 | } |
| 1629 | |
| 1630 | if (val_change) { |
| 1631 | if (term->schema->nodetype == LYS_LEAFLIST) { |
| 1632 | /* leaf-list needs to be hashed again and re-inserted into parent */ |
| 1633 | lyd_unlink_hash(term); |
| 1634 | lyd_hash(term); |
| 1635 | LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup); |
| 1636 | } else if ((term->schema->flags & LYS_KEY) && term->parent) { |
| 1637 | /* list needs to be updated if its key was changed */ |
| 1638 | assert(term->parent->schema->nodetype == LYS_LIST); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1639 | lyd_unlink_hash(lyd_parent(term)); |
| 1640 | lyd_hash(lyd_parent(term)); |
| 1641 | LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1642 | } /* else leaf that is not a key, its value is not used for its hash so it does not change */ |
| 1643 | } |
| 1644 | |
| 1645 | /* retrun value */ |
| 1646 | if (!val_change) { |
| 1647 | if (dflt_change) { |
| 1648 | /* only default flag change */ |
| 1649 | ret = LY_EEXIST; |
| 1650 | } else { |
| 1651 | /* no change */ |
| 1652 | ret = LY_ENOT; |
| 1653 | } |
| 1654 | } /* else value changed, LY_SUCCESS */ |
| 1655 | |
| 1656 | cleanup: |
Michal Vasko | 59512fc | 2020-12-09 18:13:29 +0100 | [diff] [blame] | 1657 | if (val.realtype) { |
| 1658 | type->plugin->free(LYD_CTX(term), &val); |
| 1659 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1660 | return ret; |
| 1661 | } |
| 1662 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1663 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1664 | lyd_change_term(struct lyd_node *term, const char *val_str) |
| 1665 | { |
| 1666 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1667 | |
| 1668 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON); |
| 1669 | } |
| 1670 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1671 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1672 | lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len) |
| 1673 | { |
| 1674 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1675 | |
| 1676 | return _lyd_change_term(term, value, value_len, LY_VALUE_LYB); |
| 1677 | } |
| 1678 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1679 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1680 | lyd_change_term_canon(struct lyd_node *term, const char *val_str) |
| 1681 | { |
| 1682 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1683 | |
| 1684 | return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON); |
| 1685 | } |
| 1686 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1687 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1688 | lyd_change_meta(struct lyd_meta *meta, const char *val_str) |
| 1689 | { |
| 1690 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 1691 | struct lyd_meta *m2 = NULL; |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1692 | struct lyd_value val; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1693 | ly_bool val_change; |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1694 | |
| 1695 | LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL); |
| 1696 | |
| 1697 | if (!val_str) { |
| 1698 | val_str = ""; |
| 1699 | } |
| 1700 | |
| 1701 | /* parse the new value into a new meta structure */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1702 | ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str), |
| 1703 | NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL); |
| 1704 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1705 | |
| 1706 | /* compare original and new value */ |
| 1707 | if (lyd_compare_meta(meta, m2)) { |
| 1708 | /* values differ, switch them */ |
| 1709 | val = meta->value; |
| 1710 | meta->value = m2->value; |
| 1711 | m2->value = val; |
| 1712 | val_change = 1; |
| 1713 | } else { |
| 1714 | val_change = 0; |
| 1715 | } |
| 1716 | |
| 1717 | /* retrun value */ |
| 1718 | if (!val_change) { |
| 1719 | /* no change */ |
| 1720 | ret = LY_ENOT; |
| 1721 | } /* else value changed, LY_SUCCESS */ |
| 1722 | |
| 1723 | cleanup: |
Michal Vasko | 1a66a5d | 2020-11-18 18:15:37 +0100 | [diff] [blame] | 1724 | lyd_free_meta_single(m2); |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1725 | return ret; |
| 1726 | } |
| 1727 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1728 | /** |
| 1729 | * @brief Update node value. |
| 1730 | * |
| 1731 | * @param[in] node Node to update. |
| 1732 | * @param[in] value New value to set. |
| 1733 | * @param[in] value_len Length of @p value. |
| 1734 | * @param[in] value_type Type of @p value for anydata/anyxml node. |
| 1735 | * @param[in] format Format of @p value. |
| 1736 | * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL. |
| 1737 | * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL. |
| 1738 | * @return LY_ERR value. |
| 1739 | */ |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1740 | static LY_ERR |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1741 | lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, |
| 1742 | LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node) |
| 1743 | { |
| 1744 | LY_ERR ret = LY_SUCCESS; |
| 1745 | struct lyd_node *new_any; |
| 1746 | |
| 1747 | switch (node->schema->nodetype) { |
| 1748 | case LYS_CONTAINER: |
| 1749 | case LYS_NOTIF: |
| 1750 | case LYS_RPC: |
| 1751 | case LYS_ACTION: |
| 1752 | case LYS_LIST: |
| 1753 | /* if it exists, there is nothing to update */ |
| 1754 | *new_parent = NULL; |
| 1755 | *new_node = NULL; |
| 1756 | break; |
| 1757 | case LYS_LEAFLIST: |
| 1758 | if (!lysc_is_dup_inst_list(node->schema)) { |
| 1759 | /* if it exists, there is nothing to update */ |
| 1760 | *new_parent = NULL; |
| 1761 | *new_node = NULL; |
| 1762 | break; |
| 1763 | } |
| 1764 | /* fallthrough */ |
| 1765 | case LYS_LEAF: |
| 1766 | ret = _lyd_change_term(node, value, value_len, format); |
| 1767 | if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) { |
| 1768 | /* there was an actual change (at least of the default flag) */ |
| 1769 | *new_parent = node; |
| 1770 | *new_node = node; |
| 1771 | ret = LY_SUCCESS; |
| 1772 | } else if (ret == LY_ENOT) { |
| 1773 | /* no change */ |
| 1774 | *new_parent = NULL; |
| 1775 | *new_node = NULL; |
| 1776 | ret = LY_SUCCESS; |
| 1777 | } /* else error */ |
| 1778 | break; |
| 1779 | case LYS_ANYDATA: |
| 1780 | case LYS_ANYXML: |
| 1781 | /* create a new any node */ |
| 1782 | LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any)); |
| 1783 | |
| 1784 | /* compare with the existing one */ |
| 1785 | if (lyd_compare_single(node, new_any, 0)) { |
| 1786 | /* not equal, switch values (so that we can use generic node free) */ |
| 1787 | ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value; |
| 1788 | ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type; |
| 1789 | ((struct lyd_node_any *)node)->value.str = value; |
| 1790 | ((struct lyd_node_any *)node)->value_type = value_type; |
| 1791 | |
| 1792 | *new_parent = node; |
| 1793 | *new_node = node; |
| 1794 | } else { |
| 1795 | /* they are equal */ |
| 1796 | *new_parent = NULL; |
| 1797 | *new_node = NULL; |
| 1798 | } |
| 1799 | lyd_free_tree(new_any); |
| 1800 | break; |
| 1801 | default: |
| 1802 | LOGINT(LYD_CTX(node)); |
| 1803 | ret = LY_EINT; |
| 1804 | break; |
| 1805 | } |
| 1806 | |
| 1807 | return ret; |
| 1808 | } |
| 1809 | |
| 1810 | static LY_ERR |
| 1811 | lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len, |
| 1812 | LY_VALUE_FORMAT format, uint32_t options) |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1813 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1814 | LY_ERR r; |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1815 | struct ly_path_predicate *pred; |
Michal Vasko | e1886a9 | 2022-01-10 09:30:08 +0100 | [diff] [blame] | 1816 | struct lyd_value val; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1817 | const struct lysc_node *schema = NULL; |
| 1818 | LY_ARRAY_COUNT_TYPE u, new_count; |
| 1819 | int create = 0; |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1820 | |
| 1821 | assert(path); |
| 1822 | |
| 1823 | /* go through all the compiled nodes */ |
| 1824 | LY_ARRAY_FOR(path, u) { |
| 1825 | schema = path[u].node; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1826 | |
| 1827 | if (lysc_is_dup_inst_list(schema)) { |
| 1828 | if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) { |
| 1829 | /* creating a new key-less list or state leaf-list instance */ |
| 1830 | create = 1; |
| 1831 | new_count = u; |
| 1832 | } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) { |
| 1833 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 1834 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".", |
| 1835 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1836 | LOG_LOCBACK(1, 0, 0, 0); |
| 1837 | return LY_EINVAL; |
| 1838 | } |
| 1839 | } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) { |
| 1840 | if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) { |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1841 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 1842 | LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".", |
| 1843 | lys_nodetype2str(schema->nodetype), schema->name, str_path); |
| 1844 | LOG_LOCBACK(1, 0, 0, 0); |
| 1845 | return LY_EINVAL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1846 | } /* else creating an opaque list */ |
| 1847 | } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1848 | r = LY_SUCCESS; |
| 1849 | if (options & LYD_NEW_PATH_OPAQ) { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1850 | r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1851 | } |
| 1852 | if (!r) { |
Michal Vasko | e1886a9 | 2022-01-10 09:30:08 +0100 | [diff] [blame] | 1853 | /* try to store the value */ |
| 1854 | LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type, |
| 1855 | value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL)); |
| 1856 | ++((struct lysc_type *)val.realtype)->refcount; |
| 1857 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1858 | /* store the new predicate so that it is used when searching for this instance */ |
| 1859 | path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST; |
| 1860 | LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM); |
Michal Vasko | e1886a9 | 2022-01-10 09:30:08 +0100 | [diff] [blame] | 1861 | pred->value = val; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1862 | } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */ |
| 1863 | } |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1864 | } |
| 1865 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1866 | if (create) { |
| 1867 | /* hide the nodes that should always be created so they are not found */ |
| 1868 | while (new_count < LY_ARRAY_COUNT(path)) { |
| 1869 | LY_ARRAY_DECREMENT(path); |
| 1870 | } |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1873 | return LY_SUCCESS; |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1874 | } |
| 1875 | |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1876 | /** |
| 1877 | * @brief Create a new node in the data tree based on a path. All node types can be created. |
| 1878 | * |
Michal Vasko | a918a63 | 2021-07-02 11:53:36 +0200 | [diff] [blame] | 1879 | * If @p path points to a list key, the key value from the predicate is used and @p value is ignored. |
| 1880 | * Also, if a leaf-list is being created and both a predicate is defined in @p path |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1881 | * and @p value is set, the predicate is preferred. |
| 1882 | * |
| 1883 | * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these |
| 1884 | * nodes, they are always created. |
| 1885 | * |
| 1886 | * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used, |
| 1887 | * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted |
| 1888 | * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases. |
| 1889 | * @param[in] ctx libyang context, must be set if @p parent is NULL. |
| 1890 | * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute |
| 1891 | * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces |
| 1892 | * searching for the appropriate module. |
| 1893 | * @param[in] path [Path](@ref howtoXPath) to create. |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1894 | * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an |
| 1895 | * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL. |
| 1896 | * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when |
| 1897 | * creating anyxml/anydata nodes. |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1898 | * @param[in] value_type Anyxml/anydata node @p value type. |
| 1899 | * @param[in] options Bitmask of options, see @ref pathoptions. |
| 1900 | * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node. |
| 1901 | * @param[out] new_node Optional last node created. |
| 1902 | * @return LY_ERR value. |
| 1903 | */ |
| 1904 | static LY_ERR |
| 1905 | lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path, |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1906 | const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1907 | struct lyd_node **new_parent, struct lyd_node **new_node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1908 | { |
| 1909 | LY_ERR ret = LY_SUCCESS, r; |
| 1910 | struct lyxp_expr *exp = NULL; |
| 1911 | struct ly_path *p = NULL; |
| 1912 | struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent; |
| 1913 | const struct lysc_node *schema; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1914 | const struct lyd_value *val = NULL; |
Radek Krejci | 910d013 | 2021-03-05 14:19:31 +0100 | [diff] [blame] | 1915 | LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0; |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1916 | LY_VALUE_FORMAT format; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1917 | |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 1918 | assert(parent || ctx); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1919 | assert(path && ((path[0] == '/') || parent)); |
| 1920 | assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1921 | |
| 1922 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1923 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1924 | } |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1925 | if (value && !value_len) { |
| 1926 | value_len = strlen(value); |
| 1927 | } |
| 1928 | if (options & LYD_NEW_PATH_BIN_VALUE) { |
| 1929 | format = LY_VALUE_LYB; |
| 1930 | } else if (options & LYD_NEW_PATH_CANON_VALUE) { |
| 1931 | format = LY_VALUE_CANON; |
| 1932 | } else { |
| 1933 | format = LY_VALUE_JSON; |
| 1934 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1935 | |
| 1936 | /* parse path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1937 | LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL, |
| 1938 | LY_PATH_PRED_SIMPLE, &exp), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1939 | |
| 1940 | /* compile path */ |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 1941 | LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ? |
| 1942 | LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1943 | |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1944 | /* check the compiled path before searching existing nodes, it may be shortened */ |
| 1945 | orig_count = LY_ARRAY_COUNT(p); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1946 | LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1947 | |
| 1948 | /* try to find any existing nodes in the path */ |
| 1949 | if (parent) { |
| 1950 | ret = ly_path_eval_partial(p, parent, &path_idx, &node); |
| 1951 | if (ret == LY_SUCCESS) { |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1952 | if (orig_count == LY_ARRAY_COUNT(p)) { |
| 1953 | /* the node exists, are we supposed to update it or is it just a default? */ |
| 1954 | if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) { |
| 1955 | LOG_LOCSET(NULL, node, NULL, NULL); |
| 1956 | LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path); |
| 1957 | LOG_LOCBACK(0, 1, 0, 0); |
| 1958 | ret = LY_EEXIST; |
| 1959 | goto cleanup; |
| 1960 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1961 | |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1962 | /* update the existing node */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 1963 | ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode); |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1964 | goto cleanup; |
| 1965 | } /* else we were not searching for the whole path */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1966 | } else if (ret == LY_EINCOMPLETE) { |
| 1967 | /* some nodes were found, adjust the iterator to the next segment */ |
| 1968 | ++path_idx; |
| 1969 | } else if (ret == LY_ENOTFOUND) { |
| 1970 | /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */ |
Michal Vasko | 45b0d20 | 2020-11-06 17:20:46 +0100 | [diff] [blame] | 1971 | if (lysc_data_parent(p[0].node)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1972 | node = parent; |
| 1973 | } |
| 1974 | } else { |
| 1975 | /* error */ |
| 1976 | goto cleanup; |
| 1977 | } |
| 1978 | } |
| 1979 | |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 1980 | /* restore the full path for creating new nodes */ |
| 1981 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 1982 | LY_ARRAY_INCREMENT(p); |
| 1983 | } |
| 1984 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1985 | /* create all the non-existing nodes in a loop */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1986 | for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1987 | cur_parent = node; |
| 1988 | schema = p[path_idx].node; |
| 1989 | |
| 1990 | switch (schema->nodetype) { |
| 1991 | case LYS_LIST: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1992 | if (lysc_is_dup_inst_list(schema)) { |
| 1993 | /* create key-less list instance */ |
| 1994 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1995 | } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) { |
| 1996 | /* creating opaque list without keys */ |
| 1997 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1998 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1999 | LYD_NODEHINT_LIST, &node), cleanup); |
| 2000 | } else { |
| 2001 | /* create standard list instance */ |
| 2002 | assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST); |
| 2003 | LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2004 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2005 | break; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2006 | case LYS_CONTAINER: |
| 2007 | case LYS_NOTIF: |
| 2008 | case LYS_RPC: |
| 2009 | case LYS_ACTION: |
| 2010 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 2011 | break; |
| 2012 | case LYS_LEAFLIST: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2013 | if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) { |
| 2014 | /* we have not checked this only for dup-inst lists, otherwise it must be opaque */ |
| 2015 | r = LY_EVALID; |
| 2016 | if (lysc_is_dup_inst_list(schema)) { |
| 2017 | /* validate value */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2018 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2019 | } |
| 2020 | if (r && (r != LY_EINCOMPLETE)) { |
| 2021 | /* creating opaque leaf-list */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2022 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 2023 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, |
| 2024 | LYD_NODEHINT_LEAFLIST, &node), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2025 | break; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | /* get value to set */ |
| 2030 | if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) { |
| 2031 | val = &p[path_idx].predicates[0].value; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | /* create a leaf-list instance */ |
| 2035 | if (val) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2036 | LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2037 | } else { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2038 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, |
| 2039 | NULL, &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2040 | } |
| 2041 | break; |
| 2042 | case LYS_LEAF: |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 2043 | if (lysc_is_key(schema) && cur_parent->schema) { |
Michal Vasko | 3588033 | 2020-12-08 13:08:12 +0100 | [diff] [blame] | 2044 | /* it must have been already created or some error will occur later */ |
Michal Vasko | a918a63 | 2021-07-02 11:53:36 +0200 | [diff] [blame] | 2045 | lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node); |
| 2046 | assert(node); |
Michal Vasko | 3588033 | 2020-12-08 13:08:12 +0100 | [diff] [blame] | 2047 | goto next_iter; |
| 2048 | } |
| 2049 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2050 | if (options & LYD_NEW_PATH_OPAQ) { |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 2051 | if (cur_parent && !cur_parent->schema) { |
| 2052 | /* always create opaque nodes for opaque parents */ |
| 2053 | r = LY_ENOT; |
| 2054 | } else { |
| 2055 | /* validate value */ |
| 2056 | r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL); |
| 2057 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2058 | if (r && (r != LY_EINCOMPLETE)) { |
| 2059 | /* creating opaque leaf */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2060 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len, |
| 2061 | schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node), |
| 2062 | cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2063 | break; |
| 2064 | } |
| 2065 | } |
| 2066 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2067 | /* create a leaf instance */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2068 | LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, |
| 2069 | &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2070 | break; |
| 2071 | case LYS_ANYDATA: |
| 2072 | case LYS_ANYXML: |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 2073 | LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2074 | break; |
| 2075 | default: |
| 2076 | LOGINT(ctx); |
| 2077 | ret = LY_EINT; |
| 2078 | goto cleanup; |
| 2079 | } |
| 2080 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 2081 | if (p[path_idx].ext) { |
| 2082 | node->flags |= LYD_EXT; |
| 2083 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2084 | if (cur_parent) { |
| 2085 | /* connect to the parent */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2086 | lyd_insert_node(cur_parent, NULL, node, 0); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2087 | } else if (parent) { |
| 2088 | /* connect to top-level siblings */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2089 | lyd_insert_node(NULL, &parent, node, 0); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2090 | } |
| 2091 | |
Michal Vasko | 3588033 | 2020-12-08 13:08:12 +0100 | [diff] [blame] | 2092 | next_iter: |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2093 | /* update remembered nodes */ |
| 2094 | if (!nparent) { |
| 2095 | nparent = node; |
| 2096 | } |
| 2097 | nnode = node; |
| 2098 | } |
| 2099 | |
| 2100 | cleanup: |
| 2101 | lyxp_expr_free(ctx, exp); |
Michal Vasko | 6741dc6 | 2021-02-04 09:27:45 +0100 | [diff] [blame] | 2102 | if (p) { |
| 2103 | while (orig_count > LY_ARRAY_COUNT(p)) { |
| 2104 | LY_ARRAY_INCREMENT(p); |
| 2105 | } |
| 2106 | } |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2107 | ly_path_free(ctx, p); |
| 2108 | if (!ret) { |
| 2109 | /* set out params only on success */ |
| 2110 | if (new_parent) { |
| 2111 | *new_parent = nparent; |
| 2112 | } |
| 2113 | if (new_node) { |
| 2114 | *new_node = nnode; |
| 2115 | } |
Michal Vasko | f4b95ba | 2020-12-11 08:42:33 +0100 | [diff] [blame] | 2116 | } else { |
| 2117 | lyd_free_tree(nparent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 2118 | } |
| 2119 | return ret; |
| 2120 | } |
| 2121 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2122 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2123 | lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options, |
| 2124 | struct lyd_node **node) |
| 2125 | { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2126 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 2127 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2128 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2129 | |
| 2130 | return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2133 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2134 | lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value, |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2135 | size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, |
| 2136 | struct lyd_node **new_node) |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2137 | { |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2138 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, |
| 2139 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2140 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 2141 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2142 | return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2143 | } |
| 2144 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2145 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2146 | lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value, |
| 2147 | uint32_t options, struct lyd_node **node) |
| 2148 | { |
| 2149 | const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL; |
| 2150 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2151 | LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent, |
| 2152 | !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2153 | LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL); |
| 2154 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 2155 | return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 2156 | } |
| 2157 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2158 | LY_ERR |
| 2159 | lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2160 | const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts, |
| 2161 | struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2162 | { |
| 2163 | LY_ERR ret; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2164 | const struct lysc_node *iter = NULL; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 2165 | struct lyd_node *node = NULL; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2166 | struct lyd_value **dflts; |
| 2167 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 2168 | uint32_t getnext_opts; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2169 | |
| 2170 | assert(first && (parent || sparent || mod)); |
| 2171 | |
| 2172 | if (!sparent && parent) { |
| 2173 | sparent = parent->schema; |
| 2174 | } |
| 2175 | |
Michal Vasko | 630d989 | 2020-12-08 17:11:08 +0100 | [diff] [blame] | 2176 | getnext_opts = LYS_GETNEXT_WITHCHOICE; |
| 2177 | if (impl_opts & LYD_IMPLICIT_OUTPUT) { |
| 2178 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 2179 | } |
| 2180 | |
| 2181 | while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2182 | if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) { |
| 2183 | continue; |
Michal Vasko | 44b19a1 | 2020-08-07 09:21:30 +0200 | [diff] [blame] | 2184 | } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) { |
| 2185 | continue; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | switch (iter->nodetype) { |
| 2189 | case LYS_CHOICE: |
Michal Vasko | bcf4d7d | 2020-11-18 18:16:49 +0100 | [diff] [blame] | 2190 | node = lys_getnext_data(NULL, *first, NULL, iter, NULL); |
| 2191 | if (!node && ((struct lysc_node_choice *)iter)->dflt) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2192 | /* create default case data */ |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 2193 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2194 | NULL, node_when, node_types, impl_opts, diff)); |
Michal Vasko | bcf4d7d | 2020-11-18 18:16:49 +0100 | [diff] [blame] | 2195 | } else if (node) { |
| 2196 | /* create any default data in the existing case */ |
| 2197 | assert(node->schema->parent->nodetype == LYS_CASE); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2198 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types, |
Michal Vasko | bcf4d7d | 2020-11-18 18:16:49 +0100 | [diff] [blame] | 2199 | impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2200 | } |
| 2201 | break; |
| 2202 | case LYS_CONTAINER: |
| 2203 | if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 2204 | /* create default NP container */ |
| 2205 | LY_CHECK_RET(lyd_create_inner(iter, &node)); |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2206 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2207 | lyd_insert_node(parent, first, node, 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2208 | |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2209 | if (lysc_has_when(iter) && node_when) { |
Michal Vasko | e16c7b7 | 2021-02-26 10:39:06 +0100 | [diff] [blame] | 2210 | /* remember to resolve when */ |
| 2211 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
| 2212 | } |
Michal Vasko | e49b632 | 2020-11-05 17:38:36 +0100 | [diff] [blame] | 2213 | if (diff) { |
| 2214 | /* add into diff */ |
| 2215 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 2216 | } |
| 2217 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2218 | /* create any default children */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2219 | LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2220 | impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2221 | } |
| 2222 | break; |
| 2223 | case LYS_LEAF: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2224 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt && |
| 2225 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2226 | /* create default leaf */ |
| 2227 | ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node); |
| 2228 | if (ret == LY_EINCOMPLETE) { |
| 2229 | if (node_types) { |
| 2230 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2231 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2232 | } |
| 2233 | } else if (ret) { |
| 2234 | return ret; |
| 2235 | } |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2236 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2237 | lyd_insert_node(parent, first, node, 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2238 | |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2239 | if (lysc_has_when(iter) && node_when) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2240 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2241 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2242 | } |
| 2243 | if (diff) { |
| 2244 | /* add into diff */ |
| 2245 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 2246 | } |
| 2247 | } |
| 2248 | break; |
| 2249 | case LYS_LEAFLIST: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2250 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts && |
| 2251 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2252 | /* create all default leaf-lists */ |
| 2253 | dflts = ((struct lysc_node_leaflist *)iter)->dflts; |
| 2254 | LY_ARRAY_FOR(dflts, u) { |
| 2255 | ret = lyd_create_term2(iter, dflts[u], &node); |
| 2256 | if (ret == LY_EINCOMPLETE) { |
| 2257 | if (node_types) { |
| 2258 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2259 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2260 | } |
| 2261 | } else if (ret) { |
| 2262 | return ret; |
| 2263 | } |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2264 | node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2265 | lyd_insert_node(parent, first, node, 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2266 | |
Michal Vasko | f4d67ea | 2021-03-31 13:53:21 +0200 | [diff] [blame] | 2267 | if (lysc_has_when(iter) && node_when) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2268 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 2269 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2270 | } |
| 2271 | if (diff) { |
| 2272 | /* add into diff */ |
| 2273 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | break; |
| 2278 | default: |
| 2279 | /* without defaults */ |
| 2280 | break; |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | return LY_SUCCESS; |
| 2285 | } |
| 2286 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2287 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2288 | lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2289 | { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2290 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2291 | struct lyd_node *node; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2292 | struct ly_set node_when = {0}; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2293 | |
| 2294 | LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL); |
| 2295 | if (diff) { |
| 2296 | *diff = NULL; |
| 2297 | } |
| 2298 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2299 | LYD_TREE_DFS_BEGIN(tree, node) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2300 | /* skip added default nodes */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2301 | if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) && |
| 2302 | (node->schema->nodetype & LYD_NODE_INNER)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2303 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL, |
| 2304 | implicit_options, diff), cleanup); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2305 | } |
| 2306 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2307 | LYD_TREE_DFS_END(tree, node); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2308 | } |
| 2309 | |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2310 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2311 | LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff), cleanup); |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2312 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2313 | cleanup: |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2314 | ly_set_erase(&node_when, NULL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2315 | if (ret && diff) { |
| 2316 | lyd_free_all(*diff); |
| 2317 | *diff = NULL; |
| 2318 | } |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2322 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2323 | lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2324 | { |
| 2325 | const struct lys_module *mod; |
| 2326 | struct lyd_node *d = NULL; |
| 2327 | uint32_t i = 0; |
| 2328 | LY_ERR ret = LY_SUCCESS; |
| 2329 | |
| 2330 | LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2331 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2332 | if (diff) { |
| 2333 | *diff = NULL; |
| 2334 | } |
| 2335 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2336 | ctx = LYD_CTX(*tree); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | /* add nodes for each module one-by-one */ |
| 2340 | while ((mod = ly_ctx_get_module_iter(ctx, &i))) { |
| 2341 | if (!mod->implemented) { |
| 2342 | continue; |
| 2343 | } |
| 2344 | |
| 2345 | LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup); |
| 2346 | if (d) { |
| 2347 | /* merge into one diff */ |
| 2348 | lyd_insert_sibling(*diff, d, diff); |
| 2349 | |
| 2350 | d = NULL; |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | cleanup: |
| 2355 | if (ret && diff) { |
| 2356 | lyd_free_all(*diff); |
| 2357 | *diff = NULL; |
| 2358 | } |
| 2359 | return ret; |
| 2360 | } |
| 2361 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2362 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2363 | lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, |
| 2364 | struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2365 | { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2366 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2367 | struct lyd_node *root, *d = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2368 | struct ly_set node_when = {0}; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2369 | |
| 2370 | LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2371 | LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2372 | if (diff) { |
| 2373 | *diff = NULL; |
| 2374 | } |
| 2375 | |
| 2376 | /* add all top-level defaults for this module */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2377 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, implicit_options, diff), cleanup); |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2378 | |
| 2379 | /* resolve when and remove any invalid defaults */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2380 | LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff), |
Michal Vasko | 976ec43 | 2021-12-06 15:42:22 +0100 | [diff] [blame] | 2381 | cleanup); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2382 | |
| 2383 | /* process nested nodes */ |
| 2384 | LY_LIST_FOR(*tree, root) { |
| 2385 | /* skip added default nodes */ |
| 2386 | if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) { |
| 2387 | LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup); |
| 2388 | |
| 2389 | if (d) { |
| 2390 | /* merge into one diff */ |
| 2391 | lyd_insert_sibling(*diff, d, diff); |
| 2392 | |
| 2393 | d = NULL; |
| 2394 | } |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | cleanup: |
Michal Vasko | 3488ada | 2020-12-03 14:18:19 +0100 | [diff] [blame] | 2399 | ly_set_erase(&node_when, NULL); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 2400 | if (ret && diff) { |
| 2401 | lyd_free_all(*diff); |
| 2402 | *diff = NULL; |
| 2403 | } |
| 2404 | return ret; |
| 2405 | } |
| 2406 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2407 | struct lyd_node * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2408 | lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2409 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2410 | const struct lysc_node *schema, *sparent; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2411 | struct lyd_node *match = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2412 | ly_bool found; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2413 | uint32_t getnext_opts; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2414 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2415 | assert(new_node); |
| 2416 | |
| 2417 | if (!first_sibling || !new_node->schema) { |
| 2418 | /* insert at the end, no next anchor */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2419 | return NULL; |
| 2420 | } |
| 2421 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 2422 | assert(!first_sibling || (LYD_CTX(first_sibling) == LYD_CTX(new_node))); |
| 2423 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2424 | getnext_opts = 0; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 2425 | if (new_node->schema->flags & LYS_IS_OUTPUT) { |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2426 | getnext_opts = LYS_GETNEXT_OUTPUT; |
| 2427 | } |
| 2428 | |
Michal Vasko | a2756f0 | 2021-07-09 13:20:28 +0200 | [diff] [blame] | 2429 | if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2430 | /* find the anchor using hashes */ |
| 2431 | sparent = first_sibling->parent->schema; |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2432 | schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2433 | while (schema) { |
| 2434 | /* keep trying to find the first existing instance of the closest following schema sibling, |
| 2435 | * otherwise return NULL - inserting at the end */ |
| 2436 | if (!lyd_find_sibling_schema(first_sibling, schema, &match)) { |
| 2437 | break; |
| 2438 | } |
| 2439 | |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2440 | schema = lys_getnext(schema, sparent, NULL, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2441 | } |
| 2442 | } else { |
| 2443 | /* find the anchor without hashes */ |
| 2444 | match = (struct lyd_node *)first_sibling; |
Michal Vasko | 3a70862 | 2021-07-15 14:20:10 +0200 | [diff] [blame] | 2445 | sparent = lysc_data_parent(new_node->schema); |
| 2446 | if (!sparent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2447 | /* we are in top-level, skip all the data from preceding modules */ |
| 2448 | LY_LIST_FOR(match, match) { |
| 2449 | if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) { |
| 2450 | break; |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | /* get the first schema sibling */ |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2456 | schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2457 | |
| 2458 | found = 0; |
| 2459 | LY_LIST_FOR(match, match) { |
| 2460 | if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) { |
| 2461 | /* we have found an opaque node, which must be at the end, so use it OR |
| 2462 | * modules do not match, so we must have traversed all the data from new_node module (if any), |
| 2463 | * we have found the first node of the next module, that is what we want */ |
| 2464 | break; |
| 2465 | } |
| 2466 | |
| 2467 | /* skip schema nodes until we find the instantiated one */ |
| 2468 | while (!found) { |
| 2469 | if (new_node->schema == schema) { |
| 2470 | /* we have found the schema of the new node, continue search to find the first |
| 2471 | * data node with a different schema (after our schema) */ |
| 2472 | found = 1; |
| 2473 | break; |
| 2474 | } |
| 2475 | if (match->schema == schema) { |
| 2476 | /* current node (match) is a data node still before the new node, continue search in data */ |
| 2477 | break; |
| 2478 | } |
Michal Vasko | 93b1606 | 2020-12-09 18:14:18 +0100 | [diff] [blame] | 2479 | schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2480 | assert(schema); |
| 2481 | } |
| 2482 | |
| 2483 | if (found && (match->schema != new_node->schema)) { |
| 2484 | /* find the next node after we have found our node schema data instance */ |
| 2485 | break; |
| 2486 | } |
| 2487 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | return match; |
| 2491 | } |
| 2492 | |
| 2493 | /** |
| 2494 | * @brief Insert node after a sibling. |
| 2495 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2496 | * Handles inserting into NP containers and key-less lists. |
| 2497 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2498 | * @param[in] sibling Sibling to insert after. |
| 2499 | * @param[in] node Node to insert. |
| 2500 | */ |
| 2501 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2502 | lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2503 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2504 | struct lyd_node_inner *par; |
| 2505 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2506 | assert(!node->next && (node->prev == node)); |
| 2507 | |
| 2508 | node->next = sibling->next; |
| 2509 | node->prev = sibling; |
| 2510 | sibling->next = node; |
| 2511 | if (node->next) { |
| 2512 | /* sibling had a succeeding node */ |
| 2513 | node->next->prev = node; |
| 2514 | } else { |
| 2515 | /* sibling was last, find first sibling and change its prev */ |
| 2516 | if (sibling->parent) { |
| 2517 | sibling = sibling->parent->child; |
| 2518 | } else { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2519 | for ( ; sibling->prev->next != node; sibling = sibling->prev) {} |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2520 | } |
| 2521 | sibling->prev = node; |
| 2522 | } |
| 2523 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2524 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2525 | for (par = node->parent; par; par = par->parent) { |
| 2526 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 2527 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2528 | par->flags &= ~LYD_DEFAULT; |
| 2529 | } |
| 2530 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | /** |
| 2534 | * @brief Insert node before a sibling. |
| 2535 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2536 | * Handles inserting into NP containers and key-less lists. |
| 2537 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2538 | * @param[in] sibling Sibling to insert before. |
| 2539 | * @param[in] node Node to insert. |
| 2540 | */ |
| 2541 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2542 | lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2543 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2544 | struct lyd_node_inner *par; |
| 2545 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2546 | assert(!node->next && (node->prev == node)); |
| 2547 | |
| 2548 | node->next = sibling; |
| 2549 | /* covers situation of sibling being first */ |
| 2550 | node->prev = sibling->prev; |
| 2551 | sibling->prev = node; |
| 2552 | if (node->prev->next) { |
| 2553 | /* sibling had a preceding node */ |
| 2554 | node->prev->next = node; |
| 2555 | } else if (sibling->parent) { |
| 2556 | /* sibling was first and we must also change parent child pointer */ |
| 2557 | sibling->parent->child = node; |
| 2558 | } |
| 2559 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2560 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2561 | for (par = node->parent; par; par = par->parent) { |
| 2562 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 2563 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2564 | par->flags &= ~LYD_DEFAULT; |
| 2565 | } |
| 2566 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2570 | * @brief Insert node as the first and only child of a parent. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2571 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2572 | * Handles inserting into NP containers and key-less lists. |
| 2573 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2574 | * @param[in] parent Parent to insert into. |
| 2575 | * @param[in] node Node to insert. |
| 2576 | */ |
| 2577 | static void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2578 | lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2579 | { |
| 2580 | struct lyd_node_inner *par; |
| 2581 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2582 | assert(parent && !lyd_child(parent) && !node->next && (node->prev == node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2583 | assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2584 | |
| 2585 | par = (struct lyd_node_inner *)parent; |
| 2586 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2587 | par->child = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2588 | node->parent = par; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2589 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2590 | for ( ; par; par = par->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2591 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 2592 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2593 | par->flags &= ~LYD_DEFAULT; |
| 2594 | } |
| 2595 | } |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2596 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2597 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2598 | /** |
| 2599 | * @brief Learn whether a list instance has all the keys. |
| 2600 | * |
| 2601 | * @param[in] list List instance to check. |
| 2602 | * @return non-zero if all the keys were found, |
| 2603 | * @return 0 otherwise. |
| 2604 | */ |
| 2605 | static int |
| 2606 | lyd_insert_has_keys(const struct lyd_node *list) |
| 2607 | { |
| 2608 | const struct lyd_node *key; |
| 2609 | const struct lysc_node *skey = NULL; |
| 2610 | |
| 2611 | assert(list->schema->nodetype == LYS_LIST); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2612 | key = lyd_child(list); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2613 | while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 2614 | if (!key || (key->schema != skey)) { |
| 2615 | /* key missing */ |
| 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | key = key->next; |
| 2620 | } |
| 2621 | |
| 2622 | /* all keys found */ |
| 2623 | return 1; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | void |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2627 | lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2628 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2629 | struct lyd_node *anchor, *first_sibling; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2630 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2631 | /* inserting list without its keys is not supported */ |
| 2632 | assert((parent || first_sibling_p) && node && (node->hash || !node->schema)); |
Michal Vasko | f756c94 | 2020-11-06 17:21:37 +0100 | [diff] [blame] | 2633 | assert(!parent || !parent->schema || |
| 2634 | (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF))); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2635 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2636 | if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2637 | parent = lyd_parent(*first_sibling_p); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 2638 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2639 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2640 | /* get first sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2641 | first_sibling = parent ? lyd_child(parent) : *first_sibling_p; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2642 | |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2643 | if (last) { |
| 2644 | /* no next anchor */ |
| 2645 | anchor = NULL; |
| 2646 | } else { |
| 2647 | /* find the anchor, our next node, so we can insert before it */ |
| 2648 | anchor = lyd_insert_get_next_anchor(first_sibling, node); |
| 2649 | } |
| 2650 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2651 | if (anchor) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2652 | /* insert before the anchor */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2653 | lyd_insert_before_node(anchor, node); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 2654 | if (!parent && (*first_sibling_p == anchor)) { |
| 2655 | /* move first sibling */ |
| 2656 | *first_sibling_p = node; |
| 2657 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2658 | } else if (first_sibling) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2659 | /* insert as the last node */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2660 | lyd_insert_after_node(first_sibling->prev, node); |
| 2661 | } else if (parent) { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2662 | /* insert as the only child */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2663 | lyd_insert_only_child(parent, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2664 | } else { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2665 | /* insert as the only sibling */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2666 | *first_sibling_p = node; |
| 2667 | } |
| 2668 | |
| 2669 | /* insert into parent HT */ |
| 2670 | lyd_insert_hash(node); |
| 2671 | |
| 2672 | /* finish hashes for our parent, if needed and possible */ |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 2673 | if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2674 | lyd_hash(parent); |
| 2675 | |
| 2676 | /* now we can insert even the list into its parent HT */ |
| 2677 | lyd_insert_hash(parent); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2678 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2679 | } |
| 2680 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2681 | /** |
| 2682 | * @brief Check schema place of a node to be inserted. |
| 2683 | * |
| 2684 | * @param[in] parent Schema node of the parent data node. |
| 2685 | * @param[in] sibling Schema node of a sibling data node. |
| 2686 | * @param[in] schema Schema node if the data node to be inserted. |
| 2687 | * @return LY_SUCCESS on success. |
| 2688 | * @return LY_EINVAL if the place is invalid. |
| 2689 | */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2690 | static LY_ERR |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2691 | lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *sibling, const struct lysc_node *schema) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2692 | { |
| 2693 | const struct lysc_node *par2; |
| 2694 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2695 | assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2696 | assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 2697 | assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2698 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2699 | if (!schema || (!parent && !sibling)) { |
Michal Vasko | 71e795e | 2020-12-04 16:27:37 +0100 | [diff] [blame] | 2700 | /* opaque nodes can be inserted wherever */ |
| 2701 | return LY_SUCCESS; |
| 2702 | } |
| 2703 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2704 | if (!parent) { |
| 2705 | parent = lysc_data_parent(sibling); |
| 2706 | } |
| 2707 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2708 | /* find schema parent */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 2709 | par2 = lysc_data_parent(schema); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2710 | |
| 2711 | if (parent) { |
| 2712 | /* inner node */ |
| 2713 | if (par2 != parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2714 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2715 | parent->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2716 | return LY_EINVAL; |
| 2717 | } |
| 2718 | } else { |
| 2719 | /* top-level node */ |
| 2720 | if (par2) { |
Radek Krejci | f6d14cb | 2020-07-02 16:11:45 +0200 | [diff] [blame] | 2721 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2722 | return LY_EINVAL; |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | return LY_SUCCESS; |
| 2727 | } |
| 2728 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2729 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2730 | lyd_insert_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2731 | { |
| 2732 | struct lyd_node *iter; |
| 2733 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 2734 | LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2735 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2736 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2737 | LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2738 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 2739 | if (node->schema && (node->schema->flags & LYS_KEY)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2740 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2741 | return LY_EINVAL; |
| 2742 | } |
| 2743 | |
| 2744 | if (node->parent || node->prev->next) { |
| 2745 | lyd_unlink_tree(node); |
| 2746 | } |
| 2747 | |
| 2748 | while (node) { |
| 2749 | iter = node->next; |
| 2750 | lyd_unlink_tree(node); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2751 | lyd_insert_node(parent, NULL, node, 0); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2752 | node = iter; |
| 2753 | } |
| 2754 | return LY_SUCCESS; |
| 2755 | } |
| 2756 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2757 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2758 | lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first) |
| 2759 | { |
| 2760 | struct lyd_node *iter; |
| 2761 | |
| 2762 | LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next, |
| 2763 | !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL); |
| 2764 | |
| 2765 | if (first->schema && (first->schema->flags & LYS_KEY)) { |
| 2766 | LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name); |
| 2767 | return LY_EINVAL; |
| 2768 | } |
| 2769 | |
| 2770 | while (first) { |
| 2771 | iter = first->next; |
| 2772 | lyd_unlink_tree(first); |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 2773 | lyd_insert_node(parent, NULL, first, 1); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2774 | first = iter; |
| 2775 | } |
| 2776 | return LY_SUCCESS; |
| 2777 | } |
| 2778 | |
| 2779 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2780 | lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2781 | { |
| 2782 | struct lyd_node *iter; |
| 2783 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2784 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2785 | LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2786 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2787 | if (sibling) { |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2788 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2789 | } |
| 2790 | |
Michal Vasko | 553d0b5 | 2020-12-04 16:27:52 +0100 | [diff] [blame] | 2791 | if (sibling == node) { |
| 2792 | /* we need to keep the connection to siblings so we can insert into them */ |
| 2793 | sibling = sibling->prev; |
| 2794 | } |
| 2795 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2796 | if (node->parent || node->prev->next) { |
| 2797 | lyd_unlink_tree(node); |
| 2798 | } |
| 2799 | |
| 2800 | while (node) { |
Michal Vasko | 71e795e | 2020-12-04 16:27:37 +0100 | [diff] [blame] | 2801 | if (lysc_is_key(node->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2802 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2803 | return LY_EINVAL; |
| 2804 | } |
| 2805 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2806 | iter = node->next; |
| 2807 | lyd_unlink_tree(node); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 2808 | lyd_insert_node(NULL, &sibling, node, 0); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2809 | node = iter; |
| 2810 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2811 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2812 | if (first) { |
| 2813 | /* find the first sibling */ |
| 2814 | *first = sibling; |
| 2815 | while ((*first)->prev->next) { |
| 2816 | *first = (*first)->prev; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | return LY_SUCCESS; |
| 2821 | } |
| 2822 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2823 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2824 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 2825 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2826 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2827 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2828 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2829 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2830 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2831 | if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2832 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2833 | return LY_EINVAL; |
| 2834 | } |
Michal Vasko | 7508ef2 | 2022-02-22 14:13:10 +0100 | [diff] [blame] | 2835 | if (lysc_is_key(sibling->schema)) { |
| 2836 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys."); |
| 2837 | return LY_EINVAL; |
| 2838 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2839 | |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 2840 | lyd_unlink_tree(node); |
| 2841 | lyd_insert_before_node(sibling, node); |
| 2842 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2843 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2844 | return LY_SUCCESS; |
| 2845 | } |
| 2846 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2847 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2848 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 2849 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2850 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 2851 | LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2852 | |
Michal Vasko | 717a4c3 | 2020-12-07 09:40:10 +0100 | [diff] [blame] | 2853 | LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2854 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2855 | if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2856 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2857 | return LY_EINVAL; |
| 2858 | } |
Michal Vasko | 7508ef2 | 2022-02-22 14:13:10 +0100 | [diff] [blame] | 2859 | if (sibling->next && lysc_is_key(sibling->next->schema)) { |
| 2860 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys."); |
| 2861 | return LY_EINVAL; |
| 2862 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2863 | |
Michal Vasko | 4bc2ce3 | 2020-12-08 10:06:16 +0100 | [diff] [blame] | 2864 | lyd_unlink_tree(node); |
| 2865 | lyd_insert_after_node(sibling, node); |
| 2866 | lyd_insert_hash(node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2867 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2868 | return LY_SUCCESS; |
| 2869 | } |
| 2870 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2871 | LIBYANG_API_DEF void |
Michal Vasko | 66d508c | 2021-07-21 16:07:09 +0200 | [diff] [blame] | 2872 | lyd_unlink_siblings(struct lyd_node *node) |
| 2873 | { |
| 2874 | struct lyd_node *next, *elem, *first = NULL; |
| 2875 | |
| 2876 | LY_LIST_FOR_SAFE(node, next, elem) { |
| 2877 | lyd_unlink_tree(elem); |
| 2878 | lyd_insert_node(NULL, &first, elem, 1); |
| 2879 | } |
| 2880 | } |
| 2881 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2882 | LIBYANG_API_DEF void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2883 | lyd_unlink_tree(struct lyd_node *node) |
| 2884 | { |
| 2885 | struct lyd_node *iter; |
| 2886 | |
| 2887 | if (!node) { |
| 2888 | return; |
| 2889 | } |
| 2890 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2891 | /* update hashes while still linked into the tree */ |
| 2892 | lyd_unlink_hash(node); |
| 2893 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2894 | /* unlink from siblings */ |
| 2895 | if (node->prev->next) { |
| 2896 | node->prev->next = node->next; |
| 2897 | } |
| 2898 | if (node->next) { |
| 2899 | node->next->prev = node->prev; |
| 2900 | } else { |
| 2901 | /* unlinking the last node */ |
| 2902 | if (node->parent) { |
| 2903 | iter = node->parent->child; |
| 2904 | } else { |
| 2905 | iter = node->prev; |
| 2906 | while (iter->prev != node) { |
| 2907 | iter = iter->prev; |
| 2908 | } |
| 2909 | } |
| 2910 | /* update the "last" pointer from the first node */ |
| 2911 | iter->prev = node->prev; |
| 2912 | } |
| 2913 | |
| 2914 | /* unlink from parent */ |
| 2915 | if (node->parent) { |
| 2916 | if (node->parent->child == node) { |
| 2917 | /* the node is the first child */ |
| 2918 | node->parent->child = node->next; |
| 2919 | } |
| 2920 | |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2921 | /* check for NP container whether its last non-default node is not being unlinked */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2922 | if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) && |
| 2923 | !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) { |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2924 | LY_LIST_FOR(node->parent->child, iter) { |
| 2925 | if ((iter != node) && !(iter->flags & LYD_DEFAULT)) { |
| 2926 | break; |
| 2927 | } |
| 2928 | } |
| 2929 | if (!iter) { |
| 2930 | node->parent->flags |= LYD_DEFAULT; |
| 2931 | } |
| 2932 | } |
| 2933 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2934 | node->parent = NULL; |
| 2935 | } |
| 2936 | |
| 2937 | node->next = NULL; |
| 2938 | node->prev = node; |
| 2939 | } |
| 2940 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2941 | void |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 2942 | lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2943 | { |
| 2944 | struct lyd_meta *last, *iter; |
| 2945 | |
| 2946 | assert(parent); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2947 | |
| 2948 | if (!meta) { |
| 2949 | return; |
| 2950 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2951 | |
| 2952 | for (iter = meta; iter; iter = iter->next) { |
| 2953 | iter->parent = parent; |
| 2954 | } |
| 2955 | |
| 2956 | /* insert as the last attribute */ |
| 2957 | if (parent->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2958 | for (last = parent->meta; last->next; last = last->next) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2959 | last->next = meta; |
| 2960 | } else { |
| 2961 | parent->meta = meta; |
| 2962 | } |
| 2963 | |
| 2964 | /* remove default flags from NP containers */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 2965 | while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2966 | parent->flags &= ~LYD_DEFAULT; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 2967 | parent = lyd_parent(parent); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2968 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2972 | lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 2973 | size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2974 | void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt, ly_bool *incomplete) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2975 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2976 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2977 | struct lysc_ext_instance *ant = NULL; |
Michal Vasko | b0534f9 | 2021-07-01 13:24:03 +0200 | [diff] [blame] | 2978 | const struct lysc_type **ant_type; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2979 | struct lyd_meta *mt, *last; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2980 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2981 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2982 | assert((parent || meta) && mod); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 2983 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 2984 | LOG_LOCSET(ctx_node, parent, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2985 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2986 | LY_ARRAY_FOR(mod->compiled->exts, u) { |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 2987 | if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) && |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2988 | !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2989 | /* we have the annotation definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2990 | ant = &mod->compiled->exts[u]; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2991 | break; |
| 2992 | } |
| 2993 | } |
| 2994 | if (!ant) { |
| 2995 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2996 | LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 2997 | mod->name, (int)name_len, name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2998 | ret = LY_EINVAL; |
| 2999 | goto cleanup; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3000 | } |
| 3001 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3002 | mt = calloc(1, sizeof *mt); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3003 | LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3004 | mt->parent = parent; |
| 3005 | mt->annotation = ant; |
Michal Vasko | b0534f9 | 2021-07-01 13:24:03 +0200 | [diff] [blame] | 3006 | ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage; |
| 3007 | ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3008 | ctx_node, incomplete); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3009 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
| 3010 | ret = lydict_insert(mod->ctx, name, name_len, &mt->name); |
| 3011 | LY_CHECK_ERR_GOTO(ret, free(mt), cleanup); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3012 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 3013 | /* insert as the last attribute */ |
| 3014 | if (parent) { |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 3015 | lyd_insert_meta(parent, mt, clear_dflt); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3016 | } else if (*meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3017 | for (last = *meta; last->next; last = last->next) {} |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3018 | last->next = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3019 | } |
| 3020 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3021 | if (meta) { |
| 3022 | *meta = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3023 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3024 | |
| 3025 | cleanup: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3026 | LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3027 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3028 | } |
| 3029 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 3030 | void |
| 3031 | lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr) |
| 3032 | { |
| 3033 | struct lyd_attr *last, *iter; |
| 3034 | struct lyd_node_opaq *opaq; |
| 3035 | |
| 3036 | assert(parent && !parent->schema); |
| 3037 | |
| 3038 | if (!attr) { |
| 3039 | return; |
| 3040 | } |
| 3041 | |
| 3042 | opaq = (struct lyd_node_opaq *)parent; |
| 3043 | for (iter = attr; iter; iter = iter->next) { |
| 3044 | iter->parent = opaq; |
| 3045 | } |
| 3046 | |
| 3047 | /* insert as the last attribute */ |
| 3048 | if (opaq->attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3049 | for (last = opaq->attr; last->next; last = last->next) {} |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 3050 | last->next = attr; |
| 3051 | } else { |
| 3052 | opaq->attr = attr; |
| 3053 | } |
| 3054 | } |
| 3055 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3056 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3057 | lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len, |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 3058 | const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3059 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3060 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3061 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 3062 | struct lyd_attr *at, *last; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3063 | |
| 3064 | assert(ctx && (parent || attr) && (!parent || !parent->schema)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3065 | assert(name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3066 | |
Michal Vasko | 2a3722d | 2021-06-16 11:52:39 +0200 | [diff] [blame] | 3067 | if (!value_len && (!dynamic || !*dynamic)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3068 | value = ""; |
| 3069 | } |
| 3070 | |
| 3071 | at = calloc(1, sizeof *at); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 3072 | LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 3073 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 3074 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 3075 | if (prefix_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 3076 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 3077 | } |
| 3078 | if (module_key_len) { |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 3079 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 3080 | } |
| 3081 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3082 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3083 | ret = lydict_insert_zc(ctx, (char *)value, &at->value); |
| 3084 | LY_CHECK_GOTO(ret, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3085 | *dynamic = 0; |
| 3086 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3087 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3088 | } |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 3089 | at->format = format; |
| 3090 | at->val_prefix_data = val_prefix_data; |
| 3091 | at->hints = hints; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3092 | |
| 3093 | /* insert as the last attribute */ |
| 3094 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 3095 | lyd_insert_attr(parent, at); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3096 | } else if (*attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3097 | for (last = *attr; last->next; last = last->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3098 | last->next = at; |
| 3099 | } |
| 3100 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3101 | finish: |
| 3102 | if (ret) { |
| 3103 | lyd_free_attr_single(ctx, at); |
| 3104 | } else if (attr) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3105 | *attr = at; |
| 3106 | } |
| 3107 | return LY_SUCCESS; |
| 3108 | } |
| 3109 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3110 | LIBYANG_API_DEF const struct lyd_node_term * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3111 | lyd_target(const struct ly_path *path, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3112 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3113 | struct lyd_node *target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3114 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3115 | if (ly_path_eval(path, tree, &target)) { |
| 3116 | return NULL; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3117 | } |
| 3118 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3119 | return (struct lyd_node_term *)target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3120 | } |
| 3121 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3122 | /** |
| 3123 | * @brief Check the equality of the two schemas from different contexts. |
| 3124 | * |
| 3125 | * @param schema1 of first node. |
| 3126 | * @param schema2 of second node. |
| 3127 | * @return 1 if the schemas are equal otherwise 0. |
| 3128 | */ |
| 3129 | static ly_bool |
| 3130 | lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2) |
| 3131 | { |
| 3132 | if (!schema1 && !schema2) { |
| 3133 | return 1; |
| 3134 | } else if (!schema1 || !schema2) { |
| 3135 | return 0; |
| 3136 | } |
| 3137 | |
| 3138 | assert(schema1->module->ctx != schema2->module->ctx); |
| 3139 | |
| 3140 | if (schema1->nodetype != schema2->nodetype) { |
| 3141 | return 0; |
| 3142 | } |
| 3143 | |
| 3144 | if (strcmp(schema1->name, schema2->name)) { |
| 3145 | return 0; |
| 3146 | } |
| 3147 | |
| 3148 | if (strcmp(schema1->module->name, schema2->module->name)) { |
| 3149 | return 0; |
| 3150 | } |
| 3151 | |
| 3152 | if (schema1->module->revision || schema2->module->revision) { |
| 3153 | if (!schema1->module->revision || !schema2->module->revision) { |
| 3154 | return 0; |
| 3155 | } |
| 3156 | if (strcmp(schema1->module->revision, schema2->module->revision)) { |
| 3157 | return 0; |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | return 1; |
| 3162 | } |
| 3163 | |
| 3164 | /** |
| 3165 | * @brief Check the equality of the schemas for all parent nodes. |
| 3166 | * |
| 3167 | * Both nodes must be from different contexts. |
| 3168 | * |
| 3169 | * @param node1 Data of first node. |
| 3170 | * @param node2 Data of second node. |
| 3171 | * @return 1 if the all related parental schemas are equal otherwise 0. |
| 3172 | */ |
| 3173 | static ly_bool |
| 3174 | lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2) |
| 3175 | { |
| 3176 | const struct lysc_node *parent1, *parent2; |
| 3177 | |
| 3178 | assert(node1 && node2); |
| 3179 | |
| 3180 | for (parent1 = node1->schema->parent, parent2 = node2->schema->parent; |
| 3181 | parent1 && parent2; |
| 3182 | parent1 = parent1->parent, parent2 = parent2->parent) { |
| 3183 | if (!lyd_compare_schema_equal(parent1, parent2)) { |
| 3184 | return 0; |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | if (parent1 || parent2) { |
| 3189 | return 0; |
| 3190 | } |
| 3191 | |
| 3192 | return 1; |
| 3193 | } |
| 3194 | |
| 3195 | /** |
| 3196 | * @brief Internal implementation of @ref lyd_compare_single. |
| 3197 | * @copydoc lyd_compare_single |
| 3198 | * @param[in] parental_schemas_checked Flag used for optimization. |
| 3199 | * When this function is called for the first time, the flag must be set to 0. |
| 3200 | * The @ref lyd_compare_schema_parents_equal should be called only once during |
| 3201 | * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body. |
| 3202 | */ |
| 3203 | static LY_ERR |
| 3204 | lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2, |
| 3205 | uint32_t options, ly_bool parental_schemas_checked) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3206 | { |
| 3207 | const struct lyd_node *iter1, *iter2; |
| 3208 | struct lyd_node_term *term1, *term2; |
| 3209 | struct lyd_node_any *any1, *any2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3210 | struct lyd_node_opaq *opaq1, *opaq2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3211 | size_t len1, len2; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 3212 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3213 | if (!node1 || !node2) { |
| 3214 | if (node1 == node2) { |
| 3215 | return LY_SUCCESS; |
| 3216 | } else { |
| 3217 | return LY_ENOT; |
| 3218 | } |
| 3219 | } |
| 3220 | |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3221 | if (LYD_CTX(node1) == LYD_CTX(node2)) { |
| 3222 | /* same contexts */ |
| 3223 | if (node1->schema != node2->schema) { |
| 3224 | return LY_ENOT; |
| 3225 | } |
| 3226 | } else { |
| 3227 | /* different contexts */ |
| 3228 | if (!lyd_compare_schema_equal(node1->schema, node2->schema)) { |
| 3229 | return LY_ENOT; |
| 3230 | } |
| 3231 | if (!parental_schemas_checked) { |
| 3232 | if (!lyd_compare_schema_parents_equal(node1, node2)) { |
| 3233 | return LY_ENOT; |
| 3234 | } |
| 3235 | parental_schemas_checked = 1; |
| 3236 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | if (node1->hash != node2->hash) { |
| 3240 | return LY_ENOT; |
| 3241 | } |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3242 | /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3243 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3244 | if (!node1->schema) { |
| 3245 | opaq1 = (struct lyd_node_opaq *)node1; |
| 3246 | opaq2 = (struct lyd_node_opaq *)node2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3247 | if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) || |
| 3248 | (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3249 | return LY_ENOT; |
| 3250 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3251 | switch (opaq1->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3252 | case LY_VALUE_XML: |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 3253 | if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value, |
| 3254 | opaq2->val_prefix_data)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3255 | return LY_ENOT; |
| 3256 | } |
| 3257 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 3258 | case LY_VALUE_JSON: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 3259 | /* prefixes in JSON are unique, so it is not necessary to canonize the values */ |
| 3260 | if (strcmp(opaq1->value, opaq2->value)) { |
| 3261 | return LY_ENOT; |
| 3262 | } |
| 3263 | break; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 3264 | default: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3265 | /* not allowed */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3266 | LOGINT(LYD_CTX(node1)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3267 | return LY_EINT; |
| 3268 | } |
| 3269 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 3270 | iter1 = opaq1->child; |
| 3271 | iter2 = opaq2->child; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3272 | goto all_children_compare; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3273 | } |
| 3274 | return LY_SUCCESS; |
| 3275 | } else { |
| 3276 | switch (node1->schema->nodetype) { |
| 3277 | case LYS_LEAF: |
| 3278 | case LYS_LEAFLIST: |
| 3279 | if (options & LYD_COMPARE_DEFAULTS) { |
| 3280 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 3281 | return LY_ENOT; |
| 3282 | } |
| 3283 | } |
| 3284 | |
Michal Vasko | 1a2bf2e | 2020-06-16 13:07:48 +0200 | [diff] [blame] | 3285 | term1 = (struct lyd_node_term *)node1; |
| 3286 | term2 = (struct lyd_node_term *)node2; |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3287 | |
| 3288 | /* same contexts */ |
| 3289 | if (LYD_CTX(node1) == LYD_CTX(node2)) { |
| 3290 | return term1->value.realtype->plugin->compare(&term1->value, &term2->value); |
| 3291 | } |
| 3292 | |
| 3293 | /* different contexts */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 3294 | if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3295 | return LY_ENOT; |
| 3296 | } |
| 3297 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3298 | case LYS_CONTAINER: |
| 3299 | if (options & LYD_COMPARE_DEFAULTS) { |
| 3300 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 3301 | return LY_ENOT; |
| 3302 | } |
| 3303 | } |
| 3304 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3305 | iter1 = lyd_child(node1); |
| 3306 | iter2 = lyd_child(node2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3307 | goto all_children_compare; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3308 | } |
| 3309 | return LY_SUCCESS; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3310 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3311 | case LYS_ACTION: |
| 3312 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 3313 | /* TODO action/RPC |
| 3314 | goto all_children_compare; |
| 3315 | */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3316 | } |
| 3317 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3318 | case LYS_NOTIF: |
| 3319 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 3320 | /* TODO Notification |
| 3321 | goto all_children_compare; |
| 3322 | */ |
| 3323 | } |
| 3324 | return LY_SUCCESS; |
| 3325 | case LYS_LIST: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3326 | iter1 = lyd_child(node1); |
| 3327 | iter2 = lyd_child(node2); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3328 | |
| 3329 | if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) { |
| 3330 | /* lists with keys, their equivalence is based on their keys */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 3331 | for (const struct lysc_node *key = lysc_node_child(node1->schema); |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 3332 | key && (key->flags & LYS_KEY); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3333 | key = key->next) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3334 | if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3335 | return LY_ENOT; |
| 3336 | } |
| 3337 | iter1 = iter1->next; |
| 3338 | iter2 = iter2->next; |
| 3339 | } |
| 3340 | } else { |
| 3341 | /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */ |
| 3342 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3343 | all_children_compare: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3344 | if (!iter1 && !iter2) { |
| 3345 | /* no children, nothing to compare */ |
| 3346 | return LY_SUCCESS; |
| 3347 | } |
| 3348 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3349 | for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) { |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3350 | if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3351 | return LY_ENOT; |
| 3352 | } |
| 3353 | } |
| 3354 | if (iter1 || iter2) { |
| 3355 | return LY_ENOT; |
| 3356 | } |
| 3357 | } |
| 3358 | return LY_SUCCESS; |
| 3359 | case LYS_ANYXML: |
| 3360 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3361 | any1 = (struct lyd_node_any *)node1; |
| 3362 | any2 = (struct lyd_node_any *)node2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3363 | |
| 3364 | if (any1->value_type != any2->value_type) { |
| 3365 | return LY_ENOT; |
| 3366 | } |
| 3367 | switch (any1->value_type) { |
| 3368 | case LYD_ANYDATA_DATATREE: |
| 3369 | iter1 = any1->value.tree; |
| 3370 | iter2 = any2->value.tree; |
| 3371 | goto all_children_compare; |
| 3372 | case LYD_ANYDATA_STRING: |
| 3373 | case LYD_ANYDATA_XML: |
| 3374 | case LYD_ANYDATA_JSON: |
| 3375 | len1 = strlen(any1->value.str); |
| 3376 | len2 = strlen(any2->value.str); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3377 | if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3378 | return LY_ENOT; |
| 3379 | } |
| 3380 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3381 | case LYD_ANYDATA_LYB: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 3382 | len1 = lyd_lyb_data_length(any1->value.mem); |
| 3383 | len2 = lyd_lyb_data_length(any2->value.mem); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3384 | if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3385 | return LY_ENOT; |
| 3386 | } |
| 3387 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3388 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3389 | } |
| 3390 | } |
| 3391 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3392 | LOGINT(LYD_CTX(node1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 3393 | return LY_EINT; |
| 3394 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3395 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3396 | LIBYANG_API_DEF LY_ERR |
aPiecek | 2f63f95 | 2021-03-30 12:22:18 +0200 | [diff] [blame] | 3397 | lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
| 3398 | { |
| 3399 | return lyd_compare_single_(node1, node2, options, 0); |
| 3400 | } |
| 3401 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3402 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3403 | lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3404 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3405 | for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3406 | LY_CHECK_RET(lyd_compare_single(node1, node2, options)); |
| 3407 | } |
| 3408 | |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 3409 | if (node1 == node2) { |
| 3410 | return LY_SUCCESS; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3411 | } |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 3412 | return LY_ENOT; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3413 | } |
| 3414 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3415 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 3416 | lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2) |
| 3417 | { |
| 3418 | if (!meta1 || !meta2) { |
| 3419 | if (meta1 == meta2) { |
| 3420 | return LY_SUCCESS; |
| 3421 | } else { |
| 3422 | return LY_ENOT; |
| 3423 | } |
| 3424 | } |
| 3425 | |
Michal Vasko | a808306 | 2020-11-06 17:22:10 +0100 | [diff] [blame] | 3426 | if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) { |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 3427 | return LY_ENOT; |
| 3428 | } |
| 3429 | |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 3430 | return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value); |
| 3431 | } |
| 3432 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3433 | /** |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3434 | * @brief Create a copy of the attribute. |
| 3435 | * |
| 3436 | * @param[in] attr Attribute to copy. |
| 3437 | * @param[in] node Opaque where to append the new attribute. |
| 3438 | * @param[out] dup Optional created attribute copy. |
| 3439 | * @return LY_ERR value. |
| 3440 | */ |
| 3441 | static LY_ERR |
| 3442 | lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup) |
| 3443 | { |
| 3444 | LY_ERR ret = LY_SUCCESS; |
| 3445 | struct lyd_attr *a, *last; |
| 3446 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node; |
| 3447 | |
| 3448 | LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL); |
| 3449 | |
| 3450 | /* create a copy */ |
| 3451 | a = calloc(1, sizeof *attr); |
| 3452 | LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 3453 | |
| 3454 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish); |
| 3455 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish); |
| 3456 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish); |
| 3457 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish); |
| 3458 | a->hints = attr->hints; |
| 3459 | a->format = attr->format; |
| 3460 | if (attr->val_prefix_data) { |
| 3461 | ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data); |
| 3462 | LY_CHECK_GOTO(ret, finish); |
| 3463 | } |
| 3464 | |
| 3465 | /* insert as the last attribute */ |
| 3466 | a->parent = opaq; |
| 3467 | if (opaq->attr) { |
| 3468 | for (last = opaq->attr; last->next; last = last->next) {} |
| 3469 | last->next = a; |
| 3470 | } else { |
| 3471 | opaq->attr = a; |
| 3472 | } |
| 3473 | |
| 3474 | finish: |
| 3475 | if (ret) { |
| 3476 | lyd_free_attr_single(LYD_CTX(node), a); |
| 3477 | } else if (dup) { |
| 3478 | *dup = a; |
| 3479 | } |
| 3480 | return LY_SUCCESS; |
| 3481 | } |
| 3482 | |
| 3483 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3484 | * @brief Find @p schema equivalent in @p trg_ctx. |
| 3485 | * |
| 3486 | * @param[in] schema Schema node to find. |
| 3487 | * @param[in] trg_ctx Target context to search in. |
| 3488 | * @param[in] parent Data parent of @p schema, if any. |
| 3489 | * @param[out] trg_schema Found schema from @p trg_ctx to use. |
| 3490 | * @return LY_RRR value. |
| 3491 | */ |
| 3492 | static LY_ERR |
| 3493 | lyd_dup_find_schema(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, struct lyd_node *parent, |
| 3494 | const struct lysc_node **trg_schema) |
| 3495 | { |
| 3496 | const struct lysc_node *node = NULL, *sparent = NULL; |
| 3497 | const struct lys_module *mod = NULL; |
| 3498 | char *path; |
| 3499 | |
| 3500 | if (!schema) { |
| 3501 | /* opaque node */ |
| 3502 | *trg_schema = NULL; |
| 3503 | return LY_SUCCESS; |
| 3504 | } |
| 3505 | |
| 3506 | if (parent && parent->schema) { |
| 3507 | /* start from schema parent */ |
| 3508 | sparent = parent->schema; |
| 3509 | } else { |
| 3510 | /* start from module */ |
| 3511 | mod = ly_ctx_get_module_implemented(trg_ctx, schema->module->name); |
| 3512 | if (!mod) { |
| 3513 | LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.", |
| 3514 | schema->module->name); |
| 3515 | return LY_ENOTFOUND; |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | /* find the schema node */ |
| 3520 | while ((node = lys_getnext(node, sparent, mod ? mod->compiled : NULL, 0))) { |
| 3521 | if (!strcmp(node->module->name, schema->module->name) && !strcmp(node->name, schema->name)) { |
| 3522 | *trg_schema = node; |
| 3523 | return LY_SUCCESS; |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | /* schema node not found */ |
| 3528 | path = lysc_path(schema, LYSC_PATH_LOG, NULL, 0); |
| 3529 | LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path); |
| 3530 | free(path); |
| 3531 | return LY_ENOTFOUND; |
| 3532 | } |
| 3533 | |
| 3534 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3535 | * @brief Duplicate a single node and connect it into @p parent (if present) or last of @p first siblings. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3536 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3537 | * Ignores ::LYD_DUP_WITH_PARENTS and ::LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup(). |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 3538 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3539 | * @param[in] node Node to duplicate. |
| 3540 | * @param[in] trg_ctx Target context for duplicated nodes. |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 3541 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3542 | * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for |
| 3543 | * recursive duplication as an optimization. |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 3544 | * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set. |
| 3545 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3546 | * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first). |
| 3547 | * @return LY_ERR value. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3548 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3549 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3550 | lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last, |
| 3551 | struct lyd_node **first, uint32_t options, struct lyd_node **dup_p) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3552 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3553 | LY_ERR ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3554 | struct lyd_node *dup = NULL; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 3555 | struct lyd_meta *meta; |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3556 | struct lyd_attr *attr; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 3557 | struct lyd_node_any *any; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3558 | const struct lysc_type *type; |
| 3559 | const char *val_can; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3560 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3561 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3562 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3563 | if (!node->schema) { |
| 3564 | dup = calloc(1, sizeof(struct lyd_node_opaq)); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3565 | ((struct lyd_node_opaq *)dup)->ctx = trg_ctx; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3566 | } else { |
| 3567 | switch (node->schema->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3568 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3569 | case LYS_ACTION: |
| 3570 | case LYS_NOTIF: |
| 3571 | case LYS_CONTAINER: |
| 3572 | case LYS_LIST: |
| 3573 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 3574 | break; |
| 3575 | case LYS_LEAF: |
| 3576 | case LYS_LEAFLIST: |
| 3577 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 3578 | break; |
| 3579 | case LYS_ANYDATA: |
| 3580 | case LYS_ANYXML: |
| 3581 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 3582 | break; |
| 3583 | default: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3584 | LOGINT(trg_ctx); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3585 | ret = LY_EINT; |
| 3586 | goto error; |
| 3587 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3588 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3589 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3590 | |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 3591 | if (options & LYD_DUP_WITH_FLAGS) { |
| 3592 | dup->flags = node->flags; |
| 3593 | } else { |
| 3594 | dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW; |
| 3595 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3596 | if (trg_ctx == LYD_CTX(node)) { |
| 3597 | dup->schema = node->schema; |
| 3598 | } else { |
| 3599 | LY_CHECK_GOTO(ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema), error); |
| 3600 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3601 | dup->prev = dup; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3602 | |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3603 | /* duplicate metadata/attributes */ |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3604 | if (!(options & LYD_DUP_NO_META)) { |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3605 | if (!node->schema) { |
| 3606 | LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) { |
| 3607 | LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error); |
| 3608 | } |
| 3609 | } else { |
| 3610 | LY_LIST_FOR(node->meta, meta) { |
| 3611 | LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error); |
| 3612 | } |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3613 | } |
| 3614 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3615 | |
| 3616 | /* nodetype-specific work */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3617 | if (!dup->schema) { |
| 3618 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup; |
| 3619 | struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node; |
| 3620 | struct lyd_node *child; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3621 | |
| 3622 | if (options & LYD_DUP_RECURSIVE) { |
| 3623 | /* duplicate all the children */ |
| 3624 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3625 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3626 | } |
| 3627 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3628 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error); |
| 3629 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error); |
| 3630 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error); |
| 3631 | LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error); |
Michal Vasko | 9cf6242 | 2021-07-01 13:29:32 +0200 | [diff] [blame] | 3632 | opaq->hints = orig->hints; |
| 3633 | opaq->format = orig->format; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 3634 | if (orig->val_prefix_data) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3635 | ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 3636 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3637 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3638 | } else if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 3639 | struct lyd_node_term *term = (struct lyd_node_term *)dup; |
| 3640 | struct lyd_node_term *orig = (struct lyd_node_term *)node; |
| 3641 | |
| 3642 | term->hash = orig->hash; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3643 | if (trg_ctx == LYD_CTX(node)) { |
| 3644 | ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value); |
| 3645 | LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error); |
| 3646 | } else { |
| 3647 | /* store canonical value in the target context */ |
| 3648 | val_can = lyd_get_value(node); |
| 3649 | type = ((struct lysc_node_leaf *)term->schema)->type; |
| 3650 | ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL, |
| 3651 | LYD_HINT_DATA, term->schema, NULL); |
| 3652 | LY_CHECK_GOTO(ret, error); |
| 3653 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3654 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 3655 | struct lyd_node_inner *orig = (struct lyd_node_inner *)node; |
| 3656 | struct lyd_node *child; |
| 3657 | |
| 3658 | if (options & LYD_DUP_RECURSIVE) { |
| 3659 | /* duplicate all the children */ |
| 3660 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3661 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3662 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3663 | } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3664 | /* always duplicate keys of a list */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3665 | child = orig->child; |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 3666 | for (const struct lysc_node *key = lysc_node_child(dup->schema); |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 3667 | key && (key->flags & LYS_KEY); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3668 | key = key->next) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3669 | if (!child) { |
| 3670 | /* possibly not keys are present in filtered tree */ |
| 3671 | break; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3672 | } else if (child->schema != key) { |
| 3673 | /* possibly not all keys are present in filtered tree, |
| 3674 | * but there can be also some non-key nodes */ |
| 3675 | continue; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3676 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3677 | LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3678 | child = child->next; |
| 3679 | } |
| 3680 | } |
| 3681 | lyd_hash(dup); |
| 3682 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 3683 | dup->hash = node->hash; |
| 3684 | any = (struct lyd_node_any *)node; |
| 3685 | LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3686 | } |
| 3687 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3688 | /* insert */ |
Michal Vasko | 67177e5 | 2021-08-25 11:15:15 +0200 | [diff] [blame] | 3689 | lyd_insert_node(parent, first, dup, insert_last); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3690 | |
| 3691 | if (dup_p) { |
| 3692 | *dup_p = dup; |
| 3693 | } |
| 3694 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3695 | |
| 3696 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 3697 | lyd_free_tree(dup); |
| 3698 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3699 | } |
| 3700 | |
Michal Vasko | 29d674b | 2021-08-25 11:18:35 +0200 | [diff] [blame] | 3701 | /** |
| 3702 | * @brief Get a parent node to connect duplicated subtree to. |
| 3703 | * |
| 3704 | * @param[in] node Node (subtree) to duplicate. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3705 | * @param[in] trg_ctx Target context for duplicated nodes. |
Michal Vasko | 29d674b | 2021-08-25 11:18:35 +0200 | [diff] [blame] | 3706 | * @param[in] parent Initial parent to connect to. |
| 3707 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 3708 | * @param[out] dup_parent First duplicated parent node, if any. |
| 3709 | * @param[out] local_parent Correct parent to directly connect duplicated @p node to. |
| 3710 | * @return LY_ERR value. |
| 3711 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3712 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3713 | lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent, |
| 3714 | uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3715 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3716 | const struct lyd_node_inner *orig_parent, *iter; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3717 | ly_bool repeat = 1; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3718 | |
| 3719 | *dup_parent = NULL; |
| 3720 | *local_parent = NULL; |
| 3721 | |
| 3722 | for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 3723 | if (parent && (parent->schema == orig_parent->schema)) { |
| 3724 | /* stop creating parents, connect what we have into the provided parent */ |
| 3725 | iter = parent; |
| 3726 | repeat = 0; |
| 3727 | } else { |
| 3728 | iter = NULL; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3729 | LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, trg_ctx, NULL, 0, (struct lyd_node **)&iter, options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3730 | (struct lyd_node **)&iter)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3731 | } |
| 3732 | if (!*local_parent) { |
| 3733 | *local_parent = (struct lyd_node_inner *)iter; |
| 3734 | } |
| 3735 | if (iter->child) { |
| 3736 | /* 1) list - add after keys |
| 3737 | * 2) provided parent with some children */ |
| 3738 | iter->child->prev->next = *dup_parent; |
| 3739 | if (*dup_parent) { |
| 3740 | (*dup_parent)->prev = iter->child->prev; |
| 3741 | iter->child->prev = *dup_parent; |
| 3742 | } |
| 3743 | } else { |
| 3744 | ((struct lyd_node_inner *)iter)->child = *dup_parent; |
| 3745 | } |
| 3746 | if (*dup_parent) { |
| 3747 | (*dup_parent)->parent = (struct lyd_node_inner *)iter; |
| 3748 | } |
| 3749 | *dup_parent = (struct lyd_node *)iter; |
| 3750 | } |
| 3751 | |
| 3752 | if (repeat && parent) { |
| 3753 | /* given parent and created parents chain actually do not interconnect */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3754 | LOGERR(trg_ctx, LY_EINVAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3755 | "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3756 | return LY_EINVAL; |
| 3757 | } |
| 3758 | |
| 3759 | return LY_SUCCESS; |
| 3760 | } |
| 3761 | |
| 3762 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3763 | lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options, |
| 3764 | ly_bool nosiblings, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3765 | { |
| 3766 | LY_ERR rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3767 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 3768 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3769 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 3770 | struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3771 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3772 | assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx))); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3773 | |
| 3774 | if (options & LYD_DUP_WITH_PARENTS) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3775 | LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, trg_ctx, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META), |
Michal Vasko | 2090975 | 2021-05-18 16:13:38 +0200 | [diff] [blame] | 3776 | &top, &local_parent), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3777 | } else { |
| 3778 | local_parent = parent; |
| 3779 | } |
| 3780 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3781 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 3782 | if (lysc_is_key(orig->schema)) { |
| 3783 | if (local_parent) { |
| 3784 | /* the key must already exist in the parent */ |
| 3785 | rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3786 | LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 3787 | } else { |
| 3788 | assert(!(options & LYD_DUP_WITH_PARENTS)); |
| 3789 | /* duplicating a single key, okay, I suppose... */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3790 | rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 3791 | LY_CHECK_GOTO(rc, error); |
| 3792 | } |
| 3793 | } else { |
| 3794 | /* if there is no local parent, it will be inserted into first */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3795 | rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options, |
| 3796 | first ? NULL : &first); |
Michal Vasko | 35f4d77 | 2021-01-12 12:08:57 +0100 | [diff] [blame] | 3797 | LY_CHECK_GOTO(rc, error); |
| 3798 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3799 | if (nosiblings) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3800 | break; |
| 3801 | } |
| 3802 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3803 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3804 | if (dup) { |
| 3805 | *dup = first; |
| 3806 | } |
| 3807 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3808 | |
| 3809 | error: |
| 3810 | if (top) { |
| 3811 | lyd_free_tree(top); |
| 3812 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3813 | lyd_free_siblings(first); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3814 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3815 | return rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 3816 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3817 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3818 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3819 | lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3820 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3821 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 3822 | if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) { |
| 3823 | LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication."); |
| 3824 | return LY_EINVAL; |
| 3825 | } |
| 3826 | |
| 3827 | return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup); |
| 3828 | } |
| 3829 | |
| 3830 | LIBYANG_API_DEF LY_ERR |
| 3831 | lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, |
| 3832 | uint32_t options, struct lyd_node **dup) |
| 3833 | { |
| 3834 | LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL); |
| 3835 | |
| 3836 | return lyd_dup(node, trg_ctx, parent, options, 1, dup); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3837 | } |
| 3838 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3839 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3840 | lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3841 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 3842 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
| 3843 | if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) { |
| 3844 | LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication."); |
| 3845 | return LY_EINVAL; |
| 3846 | } |
| 3847 | |
| 3848 | return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup); |
| 3849 | } |
| 3850 | |
| 3851 | LIBYANG_API_DEF LY_ERR |
| 3852 | lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, |
| 3853 | uint32_t options, struct lyd_node **dup) |
| 3854 | { |
| 3855 | LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL); |
| 3856 | |
| 3857 | return lyd_dup(node, trg_ctx, parent, options, 0, dup); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 3860 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3861 | lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup) |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3862 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3863 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3864 | struct lyd_meta *mt, *last; |
| 3865 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3866 | LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3867 | |
| 3868 | /* create a copy */ |
| 3869 | mt = calloc(1, sizeof *mt); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3870 | LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3871 | mt->annotation = meta->annotation; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3872 | ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3873 | LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish); |
| 3874 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3875 | |
| 3876 | /* insert as the last attribute */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3877 | mt->parent = node; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3878 | if (node->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3879 | for (last = node->meta; last->next; last = last->next) {} |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3880 | last->next = mt; |
| 3881 | } else { |
| 3882 | node->meta = mt; |
| 3883 | } |
| 3884 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 3885 | finish: |
| 3886 | if (ret) { |
| 3887 | lyd_free_meta_single(mt); |
| 3888 | } else if (dup) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3889 | *dup = mt; |
| 3890 | } |
| 3891 | return LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3892 | } |
| 3893 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3894 | /** |
| 3895 | * @brief Merge a source sibling into target siblings. |
| 3896 | * |
| 3897 | * @param[in,out] first_trg First target sibling, is updated if top-level. |
| 3898 | * @param[in] parent_trg Target parent. |
| 3899 | * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3900 | * @param[in] merge_cb Optional merge callback. |
| 3901 | * @param[in] cb_data Arbitrary callback data. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3902 | * @param[in] options Merge options. |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3903 | * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3904 | * @return LY_ERR value. |
| 3905 | */ |
| 3906 | static LY_ERR |
| 3907 | lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p, |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3908 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3909 | { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3910 | const struct lyd_node *child_src, *tmp, *sibling_src; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 3911 | struct lyd_node *match_trg, *dup_src, *elem; |
Michal Vasko | d1afa50 | 2022-01-27 16:18:12 +0100 | [diff] [blame] | 3912 | struct lyd_node_opaq *opaq_trg, *opaq_src; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3913 | struct lysc_type *type; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3914 | struct lyd_dup_inst *child_dup_inst = NULL; |
| 3915 | LY_ERR ret; |
| 3916 | ly_bool first_inst = 0; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3917 | |
| 3918 | sibling_src = *sibling_src_p; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3919 | if (!sibling_src->schema) { |
| 3920 | /* try to find the same opaque node */ |
| 3921 | lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg); |
| 3922 | } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3923 | /* try to find the exact instance */ |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3924 | lyd_find_sibling_first(*first_trg, sibling_src, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3925 | } else { |
| 3926 | /* try to simply find the node, there cannot be more instances */ |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3927 | lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3928 | } |
| 3929 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3930 | if (match_trg) { |
| 3931 | /* update match as needed */ |
| 3932 | LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst)); |
| 3933 | } else { |
| 3934 | /* first instance of this node */ |
| 3935 | first_inst = 1; |
| 3936 | } |
| 3937 | |
| 3938 | if (match_trg) { |
| 3939 | /* call callback */ |
| 3940 | if (merge_cb) { |
| 3941 | LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data)); |
| 3942 | } |
| 3943 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3944 | /* node found, make sure even value matches for all node types */ |
Michal Vasko | d1afa50 | 2022-01-27 16:18:12 +0100 | [diff] [blame] | 3945 | if (!match_trg->schema) { |
| 3946 | if (lyd_compare_single(sibling_src, match_trg, 0)) { |
| 3947 | /* update value */ |
| 3948 | opaq_trg = (struct lyd_node_opaq *)match_trg; |
| 3949 | opaq_src = (struct lyd_node_opaq *)sibling_src; |
| 3950 | |
| 3951 | lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value); |
| 3952 | lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value); |
| 3953 | opaq_trg->hints = opaq_src->hints; |
| 3954 | |
| 3955 | ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data); |
| 3956 | opaq_trg->format = opaq_src->format; |
| 3957 | ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data, |
| 3958 | &opaq_trg->val_prefix_data); |
| 3959 | } |
| 3960 | } else if ((match_trg->schema->nodetype == LYS_LEAF) && |
| 3961 | lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3962 | /* since they are different, they cannot both be default */ |
| 3963 | assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT)); |
| 3964 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 3965 | /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */ |
| 3966 | if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3967 | type = ((struct lysc_node_leaf *)match_trg->schema)->type; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3968 | type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value); |
| 3969 | LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3970 | &((struct lyd_node_term *)match_trg)->value)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3971 | |
| 3972 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 3973 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3974 | } |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3975 | } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) { |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 3976 | /* update value */ |
| 3977 | LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3978 | ((struct lyd_node_any *)sibling_src)->value_type)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3979 | |
| 3980 | /* copy flags and add LYD_NEW */ |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 3981 | match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | /* check descendants, recursively */ |
| 3985 | ret = LY_SUCCESS; |
| 3986 | LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) { |
| 3987 | ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options, |
| 3988 | &child_dup_inst); |
| 3989 | if (ret) { |
| 3990 | break; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3991 | } |
| 3992 | } |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 3993 | lyd_dup_inst_free(child_dup_inst); |
| 3994 | LY_CHECK_RET(ret); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 3995 | } else { |
| 3996 | /* node not found, merge it */ |
| 3997 | if (options & LYD_MERGE_DESTRUCT) { |
| 3998 | dup_src = (struct lyd_node *)sibling_src; |
| 3999 | lyd_unlink_tree(dup_src); |
| 4000 | /* spend it */ |
| 4001 | *sibling_src_p = NULL; |
| 4002 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 4003 | LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4004 | } |
| 4005 | |
Michal Vasko | 7e8315b | 2021-08-03 17:01:06 +0200 | [diff] [blame] | 4006 | if (!(options & LYD_MERGE_WITH_FLAGS)) { |
| 4007 | /* set LYD_NEW for all the new nodes, required for validation */ |
| 4008 | LYD_TREE_DFS_BEGIN(dup_src, elem) { |
| 4009 | elem->flags |= LYD_NEW; |
| 4010 | LYD_TREE_DFS_END(dup_src, elem); |
| 4011 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4012 | } |
| 4013 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4014 | /* insert */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 4015 | lyd_insert_node(parent_trg, first_trg, dup_src, 0); |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4016 | |
| 4017 | if (first_inst) { |
| 4018 | /* remember not to find this instance next time */ |
| 4019 | LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst)); |
| 4020 | } |
| 4021 | |
| 4022 | /* call callback, no source node */ |
| 4023 | if (merge_cb) { |
| 4024 | LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data)); |
| 4025 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | return LY_SUCCESS; |
| 4029 | } |
| 4030 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 4031 | static LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4032 | lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 4033 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4034 | { |
| 4035 | const struct lyd_node *sibling_src, *tmp; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4036 | struct lyd_dup_inst *dup_inst = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4037 | ly_bool first; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4038 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4039 | |
| 4040 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4041 | LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL, |
| 4042 | LY_EINVAL); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4043 | |
| 4044 | if (!source) { |
| 4045 | /* nothing to merge */ |
| 4046 | return LY_SUCCESS; |
| 4047 | } |
| 4048 | |
Michal Vasko | 831422b | 2020-11-24 11:20:51 +0100 | [diff] [blame] | 4049 | if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 4050 | LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4051 | return LY_EINVAL; |
| 4052 | } |
| 4053 | |
| 4054 | LY_LIST_FOR_SAFE(source, tmp, sibling_src) { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4055 | if (mod && (lyd_owner_module(sibling_src) != mod)) { |
| 4056 | /* skip data nodes from different modules */ |
| 4057 | continue; |
| 4058 | } |
| 4059 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4060 | first = (sibling_src == source) ? 1 : 0; |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4061 | ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst); |
| 4062 | if (ret) { |
| 4063 | break; |
| 4064 | } |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4065 | if (first && !sibling_src) { |
| 4066 | /* source was spent (unlinked), move to the next node */ |
| 4067 | source = tmp; |
| 4068 | } |
| 4069 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 4070 | if (nosiblings) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4071 | break; |
| 4072 | } |
| 4073 | } |
| 4074 | |
| 4075 | if (options & LYD_MERGE_DESTRUCT) { |
| 4076 | /* free any leftover source data that were not merged */ |
| 4077 | lyd_free_siblings((struct lyd_node *)source); |
| 4078 | } |
| 4079 | |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4080 | lyd_dup_inst_free(dup_inst); |
| 4081 | return ret; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 4082 | } |
| 4083 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4084 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4085 | lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 4086 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4087 | return lyd_merge(target, source, NULL, NULL, NULL, options, 1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 4088 | } |
| 4089 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4090 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4091 | lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 4092 | { |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4093 | return lyd_merge(target, source, NULL, NULL, NULL, options, 0); |
| 4094 | } |
| 4095 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4096 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | cd3f617 | 2021-05-18 16:14:50 +0200 | [diff] [blame] | 4097 | lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod, |
| 4098 | lyd_merge_cb merge_cb, void *cb_data, uint16_t options) |
| 4099 | { |
| 4100 | return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 4101 | } |
| 4102 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4103 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4104 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4105 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4106 | /* ending \0 */ |
| 4107 | ++reqlen; |
| 4108 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4109 | if (reqlen > *buflen) { |
| 4110 | if (is_static) { |
| 4111 | return LY_EINCOMPLETE; |
| 4112 | } |
| 4113 | |
| 4114 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 4115 | if (!*buffer) { |
| 4116 | return LY_EMEM; |
| 4117 | } |
| 4118 | |
| 4119 | *buflen = reqlen; |
| 4120 | } |
| 4121 | |
| 4122 | return LY_SUCCESS; |
| 4123 | } |
| 4124 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 4125 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4126 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4127 | { |
| 4128 | const struct lyd_node *key; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4129 | size_t len; |
| 4130 | const char *val; |
| 4131 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4132 | |
Michal Vasko | 824d083 | 2021-11-04 15:36:51 +0100 | [diff] [blame] | 4133 | for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4134 | val = lyd_get_value(key); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4135 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4136 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4137 | |
| 4138 | quot = '\''; |
| 4139 | if (strchr(val, '\'')) { |
| 4140 | quot = '"'; |
| 4141 | } |
| 4142 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | return LY_SUCCESS; |
| 4146 | } |
| 4147 | |
| 4148 | /** |
| 4149 | * @brief Append leaf-list value predicate to path. |
| 4150 | * |
| 4151 | * @param[in] node Node to print. |
| 4152 | * @param[in,out] buffer Buffer to print to. |
| 4153 | * @param[in,out] buflen Current buffer length. |
| 4154 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 4155 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 4156 | * @return LY_ERR |
| 4157 | */ |
| 4158 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4159 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4160 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4161 | size_t len; |
| 4162 | const char *val; |
| 4163 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4164 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4165 | val = lyd_get_value(node); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 4166 | len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4167 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4168 | |
| 4169 | quot = '\''; |
| 4170 | if (strchr(val, '\'')) { |
| 4171 | quot = '"'; |
| 4172 | } |
| 4173 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 4174 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4175 | return LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | /** |
| 4179 | * @brief Append node position (relative to its other instances) predicate to path. |
| 4180 | * |
| 4181 | * @param[in] node Node to print. |
| 4182 | * @param[in,out] buffer Buffer to print to. |
| 4183 | * @param[in,out] buflen Current buffer length. |
| 4184 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 4185 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 4186 | * @return LY_ERR |
| 4187 | */ |
| 4188 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4189 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4190 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4191 | size_t len; |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 4192 | uint32_t pos; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4193 | char *val = NULL; |
| 4194 | LY_ERR rc; |
| 4195 | |
Michal Vasko | 50cc056 | 2021-05-18 16:15:43 +0200 | [diff] [blame] | 4196 | pos = lyd_list_pos(node); |
| 4197 | if (asprintf(&val, "%" PRIu32, pos) == -1) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4198 | return LY_EMEM; |
| 4199 | } |
| 4200 | |
| 4201 | len = 1 + strlen(val) + 1; |
| 4202 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 4203 | if (rc != LY_SUCCESS) { |
| 4204 | goto cleanup; |
| 4205 | } |
| 4206 | |
| 4207 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 4208 | |
| 4209 | cleanup: |
| 4210 | free(val); |
| 4211 | return rc; |
| 4212 | } |
| 4213 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4214 | LIBYANG_API_DEF char * |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4215 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 4216 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4217 | ly_bool is_static = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4218 | uint32_t i, depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4219 | size_t bufused = 0, len; |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 4220 | const struct lyd_node *iter, *parent; |
| 4221 | const struct lys_module *mod, *prev_mod; |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 4222 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4223 | |
| 4224 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 4225 | if (buffer) { |
Michal Vasko | 16385f4 | 2021-05-18 16:16:09 +0200 | [diff] [blame] | 4226 | LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4227 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4228 | } else { |
| 4229 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | switch (pathtype) { |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 4233 | case LYD_PATH_STD: |
| 4234 | case LYD_PATH_STD_NO_LAST_PRED: |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4235 | depth = 1; |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4236 | for (iter = node; iter->parent; iter = lyd_parent(iter)) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4237 | ++depth; |
| 4238 | } |
| 4239 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4240 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4241 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4242 | /* find the right node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4243 | for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {} |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4244 | iter_print: |
Michal Vasko | 12eb622 | 2022-03-18 13:35:49 +0100 | [diff] [blame] | 4245 | /* get the module */ |
| 4246 | mod = iter->schema ? iter->schema->module : lyd_owner_module(iter); |
| 4247 | parent = lyd_parent(iter); |
| 4248 | prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent); |
| 4249 | if (prev_mod == mod) { |
| 4250 | mod = NULL; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | /* realloc string */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 4254 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : |
| 4255 | strlen(((struct lyd_node_opaq *)iter)->name.name)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4256 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 4257 | if (rc != LY_SUCCESS) { |
| 4258 | break; |
| 4259 | } |
| 4260 | |
| 4261 | /* print next node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 4262 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 4263 | iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4264 | |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 4265 | /* do not always print the last (first) predicate */ |
Radek Krejci | 635d2b8 | 2021-01-04 11:26:51 +0100 | [diff] [blame] | 4266 | if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) { |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 4267 | switch (iter->schema->nodetype) { |
| 4268 | case LYS_LIST: |
| 4269 | if (iter->schema->flags & LYS_KEYLESS) { |
| 4270 | /* print its position */ |
| 4271 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4272 | } else { |
| 4273 | /* print all list keys in predicates */ |
| 4274 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4275 | } |
| 4276 | break; |
| 4277 | case LYS_LEAFLIST: |
| 4278 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 4279 | /* print leaf-list value */ |
| 4280 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4281 | } else { |
| 4282 | /* print its position */ |
| 4283 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 4284 | } |
| 4285 | break; |
| 4286 | default: |
| 4287 | /* nothing to print more */ |
| 4288 | break; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4289 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4290 | } |
| 4291 | if (rc != LY_SUCCESS) { |
| 4292 | break; |
| 4293 | } |
| 4294 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 4295 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 4296 | } |
| 4297 | break; |
| 4298 | } |
| 4299 | |
| 4300 | return buffer; |
| 4301 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4302 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4303 | LIBYANG_API_DEF struct lyd_meta * |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 4304 | lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name) |
| 4305 | { |
| 4306 | struct lyd_meta *ret = NULL; |
| 4307 | const struct ly_ctx *ctx; |
| 4308 | const char *prefix, *tmp; |
| 4309 | char *str; |
| 4310 | size_t pref_len, name_len; |
| 4311 | |
| 4312 | LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4313 | LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 4314 | |
| 4315 | if (!first) { |
| 4316 | return NULL; |
| 4317 | } |
| 4318 | |
| 4319 | ctx = first->annotation->module->ctx; |
| 4320 | |
| 4321 | /* parse the name */ |
| 4322 | tmp = name; |
| 4323 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 4324 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 4325 | return NULL; |
| 4326 | } |
| 4327 | |
| 4328 | /* find the module */ |
| 4329 | if (prefix) { |
| 4330 | str = strndup(prefix, pref_len); |
| 4331 | module = ly_ctx_get_module_latest(ctx, str); |
| 4332 | free(str); |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 4333 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), NULL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | /* find the metadata */ |
| 4337 | LY_LIST_FOR(first, first) { |
| 4338 | if ((first->annotation->module == module) && !strcmp(first->name, name)) { |
| 4339 | ret = (struct lyd_meta *)first; |
| 4340 | break; |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | return ret; |
| 4345 | } |
| 4346 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4347 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4348 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 4349 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4350 | struct lyd_node **match_p, *iter; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4351 | struct lyd_node_inner *parent; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4352 | ly_bool found; |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4353 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 4354 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4355 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4356 | |
Michal Vasko | 6da1e95 | 2020-12-09 18:14:38 +0100 | [diff] [blame] | 4357 | if (!siblings || (siblings->schema && target->schema && |
| 4358 | (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 4359 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4360 | if (match) { |
| 4361 | *match = NULL; |
| 4362 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4363 | return LY_ENOTFOUND; |
| 4364 | } |
| 4365 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4366 | /* get first sibling */ |
| 4367 | siblings = lyd_first_sibling(siblings); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4368 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4369 | parent = siblings->parent; |
Michal Vasko | 6da1e95 | 2020-12-09 18:14:38 +0100 | [diff] [blame] | 4370 | if (parent && parent->schema && parent->children_ht) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4371 | assert(target->hash); |
| 4372 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4373 | if (lysc_is_dup_inst_list(target->schema)) { |
| 4374 | /* we must search the instances from beginning to find the first matching one */ |
| 4375 | found = 0; |
| 4376 | LYD_LIST_FOR_INST(siblings, target->schema, iter) { |
| 4377 | if (!lyd_compare_single(target, iter, 0)) { |
| 4378 | found = 1; |
| 4379 | break; |
| 4380 | } |
| 4381 | } |
| 4382 | if (found) { |
| 4383 | siblings = iter; |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 4384 | } else { |
| 4385 | siblings = NULL; |
| 4386 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4387 | } else { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4388 | /* find by hash */ |
| 4389 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 4390 | siblings = *match_p; |
| 4391 | } else { |
| 4392 | /* not found */ |
| 4393 | siblings = NULL; |
| 4394 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4395 | } |
| 4396 | } else { |
| 4397 | /* no children hash table */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4398 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 4399 | if (!lyd_compare_single(siblings, target, 0)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4400 | break; |
| 4401 | } |
| 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4406 | if (match) { |
| 4407 | *match = NULL; |
| 4408 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4409 | return LY_ENOTFOUND; |
| 4410 | } |
| 4411 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4412 | if (match) { |
| 4413 | *match = (struct lyd_node *)siblings; |
| 4414 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4415 | return LY_SUCCESS; |
| 4416 | } |
| 4417 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4418 | /** |
| 4419 | * @brief Comparison callback to match schema node with a schema of a data node. |
| 4420 | * |
| 4421 | * @param[in] val1_p Pointer to the schema node |
| 4422 | * @param[in] val2_p Pointer to the data node |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 4423 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4424 | */ |
| 4425 | static ly_bool |
| 4426 | lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4427 | { |
| 4428 | struct lysc_node *val1; |
| 4429 | struct lyd_node *val2; |
| 4430 | |
| 4431 | val1 = *((struct lysc_node **)val1_p); |
| 4432 | val2 = *((struct lyd_node **)val2_p); |
| 4433 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4434 | if (val1 == val2->schema) { |
| 4435 | /* schema match is enough */ |
| 4436 | return 1; |
| 4437 | } else { |
| 4438 | return 0; |
| 4439 | } |
| 4440 | } |
| 4441 | |
Michal Vasko | 92239c7 | 2020-11-18 18:17:25 +0100 | [diff] [blame] | 4442 | /** |
| 4443 | * @brief Search in the given siblings (NOT recursively) for the first schema node data instance. |
| 4444 | * Uses hashes - should be used whenever possible for best performance. |
| 4445 | * |
| 4446 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 4447 | * @param[in] schema Target data node schema to find. |
| 4448 | * @param[out] match Can be NULL, otherwise the found data node. |
| 4449 | * @return LY_SUCCESS on success, @p match set. |
| 4450 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 4451 | * @return LY_ERR value if another error occurred. |
| 4452 | */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4453 | static LY_ERR |
| 4454 | lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match) |
| 4455 | { |
| 4456 | struct lyd_node **match_p; |
| 4457 | struct lyd_node_inner *parent; |
| 4458 | uint32_t hash; |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 4459 | lyht_value_equal_cb ht_cb; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4460 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4461 | assert(siblings && schema); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4462 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4463 | parent = siblings->parent; |
Michal Vasko | 08fd613 | 2020-11-18 18:17:45 +0100 | [diff] [blame] | 4464 | if (parent && parent->schema && parent->children_ht) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4465 | /* calculate our hash */ |
| 4466 | hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name)); |
| 4467 | hash = dict_hash_multi(hash, schema->name, strlen(schema->name)); |
| 4468 | hash = dict_hash_multi(hash, NULL, 0); |
| 4469 | |
| 4470 | /* use special hash table function */ |
| 4471 | ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal); |
| 4472 | |
| 4473 | /* find by hash */ |
| 4474 | if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) { |
| 4475 | siblings = *match_p; |
| 4476 | } else { |
| 4477 | /* not found */ |
| 4478 | siblings = NULL; |
| 4479 | } |
| 4480 | |
| 4481 | /* set the original hash table compare function back */ |
| 4482 | lyht_set_cb(parent->children_ht, ht_cb); |
| 4483 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4484 | /* find first sibling */ |
| 4485 | if (siblings->parent) { |
| 4486 | siblings = siblings->parent->child; |
| 4487 | } else { |
| 4488 | while (siblings->prev->next) { |
| 4489 | siblings = siblings->prev; |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | /* search manually without hashes */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4494 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4495 | if (siblings->schema == schema) { |
| 4496 | /* schema match is enough */ |
| 4497 | break; |
| 4498 | } |
| 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4503 | if (match) { |
| 4504 | *match = NULL; |
| 4505 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4506 | return LY_ENOTFOUND; |
| 4507 | } |
| 4508 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4509 | if (match) { |
| 4510 | *match = (struct lyd_node *)siblings; |
| 4511 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4512 | return LY_SUCCESS; |
| 4513 | } |
| 4514 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4515 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4516 | lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4517 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4518 | { |
| 4519 | LY_ERR rc; |
| 4520 | struct lyd_node *target = NULL; |
| 4521 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 4522 | LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4523 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, schema->module->ctx, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4524 | |
Michal Vasko | 08fd613 | 2020-11-18 18:17:45 +0100 | [diff] [blame] | 4525 | if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) { |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 4526 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 4527 | if (match) { |
| 4528 | *match = NULL; |
| 4529 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4530 | return LY_ENOTFOUND; |
| 4531 | } |
| 4532 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 4533 | if (key_or_value && !val_len) { |
| 4534 | val_len = strlen(key_or_value); |
| 4535 | } |
| 4536 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4537 | if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) { |
| 4538 | /* create a data node and find the instance */ |
| 4539 | if (schema->nodetype == LYS_LEAFLIST) { |
| 4540 | /* target used attributes: schema, hash, value */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 4541 | rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 4542 | LY_CHECK_RET(rc); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4543 | } else { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4544 | /* target used attributes: schema, hash, child (all keys) */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4545 | LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /* find it */ |
| 4549 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 4550 | } else { |
| 4551 | /* find the first schema node instance */ |
| 4552 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4553 | } |
| 4554 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 4555 | lyd_free_tree(target); |
| 4556 | return rc; |
| 4557 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4558 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4559 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4560 | lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set) |
| 4561 | { |
| 4562 | struct lyd_node **match_p, *first, *iter; |
| 4563 | struct lyd_node_inner *parent; |
| 4564 | |
| 4565 | LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL); |
Michal Vasko | 892f5bf | 2021-11-24 10:41:05 +0100 | [diff] [blame] | 4566 | LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 4567 | |
| 4568 | LY_CHECK_RET(ly_set_new(set)); |
| 4569 | |
| 4570 | if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) { |
| 4571 | /* no data or schema mismatch */ |
| 4572 | return LY_ENOTFOUND; |
| 4573 | } |
| 4574 | |
| 4575 | /* get first sibling */ |
| 4576 | siblings = lyd_first_sibling(siblings); |
| 4577 | |
| 4578 | parent = siblings->parent; |
| 4579 | if (parent && parent->schema && parent->children_ht) { |
| 4580 | assert(target->hash); |
| 4581 | |
| 4582 | /* find the first instance */ |
| 4583 | lyd_find_sibling_first(siblings, target, &first); |
| 4584 | if (first) { |
| 4585 | /* add it so that it is the first in the set */ |
| 4586 | if (ly_set_add(*set, first, 1, NULL)) { |
| 4587 | goto error; |
| 4588 | } |
| 4589 | |
| 4590 | /* find by hash */ |
| 4591 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
| 4592 | iter = *match_p; |
| 4593 | } else { |
| 4594 | /* not found */ |
| 4595 | iter = NULL; |
| 4596 | } |
| 4597 | while (iter) { |
| 4598 | /* add all found nodes into the set */ |
| 4599 | if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) { |
| 4600 | goto error; |
| 4601 | } |
| 4602 | |
| 4603 | /* find next instance */ |
| 4604 | if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) { |
| 4605 | iter = NULL; |
| 4606 | } else { |
| 4607 | iter = *match_p; |
| 4608 | } |
| 4609 | } |
| 4610 | } |
| 4611 | } else { |
| 4612 | /* no children hash table */ |
| 4613 | LY_LIST_FOR(siblings, siblings) { |
| 4614 | if (!lyd_compare_single(target, siblings, 0)) { |
| 4615 | ly_set_add(*set, (void *)siblings, 1, NULL); |
| 4616 | } |
| 4617 | } |
| 4618 | } |
| 4619 | |
| 4620 | if (!(*set)->count) { |
| 4621 | return LY_ENOTFOUND; |
| 4622 | } |
| 4623 | return LY_SUCCESS; |
| 4624 | |
| 4625 | error: |
| 4626 | ly_set_free(*set, NULL); |
| 4627 | *set = NULL; |
| 4628 | return LY_EMEM; |
| 4629 | } |
| 4630 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4631 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 1d4af6c | 2021-02-22 13:31:26 +0100 | [diff] [blame] | 4632 | lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match) |
| 4633 | { |
| 4634 | LY_CHECK_ARG_RET(NULL, name, LY_EINVAL); |
| 4635 | |
| 4636 | for ( ; first; first = first->next) { |
| 4637 | if (!first->schema && !strcmp(LYD_NAME(first), name)) { |
| 4638 | break; |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | if (match) { |
| 4643 | *match = (struct lyd_node *)first; |
| 4644 | } |
| 4645 | return first ? LY_SUCCESS : LY_ENOTFOUND; |
| 4646 | } |
| 4647 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4648 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4649 | lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format, |
| 4650 | void *prefix_data, const struct lyxp_var *vars, struct ly_set **set) |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4651 | { |
| 4652 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 8bd9cc7 | 2021-07-16 14:06:10 +0200 | [diff] [blame] | 4653 | struct lyxp_set xp_set = {0}; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 4654 | struct lyxp_expr *exp = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4655 | uint32_t i; |
| 4656 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4657 | LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4658 | |
Michal Vasko | 37c6943 | 2021-04-12 14:48:24 +0200 | [diff] [blame] | 4659 | *set = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4660 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4661 | /* parse expression */ |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4662 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 4663 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4664 | |
| 4665 | /* evaluate expression */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4666 | ret = lyxp_eval(LYD_CTX(tree), exp, NULL, format, prefix_data, ctx_node, tree, vars, &xp_set, LYXP_IGNORE_WHEN); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4667 | LY_CHECK_GOTO(ret, cleanup); |
| 4668 | |
| 4669 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4670 | ret = ly_set_new(set); |
| 4671 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4672 | |
| 4673 | /* transform into ly_set */ |
| 4674 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 4675 | /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */ |
| 4676 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4677 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4678 | (*set)->size = xp_set.used; |
| 4679 | |
| 4680 | for (i = 0; i < xp_set.used; ++i) { |
| 4681 | if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 4682 | ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 4683 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4684 | } |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | cleanup: |
Michal Vasko | 0691d52 | 2020-05-21 13:21:47 +0200 | [diff] [blame] | 4689 | lyxp_set_free_content(&xp_set); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4690 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp); |
Radek Krejci | 8f45abc | 2020-11-26 12:15:13 +0100 | [diff] [blame] | 4691 | if (ret) { |
| 4692 | ly_set_free(*set, NULL); |
| 4693 | *set = NULL; |
| 4694 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 4695 | return ret; |
| 4696 | } |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 4697 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4698 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4699 | lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, |
| 4700 | const struct lyxp_var *vars, struct ly_set **set) |
| 4701 | { |
| 4702 | LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL); |
| 4703 | |
| 4704 | return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set); |
| 4705 | } |
| 4706 | |
| 4707 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4708 | lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set) |
| 4709 | { |
| 4710 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 4711 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4712 | return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set); |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4713 | } |
| 4714 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4715 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4716 | lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set) |
| 4717 | { |
Michal Vasko | e3716b3 | 2021-12-13 16:58:25 +0100 | [diff] [blame] | 4718 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 4719 | |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 4720 | return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4721 | } |
| 4722 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4723 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4724 | lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result) |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 4725 | { |
| 4726 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 8bd9cc7 | 2021-07-16 14:06:10 +0200 | [diff] [blame] | 4727 | struct lyxp_set xp_set = {0}; |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 4728 | struct lyxp_expr *exp = NULL; |
| 4729 | |
| 4730 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL); |
| 4731 | |
| 4732 | /* compile expression */ |
| 4733 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp); |
| 4734 | LY_CHECK_GOTO(ret, cleanup); |
| 4735 | |
| 4736 | /* evaluate expression */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4737 | ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, vars, &xp_set, LYXP_IGNORE_WHEN); |
Michal Vasko | c9eb3ca | 2021-07-16 10:20:37 +0200 | [diff] [blame] | 4738 | LY_CHECK_GOTO(ret, cleanup); |
| 4739 | |
| 4740 | /* transform into boolean */ |
| 4741 | ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN); |
| 4742 | LY_CHECK_GOTO(ret, cleanup); |
| 4743 | |
| 4744 | /* set result */ |
| 4745 | *result = xp_set.val.bln; |
| 4746 | |
| 4747 | cleanup: |
| 4748 | lyxp_set_free_content(&xp_set); |
| 4749 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp); |
| 4750 | return ret; |
| 4751 | } |
| 4752 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4753 | LIBYANG_API_DEF LY_ERR |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 4754 | lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result) |
| 4755 | { |
| 4756 | return lyd_eval_xpath2(ctx_node, xpath, NULL, result); |
| 4757 | } |
| 4758 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4759 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 4760 | lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match) |
| 4761 | { |
| 4762 | LY_ERR ret = LY_SUCCESS; |
| 4763 | struct lyxp_expr *expr = NULL; |
| 4764 | struct ly_path *lypath = NULL; |
| 4765 | |
| 4766 | LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL); |
| 4767 | |
| 4768 | /* parse the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 4769 | ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 4770 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr); |
| 4771 | LY_CHECK_GOTO(ret, cleanup); |
| 4772 | |
| 4773 | /* compile the path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 4774 | ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr, |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 4775 | output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath); |
Michal Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 4776 | LY_CHECK_GOTO(ret, cleanup); |
| 4777 | |
| 4778 | /* evaluate the path */ |
| 4779 | ret = ly_path_eval_partial(lypath, ctx_node, NULL, match); |
| 4780 | |
| 4781 | cleanup: |
| 4782 | lyxp_expr_free(LYD_CTX(ctx_node), expr); |
| 4783 | ly_path_free(LYD_CTX(ctx_node), lypath); |
| 4784 | return ret; |
| 4785 | } |
| 4786 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4787 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | bb22b18 | 2021-06-14 08:14:21 +0200 | [diff] [blame] | 4788 | lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match) |
| 4789 | { |
| 4790 | LY_ERR ret; |
| 4791 | struct lyd_node *m; |
| 4792 | |
| 4793 | LY_CHECK_ARG_RET(NULL, path, LY_EINVAL); |
| 4794 | |
| 4795 | ret = ly_path_eval(path, tree, &m); |
| 4796 | if (ret) { |
| 4797 | if (match) { |
| 4798 | *match = NULL; |
| 4799 | } |
| 4800 | return LY_ENOTFOUND; |
| 4801 | } |
| 4802 | |
| 4803 | if (match) { |
| 4804 | *match = m; |
| 4805 | } |
| 4806 | return LY_SUCCESS; |
| 4807 | } |
| 4808 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4809 | LIBYANG_API_DEF uint32_t |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 4810 | lyd_list_pos(const struct lyd_node *instance) |
| 4811 | { |
| 4812 | const struct lyd_node *iter = NULL; |
| 4813 | uint32_t pos = 0; |
| 4814 | |
| 4815 | if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 4816 | return 0; |
| 4817 | } |
| 4818 | |
| 4819 | /* data instances are ordered, so we can stop when we found instance of other schema node */ |
| 4820 | for (iter = instance; iter->schema == instance->schema; iter = iter->prev) { |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 4821 | if (pos && (iter->next == NULL)) { |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 4822 | /* overrun to the end of the siblings list */ |
| 4823 | break; |
| 4824 | } |
| 4825 | ++pos; |
| 4826 | } |
| 4827 | |
| 4828 | return pos; |
| 4829 | } |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 4830 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 4831 | LIBYANG_API_DEF struct lyd_node * |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 4832 | lyd_first_sibling(const struct lyd_node *node) |
| 4833 | { |
| 4834 | struct lyd_node *start; |
| 4835 | |
| 4836 | if (!node) { |
| 4837 | return NULL; |
| 4838 | } |
| 4839 | |
| 4840 | /* get the first sibling */ |
| 4841 | if (node->parent) { |
| 4842 | start = node->parent->child; |
| 4843 | } else { |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 4844 | for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {} |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | return start; |
| 4848 | } |