[VOL-2364] Returning a DEADLINE_EXCEEDED error from ChildDeviceDetected

Change-Id: I82559a1215702143acf9866ae187b51f149e7f15
diff --git a/VERSION b/VERSION
index 2451c27..67786e2 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.0.7
+3.0.8
diff --git a/pkg/adapters/common/core_proxy.go b/pkg/adapters/common/core_proxy.go
index c5e1c14..7cb933d 100644
--- a/pkg/adapters/common/core_proxy.go
+++ b/pkg/adapters/common/core_proxy.go
@@ -284,7 +284,7 @@
 		volthaDevice := &voltha.Device{}
 		if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil {
 			logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
-			return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
+			return nil, status.Error(codes.InvalidArgument, err.Error())
 		}
 		return volthaDevice, nil
 	} else {
@@ -294,8 +294,8 @@
 			logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
 		}
 		logger.Debugw("ChildDeviceDetected-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
-		// TODO: Need to get the real error code
-		return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason)
+
+		return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
 	}
 
 }
@@ -361,7 +361,7 @@
 		volthaDevice := &voltha.Device{}
 		if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil {
 			logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
-			return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
+			return nil, status.Error(codes.InvalidArgument, err.Error())
 		}
 		return volthaDevice, nil
 	} else {
@@ -372,7 +372,7 @@
 		}
 		logger.Debugw("GetDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
 		// TODO:  Need to get the real error code
-		return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason)
+		return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
 	}
 }
 
@@ -421,7 +421,7 @@
 		volthaDevice := &voltha.Device{}
 		if err := ptypes.UnmarshalAny(result, volthaDevice); err != nil {
 			logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
-			return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
+			return nil, status.Error(codes.InvalidArgument, err.Error())
 		}
 		return volthaDevice, nil
 	} else {
@@ -432,13 +432,7 @@
 		}
 		logger.Debugw("GetChildDevice-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
 
-		code := codes.Internal
-
-		if unpackResult.Code == ic.ErrorCode_DEADLINE_EXCEEDED {
-			code = codes.DeadlineExceeded
-		}
-
-		return nil, status.Errorf(code, "%s", unpackResult.Reason)
+		return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
 	}
 }
 
@@ -463,7 +457,7 @@
 		volthaDevices := &voltha.Devices{}
 		if err := ptypes.UnmarshalAny(result, volthaDevices); err != nil {
 			logger.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
-			return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
+			return nil, status.Error(codes.InvalidArgument, err.Error())
 		}
 		return volthaDevices, nil
 	} else {
@@ -474,13 +468,7 @@
 		}
 		logger.Debugw("GetChildDevices-return", log.Fields{"deviceid": parentDeviceId, "success": success, "error": err})
 
-		code := codes.Internal
-
-		if unpackResult.Code == ic.ErrorCode_DEADLINE_EXCEEDED {
-			code = codes.DeadlineExceeded
-		}
-
-		return nil, status.Errorf(code, "%s", unpackResult.Reason)
+		return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(unpackResult.Code), unpackResult.Reason)
 	}
 }
 
diff --git a/pkg/adapters/common/utils.go b/pkg/adapters/common/utils.go
index d3c562a..b782ebe 100644
--- a/pkg/adapters/common/utils.go
+++ b/pkg/adapters/common/utils.go
@@ -17,6 +17,9 @@
 
 import (
 	"fmt"
+	"github.com/opencord/voltha-lib-go/v3/pkg/log"
+	ic "github.com/opencord/voltha-protos/v3/go/inter_container"
+	"google.golang.org/grpc/codes"
 	"math/rand"
 	"time"
 )
@@ -71,3 +74,17 @@
 	}
 	return string(b)
 }
+
+func ICProxyErrorCodeToGrpcErrorCode(icErr ic.ErrorCodeCodes) codes.Code {
+	switch icErr {
+	case ic.ErrorCode_INVALID_PARAMETERS:
+		return codes.InvalidArgument
+	case ic.ErrorCode_UNSUPPORTED_REQUEST:
+		return codes.Unavailable
+	case ic.ErrorCode_DEADLINE_EXCEEDED:
+		return codes.DeadlineExceeded
+	default:
+		log.Warnw("cannnot-map-ic-error-code-to-grpc-error-code", log.Fields{"err": icErr})
+		return codes.Internal
+	}
+}
diff --git a/pkg/adapters/common/utils_test.go b/pkg/adapters/common/utils_test.go
index a8d7a3f..87594fd 100644
--- a/pkg/adapters/common/utils_test.go
+++ b/pkg/adapters/common/utils_test.go
@@ -16,7 +16,9 @@
 package common
 
 import (
+	ic "github.com/opencord/voltha-protos/v3/go/inter_container"
 	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc/codes"
 	"strconv"
 	sp "strings"
 	"testing"
@@ -68,3 +70,14 @@
 	}
 
 }
+
+func TestICProxyErrorCodeToGrpcErrorCode(t *testing.T) {
+	unsupported := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_UNSUPPORTED_REQUEST)
+	assert.Equal(t, unsupported, codes.Unavailable)
+
+	invalid := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_INVALID_PARAMETERS)
+	assert.Equal(t, invalid, codes.InvalidArgument)
+
+	timeout := ICProxyErrorCodeToGrpcErrorCode(ic.ErrorCode_DEADLINE_EXCEEDED)
+	assert.Equal(t, timeout, codes.DeadlineExceeded)
+}