[VOL-4277] onu-adapter should change device's operational-state to FAILED if EnableDevice fails in adapter

Change-Id: I4795a662ccf3d01c3d29a846c984947dda0a7654
diff --git a/VERSION b/VERSION
index 3c59b40..89bdc09 100755
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.3.20
+1.3.21
diff --git a/internal/pkg/onuadaptercore/device_handler.go b/internal/pkg/onuadaptercore/device_handler.go
index dcc7fa6..929d81d 100644
--- a/internal/pkg/onuadaptercore/device_handler.go
+++ b/internal/pkg/onuadaptercore/device_handler.go
@@ -2377,6 +2377,15 @@
 	}
 }
 
+func (dh *deviceHandler) processUniEnableStateFailedEvent(ctx context.Context, devEvent OnuDeviceEvent) {
+	logger.Debugw(ctx, "DeviceStateUpdate upon re-enable failure. ", log.Fields{
+		"OperStatus": voltha.OperStatus_FAILED, "device-id": dh.deviceID})
+	if err := dh.coreProxy.DeviceStateUpdate(log.WithSpanFromContext(context.TODO(), ctx), dh.deviceID, voltha.ConnectStatus_REACHABLE,
+		voltha.OperStatus_FAILED); err != nil {
+		logger.Errorw(ctx, "error-updating-device-state", log.Fields{"device-id": dh.deviceID, "error": err})
+	}
+}
+
 func (dh *deviceHandler) processOmciAniConfigDoneEvent(ctx context.Context, devEvent OnuDeviceEvent) {
 	if devEvent == OmciAniConfigDone {
 		logger.Debugw(ctx, "OmciAniConfigDone event received", log.Fields{"device-id": dh.deviceID})
@@ -2457,6 +2466,10 @@
 		{
 			dh.processUniEnableStateDoneEvent(ctx, devEvent)
 		}
+	case UniEnableStateFailed:
+		{
+			dh.processUniEnableStateFailedEvent(ctx, devEvent)
+		}
 	case UniDisableStateDone:
 		{
 			dh.processUniDisableStateDoneEvent(ctx, devEvent)
diff --git a/internal/pkg/onuadaptercore/onu_device_entry.go b/internal/pkg/onuadaptercore/onu_device_entry.go
index 64929d5..e7fc201 100644
--- a/internal/pkg/onuadaptercore/onu_device_entry.go
+++ b/internal/pkg/onuadaptercore/onu_device_entry.go
@@ -139,6 +139,8 @@
 	UniDisableStateDone
 	// UniEnableStateDone - Uni ports admin set to unlock based on device re-enable
 	UniEnableStateDone
+	// UniEnableStateFailed - Uni ports admin set to unlock failure based on device re-enable
+	UniEnableStateFailed
 	// PortLinkUp - Port link state change
 	PortLinkUp
 	// PortLinkDw - Port link state change
diff --git a/internal/pkg/onuadaptercore/uniportadmin.go b/internal/pkg/onuadaptercore/uniportadmin.go
index 0c22500..4b829d5 100644
--- a/internal/pkg/onuadaptercore/uniportadmin.go
+++ b/internal/pkg/onuadaptercore/uniportadmin.go
@@ -214,6 +214,10 @@
 	meInstance, err := oFsm.pOmciCC.sendSetOnuGLS(log.WithSpanFromContext(context.TODO(), ctx), oFsm.pDeviceHandler.pOpenOnuAc.omciTimeout, true,
 		requestedAttributes, oFsm.pAdaptFsm.commChan)
 	if err != nil {
+		//Indicate the failure in UnLock case
+		if omciAdminState == 0 {
+			oFsm.setSuccessEvent(UniEnableStateFailed)
+		}
 		oFsm.mutexPLastTxMeInstance.Unlock()
 		logger.Errorw(ctx, "OnuGLS set failed, aborting LockStateFSM", log.Fields{"device-id": oFsm.deviceID})
 		pLockStateAFsm := oFsm.pAdaptFsm
@@ -230,6 +234,10 @@
 	//  - this avoids misinterpretation of new received OMCI messages
 	oFsm.pLastTxMeInstance = meInstance
 	if oFsm.pLastTxMeInstance == nil {
+		//Indicate the failure in UnLock case
+		if omciAdminState == 0 {
+			oFsm.setSuccessEvent(UniEnableStateFailed)
+		}
 		oFsm.mutexPLastTxMeInstance.Unlock()
 		logger.Errorw(ctx, "could not send OMCI message from LockStateFsm", log.Fields{
 			"device-id": oFsm.deviceID})
@@ -276,6 +284,12 @@
 
 func (oFsm *lockStateFsm) enterResettingState(ctx context.Context, e *fsm.Event) {
 	logger.Debugw(ctx, "LockStateFSM resetting", log.Fields{"device-id": oFsm.deviceID})
+	//If the fsm is reseted because of a failure during reenable, then issue the fail event.
+	if oFsm.requestEvent == UniEnableStateFailed {
+		logger.Debugw(ctx, "LockStateFSM send notification to core", log.Fields{"state": e.FSM.Current(), "device-id": oFsm.deviceID})
+		//use DeviceHandler event notification directly, no need/support to update DeviceEntryState for lock/unlock
+		oFsm.pDeviceHandler.deviceProcStatusUpdate(ctx, oFsm.requestEvent)
+	}
 	pLockStateAFsm := oFsm.pAdaptFsm
 	if pLockStateAFsm != nil {
 		// abort running message processing
@@ -430,6 +444,10 @@
 					oFsm.mutexPLastTxMeInstance.Unlock()
 					logger.Errorw(ctx, "SetPptpEthUniLS set failed, aborting LockStateFsm!",
 						log.Fields{"device-id": oFsm.deviceID})
+					//Indicate the failure in UnLock case
+					if omciAdminState == 0 {
+						oFsm.setSuccessEvent(UniEnableStateFailed)
+					}
 					_ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
 					return
 				}
@@ -446,6 +464,10 @@
 					oFsm.mutexPLastTxMeInstance.Unlock()
 					logger.Errorw(ctx, "SetVeipLS set failed, aborting LockStateFsm!",
 						log.Fields{"device-id": oFsm.deviceID})
+					//Indicate the failure in UnLock case
+					if omciAdminState == 0 {
+						oFsm.setSuccessEvent(UniEnableStateFailed)
+					}
 					_ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
 					return
 				}
@@ -461,6 +483,10 @@
 				oFsm.mutexPLastTxMeInstance.RUnlock()
 				logger.Errorw(ctx, "could not send PortAdmin OMCI message from LockStateFsm", log.Fields{
 					"device-id": oFsm.deviceID, "Port": uniNo})
+				//Indicate the failure in UnLock case
+				if omciAdminState == 0 {
+					oFsm.setSuccessEvent(UniEnableStateFailed)
+				}
 				//some more sophisticated approach is possible, e.g. repeating once, by now let's reset the state machine in order to release all resources now
 				_ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
 				return
@@ -472,6 +498,10 @@
 			if err != nil {
 				logger.Errorw(ctx, "UniTP Admin State set failed, aborting LockState set!",
 					log.Fields{"device-id": oFsm.deviceID, "Port": uniNo})
+				//Indicate the failure in UnLock case
+				if omciAdminState == 0 {
+					oFsm.setSuccessEvent(UniEnableStateFailed)
+				}
 				_ = oFsm.pAdaptFsm.pFsm.Event(uniEvReset)
 				return
 			}