blob: 9b984a38799c48b36f338efb18a99a1b1c151879 [file] [log] [blame]
kalnagy93cc3e22019-09-19 11:29:29 +02001/*
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
20package control
21
22import (
23 "fmt"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020024 "sync"
kalnagy93cc3e22019-09-19 11:29:29 +020025)
26
27/*
28Implements a record of ongoing transactions and helper functions to CRUD the records.
29*/
30type Tracker struct {
kalnagy1455c852019-10-21 13:06:23 +020031 transactionTable map[TransactionKey]Transaction
Juha Hyttinen1a503442019-12-10 12:14:24 +020032 mutex sync.Mutex
Balint Uvegese9608cd2019-09-20 18:00:32 +000033}
34
35func (t *Tracker) Init() {
kalnagy1455c852019-10-21 13:06:23 +020036 t.transactionTable = make(map[TransactionKey]Transaction)
kalnagy93cc3e22019-09-19 11:29:29 +020037}
38
39/*
40Checks if a tranascation with similar type has been ongoing. If not then creates one.
41Returns error if there is similar transatcion ongoing.
42*/
kalnagy1455c852019-10-21 13:06:23 +020043func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error {
Juha Hyttinen1a503442019-12-10 12:14:24 +020044 t.mutex.Lock()
45 defer t.mutex.Unlock()
kalnagy1455c852019-10-21 13:06:23 +020046 if _, ok := t.transactionTable[key]; ok {
kalnagy93cc3e22019-09-19 11:29:29 +020047 // TODO: Implement merge related check here. If the key is same but the value is different.
kalnagy1455c852019-10-21 13:06:23 +020048 err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.transType)
kalnagy93cc3e22019-09-19 11:29:29 +020049 return err
50 }
kalnagy1455c852019-10-21 13:06:23 +020051 t.transactionTable[key] = xact
kalnagy93cc3e22019-09-19 11:29:29 +020052 return nil
53}
54
55/*
56Retreives the transaction table entry for the given request.
57Returns error in case the transaction cannot be found.
58*/
kalnagy1455c852019-10-21 13:06:23 +020059func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error {
60 key := TransactionKey{SubID, transType}
Juha Hyttinen1a503442019-12-10 12:14:24 +020061 t.mutex.Lock()
62 defer t.mutex.Unlock()
kalnagy1455c852019-10-21 13:06:23 +020063 if _, ok := t.transactionTable[key]; ok {
kalnagye0018682019-09-26 16:28:25 +020064 // TODO: Implement merge related check here. If the key is same but the value is different.
kalnagy1455c852019-10-21 13:06:23 +020065 err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %v is ongoing", key.SubID, key.transType)
kalnagye0018682019-09-26 16:28:25 +020066 return err
67 }
kalnagy1455c852019-10-21 13:06:23 +020068 t.transactionTable[key] = xact
kalnagye0018682019-09-26 16:28:25 +020069 return nil
70}
71
72/*
73Retreives the transaction table entry for the given request.
74Returns error in case the transaction cannot be found.
75*/
kalnagy1455c852019-10-21 13:06:23 +020076func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) {
77 key := TransactionKey{subID, act}
Juha Hyttinen1a503442019-12-10 12:14:24 +020078 t.mutex.Lock()
79 defer t.mutex.Unlock()
Balint Uvegese9608cd2019-09-20 18:00:32 +000080 var xact Transaction
kalnagy1455c852019-10-21 13:06:23 +020081 if xact, ok := t.transactionTable[key]; ok {
kalnagy93cc3e22019-09-19 11:29:29 +020082 return xact, nil
83 }
kalnagy1455c852019-10-21 13:06:23 +020084 err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
Balint Uvegese9608cd2019-09-20 18:00:32 +000085 return xact, err
kalnagy93cc3e22019-09-19 11:29:29 +020086}
87
88/*
89Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
90Returns error in case the transaction cannot be found.
91*/
kalnagy1455c852019-10-21 13:06:23 +020092func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) {
93 key := TransactionKey{subID, act}
94 var emptyTransaction Transaction
Juha Hyttinen1a503442019-12-10 12:14:24 +020095 t.mutex.Lock()
96 defer t.mutex.Unlock()
kalnagy1455c852019-10-21 13:06:23 +020097 if xact, ok := t.transactionTable[key]; ok {
98 delete(t.transactionTable, key)
kalnagye0018682019-09-26 16:28:25 +020099 return xact, nil
kalnagy93cc3e22019-09-19 11:29:29 +0200100 }
kalnagy1455c852019-10-21 13:06:23 +0200101 err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
102 return emptyTransaction, err
kalnagy93cc3e22019-09-19 11:29:29 +0200103}