blob: 957e0cad73f6d8ab8e289cf9b692e350a2004e61 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
Stephane Barbarie933b09b2019-01-09 11:12:09 -050019import (
Scott Baker807addd2019-10-24 15:16:21 -070020 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Stephane Barbarie933b09b2019-01-09 11:12:09 -050021 "sync"
22)
Stephane Barbariedc5022d2018-11-19 15:21:44 -050023
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040024// TODO: implement weak references or something equivalent
25// TODO: missing proper logging
26
Stephane Barbariedc5022d2018-11-19 15:21:44 -050027// Branch structure is used to classify a collection of transaction based revisions
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040028type Branch struct {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040029 mutex sync.RWMutex
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040030 Node *node
31 Txid string
32 Origin Revision
33 Revisions map[string]Revision
34 LatestLock sync.RWMutex
35 Latest Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040036}
37
Stephane Barbariedc5022d2018-11-19 15:21:44 -050038// NewBranch creates a new instance of the Branch structure
Stephane Barbarie06c4a742018-10-01 11:09:32 -040039func NewBranch(node *node, txid string, origin Revision, autoPrune bool) *Branch {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050040 b := &Branch{}
41 b.Node = node
42 b.Txid = txid
43 b.Origin = origin
44 b.Revisions = make(map[string]Revision)
45 b.Latest = origin
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040046
Stephane Barbariedc5022d2018-11-19 15:21:44 -050047 return b
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040048}
49
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040050// Utility function to extract all children names for a given revision (mostly for debugging purposes)
51func (b *Branch) retrieveChildrenNames(revision Revision) []string {
52 var childrenNames []string
53
54 for _, child := range revision.GetChildren("devices") {
55 childrenNames = append(childrenNames, child.GetName())
56 }
57
58 return childrenNames
59}
60
61// Utility function to compare children names and report the missing ones (mostly for debugging purposes)
62func (b *Branch) findMissingChildrenNames(previousNames, latestNames []string) []string {
63 var missingNames []string
64
65 for _, previousName := range previousNames {
66 found := false
67
68 if len(latestNames) == 0 {
69 break
70 }
71
72 for _, latestName := range latestNames {
73 if previousName == latestName {
74 found = true
75 break
76 }
77 }
78 if !found {
79 missingNames = append(missingNames, previousName)
80 }
81 }
82
83 return missingNames
84}
85
Stephane Barbariedc5022d2018-11-19 15:21:44 -050086// SetLatest assigns the latest revision for this branch
87func (b *Branch) SetLatest(latest Revision) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040088 b.mutex.Lock()
89 defer b.mutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -050090
Stephane Barbarie933b09b2019-01-09 11:12:09 -050091 if b.Latest != nil {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040092 log.Debugw("updating-latest-revision", log.Fields{"current": b.Latest.GetHash(), "new": latest.GetHash()})
Stephane Barbarie933b09b2019-01-09 11:12:09 -050093
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040094 // Go through list of children names in current revision and new revision
95 // and then compare the resulting outputs to ensure that we have not lost any entries.
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040096
Stephane Barbarie7512fc82019-05-07 12:25:46 -040097 if level, _ := log.GetPackageLogLevel(); level == log.DebugLevel {
98 var previousNames, latestNames, missingNames []string
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040099
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400100 if previousNames = b.retrieveChildrenNames(b.Latest); len(previousNames) > 0 {
101 log.Debugw("children-of-previous-revision", log.Fields{"hash": b.Latest.GetHash(), "names": previousNames})
102 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400103
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400104 if latestNames = b.retrieveChildrenNames(b.Latest); len(latestNames) > 0 {
105 log.Debugw("children-of-latest-revision", log.Fields{"hash": latest.GetHash(), "names": latestNames})
106 }
107
108 if missingNames = b.findMissingChildrenNames(previousNames, latestNames); len(missingNames) > 0 {
109 log.Debugw("children-missing-in-latest-revision", log.Fields{"hash": latest.GetHash(), "names": missingNames})
110 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400111 }
112
113 } else {
114 log.Debugw("setting-latest-revision", log.Fields{"new": latest.GetHash()})
115 }
Stephane Barbarie933b09b2019-01-09 11:12:09 -0500116
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500117 b.Latest = latest
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400118}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500119
120// GetLatest retrieves the latest revision of the branch
121func (b *Branch) GetLatest() Revision {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400122 b.mutex.RLock()
123 defer b.mutex.RUnlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500124
125 return b.Latest
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400126}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500127
128// GetOrigin retrieves the original revision of the branch
129func (b *Branch) GetOrigin() Revision {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400130 b.mutex.RLock()
131 defer b.mutex.RUnlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500132
133 return b.Origin
134}
135
136// AddRevision inserts a new revision to the branch
137func (b *Branch) AddRevision(revision Revision) {
138 if revision != nil && b.GetRevision(revision.GetHash()) == nil {
139 b.SetRevision(revision.GetHash(), revision)
140 }
141}
142
143// GetRevision pulls a revision entry at the specified hash
144func (b *Branch) GetRevision(hash string) Revision {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400145 b.mutex.RLock()
146 defer b.mutex.RUnlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500147
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500148 if revision, ok := b.Revisions[hash]; ok {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500149 return revision
150 }
151
152 return nil
153}
154
155// SetRevision updates a revision entry at the specified hash
156func (b *Branch) SetRevision(hash string, revision Revision) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400157 b.mutex.Lock()
158 defer b.mutex.Unlock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500159
160 b.Revisions[hash] = revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400161}
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400162
163// DeleteRevision removes a revision with the specified hash
164func (b *Branch) DeleteRevision(hash string) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400165 b.mutex.Lock()
166 defer b.mutex.Unlock()
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400167
168 if _, ok := b.Revisions[hash]; ok {
169 delete(b.Revisions, hash)
170 }
171}