RICPLT-3014 Subscription multiple endpoints
[ric-plt/submgr.git] / pkg / control / registry.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         "fmt"
24         "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "sync"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 type Registry struct {
33         mutex       sync.Mutex
34         register    map[uint16]*Subscription
35         subIds      []uint16
36         rtmgrClient *RtmgrClient
37 }
38
39 // This method should run as a constructor
40 func (r *Registry) Initialize() {
41         r.register = make(map[uint16]*Subscription)
42         var i uint16
43         for i = 0; i < 65535; i++ {
44                 r.subIds = append(r.subIds, i+1)
45         }
46 }
47
48 // Reserves and returns the next free sequence number
49 func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
50         r.mutex.Lock()
51         defer r.mutex.Unlock()
52         if len(r.subIds) > 0 {
53                 sequenceNumber := r.subIds[0]
54                 r.subIds = r.subIds[1:]
55                 if _, ok := r.register[sequenceNumber]; ok == false {
56                         subs := &Subscription{
57                                 registry: r,
58                                 Seq:      sequenceNumber,
59                                 Meid:     trans.Meid,
60                         }
61                         err := subs.AddEndpoint(trans.GetEndpoint())
62                         if err != nil {
63                                 return nil, err
64                         }
65                         subs.SubReqMsg = subReqMsg
66
67                         r.register[sequenceNumber] = subs
68                         xapp.Logger.Debug("Registry: Create %s", subs.String())
69                         xapp.Logger.Debug("Registry: substable=%v", r.register)
70                         return subs, nil
71                 }
72         }
73         return nil, fmt.Errorf("Registry: Failed to reserves subscription")
74 }
75
76 func (r *Registry) GetSubscription(sn uint16) *Subscription {
77         r.mutex.Lock()
78         defer r.mutex.Unlock()
79         if _, ok := r.register[sn]; ok {
80                 return r.register[sn]
81         }
82         return nil
83 }
84
85 func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) {
86         r.mutex.Lock()
87         defer r.mutex.Unlock()
88         for _, id := range ids {
89                 if _, ok := r.register[id]; ok {
90                         return r.register[id], nil
91                 }
92         }
93         return nil, fmt.Errorf("No valid subscription found with ids %v", ids)
94 }
95
96 func (r *Registry) DelSubscription(sn uint16) bool {
97         r.mutex.Lock()
98         defer r.mutex.Unlock()
99         if _, ok := r.register[sn]; ok {
100                 subs := r.register[sn]
101                 xapp.Logger.Debug("Registry: Delete %s", subs.String())
102                 r.subIds = append(r.subIds, sn)
103                 delete(r.register, sn)
104                 xapp.Logger.Debug("Registry: substable=%v", r.register)
105                 return true
106         }
107         return false
108 }