RICPLT-3014 Subs multiple rmr 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/xapp-frame/pkg/xapp"
25         "sync"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type Registry struct {
32         mutex       sync.Mutex
33         register    map[uint16]*Subscription
34         subIds      []uint16
35         rtmgrClient *RtmgrClient
36 }
37
38 // This method should run as a constructor
39 func (r *Registry) Initialize() {
40         r.register = make(map[uint16]*Subscription)
41         var i uint16
42         for i = 0; i < 65535; i++ {
43                 r.subIds = append(r.subIds, i+1)
44         }
45 }
46
47 // Reserves and returns the next free sequence number
48 func (r *Registry) ReserveSubscription(meid *xapp.RMRMeid) (*Subscription, error) {
49         r.mutex.Lock()
50         defer r.mutex.Unlock()
51         if len(r.subIds) > 0 {
52                 sequenceNumber := r.subIds[0]
53                 r.subIds = r.subIds[1:]
54                 if _, ok := r.register[sequenceNumber]; ok == false {
55                         subs := &Subscription{
56                                 registry: r,
57                                 Seq:      sequenceNumber,
58                                 Active:   false,
59                                 Meid:     meid,
60                                 Trans:    nil,
61                         }
62                         r.register[sequenceNumber] = subs
63                         xapp.Logger.Info("Registry: Create %s", subs.String())
64                         xapp.Logger.Debug("Registry: substable=%v", r.register)
65                         return subs, nil
66                 }
67         }
68         return nil, fmt.Errorf("Registry: Failed to reserves subscription")
69 }
70
71 func (r *Registry) GetSubscription(sn uint16) *Subscription {
72         r.mutex.Lock()
73         defer r.mutex.Unlock()
74         if _, ok := r.register[sn]; ok {
75                 return r.register[sn]
76         }
77         return nil
78 }
79
80 func (r *Registry) DelSubscription(sn uint16) bool {
81         r.mutex.Lock()
82         defer r.mutex.Unlock()
83         if _, ok := r.register[sn]; ok {
84                 subs := r.register[sn]
85                 xapp.Logger.Info("Registry: Delete %s", subs.String())
86                 r.subIds = append(r.subIds, sn)
87                 delete(r.register, sn)
88                 xapp.Logger.Debug("Registry: substable=%v", r.register)
89                 return true
90         }
91         return false
92 }