blob: 4f5c4fd41f4a8aedfde3a1c1be3880cdf40a0d13 [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
kalnagy45114752019-06-18 14:40:39 +020027type Registry struct {
Peter Szilagyifbc56f92019-07-23 19:29:46 +000028 register map[uint16]bool
29 counter uint16
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020030 mutex sync.Mutex
kalnagy45114752019-06-18 14:40:39 +020031}
32
Peter Szilagyifbc56f92019-07-23 19:29:46 +000033// This method should run as a constructor
34func (r *Registry) Initialize(seedsn uint16) {
35 r.register = make(map[uint16]bool)
36 r.counter = seedsn
kalnagy45114752019-06-18 14:40:39 +020037}
38
Peter Szilagyifbc56f92019-07-23 19:29:46 +000039// Reserves and returns the next free sequence number
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020040func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
41 // Check is current SequenceNumber valid
Juha Hyttinen1a503442019-12-10 12:14:24 +020042 r.mutex.Lock()
43 defer r.mutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000044 sequenceNumber := r.counter
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020045 if _, ok := r.register[sequenceNumber]; ok {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020046 xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v", sequenceNumber)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020047 return sequenceNumber, false
48 }
Peter Szilagyifbc56f92019-07-23 19:29:46 +000049 r.register[sequenceNumber] = false
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020050
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 Szilagyifbc56f92019-07-23 19:29:46 +000058}
59
60// This function checks the validity of the given subscription id
61func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020062 r.mutex.Lock()
63 defer r.mutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000064 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
72func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020073 r.mutex.Lock()
74 defer r.mutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000075 r.register[sn] = true
76}
77
kalnagy93cc3e22019-09-19 11:29:29 +020078//This function sets the given id as unused in the register
79func (r *Registry) deleteSubscription(sn uint16) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020080 r.mutex.Lock()
81 defer r.mutex.Unlock()
kalnagy93cc3e22019-09-19 11:29:29 +020082 r.register[sn] = false
83}
84
85//This function releases the given id as unused in the register
kalnagye0018682019-09-26 16:28:25 +020086func (r *Registry) releaseSequenceNumber(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020087 r.mutex.Lock()
88 defer r.mutex.Unlock()
89 if _, ok := r.register[sn]; ok {
kalnagye0018682019-09-26 16:28:25 +020090 delete(r.register, sn)
91 return true
Juha Hyttinen1a503442019-12-10 12:14:24 +020092 } else {
93 return false
kalnagye0018682019-09-26 16:28:25 +020094 }
kalnagy1455c852019-10-21 13:06:23 +020095}