Fixes for subscription merge release cases
 - Code fixes
 - UT cases fixes and improvements

Change-Id: I97c295d51029888b41ae0dacac76d366eaeb3c54
Signed-off-by: Anssi Mannila <anssi.mannila@nokia.com>
diff --git a/pkg/control/control.go b/pkg/control/control.go
index 4c09ee2..68688e2 100755
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -25,6 +25,7 @@
 	"os"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 
 	"gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap"
@@ -213,6 +214,8 @@
 //
 //-------------------------------------------------------------------
 func (c *Control) ReadRESTSubscriptions() error {
+
+	xapp.Logger.Debug("ReadRESTSubscriptions()")
 	var err error
 	var restSubscriptions map[string]*RESTSubscription
 	for i := 0; dbRetryForever == "true" || i < dbTryCount; i++ {
@@ -222,6 +225,12 @@
 			xapp.Logger.Error("%v", err)
 			<-time.After(1 * time.Second)
 		} else {
+			// Fix REST subscriptions ongoing status after restart
+			for restSubId, restSubscription := range restSubscriptions {
+				restSubscription.SubReqOngoing = false
+				restSubscription.SubDelReqOngoing = false
+				c.WriteRESTSubscriptionToSdl(restSubId, restSubscription)
+			}
 			c.registry.restSubscriptions = restSubscriptions
 			return nil
 		}
@@ -522,7 +531,7 @@
 	clientEndpoint *models.SubscriptionParamsClientEndpoint, meid *string, restSubId *string, xAppRmrEndpoint string, md5sum string, e2SubscriptionDirectives *E2SubscriptionDirectives) {
 
 	c.SubscriptionProcessingStartDelay()
-	xapp.Logger.Debug("E2 SubscriptionRequest count =%v ", len(subReqList.E2APSubscriptionRequests))
+	xapp.Logger.Debug("E2 SubscriptionRequest count = %v ", len(subReqList.E2APSubscriptionRequests))
 
 	var xAppEventInstanceID int64
 	var e2EventInstanceID int64
@@ -551,6 +560,10 @@
 		trans.Release()
 
 		if err != nil {
+			if err.Error() == "TEST: restart event received" {
+				// This is just for UT cases. Stop here subscription processing
+				return
+			}
 			c.sendUnsuccesfullResponseNotification(restSubId, restSubscription, xAppEventInstanceID, err, clientEndpoint, trans, errorInfo)
 		} else {
 			e2EventInstanceID = (int64)(subRespMsg.RequestId.InstanceId)
@@ -601,7 +614,7 @@
 	// Wake subs request
 	//
 	subs.OngoingReqCount++
-	go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives)
+	go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives, 0)
 	event, _ := trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
 	subs.OngoingReqCount--
 
@@ -627,6 +640,10 @@
 		case *SDLWriteErrortEvent:
 			err = fmt.Errorf("SDL write failure")
 			errorInfo = themsg.ErrorInfo
+		case *SubmgrRestartTestEvent:
+			err = fmt.Errorf("TEST: restart event received")
+			xapp.Logger.Debug("%s", err)
+			return nil, &errorInfo, err
 		default:
 			err = fmt.Errorf("Unexpected E2 subscription response received")
 			errorInfo.SetInfo(err.Error(), models.SubscriptionInstanceErrorSourceE2Node, "")
