Remove unused exceptions
[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 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
20
21     private static final ExecutorService MD_ACTIVATION_POOL = Executors.newCachedThreadPool();
22     private BundleContext context;
23     private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
24     private BindingAwareBroker broker;
25     private final ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> customizer =
26             new ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker>() {
27
28         @Override
29         public BindingAwareBroker addingService(ServiceReference<BindingAwareBroker> reference) {
30             broker = context.getService(reference);
31             MD_ACTIVATION_POOL.execute(() -> onBrokerAvailable(broker, context));
32             return broker;
33         }
34
35         @Override
36         public void modifiedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
37             removedService(reference, service);
38             addingService(reference);
39         }
40
41         @Override
42         public void removedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
43             broker = context.getService(reference);
44             MD_ACTIVATION_POOL.execute(() -> onBrokerRemoved(broker, context));
45         }
46
47     };
48
49
50     @Override
51     public final void start(BundleContext bundleContext) {
52         this.context = bundleContext;
53         startImpl(bundleContext);
54         tracker = new ServiceTracker<>(bundleContext, BindingAwareBroker.class, customizer);
55         tracker.open();
56
57     }
58
59
60
61     @Override
62     public final  void stop(BundleContext bundleContext) {
63         if (tracker != null) {
64             tracker.close();
65         }
66         stopImpl(bundleContext);
67     }
68
69     /**
70      * Called when this bundle is started (before
71      * {@link BindingAwareProvider#onSessionInitiated(ProviderContext)} so the Framework can perform
72      * the bundle-specific activities necessary to start this bundle. This
73      * method can be used to register services or to allocate any resources that
74      * this bundle needs.
75      *
76      * <p>
77      * This method must complete and return to its caller in a timely manner.
78      *
79      * @param bundleContext
80      *            The execution context of the bundle being started.
81      * @throws RuntimeException
82      *             If this method throws an exception, this bundle is marked as
83      *             stopped and the Framework will remove this bundle's
84      *             listeners, unregister all services registered by this bundle,
85      *             and release all services used by this bundle.
86      */
87     protected void startImpl(BundleContext bundleContext) {
88         // NOOP
89     }
90
91     /**
92      * Called when this bundle is stopped so the Framework can perform the
93      * bundle-specific activities necessary to stop the bundle. In general, this
94      * method should undo the work that the {@code BundleActivator.start} method
95      * started. There should be no active threads that were started by this
96      * bundle when this bundle returns. A stopped bundle must not call any
97      * Framework objects.
98      *
99      * <p>
100      * This method must complete and return to its caller in a timely manner.
101      *
102      * @param bundleContext The execution context of the bundle being stopped.
103      * @throws RuntimeException If this method throws an exception, the bundle is still
104      *         marked as stopped, and the Framework will remove the bundle's
105      *         listeners, unregister all services registered by the bundle, and
106      *         release all services used by the bundle.
107      */
108     protected void stopImpl(BundleContext bundleContext) {
109         // NOOP
110     }
111
112     protected abstract void onBrokerAvailable(BindingAwareBroker bindingBroker, BundleContext bundleContext);
113
114     protected void onBrokerRemoved(BindingAwareBroker bindingBroker, BundleContext bundleContext) {
115         stopImpl(bundleContext);
116     }
117 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.