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