Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file diff.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief diff functions |
| 5 | * |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 6 | * Copyright (c) 2020 - 2021 CESNET, z.s.p.o. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +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 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 15 | |
| 16 | #include "diff.h" |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <stddef.h> |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 22 | #include <stdlib.h> |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
| 25 | #include "common.h" |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 27 | #include "context.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 28 | #include "log.h" |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 29 | #include "plugins_exts.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 30 | #include "plugins_exts/metadata.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 31 | #include "plugins_types.h" |
| 32 | #include "set.h" |
| 33 | #include "tree.h" |
| 34 | #include "tree_data.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 35 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 36 | #include "tree_edit.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 37 | #include "tree_schema.h" |
| 38 | #include "tree_schema_internal.h" |
| 39 | |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 40 | #define LOGERR_META(ctx, meta_name, node) \ |
| 41 | { \ |
| 42 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 43 | LOGERR(ctx, LY_EINVAL, "Failed to find metadata \"%s\" for node \"%s\".", meta_name, __path); \ |
| 44 | free(__path); \ |
| 45 | } |
| 46 | |
| 47 | #define LOGERR_NOINST(ctx, node) \ |
| 48 | { \ |
| 49 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 50 | LOGERR(ctx, LY_EINVAL, "Failed to find node \"%s\" instance in data.", __path); \ |
| 51 | free(__path); \ |
| 52 | } |
| 53 | |
| 54 | #define LOGERR_UNEXPVAL(ctx, node, data_source) \ |
| 55 | { \ |
| 56 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 57 | LOGERR(ctx, LY_EINVAL, "Unexpected value of node \"%s\" in %s.", __path, data_source); \ |
| 58 | free(__path); \ |
| 59 | } |
| 60 | |
| 61 | #define LOGERR_MERGEOP(ctx, node, src_op, trg_op) \ |
| 62 | { \ |
| 63 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 64 | LOGERR(ctx, LY_EINVAL, "Unable to merge operation \"%s\" with \"%s\" for node \"%s\".", \ |
| 65 | lyd_diff_op2str(trg_op), lyd_diff_op2str(src_op), __path); \ |
| 66 | free(__path); \ |
| 67 | } |
| 68 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 69 | static const char * |
| 70 | lyd_diff_op2str(enum lyd_diff_op op) |
| 71 | { |
| 72 | switch (op) { |
| 73 | case LYD_DIFF_OP_CREATE: |
| 74 | return "create"; |
| 75 | case LYD_DIFF_OP_DELETE: |
| 76 | return "delete"; |
| 77 | case LYD_DIFF_OP_REPLACE: |
| 78 | return "replace"; |
| 79 | case LYD_DIFF_OP_NONE: |
| 80 | return "none"; |
| 81 | } |
| 82 | |
| 83 | LOGINT(NULL); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 87 | static enum lyd_diff_op |
| 88 | lyd_diff_str2op(const char *str) |
| 89 | { |
| 90 | switch (str[0]) { |
| 91 | case 'c': |
| 92 | assert(!strcmp(str, "create")); |
| 93 | return LYD_DIFF_OP_CREATE; |
| 94 | case 'd': |
| 95 | assert(!strcmp(str, "delete")); |
| 96 | return LYD_DIFF_OP_DELETE; |
| 97 | case 'r': |
| 98 | assert(!strcmp(str, "replace")); |
| 99 | return LYD_DIFF_OP_REPLACE; |
| 100 | case 'n': |
| 101 | assert(!strcmp(str, "none")); |
| 102 | return LYD_DIFF_OP_NONE; |
| 103 | } |
| 104 | |
| 105 | LOGINT(NULL); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 109 | /** |
| 110 | * @brief Create diff metadata for a nested user-ordered node with the effective operation "create". |
| 111 | * |
| 112 | * @param[in] node User-rodered node to update. |
| 113 | * @return LY_ERR value. |
| 114 | */ |
| 115 | static LY_ERR |
| 116 | lyd_diff_add_create_nested_userord(struct lyd_node *node) |
| 117 | { |
| 118 | LY_ERR rc = LY_SUCCESS; |
| 119 | const char *meta_name, *meta_val; |
| 120 | size_t buflen = 0, bufused = 0; |
| 121 | uint32_t pos; |
| 122 | char *dyn = NULL; |
| 123 | |
| 124 | assert(lysc_is_userordered(node->schema)); |
| 125 | |
| 126 | /* get correct metadata name and value */ |
| 127 | if (lysc_is_dup_inst_list(node->schema)) { |
| 128 | meta_name = "yang:position"; |
| 129 | |
| 130 | pos = lyd_list_pos(node); |
| 131 | if (asprintf(&dyn, "%" PRIu32, pos) == -1) { |
| 132 | LOGMEM(LYD_CTX(node)); |
| 133 | rc = LY_EMEM; |
| 134 | goto cleanup; |
| 135 | } |
| 136 | meta_val = dyn; |
| 137 | } else if (node->schema->nodetype == LYS_LIST) { |
| 138 | meta_name = "yang:key"; |
| 139 | |
| 140 | if (node->prev->next && (node->prev->schema == node->schema)) { |
| 141 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(node->prev, &dyn, &buflen, &bufused, 0), cleanup); |
| 142 | meta_val = dyn; |
| 143 | } else { |
| 144 | meta_val = ""; |
| 145 | } |
| 146 | } else { |
| 147 | meta_name = "yang:value"; |
| 148 | |
| 149 | if (node->prev->next && (node->prev->schema == node->schema)) { |
| 150 | meta_val = lyd_get_value(node->prev); |
| 151 | } else { |
| 152 | meta_val = ""; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* create the metadata */ |
| 157 | LY_CHECK_GOTO(rc = lyd_new_meta(NULL, node, NULL, meta_name, meta_val, 0, NULL), cleanup); |
| 158 | |
| 159 | cleanup: |
| 160 | free(dyn); |
| 161 | return rc; |
| 162 | } |
| 163 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 164 | LY_ERR |
| 165 | lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 166 | const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position, |
| 167 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 168 | { |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 169 | struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL, *elem; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 170 | const struct lyd_node *parent = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 171 | |
| 172 | assert(diff); |
| 173 | |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 174 | /* replace leaf always needs orig-default and orig-value */ |
| 175 | assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value)); |
| 176 | |
| 177 | /* create on userord needs key/value */ |
| 178 | assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 179 | (lysc_is_dup_inst_list(node->schema) && position) || key); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 180 | assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 181 | (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 182 | |
| 183 | /* move on userord needs both key and orig-key/value and orig-value */ |
| 184 | assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 185 | (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key)); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 186 | assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 187 | (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) || |
| 188 | (value && orig_value)); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 189 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 190 | /* find the first existing parent */ |
| 191 | siblings = *diff; |
| 192 | while (1) { |
| 193 | /* find next node parent */ |
| 194 | parent = node; |
| 195 | while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 196 | parent = lyd_parent(parent); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 197 | } |
| 198 | if (parent == node) { |
| 199 | /* no more parents to find */ |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | /* check whether it exists in the diff */ |
| 204 | if (lyd_find_sibling_first(siblings, parent, &match)) { |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | /* another parent found */ |
| 209 | diff_parent = match; |
| 210 | |
| 211 | /* move down in the diff */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 212 | siblings = lyd_child_no_keys(match); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* duplicate the subtree (and connect to the diff if possible) */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 216 | LY_CHECK_RET(lyd_dup_single(node, (struct lyd_node_inner *)diff_parent, |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 217 | LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 218 | |
| 219 | /* find the first duplicated parent */ |
| 220 | if (!diff_parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 221 | diff_parent = lyd_parent(dup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 222 | while (diff_parent && diff_parent->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 223 | diff_parent = lyd_parent(diff_parent); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 224 | } |
| 225 | } else { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 226 | diff_parent = dup; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 227 | while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 228 | diff_parent = lyd_parent(diff_parent); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /* no parent existed, must be manually connected */ |
| 233 | if (!diff_parent) { |
| 234 | /* there actually was no parent to duplicate */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 235 | lyd_insert_sibling(*diff, dup, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 236 | } else if (!diff_parent->parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 237 | lyd_insert_sibling(*diff, diff_parent, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 240 | /* add parent operation, if any */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 241 | if (diff_parent && (diff_parent != dup)) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 242 | LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* add subtree operation */ |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 246 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 247 | |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 248 | if (op == LYD_DIFF_OP_CREATE) { |
| 249 | /* all nested user-ordered (leaf-)lists need special metadata for create op */ |
| 250 | LYD_TREE_DFS_BEGIN(dup, elem) { |
| 251 | if ((elem != dup) && lysc_is_userordered(elem->schema)) { |
| 252 | LY_CHECK_RET(lyd_diff_add_create_nested_userord(elem)); |
| 253 | } |
| 254 | LYD_TREE_DFS_END(dup, elem); |
| 255 | } |
| 256 | } |
| 257 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 258 | /* orig-default */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 259 | if (orig_default) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 260 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* orig-value */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 264 | if (orig_value) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 265 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* key */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 269 | if (key) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 270 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* value */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 274 | if (value) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 275 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 278 | /* position */ |
| 279 | if (position) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 280 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 283 | /* orig-key */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 284 | if (orig_key) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 285 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 288 | /* orig-position */ |
| 289 | if (orig_position) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 290 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 293 | return LY_SUCCESS; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet. |
| 298 | * |
Michal Vasko | 1dcd73b | 2020-12-08 10:04:33 +0100 | [diff] [blame] | 299 | * @param[in] first Node from the first tree, can be NULL (on create). |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 300 | * @param[in] schema Schema node of the list/leaf-list. |
| 301 | * @param[in,out] userord Sized array of userord items. |
| 302 | * @return Userord item for all the user-ordered list/leaf-list instances. |
| 303 | */ |
| 304 | static struct lyd_diff_userord * |
| 305 | lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord) |
| 306 | { |
| 307 | struct lyd_diff_userord *item; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 308 | struct lyd_node *iter; |
| 309 | const struct lyd_node **node; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 310 | LY_ARRAY_COUNT_TYPE u; |
| 311 | |
| 312 | LY_ARRAY_FOR(*userord, u) { |
| 313 | if ((*userord)[u].schema == schema) { |
| 314 | return &(*userord)[u]; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* it was not added yet, add it now */ |
| 319 | LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL); |
| 320 | |
| 321 | item->schema = schema; |
| 322 | item->pos = 0; |
| 323 | item->inst = NULL; |
| 324 | |
| 325 | /* store all the instance pointers in the current order */ |
| 326 | if (first) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 327 | LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) { |
| 328 | LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL); |
| 329 | *node = iter; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | return item; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered |
| 338 | * lists/leaf-lists. |
| 339 | * |
| 340 | * @param[in] first Node from the first tree, can be NULL (on create). |
| 341 | * @param[in] second Node from the second tree, can be NULL (on delete). |
| 342 | * @param[in] options Diff options. |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 343 | * @param[in] userord_item Userord item of @p first and/or @p second node. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 344 | * @param[out] op Operation. |
| 345 | * @param[out] orig_default Original default metadata. |
| 346 | * @param[out] value Value metadata. |
| 347 | * @param[out] orig_value Original value metadata |
| 348 | * @param[out] key Key metadata. |
| 349 | * @param[out] orig_key Original key metadata. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 350 | * @param[out] position Position metadata. |
| 351 | * @param[out] orig_position Original position metadata. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 352 | * @return LY_SUCCESS on success, |
| 353 | * @return LY_ENOT if there is no change to be added into diff, |
| 354 | * @return LY_ERR value on other errors. |
| 355 | */ |
| 356 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 357 | lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 358 | struct lyd_diff_userord *userord_item, enum lyd_diff_op *op, const char **orig_default, char **value, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 359 | char **orig_value, char **key, char **orig_key, char **position, char **orig_position) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 360 | { |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 361 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 362 | const struct lysc_node *schema; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 363 | size_t buflen, bufused; |
| 364 | uint32_t first_pos, second_pos; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 365 | |
| 366 | assert(first || second); |
| 367 | |
| 368 | *orig_default = NULL; |
| 369 | *value = NULL; |
| 370 | *orig_value = NULL; |
| 371 | *key = NULL; |
| 372 | *orig_key = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 373 | *position = NULL; |
| 374 | *orig_position = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 375 | |
| 376 | schema = first ? first->schema : second->schema; |
| 377 | assert(lysc_is_userordered(schema)); |
| 378 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 379 | /* find user-ordered first position */ |
| 380 | if (first) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 381 | for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 382 | if (userord_item->inst[first_pos] == first) { |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | assert(first_pos < LY_ARRAY_COUNT(userord_item->inst)); |
| 387 | } else { |
| 388 | first_pos = 0; |
| 389 | } |
| 390 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 391 | /* prepare position of the next instance */ |
| 392 | second_pos = userord_item->pos++; |
| 393 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 394 | /* learn operation first */ |
| 395 | if (!second) { |
| 396 | *op = LYD_DIFF_OP_DELETE; |
| 397 | } else if (!first) { |
| 398 | *op = LYD_DIFF_OP_CREATE; |
| 399 | } else { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 400 | if (lyd_compare_single(second, userord_item->inst[second_pos], 0)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 401 | /* in first, there is a different instance on the second position, we are going to move 'first' node */ |
| 402 | *op = LYD_DIFF_OP_REPLACE; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 403 | } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 404 | /* default flag change */ |
| 405 | *op = LYD_DIFF_OP_NONE; |
| 406 | } else { |
| 407 | /* no changes */ |
| 408 | return LY_ENOT; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * set each attribute correctly based on the operation and node type |
| 414 | */ |
| 415 | |
| 416 | /* orig-default */ |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 417 | if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 418 | if (first->flags & LYD_DEFAULT) { |
| 419 | *orig_default = "true"; |
| 420 | } else { |
| 421 | *orig_default = "false"; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /* value */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 426 | if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) && |
| 427 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 428 | if (second_pos) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 429 | *value = strdup(lyd_get_value(userord_item->inst[second_pos - 1])); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 430 | LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 431 | } else { |
| 432 | *value = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 433 | LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | /* orig-value */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 438 | if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) && |
| 439 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 440 | if (first_pos) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 441 | *orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1])); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 442 | LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 443 | } else { |
| 444 | *orig_value = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 445 | LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| 449 | /* key */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 450 | if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) && |
| 451 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 452 | if (second_pos) { |
| 453 | buflen = bufused = 0; |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 454 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 455 | } else { |
| 456 | *key = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 457 | LY_CHECK_ERR_GOTO(!*key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | /* orig-key */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 462 | if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) && |
| 463 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 464 | if (first_pos) { |
| 465 | buflen = bufused = 0; |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 466 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 467 | } else { |
| 468 | *orig_key = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 469 | LY_CHECK_ERR_GOTO(!*orig_key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 473 | /* position */ |
| 474 | if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
| 475 | if (second_pos) { |
| 476 | if (asprintf(position, "%" PRIu32, second_pos) == -1) { |
| 477 | LOGMEM(schema->module->ctx); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 478 | rc = LY_EMEM; |
| 479 | goto cleanup; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 480 | } |
| 481 | } else { |
| 482 | *position = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 483 | LY_CHECK_ERR_GOTO(!*position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | /* orig-position */ |
| 488 | if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
| 489 | if (first_pos) { |
| 490 | if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) { |
| 491 | LOGMEM(schema->module->ctx); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 492 | rc = LY_EMEM; |
| 493 | goto cleanup; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 494 | } |
| 495 | } else { |
| 496 | *orig_position = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 497 | LY_CHECK_ERR_GOTO(!*orig_position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 501 | /* |
| 502 | * update our instances - apply the change |
| 503 | */ |
| 504 | if (*op == LYD_DIFF_OP_CREATE) { |
| 505 | /* insert the instance */ |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 506 | LY_ARRAY_CREATE_GOTO(schema->module->ctx, userord_item->inst, 1, rc, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 507 | if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) { |
| 508 | memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos, |
| 509 | (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst); |
| 510 | } |
| 511 | LY_ARRAY_INCREMENT(userord_item->inst); |
| 512 | userord_item->inst[second_pos] = second; |
| 513 | |
| 514 | } else if (*op == LYD_DIFF_OP_DELETE) { |
| 515 | /* remove the instance */ |
| 516 | if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) { |
| 517 | memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1, |
| 518 | (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst); |
| 519 | } |
| 520 | LY_ARRAY_DECREMENT(userord_item->inst); |
| 521 | |
| 522 | } else if (*op == LYD_DIFF_OP_REPLACE) { |
| 523 | /* move the instances */ |
| 524 | memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos, |
| 525 | (first_pos - second_pos) * sizeof *userord_item->inst); |
| 526 | userord_item->inst[second_pos] = first; |
| 527 | } |
| 528 | |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 529 | cleanup: |
| 530 | if (rc) { |
| 531 | free(*value); |
| 532 | *value = NULL; |
| 533 | free(*orig_value); |
| 534 | *orig_value = NULL; |
| 535 | free(*key); |
| 536 | *key = NULL; |
| 537 | free(*orig_key); |
| 538 | *orig_key = NULL; |
| 539 | free(*position); |
| 540 | *position = NULL; |
| 541 | free(*orig_position); |
| 542 | *orig_position = NULL; |
| 543 | } |
| 544 | return rc; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered |
| 549 | * lists/leaf-lists. |
| 550 | * |
| 551 | * @param[in] first Node from the first tree, can be NULL (on create). |
| 552 | * @param[in] second Node from the second tree, can be NULL (on delete). |
| 553 | * @param[in] options Diff options. |
| 554 | * @param[out] op Operation. |
| 555 | * @param[out] orig_default Original default metadata. |
| 556 | * @param[out] orig_value Original value metadata. |
| 557 | * @return LY_SUCCESS on success, |
| 558 | * @return LY_ENOT if there is no change to be added into diff, |
| 559 | * @return LY_ERR value on other errors. |
| 560 | */ |
| 561 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 562 | lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, enum lyd_diff_op *op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 563 | const char **orig_default, char **orig_value) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 564 | { |
| 565 | const struct lysc_node *schema; |
Michal Vasko | 6ea6fe2 | 2021-10-08 09:57:01 +0200 | [diff] [blame] | 566 | const char *str_val; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 567 | |
| 568 | assert(first || second); |
| 569 | |
| 570 | *orig_default = NULL; |
| 571 | *orig_value = NULL; |
| 572 | |
| 573 | schema = first ? first->schema : second->schema; |
| 574 | assert(!lysc_is_userordered(schema)); |
| 575 | |
| 576 | /* learn operation first */ |
| 577 | if (!second) { |
| 578 | *op = LYD_DIFF_OP_DELETE; |
| 579 | } else if (!first) { |
| 580 | *op = LYD_DIFF_OP_CREATE; |
| 581 | } else { |
| 582 | switch (schema->nodetype) { |
| 583 | case LYS_CONTAINER: |
| 584 | case LYS_RPC: |
| 585 | case LYS_ACTION: |
| 586 | case LYS_NOTIF: |
| 587 | /* no changes */ |
| 588 | return LY_ENOT; |
| 589 | case LYS_LIST: |
| 590 | case LYS_LEAFLIST: |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 591 | if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 592 | /* default flag change */ |
| 593 | *op = LYD_DIFF_OP_NONE; |
| 594 | } else { |
| 595 | /* no changes */ |
| 596 | return LY_ENOT; |
| 597 | } |
| 598 | break; |
| 599 | case LYS_LEAF: |
| 600 | case LYS_ANYXML: |
| 601 | case LYS_ANYDATA: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 602 | if (lyd_compare_single(first, second, 0)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 603 | /* different values */ |
| 604 | *op = LYD_DIFF_OP_REPLACE; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 605 | } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 606 | /* default flag change */ |
| 607 | *op = LYD_DIFF_OP_NONE; |
| 608 | } else { |
| 609 | /* no changes */ |
| 610 | return LY_ENOT; |
| 611 | } |
| 612 | break; |
| 613 | default: |
| 614 | LOGINT_RET(schema->module->ctx); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * set each attribute correctly based on the operation and node type |
| 620 | */ |
| 621 | |
| 622 | /* orig-default */ |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 623 | if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 624 | if (first->flags & LYD_DEFAULT) { |
| 625 | *orig_default = "true"; |
| 626 | } else { |
| 627 | *orig_default = "false"; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* orig-value */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 632 | if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) { |
| 633 | if (schema->nodetype == LYS_LEAF) { |
Michal Vasko | 6ea6fe2 | 2021-10-08 09:57:01 +0200 | [diff] [blame] | 634 | str_val = lyd_get_value(first); |
| 635 | *orig_value = strdup(str_val ? str_val : ""); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 636 | LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM); |
| 637 | } else { |
| 638 | LY_CHECK_RET(lyd_any_value_str(first, orig_value)); |
| 639 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return LY_SUCCESS; |
| 643 | } |
| 644 | |
| 645 | /** |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 646 | * @brief Find a matching instance of a node in a data tree. |
| 647 | * |
| 648 | * @param[in] siblings Siblings to search in. |
| 649 | * @param[in] target Target node to search for. |
| 650 | * @param[in] defaults Whether to consider (or ignore) default values. |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 651 | * @param[in,out] dup_inst_ht Duplicate instance cache. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 652 | * @param[out] match Found match, NULL if no matching node found. |
| 653 | * @return LY_ERR value. |
| 654 | */ |
| 655 | static LY_ERR |
| 656 | lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults, |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 657 | struct hash_table **dup_inst_ht, struct lyd_node **match) |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 658 | { |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 659 | LY_ERR r; |
| 660 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 661 | if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 662 | /* try to find the exact instance */ |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 663 | r = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 664 | } else { |
| 665 | /* try to simply find the node, there cannot be more instances */ |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 666 | r = lyd_find_sibling_val(siblings, target->schema, NULL, 0, match); |
| 667 | } |
| 668 | if (r && (r != LY_ENOTFOUND)) { |
| 669 | return r; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 670 | } |
| 671 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 672 | /* update match as needed */ |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 673 | LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_ht)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 674 | |
| 675 | if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) { |
| 676 | /* ignore default nodes */ |
| 677 | *match = NULL; |
| 678 | } |
| 679 | return LY_SUCCESS; |
| 680 | } |
| 681 | |
| 682 | /** |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 683 | * @brief Perform diff for all siblings at certain depth, recursively. |
| 684 | * |
| 685 | * For user-ordered lists/leaf-lists a specific structure is used for storing |
| 686 | * the current order. The idea is to apply all the generated diff changes |
| 687 | * virtually on the first tree so that we can continue to generate correct |
| 688 | * changes after some were already generated. |
| 689 | * |
| 690 | * The algorithm then uses second tree position-based changes with a before |
| 691 | * (preceding) item anchor. |
| 692 | * |
| 693 | * Example: |
| 694 | * |
| 695 | * Virtual first tree leaf-list order: |
| 696 | * 1 2 [3] 4 5 |
| 697 | * |
| 698 | * Second tree leaf-list order: |
| 699 | * 1 2 [5] 3 4 |
| 700 | * |
| 701 | * We are at the 3rd node now. We look at whether the nodes on the 3rd position |
| 702 | * match - they do not - move nodes so that the 3rd position node is final -> |
| 703 | * -> move node 5 to the 3rd position -> move node 5 after node 2. |
| 704 | * |
| 705 | * Required properties: |
| 706 | * Stored operations (move) should not be affected by later operations - |
| 707 | * - would cause a redundantly long list of operations, possibly inifinite. |
| 708 | * |
| 709 | * Implemenation justification: |
| 710 | * First, all delete operations and only then move/create operations are stored. |
| 711 | * Also, preceding anchor is used and after each iteration another node is |
| 712 | * at its final position. That results in the invariant that all preceding |
| 713 | * nodes are final and will not be changed by the later operations, meaning |
| 714 | * they can safely be used as anchors for the later operations. |
| 715 | * |
| 716 | * @param[in] first First tree first sibling. |
| 717 | * @param[in] second Second tree first sibling. |
| 718 | * @param[in] options Diff options. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 719 | * @param[in] nosiblings Whether to skip following siblings. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 720 | * @param[in,out] diff Diff to append to. |
| 721 | * @return LY_ERR value. |
| 722 | */ |
| 723 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 724 | lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 725 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 726 | { |
| 727 | LY_ERR ret = LY_SUCCESS; |
| 728 | const struct lyd_node *iter_first, *iter_second; |
| 729 | struct lyd_node *match_second, *match_first; |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 730 | struct lyd_diff_userord *userord = NULL, *userord_item; |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 731 | struct hash_table *dup_inst_first = NULL, *dup_inst_second = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 732 | LY_ARRAY_COUNT_TYPE u; |
| 733 | enum lyd_diff_op op; |
| 734 | const char *orig_default; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 735 | char *orig_value, *key, *value, *position, *orig_key, *orig_position; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 736 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 737 | /* compare first tree to the second tree - delete, replace, none */ |
| 738 | LY_LIST_FOR(first, iter_first) { |
Michal Vasko | c825ed7 | 2021-07-21 16:05:59 +0200 | [diff] [blame] | 739 | if (!iter_first->schema) { |
| 740 | continue; |
| 741 | } |
| 742 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 743 | assert(!(iter_first->schema->flags & LYS_KEY)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 744 | if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 745 | /* skip default nodes */ |
| 746 | continue; |
| 747 | } |
| 748 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 749 | /* find a match in the second tree */ |
| 750 | LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second, |
| 751 | &match_second), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 752 | |
| 753 | if (lysc_is_userordered(iter_first->schema)) { |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 754 | /* get (create) userord entry */ |
| 755 | userord_item = lyd_diff_userord_get(iter_first, iter_first->schema, &userord); |
| 756 | LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_first)); ret = LY_EMEM, cleanup); |
| 757 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 758 | /* we are handling only user-ordered node delete now */ |
| 759 | if (!match_second) { |
| 760 | /* get all the attributes */ |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 761 | LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, userord_item, &op, |
| 762 | &orig_default, &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 763 | |
| 764 | /* there must be changes, it is deleted */ |
| 765 | assert(op == LYD_DIFF_OP_DELETE); |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 766 | ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key, |
| 767 | orig_position, diff); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 768 | |
| 769 | free(orig_value); |
| 770 | free(key); |
| 771 | free(value); |
| 772 | free(position); |
| 773 | free(orig_key); |
| 774 | free(orig_position); |
| 775 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 776 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 777 | } else { |
| 778 | /* get all the attributes */ |
| 779 | ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value); |
| 780 | |
| 781 | /* add into diff if there are any changes */ |
| 782 | if (!ret) { |
| 783 | if (op == LYD_DIFF_OP_DELETE) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 784 | ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 785 | } else { |
Michal Vasko | 3c2dd6c | 2020-11-06 17:38:55 +0100 | [diff] [blame] | 786 | assert(match_second); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 787 | ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | free(orig_value); |
| 791 | LY_CHECK_GOTO(ret, cleanup); |
| 792 | } else if (ret == LY_ENOT) { |
| 793 | ret = LY_SUCCESS; |
| 794 | } else { |
| 795 | goto cleanup; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /* check descendants, if any, recursively */ |
| 800 | if (match_second) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 801 | LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second), |
| 802 | options, 0, diff), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | if (nosiblings) { |
| 806 | break; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* reset all cached positions */ |
| 811 | LY_ARRAY_FOR(userord, u) { |
| 812 | userord[u].pos = 0; |
| 813 | } |
| 814 | |
| 815 | /* compare second tree to the first tree - create, user-ordered move */ |
| 816 | LY_LIST_FOR(second, iter_second) { |
Michal Vasko | c825ed7 | 2021-07-21 16:05:59 +0200 | [diff] [blame] | 817 | if (!iter_second->schema) { |
| 818 | continue; |
| 819 | } |
| 820 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 821 | assert(!(iter_second->schema->flags & LYS_KEY)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 822 | if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 823 | /* skip default nodes */ |
| 824 | continue; |
| 825 | } |
| 826 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 827 | /* find a match in the first tree */ |
| 828 | LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first, |
| 829 | &match_first), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 830 | |
| 831 | if (lysc_is_userordered(iter_second->schema)) { |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 832 | /* get userord entry */ |
Michal Vasko | aa51cc5 | 2022-12-06 09:57:11 +0100 | [diff] [blame] | 833 | userord_item = lyd_diff_userord_get(match_first, iter_second->schema, &userord); |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 834 | LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_second)); ret = LY_EMEM, cleanup); |
| 835 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 836 | /* get all the attributes */ |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 837 | ret = lyd_diff_userord_attrs(match_first, iter_second, options, userord_item, &op, &orig_default, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 838 | &value, &orig_value, &key, &orig_key, &position, &orig_position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 839 | |
| 840 | /* add into diff if there are any changes */ |
| 841 | if (!ret) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 842 | ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key, |
| 843 | orig_position, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 844 | |
| 845 | free(orig_value); |
| 846 | free(key); |
| 847 | free(value); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 848 | free(position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 849 | free(orig_key); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 850 | free(orig_position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 851 | LY_CHECK_GOTO(ret, cleanup); |
| 852 | } else if (ret == LY_ENOT) { |
| 853 | ret = LY_SUCCESS; |
| 854 | } else { |
| 855 | goto cleanup; |
| 856 | } |
| 857 | } else if (!match_first) { |
| 858 | /* get all the attributes */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 859 | LY_CHECK_GOTO(ret = lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 860 | |
| 861 | /* there must be changes, it is created */ |
| 862 | assert(op == LYD_DIFF_OP_CREATE); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 863 | ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 864 | |
| 865 | free(orig_value); |
| 866 | LY_CHECK_GOTO(ret, cleanup); |
| 867 | } /* else was handled */ |
| 868 | |
| 869 | if (nosiblings) { |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | cleanup: |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 875 | lyd_dup_inst_free(dup_inst_first); |
| 876 | lyd_dup_inst_free(dup_inst_second); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 877 | LY_ARRAY_FOR(userord, u) { |
| 878 | LY_ARRAY_FREE(userord[u].inst); |
| 879 | } |
| 880 | LY_ARRAY_FREE(userord); |
| 881 | return ret; |
| 882 | } |
| 883 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 884 | static LY_ERR |
Michal Vasko | 5589617 | 2022-02-17 10:47:21 +0100 | [diff] [blame] | 885 | lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, |
| 886 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 887 | { |
| 888 | const struct ly_ctx *ctx; |
| 889 | |
| 890 | LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL); |
| 891 | |
| 892 | if (first) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 893 | ctx = LYD_CTX(first); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 894 | } else if (second) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 895 | ctx = LYD_CTX(second); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 896 | } else { |
| 897 | ctx = NULL; |
| 898 | } |
| 899 | |
| 900 | if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) { |
| 901 | LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__); |
| 902 | return LY_EINVAL; |
| 903 | } |
| 904 | |
| 905 | *diff = NULL; |
| 906 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 907 | return lyd_diff_siblings_r(first, second, options, nosiblings, diff); |
| 908 | } |
| 909 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 910 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 911 | lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 912 | { |
| 913 | return lyd_diff(first, second, options, 1, diff); |
| 914 | } |
| 915 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 916 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 917 | lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 918 | { |
| 919 | return lyd_diff(first, second, options, 0, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /** |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 923 | * @brief Learn operation of a diff node. |
| 924 | * |
| 925 | * @param[in] diff_node Diff node. |
| 926 | * @param[out] op Operation. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 927 | * @return LY_ERR value. |
| 928 | */ |
| 929 | static LY_ERR |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 930 | lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 931 | { |
| 932 | struct lyd_meta *meta = NULL; |
| 933 | const struct lyd_node *diff_parent; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 934 | const char *str; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 935 | char *path; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 936 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 937 | for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 938 | LY_LIST_FOR(diff_parent->meta, meta) { |
| 939 | if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 940 | str = lyd_get_meta_value(meta); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 941 | if ((str[0] == 'r') && (diff_parent != diff_node)) { |
| 942 | /* we do not care about this operation if it's in our parent */ |
| 943 | continue; |
| 944 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 945 | *op = lyd_diff_str2op(str); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | } |
| 949 | if (meta) { |
| 950 | break; |
| 951 | } |
| 952 | } |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 953 | |
| 954 | if (!meta) { |
| 955 | path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0); |
| 956 | LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path); |
| 957 | free(path); |
| 958 | return LY_EINT; |
| 959 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 960 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 961 | return LY_SUCCESS; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * @brief Insert a diff node into a data tree. |
| 966 | * |
| 967 | * @param[in,out] first_node First sibling of the data tree. |
| 968 | * @param[in] parent_node Data tree sibling parent node. |
| 969 | * @param[in] new_node Node to insert. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 970 | * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set, |
| 971 | * the user-ordered instance will be inserted at the first position. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 972 | * @return err_info, NULL on success. |
| 973 | */ |
| 974 | static LY_ERR |
| 975 | lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 976 | const char *userord_anchor) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 977 | { |
| 978 | LY_ERR ret; |
| 979 | struct lyd_node *anchor; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 980 | uint32_t pos, anchor_pos; |
| 981 | int found; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 982 | |
| 983 | assert(new_node); |
| 984 | |
| 985 | if (!*first_node) { |
| 986 | if (!parent_node) { |
| 987 | /* no parent or siblings */ |
| 988 | *first_node = new_node; |
| 989 | return LY_SUCCESS; |
| 990 | } |
| 991 | |
| 992 | /* simply insert into parent, no other children */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 993 | if (userord_anchor) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 994 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 995 | new_node->schema->name); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 996 | return LY_EINVAL; |
| 997 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 998 | return lyd_insert_child(parent_node, new_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1001 | assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1002 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1003 | if (!lysc_is_userordered(new_node->schema)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1004 | /* simple insert */ |
| 1005 | return lyd_insert_sibling(*first_node, new_node, first_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1008 | if (userord_anchor) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1009 | /* find the anchor sibling */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1010 | if (lysc_is_dup_inst_list(new_node->schema)) { |
| 1011 | anchor_pos = atoi(userord_anchor); |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1012 | if (!anchor_pos) { |
| 1013 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Invalid user-ordered anchor value \"%s\".", userord_anchor); |
| 1014 | return LY_EINVAL; |
| 1015 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1016 | |
| 1017 | found = 0; |
| 1018 | pos = 1; |
| 1019 | LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) { |
| 1020 | if (pos == anchor_pos) { |
| 1021 | found = 1; |
| 1022 | break; |
| 1023 | } |
| 1024 | ++pos; |
| 1025 | } |
| 1026 | if (!found) { |
| 1027 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
| 1028 | new_node->schema->name); |
| 1029 | return LY_EINVAL; |
| 1030 | } |
| 1031 | } else { |
| 1032 | ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor); |
| 1033 | if (ret == LY_ENOTFOUND) { |
| 1034 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
| 1035 | new_node->schema->name); |
| 1036 | return LY_EINVAL; |
| 1037 | } else if (ret) { |
| 1038 | return ret; |
| 1039 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* insert after */ |
| 1043 | LY_CHECK_RET(lyd_insert_after(anchor, new_node)); |
| 1044 | assert(new_node->prev == anchor); |
| 1045 | if (*first_node == new_node) { |
| 1046 | *first_node = anchor; |
| 1047 | } |
| 1048 | } else { |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1049 | /* find the first instance */ |
| 1050 | ret = lyd_find_sibling_val(*first_node, new_node->schema, NULL, 0, &anchor); |
| 1051 | LY_CHECK_RET(ret && (ret != LY_ENOTFOUND), ret); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1052 | |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1053 | if (anchor) { |
| 1054 | /* insert before the first instance */ |
| 1055 | LY_CHECK_RET(lyd_insert_before(anchor, new_node)); |
| 1056 | if ((*first_node)->prev->next) { |
| 1057 | assert(!new_node->prev->next); |
| 1058 | *first_node = new_node; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1059 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1060 | } else { |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1061 | /* insert anywhere */ |
| 1062 | LY_CHECK_RET(lyd_insert_sibling(*first_node, new_node, first_node)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return LY_SUCCESS; |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * @brief Apply diff subtree on data tree nodes, recursively. |
| 1071 | * |
| 1072 | * @param[in,out] first_node First sibling of the data tree. |
| 1073 | * @param[in] parent_node Parent of the first sibling. |
| 1074 | * @param[in] diff_node Current diff node. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1075 | * @param[in] diff_cb Optional diff callback. |
| 1076 | * @param[in] cb_data User data for @p diff_cb. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1077 | * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1078 | * @return LY_ERR value. |
| 1079 | */ |
| 1080 | static LY_ERR |
| 1081 | lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node, |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 1082 | lyd_diff_cb diff_cb, void *cb_data, struct hash_table **dup_inst) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1083 | { |
| 1084 | LY_ERR ret; |
| 1085 | struct lyd_node *match, *diff_child; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1086 | const char *str_val, *meta_str; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1087 | enum lyd_diff_op op; |
| 1088 | struct lyd_meta *meta; |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 1089 | struct hash_table *child_dup_inst = NULL; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1090 | const struct ly_ctx *ctx = LYD_CTX(diff_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1091 | |
| 1092 | /* read all the valid attributes */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1093 | LY_CHECK_RET(lyd_diff_get_op(diff_node, &op)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1094 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1095 | /* handle specific user-ordered (leaf-)lists operations separately */ |
| 1096 | if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) { |
| 1097 | if (op == LYD_DIFF_OP_REPLACE) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1098 | /* find the node (we must have some siblings because the node was only moved) */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1099 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1100 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1101 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1102 | /* duplicate the node */ |
| 1103 | LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1106 | /* get "key", "value", or "position" metadata string value */ |
| 1107 | if (lysc_is_dup_inst_list(diff_node->schema)) { |
| 1108 | meta_str = "yang:position"; |
| 1109 | } else if (diff_node->schema->nodetype == LYS_LIST) { |
| 1110 | meta_str = "yang:key"; |
| 1111 | } else { |
| 1112 | meta_str = "yang:value"; |
| 1113 | } |
| 1114 | meta = lyd_find_meta(diff_node->meta, NULL, meta_str); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1115 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_str, diff_node), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1116 | str_val = lyd_get_meta_value(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1117 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1118 | /* insert/move the node */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1119 | if (str_val[0]) { |
| 1120 | ret = lyd_diff_insert(first_node, parent_node, match, str_val); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1121 | } else { |
| 1122 | ret = lyd_diff_insert(first_node, parent_node, match, NULL); |
| 1123 | } |
| 1124 | if (ret) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1125 | if (op == LYD_DIFF_OP_CREATE) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1126 | lyd_free_tree(match); |
| 1127 | } |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | goto next_iter_r; |
| 1132 | } |
| 1133 | |
| 1134 | /* apply operation */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1135 | switch (op) { |
| 1136 | case LYD_DIFF_OP_NONE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1137 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1138 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1139 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1140 | |
| 1141 | if (match->schema->nodetype & LYD_NODE_TERM) { |
| 1142 | /* special case of only dflt flag change */ |
| 1143 | if (diff_node->flags & LYD_DEFAULT) { |
| 1144 | match->flags |= LYD_DEFAULT; |
| 1145 | } else { |
| 1146 | match->flags &= ~LYD_DEFAULT; |
| 1147 | } |
| 1148 | } else { |
| 1149 | /* none operation on nodes without children is redundant and hence forbidden */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1150 | if (!lyd_child_no_keys(diff_node)) { |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1151 | LOGERR(ctx, LY_EINVAL, "Operation \"none\" is invalid for node \"%s\" without children.", |
| 1152 | LYD_NAME(diff_node)); |
| 1153 | return LY_EINVAL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1157 | case LYD_DIFF_OP_CREATE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1158 | /* duplicate the node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1159 | LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1160 | |
| 1161 | /* insert it at the end */ |
| 1162 | ret = 0; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1163 | if (parent_node) { |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1164 | if (match->flags & LYD_EXT) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1165 | ret = lyplg_ext_insert(parent_node, match); |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1166 | } else { |
| 1167 | ret = lyd_insert_child(parent_node, match); |
| 1168 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1169 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1170 | ret = lyd_insert_sibling(*first_node, match, first_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1171 | } |
| 1172 | if (ret) { |
| 1173 | lyd_free_tree(match); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1178 | case LYD_DIFF_OP_DELETE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1179 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1180 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1181 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1182 | |
| 1183 | /* remove it */ |
| 1184 | if ((match == *first_node) && !match->parent) { |
| 1185 | assert(!parent_node); |
| 1186 | /* we have removed the top-level node */ |
| 1187 | *first_node = (*first_node)->next; |
| 1188 | } |
| 1189 | lyd_free_tree(match); |
| 1190 | |
| 1191 | /* we are not going recursively in this case, the whole subtree was already deleted */ |
| 1192 | return LY_SUCCESS; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1193 | case LYD_DIFF_OP_REPLACE: |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1194 | if (!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA))) { |
| 1195 | LOGERR(ctx, LY_EINVAL, "Operation \"replace\" is invalid for %s node \"%s\".", |
| 1196 | lys_nodetype2str(diff_node->schema->nodetype), LYD_NAME(diff_node)); |
| 1197 | return LY_EINVAL; |
| 1198 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1199 | |
| 1200 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1201 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1202 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1203 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1204 | /* update the value */ |
| 1205 | if (diff_node->schema->nodetype == LYS_LEAF) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1206 | ret = lyd_change_term(match, lyd_get_value(diff_node)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1207 | LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGERR_UNEXPVAL(ctx, match, "data"), LY_EINVAL); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1208 | } else { |
| 1209 | struct lyd_node_any *any = (struct lyd_node_any *)diff_node; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1210 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1211 | LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /* with flags */ |
| 1215 | match->flags = diff_node->flags; |
| 1216 | break; |
| 1217 | default: |
| 1218 | LOGINT_RET(ctx); |
| 1219 | } |
| 1220 | |
| 1221 | next_iter_r: |
| 1222 | if (diff_cb) { |
| 1223 | /* call callback */ |
| 1224 | LY_CHECK_RET(diff_cb(diff_node, match, cb_data)); |
| 1225 | } |
| 1226 | |
| 1227 | /* apply diff recursively */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1228 | ret = LY_SUCCESS; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1229 | LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1230 | ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst); |
| 1231 | if (ret) { |
| 1232 | break; |
| 1233 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1236 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1237 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1240 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1241 | lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1242 | lyd_diff_cb diff_cb, void *cb_data) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1243 | { |
| 1244 | const struct lyd_node *root; |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 1245 | struct hash_table *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1246 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1247 | |
| 1248 | LY_LIST_FOR(diff, root) { |
| 1249 | if (mod && (lyd_owner_module(root) != mod)) { |
| 1250 | /* skip data nodes from different modules */ |
| 1251 | continue; |
| 1252 | } |
| 1253 | |
| 1254 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1255 | ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst); |
| 1256 | if (ret) { |
| 1257 | break; |
| 1258 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1261 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1262 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1265 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1266 | lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1267 | { |
| 1268 | return lyd_diff_apply_module(data, diff, NULL, NULL, NULL); |
| 1269 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1270 | |
| 1271 | /** |
| 1272 | * @brief Update operations on a diff node when the new operation is NONE. |
| 1273 | * |
| 1274 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1275 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1276 | * @param[in] src_diff Current source diff node. |
| 1277 | * @return LY_ERR value. |
| 1278 | */ |
| 1279 | static LY_ERR |
| 1280 | lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1281 | { |
| 1282 | switch (cur_op) { |
| 1283 | case LYD_DIFF_OP_NONE: |
| 1284 | case LYD_DIFF_OP_CREATE: |
| 1285 | case LYD_DIFF_OP_REPLACE: |
| 1286 | if (src_diff->schema->nodetype & LYD_NODE_TERM) { |
| 1287 | /* NONE on a term means only its dflt flag was changed */ |
| 1288 | diff_match->flags &= ~LYD_DEFAULT; |
| 1289 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1290 | } |
| 1291 | break; |
| 1292 | default: |
| 1293 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1294 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE); |
| 1295 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | return LY_SUCCESS; |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * @brief Remove an attribute from a node. |
| 1303 | * |
| 1304 | * @param[in] node Node with the metadata. |
| 1305 | * @param[in] name Metadata name. |
| 1306 | */ |
| 1307 | static void |
| 1308 | lyd_diff_del_meta(struct lyd_node *node, const char *name) |
| 1309 | { |
| 1310 | struct lyd_meta *meta; |
| 1311 | |
| 1312 | LY_LIST_FOR(node->meta, meta) { |
| 1313 | if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1314 | lyd_free_meta_single(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1315 | return; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | assert(0); |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * @brief Set a specific operation of a node. Delete the previous operation, if any. |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1324 | * Does not change the default flag. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1325 | * |
| 1326 | * @param[in] node Node to change. |
| 1327 | * @param[in] op Operation to set. |
| 1328 | * @return LY_ERR value. |
| 1329 | */ |
| 1330 | static LY_ERR |
| 1331 | lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op) |
| 1332 | { |
| 1333 | struct lyd_meta *meta; |
| 1334 | |
| 1335 | LY_LIST_FOR(node->meta, meta) { |
| 1336 | if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1337 | lyd_free_meta_single(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1342 | return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | /** |
| 1346 | * @brief Update operations on a diff node when the new operation is REPLACE. |
| 1347 | * |
| 1348 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1349 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1350 | * @param[in] src_diff Current source diff node. |
| 1351 | * @return LY_ERR value. |
| 1352 | */ |
| 1353 | static LY_ERR |
| 1354 | lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1355 | { |
| 1356 | LY_ERR ret; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1357 | const char *str_val, *meta_name, *orig_meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1358 | struct lyd_meta *meta; |
| 1359 | const struct lys_module *mod; |
| 1360 | const struct lyd_node_any *any; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1361 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1362 | |
| 1363 | /* get "yang" module for the metadata */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1364 | mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1365 | assert(mod); |
| 1366 | |
| 1367 | switch (cur_op) { |
| 1368 | case LYD_DIFF_OP_REPLACE: |
| 1369 | case LYD_DIFF_OP_CREATE: |
| 1370 | switch (diff_match->schema->nodetype) { |
| 1371 | case LYS_LIST: |
| 1372 | case LYS_LEAFLIST: |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1373 | /* it was created/moved somewhere, but now it will be created/moved somewhere else, |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1374 | * keep orig_key/orig_value (only replace oper) and replace key/value */ |
| 1375 | assert(lysc_is_userordered(diff_match->schema)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1376 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1377 | meta_name = "position"; |
| 1378 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1379 | meta_name = "key"; |
| 1380 | } else { |
| 1381 | meta_name = "value"; |
| 1382 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1383 | |
| 1384 | lyd_diff_del_meta(diff_match, meta_name); |
| 1385 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1386 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1387 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1388 | break; |
| 1389 | case LYS_LEAF: |
| 1390 | /* replaced with the exact same value, impossible */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1391 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1392 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1393 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1396 | /* modify the node value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1397 | if (lyd_change_term(diff_match, lyd_get_value(src_diff))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1398 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1401 | if (cur_op == LYD_DIFF_OP_REPLACE) { |
| 1402 | /* compare values whether there is any change at all */ |
| 1403 | meta = lyd_find_meta(diff_match->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1404 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "orig-value", diff_match), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1405 | str_val = lyd_get_meta_value(meta); |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1406 | ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val)); |
| 1407 | if (!ret) { |
| 1408 | /* values are the same, remove orig-value meta and set oper to NONE */ |
| 1409 | lyd_free_meta_single(meta); |
| 1410 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1411 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* modify the default flag */ |
| 1415 | diff_match->flags &= ~LYD_DEFAULT; |
| 1416 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1417 | break; |
| 1418 | case LYS_ANYXML: |
| 1419 | case LYS_ANYDATA: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1420 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1421 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1422 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* modify the node value */ |
| 1426 | any = (struct lyd_node_any *)src_diff; |
| 1427 | LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type)); |
| 1428 | break; |
| 1429 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1430 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1431 | } |
| 1432 | break; |
| 1433 | case LYD_DIFF_OP_NONE: |
| 1434 | /* it is moved now */ |
| 1435 | assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST)); |
| 1436 | |
| 1437 | /* change the operation */ |
| 1438 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1439 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1440 | /* set orig-meta and meta */ |
| 1441 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1442 | meta_name = "position"; |
| 1443 | orig_meta_name = "orig-position"; |
| 1444 | } else { |
| 1445 | meta_name = "key"; |
| 1446 | orig_meta_name = "orig-key"; |
| 1447 | } |
| 1448 | |
| 1449 | meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1450 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, orig_meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1451 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1452 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1453 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1454 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1455 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1456 | break; |
| 1457 | default: |
| 1458 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1459 | LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE); |
| 1460 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | return LY_SUCCESS; |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * @brief Update operations in a diff node when the new operation is CREATE. |
| 1468 | * |
| 1469 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1470 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1471 | * @param[in] src_diff Current source diff node. |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1472 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1473 | * @return LY_ERR value. |
| 1474 | */ |
| 1475 | static LY_ERR |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1476 | lyd_diff_merge_create(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1477 | { |
| 1478 | struct lyd_node *child; |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1479 | const struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1480 | uint32_t trg_flags; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1481 | const char *meta_name, *orig_meta_name; |
| 1482 | struct lyd_meta *meta, *orig_meta; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1483 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1484 | |
| 1485 | switch (cur_op) { |
| 1486 | case LYD_DIFF_OP_DELETE: |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1487 | /* remember current flags */ |
| 1488 | trg_flags = diff_match->flags; |
| 1489 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1490 | if (lysc_is_userordered(diff_match->schema)) { |
| 1491 | /* get anchor metadata */ |
| 1492 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1493 | meta_name = "yang:position"; |
| 1494 | orig_meta_name = "yang:orig-position"; |
| 1495 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1496 | meta_name = "yang:key"; |
| 1497 | orig_meta_name = "yang:orig-key"; |
| 1498 | } else { |
| 1499 | meta_name = "yang:value"; |
| 1500 | orig_meta_name = "yang:orig-value"; |
| 1501 | } |
| 1502 | meta = lyd_find_meta(src_diff->meta, NULL, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1503 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1504 | orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1505 | LY_CHECK_ERR_RET(!orig_meta, LOGERR_META(ctx, orig_meta_name, diff_match), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1506 | |
| 1507 | /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect |
| 1508 | * the anchors stored in the metadata */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1509 | if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1510 | /* deleted + created at another position -> operation REPLACE */ |
| 1511 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1512 | |
| 1513 | /* add anchor metadata */ |
| 1514 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
| 1515 | } else { |
| 1516 | /* deleted + created at the same position -> operation NONE */ |
| 1517 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1518 | |
| 1519 | /* delete anchor metadata */ |
| 1520 | lyd_free_meta_single(orig_meta); |
| 1521 | } |
| 1522 | } else if (diff_match->schema->nodetype == LYS_LEAF) { |
| 1523 | if (options & LYD_DIFF_MERGE_DEFAULTS) { |
| 1524 | /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */ |
| 1525 | sleaf = (struct lysc_node_leaf *)diff_match->schema; |
| 1526 | } |
| 1527 | |
Radek Krejci | 55c4bd2 | 2021-04-26 08:09:04 +0200 | [diff] [blame] | 1528 | if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt, |
| 1529 | &((struct lyd_node_term *)src_diff)->value)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1530 | /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */ |
| 1531 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1532 | } else if (!lyd_compare_single(diff_match, src_diff, 0)) { |
| 1533 | /* deleted + created -> operation NONE */ |
| 1534 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1535 | } else { |
| 1536 | /* we deleted it, but it was created with a different value -> operation REPLACE */ |
| 1537 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1538 | |
| 1539 | /* current value is the previous one (meta) */ |
| 1540 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value", |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1541 | lyd_get_value(diff_match), 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1542 | |
| 1543 | /* update the value itself */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1544 | LY_CHECK_RET(lyd_change_term(diff_match, lyd_get_value(src_diff))); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1545 | } |
| 1546 | } else { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1547 | /* deleted + created -> operation NONE */ |
| 1548 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | if (diff_match->schema->nodetype & LYD_NODE_TERM) { |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 1552 | /* add orig-dflt metadata */ |
| 1553 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default", |
| 1554 | trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
| 1555 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1556 | /* update dflt flag itself */ |
| 1557 | diff_match->flags &= ~LYD_DEFAULT; |
| 1558 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /* but the operation of its children should remain DELETE */ |
| 1562 | LY_LIST_FOR(lyd_child_no_keys(diff_match), child) { |
| 1563 | LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1564 | } |
| 1565 | break; |
| 1566 | default: |
| 1567 | /* create and replace operations are not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1568 | LOGERR_MERGEOP(LYD_CTX(src_diff), diff_match, cur_op, LYD_DIFF_OP_CREATE); |
| 1569 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | return LY_SUCCESS; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * @brief Update operations on a diff node when the new operation is DELETE. |
| 1577 | * |
| 1578 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1579 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1580 | * @param[in] src_diff Current source diff node. |
| 1581 | * @return LY_ERR value. |
| 1582 | */ |
| 1583 | static LY_ERR |
| 1584 | lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1585 | { |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1586 | struct lyd_node *child; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1587 | struct lyd_meta *meta; |
| 1588 | const char *meta_name; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1589 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1590 | |
| 1591 | /* we can delete only exact existing nodes */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1592 | LY_CHECK_ERR_RET(lyd_compare_single(diff_match, src_diff, 0), LOGINT(LYD_CTX(src_diff)), LY_EINT); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1593 | |
| 1594 | switch (cur_op) { |
| 1595 | case LYD_DIFF_OP_CREATE: |
| 1596 | /* it was created, but then deleted -> set NONE operation */ |
| 1597 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1598 | |
| 1599 | if (diff_match->schema->nodetype & LYD_NODE_TERM) { |
| 1600 | /* add orig-default meta because it is expected */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1601 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default", |
| 1602 | diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1603 | } else if (!lysc_is_dup_inst_list(diff_match->schema)) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1604 | /* keep operation for all descendants (for now) */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1605 | LY_LIST_FOR(lyd_child_no_keys(diff_match), child) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1606 | LY_CHECK_RET(lyd_diff_change_op(child, cur_op)); |
| 1607 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1608 | } /* else key-less list, for which all the descendants act as keys */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1609 | break; |
| 1610 | case LYD_DIFF_OP_REPLACE: |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1611 | /* remove the redundant metadata */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1612 | if (lysc_is_userordered(diff_match->schema)) { |
| 1613 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1614 | meta_name = "position"; |
| 1615 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1616 | meta_name = "key"; |
| 1617 | } else { |
| 1618 | meta_name = "value"; |
| 1619 | } |
| 1620 | } else { |
| 1621 | assert(diff_match->schema->nodetype == LYS_LEAF); |
| 1622 | |
| 1623 | /* switch value for the original one */ |
| 1624 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1625 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-value", diff_match), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1626 | if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1627 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1628 | return LY_EINVAL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | /* switch default for the original one, then remove the meta */ |
| 1632 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1633 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-default", diff_match), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1634 | diff_match->flags &= ~LYD_DEFAULT; |
| 1635 | if (meta->value.boolean) { |
| 1636 | diff_match->flags |= LYD_DEFAULT; |
| 1637 | } |
| 1638 | lyd_free_meta_single(meta); |
| 1639 | |
| 1640 | meta_name = "orig-value"; |
| 1641 | } |
| 1642 | lyd_diff_del_meta(diff_match, meta_name); |
| 1643 | |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1644 | /* it was being changed, but should be deleted instead -> set DELETE operation */ |
| 1645 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE)); |
| 1646 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1647 | case LYD_DIFF_OP_NONE: |
| 1648 | /* it was not modified, but should be deleted -> set DELETE operation */ |
| 1649 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1650 | break; |
| 1651 | default: |
| 1652 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1653 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE); |
| 1654 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | return LY_SUCCESS; |
| 1658 | } |
| 1659 | |
| 1660 | /** |
| 1661 | * @brief Check whether this diff node is redundant (does not change data). |
| 1662 | * |
| 1663 | * @param[in] diff Diff node. |
| 1664 | * @return 0 if not, non-zero if it is. |
| 1665 | */ |
| 1666 | static int |
| 1667 | lyd_diff_is_redundant(struct lyd_node *diff) |
| 1668 | { |
| 1669 | enum lyd_diff_op op; |
| 1670 | struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL; |
| 1671 | struct lyd_node *child; |
| 1672 | const struct lys_module *mod; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1673 | const char *str, *orig_meta_name, *meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1674 | |
| 1675 | assert(diff); |
| 1676 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1677 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1678 | /* all descendants are keys */ |
| 1679 | child = NULL; |
| 1680 | } else { |
| 1681 | child = lyd_child_no_keys(diff); |
| 1682 | } |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1683 | mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1684 | assert(mod); |
| 1685 | |
| 1686 | /* get node operation */ |
Michal Vasko | 53bf6f2 | 2020-07-14 08:23:40 +0200 | [diff] [blame] | 1687 | LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1688 | |
| 1689 | if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1690 | /* get metadata names */ |
| 1691 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1692 | meta_name = "position"; |
| 1693 | orig_meta_name = "orig-position"; |
| 1694 | } else if (diff->schema->nodetype == LYS_LIST) { |
| 1695 | meta_name = "key"; |
| 1696 | orig_meta_name = "orig-key"; |
| 1697 | } else { |
| 1698 | meta_name = "value"; |
| 1699 | orig_meta_name = "orig-value"; |
| 1700 | } |
| 1701 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1702 | /* check for redundant move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1703 | orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name); |
| 1704 | val_meta = lyd_find_meta(diff->meta, mod, meta_name); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1705 | assert(orig_val_meta && val_meta); |
| 1706 | |
| 1707 | if (!lyd_compare_meta(orig_val_meta, val_meta)) { |
| 1708 | /* there is actually no move */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1709 | lyd_free_meta_single(orig_val_meta); |
| 1710 | lyd_free_meta_single(val_meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1711 | if (child) { |
| 1712 | /* change operation to NONE, we have siblings */ |
| 1713 | lyd_diff_change_op(diff, LYD_DIFF_OP_NONE); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | /* redundant node, BUT !! |
| 1718 | * In diff the move operation is always converted to be INSERT_AFTER, which is fine |
| 1719 | * because the data that this is applied on should not change for the diff lifetime. |
| 1720 | * However, when we are merging 2 diffs, this conversion is actually lossy because |
| 1721 | * if the data change, the move operation can also change its meaning. In this specific |
| 1722 | * case the move operation will be lost. But it can be considered a feature, it is not supported. |
| 1723 | */ |
| 1724 | return 1; |
| 1725 | } |
| 1726 | } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) { |
| 1727 | /* check whether at least the default flags are different */ |
| 1728 | meta = lyd_find_meta(diff->meta, mod, "orig-default"); |
| 1729 | assert(meta); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1730 | str = lyd_get_meta_value(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1731 | |
| 1732 | /* if previous and current dflt flags are the same, this node is redundant */ |
| 1733 | if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) { |
| 1734 | return 1; |
| 1735 | } |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | if (!child && (op == LYD_DIFF_OP_NONE)) { |
| 1740 | return 1; |
| 1741 | } |
| 1742 | |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | /** |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1747 | * @brief Merge sysrepo diff subtree with another diff, recursively. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1748 | * |
| 1749 | * @param[in] src_diff Source diff node. |
| 1750 | * @param[in] diff_parent Current sysrepo diff parent. |
| 1751 | * @param[in] diff_cb Optional diff callback. |
| 1752 | * @param[in] cb_data User data for @p diff_cb. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1753 | * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings. |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1754 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1755 | * @param[in,out] diff Diff root node. |
| 1756 | * @return LY_ERR value. |
| 1757 | */ |
| 1758 | static LY_ERR |
| 1759 | lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data, |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 1760 | struct hash_table **dup_inst, uint16_t options, struct lyd_node **diff) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1761 | { |
| 1762 | LY_ERR ret = LY_SUCCESS; |
| 1763 | struct lyd_node *child, *diff_node = NULL; |
| 1764 | enum lyd_diff_op src_op, cur_op; |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 1765 | struct hash_table *child_dup_inst = NULL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1766 | |
| 1767 | /* get source node operation */ |
| 1768 | LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op)); |
| 1769 | |
| 1770 | /* find an equal node in the current diff */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1771 | LY_CHECK_RET(lyd_diff_find_match(diff_parent ? lyd_child_no_keys(diff_parent) : *diff, src_diff, 1, dup_inst, &diff_node)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1772 | |
| 1773 | if (diff_node) { |
| 1774 | /* get target (current) operation */ |
| 1775 | LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op)); |
| 1776 | |
| 1777 | /* merge operations */ |
| 1778 | switch (src_op) { |
| 1779 | case LYD_DIFF_OP_REPLACE: |
| 1780 | ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff); |
| 1781 | break; |
| 1782 | case LYD_DIFF_OP_CREATE: |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1783 | if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1784 | /* special case of creating duplicate (leaf-)list instances */ |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1785 | goto add_diff; |
| 1786 | } |
| 1787 | |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1788 | ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1789 | break; |
| 1790 | case LYD_DIFF_OP_DELETE: |
| 1791 | ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff); |
| 1792 | break; |
| 1793 | case LYD_DIFF_OP_NONE: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1794 | /* key-less list can never have "none" operation since all its descendants are acting as "keys" */ |
| 1795 | assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1796 | ret = lyd_diff_merge_none(diff_node, cur_op, src_diff); |
| 1797 | break; |
| 1798 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1799 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1800 | } |
| 1801 | if (ret) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1802 | LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1803 | return ret; |
| 1804 | } |
| 1805 | |
| 1806 | if (diff_cb) { |
| 1807 | /* call callback */ |
Michal Vasko | bc5fba9 | 2020-08-07 12:14:39 +0200 | [diff] [blame] | 1808 | LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /* update diff parent */ |
| 1812 | diff_parent = diff_node; |
| 1813 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1814 | /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances), |
| 1815 | * so there is nothing to merge for these "keys" */ |
| 1816 | if (!lysc_is_dup_inst_list(src_diff->schema)) { |
| 1817 | /* merge src_diff recursively */ |
| 1818 | LY_LIST_FOR(lyd_child_no_keys(src_diff), child) { |
| 1819 | ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff); |
| 1820 | if (ret) { |
| 1821 | break; |
| 1822 | } |
| 1823 | } |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1824 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1825 | LY_CHECK_RET(ret); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1826 | } |
| 1827 | } else { |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1828 | add_diff: |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1829 | /* add new diff node with all descendants */ |
Michal Vasko | 9ad7685 | 2022-07-12 10:18:03 +0200 | [diff] [blame] | 1830 | if ((src_diff->flags & LYD_EXT) && diff_parent) { |
| 1831 | LY_CHECK_RET(lyd_dup_single_to_ctx(src_diff, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent, |
| 1832 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 1833 | } else { |
| 1834 | LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, |
| 1835 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 1836 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1837 | |
| 1838 | /* insert node into diff if not already */ |
| 1839 | if (!diff_parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1840 | lyd_insert_sibling(*diff, diff_node, diff); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /* update operation */ |
| 1844 | LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op)); |
| 1845 | |
| 1846 | if (diff_cb) { |
Michal Vasko | e2af841 | 2020-12-03 14:11:38 +0100 | [diff] [blame] | 1847 | /* call callback with no source diff node since it was duplicated and just added */ |
| 1848 | LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | /* update diff parent */ |
| 1852 | diff_parent = diff_node; |
| 1853 | } |
| 1854 | |
| 1855 | /* remove any redundant nodes */ |
Michal Vasko | b98d708 | 2020-07-15 16:38:36 +0200 | [diff] [blame] | 1856 | if (lyd_diff_is_redundant(diff_parent)) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1857 | if (diff_parent == *diff) { |
| 1858 | *diff = (*diff)->next; |
| 1859 | } |
| 1860 | lyd_free_tree(diff_parent); |
| 1861 | } |
| 1862 | |
| 1863 | return LY_SUCCESS; |
| 1864 | } |
| 1865 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1866 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 1867 | lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod, |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1868 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1869 | { |
| 1870 | const struct lyd_node *src_root; |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 1871 | struct hash_table *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1872 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1873 | |
| 1874 | LY_LIST_FOR(src_diff, src_root) { |
| 1875 | if (mod && (lyd_owner_module(src_root) != mod)) { |
| 1876 | /* skip data nodes from different modules */ |
| 1877 | continue; |
| 1878 | } |
| 1879 | |
| 1880 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1881 | LY_CHECK_GOTO(ret = lyd_diff_merge_r(src_root, NULL, diff_cb, cb_data, &dup_inst, options, diff), cleanup); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1882 | } |
| 1883 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1884 | cleanup: |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1885 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1886 | return ret; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1887 | } |
| 1888 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1889 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1890 | lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling, |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1891 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1892 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1893 | LY_ERR ret; |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame^] | 1894 | struct hash_table *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1895 | |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1896 | if (!src_sibling) { |
| 1897 | return LY_SUCCESS; |
| 1898 | } |
| 1899 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1900 | ret = lyd_diff_merge_r(src_sibling, diff_parent, diff_cb, cb_data, &dup_inst, options, diff_first); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1901 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1902 | return ret; |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1903 | } |
| 1904 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1905 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1906 | lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1907 | { |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1908 | return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1909 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1910 | |
| 1911 | static LY_ERR |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1912 | lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod) |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1913 | { |
| 1914 | LY_ERR ret = LY_SUCCESS; |
| 1915 | struct lyd_meta *meta; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1916 | const char *val1 = NULL; |
| 1917 | char *val2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1918 | uint32_t flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1919 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1920 | assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA)); |
| 1921 | |
| 1922 | meta = lyd_find_meta(node->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1923 | LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), "orig-value", node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1924 | |
| 1925 | /* orig-value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1926 | val1 = lyd_get_meta_value(meta); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1927 | |
| 1928 | /* current value */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1929 | if (node->schema->nodetype == LYS_LEAF) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1930 | val2 = strdup(lyd_get_value(node)); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1931 | } else { |
| 1932 | LY_CHECK_RET(lyd_any_value_str(node, &val2)); |
| 1933 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1934 | |
| 1935 | /* switch values, keep default flag */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1936 | flags = node->flags; |
| 1937 | if (node->schema->nodetype == LYS_LEAF) { |
| 1938 | LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup); |
| 1939 | } else { |
| 1940 | union lyd_any_value anyval = {.str = val1}; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1941 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1942 | LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup); |
| 1943 | } |
| 1944 | node->flags = flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1945 | LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup); |
| 1946 | |
| 1947 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1948 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1949 | return ret; |
| 1950 | } |
| 1951 | |
| 1952 | static LY_ERR |
| 1953 | lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod) |
| 1954 | { |
| 1955 | struct lyd_meta *meta; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1956 | uint32_t flag1, flag2; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1957 | |
| 1958 | meta = lyd_find_meta(node->meta, mod, "orig-default"); |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 1959 | LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1960 | |
| 1961 | /* orig-default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1962 | if (meta->value.boolean) { |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1963 | flag1 = LYD_DEFAULT; |
| 1964 | } else { |
| 1965 | flag1 = 0; |
| 1966 | } |
| 1967 | |
| 1968 | /* current default */ |
| 1969 | flag2 = node->flags & LYD_DEFAULT; |
| 1970 | |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 1971 | if (flag1 == flag2) { |
| 1972 | /* no default state change so nothing to reverse */ |
| 1973 | return LY_SUCCESS; |
| 1974 | } |
| 1975 | |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1976 | /* switch defaults */ |
| 1977 | node->flags &= ~LYD_DEFAULT; |
| 1978 | node->flags |= flag1; |
| 1979 | LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false")); |
| 1980 | |
| 1981 | return LY_SUCCESS; |
| 1982 | } |
| 1983 | |
| 1984 | static LY_ERR |
| 1985 | lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2) |
| 1986 | { |
| 1987 | LY_ERR ret = LY_SUCCESS; |
| 1988 | struct lyd_meta *meta1, *meta2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1989 | const char *val1 = NULL; |
| 1990 | char *val2 = NULL; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1991 | |
| 1992 | meta1 = lyd_find_meta(node->meta, mod, name1); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1993 | LY_CHECK_ERR_RET(!meta1, LOGERR_META(LYD_CTX(node), name1, node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1994 | |
| 1995 | meta2 = lyd_find_meta(node->meta, mod, name2); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1996 | LY_CHECK_ERR_RET(!meta2, LOGERR_META(LYD_CTX(node), name2, node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* value1 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1999 | val1 = lyd_get_meta_value(meta1); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2000 | |
| 2001 | /* value2 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2002 | val2 = strdup(lyd_get_meta_value(meta2)); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* switch values */ |
| 2005 | LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup); |
| 2006 | LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup); |
| 2007 | |
| 2008 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2009 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2010 | return ret; |
| 2011 | } |
| 2012 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2013 | /** |
| 2014 | * @brief Remove specific operation from all the nodes in a subtree. |
| 2015 | * |
| 2016 | * @param[in] diff Diff subtree to process. |
| 2017 | * @param[in] op Only expected operation. |
| 2018 | * @return LY_ERR value. |
| 2019 | */ |
| 2020 | static LY_ERR |
| 2021 | lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op) |
| 2022 | { |
| 2023 | struct lyd_node *elem; |
| 2024 | struct lyd_meta *meta; |
| 2025 | |
| 2026 | LYD_TREE_DFS_BEGIN(diff, elem) { |
| 2027 | meta = lyd_find_meta(elem->meta, NULL, "yang:operation"); |
| 2028 | if (meta) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2029 | LY_CHECK_ERR_RET(lyd_diff_str2op(lyd_get_meta_value(meta)) != op, LOGINT(LYD_CTX(diff)), LY_EINT); |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2030 | lyd_free_meta_single(meta); |
| 2031 | } |
| 2032 | |
| 2033 | LYD_TREE_DFS_END(diff, elem); |
| 2034 | } |
| 2035 | |
| 2036 | return LY_SUCCESS; |
| 2037 | } |
| 2038 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2039 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6653581 | 2020-08-11 08:44:22 +0200 | [diff] [blame] | 2040 | lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff) |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2041 | { |
| 2042 | LY_ERR ret = LY_SUCCESS; |
| 2043 | const struct lys_module *mod; |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2044 | struct lyd_node *root, *elem, *iter; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2045 | enum lyd_diff_op op; |
| 2046 | |
| 2047 | LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL); |
| 2048 | |
| 2049 | if (!src_diff) { |
| 2050 | *diff = NULL; |
| 2051 | return LY_SUCCESS; |
| 2052 | } |
| 2053 | |
| 2054 | /* duplicate diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2055 | LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff)); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2056 | |
| 2057 | /* find module with metadata needed for later */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2058 | mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang"); |
| 2059 | LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2060 | |
| 2061 | LY_LIST_FOR(*diff, root) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2062 | LYD_TREE_DFS_BEGIN(root, elem) { |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2063 | /* skip all keys */ |
| 2064 | if (!lysc_is_key(elem->schema)) { |
| 2065 | /* find operation attribute, if any */ |
| 2066 | LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2067 | |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2068 | switch (op) { |
| 2069 | case LYD_DIFF_OP_CREATE: |
| 2070 | /* reverse create to delete */ |
| 2071 | LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup); |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2072 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2073 | /* check all the children for the same operation, nothing else is expected */ |
| 2074 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2075 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE); |
| 2076 | } |
| 2077 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2078 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2079 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2080 | case LYD_DIFF_OP_DELETE: |
| 2081 | /* reverse delete to create */ |
| 2082 | LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup); |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2083 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2084 | /* check all the children for the same operation, nothing else is expected */ |
| 2085 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2086 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE); |
| 2087 | } |
| 2088 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2089 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2090 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2091 | case LYD_DIFF_OP_REPLACE: |
| 2092 | switch (elem->schema->nodetype) { |
| 2093 | case LYS_LEAF: |
| 2094 | /* leaf value change */ |
| 2095 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2096 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2097 | break; |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2098 | case LYS_ANYXML: |
| 2099 | case LYS_ANYDATA: |
| 2100 | /* any value change */ |
| 2101 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2102 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2103 | case LYS_LEAFLIST: |
| 2104 | /* leaf-list move */ |
| 2105 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2106 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2107 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2108 | } else { |
| 2109 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup); |
| 2110 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2111 | break; |
| 2112 | case LYS_LIST: |
| 2113 | /* list move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2114 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2115 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2116 | } else { |
| 2117 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup); |
| 2118 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2119 | break; |
| 2120 | default: |
| 2121 | LOGINT(LYD_CTX(src_diff)); |
| 2122 | ret = LY_EINT; |
| 2123 | goto cleanup; |
| 2124 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2125 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2126 | case LYD_DIFF_OP_NONE: |
| 2127 | switch (elem->schema->nodetype) { |
| 2128 | case LYS_LEAF: |
| 2129 | case LYS_LEAFLIST: |
| 2130 | /* default flag change */ |
| 2131 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2132 | break; |
| 2133 | default: |
| 2134 | /* nothing to do */ |
| 2135 | break; |
| 2136 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2137 | break; |
| 2138 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2139 | } |
| 2140 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2141 | LYD_TREE_DFS_END(root, elem); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | cleanup: |
| 2146 | if (ret) { |
| 2147 | lyd_free_siblings(*diff); |
| 2148 | *diff = NULL; |
| 2149 | } |
| 2150 | return ret; |
| 2151 | } |