46e9cdcd718917c4f26f33647eb1bf197a477d3f
[controller.git] / opendaylight / md-sal / sal-dom-api / src / main / java / org / opendaylight / controller / sal / core / api / AbstractConsumer.java
1 /*
2  * Copyright (c) 2013 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.core.api;
9
10 import java.util.Collection;
11 import java.util.Collections;
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 AbstractConsumer implements Consumer, BundleActivator,ServiceTrackerCustomizer<Broker, Broker> {
19
20     private BundleContext context;
21     private ServiceTracker<Broker, Broker> tracker;
22     private Broker broker;
23
24     @Override
25     public final void start(final BundleContext bundleContext) throws Exception {
26         this.context = bundleContext;
27         this.startImpl(bundleContext);
28         tracker = new ServiceTracker<>(bundleContext, Broker.class, this);
29         tracker.open();
30     }
31
32     @Override
33     public final void stop(final BundleContext bundleContext) throws Exception {
34         stopImpl(bundleContext);
35         broker = null;
36
37         if (tracker != null) {
38             tracker.close();
39         }
40     }
41
42     protected void startImpl(final BundleContext bundleContext) {
43         // NOOP
44     }
45
46     protected void stopImpl(final BundleContext bundleContext) {
47         // NOOP
48     }
49
50     @Override
51     public Collection<ConsumerFunctionality> getConsumerFunctionality() {
52         return Collections.emptySet();
53     }
54
55
56     @Override
57     public Broker addingService(final ServiceReference<Broker> reference) {
58         if (broker == null && context != null) {
59             broker = context.getService(reference);
60             broker.registerConsumer(this, context);
61             return broker;
62         }
63
64         return null;
65     }
66
67     @Override
68     public void modifiedService(final ServiceReference<Broker> reference, final Broker service) {
69         // NOOP
70     }
71
72     @Override
73     public void removedService(final ServiceReference<Broker> reference, final Broker service) {
74         stopImpl(context);
75     }
76 }