blob: 0970a3aeb281f6a88b2b37df205741ea8af6b275 [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
34 counter uint16
35 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
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
Juha Hyttinen86a46202020-01-14 12:49:09 +020045func (r *Registry) ReserveSubscription(endPoint *RmrEndpoint, meid *xapp.RMRMeid) (*Subscription, error) {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020046 // Check is current SequenceNumber valid
Juha Hyttinen47b842b2020-01-08 13:01:52 +020047 // Allocate next SequenceNumber value and retry N times
Juha Hyttinen1a503442019-12-10 12:14:24 +020048 r.mutex.Lock()
49 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +020050 var subs *Subscription = nil
51 var retrytimes uint16 = 1000
52 for ; subs == nil && retrytimes > 0; retrytimes-- {
53 sequenceNumber := r.counter
54 if r.counter == 65535 {
55 r.counter = 0
56 } else {
57 r.counter++
58 }
59 if _, ok := r.register[sequenceNumber]; ok == false {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020060 subs := &Subscription{
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020061 Seq: sequenceNumber,
62 Active: false,
Juha Hyttinen86a46202020-01-14 12:49:09 +020063 RmrEndpoint: *endPoint,
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020064 Meid: meid,
65 Trans: nil,
66 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +020067 r.register[sequenceNumber] = subs
68
69 // Update routing
70 r.mutex.Unlock()
71 err := subs.UpdateRoute(CREATE, r.rtmgrClient)
72 r.mutex.Lock()
73 if err != nil {
74 if _, ok := r.register[sequenceNumber]; ok {
75 delete(r.register, sequenceNumber)
76 }
77 return nil, err
78 }
79 return subs, nil
Juha Hyttinen47b842b2020-01-08 13:01:52 +020080 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020081 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +020082 return nil, fmt.Errorf("Registry: Failed to reserves subcription. RmrEndpoint: %s, Meid: %s", endPoint, meid.RanName)
Juha Hyttinen47b842b2020-01-08 13:01:52 +020083}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020084
Juha Hyttinen47b842b2020-01-08 13:01:52 +020085func (r *Registry) GetSubscription(sn uint16) *Subscription {
86 r.mutex.Lock()
87 defer r.mutex.Unlock()
88 xapp.Logger.Debug("Registry map: %v", r.register)
89 if _, ok := r.register[sn]; ok {
90 return r.register[sn]
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020091 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +020092 return nil
Peter Szilagyifbc56f92019-07-23 19:29:46 +000093}
94
Juha Hyttinen0388dd92020-01-09 14:14:16 +020095func (r *Registry) DelSubscription(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +020096 r.mutex.Lock()
97 defer r.mutex.Unlock()
98 if _, ok := r.register[sn]; ok {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020099 subs := r.register[sn]
kalnagye0018682019-09-26 16:28:25 +0200100 delete(r.register, sn)
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200101
102 // Update routing
103 r.mutex.Unlock()
104 err := subs.UpdateRoute(DELETE, r.rtmgrClient)
105 r.mutex.Lock()
106 if err != nil {
107 xapp.Logger.Error("Registry: Failed to del route. SubId: %d, RmrEndpoint: %s", subs.Seq, subs.RmrEndpoint)
108 }
kalnagye0018682019-09-26 16:28:25 +0200109 return true
110 }
Juha Hyttinen0388dd92020-01-09 14:14:16 +0200111 return false
kalnagy1455c852019-10-21 13:06:23 +0200112}