khenaidoo | bf6e7bb | 2018-08-14 22:27: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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 19 | import ( |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 20 | "sync" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 21 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 23 | ) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 24 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | // TODO: implement weak references or something equivalent |
| 26 | // TODO: missing proper logging |
| 27 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 28 | // Branch structure is used to classify a collection of transaction based revisions |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 29 | type Branch struct { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 30 | mutex sync.RWMutex |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 31 | Node *node |
| 32 | Txid string |
| 33 | Origin Revision |
| 34 | Revisions map[string]Revision |
| 35 | LatestLock sync.RWMutex |
| 36 | Latest Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 39 | // NewBranch creates a new instance of the Branch structure |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 40 | func NewBranch(node *node, txid string, origin Revision, autoPrune bool) *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 41 | b := &Branch{} |
| 42 | b.Node = node |
| 43 | b.Txid = txid |
| 44 | b.Origin = origin |
| 45 | b.Revisions = make(map[string]Revision) |
| 46 | b.Latest = origin |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 47 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 48 | return b |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 51 | // Utility function to extract all children names for a given revision (mostly for debugging purposes) |
| 52 | func (b *Branch) retrieveChildrenNames(revision Revision) []string { |
| 53 | var childrenNames []string |
| 54 | |
| 55 | for _, child := range revision.GetChildren("devices") { |
| 56 | childrenNames = append(childrenNames, child.GetName()) |
| 57 | } |
| 58 | |
| 59 | return childrenNames |
| 60 | } |
| 61 | |
| 62 | // Utility function to compare children names and report the missing ones (mostly for debugging purposes) |
| 63 | func (b *Branch) findMissingChildrenNames(previousNames, latestNames []string) []string { |
| 64 | var missingNames []string |
| 65 | |
| 66 | for _, previousName := range previousNames { |
| 67 | found := false |
| 68 | |
| 69 | if len(latestNames) == 0 { |
| 70 | break |
| 71 | } |
| 72 | |
| 73 | for _, latestName := range latestNames { |
| 74 | if previousName == latestName { |
| 75 | found = true |
| 76 | break |
| 77 | } |
| 78 | } |
| 79 | if !found { |
| 80 | missingNames = append(missingNames, previousName) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return missingNames |
| 85 | } |
| 86 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 87 | // SetLatest assigns the latest revision for this branch |
| 88 | func (b *Branch) SetLatest(latest Revision) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 89 | b.mutex.Lock() |
| 90 | defer b.mutex.Unlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 91 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 92 | if b.Latest != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame^] | 93 | logger.Debugw("updating-latest-revision", log.Fields{"current": b.Latest.GetHash(), "new": latest.GetHash()}) |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 94 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 95 | // Go through list of children names in current revision and new revision |
| 96 | // and then compare the resulting outputs to ensure that we have not lost any entries. |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 97 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 98 | if level, _ := log.GetPackageLogLevel(); level == log.DebugLevel { |
| 99 | var previousNames, latestNames, missingNames []string |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 100 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 101 | if previousNames = b.retrieveChildrenNames(b.Latest); len(previousNames) > 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame^] | 102 | logger.Debugw("children-of-previous-revision", log.Fields{"hash": b.Latest.GetHash(), "names": previousNames}) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 103 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 104 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 105 | if latestNames = b.retrieveChildrenNames(b.Latest); len(latestNames) > 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame^] | 106 | logger.Debugw("children-of-latest-revision", log.Fields{"hash": latest.GetHash(), "names": latestNames}) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if missingNames = b.findMissingChildrenNames(previousNames, latestNames); len(missingNames) > 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame^] | 110 | logger.Debugw("children-missing-in-latest-revision", log.Fields{"hash": latest.GetHash(), "names": missingNames}) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 111 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame^] | 115 | logger.Debugw("setting-latest-revision", log.Fields{"new": latest.GetHash()}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 116 | } |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 117 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 118 | b.Latest = latest |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 119 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 120 | |
| 121 | // GetLatest retrieves the latest revision of the branch |
| 122 | func (b *Branch) GetLatest() Revision { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 123 | b.mutex.RLock() |
| 124 | defer b.mutex.RUnlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 125 | |
| 126 | return b.Latest |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 127 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 128 | |
| 129 | // GetOrigin retrieves the original revision of the branch |
| 130 | func (b *Branch) GetOrigin() Revision { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 131 | b.mutex.RLock() |
| 132 | defer b.mutex.RUnlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 133 | |
| 134 | return b.Origin |
| 135 | } |
| 136 | |
| 137 | // AddRevision inserts a new revision to the branch |
| 138 | func (b *Branch) AddRevision(revision Revision) { |
| 139 | if revision != nil && b.GetRevision(revision.GetHash()) == nil { |
| 140 | b.SetRevision(revision.GetHash(), revision) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // GetRevision pulls a revision entry at the specified hash |
| 145 | func (b *Branch) GetRevision(hash string) Revision { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 146 | b.mutex.RLock() |
| 147 | defer b.mutex.RUnlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 148 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 149 | if revision, ok := b.Revisions[hash]; ok { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 150 | return revision |
| 151 | } |
| 152 | |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | // SetRevision updates a revision entry at the specified hash |
| 157 | func (b *Branch) SetRevision(hash string, revision Revision) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 158 | b.mutex.Lock() |
| 159 | defer b.mutex.Unlock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 160 | |
| 161 | b.Revisions[hash] = revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 162 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 163 | |
| 164 | // DeleteRevision removes a revision with the specified hash |
| 165 | func (b *Branch) DeleteRevision(hash string) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 166 | b.mutex.Lock() |
| 167 | defer b.mutex.Unlock() |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 168 | |
Kent Hagerman | dcd4dcc | 2020-02-25 17:56:17 -0500 | [diff] [blame] | 169 | delete(b.Revisions, hash) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 170 | } |