Fix for RequestorId returned to xApp
[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 func (t *Transaction) GetPayload() *e2ap.PackedData {
105         t.mutex.Lock()
106         defer t.mutex.Unlock()
107         return t.Payload
108 }
109
110 //-----------------------------------------------------------------------------
111 //
112 //-----------------------------------------------------------------------------
113 type TransactionSubs struct {
114         Transaction //
115 }
116
117 func (t *TransactionSubs) String() string {
118         return "transsubs(" + t.Transaction.String() + ")"
119 }
120
121 func (t *TransactionSubs) Release() {
122         t.mutex.Lock()
123         xapp.Logger.Debug("RELEASE %s", t.String())
124         t.tracker = nil
125         t.mutex.Unlock()
126 }
127
128 //-----------------------------------------------------------------------------
129 //
130 //-----------------------------------------------------------------------------
131 type TransactionXappKey struct {
132         xapp.RmrEndpoint
133         Xid string // xapp xid in req
134 }
135
136 func (key *TransactionXappKey) String() string {
137         return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
138 }
139
140 //-----------------------------------------------------------------------------
141 //
142 //-----------------------------------------------------------------------------
143 type TransactionXapp struct {
144         Transaction
145         XappKey   *TransactionXappKey
146         RequestId e2ap.RequestId
147 }
148
149 func (t *TransactionXapp) String() string {
150         var transkey string = "transkey(N/A)"
151         if t.XappKey != nil {
152                 transkey = t.XappKey.String()
153         }
154         return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.RequestId.InstanceId), 10) + ")"
155 }
156
157 func (t *TransactionXapp) GetEndpoint() *xapp.RmrEndpoint {
158         t.mutex.Lock()
159         defer t.mutex.Unlock()
160         if t.XappKey != nil {
161                 return &t.XappKey.RmrEndpoint
162         }
163         return nil
164 }
165
166 func (t *TransactionXapp) GetXid() string {
167         t.mutex.Lock()
168         defer t.mutex.Unlock()
169         if t.XappKey != nil {
170                 return t.XappKey.Xid
171         }
172         return ""
173 }
174
175 func (t *TransactionXapp) GetSrc() string {
176         t.mutex.Lock()
177         defer t.mutex.Unlock()
178         if t.XappKey != nil {
179                 return t.XappKey.RmrEndpoint.String()
180         }
181         return ""
182 }
183
184 func (t *TransactionXapp) GetSubId() uint32 {
185         t.mutex.Lock()
186         defer t.mutex.Unlock()
187         return t.RequestId.InstanceId
188 }
189
190 func (t *TransactionXapp) Release() {
191         t.mutex.Lock()
192         xapp.Logger.Debug("RELEASE %s", t.String())
193         tracker := t.tracker
194         xappkey := t.XappKey
195         t.tracker = nil
196         t.mutex.Unlock()
197
198         if tracker != nil && xappkey != nil {
199                 tracker.UnTrackTransaction(*xappkey)
200         }
201 }