Bug 6396: Integrate vpp-renderer with clustering singleton service
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / controller / config / yang / config / vpp_provider / impl / GbpVppProviderInstance.java
index b33484f0aa43c2f8f6ea97a325246da57a32e843..f483a03bc31770f614b9c507d5b9099b1e327825 100644 (file)
@@ -8,19 +8,76 @@
 
 package org.opendaylight.controller.config.yang.config.vpp_provider.impl;
 
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
+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 GbpVppProviderInstance implements AutoCloseable {
+public class GbpVppProviderInstance implements ClusterSingletonService, AutoCloseable {
 
+    private static final Logger LOG = LoggerFactory.getLogger(GbpVppProviderInstance.class);
+
+    private static final ServiceGroupIdentifier IDENTIFIER =
+            ServiceGroupIdentifier.create(GbpVppProviderInstance.class.getName());
+    private final DataBroker dataBroker;
+    private final BindingAwareBroker bindingAwareBroker;
+    private final ClusterSingletonServiceProvider clusterSingletonService;
+    private ClusterSingletonServiceRegistration singletonServiceRegistration;
     private VppRenderer renderer;
 
-    public GbpVppProviderInstance (DataBroker dataBroker, BindingAwareBroker broker) {
-        renderer = new VppRenderer(dataBroker, broker);
+    public GbpVppProviderInstance(final DataBroker dataBroker,
+                                  final BindingAwareBroker bindingAwareBroker,
+                                  final ClusterSingletonServiceProvider clusterSingletonService) {
+        this.dataBroker = Preconditions.checkNotNull(dataBroker);
+        this.bindingAwareBroker = Preconditions.checkNotNull(bindingAwareBroker);
+        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());
+        renderer = new VppRenderer(dataBroker, bindingAwareBroker);
+    }
+
+    @Override
+    public ListenableFuture<Void> closeServiceInstance() {
+        LOG.info("Instance {} closed", this.getClass().getSimpleName());
+        try {
+            renderer.close();
+        } catch (Exception e) {
+            LOG.warn("Exception while closing ... {}", e.getMessage());
+        }
+        return Futures.immediateFuture(null);
+    }
+
     @Override
     public void close() throws Exception {
-        renderer.close();
+
+        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;
+    }
 }