Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 20 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func revisionsAreEqual(a, b []Revision) bool { |
| 24 | // If one is nil, the other must also be nil. |
| 25 | if (a == nil) != (b == nil) { |
| 26 | return false |
| 27 | } |
| 28 | |
| 29 | if len(a) != len(b) { |
| 30 | return false |
| 31 | } |
| 32 | |
| 33 | for i := range a { |
| 34 | if a[i] != b[i] { |
| 35 | return false |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return true |
| 40 | } |
| 41 | |
| 42 | type changeAnalysis struct { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 43 | KeyMap1 map[string]int |
| 44 | KeyMap2 map[string]int |
| 45 | AddedKeys map[string]struct{} |
| 46 | RemovedKeys map[string]struct{} |
| 47 | ChangedKeys map[string]struct{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func newChangeAnalysis(lst1, lst2 []Revision, keyName string) *changeAnalysis { |
| 51 | changes := &changeAnalysis{} |
| 52 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 53 | changes.KeyMap1 = make(map[string]int) |
| 54 | changes.KeyMap2 = make(map[string]int) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 55 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 56 | changes.AddedKeys = make(map[string]struct{}) |
| 57 | changes.RemovedKeys = make(map[string]struct{}) |
| 58 | changes.ChangedKeys = make(map[string]struct{}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 59 | |
| 60 | for i, rev := range lst1 { |
| 61 | _, v := GetAttributeValue(rev.GetData(), keyName, 0) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 62 | changes.KeyMap1[v.String()] = i |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 63 | } |
| 64 | for i, rev := range lst2 { |
| 65 | _, v := GetAttributeValue(rev.GetData(), keyName, 0) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 66 | changes.KeyMap2[v.String()] = i |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 67 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 68 | for v := range changes.KeyMap2 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 69 | if _, ok := changes.KeyMap1[v]; !ok { |
| 70 | changes.AddedKeys[v] = struct{}{} |
| 71 | } |
| 72 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 73 | for v := range changes.KeyMap1 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 74 | if _, ok := changes.KeyMap2[v]; !ok { |
| 75 | changes.RemovedKeys[v] = struct{}{} |
| 76 | } |
| 77 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 78 | for v := range changes.KeyMap1 { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 79 | if _, ok := changes.KeyMap2[v]; ok && lst1[changes.KeyMap1[v]].GetHash() != lst2[changes.KeyMap2[v]].GetHash() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 80 | changes.ChangedKeys[v] = struct{}{} |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return changes |
| 85 | } |
| 86 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 87 | // Merge3Way takes care of combining the revision contents of the same data set |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 88 | func Merge3Way( |
| 89 | forkRev, srcRev, dstRev Revision, |
| 90 | mergeChildFunc func(Revision) Revision, |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 91 | dryRun bool) (rev Revision, changes []ChangeTuple) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 92 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 93 | log.Debugw("3-way-merge-request", log.Fields{"dryRun": dryRun}) |
| 94 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 95 | var configChanged bool |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 96 | var revsToDiscard []Revision |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 97 | |
| 98 | if dstRev.GetConfig() == forkRev.GetConfig() { |
| 99 | configChanged = dstRev.GetConfig() != srcRev.GetConfig() |
| 100 | } else { |
| 101 | if dstRev.GetConfig().Hash != srcRev.GetConfig().Hash { |
| 102 | log.Error("config-collision") |
| 103 | } |
| 104 | configChanged = true |
| 105 | } |
| 106 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 107 | //newChildren := reflect.ValueOf(dstRev.GetAllChildren()).Interface().(map[string][]Revision) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 108 | newChildren := make(map[string][]Revision) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 109 | for entryName, childrenEntry := range dstRev.GetAllChildren() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 110 | //newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 111 | newChildren[entryName] = make([]Revision, len(childrenEntry)) |
| 112 | copy(newChildren[entryName], childrenEntry) |
| 113 | } |
| 114 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 115 | childrenFields := ChildrenFields(forkRev.GetData()) |
| 116 | |
| 117 | for fieldName, field := range childrenFields { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 118 | forkList := forkRev.GetChildren(fieldName) |
| 119 | srcList := srcRev.GetChildren(fieldName) |
| 120 | dstList := dstRev.GetChildren(fieldName) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 121 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 122 | if revisionsAreEqual(dstList, srcList) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 123 | for _, rev := range srcList { |
| 124 | mergeChildFunc(rev) |
| 125 | } |
| 126 | continue |
| 127 | } |
| 128 | |
| 129 | if field.Key == "" { |
| 130 | if revisionsAreEqual(dstList, forkList) { |
| 131 | if !revisionsAreEqual(srcList, forkList) { |
| 132 | log.Error("we should not be here") |
| 133 | } else { |
| 134 | for _, rev := range srcList { |
| 135 | newChildren[fieldName] = append(newChildren[fieldName], mergeChildFunc(rev)) |
| 136 | } |
| 137 | if field.IsContainer { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 138 | changes = append( |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 139 | changes, ChangeTuple{PostListchange, |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 140 | NewOperationContext("", nil, fieldName, ""), nil}, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 141 | ) |
| 142 | } |
| 143 | } |
| 144 | } else { |
| 145 | if !revisionsAreEqual(srcList, forkList) { |
| 146 | log.Error("cannot merge - single child node or un-keyed children list has changed") |
| 147 | } |
| 148 | } |
| 149 | } else { |
| 150 | if revisionsAreEqual(dstList, forkList) { |
| 151 | src := newChangeAnalysis(forkList, srcList, field.Key) |
| 152 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 153 | newList := make([]Revision, len(srcList)) |
| 154 | copy(newList, srcList) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 155 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 156 | for key := range src.AddedKeys { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 157 | idx := src.KeyMap2[key] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 158 | newRev := mergeChildFunc(newList[idx]) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 159 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 160 | // FIXME: newRev may come back as nil... exclude those entries for now |
| 161 | if newRev != nil { |
| 162 | newList[idx] = newRev |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 163 | changes = append(changes, ChangeTuple{PostAdd, newList[idx].GetData(), newRev.GetData()}) |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 164 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 165 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 166 | for key := range src.RemovedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 167 | oldRev := forkList[src.KeyMap1[key]] |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 168 | revsToDiscard = append(revsToDiscard, oldRev) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 169 | changes = append(changes, ChangeTuple{PostRemove, oldRev.GetData(), nil}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 170 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 171 | for key := range src.ChangedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 172 | idx := src.KeyMap2[key] |
| 173 | newRev := mergeChildFunc(newList[idx]) |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 174 | |
| 175 | // FIXME: newRev may come back as nil... exclude those entries for now |
| 176 | if newRev != nil { |
| 177 | newList[idx] = newRev |
| 178 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 181 | if !dryRun { |
| 182 | newChildren[fieldName] = newList |
| 183 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 184 | } else { |
| 185 | src := newChangeAnalysis(forkList, srcList, field.Key) |
| 186 | dst := newChangeAnalysis(forkList, dstList, field.Key) |
| 187 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 188 | newList := make([]Revision, len(dstList)) |
| 189 | copy(newList, dstList) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 190 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 191 | for key := range src.AddedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 192 | if _, exists := dst.AddedKeys[key]; exists { |
| 193 | childDstRev := dstList[dst.KeyMap2[key]] |
| 194 | childSrcRev := srcList[src.KeyMap2[key]] |
| 195 | if childDstRev.GetHash() == childSrcRev.GetHash() { |
| 196 | mergeChildFunc(childDstRev) |
| 197 | } else { |
| 198 | log.Error("conflict error - revision has been added is different") |
| 199 | } |
| 200 | } else { |
| 201 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 202 | newList = append(newList, newRev) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 203 | changes = append(changes, ChangeTuple{PostAdd, srcList[src.KeyMap2[key]], newRev.GetData()}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 206 | for key := range src.ChangedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 207 | if _, removed := dst.RemovedKeys[key]; removed { |
| 208 | log.Error("conflict error - revision has been removed") |
| 209 | } else if _, changed := dst.ChangedKeys[key]; changed { |
| 210 | childDstRev := dstList[dst.KeyMap2[key]] |
| 211 | childSrcRev := srcList[src.KeyMap2[key]] |
| 212 | if childDstRev.GetHash() == childSrcRev.GetHash() { |
| 213 | mergeChildFunc(childSrcRev) |
| 214 | } else if childDstRev.GetConfig().Hash != childSrcRev.GetConfig().Hash { |
| 215 | log.Error("conflict error - revision has been changed and is different") |
| 216 | } else { |
| 217 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 218 | newList[dst.KeyMap2[key]] = newRev |
| 219 | } |
| 220 | } else { |
| 221 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 222 | newList[dst.KeyMap2[key]] = newRev |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // TODO: how do i sort this map in reverse order? |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 227 | for key := range src.RemovedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 228 | if _, changed := dst.ChangedKeys[key]; changed { |
| 229 | log.Error("conflict error - revision has changed") |
| 230 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 231 | if _, removed := dst.RemovedKeys[key]; !removed { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 232 | dstIdx := dst.KeyMap2[key] |
| 233 | oldRev := newList[dstIdx] |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 234 | revsToDiscard = append(revsToDiscard, oldRev) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 235 | |
| 236 | copy(newList[dstIdx:], newList[dstIdx+1:]) |
| 237 | newList[len(newList)-1] = nil |
| 238 | newList = newList[:len(newList)-1] |
| 239 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 240 | changes = append(changes, ChangeTuple{PostRemove, oldRev.GetData(), nil}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 244 | if !dryRun { |
| 245 | newChildren[fieldName] = newList |
| 246 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 251 | if !dryRun && len(newChildren) > 0 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 252 | if configChanged { |
| 253 | rev = srcRev |
| 254 | } else { |
| 255 | rev = dstRev |
| 256 | } |
| 257 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 258 | for _, discarded := range revsToDiscard { |
| 259 | discarded.Drop("", true) |
| 260 | } |
| 261 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 262 | // FIXME: Do not discard the latest value for now |
| 263 | //dstRev.GetBranch().GetLatest().Drop("", configChanged) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 264 | rev = rev.UpdateAllChildren(newChildren, dstRev.GetBranch()) |
| 265 | |
| 266 | if configChanged { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 267 | changes = append(changes, ChangeTuple{PostUpdate, dstRev.GetBranch().GetLatest().GetData(), rev.GetData()}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 268 | } |
| 269 | return rev, changes |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 270 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 271 | |
| 272 | return nil, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 273 | } |