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