b0da077d4a56cf99179ad24e7578e9ca2288807f
[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 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         Mtype             int                                  //type of initiating message
51         Xid               string                               //xapp xid in req
52         Meid              *xapp.RMRMeid                        //meid transaction related
53         SubReqMsg         *e2ap.E2APSubscriptionRequest        //SubReq TODO: maybe own transactions per type
54         SubRespMsg        *e2ap.E2APSubscriptionResponse       //SubResp TODO: maybe own transactions per type
55         SubFailMsg        *e2ap.E2APSubscriptionFailure        //SubFail TODO: maybe own transactions per type
56         SubDelReqMsg      *e2ap.E2APSubscriptionDeleteRequest  //SubDelReq TODO: maybe own transactions per type
57         SubDelRespMsg     *e2ap.E2APSubscriptionDeleteResponse //SubDelResp TODO: maybe own transactions per type
58         SubDelFailMsg     *e2ap.E2APSubscriptionDeleteFailure  //SubDelFail TODO: maybe own transactions per type
59         Payload           *packer.PackedData                   //Encoded message to be send. Optimized
60         RespReceived      bool
61         ForwardRespToXapp bool
62 }
63
64 func (t *Transaction) String() string {
65         t.mutex.Lock()
66         defer t.mutex.Unlock()
67         var subId string = "?"
68         if t.Subs != nil {
69                 subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
70         }
71         return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid
72 }
73
74 func (t *Transaction) GetXid() string {
75         t.mutex.Lock()
76         defer t.mutex.Unlock()
77         return t.Xid
78 }
79
80 func (t *Transaction) GetMtype() int {
81         t.mutex.Lock()
82         defer t.mutex.Unlock()
83         return t.Mtype
84 }
85
86 func (t *Transaction) GetMeid() *xapp.RMRMeid {
87         t.mutex.Lock()
88         defer t.mutex.Unlock()
89         if t.Meid != nil {
90                 return t.Meid
91         }
92         return nil
93 }
94
95 func (t *Transaction) GetSrc() string {
96         t.mutex.Lock()
97         defer t.mutex.Unlock()
98         return t.RmrEndpoint.String()
99 }
100
101 func (t *Transaction) CheckResponseReceived() bool {
102         t.mutex.Lock()
103         defer t.mutex.Unlock()
104         if t.RespReceived == false {
105                 t.RespReceived = true
106                 return false
107         }
108         return true
109 }
110
111 func (t *Transaction) RetryTransaction() {
112         t.mutex.Lock()
113         defer t.mutex.Unlock()
114         t.RespReceived = false
115 }
116
117 func (t *Transaction) Release() {
118         xapp.Logger.Info("Transaction: Releasing %s", t)
119         t.mutex.Lock()
120         defer t.mutex.Unlock()
121         if t.Subs != nil {
122                 t.Subs.UnSetTransaction(t)
123         }
124         if t.tracker != nil {
125                 xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
126                 t.tracker.UnTrackTransaction(xappkey)
127         }
128         t.Subs = nil
129         t.tracker = nil
130 }