blob: 5edc3eb7182893bd6d145445db72bf24da8bf17b [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"
Juha Hyttinen422d0182020-01-17 13:37:05 +020024 "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020025 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26 "sync"
27)
28
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020029//-----------------------------------------------------------------------------
30//
31//-----------------------------------------------------------------------------
kalnagy45114752019-06-18 14:40:39 +020032type Registry struct {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020033 mutex sync.Mutex
34 register map[uint16]*Subscription
Anssi Mannila5c161a92020-01-15 15:40:57 +020035 subIds []uint16
Juha Hyttinen0388dd92020-01-09 14:14:16 +020036 rtmgrClient *RtmgrClient
kalnagy45114752019-06-18 14:40:39 +020037}
38
Peter Szilagyifbc56f92019-07-23 19:29:46 +000039// This method should run as a constructor
Anssi Mannila5c161a92020-01-15 15:40:57 +020040func (r *Registry) Initialize() {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020041 r.register = make(map[uint16]*Subscription)
Anssi Mannila5c161a92020-01-15 15:40:57 +020042 var i uint16
43 for i = 0; i < 65535; i++ {
44 r.subIds = append(r.subIds, i+1)
45 }
kalnagy45114752019-06-18 14:40:39 +020046}
47
Peter Szilagyifbc56f92019-07-23 19:29:46 +000048// Reserves and returns the next free sequence number
Juha Hyttinen422d0182020-01-17 13:37:05 +020049func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020050 r.mutex.Lock()
51 defer r.mutex.Unlock()
Juha Hyttinen31797b42020-01-16 14:05:01 +020052 if len(r.subIds) > 0 {
Anssi Mannila5c161a92020-01-15 15:40:57 +020053 sequenceNumber := r.subIds[0]
54 r.subIds = r.subIds[1:]
Juha Hyttinen47b842b2020-01-08 13:01:52 +020055 if _, ok := r.register[sequenceNumber]; ok == false {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020056 subs := &Subscription{
Juha Hyttinen31797b42020-01-16 14:05:01 +020057 registry: r,
58 Seq: sequenceNumber,
Juha Hyttinen422d0182020-01-17 13:37:05 +020059 Meid: trans.Meid,
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020060 }
Juha Hyttinen422d0182020-01-17 13:37:05 +020061 err := subs.AddEndpoint(trans.GetEndpoint())
62 if err != nil {
63 return nil, err
64 }
65 subs.SubReqMsg = subReqMsg
66
Juha Hyttinen0388dd92020-01-09 14:14:16 +020067 r.register[sequenceNumber] = subs
Juha Hyttinen422d0182020-01-17 13:37:05 +020068 xapp.Logger.Debug("Registry: Create %s", subs.String())
Juha Hyttinen31797b42020-01-16 14:05:01 +020069 xapp.Logger.Debug("Registry: substable=%v", r.register)
Juha Hyttinen0388dd92020-01-09 14:14:16 +020070 return subs, nil
Juha Hyttinen47b842b2020-01-08 13:01:52 +020071 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020072 }
Juha Hyttinen31797b42020-01-16 14:05:01 +020073 return nil, fmt.Errorf("Registry: Failed to reserves subscription")
Juha Hyttinen47b842b2020-01-08 13:01:52 +020074}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020075
Juha Hyttinen47b842b2020-01-08 13:01:52 +020076func (r *Registry) GetSubscription(sn uint16) *Subscription {
77 r.mutex.Lock()
78 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +020079 if _, ok := r.register[sn]; ok {
80 return r.register[sn]
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020081 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +020082 return nil
Peter Szilagyifbc56f92019-07-23 19:29:46 +000083}
84
Juha Hyttinen422d0182020-01-17 13:37:05 +020085func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) {
86 r.mutex.Lock()
87 defer r.mutex.Unlock()
88 for _, id := range ids {
89 if _, ok := r.register[id]; ok {
90 return r.register[id], nil
91 }
92 }
93 return nil, fmt.Errorf("No valid subscription found with ids %v", ids)
94}
95
Juha Hyttinen0388dd92020-01-09 14:14:16 +020096func (r *Registry) DelSubscription(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020097 r.mutex.Lock()
98 defer r.mutex.Unlock()
99 if _, ok := r.register[sn]; ok {
Juha Hyttinen31797b42020-01-16 14:05:01 +0200100 subs := r.register[sn]
Juha Hyttinen422d0182020-01-17 13:37:05 +0200101 xapp.Logger.Debug("Registry: Delete %s", subs.String())
Anssi Mannila5c161a92020-01-15 15:40:57 +0200102 r.subIds = append(r.subIds, sn)
kalnagye0018682019-09-26 16:28:25 +0200103 delete(r.register, sn)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200104 xapp.Logger.Debug("Registry: substable=%v", r.register)
kalnagye0018682019-09-26 16:28:25 +0200105 return true
106 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200107 return false
kalnagy1455c852019-10-21 13:06:23 +0200108}