blob: 1298925e3a6401ac80c565a83c6f6ed637ee332a [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 Hyttinena9bf76c2020-02-05 14:06:57 +020023 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
Juha Hyttinend708a432020-03-09 09:37:06 +020024 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks"
Juha Hyttinen86a46202020-01-14 12:49:09 +020025 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Juha Hyttinen0388dd92020-01-09 14:14:16 +020026 "strconv"
27 "sync"
Juha Hyttinen422d0182020-01-17 13:37:05 +020028 "time"
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 Hyttinend708a432020-03-09 09:37:06 +020056 return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + (&xapptweaks.RMRMeid{t.Meid}).String() + ")"
Juha Hyttinen83ada002020-01-30 10:36:33 +020057}
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) GetMtype() int {
Juha Hyttinen422d0182020-01-17 13:37:05 +020087 t.mutex.Lock()
88 defer t.mutex.Unlock()
89 return t.Mtype
90}
91
Juha Hyttinen83ada002020-01-30 10:36:33 +020092func (t *Transaction) GetMeid() *xapp.RMRMeid {
Juha Hyttinen422d0182020-01-17 13:37:05 +020093 t.mutex.Lock()
94 defer t.mutex.Unlock()
95 if t.Meid != nil {
96 return t.Meid
97 }
98 return nil
99}
100
Juha Hyttinena9bf76c2020-02-05 14:06:57 +0200101func (t *Transaction) GetPayload() *e2ap.PackedData {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200102 t.mutex.Lock()
103 defer t.mutex.Unlock()
104 return t.Payload
105}
106
107//-----------------------------------------------------------------------------
108//
109//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200110type TransactionSubs struct {
111 Transaction //
112}
113
114func (t *TransactionSubs) String() string {
115 return "transsubs(" + t.Transaction.String() + ")"
116}
117
118func (t *TransactionSubs) Release() {
119 t.mutex.Lock()
120 xapp.Logger.Debug("RELEASE %s", t.String())
121 t.tracker = nil
122 t.mutex.Unlock()
123}
124
125//-----------------------------------------------------------------------------
126//
127//-----------------------------------------------------------------------------
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200128type TransactionXappKey struct {
129 RmrEndpoint
130 Xid string // xapp xid in req
131}
132
133func (key *TransactionXappKey) String() string {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200134 return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200135}
136
137//-----------------------------------------------------------------------------
138//
139//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200140type TransactionXapp struct {
Juha Hyttinen47942b42020-02-27 10:41:43 +0200141 Transaction
142 XappKey *TransactionXappKey
143 SubId uint32
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200144}
145
Juha Hyttinen83ada002020-01-30 10:36:33 +0200146func (t *TransactionXapp) String() string {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200147 var transkey string = "transkey(N/A)"
148 if t.XappKey != nil {
149 transkey = t.XappKey.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200150 }
Juha Hyttinen47942b42020-02-27 10:41:43 +0200151 return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.SubId), 10) + ")"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200152}
153
Juha Hyttinen83ada002020-01-30 10:36:33 +0200154func (t *TransactionXapp) GetEndpoint() *RmrEndpoint {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200155 t.mutex.Lock()
156 defer t.mutex.Unlock()
157 if t.XappKey != nil {
158 return &t.XappKey.RmrEndpoint
159 }
160 return nil
161}
162
Juha Hyttinen83ada002020-01-30 10:36:33 +0200163func (t *TransactionXapp) GetXid() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200164 t.mutex.Lock()
165 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200166 if t.XappKey != nil {
167 return t.XappKey.Xid
Juha Hyttinen86a46202020-01-14 12:49:09 +0200168 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200169 return ""
Juha Hyttinen86a46202020-01-14 12:49:09 +0200170}
171
Juha Hyttinen83ada002020-01-30 10:36:33 +0200172func (t *TransactionXapp) GetSrc() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200173 t.mutex.Lock()
174 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200175 if t.XappKey != nil {
176 return t.XappKey.RmrEndpoint.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200177 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200178 return ""
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200179}
180
Juha Hyttinen47942b42020-02-27 10:41:43 +0200181func (t *TransactionXapp) GetSubId() uint32 {
182 t.mutex.Lock()
183 defer t.mutex.Unlock()
184 return t.SubId
185}
186
Juha Hyttinen83ada002020-01-30 10:36:33 +0200187func (t *TransactionXapp) Release() {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200188 t.mutex.Lock()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200189 xapp.Logger.Debug("RELEASE %s", t.String())
Juha Hyttinen31797b42020-01-16 14:05:01 +0200190 tracker := t.tracker
Juha Hyttinen422d0182020-01-17 13:37:05 +0200191 xappkey := t.XappKey
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200192 t.tracker = nil
Juha Hyttinen31797b42020-01-16 14:05:01 +0200193 t.mutex.Unlock()
194
Juha Hyttinen422d0182020-01-17 13:37:05 +0200195 if tracker != nil && xappkey != nil {
196 tracker.UnTrackTransaction(*xappkey)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200197 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200198}