Improvements in code to avoid proper cleanups on DeleteDevice call

- Stop onu indication channels before invoking DeleteDevice call towards
the OLT. This is to ensure that we stop processing on the channels
irrespective of failure to reach the device for DeleteDevice call.
- Process cleanup on DB in a synchronous manner so that core is inimated
of delete device success only after all the cleanup is completed at the
adapter

Change-Id: I9bf27cd4bf9035da366490c945f8344df8e8741a
diff --git a/internal/pkg/core/device_handler.go b/internal/pkg/core/device_handler.go
index 4243a26..4a84b78 100644
--- a/internal/pkg/core/device_handler.go
+++ b/internal/pkg/core/device_handler.go
@@ -1719,13 +1719,13 @@
 
 // DeleteDevice deletes the device instance from openolt handler array.  Also clears allocated resource manager resources.  Also reboots the OLT hardware!
 func (dh *DeviceHandler) DeleteDevice(ctx context.Context, device *voltha.Device) error {
-	logger.Debug(ctx, "function-entry-delete-device")
+	logger.Debugw(ctx, "function-entry-delete-device", log.Fields{"device-id": dh.device.Id})
 	/* Clear the KV store data associated with the all the UNI ports
 	   This clears up flow data and also resource map data for various
 	   other pon resources like alloc_id and gemport_id
 	*/
-	go dh.cleanupDeviceResources(ctx)
-	logger.Debug(ctx, "removed-device-from-Resource-manager-KV-store")
+	dh.cleanupDeviceResources(ctx)
+	logger.Debugw(ctx, "removed-device-from-Resource-manager-KV-store", log.Fields{"device-id": dh.device.Id})
 	// Stop the Stats collector
 	dh.stopCollector <- true
 	// stop the heartbeat check routine
@@ -1736,17 +1736,18 @@
 		dh.stopIndications <- true
 	}
 	dh.lockDevice.RUnlock()
+	dh.removeOnuIndicationChannels(ctx)
 	//Reset the state
 	if dh.Client != nil {
 		if _, err := dh.Client.Reboot(ctx, new(oop.Empty)); err != nil {
 			return olterrors.NewErrAdapter("olt-reboot-failed", log.Fields{"device-id": dh.device.Id}, err).Log()
 		}
 	}
-	dh.removeOnuIndicationChannels(ctx)
 	// There is no need to update the core about operation status and connection status of the OLT.
 	// The OLT is getting deleted anyway and the core might have already cleared the OLT device from its DB.
 	// So any attempt to update the operation status and connection status of the OLT will result in core throwing an error back,
 	// because the device does not exist in DB.
+
 	return nil
 }
 func (dh *DeviceHandler) cleanupDeviceResources(ctx context.Context) {
@@ -1773,12 +1774,9 @@
 					logger.Errorw(ctx, "failed-to-update-onugem-info", log.Fields{"intfid": ponPort, "onugeminfo": onuGemData})
 				}
 			}
-			/* Clear the resource pool for each PON port in the background */
-			go func(ponPort uint32) {
-				if err := dh.resourceMgr[ponPort].Delete(ctx, ponPort); err != nil {
-					logger.Debug(ctx, err)
-				}
-			}(ponPort)
+			if err := dh.resourceMgr[ponPort].Delete(ctx, ponPort); err != nil {
+				logger.Debug(ctx, err)
+			}
 		}
 	}
 
@@ -2027,7 +2025,7 @@
 		device.OperStatus = voltha.OperStatus_UNKNOWN
 		go dh.eventMgr.oltCommunicationEvent(ctx, device, raisedTs)
 
-		go dh.cleanupDeviceResources(ctx)
+		dh.cleanupDeviceResources(ctx)
 		// Stop the Stats collector
 		dh.stopCollector <- true
 		// stop the heartbeat check routine
diff --git a/internal/pkg/resourcemanager/resourcemanager.go b/internal/pkg/resourcemanager/resourcemanager.go
index 737f694..c256a3d 100755
--- a/internal/pkg/resourcemanager/resourcemanager.go
+++ b/internal/pkg/resourcemanager/resourcemanager.go
@@ -474,13 +474,18 @@
 
 // FreeonuID releases(make free) onu id for a particular pon-port
 func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
-
+	if len(onuID) == 0 {
+		logger.Info(ctx, "onu id slice is nil, nothing to free")
+		return
+	}
 	if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
 		logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
 			"intf-id": intfID,
 			"onu-id":  onuID,
 			"err":     err.Error(),
 		})
+	} else {
+		logger.Infow(ctx, "freed onu id", log.Fields{"intfID": intfID, "onuID": onuID})
 	}
 
 	/* Free onu id for a particular interface.*/