blob: c7ca81258eb02c7e8684e2137ca4cb1342a75836 [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 Hyttinen9dc5adc2020-08-13 10:02:40 +030055 meidstr := "N/A"
56 if t.Meid != nil {
57 meidstr = t.Meid.String()
58 }
59 return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + meidstr + ")"
Juha Hyttinen83ada002020-01-30 10:36:33 +020060}
61
62func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020063 if waittime > 0 {
64 select {
65 case t.EventChan <- event:
66 return true, false
67 case <-time.After(waittime):
68 return false, true
69 }
70 return false, false
71 }
72 t.EventChan <- event
73 return true, false
74}
75
Juha Hyttinen83ada002020-01-30 10:36:33 +020076func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) {
Juha Hyttinen422d0182020-01-17 13:37:05 +020077 if waittime > 0 {
78 select {
79 case event := <-t.EventChan:
80 return event, false
81 case <-time.After(waittime):
82 return nil, true
83 }
84 }
85 event := <-t.EventChan
86 return event, false
87}
88
Juha Hyttinen83ada002020-01-30 10:36:33 +020089func (t *Transaction) GetMtype() int {
Juha Hyttinen422d0182020-01-17 13:37:05 +020090 t.mutex.Lock()
91 defer t.mutex.Unlock()
92 return t.Mtype
93}
94
Juha Hyttinen83ada002020-01-30 10:36:33 +020095func (t *Transaction) GetMeid() *xapp.RMRMeid {
Juha Hyttinen422d0182020-01-17 13:37:05 +020096 t.mutex.Lock()
97 defer t.mutex.Unlock()
98 if t.Meid != nil {
99 return t.Meid
100 }
101 return nil
102}
103
Juha Hyttinena9bf76c2020-02-05 14:06:57 +0200104func (t *Transaction) GetPayload() *e2ap.PackedData {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200105 t.mutex.Lock()
106 defer t.mutex.Unlock()
107 return t.Payload
108}
109
110//-----------------------------------------------------------------------------
111//
112//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200113type TransactionSubs struct {
114 Transaction //
115}
116
117func (t *TransactionSubs) String() string {
118 return "transsubs(" + t.Transaction.String() + ")"
119}
120
121func (t *TransactionSubs) Release() {
122 t.mutex.Lock()
123 xapp.Logger.Debug("RELEASE %s", t.String())
124 t.tracker = nil
125 t.mutex.Unlock()
126}
127
128//-----------------------------------------------------------------------------
129//
130//-----------------------------------------------------------------------------
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200131type TransactionXappKey struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300132 xapp.RmrEndpoint
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200133 Xid string // xapp xid in req
134}
135
136func (key *TransactionXappKey) String() string {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200137 return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200138}
139
140//-----------------------------------------------------------------------------
141//
142//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +0200143type TransactionXapp struct {
Juha Hyttinen47942b42020-02-27 10:41:43 +0200144 Transaction
Anssi Mannila4c626a22021-02-11 12:50:48 +0200145 XappKey *TransactionXappKey
146 RequestId e2ap.RequestId
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200147}
148
Juha Hyttinen83ada002020-01-30 10:36:33 +0200149func (t *TransactionXapp) String() string {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200150 var transkey string = "transkey(N/A)"
151 if t.XappKey != nil {
152 transkey = t.XappKey.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200153 }
Anssi Mannila4c626a22021-02-11 12:50:48 +0200154 return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.RequestId.InstanceId), 10) + ")"
Juha Hyttinen31797b42020-01-16 14:05:01 +0200155}
156
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +0300157func (t *TransactionXapp) GetEndpoint() *xapp.RmrEndpoint {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200158 t.mutex.Lock()
159 defer t.mutex.Unlock()
160 if t.XappKey != nil {
161 return &t.XappKey.RmrEndpoint
162 }
163 return nil
164}
165
Juha Hyttinen83ada002020-01-30 10:36:33 +0200166func (t *TransactionXapp) GetXid() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200167 t.mutex.Lock()
168 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200169 if t.XappKey != nil {
170 return t.XappKey.Xid
Juha Hyttinen86a46202020-01-14 12:49:09 +0200171 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200172 return ""
Juha Hyttinen86a46202020-01-14 12:49:09 +0200173}
174
Juha Hyttinen83ada002020-01-30 10:36:33 +0200175func (t *TransactionXapp) GetSrc() string {
Juha Hyttinene406a342020-01-13 13:02:26 +0200176 t.mutex.Lock()
177 defer t.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +0200178 if t.XappKey != nil {
179 return t.XappKey.RmrEndpoint.String()
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200180 }
Juha Hyttinen422d0182020-01-17 13:37:05 +0200181 return ""
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200182}
183
Juha Hyttinen47942b42020-02-27 10:41:43 +0200184func (t *TransactionXapp) GetSubId() uint32 {
185 t.mutex.Lock()
186 defer t.mutex.Unlock()
Anssi Mannila4c626a22021-02-11 12:50:48 +0200187 return t.RequestId.InstanceId
Juha Hyttinen47942b42020-02-27 10:41:43 +0200188}
189
Juha Hyttinen83ada002020-01-30 10:36:33 +0200190func (t *TransactionXapp) Release() {
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200191 t.mutex.Lock()
Juha Hyttinen83ada002020-01-30 10:36:33 +0200192 xapp.Logger.Debug("RELEASE %s", t.String())
Juha Hyttinen31797b42020-01-16 14:05:01 +0200193 tracker := t.tracker
Juha Hyttinen422d0182020-01-17 13:37:05 +0200194 xappkey := t.XappKey
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200195 t.tracker = nil
Juha Hyttinen31797b42020-01-16 14:05:01 +0200196 t.mutex.Unlock()
197
Juha Hyttinen422d0182020-01-17 13:37:05 +0200198 if tracker != nil && xappkey != nil {
199 tracker.UnTrackTransaction(*xappkey)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200200 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200201}