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" |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | /* |
| 27 | Implements a record of ongoing transactions and helper functions to CRUD the records. |
| 28 | */ |
| 29 | type Tracker struct { |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 30 | transaction_table map[Transaction_key]Transaction |
| 31 | } |
| 32 | |
| 33 | func (t *Tracker) Init() { |
| 34 | t.transaction_table = make(map[Transaction_key]Transaction) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* |
| 38 | Checks if a tranascation with similar type has been ongoing. If not then creates one. |
| 39 | Returns error if there is similar transatcion ongoing. |
| 40 | */ |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 41 | func (t *Tracker) Track_transaction(key Transaction_key, xact Transaction) error{ |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 42 | if _, ok := t.transaction_table[key]; ok { |
| 43 | // TODO: Implement merge related check here. If the key is same but the value is different. |
| 44 | err := fmt.Errorf("Transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.trans_type ) |
| 45 | return err |
| 46 | } |
| 47 | t.transaction_table[key] = xact |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | Retreives the transaction table entry for the given request. |
| 53 | Returns error in case the transaction cannot be found. |
| 54 | */ |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 55 | func (t *Tracker) Update_transaction(SubID uint16, trans_type Action, xact Transaction) error{ |
| 56 | key := Transaction_key{SubID, trans_type} |
| 57 | if _, ok := t.transaction_table[key]; ok { |
| 58 | // TODO: Implement merge related check here. If the key is same but the value is different. |
| 59 | err := fmt.Errorf("Transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.trans_type ) |
| 60 | return err |
| 61 | } |
| 62 | t.transaction_table[key] = xact |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | Retreives the transaction table entry for the given request. |
| 68 | Returns error in case the transaction cannot be found. |
| 69 | */ |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 70 | func (t *Tracker) Retrive_transaction(subID uint16, act Action) (Transaction, error){ |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 71 | key := Transaction_key{subID, act} |
Balint Uveges | e9608cd | 2019-09-20 18:00:32 +0000 | [diff] [blame] | 72 | var xact Transaction |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 73 | if xact, ok := t.transaction_table[key]; ok { |
| 74 | return xact, nil |
| 75 | } |
| 76 | err := fmt.Errorf("Tranaction 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] | 77 | return xact, err |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
| 81 | Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference. |
| 82 | Returns error in case the transaction cannot be found. |
| 83 | */ |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 84 | func (t *Tracker) complete_transaction(subID uint16, act Action) (Transaction, error){ |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 85 | key := Transaction_key{subID, act} |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 86 | var empty_transaction Transaction |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 87 | if xact, ok := t.transaction_table[key]; ok { |
| 88 | delete(t.transaction_table, key) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 89 | return xact, nil |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 90 | } |
| 91 | err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act) |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 92 | return empty_transaction, err |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 93 | } |