VOL-4425: Fix panic where flows arrive at adapter before device has
          fully reconciled
- Have more checks to avoid processing flow before flow manager has
  initialized
- Fix issue where OperStatus of device was accidentally marked as
  ACTIVE from RECONCILING before the reconciled actually finished
- Fix function complexity of UpdateFlowsIncrementally function

Change-Id: Ib00c88189d44c9081825985c3ccfefca17fe32d1
diff --git a/internal/pkg/core/openolt.go b/internal/pkg/core/openolt.go
index f84910d..30aa33c 100644
--- a/internal/pkg/core/openolt.go
+++ b/internal/pkg/core/openolt.go
@@ -155,11 +155,6 @@
 	logger.Infow(ctx, "reconcile-device", log.Fields{"device-id": device.Id})
 	var handler *DeviceHandler
 	if handler = oo.getDeviceHandler(device.Id); handler == nil {
-		handler := NewDeviceHandler(oo.coreClient, oo.eventProxy, device, oo, oo.configManager, oo.config)
-		handler.adapterPreviouslyConnected = true
-		oo.addDeviceHandlerToMap(handler)
-		handler.transitionMap = NewTransitionMap(handler)
-
 		//Setting state to RECONCILING
 		cgClient, err := oo.coreClient.GetCoreServiceClient()
 		if err != nil {
@@ -175,6 +170,14 @@
 			return nil, olterrors.NewErrAdapter("device-update-failed", log.Fields{"device-id": device.Id}, err)
 		}
 
+		// The OperState of the device is set to RECONCILING in the previous section. This also needs to be set on the
+		// locally cached copy of the device struct.
+		device.OperStatus = voltha.OperStatus_RECONCILING
+		handler := NewDeviceHandler(oo.coreClient, oo.eventProxy, device, oo, oo.configManager, oo.config)
+		handler.adapterPreviouslyConnected = true
+		oo.addDeviceHandlerToMap(handler)
+		handler.transitionMap = NewTransitionMap(handler)
+
 		handler.transitionMap.Handle(log.WithSpanFromContext(context.Background(), ctx), DeviceInit)
 	}
 	return &empty.Empty{}, nil