6465e1deb20e248fd090b9f5dfcd8f81945ae81f
[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
33
34     @Override
35     public final void stop(final BundleContext bundleContext) throws Exception {
36         stopImpl(bundleContext);
37         broker = null;
38         tracker.close();
39     }
40
41     protected void startImpl(final BundleContext bundleContext) {
42         // NOOP
43     }
44
45     protected void stopImpl(final BundleContext bundleContext) {
46         // NOOP
47     }
48
49     @Override
50     public Collection<ConsumerFunctionality> getConsumerFunctionality() {
51         return Collections.emptySet();
52     }
53
54
55     @Override
56     public Broker addingService(final ServiceReference<Broker> reference) {
57         if (broker == null) {
58             broker = context.getService(reference);
59             broker.registerConsumer(this, context);
60             return broker;
61         }
62
63         return null;
64     }
65
66     @Override
67     public void modifiedService(final ServiceReference<Broker> reference, final Broker service) {
68         // NOOP
69     }
70
71     @Override
72     public void removedService(final ServiceReference<Broker> reference, final Broker service) {
73         stopImpl(context);
74     }
75 }