kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 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 | |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 22 | import ( |
| 23 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 24 | "sync" |
| 25 | ) |
| 26 | |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 27 | type Subscription struct { |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 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 |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 42 | } |
| 43 | |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 44 | type Registry struct { |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 45 | register map[uint16]*Subscription |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 46 | counter uint16 |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 47 | mutex sync.Mutex |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 50 | // This method should run as a constructor |
| 51 | func (r *Registry) Initialize(seedsn uint16) { |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 52 | r.register = make(map[uint16]*Subscription) |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 53 | r.counter = seedsn |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 56 | // Reserves and returns the next free sequence number |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 57 | func (r *Registry) ReserveSubscription() *Subscription { |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 58 | // Check is current SequenceNumber valid |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 59 | // Allocate next SequenceNumber value and retry N times |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 60 | r.mutex.Lock() |
| 61 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 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 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 75 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 76 | return nil |
| 77 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 78 | |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 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] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 86 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 87 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // This function checks the validity of the given subscription id |
| 91 | func (r *Registry) IsValidSequenceNumber(sn uint16) bool { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 92 | r.mutex.Lock() |
| 93 | defer r.mutex.Unlock() |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 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) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 103 | r.mutex.Lock() |
| 104 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 105 | r.register[sn].Confirmed() |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 106 | } |
| 107 | |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 108 | //This function sets the given id as unused in the register |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 109 | func (r *Registry) setSubscriptionToUnConfirmed(sn uint16) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 110 | r.mutex.Lock() |
| 111 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 112 | r.register[sn].UnConfirmed() |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | //This function releases the given id as unused in the register |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 116 | func (r *Registry) releaseSequenceNumber(sn uint16) bool { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 117 | r.mutex.Lock() |
| 118 | defer r.mutex.Unlock() |
| 119 | if _, ok := r.register[sn]; ok { |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 120 | delete(r.register, sn) |
| 121 | return true |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 122 | } else { |
| 123 | return false |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 124 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 125 | } |