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