1152520ec86cb613016296a4c832412705656abc
[ric-plt/submgr.git] / pkg / control / subscription.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
26         "sync"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 type Subscription struct {
33         mutex            sync.Mutex                    // Lock
34         valid            bool                          // valid
35         registry         *Registry                     // Registry
36         ReqId            RequestId                     // ReqId (Requestor Id + Seq Nro a.k.a subsid)
37         Meid             *xapp.RMRMeid                 // Meid/RanName
38         EpList           xapp.RmrEndpointList          // Endpoints
39         RMRRouteCreated  bool                          // Does subscription have RMR route
40         TransLock        sync.Mutex                    // Lock transactions, only one executed per time for subs
41         TheTrans         TransactionIf                 // Ongoing transaction
42         SubReqMsg        *e2ap.E2APSubscriptionRequest // Subscription information
43         SubRFMsg         interface{}                   // Subscription information
44         PolicyUpdate     bool                          // This is true when policy subscrition is being updated. Used not to send delete for update after timeout or restart
45         RetryFromXapp    bool                          // Retry form xApp for subscription that already exist
46         SubRespRcvd      bool                          // Subscription response received
47         DeleteFromDb     bool                          // Delete subscription from db
48         NoRespToXapp     bool                          // Send no response for subscription delete to xApp after restart
49         DoNotWaitSubResp bool                          // Test flag. Response is not waited for Subscription Request
50 }
51
52 func (s *Subscription) String() string {
53         meidstr := "N/A"
54         if s.Meid != nil {
55                 meidstr = s.Meid.String()
56         }
57         return "subs(" + s.ReqId.String() + "/" + meidstr + "/" + s.EpList.String() + ")"
58 }
59
60 func (s *Subscription) GetCachedResponse() (interface{}, bool) {
61         s.mutex.Lock()
62         defer s.mutex.Unlock()
63         return s.SubRFMsg, s.valid
64 }
65
66 func (s *Subscription) SetCachedResponse(subRFMsg interface{}, valid bool) (interface{}, bool) {
67         s.mutex.Lock()
68         defer s.mutex.Unlock()
69         s.SubRFMsg = subRFMsg
70         s.valid = valid
71         return s.SubRFMsg, s.valid
72 }
73
74 func (s *Subscription) GetReqId() *RequestId {
75         s.mutex.Lock()
76         defer s.mutex.Unlock()
77         return &s.ReqId
78 }
79
80 func (s *Subscription) GetMeid() *xapp.RMRMeid {
81         s.mutex.Lock()
82         defer s.mutex.Unlock()
83         return s.Meid
84 }
85
86 func (s *Subscription) GetTransaction() TransactionIf {
87         s.mutex.Lock()
88         defer s.mutex.Unlock()
89         return s.TheTrans
90 }
91
92 func (s *Subscription) WaitTransactionTurn(trans TransactionIf) {
93         s.TransLock.Lock()
94         s.mutex.Lock()
95         s.TheTrans = trans
96         s.mutex.Unlock()
97 }
98
99 func (s *Subscription) ReleaseTransactionTurn(trans TransactionIf) {
100         s.mutex.Lock()
101         if trans != nil && trans == s.TheTrans {
102                 s.TheTrans = nil
103         }
104         s.mutex.Unlock()
105         s.TransLock.Unlock()
106 }
107
108 func (s *Subscription) IsMergeable(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) bool {
109         s.mutex.Lock()
110         defer s.mutex.Unlock()
111
112         if s.valid == false {
113                 return false
114         }
115
116         if s.SubReqMsg == nil {
117                 return false
118         }
119
120         if s.Meid.RanName != trans.Meid.RanName {
121                 return false
122         }
123
124         // EventTrigger check
125         if s.SubReqMsg.EventTriggerDefinition.Data.Length != subReqMsg.EventTriggerDefinition.Data.Length {
126                 return false
127         }
128         for i := uint64(0); i < s.SubReqMsg.EventTriggerDefinition.Data.Length; i++ {
129                 if s.SubReqMsg.EventTriggerDefinition.Data.Data[i] != subReqMsg.EventTriggerDefinition.Data.Data[i] {
130                         return false
131                 }
132         }
133
134         // Actions check
135         if len(s.SubReqMsg.ActionSetups) != len(subReqMsg.ActionSetups) {
136                 return false
137         }
138
139         for _, acts := range s.SubReqMsg.ActionSetups {
140                 for _, actt := range subReqMsg.ActionSetups {
141                         if acts.ActionId != actt.ActionId {
142                                 return false
143                         }
144                         if acts.ActionType != actt.ActionType {
145                                 return false
146                         }
147
148                         if acts.ActionType != e2ap.E2AP_ActionTypeReport {
149                                 return false
150                         }
151
152                         if acts.RicActionDefinitionPresent != actt.RicActionDefinitionPresent {
153                                 return false
154                         }
155
156                         if acts.ActionDefinitionChoice.Data.Length != actt.ActionDefinitionChoice.Data.Length {
157                                 return false
158                         }
159                         for i := uint64(0); i < acts.ActionDefinitionChoice.Data.Length; i++ {
160                                 if acts.ActionDefinitionChoice.Data.Data[i] != actt.ActionDefinitionChoice.Data.Data[i] {
161                                         return false
162                                 }
163                         }
164                         if acts.SubsequentAction.Present != actt.SubsequentAction.Present ||
165                                 acts.SubsequentAction.Type != actt.SubsequentAction.Type ||
166                                 acts.SubsequentAction.TimetoWait != actt.SubsequentAction.TimetoWait {
167                                 return false
168                         }
169                 }
170         }
171         return true
172 }