RICPLT-2801, RICPLT-2802
[ric-plt/submgr.git] / pkg / control / control.go
old mode 100644 (file)
new mode 100755 (executable)
index 291075d..d5a92b6
 
 package control
 
-/*
-#include <rmr/RIC_message_types.h>
-#include <rmr/rmr.h>
-
-#cgo CFLAGS: -I../
-#cgo LDFLAGS: -lrmr_nng -lnng
-*/
-import "C"
-
 import (
        "errors"
-       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
-       "github.com/spf13/viper"
-       "github.com/go-openapi/strfmt"
-       httptransport "github.com/go-openapi/runtime/client"
        rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
        rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+       httptransport "github.com/go-openapi/runtime/client"
+       "github.com/go-openapi/strfmt"
+       "github.com/spf13/viper"
        "math/rand"
-       "strconv"
+       "sync"
        "time"
 )
 
+var subReqTime time.Duration = 5 * time.Second
+var subDelReqTime time.Duration = 5 * time.Second
+var maxSubReqTryCount uint64 = 2    // Initial try + retry
+var maxSubDelReqTryCount uint64 = 2 // Initial try + retry
+
 type Control struct {
-       e2ap        *E2ap
-       registry    *Registry
-       rtmgrClient *RtmgrClient
-       tracker     *Tracker
-       rc_chan     chan *xapp.RMRParams
+       e2ap         *E2ap
+       registry     *Registry
+       rtmgrClient  *RtmgrClient
+       tracker      *Tracker
+       timerMap     *TimerMap
+       rmrSendMutex sync.Mutex
 }
 
 type RMRMeid struct {
-       PlmnID string
-       EnbID  string
+       PlmnID  string
+       EnbID   string
+       RanName string
 }
 
