blob: 3d70d63033f5ddc433d31fdbdd7cb93f6704a6b4 [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 {
Juha Hyttinen47b842b2020-01-08 13:01:52 +020028 Seq uint16
29 Active bool
30}
31
32func (s *Subscription) Confirmed() {
33 s.Active = true
34}
35
36func (s *Subscription) UnConfirmed() {
37 s.Active = false
38}
39
40func (s *Subscription) IsConfirmed() bool {
41 return s.Active
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020042}
43
kalnagy45114752019-06-18 14:40:39 +020044type Registry struct {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020045 register map[uint16]*Subscription
Peter Szilagyifbc56f92019-07-23 19:29:46 +000046 counter uint16
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020047 mutex sync.Mutex
kalnagy45114752019-06-18 14:40:39 +020048}
49
Peter Szilagyifbc56f92019-07-23 19:29:46 +000050// This method should run as a constructor
51func (r *Registry) Initialize(seedsn uint16) {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020052 r.register = make(map[uint16]*Subscription)
Peter Szilagyifbc56f92019-07-23 19:29:46 +000053 r.counter = seedsn
kalnagy45114752019-06-18 14:40:39 +020054}
55
Peter Szilagyifbc56f92019-07-23 19:29:46 +000056// Reserves and returns the next free sequence number
Juha Hyttinen47b842b2020-01-08 13:01:52 +020057func (r *Registry) ReserveSubscription() *Subscription {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020058 // Check is current SequenceNumber valid
Juha Hyttinen47b842b2020-01-08 13:01:52 +020059 // Allocate next SequenceNumber value and retry N times
Juha Hyttinen1a503442019-12-10 12:14:24 +020060 r.mutex.Lock()
61 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +020062 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 Mannila2e99e2f2019-12-05 13:57:06 +020075 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +020076 return nil
77}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020078
Juha Hyttinen47b842b2020-01-08 13:01:52 +020079// This function checks the validity of the given subscription id
80func (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 Mannila2e99e2f2019-12-05 13:57:06 +020086 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +020087 return nil
Peter Szilagyifbc56f92019-07-23 19:29:46 +000088}
89
90// This function checks the validity of the given subscription id
91func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020092 r.mutex.Lock()
93 defer r.mutex.Unlock()
Peter Szilagyifbc56f92019-07-23 19:29:46 +000094 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
102func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
Juha Hyttinen1a503442019-12-10 12:14:24 +0200103 r.mutex.Lock()
104 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200105 r.register[sn].Confirmed()
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000106}
107
kalnagy93cc3e22019-09-19 11:29:29 +0200108//This function sets the given id as unused in the register
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200109func (r *Registry) setSubscriptionToUnConfirmed(sn uint16) {
Juha Hyttinen1a503442019-12-10 12:14:24 +0200110 r.mutex.Lock()
111 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200112 r.register[sn].UnConfirmed()
kalnagy93cc3e22019-09-19 11:29:29 +0200113}
114
115//This function releases the given id as unused in the register
kalnagye0018682019-09-26 16:28:25 +0200116func (r *Registry) releaseSequenceNumber(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +0200117 r.mutex.Lock()
118 defer r.mutex.Unlock()
119 if _, ok := r.register[sn]; ok {
kalnagye0018682019-09-26 16:28:25 +0200120 delete(r.register, sn)
121 return true
Juha Hyttinen1a503442019-12-10 12:14:24 +0200122 } else {
123 return false
kalnagye0018682019-09-26 16:28:25 +0200124 }
kalnagy1455c852019-10-21 13:06:23 +0200125}