75127a7a7190d2825a0c44f49dec0697527b5a40
[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         "sync"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type Tracker struct {
32         mutex                sync.Mutex
33         transactionXappTable map[TransactionXappKey]*Transaction
34 }
35
36 func (t *Tracker) Init() {
37         t.transactionXappTable = make(map[TransactionXappKey]*Transaction)
38 }
39
40 func (t *Tracker) TrackTransaction(
41         endpoint *RmrEndpoint,
42         mtype int,
43         xid string,
44         meid *xapp.RMRMeid,
45         respReceived bool,
46         forwardRespToXapp bool) (*Transaction, error) {
47
48         if endpoint == nil {
49                 err := fmt.Errorf("Tracker: No valid endpoint given")
50                 return nil, err
51         }
52
53         trans := &Transaction{
54                 tracker:           nil,
55                 Subs:              nil,
56                 RmrEndpoint:       *endpoint,
57                 Mtype:             mtype,
58                 Xid:               xid,
59                 Meid:              meid,
60                 RespReceived:      respReceived,
61                 ForwardRespToXapp: forwardRespToXapp,
62         }
63
64         t.mutex.Lock()
65         defer t.mutex.Unlock()
66
67         xappkey := TransactionXappKey{*endpoint, xid}
68         if _, ok := t.transactionXappTable[xappkey]; ok {
69                 err := fmt.Errorf("Tracker: Similar transaction with xappkey %s is ongoing, transaction %s not created ", xappkey, trans)
70                 return nil, err
71         }
72
73         trans.tracker = t
74         t.transactionXappTable[xappkey] = trans
75         return trans, nil
76 }
77
78 func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
79         t.mutex.Lock()
80         defer t.mutex.Unlock()
81         if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
82                 delete(t.transactionXappTable, xappKey)
83                 return trans, nil
84         }
85         return nil, fmt.Errorf("Tracker: No record for xappkey %s", xappKey)
86 }