VOL-1981: Same Device Provisioning Multiple Times

Change-Id: Iee4478bcc0b5bffe216d7941bf02d8e045d1ad21
diff --git a/rw_core/core/device_manager.go b/rw_core/core/device_manager.go
index d9aa5ea..b3c11af 100755
--- a/rw_core/core/device_manager.go
+++ b/rw_core/core/device_manager.go
@@ -147,6 +147,11 @@
 }
 
 func (dMgr *DeviceManager) createDevice(ctx context.Context, device *voltha.Device, ch chan interface{}) {
+	if dMgr.isParentDeviceExist(device) {
+		log.Errorf("Device is Pre-provisioned already with same IP-Port or MAC Address")
+		sendResponse(ctx, ch, errors.New("Device is already pre-provisioned"))
+		return
+	}
 	log.Debugw("createDevice", log.Fields{"device": device, "aproxy": dMgr.adapterProxy})
 
 	// Ensure this device is set as root
@@ -385,6 +390,25 @@
 	return result, nil
 }
 
+//isParentDeviceExist checks whether device is already preprovisioned.
+func (dMgr *DeviceManager) isParentDeviceExist(newDevice *voltha.Device) bool {
+	hostPort := newDevice.GetHostAndPort()
+	if devices := dMgr.clusterDataProxy.List(context.Background(), "/devices", 0, false, ""); devices != nil {
+		for _, device := range devices.([]interface{}) {
+			if !device.(*voltha.Device).Root {
+				continue
+			}
+			if hostPort != "" && hostPort == device.(*voltha.Device).GetHostAndPort() {
+				return true
+			}
+			if newDevice.MacAddress != "" && newDevice.MacAddress == device.(*voltha.Device).MacAddress {
+				return true
+			}
+		}
+	}
+	return false
+}
+
 //getDeviceFromModelretrieves the device data from the model.
 func (dMgr *DeviceManager) getDeviceFromModel(deviceId string) (*voltha.Device, error) {
 	if device := dMgr.clusterDataProxy.Get(context.Background(), "/devices/"+deviceId, 0, false, ""); device != nil {
diff --git a/rw_core/core/grpc_nbi_api_handler.go b/rw_core/core/grpc_nbi_api_handler.go
index e97956a..aff80bc 100755
--- a/rw_core/core/grpc_nbi_api_handler.go
+++ b/rw_core/core/grpc_nbi_api_handler.go
@@ -426,7 +426,11 @@
 
 // CreateDevice creates a new parent device in the data model
 func (handler *APIHandler) CreateDevice(ctx context.Context, device *voltha.Device) (*voltha.Device, error) {
-	log.Debugw("createdevice", log.Fields{"device": *device})
+	if device.MacAddress == "" && device.GetHostAndPort() == "" {
+		log.Errorf("No Device Info Present")
+		return nil, errors.New("No Device Info Present; MAC or HOSTIP&PORT")
+	}
+	log.Debugw("create-device", log.Fields{"device": *device})
 	if isTestMode(ctx) {
 		return &voltha.Device{Id: device.Id}, nil
 	}