Fixed a slew of shutdown exceptions that arose due to improper or out-of-order resource clean-up, e.g. listeners, timers, executors.

Change-Id: I37c351c4202b32e92c076d9d566b96d7ff8d313a
diff --git a/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/PortStatsCollector.java b/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/PortStatsCollector.java
index 36d7948..c872a82 100644
--- a/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/PortStatsCollector.java
+++ b/providers/openflow/device/src/main/java/org/onosproject/provider/of/device/impl/PortStatsCollector.java
@@ -45,8 +45,7 @@
     private final AtomicLong xidAtomic = new AtomicLong(1);
 
     private Timeout timeout;
-
-    private boolean stopTimer = false;
+    private volatile boolean stopped;
 
     /**
      * Creates a GroupStatsCollector object.
@@ -60,23 +59,22 @@
     }
 
     @Override
-    public void run(Timeout timeout) throws Exception {
+    public void run(Timeout to) throws Exception {
+        if (stopped || timeout.isCancelled()) {
+            return;
+        }
         log.trace("Collecting stats for {}", sw.getStringId());
 
         sendPortStatistic();
 
-        if (!this.stopTimer) {
+        if (!stopped && !timeout.isCancelled()) {
             log.trace("Scheduling stats collection in {} seconds for {}",
                     this.refreshInterval, this.sw.getStringId());
-            timeout.getTimer().newTimeout(this, refreshInterval,
-                    TimeUnit.SECONDS);
+            timeout.getTimer().newTimeout(this, refreshInterval, TimeUnit.SECONDS);
         }
     }
 
     private void sendPortStatistic() {
-        if (log.isTraceEnabled()) {
-            log.trace("sendGroupStatistics {}:{}", sw.getStringId(), sw.getRole());
-        }
         if (sw.getRole() != RoleState.MASTER) {
             return;
         }
@@ -91,17 +89,18 @@
     /**
      * Starts the collector.
      */
-    public void start() {
+    public synchronized void start() {
         log.info("Starting Port Stats collection thread for {}", sw.getStringId());
+        stopped = false;
         timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
     }
 
     /**
      * Stops the collector.
      */
-    public void stop() {
+    public synchronized void stop() {
         log.info("Stopping Port Stats collection thread for {}", sw.getStringId());
-        this.stopTimer = true;
+        stopped = true;
         timeout.cancel();
     }
 }