Ignore device event if not master not to cause concurrent registrations

This patch resolves ONOS-4032

Change-Id: I12e2ed7d352928fe94559ab978f5db7e9f56f1b0
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
index ac9f894..1792bce 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
@@ -21,6 +21,7 @@
 import org.onlab.packet.VlanId;
 import org.onlab.util.Bandwidth;
 import org.onlab.util.ItemNotFoundException;
+import org.onosproject.mastership.MastershipService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
@@ -67,6 +68,7 @@
     private final ResourceAdminService adminService;
     private final ResourceService resourceService;
     private final DeviceService deviceService;
+    private final MastershipService mastershipService;
     private final DriverService driverService;
     private final NetworkConfigService netcfgService;
     private final ExecutorService executor;
@@ -78,16 +80,19 @@
      * @param adminService instance invoked to register resources
      * @param resourceService {@link ResourceService} to be used
      * @param deviceService {@link DeviceService} to be used
+     * @param mastershipService {@link MastershipService} to be used
      * @param driverService {@link DriverService} to be used
      * @param netcfgService {@link NetworkConfigService} to be used.
      * @param executor executor used for processing resource registration
      */
     ResourceDeviceListener(ResourceAdminService adminService, ResourceService resourceService,
-                           DeviceService deviceService, DriverService driverService,
-                           NetworkConfigService netcfgService, ExecutorService executor) {
+                           DeviceService deviceService, MastershipService mastershipService,
+                           DriverService driverService, NetworkConfigService netcfgService,
+                           ExecutorService executor) {
         this.adminService = checkNotNull(adminService);
         this.resourceService = checkNotNull(resourceService);
         this.deviceService = checkNotNull(deviceService);
+        this.mastershipService = checkNotNull(mastershipService);
         this.driverService = checkNotNull(driverService);
         this.netcfgService = checkNotNull(netcfgService);
         this.executor = checkNotNull(executor);
@@ -96,6 +101,11 @@
     @Override
     public void event(DeviceEvent event) {
         Device device = event.subject();
+        // registration happens only when the caller is the master of the device
+        if (!mastershipService.isLocalMaster(device.id())) {
+            return;
+        }
+
         switch (event.type()) {
             case DEVICE_ADDED:
                 registerDeviceResource(device);
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java
index e2666dd..b4b723e 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java
@@ -23,6 +23,7 @@
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.mastership.MastershipService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.config.ConfigFactory;
 import org.onosproject.net.config.NetworkConfigListener;
@@ -63,6 +64,9 @@
     protected DeviceService deviceService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MastershipService mastershipService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigRegistry cfgRegistry;
 
     private final Logger log = getLogger(getClass());
@@ -92,7 +96,7 @@
         cfgRegistry.addListener(cfgListener);
 
         deviceListener = new ResourceDeviceListener(adminService, resourceService,
-                deviceService, driverService, cfgRegistry, executor);
+                deviceService, mastershipService, driverService, cfgRegistry, executor);
         deviceService.addListener(deviceListener);
 
         // TODO Attempt initial registration of existing resources?