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 | //----------------------------------------------------------------------------- |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 33 | |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 34 | type Registry struct { |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 35 | mutex sync.Mutex |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 36 | register map[uint32]*Subscription |
| 37 | subIds []uint32 |
Juha Hyttinen | 0388dd9 | 2020-01-09 14:14:16 +0200 | [diff] [blame] | 38 | rtmgrClient *RtmgrClient |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 41 | func (r *Registry) Initialize() { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 42 | r.register = make(map[uint32]*Subscription) |
| 43 | var i uint32 |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 44 | for i = 0; i < 65535; i++ { |
| 45 | r.subIds = append(r.subIds, i+1) |
| 46 | } |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 49 | func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) { |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 50 | if len(r.subIds) > 0 { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 51 | sequenceNumber := r.subIds[0] |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 52 | r.subIds = r.subIds[1:] |
| 53 | if _, ok := r.register[sequenceNumber]; ok == true { |
| 54 | r.subIds = append(r.subIds, sequenceNumber) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 55 | return nil, fmt.Errorf("Registry: Failed to reserve subscription exists") |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 56 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 57 | subs := &Subscription{ |
| 58 | registry: r, |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 59 | Meid: trans.Meid, |
| 60 | SubReqMsg: subReqMsg, |
| 61 | valid: true, |
| 62 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 63 | subs.ReqId.Id = 123 |
| 64 | subs.ReqId.Seq = sequenceNumber |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 65 | |
| 66 | if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 67 | r.subIds = append(r.subIds, subs.ReqId.Seq) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 68 | return nil, fmt.Errorf("Registry: Endpoint existing already in subscription") |
| 69 | } |
| 70 | |
| 71 | return subs, nil |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 72 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 73 | return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids") |
| 74 | } |
| 75 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 76 | func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) *Subscription { |
| 77 | |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 78 | for _, subs := range r.register { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 79 | if subs.IsMergeable(trans, subReqMsg) { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 80 | |
| 81 | // |
| 82 | // check if there has been race conditions |
| 83 | // |
| 84 | subs.mutex.Lock() |
| 85 | //subs has been set to invalid |
| 86 | if subs.valid == false { |
| 87 | subs.mutex.Unlock() |
| 88 | continue |
| 89 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 90 | // If size is zero, entry is to be deleted |
| 91 | if subs.EpList.Size() == 0 { |
| 92 | subs.mutex.Unlock() |
| 93 | continue |
| 94 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 95 | // try to add to endpointlist. |
| 96 | if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false { |
| 97 | subs.mutex.Unlock() |
| 98 | continue |
| 99 | } |
| 100 | subs.mutex.Unlock() |
| 101 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 102 | xapp.Logger.Debug("Registry: Mergeable subs found %s for %s", subs.String(), trans.String()) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 103 | return subs |
| 104 | } |
| 105 | } |
| 106 | return nil |
| 107 | } |
| 108 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 109 | func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, error) { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 110 | var err error |
| 111 | var newAlloc bool |
| 112 | r.mutex.Lock() |
| 113 | defer r.mutex.Unlock() |
| 114 | |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame^] | 115 | // |
| 116 | // Check validity of subscription action types |
| 117 | // |
| 118 | actionType, err := r.CheckActionTypes(subReqMsg) |
| 119 | if err != nil { |
| 120 | xapp.Logger.Debug("CREATE %s", err) |
| 121 | return nil, err |
| 122 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 123 | |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame^] | 124 | // |
| 125 | // Find possible existing Policy subscription |
| 126 | // |
| 127 | if actionType == e2ap.E2AP_ActionTypePolicy { |
| 128 | if subs, ok := r.register[subReqMsg.RequestId.Seq]; ok { |
| 129 | xapp.Logger.Debug("CREATE %s. Existing subscription for Policy found", subs.String()) |
| 130 | subs.SetCachedResponse(nil, true) |
| 131 | return subs, nil |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | subs := r.findExistingSubs(trans, subReqMsg) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 136 | if subs == nil { |
| 137 | subs, err = r.allocateSubs(trans, subReqMsg) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | newAlloc = true |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // |
| 145 | // Add to subscription |
| 146 | // |
| 147 | subs.mutex.Lock() |
| 148 | defer subs.mutex.Unlock() |
| 149 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 150 | epamount := subs.EpList.Size() |
| 151 | |
| 152 | r.mutex.Unlock() |
| 153 | // |
| 154 | // Subscription route updates |
| 155 | // |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 156 | if epamount == 1 { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 157 | subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.Seq)} |
| 158 | err = r.rtmgrClient.SubscriptionRequestCreate(subRouteAction) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 159 | } else { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 160 | subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.Seq)} |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 161 | err = r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 162 | } |
| 163 | r.mutex.Lock() |
| 164 | |
| 165 | if err != nil { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 166 | if newAlloc { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 167 | r.subIds = append(r.subIds, subs.ReqId.Seq) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 168 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 169 | return nil, err |
| 170 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 171 | |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 172 | if newAlloc { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 173 | r.register[subs.ReqId.Seq] = subs |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 174 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 175 | xapp.Logger.Debug("CREATE %s", subs.String()) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 176 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
| 177 | return subs, nil |
| 178 | } |
| 179 | |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame^] | 180 | func (r *Registry) CheckActionTypes(subReqMsg *e2ap.E2APSubscriptionRequest) (uint64, error) { |
| 181 | var reportFound bool = false |
| 182 | var policyFound bool = false |
| 183 | |
| 184 | for _, acts := range subReqMsg.ActionSetups { |
| 185 | if acts.ActionType == e2ap.E2AP_ActionTypeReport { |
| 186 | reportFound = true |
| 187 | } |
| 188 | if acts.ActionType == e2ap.E2AP_ActionTypePolicy { |
| 189 | policyFound = true |
| 190 | } |
| 191 | } |
| 192 | if reportFound == true && policyFound == true { |
| 193 | return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Report and Policy in same RICactions-ToBeSetup-List") |
| 194 | } |
| 195 | if reportFound == true { |
| 196 | return e2ap.E2AP_ActionTypeReport, nil |
| 197 | } |
| 198 | if policyFound == true { |
| 199 | return e2ap.E2AP_ActionTypePolicy, nil |
| 200 | } |
| 201 | return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Invalid action type in RICactions-ToBeSetup-List") |
| 202 | } |
| 203 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 204 | // TODO: Works with concurrent calls, but check if can be improved |
| 205 | func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *TransactionXapp, waitRouteClean time.Duration) error { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 206 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 207 | r.mutex.Lock() |
| 208 | defer r.mutex.Unlock() |
| 209 | subs.mutex.Lock() |
| 210 | defer subs.mutex.Unlock() |
| 211 | |
| 212 | delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint()) |
| 213 | epamount := subs.EpList.Size() |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 214 | seqId := subs.ReqId.Seq |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 215 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 216 | if delStatus == false { |
| 217 | return nil |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 218 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 219 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 220 | r.mutex.Unlock() |
| 221 | |
| 222 | // |
| 223 | // Wait some time before really do route updates |
| 224 | // |
| 225 | if waitRouteClean > 0 { |
| 226 | subs.mutex.Unlock() |
| 227 | time.Sleep(waitRouteClean) |
| 228 | subs.mutex.Lock() |
| 229 | } |
| 230 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 231 | xapp.Logger.Info("CLEAN %s", subs.String()) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 232 | |
| 233 | // |
| 234 | // Subscription route updates |
| 235 | // |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 236 | if epamount == 0 { |
| 237 | tmpList := RmrEndpointList{} |
| 238 | tmpList.AddEndpoint(trans.GetEndpoint()) |
| 239 | subRouteAction := SubRouteInfo{tmpList, uint16(seqId)} |
| 240 | r.rtmgrClient.SubscriptionRequestDelete(subRouteAction) |
| 241 | } else if subs.EpList.Size() > 0 { |
| 242 | subRouteAction := SubRouteInfo{subs.EpList, uint16(seqId)} |
| 243 | r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | r.mutex.Lock() |
| 247 | // |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 248 | // If last endpoint, release and free seqid |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 249 | // |
| 250 | if epamount == 0 { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 251 | if _, ok := r.register[seqId]; ok { |
| 252 | xapp.Logger.Debug("RELEASE %s", subs.String()) |
| 253 | delete(r.register, seqId) |
| 254 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
| 255 | } |
| 256 | r.subIds = append(r.subIds, seqId) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | return nil |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 260 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 261 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 262 | func (r *Registry) GetSubscription(sn uint32) *Subscription { |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 263 | r.mutex.Lock() |
| 264 | defer r.mutex.Unlock() |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 265 | if _, ok := r.register[sn]; ok { |
| 266 | return r.register[sn] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 267 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 268 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 271 | func (r *Registry) GetSubscriptionFirstMatch(ids []uint32) (*Subscription, error) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 272 | r.mutex.Lock() |
| 273 | defer r.mutex.Unlock() |
| 274 | for _, id := range ids { |
| 275 | if _, ok := r.register[id]; ok { |
| 276 | return r.register[id], nil |
| 277 | } |
| 278 | } |
| 279 | return nil, fmt.Errorf("No valid subscription found with ids %v", ids) |
| 280 | } |