Some cleaning and bug fixes
[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]*TransactionXapp
34         transSeq             uint64
35 }
36
37 func (t *Tracker) Init() {
38         t.transactionXappTable = make(map[TransactionXappKey]*TransactionXapp)
39 }
40
41 func (t *Tracker) initTransaction(transBase *Transaction) {
42         t.mutex.Lock()
43         defer t.mutex.Unlock()
44         transBase.EventChan = make(chan interface{})
45         transBase.tracker = t
46         transBase.Seq = t.transSeq
47         t.transSeq++
48 }
49
50 func (t *Tracker) NewSubsTransaction(subs *Subscription) *TransactionSubs {
51         trans := &TransactionSubs{}
52         trans.Meid = subs.GetMeid()
53         rid := subs.GetReqId()
54         if rid != nil {
55                 trans.ReqId = *rid
56         }
57         t.initTransaction(&trans.Transaction)
58         xapp.Logger.Debug("CREATE %s", trans.String())
59         return trans
60 }
61
62 func (t *Tracker) NewXappTransaction(
63         endpoint *RmrEndpoint,
64         xid string,
65         reqId *RequestId,
66         meid *xapp.RMRMeid) *TransactionXapp {
67
68         trans := &TransactionXapp{}
69         trans.XappKey = &TransactionXappKey{*endpoint, xid}
70         trans.Meid = meid
71         if reqId != nil {
72                 trans.ReqId = *reqId
73         }
74         t.initTransaction(&trans.Transaction)
75         xapp.Logger.Debug("CREATE %s", trans.String())
76         return trans
77 }
78
79 func (t *Tracker) Track(trans *TransactionXapp) error {
80
81         if trans.GetEndpoint() == nil {
82                 err := fmt.Errorf("Tracker: No valid endpoint given in %s", trans.String())
83                 return err
84         }
85
86         t.mutex.Lock()
87         defer t.mutex.Unlock()
88
89         theKey := *trans.XappKey
90
91         if othtrans, ok := t.transactionXappTable[theKey]; ok {
92                 err := fmt.Errorf("Tracker: %s is ongoing, not tracking %s", othtrans, trans)
93                 return err
94         }
95
96         trans.tracker = t
97         t.transactionXappTable[theKey] = trans
98         xapp.Logger.Debug("Tracker: Append %s", trans.String())
99         //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
100         return nil
101 }
102
103 func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*TransactionXapp, error) {
104         t.mutex.Lock()
105         defer t.mutex.Unlock()
106         if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
107                 xapp.Logger.Debug("Tracker: Remove %s", trans.String())
108                 delete(t.transactionXappTable, xappKey)
109                 //xapp.Logger.Debug("Tracker: transtable=%v", t.transactionXappTable)
110                 return trans, nil
111         }
112         return nil, fmt.Errorf("Tracker: No record %s", xappKey)
113 }