087b7811a22f2bb9710aa47bba528829a911a5d3
[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(subs *Subscription, endpoint RmrEndpoint, params *xapp.RMRParams, respReceived bool, forwardRespToXapp bool) (*Transaction, error) {
41
42         trans := &Transaction{
43                 tracker:           nil,
44                 Subs:              nil,
45                 RmrEndpoint:       endpoint,
46                 Xid:               params.Xid,
47                 OrigParams:        params,
48                 RespReceived:      respReceived,
49                 ForwardRespToXapp: forwardRespToXapp,
50         }
51
52         t.mutex.Lock()
53         defer t.mutex.Unlock()
54
55         xappkey := TransactionXappKey{endpoint, params.Xid}
56         if _, ok := t.transactionXappTable[xappkey]; ok {
57                 err := fmt.Errorf("Tracker: Similar transaction with xappkey %s is ongoing, transaction %s not created ", xappkey, trans)
58                 return nil, err
59         }
60
61         err := subs.SetTransaction(trans)
62         if err != nil {
63                 return nil, err
64         }
65         trans.tracker = t
66         t.transactionXappTable[xappkey] = trans
67         return trans, nil
68 }
69
70 func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
71         t.mutex.Lock()
72         defer t.mutex.Unlock()
73         if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
74                 delete(t.transactionXappTable, xappKey)
75                 return trans, nil
76         }
77         return nil, fmt.Errorf("Tracker: No record for xappkey %s", xappKey)
78 }