[VOL-5284] resetFsm enters race during simultaneous delete device and device down ind

Change-Id: I451d48650702ab901a03a14fa4a965939f2c8250
Signed-off-by: Sridhar Ravindra <sridhar.ravindra@radisys.com>
diff --git a/internal/pkg/core/device_handler.go b/internal/pkg/core/device_handler.go
index c4dd037..929d117 100755
--- a/internal/pkg/core/device_handler.go
+++ b/internal/pkg/core/device_handler.go
@@ -2290,16 +2290,31 @@
 			}
 		}
 	}
-	if dh.GetCollectorIsRunning() {
+
+	dh.mutexCollectorFlag.Lock()
+	logger.Debugw(ctx, "check-collector-is-running", log.Fields{"device-id": dh.device.Id, "flag": dh.collectorIsRunning})
+	if dh.collectorIsRunning {
 		// Stop collector routine
 		dh.stopCollector <- true
+		dh.collectorIsRunning = false
 	}
-	if dh.GetAlarmManagerIsRunning(ctx) {
+	dh.mutexCollectorFlag.Unlock()
+
+	dh.mutextAlarmManagerFlag.Lock()
+	logger.Debugw(ctx, "check-alarm-manager-is-running", log.Fields{"device-id": dh.device.Id, "flag": dh.alarmManagerIsRunning})
+	if dh.alarmManagerIsRunning {
 		dh.stopAlarmManager <- true
+		dh.alarmManagerIsRunning = false
 	}
-	if dh.pSelfTestHdlr.GetSelfTestHandlerIsRunning() {
+	dh.mutextAlarmManagerFlag.Unlock()
+
+	dh.pSelfTestHdlr.SelfTestHandlerLock.Lock()
+	logger.Debugw(ctx, "check-self-test-control-block-is-running", log.Fields{"device-id": dh.device.Id, "flag": dh.pSelfTestHdlr.SelfTestHandlerActive})
+	if dh.pSelfTestHdlr.SelfTestHandlerActive {
 		dh.pSelfTestHdlr.StopSelfTestModule <- true
+		dh.pSelfTestHdlr.SelfTestHandlerActive = false
 	}
+	dh.pSelfTestHdlr.SelfTestHandlerLock.Unlock()
 
 	// Note: We want flow deletes to be processed on onu down, so do not stop flow monitoring routines
 
@@ -3774,7 +3789,6 @@
 
 		select {
 		case <-dh.stopCollector:
-			dh.setCollectorIsRunning(false)
 			logger.Debugw(ctx, "stopping-collector-for-onu", log.Fields{"device-id": dh.device.Id})
 			// Stop the L2 PM FSM
 			go func() {
@@ -4058,8 +4072,7 @@
 	go dh.pAlarmMgr.StartOMCIAlarmMessageProcessing(ctx)
 	dh.setAlarmManagerIsRunning(true)
 	if stop := <-dh.stopAlarmManager; stop {
-		logger.Debugw(ctx, "stopping-collector-for-onu", log.Fields{"device-id": dh.device.Id})
-		dh.setAlarmManagerIsRunning(false)
+		logger.Debugw(ctx, "stopping-alarm-manager-for-onu", log.Fields{"device-id": dh.device.Id})
 		go func() {
 			if dh.pAlarmMgr.AlarmSyncFsm != nil && dh.pAlarmMgr.AlarmSyncFsm.PFsm != nil {
 				_ = dh.pAlarmMgr.AlarmSyncFsm.PFsm.Event(almgr.AsEvStop)
diff --git a/internal/pkg/omcitst/omci_self_test_handler.go b/internal/pkg/omcitst/omci_self_test_handler.go
index f8def86..00a1eee 100755
--- a/internal/pkg/omcitst/omci_self_test_handler.go
+++ b/internal/pkg/omcitst/omci_self_test_handler.go
@@ -69,8 +69,8 @@
 	selfTestFsmMap  map[generated.ClassID]*fsmCb // The fsmCb is indexed by ME Class ID of the Test Action procedure
 	selfTestFsmLock sync.RWMutex
 
-	selfTestHandlerLock   sync.RWMutex
-	selfTestHandlerActive bool
+	SelfTestHandlerLock   sync.RWMutex
+	SelfTestHandlerActive bool
 	StopSelfTestModule    chan bool
 }
 
@@ -359,7 +359,6 @@
 func (selfTestCb *SelfTestControlBlock) waitForStopSelfTestModuleSignal(ctx context.Context) {
 	selfTestCb.SetSelfTestHandlerIsRunning(true)
 	<-selfTestCb.StopSelfTestModule // block on stop signal
-	selfTestCb.SetSelfTestHandlerIsRunning(false)
 
 	logger.Infow(ctx, "received stop signal - clean up start", log.Fields{"device-id": selfTestCb.deviceID})
 	selfTestCb.selfTestFsmLock.Lock()
@@ -381,16 +380,16 @@
 
 // SetSelfTestHandlerIsRunning sets the value to selfTestCb.selfTestHandlerActive
 func (selfTestCb *SelfTestControlBlock) SetSelfTestHandlerIsRunning(active bool) {
-	selfTestCb.selfTestHandlerLock.Lock()
-	defer selfTestCb.selfTestHandlerLock.Unlock()
-	selfTestCb.selfTestHandlerActive = active
+	selfTestCb.SelfTestHandlerLock.Lock()
+	defer selfTestCb.SelfTestHandlerLock.Unlock()
+	selfTestCb.SelfTestHandlerActive = active
 }
 
 // GetSelfTestHandlerIsRunning gets selfTestCb.selfTestHandlerActive
 func (selfTestCb *SelfTestControlBlock) GetSelfTestHandlerIsRunning() bool {
-	selfTestCb.selfTestHandlerLock.RLock()
-	defer selfTestCb.selfTestHandlerLock.RUnlock()
-	return selfTestCb.selfTestHandlerActive
+	selfTestCb.SelfTestHandlerLock.RLock()
+	defer selfTestCb.SelfTestHandlerLock.RUnlock()
+	return selfTestCb.SelfTestHandlerActive
 }
 
 // SelfTestRequestStart initiate Test Request handling procedure. The results are asynchronously conveyed on the respChan.