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 | "bytes" |
| 20 | "crypto/md5" |
| 21 | "fmt" |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 23 | "reflect" |
| 24 | "sort" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 25 | "sync" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 28 | type revCacheSingleton struct { |
| 29 | sync.RWMutex |
| 30 | Cache map[string]interface{} |
| 31 | } |
| 32 | |
| 33 | var revCacheInstance *revCacheSingleton |
| 34 | var revCacheOnce sync.Once |
| 35 | |
| 36 | func GetRevCache() *revCacheSingleton { |
| 37 | revCacheOnce.Do(func() { |
| 38 | revCacheInstance = &revCacheSingleton{Cache: make(map[string]interface{})} |
| 39 | }) |
| 40 | return revCacheInstance |
| 41 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 42 | |
| 43 | type NonPersistedRevision struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 44 | mutex sync.RWMutex |
| 45 | Root *root |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 46 | Config *DataRevision |
| 47 | Children map[string][]Revision |
| 48 | Hash string |
| 49 | Branch *Branch |
| 50 | WeakRef string |
| 51 | } |
| 52 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 53 | func NewNonPersistedRevision(root *root, branch *Branch, data interface{}, children map[string][]Revision) Revision { |
| 54 | r := &NonPersistedRevision{} |
| 55 | r.Root = root |
| 56 | r.Branch = branch |
| 57 | r.Config = NewDataRevision(root, data) |
| 58 | r.Children = children |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 59 | return r |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func (npr *NonPersistedRevision) SetConfig(config *DataRevision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 63 | npr.mutex.Lock() |
| 64 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 65 | npr.Config = config |
| 66 | } |
| 67 | |
| 68 | func (npr *NonPersistedRevision) GetConfig() *DataRevision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 69 | npr.mutex.Lock() |
| 70 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 71 | return npr.Config |
| 72 | } |
| 73 | |
| 74 | func (npr *NonPersistedRevision) SetChildren(children map[string][]Revision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 75 | npr.mutex.Lock() |
| 76 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 77 | npr.Children = children |
| 78 | } |
| 79 | |
| 80 | func (npr *NonPersistedRevision) GetChildren() map[string][]Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 81 | npr.mutex.Lock() |
| 82 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 83 | return npr.Children |
| 84 | } |
| 85 | |
| 86 | func (npr *NonPersistedRevision) SetHash(hash string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 87 | npr.mutex.Lock() |
| 88 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 89 | npr.Hash = hash |
| 90 | } |
| 91 | |
| 92 | func (npr *NonPersistedRevision) GetHash() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 93 | //npr.mutex.Lock() |
| 94 | //defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 95 | return npr.Hash |
| 96 | } |
| 97 | |
| 98 | func (npr *NonPersistedRevision) ClearHash() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 99 | npr.mutex.Lock() |
| 100 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 101 | npr.Hash = "" |
| 102 | } |
| 103 | |
| 104 | func (npr *NonPersistedRevision) SetBranch(branch *Branch) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 105 | npr.mutex.Lock() |
| 106 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 107 | npr.Branch = branch |
| 108 | } |
| 109 | |
| 110 | func (npr *NonPersistedRevision) GetBranch() *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 111 | npr.mutex.Lock() |
| 112 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 113 | return npr.Branch |
| 114 | } |
| 115 | |
| 116 | func (npr *NonPersistedRevision) GetData() interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 117 | npr.mutex.Lock() |
| 118 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 119 | if npr.Config == nil { |
| 120 | return nil |
| 121 | } |
| 122 | return npr.Config.Data |
| 123 | } |
| 124 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 125 | func (npr *NonPersistedRevision) GetNode() *node { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 126 | npr.mutex.Lock() |
| 127 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 128 | return npr.Branch.Node |
| 129 | } |
| 130 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 131 | func (npr *NonPersistedRevision) Finalize(skipOnExist bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 132 | GetRevCache().Lock() |
| 133 | defer GetRevCache().Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 134 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 135 | npr.Hash = npr.hashContent() |
| 136 | |
| 137 | if _, exists := GetRevCache().Cache[npr.Hash]; !exists { |
| 138 | GetRevCache().Cache[npr.Hash] = npr |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 139 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 140 | if _, exists := GetRevCache().Cache[npr.Config.Hash]; !exists { |
| 141 | GetRevCache().Cache[npr.Config.Hash] = npr.Config |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 142 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 143 | npr.Config = GetRevCache().Cache[npr.Config.Hash].(*DataRevision) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | func (npr *NonPersistedRevision) hashContent() string { |
| 148 | var buffer bytes.Buffer |
| 149 | var childrenKeys []string |
| 150 | |
| 151 | if npr.Config != nil { |
| 152 | buffer.WriteString(npr.Config.Hash) |
| 153 | } |
| 154 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 155 | for key := range npr.Children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 156 | childrenKeys = append(childrenKeys, key) |
| 157 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 158 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 159 | sort.Strings(childrenKeys) |
| 160 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 161 | if len(npr.Children) > 0 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 162 | // Loop through sorted Children keys |
| 163 | for _, key := range childrenKeys { |
| 164 | for _, child := range npr.Children[key] { |
| 165 | if child != nil && child.GetHash() != "" { |
| 166 | buffer.WriteString(child.GetHash()) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return fmt.Sprintf("%x", md5.Sum(buffer.Bytes()))[:12] |
| 173 | } |
| 174 | |
| 175 | func (npr *NonPersistedRevision) Get(depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 176 | // 1. Clone the data to avoid any concurrent access issues |
| 177 | // 2. The current rev might still be pointing to an old config |
| 178 | // thus, force the revision to get its latest value |
| 179 | latestRev := npr.GetBranch().GetLatest() |
| 180 | originalData := proto.Clone(latestRev.GetData().(proto.Message)) |
| 181 | |
| 182 | data := originalData |
| 183 | // Get back to the interface type |
| 184 | //data := reflect.ValueOf(originalData).Interface() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 185 | |
| 186 | if depth != 0 { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 187 | for fieldName, field := range ChildrenFields(latestRev.GetData()) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 188 | childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0) |
| 189 | if field.IsContainer { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 190 | for _, rev := range latestRev.GetChildren()[fieldName] { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 191 | childData := rev.Get(depth - 1) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 192 | foundEntry := false |
| 193 | for i := 0; i < childDataHolder.Len(); i++ { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 194 | cdh_if := childDataHolder.Index(i).Interface() |
| 195 | if cdh_if.(proto.Message).String() == childData.(proto.Message).String() { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 196 | foundEntry = true |
| 197 | break |
| 198 | } |
| 199 | } |
| 200 | if !foundEntry { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 201 | // avoid duplicates by adding it only if the child was not found in the holder |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 202 | childDataHolder = reflect.Append(childDataHolder, reflect.ValueOf(childData)) |
| 203 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 204 | } |
| 205 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 206 | if revs := latestRev.GetChildren()[fieldName]; revs != nil && len(revs) > 0 { |
| 207 | rev := latestRev.GetChildren()[fieldName][0] |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 208 | if rev != nil { |
| 209 | childData := rev.Get(depth - 1) |
| 210 | if reflect.TypeOf(childData) == reflect.TypeOf(childDataHolder.Interface()) { |
| 211 | childDataHolder = reflect.ValueOf(childData) |
| 212 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 215 | } |
| 216 | // Merge child data with cloned object |
| 217 | reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder) |
| 218 | } |
| 219 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 220 | |
| 221 | result := data |
| 222 | |
| 223 | if result != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 224 | // We need to send back a copy of the retrieved object |
| 225 | result = proto.Clone(data.(proto.Message)) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return result |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | func (npr *NonPersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 232 | npr.mutex.Lock() |
| 233 | defer npr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 234 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 235 | newRev := NonPersistedRevision{} |
| 236 | newRev.Config = NewDataRevision(npr.Root, data) |
| 237 | newRev.Hash = npr.Hash |
| 238 | newRev.Branch = branch |
| 239 | |
| 240 | newRev.Children = make(map[string][]Revision) |
| 241 | for entryName, childrenEntry := range npr.Children { |
| 242 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 243 | } |
| 244 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 245 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 246 | |
| 247 | return &newRev |
| 248 | } |
| 249 | |
| 250 | func (npr *NonPersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 251 | npr.mutex.Lock() |
| 252 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 253 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 254 | newRev := NonPersistedRevision{} |
| 255 | newRev.Children = make(map[string][]Revision) |
| 256 | for entryName, childrenEntry := range npr.Children { |
| 257 | newRev.Children[entryName] = make([]Revision, len(childrenEntry)) |
| 258 | copy(newRev.Children[entryName], childrenEntry) |
| 259 | } |
| 260 | |
| 261 | newRev.Children[name] = make([]Revision, len(children)) |
| 262 | copy(newRev.Children[name], children) |
| 263 | |
| 264 | newRev.Config = NewDataRevision(npr.Root, npr.Config.Data) |
| 265 | newRev.Hash = npr.Hash |
| 266 | newRev.Branch = branch |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 267 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 268 | |
| 269 | return &newRev |
| 270 | } |
| 271 | |
| 272 | func (npr *NonPersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 273 | npr.mutex.Lock() |
| 274 | defer npr.mutex.Unlock() |
| 275 | |
| 276 | newRev := &NonPersistedRevision{} |
| 277 | newRev.Config = npr.Config |
| 278 | newRev.Hash = npr.Hash |
| 279 | newRev.Branch = branch |
| 280 | newRev.Children = make(map[string][]Revision) |
| 281 | for entryName, childrenEntry := range npr.Children { |
| 282 | newRev.Children[entryName] = make([]Revision, len(childrenEntry)) |
| 283 | copy(newRev.Children[entryName], childrenEntry) |
| 284 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 285 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 286 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 287 | return newRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 288 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 289 | |
| 290 | func (npr *NonPersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 291 | GetRevCache().Lock() |
| 292 | defer GetRevCache().Unlock() |
| 293 | |
| 294 | npr.mutex.Lock() |
| 295 | defer npr.mutex.Unlock() |
| 296 | |
| 297 | if includeConfig { |
| 298 | delete(GetRevCache().Cache, npr.Config.Hash) |
| 299 | } |
| 300 | delete(GetRevCache().Cache, npr.Hash) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 301 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 302 | |
| 303 | func (npr *NonPersistedRevision) LoadFromPersistence(path string, txid string) []Revision { |
| 304 | return nil |
| 305 | } |