2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
27 var trackerMutex = &sync.Mutex{}
30 Implements a record of ongoing transactions and helper functions to CRUD the records.
33 transactionTable map[TransactionKey]Transaction
36 func (t *Tracker) Init() {
37 t.transactionTable = make(map[TransactionKey]Transaction)
41 Checks if a tranascation with similar type has been ongoing. If not then creates one.
42 Returns error if there is similar transatcion ongoing.
44 func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error {
46 defer trackerMutex.Unlock()
47 if _, ok := t.transactionTable[key]; ok {
48 // TODO: Implement merge related check here. If the key is same but the value is different.
49 err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.transType)
52 t.transactionTable[key] = xact
57 Retreives the transaction table entry for the given request.
58 Returns error in case the transaction cannot be found.
60 func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error {
61 key := TransactionKey{SubID, transType}
63 defer trackerMutex.Unlock()
64 if _, ok := t.transactionTable[key]; ok {
65 // TODO: Implement merge related check here. If the key is same but the value is different.
66 err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %v is ongoing", key.SubID, key.transType)
69 t.transactionTable[key] = xact
74 Retreives the transaction table entry for the given request.
75 Returns error in case the transaction cannot be found.
77 func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) {
78 key := TransactionKey{subID, act}
80 defer trackerMutex.Unlock()
82 if xact, ok := t.transactionTable[key]; ok {
85 err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
90 Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
91 Returns error in case the transaction cannot be found.
93 func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) {
94 key := TransactionKey{subID, act}
95 var emptyTransaction Transaction
97 defer trackerMutex.Unlock()
98 if xact, ok := t.transactionTable[key]; ok {
99 delete(t.transactionTable, key)
102 err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
103 return emptyTransaction, err