b2b838b1d132231721db804ccd3b001332254a7a
[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/packer"
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         ReqId     RequestId          //
50         Mtype     int                //Encoded message type to be send
51         Payload   *packer.PackedData //Encoded message to be send
52         EventChan chan interface{}
53 }
54
55 func (t *Transaction) String() string {
56         return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + "/" + t.ReqId.String() + ")"
57 }
58
59 func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
60         if waittime > 0 {
61                 select {
62                 case t.EventChan <- event:
63                         return true, false
64                 case <-time.After(waittime):
65                         return false, true
66                 }
67                 return false, false
68         }
69         t.EventChan <- event
70         return true, false
71 }
72
73 func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
74         if waittime > 0 {
75                 select {
76                 case event := <-t.EventChan:
77                         return event, false
78                 case <-time.After(waittime):
79                         return nil, true
80                 }
81         }
82         event := <-t.EventChan
83         return event, false
84 }
85
86 func (t *Transaction) GetReqId() *RequestId {
87         t.mutex.Lock()
88         defer t.mutex.Unlock()
89         return &t.ReqId
90 }
91
92 func (t *Transaction) GetMtype() int {
93         t.mutex.Lock()
94         defer t.mutex.Unlock()
95         return t.Mtype
96 }
97
98 func (t *Transaction) GetMeid() *xapp.RMRMeid {
99         t.mutex.Lock()
100         defer t.mutex.Unlock()
101         if t.Meid != nil {
102                 return t.Meid
103         }
104         return nil
105 }
106
107 func (t *Transaction) GetPayload() *packer.PackedData {
108         t.mutex.Lock()
109         defer t.mutex.Unlock()
110         return t.Payload
111 }
112
113 //-----------------------------------------------------------------------------
114 //
115 //-----------------------------------------------------------------------------
116 type TransactionSubs struct {
117         Transaction //
118 }
119
120 func (t *TransactionSubs) String() string {
121         return "transsubs(" + t.Transaction.String() + ")"
122 }
123
124 func (t *TransactionSubs) Release() {
125         t.mutex.Lock()
126         xapp.Logger.Debug("RELEASE %s", t.String())
127         t.tracker = nil
128         t.mutex.Unlock()
129 }
130
131 //-----------------------------------------------------------------------------
132 //
133 //-----------------------------------------------------------------------------
134 type TransactionXappKey struct {
135         RmrEndpoint
136         Xid string // xapp xid in req
137 }
138
139 func (key *TransactionXappKey) String() string {
140         return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
141 }
142
143 //-----------------------------------------------------------------------------
144 //
145 //-----------------------------------------------------------------------------
146 type TransactionXapp struct {
147         Transaction                     //
148         XappKey     *TransactionXappKey //
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 + ")"
157 }
158
159 func (t *TransactionXapp) GetEndpoint() *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 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) Release() {
187         t.mutex.Lock()
188         xapp.Logger.Debug("RELEASE %s", t.String())
189         tracker := t.tracker
190         xappkey := t.XappKey
191         t.tracker = nil
192         t.mutex.Unlock()
193
194         if tracker != nil && xappkey != nil {
195                 tracker.UnTrackTransaction(*xappkey)
196         }
197 }