RIC-851: Updated submgr to trigger Delete Subscription Procedure
[ric-plt/submgr.git] / pkg / control / control.go
index 0c97afb..3dacc59 100755 (executable)
@@ -170,16 +170,31 @@ func NewControl() *Control {
 
        if readSubsFromDb == "true" {
                // Read subscriptions from db
-               c.ReadE2Subscriptions()
-               c.ReadRESTSubscriptions()
+               err := c.ReadE2Subscriptions()
+               if err != nil {
+                       xapp.Logger.Error("ReadE2Subscriptions() failed %s", err.Error())
+               }
+               err = c.ReadRESTSubscriptions()
+               if err != nil {
+                       xapp.Logger.Error("ReadRESTSubscriptions() failed %s", err.Error())
+               }
        }
 
-       go xapp.Subscription.Listen(c.RESTSubscriptionHandler, c.RESTQueryHandler, c.RESTSubscriptionDeleteHandler)
+       go func() {
+               err := xapp.Subscription.Listen(c.RESTSubscriptionHandler, c.RESTQueryHandler, c.RESTSubscriptionDeleteHandler)
+               if err != nil {
+                       xapp.Logger.Error("xapp.Subscription.Listen failure: %s", err.Error())
+               }
+       }()
        return c
 }
 
 func (c *Control) SymptomDataHandler(w http.ResponseWriter, r *http.Request) {
-       subscriptions, _ := c.registry.QueryHandler()
+       subscriptions, err := c.registry.QueryHandler()
+       if err != nil {
+               xapp.Logger.Error("QueryHandler() failed %s", err.Error())
+       }
+
        xapp.Resource.SendSymptomDataJson(w, r, subscriptions, "platform/subscriptions.json")
 }
 
@@ -237,7 +252,10 @@ func (c *Control) ReadRESTSubscriptions() error {
                        for restSubId, restSubscription := range restSubscriptions {
                                restSubscription.SubReqOngoing = false
                                restSubscription.SubDelReqOngoing = false
-                               c.WriteRESTSubscriptionToSdl(restSubId, restSubscription)
+                               err := c.WriteRESTSubscriptionToSdl(restSubId, restSubscription)
+                               if err != nil {
+                                       xapp.Logger.Error("WriteRESTSubscriptionToSdl() failed:%s", err.Error())
+                               }
                        }
                        c.registry.restSubscriptions = restSubscriptions
                        return nil
@@ -344,7 +362,7 @@ func (c *Control) HandleUncompletedSubscriptions(register map[uint32]*Subscripti
                        if subs.PolicyUpdate == false {
                                subs.NoRespToXapp = true
                                xapp.Logger.Debug("SendSubscriptionDeleteReq. subId = %v", subId)
-                               c.SendSubscriptionDeleteReq(subs)
+                               c.SendSubscriptionDeleteReq(subs, false)
                        }
                }
        }
@@ -445,6 +463,12 @@ func (c *Control) RESTSubscriptionHandler(params interface{}) (*models.Subscript
                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())
@@ -484,12 +508,6 @@ func (c *Control) RESTSubscriptionHandler(params interface{}) (*models.Subscript
        }
 
        c.WriteRESTSubscriptionToDb(restSubId, restSubscription)
-       e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(p)
-       if err != nil {
-               xapp.Logger.Error("%s", err)
-               c.registry.DeleteRESTSubscription(&restSubId)
-               return nil, common.SubscribeBadRequestCode
-       }
        go c.processSubscriptionRequests(restSubscription, &subReqList, p.ClientEndpoint, p.Meid, &restSubId, xAppRmrEndpoint, md5sum, e2SubscriptionDirectives)
 
        c.UpdateCounter(cRestSubRespToXapp)
@@ -667,8 +685,9 @@ func (c *Control) handleSubscriptionRequest(trans *TransactionXapp, subReqMsg *e
                }
        }
 
-       xapp.Logger.Error("XAPP-SubReq E2 subscription failed %s", idstring(err, trans, subs))
+       xapp.Logger.Error("XAPP-SubReq E2 subscription failed: %s", idstring(err, trans, subs))
        c.registry.RemoveFromSubscription(subs, trans, waitRouteCleanup_ms, c)
+
        return nil, &errorInfo, err
 }
 
