Check processOmciMessages and generateTicks routines are active before
closing respective channels (stopProcessingOmciResponses and stopTicks).

Change-Id: Iaaab422f175f634d36d3ec095b8802c3c1c2634e
diff --git a/VERSION b/VERSION
index c35cbf3..927ac59 100755
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.2.7-dev183
+1.2.7-dev184
diff --git a/internal/pkg/onuadaptercore/device_handler.go b/internal/pkg/onuadaptercore/device_handler.go
index 0f067d0..ac04031 100644
--- a/internal/pkg/onuadaptercore/device_handler.go
+++ b/internal/pkg/onuadaptercore/device_handler.go
@@ -1810,6 +1810,14 @@
 	}
 	_ = dh.deviceReasonUpdate(ctx, drInitialMibDownloaded, !dh.isReconciling())
 
+	if !dh.getCollectorIsRunning() {
+		// Start PM collector routine
+		go dh.startCollector(ctx)
+	}
+	if !dh.getAlarmManagerIsRunning(ctx) {
+		go dh.startAlarmManager(ctx)
+	}
+
 	// Initialize classical L2 PM Interval Counters
 	if err := dh.pOnuMetricsMgr.pAdaptFsm.pFsm.Event(l2PmEventInit); err != nil {
 		// There is no way we should be landing here, but if we do then
@@ -2961,9 +2969,12 @@
 					logger.Errorw(ctx, "metrics manager fsm not initialized", log.Fields{"device-id": dh.deviceID})
 				}
 			}()
-
-			dh.pOnuMetricsMgr.stopProcessingOmciResponses <- true // Stop the OMCI GET response processing routine
-			dh.pOnuMetricsMgr.stopTicks <- true
+			if dh.pOnuMetricsMgr.getOmciProcessingStatus() {
+				dh.pOnuMetricsMgr.stopProcessingOmciResponses <- true // Stop the OMCI GET response processing routine
+			}
+			if dh.pOnuMetricsMgr.getTickGenerationStatus() {
+				dh.pOnuMetricsMgr.stopTicks <- true
+			}
 
 			return
 		case <-time.After(time.Duration(FrequencyGranularity) * time.Second): // Check every FrequencyGranularity to see if it is time for collecting metrics
diff --git a/internal/pkg/onuadaptercore/onu_metrics_manager.go b/internal/pkg/onuadaptercore/onu_metrics_manager.go
index 984f8ab..7d2e787 100644
--- a/internal/pkg/onuadaptercore/onu_metrics_manager.go
+++ b/internal/pkg/onuadaptercore/onu_metrics_manager.go
@@ -271,8 +271,10 @@
 	standaloneMetricMap map[string]*standaloneMetric
 
 	stopProcessingOmciResponses chan bool
+	omciProcessingActive        bool
 
-	stopTicks chan bool
+	stopTicks            chan bool
+	tickGenerationActive bool
 
 	nextGlobalMetricCollectionTime time.Time // valid only if pmConfig.FreqOverride is set to false.
 
@@ -916,11 +918,12 @@
 	// The processOmciMessages routine will get stopped if startCollector routine (in device_handler.go)
 	// is stopped - as a result of ONU going down.
 	mm.flushMetricCollectionChannels(ctx)
-
+	mm.updateOmciProcessingStatus(true)
 	for {
 		select {
 		case <-mm.stopProcessingOmciResponses: // stop this routine
 			logger.Infow(ctx, "Stop routine to process OMCI-GET messages for device-id", log.Fields{"device-id": mm.pDeviceHandler.deviceID})
+			mm.updateOmciProcessingStatus(false)
 			return
 		case message, ok := <-mm.pAdaptFsm.commChan:
 			if !ok {
@@ -1558,7 +1561,7 @@
 
 	select {
 	case <-time.After(time.Duration(mm.pDeviceHandler.pOpenOnuAc.omciTimeout) * time.Second):
-		logger.Errorf(ctx, "timed out waiting for sync time response from onu", log.Fields{"device-id": mm.pDeviceHandler.deviceID})
+		logger.Errorw(ctx, "timed out waiting for sync time response from onu", log.Fields{"device-id": mm.pDeviceHandler.deviceID})
 		return fmt.Errorf("timed-out-waiting-for-sync-time-response-%v", mm.pDeviceHandler.deviceID)
 	case syncTimeRes := <-mm.syncTimeResponseChan:
 		if !syncTimeRes {
@@ -2087,6 +2090,7 @@
 }
 
 func (mm *onuMetricsManager) generateTicks(ctx context.Context) {
+	mm.updateTickGenerationStatus(true)
 	for {
 		select {
 		case <-time.After(L2PmCollectionInterval * time.Second):
@@ -2097,6 +2101,7 @@
 			}()
 		case <-mm.stopTicks:
 			logger.Infow(ctx, "stopping ticks", log.Fields{"device-id": mm.pDeviceHandler.deviceID})
+			mm.updateTickGenerationStatus(false)
 			return
 		}
 	}
@@ -2596,6 +2601,30 @@
 	return value
 }
 
+func (mm *onuMetricsManager) updateOmciProcessingStatus(status bool) {
+	mm.onuMetricsManagerLock.Lock()
+	defer mm.onuMetricsManagerLock.Unlock()
+	mm.omciProcessingActive = status
+}
+
+func (mm *onuMetricsManager) updateTickGenerationStatus(status bool) {
+	mm.onuMetricsManagerLock.Lock()
+	defer mm.onuMetricsManagerLock.Unlock()
+	mm.tickGenerationActive = status
+}
+
+func (mm *onuMetricsManager) getOmciProcessingStatus() bool {
+	mm.onuMetricsManagerLock.RLock()
+	defer mm.onuMetricsManagerLock.RUnlock()
+	return mm.omciProcessingActive
+}
+
+func (mm *onuMetricsManager) getTickGenerationStatus() bool {
+	mm.onuMetricsManagerLock.RLock()
+	defer mm.onuMetricsManagerLock.RUnlock()
+	return mm.tickGenerationActive
+}
+
 func (mm *onuMetricsManager) appendIfMissingString(slice []string, n string) []string {
 	for _, ele := range slice {
 		if ele == n {