Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 | "context" |
| 21 | "crypto/md5" |
| 22 | "fmt" |
| 23 | "github.com/golang/protobuf/proto" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/pkg/db/kvstore" |
Scott Baker | e73f91e | 2019-10-17 12:58:11 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 26 | "reflect" |
| 27 | "sort" |
Stephane Barbarie | 42b97a4 | 2019-10-23 11:05:04 -0400 | [diff] [blame] | 28 | "strings" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 29 | "sync" |
| 30 | "time" |
| 31 | ) |
| 32 | |
| 33 | // TODO: Cache logic will have to be revisited to cleanup unused entries in memory (disabled for now) |
| 34 | // |
| 35 | type revCacheSingleton struct { |
| 36 | sync.RWMutex |
| 37 | Cache sync.Map |
| 38 | } |
| 39 | |
| 40 | func (s *revCacheSingleton) Get(path string) (interface{}, bool) { |
| 41 | return s.Cache.Load(path) |
| 42 | } |
| 43 | func (s *revCacheSingleton) Set(path string, value interface{}) { |
| 44 | s.Cache.Store(path, value) |
| 45 | } |
| 46 | func (s *revCacheSingleton) Delete(path string) { |
| 47 | s.Cache.Delete(path) |
| 48 | } |
| 49 | |
| 50 | var revCacheInstance *revCacheSingleton |
| 51 | var revCacheOnce sync.Once |
| 52 | |
| 53 | func GetRevCache() *revCacheSingleton { |
| 54 | revCacheOnce.Do(func() { |
| 55 | revCacheInstance = &revCacheSingleton{Cache: sync.Map{}} |
| 56 | }) |
| 57 | return revCacheInstance |
| 58 | } |
| 59 | |
| 60 | type NonPersistedRevision struct { |
| 61 | mutex sync.RWMutex |
| 62 | Root *root |
| 63 | Config *DataRevision |
| 64 | childrenLock sync.RWMutex |
| 65 | Children map[string][]Revision |
| 66 | Hash string |
| 67 | Branch *Branch |
| 68 | WeakRef string |
| 69 | Name string |
| 70 | lastUpdate time.Time |
| 71 | } |
| 72 | |
| 73 | func NewNonPersistedRevision(root *root, branch *Branch, data interface{}, children map[string][]Revision) Revision { |
| 74 | r := &NonPersistedRevision{} |
| 75 | r.Root = root |
| 76 | r.Branch = branch |
| 77 | r.Config = NewDataRevision(root, data) |
| 78 | r.Children = children |
| 79 | r.Hash = r.hashContent() |
| 80 | return r |
| 81 | } |
| 82 | |
| 83 | func (npr *NonPersistedRevision) SetConfig(config *DataRevision) { |
| 84 | npr.mutex.Lock() |
| 85 | defer npr.mutex.Unlock() |
| 86 | npr.Config = config |
| 87 | } |
| 88 | |
| 89 | func (npr *NonPersistedRevision) GetConfig() *DataRevision { |
| 90 | npr.mutex.Lock() |
| 91 | defer npr.mutex.Unlock() |
| 92 | return npr.Config |
| 93 | } |
| 94 | |
| 95 | func (npr *NonPersistedRevision) SetAllChildren(children map[string][]Revision) { |
| 96 | npr.childrenLock.Lock() |
| 97 | defer npr.childrenLock.Unlock() |
| 98 | npr.Children = make(map[string][]Revision) |
| 99 | |
| 100 | for key, value := range children { |
| 101 | npr.Children[key] = make([]Revision, len(value)) |
| 102 | copy(npr.Children[key], value) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func (npr *NonPersistedRevision) SetChildren(name string, children []Revision) { |
| 107 | npr.childrenLock.Lock() |
| 108 | defer npr.childrenLock.Unlock() |
| 109 | |
| 110 | npr.Children[name] = make([]Revision, len(children)) |
| 111 | copy(npr.Children[name], children) |
| 112 | } |
| 113 | |
| 114 | func (npr *NonPersistedRevision) GetAllChildren() map[string][]Revision { |
| 115 | npr.childrenLock.Lock() |
| 116 | defer npr.childrenLock.Unlock() |
| 117 | |
| 118 | return npr.Children |
| 119 | } |
| 120 | |
| 121 | func (npr *NonPersistedRevision) GetChildren(name string) []Revision { |
| 122 | npr.childrenLock.Lock() |
| 123 | defer npr.childrenLock.Unlock() |
| 124 | |
| 125 | if _, exists := npr.Children[name]; exists { |
| 126 | return npr.Children[name] |
| 127 | } |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | func (npr *NonPersistedRevision) SetHash(hash string) { |
| 132 | npr.mutex.Lock() |
| 133 | defer npr.mutex.Unlock() |
| 134 | npr.Hash = hash |
| 135 | } |
| 136 | |
| 137 | func (npr *NonPersistedRevision) GetHash() string { |
| 138 | //npr.mutex.Lock() |
| 139 | //defer npr.mutex.Unlock() |
| 140 | return npr.Hash |
| 141 | } |
| 142 | |
| 143 | func (npr *NonPersistedRevision) ClearHash() { |
| 144 | npr.mutex.Lock() |
| 145 | defer npr.mutex.Unlock() |
| 146 | npr.Hash = "" |
| 147 | } |
| 148 | |
| 149 | func (npr *NonPersistedRevision) GetName() string { |
| 150 | //npr.mutex.Lock() |
| 151 | //defer npr.mutex.Unlock() |
| 152 | return npr.Name |
| 153 | } |
| 154 | |
| 155 | func (npr *NonPersistedRevision) SetName(name string) { |
| 156 | //npr.mutex.Lock() |
| 157 | //defer npr.mutex.Unlock() |
| 158 | npr.Name = name |
| 159 | } |
| 160 | func (npr *NonPersistedRevision) SetBranch(branch *Branch) { |
| 161 | npr.mutex.Lock() |
| 162 | defer npr.mutex.Unlock() |
| 163 | npr.Branch = branch |
| 164 | } |
| 165 | |
| 166 | func (npr *NonPersistedRevision) GetBranch() *Branch { |
| 167 | npr.mutex.Lock() |
| 168 | defer npr.mutex.Unlock() |
| 169 | return npr.Branch |
| 170 | } |
| 171 | |
| 172 | func (npr *NonPersistedRevision) GetData() interface{} { |
| 173 | npr.mutex.Lock() |
| 174 | defer npr.mutex.Unlock() |
| 175 | if npr.Config == nil { |
| 176 | return nil |
| 177 | } |
| 178 | return npr.Config.Data |
| 179 | } |
| 180 | |
| 181 | func (npr *NonPersistedRevision) GetNode() *node { |
| 182 | npr.mutex.Lock() |
| 183 | defer npr.mutex.Unlock() |
| 184 | return npr.Branch.Node |
| 185 | } |
| 186 | |
| 187 | func (npr *NonPersistedRevision) Finalize(skipOnExist bool) { |
| 188 | npr.Hash = npr.hashContent() |
| 189 | } |
| 190 | |
| 191 | // hashContent generates a hash string based on the contents of the revision. |
| 192 | // The string should be unique to avoid conflicts with other revisions |
| 193 | func (npr *NonPersistedRevision) hashContent() string { |
| 194 | var buffer bytes.Buffer |
| 195 | var childrenKeys []string |
| 196 | |
| 197 | if npr.Config != nil { |
| 198 | buffer.WriteString(npr.Config.Hash) |
| 199 | } |
| 200 | |
| 201 | if npr.Name != "" { |
| 202 | buffer.WriteString(npr.Name) |
| 203 | } |
| 204 | |
| 205 | for key := range npr.Children { |
| 206 | childrenKeys = append(childrenKeys, key) |
| 207 | } |
| 208 | |
| 209 | sort.Strings(childrenKeys) |
| 210 | |
| 211 | if len(npr.Children) > 0 { |
| 212 | // Loop through sorted Children keys |
| 213 | for _, key := range childrenKeys { |
| 214 | for _, child := range npr.Children[key] { |
| 215 | if child != nil && child.GetHash() != "" { |
| 216 | buffer.WriteString(child.GetHash()) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return fmt.Sprintf("%x", md5.Sum(buffer.Bytes()))[:12] |
| 223 | } |
| 224 | |
| 225 | // Get will retrieve the data for the current revision |
| 226 | func (npr *NonPersistedRevision) Get(depth int) interface{} { |
| 227 | // 1. Clone the data to avoid any concurrent access issues |
| 228 | // 2. The current rev might still be pointing to an old config |
| 229 | // thus, force the revision to get its latest value |
| 230 | latestRev := npr.GetBranch().GetLatest() |
| 231 | originalData := proto.Clone(latestRev.GetData().(proto.Message)) |
| 232 | data := originalData |
| 233 | |
| 234 | if depth != 0 { |
| 235 | // FIXME: Traversing the struct through reflection sometimes corrupts the data. |
| 236 | // Unlike the original python implementation, golang structs are not lazy loaded. |
| 237 | // Keeping this non-critical logic for now, but Get operations should be forced to |
| 238 | // depth=0 to avoid going through the following loop. |
| 239 | for fieldName, field := range ChildrenFields(latestRev.GetData()) { |
| 240 | childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0) |
| 241 | if field.IsContainer { |
| 242 | for _, rev := range latestRev.GetChildren(fieldName) { |
| 243 | childData := rev.Get(depth - 1) |
| 244 | foundEntry := false |
| 245 | for i := 0; i < childDataHolder.Len(); i++ { |
| 246 | cdh_if := childDataHolder.Index(i).Interface() |
| 247 | if cdh_if.(proto.Message).String() == childData.(proto.Message).String() { |
| 248 | foundEntry = true |
| 249 | break |
| 250 | } |
| 251 | } |
| 252 | if !foundEntry { |
| 253 | // avoid duplicates by adding it only if the child was not found in the holder |
| 254 | childDataHolder = reflect.Append(childDataHolder, reflect.ValueOf(childData)) |
| 255 | } |
| 256 | } |
| 257 | } else { |
| 258 | if revs := npr.GetBranch().GetLatest().GetChildren(fieldName); revs != nil && len(revs) > 0 { |
| 259 | rev := revs[0] |
| 260 | if rev != nil { |
| 261 | childData := rev.Get(depth - 1) |
| 262 | if reflect.TypeOf(childData) == reflect.TypeOf(childDataHolder.Interface()) { |
| 263 | childDataHolder = reflect.ValueOf(childData) |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | // Merge child data with cloned object |
| 269 | reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | result := data |
| 274 | |
| 275 | if result != nil { |
| 276 | // We need to send back a copy of the retrieved object |
| 277 | result = proto.Clone(data.(proto.Message)) |
| 278 | } |
| 279 | |
| 280 | return result |
| 281 | } |
| 282 | |
| 283 | // UpdateData will refresh the data content of the revision |
| 284 | func (npr *NonPersistedRevision) UpdateData(ctx context.Context, data interface{}, branch *Branch) Revision { |
| 285 | npr.mutex.Lock() |
| 286 | defer npr.mutex.Unlock() |
| 287 | |
Stephane Barbarie | 42b97a4 | 2019-10-23 11:05:04 -0400 | [diff] [blame] | 288 | log.Debugw("update-data", log.Fields{"hash": npr.GetHash(), "current": npr.Config.Data, "provided": data}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 289 | |
| 290 | // Do not update the revision if data is the same |
| 291 | if npr.Config.Data != nil && npr.Config.hashData(npr.Root, data) == npr.Config.Hash { |
| 292 | log.Debugw("stored-data-matches-latest", log.Fields{"stored": npr.Config.Data, "provided": data}) |
| 293 | return npr |
| 294 | } |
| 295 | |
| 296 | // Construct a new revision based on the current one |
| 297 | newRev := NonPersistedRevision{} |
| 298 | newRev.Config = NewDataRevision(npr.Root, data) |
| 299 | newRev.Hash = npr.Hash |
| 300 | newRev.Root = npr.Root |
| 301 | newRev.Name = npr.Name |
| 302 | newRev.Branch = branch |
| 303 | newRev.lastUpdate = npr.lastUpdate |
| 304 | |
| 305 | newRev.Children = make(map[string][]Revision) |
| 306 | for entryName, childrenEntry := range branch.GetLatest().GetAllChildren() { |
| 307 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 308 | } |
| 309 | |
| 310 | newRev.Finalize(false) |
| 311 | |
Stephane Barbarie | 42b97a4 | 2019-10-23 11:05:04 -0400 | [diff] [blame] | 312 | log.Debugw("update-data-complete", log.Fields{"updated": newRev.Config.Data, "provided": data}) |
| 313 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 314 | return &newRev |
| 315 | } |
| 316 | |
| 317 | // UpdateChildren will refresh the list of children with the provided ones |
| 318 | // It will carefully go through the list and ensure that no child is lost |
| 319 | func (npr *NonPersistedRevision) UpdateChildren(ctx context.Context, name string, children []Revision, branch *Branch) Revision { |
| 320 | npr.mutex.Lock() |
| 321 | defer npr.mutex.Unlock() |
| 322 | |
| 323 | // Construct a new revision based on the current one |
| 324 | updatedRev := &NonPersistedRevision{} |
| 325 | updatedRev.Config = NewDataRevision(npr.Root, npr.Config.Data) |
| 326 | updatedRev.Hash = npr.Hash |
| 327 | updatedRev.Branch = branch |
| 328 | updatedRev.Name = npr.Name |
| 329 | updatedRev.lastUpdate = npr.lastUpdate |
| 330 | |
| 331 | updatedRev.Children = make(map[string][]Revision) |
| 332 | for entryName, childrenEntry := range branch.GetLatest().GetAllChildren() { |
| 333 | updatedRev.Children[entryName] = append(updatedRev.Children[entryName], childrenEntry...) |
| 334 | } |
| 335 | |
| 336 | var updatedChildren []Revision |
| 337 | |
| 338 | // Verify if the map contains already contains an entry matching the name value |
| 339 | // If so, we need to retain the contents of that entry and merge them with the provided children revision list |
| 340 | if existingChildren := branch.GetLatest().GetChildren(name); existingChildren != nil { |
| 341 | // Construct a map of unique child names with the respective index value |
| 342 | // for the children in the existing revision as well as the new ones |
| 343 | existingNames := make(map[string]int) |
| 344 | newNames := make(map[string]int) |
| 345 | |
| 346 | for i, newChild := range children { |
| 347 | newNames[newChild.GetName()] = i |
| 348 | } |
| 349 | |
| 350 | for i, existingChild := range existingChildren { |
| 351 | existingNames[existingChild.GetName()] = i |
| 352 | |
| 353 | // If an existing entry is not in the new list, add it to the updated list, so it is not forgotten |
| 354 | if _, exists := newNames[existingChild.GetName()]; !exists { |
| 355 | updatedChildren = append(updatedChildren, existingChild) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | log.Debugw("existing-children-names", log.Fields{"hash": npr.GetHash(), "names": existingNames}) |
| 360 | |
| 361 | // Merge existing and new children |
| 362 | for _, newChild := range children { |
| 363 | nameIndex, nameExists := existingNames[newChild.GetName()] |
| 364 | |
| 365 | // Does the existing list contain a child with that name? |
| 366 | if nameExists { |
| 367 | // Check if the data has changed or not |
| 368 | if existingChildren[nameIndex].GetData().(proto.Message).String() != newChild.GetData().(proto.Message).String() { |
| 369 | log.Debugw("replacing-existing-child", log.Fields{ |
| 370 | "old-hash": existingChildren[nameIndex].GetHash(), |
| 371 | "old-data": existingChildren[nameIndex].GetData(), |
| 372 | "new-hash": newChild.GetHash(), |
| 373 | "new-data": newChild.GetData(), |
| 374 | }) |
| 375 | |
| 376 | // replace entry |
| 377 | newChild.GetNode().SetRoot(existingChildren[nameIndex].GetNode().GetRoot()) |
| 378 | updatedChildren = append(updatedChildren, newChild) |
| 379 | } else { |
| 380 | log.Debugw("keeping-existing-child", log.Fields{ |
| 381 | "old-hash": existingChildren[nameIndex].GetHash(), |
| 382 | "old-data": existingChildren[nameIndex].GetData(), |
| 383 | "new-hash": newChild.GetHash(), |
| 384 | "new-data": newChild.GetData(), |
| 385 | }) |
| 386 | |
| 387 | // keep existing entry |
| 388 | updatedChildren = append(updatedChildren, existingChildren[nameIndex]) |
| 389 | } |
| 390 | } else { |
| 391 | log.Debugw("adding-unknown-child", log.Fields{ |
| 392 | "hash": newChild.GetHash(), |
| 393 | "data": newChild.GetData(), |
| 394 | }) |
| 395 | |
| 396 | // new entry ... just add it |
| 397 | updatedChildren = append(updatedChildren, newChild) |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Save children in new revision |
| 402 | updatedRev.SetChildren(name, updatedChildren) |
| 403 | |
| 404 | updatedNames := make(map[string]int) |
| 405 | for i, updatedChild := range updatedChildren { |
| 406 | updatedNames[updatedChild.GetName()] = i |
| 407 | } |
| 408 | |
| 409 | log.Debugw("updated-children-names", log.Fields{"hash": npr.GetHash(), "names": updatedNames}) |
| 410 | |
| 411 | } else { |
| 412 | // There are no children available, just save the provided ones |
| 413 | updatedRev.SetChildren(name, children) |
| 414 | } |
| 415 | |
| 416 | updatedRev.Finalize(false) |
| 417 | |
| 418 | return updatedRev |
| 419 | } |
| 420 | |
| 421 | // UpdateAllChildren will replace the current list of children with the provided ones |
| 422 | func (npr *NonPersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
| 423 | npr.mutex.Lock() |
| 424 | defer npr.mutex.Unlock() |
| 425 | |
| 426 | newRev := npr |
| 427 | newRev.Config = npr.Config |
| 428 | newRev.Hash = npr.Hash |
| 429 | newRev.Branch = branch |
| 430 | newRev.Name = npr.Name |
| 431 | newRev.lastUpdate = npr.lastUpdate |
| 432 | |
| 433 | newRev.Children = make(map[string][]Revision) |
| 434 | for entryName, childrenEntry := range children { |
| 435 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 436 | } |
| 437 | newRev.Finalize(false) |
| 438 | |
| 439 | return newRev |
| 440 | } |
| 441 | |
| 442 | // Drop is used to indicate when a revision is no longer required |
| 443 | func (npr *NonPersistedRevision) Drop(txid string, includeConfig bool) { |
| 444 | log.Debugw("dropping-revision", log.Fields{"hash": npr.GetHash(), "name": npr.GetName()}) |
| 445 | } |
| 446 | |
| 447 | // ChildDrop will remove a child entry matching the provided parameters from the current revision |
| 448 | func (npr *NonPersistedRevision) ChildDrop(childType string, childHash string) { |
| 449 | if childType != "" { |
| 450 | children := make([]Revision, len(npr.GetChildren(childType))) |
| 451 | copy(children, npr.GetChildren(childType)) |
| 452 | for i, child := range children { |
| 453 | if child.GetHash() == childHash { |
| 454 | children = append(children[:i], children[i+1:]...) |
| 455 | npr.SetChildren(childType, children) |
| 456 | break |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
Stephane Barbarie | 42b97a4 | 2019-10-23 11:05:04 -0400 | [diff] [blame] | 462 | /// ChildDropByName will remove a child entry matching the type and name |
| 463 | func (npr *NonPersistedRevision) ChildDropByName(childName string) { |
| 464 | // Extract device type |
| 465 | parts := strings.SplitN(childName, "/", 2) |
| 466 | childType := parts[0] |
| 467 | |
| 468 | if childType != "" { |
| 469 | children := make([]Revision, len(npr.GetChildren(childType))) |
| 470 | copy(children, npr.GetChildren(childType)) |
| 471 | for i, child := range children { |
| 472 | if child.GetName() == childName { |
| 473 | children = append(children[:i], children[i+1:]...) |
| 474 | npr.SetChildren(childType, children) |
| 475 | break |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 481 | func (npr *NonPersistedRevision) SetLastUpdate(ts ...time.Time) { |
| 482 | npr.mutex.Lock() |
| 483 | defer npr.mutex.Unlock() |
| 484 | |
| 485 | if ts != nil && len(ts) > 0 { |
| 486 | npr.lastUpdate = ts[0] |
| 487 | } else { |
| 488 | npr.lastUpdate = time.Now() |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | func (npr *NonPersistedRevision) GetLastUpdate() time.Time { |
| 493 | npr.mutex.RLock() |
| 494 | defer npr.mutex.RUnlock() |
| 495 | |
| 496 | return npr.lastUpdate |
| 497 | } |
| 498 | |
| 499 | func (npr *NonPersistedRevision) LoadFromPersistence(ctx context.Context, path string, txid string, blobs map[string]*kvstore.KVPair) []Revision { |
| 500 | // stub... required by interface |
| 501 | return nil |
| 502 | } |
| 503 | |
| 504 | func (npr *NonPersistedRevision) SetupWatch(key string) { |
| 505 | // stub ... required by interface |
| 506 | } |
| 507 | |
| 508 | func (npr *NonPersistedRevision) StorageDrop(txid string, includeConfig bool) { |
| 509 | // stub ... required by interface |
| 510 | } |
| 511 | |
| 512 | func (npr *NonPersistedRevision) getVersion() int64 { |
| 513 | return -1 |
| 514 | } |