[emu][onos-3159] fix concurrency flaws in OpenFlowDeviceProvider.java and OpenFlowGroupProvider.java

Change-Id: I76b21b221d3ef71e1701c13810c4df374afe1776
diff --git a/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java b/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java
index 329df20..a7e334f 100644
--- a/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java
+++ b/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProvider.java
@@ -319,6 +319,9 @@
             }
             DeviceId did = deviceId(uri(dpid));
             OpenFlowSwitch sw = controller.getSwitch(dpid);
+            if (sw == null) {
+                return;
+            }
 
             ChassisId cId = new ChassisId(dpid.value());
 
@@ -339,9 +342,14 @@
             providerService.updatePorts(did, buildPortDescriptions(sw));
 
             PortStatsCollector psc =
-                    new PortStatsCollector(controller.getSwitch(dpid), portStatsPollFrequency);
+                    new PortStatsCollector(sw, portStatsPollFrequency);
             psc.start();
             collectors.put(dpid, psc);
+
+            //figure out race condition for collectors.remove() and collectors.put()
+            if (controller.getSwitch(dpid) == null) {
+                switchRemoved(dpid);
+            }
         }
 
         @Override
@@ -364,6 +372,9 @@
             }
             DeviceId did = deviceId(uri(dpid));
             OpenFlowSwitch sw = controller.getSwitch(dpid);
+            if (sw == null) {
+                return;
+            }
             providerService.updatePorts(did, buildPortDescriptions(sw));
         }
 
diff --git a/providers/openflow/group/src/main/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProvider.java b/providers/openflow/group/src/main/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProvider.java
index 78650fe..8acf08e 100644
--- a/providers/openflow/group/src/main/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProvider.java
+++ b/providers/openflow/group/src/main/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProvider.java
@@ -334,12 +334,20 @@
         @Override
         public void switchAdded(Dpid dpid) {
             OpenFlowSwitch sw = controller.getSwitch(dpid);
+            if (sw == null) {
+                return;
+            }
             if (isGroupSupported(sw)) {
                 GroupStatsCollector gsc = new GroupStatsCollector(
                         controller.getSwitch(dpid), POLL_INTERVAL);
                 gsc.start();
                 collectors.put(dpid, gsc);
             }
+
+            //figure out race condition
+            if (controller.getSwitch(dpid) == null) {
+                switchRemoved(dpid);
+            }
         }
 
         @Override