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 | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1209 | 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] | 1210 | } |
| 1211 | |
| 1212 | /* with flags */ |
| 1213 | match->flags = diff_node->flags; |
| 1214 | break; |
| 1215 | default: |
| 1216 | LOGINT_RET(ctx); |
| 1217 | } |
| 1218 | |
| 1219 | next_iter_r: |
| 1220 | if (diff_cb) { |
| 1221 | /* call callback */ |
| 1222 | LY_CHECK_RET(diff_cb(diff_node, match, cb_data)); |
| 1223 | } |
| 1224 | |
| 1225 | /* apply diff recursively */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1226 | ret = LY_SUCCESS; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1227 | LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1228 | ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst); |
| 1229 | if (ret) { |
| 1230 | break; |
| 1231 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1234 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1235 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1236 | } |
| 1237 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1238 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1239 | 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] | 1240 | lyd_diff_cb diff_cb, void *cb_data) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1241 | { |
| 1242 | const struct lyd_node *root; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1243 | struct lyd_dup_inst *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1244 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1245 | |
| 1246 | LY_LIST_FOR(diff, root) { |
| 1247 | if (mod && (lyd_owner_module(root) != mod)) { |
| 1248 | /* skip data nodes from different modules */ |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1253 | ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst); |
| 1254 | if (ret) { |
| 1255 | break; |
| 1256 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1259 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1260 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1261 | } |
| 1262 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1263 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1264 | 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] | 1265 | { |
| 1266 | return lyd_diff_apply_module(data, diff, NULL, NULL, NULL); |
| 1267 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1268 | |
| 1269 | /** |
| 1270 | * @brief Update operations on a diff node when the new operation is NONE. |
| 1271 | * |
| 1272 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1273 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1274 | * @param[in] src_diff Current source diff node. |
| 1275 | * @return LY_ERR value. |
| 1276 | */ |
| 1277 | static LY_ERR |
| 1278 | lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1279 | { |
| 1280 | switch (cur_op) { |
| 1281 | case LYD_DIFF_OP_NONE: |
| 1282 | case LYD_DIFF_OP_CREATE: |
| 1283 | case LYD_DIFF_OP_REPLACE: |
| 1284 | if (src_diff->schema->nodetype & LYD_NODE_TERM) { |
| 1285 | /* NONE on a term means only its dflt flag was changed */ |
| 1286 | diff_match->flags &= ~LYD_DEFAULT; |
| 1287 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1288 | } |
| 1289 | break; |
| 1290 | default: |
| 1291 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1292 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE); |
| 1293 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | return LY_SUCCESS; |
| 1297 | } |
| 1298 | |
| 1299 | /** |
| 1300 | * @brief Remove an attribute from a node. |
| 1301 | * |
| 1302 | * @param[in] node Node with the metadata. |
| 1303 | * @param[in] name Metadata name. |
| 1304 | */ |
| 1305 | static void |
| 1306 | lyd_diff_del_meta(struct lyd_node *node, const char *name) |
| 1307 | { |
| 1308 | struct lyd_meta *meta; |
| 1309 | |
| 1310 | LY_LIST_FOR(node->meta, meta) { |
| 1311 | if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1312 | lyd_free_meta_single(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1313 | return; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | assert(0); |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * @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] | 1322 | * Does not change the default flag. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1323 | * |
| 1324 | * @param[in] node Node to change. |
| 1325 | * @param[in] op Operation to set. |
| 1326 | * @return LY_ERR value. |
| 1327 | */ |
| 1328 | static LY_ERR |
| 1329 | lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op) |
| 1330 | { |
| 1331 | struct lyd_meta *meta; |
| 1332 | |
| 1333 | LY_LIST_FOR(node->meta, meta) { |
| 1334 | if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1335 | lyd_free_meta_single(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1340 | 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] | 1341 | } |
| 1342 | |
| 1343 | /** |
| 1344 | * @brief Update operations on a diff node when the new operation is REPLACE. |
| 1345 | * |
| 1346 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1347 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1348 | * @param[in] src_diff Current source diff node. |
| 1349 | * @return LY_ERR value. |
| 1350 | */ |
| 1351 | static LY_ERR |
| 1352 | lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1353 | { |
| 1354 | LY_ERR ret; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1355 | const char *str_val, *meta_name, *orig_meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1356 | struct lyd_meta *meta; |
| 1357 | const struct lys_module *mod; |
| 1358 | const struct lyd_node_any *any; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1359 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1360 | |
| 1361 | /* get "yang" module for the metadata */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1362 | mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1363 | assert(mod); |
| 1364 | |
| 1365 | switch (cur_op) { |
| 1366 | case LYD_DIFF_OP_REPLACE: |
| 1367 | case LYD_DIFF_OP_CREATE: |
| 1368 | switch (diff_match->schema->nodetype) { |
| 1369 | case LYS_LIST: |
| 1370 | case LYS_LEAFLIST: |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1371 | /* 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] | 1372 | * keep orig_key/orig_value (only replace oper) and replace key/value */ |
| 1373 | assert(lysc_is_userordered(diff_match->schema)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1374 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1375 | meta_name = "position"; |
| 1376 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1377 | meta_name = "key"; |
| 1378 | } else { |
| 1379 | meta_name = "value"; |
| 1380 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1381 | |
| 1382 | lyd_diff_del_meta(diff_match, meta_name); |
| 1383 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1384 | 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] | 1385 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1386 | break; |
| 1387 | case LYS_LEAF: |
| 1388 | /* replaced with the exact same value, impossible */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1389 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1390 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1391 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1392 | } |
| 1393 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1394 | /* modify the node value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1395 | if (lyd_change_term(diff_match, lyd_get_value(src_diff))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1396 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1399 | if (cur_op == LYD_DIFF_OP_REPLACE) { |
| 1400 | /* compare values whether there is any change at all */ |
| 1401 | meta = lyd_find_meta(diff_match->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1402 | 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] | 1403 | str_val = lyd_get_meta_value(meta); |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1404 | ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val)); |
| 1405 | if (!ret) { |
| 1406 | /* values are the same, remove orig-value meta and set oper to NONE */ |
| 1407 | lyd_free_meta_single(meta); |
| 1408 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1409 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | /* modify the default flag */ |
| 1413 | diff_match->flags &= ~LYD_DEFAULT; |
| 1414 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1415 | break; |
| 1416 | case LYS_ANYXML: |
| 1417 | case LYS_ANYDATA: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1418 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1419 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1420 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* modify the node value */ |
| 1424 | any = (struct lyd_node_any *)src_diff; |
| 1425 | LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type)); |
| 1426 | break; |
| 1427 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1428 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1429 | } |
| 1430 | break; |
| 1431 | case LYD_DIFF_OP_NONE: |
| 1432 | /* it is moved now */ |
| 1433 | assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST)); |
| 1434 | |
| 1435 | /* change the operation */ |
| 1436 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1437 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1438 | /* set orig-meta and meta */ |
| 1439 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1440 | meta_name = "position"; |
| 1441 | orig_meta_name = "orig-position"; |
| 1442 | } else { |
| 1443 | meta_name = "key"; |
| 1444 | orig_meta_name = "orig-key"; |
| 1445 | } |
| 1446 | |
| 1447 | meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1448 | 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] | 1449 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1450 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1451 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1452 | 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] | 1453 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1454 | break; |
| 1455 | default: |
| 1456 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1457 | LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE); |
| 1458 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | return LY_SUCCESS; |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * @brief Update operations in a diff node when the new operation is CREATE. |
| 1466 | * |
| 1467 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1468 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1469 | * @param[in] src_diff Current source diff node. |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1470 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1471 | * @return LY_ERR value. |
| 1472 | */ |
| 1473 | static LY_ERR |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1474 | 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] | 1475 | { |
| 1476 | struct lyd_node *child; |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1477 | const struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1478 | uint32_t trg_flags; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1479 | const char *meta_name, *orig_meta_name; |
| 1480 | struct lyd_meta *meta, *orig_meta; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1481 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1482 | |
| 1483 | switch (cur_op) { |
| 1484 | case LYD_DIFF_OP_DELETE: |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1485 | /* remember current flags */ |
| 1486 | trg_flags = diff_match->flags; |
| 1487 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1488 | if (lysc_is_userordered(diff_match->schema)) { |
| 1489 | /* get anchor metadata */ |
| 1490 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1491 | meta_name = "yang:position"; |
| 1492 | orig_meta_name = "yang:orig-position"; |
| 1493 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1494 | meta_name = "yang:key"; |
| 1495 | orig_meta_name = "yang:orig-key"; |
| 1496 | } else { |
| 1497 | meta_name = "yang:value"; |
| 1498 | orig_meta_name = "yang:orig-value"; |
| 1499 | } |
| 1500 | meta = lyd_find_meta(src_diff->meta, NULL, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1501 | 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] | 1502 | orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1503 | 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] | 1504 | |
| 1505 | /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect |
| 1506 | * the anchors stored in the metadata */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1507 | 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] | 1508 | /* deleted + created at another position -> operation REPLACE */ |
| 1509 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1510 | |
| 1511 | /* add anchor metadata */ |
| 1512 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
| 1513 | } else { |
| 1514 | /* deleted + created at the same position -> operation NONE */ |
| 1515 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1516 | |
| 1517 | /* delete anchor metadata */ |
| 1518 | lyd_free_meta_single(orig_meta); |
| 1519 | } |
| 1520 | } else if (diff_match->schema->nodetype == LYS_LEAF) { |
| 1521 | if (options & LYD_DIFF_MERGE_DEFAULTS) { |
| 1522 | /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */ |
| 1523 | sleaf = (struct lysc_node_leaf *)diff_match->schema; |
| 1524 | } |
| 1525 | |
Radek Krejci | 55c4bd2 | 2021-04-26 08:09:04 +0200 | [diff] [blame] | 1526 | if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt, |
| 1527 | &((struct lyd_node_term *)src_diff)->value)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1528 | /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */ |
| 1529 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1530 | } else if (!lyd_compare_single(diff_match, src_diff, 0)) { |
| 1531 | /* deleted + created -> operation NONE */ |
| 1532 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1533 | } else { |
| 1534 | /* we deleted it, but it was created with a different value -> operation REPLACE */ |
| 1535 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1536 | |
| 1537 | /* current value is the previous one (meta) */ |
| 1538 | 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] | 1539 | lyd_get_value(diff_match), 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1540 | |
| 1541 | /* update the value itself */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1542 | 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] | 1543 | } |
| 1544 | } else { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1545 | /* deleted + created -> operation NONE */ |
| 1546 | 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] | 1547 | } |
| 1548 | |
| 1549 | if (diff_match->schema->nodetype & LYD_NODE_TERM) { |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 1550 | /* add orig-dflt metadata */ |
| 1551 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default", |
| 1552 | trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
| 1553 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1554 | /* update dflt flag itself */ |
| 1555 | diff_match->flags &= ~LYD_DEFAULT; |
| 1556 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* but the operation of its children should remain DELETE */ |
| 1560 | LY_LIST_FOR(lyd_child_no_keys(diff_match), child) { |
| 1561 | LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1562 | } |
| 1563 | break; |
| 1564 | default: |
| 1565 | /* create and replace operations are not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1566 | LOGERR_MERGEOP(LYD_CTX(src_diff), diff_match, cur_op, LYD_DIFF_OP_CREATE); |
| 1567 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | return LY_SUCCESS; |
| 1571 | } |
| 1572 | |
| 1573 | /** |
| 1574 | * @brief Update operations on a diff node when the new operation is DELETE. |
| 1575 | * |
| 1576 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1577 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1578 | * @param[in] src_diff Current source diff node. |
| 1579 | * @return LY_ERR value. |
| 1580 | */ |
| 1581 | static LY_ERR |
| 1582 | lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1583 | { |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1584 | struct lyd_node *child; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1585 | struct lyd_meta *meta; |
| 1586 | const char *meta_name; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1587 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1588 | |
| 1589 | /* we can delete only exact existing nodes */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1590 | 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] | 1591 | |
| 1592 | switch (cur_op) { |
| 1593 | case LYD_DIFF_OP_CREATE: |
| 1594 | /* it was created, but then deleted -> set NONE operation */ |
| 1595 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1596 | |
| 1597 | if (diff_match->schema->nodetype & LYD_NODE_TERM) { |
| 1598 | /* add orig-default meta because it is expected */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1599 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default", |
| 1600 | diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1601 | } else if (!lysc_is_dup_inst_list(diff_match->schema)) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1602 | /* keep operation for all descendants (for now) */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1603 | LY_LIST_FOR(lyd_child_no_keys(diff_match), child) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1604 | LY_CHECK_RET(lyd_diff_change_op(child, cur_op)); |
| 1605 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1606 | } /* else key-less list, for which all the descendants act as keys */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1607 | break; |
| 1608 | case LYD_DIFF_OP_REPLACE: |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1609 | /* remove the redundant metadata */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1610 | if (lysc_is_userordered(diff_match->schema)) { |
| 1611 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1612 | meta_name = "position"; |
| 1613 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1614 | meta_name = "key"; |
| 1615 | } else { |
| 1616 | meta_name = "value"; |
| 1617 | } |
| 1618 | } else { |
| 1619 | assert(diff_match->schema->nodetype == LYS_LEAF); |
| 1620 | |
| 1621 | /* switch value for the original one */ |
| 1622 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1623 | 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] | 1624 | if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1625 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1626 | return LY_EINVAL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* switch default for the original one, then remove the meta */ |
| 1630 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1631 | 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] | 1632 | diff_match->flags &= ~LYD_DEFAULT; |
| 1633 | if (meta->value.boolean) { |
| 1634 | diff_match->flags |= LYD_DEFAULT; |
| 1635 | } |
| 1636 | lyd_free_meta_single(meta); |
| 1637 | |
| 1638 | meta_name = "orig-value"; |
| 1639 | } |
| 1640 | lyd_diff_del_meta(diff_match, meta_name); |
| 1641 | |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1642 | /* it was being changed, but should be deleted instead -> set DELETE operation */ |
| 1643 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE)); |
| 1644 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1645 | case LYD_DIFF_OP_NONE: |
| 1646 | /* it was not modified, but should be deleted -> set DELETE operation */ |
| 1647 | 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] | 1648 | break; |
| 1649 | default: |
| 1650 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1651 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE); |
| 1652 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | return LY_SUCCESS; |
| 1656 | } |
| 1657 | |
| 1658 | /** |
| 1659 | * @brief Check whether this diff node is redundant (does not change data). |
| 1660 | * |
| 1661 | * @param[in] diff Diff node. |
| 1662 | * @return 0 if not, non-zero if it is. |
| 1663 | */ |
| 1664 | static int |
| 1665 | lyd_diff_is_redundant(struct lyd_node *diff) |
| 1666 | { |
| 1667 | enum lyd_diff_op op; |
| 1668 | struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL; |
| 1669 | struct lyd_node *child; |
| 1670 | const struct lys_module *mod; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1671 | const char *str, *orig_meta_name, *meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1672 | |
| 1673 | assert(diff); |
| 1674 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1675 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1676 | /* all descendants are keys */ |
| 1677 | child = NULL; |
| 1678 | } else { |
| 1679 | child = lyd_child_no_keys(diff); |
| 1680 | } |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1681 | mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1682 | assert(mod); |
| 1683 | |
| 1684 | /* get node operation */ |
Michal Vasko | 53bf6f2 | 2020-07-14 08:23:40 +0200 | [diff] [blame] | 1685 | LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1686 | |
| 1687 | if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1688 | /* get metadata names */ |
| 1689 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1690 | meta_name = "position"; |
| 1691 | orig_meta_name = "orig-position"; |
| 1692 | } else if (diff->schema->nodetype == LYS_LIST) { |
| 1693 | meta_name = "key"; |
| 1694 | orig_meta_name = "orig-key"; |
| 1695 | } else { |
| 1696 | meta_name = "value"; |
| 1697 | orig_meta_name = "orig-value"; |
| 1698 | } |
| 1699 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1700 | /* check for redundant move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1701 | orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name); |
| 1702 | val_meta = lyd_find_meta(diff->meta, mod, meta_name); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1703 | assert(orig_val_meta && val_meta); |
| 1704 | |
| 1705 | if (!lyd_compare_meta(orig_val_meta, val_meta)) { |
| 1706 | /* there is actually no move */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1707 | lyd_free_meta_single(orig_val_meta); |
| 1708 | lyd_free_meta_single(val_meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1709 | if (child) { |
| 1710 | /* change operation to NONE, we have siblings */ |
| 1711 | lyd_diff_change_op(diff, LYD_DIFF_OP_NONE); |
| 1712 | return 0; |
| 1713 | } |
| 1714 | |
| 1715 | /* redundant node, BUT !! |
| 1716 | * In diff the move operation is always converted to be INSERT_AFTER, which is fine |
| 1717 | * because the data that this is applied on should not change for the diff lifetime. |
| 1718 | * However, when we are merging 2 diffs, this conversion is actually lossy because |
| 1719 | * if the data change, the move operation can also change its meaning. In this specific |
| 1720 | * case the move operation will be lost. But it can be considered a feature, it is not supported. |
| 1721 | */ |
| 1722 | return 1; |
| 1723 | } |
| 1724 | } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) { |
| 1725 | /* check whether at least the default flags are different */ |
| 1726 | meta = lyd_find_meta(diff->meta, mod, "orig-default"); |
| 1727 | assert(meta); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1728 | str = lyd_get_meta_value(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1729 | |
| 1730 | /* if previous and current dflt flags are the same, this node is redundant */ |
| 1731 | if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) { |
| 1732 | return 1; |
| 1733 | } |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | if (!child && (op == LYD_DIFF_OP_NONE)) { |
| 1738 | return 1; |
| 1739 | } |
| 1740 | |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | /** |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1745 | * @brief Merge sysrepo diff subtree with another diff, recursively. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1746 | * |
| 1747 | * @param[in] src_diff Source diff node. |
| 1748 | * @param[in] diff_parent Current sysrepo diff parent. |
| 1749 | * @param[in] diff_cb Optional diff callback. |
| 1750 | * @param[in] cb_data User data for @p diff_cb. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1751 | * @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] | 1752 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1753 | * @param[in,out] diff Diff root node. |
| 1754 | * @return LY_ERR value. |
| 1755 | */ |
| 1756 | static LY_ERR |
| 1757 | 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] | 1758 | 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] | 1759 | { |
| 1760 | LY_ERR ret = LY_SUCCESS; |
| 1761 | struct lyd_node *child, *diff_node = NULL; |
| 1762 | enum lyd_diff_op src_op, cur_op; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1763 | struct lyd_dup_inst *child_dup_inst = NULL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1764 | |
| 1765 | /* get source node operation */ |
| 1766 | LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op)); |
| 1767 | |
| 1768 | /* find an equal node in the current diff */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1769 | 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] | 1770 | |
| 1771 | if (diff_node) { |
| 1772 | /* get target (current) operation */ |
| 1773 | LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op)); |
| 1774 | |
| 1775 | /* merge operations */ |
| 1776 | switch (src_op) { |
| 1777 | case LYD_DIFF_OP_REPLACE: |
| 1778 | ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff); |
| 1779 | break; |
| 1780 | case LYD_DIFF_OP_CREATE: |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1781 | 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] | 1782 | /* special case of creating duplicate (leaf-)list instances */ |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1783 | goto add_diff; |
| 1784 | } |
| 1785 | |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1786 | ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1787 | break; |
| 1788 | case LYD_DIFF_OP_DELETE: |
| 1789 | ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff); |
| 1790 | break; |
| 1791 | case LYD_DIFF_OP_NONE: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1792 | /* key-less list can never have "none" operation since all its descendants are acting as "keys" */ |
| 1793 | 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] | 1794 | ret = lyd_diff_merge_none(diff_node, cur_op, src_diff); |
| 1795 | break; |
| 1796 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1797 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1798 | } |
| 1799 | if (ret) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1800 | 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] | 1801 | return ret; |
| 1802 | } |
| 1803 | |
| 1804 | if (diff_cb) { |
| 1805 | /* call callback */ |
Michal Vasko | bc5fba9 | 2020-08-07 12:14:39 +0200 | [diff] [blame] | 1806 | LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /* update diff parent */ |
| 1810 | diff_parent = diff_node; |
| 1811 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1812 | /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances), |
| 1813 | * so there is nothing to merge for these "keys" */ |
| 1814 | if (!lysc_is_dup_inst_list(src_diff->schema)) { |
| 1815 | /* merge src_diff recursively */ |
| 1816 | LY_LIST_FOR(lyd_child_no_keys(src_diff), child) { |
| 1817 | ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff); |
| 1818 | if (ret) { |
| 1819 | break; |
| 1820 | } |
| 1821 | } |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1822 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1823 | LY_CHECK_RET(ret); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1824 | } |
| 1825 | } else { |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1826 | add_diff: |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1827 | /* add new diff node with all descendants */ |
Michal Vasko | 9ad7685 | 2022-07-12 10:18:03 +0200 | [diff] [blame] | 1828 | if ((src_diff->flags & LYD_EXT) && diff_parent) { |
| 1829 | LY_CHECK_RET(lyd_dup_single_to_ctx(src_diff, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent, |
| 1830 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 1831 | } else { |
| 1832 | LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, |
| 1833 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 1834 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1835 | |
| 1836 | /* insert node into diff if not already */ |
| 1837 | if (!diff_parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1838 | lyd_insert_sibling(*diff, diff_node, diff); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | /* update operation */ |
| 1842 | LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op)); |
| 1843 | |
| 1844 | if (diff_cb) { |
Michal Vasko | e2af841 | 2020-12-03 14:11:38 +0100 | [diff] [blame] | 1845 | /* call callback with no source diff node since it was duplicated and just added */ |
| 1846 | LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | /* update diff parent */ |
| 1850 | diff_parent = diff_node; |
| 1851 | } |
| 1852 | |
| 1853 | /* remove any redundant nodes */ |
Michal Vasko | b98d708 | 2020-07-15 16:38:36 +0200 | [diff] [blame] | 1854 | if (lyd_diff_is_redundant(diff_parent)) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1855 | if (diff_parent == *diff) { |
| 1856 | *diff = (*diff)->next; |
| 1857 | } |
| 1858 | lyd_free_tree(diff_parent); |
| 1859 | } |
| 1860 | |
| 1861 | return LY_SUCCESS; |
| 1862 | } |
| 1863 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1864 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 1865 | 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] | 1866 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1867 | { |
| 1868 | const struct lyd_node *src_root; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1869 | struct lyd_dup_inst *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1870 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1871 | |
| 1872 | LY_LIST_FOR(src_diff, src_root) { |
| 1873 | if (mod && (lyd_owner_module(src_root) != mod)) { |
| 1874 | /* skip data nodes from different modules */ |
| 1875 | continue; |
| 1876 | } |
| 1877 | |
| 1878 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1879 | 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] | 1880 | } |
| 1881 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1882 | cleanup: |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1883 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1884 | return ret; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1885 | } |
| 1886 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1887 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1888 | 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] | 1889 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1890 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1891 | LY_ERR ret; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1892 | struct lyd_dup_inst *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1893 | |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1894 | if (!src_sibling) { |
| 1895 | return LY_SUCCESS; |
| 1896 | } |
| 1897 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1898 | 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] | 1899 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1900 | return ret; |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1901 | } |
| 1902 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1903 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1904 | 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] | 1905 | { |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1906 | return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1907 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1908 | |
| 1909 | static LY_ERR |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1910 | 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] | 1911 | { |
| 1912 | LY_ERR ret = LY_SUCCESS; |
| 1913 | struct lyd_meta *meta; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1914 | const char *val1 = NULL; |
| 1915 | char *val2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1916 | uint32_t flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1917 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1918 | assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA)); |
| 1919 | |
| 1920 | meta = lyd_find_meta(node->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1921 | 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] | 1922 | |
| 1923 | /* orig-value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1924 | val1 = lyd_get_meta_value(meta); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1925 | |
| 1926 | /* current value */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1927 | if (node->schema->nodetype == LYS_LEAF) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1928 | val2 = strdup(lyd_get_value(node)); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1929 | } else { |
| 1930 | LY_CHECK_RET(lyd_any_value_str(node, &val2)); |
| 1931 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1932 | |
| 1933 | /* switch values, keep default flag */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1934 | flags = node->flags; |
| 1935 | if (node->schema->nodetype == LYS_LEAF) { |
| 1936 | LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup); |
| 1937 | } else { |
| 1938 | union lyd_any_value anyval = {.str = val1}; |
| 1939 | LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup); |
| 1940 | } |
| 1941 | node->flags = flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1942 | LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup); |
| 1943 | |
| 1944 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1945 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1946 | return ret; |
| 1947 | } |
| 1948 | |
| 1949 | static LY_ERR |
| 1950 | lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod) |
| 1951 | { |
| 1952 | struct lyd_meta *meta; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1953 | uint32_t flag1, flag2; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1954 | |
| 1955 | meta = lyd_find_meta(node->meta, mod, "orig-default"); |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 1956 | LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1957 | |
| 1958 | /* orig-default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1959 | if (meta->value.boolean) { |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1960 | flag1 = LYD_DEFAULT; |
| 1961 | } else { |
| 1962 | flag1 = 0; |
| 1963 | } |
| 1964 | |
| 1965 | /* current default */ |
| 1966 | flag2 = node->flags & LYD_DEFAULT; |
| 1967 | |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 1968 | if (flag1 == flag2) { |
| 1969 | /* no default state change so nothing to reverse */ |
| 1970 | return LY_SUCCESS; |
| 1971 | } |
| 1972 | |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1973 | /* switch defaults */ |
| 1974 | node->flags &= ~LYD_DEFAULT; |
| 1975 | node->flags |= flag1; |
| 1976 | LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false")); |
| 1977 | |
| 1978 | return LY_SUCCESS; |
| 1979 | } |
| 1980 | |
| 1981 | static LY_ERR |
| 1982 | lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2) |
| 1983 | { |
| 1984 | LY_ERR ret = LY_SUCCESS; |
| 1985 | struct lyd_meta *meta1, *meta2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1986 | const char *val1 = NULL; |
| 1987 | char *val2 = NULL; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1988 | |
| 1989 | meta1 = lyd_find_meta(node->meta, mod, name1); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1990 | 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] | 1991 | |
| 1992 | meta2 = lyd_find_meta(node->meta, mod, name2); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1993 | 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] | 1994 | |
| 1995 | /* value1 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1996 | val1 = lyd_get_meta_value(meta1); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* value2 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1999 | val2 = strdup(lyd_get_meta_value(meta2)); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2000 | |
| 2001 | /* switch values */ |
| 2002 | LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup); |
| 2003 | LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup); |
| 2004 | |
| 2005 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2006 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2007 | return ret; |
| 2008 | } |
| 2009 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2010 | /** |
| 2011 | * @brief Remove specific operation from all the nodes in a subtree. |
| 2012 | * |
| 2013 | * @param[in] diff Diff subtree to process. |
| 2014 | * @param[in] op Only expected operation. |
| 2015 | * @return LY_ERR value. |
| 2016 | */ |
| 2017 | static LY_ERR |
| 2018 | lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op) |
| 2019 | { |
| 2020 | struct lyd_node *elem; |
| 2021 | struct lyd_meta *meta; |
| 2022 | |
| 2023 | LYD_TREE_DFS_BEGIN(diff, elem) { |
| 2024 | meta = lyd_find_meta(elem->meta, NULL, "yang:operation"); |
| 2025 | if (meta) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2026 | 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] | 2027 | lyd_free_meta_single(meta); |
| 2028 | } |
| 2029 | |
| 2030 | LYD_TREE_DFS_END(diff, elem); |
| 2031 | } |
| 2032 | |
| 2033 | return LY_SUCCESS; |
| 2034 | } |
| 2035 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2036 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6653581 | 2020-08-11 08:44:22 +0200 | [diff] [blame] | 2037 | 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] | 2038 | { |
| 2039 | LY_ERR ret = LY_SUCCESS; |
| 2040 | const struct lys_module *mod; |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2041 | struct lyd_node *root, *elem, *iter; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2042 | enum lyd_diff_op op; |
| 2043 | |
| 2044 | LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL); |
| 2045 | |
| 2046 | if (!src_diff) { |
| 2047 | *diff = NULL; |
| 2048 | return LY_SUCCESS; |
| 2049 | } |
| 2050 | |
| 2051 | /* duplicate diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2052 | 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] | 2053 | |
| 2054 | /* find module with metadata needed for later */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2055 | mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang"); |
| 2056 | 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] | 2057 | |
| 2058 | LY_LIST_FOR(*diff, root) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2059 | LYD_TREE_DFS_BEGIN(root, elem) { |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2060 | /* skip all keys */ |
| 2061 | if (!lysc_is_key(elem->schema)) { |
| 2062 | /* find operation attribute, if any */ |
| 2063 | LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2064 | |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2065 | switch (op) { |
| 2066 | case LYD_DIFF_OP_CREATE: |
| 2067 | /* reverse create to delete */ |
| 2068 | 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] | 2069 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2070 | /* check all the children for the same operation, nothing else is expected */ |
| 2071 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2072 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE); |
| 2073 | } |
| 2074 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2075 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2076 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2077 | case LYD_DIFF_OP_DELETE: |
| 2078 | /* reverse delete to create */ |
| 2079 | 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] | 2080 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2081 | /* check all the children for the same operation, nothing else is expected */ |
| 2082 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2083 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE); |
| 2084 | } |
| 2085 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2086 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2087 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2088 | case LYD_DIFF_OP_REPLACE: |
| 2089 | switch (elem->schema->nodetype) { |
| 2090 | case LYS_LEAF: |
| 2091 | /* leaf value change */ |
| 2092 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2093 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2094 | break; |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2095 | case LYS_ANYXML: |
| 2096 | case LYS_ANYDATA: |
| 2097 | /* any value change */ |
| 2098 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2099 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2100 | case LYS_LEAFLIST: |
| 2101 | /* leaf-list move */ |
| 2102 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2103 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2104 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2105 | } else { |
| 2106 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup); |
| 2107 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2108 | break; |
| 2109 | case LYS_LIST: |
| 2110 | /* list move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2111 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2112 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2113 | } else { |
| 2114 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup); |
| 2115 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2116 | break; |
| 2117 | default: |
| 2118 | LOGINT(LYD_CTX(src_diff)); |
| 2119 | ret = LY_EINT; |
| 2120 | goto cleanup; |
| 2121 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2122 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2123 | case LYD_DIFF_OP_NONE: |
| 2124 | switch (elem->schema->nodetype) { |
| 2125 | case LYS_LEAF: |
| 2126 | case LYS_LEAFLIST: |
| 2127 | /* default flag change */ |
| 2128 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2129 | break; |
| 2130 | default: |
| 2131 | /* nothing to do */ |
| 2132 | break; |
| 2133 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2134 | break; |
| 2135 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2138 | LYD_TREE_DFS_END(root, elem); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | cleanup: |
| 2143 | if (ret) { |
| 2144 | lyd_free_siblings(*diff); |
| 2145 | *diff = NULL; |
| 2146 | } |
| 2147 | return ret; |
| 2148 | } |