RICPLT-2571 Make code change for MEID support

Change-Id: I2fce09a4165a6108de47a4016afa499c2a07fd64
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
diff --git a/pkg/control/control.go b/pkg/control/control.go
index 9ce34a0..5a38656 100755
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -157,40 +157,39 @@
 	xapp.Rmr.Free(params.Mbuf)
 	params.Mbuf = nil
 
+	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)
+		return
+	}
+
 	/* Reserve a sequence number and set it in the payload */
-	subs := c.registry.ReserveSubscription()
+	subs := c.registry.ReserveSubscription(RmrEndpoint{*srcAddr, *srcPort}, params.Meid)
 	if subs == nil {
 		xapp.Logger.Error("SubReq: Failed to reserve sequence number. Dropping this msg. SubId: %v, Xid: %s", params.SubId, params.Xid)
 		return
 	}
 
 	params.SubId = int(subs.Seq)
-	err := c.e2ap.SetSubscriptionRequestSequenceNumber(params.Payload, subs.Seq)
+	err = c.e2ap.SetSubscriptionRequestSequenceNumber(params.Payload, subs.Seq)
 	if err != nil {
 		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(subs.Seq)
 		return
 	}
 
-	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(subs.Seq)
-		return
-	}
-
 	// Create transatcion record for every subscription request
 	var forwardRespToXapp bool = true
 	var responseReceived bool = false
-	transaction, err := c.tracker.TrackTransaction(subs.Seq, CREATE, *srcAddr, *srcPort, params, responseReceived, forwardRespToXapp)
+	_, err = c.tracker.TrackTransaction(subs, RmrEndpoint{*srcAddr, *srcPort}, params, responseReceived, forwardRespToXapp)
 	if err != nil {
-		xapp.Logger.Error("SubReq: Failed to create transaction record. Dropping this msg. Err: %v SubId: %v, Xid: %s", err, params.SubId, params.Xid)
+		xapp.Logger.Error("SubReq: %s, Dropping this msg.", err.Error())
 		c.registry.releaseSequenceNumber(subs.Seq)
 		return
 	}
 
 	// Update routing manager about the new subscription
-	subRouteAction := transaction.SubRouteInfo()
+	subRouteAction := subs.SubRouteInfo(CREATE)
 	xapp.Logger.Info("SubReq: Starting routing manager update. SubId: %v, Xid: %s", params.SubId, params.Xid)
 
 	err = c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
@@ -207,7 +206,7 @@
 		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(subs.Seq), subReqTime, FirstTry, c.handleSubscriptionRequestTimer)
-	xapp.Logger.Debug("SubReq: Debugging transaction table = %v", c.tracker.transactionTable)
+	xapp.Logger.Debug("SubReq: Debugging transaction table = %v", c.tracker.transactionXappTable)
 	return
 }
 
@@ -223,26 +222,25 @@
 	}
 	xapp.Logger.Info("SubResp: Received payloadSeqNum: %v", payloadSeqNum)
 
-	if !c.registry.IsValidSequenceNumber(payloadSeqNum) {
+	subs := c.registry.GetSubscription(payloadSeqNum)
+	if subs == nil {
 		xapp.Logger.Error("SubResp: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
 		return
 	}
 
+	transaction := subs.GetTransaction()
+
 	c.timerMap.StopTimer("RIC_SUB_REQ", int(payloadSeqNum))
 
-	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
-	}
-
+	responseReceived := transaction.CheckResponseReceived()
 	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)
+	xapp.Logger.Info("SubResp: SubId: %v, from address: %s.", payloadSeqNum, transaction.RmrEndpoint)
 
-	c.registry.setSubscriptionToConfirmed(payloadSeqNum)
+	subs.Confirmed()
+	transaction.Release()
 
 	params.SubId = int(payloadSeqNum)
 	params.Xid = transaction.OrigParams.Xid
@@ -253,12 +251,7 @@
 		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
-	}
+	xapp.Logger.Info("SubResp: SubId: %v, from address: %s. Deleting transaction record", payloadSeqNum, transaction.RmrEndpoint)
 	return
 }
 
@@ -274,9 +267,21 @@
 	}
 	xapp.Logger.Info("SubFail: Received payloadSeqNum: %v", payloadSeqNum)
 
