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" |
archagge | afbf95f | 2021-04-14 08:54:05 +0300 | [diff] [blame^] | 24 | "sync" |
| 25 | "time" |
| 26 | |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" |
Juha Hyttinen | c9eb08a | 2020-02-28 08:53:33 +0200 | [diff] [blame] | 28 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 29 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
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 | |
archagge | afbf95f | 2021-04-14 08:54:05 +0300 | [diff] [blame^] | 36 | type RESTSubscription struct { |
| 37 | xAppRmrEndPoint string |
| 38 | Meid string |
| 39 | InstanceIds []uint32 |
| 40 | SubReqOngoing bool |
| 41 | SubDelReqOngoing bool |
| 42 | } |
| 43 | |
| 44 | func (r *RESTSubscription) AddInstanceId(instanceId uint32) { |
| 45 | r.InstanceIds = append(r.InstanceIds, instanceId) |
| 46 | } |
| 47 | |
| 48 | func (r *RESTSubscription) SetProcessed() { |
| 49 | r.SubReqOngoing = false |
| 50 | } |
| 51 | |
| 52 | func (r *RESTSubscription) DeleteInstanceId(instanceId uint32) { |
| 53 | r.InstanceIds = r.InstanceIds[1:] |
| 54 | } |
| 55 | |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 56 | type Registry struct { |
archagge | afbf95f | 2021-04-14 08:54:05 +0300 | [diff] [blame^] | 57 | mutex sync.Mutex |
| 58 | register map[uint32]*Subscription |
| 59 | subIds []uint32 |
| 60 | rtmgrClient *RtmgrClient |
| 61 | restSubscriptions map[string]*RESTSubscription |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 64 | func (r *Registry) Initialize() { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 65 | r.register = make(map[uint32]*Subscription) |
archagge | afbf95f | 2021-04-14 08:54:05 +0300 | [diff] [blame^] | 66 | r.restSubscriptions = make(map[string]*RESTSubscription) |
| 67 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 68 | var i uint32 |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 69 | for i = 1; i < 65535; i++ { |
| 70 | r.subIds = append(r.subIds, i) |
Anssi Mannila | 5c161a9 | 2020-01-15 15:40:57 +0200 | [diff] [blame] | 71 | } |
kalnagy | 4511475 | 2019-06-18 14:40:39 +0200 | [diff] [blame] | 72 | } |
| 73 | |
archagge | afbf95f | 2021-04-14 08:54:05 +0300 | [diff] [blame^] | 74 | func (r *Registry) CreateRESTSubscription(restSubId *string, xAppRmrEndPoint *string, maid *string) (*RESTSubscription, error) { |
| 75 | r.mutex.Lock() |
| 76 | defer r.mutex.Unlock() |
| 77 | newRestSubscription := RESTSubscription{} |
| 78 | newRestSubscription.xAppRmrEndPoint = *xAppRmrEndPoint |
| 79 | newRestSubscription.Meid = *maid |
| 80 | newRestSubscription.SubReqOngoing = true |
| 81 | newRestSubscription.SubDelReqOngoing = false |
| 82 | r.restSubscriptions[*restSubId] = &newRestSubscription |
| 83 | xapp.Logger.Info("Registry: Created REST subscription successfully. restSubId=%v, subscriptionCount=%v, e2apCubscriptionCount=%v", *restSubId, len(r.restSubscriptions), len(r.register)) |
| 84 | return &newRestSubscription, nil |
| 85 | } |
| 86 | |
| 87 | func (r *Registry) DeleteRESTSubscription(restSubId *string) { |
| 88 | r.mutex.Lock() |
| 89 | defer r.mutex.Unlock() |
| 90 | delete(r.restSubscriptions, *restSubId) |
| 91 | xapp.Logger.Info("Registry: Deleted REST subscription successfully. restSubId=%v, subscriptionCount=%v", *restSubId, len(r.restSubscriptions)) |
| 92 | } |
| 93 | |
| 94 | func (r *Registry) GetRESTSubscription(restSubId string) (*RESTSubscription, error) { |
| 95 | r.mutex.Lock() |
| 96 | defer r.mutex.Unlock() |
| 97 | if restSubscription, ok := r.restSubscriptions[restSubId]; ok { |
| 98 | // Subscription deletion is not allowed if prosessing subscription request in not ready |
| 99 | if restSubscription.SubDelReqOngoing == false && restSubscription.SubReqOngoing == false { |
| 100 | restSubscription.SubDelReqOngoing = true |
| 101 | r.restSubscriptions[restSubId] = restSubscription |
| 102 | return restSubscription, nil |
| 103 | } else { |
| 104 | return restSubscription, fmt.Errorf("Registry: REST delete request is still ongoing for the endpoint=%v, restSubId=%v, SubDelReqOngoing=%v, SubReqOngoing=%v", restSubscription, restSubId, restSubscription.SubDelReqOngoing, restSubscription.SubReqOngoing) |
| 105 | } |
| 106 | return restSubscription, nil |
| 107 | } |
| 108 | return nil, fmt.Errorf("Registry: No valid subscription found with restSubId=%v", restSubId) |
| 109 | } |
| 110 | |
Juha Hyttinen | c9eb08a | 2020-02-28 08:53:33 +0200 | [diff] [blame] | 111 | func (r *Registry) QueryHandler() (models.SubscriptionList, error) { |
| 112 | r.mutex.Lock() |
| 113 | defer r.mutex.Unlock() |
| 114 | |
| 115 | resp := models.SubscriptionList{} |
| 116 | for _, subs := range r.register { |
| 117 | subs.mutex.Lock() |
archagge | a5c58bc | 2021-04-14 08:54:05 +0300 | [diff] [blame] | 118 | resp = append(resp, &models.SubscriptionData{SubscriptionID: int64(subs.ReqId.InstanceId), Meid: subs.Meid.RanName, ClientEndpoint: subs.EpList.StringList()}) |
Juha Hyttinen | c9eb08a | 2020-02-28 08:53:33 +0200 | [diff] [blame] | 119 | subs.mutex.Unlock() |
| 120 | } |
| 121 | return resp, nil |
| 122 | } |
| 123 | |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 124 | func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool) (*Subscription, error) { |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 125 | if len(r.subIds) > 0 { |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 126 | subId := r.subIds[0] |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 127 | r.subIds = r.subIds[1:] |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 128 | if _, ok := r.register[subId]; ok == true { |
| 129 | r.subIds = append(r.subIds, subId) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 130 | return nil, fmt.Errorf("Registry: Failed to reserve subscription exists") |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 131 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 132 | subs := &Subscription{ |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 133 | registry: r, |
| 134 | Meid: trans.Meid, |
| 135 | SubReqMsg: subReqMsg, |
| 136 | valid: true, |
| 137 | RetryFromXapp: false, |
| 138 | SubRespRcvd: false, |
| 139 | DeleteFromDb: false, |
| 140 | NoRespToXapp: false, |
| 141 | DoNotWaitSubResp: false, |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 142 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 143 | subs.ReqId.Id = 123 |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 144 | subs.ReqId.InstanceId = subId |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 145 | if resetTestFlag == true { |
| 146 | subs.DoNotWaitSubResp = true |
| 147 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 148 | |
| 149 | if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false { |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 150 | r.subIds = append(r.subIds, subs.ReqId.InstanceId) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 151 | return nil, fmt.Errorf("Registry: Endpoint existing already in subscription") |
| 152 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 153 | return subs, nil |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 154 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 155 | return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids") |
| 156 | } |
| 157 | |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 158 | func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, bool) { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 159 | |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 160 | for _, subs := range r.register { |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 161 | if subs.IsMergeable(trans, subReqMsg) { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 162 | |
| 163 | // |
| 164 | // check if there has been race conditions |
| 165 | // |
| 166 | subs.mutex.Lock() |
| 167 | //subs has been set to invalid |
| 168 | if subs.valid == false { |
| 169 | subs.mutex.Unlock() |
| 170 | continue |
| 171 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 172 | // If size is zero, entry is to be deleted |
| 173 | if subs.EpList.Size() == 0 { |
| 174 | subs.mutex.Unlock() |
| 175 | continue |
| 176 | } |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 177 | // Try to add to endpointlist. Adding fails if endpoint is already in the list |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 178 | if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false { |
| 179 | subs.mutex.Unlock() |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 180 | xapp.Logger.Debug("Registry: Subs with requesting endpoint found. %s for %s", subs.String(), trans.String()) |
| 181 | return subs, true |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 182 | } |
| 183 | subs.mutex.Unlock() |
| 184 | |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 185 | xapp.Logger.Debug("Registry: Mergeable subs found. %s for %s", subs.String(), trans.String()) |
| 186 | return subs, false |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 189 | return nil, false |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 192 | func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool, c *Control) (*Subscription, error) { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 193 | var err error |
| 194 | var newAlloc bool |
| 195 | r.mutex.Lock() |
| 196 | defer r.mutex.Unlock() |
| 197 | |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 198 | // |
| 199 | // Check validity of subscription action types |
| 200 | // |
| 201 | actionType, err := r.CheckActionTypes(subReqMsg) |
| 202 | if err != nil { |
| 203 | xapp.Logger.Debug("CREATE %s", err) |
| 204 | return nil, err |
| 205 | } |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 206 | |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 207 | // |
| 208 | // Find possible existing Policy subscription |
| 209 | // |
| 210 | if actionType == e2ap.E2AP_ActionTypePolicy { |
Juha Hyttinen | 47942b4 | 2020-02-27 10:41:43 +0200 | [diff] [blame] | 211 | if subs, ok := r.register[trans.GetSubId()]; ok { |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 212 | xapp.Logger.Debug("CREATE %s. Existing subscription for Policy found.", subs.String()) |
Anssi Mannila | cc7d9e0 | 2020-04-08 12:58:53 +0300 | [diff] [blame] | 213 | // Update message data to subscription |
| 214 | subs.SubReqMsg = subReqMsg |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 215 | subs.SetCachedResponse(nil, true) |
| 216 | return subs, nil |
| 217 | } |
| 218 | } |
| 219 | |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 220 | subs, endPointFound := r.findExistingSubs(trans, subReqMsg) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 221 | if subs == nil { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 222 | if subs, err = r.allocateSubs(trans, subReqMsg, resetTestFlag); err != nil { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 223 | return nil, err |
| 224 | } |
| 225 | newAlloc = true |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 226 | } else if endPointFound == true { |
| 227 | // Requesting endpoint is already present in existing subscription. This can happen if xApp is restarted. |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 228 | subs.RetryFromXapp = true |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 229 | xapp.Logger.Debug("CREATE: subscription already exists. %s", subs.String()) |
Anssi Mannila | 2f26fb2 | 2020-12-07 08:32:13 +0200 | [diff] [blame] | 230 | return subs, nil |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // |
| 234 | // Add to subscription |
| 235 | // |
| 236 | subs.mutex.Lock() |
| 237 | defer subs.mutex.Unlock() |
| 238 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 239 | epamount := subs.EpList.Size() |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 240 | xapp.Logger.Info("AssignToSubscription subs.EpList.Size() = %v", subs.EpList.Size()) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 241 | |
| 242 | r.mutex.Unlock() |
| 243 | // |
| 244 | // Subscription route updates |
| 245 | // |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 246 | if epamount == 1 { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 247 | err = r.RouteCreate(subs, c) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 248 | } else { |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 249 | err = r.RouteCreateUpdate(subs, c) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 250 | } |
| 251 | r.mutex.Lock() |
| 252 | |
| 253 | if err != nil { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 254 | if newAlloc { |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 255 | r.subIds = append(r.subIds, subs.ReqId.InstanceId) |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 256 | } |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 257 | // Delete already added endpoint for the request |
| 258 | subs.EpList.DelEndpoint(trans.GetEndpoint()) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 259 | return nil, err |
| 260 | } |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 261 | |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 262 | if newAlloc { |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 263 | r.register[subs.ReqId.InstanceId] = subs |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 264 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 265 | xapp.Logger.Debug("CREATE %s", subs.String()) |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 266 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
| 267 | return subs, nil |
| 268 | } |
| 269 | |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 270 | func (r *Registry) RouteCreate(subs *Subscription, c *Control) error { |
| 271 | subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)} |
| 272 | err := r.rtmgrClient.SubscriptionRequestCreate(subRouteAction) |
| 273 | if err != nil { |
| 274 | c.UpdateCounter(cRouteCreateFail) |
| 275 | } |
| 276 | return err |
| 277 | } |
| 278 | |
| 279 | func (r *Registry) RouteCreateUpdate(subs *Subscription, c *Control) error { |
| 280 | subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)} |
| 281 | err := r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction) |
| 282 | if err != nil { |
| 283 | c.UpdateCounter(cRouteCreateUpdateFail) |
| 284 | return err |
| 285 | } |
| 286 | c.UpdateCounter(cMergedSubscriptions) |
| 287 | return err |
| 288 | } |
| 289 | |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 290 | func (r *Registry) CheckActionTypes(subReqMsg *e2ap.E2APSubscriptionRequest) (uint64, error) { |
| 291 | var reportFound bool = false |
| 292 | var policyFound bool = false |
Anssi Mannila | f0d9526 | 2020-08-17 13:00:20 +0300 | [diff] [blame] | 293 | var insertFound bool = false |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 294 | |
| 295 | for _, acts := range subReqMsg.ActionSetups { |
| 296 | if acts.ActionType == e2ap.E2AP_ActionTypeReport { |
| 297 | reportFound = true |
| 298 | } |
| 299 | if acts.ActionType == e2ap.E2AP_ActionTypePolicy { |
| 300 | policyFound = true |
| 301 | } |
Anssi Mannila | f0d9526 | 2020-08-17 13:00:20 +0300 | [diff] [blame] | 302 | if acts.ActionType == e2ap.E2AP_ActionTypeInsert { |
| 303 | insertFound = true |
| 304 | } |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 305 | } |
Anssi Mannila | f0d9526 | 2020-08-17 13:00:20 +0300 | [diff] [blame] | 306 | if reportFound == true && policyFound == true || reportFound == true && insertFound == true || policyFound == true && insertFound == true { |
| 307 | return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Different action types (Report, Policy or Insert) in same RICactions-ToBeSetup-List") |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 308 | } |
| 309 | if reportFound == true { |
| 310 | return e2ap.E2AP_ActionTypeReport, nil |
| 311 | } |
| 312 | if policyFound == true { |
| 313 | return e2ap.E2AP_ActionTypePolicy, nil |
| 314 | } |
Anssi Mannila | f0d9526 | 2020-08-17 13:00:20 +0300 | [diff] [blame] | 315 | if insertFound == true { |
| 316 | return e2ap.E2AP_ActionTypeInsert, nil |
| 317 | } |
Anssi Mannila | 9bcb0a4 | 2020-02-11 11:30:44 +0200 | [diff] [blame] | 318 | return e2ap.E2AP_ActionTypeInvalid, fmt.Errorf("Invalid action type in RICactions-ToBeSetup-List") |
| 319 | } |
| 320 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 321 | // TODO: Works with concurrent calls, but check if can be improved |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 322 | func (r *Registry) RemoveFromSubscription(subs *Subscription, trans *TransactionXapp, waitRouteClean time.Duration, c *Control) error { |
Juha Hyttinen | 3944a22 | 2020-01-24 11:51:46 +0200 | [diff] [blame] | 323 | |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 324 | r.mutex.Lock() |
| 325 | defer r.mutex.Unlock() |
| 326 | subs.mutex.Lock() |
| 327 | defer subs.mutex.Unlock() |
| 328 | |
| 329 | delStatus := subs.EpList.DelEndpoint(trans.GetEndpoint()) |
| 330 | epamount := subs.EpList.Size() |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 331 | subId := subs.ReqId.InstanceId |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 332 | |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 333 | if delStatus == false { |
| 334 | return nil |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 335 | } |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 336 | |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 337 | go func() { |
| 338 | if waitRouteClean > 0 { |
| 339 | time.Sleep(waitRouteClean) |
Juha Hyttinen | 83ada00 | 2020-01-30 10:36:33 +0200 | [diff] [blame] | 340 | } |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 341 | |
| 342 | subs.mutex.Lock() |
| 343 | defer subs.mutex.Unlock() |
| 344 | xapp.Logger.Info("CLEAN %s", subs.String()) |
| 345 | |
| 346 | if epamount == 0 { |
| 347 | // |
| 348 | // Subscription route delete |
| 349 | // |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 350 | r.RouteDelete(subs, trans, c) |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 351 | |
| 352 | // |
| 353 | // Subscription release |
| 354 | // |
| 355 | r.mutex.Lock() |
| 356 | defer r.mutex.Unlock() |
| 357 | |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 358 | if _, ok := r.register[subId]; ok { |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 359 | xapp.Logger.Debug("RELEASE %s", subs.String()) |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 360 | delete(r.register, subId) |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 361 | xapp.Logger.Debug("Registry: substable=%v", r.register) |
| 362 | } |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 363 | r.subIds = append(r.subIds, subId) |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 364 | } else if subs.EpList.Size() > 0 { |
| 365 | // |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 366 | // Subscription route update |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 367 | // |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 368 | r.RouteDeleteUpdate(subs, c) |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 369 | } |
Juha Hyttinen | f44377d | 2020-02-12 10:18:40 +0200 | [diff] [blame] | 370 | }() |
Juha Hyttinen | 12d31af | 2020-01-22 12:59:01 +0200 | [diff] [blame] | 371 | |
| 372 | return nil |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 373 | } |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 374 | |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 375 | func (r *Registry) RouteDelete(subs *Subscription, trans *TransactionXapp, c *Control) { |
| 376 | tmpList := xapp.RmrEndpointList{} |
| 377 | tmpList.AddEndpoint(trans.GetEndpoint()) |
| 378 | subRouteAction := SubRouteInfo{tmpList, uint16(subs.ReqId.InstanceId)} |
| 379 | if err := r.rtmgrClient.SubscriptionRequestDelete(subRouteAction); err != nil { |
| 380 | c.UpdateCounter(cRouteDeleteFail) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | func (r *Registry) RouteDeleteUpdate(subs *Subscription, c *Control) { |
| 385 | subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)} |
| 386 | if err := r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction); err != nil { |
| 387 | c.UpdateCounter(cRouteDeleteUpdateFail) |
| 388 | } |
| 389 | } |
| 390 | |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 391 | func (r *Registry) UpdateSubscriptionToDb(subs *Subscription, c *Control) { |
| 392 | r.mutex.Lock() |
| 393 | defer r.mutex.Unlock() |
| 394 | subs.mutex.Lock() |
| 395 | defer subs.mutex.Unlock() |
| 396 | |
| 397 | epamount := subs.EpList.Size() |
| 398 | if epamount == 0 { |
| 399 | if _, ok := r.register[subs.ReqId.InstanceId]; ok { |
| 400 | // Not merged subscription is being deleted |
| 401 | c.RemoveSubscriptionFromDb(subs) |
| 402 | |
| 403 | } |
| 404 | } else if subs.EpList.Size() > 0 { |
| 405 | // Endpoint of merged subscription is being deleted |
| 406 | c.WriteSubscriptionToDb(subs) |
Anssi Mannila | 4abf180 | 2021-01-28 13:06:46 +0200 | [diff] [blame] | 407 | c.UpdateCounter(cUnmergedSubscriptions) |
Anssi Mannila | c92b421 | 2020-12-07 14:59:34 +0200 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 411 | func (r *Registry) GetSubscription(subId uint32) *Subscription { |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 412 | r.mutex.Lock() |
| 413 | defer r.mutex.Unlock() |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 414 | if _, ok := r.register[subId]; ok { |
| 415 | return r.register[subId] |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 416 | } |
Juha Hyttinen | 47b842b | 2020-01-08 13:01:52 +0200 | [diff] [blame] | 417 | return nil |
Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 420 | func (r *Registry) GetSubscriptionFirstMatch(subIds []uint32) (*Subscription, error) { |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 421 | r.mutex.Lock() |
| 422 | defer r.mutex.Unlock() |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 423 | for _, subId := range subIds { |
| 424 | if _, ok := r.register[subId]; ok { |
| 425 | return r.register[subId], nil |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 426 | } |
| 427 | } |
Juha Hyttinen | aada645 | 2020-04-07 08:47:58 +0300 | [diff] [blame] | 428 | return nil, fmt.Errorf("No valid subscription found with subIds %v", subIds) |
Juha Hyttinen | 422d018 | 2020-01-17 13:37:05 +0200 | [diff] [blame] | 429 | } |