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