2 * Copyright (c) 2014 Cisco 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.sal.binding.api;
10 import static java.util.Objects.requireNonNull;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.osgi.framework.BundleActivator;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceReference;
18 import org.osgi.util.tracker.ServiceTracker;
19 import org.osgi.util.tracker.ServiceTrackerCustomizer;
22 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
23 private final class Customizer implements ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> {
24 private final BundleContext context;
26 Customizer(final BundleContext context) {
27 this.context = requireNonNull(context);
31 public BindingAwareBroker addingService(final ServiceReference<BindingAwareBroker> reference) {
32 final BindingAwareBroker broker = context.getService(reference);
33 MD_ACTIVATION_POOL.execute(() -> onBrokerAvailable(broker, context));
38 public void modifiedService(final ServiceReference<BindingAwareBroker> reference,
39 final BindingAwareBroker service) {
40 removedService(reference, service);
41 addingService(reference);
45 public void removedService(final ServiceReference<BindingAwareBroker> reference,
46 final BindingAwareBroker service) {
47 final BindingAwareBroker broker = context.getService(reference);
48 MD_ACTIVATION_POOL.execute(() -> onBrokerRemoved(broker, context));
52 private static final ExecutorService MD_ACTIVATION_POOL = Executors.newCachedThreadPool();
54 private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
57 public final void start(final BundleContext bundleContext) {
58 startImpl(bundleContext);
59 tracker = new ServiceTracker<>(bundleContext, BindingAwareBroker.class, new Customizer(bundleContext));
64 public final void stop(final BundleContext bundleContext) {
65 if (tracker != null) {
68 stopImpl(bundleContext);
72 * Called when this bundle is started (before
73 * {@link BindingAwareProvider#onSessionInitiated(ProviderContext)} so the Framework can perform
74 * the bundle-specific activities necessary to start this bundle. This
75 * method can be used to register services or to allocate any resources that
79 * This method must complete and return to its caller in a timely manner.
81 * @param bundleContext
82 * The execution context of the bundle being started.
83 * @throws RuntimeException
84 * If this method throws an exception, this bundle is marked as
85 * stopped and the Framework will remove this bundle's
86 * listeners, unregister all services registered by this bundle,
87 * and release all services used by this bundle.
89 protected void startImpl(final BundleContext bundleContext) {
94 * Called when this bundle is stopped so the Framework can perform the
95 * bundle-specific activities necessary to stop the bundle. In general, this
96 * method should undo the work that the {@code BundleActivator.start} method
97 * started. There should be no active threads that were started by this
98 * bundle when this bundle returns. A stopped bundle must not call any
102 * This method must complete and return to its caller in a timely manner.
104 * @param bundleContext The execution context of the bundle being stopped.
105 * @throws RuntimeException If this method throws an exception, the bundle is still
106 * marked as stopped, and the Framework will remove the bundle's
107 * listeners, unregister all services registered by the bundle, and
108 * release all services used by the bundle.
110 protected void stopImpl(final BundleContext bundleContext) {
114 protected abstract void onBrokerAvailable(BindingAwareBroker bindingBroker, BundleContext bundleContext);
116 protected void onBrokerRemoved(final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
117 stopImpl(bundleContext);