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 ( |
| 20 | "github.com/opencord/voltha-go/common/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 | |
| 93 | var configChanged bool |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 94 | var revsToDiscard []Revision |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 95 | |
| 96 | if dstRev.GetConfig() == forkRev.GetConfig() { |
| 97 | configChanged = dstRev.GetConfig() != srcRev.GetConfig() |
| 98 | } else { |
| 99 | if dstRev.GetConfig().Hash != srcRev.GetConfig().Hash { |
| 100 | log.Error("config-collision") |
| 101 | } |
| 102 | configChanged = true |
| 103 | } |
| 104 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 105 | //newChildren := reflect.ValueOf(dstRev.GetAllChildren()).Interface().(map[string][]Revision) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 106 | newChildren := make(map[string][]Revision) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 107 | for entryName, childrenEntry := range dstRev.GetAllChildren() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 108 | //newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 109 | newChildren[entryName] = make([]Revision, len(childrenEntry)) |
| 110 | copy(newChildren[entryName], childrenEntry) |
| 111 | } |
| 112 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 113 | childrenFields := ChildrenFields(forkRev.GetData()) |
| 114 | |
| 115 | for fieldName, field := range childrenFields { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 116 | forkList := forkRev.GetChildren(fieldName) |
| 117 | srcList := srcRev.GetChildren(fieldName) |
| 118 | dstList := dstRev.GetChildren(fieldName) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 119 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 120 | if revisionsAreEqual(dstList, srcList) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 121 | for _, rev := range srcList { |
| 122 | mergeChildFunc(rev) |
| 123 | } |
| 124 | continue |
| 125 | } |
| 126 | |
| 127 | if field.Key == "" { |
| 128 | if revisionsAreEqual(dstList, forkList) { |
| 129 | if !revisionsAreEqual(srcList, forkList) { |
| 130 | log.Error("we should not be here") |
| 131 | } else { |
| 132 | for _, rev := range srcList { |
| 133 | newChildren[fieldName] = append(newChildren[fieldName], mergeChildFunc(rev)) |
| 134 | } |
| 135 | if field.IsContainer { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 136 | changes = append( |
| 137 | changes, ChangeTuple{POST_LISTCHANGE, |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 138 | NewOperationContext("", nil, fieldName, ""), nil}, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 139 | ) |
| 140 | } |
| 141 | } |
| 142 | } else { |
| 143 | if !revisionsAreEqual(srcList, forkList) { |
| 144 | log.Error("cannot merge - single child node or un-keyed children list has changed") |
| 145 | } |
| 146 | } |
| 147 | } else { |
| 148 | if revisionsAreEqual(dstList, forkList) { |
| 149 | src := newChangeAnalysis(forkList, srcList, field.Key) |
| 150 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 151 | newList := make([]Revision, len(srcList)) |
| 152 | copy(newList, srcList) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 153 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 154 | for key := range src.AddedKeys { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 155 | idx := src.KeyMap2[key] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 156 | newRev := mergeChildFunc(newList[idx]) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 157 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 158 | // FIXME: newRev may come back as nil... exclude those entries for now |
| 159 | if newRev != nil { |
| 160 | newList[idx] = newRev |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 161 | changes = append(changes, ChangeTuple{POST_ADD, newList[idx].GetData(), newRev.GetData()}) |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 162 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 163 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 164 | for key := range src.RemovedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 165 | oldRev := forkList[src.KeyMap1[key]] |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 166 | revsToDiscard = append(revsToDiscard, oldRev) |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 167 | changes = append(changes, ChangeTuple{POST_REMOVE, oldRev.GetData(), nil}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 168 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 169 | for key := range src.ChangedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 170 | idx := src.KeyMap2[key] |
| 171 | newRev := mergeChildFunc(newList[idx]) |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 172 | |
| 173 | // FIXME: newRev may come back as nil... exclude those entries for now |
| 174 | if newRev != nil { |
| 175 | newList[idx] = newRev |
| 176 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 179 | if !dryRun { |
| 180 | newChildren[fieldName] = newList |
| 181 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 182 | } else { |
| 183 | src := newChangeAnalysis(forkList, srcList, field.Key) |
| 184 | dst := newChangeAnalysis(forkList, dstList, field.Key) |
| 185 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 186 | newList := make([]Revision, len(dstList)) |
| 187 | copy(newList, dstList) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 188 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 189 | for key := range src.AddedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 190 | if _, exists := dst.AddedKeys[key]; exists { |
| 191 | childDstRev := dstList[dst.KeyMap2[key]] |
| 192 | childSrcRev := srcList[src.KeyMap2[key]] |
| 193 | if childDstRev.GetHash() == childSrcRev.GetHash() { |
| 194 | mergeChildFunc(childDstRev) |
| 195 | } else { |
| 196 | log.Error("conflict error - revision has been added is different") |
| 197 | } |
| 198 | } else { |
| 199 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 200 | newList = append(newList, newRev) |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 201 | changes = append(changes, ChangeTuple{POST_ADD, srcList[src.KeyMap2[key]], newRev.GetData()}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 204 | for key := range src.ChangedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 205 | if _, removed := dst.RemovedKeys[key]; removed { |
| 206 | log.Error("conflict error - revision has been removed") |
| 207 | } else if _, changed := dst.ChangedKeys[key]; changed { |
| 208 | childDstRev := dstList[dst.KeyMap2[key]] |
| 209 | childSrcRev := srcList[src.KeyMap2[key]] |
| 210 | if childDstRev.GetHash() == childSrcRev.GetHash() { |
| 211 | mergeChildFunc(childSrcRev) |
| 212 | } else if childDstRev.GetConfig().Hash != childSrcRev.GetConfig().Hash { |
| 213 | log.Error("conflict error - revision has been changed and is different") |
| 214 | } else { |
| 215 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 216 | newList[dst.KeyMap2[key]] = newRev |
| 217 | } |
| 218 | } else { |
| 219 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 220 | newList[dst.KeyMap2[key]] = newRev |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // TODO: how do i sort this map in reverse order? |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 225 | for key := range src.RemovedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 226 | if _, changed := dst.ChangedKeys[key]; changed { |
| 227 | log.Error("conflict error - revision has changed") |
| 228 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 229 | if _, removed := dst.RemovedKeys[key]; !removed { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 230 | dstIdx := dst.KeyMap2[key] |
| 231 | oldRev := newList[dstIdx] |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 232 | revsToDiscard = append(revsToDiscard, oldRev) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 233 | |
| 234 | copy(newList[dstIdx:], newList[dstIdx+1:]) |
| 235 | newList[len(newList)-1] = nil |
| 236 | newList = newList[:len(newList)-1] |
| 237 | |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 238 | changes = append(changes, ChangeTuple{POST_REMOVE, oldRev.GetData(), nil}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 242 | if !dryRun { |
| 243 | newChildren[fieldName] = newList |
| 244 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 249 | if !dryRun && len(newChildren) > 0{ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 250 | if configChanged { |
| 251 | rev = srcRev |
| 252 | } else { |
| 253 | rev = dstRev |
| 254 | } |
| 255 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 256 | for _, discarded := range revsToDiscard { |
| 257 | discarded.Drop("", true) |
| 258 | } |
| 259 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 260 | dstRev.GetBranch().GetLatest().Drop("", configChanged) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 261 | rev = rev.UpdateAllChildren(newChildren, dstRev.GetBranch()) |
| 262 | |
| 263 | if configChanged { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 264 | // FIXME: what type of previous/latest data do we want to show? Specific node or Root |
| 265 | changes = append(changes, ChangeTuple{POST_UPDATE, dstRev.GetBranch().GetLatest().GetData(), rev.GetData()}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 266 | } |
| 267 | return rev, changes |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 268 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 269 | |
| 270 | return nil, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 271 | } |