BUG-7608: activate action-service element
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / BlueprintContainerRestartServiceImpl.java
index e70fc372b73f7ed5e328d9d37151fb863abb7908..5a353e1f771b5f448ad8efc0e6f32c6a1241e47a 100644 (file)
@@ -49,7 +49,6 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.blueprint.container.EventConstants;
-import org.osgi.service.event.Event;
 import org.osgi.service.event.EventHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -63,33 +62,41 @@ import org.w3c.dom.Element;
  */
 class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintContainerRestartService {
     private static final Logger LOG = LoggerFactory.getLogger(BlueprintContainerRestartServiceImpl.class);
+    private static final int CONTAINER_CREATE_TIMEOUT_IN_MINUTES = 5;
     private static final String CONFIG_MODULE_NAMESPACE_PROP = "config-module-namespace";
     private static final String CONFIG_MODULE_NAME_PROP = "config-module-name";
     private static final String CONFIG_INSTANCE_NAME_PROP = "config-instance-name";
 
-    private final ExecutorService restartExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().
-            setDaemon(true).setNameFormat("BlueprintContainerRestartService").build());
+    private final ExecutorService restartExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
+            .setDaemon(true).setNameFormat("BlueprintContainerRestartService").build());
     private final BlueprintExtenderService blueprintExtenderService;
 
     BlueprintContainerRestartServiceImpl(BlueprintExtenderService blueprintExtenderService) {
         this.blueprintExtenderService = blueprintExtenderService;
     }
 
