4fc1a517c5551f8efa1d6e9f57b10dbcdd68e5e5
[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 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;
18
19 @Deprecated
20 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
21
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>() {
28
29         @Override
30         public BindingAwareBroker addingService(ServiceReference<BindingAwareBroker> reference) {
31             broker = context.getService(reference);
32             MD_ACTIVATION_POOL.execute(() -> onBrokerAvailable(broker, context));
33             return broker;
34         }
35
36         @Override
37         public void modifiedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
38             removedService(reference, service);
39             addingService(reference);
40         }
41
42         @Override
43         public void removedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
44             broker = context.getService(reference);
45             MD_ACTIVATION_POOL.execute(() -> onBrokerRemoved(broker, context));
46         }
47
48     };
49
50
51     @Override
52     public final void start(BundleContext bundleContext) {
53         this.context = bundleContext;
54         startImpl(bundleContext);
55         tracker = new ServiceTracker<>(bundleContext, BindingAwareBroker.class, customizer);
56         tracker.open();
57
58     }
59
60
61
62     @Override
63     public final  void stop(BundleContext bundleContext) {
64         if (tracker != null) {
65             tracker.close();
66         }
67         stopImpl(bundleContext);
68     }
69
70     /**
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
75      * this bundle needs.
76      *
77      * <p>
78      * This method must complete and return to its caller in a timely manner.
79      *
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.
87      */
88     protected void startImpl(BundleContext bundleContext) {
89         // NOOP
90     }
91
92     /**
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
98      * Framework objects.
99      *
100      * <p>
101      * This method must complete and return to its caller in a timely manner.
102      *
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.
108      */
109     protected void stopImpl(BundleContext bundleContext) {
110         // NOOP
111     }
112
113     protected abstract void onBrokerAvailable(BindingAwareBroker bindingBroker, BundleContext bundleContext);
114
115     protected void onBrokerRemoved(BindingAwareBroker bindingBroker, BundleContext bundleContext) {
116         stopImpl(bundleContext);
117     }
118 }