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