[VOL-5398]: Added error handling for SendOnuSwSectionsWindowWithRxSupervision function | https://gerrit.opencord.org/c/voltha-openonu-adapter-go/+/35507

Signed-off-by: Balaji Seenivasan <balaji.seenivasan@radisys.com>
Change-Id: I6d10ff5153593581d7516cef8b4773d8f1a0ff8e
diff --git a/internal/pkg/common/omci_cc.go b/internal/pkg/common/omci_cc.go
index f9f850b..8763443 100755
--- a/internal/pkg/common/omci_cc.go
+++ b/internal/pkg/common/omci_cc.go
@@ -4411,12 +4411,12 @@
 
 // SendOnuSwSectionsWindowWithRxSupervision sends onu swd sections
 func (oo *OmciCC) SendOnuSwSectionsWindowWithRxSupervision(ctx context.Context,
-	aOmciTxRequest OmciTransferStructure, aTimeout int, rxChan chan Message) {
+	aOmciTxRequest OmciTransferStructure, aTimeout int, rxChan chan Message) error {
 	if aOmciTxRequest.OnuSwWindow == nil {
 		logger.Errorw(ctx, "SendOnuSwSectionsWindowWithRxSupervision: omciTxRequest.OnuSwWindow is nil",
 			log.Fields{"device-id": oo.deviceID})
-		return
-
+		return fmt.Errorf("sendOnuSwSectionsWindowWithRxSupervision: omciTxRequest.OnuSwWindow is nil device-id: %v",
+			oo.deviceID)
 	}
 
 	tid := oo.GetOnuSwSecLastTid()
@@ -4446,7 +4446,7 @@
 	retryCounter := 0
 	if aTimeout == 0 {
 		logger.Errorw(ctx, "no timeout present for last section of window", log.Fields{"device-id": oo.deviceID})
-		return
+		return fmt.Errorf("no timeout present for last section of window device-id: %v", oo.deviceID)
 	}
 loop:
 	for retryCounter <= retries {
@@ -4478,18 +4478,33 @@
 						log.Fields{"tid": tid, "retries": retryCounter, "device-id": oo.deviceID})
 				}
 				oo.incrementTxTimesouts()
-				break loop
-			} else {
-				logger.Infow(ctx, "reqMon: timeout waiting for response - retry",
-					log.Fields{"tid": tid, "retries": retryCounter, "device-id": oo.deviceID})
-				oo.incrementTxRetries()
+				oo.mutexMonReq.Lock()
+				delete(oo.monitoredRequests, tid)
+				oo.mutexMonReq.Unlock()
+				return fmt.Errorf("reqMon: timeout waiting for response - no of max retries reached device-id: %v", oo.deviceID)
 			}
+			logger.Infow(ctx, "reqMon: timeout waiting for response - retry",
+				log.Fields{"tid": tid, "retries": retryCounter, "device-id": oo.deviceID})
+			oo.incrementTxRetries()
+		case _, ok := <-oo.pBaseDeviceHandler.GetDeviceDeleteCommChan(ctx):
+			if !ok {
+				logger.Warnw(ctx, "device deletion channel is closed", log.Fields{"device-id": oo.deviceID})
+				oo.mutexMonReq.Lock()
+				delete(oo.monitoredRequests, tid)
+				oo.mutexMonReq.Unlock()
+				return fmt.Errorf("device deletion channel is closed device-id: %v", oo.deviceID)
+			}
+			oo.mutexMonReq.Lock()
+			delete(oo.monitoredRequests, tid)
+			oo.mutexMonReq.Unlock()
+			return fmt.Errorf("received response from device deletion comm channel while waiting for a OMCI response device-id: %v", oo.deviceID)
 		}
 		retryCounter++
 	}
 	oo.mutexMonReq.Lock()
 	delete(oo.monitoredRequests, tid)
 	oo.mutexMonReq.Unlock()
+	return nil
 }
 
 func (oo *OmciCC) sendOnuSwSectionsOfWindow(ctx context.Context, omciTxRequest OmciTransferStructure) error {
diff --git a/internal/pkg/swupg/omci_onu_upgrade.go b/internal/pkg/swupg/omci_onu_upgrade.go
index 2143a10..3da4913 100755
--- a/internal/pkg/swupg/omci_onu_upgrade.go
+++ b/internal/pkg/swupg/omci_onu_upgrade.go
@@ -845,7 +845,12 @@
 			pUpgradeFsm := oFsm.PAdaptFsm
 			if pUpgradeFsm != nil {
 				_ = pUpgradeFsm.PFsm.Event(UpgradeEvWaitWindowAck) //state transition to upgradeStVerifyWindow
-				oFsm.pOmciCC.SendOnuSwSectionsWindowWithRxSupervision(ctx, omciTxReq, oFsm.pDeviceHandler.GetOmciTimeout(), oFsm.PAdaptFsm.CommChan)
+				err := oFsm.pOmciCC.SendOnuSwSectionsWindowWithRxSupervision(ctx, omciTxReq, oFsm.pDeviceHandler.GetOmciTimeout(), oFsm.PAdaptFsm.CommChan)
+				if err != nil {
+					logger.Errorw(ctx, "DlSection abort: can't send window acknowledgement request to ONU ", log.Fields{
+						"device-id": oFsm.deviceID, "section absolute": oFsm.nextDownloadSectionsAbsolute, "error": err})
+					oFsm.abortOnOmciError(ctx, false)
+				}
 				return
 			}
 			logger.Warnw(ctx, "pUpgradeFsm is nil", log.Fields{"device-id": oFsm.deviceID})
@@ -1969,7 +1974,7 @@
 				oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
 			}
 			//reset the image state from Downloading in this case
-			oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
+			oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING_ABORTED
 		//in all other upgrade phases the last set download state remains valid
 		case cUpgradeActivating:
 			//reset the image state from Activating in this case
@@ -1986,7 +1991,7 @@
 		oFsm.volthaDownloadState = voltha.ImageState_DOWNLOAD_FAILED
 		oFsm.volthaDownloadReason = voltha.ImageState_CANCELLED_ON_ONU_STATE
 		//reset the image state from Downloading in this case
-		oFsm.volthaImageState = voltha.ImageState_IMAGE_UNKNOWN //something like 'IMAGE_DOWNLOAD_ABORTED' would be better (proto)
+		oFsm.volthaImageState = voltha.ImageState_IMAGE_DOWNLOADING_ABORTED
 	}
 }