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 | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 55 | return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + ")" |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 59 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 72 | func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 73 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 85 | func (t *Transaction) GetMtype() int { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 86 | t.mutex.Lock() |
| 87 | defer t.mutex.Unlock() |
| 88 | return t.Mtype |
| 89 | } |
| 90 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 91 | func (t *Transaction) GetMeid() *xapp.RMRMeid { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 92 | t.mutex.Lock() |
| 93 | defer t.mutex.Unlock() |
| 94 | if t.Meid != nil { |
| 95 | return t.Meid |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
Juha Hyttinen | a9bf76c | 2020-02-05 14:06:57 +0200 | [diff] [blame] | 100 | func (t *Transaction) GetPayload() *e2ap.PackedData { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 101 | t.mutex.Lock() |
| 102 | defer t.mutex.Unlock() |
| 103 | return t.Payload |
| 104 | } |
| 105 | |
| 106 | //----------------------------------------------------------------------------- |
| 107 | // |
| 108 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 109 | type TransactionSubs struct { |
| 110 | Transaction // |
| 111 | } |
| 112 | |
| 113 | func (t *TransactionSubs) String() string { |
| 114 | return "transsubs(" + t.Transaction.String() + ")" |
| 115 | } |
| 116 | |
| 117 | func (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 Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 127 | type TransactionXappKey struct { |
| 128 | RmrEndpoint |
| 129 | Xid string // xapp xid in req |
| 130 | } |
| 131 | |
| 132 | func (key *TransactionXappKey) String() string { |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 133 | return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | //----------------------------------------------------------------------------- |
| 137 | // |
| 138 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 139 | type TransactionXapp struct { |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 140 | Transaction |
| 141 | XappKey *TransactionXappKey |
| 142 | SubId uint32 |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 145 | func (t *TransactionXapp) String() string { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 146 | var transkey string = "transkey(N/A)" |
| 147 | if t.XappKey != nil { |
| 148 | transkey = t.XappKey.String() |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 149 | } |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 150 | return "transxapp(" + t.Transaction.String() + "/" + transkey + "/" + strconv.FormatUint(uint64(t.SubId), 10) + ")" |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 153 | func (t *TransactionXapp) GetEndpoint() *RmrEndpoint { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 154 | 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 Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 162 | func (t *TransactionXapp) GetXid() string { |
Juha Hyttinen | e406a34 | 2020-01-13 13:02:26 +0200 | [diff] [blame] | 163 | t.mutex.Lock() |
| 164 | defer t.mutex.Unlock() |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 165 | if t.XappKey != nil { |
| 166 | return t.XappKey.Xid |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 167 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 168 | return "" |
Juha Hyttinen | 86a4620 | 2020-01-14 12:49:09 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 171 | func (t *TransactionXapp) GetSrc() string { |
Juha Hyttinen | e406a34 | 2020-01-13 13:02:26 +0200 | [diff] [blame] | 172 | t.mutex.Lock() |
| 173 | defer t.mutex.Unlock() |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 174 | if t.XappKey != nil { |
| 175 | return t.XappKey.RmrEndpoint.String() |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 176 | } |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 177 | return "" |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 180 | func (t *TransactionXapp) GetSubId() uint32 { |
| 181 | t.mutex.Lock() |
| 182 | defer t.mutex.Unlock() |
| 183 | return t.SubId |
| 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 | } |