a1fe4b85f17208cd74221b69e7d4b2e938bfac0f
[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         counter     uint16
35         rtmgrClient *RtmgrClient
36 }
37
38 // This method should run as a constructor
39 func (r *Registry) Initialize(seedsn uint16) {
40         r.register = make(map[uint16]*Subscription)
41         r.counter = seedsn
42 }
43
44 // Reserves and returns the next free sequence number
45 func (r *Registry) ReserveSubscription(endPoint *RmrEndpoint, meid *xapp.RMRMeid) (*Subscription, error) {
46         // Check is current SequenceNumber valid
47         // Allocate next SequenceNumber value and retry N times
48         r.mutex.Lock()
49         defer r.mutex.Unlock()
50         var subs *Subscription = nil
51         var retrytimes uint16 = 1000
52         for ; subs == nil && retrytimes > 0; retrytimes-- {
53                 sequenceNumber := r.counter
54                 if r.counter == 65535 {
55                         r.counter = 0
56                 } else {
57                         r.counter++
58                 }
59                 if _, ok := r.register[sequenceNumber]; ok == false {
60                         subs := &Subscription{
61                                 registry:    r,
62                                 Seq:         sequenceNumber,
63                                 Active:      false,
64                                 RmrEndpoint: *endPoint,
65                                 Meid:        meid,
66                                 Trans:       nil,
67                         }
68                         r.register[sequenceNumber] = subs
69
70                         // Update routing
71                         r.mutex.Unlock()
72                         err := subs.UpdateRoute(CREATE)
73                         r.mutex.Lock()
74                         if err != nil {
75                                 if _, ok := r.register[sequenceNumber]; ok {
76                                         delete(r.register, sequenceNumber)
77                                 }
78                                 return nil, err
79                         }
80                         return subs, nil
81                 }
82         }
83         return nil, fmt.Errorf("Registry: Failed to reserves subcription. RmrEndpoint: %s, Meid: %s", endPoint, meid.RanName)
84 }
85
86 func (r *Registry) GetSubscription(sn uint16) *Subscription {
87         r.mutex.Lock()
88         defer r.mutex.Unlock()
89         xapp.Logger.Debug("Registry map: %v", r.register)
90         if _, ok := r.register[sn]; ok {
91                 return r.register[sn]
92         }
93         return nil
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                 delete(r.register, sn)
101                 return true
102         }
103         return false
104 }