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