Fixes added and UT-coverage improved over 85%
[ric-plt/submgr.git] / pkg / control / transaction.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         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
24         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
25         "strconv"
26         "sync"
27         "time"
28 )
29
30 //-----------------------------------------------------------------------------
31 //
32 //-----------------------------------------------------------------------------
33 type TransactionIf interface {
34         String() string
35         Release()
36         SendEvent(interface{}, time.Duration) (bool, bool)
37         WaitEvent(time.Duration) (interface{}, bool)
38 }
39
40 //-----------------------------------------------------------------------------
41 //
42 //-----------------------------------------------------------------------------
43
44 type Transaction struct {
45         mutex     sync.Mutex       //
46         Seq       uint64           //transaction sequence
47         tracker   *Tracker         //tracker instance
48         Meid      *xapp.RMRMeid    //meid transaction related
49         Mtype     int              //Encoded message type to be send
50         Payload   *e2ap.PackedData //Encoded message to be send
51         EventChan chan interface{}
52 }
53
54 func (t *Transaction) String() string {
55         meidstr := "N/A"
56         if t.Meid != nil {
57                 meidstr = t.Meid.String()
58         }
59         return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + meidstr + ")"
60 }
61
62 func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
63         if waittime > 0 {
64                 select {
65                 case t.EventChan <- event:
66                         return true, false
67                 case <-time.After(waittime):
68                         return false, true
69                 }
70                 return false, false
71         }
72         t.EventChan <- event
73         return true, false
74 }
75
76 func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
77         if waittime > 0 {
78                 select {
79                 case event := <-t.EventChan:
80                         return event, false
81                 case <-time.After(waittime):
82                         return nil, true
83                 }
84         }
85         event := <-t.EventChan
86         return event, false
87 }
88
89 func (t *Transaction) GetMtype() int {
90         t.mutex.Lock()
91         defer t.mutex.Unlock()
92         return t.Mtype
93 }
94
95 func (t *Transaction) GetMeid() *xapp.RMRMeid {
96         t.mutex.Lock()
97         defer t.mutex.Unlock()
98         if t.Meid != nil {
99                 return t.Meid
100         }
101         return nil
102 }
103
104 /*  // This function is not used. Commented out to get better test coverage result
105 func (t *Transaction) GetPayload() *e2ap.PackedData {
106         t.mutex.Lock()
107         defer t.mutex.Unlock()
108         return t.Payload
109 }
110 */
111 //-----------------------------------------------------------------------------
112 //
113 //-----------------------------------------------------------------------------
114 type TransactionSubs struct {
115         Transaction //
116 }
117
118 func (t *TransactionSubs) String() string {
119         return "transsubs(" + t.Transaction.String() + ")"
120 }
121
122 func (t *TransactionSubs) Release() {
123         t.mutex.Lock()
124         xapp.Logger.Debug("RELEASE %s", t.String())
125         t.tracker = nil
126         t.mutex.Unlock()
127 }
128
129 //-----------------------------------------------------------------------------
130 //
131 //-----------------------------------------------------------------------------
132 type TransactionXappKey struct {
133         xapp.RmrEndpoint
134         Xid string // xapp xid in req
135 }
136
137 func (key *TransactionXappKey) String() string {
138         return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
139 }
140
141 //-----------------------------------------------------------------------------
142 //
143 //-----------------------------------------------------------------------------
144 type TransactionXapp struct {
145         Transaction
146         XappKey   *TransactionXappKey
147         RequestId e2ap.RequestId
148 }
149
150 func (t *TransactionXapp) String() string {
151         var transkey string = "transkey(N/A)"
152         if t.XappKey != nil {
153                 transkey = t.XappKey.String()
154         }
155         return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.RequestId.InstanceId), 10) + ")"
156 }
157
158 func (t *TransactionXapp) GetEndpoint() *xapp.RmrEndpoint {
159         t.mutex.Lock()
160         defer t.mutex.Unlock()
161         if t.XappKey != nil {
162                 return &t.XappKey.RmrEndpoint
163         }
164         return nil
165 }
166
167 func (t *TransactionXapp) GetXid() string {
168         t.mutex.Lock()
169         defer t.mutex.Unlock()
170         if t.XappKey != nil {
171                 return t.XappKey.Xid
172         }
173         return ""
174 }
175
176 /*  // This function is not used. Commented out to get better test coverage result
177 func (t *TransactionXapp) GetSrc() string {
178         t.mutex.Lock()
179         defer t.mutex.Unlock()
180         if t.XappKey != nil {
181                 return t.XappKey.RmrEndpoint.String()
182         }
183         return ""
184 }
185 */
186 func (t *TransactionXapp) GetSubId() uint32 {
187         t.mutex.Lock()
188         defer t.mutex.Unlock()
189         return t.RequestId.InstanceId
190 }
191
192 func (t *TransactionXapp) Release() {
193         t.mutex.Lock()
194         xapp.Logger.Debug("RELEASE %s", t.String())
195         tracker := t.tracker
196         xappkey := t.XappKey
197         t.tracker = nil
198         t.mutex.Unlock()
199
200         if tracker != nil && xappkey != nil {
201                 tracker.UnTrackTransaction(*xappkey)
202         }
203 }