+    public void restartContainer(final Bundle bundle, final List<Object> paths) {
+        if (restartExecutor.isShutdown()) {
+            return;
+        }
+
+        LOG.debug("restartContainer for bundle {}", bundle);
+
+        restartExecutor.execute(() -> {
+            blueprintExtenderService.destroyContainer(bundle, blueprintExtenderService.getContainer(bundle));
+            blueprintExtenderService.createContainer(bundle, paths);
+        });
+    }
+
     @Override
     public void restartContainerAndDependents(final Bundle bundle) {
-        if(restartExecutor.isShutdown()) {
+        if (restartExecutor.isShutdown()) {
             return;
         }
 
         LOG.debug("restartContainerAndDependents for bundle {}", bundle);
 
-        restartExecutor.execute(new Runnable() {
-            @Override
-            public void run() {
-                restartContainerAndDependentsInternal(bundle);
-
-            }
-        });
+        restartExecutor.execute(() -> restartContainerAndDependentsInternal(bundle));
     }
 
     private void restartContainerAndDependentsInternal(Bundle forBundle) {
@@ -104,7 +111,7 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
                 containerBundles.subList(1, containerBundles.size()));
 
         // Destroy the containers in reverse order with 'forBundle' last, ie bottom-up in the service tree.
-        for(Bundle bundle: Lists.reverse(containerBundles)) {
+        for (Bundle bundle: Lists.reverse(containerBundles)) {
             blueprintExtenderService.destroyContainer(bundle, blueprintExtenderService.getContainer(bundle));
         }
 
@@ -113,18 +120,15 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
         // containers are complete. This is done to ensure all blueprint containers are finished before we
         // restart config modules.
         final CountDownLatch containerCreationComplete = new CountDownLatch(containerBundles.size());
-        ServiceRegistration<?> eventHandlerReg = registerEventHandler(forBundle.getBundleContext(), new EventHandler() {
-            @Override
-            public void handleEvent(Event event) {
-                LOG.debug("handleEvent {} for bundle {}", event.getTopic(), event.getProperty(EventConstants.BUNDLE));
-                if(containerBundles.contains(event.getProperty(EventConstants.BUNDLE))) {
-                    containerCreationComplete.countDown();
-                }
+        ServiceRegistration<?> eventHandlerReg = registerEventHandler(forBundle.getBundleContext(), event -> {
+            LOG.debug("handleEvent {} for bundle {}", event.getTopic(), event.getProperty(EventConstants.BUNDLE));
+            if (containerBundles.contains(event.getProperty(EventConstants.BUNDLE))) {
+                containerCreationComplete.countDown();
             }
         });
 
         // Restart the containers top-down starting with 'forBundle'.
-        for(Bundle bundle: containerBundles) {
+        for (Bundle bundle: containerBundles) {
             List<Object> paths = BlueprintBundleTracker.findBlueprintPaths(bundle);
 
             LOG.info("Restarting blueprint container for bundle {} with paths {}", bundle, paths);
@@ -133,8 +137,13 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
         }
 
         try {
-            containerCreationComplete.await(5, TimeUnit.MINUTES);
-        } catch(InterruptedException e) {
+            if (!containerCreationComplete.await(CONTAINER_CREATE_TIMEOUT_IN_MINUTES, TimeUnit.MINUTES)) {
+                LOG.warn("Failed to restart all blueprint containers within {} minutes. Attempted to restart {} {} "
+                        + "but only {} completed restart", CONTAINER_CREATE_TIMEOUT_IN_MINUTES, containerBundles.size(),
+                        containerBundles, containerBundles.size() - containerCreationComplete.getCount());
+                return;
+            }
+        } catch (InterruptedException e) {
             LOG.debug("CountDownLatch await was interrupted - returning");
             return;
         }
@@ -145,20 +154,21 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
         restartConfigModules(forBundle.getBundleContext(), configModules);
     }
 
-    private void restartConfigModules(BundleContext bundleContext, List<Entry<String, ModuleIdentifier>> configModules) {
-        if(configModules.isEmpty()) {
+    private void restartConfigModules(BundleContext bundleContext, List<Entry<String,
+            ModuleIdentifier>> configModules) {
+        if (configModules.isEmpty()) {
             return;
         }
 
-        ServiceReference<ConfigSubsystemFacadeFactory> configFacadeFactoryRef =
-                bundleContext.getServiceReference(ConfigSubsystemFacadeFactory.class);
-        if(configFacadeFactoryRef == null) {
+        ServiceReference<ConfigSubsystemFacadeFactory> configFacadeFactoryRef = bundleContext
+                .getServiceReference(ConfigSubsystemFacadeFactory.class);
+        if (configFacadeFactoryRef == null) {
             LOG.debug("ConfigSubsystemFacadeFactory service reference not found");
             return;
         }
 
         ConfigSubsystemFacadeFactory configFacadeFactory = bundleContext.getService(configFacadeFactoryRef);
-        if(configFacadeFactory == null) {
+        if (configFacadeFactory == null) {
             LOG.debug("ConfigSubsystemFacadeFactory service not found");
             return;
         }
@@ -166,7 +176,8 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
         ConfigSubsystemFacade configFacade = configFacadeFactory.createFacade("BlueprintContainerRestartService");
         try {
             restartConfigModules(configModules, configFacade);
-        } catch(Exception e) {
+        } catch (ParserConfigurationException | DocumentedException | ValidationException
+                | ConflictingVersionException e) {
             LOG.error("Error restarting config modules", e);
         } finally {
             configFacade.close();
@@ -188,7 +199,7 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
         Config configMapping = configFacade.getConfigMapping();
 
         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory.getPlatformMBeanServer());
-        for(Entry<String, ModuleIdentifier> entry: configModules) {
+        for (Entry<String, ModuleIdentifier> entry: configModules) {
             String moduleNamespace = entry.getKey();
             ModuleIdentifier moduleId = entry.getValue();
             try {
@@ -200,13 +211,13 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
                 Element moduleElement = configMapping.moduleToXml(moduleNamespace, moduleId.getFactoryName(),
                         moduleId.getInstanceName(), instanceON, document);
                 modulesElement.appendChild(moduleElement);
-            } catch(InstanceNotFoundException e) {
+            } catch (InstanceNotFoundException e) {
                 LOG.warn("Error looking up config module: namespace {}, module name {}, instance {}",
                         moduleNamespace, moduleId.getFactoryName(), moduleId.getInstanceName(), e);
             }
         }
 
-        if(LOG.isDebugEnabled()) {
+        if (LOG.isDebugEnabled()) {
             LOG.debug("Pushing config xml: {}", XmlUtil.toString(dataElement));
         }
 
@@ -227,7 +238,7 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
      */
     private void findDependentContainersRecursively(Bundle bundle, Set<Bundle> containerBundles,
             List<Entry<String, ModuleIdentifier>> configModules) {
-        if(!containerBundles.add(bundle)) {
+        if (!containerBundles.add(bundle)) {
             // Already seen this bundle...
             return;
         }
@@ -238,9 +249,9 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
                 possiblyAddConfigModuleIdentifier(reference, configModules);
 
                 Bundle[] usingBundles = reference.getUsingBundles();
-                if(usingBundles != null) {
-                    for(Bundle usingBundle: usingBundles) {
-                        if(blueprintExtenderService.getContainer(usingBundle) != null) {
+                if (usingBundles != null) {
+                    for (Bundle usingBundle : usingBundles) {
+                        if (blueprintExtenderService.getContainer(usingBundle) != null) {
                             findDependentContainersRecursively(usingBundle, containerBundles, configModules);
                         }
                     }
@@ -252,7 +263,7 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
     private void possiblyAddConfigModuleIdentifier(ServiceReference<?> reference,
             List<Entry<String, ModuleIdentifier>> configModules) {
         Object moduleNamespace = reference.getProperty(CONFIG_MODULE_NAMESPACE_PROP);
-        if(moduleNamespace == null) {
+        if (moduleNamespace == null) {
             return;
         }
 
@@ -260,7 +271,7 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
                 reference);
         String instanceName = getRequiredConfigModuleProperty(CONFIG_INSTANCE_NAME_PROP, moduleNamespace,
                 reference);
-        if(moduleName == null || instanceName == null) {
+        if (moduleName == null || instanceName == null) {
             return;
         }
 
@@ -275,9 +286,10 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo
     private String getRequiredConfigModuleProperty(String propName, Object moduleNamespace,
             ServiceReference<?> reference) {
         Object value = reference.getProperty(propName);
-        if(value == null) {
-            LOG.warn("OSGi service with {} property is missing property {} therefore the config module can't be restarted",
-                    CONFIG_MODULE_NAMESPACE_PROP, propName);
+        if (value == null) {
+            LOG.warn(
+                "OSGi service with {} property is missing property {} therefore the config module can't be restarted",
+                CONFIG_MODULE_NAMESPACE_PROP, propName);
             return null;
         }