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 ( |
| 23 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 24 | "strconv" |
| 25 | "sync" |
| 26 | ) |
| 27 | |
| 28 | //----------------------------------------------------------------------------- |
| 29 | // |
| 30 | //----------------------------------------------------------------------------- |
| 31 | type TransactionXappKey struct { |
| 32 | RmrEndpoint |
| 33 | Xid string // xapp xid in req |
| 34 | } |
| 35 | |
| 36 | func (key *TransactionXappKey) String() string { |
| 37 | return key.RmrEndpoint.String() + "/" + key.Xid |
| 38 | } |
| 39 | |
| 40 | //----------------------------------------------------------------------------- |
| 41 | // |
| 42 | //----------------------------------------------------------------------------- |
| 43 | type Transaction struct { |
| 44 | mutex sync.Mutex |
| 45 | tracker *Tracker // tracker instance |
| 46 | Subs *Subscription |
| 47 | RmrEndpoint RmrEndpoint |
| 48 | Xid string // xapp xid in req |
| 49 | OrigParams *xapp.RMRParams // request orginal params |
| 50 | RespReceived bool |
| 51 | ForwardRespToXapp bool |
| 52 | } |
| 53 | |
| 54 | func (t *Transaction) String() string { |
| 55 | t.mutex.Lock() |
| 56 | defer t.mutex.Unlock() |
| 57 | var subId string = "?" |
| 58 | if t.Subs != nil { |
| 59 | subId = strconv.FormatUint(uint64(t.Subs.Seq), 10) |
| 60 | } |
| 61 | return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid |
| 62 | } |
| 63 | |
| 64 | func (t *Transaction) CheckResponseReceived() bool { |
| 65 | t.mutex.Lock() |
| 66 | defer t.mutex.Unlock() |
| 67 | if t.RespReceived == false { |
| 68 | t.RespReceived = true |
| 69 | return false |
| 70 | } |
| 71 | return true |
| 72 | } |
| 73 | |
| 74 | func (t *Transaction) RetryTransaction() { |
| 75 | t.mutex.Lock() |
| 76 | defer t.mutex.Unlock() |
| 77 | t.RespReceived = false |
| 78 | } |
| 79 | |
| 80 | func (t *Transaction) Release() { |
| 81 | t.mutex.Lock() |
| 82 | defer t.mutex.Unlock() |
| 83 | if t.Subs != nil { |
| 84 | t.Subs.UnSetTransaction(t) |
| 85 | } |
| 86 | if t.tracker != nil { |
| 87 | xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid} |
| 88 | t.tracker.UnTrackTransaction(xappkey) |
| 89 | } |
| 90 | t.Subs = nil |
| 91 | t.tracker = nil |
| 92 | } |