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

©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.