Bug 6396: Integrate ne-location-provider with clustering singleton service
[groupbasedpolicy.git] / location-providers / ne-location-provider / src / main / java / org / opendaylight / controller / config / yang / config / ne / location / provider / cfg / NeLocationProviderInstance.java
index f894a91a9ac2c23f626559be1ecd30dde6a42f5e..c932b86544ed1ffdb6c6328fea7b2084d931a3e3 100644 (file)
@@ -8,19 +8,69 @@
 
 package org.opendaylight.controller.config.yang.config.ne.location.provider.cfg;
 
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.opendaylight.controller.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.groupbasedpolicy.ne.location.provider.NeLocationProvider;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class NeLocationProviderInstance implements AutoCloseable {
+public class NeLocationProviderInstance implements ClusterSingletonService, AutoCloseable {
 
+    private static final Logger LOG = LoggerFactory.getLogger(NeLocationProviderInstance.class);
+
+    private static final ServiceGroupIdentifier IDENTIFIER =
+            ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
+    private final DataBroker dataBroker;
+    private final ClusterSingletonServiceProvider clusterSingletonService;
+    private ClusterSingletonServiceRegistration singletonServiceRegistration;
     private NeLocationProvider provider;
 
-    public NeLocationProviderInstance (DataBroker dataBroker) {
+    public NeLocationProviderInstance(final DataBroker dataBroker,
+                                      final ClusterSingletonServiceProvider clusterSingletonService) {
+        this.dataBroker = Preconditions.checkNotNull(dataBroker);
+        this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
+    }
+
+    public void initialize() {
+        LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
+        singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
+    }
+
+    @Override
+    public void instantiateServiceInstance() {
+        LOG.info("Instantiating {}", this.getClass().getSimpleName());
         provider = new NeLocationProvider(dataBroker);
     }
 
     @Override
-    public void close() throws Exception {
+    public ListenableFuture<Void> closeServiceInstance() {
+        LOG.info("Instance {} closed", this.getClass().getSimpleName());
         provider.close();
+        return Futures.immediateFuture(null);
+    }
+
+    @Override
+    public void close() throws Exception {
+        LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
+        if (singletonServiceRegistration != null) {
+            try {
+                singletonServiceRegistration.close();
+            } catch (Exception e) {
+                LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e.getMessage());
+            }
+            singletonServiceRegistration = null;
+        }
+    }
+
+    @Override
+    public ServiceGroupIdentifier getIdentifier() {
+        return IDENTIFIER;
     }
 }