RICPLT-2983 SubDelResp go asn into use
[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/xapp-frame/pkg/xapp"
25         "strconv"
26         "sync"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 type TransactionXappKey struct {
33         RmrEndpoint
34         Xid string // xapp xid in req
35 }
36
37 func (key *TransactionXappKey) String() string {
38         return key.RmrEndpoint.String() + "/" + key.Xid
39 }
40
41 //-----------------------------------------------------------------------------
42 //
43 //-----------------------------------------------------------------------------
44 type Transaction struct {
45         mutex             sync.Mutex
46         tracker           *Tracker                             //tracker instance
47         Subs              *Subscription                        //related subscription
48         RmrEndpoint       RmrEndpoint                          //xapp endpoint
49         Mtype             int                                  //type of initiating message
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         Payload           []byte                               //packed message to optimize retransmissions
58         PayloadLen        int                                  //packed message len to optimize  retransmissions
59         RespReceived      bool
60         ForwardRespToXapp bool
61 }
62
63 func (t *Transaction) String() string {
64         t.mutex.Lock()
65         defer t.mutex.Unlock()
66         var subId string = "?"
67         if t.Subs != nil {
68                 subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
69         }
70         return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid
71 }
72
73 func (t *Transaction) GetXid() string {
74         t.mutex.Lock()
75         defer t.mutex.Unlock()
76         return t.Xid
77 }
78
79 func (t *Transaction) GetMtype() int {
80         t.mutex.Lock()
81         defer t.mutex.Unlock()
82         return t.Mtype
83 }
84
85 func (t *Transaction) GetMeid() *xapp.RMRMeid {
86         t.mutex.Lock()
87         defer t.mutex.Unlock()
88         if t.Meid != nil {
89                 return t.Meid
90         }
91         return nil
92 }
93
94 func (t *Transaction) GetSrc() string {
95         t.mutex.Lock()
96         defer t.mutex.Unlock()
97         return t.RmrEndpoint.String()
98 }
99
100 func (t *Transaction) CheckResponseReceived() bool {
101         t.mutex.Lock()
102         defer t.mutex.Unlock()
103         if t.RespReceived == false {
104                 t.RespReceived = true
105                 return false
106         }
107         return true
108 }
109
110 func (t *Transaction) RetryTransaction() {
111         t.mutex.Lock()
112         defer t.mutex.Unlock()
113         t.RespReceived = false
114 }
115
116 func (t *Transaction) Release() {
117         xapp.Logger.Info("Transaction: Releasing %s", t)
118         t.mutex.Lock()
119         defer t.mutex.Unlock()
120         if t.Subs != nil {
121                 t.Subs.UnSetTransaction(t)
122         }
123         if t.tracker != nil {
124                 xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
125                 t.tracker.UnTrackTransaction(xappkey)
126         }
127         t.Subs = nil
128         t.tracker = nil
129 }