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