Bug 6396: Integrate l2-l3-domain-extension with clustering singleton service
[groupbasedpolicy.git] / domain-extensions / l2-l3 / src / main / java / org / opendaylight / controller / config / yang / config / domain_extension / l2_l3 / impl / L2L3DomainExtensionInstance.java
index b43ba9a0430d28f7fa0acd88629a343fece6c728..51d7c4c971ea16fb031349d76f8755b4ccc95d9c 100644 (file)
@@ -8,19 +8,69 @@
 
 package org.opendaylight.controller.config.yang.config.domain_extension.l2_l3.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.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
 import org.opendaylight.groupbasedpolicy.api.DomainSpecificRegistry;
 import org.opendaylight.groupbasedpolicy.domain_extension.l2_l3.L2L3NetworkDomainAugmentor;
+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 L2L3DomainExtensionInstance implements AutoCloseable {
+public class L2L3DomainExtensionInstance implements ClusterSingletonService, AutoCloseable {
 
+    private static final Logger LOG = LoggerFactory.getLogger(L2L3DomainExtensionInstance.class);
+
+    private static final ServiceGroupIdentifier IDENTIFIER =
+            ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
+    private final DomainSpecificRegistry domainSpecificRegistry;
+    private ClusterSingletonServiceProvider clusterSingletonService;
+    private ClusterSingletonServiceRegistration singletonServiceRegistration;
     private L2L3NetworkDomainAugmentor l2l3NetworkDomainAugmentor;
 
-    public L2L3DomainExtensionInstance (DomainSpecificRegistry domainSpecificRegistry) {
-         l2l3NetworkDomainAugmentor = new L2L3NetworkDomainAugmentor(domainSpecificRegistry.getNetworkDomainAugmentorRegistry());
+    public L2L3DomainExtensionInstance(final DomainSpecificRegistry domainSpecificRegistry,
+                                       final ClusterSingletonServiceProvider clusterSingletonService) {
+        this.domainSpecificRegistry = Preconditions.checkNotNull(domainSpecificRegistry);
+        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());
+        l2l3NetworkDomainAugmentor = new L2L3NetworkDomainAugmentor(domainSpecificRegistry.getNetworkDomainAugmentorRegistry());
     }
 
     @Override
-    public void close() throws Exception {
+    public ListenableFuture<Void> closeServiceInstance() {
+        LOG.info("Instance {} closed", this.getClass().getSimpleName());
         l2l3NetworkDomainAugmentor.close();
+        return Futures.immediateFuture(null);
+    }
+
+    @Override
+    public void 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;
     }
 }