blob: eae870df2419985ba6390c04da0eadab84ab4951 [file] [log] [blame]
kalnagy45114752019-06-18 14:40:39 +02001/*
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
20package control
21
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020022import (
Juha Hyttinen0388dd92020-01-09 14:14:16 +020023 "fmt"
archaggeafbf95f2021-04-14 08:54:05 +030024 "sync"
25 "time"
26
Juha Hyttinen422d0182020-01-17 13:37:05 +020027 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +020028 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020029 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020030)
31
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020032//-----------------------------------------------------------------------------
33//
34//-----------------------------------------------------------------------------
Juha Hyttinen83ada002020-01-30 10:36:33 +020035
archaggeafbf95f2021-04-14 08:54:05 +030036type RESTSubscription struct {
37 xAppRmrEndPoint string
38 Meid string
39 InstanceIds []uint32
40 SubReqOngoing bool
41 SubDelReqOngoing bool
42}
43
44func (r *RESTSubscription) AddInstanceId(instanceId uint32) {
45 r.InstanceIds = append(r.InstanceIds, instanceId)
46}
47
48func (r *RESTSubscription) SetProcessed() {
49 r.SubReqOngoing = false
50}
51
52func (r *RESTSubscription) DeleteInstanceId(instanceId uint32) {
53 r.InstanceIds = r.InstanceIds[1:]
54}
55
kalnagy45114752019-06-18 14:40:39 +020056type Registry struct {
archaggeafbf95f2021-04-14 08:54:05 +030057 mutex sync.Mutex
58 register map[uint32]*Subscription
59 subIds []uint32
60 rtmgrClient *RtmgrClient
61 restSubscriptions map[string]*RESTSubscription
kalnagy45114752019-06-18 14:40:39 +020062}
63
Anssi Mannila5c161a92020-01-15 15:40:57 +020064func (r *Registry) Initialize() {
Juha Hyttinen83ada002020-01-30 10:36:33 +020065 r.register = make(map[uint32]*Subscription)
archaggeafbf95f2021-04-14 08:54:05 +030066 r.restSubscriptions = make(map[string]*RESTSubscription)
67
Juha Hyttinen83ada002020-01-30 10:36:33 +020068 var i uint32
Anssi Mannilac92b4212020-12-07 14:59:34 +020069 for i = 1; i < 65535; i++ {
70 r.subIds = append(r.subIds, i)
Anssi Mannila5c161a92020-01-15 15:40:57 +020071 }
kalnagy45114752019-06-18 14:40:39 +020072}
73
archaggeafbf95f2021-04-14 08:54:05 +030074func (r *Registry) CreateRESTSubscription(restSubId *string, xAppRmrEndPoint *string, maid *string) (*RESTSubscription, error) {
75 r.mutex.Lock()
76 defer r.mutex.Unlock()
77 newRestSubscription := RESTSubscription{}
78 newRestSubscription.xAppRmrEndPoint = *xAppRmrEndPoint
79 newRestSubscription.Meid = *maid
80 newRestSubscription.SubReqOngoing = true
81 newRestSubscription.SubDelReqOngoing = false
82 r.restSubscriptions[*restSubId] = &newRestSubscription
Anssi Mannila316d8a12021-06-02 11:08:54 +030083 xapp.Logger.Info("Registry: Created REST subscription successfully. restSubId=%v, subscriptionCount=%v, e2apSubscriptionCount=%v", *restSubId, len(r.restSubscriptions), len(r.register))
archaggeafbf95f2021-04-14 08:54:05 +030084 return &newRestSubscription, nil
85}
86
87func (r *Registry) DeleteRESTSubscription(restSubId *string) {
88 r.mutex.Lock()
89 defer r.mutex.Unlock()
90 delete(r.restSubscriptions, *restSubId)
91 xapp.Logger.Info("Registry: Deleted REST subscription successfully. restSubId=%v, subscriptionCount=%v", *restSubId, len(r.restSubscriptions))
92}
93
94func (r *Registry) GetRESTSubscription(restSubId string) (*RESTSubscription, error) {
95 r.mutex.Lock()
96 defer r.mutex.Unlock()
97 if restSubscription, ok := r.restSubscriptions[restSubId]; ok {
98 // Subscription deletion is not allowed if prosessing subscription request in not ready
99 if restSubscription.SubDelReqOngoing == false && restSubscription.SubReqOngoing == false {
100 restSubscription.SubDelReqOngoing = true
101 r.restSubscriptions[restSubId] = restSubscription
102 return restSubscription, nil
103 } else {
Markku Virtanenda34eec2021-05-20 08:22:04 +0000104 return restSubscription, fmt.Errorf("Registry: REST request is still ongoing for the endpoint=%v, restSubId=%v, SubDelReqOngoing=%v, SubReqOngoing=%v", restSubscription, restSubId, restSubscription.SubDelReqOngoing, restSubscription.SubReqOngoing)
archaggeafbf95f2021-04-14 08:54:05 +0300105 }
106 return restSubscription, nil
107 }
108 return nil, fmt.Errorf("Registry: No valid subscription found with restSubId=%v", restSubId)
109}
110
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200111func (r *Registry) QueryHandler() (models.SubscriptionList, error) {
112 r.mutex.Lock()
113 defer r.mutex.Unlock()
114
115 resp := models.SubscriptionList{}
116 for _, subs := range r.register {
117 subs.mutex.Lock()
archaggea5c58bc2021-04-14 08:54:05 +0300118 resp = append(resp, &models.SubscriptionData{SubscriptionID: int64(subs.ReqId.InstanceId), Meid: subs.Meid.RanName, ClientEndpoint: subs.EpList.StringList()})
Juha Hyttinenc9eb08a2020-02-28 08:53:33 +0200119 subs.mutex.Unlock()
120 }
121 return resp, nil
122}
123
Anssi Mannilac92b4212020-12-07 14:59:34 +0200124func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool) (*Subscription, error) {
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200125 if len(r.subIds) > 0 {
Juha Hyttinenaada6452020-04-07 08:47:58 +0300126 subId := r.subIds[0]
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200127 r.subIds = r.subIds[1:]
Juha Hyttinenaada6452020-04-07 08:47:58 +0300128 if _, ok := r.register[subId]; ok == true {
129 r.subIds = append(r.subIds, subId)
Juha Hyttinen3944a222020-01-24 11:51:46 +0200130 return nil, fmt.Errorf("Registry: Failed to reserve subscription exists")
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200131 }
Juha Hyttinen3944a222020-01-24 11:51:46 +0200132 subs := &Subscription{
Anssi Mannilac92b4212020-12-07 14:59:34 +0200133 registry: r,
134 Meid: trans.Meid,
135 SubReqMsg: subReqMsg,
136 valid: true,
137 RetryFromXapp: false,
138 SubRespRcvd: false,
139 DeleteFromDb: false,
140 NoRespToXapp: false,
141 DoNotWaitSubResp: false,
Juha Hyttinen3944a222020-01-24 11:51:46 +0200142 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200143 subs.ReqId.Id = 123
Juha Hyttinenaada6452020-04-07 08:47:58 +0300144 subs.ReqId.InstanceId = subId
Anssi Mannilac92b4212020-12-07 14:59:34 +0200145 if resetTestFlag == true {
146 subs.DoNotWaitSubResp = true
147 }
Juha Hyttinen3944a222020-01-24 11:51:46 +0200148
149 if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
Juha Hyttinenaada6452020-04-07 08:47:58 +0300150 r.subIds = append(r.subIds, subs.ReqId.InstanceId)
Juha Hyttinen3944a222020-01-24 11:51:46 +0200151 return nil, fmt.Errorf("Registry: Endpoint existing already in subscription")
152 }
Juha Hyttinen3944a222020-01-24 11:51:46 +0200153 return subs, nil
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200154 }
Juha Hyttinen3944a222020-01-24 11:51:46 +0200155 return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids")
156}
157
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200158func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, bool) {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200159
Juha Hyttinen3944a222020-01-24 11:51:46 +0200160 for _, subs := range r.register {
Juha Hyttinen83ada002020-01-30 10:36:33 +0200161 if subs.IsMergeable(trans, subReqMsg) {
Juha Hyttinen3944a222020-01-24 11:51:46 +0200162
163 //
164 // check if there has been race conditions
165 //
166 subs.mutex.Lock()
167 //subs has been set to invalid
168 if subs.valid == false {
169 subs.mutex.Unlock()
170 continue
171 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200172 // If size is zero, entry is to be deleted
173 if subs.EpList.Size() == 0 {
174 subs.mutex.Unlock()
175 continue
176 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200177 // Try to add to endpointlist. Adding fails if endpoint is already in the list
Juha Hyttinen3944a222020-01-24 11:51:46 +0200178 if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
179 subs.mutex.Unlock()
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200180 xapp.Logger.Debug("Registry: Subs with requesting endpoint found. %s for %s", subs.String(), trans.String())
181 return subs, true
Juha Hyttinen3944a222020-01-24 11:51:46 +0200182 }
183 subs.mutex.Unlock()
184
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200185 xapp.Logger.Debug("Registry: Mergeable subs found. %s for %s", subs.String(), trans.String())
186 return subs, false
Juha Hyttinen3944a222020-01-24 11:51:46 +0200187 }
188 }
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200189 return nil, false
Juha Hyttinen3944a222020-01-24 11:51:46 +0200190}
191
Anssi Mannila4abf1802021-01-28 13:06:46 +0200192func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool, c *Control) (*Subscription, error) {
Juha Hyttinen3944a222020-01-24 11:51:46 +0200193 var err error
194 var newAlloc bool
195 r.mutex.Lock()
196 defer r.mutex.Unlock()
197
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200198 //
199 // Check validity of subscription action types
200 //
201 actionType, err := r.CheckActionTypes(subReqMsg)
202 if err != nil {
203 xapp.Logger.Debug("CREATE %s", err)
204 return nil, err
205 }
Juha Hyttinen3944a222020-01-24 11:51:46 +0200206
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200207 //
208 // Find possible existing Policy subscription
209 //
210 if actionType == e2ap.E2AP_ActionTypePolicy {
Juha Hyttinen47942b42020-02-27 10:41:43 +0200211 if subs, ok := r.register[trans.GetSubId()]; ok {
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200212 xapp.Logger.Debug("CREATE %s. Existing subscription for Policy found.", subs.String())
Anssi Mannilacc7d9e02020-04-08 12:58:53 +0300213 // Update message data to subscription
214 subs.SubReqMsg = subReqMsg
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200215 subs.SetCachedResponse(nil, true)
216 return subs, nil
217 }
218 }
219
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200220 subs, endPointFound := r.findExistingSubs(trans, subReqMsg)
Juha Hyttinen3944a222020-01-24 11:51:46 +0200221 if subs == nil {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200222 if subs, err = r.allocateSubs(trans, subReqMsg, resetTestFlag); err != nil {
Juha Hyttinen3944a222020-01-24 11:51:46 +0200223 return nil, err
224 }
225 newAlloc = true
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200226 } else if endPointFound == true {
227 // Requesting endpoint is already present in existing subscription. This can happen if xApp is restarted.
Anssi Mannilac92b4212020-12-07 14:59:34 +0200228 subs.RetryFromXapp = true
Anssi Mannila316d8a12021-06-02 11:08:54 +0300229 xapp.Logger.Debug("CREATE subReqMsg.InstanceId=%v. Same subscription %s already exists.", subReqMsg.InstanceId, subs.String())
230 c.UpdateCounter(cDuplicateE2SubReq)
Anssi Mannila2f26fb22020-12-07 08:32:13 +0200231 return subs, nil
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200232 }
233
234 //
235 // Add to subscription
236 //
237 subs.mutex.Lock()
238 defer subs.mutex.Unlock()
239
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200240 epamount := subs.EpList.Size()
Anssi Mannila316d8a12021-06-02 11:08:54 +0300241 xapp.Logger.Info("AssignToSubscription subs.EpList.Size()=%v", subs.EpList.Size())
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200242
243 r.mutex.Unlock()
244 //
245 // Subscription route updates
246 //
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200247 if epamount == 1 {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200248 err = r.RouteCreate(subs, c)
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200249 } else {
Anssi Mannila4abf1802021-01-28 13:06:46 +0200250 err = r.RouteCreateUpdate(subs, c)
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200251 }
252 r.mutex.Lock()
253
254 if err != nil {
Juha Hyttinen3944a222020-01-24 11:51:46 +0200255 if newAlloc {
Juha Hyttinenaada6452020-04-07 08:47:58 +0300256 r.subIds = append(r.subIds, subs.ReqId.InstanceId)
Juha Hyttinen3944a222020-01-24 11:51:46 +0200257 }
Anssi Mannila4abf1802021-01-28 13:06:46 +0200258 // Delete already added endpoint for the request
259 subs.EpList.DelEndpoint(trans.GetEndpoint())
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200260 return nil, err
261 }
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200262
Juha Hyttinen3944a222020-01-24 11:51:46 +0200263 if newAlloc {
Juha Hyttinenaada6452020-04-07 08:47:58 +0300264 r.register[subs.ReqId.InstanceId] = subs
Juha Hyttinen3944a222020-01-24 11:51:46 +0200265 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200266 xapp.Logger.Debug("CREATE %s", subs.String())
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200267 xapp.Logger.Debug("Registry: substable=%v", r.register)
268 return subs, nil
269}
270
Anssi Mannila4abf1802021-01-28 13:06:46 +0200271func (r *Registry) RouteCreate(subs *Subscription, c *Control) error {
272 subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)}
273 err := r.rtmgrClient.SubscriptionRequestCreate(subRouteAction)
274 if err != nil {
275 c.UpdateCounter(cRouteCreateFail)
276 }
277 return err
278}
279
280func (r *Registry) RouteCreateUpdate(subs *Subscription, c *Control) error {
281 subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)}
282 err := r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
283 if err != nil {
284 c.UpdateCounter(cRouteCreateUpdateFail)
285 return err
286 }
287 c.UpdateCounter(cMergedSubscriptions)
288 return err
289}
290
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200291func (r *Registry) CheckActionTypes(subReqMsg *e2ap.E2APSubscriptionRequest) (uint64, error) {
292 var reportFound bool = false
293 var policyFound bool = false
Anssi Mannilaf0d95262020-08-17 13:00:20 +0300294 var insertFound bool = false
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200295
296 for _, acts := range subReqMsg.ActionSetups {
297 if acts.ActionType == e2ap.E2AP_ActionTypeReport {
298 reportFound = true
299 }
300 if acts.ActionType == e2ap.E2AP_ActionTypePolicy {
301 policyFound = true
302 }
Anssi Mannilaf0d95262020-08-17 13:00:20 +0300303 if acts.ActionType == e2ap.E2AP_ActionTypeInsert {
304 insertFound = true
305 }
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200306 }
Anssi Mannilaf0d95262020-08-17 13:00:20 +0300307 if reportFound == true && policyFound == true || reportFound == true && insertFound == true || policyFound == true && insertFound == true {
308 return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Different action types (Report, Policy or Insert) in same RICactions-ToBeSetup-List")
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200309 }
310 if reportFound == true {
311 return e2ap.E2AP_ActionTypeReport, nil
312 }
313 if policyFound == true {
314 return e2ap.E2AP_ActionTypePolicy, nil
315 }
Anssi Mannilaf0d95262020-08-17 13:00:20 +0300316 if insertFound == true {
317 return e2ap.E2AP_ActionTypeInsert, nil
318 }
Anssi Mannila9bcb0a42020-02-11 11:30:44 +0200319 return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Invalid action type in RICactions-ToBeSetup-List")
320}
321
Juha Hyttinen83ada002020-01-30 10:36:33 +0200322// TODO: Works with concurrent calls, but check if can be improved
Anssi Mannilac92b4212020-12-07 14:59:34 +0200323func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *TransactionXapp, waitRouteClean time.Duration, c *Control) error {
Juha Hyttinen3944a222020-01-24 11:51:46 +0200324
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200325 r.mutex.Lock()
326 defer r.mutex.Unlock()
327 subs.mutex.Lock()
328 defer subs.mutex.Unlock()
329
330 delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint())
331 epamount := subs.EpList.Size()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300332 subId := subs.ReqId.InstanceId
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200333
Juha Hyttinen83ada002020-01-30 10:36:33 +0200334 if delStatus == false {
335 return nil
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200336 }
Juha Hyttinen83ada002020-01-30 10:36:33 +0200337
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200338 go func() {
339 if waitRouteClean > 0 {
Markku Virtanenfe2cdab2021-05-21 10:59:29 +0000340 xapp.Logger.Debug("Pending %v in order to wait route cleanup", waitRouteClean)
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200341 time.Sleep(waitRouteClean)
Juha Hyttinen83ada002020-01-30 10:36:33 +0200342 }
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200343
344 subs.mutex.Lock()
345 defer subs.mutex.Unlock()
346 xapp.Logger.Info("CLEAN %s", subs.String())
347
348 if epamount == 0 {
349 //
350 // Subscription route delete
351 //
Anssi Mannila4abf1802021-01-28 13:06:46 +0200352 r.RouteDelete(subs, trans, c)
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200353
354 //
355 // Subscription release
356 //
357 r.mutex.Lock()
358 defer r.mutex.Unlock()
359
Juha Hyttinenaada6452020-04-07 08:47:58 +0300360 if _, ok := r.register[subId]; ok {
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200361 xapp.Logger.Debug("RELEASE %s", subs.String())
Juha Hyttinenaada6452020-04-07 08:47:58 +0300362 delete(r.register, subId)
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200363 xapp.Logger.Debug("Registry: substable=%v", r.register)
364 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300365 r.subIds = append(r.subIds, subId)
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200366 } else if subs.EpList.Size() > 0 {
367 //
Anssi Mannila4abf1802021-01-28 13:06:46 +0200368 // Subscription route update
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200369 //
Anssi Mannila4abf1802021-01-28 13:06:46 +0200370 r.RouteDeleteUpdate(subs, c)
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200371 }
Juha Hyttinenf44377d2020-02-12 10:18:40 +0200372 }()
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200373
374 return nil
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200375}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200376
Anssi Mannila4abf1802021-01-28 13:06:46 +0200377func (r *Registry) RouteDelete(subs *Subscription, trans *TransactionXapp, c *Control) {
378 tmpList := xapp.RmrEndpointList{}
379 tmpList.AddEndpoint(trans.GetEndpoint())
380 subRouteAction := SubRouteInfo{tmpList, uint16(subs.ReqId.InstanceId)}
381 if err := r.rtmgrClient.SubscriptionRequestDelete(subRouteAction); err != nil {
382 c.UpdateCounter(cRouteDeleteFail)
383 }
384}
385
386func (r *Registry) RouteDeleteUpdate(subs *Subscription, c *Control) {
387 subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)}
388 if err := r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction); err != nil {
389 c.UpdateCounter(cRouteDeleteUpdateFail)
390 }
391}
392
Anssi Mannilac92b4212020-12-07 14:59:34 +0200393func (r *Registry) UpdateSubscriptionToDb(subs *Subscription, c *Control) {
394 r.mutex.Lock()
395 defer r.mutex.Unlock()
396 subs.mutex.Lock()
397 defer subs.mutex.Unlock()
398
399 epamount := subs.EpList.Size()
400 if epamount == 0 {
401 if _, ok := r.register[subs.ReqId.InstanceId]; ok {
402 // Not merged subscription is being deleted
403 c.RemoveSubscriptionFromDb(subs)
404
405 }
406 } else if subs.EpList.Size() > 0 {
407 // Endpoint of merged subscription is being deleted
408 c.WriteSubscriptionToDb(subs)
Anssi Mannila4abf1802021-01-28 13:06:46 +0200409 c.UpdateCounter(cUnmergedSubscriptions)
Anssi Mannilac92b4212020-12-07 14:59:34 +0200410 }
411}
412
Juha Hyttinenaada6452020-04-07 08:47:58 +0300413func (r *Registry) GetSubscription(subId uint32) *Subscription {
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200414 r.mutex.Lock()
415 defer r.mutex.Unlock()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300416 if _, ok := r.register[subId]; ok {
417 return r.register[subId]
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200418 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200419 return nil
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000420}
421
Juha Hyttinenaada6452020-04-07 08:47:58 +0300422func (r *Registry) GetSubscriptionFirstMatch(subIds []uint32) (*Subscription, error) {
Juha Hyttinen422d0182020-01-17 13:37:05 +0200423 r.mutex.Lock()
424 defer r.mutex.Unlock()
Juha Hyttinenaada6452020-04-07 08:47:58 +0300425 for _, subId := range subIds {
426 if _, ok := r.register[subId]; ok {
427 return r.register[subId], nil
Juha Hyttinen422d0182020-01-17 13:37:05 +0200428 }
429 }
Juha Hyttinenaada6452020-04-07 08:47:58 +0300430 return nil, fmt.Errorf("No valid subscription found with subIds %v", subIds)
Juha Hyttinen422d0182020-01-17 13:37:05 +0200431}