blob: 0fadabbc7780b4cf01c69838312a4eb8b413ee63 [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 (
Juha Hyttinen0388dd92020-01-09 14:14:16 +020023 "fmt"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020024 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
25 "sync"
26)
27
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020028//-----------------------------------------------------------------------------
29//
30//-----------------------------------------------------------------------------
kalnagy45114752019-06-18 14:40:39 +020031type Registry struct {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020032 mutex sync.Mutex
33 register map[uint16]*Subscription
Anssi Mannila5c161a92020-01-15 15:40:57 +020034 subIds []uint16
Juha Hyttinen0388dd92020-01-09 14:14:16 +020035 rtmgrClient *RtmgrClient
kalnagy45114752019-06-18 14:40:39 +020036}
37
Peter Szilagyifbc56f92019-07-23 19:29:46 +000038// This method should run as a constructor
Anssi Mannila5c161a92020-01-15 15:40:57 +020039func (r *Registry) Initialize() {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020040 r.register = make(map[uint16]*Subscription)
Anssi Mannila5c161a92020-01-15 15:40:57 +020041 var i uint16
42 for i = 0; i < 65535; i++ {
43 r.subIds = append(r.subIds, i+1)
44 }
kalnagy45114752019-06-18 14:40:39 +020045}
46
Peter Szilagyifbc56f92019-07-23 19:29:46 +000047// Reserves and returns the next free sequence number
Juha Hyttinen31797b42020-01-16 14:05:01 +020048func (r *Registry) ReserveSubscription(meid *xapp.RMRMeid) (*Subscription, error) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020049 r.mutex.Lock()
50 defer r.mutex.Unlock()
Juha Hyttinen31797b42020-01-16 14:05:01 +020051 if len(r.subIds) > 0 {
Anssi Mannila5c161a92020-01-15 15:40:57 +020052 sequenceNumber := r.subIds[0]
53 r.subIds = r.subIds[1:]
Juha Hyttinen47b842b2020-01-08 13:01:52 +020054 if _, ok := r.register[sequenceNumber]; ok == false {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020055 subs := &Subscription{
Juha Hyttinen31797b42020-01-16 14:05:01 +020056 registry: r,
57 Seq: sequenceNumber,
58 Active: false,
59 Meid: meid,
60 Trans: nil,
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020061 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +020062 r.register[sequenceNumber] = subs
Juha Hyttinen31797b42020-01-16 14:05:01 +020063 xapp.Logger.Info("Registry: Create %s", subs.String())
64 xapp.Logger.Debug("Registry: substable=%v", r.register)
Juha Hyttinen0388dd92020-01-09 14:14:16 +020065 return subs, nil
Juha Hyttinen47b842b2020-01-08 13:01:52 +020066 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020067 }
Juha Hyttinen31797b42020-01-16 14:05:01 +020068 return nil, fmt.Errorf("Registry: Failed to reserves subscription")
Juha Hyttinen47b842b2020-01-08 13:01:52 +020069}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020070
Juha Hyttinen47b842b2020-01-08 13:01:52 +020071func (r *Registry) GetSubscription(sn uint16) *Subscription {
72 r.mutex.Lock()
73 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +020074 if _, ok := r.register[sn]; ok {
75 return r.register[sn]
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020076 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +020077 return nil
Peter Szilagyifbc56f92019-07-23 19:29:46 +000078}
79
Juha Hyttinen0388dd92020-01-09 14:14:16 +020080func (r *Registry) DelSubscription(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020081 r.mutex.Lock()
82 defer r.mutex.Unlock()
83 if _, ok := r.register[sn]; ok {
Juha Hyttinen31797b42020-01-16 14:05:01 +020084 subs := r.register[sn]
85 xapp.Logger.Info("Registry: Delete %s", subs.String())
Anssi Mannila5c161a92020-01-15 15:40:57 +020086 r.subIds = append(r.subIds, sn)
kalnagye0018682019-09-26 16:28:25 +020087 delete(r.register, sn)
Juha Hyttinen31797b42020-01-16 14:05:01 +020088 xapp.Logger.Debug("Registry: substable=%v", r.register)
kalnagye0018682019-09-26 16:28:25 +020089 return true
90 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +020091 return false
kalnagy1455c852019-10-21 13:06:23 +020092}