Modifying preDeactivate hook in OFControllerImpl to remove double deactivation causing NPE.
The preDeactivate is needed to clean up connections when the app is deactivated but needs
to be called also when the component goes down.
If the two,app and component, go down together we should not trigger the cleaning twice.

Change-Id: I66a40297c78f995c5bcd95226efc33fb135732b3
diff --git a/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/Controller.java b/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/Controller.java
index e515a62..93301a4 100644
--- a/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/Controller.java
+++ b/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/Controller.java
@@ -64,7 +64,6 @@
 
 /**
  * The main controller class.  Handles all setup and network listeners
- * - Distributed ownership control of switch through IControllerRegistryService
  */
 public class Controller {
 
diff --git a/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImpl.java b/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImpl.java
index 333a756..ba3eab5 100644
--- a/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImpl.java
+++ b/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImpl.java
@@ -155,25 +155,28 @@
 
     @Activate
     public void activate(ComponentContext context) {
-        coreService.registerApplication(APP_ID, this::preDeactivate);
+        coreService.registerApplication(APP_ID, this::cleanup);
         cfgService.registerProperties(getClass());
         ctrl.setConfigParams(context.getProperties());
         ctrl.start(agent, driverService);
     }
 
-    private void preDeactivate() {
-        // Close listening channel and all OF channels before deactivating
+    private void cleanup() {
+        // Close listening channel and all OF channels. Clean information about switches
+        // before deactivating
         ctrl.stop();
         connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
+        connectedSwitches.clear();
+        activeMasterSwitches.clear();
+        activeEqualSwitches.clear();
     }
 
     @Deactivate
     public void deactivate() {
-        preDeactivate();
+        if (!connectedSwitches.isEmpty()) {
+            cleanup();
+        }
         cfgService.unregisterProperties(getClass(), false);
-        connectedSwitches.clear();
-        activeMasterSwitches.clear();
-        activeEqualSwitches.clear();
     }
 
     @Modified