Migrate OSGI compendium reference
[controller.git] / opendaylight / md-sal / sal-binding-api / src / main / java / org / opendaylight / controller / sal / binding / api / AbstractBrokerAwareActivator.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.api;
9
10 import static java.util.Objects.requireNonNull;
11
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;
20
21 @Deprecated(forRemoval = true)
22 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
23     private final class Customizer implements ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> {
24         private final BundleContext context;
25
26         Customizer(final BundleContext context) {
27             this.context = requireNonNull(context);
28         }
29
30         @Override
31         public BindingAwareBroker addingService(final ServiceReference<BindingAwareBroker> reference) {
32             final BindingAwareBroker broker = context.getService(reference);
33             MD_ACTIVATION_POOL.execute(() -> onBrokerAvailable(broker, context));
34             return broker;
35         }
36
37         @Override
38         public void modifiedService(final ServiceReference<BindingAwareBroker> reference,
39                 final BindingAwareBroker service) {
40             removedService(reference, service);
41             addingService(reference);
42         }
43
44         @Override
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));
49         }
50     }
51
52     private static final ExecutorService MD_ACTIVATION_POOL = Executors.newCachedThreadPool();
53
54     private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
55
56     @Override
57     public final void start(final BundleContext bundleContext) {
58         startImpl(bundleContext);
59         tracker = new ServiceTracker<>(bundleContext, BindingAwareBroker.class, new Customizer(bundleContext));
60         tracker.open();
61     }
62
63     @Override
64     public final void stop(final BundleContext bundleContext) {
65         if (tracker != null) {
66             tracker.close();
67         }
68         stopImpl(bundleContext);
69     }
70
71     /**
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
76      * this bundle needs.
77      *
78      * <p>
79      * This method must complete and return to its caller in a timely manner.
80      *
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.
88      */
89     protected void startImpl(final BundleContext bundleContext) {
90         // NOOP
91     }
92
93     /**
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
99      * Framework objects.
100      *
101      * <p>
102      * This method must complete and return to its caller in a timely manner.
103      *
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.
109      */
110     protected void stopImpl(final BundleContext bundleContext) {
111         // NOOP
112     }
113
114     protected abstract void onBrokerAvailable(BindingAwareBroker bindingBroker, BundleContext bundleContext);
115
116     protected void onBrokerRemoved(final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
117         stopImpl(bundleContext);
118     }
119 }