kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | Copyright (c) 2019 Nokia |
| 5 | |
| 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 |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 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 | ================================================================================== |
| 18 | */ |
| 19 | |
| 20 | package control |
| 21 | |
| 22 | import ( |
| 23 | "fmt" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 24 | "sync" |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | /* |
| 28 | Implements a record of ongoing transactions and helper functions to CRUD the records. |
| 29 | */ |
| 30 | type Tracker struct { |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 31 | transactionTable map[TransactionKey]Transaction |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 32 | mutex sync.Mutex |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | func (t *Tracker) Init() { |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 36 | t.transactionTable = make(map[TransactionKey]Transaction) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* |
| 40 | Checks if a tranascation with similar type has been ongoing. If not then creates one. |
| 41 | Returns error if there is similar transatcion ongoing. |
| 42 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 43 | func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 44 | t.mutex.Lock() |
| 45 | defer t.mutex.Unlock() |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 46 | if _, ok := t.transactionTable[key]; ok { |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 47 | // TODO: Implement merge related check here. If the key is same but the value is different. |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 48 | err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.transType) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 49 | return err |
| 50 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 51 | t.transactionTable[key] = xact |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 52 | return nil |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | Retreives the transaction table entry for the given request. |
| 57 | Returns error in case the transaction cannot be found. |
| 58 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 59 | func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error { |
| 60 | key := TransactionKey{SubID, transType} |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 61 | t.mutex.Lock() |
| 62 | defer t.mutex.Unlock() |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 63 | if _, ok := t.transactionTable[key]; ok { |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 64 | // TODO: Implement merge related check here. If the key is same but the value is different. |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 65 | err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %v is ongoing", key.SubID, key.transType) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 66 | return err |
| 67 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 68 | t.transactionTable[key] = xact |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 69 | return nil |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | Retreives the transaction table entry for the given request. |
| 74 | Returns error in case the transaction cannot be found. |
| 75 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 76 | func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) { |
| 77 | key := TransactionKey{subID, act} |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 78 | t.mutex.Lock() |
| 79 | defer t.mutex.Unlock() |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 80 | var xact Transaction |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 81 | if xact, ok := t.transactionTable[key]; ok { |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 82 | return xact, nil |
| 83 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 84 | err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act) |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 85 | return xact, err |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference. |
| 90 | Returns error in case the transaction cannot be found. |
| 91 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 92 | func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) { |
| 93 | key := TransactionKey{subID, act} |
| 94 | var emptyTransaction Transaction |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 95 | t.mutex.Lock() |
| 96 | defer t.mutex.Unlock() |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 97 | if xact, ok := t.transactionTable[key]; ok { |
| 98 | delete(t.transactionTable, key) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 99 | return xact, nil |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 100 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 101 | err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act) |
| 102 | return emptyTransaction, err |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 103 | } |