@@ -698,15 +717,18 @@ func (c *Control) sendUnsuccesfullResponseNotification(restSubId *string, restSu
        restSubscription.SetProcessed(err)
        c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
        if trans != nil {
-               xapp.Logger.Debug("Sending unsuccessful REST notification (Error cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
-                       errorInfo.ErrorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
+               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 (Error cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v",
-                       errorInfo.ErrorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID)
+               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)
-       xapp.Subscription.Notify(resp, *clientEndpoint)
+       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 {
@@ -738,15 +760,13 @@ func (c *Control) sendSuccesfullResponseNotification(restSubId *string, restSubs
        // Mark REST subscription request processesd.
        restSubscription.SetProcessed(nil)
        c.UpdateRESTSubscriptionInDB(*restSubId, restSubscription, false)
-       if errorInfo.ErrorCause != " " {
-               xapp.Logger.Debug("Sending successful REST notification (Error cause %s) to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
-                       errorInfo.ErrorCause, clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
-       } else {
-               xapp.Logger.Debug("Sending successful REST notification to endpoint=%v:%v, XappEventInstanceID=%v, E2EventInstanceID=%v, %s",
-                       clientEndpoint.Host, *clientEndpoint.HTTPPort, xAppEventInstanceID, e2EventInstanceID, idstring(nil, trans))
-       }
+       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)
-       xapp.Subscription.Notify(resp, *clientEndpoint)
+       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 {
@@ -924,6 +944,8 @@ func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
                go c.handleE2TSubscriptionDeleteResponse(msg)
        case xapp.RIC_SUB_DEL_FAILURE:
                go c.handleE2TSubscriptionDeleteFailure(msg)
+       case xapp.RIC_SUB_DEL_REQUIRED:
+               go c.handleE2TSubscriptionDeleteRequired(msg)
        default:
                xapp.Logger.Debug("Unknown Message Type '%d', discarding", msg.Mtype)
        }
@@ -974,12 +996,14 @@ func (c *Control) handleXAPPSubscriptionRequest(params *xapp.RMRParams) {
 //------------------------------------------------------------------
 func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *TransactionXapp) {
 
-       e2SubscriptionDirectives, _ := c.GetE2SubscriptionDirectives(nil)
+       e2SubscriptionDirectives, err := c.GetE2SubscriptionDirectives(nil)
+       if err != nil {
+               xapp.Logger.Error("c.GetE2SubscriptionDirectives failure: %s", err.Error())
+       }
        subs.OngoingReqCount++
        go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, waitRouteCleanup_ms)
        event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
        subs.OngoingReqCount--
-       var err error
        if event != nil {
                switch themsg := event.(type) {
                case *e2ap.E2APSubscriptionResponse:
@@ -988,7 +1012,10 @@ func (c *Control) wakeSubscriptionRequest(subs *Subscription, trans *Transaction
                        if err == nil {
                                trans.Release()
                                c.UpdateCounter(cSubRespToXapp)
-                               c.rmrSendToXapp("", subs, trans)
+                               err := c.rmrSendToXapp("", subs, trans)
+                               if err != nil {
+                                       xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
+                               }
                                return
                        }
                case *e2ap.E2APSubscriptionFailure:
@@ -1066,7 +1093,10 @@ func (c *Control) handleXAPPSubscriptionDeleteRequest(params *xapp.RMRParams) {
        trans.Mtype, trans.Payload, err = c.e2ap.PackSubscriptionDeleteResponse(subDelRespMsg)
        if err == nil {
                c.UpdateCounter(cSubDelRespToXapp)
-               c.rmrSendToXapp("", subs, trans)
+               err := c.rmrSendToXapp("", subs, trans)
+               if err != nil {
+                       xapp.Logger.Error("rmrSendToXapp() failed:%s", err.Error())
+               }
        }
 }
 
@@ -1124,6 +1154,7 @@ func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *Tran
        if err != nil {
                valid = false
                c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
+
        }
 
        // Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
@@ -1203,7 +1234,11 @@ func (c *Control) sendE2TSubscriptionRequest(subs *Subscription, trans *Transact
                } else {
                        c.UpdateCounter(cSubReReqToE2)
                }
-               c.rmrSendToE2T(desc, subs, trans)
+               err := c.rmrSendToE2T(desc, subs, trans)
+               if err != nil {
+                       xapp.Logger.Error("rmrSendToE2T() failed:%s", err.Error())
+               }
+
                if subs.DoNotWaitSubResp == false {
                        event, timedOut = trans.WaitEvent(e2SubscriptionDirectives.E2TimeoutTimerValue)
                        if timedOut {
@@ -1248,7 +1283,10 @@ func (c *Control) sendE2TSubscriptionDeleteRequest(subs *Subscription, trans *Tr
                } else {
                        c.UpdateCounter(cSubDelReReqToE2)
                }
-               c.rmrSendToE2T(desc, subs, trans)
+               err := c.rmrSendToE2T(desc, subs, trans)
+               if err != nil {
+                       xapp.Logger.Error("SUBS-SubDelReq: rmrSendToE2T failure: %s", idstring(err, trans, subs, parentTrans))
+               }
                event, timedOut = trans.WaitEvent(e2tSubDelReqTime)
                if timedOut {
                        c.UpdateCounter(cSubDelReqTimerExpiry)
@@ -1325,7 +1363,7 @@ func (c *Control) handleE2TSubscriptionFailure(params *xapp.RMRParams) {
 //-------------------------------------------------------------------
 // handle from E2T Subscription Delete Response
 //-------------------------------------------------------------------
-func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
+func (c *Control) handleE2TSubscriptionDeleteResponse(params *xapp.RMRParams) {
        xapp.Logger.Debug("MSG from E2T: %s", params.String())
        c.UpdateCounter(cSubDelRespFromE2)
        subDelRespMsg, err := c.e2ap.UnpackSubscriptionDeleteResponse(params.Payload)
@@ -1485,7 +1523,7 @@ func (c *Control) RemoveRESTSubscriptionFromDb(restSubId string) {
        }
 }
 
-func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
+func (c *Control) SendSubscriptionDeleteReq(subs *Subscription, e2SubsDelRequired bool) {
 
        if c.UTTesting == true {
                // Reqistry mutex is not locked after real restart but it can be when restart is simulated in unit tests
@@ -1517,7 +1555,11 @@ func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
                        params.Payload = payload.Buf
                        params.Mbuf = nil
                        subs.DeleteFromDb = true
-                       c.handleXAPPSubscriptionDeleteRequest(params)
+                       if !e2SubsDelRequired {
+                               c.handleXAPPSubscriptionDeleteRequest(params)
+                       } else {
+                               c.SendSubscriptionDeleteReqToE2T(subs, params)
+                       }
                }
        }
 }
@@ -1591,3 +1633,83 @@ func (c *Control) PrintRESTSubscriptionRequest(p *models.SubscriptionParams) {
                }
        }
 }
+
+//-------------------------------------------------------------------
+// handle from E2T Subscription Delete Required
+//-------------------------------------------------------------------
+func (c *Control) handleE2TSubscriptionDeleteRequired(params *xapp.RMRParams) {
+       xapp.Logger.Info("MSG from E2T: %s", params.String())
+       c.UpdateCounter(cSubDelRequFromE2)
+       subsDelRequMsg, err := c.e2ap.UnpackSubscriptionDeleteRequired(params.Payload)
+       if err != nil {
+               xapp.Logger.Error("MSG-SubDelRequired: %s", idstring(err, params))
+               //c.sendE2TErrorIndication(nil)
+               return
+       }
+       var subscriptions = map[string][]e2ap.E2APSubscriptionDeleteRequired{}
+       var subDB = []*Subscription{}
+       for _, subsTobeRemove := range subsDelRequMsg.E2APSubscriptionDeleteRequiredRequests {
+               subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{subsTobeRemove.RequestId.InstanceId})
+               if err != nil {
+                       xapp.Logger.Error("MSG-SubDelFail: %s", idstring(err, params))
+                       continue
+               }
+               // Check if Delete Subscription Already triggered
+               if subs.OngoingDelCount > 0 {
+                       continue
+               }
+               subDB = append(subDB, subs)
+               for _, endpoint := range subs.EpList.Endpoints {
+                       subscriptions[endpoint.Addr] = append(subscriptions[endpoint.Addr], subsTobeRemove)
+               }
+               // Sending Subscription Delete Request to E2T
+               //      c.SendSubscriptionDeleteReq(subs, true)
+       }
+       for _, subsTobeRemove := range subDB {
+               // Sending Subscription Delete Request to E2T
+               c.SendSubscriptionDeleteReq(subsTobeRemove, true)
+       }
+}
+
+//-----------------------------------------------------------------
+// Initiate RIC Subscription Delete Request after receiving
+// RIC Subscription Delete Required from E2T
+//-----------------------------------------------------------------
+func (c *Control) SendSubscriptionDeleteReqToE2T(subs *Subscription, params *xapp.RMRParams) {
+       xapp.Logger.Debug("MSG TO E2T: %s", params.String())
+       c.UpdateCounter(cSubDelReqToE2)
+
+       if c.e2IfState.IsE2ConnectionUp(&params.Meid.RanName) == false {
+               xapp.Logger.Error("No E2 connection for ranName %v", params.Meid.RanName)
+               return
+       }
+
+       trans := c.tracker.NewXappTransaction(xapp.NewRmrEndpoint(params.Src), params.Xid, subs.ReqId.RequestId, params.Meid)
+       if trans == nil {
+               xapp.Logger.Error("XAPP-SubDelReq: %s", idstring(fmt.Errorf("transaction not created"), params))
+               return
+       }
+       defer trans.Release()
+
+       err := c.tracker.Track(trans)
+       if err != nil {
+               xapp.Logger.Error("XAPP-SubReq: %s", idstring(err, trans))
+               return
+       }
+
+       //
+       // Wake subs delete
+       //
+       subs.OngoingDelCount++
+       go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
+       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))
+
+       if subs.NoRespToXapp == true {
+               // Do no send delete responses to xapps due to submgr restart is deleting uncompleted subscriptions
+               xapp.Logger.Debug("XAPP-SubDelReq: subs.NoRespToXapp == true")
+               return
+       }
+}