d922d18426a01270f1d302da0f2fb2c9956cb36a
[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/submgr/pkg/xapptweaks"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "strconv"
27         "sync"
28         "time"
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         return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + (&xapptweaks.RMRMeid{t.Meid}).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) GetMtype() int {
87         t.mutex.Lock()
88         defer t.mutex.Unlock()
89         return t.Mtype
90 }
91
92 func (t *Transaction) GetMeid() *xapp.RMRMeid {
93         t.mutex.Lock()
94         defer t.mutex.Unlock()
95         if t.Meid != nil {
96                 return t.Meid
97         }
98         return nil
99 }
100
101 func (t *Transaction) GetPayload() *e2ap.PackedData {
102         t.mutex.Lock()
103         defer t.mutex.Unlock()
104         return t.Payload
105 }
106
107 //-----------------------------------------------------------------------------
108 //
109 //-----------------------------------------------------------------------------
110 type TransactionSubs struct {
111         Transaction //
112 }
113
114 func (t *TransactionSubs) String() string {
115         return "transsubs(" + t.Transaction.String() + ")"
116 }
117
118 func (t *TransactionSubs) Release() {
119         t.mutex.Lock()
120         xapp.Logger.Debug("RELEASE %s", t.String())
121         t.tracker = nil
122         t.mutex.Unlock()
123 }
124
125 //-----------------------------------------------------------------------------
126 //
127 //-----------------------------------------------------------------------------
128 type TransactionXappKey struct {
129         xapptweaks.RmrEndpoint
130         Xid string // xapp xid in req
131 }
132
133 func (key *TransactionXappKey) String() string {
134         return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
135 }
136
137 //-----------------------------------------------------------------------------
138 //
139 //-----------------------------------------------------------------------------
140 type TransactionXapp struct {
141         Transaction
142         XappKey *TransactionXappKey
143         SubId   uint32
144 }
145
146 func (t *TransactionXapp) String() string {
147         var transkey string = "transkey(N/A)"
148         if t.XappKey != nil {
149                 transkey = t.XappKey.String()
150         }
151         return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.SubId), 10) + ")"
152 }
153
154 func (t *TransactionXapp) GetEndpoint() *xapptweaks.RmrEndpoint {
155         t.mutex.Lock()
156         defer t.mutex.Unlock()
157         if t.XappKey != nil {
158                 return &t.XappKey.RmrEndpoint
159         }
160         return nil
161 }
162
163 func (t *TransactionXapp) GetXid() string {
164         t.mutex.Lock()
165         defer t.mutex.Unlock()
166         if t.XappKey != nil {
167                 return t.XappKey.Xid
168         }
169         return ""
170 }
171
172 func (t *TransactionXapp) GetSrc() string {
173         t.mutex.Lock()
174         defer t.mutex.Unlock()
175         if t.XappKey != nil {
176                 return t.XappKey.RmrEndpoint.String()
177         }
178         return ""
179 }
180
181 func (t *TransactionXapp) GetSubId() uint32 {
182         t.mutex.Lock()
183         defer t.mutex.Unlock()
184         return t.SubId
185 }
186
187 func (t *TransactionXapp) Release() {
188         t.mutex.Lock()
189         xapp.Logger.Debug("RELEASE %s", t.String())
190         tracker := t.tracker
191         xappkey := t.XappKey
192         t.tracker = nil
193         t.mutex.Unlock()
194
195         if tracker != nil && xappkey != nil {
196                 tracker.UnTrackTransaction(*xappkey)
197         }
198 }