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