[ONOS-7352] Activate applications locally on bootstrap

Change-Id: I9eeff0fc6dbfa6b82c074a0e3a8ca1b4e0fa1227
diff --git a/core/store/dist/src/main/java/org/onosproject/store/app/DistributedApplicationStore.java b/core/store/dist/src/main/java/org/onosproject/store/app/DistributedApplicationStore.java
index c9981da..0ff5238 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/app/DistributedApplicationStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/app/DistributedApplicationStore.java
@@ -184,16 +184,37 @@
         apps.addListener(appsListener, activationExecutor);
         apps.addStatusChangeListener(statusChangeListener);
         coreAppId = getId(CoreService.CORE_APP_NAME);
+
+        activateExistingApplications();
         log.info("Started");
     }
 
     /**
+     * Activates applications locally on startup.
+     */
+    private void activateExistingApplications() {
+        getApplicationNames().forEach(appName -> {
+            // Only update the application state if the application has already been installed.
+            ApplicationId appId = getId(appName);
+            if (appId != null) {
+                Application application = getApplication(appId);
+                if (application != null) {
+                    InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(application.id()));
+                    if (appHolder != null && appHolder.state == ACTIVATED && !isActive(appName)) {
+                        setActive(appName);
+                        updateTime(appName);
+                    }
+                }
+            }
+        });
+    }
+
+    /**
      * Processes existing applications from the distributed map. This is done to
      * account for events that this instance may be have missed due to a staggered start.
      */
     private void bootstrapExistingApplications() {
         apps.asJavaMap().forEach((appId, holder) -> setupApplicationAndNotify(appId, holder.app(), holder.state()));
-
     }
 
     /**