bd062da9fa2ef4e520f7a7c685732ff1a73e047c
[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 /*
35 Checks if a tranascation with similar type has been ongoing. If not then creates one.
36 Returns error if there is similar transatcion ongoing.
37 */
38 func (t *Tracker) Track_transaction(key Transaction_key, xact *Transaction) error{
39         if _, ok := t.transaction_table[key]; ok {
40                 // TODO: Implement merge related check here. If the key is same but the value is different.
41                 err := fmt.Errorf("Transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.trans_type )
42                 return err
43         }
44         t.transaction_table[key] = xact
45         return nil
46 }
47
48 /*
49 Retreives the transaction table entry for the given request.
50 Returns error in case the transaction cannot be found.
51 */
52 func (t *Tracker) Retrive_transaction(subID uint16, act Action) (*Transaction, error){
53         key := Transaction_key{subID, act}
54         if xact, ok := t.transaction_table[key]; ok {
55                 return xact, nil
56         }
57         err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
58         return nil, err
59 }
60
61 /*
62 Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
63 Returns error in case the transaction cannot be found.
64 */
65 func (t *Tracker) complete_transaction(subID uint16, act Action) (*string, *uint16, error){
66         key := Transaction_key{subID, act}
67         if xact, ok := t.transaction_table[key]; ok {
68                 delete(t.transaction_table, key)
69                 return &(xact.Xapp_instance_address), &(xact.Xapp_port), nil
70         }
71         err := fmt.Errorf("Tranaction record for Subscription ID %d and action %s does not exist", subID, act)
72         return nil, nil, err
73 }