RICPLT-2571 Make code change for MEID support
[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         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
24         "strconv"
25         "sync"
26 )
27
28 //-----------------------------------------------------------------------------
29 //
30 //-----------------------------------------------------------------------------
31 type Subscription struct {
32         mutex  sync.Mutex
33         Seq    uint16
34         Active bool
35         //
36         Meid        *xapp.RMRMeid
37         RmrEndpoint // xapp endpoint
38         Trans       *Transaction
39 }
40
41 func (s *Subscription) String() string {
42         s.mutex.Lock()
43         defer s.mutex.Unlock()
44         return strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
45 }
46
47 func (s *Subscription) Confirmed() {
48         s.mutex.Lock()
49         defer s.mutex.Unlock()
50         s.Active = true
51 }
52
53 func (s *Subscription) UnConfirmed() {
54         s.mutex.Lock()
55         defer s.mutex.Unlock()
56         s.Active = false
57 }
58
59 func (s *Subscription) IsConfirmed() bool {
60         s.mutex.Lock()
61         defer s.mutex.Unlock()
62         return s.Active
63 }
64
65 func (s *Subscription) SetTransaction(trans *Transaction) bool {
66         s.mutex.Lock()
67         defer s.mutex.Unlock()
68         if s.Trans == nil {
69                 s.Trans = trans
70                 return true
71         }
72         return false
73 }
74
75 func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
76         s.mutex.Lock()
77         defer s.mutex.Unlock()
78         if trans == nil || trans == s.Trans {
79                 s.Trans = nil
80                 return true
81         }
82         return false
83 }
84
85 func (s *Subscription) GetTransaction() *Transaction {
86         s.mutex.Lock()
87         defer s.mutex.Unlock()
88         return s.Trans
89 }
90
91 func (s *Subscription) SubRouteInfo(act Action) SubRouteInfo {
92         s.mutex.Lock()
93         defer s.mutex.Unlock()
94         return SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq}
95 }
96
97 //-----------------------------------------------------------------------------
98 //
99 //-----------------------------------------------------------------------------
100 type Registry struct {
101         register map[uint16]*Subscription
102         counter  uint16
103         mutex    sync.Mutex
104 }
105
106 // This method should run as a constructor
107 func (r *Registry) Initialize(seedsn uint16) {
108         r.register = make(map[uint16]*Subscription)
109         r.counter = seedsn
110 }
111
112 // Reserves and returns the next free sequence number
113 func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid) *Subscription {
114         // Check is current SequenceNumber valid
115         // Allocate next SequenceNumber value and retry N times
116         r.mutex.Lock()
117         defer r.mutex.Unlock()
118         var subs *Subscription = nil
119         var retrytimes uint16 = 1000
120         for ; subs == nil && retrytimes > 0; retrytimes-- {
121                 sequenceNumber := r.counter
122                 if r.counter == 65535 {
123                         r.counter = 0
124                 } else {
125                         r.counter++
126                 }
127                 if _, ok := r.register[sequenceNumber]; ok == false {
128                         r.register[sequenceNumber] = &Subscription{
129                                 Seq:         sequenceNumber,
130                                 Active:      false,
131                                 RmrEndpoint: endPoint,
132                                 Meid:        meid,
133                                 Trans:       nil,
134                         }
135                         return r.register[sequenceNumber]
136                 }
137         }
138         return nil
139 }
140
141 func (r *Registry) GetSubscription(sn uint16) *Subscription {
142         r.mutex.Lock()
143         defer r.mutex.Unlock()
144         xapp.Logger.Debug("Registry map: %v", r.register)
145         if _, ok := r.register[sn]; ok {
146                 return r.register[sn]
147         }
148         return nil
149 }
150
151 //This function releases the given id as unused in the register
152 func (r *Registry) releaseSequenceNumber(sn uint16) bool {
153         r.mutex.Lock()
154         defer r.mutex.Unlock()
155         if _, ok := r.register[sn]; ok {
156                 delete(r.register, sn)
157                 return true
158         } else {
159                 return false
160         }
161 }