@@ -759,7 +776,7 @@
 	go func() {
 		xapp.Logger.Debug("Deleteting handler: processing instances = %v", restSubscription.InstanceIds)
 		for _, instanceId := range restSubscription.InstanceIds {
-			xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId)
+			xAppEventInstanceID, err := c.SubscriptionDeleteHandler(&restSubId, &xAppRmrEndPoint, &restSubscription.Meid, instanceId, 0)
 
 			if err != nil {
 				xapp.Logger.Error("%s", err.Error())
@@ -780,7 +797,7 @@
 //-------------------------------------------------------------------
 //
 //-------------------------------------------------------------------
-func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32) (int64, error) {
+func (c *Control) SubscriptionDeleteHandler(restSubId *string, endPoint *string, meid *string, instanceId uint32, waitRouteCleanupTime time.Duration) (int64, error) {
 
 	var xAppEventInstanceID int64
 	subs, err := c.registry.GetSubscriptionFirstMatch([]uint32{instanceId})
@@ -808,7 +825,7 @@
 	// Wake subs delete
 	//
 	subs.OngoingDelCount++
-	go c.handleSubscriptionDelete(subs, trans)
+	go c.handleSubscriptionDelete(subs, trans, waitRouteCleanupTime)
 	trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
 	subs.OngoingDelCount--
 
@@ -990,7 +1007,7 @@
 
 	e2SubscriptionDirectives, _ := c.GetE2SubscriptionDirectives(nil)
 	subs.OngoingReqCount++
-	go c.handleSubscriptionCreate(subs, trans, e2SubscriptionDirectives)
+	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
@@ -1061,7 +1078,7 @@
 	// Wake subs delete
 	//
 	subs.OngoingDelCount++
-	go c.handleSubscriptionDelete(subs, trans)
+	go c.handleSubscriptionDelete(subs, trans, waitRouteCleanup_ms)
 	trans.WaitEvent(0) //blocked wait as timeout is handled in subs side
 	subs.OngoingDelCount--
 
@@ -1091,7 +1108,7 @@
 //-------------------------------------------------------------------
 // SUBS CREATE Handling
 //-------------------------------------------------------------------
-func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives) {
+func (c *Control) handleSubscriptionCreate(subs *Subscription, parentTrans *TransactionXapp, e2SubscriptionDirectives *E2SubscriptionDirectives, waitRouteCleanupTime time.Duration) {
 
 	var event interface{} = nil
 	var removeSubscriptionFromDb bool = false
@@ -1110,38 +1127,43 @@
 			subRfMsg, valid = subs.SetCachedResponse(event, true)
 			subs.SubRespRcvd = true
 		case *e2ap.E2APSubscriptionFailure:
-			removeSubscriptionFromDb = true
 			subRfMsg, valid = subs.SetCachedResponse(event, false)
 			xapp.Logger.Debug("SUBS-SubReq: internal delete due failure event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
 		case *SubmgrRestartTestEvent:
-			// This simulates that no response has been received and after restart subscriptions are restored from db
+			// This is used to simulate that no response has been received and after restart, subscriptions are restored from db
 			xapp.Logger.Debug("Test restart flag is active. Dropping this transaction to test restart case")
+			subRfMsg, valid = subs.SetCachedResponse(event, false)
+			parentTrans.SendEvent(subRfMsg, 0)
+			return
 		case *PackSubscriptionRequestErrortEvent, *SDLWriteErrortEvent:
-			removeSubscriptionFromDb = true
 			subRfMsg, valid = subs.SetCachedResponse(event, false)
 		default:
 			// Timer expiry
 			if subs.PolicyUpdate == false {
 				xapp.Logger.Debug("SUBS-SubReq: internal delete due default event(%s) %s", typeofSubsMessage(event), idstring(nil, trans, subs, parentTrans))
-				removeSubscriptionFromDb = true
 				subRfMsg, valid = subs.SetCachedResponse(nil, false)
 				c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
+			} else {
+				subRfMsg, valid = subs.SetCachedResponse(nil, true)
 			}
 		}
 		xapp.Logger.Debug("SUBS-SubReq: Handling (e2t response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
 	} else {
 		xapp.Logger.Debug("SUBS-SubReq: Handling (cached response %s) %s", typeofSubsMessage(subRfMsg), idstring(nil, trans, subs, parentTrans))
 	}
+	if valid == false {
+		removeSubscriptionFromDb = true
+	}
 
 	err := c.UpdateSubscriptionInDB(subs, removeSubscriptionFromDb)
 	if err != nil {
-		subRfMsg, valid = subs.SetCachedResponse(event, false)
+		valid = false
 		c.sendE2TSubscriptionDeleteRequest(subs, trans, parentTrans)
 	}
 
 	//Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
 	if valid == false {
-		c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
+		c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
 	}
 
 	parentTrans.SendEvent(subRfMsg, 0)
@@ -1151,7 +1173,7 @@
 // SUBS DELETE Handling
 //-------------------------------------------------------------------
 
-func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp) {
+func (c *Control) handleSubscriptionDelete(subs *Subscription, parentTrans *TransactionXapp, waitRouteCleanupTime time.Duration) {
 
 	trans := c.tracker.NewSubsTransaction(subs)
 	subs.WaitTransactionTurn(trans)
@@ -1172,8 +1194,7 @@
 	//Now RemoveFromSubscription in here to avoid race conditions (mostly concerns delete)
 	//  If parallel deletes ongoing both might pass earlier sendE2TSubscriptionDeleteRequest(...) if
 	//  RemoveFromSubscription locates in caller side (now in handleXAPPSubscriptionDeleteRequest(...))
-	c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanup_ms, c)
-	c.registry.UpdateSubscriptionToDb(subs, c)
+	c.registry.RemoveFromSubscription(subs, parentTrans, waitRouteCleanupTime, c)
 	parentTrans.SendEvent(nil, 0)
 }
 
@@ -1298,6 +1319,7 @@
 		xapp.Logger.Error("MSG-SubResp: %s", idstring(err, params, subs))
 		return
 	}
+	xapp.Logger.Debug("SUBS-SubResp: Sending event, trans= %v", trans)
 	sendOk, timedOut := trans.SendEvent(subRespMsg, e2tRecvMsgTimeout)
 	if sendOk == false {
 		err = fmt.Errorf("Passing event to transaction failed: sendOk(%t) timedOut(%t)", sendOk, timedOut)
@@ -1501,6 +1523,11 @@
 
 func (c *Control) SendSubscriptionDeleteReq(subs *Subscription) {
 
+	if c.UTTesting == true {
+		// Reqistry mutex is not locked after real restart but it can be when restart is simulated in unit tests
+		c.registry.mutex = new(sync.Mutex)
+	}
+
 	const ricRequestorId = 123
 	xapp.Logger.Debug("Sending subscription delete due to restart. subId = %v", subs.ReqId.InstanceId)