Updated xapp-frame to 0.4.18. Updated rmr to 4.1.2.
[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         //"reflect"
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         TransLock sync.Mutex                    // Lock transactions, only one executed per time for subs
40         TheTrans  TransactionIf                 // Ongoing transaction
41         SubReqMsg *e2ap.E2APSubscriptionRequest // Subscription information
42         SubRFMsg  interface{}                   // Subscription information
43 }
44
45 func (s *Subscription) String() string {
46         meidstr := "N/A"
47         if s.Meid != nil {
48                 meidstr = s.Meid.String()
49         }
50         return "subs(" + s.ReqId.String() + "/" + meidstr + "/" + s.EpList.String() + ")"
51 }
52
53 func (s *Subscription) GetCachedResponse() (interface{}, bool) {
54         s.mutex.Lock()
55         defer s.mutex.Unlock()
56         return s.SubRFMsg, s.valid
57 }
58
59 func (s *Subscription) SetCachedResponse(subRFMsg interface{}, valid bool) (interface{}, bool) {
60         s.mutex.Lock()
61         defer s.mutex.Unlock()
62         s.SubRFMsg = subRFMsg
63         s.valid = valid
64         return s.SubRFMsg, s.valid
65 }
66
67 func (s *Subscription) GetReqId() *RequestId {
68         s.mutex.Lock()
69         defer s.mutex.Unlock()
70         return &s.ReqId
71 }
72
73 func (s *Subscription) GetMeid() *xapp.RMRMeid {
74         s.mutex.Lock()
75         defer s.mutex.Unlock()
76         if s.Meid != nil {
77                 return s.Meid
78         }
79         return nil
80 }
81
82 func (s *Subscription) GetTransaction() TransactionIf {
83         s.mutex.Lock()
84         defer s.mutex.Unlock()
85         return s.TheTrans
86 }
87
88 func (s *Subscription) WaitTransactionTurn(trans TransactionIf) {
89         s.TransLock.Lock()
90         s.mutex.Lock()
91         s.TheTrans = trans
92         s.mutex.Unlock()
93 }
94
95 func (s *Subscription) ReleaseTransactionTurn(trans TransactionIf) {
96         s.mutex.Lock()
97         if trans != nil && trans == s.TheTrans {
98                 s.TheTrans = nil
99         }
100         s.mutex.Unlock()
101         s.TransLock.Unlock()
102 }
103
104 func (s *Subscription) IsMergeable(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) bool {
105         s.mutex.Lock()
106         defer s.mutex.Unlock()
107
108         if s.valid == false {
109                 return false
110         }
111
112         if s.SubReqMsg == nil {
113                 return false
114         }
115
116         if s.Meid.RanName != trans.Meid.RanName {
117                 return false
118         }
119
120         // EventTrigger check
121         if s.SubReqMsg.EventTriggerDefinition.Data.Length != subReqMsg.EventTriggerDefinition.Data.Length {
122                 return false
123         }
124         for i := uint64(0); i < s.SubReqMsg.EventTriggerDefinition.Data.Length; i++ {
125                 if s.SubReqMsg.EventTriggerDefinition.Data.Data[i] != subReqMsg.EventTriggerDefinition.Data.Data[i] {
126                         return false
127                 }
128         }
129
130         // Actions check
131         if len(s.SubReqMsg.ActionSetups) != len(subReqMsg.ActionSetups) {
132                 return false
133         }
134
135         for _, acts := range s.SubReqMsg.ActionSetups {
136                 for _, actt := range subReqMsg.ActionSetups {
137                         if acts.ActionId != actt.ActionId {
138                                 return false
139                         }
140                         if acts.ActionType != actt.ActionType {
141                                 return false
142                         }
143
144                         if acts.ActionType != e2ap.E2AP_ActionTypeReport {
145                                 return false
146                         }
147
148                         if acts.RicActionDefinitionPresent != actt.RicActionDefinitionPresent {
149                                 return false
150                         }
151
152                         if acts.ActionDefinitionChoice.Data.Length != actt.ActionDefinitionChoice.Data.Length {
153                                 return false
154                         }
155                         for i := uint64(0); i < acts.ActionDefinitionChoice.Data.Length; i++ {
156                                 if acts.ActionDefinitionChoice.Data.Data[i] != actt.ActionDefinitionChoice.Data.Data[i] {
157                                         return false
158                                 }
159                         }
160                         //reflect.DeepEqual(acts.ActionDefinitionChoice, actt.ActionDefinitionChoice)
161
162                         if acts.SubsequentAction.Present != actt.SubsequentAction.Present ||
163                                 acts.SubsequentAction.Type != actt.SubsequentAction.Type ||
164                                 acts.SubsequentAction.TimetoWait != actt.SubsequentAction.TimetoWait {
165                                 return false
166                         }
167                 }
168         }
169
170         return true
171 }