VOL-4613: Sometimes the ONUs are stuck in ACTIVATING state after
Disable and Enable of the OLT

Change-Id: Ib1dd46ca5239206319ad910d36038868eaced9fa
diff --git a/internal/pkg/mib/mib_sync.go b/internal/pkg/mib/mib_sync.go
index 9b8196b..7f33fc0 100755
--- a/internal/pkg/mib/mib_sync.go
+++ b/internal/pkg/mib/mib_sync.go
@@ -473,6 +473,9 @@
 
 func (oo *OnuDeviceEntry) processMibSyncMessages(ctx context.Context) {
 	logger.Debugw(ctx, "MibSync Msg", log.Fields{"Start routine to process OMCI-messages for device-id": oo.deviceID})
+	oo.mutexMibSyncMsgProcessorRunning.Lock()
+	oo.mibSyncMsgProcessorRunning = true
+	oo.mutexMibSyncMsgProcessorRunning.Unlock()
 loop:
 	for {
 		// case <-ctx.Done():
@@ -481,6 +484,9 @@
 		message, ok := <-oo.PMibUploadFsm.CommChan
 		if !ok {
 			logger.Info(ctx, "MibSync Msg", log.Fields{"Message couldn't be read from channel for device-id": oo.deviceID})
+			oo.mutexMibSyncMsgProcessorRunning.Lock()
+			oo.mibSyncMsgProcessorRunning = false
+			oo.mutexMibSyncMsgProcessorRunning.Unlock()
 			break loop
 		}
 		logger.Debugw(ctx, "MibSync Msg", log.Fields{"Received message on ONU MibSyncChan for device-id": oo.deviceID})
@@ -490,6 +496,9 @@
 			msg, _ := message.Data.(cmn.TestMessage)
 			if msg.TestMessageVal == cmn.AbortMessageProcessing {
 				logger.Debugw(ctx, "MibSync Msg abort ProcessMsg", log.Fields{"for device-id": oo.deviceID})
+				oo.mutexMibSyncMsgProcessorRunning.Lock()
+				oo.mibSyncMsgProcessorRunning = false
+				oo.mutexMibSyncMsgProcessorRunning.Unlock()
 				break loop
 			}
 			oo.handleTestMsg(ctx, msg)
@@ -1211,16 +1220,20 @@
 	}
 	//the MibSync FSM might be active all the ONU-active time,
 	// hence it must be stopped unconditionally
-	pMibUlFsm := oo.PMibUploadFsm
-	if pMibUlFsm != nil {
-		// abort running message processing
-		fsmAbortMsg := cmn.Message{
-			Type: cmn.TestMsg,
-			Data: cmn.TestMessage{
-				TestMessageVal: cmn.AbortMessageProcessing,
-			},
+	oo.mutexMibSyncMsgProcessorRunning.RLock()
+	defer oo.mutexMibSyncMsgProcessorRunning.RUnlock()
+	if oo.mibSyncMsgProcessorRunning {
+		pMibUlFsm := oo.PMibUploadFsm
+		if pMibUlFsm != nil {
+			// abort running message processing
+			fsmAbortMsg := cmn.Message{
+				Type: cmn.TestMsg,
+				Data: cmn.TestMessage{
+					TestMessageVal: cmn.AbortMessageProcessing,
+				},
+			}
+			pMibUlFsm.CommChan <- fsmAbortMsg
+			_ = pMibUlFsm.PFsm.Event(UlEvStop)
 		}
-		pMibUlFsm.CommChan <- fsmAbortMsg
-		_ = pMibUlFsm.PFsm.Event(UlEvStop)
 	}
 }
diff --git a/internal/pkg/mib/onu_device_entry.go b/internal/pkg/mib/onu_device_entry.go
index 47c7b90..ea47bbf 100755
--- a/internal/pkg/mib/onu_device_entry.go
+++ b/internal/pkg/mib/onu_device_entry.go
@@ -191,9 +191,11 @@
 	//mibNextDbResync uint32
 
 	// for mibUpload
-	PMibUploadFsm          *cmn.AdapterFsm //could be handled dynamically and more general as pcmn.AdapterFsm - perhaps later
-	mutexLastTxParamStruct sync.RWMutex
-	lastTxParamStruct      sLastTxMeParameter
+	PMibUploadFsm                   *cmn.AdapterFsm //could be handled dynamically and more general as pcmn.AdapterFsm - perhaps later
+	mutexLastTxParamStruct          sync.RWMutex
+	lastTxParamStruct               sLastTxMeParameter
+	mibSyncMsgProcessorRunning      bool
+	mutexMibSyncMsgProcessorRunning sync.RWMutex
 	// for mibDownload
 	PMibDownloadFsm *cmn.AdapterFsm //could be handled dynamically and more general as pcmn.AdapterFsm - perhaps later
 	//remark: general usage of pAdapterFsm would require generalization of CommChan  usage and internal event setting