blob: 282d291c201a374f023178d5a428d798f76aec54 [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 Hyttinen0388dd92020-01-09 14:14:16 +020023 "strconv"
24 "sync"
Juha Hyttinen422d0182020-01-17 13:37:05 +020025 "time"
archaggeafbf95f2021-04-14 08:54:05 +030026
27 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
28 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Juha Hyttinen0388dd92020-01-09 14:14:16 +020029)
30
31//-----------------------------------------------------------------------------
32//
33//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +020034type TransactionIf interface {
35 String() string
36 Release()
37 SendEvent(interface{}, time.Duration) (bool, bool)
38 WaitEvent(time.Duration) (interface{}, bool)
39}
Juha Hyttinen422d0182020-01-17 13:37:05 +020040
Juha Hyttinen83ada002020-01-30 10:36:33 +020041//-----------------------------------------------------------------------------
42//
43//-----------------------------------------------------------------------------
44
45type Transaction struct {
Juha Hyttinena9bf76c2020-02-05 14:06:57 +020046 mutex sync.Mutex //
47 Seq uint64 //transaction sequence
48 tracker *Tracker //tracker instance
49 Meid *xapp.RMRMeid //meid transaction related
Juha Hyttinena9bf76c2020-02-05 14:06:57 +020050 Mtype int //Encoded message type to be send
51 Payload *e2ap.PackedData //Encoded message to be send
Juha Hyttinen422d0182020-01-17 13:37:05 +020052 EventChan chan interface{}
53}
54
Juha Hyttinen83ada002020-01-30 10:36:33 +020055func (t *Transaction) String() string {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030056 meidstr := "N/A"
57 if t.Meid != nil {
58 meidstr = t.Meid.String()
59 }
60 return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + meidstr + ")"
Juha Hyttinen83ada002020-01-30 10:36:33 +020061}
62
63func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020064 if waittime > 0 {
65 select {
66 case t.EventChan <- event:
67 return true, false
68 case <-time.After(waittime):
69 return false, true
70 }
71 return false, false
72 }
73 t.EventChan <- event
74 return true, false
75}
76
Juha Hyttinen83ada002020-01-30 10:36:33 +020077func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020078 if waittime > 0 {
79 select {
80 case event := <-t.EventChan:
81 return event, false
82 case <-time.After(waittime):
83 return nil, true
84 }
85 }
86 event := <-t.EventChan
87 return event, false
88}
89
Juha Hyttinen83ada002020-01-30 10:36:33 +020090func (t *Transaction) GetMtype() int {
Juha Hyttinen422d0182020-01-17 13:37:05 +020091 t.mutex.Lock()
92 defer t.mutex.Unlock()
93 return t.Mtype
94}
95
Juha Hyttinen83ada002020-01-30 10:36:33 +020096func (t *Transaction) GetMeid() *xapp.RMRMeid {
Juha Hyttinen422d0182020-01-17 13:37:05 +020097 t.mutex.Lock()
98 defer t.mutex.Unlock()
99 if t.Meid != nil {
100 return t.Meid
101 }
102 return nil
103}
104
Anssi Mannila47518ae2021-04-16 09:27:07 +0300105/* // This function is not used. Commented out to get better test coverage result
Juha Hyttinena9bf76c2020-02-05 14:06:57 +0200106func (t *Transaction) GetPayload() *e2ap.PackedData {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200107 t.mutex.Lock()
108 defer t.mutex.Unlock()
109 return t.Payload
110}
Anssi Mannila47518ae2021-04-16 09:27:07 +0300111*/
Juha Hyttinen422d0182020-01-17 13:37:05 +0200112//-----------------------------------------------------------------------------
113//
114//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200115type TransactionSubs struct {
116 Transaction //
117}
118
119func (t *TransactionSubs) String() string {
120 return "transsubs(" + t.Transaction.String() + ")"
121}
122
123func (t *TransactionSubs) Release() {
124 t.mutex.Lock()
125 xapp.Logger.Debug("RELEASE %s", t.String())
126 t.tracker = nil
127 t.mutex.Unlock()
128}
129
130//-----------------------------------------------------------------------------
131//
132//-----------------------------------------------------------------------------
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200133type TransactionXappKey struct {
Konstantinos Archangelofe93b00f2021-06-03 10:00:19 +0000134 InstanceID uint32
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300135 xapp.RmrEndpoint
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200136 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 {
Juha Hyttinen47942b42020-02-27 10:41:43 +0200147 Transaction
Anssi Mannila4c626a22021-02-11 12:50:48 +0200148 XappKey *TransactionXappKey
149 RequestId e2ap.RequestId
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200150}
151
Juha Hyttinen83ada002020-01-30 10:36:33 +0200152func (t *TransactionXapp) String() string {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200153 var transkey string = "transkey(N/A)"
154 if t.XappKey != nil {
155 transkey = t.XappKey.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200156 }
Anssi Mannila4c626a22021-02-11 12:50:48 +0200157 return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.RequestId.InstanceId), 10) + ")"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200158}
159
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300160func (t *TransactionXapp) GetEndpoint() *xapp.RmrEndpoint {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200161 t.mutex.Lock()
162 defer t.mutex.Unlock()
163 if t.XappKey != nil {
164 return &t.XappKey.RmrEndpoint
165 }
166 return nil
167}
168
Juha Hyttinen83ada002020-01-30 10:36:33 +0200169func (t *TransactionXapp) GetXid() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200170 t.mutex.Lock()
171 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200172 if t.XappKey != nil {
173 return t.XappKey.Xid
Juha Hyttinen86a46202020-01-14 12:49:09 +0200174 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200175 return ""
Juha Hyttinen86a46202020-01-14 12:49:09 +0200176}
177
Anssi Mannila47518ae2021-04-16 09:27:07 +0300178/* // This function is not used. Commented out to get better test coverage result
Juha Hyttinen83ada002020-01-30 10:36:33 +0200179func (t *TransactionXapp) GetSrc() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200180 t.mutex.Lock()
181 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200182 if t.XappKey != nil {
183 return t.XappKey.RmrEndpoint.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200184 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200185 return ""
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200186}
Anssi Mannila47518ae2021-04-16 09:27:07 +0300187*/
Juha Hyttinen47942b42020-02-27 10:41:43 +0200188func (t *TransactionXapp) GetSubId() uint32 {
189 t.mutex.Lock()
190 defer t.mutex.Unlock()
Anssi Mannila4c626a22021-02-11 12:50:48 +0200191 return t.RequestId.InstanceId
Juha Hyttinen47942b42020-02-27 10:41:43 +0200192}
193
Juha Hyttinen83ada002020-01-30 10:36:33 +0200194func (t *TransactionXapp) Release() {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200195 t.mutex.Lock()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200196 xapp.Logger.Debug("RELEASE %s", t.String())
Juha Hyttinen31797b42020-01-16 14:05:01 +0200197 tracker := t.tracker
Juha Hyttinen422d0182020-01-17 13:37:05 +0200198 xappkey := t.XappKey
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200199 t.tracker = nil
Juha Hyttinen31797b42020-01-16 14:05:01 +0200200 t.mutex.Unlock()
201
Juha Hyttinen422d0182020-01-17 13:37:05 +0200202 if tracker != nil && xappkey != nil {
Anssi Mannila9c4697f2022-07-04 15:51:38 +0300203 _, err := tracker.UnTrackTransaction(*xappkey)
204 if err != nil {
205 xapp.Logger.Error("tracker.UnTrackTransaction() failed:%s", err.Error())
206 }
Juha Hyttinen31797b42020-01-16 14:05:01 +0200207 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200208}