Merge "Added conflict handling between configuration and state choice nodes. -unique...
[controller.git] / opendaylight / md-sal / sal-dom-api / src / main / java / org / opendaylight / controller / sal / core / api / AbstractProvider.java
index c25b2f9cadcd3186cc36e7611736f80fa8e96f09..1cb1a2bc8522b65301e3f70470e2c41e06d3b32c 100644 (file)
@@ -1,18 +1,29 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.controller.sal.core.api;
 
 import java.util.Collection;
 import java.util.Collections;
 
+import javax.naming.Context;
+
 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
-public abstract class AbstractProvider implements BundleActivator, Provider {
+public abstract class AbstractProvider implements BundleActivator, Provider,ServiceTrackerCustomizer<Broker, Broker> {
 
-    private ServiceReference<Broker> brokerRef;
     private Broker broker;
-
+    private BundleContext context;
+    private ServiceTracker<Broker, Broker> tracker;
     @Override
     public Collection<ProviderFunctionality> getProviderFunctionality() {
         return Collections.emptySet();
@@ -20,20 +31,46 @@ public abstract class AbstractProvider implements BundleActivator, Provider {
 
     @Override
     public final void start(BundleContext context) throws Exception {
-        brokerRef = context.getServiceReference(Broker.class);
-        broker = context.getService(brokerRef);
-
+        this.context = context;
         this.startImpl(context);
-
-        broker.registerProvider(this,context);
+        tracker = new ServiceTracker<>(context, Broker.class, this);
+        tracker.open();
     }
 
-    public abstract void startImpl(BundleContext context);
+    protected void startImpl(BundleContext context) {
+        // NOOP
+    }
+    protected void stopImpl(BundleContext context) {
+        // NOOP
+    }
 
     @Override
     public final void stop(BundleContext context) throws Exception {
-        // TODO Auto-generated method stub
-
+        broker = null;
+        tracker.close();
+        tracker = null;
+        stopImpl(context);
     }
 
+    @Override
+    public Broker addingService(ServiceReference<Broker> reference) {
+        if(broker == null) {
+            broker = context.getService(reference);
+            broker.registerProvider(this, context);
+            return broker;
+        }
+        
+        return null;
+    }
+    
+    @Override
+    public void modifiedService(ServiceReference<Broker> reference, Broker service) {
+        // NOOP
+    }
+    
+    @Override
+    public void removedService(ServiceReference<Broker> reference, Broker service) {
+        stopImpl(context);
+    }
+    
 }