blob: 2750b78e28d45424d6dca1acd28b443c6bd847e0 [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"
Juha Hyttinen12d31af2020-01-22 12:59:01 +020027 "time"
Anssi Mannila2e99e2f2019-12-05 13:57:06 +020028)
29
Juha Hyttinen0d064ec2020-01-09 09:08:53 +020030//-----------------------------------------------------------------------------
31//
32//-----------------------------------------------------------------------------
kalnagy45114752019-06-18 14:40:39 +020033type Registry struct {
Juha Hyttinen0388dd92020-01-09 14:14:16 +020034 mutex sync.Mutex
35 register map[uint16]*Subscription
Anssi Mannila5c161a92020-01-15 15:40:57 +020036 subIds []uint16
Juha Hyttinen0388dd92020-01-09 14:14:16 +020037 rtmgrClient *RtmgrClient
kalnagy45114752019-06-18 14:40:39 +020038}
39
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
Juha Hyttinen422d0182020-01-17 13:37:05 +020048func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) {
Juha Hyttinen1a503442019-12-10 12:14:24 +020049 r.mutex.Lock()
50 defer r.mutex.Unlock()
Juha Hyttinen422d0182020-01-17 13:37:05 +020051
Juha Hyttinen12d31af2020-01-22 12:59:01 +020052 var sequenceNumber uint16
53
54 //
55 // Allocate subscription
56 //
57 if len(r.subIds) > 0 {
58 sequenceNumber = r.subIds[0]
59 r.subIds = r.subIds[1:]
60 if _, ok := r.register[sequenceNumber]; ok == true {
61 r.subIds = append(r.subIds, sequenceNumber)
62 return nil, fmt.Errorf("Registry: Failed to reserves subscription")
63 }
64 } else {
65 return nil, fmt.Errorf("Registry: Failed to reserves subscription no free ids")
66 }
67 subs := &Subscription{
68 registry: r,
69 Seq: sequenceNumber,
70 Meid: trans.Meid,
71 }
72
73 //
74 // Add to subscription
75 //
76 subs.mutex.Lock()
77 defer subs.mutex.Unlock()
78
79 if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
80 r.subIds = append(r.subIds, sequenceNumber)
81 return nil, fmt.Errorf("Registry: Endpoint existing already in subscription")
82 }
83 epamount := subs.EpList.Size()
84
85 r.mutex.Unlock()
86 //
87 // Subscription route updates
88 //
89 var err error
90 if epamount == 1 {
91 subRouteAction := SubRouteInfo{CREATE, subs.EpList, subs.Seq}
92 err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
93 } else {
94 subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq}
95 err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
96 }
97 r.mutex.Lock()
98
99 if err != nil {
100 r.subIds = append(r.subIds, sequenceNumber)
101 return nil, err
102 }
103 subs.SubReqMsg = subReqMsg
104
105 r.register[sequenceNumber] = subs
106 xapp.Logger.Debug("Registry: Create %s", subs.String())
107 xapp.Logger.Debug("Registry: substable=%v", r.register)
108 return subs, nil
109}
110
111func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *Transaction, waitRouteClean time.Duration) error {
112 r.mutex.Lock()
113 defer r.mutex.Unlock()
114 subs.mutex.Lock()
115 defer subs.mutex.Unlock()
116
117 delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint())
118 epamount := subs.EpList.Size()
119
120 //
121 // If last endpoint remove from register map
122 //
123 if epamount == 0 {
124 if _, ok := r.register[subs.Seq]; ok {
125 xapp.Logger.Debug("Registry: Delete %s", subs.String())
126 delete(r.register, subs.Seq)
Juha Hyttinen31797b42020-01-16 14:05:01 +0200127 xapp.Logger.Debug("Registry: substable=%v", r.register)
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200128 }
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200129 }
Juha Hyttinen12d31af2020-01-22 12:59:01 +0200130 r.mutex.Unlock()
131
132 //
133 // Wait some time before really do route updates
134 //
135 if waitRouteClean > 0 {
136 subs.mutex.Unlock()
137 time.Sleep(waitRouteClean)
138 subs.mutex.Lock()
139 }
140
141 xapp.Logger.Info("Registry: Cleaning %s", subs.String())
142
143 //
144 // Subscription route updates
145 //
146 if delStatus {
147 if epamount == 0 {
148 tmpList := RmrEndpointList{}
149 tmpList.AddEndpoint(trans.GetEndpoint())
150 subRouteAction := SubRouteInfo{DELETE, tmpList, subs.Seq}
151 r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
152 } else {
153 subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq}
154 r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
155 }
156 }
157
158 r.mutex.Lock()
159 //
160 // If last endpoint free seq nro
161 //
162 if epamount == 0 {
163 r.subIds = append(r.subIds, subs.Seq)
164 }
165
166 return nil
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200167}
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200168
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200169func (r *Registry) GetSubscription(sn uint16) *Subscription {
170 r.mutex.Lock()
171 defer r.mutex.Unlock()
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200172 if _, ok := r.register[sn]; ok {
173 return r.register[sn]
Anssi Mannila2e99e2f2019-12-05 13:57:06 +0200174 }
Juha Hyttinen47b842b2020-01-08 13:01:52 +0200175 return nil
Peter Szilagyifbc56f92019-07-23 19:29:46 +0000176}
177
Juha Hyttinen422d0182020-01-17 13:37:05 +0200178func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) {
179 r.mutex.Lock()
180 defer r.mutex.Unlock()
181 for _, id := range ids {
182 if _, ok := r.register[id]; ok {
183 return r.register[id], nil
184 }
185 }
186 return nil, fmt.Errorf("No valid subscription found with ids %v", ids)
187}