RICPLT-3014 Subscription 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) StringImpl() 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) String() string {
121         t.mutex.Lock()
122         defer t.mutex.Unlock()
123         return t.StringImpl()
124 }
125
126 func (t *Transaction) GetEndpoint() *RmrEndpoint {
127         t.mutex.Lock()
128         defer t.mutex.Unlock()
129         if t.XappKey != nil {
130                 return &t.XappKey.RmrEndpoint
131         }
132         return nil
133 }
134
135 func (t *Transaction) GetXid() string {
136         t.mutex.Lock()
137         defer t.mutex.Unlock()
138         if t.XappKey != nil {
139                 return t.XappKey.Xid
140         }
141         return ""
142 }
143
144 func (t *Transaction) GetSrc() string {
145         t.mutex.Lock()
146         defer t.mutex.Unlock()
147         if t.XappKey != nil {
148                 return t.XappKey.RmrEndpoint.String()
149         }
150         return ""
151 }
152
153 func (t *Transaction) Release() {
154         t.mutex.Lock()
155         xapp.Logger.Debug("Transaction: Release %s", t.StringImpl())
156         tracker := t.tracker
157         xappkey := t.XappKey
158         t.tracker = nil
159         t.mutex.Unlock()
160
161         if tracker != nil && xappkey != nil {
162                 tracker.UnTrackTransaction(*xappkey)
163         }
164 }