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