RICPLT-2962 Preparation for subs merge
[ric-plt/submgr.git] / pkg / control / transaction.go
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         "strconv"
24         "sync"
25 )
26
27 //-----------------------------------------------------------------------------
28 //
29 //-----------------------------------------------------------------------------
30 type TransactionXappKey struct {
31         RmrEndpoint
32         Xid string // xapp xid in req
33 }
34
35 func (key *TransactionXappKey) String() string {
36         return key.RmrEndpoint.String() + "/" + key.Xid
37 }
38
39 //-----------------------------------------------------------------------------
40 //
41 //-----------------------------------------------------------------------------
42 type Transaction struct {
43         mutex             sync.Mutex
44         tracker           *Tracker // tracker instance
45         Subs              *Subscription
46         RmrEndpoint       RmrEndpoint
47         Mtype             int
48         Xid               string     // xapp xid in req
49         OrigParams        *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) GetXid() string {
65         t.mutex.Lock()
66         defer t.mutex.Unlock()
67         return t.Xid
68 }
69
70 func (t *Transaction) GetMtype() int {
71         t.mutex.Lock()
72         defer t.mutex.Unlock()
73         return t.Mtype
74 }
75
76 func (t *Transaction) GetSrc() string {
77         t.mutex.Lock()
78         defer t.mutex.Unlock()
79         return t.RmrEndpoint.String()
80 }
81
82 func (t *Transaction) CheckResponseReceived() bool {
83         t.mutex.Lock()
84         defer t.mutex.Unlock()
85         if t.RespReceived == false {
86                 t.RespReceived = true
87                 return false
88         }
89         return true
90 }
91
92 func (t *Transaction) RetryTransaction() {
93         t.mutex.Lock()
94         defer t.mutex.Unlock()
95         t.RespReceived = false
96 }
97
98 func (t *Transaction) Release() {
99         t.mutex.Lock()
100         defer t.mutex.Unlock()
101         if t.Subs != nil {
102                 t.Subs.UnSetTransaction(t)
103         }
104         if t.tracker != nil {
105                 xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
106                 t.tracker.UnTrackTransaction(xappkey)
107         }
108         t.Subs = nil
109         t.tracker = nil
110 }