blob: 1adb4f7253d8dc945ec317f121f843b65256e862 [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"
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020024 "strconv"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020025 "sync"
26)
27
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020028//-----------------------------------------------------------------------------
29//
30//-----------------------------------------------------------------------------
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020031type Subscription struct {
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020032 mutex sync.Mutex
Juha Hyttinen47b842b2020-01-08 13:01:52 +020033 Seq uint16
34 Active bool
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020035 //
36 Meid *xapp.RMRMeid
37 RmrEndpoint // xapp endpoint
38 Trans *Transaction
39}
40
41func (s *Subscription) String() string {
42 s.mutex.Lock()
43 defer s.mutex.Unlock()
44 return strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
Juha Hyttinen47b842b2020-01-08 13:01:52 +020045}
46
47func (s *Subscription) Confirmed() {
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020048 s.mutex.Lock()
49 defer s.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +020050 s.Active = true
51}
52
53func (s *Subscription) UnConfirmed() {
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020054 s.mutex.Lock()
55 defer s.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +020056 s.Active = false
57}
58
59func (s *Subscription) IsConfirmed() bool {
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020060 s.mutex.Lock()
61 defer s.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +020062 return s.Active
Juha Hyttinenbf2f4122020-01-02 14:11:02 +020063}
64
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020065func (s *Subscription) SetTransaction(trans *Transaction) bool {
66 s.mutex.Lock()
67 defer s.mutex.Unlock()
68 if s.Trans == nil {
69 s.Trans = trans
70 return true
71 }
72 return false
73}
74
75func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
76 s.mutex.Lock()
77 defer s.mutex.Unlock()
78 if trans == nil || trans == s.Trans {
79 s.Trans = nil
80 return true
81 }
82 return false
83}
84
85func (s *Subscription) GetTransaction() *Transaction {
86 s.mutex.Lock()
87 defer s.mutex.Unlock()
88 return s.Trans
89}
90
91func (s *Subscription) SubRouteInfo(act Action) SubRouteInfo {
92 s.mutex.Lock()
93 defer s.mutex.Unlock()
94 return SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq}
95}
96
97//-----------------------------------------------------------------------------
98//
99//-----------------------------------------------------------------------------
kalnagy45114752019-06-18 14:40:39 +0200100type Registry struct {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +0200101 register map[uint16]*Subscription
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000102 counter uint16
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200103 mutex sync.Mutex
kalnagy45114752019-06-18 14:40:39 +0200104}
105
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000106// This method should run as a constructor
107func (r *Registry) Initialize(seedsn uint16) {
Juha Hyttinenbf2f4122020-01-02 14:11:02 +0200108 r.register = make(map[uint16]*Subscription)
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000109 r.counter = seedsn
kalnagy45114752019-06-18 14:40:39 +0200110}
111
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000112// Reserves and returns the next free sequence number
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200113func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid) *Subscription {
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200114 // Check is current SequenceNumber valid
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200115 // Allocate next SequenceNumber value and retry N times
Juha Hyttinen1a503442019-12-10 12:14:24 +0200116 r.mutex.Lock()
117 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200118 var subs *Subscription = nil
119 var retrytimes uint16 = 1000
120 for ; subs == nil && retrytimes > 0; retrytimes-- {
121 sequenceNumber := r.counter
122 if r.counter == 65535 {
123 r.counter = 0
124 } else {
125 r.counter++
126 }
127 if _, ok := r.register[sequenceNumber]; ok == false {
Juha Hyttinen0d064ec2020-01-09 09:08:53 +0200128 r.register[sequenceNumber] = &Subscription{
129 Seq: sequenceNumber,
130 Active: false,
131 RmrEndpoint: endPoint,
132 Meid: meid,
133 Trans: nil,
134 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200135 return r.register[sequenceNumber]
136 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200137 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200138 return nil
139}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200140
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200141func (r *Registry) GetSubscription(sn uint16) *Subscription {
142 r.mutex.Lock()
143 defer r.mutex.Unlock()
144 xapp.Logger.Debug("Registry map: %v", r.register)
145 if _, ok := r.register[sn]; ok {
146 return r.register[sn]
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200147 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200148 return nil
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000149}
150
kalnagy93cc3e22019-09-19 11:29:29 +0200151//This function releases the given id as unused in the register
kalnagye0018682019-09-26 16:28:25 +0200152func (r *Registry) releaseSequenceNumber(sn uint16) bool {
Juha Hyttinen1a503442019-12-10 12:14:24 +0200153 r.mutex.Lock()
154 defer r.mutex.Unlock()
155 if _, ok := r.register[sn]; ok {
kalnagye0018682019-09-26 16:28:25 +0200156 delete(r.register, sn)
157 return true
Juha Hyttinen1a503442019-12-10 12:14:24 +0200158 } else {
159 return false
kalnagye0018682019-09-26 16:28:25 +0200160 }
kalnagy1455c852019-10-21 13:06:23 +0200161}