blob: 3ac15f1b1278f6452dd88c2c687202d1361eb89d [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
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020027type Subscription struct {
28 Seq uint16
29 Confirmed bool
30}
31
kalnagy45114752019-06-18 14:40:39 +020032type Registry struct {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020033 register map[uint16]*Subscription
Peter Szilagyifbc56f92019-07-23 19:29:46 +000034 counter uint16
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020035 mutex sync.Mutex
kalnagy45114752019-06-18 14:40:39 +020036}
37
Peter Szilagyifbc56f92019-07-23 19:29:46 +000038// This method should run as a constructor
39func (r *Registry) Initialize(seedsn uint16) {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020040 r.register = make(map[uint16]*Subscription)
Peter Szilagyifbc56f92019-07-23 19:29:46 +000041 r.counter = seedsn
kalnagy45114752019-06-18 14:40:39 +020042}
43
Peter Szilagyifbc56f92019-07-23 19:29:46 +000044// Reserves and returns the next free sequence number
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020045func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
46 // Check is current SequenceNumber valid
Juha Hyttinen1a503442019-12-10 12:14:24 +020047 r.mutex.Lock()
48 defer r.mutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000049 sequenceNumber := r.counter
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020050 if _, ok := r.register[sequenceNumber]; ok {
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020051 xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v", sequenceNumber)
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020052 return sequenceNumber, false
53 }
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020054 r.register[sequenceNumber] = &Subscription{sequenceNumber, false}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020055
56 // Allocate next SequenceNumber value
57 if r.counter == 65535 {
58 r.counter = 0
59 } else {
60 r.counter++
61 }
62 return sequenceNumber, true
Peter Szilagyifbc56f92019-07-23 19:29:46 +000063}
64
65// This function checks the validity of the given subscription id
66func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020067 r.mutex.Lock()
68 defer r.mutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000069 xapp.Logger.Debug("Registry map: %v", r.register)
70 if _, ok := r.register[sn]; ok {
71 return true
72 }
73 return false
74}
75
76// This function sets the give id as confirmed in the register
77func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020078 r.mutex.Lock()
79 defer r.mutex.Unlock()
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020080 r.register[sn].Confirmed = true
Peter Szilagyifbc56f92019-07-23 19:29:46 +000081}
82
kalnagy93cc3e22019-09-19 11:29:29 +020083//This function sets the given id as unused in the register
84func (r *Registry) deleteSubscription(sn uint16) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020085 r.mutex.Lock()
86 defer r.mutex.Unlock()
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020087 r.register[sn].Confirmed = false
kalnagy93cc3e22019-09-19 11:29:29 +020088}
89
90//This function releases the given id as unused in the register
kalnagye0018682019-09-26 16:28:25 +020091func (r *Registry) releaseSequenceNumber(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020092 r.mutex.Lock()
93 defer r.mutex.Unlock()
94 if _, ok := r.register[sn]; ok {
kalnagye0018682019-09-26 16:28:25 +020095 delete(r.register, sn)
96 return true
Juha Hyttinen1a503442019-12-10 12:14:24 +020097 } else {
98 return false
kalnagye0018682019-09-26 16:28:25 +020099 }
kalnagy1455c852019-10-21 13:06:23 +0200100}