+	subs := c.registry.GetSubscription(payloadSeqNum)
+	if subs == nil {
+		xapp.Logger.Error("SubFail: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
+		return
+	}
+
+	transaction := subs.GetTransaction()
+	if transaction == nil {
+		xapp.Logger.Error("SubFail: Unknown transaction. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
+		return
+	}
+
 	c.timerMap.StopTimer("RIC_SUB_REQ", int(payloadSeqNum))
 
-	transaction, responseReceived, err := c.tracker.CheckResponseReceived(payloadSeqNum, CREATE)
+	responseReceived := transaction.CheckResponseReceived()
 	if err != nil {
 		xapp.Logger.Info("SubFail: Dropping this msg. Err: %v SubId: %v", err, payloadSeqNum)
 		return
@@ -286,27 +291,21 @@
 		// 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)
+	xapp.Logger.Info("SubFail: SubId: %v, from address: %s. Forwarding response to xApp", payloadSeqNum, transaction.RmrEndpoint)
 
 	time.Sleep(3 * time.Second)
 
 	xapp.Logger.Info("SubFail: Starting routing manager update. SubId: %v, Xid: %s", params.SubId, params.Xid)
-	subRouteAction := transaction.SubRouteInfo()
+	subRouteAction := subs.SubRouteInfo(DELETE)
 	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 {
+	transaction.Release()
+	if !c.registry.releaseSequenceNumber(payloadSeqNum) {
 		xapp.Logger.Error("SubFail: Failed to release sequency number. SubId: %v, Xid: %s", params.SubId, params.Xid)
-		return
 	}
 	return
 }
@@ -315,12 +314,20 @@
 	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)
+	subs := c.registry.GetSubscription(subId)
+	if subs == nil {
+		xapp.Logger.Error("SubFail: Unknown payloadSeqNum. Dropping this msg. SubId: %v", subId)
 		return
 	}
 
+	transaction := subs.GetTransaction()
+	if transaction == nil {
+		xapp.Logger.Error("SubFail: Unknown transaction. Dropping this msg. SubId: %v", subId)
+		return
+	}
+
+	responseReceived := transaction.CheckResponseReceived()
+
 	if responseReceived == true {
 		// Subscription Response or Failure already received
 		return
@@ -328,14 +335,10 @@
 
 	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)
+		transaction.RetryTransaction()
+
+		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)
 		}
@@ -346,7 +349,7 @@
 	}
 
 	var subDelReqPayload []byte
-	subDelReqPayload, err = c.e2ap.PackSubscriptionDeleteRequest(transaction.OrigParams.Payload, subId)
+	subDelReqPayload, err := c.e2ap.PackSubscriptionDeleteRequest(transaction.OrigParams.Payload, subId)
 	if err != nil {
 		xapp.Logger.Error("handleSubTimer: Packing SubDelReq failed. Err: %v", err)
 		return
@@ -364,17 +367,12 @@
 	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
-	}
+	transaction.Release()
 
 	// Create DELETE transaction
-	var forwardRespToXapp bool = false
-	_, err = c.trackDeleteTransaction(&params, subId, forwardRespToXapp)
+	_, err = c.trackDeleteTransaction(subs, &params, subId, false)
 	if err != nil {
-		xapp.Logger.Error("handleSubTimer: Failed to create delete transaction record. Dropping this timer action. Err: %v, SubId: %v, Xid: %s", err, subId, params.Xid)
+		xapp.Logger.Error("handleSubTimer: %s, Dropping this msg.", err.Error())
 		return
 	}
 
@@ -422,14 +420,15 @@
 	}
 	xapp.Logger.Info("SubDelReq: Received payloadSeqNum: %v", payloadSeqNum)
 
-	if c.registry.IsValidSequenceNumber(payloadSeqNum) {
+	subs := c.registry.GetSubscription(payloadSeqNum)
+	if subs != nil {
 		var forwardRespToXapp bool = true
-		_, err = c.trackDeleteTransaction(params, payloadSeqNum, forwardRespToXapp)
+		_, err = c.trackDeleteTransaction(subs, 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)
+			xapp.Logger.Error("SubDelReq: %s, Dropping this msg.", err.Error())
 			return
 		}
-		c.registry.setSubscriptionToUnConfirmed(payloadSeqNum)
+		subs.UnConfirmed()
 	} else {
 		xapp.Logger.Error("SubDelReq: Not valid sequence number. Dropping this msg. SubId: %v, Xid: %s", params.SubId, params.Xid)
 		return
@@ -444,13 +443,13 @@
 	return
 }
 
