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