blob: 56dff1d912ba3e0729b9d917c099f79c40c368ec [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 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 Hyttinena9bf76c2020-02-05 14:06:57 +020045 mutex sync.Mutex //
46 Seq uint64 //transaction sequence
47 tracker *Tracker //tracker instance
48 Meid *xapp.RMRMeid //meid transaction related
Juha Hyttinena9bf76c2020-02-05 14:06:57 +020049 Mtype int //Encoded message type to be send
50 Payload *e2ap.PackedData //Encoded message to be send
Juha Hyttinen422d0182020-01-17 13:37:05 +020051 EventChan chan interface{}
52}
53
Juha Hyttinen83ada002020-01-30 10:36:33 +020054func (t *Transaction) String() string {
Juha Hyttinen47942b42020-02-27 10:41:43 +020055 return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + ")"
Juha Hyttinen83ada002020-01-30 10:36:33 +020056}
57
58func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020059 if waittime > 0 {
60 select {
61 case t.EventChan <- event:
62 return true, false
63 case <-time.After(waittime):
64 return false, true
65 }
66 return false, false
67 }
68 t.EventChan <- event
69 return true, false
70}
71
Juha Hyttinen83ada002020-01-30 10:36:33 +020072func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020073 if waittime > 0 {
74 select {
75 case event := <-t.EventChan:
76 return event, false
77 case <-time.After(waittime):
78 return nil, true
79 }
80 }
81 event := <-t.EventChan
82 return event, false
83}
84
Juha Hyttinen83ada002020-01-30 10:36:33 +020085func (t *Transaction) GetMtype() int {
Juha Hyttinen422d0182020-01-17 13:37:05 +020086 t.mutex.Lock()
87 defer t.mutex.Unlock()
88 return t.Mtype
89}
90
Juha Hyttinen83ada002020-01-30 10:36:33 +020091func (t *Transaction) GetMeid() *xapp.RMRMeid {
Juha Hyttinen422d0182020-01-17 13:37:05 +020092 t.mutex.Lock()
93 defer t.mutex.Unlock()
94 if t.Meid != nil {
95 return t.Meid
96 }
97 return nil
98}
99
Juha Hyttinena9bf76c2020-02-05 14:06:57 +0200100func (t *Transaction) GetPayload() *e2ap.PackedData {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200101 t.mutex.Lock()
102 defer t.mutex.Unlock()
103 return t.Payload
104}
105
106//-----------------------------------------------------------------------------
107//
108//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200109type TransactionSubs struct {
110 Transaction //
111}
112
113func (t *TransactionSubs) String() string {
114 return "transsubs(" + t.Transaction.String() + ")"
115}
116
117func (t *TransactionSubs) Release() {
118 t.mutex.Lock()
119 xapp.Logger.Debug("RELEASE %s", t.String())
120 t.tracker = nil
121 t.mutex.Unlock()
122}
123
124//-----------------------------------------------------------------------------
125//
126//-----------------------------------------------------------------------------
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200127type TransactionXappKey struct {
128 RmrEndpoint
129 Xid string // xapp xid in req
130}
131
132func (key *TransactionXappKey) String() string {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200133 return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200134}
135
136//-----------------------------------------------------------------------------
137//
138//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200139type TransactionXapp struct {
Juha Hyttinen47942b42020-02-27 10:41:43 +0200140 Transaction
141 XappKey *TransactionXappKey
142 SubId uint32
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200143}
144
Juha Hyttinen83ada002020-01-30 10:36:33 +0200145func (t *TransactionXapp) String() string {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200146 var transkey string = "transkey(N/A)"
147 if t.XappKey != nil {
148 transkey = t.XappKey.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200149 }
Juha Hyttinen47942b42020-02-27 10:41:43 +0200150 return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.SubId), 10) + ")"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200151}
152
Juha Hyttinen83ada002020-01-30 10:36:33 +0200153func (t *TransactionXapp) GetEndpoint() *RmrEndpoint {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200154 t.mutex.Lock()
155 defer t.mutex.Unlock()
156 if t.XappKey != nil {
157 return &t.XappKey.RmrEndpoint
158 }
159 return nil
160}
161
Juha Hyttinen83ada002020-01-30 10:36:33 +0200162func (t *TransactionXapp) GetXid() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200163 t.mutex.Lock()
164 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200165 if t.XappKey != nil {
166 return t.XappKey.Xid
Juha Hyttinen86a46202020-01-14 12:49:09 +0200167 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200168 return ""
Juha Hyttinen86a46202020-01-14 12:49:09 +0200169}
170
Juha Hyttinen83ada002020-01-30 10:36:33 +0200171func (t *TransactionXapp) GetSrc() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200172 t.mutex.Lock()
173 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200174 if t.XappKey != nil {
175 return t.XappKey.RmrEndpoint.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200176 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200177 return ""
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200178}
179
Juha Hyttinen47942b42020-02-27 10:41:43 +0200180func (t *TransactionXapp) GetSubId() uint32 {
181 t.mutex.Lock()
182 defer t.mutex.Unlock()
183 return t.SubId
184}
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}