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