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