[VOL-4301] Onuimage download failure status in case of wrong device-id

Change-Id: I8882aa6bc1956b32d4cbd88efb026ee5d31d5261
diff --git a/rw_core/core/device/manager_nbi.go b/rw_core/core/device/manager_nbi.go
index 9a16812..9632eb1 100644
--- a/rw_core/core/device/manager_nbi.go
+++ b/rw_core/core/device/manager_nbi.go
@@ -285,22 +285,46 @@
 		go func(deviceID string, req *voltha.DeviceImageDownloadRequest, ch chan []*voltha.DeviceImageState) {
 			agent := dMgr.getDeviceAgent(ctx, deviceID)
 			if agent == nil {
-				logger.Errorw(ctx, "Not-found", log.Fields{"device-id": deviceID})
-				ch <- nil
+				logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID})
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       req.GetImage().GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_FAILED,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 
 			resp, err := agent.downloadImageToDevice(ctx, req)
 			if err != nil {
 				logger.Errorw(ctx, "download-image-to-device-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       req.GetImage().GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_FAILED,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 
 			err = dMgr.validateDeviceImageResponse(resp)
 			if err != nil {
 				logger.Errorw(ctx, "download-image-to-device-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       req.GetImage().GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_FAILED,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 			ch <- resp.GetDeviceImageStates()
@@ -332,22 +356,46 @@
 		go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) {
 			agent := dMgr.getDeviceAgent(ctx, deviceID)
 			if agent == nil {
-				logger.Errorw(ctx, "Not-found", log.Fields{"device-id": deviceID})
-				ch <- nil
+				logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID})
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 
 			resp, err := agent.getImageStatus(ctx, req)
 			if err != nil {
 				logger.Errorw(ctx, "get-image-status-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 
 			err = dMgr.validateDeviceImageResponse(resp)
 			if err != nil {
 				logger.Errorw(ctx, "get-image-status-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 			ch <- resp.GetDeviceImageStates()
@@ -379,22 +427,46 @@
 		go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) {
 			agent := dMgr.getDeviceAgent(ctx, deviceID)
 			if agent == nil {
-				logger.Errorw(ctx, "Not-found", log.Fields{"device-id": deviceID})
-				ch <- nil
+				logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID})
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 
 			resp, err := agent.abortImageUpgradeToDevice(ctx, req)
 			if err != nil {
 				logger.Errorw(ctx, "abort-image-upgrade-to-device-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 
 			err = dMgr.validateDeviceImageResponse(resp)
 			if err != nil {
 				logger.Errorw(ctx, "abort-image-upgrade-to-device-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 			ch <- resp.GetDeviceImageStates()
@@ -448,22 +520,46 @@
 		go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) {
 			agent := dMgr.getDeviceAgent(ctx, deviceID)
 			if agent == nil {
-				logger.Errorw(ctx, "Not-found", log.Fields{"device-id": deviceID})
-				ch <- nil
+				logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID})
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_ACTIVATION_ABORTED,
+					},
+				}}
 				return
 			}
 
 			resp, err := agent.activateImageOnDevice(ctx, req)
 			if err != nil {
 				logger.Errorw(ctx, "activate-image-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_ACTIVATION_ABORTED,
+					},
+				}}
 				return
 			}
 
 			err = dMgr.validateDeviceImageResponse(resp)
 			if err != nil {
 				logger.Errorw(ctx, "activate-image-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 
@@ -495,22 +591,46 @@
 		go func(deviceID string, req *voltha.DeviceImageRequest, ch chan []*voltha.DeviceImageState) {
 			agent := dMgr.getDeviceAgent(ctx, deviceID)
 			if agent == nil {
-				logger.Errorw(ctx, "Not-found", log.Fields{"device-id": deviceID})
-				ch <- nil
+				logger.Errorw(ctx, "Device-agent-not-found", log.Fields{"device-id": deviceID})
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_COMMIT_ABORTED,
+					},
+				}}
 				return
 			}
 
 			resp, err := agent.commitImage(ctx, req)
 			if err != nil {
 				logger.Errorw(ctx, "commit-image-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_COMMIT_ABORTED,
+					},
+				}}
 				return
 			}
 
 			err = dMgr.validateDeviceImageResponse(resp)
 			if err != nil {
 				logger.Errorf(ctx, "commit-image-failed", log.Fields{"device-id": deviceID, "error": err})
-				ch <- nil
+				ch <- []*voltha.DeviceImageState{{
+					DeviceId: deviceID,
+					ImageState: &voltha.ImageState{
+						Version:       request.GetVersion(),
+						DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN,
+						Reason:        voltha.ImageState_UNKNOWN_ERROR,
+						ImageState:    voltha.ImageState_IMAGE_UNKNOWN,
+					},
+				}}
 				return
 			}
 			ch <- resp.GetDeviceImageStates()