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