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/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java b/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
index 076ae4b..49ae748 100644
--- a/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
+++ b/core/net/src/main/java/org/onosproject/cfg/impl/ComponentConfigManager.java
@@ -74,7 +74,7 @@
     private final Logger log = getLogger(getClass());
 
     private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
-    private final InternalAccumulator accumulator = new InternalAccumulator();
+    private InternalAccumulator accumulator;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected ComponentConfigStore store;
@@ -89,12 +89,17 @@
 
     @Activate
     public void activate() {
+        // Accumulator depends on SharedExecutors, we create a new one at each
+        // activation to avoid using executors that have been terminated, which
+        // can happen during core bundle refresh.
+        accumulator = new InternalAccumulator();
         store.setDelegate(delegate);
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
+        accumulator = null;
         store.unsetDelegate(delegate);
         log.info("Stopped");
     }
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;
+            }
         }
     }
 }