RICPLT-2989 Submgr routing manager client code to support multiple endpoints
[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
34 type TransactionBase struct {
35         mutex     sync.Mutex         //
36         Seq       uint64             //
37         tracker   *Tracker           //tracker instance
38         Meid      *xapp.RMRMeid      //meid transaction related
39         Mtype     int                //Encoded message type to be send
40         Payload   *packer.PackedData //Encoded message to be send
41         EventChan chan interface{}
42 }
43
44 func (t *TransactionBase) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
45         if waittime > 0 {
46                 select {
47                 case t.EventChan <- event:
48                         return true, false
49                 case <-time.After(waittime):
50                         return false, true
51                 }
52                 return false, false
53         }
54         t.EventChan <- event
55         return true, false
56 }
57
58 func (t *TransactionBase) WaitEvent(waittime time.Duration) (interface{}, bool) {
59         if waittime > 0 {
60                 select {
61                 case event := <-t.EventChan:
62                         return event, false
63                 case <-time.After(waittime):
64                         return nil, true
65                 }
66         }
67         event := <-t.EventChan
68         return event, false
69 }
70
71 func (t *TransactionBase) GetMtype() int {
72         t.mutex.Lock()
73         defer t.mutex.Unlock()
74         return t.Mtype
75 }
76
77 func (t *TransactionBase) GetMeid() *xapp.RMRMeid {
78         t.mutex.Lock()
79         defer t.mutex.Unlock()
80         if t.Meid != nil {
81                 return t.Meid
82         }
83         return nil
84 }
85
86 func (t *TransactionBase) GetPayload() *packer.PackedData {
87         t.mutex.Lock()
88         defer t.mutex.Unlock()
89         return t.Payload
90 }
91
92 //-----------------------------------------------------------------------------
93 //
94 //-----------------------------------------------------------------------------
95 type TransactionXappKey struct {
96         RmrEndpoint
97         Xid string // xapp xid in req
98 }
99
100 func (key *TransactionXappKey) String() string {
101         return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
102 }
103
104 //-----------------------------------------------------------------------------
105 //
106 //-----------------------------------------------------------------------------
107 type Transaction struct {
108         TransactionBase                     //
109         XappKey         *TransactionXappKey //
110 }
111
112 func (t *Transaction) String() string {
113         var transkey string = "transkey(N/A)"
114         if t.XappKey != nil {
115                 transkey = t.XappKey.String()
116         }
117         return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + "/" + transkey + ")"
118 }
119
120 func (t *Transaction) GetEndpoint() *RmrEndpoint {
121         t.mutex.Lock()
122         defer t.mutex.Unlock()
123         if t.XappKey != nil {
124                 return &t.XappKey.RmrEndpoint
125         }
126         return nil
127 }
128
129 func (t *Transaction) GetXid() string {
130         t.mutex.Lock()
131         defer t.mutex.Unlock()
132         if t.XappKey != nil {
133                 return t.XappKey.Xid
134         }
135         return ""
136 }
137
138 func (t *Transaction) GetSrc() string {
139         t.mutex.Lock()
140         defer t.mutex.Unlock()
141         if t.XappKey != nil {
142                 return t.XappKey.RmrEndpoint.String()
143         }
144         return ""
145 }
146
147 func (t *Transaction) Release() {
148         t.mutex.Lock()
149         xapp.Logger.Debug("Transaction: Release %s", t.String())
150         tracker := t.tracker
151         xappkey := t.XappKey
152         t.tracker = nil
153         t.mutex.Unlock()
154
155         if tracker != nil && xappkey != nil {
156                 tracker.UnTrackTransaction(*xappkey)
157         }
158 }