2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.blueprint;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.Dictionary;
15 import java.util.Enumeration;
16 import java.util.HashSet;
17 import java.util.Hashtable;
18 import java.util.List;
19 import org.apache.aries.blueprint.NamespaceHandler;
20 import org.apache.aries.blueprint.services.BlueprintExtenderService;
21 import org.apache.aries.quiesce.participant.QuiesceParticipant;
22 import org.apache.aries.util.AriesFrameworkUtil;
23 import org.opendaylight.controller.blueprint.ext.OpendaylightNamespaceHandler;
24 import org.opendaylight.controller.config.api.ConfigSystemService;
25 import org.osgi.framework.Bundle;
26 import org.osgi.framework.BundleActivator;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.BundleEvent;
29 import org.osgi.framework.ServiceReference;
30 import org.osgi.framework.ServiceRegistration;
31 import org.osgi.framework.SynchronousBundleListener;
32 import org.osgi.service.blueprint.container.BlueprintContainer;
33 import org.osgi.service.blueprint.container.EventConstants;
34 import org.osgi.service.event.Event;
35 import org.osgi.service.event.EventHandler;
36 import org.osgi.util.tracker.BundleTracker;
37 import org.osgi.util.tracker.BundleTrackerCustomizer;
38 import org.osgi.util.tracker.ServiceTracker;
39 import org.osgi.util.tracker.ServiceTrackerCustomizer;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * This class is created in bundle activation and scans ACTIVE bundles for blueprint XML files located under
45 * the well-known org/opendaylight/blueprint/ path and deploys the XML files via the Aries
46 * BlueprintExtenderService. This path differs from the standard OSGI-INF/blueprint path to allow for
47 * controlled deployment of blueprint containers in an orderly manner.
49 * @author Thomas Pantelis
51 public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCustomizer<Bundle>, EventHandler,
52 SynchronousBundleListener {
53 private static final Logger LOG = LoggerFactory.getLogger(BlueprintBundleTracker.class);
54 private static final String BLUEPRINT_FILE_PATH = "org/opendaylight/blueprint/";
55 private static final String BLUEPRINT_FLE_PATTERN = "*.xml";
56 private static final long SYSTEM_BUNDLE_ID = 0;
58 private ServiceTracker<BlueprintExtenderService, BlueprintExtenderService> blueprintExtenderServiceTracker;
59 private ServiceTracker<QuiesceParticipant, QuiesceParticipant> quiesceParticipantTracker;
60 private BundleTracker<Bundle> bundleTracker;
61 private BundleContext bundleContext;
62 private volatile BlueprintExtenderService blueprintExtenderService;
63 private volatile QuiesceParticipant quiesceParticipant;
64 private volatile ServiceRegistration<?> blueprintContainerRestartReg;
65 private volatile BlueprintContainerRestartServiceImpl restartService;
66 private volatile boolean shuttingDown;
67 private ServiceRegistration<?> eventHandlerReg;
68 private ServiceRegistration<?> namespaceReg;
71 * Implemented from BundleActivator.
74 public void start(final BundleContext context) {
75 LOG.info("Starting {}", getClass().getSimpleName());
77 restartService = new BlueprintContainerRestartServiceImpl();
79 bundleContext = context;
81 registerBlueprintEventHandler(context);
83 registerNamespaceHandler(context);
85 bundleTracker = new BundleTracker<>(context, Bundle.ACTIVE, this);
87 blueprintExtenderServiceTracker = new ServiceTracker<>(context, BlueprintExtenderService.class.getName(),
88 new ServiceTrackerCustomizer<BlueprintExtenderService, BlueprintExtenderService>() {
90 public BlueprintExtenderService addingService(
91 final ServiceReference<BlueprintExtenderService> reference) {
92 blueprintExtenderService = reference.getBundle().getBundleContext().getService(reference);
95 context.addBundleListener(BlueprintBundleTracker.this);
97 LOG.debug("Got BlueprintExtenderService");
99 restartService.setBlueprintExtenderService(blueprintExtenderService);
101 blueprintContainerRestartReg = context.registerService(
102 BlueprintContainerRestartService.class.getName(), restartService, new Hashtable<>());
104 return blueprintExtenderService;
108 public void modifiedService(final ServiceReference<BlueprintExtenderService> reference,
109 final BlueprintExtenderService service) {
113 public void removedService(final ServiceReference<BlueprintExtenderService> reference,
114 final BlueprintExtenderService service) {
117 blueprintExtenderServiceTracker.open();
119 quiesceParticipantTracker = new ServiceTracker<>(context, QuiesceParticipant.class.getName(),
120 new ServiceTrackerCustomizer<QuiesceParticipant, QuiesceParticipant>() {
122 public QuiesceParticipant addingService(
123 final ServiceReference<QuiesceParticipant> reference) {
124 quiesceParticipant = reference.getBundle().getBundleContext().getService(reference);
126 LOG.debug("Got QuiesceParticipant");
128 restartService.setQuiesceParticipant(quiesceParticipant);
130 return quiesceParticipant;
134 public void modifiedService(final ServiceReference<QuiesceParticipant> reference,
135 final QuiesceParticipant service) {
139 public void removedService(final ServiceReference<QuiesceParticipant> reference,
140 final QuiesceParticipant service) {
143 quiesceParticipantTracker.open();
146 private void registerNamespaceHandler(final BundleContext context) {
147 Dictionary<String, Object> props = new Hashtable<>();
148 props.put("osgi.service.blueprint.namespace", OpendaylightNamespaceHandler.NAMESPACE_1_0_0);
149 namespaceReg = context.registerService(NamespaceHandler.class.getName(),
150 new OpendaylightNamespaceHandler(), props);
153 private void registerBlueprintEventHandler(final BundleContext context) {
154 Dictionary<String, Object> props = new Hashtable<>();
155 props.put(org.osgi.service.event.EventConstants.EVENT_TOPIC,
156 new String[]{EventConstants.TOPIC_CREATED, EventConstants.TOPIC_FAILURE});
157 eventHandlerReg = context.registerService(EventHandler.class.getName(), this, props);
161 * Implemented from BundleActivator.
164 public void stop(final BundleContext context) {
165 bundleTracker.close();
166 blueprintExtenderServiceTracker.close();
167 quiesceParticipantTracker.close();
169 AriesFrameworkUtil.safeUnregisterService(eventHandlerReg);
170 AriesFrameworkUtil.safeUnregisterService(namespaceReg);
171 AriesFrameworkUtil.safeUnregisterService(blueprintContainerRestartReg);
175 * Implemented from SynchronousBundleListener.
178 public void bundleChanged(final BundleEvent event) {
179 // If the system bundle (id 0) is stopping, do an orderly shutdown of all blueprint containers. On
180 // shutdown the system bundle is stopped first.
181 if (event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) {
182 shutdownAllContainers();
187 * Implemented from BundleActivator.
190 public Bundle addingBundle(final Bundle bundle, final BundleEvent event) {
191 modifiedBundle(bundle, event, bundle);
196 * Implemented from BundleTrackerCustomizer.
199 public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Bundle object) {
204 if (bundle.getState() == Bundle.ACTIVE) {
205 List<Object> paths = findBlueprintPaths(bundle);
207 if (!paths.isEmpty()) {
208 LOG.info("Creating blueprint container for bundle {} with paths {}", bundle, paths);
210 blueprintExtenderService.createContainer(bundle, paths);
216 * Implemented from BundleTrackerCustomizer.
219 public void removedBundle(final Bundle bundle, final BundleEvent event, final Bundle object) {
220 // BlueprintExtenderService will handle this.
224 * Implemented from EventHandler to listen for blueprint events.
226 * @param event the event to handle
229 public void handleEvent(final Event event) {
230 if (EventConstants.TOPIC_CREATED.equals(event.getTopic())) {
231 LOG.info("Blueprint container for bundle {} was successfully created",
232 event.getProperty(EventConstants.BUNDLE));
233 } else if (EventConstants.TOPIC_FAILURE.equals(event.getTopic())) {
234 // If the container timed out waiting for dependencies, we'll destroy it and start it again. This
235 // is indicated via a non-null DEPENDENCIES property containing the missing dependencies. The
236 // default timeout is 5 min and ideally we would set this to infinite but the timeout can only
237 // be set at the bundle level in the manifest - there's no way to set it globally.
238 if (event.getProperty(EventConstants.DEPENDENCIES) != null) {
239 Bundle bundle = (Bundle) event.getProperty(EventConstants.BUNDLE);
241 List<Object> paths = findBlueprintPaths(bundle);
242 if (!paths.isEmpty()) {
243 LOG.warn("Blueprint container for bundle {} timed out waiting for dependencies - restarting it",
244 event.getProperty(EventConstants.BUNDLE));
246 restartService.restartContainer(bundle, paths);
252 @SuppressWarnings({ "rawtypes", "unchecked" })
253 static List<Object> findBlueprintPaths(final Bundle bundle) {
254 Enumeration<?> rntries = bundle.findEntries(BLUEPRINT_FILE_PATH, BLUEPRINT_FLE_PATTERN, false);
255 if (rntries == null) {
256 return Collections.emptyList();
258 return Collections.list((Enumeration)rntries);
262 private void shutdownAllContainers() {
265 restartService.close();
267 // Close all CSS modules first.
268 ConfigSystemService configSystem = getOSGiService(ConfigSystemService.class);
269 if (configSystem != null) {
270 configSystem.closeAllConfigModules();
273 LOG.info("Shutting down all blueprint containers...");
275 Collection<Bundle> containerBundles = new HashSet<>(Arrays.asList(bundleContext.getBundles()));
276 while (!containerBundles.isEmpty()) {
277 // For each iteration of getBundlesToDestroy, as containers are destroyed, other containers become
278 // eligible to be destroyed. We loop until we've destroyed them all.
279 for (Bundle bundle : getBundlesToDestroy(containerBundles)) {
280 containerBundles.remove(bundle);
281 BlueprintContainer container = blueprintExtenderService.getContainer(bundle);
282 if (container != null) {
283 blueprintExtenderService.destroyContainer(bundle, container);
288 LOG.info("Shutdown of blueprint containers complete");
291 private List<Bundle> getBundlesToDestroy(final Collection<Bundle> containerBundles) {
292 List<Bundle> bundlesToDestroy = new ArrayList<>();
294 // Find all container bundles that either have no registered services or whose services are no
296 for (Bundle bundle : containerBundles) {
297 ServiceReference<?>[] references = bundle.getRegisteredServices();
299 if (references != null) {
300 for (ServiceReference<?> reference : references) {
301 usage += getServiceUsage(reference);
305 LOG.debug("Usage for bundle {} is {}", bundle, usage);
307 bundlesToDestroy.add(bundle);
311 if (!bundlesToDestroy.isEmpty()) {
312 Collections.sort(bundlesToDestroy, (b1, b2) -> (int) (b2.getLastModified() - b1.getLastModified()));
314 LOG.debug("Selected bundles {} for destroy (no services in use)", bundlesToDestroy);
316 // There's either no more container bundles or they all have services being used. For
317 // the latter it means there's either circular service usage or a service is being used
318 // by a non-container bundle. But we need to make progress so we pick the bundle with a
319 // used service with the highest service ID. Each service is assigned a monotonically
320 // increasing ID as they are registered. By picking the bundle with the highest service
321 // ID, we're picking the bundle that was (likely) started after all the others and thus
322 // is likely the safest to destroy at this point.
324 ServiceReference<?> ref = null;
325 for (Bundle bundle : containerBundles) {
326 ServiceReference<?>[] references = bundle.getRegisteredServices();
327 if (references == null) {
331 for (ServiceReference<?> reference : references) {
332 // We did check the service usage above but it's possible the usage has changed since
334 if (getServiceUsage(reference) == 0) {
338 // Choose 'reference' if it has a lower service ranking or, if the rankings are equal
339 // which is usually the case, if it has a higher service ID. For the latter the < 0
340 // check looks backwards but that's how ServiceReference#compareTo is documented to work.
341 if (ref == null || reference.compareTo(ref) < 0) {
342 LOG.debug("Currently selecting bundle {} for destroy (with reference {})", bundle, reference);
349 bundlesToDestroy.add(ref.getBundle());
352 LOG.debug("Selected bundle {} for destroy (lowest ranking service or highest service ID)",
356 return bundlesToDestroy;
359 private static int getServiceUsage(final ServiceReference<?> ref) {
360 Bundle[] usingBundles = ref.getUsingBundles();
361 return usingBundles != null ? usingBundles.length : 0;
364 private <T> T getOSGiService(final Class<T> serviceInterface) {
366 ServiceReference<T> serviceReference = bundleContext.getServiceReference(serviceInterface);
367 if (serviceReference == null) {
368 LOG.warn("{} service reference not found", serviceInterface.getSimpleName());
372 T service = bundleContext.getService(serviceReference);
373 if (service == null) {
374 // This could happen on shutdown if the service was already unregistered so we log as debug.
375 LOG.debug("{} service instance was not found", serviceInterface.getSimpleName());
379 } catch (final IllegalStateException e) {
380 // This is thrown if the BundleContext is no longer valid which is possible on shutdown so we
382 LOG.debug("Error obtaining OSGi service {}", serviceInterface.getSimpleName(), e);