9adaeca6067090c1cfeb99fef9f11263fa9cb5e1
[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         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
24         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/packer"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "strconv"
27         "sync"
28 )
29
30 //-----------------------------------------------------------------------------
31 //
32 //-----------------------------------------------------------------------------
33 type TransactionXappKey struct {
34         RmrEndpoint
35         Xid string // xapp xid in req
36 }
37
38 func (key *TransactionXappKey) String() string {
39         return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")"
40 }
41
42 //-----------------------------------------------------------------------------
43 //
44 //-----------------------------------------------------------------------------
45 type Transaction struct {
46         mutex             sync.Mutex
47         tracker           *Tracker                             //tracker instance
48         Subs              *Subscription                        //related subscription
49         RmrEndpoint       RmrEndpoint                          //xapp endpoint
50         Xid               string                               //xapp xid in req
51         Meid              *xapp.RMRMeid                        //meid transaction related
52         SubReqMsg         *e2ap.E2APSubscriptionRequest        //SubReq TODO: maybe own transactions per type
53         SubRespMsg        *e2ap.E2APSubscriptionResponse       //SubResp TODO: maybe own transactions per type
54         SubFailMsg        *e2ap.E2APSubscriptionFailure        //SubFail TODO: maybe own transactions per type
55         SubDelReqMsg      *e2ap.E2APSubscriptionDeleteRequest  //SubDelReq TODO: maybe own transactions per type
56         SubDelRespMsg     *e2ap.E2APSubscriptionDeleteResponse //SubDelResp TODO: maybe own transactions per type
57         SubDelFailMsg     *e2ap.E2APSubscriptionDeleteFailure  //SubDelFail TODO: maybe own transactions per type
58         Mtype             int                                  //Encoded message type to be send
59         Payload           *packer.PackedData                   //Encoded message to be send
60         RespReceived      bool
61         ForwardRespToXapp bool
62 }
63
64 func (t *Transaction) StringImpl() string {
65         var subId string = "?"
66         if t.Subs != nil {
67                 subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
68         }
69         return "trans(" + t.RmrEndpoint.String() + "/" + t.Xid + "/" + t.Meid.RanName + "/" + subId + ")"
70 }
71
72 func (t *Transaction) String() string {
73         t.mutex.Lock()
74         defer t.mutex.Unlock()
75         return t.StringImpl()
76 }
77
78 func (t *Transaction) GetXid() string {
79         t.mutex.Lock()
80         defer t.mutex.Unlock()
81         return t.Xid
82 }
83
84 func (t *Transaction) GetMtype() int {
85         t.mutex.Lock()
86         defer t.mutex.Unlock()
87         return t.Mtype
88 }
89
90 func (t *Transaction) GetMeid() *xapp.RMRMeid {
91         t.mutex.Lock()
92         defer t.mutex.Unlock()
93         if t.Meid != nil {
94                 return t.Meid
95         }
96         return nil
97 }
98
99 func (t *Transaction) GetSrc() string {
100         t.mutex.Lock()
101         defer t.mutex.Unlock()
102         return t.RmrEndpoint.String()
103 }
104
105 func (t *Transaction) CheckResponseReceived() bool {
106         t.mutex.Lock()
107         defer t.mutex.Unlock()
108         if t.RespReceived == false {
109                 t.RespReceived = true
110                 return false
111         }
112         return true
113 }
114
115 func (t *Transaction) RetryTransaction() {
116         t.mutex.Lock()
117         defer t.mutex.Unlock()
118         t.RespReceived = false
119 }
120
121 func (t *Transaction) Release() {
122         t.mutex.Lock()
123         subs := t.Subs
124         tracker := t.tracker
125         xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
126         t.Subs = nil
127         t.tracker = nil
128         t.mutex.Unlock()
129
130         if subs != nil {
131                 subs.UnSetTransaction(t)
132         }
133         if tracker != nil {
134                 tracker.UnTrackTransaction(xappkey)
135         }
136 }