-func (c *Control) trackDeleteTransaction(params *xapp.RMRParams, payloadSeqNum uint16, forwardRespToXapp bool) (transaction *Transaction, err error) {
+func (c *Control) trackDeleteTransaction(subs *Subscription, 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)
+	transaction, err = c.tracker.TrackTransaction(subs, RmrEndpoint{*srcAddr, *srcPort}, params, respReceived, forwardRespToXapp)
 	return
 }
 
@@ -466,20 +465,29 @@
 	}
 	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)
+	subs := c.registry.GetSubscription(payloadSeqNum)
+	if subs == nil {
+		xapp.Logger.Error("SubDelResp: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
 		return
 	}
 
+	transaction := subs.GetTransaction()
+	if transaction == nil {
+		xapp.Logger.Error("SubDelResp: Unknown transaction. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
+		return
+	}
+
+	c.timerMap.StopTimer("RIC_SUB_DEL_REQ", int(payloadSeqNum))
+
+	responseReceived := transaction.CheckResponseReceived()
 	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)
 
+	transaction.Release()
+
+	xapp.Logger.Info("SubDelResp: SubId: %v, from address: %s. Forwarding response to xApp", payloadSeqNum, transaction.RmrEndpoint)
 	if transaction.ForwardRespToXapp == true {
 		params.SubId = int(payloadSeqNum)
 		params.Xid = transaction.OrigParams.Xid
@@ -493,7 +501,7 @@
 	}
 
 	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}
+	subRouteAction := subs.SubRouteInfo(DELETE)
 	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)
@@ -501,13 +509,7 @@
 	}
 
 	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 {
+	if !c.registry.releaseSequenceNumber(payloadSeqNum) {
 		xapp.Logger.Error("SubDelResp: Failed to release sequency number. SubId: %v, Xid: %s", params.SubId, params.Xid)
 		return
 	}
@@ -526,19 +528,26 @@
 	}
 	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)
+	subs := c.registry.GetSubscription(payloadSeqNum)
+	if subs == nil {
+		xapp.Logger.Error("SubDelFail: Unknown payloadSeqNum. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
 		return
 	}
 
+	transaction := subs.GetTransaction()
+	if transaction == nil {
+		xapp.Logger.Error("SubDelFail: Unknown transaction. Dropping this msg. PayloadSeqNum: %v, SubId: %v", payloadSeqNum, params.SubId)
+		return
+	}
+
+	c.timerMap.StopTimer("RIC_SUB_DEL_REQ", int(payloadSeqNum))
+
+	responseReceived := transaction.CheckResponseReceived()
 	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)
+	xapp.Logger.Info("SubDelFail: SubId: %v, from address: %s. Forwarding response to xApp", payloadSeqNum, transaction.RmrEndpoint)
 
 	if transaction.ForwardRespToXapp == true {
 		var subDelRespPayload []byte
@@ -566,7 +575,7 @@
 	}
 
 	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}
+	subRouteAction := subs.SubRouteInfo(DELETE)
 	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)
@@ -574,13 +583,8 @@
 	}
 
 	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 {
+	transaction.Release()
+	if !c.registry.releaseSequenceNumber(payloadSeqNum) {
 		xapp.Logger.Error("SubDelFail: Failed to release sequency number. Err: %v, SubId: %v, Xid: %s", err, params.SubId, params.Xid)
 		return
 	}
@@ -591,12 +595,19 @@
 	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)
+	subs := c.registry.GetSubscription(subId)
+	if subs == nil {
+		xapp.Logger.Error("handleSubDelTimer: Unknown payloadSeqNum. Dropping this msg. SubId: %v", subId)
 		return
 	}
 
