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