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