+ delete(r.restSubscriptions, *restSubId)
+ xapp.Logger.Debug("Registry: Deleted REST subscription successfully. restSubId=%v, subscriptionCount=%v", *restSubId, len(r.restSubscriptions))
+}
+
+func (r *Registry) GetRESTSubscription(restSubId string, IsDelReqOngoing bool) (*RESTSubscription, error) {
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+ if restSubscription, ok := r.restSubscriptions[restSubId]; ok {
+ // Subscription deletion is not allowed if prosessing subscription request in not ready
+ if restSubscription.SubDelReqOngoing == false && restSubscription.SubReqOngoing == false {
+ if IsDelReqOngoing == true {
+ restSubscription.SubDelReqOngoing = true
+ }
+ r.restSubscriptions[restSubId] = restSubscription
+ return restSubscription, nil
+ } else {
+ 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)
+ }
+ }
+ return nil, fmt.Errorf("Registry: No valid subscription found with restSubId=%v", restSubId)
+}
+
+func (r *Registry) QueryHandler() (models.SubscriptionList, error) {
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+
+ resp := models.SubscriptionList{}
+ for _, subs := range r.register {
+ subs.mutex.Lock()
+ resp = append(resp, &models.SubscriptionData{SubscriptionID: int64(subs.ReqId.InstanceId), Meid: subs.Meid.RanName, ClientEndpoint: subs.EpList.StringList()})
+ subs.mutex.Unlock()
+ }
+ return resp, nil
+}
+
+func (r *Registry) allocateSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool, rmrRoutecreated bool) (*Subscription, error) {
+ if len(r.subIds) > 0 {
+ subId := r.subIds[0]
+ r.subIds = r.subIds[1:]
+ if _, ok := r.register[subId]; ok == true {
+ r.subIds = append(r.subIds, subId)
+ return nil, fmt.Errorf("Registry: Failed to reserve subscription exists")
+ }
+ subs := &Subscription{
+ registry: r,
+ Meid: trans.Meid,
+ RMRRouteCreated: rmrRoutecreated,
+ SubReqMsg: subReqMsg,
+ OngoingReqCount: 0,
+ OngoingDelCount: 0,
+ valid: true,
+ PolicyUpdate: false,
+ RetryFromXapp: false,
+ SubRespRcvd: false,
+ DeleteFromDb: false,
+ NoRespToXapp: false,
+ DoNotWaitSubResp: false,
+ }
+ subs.ReqId.Id = subReqMsg.RequestId.Id
+ subs.ReqId.InstanceId = subId
+ r.SetResetTestFlag(resetTestFlag, subs)
+
+ if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
+ r.subIds = append(r.subIds, subs.ReqId.InstanceId)
+ return nil, fmt.Errorf("Registry: Endpoint existing already in subscription")
+ }
+ return subs, nil
+ }
+ return nil, fmt.Errorf("Registry: Failed to reserve subscription no free ids")
+}
+
+func (r *Registry) findExistingSubs(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest) (*Subscription, bool) {
+
+ for _, subs := range r.register {
+ if subs.IsMergeable(trans, subReqMsg) {
+
+ //
+ // check if there has been race conditions
+ //
+ subs.mutex.Lock()
+ //subs has been set to invalid
+ if subs.valid == false {
+ subs.mutex.Unlock()
+ continue
+ }
+ // If size is zero, entry is to be deleted
+ if subs.EpList.Size() == 0 {
+ subs.mutex.Unlock()
+ continue
+ }
+ // Try to add to endpointlist. Adding fails if endpoint is already in the list
+ if subs.EpList.AddEndpoint(trans.GetEndpoint()) == false {
+ subs.mutex.Unlock()
+ xapp.Logger.Debug("Registry: Subs with requesting endpoint found. %s for %s", subs.String(), trans.String())
+ return subs, true
+ }
+ subs.mutex.Unlock()
+
+ xapp.Logger.Debug("Registry: Mergeable subs found. %s for %s", subs.String(), trans.String())
+ return subs, false
+ }
+ }
+ return nil, false
+}
+
+func (r *Registry) AssignToSubscription(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, resetTestFlag bool, c *Control, createRMRRoute bool) (*Subscription, ErrorInfo, error) {
+ var err error
+ var newAlloc bool
+ errorInfo := ErrorInfo{}
+ r.mutex.Lock()
+ defer r.mutex.Unlock()
+
+ //
+ // Check validity of subscription action types
+ //
+ actionType, err := r.CheckActionTypes(subReqMsg)
+ if err != nil {
+ xapp.Logger.Debug("CREATE %s", err)
+ err = fmt.Errorf("E2 content validation failed")
+ return nil, errorInfo, err
+ }
+
+ //
+ // Find possible existing Policy subscription
+ //
+ if actionType == e2ap.E2AP_ActionTypePolicy {
+ if subs, ok := r.register[trans.GetSubId()]; ok {
+ xapp.Logger.Debug("CREATE %s. Existing subscription for Policy found.", subs.String())
+ // Update message data to subscription
+ subs.SubReqMsg = subReqMsg
+ subs.PolicyUpdate = true
+ subs.SetCachedResponse(nil, true)
+ r.SetResetTestFlag(resetTestFlag, subs)
+ return subs, errorInfo, nil
+ }
+ }
+
+ subs, endPointFound := r.findExistingSubs(trans, subReqMsg)
+ if subs == nil {
+ if subs, err = r.allocateSubs(trans, subReqMsg, resetTestFlag, createRMRRoute); err != nil {
+ xapp.Logger.Error("%s", err.Error())
+ err = fmt.Errorf("subscription not allocated")
+ return nil, errorInfo, err
+ }
+ newAlloc = true
+ } else if endPointFound == true {
+ // Requesting endpoint is already present in existing subscription. This can happen if xApp is restarted.
+ subs.RetryFromXapp = true
+ xapp.Logger.Debug("CREATE subReqMsg.InstanceId=%v. Same subscription %s already exists.", subReqMsg.InstanceId, subs.String())
+ c.UpdateCounter(cDuplicateE2SubReq)
+ return subs, errorInfo, nil
+ }
+
+ //
+ // Add to subscription
+ //
+ subs.mutex.Lock()
+ defer subs.mutex.Unlock()
+
+ epamount := subs.EpList.Size()
+ xapp.Logger.Debug("AssignToSubscription subs.EpList.Size()=%v", subs.EpList.Size())
+
+ r.mutex.Unlock()
+ //
+ // Subscription route updates
+ //
+ if createRMRRoute == true {
+ if epamount == 1 {
+ errorInfo, err = r.RouteCreate(subs, c)
+ } else {
+ errorInfo, err = r.RouteCreateUpdate(subs, c)
+ }
+ } else {
+ xapp.Logger.Debug("RMR route not created: createRMRRoute=%v", createRMRRoute)
+ }
+ r.mutex.Lock()
+
+ if err != nil {
+ if newAlloc {
+ r.subIds = append(r.subIds, subs.ReqId.InstanceId)
+ }
+ // Delete already added endpoint for the request
+ subs.EpList.DelEndpoint(trans.GetEndpoint())
+ return nil, errorInfo, err
+ }
+
+ if newAlloc {
+ r.register[subs.ReqId.InstanceId] = subs
+ }
+ xapp.Logger.Debug("CREATE %s", subs.String())
+ xapp.Logger.Debug("Registry: substable=%v", r.register)
+ return subs, errorInfo, nil
+}
+
+func (r *Registry) RouteCreate(subs *Subscription, c *Control) (ErrorInfo, error) {
+ errorInfo := ErrorInfo{}
+ subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)}
+ err := r.rtmgrClient.SubscriptionRequestCreate(subRouteAction)
+ if err != nil {
+ if strings.Contains(err.Error(), "status 400") {
+ errorInfo.TimeoutType = models.SubscriptionInstanceTimeoutTypeRTMGRTimeout
+ } else {
+ errorInfo.ErrorSource = models.SubscriptionInstanceErrorSourceRTMGR
+ }
+ errorInfo.ErrorCause = err.Error()
+ c.UpdateCounter(cRouteCreateFail)
+ xapp.Logger.Error("%s", err.Error())
+ err = fmt.Errorf("RTMGR route create failure")
+ }
+ return errorInfo, err
+}
+
+func (r *Registry) RouteCreateUpdate(subs *Subscription, c *Control) (ErrorInfo, error) {
+ errorInfo := ErrorInfo{}
+ subRouteAction := SubRouteInfo{subs.EpList, uint16(subs.ReqId.InstanceId)}
+ err := r.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+ if err != nil {
+ if strings.Contains(err.Error(), "status 400") {
+ errorInfo.TimeoutType = models.SubscriptionInstanceTimeoutTypeRTMGRTimeout