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