c2b52838d2e12868a82307c65d7b483263166772
[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         "sync"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type Subscription struct {
32         mutex     sync.Mutex                    // Lock
33         valid     bool                          // valid
34         registry  *Registry                     // Registry
35         ReqId     RequestId                     // ReqId (Requestor Id + Seq Nro a.k.a subsid)
36         Meid      *xapp.RMRMeid                 // Meid/ RanName
37         EpList    RmrEndpointList               // Endpoints
38         TransLock sync.Mutex                    // Lock transactions, only one executed per time for subs
39         TheTrans  TransactionIf                 // Ongoing transaction
40         SubReqMsg *e2ap.E2APSubscriptionRequest // Subscription information
41         SubRFMsg  interface{}                   // Subscription information
42 }
43
44 func (s *Subscription) String() string {
45         return "subs(" + s.ReqId.String() + "/" + s.Meid.RanName + "/" + s.EpList.String() + ")"
46 }
47
48 func (s *Subscription) GetCachedResponse() (interface{}, bool) {
49         s.mutex.Lock()
50         defer s.mutex.Unlock()
51         return s.SubRFMsg, s.valid
52 }
53
54 func (s *Subscription) SetCachedResponse(subRFMsg interface{}, valid bool) (interface{}, bool) {
55         s.mutex.Lock()
56         defer s.mutex.Unlock()
57         s.SubRFMsg = subRFMsg
58         s.valid = valid
59         return s.SubRFMsg, s.valid
60 }
61
62 func (s *Subscription) GetReqId() *RequestId {
63         s.mutex.Lock()
64         defer s.mutex.Unlock()
65         return &s.ReqId
66 }
67
68 func (s *Subscription) GetMeid() *xapp.RMRMeid {
69         s.mutex.Lock()
70         defer s.mutex.Unlock()
71         if s.Meid != nil {
72                 return s.Meid
73         }
74         return nil
75 }
76
77 func (s *Subscription) GetTransaction() TransactionIf {
78         s.mutex.Lock()
79         defer s.mutex.Unlock()
80         return s.TheTrans
81 }
82
83 func (s *Subscription) WaitTransactionTurn(trans TransactionIf) {
84         s.TransLock.Lock()
85         s.mutex.Lock()
86         s.TheTrans = trans
87         s.mutex.Unlock()
88 }
89
90 func (s *Subscription) ReleaseTransactionTurn(trans TransactionIf) {
91         s.mutex.Lock()
92         if trans != nil && trans == s.TheTrans {
93                 s.TheTrans = nil
94         }
95         s.mutex.Unlock()
96         s.TransLock.Unlock()
97 }
98
99 func (s *Subscription) IsMergeable(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) bool {
100         s.mutex.Lock()
101         defer s.mutex.Unlock()
102
103         if s.valid == false {
104                 return false
105         }
106
107         if s.SubReqMsg == nil {
108                 return false
109         }
110
111         if s.Meid.RanName != trans.Meid.RanName {
112                 return false
113         }
114
115         // EventTrigger check
116         if s.SubReqMsg.EventTriggerDefinition.InterfaceDirection != subReqMsg.EventTriggerDefinition.InterfaceDirection ||
117                 s.SubReqMsg.EventTriggerDefinition.ProcedureCode != subReqMsg.EventTriggerDefinition.ProcedureCode ||
118                 s.SubReqMsg.EventTriggerDefinition.TypeOfMessage != subReqMsg.EventTriggerDefinition.TypeOfMessage {
119                 return false
120         }
121
122         if s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.Present ||
123                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.NodeId ||
124                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[0] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[0] ||
125                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[1] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[1] ||
126                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[2] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalEnbId.PlmnIdentity.Val[2] {
127                 return false
128         }
129
130         if s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.Present != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.Present ||
131                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.NodeId != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.NodeId ||
132                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[0] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[0] ||
133                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[1] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[1] ||
134                 s.SubReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[2] != subReqMsg.EventTriggerDefinition.InterfaceId.GlobalGnbId.PlmnIdentity.Val[2] {
135                 return false
136         }
137
138         // Actions check
139         if len(s.SubReqMsg.ActionSetups) != len(subReqMsg.ActionSetups) {
140                 return false
141         }
142
143         for _, acts := range s.SubReqMsg.ActionSetups {
144                 for _, actt := range subReqMsg.ActionSetups {
145                         if acts.ActionId != actt.ActionId {
146                                 return false
147                         }
148                         if acts.ActionType != actt.ActionType {
149                                 return false
150                         }
151
152                         if acts.ActionType != e2ap.E2AP_ActionTypeReport {
153                                 return false
154                         }
155
156                         if acts.ActionDefinition.Present != actt.ActionDefinition.Present ||
157                                 acts.ActionDefinition.StyleId != actt.ActionDefinition.StyleId ||
158                                 acts.ActionDefinition.ParamId != actt.ActionDefinition.ParamId {
159                                 return false
160                         }
161                         if acts.SubsequentAction.Present != actt.SubsequentAction.Present ||
162                                 acts.SubsequentAction.Type != actt.SubsequentAction.Type ||
163                                 acts.SubsequentAction.TimetoWait != actt.SubsequentAction.TimetoWait {
164                                 return false
165                         }
166                 }
167         }
168
169         return true
170 }