RICPLT-2980 SubResp 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         SubDelReqMsg      *e2ap.E2APSubscriptionDeleteRequest //SubDelReq TODO: maybe own transactions per type
55         Payload           []byte                              //packed message to optimize retransmissions
56         PayloadLen        int                                 //packed message len to optimize  retransmissions
57         RespReceived      bool
58         ForwardRespToXapp bool
59 }
60
61 func (t *Transaction) String() string {
62         t.mutex.Lock()
63         defer t.mutex.Unlock()
64         var subId string = "?"
65         if t.Subs != nil {
66                 subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
67         }
68         return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid
69 }
70
71 func (t *Transaction) GetXid() string {
72         t.mutex.Lock()
73         defer t.mutex.Unlock()
74         return t.Xid
75 }
76
77 func (t *Transaction) GetMtype() int {
78         t.mutex.Lock()
79         defer t.mutex.Unlock()
80         return t.Mtype
81 }
82
83 func (t *Transaction) GetMeid() *xapp.RMRMeid {
84         t.mutex.Lock()
85         defer t.mutex.Unlock()
86         if t.Meid != nil {
87                 return t.Meid
88         }
89         return nil
90 }
91
92 func (t *Transaction) GetSrc() string {
93         t.mutex.Lock()
94         defer t.mutex.Unlock()
95         return t.RmrEndpoint.String()
96 }
97
98 func (t *Transaction) CheckResponseReceived() bool {
99         t.mutex.Lock()
100         defer t.mutex.Unlock()
101         if t.RespReceived == false {
102                 t.RespReceived = true
103                 return false
104         }
105         return true
106 }
107
108 func (t *Transaction) RetryTransaction() {
109         t.mutex.Lock()
110         defer t.mutex.Unlock()
111         t.RespReceived = false
112 }
113
114 func (t *Transaction) Release() {
115         t.mutex.Lock()
116         defer t.mutex.Unlock()
117         if t.Subs != nil {
118                 t.Subs.UnSetTransaction(t)
119         }
120         if t.tracker != nil {
121                 xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
122                 t.tracker.UnTrackTransaction(xappkey)
123         }
124         t.Subs = nil
125         t.tracker = nil
126 }