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 | 9340072 | 2020-01-15 09:25:13 +0200 | [diff] [blame] | 23 | "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer" |
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 | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 45 | mutex sync.Mutex // |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 46 | Seq uint64 //transaction sequence |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 47 | tracker *Tracker //tracker instance |
| 48 | Meid *xapp.RMRMeid //meid transaction related |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 49 | ReqId RequestId // |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 50 | Mtype int //Encoded message type to be send |
| 51 | Payload *packer.PackedData //Encoded message to be send |
| 52 | EventChan chan interface{} |
| 53 | } |
| 54 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 55 | func (t *Transaction) String() string { |
| 56 | return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + "/" + t.ReqId.String() + ")" |
| 57 | } |
| 58 | |
| 59 | func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 60 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 73 | func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 74 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 86 | func (t *Transaction) GetReqId() *RequestId { |
| 87 | t.mutex.Lock() |
| 88 | defer t.mutex.Unlock() |
| 89 | return &t.ReqId |
| 90 | } |
| 91 | |
| 92 | func (t *Transaction) GetMtype() int { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 93 | t.mutex.Lock() |
| 94 | defer t.mutex.Unlock() |
| 95 | return t.Mtype |
| 96 | } |
| 97 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 98 | func (t *Transaction) GetMeid() *xapp.RMRMeid { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 99 | t.mutex.Lock() |
| 100 | defer t.mutex.Unlock() |
| 101 | if t.Meid != nil { |
| 102 | return t.Meid |
| 103 | } |
| 104 | return nil |
| 105 | } |
| 106 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 107 | func (t *Transaction) GetPayload() *packer.PackedData { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 108 | t.mutex.Lock() |
| 109 | defer t.mutex.Unlock() |
| 110 | return t.Payload |
| 111 | } |
| 112 | |
| 113 | //----------------------------------------------------------------------------- |
| 114 | // |
| 115 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 116 | type TransactionSubs struct { |
| 117 | Transaction // |
| 118 | } |
| 119 | |
| 120 | func (t *TransactionSubs) String() string { |
| 121 | return "transsubs(" + t.Transaction.String() + ")" |
| 122 | } |
| 123 | |
| 124 | func (t *TransactionSubs) Release() { |
| 125 | t.mutex.Lock() |
| 126 | xapp.Logger.Debug("RELEASE %s", t.String()) |
| 127 | t.tracker = nil |
| 128 | t.mutex.Unlock() |
| 129 | } |
| 130 | |
| 131 | //----------------------------------------------------------------------------- |
| 132 | // |
| 133 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 134 | type TransactionXappKey struct { |
| 135 | RmrEndpoint |
| 136 | Xid string // xapp xid in req |
| 137 | } |
| 138 | |
| 139 | func (key *TransactionXappKey) String() string { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 140 | return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | //----------------------------------------------------------------------------- |
| 144 | // |
| 145 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 146 | type TransactionXapp struct { |
| 147 | Transaction // |
| 148 | XappKey *TransactionXappKey // |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 151 | func (t *TransactionXapp) String() string { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 152 | var transkey string = "transkey(N/A)" |
| 153 | if t.XappKey != nil { |
| 154 | transkey = t.XappKey.String() |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 155 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 156 | return "transxapp(" + t.Transaction.String() + "/" + transkey + ")" |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 159 | func (t *TransactionXapp) GetEndpoint() *RmrEndpoint { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 160 | t.mutex.Lock() |
| 161 | defer t.mutex.Unlock() |
| 162 | if t.XappKey != nil { |
| 163 | return &t.XappKey.RmrEndpoint |
| 164 | } |
| 165 | return nil |
| 166 | } |
| 167 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 168 | func (t *TransactionXapp) GetXid() string { |
Juha Hyttinen | e406a34 | 2020-01-13 13:02:26 +0200 | [diff] [blame] | 169 | t.mutex.Lock() |
| 170 | defer t.mutex.Unlock() |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 171 | if t.XappKey != nil { |
| 172 | return t.XappKey.Xid |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 173 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 174 | return "" |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 177 | func (t *TransactionXapp) GetSrc() string { |
Juha Hyttinen | e406a34 | 2020-01-13 13:02:26 +0200 | [diff] [blame] | 178 | t.mutex.Lock() |
| 179 | defer t.mutex.Unlock() |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 180 | if t.XappKey != nil { |
| 181 | return t.XappKey.RmrEndpoint.String() |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 182 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 183 | return "" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 186 | func (t *TransactionXapp) Release() { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 187 | t.mutex.Lock() |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame^] | 188 | xapp.Logger.Debug("RELEASE %s", t.String()) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 189 | tracker := t.tracker |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 190 | xappkey := t.XappKey |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 191 | t.tracker = nil |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 192 | t.mutex.Unlock() |
| 193 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 194 | if tracker != nil && xappkey != nil { |
| 195 | tracker.UnTrackTransaction(*xappkey) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 196 | } |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 197 | } |