blob: 98aa97e8e84a099f2b2f9ee844b3f9ddd957b9de [file] [log] [blame]
kalnagy45114752019-06-18 14:40:39 +02001/*
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
20package control
21
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020022import (
23 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
24 "sync"
25)
26
27var registryMutex = &sync.Mutex{}
Peter Szilagyifbc56f92019-07-23 19:29:46 +000028
kalnagy45114752019-06-18 14:40:39 +020029type Registry struct {
Peter Szilagyifbc56f92019-07-23 19:29:46 +000030 register map[uint16]bool
31 counter uint16
kalnagy45114752019-06-18 14:40:39 +020032}
33
Peter Szilagyifbc56f92019-07-23 19:29:46 +000034// This method should run as a constructor
35func (r *Registry) Initialize(seedsn uint16) {
36 r.register = make(map[uint16]bool)
37 r.counter = seedsn
kalnagy45114752019-06-18 14:40:39 +020038}
39
Peter Szilagyifbc56f92019-07-23 19:29:46 +000040// Reserves and returns the next free sequence number
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020041func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
42 // Check is current SequenceNumber valid
43 registryMutex.Lock()
44 defer registryMutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000045 sequenceNumber := r.counter
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020046 if _, ok := r.register[sequenceNumber]; ok {
47 xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v",sequenceNumber)
48 return sequenceNumber, false
49 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +000050 r.register[sequenceNumber] = false
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020051
52 // Allocate next SequenceNumber value
53 if r.counter == 65535 {
54 r.counter = 0
55 } else {
56 r.counter++
57 }
58 return sequenceNumber, true
Peter Szilagyifbc56f92019-07-23 19:29:46 +000059}
60
61// This function checks the validity of the given subscription id
62func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020063 registryMutex.Lock()
64 defer registryMutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000065 xapp.Logger.Debug("Registry map: %v", r.register)
66 if _, ok := r.register[sn]; ok {
67 return true
68 }
69 return false
70}
71
72// This function sets the give id as confirmed in the register
73func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020074 registryMutex.Lock()
75 defer registryMutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000076 r.register[sn] = true
77}
78
kalnagy93cc3e22019-09-19 11:29:29 +020079//This function sets the given id as unused in the register
80func (r *Registry) deleteSubscription(sn uint16) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020081 registryMutex.Lock()
82 defer registryMutex.Unlock()
kalnagy93cc3e22019-09-19 11:29:29 +020083 r.register[sn] = false
84}
85
86//This function releases the given id as unused in the register
kalnagye0018682019-09-26 16:28:25 +020087func (r *Registry) releaseSequenceNumber(sn uint16) bool {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020088 registryMutex.Lock()
89 defer registryMutex.Unlock()
kalnagye0018682019-09-26 16:28:25 +020090 if r.register[sn] {
91 return false
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020092 } else {
kalnagye0018682019-09-26 16:28:25 +020093 delete(r.register, sn)
94 return true
95 }
kalnagy1455c852019-10-21 13:06:23 +020096}