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