blob: b2b838b1d132231721db804ccd3b001332254a7a [file] [log] [blame]
Juha Hyttinen0388dd92020-01-09 14:14:16 +02001/*
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
20package control
21
22import (
Juha Hyttinen93400722020-01-15 09:25:13 +020023 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
Juha Hyttinen86a46202020-01-14 12:49:09 +020024 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Juha Hyttinen0388dd92020-01-09 14:14:16 +020025 "strconv"
26 "sync"
Juha Hyttinen422d0182020-01-17 13:37:05 +020027 "time"
Juha Hyttinen0388dd92020-01-09 14:14:16 +020028)
29
30//-----------------------------------------------------------------------------
31//
32//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +020033type TransactionIf interface {
34 String() string
35 Release()
36 SendEvent(interface{}, time.Duration) (bool, bool)
37 WaitEvent(time.Duration) (interface{}, bool)
38}
Juha Hyttinen422d0182020-01-17 13:37:05 +020039
Juha Hyttinen83ada002020-01-30 10:36:33 +020040//-----------------------------------------------------------------------------
41//
42//-----------------------------------------------------------------------------
43
44type Transaction struct {
Juha Hyttinen422d0182020-01-17 13:37:05 +020045 mutex sync.Mutex //
Juha Hyttinen83ada002020-01-30 10:36:33 +020046 Seq uint64 //transaction sequence
Juha Hyttinen422d0182020-01-17 13:37:05 +020047 tracker *Tracker //tracker instance
48 Meid *xapp.RMRMeid //meid transaction related
Juha Hyttinen83ada002020-01-30 10:36:33 +020049 ReqId RequestId //
Juha Hyttinen422d0182020-01-17 13:37:05 +020050 Mtype int //Encoded message type to be send
51 Payload *packer.PackedData //Encoded message to be send
52 EventChan chan interface{}
53}
54
Juha Hyttinen83ada002020-01-30 10:36:33 +020055func (t *Transaction) String() string {
56 return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + "/" + t.ReqId.String() + ")"
57}
58
59func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020060 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
Juha Hyttinen83ada002020-01-30 10:36:33 +020073func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020074 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
Juha Hyttinen83ada002020-01-30 10:36:33 +020086func (t *Transaction) GetReqId() *RequestId {
87 t.mutex.Lock()
88 defer t.mutex.Unlock()
89 return &t.ReqId
90}
91
92func (t *Transaction) GetMtype() int {
Juha Hyttinen422d0182020-01-17 13:37:05 +020093 t.mutex.Lock()
94 defer t.mutex.Unlock()
95 return t.Mtype
96}
97
Juha Hyttinen83ada002020-01-30 10:36:33 +020098func (t *Transaction) GetMeid() *xapp.RMRMeid {
Juha Hyttinen422d0182020-01-17 13:37:05 +020099 t.mutex.Lock()
100 defer t.mutex.Unlock()
101 if t.Meid != nil {
102 return t.Meid
103 }
104 return nil
105}
106
Juha Hyttinen83ada002020-01-30 10:36:33 +0200107func (t *Transaction) GetPayload() *packer.PackedData {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200108 t.mutex.Lock()
109 defer t.mutex.Unlock()
110 return t.Payload
111}
112
113//-----------------------------------------------------------------------------
114//
115//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200116type TransactionSubs struct {
117 Transaction //
118}
119
120func (t *TransactionSubs) String() string {
121 return "transsubs(" + t.Transaction.String() + ")"
122}
123
124func (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//-----------------------------------------------------------------------------
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200134type TransactionXappKey struct {
135 RmrEndpoint
136 Xid string // xapp xid in req
137}
138
139func (key *TransactionXappKey) String() string {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200140 return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200141}
142
143//-----------------------------------------------------------------------------
144//
145//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200146type TransactionXapp struct {
147 Transaction //
148 XappKey *TransactionXappKey //
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200149}
150
Juha Hyttinen83ada002020-01-30 10:36:33 +0200151func (t *TransactionXapp) String() string {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200152 var transkey string = "transkey(N/A)"
153 if t.XappKey != nil {
154 transkey = t.XappKey.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200155 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200156 return "transxapp(" + t.Transaction.String() + "/" + transkey + ")"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200157}
158
Juha Hyttinen83ada002020-01-30 10:36:33 +0200159func (t *TransactionXapp) GetEndpoint() *RmrEndpoint {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200160 t.mutex.Lock()
161 defer t.mutex.Unlock()
162 if t.XappKey != nil {
163 return &t.XappKey.RmrEndpoint
164 }
165 return nil
166}
167
Juha Hyttinen83ada002020-01-30 10:36:33 +0200168func (t *TransactionXapp) GetXid() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200169 t.mutex.Lock()
170 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200171 if t.XappKey != nil {
172 return t.XappKey.Xid
Juha Hyttinen86a46202020-01-14 12:49:09 +0200173 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200174 return ""
Juha Hyttinen86a46202020-01-14 12:49:09 +0200175}
176
Juha Hyttinen83ada002020-01-30 10:36:33 +0200177func (t *TransactionXapp) GetSrc() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200178 t.mutex.Lock()
179 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200180 if t.XappKey != nil {
181 return t.XappKey.RmrEndpoint.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200182 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200183 return ""
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200184}
185
Juha Hyttinen83ada002020-01-30 10:36:33 +0200186func (t *TransactionXapp) Release() {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200187 t.mutex.Lock()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200188 xapp.Logger.Debug("RELEASE %s", t.String())
Juha Hyttinen31797b42020-01-16 14:05:01 +0200189 tracker := t.tracker
Juha Hyttinen422d0182020-01-17 13:37:05 +0200190 xappkey := t.XappKey
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200191 t.tracker = nil
Juha Hyttinen31797b42020-01-16 14:05:01 +0200192 t.mutex.Unlock()
193
Juha Hyttinen422d0182020-01-17 13:37:05 +0200194 if tracker != nil && xappkey != nil {
195 tracker.UnTrackTransaction(*xappkey)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200196 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200197}