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 | |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 27 | type Registry struct { |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 28 | register map[uint16]bool |
| 29 | counter uint16 |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 30 | mutex sync.Mutex |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 33 | // This method should run as a constructor |
| 34 | func (r *Registry) Initialize(seedsn uint16) { |
| 35 | r.register = make(map[uint16]bool) |
| 36 | r.counter = seedsn |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 39 | // Reserves and returns the next free sequence number |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 40 | func (r *Registry) ReserveSequenceNumber() (uint16, bool) { |
| 41 | // Check is current SequenceNumber valid |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 42 | r.mutex.Lock() |
| 43 | defer r.mutex.Unlock() |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 44 | sequenceNumber := r.counter |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 45 | if _, ok := r.register[sequenceNumber]; ok { |
Juha Hyttinen | ff8dccd | 2019-12-10 14:34:07 +0200 | [diff] [blame] | 46 | xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v", sequenceNumber) |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 47 | return sequenceNumber, false |
| 48 | } |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 49 | r.register[sequenceNumber] = false |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 50 | |
| 51 | // Allocate next SequenceNumber value |
| 52 | if r.counter == 65535 { |
| 53 | r.counter = 0 |
| 54 | } else { |
| 55 | r.counter++ |
| 56 | } |
| 57 | return sequenceNumber, true |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // This function checks the validity of the given subscription id |
| 61 | func (r *Registry) IsValidSequenceNumber(sn uint16) bool { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 62 | r.mutex.Lock() |
| 63 | defer r.mutex.Unlock() |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 64 | xapp.Logger.Debug("Registry map: %v", r.register) |
| 65 | if _, ok := r.register[sn]; ok { |
| 66 | return true |
| 67 | } |
| 68 | return false |
| 69 | } |
| 70 | |
| 71 | // This function sets the give id as confirmed in the register |
| 72 | func (r *Registry) setSubscriptionToConfirmed(sn uint16) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 73 | r.mutex.Lock() |
| 74 | defer r.mutex.Unlock() |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 75 | r.register[sn] = true |
| 76 | } |
| 77 | |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 78 | //This function sets the given id as unused in the register |
| 79 | func (r *Registry) deleteSubscription(sn uint16) { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 80 | r.mutex.Lock() |
| 81 | defer r.mutex.Unlock() |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 82 | r.register[sn] = false |
| 83 | } |
| 84 | |
| 85 | //This function releases the given id as unused in the register |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 86 | func (r *Registry) releaseSequenceNumber(sn uint16) bool { |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 87 | r.mutex.Lock() |
| 88 | defer r.mutex.Unlock() |
| 89 | if _, ok := r.register[sn]; ok { |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 90 | delete(r.register, sn) |
| 91 | return true |
Juha Hyttinen | 1a50344 | 2019-12-10 12:14:24 +0200 | [diff] [blame] | 92 | } else { |
| 93 | return false |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 94 | } |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 95 | } |