5cdc26d35e63eeb83e238c2e2c7fe13f6a9e4847
[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         tracker.close();
49         tracker = null;
50         stopImpl(bundleContext);
51     }
52
53     @Override
54     public Broker addingService(final ServiceReference<Broker> reference) {
55         if (broker == null) {
56             broker = context.getService(reference);
57             broker.registerProvider(this, context);
58             return broker;
59         }
60
61         return null;
62     }
63
64     @Override
65     public void modifiedService(final ServiceReference<Broker> reference, final Broker service) {
66         // NOOP
67     }
68
69     @Override
70     public void removedService(final ServiceReference<Broker> reference, final Broker service) {
71         stopImpl(context);
72     }
73 }