[VOL-3916] KV-store contains ONU specific data after RPC delete_device()

Change-Id: I0623bebc847a82a5b34790c6a1ff036406899d2b
diff --git a/VERSION b/VERSION
index 909897b..25b43e4 100755
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.2.7-dev177
+1.2.7-dev178
diff --git a/internal/pkg/onuadaptercore/device_handler.go b/internal/pkg/onuadaptercore/device_handler.go
index cadd230..f609a43 100644
--- a/internal/pkg/onuadaptercore/device_handler.go
+++ b/internal/pkg/onuadaptercore/device_handler.go
@@ -200,23 +200,25 @@
 	//discOnus sync.Map
 	//onus     sync.Map
 	//portStats          *OpenOltStatisticsMgr
-	collectorIsRunning         bool
-	mutexCollectorFlag         sync.RWMutex
-	stopCollector              chan bool
-	alarmManagerIsRunning      bool
-	mutextAlarmManagerFlag     sync.RWMutex
-	stopAlarmManager           chan bool
-	stopHeartbeatCheck         chan bool
-	uniEntityMap               map[uint32]*onuUniPort
-	mutexKvStoreContext        sync.Mutex
-	lockVlanConfig             sync.RWMutex
-	UniVlanConfigFsmMap        map[uint8]*UniVlanConfigFsm
-	lockUpgradeFsm             sync.RWMutex
-	pOnuUpradeFsm              *OnuUpgradeFsm
-	reconciling                uint8
-	mutexReconcilingFlag       sync.RWMutex
-	chReconcilingFinished      chan bool //channel to indicate that reconciling has been finished
-	ReadyForSpecificOmciConfig bool
+	collectorIsRunning          bool
+	mutexCollectorFlag          sync.RWMutex
+	stopCollector               chan bool
+	alarmManagerIsRunning       bool
+	mutextAlarmManagerFlag      sync.RWMutex
+	stopAlarmManager            chan bool
+	stopHeartbeatCheck          chan bool
+	uniEntityMap                map[uint32]*onuUniPort
+	mutexKvStoreContext         sync.Mutex
+	lockVlanConfig              sync.RWMutex
+	UniVlanConfigFsmMap         map[uint8]*UniVlanConfigFsm
+	lockUpgradeFsm              sync.RWMutex
+	pOnuUpradeFsm               *OnuUpgradeFsm
+	reconciling                 uint8
+	mutexReconcilingFlag        sync.RWMutex
+	chReconcilingFinished       chan bool //channel to indicate that reconciling has been finished
+	ReadyForSpecificOmciConfig  bool
+	deletionInProgress          bool
+	mutexDeletionInProgressFlag sync.RWMutex
 }
 
 //newDeviceHandler creates a new device handler
@@ -248,6 +250,7 @@
 	dh.reconciling = cNoReconciling
 	dh.chReconcilingFinished = make(chan bool)
 	dh.ReadyForSpecificOmciConfig = false
+	dh.deletionInProgress = false
 
 	if dh.device.PmConfigs != nil { // can happen after onu adapter restart
 		dh.pmConfigs = cloned.PmConfigs
diff --git a/internal/pkg/onuadaptercore/onu_device_entry.go b/internal/pkg/onuadaptercore/onu_device_entry.go
index c52f96a..2603ca0 100644
--- a/internal/pkg/onuadaptercore/onu_device_entry.go
+++ b/internal/pkg/onuadaptercore/onu_device_entry.go
@@ -708,6 +708,24 @@
 		oo.chOnuKvProcessingStep <- 0 //error indication
 		return
 	}
+	oo.pOpenOnuAc.lockDeviceHandlersMap.RLock()
+	if _, exist := oo.pOpenOnuAc.deviceHandlers[oo.deviceID]; !exist {
+		logger.Debugw(ctx, "delete_device in progress - skip write request", log.Fields{"device-id": oo.deviceID})
+		oo.chOnuKvProcessingStep <- aProcessingStep
+		oo.pOpenOnuAc.lockDeviceHandlersMap.RUnlock()
+		return
+	}
+	oo.baseDeviceHandler.mutexDeletionInProgressFlag.RLock()
+	if oo.baseDeviceHandler.deletionInProgress {
+		logger.Debugw(ctx, "delete_device in progress - skip write request", log.Fields{"device-id": oo.deviceID})
+		oo.chOnuKvProcessingStep <- aProcessingStep
+		oo.pOpenOnuAc.lockDeviceHandlersMap.RUnlock()
+		oo.baseDeviceHandler.mutexDeletionInProgressFlag.RUnlock()
+		return
+	}
+	oo.pOpenOnuAc.lockDeviceHandlersMap.RUnlock()
+	oo.baseDeviceHandler.mutexDeletionInProgressFlag.RUnlock()
+
 	oo.onuKVStoreMutex.Lock()
 	err = oo.onuKVStore.Put(ctx, oo.onuKVStorePath, Value)
 	oo.onuKVStoreMutex.Unlock()
diff --git a/internal/pkg/onuadaptercore/openonu.go b/internal/pkg/onuadaptercore/openonu.go
index cb74d5a..71a9034 100644
--- a/internal/pkg/onuadaptercore/openonu.go
+++ b/internal/pkg/onuadaptercore/openonu.go
@@ -384,6 +384,11 @@
 	logger.Infow(ctx, "delete-device", log.Fields{"device-id": device.Id, "SerialNumber": device.SerialNumber})
 	if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
 		var errorsList []error
+
+		handler.mutexDeletionInProgressFlag.Lock()
+		handler.deletionInProgress = true
+		handler.mutexDeletionInProgressFlag.Unlock()
+
 		if err := handler.deleteDevicePersistencyData(ctx); err != nil {
 			errorsList = append(errorsList, err)
 		}