RICPLT-2988 Unittest timing issues during retransmission case
[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         "sync"
25 )
26
27 type Subscription struct {
28         Seq    uint16
29         Active bool
30 }
31
32 func (s *Subscription) Confirmed() {
33         s.Active = true
34 }
35
36 func (s *Subscription) UnConfirmed() {
37         s.Active = false
38 }
39
40 func (s *Subscription) IsConfirmed() bool {
41         return s.Active
42 }
43
44 type Registry struct {
45         register map[uint16]*Subscription
46         counter  uint16
47         mutex    sync.Mutex
48 }
49
50 // This method should run as a constructor
51 func (r *Registry) Initialize(seedsn uint16) {
52         r.register = make(map[uint16]*Subscription)
53         r.counter = seedsn
54 }
55
56 // Reserves and returns the next free sequence number
57 func (r *Registry) ReserveSubscription() *Subscription {
58         // Check is current SequenceNumber valid
59         // Allocate next SequenceNumber value and retry N times
60         r.mutex.Lock()
61         defer r.mutex.Unlock()
62         var subs *Subscription = nil
63         var retrytimes uint16 = 1000
64         for ; subs == nil && retrytimes > 0; retrytimes-- {
65                 sequenceNumber := r.counter
66                 if r.counter == 65535 {
67                         r.counter = 0
68                 } else {
69                         r.counter++
70                 }
71                 if _, ok := r.register[sequenceNumber]; ok == false {
72                         r.register[sequenceNumber] = &Subscription{sequenceNumber, false}
73                         return r.register[sequenceNumber]
74                 }
75         }
76         return nil
77 }
78
79 // This function checks the validity of the given subscription id
80 func (r *Registry) GetSubscription(sn uint16) *Subscription {
81         r.mutex.Lock()
82         defer r.mutex.Unlock()
83         xapp.Logger.Debug("Registry map: %v", r.register)
84         if _, ok := r.register[sn]; ok {
85                 return r.register[sn]
86         }
87         return nil
88 }
89
90 // This function checks the validity of the given subscription id
91 func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
92         r.mutex.Lock()
93         defer r.mutex.Unlock()
94         xapp.Logger.Debug("Registry map: %v", r.register)
95         if _, ok := r.register[sn]; ok {
96                 return true
97         }
98         return false
99 }
100
101 // This function sets the give id as confirmed in the register
102 func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
103         r.mutex.Lock()
104         defer r.mutex.Unlock()
105         r.register[sn].Confirmed()
106 }
107
108 //This function sets the given id as unused in the register
109 func (r *Registry) setSubscriptionToUnConfirmed(sn uint16) {
110         r.mutex.Lock()
111         defer r.mutex.Unlock()
112         r.register[sn].UnConfirmed()
113 }
114
115 //This function releases the given id as unused in the register
116 func (r *Registry) releaseSequenceNumber(sn uint16) bool {
117         r.mutex.Lock()
118         defer r.mutex.Unlock()
119         if _, ok := r.register[sn]; ok {
120                 delete(r.register, sn)
121                 return true
122         } else {
123                 return false
124         }
125 }