[VOL-4459] ONU connection state may be wrong or misleading after ONU soft reboot

Signed-off-by: mpagenko <michael.pagenkopf@adtran.com>
Change-Id: I845d999aa70cc02fc32c0a879ed31748a1fea70c
diff --git a/VERSION b/VERSION
index 5ec77cf..1b98dbf 100755
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-2.1.1-dev249
+2.1.1-dev250
diff --git a/internal/pkg/core/device_handler.go b/internal/pkg/core/device_handler.go
index 87a7410..f1005e6 100755
--- a/internal/pkg/core/device_handler.go
+++ b/internal/pkg/core/device_handler.go
@@ -61,6 +61,13 @@
 )
 
 const (
+	// dummy constant - irregular value for ConnState - used to avoiding setting this state in the updateDeviceState()
+	// should better be defined in voltha protobuf or best solution would be to define an interface to just set the OperState
+	// as long as such is not available by the libraries - use this workaround
+	connectStatusINVALID = 255 // as long as not used as key in voltha.ConnectStatus_Types_name
+)
+
+const (
 	// events of Device FSM
 	devEvDeviceInit       = "devEvDeviceInit"
 	devEvGrpcConnected    = "devEvGrpcConnected"
@@ -361,7 +368,7 @@
 	dh.pOnuTP.LockTpProcMutex()
 	defer dh.pOnuTP.UnlockTpProcMutex()
 
-	if techProfMsg.UniId > 255 {
+	if techProfMsg.UniId >= platform.MaxUnisPerOnu {
 		return fmt.Errorf(fmt.Sprintf("received UniId value exceeds range: %d, device-id: %s",
 			techProfMsg.UniId, dh.DeviceID))
 	}
@@ -748,11 +755,12 @@
 				dh.runUniLockFsm(ctx, true)
 			}
 		} else {
-			logger.Debugw(ctx, "DeviceStateUpdate upon disable", log.Fields{"ConnectStatus": voltha.ConnectStatus_REACHABLE,
+			logger.Debugw(ctx, "DeviceStateUpdate upon disable", log.Fields{
 				"OperStatus": voltha.OperStatus_UNKNOWN, "device-id": dh.DeviceID})
+			// disable device should have no impact on ConnStatus
 			if err := dh.updateDeviceStateInCore(ctx, &ca.DeviceStateFilter{
 				DeviceId:   dh.DeviceID,
-				ConnStatus: voltha.ConnectStatus_REACHABLE,
+				ConnStatus: connectStatusINVALID, //use some dummy value to prevent modification of the ConnStatus
 				OperStatus: voltha.OperStatus_UNKNOWN,
 			}); err != nil {
 				//TODO with VOL-3045/VOL-3046: return the error and stop further processing
@@ -1101,11 +1109,12 @@
 	//transfer the possibly modified logical uni port state
 	dh.DisableUniPortStateUpdate(ctx)
 
-	logger.Debugw(ctx, "call DeviceStateUpdate upon reboot", log.Fields{"ConnectStatus": voltha.ConnectStatus_REACHABLE,
+	logger.Debugw(ctx, "call DeviceStateUpdate upon reboot", log.Fields{
 		"OperStatus": voltha.OperStatus_DISCOVERED, "device-id": dh.DeviceID})
+	// do not set the ConnStatus here as it may conflict with the parallel setting from ONU down indication (updateInterface())
 	if err := dh.updateDeviceStateInCore(ctx, &ca.DeviceStateFilter{
 		DeviceId:   dh.DeviceID,
-		ConnStatus: voltha.ConnectStatus_REACHABLE,
+		ConnStatus: connectStatusINVALID, //use some dummy value to prevent modification of the ConnStatus
 		OperStatus: voltha.OperStatus_DISCOVERED,
 	}); err != nil {
 		//TODO with VOL-3045/VOL-3046: return the error and stop further processing
@@ -1986,8 +1995,7 @@
 		_ = dh.deleteDevicePersistencyData(ctx) //ignore possible errors here and continue, hope is that data is synchronized with new ONU-Up
 
 		//deviceEntry stop without omciCC reset here, regarding the OMCI_CC still valid for this ONU
-		// - in contrary to disableDevice - compare with processUniDisableStateDoneEvent
-		//stop the device entry which resets the attached omciCC
+		//stop the device entry to allow for all system event transfers again
 		pDevEntry := dh.GetOnuDeviceEntry(ctx, false)
 		if pDevEntry == nil {
 			logger.Errorw(ctx, "No valid OnuDevice -aborting", log.Fields{"device-id": dh.DeviceID})
@@ -2294,12 +2302,13 @@
 }
 
 func (dh *deviceHandler) processUniDisableStateDoneEvent(ctx context.Context, devEvent cmn.OnuDeviceEvent) {
-	logger.Debugw(ctx, "DeviceStateUpdate upon disable", log.Fields{"ConnectStatus": voltha.ConnectStatus_REACHABLE,
+	logger.Debugw(ctx, "DeviceStateUpdate upon disable", log.Fields{
 		"OperStatus": voltha.OperStatus_UNKNOWN, "device-id": dh.DeviceID})
 
+	// disable device should have no impact on ConnStatus
 	if err := dh.updateDeviceStateInCore(ctx, &ca.DeviceStateFilter{
 		DeviceId:   dh.DeviceID,
-		ConnStatus: voltha.ConnectStatus_REACHABLE,
+		ConnStatus: connectStatusINVALID, //use some dummy value to prevent modification of the ConnStatus
 		OperStatus: voltha.OperStatus_UNKNOWN,
 	}); err != nil {
 		//TODO with VOL-3045/VOL-3046: return the error and stop further processing
@@ -2366,6 +2375,7 @@
 		"OperStatus": voltha.OperStatus_FAILED, "device-id": dh.DeviceID})
 	if err := dh.updateDeviceStateInCore(ctx, &ca.DeviceStateFilter{
 		DeviceId:   dh.DeviceID,
+		ConnStatus: connectStatusINVALID, //use some dummy value to prevent modification of the ConnStatus
 		OperStatus: voltha.OperStatus_FAILED,
 	}); err != nil {
 		logger.Errorw(ctx, "error-updating-device-state", log.Fields{"device-id": dh.DeviceID, "error": err})