Moved binding MD-SAL APIs to binding folder
[mdsal.git] / binding / mdsal-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.osgi.framework.BundleActivator;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.util.tracker.ServiceTracker;
16 import org.osgi.util.tracker.ServiceTrackerCustomizer;
17
18 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
19
20     private static final ExecutorService mdActivationPool = Executors.newCachedThreadPool();
21     private BundleContext context;
22     private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
23     private BindingAwareBroker broker;
24     private final ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> customizer = new ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker>() {
25
26         @Override
27         public BindingAwareBroker addingService(final ServiceReference<BindingAwareBroker> reference) {
28             broker = context.getService(reference);
29             mdActivationPool.execute(new Runnable() {
30
31                 @Override
32                 public void run() {
33                     onBrokerAvailable(broker, context);
34                 }
35             });
36             return broker;
37         }
38
39         @Override
40         public void modifiedService(final ServiceReference<BindingAwareBroker> reference, final BindingAwareBroker service) {
41             removedService(reference, service);
42             addingService(reference);
43         }
44
45         @Override
46         public void removedService(final ServiceReference<BindingAwareBroker> reference, final BindingAwareBroker service) {
47             broker = context.getService(reference);
48             mdActivationPool.execute(new Runnable() {
49
50                 @Override
51                 public void run() {
52                     onBrokerRemoved(broker, context);
53                 }
54             });
55         }
56
57     };
58
59
60     @Override
61     public final void start(final BundleContext context) throws Exception {
62         this.context = context;
63         startImpl(context);
64         tracker = new ServiceTracker<>(context, BindingAwareBroker.class, customizer);
65         tracker.open();
66
67     }
68
69
70
71     @Override
72     public final  void stop(final BundleContext context) throws Exception {
73         tracker.close();
74         stopImpl(context);
75     }
76
77     protected void startImpl(final BundleContext context) throws Exception {
78         // NOOP
79     }
80
81     /**
82      * Called when this bundle is stopped so the Framework can perform the
83      * bundle-specific activities necessary to stop the bundle. In general, this
84      * method should undo the work that the {@code BundleActivator.start} method
85      * started. There should be no active threads that were started by this
86      * bundle when this bundle returns. A stopped bundle must not call any
87      * Framework objects.
88      *
89      * <p>
90      * This method must complete and return to its caller in a timely manner.
91      *
92      * @param context The execution context of the bundle being stopped.
93      */
94     protected void stopImpl(final BundleContext context) {
95         // NOOP
96     }
97
98
99     protected abstract void onBrokerAvailable(BindingAwareBroker broker, BundleContext context);
100
101     protected void onBrokerRemoved(final BindingAwareBroker broker, final BundleContext context) {
102         stopImpl(context);
103     }
104 }