+	transaction := subs.GetTransaction()
+	if transaction == nil {
+		xapp.Logger.Error("handleSubDelTimer: Unknown transaction. Dropping this msg. SubId: %v", subId)
+		return
+	}
+
+	responseReceived := transaction.CheckResponseReceived()
 	if responseReceived == true {
 		// Subscription Delete Response or Failure already received
 		return
@@ -605,13 +616,10 @@
 	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)
+		transaction.RetryTransaction()
+
+		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)
 		}
@@ -624,7 +632,7 @@
 	var params xapp.RMRParams
 	if transaction.ForwardRespToXapp == true {
 		var subDelRespPayload []byte
-		subDelRespPayload, err = c.e2ap.PackSubscriptionDeleteResponse(transaction.OrigParams.Payload, subId)
+		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
@@ -649,21 +657,16 @@
 	}
 
 	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)
+	subRouteAction := subs.SubRouteInfo(DELETE)
+	err := c.rtmgrClient.SubscriptionRequestUpdate(subRouteAction)
 	if err != nil {
 		xapp.Logger.Error("handleSubDelTimer: Failed to update routing manager. Err: %v, SubId: %v, Xid: %s", err, subId, params.Xid)
 		return
 	}
 
 	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("handleSubDelTimer: Failed to delete transaction record. Err: %v, SubId: %v, Xid: %s", err, subId, params.Xid)
-			return
-		}
-	} else {
+	transaction.Release()
+	if !c.registry.releaseSequenceNumber(subId) {
 		xapp.Logger.Error("handleSubDelTimer: Failed to release sequency number. SubId: %v, Xid: %s", subId, params.Xid)
 	}
 	return
diff --git a/pkg/control/main_test.go b/pkg/control/main_test.go
index dc62225..69dadff 100644
--- a/pkg/control/main_test.go
+++ b/pkg/control/main_test.go
@@ -129,7 +129,6 @@
 type testingXappControl struct {
 	testingRmrControl
 	testingMessageChannel
-	meid    *xapp.RMRMeid
 	xid_seq uint64
 }
 
@@ -163,11 +162,10 @@
 	return
 }
 
