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 | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/common/log" |
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 |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 60 | return r |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func (npr *NonPersistedRevision) SetConfig(config *DataRevision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 64 | npr.mutex.Lock() |
| 65 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 66 | npr.Config = config |
| 67 | } |
| 68 | |
| 69 | func (npr *NonPersistedRevision) GetConfig() *DataRevision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 70 | npr.mutex.Lock() |
| 71 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 72 | return npr.Config |
| 73 | } |
| 74 | |
| 75 | func (npr *NonPersistedRevision) SetChildren(children map[string][]Revision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 76 | npr.mutex.Lock() |
| 77 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 78 | npr.Children = children |
| 79 | } |
| 80 | |
| 81 | func (npr *NonPersistedRevision) GetChildren() map[string][]Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 82 | npr.mutex.Lock() |
| 83 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 84 | return npr.Children |
| 85 | } |
| 86 | |
| 87 | func (npr *NonPersistedRevision) SetHash(hash string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 88 | npr.mutex.Lock() |
| 89 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 90 | npr.Hash = hash |
| 91 | } |
| 92 | |
| 93 | func (npr *NonPersistedRevision) GetHash() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 94 | //npr.mutex.Lock() |
| 95 | //defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 96 | return npr.Hash |
| 97 | } |
| 98 | |
| 99 | func (npr *NonPersistedRevision) ClearHash() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 100 | npr.mutex.Lock() |
| 101 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 102 | npr.Hash = "" |
| 103 | } |
| 104 | |
| 105 | func (npr *NonPersistedRevision) SetBranch(branch *Branch) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 106 | npr.mutex.Lock() |
| 107 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 108 | npr.Branch = branch |
| 109 | } |
| 110 | |
| 111 | func (npr *NonPersistedRevision) GetBranch() *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 112 | npr.mutex.Lock() |
| 113 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 114 | return npr.Branch |
| 115 | } |
| 116 | |
| 117 | func (npr *NonPersistedRevision) GetData() interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 118 | npr.mutex.Lock() |
| 119 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 120 | if npr.Config == nil { |
| 121 | return nil |
| 122 | } |
| 123 | return npr.Config.Data |
| 124 | } |
| 125 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 126 | func (npr *NonPersistedRevision) GetNode() *node { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 127 | npr.mutex.Lock() |
| 128 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 129 | return npr.Branch.Node |
| 130 | } |
| 131 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 132 | func (npr *NonPersistedRevision) Finalize(skipOnExist bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 133 | GetRevCache().Lock() |
| 134 | defer GetRevCache().Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 135 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 136 | npr.Hash = npr.hashContent() |
| 137 | |
| 138 | if _, exists := GetRevCache().Cache[npr.Hash]; !exists { |
| 139 | GetRevCache().Cache[npr.Hash] = npr |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 140 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 141 | if _, exists := GetRevCache().Cache[npr.Config.Hash]; !exists { |
| 142 | GetRevCache().Cache[npr.Config.Hash] = npr.Config |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 143 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 144 | npr.Config = GetRevCache().Cache[npr.Config.Hash].(*DataRevision) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | func (npr *NonPersistedRevision) hashContent() string { |
| 149 | var buffer bytes.Buffer |
| 150 | var childrenKeys []string |
| 151 | |
| 152 | if npr.Config != nil { |
| 153 | buffer.WriteString(npr.Config.Hash) |
| 154 | } |
| 155 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 156 | for key := range npr.Children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 157 | childrenKeys = append(childrenKeys, key) |
| 158 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 159 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 160 | sort.Strings(childrenKeys) |
| 161 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 162 | if len(npr.Children) > 0 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 163 | // Loop through sorted Children keys |
| 164 | for _, key := range childrenKeys { |
| 165 | for _, child := range npr.Children[key] { |
| 166 | if child != nil && child.GetHash() != "" { |
| 167 | buffer.WriteString(child.GetHash()) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return fmt.Sprintf("%x", md5.Sum(buffer.Bytes()))[:12] |
| 174 | } |
| 175 | |
| 176 | func (npr *NonPersistedRevision) Get(depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 177 | // 1. Clone the data to avoid any concurrent access issues |
| 178 | // 2. The current rev might still be pointing to an old config |
| 179 | // thus, force the revision to get its latest value |
| 180 | latestRev := npr.GetBranch().GetLatest() |
| 181 | originalData := proto.Clone(latestRev.GetData().(proto.Message)) |
| 182 | |
| 183 | data := originalData |
| 184 | // Get back to the interface type |
| 185 | //data := reflect.ValueOf(originalData).Interface() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 186 | |
| 187 | if depth != 0 { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 188 | for fieldName, field := range ChildrenFields(latestRev.GetData()) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 189 | childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0) |
| 190 | if field.IsContainer { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 191 | for _, rev := range latestRev.GetChildren()[fieldName] { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 192 | childData := rev.Get(depth - 1) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 193 | foundEntry := false |
| 194 | for i := 0; i < childDataHolder.Len(); i++ { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 195 | cdh_if := childDataHolder.Index(i).Interface() |
| 196 | if cdh_if.(proto.Message).String() == childData.(proto.Message).String() { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 197 | foundEntry = true |
| 198 | break |
| 199 | } |
| 200 | } |
| 201 | if !foundEntry { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 202 | // 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] | 203 | childDataHolder = reflect.Append(childDataHolder, reflect.ValueOf(childData)) |
| 204 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 205 | } |
| 206 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 207 | if revs := latestRev.GetChildren()[fieldName]; revs != nil && len(revs) > 0 { |
| 208 | rev := latestRev.GetChildren()[fieldName][0] |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 209 | if rev != nil { |
| 210 | childData := rev.Get(depth - 1) |
| 211 | if reflect.TypeOf(childData) == reflect.TypeOf(childDataHolder.Interface()) { |
| 212 | childDataHolder = reflect.ValueOf(childData) |
| 213 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 214 | } |
| 215 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 216 | } |
| 217 | // Merge child data with cloned object |
| 218 | reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder) |
| 219 | } |
| 220 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 221 | |
| 222 | result := data |
| 223 | |
| 224 | if result != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 225 | // We need to send back a copy of the retrieved object |
| 226 | result = proto.Clone(data.(proto.Message)) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return result |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | func (npr *NonPersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 233 | npr.mutex.Lock() |
| 234 | defer npr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 235 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 236 | if npr.Config.Data != nil && npr.Config.Data == data { |
| 237 | log.Debugw("stored-data-matches-latest", log.Fields{"stored": npr.Config.Data, "provided": data}) |
| 238 | return nil |
| 239 | } |
| 240 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 241 | newRev := NonPersistedRevision{} |
| 242 | newRev.Config = NewDataRevision(npr.Root, data) |
| 243 | newRev.Hash = npr.Hash |
| 244 | newRev.Branch = branch |
| 245 | |
| 246 | newRev.Children = make(map[string][]Revision) |
| 247 | for entryName, childrenEntry := range npr.Children { |
| 248 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 249 | } |
| 250 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 251 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 252 | |
| 253 | return &newRev |
| 254 | } |
| 255 | |
| 256 | func (npr *NonPersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 257 | npr.mutex.Lock() |
| 258 | defer npr.mutex.Unlock() |
| 259 | |
| 260 | updatedRev := npr |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 261 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 262 | // Verify if the map contains already contains an entry matching the name value |
| 263 | // If so, we need to retain the contents of that entry and merge them with the provided children revision list |
| 264 | if _, exists := updatedRev.Children[name]; exists { |
| 265 | // Go through all child hashes and save their index within the map |
| 266 | existChildMap := make(map[string]int) |
| 267 | for i, child := range updatedRev.Children[name] { |
| 268 | existChildMap[child.GetHash()] = i |
| 269 | } |
| 270 | |
| 271 | // Identify the revisions that are not present in the existing list and add them |
| 272 | for _, newChild := range children { |
| 273 | if _, childExists := existChildMap[newChild.GetHash()]; !childExists { |
| 274 | updatedRev.Children[name] = append(updatedRev.Children[name], newChild) |
| 275 | } |
| 276 | } |
| 277 | } else { |
| 278 | // Map entry does not exist, thus just create a new entry and assign the provided revisions |
| 279 | updatedRev.Children[name] = make([]Revision, len(children)) |
| 280 | copy(updatedRev.Children[name], children) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 281 | } |
| 282 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 283 | updatedRev.Config = NewDataRevision(npr.Root, npr.Config.Data) |
| 284 | updatedRev.Hash = npr.Hash |
| 285 | updatedRev.Branch = branch |
| 286 | updatedRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 287 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 288 | return updatedRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | func (npr *NonPersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 292 | npr.mutex.Lock() |
| 293 | defer npr.mutex.Unlock() |
| 294 | |
| 295 | newRev := &NonPersistedRevision{} |
| 296 | newRev.Config = npr.Config |
| 297 | newRev.Hash = npr.Hash |
| 298 | newRev.Branch = branch |
| 299 | newRev.Children = make(map[string][]Revision) |
| 300 | for entryName, childrenEntry := range npr.Children { |
| 301 | newRev.Children[entryName] = make([]Revision, len(childrenEntry)) |
| 302 | copy(newRev.Children[entryName], childrenEntry) |
| 303 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 304 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 305 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 306 | return newRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 307 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 308 | |
| 309 | func (npr *NonPersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 310 | GetRevCache().Lock() |
| 311 | defer GetRevCache().Unlock() |
| 312 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 313 | if includeConfig { |
| 314 | delete(GetRevCache().Cache, npr.Config.Hash) |
| 315 | } |
| 316 | delete(GetRevCache().Cache, npr.Hash) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 317 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 318 | |
| 319 | func (npr *NonPersistedRevision) LoadFromPersistence(path string, txid string) []Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 320 | // stub... required by interface |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 321 | return nil |
| 322 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 323 | |
| 324 | func (npr *NonPersistedRevision) SetupWatch(key string) { |
| 325 | // stub ... required by interface |
| 326 | } |