VOL-1801 : Inconsistent data update

rhp.mergeDeviceInfoFromAdapter function moved to DeviceAgent.
Geting device, merging with new values and commiting to db done in same Lock.

Change-Id: I5e72a229d00ce876ffdbbe5339feea916803f38d
diff --git a/rw_core/core/adapter_request_handler.go b/rw_core/core/adapter_request_handler.go
index 33d14a9..2b28fa5 100644
--- a/rw_core/core/adapter_request_handler.go
+++ b/rw_core/core/adapter_request_handler.go
@@ -17,7 +17,6 @@
 
 import (
 	"errors"
-	"github.com/gogo/protobuf/proto"
 	"github.com/golang/protobuf/ptypes"
 	"github.com/golang/protobuf/ptypes/empty"
 	"github.com/opencord/voltha-go/common/log"
@@ -211,26 +210,6 @@
 	}
 }
 
-// updatePartialDeviceData updates a subset of a device that an Adapter can update.
-// TODO:  May need a specific proto to handle only a subset of a device that can be changed by an adapter
-func (rhp *AdapterRequestHandlerProxy) mergeDeviceInfoFromAdapter(device *voltha.Device) (*voltha.Device, error) {
-	//		First retrieve the most up to date device info
-	var currentDevice *voltha.Device
-	var err error
-	if currentDevice, err = rhp.deviceMgr.GetDevice(device.Id); err != nil {
-		return nil, err
-	}
-	cloned := proto.Clone(currentDevice).(*voltha.Device)
-	cloned.Root = device.Root
-	cloned.Vendor = device.Vendor
-	cloned.Model = device.Model
-	cloned.SerialNumber = device.SerialNumber
-	cloned.MacAddress = device.MacAddress
-	cloned.Vlan = device.Vlan
-	cloned.Reason = device.Reason
-	return cloned, nil
-}
-
 func (rhp *AdapterRequestHandlerProxy) DeviceUpdate(args []*ic.Argument) (*empty.Empty, error) {
 	if len(args) < 2 {
 		log.Warn("invalid-number-of-args", log.Fields{"args": args})
@@ -271,17 +250,7 @@
 	if rhp.TestMode { // Execute only for test cases
 		return new(empty.Empty), nil
 	}
-
-	//Merge the adapter device info (only the ones an adapter can change) with the latest device data
-	if updatedDevice, err := rhp.mergeDeviceInfoFromAdapter(device); err != nil {
-		return nil, status.Errorf(codes.Internal, "%s", err.Error())
-	} else {
-		go rhp.deviceMgr.updateDevice(updatedDevice)
-		//if err := rhp.deviceMgr.updateDevice(updatedDevice); err != nil {
-		//	return nil, err
-		//}
-	}
-
+	go rhp.deviceMgr.updateDeviceUsingAdapterData(device)
 	return new(empty.Empty), nil
 }
 
diff --git a/rw_core/core/device_agent.go b/rw_core/core/device_agent.go
index 25b12b5..7b9e00b 100755
--- a/rw_core/core/device_agent.go
+++ b/rw_core/core/device_agent.go
@@ -1020,12 +1020,36 @@
 	return nil
 }
 
-func (agent *DeviceAgent) updateDevice(device *voltha.Device) error {
+// updatePartialDeviceData updates a subset of a device that an Adapter can update.
+// TODO:  May need a specific proto to handle only a subset of a device that can be changed by an adapter
+func (agent *DeviceAgent) mergeDeviceInfoFromAdapter(device *voltha.Device) (*voltha.Device, error) {
+	//		First retrieve the most up to date device info
+	var currentDevice *voltha.Device
+	var err error
+	if currentDevice, err = agent.getDeviceWithoutLock(); err != nil {
+		return nil, err
+	}
+	cloned := proto.Clone(currentDevice).(*voltha.Device)
+	cloned.Root = device.Root
+	cloned.Vendor = device.Vendor
+	cloned.Model = device.Model
+	cloned.SerialNumber = device.SerialNumber
+	cloned.MacAddress = device.MacAddress
+	cloned.Vlan = device.Vlan
+	cloned.Reason = device.Reason
+	return cloned, nil
+}
+func (agent *DeviceAgent) updateDeviceUsingAdapterData(device *voltha.Device) error {
 	agent.lockDevice.Lock()
 	defer agent.lockDevice.Unlock()
-	log.Debugw("updateDevice", log.Fields{"deviceId": device.Id})
-	cloned := proto.Clone(device).(*voltha.Device)
-	return agent.updateDeviceInStoreWithoutLock(cloned, false, "")
+	log.Debugw("updateDeviceUsingAdapterData", log.Fields{"deviceId": device.Id})
+	if updatedDevice, err := agent.mergeDeviceInfoFromAdapter(device); err != nil {
+		log.Errorw("failed to update device ", log.Fields{"deviceId": device.Id})
+		return status.Errorf(codes.Internal, "%s", err.Error())
+	} else {
+		cloned := proto.Clone(updatedDevice).(*voltha.Device)
+		return agent.updateDeviceInStoreWithoutLock(cloned, false, "")
+	}
 }
 
 func (agent *DeviceAgent) updateDeviceWithoutLock(device *voltha.Device) error {
diff --git a/rw_core/core/device_manager.go b/rw_core/core/device_manager.go
index d053ac4..606e36f 100755
--- a/rw_core/core/device_manager.go
+++ b/rw_core/core/device_manager.go
@@ -625,10 +625,10 @@
 	return nil
 }
 
-func (dMgr *DeviceManager) updateDevice(device *voltha.Device) error {
-	log.Debugw("updateDevice", log.Fields{"deviceid": device.Id, "device": device})
+func (dMgr *DeviceManager) updateDeviceUsingAdapterData(device *voltha.Device) error {
+	log.Debugw("updateDeviceUsingAdapterData", log.Fields{"deviceid": device.Id, "device": device})
 	if agent := dMgr.getDeviceAgent(device.Id); agent != nil {
-		return agent.updateDevice(device)
+		return agent.updateDeviceUsingAdapterData(device)
 	}
 	return status.Errorf(codes.NotFound, "%s", device.Id)
 }