2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
24 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
32 type TransactionXappKey struct {
34 Xid string // xapp xid in req
37 func (key *TransactionXappKey) String() string {
38 return key.RmrEndpoint.String() + "/" + key.Xid
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 type Transaction struct {
45 tracker *Tracker // tracker instance
47 RmrEndpoint RmrEndpoint
48 Xid string // xapp xid in req
49 OrigParams *xapp.RMRParams // request orginal params
51 ForwardRespToXapp bool
55 func (t *Transaction) String() string {
57 defer t.mutex.Unlock()
58 var subId string = "?"
60 subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
62 return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid
65 func (t *Transaction) CheckResponseReceived() bool {
67 defer t.mutex.Unlock()
68 if t.RespReceived == false {
75 func (t *Transaction) RetryTransaction() {
77 defer t.mutex.Unlock()
78 t.RespReceived = false
81 func (t *Transaction) Release() {
83 defer t.mutex.Unlock()
85 t.Subs.UnSetTransaction(t)
88 xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
89 t.tracker.UnTrackTransaction(xappkey)
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 transactionXappTable map[TransactionXappKey]*Transaction
103 func (t *Tracker) Init() {
104 t.transactionXappTable = make(map[TransactionXappKey]*Transaction)
107 func (t *Tracker) TrackTransaction(subs *Subscription, endpoint RmrEndpoint, params *xapp.RMRParams, respReceived bool, forwardRespToXapp bool) (*Transaction, error) {
109 trans := &Transaction{
112 RmrEndpoint: endpoint,
115 RespReceived: respReceived,
116 ForwardRespToXapp: forwardRespToXapp,
120 defer t.mutex.Unlock()
122 xappkey := TransactionXappKey{endpoint, params.Xid}
123 if _, ok := t.transactionXappTable[xappkey]; ok {
124 err := fmt.Errorf("Tracker: Similar transaction with xappkey %s is ongoing, transaction %s not created ", xappkey, trans)
128 if subs.SetTransaction(trans) == false {
129 othTrans := subs.GetTransaction()
130 err := fmt.Errorf("Tracker: Subscription %s got already transaction ongoing: %s, transaction %s not created", subs, othTrans, trans)
134 if (trans.Subs.RmrEndpoint.Addr != trans.RmrEndpoint.Addr) || (trans.Subs.RmrEndpoint.Port != trans.RmrEndpoint.Port) {
135 err := fmt.Errorf("Tracker: Subscription endpoint %s mismatch with trans: %s", subs, trans)
136 trans.Subs.UnSetTransaction(nil)
141 t.transactionXappTable[xappkey] = trans
145 func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
147 defer t.mutex.Unlock()
148 if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
149 delete(t.transactionXappTable, xappKey)
152 return nil, fmt.Errorf("Tracker: No record for xappkey %s", xappKey)