+
+ return restSubscription, restSubId, nil
+}
+
+//-------------------------------------------------------------------
+//
+//-------------------------------------------------------------------
+func (c *Control) RESTSubscriptionHandler(params interface{}) (*models.SubscriptionResponse, int) {
+
+ c.CntRecvMsg++
+ c.UpdateCounter(cRestSubReqFromXapp)
+
+ subResp := models.SubscriptionResponse{}
+ p := params.(*models.SubscriptionParams)
+
+ if c.LoggerLevel > 2 {
+ c.PrintRESTSubscriptionRequest(p)
+ }
+
+ if c.e2IfState.IsE2ConnectionUp(p.Meid) == false || c.e2IfState.IsE2ConnectionUnderReset(p.Meid) == true {
+ if c.e2IfState.IsE2ConnectionUp(p.Meid) == false {
+ xapp.Logger.Error("No E2 connection for ranName %v", *p.Meid)
+ } else if c.e2IfState.IsE2ConnectionUnderReset(p.Meid) == true {
+ xapp.Logger.Error("E2 Node for ranName %v UNDER RESET", *p.Meid)
+ }
+ c.UpdateCounter(cRestReqRejDueE2Down)
+ return nil, common.SubscribeServiceUnavailableCode
+ }
+
+ if p.ClientEndpoint == nil {
+ err := fmt.Errorf("ClientEndpoint == nil")
+ xapp.Logger.Error("%v", err)
+ c.UpdateCounter(cRestSubFailToXapp)
+ return nil, common.SubscribeBadRequestCode
+ }
+
+ e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(p)
+ if err != nil {
+ xapp.Logger.Error("%s", err)
+ c.UpdateCounter(cRestSubFailToXapp)
+ return nil, common.SubscribeBadRequestCode
+ }
+ _, xAppRmrEndpoint, err := ConstructEndpointAddresses(*p.ClientEndpoint)
+ if err != nil {
+ xapp.Logger.Error("%s", err.Error())
+ c.UpdateCounter(cRestSubFailToXapp)
+ return nil, common.SubscribeBadRequestCode
+ }
+
+ md5sum, err := CalculateRequestMd5sum(params)
+ if err != nil {
+ xapp.Logger.Error("Failed to generate md5sum from incoming request - %s", err.Error())
+ }
+
+ restSubscription, restSubId, err := c.GetOrCreateRestSubscription(p, md5sum, xAppRmrEndpoint, p.ClientEndpoint.Host)
+ if err != nil {
+ xapp.Logger.Error("Subscription with id in REST request does not exist")
+ return nil, common.SubscribeNotFoundCode
+ }
+
+ subResp.SubscriptionID = &restSubId
+ subReqList := e2ap.SubscriptionRequestList{}
+ err = c.e2ap.FillSubscriptionReqMsgs(params, &subReqList, restSubscription)
+ if err != nil {
+ xapp.Logger.Error("%s", err.Error())
+ c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum)
+ c.registry.DeleteRESTSubscription(&restSubId)
+ c.UpdateCounter(cRestSubFailToXapp)
+ return nil, common.SubscribeBadRequestCode
+ }
+
+ duplicate := c.restDuplicateCtrl.IsDuplicateToOngoingTransaction(restSubId, md5sum)
+ if duplicate {
+ err := fmt.Errorf("Retransmission blocker direct ACK for request of restSubsId %s restSubId MD5sum %s as retransmission", restSubId, md5sum)
+ xapp.Logger.Debug("%s", err)
+ c.registry.DeleteRESTSubscription(&restSubId)
+ c.UpdateCounter(cRestSubRespToXapp)
+ return &subResp, common.SubscribeCreatedCode
+ }
+
+ c.WriteRESTSubscriptionToDb(restSubId, restSubscription)
+ go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId, xAppRmrEndpoint, md5sum, e2SubscriptionDirectives)
+
+ c.UpdateCounter(cRestSubRespToXapp)
+ return &subResp, common.SubscribeCreatedCode
+}
+
+//-------------------------------------------------------------------
+//
+//-------------------------------------------------------------------
+func (c *Control) GetE2SubscriptionDirectives(p *models.SubscriptionParams) (*E2SubscriptionDirectives, error) {
+
+ e2SubscriptionDirectives := &E2SubscriptionDirectives{}
+ if p == nil || p.E2SubscriptionDirectives == nil {
+ e2SubscriptionDirectives.E2TimeoutTimerValue = e2tSubReqTimeout
+ e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
+ e2SubscriptionDirectives.CreateRMRRoute = true
+ xapp.Logger.Debug("p == nil || p.E2SubscriptionDirectives == nil. Using default values for E2TimeoutTimerValue = %v and E2RetryCount = %v RMRRoutingNeeded = true", e2tSubReqTimeout, e2tMaxSubReqTryCount)
+ } else {
+ if p.E2SubscriptionDirectives.E2TimeoutTimerValue >= 1 && p.E2SubscriptionDirectives.E2TimeoutTimerValue <= 10 {
+ e2SubscriptionDirectives.E2TimeoutTimerValue = time.Duration(p.E2SubscriptionDirectives.E2TimeoutTimerValue) * 1000000000 // Duration type cast returns nano seconds
+ } else {
+ return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2TimeoutTimerValue out of range (1-10 seconds): %v", p.E2SubscriptionDirectives.E2TimeoutTimerValue)
+ }
+ if p.E2SubscriptionDirectives.E2RetryCount == nil {
+ xapp.Logger.Error("p.E2SubscriptionDirectives.E2RetryCount == nil. Using default value")
+ e2SubscriptionDirectives.E2MaxTryCount = int64(e2tMaxSubReqTryCount)
+ } else {
+ if *p.E2SubscriptionDirectives.E2RetryCount >= 0 && *p.E2SubscriptionDirectives.E2RetryCount <= 10 {
+ e2SubscriptionDirectives.E2MaxTryCount = *p.E2SubscriptionDirectives.E2RetryCount + 1 // E2MaxTryCount = First sending plus two retries
+ } else {
+ return nil, fmt.Errorf("p.E2SubscriptionDirectives.E2RetryCount out of range (0-10): %v", *p.E2SubscriptionDirectives.E2RetryCount)
+ }
+ }
+ e2SubscriptionDirectives.CreateRMRRoute = p.E2SubscriptionDirectives.RMRRoutingNeeded
+ }
+ xapp.Logger.Debug("e2SubscriptionDirectives.E2TimeoutTimerValue: %v", e2SubscriptionDirectives.E2TimeoutTimerValue)
+ xapp.Logger.Debug("e2SubscriptionDirectives.E2MaxTryCount: %v", e2SubscriptionDirectives.E2MaxTryCount)
+ xapp.Logger.Debug("e2SubscriptionDirectives.CreateRMRRoute: %v", e2SubscriptionDirectives.CreateRMRRoute)
+ return e2SubscriptionDirectives, nil
+}
+
+//-------------------------------------------------------------------
+//
+//-------------------------------------------------------------------
+
+func (c *Control) processSubscriptionRequests(restSubscription *RESTSubscription, subReqList *e2ap.SubscriptionRequestList,
+ clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string, md5sum string, e2SubscriptionDirectives *E2SubscriptionDirectives) {
+
+ c.SubscriptionProcessingStartDelay()
+ xapp.Logger.Debug("E2 SubscriptionRequest count = %v ", len(subReqList.E2APSubscriptionRequests))
+
+ var xAppEventInstanceID int64
+ var e2EventInstanceID int64
+ errorInfo := &ErrorInfo{}
+
+ defer c.restDuplicateCtrl.SetMd5sumFromLastOkRequest(*restSubId, md5sum)
+
+ for index := 0; index < len(subReqList.E2APSubscriptionRequests); index++ {
+ subReqMsg := subReqList.E2APSubscriptionRequests[index]
+ xAppEventInstanceID = (int64)(subReqMsg.RequestId.Id)
+
+ trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(xAppRmrEndpoint), *restSubId, subReqMsg.RequestId, &xapp.RMRMeid{RanName: *meid})
+ if trans == nil {
+ // Send notification to xApp that prosessing of a Subscription Request has failed.
+ err := fmt.Errorf("Tracking failure")
+ errorInfo.ErrorCause = err.Error()
+ c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
+ continue
+ }
+
+ xapp.Logger.Debug("Handle SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
+
+ subRespMsg, errorInfo, err := c.handleSubscriptionRequest(trans, &subReqMsg, meid, *restSubId, e2SubscriptionDirectives)
+
+ xapp.Logger.Debug("Handled SubscriptionRequest index=%v, %s", index, idstring(nil, trans))
+ trans.Release()
+
+ if err != nil {
+ if err.Error() == "TEST: restart event received" {
+ // This is just for UT cases. Stop here subscription processing
+ return
+ }
+ c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
+ } else {
+ e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
+ restSubscription.AddMd5Sum(md5sum)
+ xapp.Logger.Debug("SubscriptionRequest index=%v processed successfullyfor %s. endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
+ index, *restSubId, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
+ c.sendSuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, e2EventInstanceID, clientEndpoint, trans, errorInfo)
+ }
+ }
+}
+
+//-------------------------------------------------------------------
+//
+//------------------------------------------------------------------
+func (c *Control) SubscriptionProcessingStartDelay() {
+ if c.UTTesting == true {
+ // This is temporary fix for the UT problem that notification arrives before subscription response
+ // Correct fix would be to allow notification come before response and process it correctly
+ xapp.Logger.Debug("Setting 50 ms delay before starting processing Subscriptions")
+ <-time.After(time.Millisecond * 50)
+ xapp.Logger.Debug("Continuing after delay")
+ }
+}
+
+//-------------------------------------------------------------------
+//
+//------------------------------------------------------------------
+func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e2ap.E2APSubscriptionRequest, meid *string,
+ restSubId string, e2SubscriptionDirectives *E2SubscriptionDirectives) (*e2ap.E2APSubscriptionResponse, *ErrorInfo, error) {
+
+ errorInfo := ErrorInfo{}
+
+ err := c.tracker.Track(trans)
+ if err != nil {
+ xapp.Logger.Error("XAPP-SubReq Tracking error: %s", idstring(err, trans))
+ errorInfo.ErrorCause = err.Error()
+ err = fmt.Errorf("Tracking failure")
+ return nil, &errorInfo, err
+ }
+
+ subs, errorInfo, err := c.registry.AssignToSubscription(trans, subReqMsg, c.ResetTestFlag, c, e2SubscriptionDirectives.CreateRMRRoute)
+ if err != nil {
+ xapp.Logger.Error("XAPP-SubReq Assign error: %s", idstring(err, trans))
+ return nil, &errorInfo, err
+ }
+
+ //
+ // Wake subs request
+ //
+ subs.OngoingReqCount++
+ go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, 0)
+ event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
+ subs.OngoingReqCount--
+
+ err = nil
+ if event != nil {
+ switch themsg := event.(type) {
+ case *e2ap.E2APSubscriptionResponse:
+ trans.Release()
+ if c.e2IfState.IsE2ConnectionUp(meid) == true {
+ errorInfo = c.e2ap.CheckActionNotAdmittedList(xapp.RIC_SUB_RESP, themsg.ActionNotAdmittedList, c)
+ return themsg, &errorInfo, nil
+ } else {
+ c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
+ c.RemoveSubscriptionFromDb(subs)
+ err = fmt.Errorf("E2 interface down")
+ errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
+ }
+ case *e2ap.E2APSubscriptionFailure:
+ err = fmt.Errorf("RICSubscriptionFailure. E2NodeCause: (Cause:%v, Value %v)", themsg.Cause.Content, themsg.Cause.Value)
+ errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
+ case *PackSubscriptionRequestErrortEvent:
+ err = fmt.Errorf("E2 RICSubscriptionRequest pack failure")
+ errorInfo = themsg.ErrorInfo
+ case *SDLWriteErrortEvent:
+ err = fmt.Errorf("SDL write failure")
+ errorInfo = themsg.ErrorInfo
+ case *SubmgrRestartTestEvent:
+ err = fmt.Errorf("TEST: restart event received")
+ xapp.Logger.Debug("%s", err)
+ return nil, &errorInfo, err
+ default:
+ err = fmt.Errorf("Unexpected E2 subscription response received")
+ errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
+ break
+ }
+ } else {
+ // Timer expiry
+ err = fmt.Errorf("E2 RICSubscriptionResponse timeout")
+ errorInfo.SetInfo(err.Error(), "", models.SubscriptionInstanceTimeoutTypeE2Timeout)
+ if subs.PolicyUpdate == true {
+ return nil, &errorInfo, err
+ }
+ }
+
+ xapp.Logger.Error("XAPP-SubReq E2 subscription failed: %s", idstring(err, trans, subs))
+ // If policy type subscription fails we cannot remove it only internally. Once subscription has been created
+ // successfully, it must be deleted on both sides.
+ if subs.PolicyUpdate == false {
+ c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
+ }
+
+ return nil, &errorInfo, err
+}
+
+//-------------------------------------------------------------------
+//
+//-------------------------------------------------------------------
+func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, err error,
+ clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
+
+ // Send notification to xApp that prosessing of a Subscription Request has failed.
+ e2EventInstanceID := (int64)(0)
+ if errorInfo.ErrorSource == "" {
+ // Submgr is default source of error
+ errorInfo.ErrorSource = models.SubscriptionInstanceErrorSourceSUBMGR
+ }
+ resp := &models.SubscriptionResponse{
+ SubscriptionID: restSubId,
+ SubscriptionInstances: []*models.SubscriptionInstance{
+ &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
+ ErrorCause: errorInfo.ErrorCause,
+ ErrorSource: errorInfo.ErrorSource,
+ TimeoutType: errorInfo.TimeoutType,
+ XappEventInstanceID: &xAppEventInstanceID},
+ },
+ }
+ // Mark REST subscription request processed.
+ restSubscription.SetProcessed(err)
+ c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
+ if trans != nil {
+ xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
+ errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
+ } else {
+ xapp.Logger.Debug("Sending unsuccessful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
+ errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
+ }
+
+ c.UpdateCounter(cRestSubFailNotifToXapp)
+ err = xapp.Subscription.Notify(resp, *clientEndpoint)
+ if err != nil {
+ xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
+ }
+
+ // E2 is down. Delete completely processed request safely now
+ if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
+ c.registry.DeleteRESTSubscription(restSubId)
+ c.RemoveRESTSubscriptionFromDb(*restSubId)
+ }
+}
+
+//-------------------------------------------------------------------
+//
+//-------------------------------------------------------------------
+func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubscription *RESTSubscription, xAppEventInstanceID int64, e2EventInstanceID int64,
+ clientEndpoint *models.SubscriptionParamsClientEndpoint, trans *TransactionXapp, errorInfo *ErrorInfo) {
+
+ // Store successfully processed InstanceId for deletion
+ restSubscription.AddE2InstanceId((uint32)(e2EventInstanceID))
+ restSubscription.AddXappIdToE2Id(xAppEventInstanceID, e2EventInstanceID)
+
+ // Send notification to xApp that a Subscription Request has been processed.
+ resp := &models.SubscriptionResponse{
+ SubscriptionID: restSubId,
+ SubscriptionInstances: []*models.SubscriptionInstance{
+ &models.SubscriptionInstance{E2EventInstanceID: &e2EventInstanceID,
+ ErrorCause: errorInfo.ErrorCause,
+ ErrorSource: errorInfo.ErrorSource,
+ XappEventInstanceID: &xAppEventInstanceID},
+ },
+ }
+ // Mark REST subscription request processesd.
+ restSubscription.SetProcessed(nil)
+ c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
+ xapp.Logger.Debug("Sending successful REST notification: ErrorCause:%s, ErrorSource:%s, TimeoutType:%s, to Endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
+ errorInfo.ErrorCause, errorInfo.ErrorSource, errorInfo.TimeoutType, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
+ c.UpdateCounter(cRestSubNotifToXapp)
+ err := xapp.Subscription.Notify(resp, *clientEndpoint)
+ if err != nil {
+ xapp.Logger.Error("xapp.Subscription.Notify failed %s", err.Error())
+ }
+
+ // E2 is down. Delete completely processed request safely now
+ if c.e2IfState.IsE2ConnectionUp(&restSubscription.Meid) == false && restSubscription.SubReqOngoing == false {
+ c.registry.DeleteRESTSubscription(restSubId)
+ c.RemoveRESTSubscriptionFromDb(*restSubId)
+ }
+}
+
+//-------------------------------------------------------------------
+//
+//-------------------------------------------------------------------
+func (c *Control) RESTSubscriptionDeleteHandler(restSubId string) int {
+
+ c.CntRecvMsg++
+ c.UpdateCounter(cRestSubDelReqFromXapp)
+
+ xapp.Logger.Debug("SubscriptionDeleteRequest from XAPP")
+
+ restSubscription, err := c.registry.GetRESTSubscription(restSubId, true)
+ if err != nil {
+ xapp.Logger.Error("%s", err.Error())
+ if restSubscription == nil {
+ // Subscription was not found
+ c.UpdateCounter(cRestSubDelRespToXapp)
+ return common.UnsubscribeNoContentCode
+ } else {
+ if restSubscription.SubReqOngoing == true {
+ err := fmt.Errorf("Handling of the REST Subscription Request still ongoing %s", restSubId)
+ xapp.Logger.Error("%s", err.Error())
+ c.UpdateCounter(cRestSubDelFailToXapp)
+ return common.UnsubscribeBadRequestCode
+ } else if restSubscription.SubDelReqOngoing == true {
+ // Previous request for same restSubId still ongoing
+ c.UpdateCounter(cRestSubDelRespToXapp)
+ return common.UnsubscribeNoContentCode
+ }
+ }
+ }
+
+ xAppRmrEndPoint := restSubscription.xAppRmrEndPoint
+ go func() {
+ xapp.Logger.Debug("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
+ for _, instanceId := range restSubscription.InstanceIds {
+ xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId, 0)
+
+ if err != nil {
+ xapp.Logger.Error("%s", err.Error())
+ }
+ xapp.Logger.Debug("Deleteting instanceId = %v", instanceId)
+ restSubscription.DeleteXappIdToE2Id(xAppEventInstanceID)
+ restSubscription.DeleteE2InstanceId(instanceId)
+ }
+ c.restDuplicateCtrl.DeleteLastKnownRestSubsIdBasedOnMd5sum(restSubscription.lastReqMd5sum)
+ c.registry.DeleteRESTSubscription(&restSubId)
+ c.RemoveRESTSubscriptionFromDb(restSubId)
+ }()
+
+ c.UpdateCounter(cRestSubDelRespToXapp)
+ return common.UnsubscribeNoContentCode
+}
+
+//-------------------------------------------------------------------
+//
+//-------------------------------------------------------------------
+func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32, waitRouteCleanupTime time.Duration) (int64, error) {
+
+ var xAppEventInstanceID int64
+ subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
+ if err != nil {
+ xapp.Logger.Debug("Subscription Delete Handler subscription for restSubId=%v, E2EventInstanceID=%v not found %s",
+ restSubId, instanceId, idstring(err, nil))
+ return xAppEventInstanceID, nil
+ }
+
+ xAppEventInstanceID = int64(subs.ReqId.Id)
+ trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(*endPoint), *restSubId, e2ap.RequestId{subs.ReqId.Id, 0}, &xapp.RMRMeid{RanName: *meid})
+ if trans == nil {
+ err := fmt.Errorf("XAPP-SubDelReq transaction not created. restSubId %s, endPoint %s, meid %s, instanceId %v", *restSubId, *endPoint, *meid, instanceId)
+ xapp.Logger.Error("%s", err.Error())
+ }
+ defer trans.Release()
+
+ err = c.tracker.Track(trans)
+ if err != nil {
+ err := fmt.Errorf("XAPP-SubDelReq %s:", idstring(err, trans))
+ xapp.Logger.Error("%s", err.Error())
+ return xAppEventInstanceID, &time.ParseError{}
+ }
+ //
+ // Wake subs delete
+ //
+ subs.OngoingDelCount++
+ go c.handleSubscriptionDelete(subs, trans, waitRouteCleanupTime)
+ trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
+ subs.OngoingDelCount--
+
+ xapp.Logger.Debug("XAPP-SubDelReq: Handling event %s ", idstring(nil, trans, subs))
+
+ c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
+
+ return xAppEventInstanceID, nil