X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fblueprint%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fblueprint%2FBlueprintContainerRestartServiceImpl.java;h=4a8c0157d7d9ff072d6554c15b9257ff44eda530;hb=abaef4a5ae37f27542155457fe7306a4662b1eeb;hp=7985c5bce00e2090016986394bc17287033b866e;hpb=785094ca8092934bc5ba7dcdb75fbc50665bb6f6;p=controller.git diff --git a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintContainerRestartServiceImpl.java b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintContainerRestartServiceImpl.java index 7985c5bce0..4a8c0157d7 100644 --- a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintContainerRestartServiceImpl.java +++ b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintContainerRestartServiceImpl.java @@ -7,59 +7,33 @@ */ package org.opendaylight.controller.blueprint; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.Lists; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import java.lang.management.ManagementFactory; -import java.util.AbstractMap.SimpleEntry; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; -import java.util.Dictionary; -import java.util.Hashtable; import java.util.LinkedHashSet; import java.util.List; -import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; -import javax.management.InstanceNotFoundException; -import javax.management.ObjectName; -import javax.xml.parsers.ParserConfigurationException; import org.apache.aries.blueprint.services.BlueprintExtenderService; import org.apache.aries.quiesce.participant.QuiesceParticipant; import org.apache.aries.util.AriesFrameworkUtil; -import org.opendaylight.controller.config.api.ConfigRegistry; -import org.opendaylight.controller.config.api.ConflictingVersionException; -import org.opendaylight.controller.config.api.ModuleIdentifier; -import org.opendaylight.controller.config.api.ValidationException; -import org.opendaylight.controller.config.facade.xml.ConfigExecution; -import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade; -import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory; -import org.opendaylight.controller.config.facade.xml.TestOption; -import org.opendaylight.controller.config.facade.xml.mapping.config.Config; -import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType; -import org.opendaylight.controller.config.util.ConfigRegistryJMXClient; -import org.opendaylight.controller.config.util.xml.DocumentedException; -import org.opendaylight.controller.config.util.xml.XmlElement; -import org.opendaylight.controller.config.util.xml.XmlMappingConstants; -import org.opendaylight.controller.config.util.xml.XmlUtil; import org.osgi.framework.Bundle; 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.EventHandler; +import org.osgi.service.blueprint.container.BlueprintEvent; +import org.osgi.service.blueprint.container.BlueprintListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; /** * Implementation of the BlueprintContainerRestartService. @@ -69,9 +43,6 @@ 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()); @@ -88,12 +59,13 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo } public void restartContainer(final Bundle bundle, final List paths) { + LOG.debug("restartContainer for bundle {}", bundle); + if (restartExecutor.isShutdown()) { + LOG.debug("Already closed - returning"); return; } - LOG.debug("restartContainer for bundle {}", bundle); - restartExecutor.execute(() -> { blueprintExtenderService.destroyContainer(bundle, blueprintExtenderService.getContainer(bundle)); blueprintExtenderService.createContainer(bundle, paths); @@ -111,14 +83,13 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo restartExecutor.execute(() -> restartContainerAndDependentsInternal(bundle)); } - private void restartContainerAndDependentsInternal(Bundle forBundle) { - Preconditions.checkNotNull(blueprintExtenderService); - Preconditions.checkNotNull(quiesceParticipant); + private void restartContainerAndDependentsInternal(final Bundle forBundle) { + requireNonNull(blueprintExtenderService); + requireNonNull(quiesceParticipant); // We use a LinkedHashSet to preserve insertion order as we walk the service usage hierarchy. Set containerBundlesSet = new LinkedHashSet<>(); - List> configModules = new ArrayList<>(); - findDependentContainersRecursively(forBundle, containerBundlesSet, configModules); + findDependentContainersRecursively(forBundle, containerBundlesSet); List containerBundles = new ArrayList<>(containerBundlesSet); @@ -131,10 +102,17 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo // restart config modules. final CountDownLatch containerCreationComplete = new CountDownLatch(containerBundles.size()); ServiceRegistration eventHandlerReg = registerEventHandler(forBundle.getBundleContext(), event -> { - final Bundle bundle = (Bundle) event.getProperty(EventConstants.BUNDLE); - LOG.debug("handleEvent {} for bundle {}", event.getTopic(), bundle); - if (containerBundles.contains(bundle)) { + final Bundle bundle = event.getBundle(); + if (event.isReplay()) { + LOG.trace("Got replay BlueprintEvent {} for bundle {}", event.getType(), bundle); + return; + } + + LOG.debug("Got BlueprintEvent {} for bundle {}", event.getType(), bundle); + if (containerBundles.contains(bundle) + && (event.getType() == BlueprintEvent.CREATED || event.getType() == BlueprintEvent.FAILURE)) { containerCreationComplete.countDown(); + LOG.debug("containerCreationComplete is now {}", containerCreationComplete.getCount()); } }); @@ -151,15 +129,14 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo containerBundles, containerBundles.size() - containerCreationComplete.getCount()); return; } - } catch (InterruptedException e) { + } catch (final InterruptedException e) { LOG.debug("CountDownLatch await was interrupted - returning"); return; } AriesFrameworkUtil.safeUnregisterService(eventHandlerReg); - // Now restart any associated config system Modules. - restartConfigModules(forBundle.getBundleContext(), configModules); + LOG.info("Finished restarting blueprint containers for bundle {} and its dependent bundles", forBundle); } /** @@ -208,7 +185,7 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo }, Collections.singletonList(nextBundle)); } - private void createContainers(List containerBundles) { + private void createContainers(final List containerBundles) { containerBundles.forEach(bundle -> { List paths = BlueprintBundleTracker.findBlueprintPaths(bundle); @@ -218,90 +195,14 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo }); } - private void restartConfigModules(BundleContext bundleContext, List> configModules) { - if (configModules.isEmpty()) { - return; - } - - ServiceReference configFacadeFactoryRef = bundleContext - .getServiceReference(ConfigSubsystemFacadeFactory.class); - if (configFacadeFactoryRef == null) { - LOG.debug("ConfigSubsystemFacadeFactory service reference not found"); - return; - } - - ConfigSubsystemFacadeFactory configFacadeFactory = bundleContext.getService(configFacadeFactoryRef); - if (configFacadeFactory == null) { - LOG.debug("ConfigSubsystemFacadeFactory service not found"); - return; - } - - try (ConfigSubsystemFacade configFacade = configFacadeFactory.createFacade( - "BlueprintContainerRestartService")) { - restartConfigModules(configModules, configFacade); - } catch (ParserConfigurationException | DocumentedException | ValidationException - | ConflictingVersionException e) { - LOG.error("Error restarting config modules", e); - } finally { - bundleContext.ungetService(configFacadeFactoryRef); - } - - } - - private void restartConfigModules(List> configModules, - ConfigSubsystemFacade configFacade) throws ParserConfigurationException, DocumentedException, - ValidationException, ConflictingVersionException { - - Document document = XmlUtil.newDocument(); - Element dataElement = XmlUtil.createElement(document, XmlMappingConstants.DATA_KEY, Optional.absent()); - Element modulesElement = XmlUtil.createElement(document, XmlMappingConstants.MODULES_KEY, - Optional.of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG)); - dataElement.appendChild(modulesElement); - - Config configMapping = configFacade.getConfigMapping(); - - ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory.getPlatformMBeanServer()); - for (Entry entry : configModules) { - String moduleNamespace = entry.getKey(); - ModuleIdentifier moduleId = entry.getValue(); - try { - ObjectName instanceON = configRegistryClient.lookupConfigBean(moduleId.getFactoryName(), - moduleId.getInstanceName()); - - LOG.debug("Found config module instance ObjectName: {}", instanceON); - - Element moduleElement = configMapping.moduleToXml(moduleNamespace, moduleId.getFactoryName(), - moduleId.getInstanceName(), instanceON, document); - modulesElement.appendChild(moduleElement); - } catch (InstanceNotFoundException e) { - LOG.warn("Error looking up config module: namespace {}, module name {}, instance {}", - moduleNamespace, moduleId.getFactoryName(), moduleId.getInstanceName(), e); - } - } - - if (LOG.isDebugEnabled()) { - LOG.debug("Pushing config xml: {}", XmlUtil.toString(dataElement)); - } - - ConfigExecution execution = new ConfigExecution(configMapping, XmlElement.fromDomElement(dataElement), - TestOption.testThenSet, EditStrategyType.recreate); - configFacade.executeConfigExecution(execution); - configFacade.commitSilentTransaction(); - } - /** * Recursively finds the services registered by the given bundle and the bundles using those services. - * User bundles that have an associated blueprint container are added to containerBundles. In addition, - * if a registered service has an associated config system Module, as determined via the presence of - * certain service properties, the ModuleIdentifier is added to the configModules list. + * User bundles that have an associated blueprint container are added to containerBundles. * * @param bundle the bundle to traverse * @param containerBundles the current set of bundles containing blueprint containers - * @param configModules the current set of bundles containing config modules */ - private void findDependentContainersRecursively(Bundle bundle, Set containerBundles, - List> configModules) { + private void findDependentContainersRecursively(final Bundle bundle, final Set containerBundles) { if (!containerBundles.add(bundle)) { // Already seen this bundle... return; @@ -310,13 +211,11 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo ServiceReference[] references = bundle.getRegisteredServices(); if (references != null) { for (ServiceReference reference : references) { - possiblyAddConfigModuleIdentifier(reference, configModules); - Bundle[] usingBundles = reference.getUsingBundles(); if (usingBundles != null) { for (Bundle usingBundle : usingBundles) { if (blueprintExtenderService.getContainer(usingBundle) != null) { - findDependentContainersRecursively(usingBundle, containerBundles, configModules); + findDependentContainersRecursively(usingBundle, containerBundles); } } } @@ -324,51 +223,15 @@ class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintCo } } - private void possiblyAddConfigModuleIdentifier(ServiceReference reference, - List> configModules) { - Object moduleNamespace = reference.getProperty(CONFIG_MODULE_NAMESPACE_PROP); - if (moduleNamespace == null) { - return; - } - - String moduleName = getRequiredConfigModuleProperty(CONFIG_MODULE_NAME_PROP, moduleNamespace, - reference); - String instanceName = getRequiredConfigModuleProperty(CONFIG_INSTANCE_NAME_PROP, moduleNamespace, - reference); - if (moduleName == null || instanceName == null) { - return; - } - - LOG.debug("Found service with config module: namespace {}, module name {}, instance {}", - moduleNamespace, moduleName, instanceName); - - configModules.add(new SimpleEntry<>(moduleNamespace.toString(), - new ModuleIdentifier(moduleName, instanceName))); - } - - @Nullable - 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); - return null; - } - - return value.toString(); - } - - private ServiceRegistration registerEventHandler(BundleContext bundleContext, EventHandler handler) { - Dictionary props = new Hashtable<>(); - props.put(org.osgi.service.event.EventConstants.EVENT_TOPIC, - new String[]{EventConstants.TOPIC_CREATED, EventConstants.TOPIC_FAILURE}); - return bundleContext.registerService(EventHandler.class.getName(), handler, props); + private static ServiceRegistration registerEventHandler(final BundleContext bundleContext, + final BlueprintListener listener) { + return bundleContext.registerService(BlueprintListener.class, listener, null); } @Override public void close() { + LOG.debug("Closing"); + restartExecutor.shutdownNow(); } }