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 java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
13 import org.osgi.framework.BundleActivator;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.ServiceReference;
16 import org.osgi.util.tracker.ServiceTracker;
17 import org.osgi.util.tracker.ServiceTrackerCustomizer;
20 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
22 private static final ExecutorService MD_ACTIVATION_POOL = Executors.newCachedThreadPool();
23 private BundleContext context;
24 private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
25 private BindingAwareBroker broker;
26 private final ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> customizer =
27 new ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker>() {
30 public BindingAwareBroker addingService(ServiceReference<BindingAwareBroker> reference) {
31 broker = context.getService(reference);
32 MD_ACTIVATION_POOL.execute(() -> onBrokerAvailable(broker, context));
37 public void modifiedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
38 removedService(reference, service);
39 addingService(reference);
43 public void removedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
44 broker = context.getService(reference);
45 MD_ACTIVATION_POOL.execute(() -> onBrokerRemoved(broker, context));
52 public final void start(BundleContext bundleContext) {
53 this.context = bundleContext;
54 startImpl(bundleContext);
55 tracker = new ServiceTracker<>(bundleContext, BindingAwareBroker.class, customizer);
63 public final void stop(BundleContext bundleContext) {
64 if (tracker != null) {
67 stopImpl(bundleContext);
71 * Called when this bundle is started (before
72 * {@link BindingAwareProvider#onSessionInitiated(ProviderContext)} so the Framework can perform
73 * the bundle-specific activities necessary to start this bundle. This
74 * method can be used to register services or to allocate any resources that
78 * This method must complete and return to its caller in a timely manner.
80 * @param bundleContext
81 * The execution context of the bundle being started.
82 * @throws RuntimeException
83 * If this method throws an exception, this bundle is marked as
84 * stopped and the Framework will remove this bundle's
85 * listeners, unregister all services registered by this bundle,
86 * and release all services used by this bundle.
88 protected void startImpl(BundleContext bundleContext) {
93 * Called when this bundle is stopped so the Framework can perform the
94 * bundle-specific activities necessary to stop the bundle. In general, this
95 * method should undo the work that the {@code BundleActivator.start} method
96 * started. There should be no active threads that were started by this
97 * bundle when this bundle returns. A stopped bundle must not call any
101 * This method must complete and return to its caller in a timely manner.
103 * @param bundleContext The execution context of the bundle being stopped.
104 * @throws RuntimeException If this method throws an exception, the bundle is still
105 * marked as stopped, and the Framework will remove the bundle's
106 * listeners, unregister all services registered by the bundle, and
107 * release all services used by the bundle.
109 protected void stopImpl(BundleContext bundleContext) {
113 protected abstract void onBrokerAvailable(BindingAwareBroker bindingBroker, BundleContext bundleContext);
115 protected void onBrokerRemoved(BindingAwareBroker bindingBroker, BundleContext bundleContext) {
116 stopImpl(bundleContext);