kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package control |
| 21 | |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 22 | import ( |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 23 | "fmt" |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 24 | "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 25 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 26 | "sync" |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 27 | "time" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 28 | ) |
| 29 | |
Juha Hyttinen | 0d064ec | 2020-01-09 09:08:53 +0200 | [diff] [blame] | 30 | //----------------------------------------------------------------------------- |
| 31 | // |
| 32 | //----------------------------------------------------------------------------- |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 33 | type Registry struct { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 34 | mutex sync.Mutex |
| 35 | register map[uint16]*Subscription |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 36 | subIds []uint16 |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 37 | rtmgrClient *RtmgrClient |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 40 | func (r *Registry) Initialize() { |
Juha Hyttinen | bf2f412 | 2020-01-02 14:11:02 +0200 | [diff] [blame] | 41 | r.register = make(map[uint16]*Subscription) |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 42 | var i uint16 |
| 43 | for i = 0; i < 65535; i++ { |
| 44 | r.subIds = append(r.subIds, i+1) |
| 45 | } |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 48 | func (r *Registry) allocateSubs(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) { |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 49 | if len(r.subIds) > 0 { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 50 | sequenceNumber := r.subIds[0] |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 51 | r.subIds = r.subIds[1:] |
| 52 | if _, ok := r.register[sequenceNumber]; ok == true { |
| 53 | r.subIds = append(r.subIds, sequenceNumber) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 54 | return nil, fmt.Errorf("Registry: Failed to reserve subscription exists") |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 55 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 56 | subs := &Subscription{ |
| 57 | registry: r, |
| 58 | Seq: sequenceNumber, |
| 59 | Meid: trans.Meid, |
| 60 | SubReqMsg: subReqMsg, |
| 61 | valid: true, |
| 62 | } |
| 63 | |
| 64 | if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false { |
| 65 | r.subIds = append(r.subIds, subs.Seq) |
| 66 | return nil, fmt.Errorf("Registry: Endpoint existing already in subscription") |
| 67 | } |
| 68 | |
| 69 | return subs, nil |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 70 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 71 | return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids") |
| 72 | } |
| 73 | |
| 74 | func (r *Registry) findExistingSubs(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) *Subscription { |
| 75 | for _, subs := range r.register { |
| 76 | if subs.IsSame(trans, subReqMsg) { |
| 77 | |
| 78 | // |
| 79 | // check if there has been race conditions |
| 80 | // |
| 81 | subs.mutex.Lock() |
| 82 | //subs has been set to invalid |
| 83 | if subs.valid == false { |
| 84 | subs.mutex.Unlock() |
| 85 | continue |
| 86 | } |
| 87 | // try to add to endpointlist. |
| 88 | if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false { |
| 89 | subs.mutex.Unlock() |
| 90 | continue |
| 91 | } |
| 92 | subs.mutex.Unlock() |
| 93 | |
| 94 | //Race collision during parallel incoming and deleted |
| 95 | xapp.Logger.Debug("Registry: Identical subs found %s for %s", subs.String(), trans.String()) |
| 96 | return subs |
| 97 | } |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | func (r *Registry) AssignToSubscription(trans *Transaction, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) { |
| 103 | var err error |
| 104 | var newAlloc bool |
| 105 | r.mutex.Lock() |
| 106 | defer r.mutex.Unlock() |
| 107 | |
| 108 | subs := r.findExistingSubs(trans, subReqMsg) |
| 109 | |
| 110 | if subs == nil { |
| 111 | subs, err = r.allocateSubs(trans, subReqMsg) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | newAlloc = true |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // |
| 119 | // Add to subscription |
| 120 | // |
| 121 | subs.mutex.Lock() |
| 122 | defer subs.mutex.Unlock() |
| 123 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 124 | epamount := subs.EpList.Size() |
| 125 | |
| 126 | r.mutex.Unlock() |
| 127 | // |
| 128 | // Subscription route updates |
| 129 | // |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 130 | if epamount == 1 { |
| 131 | subRouteAction := SubRouteInfo{CREATE, subs.EpList, subs.Seq} |
| 132 | err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 133 | } else { |
| 134 | subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq} |
| 135 | err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 136 | } |
| 137 | r.mutex.Lock() |
| 138 | |
| 139 | if err != nil { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 140 | if newAlloc { |
| 141 | r.subIds = append(r.subIds, subs.Seq) |
| 142 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 143 | return nil, err |
| 144 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 145 | |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 146 | if newAlloc { |
| 147 | r.register[subs.Seq] = subs |
| 148 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 149 | xapp.Logger.Debug("Registry: Create %s", subs.String()) |
| 150 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
| 151 | return subs, nil |
| 152 | } |
| 153 | |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 154 | // TODO: Needs better logic when there is concurrent calls |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 155 | func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *Transaction, waitRouteClean time.Duration) error { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame^] | 156 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 157 | r.mutex.Lock() |
| 158 | defer r.mutex.Unlock() |
| 159 | subs.mutex.Lock() |
| 160 | defer subs.mutex.Unlock() |
| 161 | |
| 162 | delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint()) |
| 163 | epamount := subs.EpList.Size() |
| 164 | |
| 165 | // |
| 166 | // If last endpoint remove from register map |
| 167 | // |
| 168 | if epamount == 0 { |
| 169 | if _, ok := r.register[subs.Seq]; ok { |
| 170 | xapp.Logger.Debug("Registry: Delete %s", subs.String()) |
| 171 | delete(r.register, subs.Seq) |
Juha Hyttinen | 31797b4 | 2020-01-16 14:05:01 +0200 | [diff] [blame] | 172 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 173 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 174 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 175 | r.mutex.Unlock() |
| 176 | |
| 177 | // |
| 178 | // Wait some time before really do route updates |
| 179 | // |
| 180 | if waitRouteClean > 0 { |
| 181 | subs.mutex.Unlock() |
| 182 | time.Sleep(waitRouteClean) |
| 183 | subs.mutex.Lock() |
| 184 | } |
| 185 | |
| 186 | xapp.Logger.Info("Registry: Cleaning %s", subs.String()) |
| 187 | |
| 188 | // |
| 189 | // Subscription route updates |
| 190 | // |
| 191 | if delStatus { |
| 192 | if epamount == 0 { |
| 193 | tmpList := RmrEndpointList{} |
| 194 | tmpList.AddEndpoint(trans.GetEndpoint()) |
| 195 | subRouteAction := SubRouteInfo{DELETE, tmpList, subs.Seq} |
| 196 | r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 197 | } else { |
| 198 | subRouteAction := SubRouteInfo{UPDATE, subs.EpList, subs.Seq} |
| 199 | r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | r.mutex.Lock() |
| 204 | // |
| 205 | // If last endpoint free seq nro |
| 206 | // |
| 207 | if epamount == 0 { |
| 208 | r.subIds = append(r.subIds, subs.Seq) |
| 209 | } |
| 210 | |
| 211 | return nil |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 212 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 213 | |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 214 | func (r *Registry) GetSubscription(sn uint16) *Subscription { |
| 215 | r.mutex.Lock() |
| 216 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 217 | if _, ok := r.register[sn]; ok { |
| 218 | return r.register[sn] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 219 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 220 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 223 | func (r *Registry) GetSubscriptionFirstMatch(ids []uint16) (*Subscription, error) { |
| 224 | r.mutex.Lock() |
| 225 | defer r.mutex.Unlock() |
| 226 | for _, id := range ids { |
| 227 | if _, ok := r.register[id]; ok { |
| 228 | return r.register[id], nil |
| 229 | } |
| 230 | } |
| 231 | return nil, fmt.Errorf("No valid subscription found with ids %v", ids) |
| 232 | } |