Submgr v0.7.2
[ric-plt/submgr.git] / pkg / control / tracker.go
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"
24 //      "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
25 )
26
27 /*
28 Implements a record of ongoing transactions and helper functions to CRUD the records.
29 */
30 type Tracker struct {
31         transaction_table map[Transaction_key]Transaction
32 }
33
34 func (t *Tracker) Init() {
35         t.transaction_table = make(map[Transaction_key]Transaction)
36 }
37
38 /*
39 Checks if a tranascation with similar type has been ongoing. If not then creates one.
40 Returns error if there is similar transatcion ongoing.
41 */
42 func (t *Tracker) Track_transaction(key Transaction_key, xact Transaction) error{
43         if _, ok := t.transaction_table[key]; ok {
44                 // TODO: Implement merge related check here. If the key is same but the value is different.
45                 err := fmt.Errorf("Transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.trans_type )
46                 return err
47         }
48         t.transaction_table[key] = xact
49         return nil
50 }
51
52 /*
53 Retreives the transaction table entry for the given request.
54 Returns error in case the transaction cannot be found.
55 */
56 func (t *Tracker) Retrive_transaction(subID uint16, act Action) (Transaction, error){
57         key := Transaction_key{subID, act}
58         var xact Transaction
59         if xact, ok := t.transaction_table[key]; ok {
60                 return xact, nil
61         }
62         err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
63         return xact, err
64 }
65
66 /*
67 Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
68 Returns error in case the transaction cannot be found.
69 */
70 func (t *Tracker) complete_transaction(subID uint16, act Action) (string, uint16, error){
71         key := Transaction_key{subID, act}
72         var empty_address string
73         var empty_port uint16
74         if xact, ok := t.transaction_table[key]; ok {
75                 delete(t.transaction_table, key)
76                 return xact.Xapp_instance_address, xact.Xapp_port, nil
77         }
78         err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
79         return empty_address, empty_port, err
80 }