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