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 | |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 27 | var trackerMutex = &sync.Mutex{} |
| 28 | |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 29 | /* |
| 30 | Implements a record of ongoing transactions and helper functions to CRUD the records. |
| 31 | */ |
| 32 | type Tracker struct { |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 33 | transactionTable map[TransactionKey]Transaction |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func (t *Tracker) Init() { |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 37 | t.transactionTable = make(map[TransactionKey]Transaction) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* |
| 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. |
| 43 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 44 | func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error { |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 45 | trackerMutex.Lock() |
| 46 | defer trackerMutex.Unlock() |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 47 | if _, ok := t.transactionTable[key]; ok { |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 48 | // 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] | 49 | 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] | 50 | return err |
| 51 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 52 | t.transactionTable[key] = xact |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 53 | return nil |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | Retreives the transaction table entry for the given request. |
| 58 | Returns error in case the transaction cannot be found. |
| 59 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 60 | func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error { |
| 61 | key := TransactionKey{SubID, transType} |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 62 | trackerMutex.Lock() |
| 63 | defer trackerMutex.Unlock() |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 64 | if _, ok := t.transactionTable[key]; ok { |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 65 | // 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] | 66 | 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] | 67 | return err |
| 68 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 69 | t.transactionTable[key] = xact |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 70 | return nil |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | Retreives the transaction table entry for the given request. |
| 75 | Returns error in case the transaction cannot be found. |
| 76 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 77 | func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) { |
| 78 | key := TransactionKey{subID, act} |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 79 | trackerMutex.Lock() |
| 80 | defer trackerMutex.Unlock() |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 81 | var xact Transaction |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 82 | if xact, ok := t.transactionTable[key]; ok { |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 83 | return xact, nil |
| 84 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 85 | 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] | 86 | return xact, err |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 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. |
| 92 | */ |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 93 | func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) { |
| 94 | key := TransactionKey{subID, act} |
| 95 | var emptyTransaction Transaction |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 96 | trackerMutex.Lock() |
| 97 | defer trackerMutex.Unlock() |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 98 | if xact, ok := t.transactionTable[key]; ok { |
| 99 | delete(t.transactionTable, key) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 100 | return xact, nil |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 101 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 102 | err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act) |
| 103 | return emptyTransaction, err |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 104 | } |