-var SEEDSN uint16
-var SubscriptionReqChan = make(chan subRouteInfo, 10)
+var seedSN uint16
 
 const (
        CREATE Action = 0
-       MERGE Action = 1
+       MERGE  Action = 1
+       NONE   Action = 2
        DELETE Action = 3
 )
 
 func init() {
+       xapp.Logger.Info("SUBMGR")
        viper.AutomaticEnv()
        viper.SetEnvPrefix("submgr")
        viper.AllowEmptyEnv(true)
-       SEEDSN = uint16(viper.GetInt("seed_sn"))
-       if SEEDSN == 0 {
+       seedSN = uint16(viper.GetInt("seed_sn"))
+       if seedSN == 0 {
                rand.Seed(time.Now().UnixNano())
-               SEEDSN = uint16(rand.Intn(65535))
+               seedSN = uint16(rand.Intn(65535))
        }
-       if SEEDSN > 65535 {
-               SEEDSN = 0
+       if seedSN > 65535 {
+               seedSN = 0
        }
-       xapp.Logger.Info("SUBMGR: Initial Sequence Number: %v", SEEDSN)
+       xapp.Logger.Info("SUBMGR: Initial Sequence Number: %v", seedSN)
 }
 
-func NewControl() Control {
+func NewControl() *Control {
        registry := new(Registry)
-       registry.Initialize(SEEDSN)
+       registry.Initialize(seedSN)
 
        tracker := new(Tracker)
        tracker.Init()
 
-       transport := httptransport.New(viper.GetString("rtmgr.HostAddr") + ":" + viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
+       timerMap := new(TimerMap)
+       timerMap.Init()
+
+       transport := httptransport.New(viper.GetString("rtmgr.HostAddr")+":"+viper.GetString("rtmgr.port"), viper.GetString("rtmgr.baseUrl"), []string{"http"})
        client := rtmgrclient.New(transport, strfmt.Default)
        handle := rtmgrhandle.NewProvideXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
-       delete_handle := rtmgrhandle.NewDeleteXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
-       rtmgrClient := RtmgrClient{client, handle, delete_handle}
-
-       return Control{new(E2ap), registry, &rtmgrClient, tracker, make(chan *xapp.RMRParams)}
+       deleteHandle := rtmgrhandle.NewDeleteXappSubscriptionHandleParamsWithTimeout(10 * time.Second)
+       rtmgrClient := RtmgrClient{client, handle, deleteHandle}
+
+       return &Control{e2ap: new(E2ap),
+               registry:    registry,
+               rtmgrClient: &rtmgrClient,
+               tracker:     tracker,
+               timerMap:    timerMap,
+       }
 }
 
 func (c *Control) Run() {
-       go c.controlLoop()
        xapp.Run(c)
 }
 
-func (c *Control) Consume(rp *xapp.RMRParams) (err error) {
-       c.rc_chan <- rp
-       return
-}
-
 func (c *Control) rmrSend(params *xapp.RMRParams) (err error) {
-       if !xapp.Rmr.Send(params, false) {
+       status := false
+       i := 1
+       for ; i <= 10 && status == false; i++ {
+               c.rmrSendMutex.Lock()
+               status = xapp.Rmr.Send(params, false)
+               c.rmrSendMutex.Unlock()
+               if status == false {
+                       xapp.Logger.Info("rmr.Send() failed. Retry count %v, Mtype: %v, SubId: %v, Xid %s", i, params.Mtype, params.SubId, params.Xid)
+                       time.Sleep(500 * time.Millisecond)
+               }
+       }
+       if status == false {
                err = errors.New("rmr.Send() failed")
+               xapp.Rmr.Free(params.Mbuf)
        }
        return
 }
 
 func (c *Control) rmrReplyToSender(params *xapp.RMRParams) (err error) {
-       if !xapp.Rmr.Send(params, true) {
-               err = errors.New("rmr.Send() failed")
-       }
+       c.rmrSend(params)
        return
 }
 
-func (c *Control) controlLoop() {
-       for {
-               msg := <-c.rc_chan
-               switch msg.Mtype {
-                       case C.RIC_SUB_REQ:
-                               c.handleSubscriptionRequest(msg)
-                       case C.RIC_SUB_RESP:
-                               c.handleSubscriptionResponse(msg)
-                       case C.RIC_SUB_DEL_REQ:
-                               c.handleSubscriptionDeleteRequest(msg)
-                       case C.RIC_SUB_DEL_RESP:
-                               c.handleSubscriptionDeleteResponse(msg)
-                       default:
-                               err := errors.New("Message Type " + strconv.Itoa(msg.Mtype) + " is discarded")
-                               xapp.Logger.Error("Unknown message type: %v", err)
-               }
+func (c *Control) Consume(msg *xapp.RMRParams) (err error) {
+       switch msg.Mtype {
+       case xapp.RICMessageTypes["RIC_SUB_REQ"]:
+               go c.handleSubscriptionRequest(msg)
+       case xapp.RICMessageTypes["RIC_SUB_RESP"]:
+               go c.handleSubscriptionResponse(msg)
+       case xapp.RICMessageTypes["RIC_SUB_FAILURE"]:
+               go c.handleSubscriptionFailure(msg)
+       case xapp.RICMessageTypes["RIC_SUB_DEL_REQ"]:
+               go c.handleSubscriptionDeleteRequest(msg)
+       case xapp.RICMessageTypes["RIC_SUB_DEL_RESP"]:
+               go c.handleSubscriptionDeleteResponse(msg)
+       case xapp.RICMessageTypes["RIC_SUB_DEL_FAILURE"]:
+               go c.handleSubscriptionDeleteFailure(msg)
+       default:
+               xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
        }
+       return nil
 }
 
-func (c *Control) handleSubscriptionRequest(params *xapp.RMRParams) (err error) {
-       payload_seq_num, err := c.e2ap.GetSubscriptionRequestSequenceNumber(params.Payload)
+func (c *Control) handleSubscriptionRequest(params *xapp.RMRParams) {
+       xapp.Logger.Info("SubReq received from Src: %s, Mtype: %v, SubId: %v, Xid: %s, Meid: %v", params.Src, params.Mtype, params.SubId, params.Xid, params.Meid)
+       xapp.Rmr.Free(params.Mbuf)
+       params.Mbuf = nil
+
+       /* Reserve a sequence number and set it in the payload */
+       newSubId, isIdValid := c.registry.ReserveSequenceNumber()
+       if isIdValid != true {
+               xapp.Logger.Error("SubReq: Failed to reserve sequence number. Dropping this msg. SubId: %v, Xid: %s", params.SubId, params.Xid)
+               c.registry.releaseSequenceNumber(newSubId)
+               return
+       }
+
+       params.SubId = int(newSubId)
+       err := c.e2ap.SetSubscriptionRequestSequenceNumber(params.Payload, newSubId)
        if err != nil {
-               err = errors.New("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())
+               xapp.Logger.Error("SubReq: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
+               c.registry.releaseSequenceNumber(newSubId)
                return
        }
-       xapp.Logger.Info("Subscription Request Received. RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v", params.SubId, payload_seq_num)
 
-       /* Reserve a sequence number and set it in the payload */
-       new_sub_id := c.registry.ReserveSequenceNumber()
+       srcAddr, srcPort, err := c.rtmgrClient.SplitSource(params.Src)
+       if err != nil {
+               xapp.Logger.Error("SubReq: Failed to update routing-manager. Dropping this msg. Err: %s, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               c.registry.releaseSequenceNumber(newSubId)
+               return
+       }
 
-       _, err = c.e2ap.SetSubscriptionRequestSequenceNumber(params.Payload, new_sub_id)
+       // Create transatcion record for every subscription request
+       var forwardRespToXapp bool = true
+       var responseReceived bool = false
+       transaction, err := c.tracker.TrackTransaction(newSubId, CREATE, *srcAddr, *srcPort, params, responseReceived, forwardRespToXapp)
        if err != nil {
-               err = errors.New("Unable to set Subscription Sequence Number in Payload due to: " + err.Error())
+               xapp.Logger.Error("SubReq: Failed to create transaction record. Dropping this msg. Err: %v SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               c.registry.releaseSequenceNumber(newSubId)
                return
        }
 
-       src_addr, src_port, err := c.rtmgrClient.SplitSource(params.Src)
+       // Update routing manager about the new subscription
+       subRouteAction := transaction.SubRouteInfo()
+       xapp.Logger.Info("SubReq: Starting routing manager update. SubId: %v, Xid: %s", params.SubId, params.Xid)
+
+       err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
        if err != nil {
-               xapp.Logger.Error("Failed to update routing-manager about the subscription request with reason: %s", err)
+               xapp.Logger.Error("SubReq: Failed to update routing manager. Dropping this msg. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               c.registry.releaseSequenceNumber(newSubId)
                return
        }
 
-       /* Create transatcion records for every subscription request */
-       xact_key := Transaction_key{new_sub_id, CREATE}
-       xact_value := Transaction{*src_addr, *src_port, params.Payload, params.Mbuf}
-       err = c.tracker.Track_transaction(xact_key, xact_value)
+       // Setting new subscription ID in the RMR header
+       xapp.Logger.Info("SubReq: Forwarding SubReq to E2T: Mtype: %v, SubId: %v, Xid %s, Meid %v", params.Mtype, params.SubId, params.Xid, params.Meid)
+       err = c.rmrSend(params)
+       if err != nil {
+               xapp.Logger.Error("SubReq: Failed to send request to E2T %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+       }
+       c.timerMap.StartTimer("RIC_SUB_REQ", int(newSubId), subReqTime, FirstTry, c.handleSubscriptionRequestTimer)
+       xapp.Logger.Debug("SubReq: Debugging transaction table = %v", c.tracker.transactionTable)
+       return
+}
+
+func (c *Control) handleSubscriptionResponse(params *xapp.RMRParams) {
+       xapp.Logger.Info("SubResp received from Src: %s, Mtype: %v, SubId: %v, Meid: %v", params.Src, params.Mtype, params.SubId, params.Meid)
+       xapp.Rmr.Free(params.Mbuf)
+       params.Mbuf = nil
+
+       payloadSeqNum, err := c.e2ap.GetSubscriptionResponseSequenceNumber(params.Payload)
        if err != nil {
-               xapp.Logger.Error("Failed to create a transaction record due to %v", err)
+               xapp.Logger.Error("SubResp: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
                return
        }
+       xapp.Logger.Info("SubResp: Received payloadSeqNum: %v", payloadSeqNum)
 
-       /* Update routing manager about the new subscription*/
-       sub_route_action := subRouteInfo{CREATE, *src_addr, *src_port, new_sub_id }
-       go c.rtmgrClient.SubscriptionRequestUpdate()
-       SubscriptionReqChan <- sub_route_action
+       if !c.registry.IsValidSequenceNumber(payloadSeqNum) {
+               xapp.Logger.Error("SubResp: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
+               return
+       }
 
-       // Setting new subscription ID in the RMR header
-       params.SubId = int(new_sub_id)
+       c.timerMap.StopTimer("RIC_SUB_REQ", int(payloadSeqNum))
 
-       xapp.Logger.Info("Generated ID: %v. Forwarding to E2 Termination...", int(new_sub_id))
-       c.rmrSend(params)
-       xapp.Logger.Info("--- Debugging transaction table = %v", c.tracker.transaction_table)
+       transaction, responseReceived, err := c.tracker.CheckResponseReceived(payloadSeqNum, CREATE)
+       if err != nil {
+               xapp.Logger.Info("SubResp: Dropping this msg. Err: %v SubId: %v", err, payloadSeqNum)
+               return
+       }
+
+       if responseReceived == true {
+               // Subscription timer already received
+               return
+       }
+       xapp.Logger.Info("SubResp: SubId: %v, from address: %v:%v.", payloadSeqNum, transaction.Xappkey.Addr, transaction.Xappkey.Port)
+
+       c.registry.setSubscriptionToConfirmed(payloadSeqNum)
+
+       params.SubId = int(payloadSeqNum)
+       params.Xid = transaction.OrigParams.Xid
+
+       xapp.Logger.Info("SubResp: Forwarding Subscription Response to xApp Mtype: %v, SubId: %v, Meid: %v", params.Mtype, params.SubId, params.Meid)
+       err = c.rmrReplyToSender(params)
+       if err != nil {
+               xapp.Logger.Error("SubResp: Failed to send response to xApp. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+       }
+
+       xapp.Logger.Info("SubResp: SubId: %v, from address: %v:%v. Deleting transaction record", payloadSeqNum, transaction.Xappkey.Addr, transaction.Xappkey.Port)
+       _, err = c.tracker.completeTransaction(payloadSeqNum, CREATE)
+       if err != nil {
+               xapp.Logger.Error("SubResp: Failed to delete transaction record. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               return
+       }
        return
 }
 
-func (c *Control) handleSubscriptionResponse(params *xapp.RMRParams) (err error) {
-       payload_seq_num, err := c.e2ap.GetSubscriptionResponseSequenceNumber(params.Payload)
+func (c *Control) handleSubscriptionFailure(params *xapp.RMRParams) {
+       xapp.Logger.Info("SubFail received from Src: %s, Mtype: %v, SubId: %v, Meid: %v", params.Src, params.Mtype, params.SubId, params.Meid)
+       xapp.Rmr.Free(params.Mbuf)
+       params.Mbuf = nil
+
+       payloadSeqNum, err := c.e2ap.GetSubscriptionFailureSequenceNumber(params.Payload)
        if err != nil {
-               err = errors.New("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())
+               xapp.Logger.Error("SubFail: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
                return
        }
-       xapp.Logger.Info("Subscription Response Received. RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v", params.SubId, payload_seq_num)
-       if !c.registry.IsValidSequenceNumber(payload_seq_num) {
-               err = errors.New("Unknown Subscription ID: " + strconv.Itoa(int(payload_seq_num)) + " in Subscritpion Response. Message discarded.")
+       xapp.Logger.Info("SubFail: Received payloadSeqNum: %v", payloadSeqNum)
+
+       c.timerMap.StopTimer("RIC_SUB_REQ", int(payloadSeqNum))
+
+       transaction, responseReceived, err := c.tracker.CheckResponseReceived(payloadSeqNum, CREATE)
+       if err != nil {
+               xapp.Logger.Info("SubFail: Dropping this msg. Err: %v SubId: %v", err, payloadSeqNum)
+               return
+       }
+
+       if responseReceived == true {
+               // Subscription timer already received
+               return
+       }
+       xapp.Logger.Info("SubFail: SubId: %v, from address: %v:%v. Forwarding response to xApp", payloadSeqNum, transaction.Xappkey.Addr, transaction.Xappkey.Port)
+
+       time.Sleep(3 * time.Second)
+
+       xapp.Logger.Info("SubFail: Starting routing manager update. SubId: %v, Xid: %s", params.SubId, params.Xid)
+       subRouteAction := transaction.SubRouteInfo()
+       err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+       if err != nil {
+               xapp.Logger.Error("SubFail: Failed to update routing manager. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+       }
+
+       xapp.Logger.Info("SubFail: Deleting transaction record. SubId: %v, Xid: %s", params.SubId, params.Xid)
+       if c.registry.releaseSequenceNumber(payloadSeqNum) {
+               _, err = c.tracker.completeTransaction(payloadSeqNum, CREATE)
+               if err != nil {
+                       xapp.Logger.Error("SubFail: Failed to delete transaction record. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+                       return
+               }
+       } else {
+               xapp.Logger.Error("SubFail: Failed to release sequency number. SubId: %v, Xid: %s", params.SubId, params.Xid)
+               return
+       }
+       return
+}
+
+func (c *Control) handleSubscriptionRequestTimer(strId string, nbrId int, tryCount uint64) {
+       subId := uint16(nbrId)
+       xapp.Logger.Info("handleSubTimer: SubReq timer expired. subId: %v,  tryCount: %v", subId, tryCount)
+
+       transaction, responseReceived, err := c.tracker.CheckResponseReceived(subId, CREATE)
+       if err != nil {
+               xapp.Logger.Info("handleSubTimer: Dropping this timer action. Err: %v SubId: %v", err, subId)
+               return
+       }
+
+       if responseReceived == true {
+               // Subscription Response or Failure already received
+               return
+       }
+
+       if tryCount < maxSubReqTryCount {
+               xapp.Logger.Info("handleSubTimer: Resending SubReq to E2T: Mtype: %v, SubId: %v, Xid %s, Meid %v", transaction.OrigParams.Mtype, transaction.OrigParams.SubId, transaction.OrigParams.Xid, transaction.OrigParams.Meid)
+               // Set possible to handle new response for the subId
+               err = c.tracker.RetryTransaction(subId, CREATE)
+               if err != nil {
+                       xapp.Logger.Error("handleSubDelTimer: Failed to retry transaction record. Dropping timer action. Err %v, SubId: %v", err, transaction.OrigParams.SubId)
+                       return
+               }
+
+               err = c.rmrSend(transaction.OrigParams)
+               if err != nil {
+                       xapp.Logger.Error("handleSubTimer: Failed to send request to E2T %v, SubId: %v, Xid: %s", err, transaction.OrigParams.SubId, transaction.OrigParams.Xid)
+               }
+
+               tryCount++
+               c.timerMap.StartTimer("RIC_SUB_REQ", int(subId), subReqTime, tryCount, c.handleSubscriptionRequestTimer)
+               return
+       }
+
+       var subDelReqPayload []byte
+       subDelReqPayload, err = c.e2ap.PackSubscriptionDeleteRequest(transaction.OrigParams.Payload, subId)
+       if err != nil {
+               xapp.Logger.Error("handleSubTimer: Packing SubDelReq failed. Err: %v", err)
+               return
+       }
+
+       // Cancel failed subscription
+       var params xapp.RMRParams
+       params.Mtype = 12020 // RIC SUBSCRIPTION DELETE
+       params.SubId = int(subId)
+       params.Xid = transaction.OrigParams.Xid
+       params.Meid = transaction.OrigParams.Meid
+       params.Src = transaction.OrigParams.Src
+       params.PayloadLen = len(subDelReqPayload)
+       params.Payload = subDelReqPayload
+       params.Mbuf = nil
+
+       // Delete CREATE transaction
+       _, err = c.tracker.completeTransaction(subId, CREATE)
+       if err != nil {
+               xapp.Logger.Error("handleSubTimer: Failed to delete create transaction record. Dropping this timer action. Err: %v, SubId: %v, Xid: %s", err, subId, params.Xid)
                return
        }
-       c.registry.setSubscriptionToConfirmed(payload_seq_num)
-       xapp.Logger.Info("Subscription Response Registered. Forwarding to Requestor...")
-       transaction, err := c.tracker.complete_transaction(payload_seq_num, DELETE)
+
+       // Create DELETE transaction
+       var forwardRespToXapp bool = false
+       _, err = c.trackDeleteTransaction(&params, subId, forwardRespToXapp)
        if err != nil {
-               xapp.Logger.Error("Failed to create a transaction record due to %v", err)
+               xapp.Logger.Error("handleSubTimer: Failed to create delete transaction record. Dropping this timer action. Err: %v, SubId: %v, Xid: %s", err, subId, params.Xid)
                return
        }
-       xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Forwarding to E2 Termination...", int(payload_seq_num), transaction.Xapp_instance_address, transaction.Xapp_port)
-       params.Mbuf = transaction.Mbuf
-       c.rmrReplyToSender(params)
+
+       xapp.Logger.Info("handleSubTimer: Sending SubDelReq to E2T: Mtype: %v, SubId: %v, Meid: %v", params.Mtype, params.SubId, params.Meid)
+       c.rmrSend(&params)
+       if err != nil {
+               xapp.Logger.Error("handleSubTimer: Failed to send request to E2T %v. SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+       }
+       c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subId), subDelReqTime, FirstTry, c.handleSubscriptionDeleteRequestTimer)
        return
 }
 
@@ -211,6 +389,7 @@ func (act Action) String() string {
        actions := [...]string{
                "CREATE",
                "MERGE",
+               "NONE",
                "DELETE",
        }
 
@@ -229,56 +408,261 @@ func (act Action) valid() bool {
        }
 }
 
-func (c *Control) handleSubscriptionDeleteRequest(params *xapp.RMRParams) (err error) {
-       payload_seq_num, err := c.e2ap.GetSubscriptionDeleteRequestSequenceNumber(params.Payload)
+func (c *Control) handleSubscriptionDeleteRequest(params *xapp.RMRParams) {
+       xapp.Logger.Info("SubDelReq received from Src: %s, Mtype: %v, SubId: %v, Xid: %s, Meid: %v", params.Src, params.Mtype, params.SubId, params.Xid, params.Meid)
+       xapp.Rmr.Free(params.Mbuf)
+       params.Mbuf = nil
+
+       payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteRequestSequenceNumber(params.Payload)
        if err != nil {
-               err = errors.New("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())
+               xapp.Logger.Error("SubDelReq: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
                return
        }
-       xapp.Logger.Info("Subscription Delete Request Received. RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v", params.SubId, payload_seq_num)
-       if c.registry.IsValidSequenceNumber(payload_seq_num) {
-               c.registry.deleteSubscription(payload_seq_num)
-               trackErr := c.trackDeleteTransaction(params, payload_seq_num)
-               if trackErr != nil {
-                       xapp.Logger.Error("Failed to create a transaction record due to %v", err)
-                       return trackErr
+       xapp.Logger.Info("SubDelReq: Received payloadSeqNum: %v", payloadSeqNum)
+
+       if c.registry.IsValidSequenceNumber(payloadSeqNum) {
+               c.registry.deleteSubscription(payloadSeqNum)
+               var forwardRespToXapp bool = true
+               _, err = c.trackDeleteTransaction(params, payloadSeqNum, forwardRespToXapp)
+               if err != nil {
+                       xapp.Logger.Error("SubDelReq: Failed to create transaction record. Dropping this msg. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+                       return
                }
+       } else {
+               xapp.Logger.Error("SubDelReq: Not valid sequence number. Dropping this msg. SubId: %v, Xid: %s", params.SubId, params.Xid)
+               return
        }
-       xapp.Logger.Info("Subscription ID: %v. Forwarding to E2 Termination...", int(payload_seq_num))
+
+       xapp.Logger.Info("SubDelReq: Forwarding Request to E2T. Mtype: %v, SubId: %v, Xid: %s, Meid: %v", params.Mtype, params.SubId, params.Xid, params.Meid)
        c.rmrSend(params)
+       if err != nil {
+               xapp.Logger.Error("SubDelReq: Failed to send request to E2T. Err %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+       }
+       c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(payloadSeqNum), subDelReqTime, FirstTry, c.handleSubscriptionDeleteRequestTimer)
        return
 }
 
-func (c *Control) trackDeleteTransaction(params *xapp.RMRParams, payload_seq_num uint16) (err error) {
-       src_addr, src_port, err := c.rtmgrClient.SplitSource(params.Src)
-       xact_key := Transaction_key{payload_seq_num, DELETE}
-       xact_value := Transaction{*src_addr, *src_port, params.Payload, params.Mbuf}
-       err = c.tracker.Track_transaction(xact_key, xact_value)
+func (c *Control) trackDeleteTransaction(params *xapp.RMRParams, payloadSeqNum uint16, forwardRespToXapp bool) (transaction *Transaction, err error) {
+       srcAddr, srcPort, err := c.rtmgrClient.SplitSource(params.Src)
+       if err != nil {
+               xapp.Logger.Error("Failed to split source address. Err: %s, SubId: %v, Xid: %s", err, payloadSeqNum, params.Xid)
+       }
+       var respReceived bool = false
+       transaction, err = c.tracker.TrackTransaction(payloadSeqNum, DELETE, *srcAddr, *srcPort, params, respReceived, forwardRespToXapp)
        return
 }
 
 func (c *Control) handleSubscriptionDeleteResponse(params *xapp.RMRParams) (err error) {
-       payload_seq_num, err := c.e2ap.GetSubscriptionDeleteResponseSequenceNumber(params.Payload)
+       xapp.Logger.Info("SubDelResp received from Src: %s, Mtype: %v, SubId: %v, Meid: %v", params.Src, params.Mtype, params.SubId, params.Meid)
+       xapp.Rmr.Free(params.Mbuf)
+       params.Mbuf = nil
+
+       payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteResponseSequenceNumber(params.Payload)
+       if err != nil {
+               xapp.Logger.Error("SubDelResp: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
+               return
+       }
+       xapp.Logger.Info("SubDelResp: Received payloadSeqNum: %v", payloadSeqNum)
+
+       c.timerMap.StopTimer("RIC_SUB_DEL_REQ", int(payloadSeqNum))
+
+       transaction, responseReceived, err := c.tracker.CheckResponseReceived(payloadSeqNum, DELETE)
+       if err != nil {
+               xapp.Logger.Info("SubDelResp: Dropping this msg. Err: %v SubId: %v", err, payloadSeqNum)
+               return
+       }
+
+       if responseReceived == true {
+               // Subscription Delete timer already received
+               return
+       }
+       xapp.Logger.Info("SubDelResp: SubId: %v, from address: %v:%v. Forwarding response to xApp", payloadSeqNum, transaction.Xappkey.Addr, transaction.Xappkey.Port)
+
+       if transaction.ForwardRespToXapp == true {
+               params.SubId = int(payloadSeqNum)
+               params.Xid = transaction.OrigParams.Xid
+               xapp.Logger.Info("Forwarding SubDelResp to xApp: Mtype: %v, SubId: %v, Xid: %v, Meid: %v", params.Mtype, params.SubId, params.Xid, params.Meid)
+               err = c.rmrReplyToSender(params)
+               if err != nil {
+                       xapp.Logger.Error("SubDelResp: Failed to send response to xApp. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               }
+
+               time.Sleep(3 * time.Second)
+       }
+
+       xapp.Logger.Info("SubDelResp: Starting routing manager update. SubId: %v, Xid: %s", params.SubId, params.Xid)
+       subRouteAction := SubRouteInfo{DELETE, transaction.Xappkey.Addr, transaction.Xappkey.Port, payloadSeqNum}
+       err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+       if err != nil {
+               xapp.Logger.Error("SubDelResp: Failed to update routing manager. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               return
+       }
+
+       xapp.Logger.Info("SubDelResp: Deleting transaction record. SubId: %v, Xid: %s", params.SubId, params.Xid)
+       if c.registry.releaseSequenceNumber(payloadSeqNum) {
+               _, err = c.tracker.completeTransaction(payloadSeqNum, DELETE)
+               if err != nil {
+                       xapp.Logger.Error("SubDelResp: Failed to delete transaction record. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+                       return
+               }
+       } else {
+               xapp.Logger.Error("SubDelResp: Failed to release sequency number. SubId: %v, Xid: %s", params.SubId, params.Xid)
+               return
+       }
+       return
+}
+
+func (c *Control) handleSubscriptionDeleteFailure(params *xapp.RMRParams) {
+       xapp.Logger.Info("SubDelFail received from Src: %s, Mtype: %v, SubId: %v, Meid: %v", params.Src, params.Mtype, params.SubId, params.Meid)
+       xapp.Rmr.Free(params.Mbuf)
+       params.Mbuf = nil
+
+       payloadSeqNum, err := c.e2ap.GetSubscriptionDeleteFailureSequenceNumber(params.Payload)
+       if err != nil {
+               xapp.Logger.Error("SubDelFail: Unable to get Sequence Number from Payload. Dropping this msg. Err: %v, SubId: %v, Xid: %s, Payload %X", err, params.SubId, params.Xid, params.Payload)
+               return
+       }
+       xapp.Logger.Info("SubDelFail: Received payloadSeqNum: %v", payloadSeqNum)
+
+       c.timerMap.StopTimer("RIC_SUB_DEL_REQ", int(payloadSeqNum))
+
+       transaction, responseReceived, err := c.tracker.CheckResponseReceived(payloadSeqNum, DELETE)
+       if err != nil {
+               xapp.Logger.Info("SubDelFail: Dropping this msg. Err: %v SubId: %v", err, payloadSeqNum)
+               return
+       }
+
+       if responseReceived == true {
+               // Subscription Delete timer already received
+               return
+       }
+       xapp.Logger.Info("SubDelFail: SubId: %v, from address: %v:%v. Forwarding response to xApp", payloadSeqNum, transaction.Xappkey.Addr, transaction.Xappkey.Port)
+
+       if transaction.ForwardRespToXapp == true {
+               var subDelRespPayload []byte
+               subDelRespPayload, err = c.e2ap.PackSubscriptionDeleteResponse(transaction.OrigParams.Payload, payloadSeqNum)
+               if err != nil {
+                       xapp.Logger.Error("SubDelFail:Packing SubDelResp failed. Err: %v", err)
+                       return
+               }
+
+               params.Mtype = 12021 // RIC SUBSCRIPTION DELETE RESPONSE
+               params.SubId = int(payloadSeqNum)
+               params.Xid = transaction.OrigParams.Xid
+               params.Meid = transaction.OrigParams.Meid
+               params.Src = transaction.OrigParams.Src
+               params.PayloadLen = len(subDelRespPayload)
+               params.Payload = subDelRespPayload
+               params.Mbuf = nil
+               xapp.Logger.Info("SubDelFail: Forwarding SubDelFail to xApp: Mtype: %v, SubId: %v, Xid: %v, Meid: %v", params.Mtype, params.SubId, params.Xid, params.Meid)
+               err = c.rmrReplyToSender(params)
+               if err != nil {
+                       xapp.Logger.Error("SubDelFail: Failed to send SubDelFail to xApp. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               }
+
+               time.Sleep(3 * time.Second)
+       }
+
+       xapp.Logger.Info("SubDelFail: Starting routing manager update. SubId: %v, Xid: %s", params.SubId, params.Xid)
+       subRouteAction := SubRouteInfo{DELETE, transaction.Xappkey.Addr, transaction.Xappkey.Port, payloadSeqNum}
+       err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
+       if err != nil {
+               xapp.Logger.Error("SubDelFail: Failed to update routing manager. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               return
+       }
+
+       xapp.Logger.Info("SubDelFail: Deleting transaction record. SubId: %v, Xid: %s", params.SubId, params.Xid)
+       if c.registry.releaseSequenceNumber(payloadSeqNum) {
+               _, err = c.tracker.completeTransaction(payloadSeqNum, DELETE)
+               if err != nil {
+                       xapp.Logger.Error("SubDelFail: Failed to delete transaction record. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+                       return
+               }
+       } else {
+               xapp.Logger.Error("SubDelFail: Failed to release sequency number. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               return
+       }
+       return
+}
+
+func (c *Control) handleSubscriptionDeleteRequestTimer(strId string, nbrId int, tryCount uint64) {
+       subId := uint16(nbrId)
+       xapp.Logger.Info("handleSubDelTimer: SubDelReq timer expired. subId: %v, tryCount: %v", subId, tryCount)
+
+       transaction, responseReceived, err := c.tracker.CheckResponseReceived(subId, DELETE)
+       if err != nil {
+               xapp.Logger.Info("handleSubTimer: Dropping this timer action. Err: %v SubId: %v", err, subId)
+               return
+       }
+
+       if responseReceived == true {
+               // Subscription Delete Response or Failure already received
+               return
+       }
+
+       if tryCount < maxSubDelReqTryCount {
+               xapp.Logger.Info("handleSubDelTimer: Resending SubDelReq to E2T: Mtype: %v, SubId: %v, Xid %s, Meid %v", transaction.OrigParams.Mtype, transaction.OrigParams.SubId, transaction.OrigParams.Xid, transaction.OrigParams.Meid)
+               // Set possible to handle new response for the subId
+               err = c.tracker.RetryTransaction(subId, DELETE)
+               if err != nil {
+                       xapp.Logger.Error("handleSubDelTimer: Failed to retry transaction record. Dropping  timer action. Err %v, SubId: %v", err, transaction.OrigParams.SubId)
+                       return
+               }
+
+               err = c.rmrSend(transaction.OrigParams)
+               if err != nil {
+                       xapp.Logger.Error("handleSubDelTimer: Failed to send request to E2T %v, SubId: %v, Xid: %s", err, transaction.OrigParams.SubId, transaction.OrigParams.Xid)
+               }
+
+               tryCount++
+               c.timerMap.StartTimer("RIC_SUB_DEL_REQ", int(subId), subReqTime, tryCount, c.handleSubscriptionDeleteRequestTimer)
+               return
+       }
+
+       var params xapp.RMRParams
+       if transaction.ForwardRespToXapp == true {
+               var subDelRespPayload []byte
+               subDelRespPayload, err = c.e2ap.PackSubscriptionDeleteResponse(transaction.OrigParams.Payload, subId)
+               if err != nil {
+                       xapp.Logger.Error("handleSubDelTimer: Unable to pack payload. Dropping this timer action. Err: %v, SubId: %v, Xid: %s, Payload %x", err, subId, transaction.OrigParams.Xid, transaction.OrigParams.Payload)
+                       return
+               }
+
+               params.Mtype = 12021 // RIC SUBSCRIPTION DELETE RESPONSE
+               params.SubId = int(subId)
+               params.Meid = transaction.OrigParams.Meid
+               params.Xid = transaction.OrigParams.Xid
+               params.Src = transaction.OrigParams.Src
+               params.PayloadLen = len(subDelRespPayload)
+               params.Payload = subDelRespPayload
+               params.Mbuf = nil
+
+               xapp.Logger.Info("handleSubDelTimer: Sending SubDelResp to xApp: Mtype: %v, SubId: %v, Xid: %s, Meid: %v", params.Mtype, params.SubId, params.Xid, params.Meid)
+               err = c.rmrReplyToSender(&params)
+               if err != nil {
+                       xapp.Logger.Error("handleSubDelTimer: Failed to send response to xApp: Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+               }
+
+               time.Sleep(3 * time.Second)
+       }
+
+       xapp.Logger.Info("handleSubDelTimer: Starting routing manager update. SubId: %v, Xid: %s", subId, params.Xid)
+       subRouteAction := SubRouteInfo{DELETE, transaction.Xappkey.Addr, transaction.Xappkey.Port, subId}
+       err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
        if err != nil {
-               err = errors.New("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())
+               xapp.Logger.Error("handleSubDelTimer: Failed to update routing manager. Err: %v, SubId: %v, Xid: %s", err, subId, params.Xid)
                return
        }
-    var transaction , _= c.tracker.Retrive_transaction(payload_seq_num, DELETE)
-       sub_route_action := subRouteInfo{DELETE, transaction.Xapp_instance_address, transaction.Xapp_port, payload_seq_num }
-       go c.rtmgrClient.SubscriptionRequestUpdate()
-       SubscriptionReqChan <- sub_route_action
 
-       xapp.Logger.Info("Subscription Delete Response Received. RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v", params.SubId, payload_seq_num)
-       if c.registry.releaseSequenceNumber(payload_seq_num) {
-               transaction, err = c.tracker.complete_transaction(payload_seq_num, DELETE)
+       xapp.Logger.Info("handleSubDelTimer: Deleting transaction record. SubId: %v, Xid: %s", subId, params.Xid)
+       if c.registry.releaseSequenceNumber(subId) {
+               _, err = c.tracker.completeTransaction(subId, DELETE)
                if err != nil {
-                       xapp.Logger.Error("Failed to create a transaction record due to %v", err)
+                       xapp.Logger.Error("handleSubDelTimer: Failed to delete transaction record. Err: %v, SubId: %v, Xid: %s", err, subId, params.Xid)
                        return
                }
-               xapp.Logger.Info("Subscription ID: %v, from address: %v:%v. Forwarding to E2 Termination...", int(payload_seq_num), transaction.Xapp_instance_address, transaction.Xapp_port)
-               //params.Src = xAddress + ":" + strconv.Itoa(int(xPort))
-               params.Mbuf = transaction.Mbuf
-               c.rmrReplyToSender(params)
+       } else {
+               xapp.Logger.Error("handleSubDelTimer: Failed to release sequency number. SubId: %v, Xid: %s", subId, params.Xid)
        }
        return
 }