-func createNewXappControl(desc string, rtfile string, port string, stat string, ranname string) *testingXappControl {
+func createNewXappControl(desc string, rtfile string, port string, stat string) *testingXappControl {
 	xappCtrl := &testingXappControl{}
 	xappCtrl.testingRmrControl = initTestingRmrControl(desc, rtfile, port, stat, xappCtrl)
 	xappCtrl.testingMessageChannel = initTestingMessageChannel()
-	xappCtrl.meid = &xapp.RMRMeid{RanName: ranname}
 	xappCtrl.xid_seq = 1
 	return xappCtrl
 }
@@ -330,7 +328,7 @@
 
 	xapprtfilename1, _ := testCreateTmpFile(xapprt1)
 	defer os.Remove(xapprtfilename1)
-	xappConn1 = createNewXappControl("xappConn1", xapprtfilename1, "13560", "RMRXAPP1STUB", "RAN_NAME_1")
+	xappConn1 = createNewXappControl("xappstub1", xapprtfilename1, "13560", "RMRXAPP1STUB")
 
 	//---------------------------------
 	//
@@ -350,7 +348,7 @@
 
 	xapprtfilename2, _ := testCreateTmpFile(xapprt2)
 	defer os.Remove(xapprtfilename2)
-	xappConn2 = createNewXappControl("xappConn2", xapprtfilename2, "13660", "RMRXAPP2STUB", "RAN_NAME_1")
+	xappConn2 = createNewXappControl("xappstub2", xapprtfilename2, "13660", "RMRXAPP2STUB")
 
 	//---------------------------------
 	//
@@ -369,7 +367,7 @@
 
 	e2termrtfilename, _ := testCreateTmpFile(e2termrt)
 	defer os.Remove(e2termrtfilename)
-	e2termConn = createNewE2termControl("e2termConn", e2termrtfilename, "15560", "RMRE2TERMSTUB")
+	e2termConn = createNewE2termControl("e2termstub", e2termrtfilename, "15560", "RMRE2TERMSTUB")
 
 	//---------------------------------
 	//
diff --git a/pkg/control/messaging_test.go b/pkg/control/messaging_test.go
index 3edf7a5..acd1544 100644
--- a/pkg/control/messaging_test.go
+++ b/pkg/control/messaging_test.go
@@ -395,7 +395,24 @@
 func (mc *testingMainControl) wait_subs_clean(t *testing.T, e2SubsId int, secs int) bool {
 	i := 1
 	for ; i <= secs*2; i++ {
-		if mc.c.registry.IsValidSequenceNumber(uint16(e2SubsId)) == false {
+		if mc.c.registry.GetSubscription(uint16(e2SubsId)) == nil {
+			return true
+		}
+		time.Sleep(500 * time.Millisecond)
+	}
+	testError(t, "(general) no clean within %d secs", secs)
+	return false
+}
+
+func (mc *testingMainControl) wait_subs_trans_clean(t *testing.T, e2SubsId int, secs int) bool {
+	i := 1
+	for ; i <= secs*2; i++ {
+		subs := mc.c.registry.GetSubscription(uint16(e2SubsId))
+		if subs == nil {
+			return true
+		}
+		trans := subs.GetTransaction()
+		if trans == nil {
 			return true
 		}
 		time.Sleep(500 * time.Millisecond)
@@ -563,6 +580,8 @@
 //     |              |------------->|
 //     |              |              |
 //     | SubDelReq    |              |
+//     | (same sub)   |              |
+//     | (same xid)   |              |
 //     |------------->|              |
 //     |              |              |
 //     |              |   SubDelResp |
@@ -597,6 +616,60 @@
 }
 
 //-----------------------------------------------------------------------------
+// TestSubDelReqCollision
+//
+//   stub                          stub
+// +-------+     +---------+    +---------+
+// | xapp  |     | submgr  |    | e2term  |
+// +-------+     +---------+    +---------+
+//     |              |              |
+//     |         [SUBS CREATE]       |
+//     |              |              |
+//     |              |              |
+//     | SubDelReq    |              |
+//     |------------->|              |
+//     |              |              |
+//     |              | SubDelReq    |
+//     |              |------------->|
+//     |              |              |
+//     | SubDelReq    |              |
+//     | (same sub)   |              |
+//     | (diff xid)   |              |
+//     |------------->|              |
+//     |              |              |
+//     |              |   SubDelResp |
+//     |              |<-------------|
+//     |              |              |
+//     |   SubDelResp |              |
+//     |<-------------|              |
+//
+//-----------------------------------------------------------------------------
+func TestSubDelReqCollision(t *testing.T) {
+	xapp.Logger.Info("TestSubDelReqCollision")
+
+	//Subs Create
+	cretrans := xappConn1.handle_xapp_subs_req(t, nil)
+	crereq, cremsg := e2termConn.handle_e2term_subs_req(t)
+	e2termConn.handle_e2term_subs_resp(t, crereq, cremsg)
+	e2SubsId := xappConn1.handle_xapp_subs_resp(t, cretrans)
+
+	//Subs Delete
+	deltrans := xappConn1.handle_xapp_subs_del_req(t, nil, e2SubsId)
+	delreq, delmsg := e2termConn.handle_e2term_subs_del_req(t)
+
+	seqBef := mainCtrl.get_msgcounter(t)
+	deltranscol := xappConn1.newXappTransaction(nil, "RAN_NAME_1")
+	xappConn1.handle_xapp_subs_del_req(t, deltranscol, e2SubsId) //Colliding SubDelReq
+	mainCtrl.wait_msgcounter_change(t, seqBef, 10)
+
+	e2termConn.handle_e2term_subs_del_resp(t, delreq, delmsg)
+	xappConn1.handle_xapp_subs_del_resp(t, deltrans)
+
+	//Wait that subs is cleaned
+	mainCtrl.wait_subs_clean(t, e2SubsId, 10)
+}
+
+//-----------------------------------------------------------------------------
 // TestSubReqAndSubDelOkTwoParallel
 //
 //   stub                          stub
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
index 3d70d63..1adb4f7 100644
--- a/pkg/control/registry.go
+++ b/pkg/control/registry.go
@@ -21,26 +21,82 @@
 
 import (
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+	"strconv"
 	"sync"
 )
 
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
 type Subscription struct {
+	mutex  sync.Mutex
 	Seq    uint16
 	Active bool
+	//
+	Meid        *xapp.RMRMeid
+	RmrEndpoint // xapp endpoint
+	Trans       *Transaction
+}
+
+func (s *Subscription) String() string {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
+	return strconv.FormatUint(uint64(s.Seq), 10) + "/" + s.RmrEndpoint.String() + "/" + s.Meid.RanName
 }
 
 func (s *Subscription) Confirmed() {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
 	s.Active = true
 }
 
 func (s *Subscription) UnConfirmed() {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
 	s.Active = false
 }
 
 func (s *Subscription) IsConfirmed() bool {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
 	return s.Active
 }
 
+func (s *Subscription) SetTransaction(trans *Transaction) bool {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
+	if s.Trans == nil {
+		s.Trans = trans
+		return true
+	}
+	return false
+}
+
+func (s *Subscription) UnSetTransaction(trans *Transaction) bool {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
+	if trans == nil || trans == s.Trans {
+		s.Trans = nil
+		return true
+	}
+	return false
+}
+
+func (s *Subscription) GetTransaction() *Transaction {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
+	return s.Trans
+}
+
+func (s *Subscription) SubRouteInfo(act Action) SubRouteInfo {
+	s.mutex.Lock()
+	defer s.mutex.Unlock()
+	return SubRouteInfo{act, s.RmrEndpoint.Addr, s.RmrEndpoint.Port, s.Seq}
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
 type Registry struct {
 	register map[uint16]*Subscription
 	counter  uint16
@@ -54,7 +110,7 @@
 }
 
 // Reserves and returns the next free sequence number
-func (r *Registry) ReserveSubscription() *Subscription {
+func (r *Registry) ReserveSubscription(endPoint RmrEndpoint, meid *xapp.RMRMeid) *Subscription {
 	// Check is current SequenceNumber valid
 	// Allocate next SequenceNumber value and retry N times
 	r.mutex.Lock()
@@ -69,14 +125,19 @@
 			r.counter++
 		}
 		if _, ok := r.register[sequenceNumber]; ok == false {
-			r.register[sequenceNumber] = &Subscription{sequenceNumber, false}
+			r.register[sequenceNumber] = &Subscription{
+				Seq:         sequenceNumber,
+				Active:      false,
+				RmrEndpoint: endPoint,
+				Meid:        meid,
+				Trans:       nil,
+			}
 			return r.register[sequenceNumber]
 		}
 	}
 	return nil
 }
 
-// This function checks the validity of the given subscription id
 func (r *Registry) GetSubscription(sn uint16) *Subscription {
 	r.mutex.Lock()
 	defer r.mutex.Unlock()
@@ -87,31 +148,6 @@
 	return nil
 }
 
-// This function checks the validity of the given subscription id
-func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
-	r.mutex.Lock()
-	defer r.mutex.Unlock()
-	xapp.Logger.Debug("Registry map: %v", r.register)
-	if _, ok := r.register[sn]; ok {
-		return true
-	}
-	return false
-}
-
-// This function sets the give id as confirmed in the register
-func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
-	r.mutex.Lock()
-	defer r.mutex.Unlock()
-	r.register[sn].Confirmed()
-}
-
-//This function sets the given id as unused in the register
-func (r *Registry) setSubscriptionToUnConfirmed(sn uint16) {
-	r.mutex.Lock()
-	defer r.mutex.Unlock()
-	r.register[sn].UnConfirmed()
-}
-
 //This function releases the given id as unused in the register
 func (r *Registry) releaseSequenceNumber(sn uint16) bool {
 	r.mutex.Lock()
diff --git a/pkg/control/tracker.go b/pkg/control/tracker.go
index 9287ea8..0e6941d 100644
--- a/pkg/control/tracker.go
+++ b/pkg/control/tracker.go
@@ -22,138 +22,132 @@
 import (
 	"fmt"
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+	"strconv"
 	"sync"
 )
 
-type TransactionKey struct {
-	SubID     uint16 // subscription id / sequence number
-	TransType Action // action ongoing (CREATE/DELETE etc)
-}
-
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
 type TransactionXappKey struct {
 	RmrEndpoint
 	Xid string // xapp xid in req
 }
 
+func (key *TransactionXappKey) String() string {
+	return key.RmrEndpoint.String() + "/" + key.Xid
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
 type Transaction struct {
-	tracker           *Tracker           // tracker instance
-	Key               TransactionKey     // action key
-	Xappkey           TransactionXappKey // transaction key
-	OrigParams        *xapp.RMRParams    // request orginal params
+	tracker           *Tracker // tracker instance
+	Subs              *Subscription
+	RmrEndpoint       RmrEndpoint
+	Xid               string          // xapp xid in req
+	OrigParams        *xapp.RMRParams // request orginal params
 	RespReceived      bool
 	ForwardRespToXapp bool
+	mutex             sync.Mutex
 }
 
-func (t *Transaction) SubRouteInfo() SubRouteInfo {
-	return SubRouteInfo{t.Key.TransType, t.Xappkey.RmrEndpoint.Addr, t.Xappkey.RmrEndpoint.Port, t.Key.SubID}
+func (t *Transaction) String() string {
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
+	var subId string = "?"
+	if t.Subs != nil {
+		subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
+	}
+	return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid
 }
 
-/*
-Implements a record of ongoing transactions and helper functions to CRUD the records.
-*/
+func (t *Transaction) CheckResponseReceived() bool {
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
+	if t.RespReceived == false {
+		t.RespReceived = true
+		return false
+	}
+	return true
+}
+
+func (t *Transaction) RetryTransaction() {
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
+	t.RespReceived = false
+}
+
+func (t *Transaction) Release() {
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
+	if t.Subs != nil {
+		t.Subs.UnSetTransaction(t)
+	}
+	if t.tracker != nil {
+		xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
+		t.tracker.UnTrackTransaction(xappkey)
+	}
+	t.Subs = nil
+	t.tracker = nil
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
 type Tracker struct {
-	transactionTable     map[TransactionKey]*Transaction
 	transactionXappTable map[TransactionXappKey]*Transaction
 	mutex                sync.Mutex
 }
 
 func (t *Tracker) Init() {
-	t.transactionTable = make(map[TransactionKey]*Transaction)
 	t.transactionXappTable = make(map[TransactionXappKey]*Transaction)
 }
 
-/*
-Checks if a tranascation with similar type has been ongoing. If not then creates one.
-Returns error if there is similar transatcion ongoing.
-*/
-func (t *Tracker) TrackTransaction(subID uint16, act Action, addr string, port uint16, params *xapp.RMRParams, respReceived bool, forwardRespToXapp bool) (*Transaction, error) {
-	key := TransactionKey{subID, act}
-	endpoint := RmrEndpoint{addr, port}
-	xappkey := TransactionXappKey{endpoint, params.Xid}
-	trans := &Transaction{t, key, xappkey, params, respReceived, forwardRespToXapp}
+func (t *Tracker) TrackTransaction(subs *Subscription, endpoint RmrEndpoint, params *xapp.RMRParams, respReceived bool, forwardRespToXapp bool) (*Transaction, error) {
+
+	trans := &Transaction{
+		tracker:           nil,
+		Subs:              nil,
+		RmrEndpoint:       endpoint,
+		Xid:               params.Xid,
+		OrigParams:        params,
+		RespReceived:      respReceived,
+		ForwardRespToXapp: forwardRespToXapp,
+	}
+
 	t.mutex.Lock()
 	defer t.mutex.Unlock()
-	if _, ok := t.transactionTable[key]; ok {
-		// TODO: Implement merge related check here. If the key is same but the value is different.
-		err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.TransType)
-		return nil, err
-	}
+
+	xappkey := TransactionXappKey{endpoint, params.Xid}
 	if _, ok := t.transactionXappTable[xappkey]; ok {
-		// TODO: Implement merge related check here. If the key is same but the value is different.
-		err := fmt.Errorf("transaction tracker: Similar transaction with xapp key %v is ongoing", xappkey)
+		err := fmt.Errorf("Tracker: Similar transaction with xappkey %s is ongoing, transaction %s not created ", xappkey, trans)
 		return nil, err
 	}
-	t.transactionTable[key] = trans
+
+	if subs.SetTransaction(trans) == false {
+		othTrans := subs.GetTransaction()
+		err := fmt.Errorf("Tracker: Subscription %s got already transaction ongoing: %s, transaction %s not created", subs, othTrans, trans)
+		return nil, err
+	}
+	trans.Subs = subs
+	if (trans.Subs.RmrEndpoint.Addr != trans.RmrEndpoint.Addr) || (trans.Subs.RmrEndpoint.Port != trans.RmrEndpoint.Port) {
+		err := fmt.Errorf("Tracker: Subscription endpoint %s mismatch with trans: %s", subs, trans)
+		trans.Subs.UnSetTransaction(nil)
+		return nil, err
+	}
+
+	trans.tracker = t
 	t.transactionXappTable[xappkey] = trans
 	return trans, nil
 }
 
-/*
-Retreives the transaction table entry for the given request. Controls that only one response is sent to xapp.
-Returns error in case the transaction cannot be found.
-*/
-func (t *Tracker) RetriveTransaction(subID uint16, act Action) (*Transaction, error) {
-	key := TransactionKey{subID, act}
+func (t *Tracker) UnTrackTransaction(xappKey TransactionXappKey) (*Transaction, error) {
 	t.mutex.Lock()
 	defer t.mutex.Unlock()
-	if trans, ok := t.transactionTable[key]; ok {
+	if trans, ok2 := t.transactionXappTable[xappKey]; ok2 {
+		delete(t.transactionXappTable, xappKey)
 		return trans, nil
 	}
-	err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
-	return nil, err
-}
-
-/*
-Deletes the transaction table entry for the given request and returns the deleted xapp's address and port for reference.
-Returns error in case the transaction cannot be found.
-*/
-func (t *Tracker) completeTransaction(subID uint16, act Action) (*Transaction, error) {
-	key := TransactionKey{subID, act}
-	t.mutex.Lock()
-	defer t.mutex.Unlock()
-	if trans, ok1 := t.transactionTable[key]; ok1 {
-		if _, ok2 := t.transactionXappTable[trans.Xappkey]; ok2 {
-			delete(t.transactionXappTable, trans.Xappkey)
-		}
-		delete(t.transactionTable, key)
-		return trans, nil
-	}
-	err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
-	return nil, err
-}
-
-/*
-Makes possible to to detect has response already received from BTS
-Returns error in case the transaction cannot be found.
-*/
-func (t *Tracker) CheckResponseReceived(subID uint16, act Action) (*Transaction, bool, error) {
-	key := TransactionKey{subID, act}
-	t.mutex.Lock()
-	defer t.mutex.Unlock()
-	if trans, ok := t.transactionTable[key]; ok {
-		if trans.RespReceived == false {
-			trans.RespReceived = true
-			// This is used to control that only one response action (success response, failure or timer) is excecuted for the transaction
-			return trans, false, nil
-		}
-		return trans, true, nil
-	}
-	err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
-	return nil, false, err
-}
-
-/*
-Makes possible to receive response to retransmitted request to BTS
-Returns error in case the transaction cannot be found.
-*/
-func (t *Tracker) RetryTransaction(subID uint16, act Action) error {
-	key := TransactionKey{subID, act}
-	t.mutex.Lock()
-	defer t.mutex.Unlock()
-	if trans, ok := t.transactionTable[key]; ok {
-		trans.RespReceived = false
-		return nil
-	}
-	err := fmt.Errorf("transaction record for Subscription ID %d and action %s does not exist", subID, act)
-	return err
+	return nil, fmt.Errorf("Tracker: No record for xappkey %s", xappKey)
 }
diff --git a/pkg/control/types.go b/pkg/control/types.go
index febd41a..64fd15f 100644
--- a/pkg/control/types.go
+++ b/pkg/control/types.go
@@ -19,6 +19,10 @@
 
 package control
 
+import (
+	"strconv"
+)
+
 type RmrDatagram struct {
 	MessageType    int
 	SubscriptionId uint16
@@ -36,4 +40,9 @@
 	Addr string // xapp addr
 	Port uint16 // xapp port
 }
+
+func (endpoint RmrEndpoint) String() string {
+	return endpoint.Addr + ":" + strconv.FormatUint(uint64(endpoint.Port), 10)
+}
+
 type Action int