blob: dfab96e55aeb5c4af7b48ed17600e964eab14762 [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
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020027var trackerMutex = &sync.Mutex{}
28
kalnagy93cc3e22019-09-19 11:29:29 +020029/*
30Implements a record of ongoing transactions and helper functions to CRUD the records.
31*/
32type Tracker struct {
kalnagy1455c852019-10-21 13:06:23 +020033 transactionTable map[TransactionKey]Transaction
Balint Uvegese9608cd2019-09-20 18:00:32 +000034}
35
36func (t *Tracker) Init() {
kalnagy1455c852019-10-21 13:06:23 +020037 t.transactionTable = make(map[TransactionKey]Transaction)
kalnagy93cc3e22019-09-19 11:29:29 +020038}
39
40/*
41Checks if a tranascation with similar type has been ongoing. If not then creates one.
42Returns error if there is similar transatcion ongoing.
43*/
kalnagy1455c852019-10-21 13:06:23 +020044func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020045 trackerMutex.Lock()
46 defer trackerMutex.Unlock()
kalnagy1455c852019-10-21 13:06:23 +020047 if _, ok := t.transactionTable[key]; ok {
kalnagy93cc3e22019-09-19 11:29:29 +020048 // TODO: Implement merge related check here. If the key is same but the value is different.
kalnagy1455c852019-10-21 13:06:23 +020049 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 +020050 return err
51 }
kalnagy1455c852019-10-21 13:06:23 +020052 t.transactionTable[key] = xact
kalnagy93cc3e22019-09-19 11:29:29 +020053 return nil
54}
55
56/*
57Retreives the transaction table entry for the given request.
58Returns error in case the transaction cannot be found.
59*/
kalnagy1455c852019-10-21 13:06:23 +020060func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error {
61 key := TransactionKey{SubID, transType}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020062 trackerMutex.Lock()
63 defer trackerMutex.Unlock()
kalnagy1455c852019-10-21 13:06:23 +020064 if _, ok := t.transactionTable[key]; ok {
kalnagye0018682019-09-26 16:28:25 +020065 // TODO: Implement merge related check here. If the key is same but the value is different.
kalnagy1455c852019-10-21 13:06:23 +020066 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 +020067 return err
68 }
kalnagy1455c852019-10-21 13:06:23 +020069 t.transactionTable[key] = xact
kalnagye0018682019-09-26 16:28:25 +020070 return nil
71}
72
73/*
74Retreives the transaction table entry for the given request.
75Returns error in case the transaction cannot be found.
76*/
kalnagy1455c852019-10-21 13:06:23 +020077func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) {
78 key := TransactionKey{subID, act}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020079 trackerMutex.Lock()
80 defer trackerMutex.Unlock()
Balint Uvegese9608cd2019-09-20 18:00:32 +000081 var xact Transaction
kalnagy1455c852019-10-21 13:06:23 +020082 if xact, ok := t.transactionTable[key]; ok {
kalnagy93cc3e22019-09-19 11:29:29 +020083 return xact, nil
84 }
kalnagy1455c852019-10-21 13:06:23 +020085 err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
Balint Uvegese9608cd2019-09-20 18:00:32 +000086 return xact, err
kalnagy93cc3e22019-09-19 11:29:29 +020087}
88
89/*
90Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
91Returns error in case the transaction cannot be found.
92*/
kalnagy1455c852019-10-21 13:06:23 +020093func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) {
94 key := TransactionKey{subID, act}
95 var emptyTransaction Transaction
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020096 trackerMutex.Lock()
97 defer trackerMutex.Unlock()
kalnagy1455c852019-10-21 13:06:23 +020098 if xact, ok := t.transactionTable[key]; ok {
99 delete(t.transactionTable, key)
kalnagye0018682019-09-26 16:28:25 +0200100 return xact, nil
kalnagy93cc3e22019-09-19 11:29:29 +0200101 }
kalnagy1455c852019-10-21 13:06:23 +0200102 err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
103 return emptyTransaction, err
kalnagy93cc3e22019-09-19 11:29:29 +0200104}