VOL-1689 : ONU stays in DISCOVERED state
VOL-1586 : Possible race condition in openolt python adapter during onu discovery

Change-Id: Iadeb5c284f203a37f2f4dd3fcb6c922bb281f242
diff --git a/rw_core/core/device_manager.go b/rw_core/core/device_manager.go
index 9bc723f..ad4f362 100755
--- a/rw_core/core/device_manager.go
+++ b/rw_core/core/device_manager.go
@@ -270,7 +270,7 @@
 		return nil, status.Errorf(codes.Aborted, "%s", err.Error())
 	}
 	if len(childDeviceIds) == 0 {
-		log.Debugw("no-child-devices", log.Fields{"parentDeviceId": parentDevice.Id})
+		log.Debugw("no-child-devices", log.Fields{"parentDeviceId": parentDevice.Id, "serialNumber": serialNumber, "onuId": onuId})
 		return nil, status.Errorf(codes.NotFound, "%s", parentDeviceId)
 	}
 
@@ -742,7 +742,7 @@
 }
 
 func (dMgr *DeviceManager) childDeviceDetected(parentDeviceId string, parentPortNo int64, deviceType string,
-	channelId int64, vendorId string, serialNumber string, onuId int64) error {
+	channelId int64, vendorId string, serialNumber string, onuId int64) (*voltha.Device, error) {
 	log.Debugw("childDeviceDetected", log.Fields{"parentDeviceId": parentDeviceId})
 
 	// Create the ONU device
@@ -758,12 +758,12 @@
 	parent, err := dMgr.GetDevice(parentDeviceId)
 	if err != nil {
 		log.Error("no-parent-found", log.Fields{"parentId": parentDeviceId})
-		return status.Errorf(codes.NotFound, "%s", parentDeviceId)
+		return nil, status.Errorf(codes.NotFound, "%s", parentDeviceId)
 	}
 
-	if _, err := dMgr.GetChildDevice(parentDeviceId, serialNumber, onuId, parentPortNo); err == nil {
+	if device, err := dMgr.GetChildDevice(parentDeviceId, serialNumber, onuId, parentPortNo); err == nil {
 		log.Warnw("child-device-exists", log.Fields{"parentId": parentDeviceId, "serialNumber": serialNumber})
-		return status.Errorf(codes.AlreadyExists, "%s", serialNumber)
+		return device, status.Errorf(codes.AlreadyExists, "%s", serialNumber)
 	}
 
 	childDevice.ProxyAddress = &voltha.Device_ProxyAddress{DeviceId: parentDeviceId, DeviceType: parent.Type, ChannelId: uint32(channelId), OnuId: uint32(onuId)}
@@ -781,7 +781,7 @@
 	// Publish on the messaging bus that we have discovered new devices
 	go dMgr.kafkaICProxy.DeviceDiscovered(agent.deviceId, deviceType, parentDeviceId, dMgr.coreInstanceId)
 
-	return nil
+	return agent.lastData, nil
 }
 
 func (dMgr *DeviceManager) processTransition(previous *voltha.Device, current *voltha.Device) error {