X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fblueprint%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fblueprint%2FBlueprintBundleTracker.java;h=7ad8ddb4e3a5e4b9fd83096d99344cff4a257a06;hb=abaef4a5ae37f27542155457fe7306a4662b1eeb;hp=6c267ac5fe62494793d29270420eccfe1685912d;hpb=67ff0fc78b2933b8b4f5a8544c7639499824e622;p=controller.git diff --git a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintBundleTracker.java b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintBundleTracker.java index 6c267ac5fe..7ad8ddb4e3 100644 --- a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintBundleTracker.java +++ b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/BlueprintBundleTracker.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.blueprint; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -18,9 +19,12 @@ import java.util.Hashtable; import java.util.List; import org.apache.aries.blueprint.NamespaceHandler; import org.apache.aries.blueprint.services.BlueprintExtenderService; +import org.apache.aries.quiesce.participant.QuiesceParticipant; import org.apache.aries.util.AriesFrameworkUtil; +import org.eclipse.jdt.annotation.Nullable; +import org.gaul.modernizer_maven_annotations.SuppressModernizer; import org.opendaylight.controller.blueprint.ext.OpendaylightNamespaceHandler; -import org.opendaylight.controller.config.api.ConfigSystemService; +import org.opendaylight.yangtools.util.xml.UntrustedXML; import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -29,9 +33,8 @@ import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import org.osgi.framework.SynchronousBundleListener; import org.osgi.service.blueprint.container.BlueprintContainer; -import org.osgi.service.blueprint.container.EventConstants; -import org.osgi.service.event.Event; -import org.osgi.service.event.EventHandler; +import org.osgi.service.blueprint.container.BlueprintEvent; +import org.osgi.service.blueprint.container.BlueprintListener; import org.osgi.util.tracker.BundleTracker; import org.osgi.util.tracker.BundleTrackerCustomizer; import org.osgi.util.tracker.ServiceTracker; @@ -47,17 +50,20 @@ import org.slf4j.LoggerFactory; * * @author Thomas Pantelis */ -public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCustomizer, EventHandler, +public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCustomizer, BlueprintListener, SynchronousBundleListener { private static final Logger LOG = LoggerFactory.getLogger(BlueprintBundleTracker.class); - private static final String BLUEPRINT_FILE_PATH = "org/opendaylight/blueprint/"; + private static final String ODL_CUSTOM_BLUEPRINT_FILE_PATH = "org/opendaylight/blueprint/"; + private static final String STANDARD_BLUEPRINT_FILE_PATH = "OSGI-INF/blueprint/"; private static final String BLUEPRINT_FLE_PATTERN = "*.xml"; private static final long SYSTEM_BUNDLE_ID = 0; - private ServiceTracker serviceTracker; + private ServiceTracker blueprintExtenderServiceTracker; + private ServiceTracker quiesceParticipantTracker; private BundleTracker bundleTracker; private BundleContext bundleContext; private volatile BlueprintExtenderService blueprintExtenderService; + private volatile QuiesceParticipant quiesceParticipant; private volatile ServiceRegistration blueprintContainerRestartReg; private volatile BlueprintContainerRestartServiceImpl restartService; private volatile boolean shuttingDown; @@ -68,9 +74,14 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus * Implemented from BundleActivator. */ @Override - public void start(BundleContext context) { + public void start(final BundleContext context) { LOG.info("Starting {}", getClass().getSimpleName()); + // CONTROLLER-1867: force UntrustedXML initialization, so that it uses our TCCL to initialize + UntrustedXML.newDocumentBuilder(); + + restartService = new BlueprintContainerRestartServiceImpl(); + bundleContext = context; registerBlueprintEventHandler(context); @@ -79,59 +90,101 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus bundleTracker = new BundleTracker<>(context, Bundle.ACTIVE, this); - serviceTracker = new ServiceTracker<>(context, BlueprintExtenderService.class.getName(), + blueprintExtenderServiceTracker = new ServiceTracker<>(context, BlueprintExtenderService.class, new ServiceTrackerCustomizer() { @Override public BlueprintExtenderService addingService( - ServiceReference reference) { - blueprintExtenderService = reference.getBundle().getBundleContext().getService(reference); - bundleTracker.open(); - - context.addBundleListener(BlueprintBundleTracker.this); + final ServiceReference reference) { + return onBlueprintExtenderServiceAdded(reference); + } - LOG.debug("Got BlueprintExtenderService"); + @Override + public void modifiedService(final ServiceReference reference, + final BlueprintExtenderService service) { + } - restartService = new BlueprintContainerRestartServiceImpl(blueprintExtenderService); - blueprintContainerRestartReg = context.registerService( - BlueprintContainerRestartService.class.getName(), restartService, new Hashtable<>()); + @Override + public void removedService(final ServiceReference reference, + final BlueprintExtenderService service) { + } + }); + blueprintExtenderServiceTracker.open(); - return blueprintExtenderService; + quiesceParticipantTracker = new ServiceTracker<>(context, QuiesceParticipant.class, + new ServiceTrackerCustomizer() { + @Override + public QuiesceParticipant addingService( + final ServiceReference reference) { + return onQuiesceParticipantAdded(reference); } @Override - public void modifiedService(ServiceReference reference, - BlueprintExtenderService service) { + public void modifiedService(final ServiceReference reference, + final QuiesceParticipant service) { } @Override - public void removedService(ServiceReference reference, - BlueprintExtenderService service) { + public void removedService(final ServiceReference reference, + final QuiesceParticipant service) { } }); - serviceTracker.open(); + quiesceParticipantTracker.open(); + } + + @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", + justification = "https://github.com/spotbugs/spotbugs/issues/811") + private QuiesceParticipant onQuiesceParticipantAdded(final ServiceReference reference) { + quiesceParticipant = reference.getBundle().getBundleContext().getService(reference); + + LOG.debug("Got QuiesceParticipant"); + + restartService.setQuiesceParticipant(quiesceParticipant); + + return quiesceParticipant; } - private void registerNamespaceHandler(BundleContext context) { - Dictionary props = new Hashtable<>(); + @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", + justification = "https://github.com/spotbugs/spotbugs/issues/811") + private BlueprintExtenderService onBlueprintExtenderServiceAdded( + final ServiceReference reference) { + blueprintExtenderService = reference.getBundle().getBundleContext().getService(reference); + bundleTracker.open(); + + bundleContext.addBundleListener(BlueprintBundleTracker.this); + + LOG.debug("Got BlueprintExtenderService"); + + restartService.setBlueprintExtenderService(blueprintExtenderService); + + blueprintContainerRestartReg = bundleContext.registerService(BlueprintContainerRestartService.class, + restartService, null); + + return blueprintExtenderService; + } + + private void registerNamespaceHandler(final BundleContext context) { + Dictionary props = emptyDict(); props.put("osgi.service.blueprint.namespace", OpendaylightNamespaceHandler.NAMESPACE_1_0_0); - namespaceReg = context.registerService(NamespaceHandler.class.getName(), - new OpendaylightNamespaceHandler(), props); + namespaceReg = context.registerService(NamespaceHandler.class, new OpendaylightNamespaceHandler(), props); + } + + private void registerBlueprintEventHandler(final BundleContext context) { + eventHandlerReg = context.registerService(BlueprintListener.class, this, null); } - private void registerBlueprintEventHandler(BundleContext context) { - Dictionary props = new Hashtable<>(); - props.put(org.osgi.service.event.EventConstants.EVENT_TOPIC, - new String[]{EventConstants.TOPIC_CREATED, EventConstants.TOPIC_FAILURE}); - eventHandlerReg = context.registerService(EventHandler.class.getName(), this, props); + @SuppressModernizer + private static Dictionary emptyDict() { + return new Hashtable<>(); } /** * Implemented from BundleActivator. */ @Override - public void stop(BundleContext context) { + public void stop(final BundleContext context) { bundleTracker.close(); - serviceTracker.close(); + blueprintExtenderServiceTracker.close(); + quiesceParticipantTracker.close(); AriesFrameworkUtil.safeUnregisterService(eventHandlerReg); AriesFrameworkUtil.safeUnregisterService(namespaceReg); @@ -142,7 +195,7 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus * Implemented from SynchronousBundleListener. */ @Override - public void bundleChanged(BundleEvent event) { + public void bundleChanged(final BundleEvent event) { // If the system bundle (id 0) is stopping, do an orderly shutdown of all blueprint containers. On // shutdown the system bundle is stopped first. if (event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) { @@ -154,22 +207,22 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus * Implemented from BundleActivator. */ @Override - public Bundle addingBundle(Bundle bundle, BundleEvent event) { + public Bundle addingBundle(final Bundle bundle, final BundleEvent event) { modifiedBundle(bundle, event, bundle); return bundle; } /** - * Implemented from BundleActivator. + * Implemented from BundleTrackerCustomizer. */ @Override - public void modifiedBundle(Bundle bundle, BundleEvent event, Bundle object) { + public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Bundle object) { if (shuttingDown) { return; } if (bundle.getState() == Bundle.ACTIVE) { - List paths = findBlueprintPaths(bundle); + List paths = findBlueprintPaths(bundle, ODL_CUSTOM_BLUEPRINT_FILE_PATH); if (!paths.isEmpty()) { LOG.info("Creating blueprint container for bundle {} with paths {}", bundle, paths); @@ -180,45 +233,50 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus } /** - * Implemented from BundleActivator. + * Implemented from BundleTrackerCustomizer. */ @Override - public void removedBundle(Bundle bundle, BundleEvent event, Bundle object) { + public void removedBundle(final Bundle bundle, final BundleEvent event, final Bundle object) { // BlueprintExtenderService will handle this. } /** - * Implemented from EventHandler to listen for blueprint events. + * Implemented from BlueprintListener to listen for blueprint events. * * @param event the event to handle */ @Override - public void handleEvent(Event event) { - if (EventConstants.TOPIC_CREATED.equals(event.getTopic())) { - LOG.info("Blueprint container for bundle {} was successfully created", - event.getProperty(EventConstants.BUNDLE)); - } else if (EventConstants.TOPIC_FAILURE.equals(event.getTopic())) { - // If the container timed out waiting for dependencies, we'll destroy it and start it again. This - // is indicated via a non-null DEPENDENCIES property containing the missing dependencies. The - // default timeout is 5 min and ideally we would set this to infinite but the timeout can only - // be set at the bundle level in the manifest - there's no way to set it globally. - if (event.getProperty(EventConstants.DEPENDENCIES) != null) { - Bundle bundle = (Bundle) event.getProperty(EventConstants.BUNDLE); - - List paths = findBlueprintPaths(bundle); - if (!paths.isEmpty()) { - LOG.warn("Blueprint container for bundle {} timed out waiting for dependencies - restarting it", - event.getProperty(EventConstants.BUNDLE)); - - restartService.restartContainer(bundle, paths); - } + public void blueprintEvent(final BlueprintEvent event) { + if (event.getType() == BlueprintEvent.CREATED) { + LOG.info("Blueprint container for bundle {} was successfully created", event.getBundle()); + return; + } + + // If the container timed out waiting for dependencies, we'll destroy it and start it again. This + // is indicated via a non-null DEPENDENCIES property containing the missing dependencies. The + // default timeout is 5 min and ideally we would set this to infinite but the timeout can only + // be set at the bundle level in the manifest - there's no way to set it globally. + if (event.getType() == BlueprintEvent.FAILURE && event.getDependencies() != null) { + Bundle bundle = event.getBundle(); + + List paths = findBlueprintPaths(bundle); + if (!paths.isEmpty()) { + LOG.warn("Blueprint container for bundle {} timed out waiting for dependencies - restarting it", + bundle); + + restartService.restartContainer(bundle, paths); } } } + static List findBlueprintPaths(final Bundle bundle) { + List paths = findBlueprintPaths(bundle, STANDARD_BLUEPRINT_FILE_PATH); + return !paths.isEmpty() ? paths : findBlueprintPaths(bundle, ODL_CUSTOM_BLUEPRINT_FILE_PATH); + } + @SuppressWarnings({ "rawtypes", "unchecked" }) - static List findBlueprintPaths(Bundle bundle) { - Enumeration rntries = bundle.findEntries(BLUEPRINT_FILE_PATH, BLUEPRINT_FLE_PATTERN, false); + private static List findBlueprintPaths(final Bundle bundle, final String path) { + Enumeration rntries = bundle.findEntries(path, BLUEPRINT_FLE_PATTERN, false); if (rntries == null) { return Collections.emptyList(); } else { @@ -231,12 +289,6 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus restartService.close(); - // Close all CSS modules first. - ConfigSystemService configSystem = getOSGiService(ConfigSystemService.class); - if (configSystem != null) { - configSystem.closeAllConfigModules(); - } - LOG.info("Shutting down all blueprint containers..."); Collection containerBundles = new HashSet<>(Arrays.asList(bundleContext.getBundles())); @@ -255,7 +307,7 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus LOG.info("Shutdown of blueprint containers complete"); } - private List getBundlesToDestroy(Collection containerBundles) { + private static List getBundlesToDestroy(final Collection containerBundles) { List bundlesToDestroy = new ArrayList<>(); // Find all container bundles that either have no registered services or whose services are no @@ -276,7 +328,7 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus } if (!bundlesToDestroy.isEmpty()) { - Collections.sort(bundlesToDestroy, (b1, b2) -> (int) (b2.getLastModified() - b1.getLastModified())); + bundlesToDestroy.sort((b1, b2) -> (int) (b2.getLastModified() - b1.getLastModified())); LOG.debug("Selected bundles {} for destroy (no services in use)", bundlesToDestroy); } else { @@ -288,32 +340,9 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus // ID, we're picking the bundle that was (likely) started after all the others and thus // is likely the safest to destroy at this point. - ServiceReference ref = null; - for (Bundle bundle : containerBundles) { - ServiceReference[] references = bundle.getRegisteredServices(); - if (references == null) { - continue; - } - - for (ServiceReference reference : references) { - // We did check the service usage above but it's possible the usage has changed since - // then, - if (getServiceUsage(reference) == 0) { - continue; - } - - // Choose 'reference' if it has a lower service ranking or, if the rankings are equal - // which is usually the case, if it has a higher service ID. For the latter the < 0 - // check looks backwards but that's how ServiceReference#compareTo is documented to work. - if (ref == null || reference.compareTo(ref) < 0) { - LOG.debug("Currently selecting bundle {} for destroy (with reference {})", bundle, reference); - ref = reference; - } - } - } - - if (ref != null) { - bundlesToDestroy.add(ref.getBundle()); + Bundle bundle = findBundleWithHighestUsedServiceId(containerBundles); + if (bundle != null) { + bundlesToDestroy.add(bundle); } LOG.debug("Selected bundle {} for destroy (lowest ranking service or highest service ID)", @@ -323,32 +352,35 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus return bundlesToDestroy; } - private static int getServiceUsage(ServiceReference ref) { - Bundle[] usingBundles = ref.getUsingBundles(); - return usingBundles != null ? usingBundles.length : 0; - } - - private T getOSGiService(Class serviceInterface) { - try { - ServiceReference serviceReference = bundleContext.getServiceReference(serviceInterface); - if (serviceReference == null) { - LOG.warn("{} service reference not found", serviceInterface.getSimpleName()); - return null; + private static @Nullable Bundle findBundleWithHighestUsedServiceId(final Collection containerBundles) { + ServiceReference highestServiceRef = null; + for (Bundle bundle : containerBundles) { + ServiceReference[] references = bundle.getRegisteredServices(); + if (references == null) { + continue; } - T service = bundleContext.getService(serviceReference); - if (service == null) { - // This could happen on shutdown if the service was already unregistered so we log as debug. - LOG.debug("{} service instance was not found", serviceInterface.getSimpleName()); - } + for (ServiceReference reference : references) { + // We did check the service usage previously but it's possible the usage has changed since then. + if (getServiceUsage(reference) == 0) { + continue; + } - return service; - } catch (IllegalStateException e) { - // This is thrown if the BundleContext is no longer valid which is possible on shutdown so we - // log as debug. - LOG.debug("Error obtaining OSGi service {}", serviceInterface.getSimpleName(), e); + // Choose 'reference' if it has a lower service ranking or, if the rankings are equal + // which is usually the case, if it has a higher service ID. For the latter the < 0 + // check looks backwards but that's how ServiceReference#compareTo is documented to work. + if (highestServiceRef == null || reference.compareTo(highestServiceRef) < 0) { + LOG.debug("Currently selecting bundle {} for destroy (with reference {})", bundle, reference); + highestServiceRef = reference; + } + } } - return null; + return highestServiceRef == null ? null : highestServiceRef.getBundle(); + } + + private static int getServiceUsage(final ServiceReference ref) { + Bundle[] usingBundles = ref.getUsingBundles(); + return usingBundles != null ? usingBundles.length : 0; } }