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