Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package control |
| 21 | |
| 22 | import ( |
Juha Hyttinen | a9bf76c | 2020-02-05 14:06:57 +0200 | [diff] [blame] | 23 | "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 24 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 25 | "strconv" |
| 26 | "sync" |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 27 | "time" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | //----------------------------------------------------------------------------- |
| 31 | // |
| 32 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 33 | type TransactionIf interface { |
| 34 | String() string |
| 35 | Release() |
| 36 | SendEvent(interface{}, time.Duration) (bool, bool) |
| 37 | WaitEvent(time.Duration) (interface{}, bool) |
| 38 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 39 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 40 | //----------------------------------------------------------------------------- |
| 41 | // |
| 42 | //----------------------------------------------------------------------------- |
| 43 | |
| 44 | type Transaction struct { |
Juha Hyttinen | a9bf76c | 2020-02-05 14:06:57 +0200 | [diff] [blame] | 45 | mutex sync.Mutex // |
| 46 | Seq uint64 //transaction sequence |
| 47 | tracker *Tracker //tracker instance |
| 48 | Meid *xapp.RMRMeid //meid transaction related |
Juha Hyttinen | a9bf76c | 2020-02-05 14:06:57 +0200 | [diff] [blame] | 49 | Mtype int //Encoded message type to be send |
| 50 | Payload *e2ap.PackedData //Encoded message to be send |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 51 | EventChan chan interface{} |
| 52 | } |
| 53 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 54 | func (t *Transaction) String() string { |
Juha Hyttinen | 9dc5adc | 2020-08-13 10:02:40 +0300 | [diff] [blame] | 55 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 63 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 76 | func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 77 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 89 | func (t *Transaction) GetMtype() int { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 90 | t.mutex.Lock() |
| 91 | defer t.mutex.Unlock() |
| 92 | return t.Mtype |
| 93 | } |
| 94 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 95 | func (t *Transaction) GetMeid() *xapp.RMRMeid { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 96 | t.mutex.Lock() |
| 97 | defer t.mutex.Unlock() |
| 98 | if t.Meid != nil { |
| 99 | return t.Meid |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
Juha Hyttinen | a9bf76c | 2020-02-05 14:06:57 +0200 | [diff] [blame] | 104 | func (t *Transaction) GetPayload() *e2ap.PackedData { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 105 | t.mutex.Lock() |
| 106 | defer t.mutex.Unlock() |
| 107 | return t.Payload |
| 108 | } |
| 109 | |
| 110 | //----------------------------------------------------------------------------- |
| 111 | // |
| 112 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 113 | type TransactionSubs struct { |
| 114 | Transaction // |
| 115 | } |
| 116 | |
| 117 | func (t *TransactionSubs) String() string { |
| 118 | return "transsubs(" + t.Transaction.String() + ")" |
| 119 | } |
| 120 | |
| 121 | func (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 Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 131 | type TransactionXappKey struct { |
Juha Hyttinen | 9dc5adc | 2020-08-13 10:02:40 +0300 | [diff] [blame] | 132 | xapp.RmrEndpoint |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 133 | Xid string // xapp xid in req |
| 134 | } |
| 135 | |
| 136 | func (key *TransactionXappKey) String() string { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 137 | return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | //----------------------------------------------------------------------------- |
| 141 | // |
| 142 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 143 | type TransactionXapp struct { |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 144 | Transaction |
Anssi Mannila | 4c626a2 | 2021-02-11 12:50:48 +0200 | [diff] [blame^] | 145 | XappKey *TransactionXappKey |
| 146 | RequestId e2ap.RequestId |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 149 | func (t *TransactionXapp) String() string { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 150 | var transkey string = "transkey(N/A)" |
| 151 | if t.XappKey != nil { |
| 152 | transkey = t.XappKey.String() |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 153 | } |
Anssi Mannila | 4c626a2 | 2021-02-11 12:50:48 +0200 | [diff] [blame^] | 154 | return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.RequestId.InstanceId), 10) + ")" |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Juha Hyttinen | 9dc5adc | 2020-08-13 10:02:40 +0300 | [diff] [blame] | 157 | func (t *TransactionXapp) GetEndpoint() *xapp.RmrEndpoint { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 158 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 166 | func (t *TransactionXapp) GetXid() string { |
Juha Hyttinen | e406a34 | 2020-01-13 13:02:26 +0200 | [diff] [blame] | 167 | t.mutex.Lock() |
| 168 | defer t.mutex.Unlock() |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 169 | if t.XappKey != nil { |
| 170 | return t.XappKey.Xid |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 171 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 172 | return "" |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 175 | func (t *TransactionXapp) GetSrc() string { |
Juha Hyttinen | e406a34 | 2020-01-13 13:02:26 +0200 | [diff] [blame] | 176 | t.mutex.Lock() |
| 177 | defer t.mutex.Unlock() |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 178 | if t.XappKey != nil { |
| 179 | return t.XappKey.RmrEndpoint.String() |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 180 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 181 | return "" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 184 | func (t *TransactionXapp) GetSubId() uint32 { |
| 185 | t.mutex.Lock() |
| 186 | defer t.mutex.Unlock() |
Anssi Mannila | 4c626a2 | 2021-02-11 12:50:48 +0200 | [diff] [blame^] | 187 | return t.RequestId.InstanceId |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 190 | func (t *TransactionXapp) Release() { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 191 | t.mutex.Lock() |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 192 | xapp.Logger.Debug("RELEASE %s", t.String()) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 193 | tracker := t.tracker |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 194 | xappkey := t.XappKey |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 195 | t.tracker = nil |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 196 | t.mutex.Unlock() |
| 197 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 198 | if tracker != nil && xappkey != nil { |
| 199 | tracker.UnTrackTransaction(*xappkey) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 200 | } |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 201 | } |