Fix issues seen during bundle refresh with shared executors

Shared executors are shut down by CoreManager during bundle refresh,
which creates issues in many apps/subsystems. This change fixes only
those issues seen in TOST.

- Fix NPE, by shutting down executors only if initialized
- Fix timer already cancelled by re-creating resources depending
  on terminated executors

Change-Id: I00cfb451c54ada67ab94b09edb486d49a5bd084f
diff --git a/utils/misc/src/main/java/org/onlab/util/SharedExecutors.java b/utils/misc/src/main/java/org/onlab/util/SharedExecutors.java
index 6d92ce6..cc99bcc 100644
--- a/utils/misc/src/main/java/org/onlab/util/SharedExecutors.java
+++ b/utils/misc/src/main/java/org/onlab/util/SharedExecutors.java
@@ -108,12 +108,14 @@
      */
     public static void shutdown() {
         synchronized (SHARED_TIMER_LOCK) {
-            sharedTimer.shutdown();
-            singleThreadExecutor.backingExecutor().shutdown();
-            poolThreadExecutor.backingExecutor().shutdown();
-            sharedTimer = null;
-            singleThreadExecutor = null;
-            poolThreadExecutor = null;
+            if (sharedTimer != null) {
+                sharedTimer.shutdown();
+                singleThreadExecutor.backingExecutor().shutdown();
+                poolThreadExecutor.backingExecutor().shutdown();
+                sharedTimer = null;
+                singleThreadExecutor = null;
+                poolThreadExecutor = null;
+            }
         }
     }
 
diff --git a/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java b/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java
index cfba06d..9141438 100644
--- a/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java
+++ b/utils/misc/src/main/java/org/onlab/util/SharedScheduledExecutors.java
@@ -116,10 +116,12 @@
      */
     public static void shutdown() {
         synchronized (EXECUTORS_LOCK) {
-            singleThreadExecutor.backingExecutor().shutdown();
-            singleThreadExecutor = null;
-            poolThreadExecutor.backingExecutor().shutdown();
-            poolThreadExecutor = null;
+            if (singleThreadExecutor != null) {
+                singleThreadExecutor.backingExecutor().shutdown();
+                singleThreadExecutor = null;
+                poolThreadExecutor.backingExecutor().shutdown();
+                poolThreadExecutor = null;
+            }
         }
     }
 }