[VOL-1436] Configuring the northbound API request timeout.

Change-Id: Ie595c3250bfc8dc8247ae8b821ba5d5c841ea399
diff --git a/compose/rw_core.yml b/compose/rw_core.yml
index dba2544..6948546 100644
--- a/compose/rw_core.yml
+++ b/compose/rw_core.yml
@@ -31,6 +31,8 @@
         - -rw_core_topic=rwcore
         - -kv_store_data_prefix=service/voltha
         - -in_competing_mode=false
+        - -timeout_long_request=3000
+        - -timeout_request=300
         - -log_level=0
     ports:
       - 50057:50057
diff --git a/rw_core/config/config.go b/rw_core/config/config.go
index fc819a1..b924376 100644
--- a/rw_core/config/config.go
+++ b/rw_core/config/config.go
@@ -48,6 +48,8 @@
 	default_RWCoreCA            = "pki/voltha-CA.pem"
 	default_AffinityRouterTopic = "affinityRouter"
 	default_InCompetingMode     = true
+	default_LongRunningRequestTimeout = int64(2000)
+	default_DefaultRequestTimeout = int64(500)
 )
 
 // RWCoreFlags represents the set of configurations used by the read-write core service
@@ -75,6 +77,8 @@
 	RWCoreCA            string
 	AffinityRouterTopic string
 	InCompetingMode     bool
+	LongRunningRequestTimeout int64
+	DefaultRequestTimeout int64
 }
 
 func init() {
@@ -106,6 +110,8 @@
 		RWCoreCA:            default_RWCoreCA,
 		AffinityRouterTopic: default_AffinityRouterTopic,
 		InCompetingMode:     default_InCompetingMode,
+		DefaultRequestTimeout:default_DefaultRequestTimeout,
+		LongRunningRequestTimeout:default_LongRunningRequestTimeout,
 	}
 	return &rwCoreFlag
 }
@@ -166,6 +172,12 @@
 	help = fmt.Sprintf("Log level")
 	flag.IntVar(&(cf.LogLevel), "log_level", default_LogLevel, help)
 
+	help = fmt.Sprintf("Timeout for long running request")
+	flag.Int64Var(&(cf.LongRunningRequestTimeout), "timeout_long_request", default_LongRunningRequestTimeout, help)
+
+	help = fmt.Sprintf("Default timeout for regular request")
+	flag.Int64Var(&(cf.DefaultRequestTimeout), "timeout_request", default_DefaultRequestTimeout, help)
+
 	help = fmt.Sprintf("Show startup banner log lines")
 	flag.BoolVar(&cf.Banner, "banner", default_Banner, help)
 
diff --git a/rw_core/core/core.go b/rw_core/core/core.go
index c13face..1496200 100644
--- a/rw_core/core/core.go
+++ b/rw_core/core/core.go
@@ -110,7 +110,7 @@
 	core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false)
 	log.Info("grpc-server-created")
 
-	core.grpcNBIAPIHandler = NewAPIHandler(core.deviceMgr, core.logicalDeviceMgr, core.config.InCompetingMode)
+	core.grpcNBIAPIHandler = NewAPIHandler(core.deviceMgr, core.logicalDeviceMgr, core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout)
 	core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
 	//	Create a function to register the core GRPC service with the GRPC server
 	f := func(gs *grpc.Server) {
diff --git a/rw_core/core/grpc_nbi_api_handler.go b/rw_core/core/grpc_nbi_api_handler.go
index 4c82471..4d88459 100644
--- a/rw_core/core/grpc_nbi_api_handler.go
+++ b/rw_core/core/grpc_nbi_api_handler.go
@@ -35,7 +35,7 @@
 //TODO:  Move this Tag into the proto file
 const OF_CONTROLLER_TAG= "voltha_backend_name"
 
-const MAX_RESPONSE_TIME = int64(500) // milliseconds
+//const MAX_RESPONSE_TIME = int64(500) // milliseconds
 
 const (
 	IMAGE_DOWNLOAD = iota
@@ -49,14 +49,18 @@
 	logicalDeviceMgr *LogicalDeviceManager
 	packetInQueue    *queue.Queue
 	coreInCompetingMode bool
+	longRunningRequestTimeout int64
+	defaultRequestTimeout int64
 	da.DefaultAPIHandler
 }
 
-func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager, inCompetingMode bool) *APIHandler {
+func NewAPIHandler(deviceMgr *DeviceManager, lDeviceMgr *LogicalDeviceManager, inCompetingMode bool, longRunningRequestTimeout int64, defaultRequestTimeout int64 ) *APIHandler {
 	handler := &APIHandler{
 		deviceMgr:        deviceMgr,
 		logicalDeviceMgr: lDeviceMgr,
 		coreInCompetingMode:inCompetingMode,
+		longRunningRequestTimeout:longRunningRequestTimeout,
+		defaultRequestTimeout:defaultRequestTimeout,
 		// TODO: Figure out what the 'hint' parameter to queue.New does
 		packetInQueue: queue.New(10),
 	}
@@ -113,10 +117,11 @@
 }
 
 func (handler *APIHandler) acquireTransaction(ctx context.Context, maxTimeout ...int64) (*KVTransaction, error) {
-	timeout := MAX_RESPONSE_TIME
+	timeout := handler.defaultRequestTimeout
 	if len(maxTimeout) > 0 {
 		timeout = maxTimeout[0]
 	}
+	log.Debugw("transaction-timeout", log.Fields{"timeout": timeout})
 	txn, err := handler.createKvTransaction(ctx)
 	if txn == nil {
 		return nil,  err
@@ -347,7 +352,7 @@
 	}
 
 	if handler.competeForTransaction() {
-		if txn, err := handler.acquireTransaction(ctx); err != nil {
+		if txn, err := handler.acquireTransaction(ctx, handler.longRunningRequestTimeout); err != nil {
 			return new(empty.Empty), err
 		} else {
 			defer txn.Close()