[VOL-4771] Error deleting device via voltctl when OLT is unreachable

Change-Id: Icffd0e1faa2947b0a713e0ffa66643e37170c6eb
diff --git a/rw_core/core/device/agent.go b/rw_core/core/device/agent.go
index 499124c..6599e13 100755
--- a/rw_core/core/device/agent.go
+++ b/rw_core/core/device/agent.go
@@ -742,6 +742,12 @@
 		// Change the state to DELETING POST ADAPTER RESPONSE directly as adapters have no info of the device.
 		currentDeviceTransientState = core.DeviceTransientState_DELETING_POST_ADAPTER_RESPONSE
 	}
+	// Update device and release lock
+	if err = agent.updateDeviceWithTransientStateAndReleaseLock(ctx, device,
+		currentDeviceTransientState, previousDeviceTransientState); err != nil {
+		desc = err.Error()
+		return err
+	}
 	// If the device was in pre-prov state (only parent device are in that state) then do not send the request to the
 	// adapter
 	if previousAdminState != common.AdminState_PREPROVISIONED {
@@ -760,25 +766,14 @@
 		}
 		subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout)
 		requestStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS
-		go func() {
-			defer cancel()
-			_, err := client.DeleteDevice(subCtx, device)
-			if err == nil {
-				agent.onDeleteSuccess(subCtx, nil, nil)
-			} else {
-				agent.onDeleteFailure(subCtx, err, nil, nil)
-			}
-		}()
+		if _, err = client.DeleteDevice(subCtx, device); err != nil {
+			agent.onDeleteFailure(subCtx, err, &previousAdminState, &agent.device.AdminState)
+		} else {
+			agent.onDeleteSuccess(subCtx, &previousAdminState, &agent.device.AdminState)
+		}
+		cancel()
 	}
-
-	// Update device and release lock
-	if err = agent.updateDeviceWithTransientStateAndReleaseLock(ctx, device,
-		currentDeviceTransientState, previousDeviceTransientState); err != nil {
-		desc = err.Error()
-		return err
-	}
-
-	return nil
+	return err
 }
 
 func (agent *Agent) setParentID(ctx context.Context, device *voltha.Device, parentID string) error {
diff --git a/rw_core/test/core_nbi_handler_multi_test.go b/rw_core/test/core_nbi_handler_multi_test.go
index 88b00eb..e6c4213 100755
--- a/rw_core/test/core_nbi_handler_multi_test.go
+++ b/rw_core/test/core_nbi_handler_multi_test.go
@@ -22,8 +22,14 @@
 	"fmt"
 	"io"
 	"math/rand"
+	"os"
+	"runtime"
+	"runtime/pprof"
+	"strconv"
 	"strings"
 	"sync"
+	"testing"
+	"time"
 
 	"github.com/Shopify/sarama"
 	"github.com/golang/protobuf/ptypes/empty"
@@ -36,13 +42,6 @@
 	"github.com/opencord/voltha-protos/v5/go/voltha"
 	"google.golang.org/grpc"
 
-	"os"
-	"runtime"
-	"runtime/pprof"
-	"strconv"
-	"testing"
-	"time"
-
 	"github.com/golang/protobuf/jsonpb"
 	"github.com/opencord/voltha-go/rw_core/config"
 	c "github.com/opencord/voltha-go/rw_core/core"
@@ -683,7 +682,7 @@
 
 	//Now remove the device
 	_, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
-	assert.Nil(t, err)
+	assert.NotNil(t, err)
 
 	// Wait for the delete event
 	event := <-ch