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