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