Bug 6396: Integrate faas renderer with clustering singleton service 69/43969/2
authorVladimir Lavor <vlavor@cisco.com>
Mon, 15 Aug 2016 12:09:53 +0000 (14:09 +0200)
committerVladimir Lavor <vlavor@cisco.com>
Mon, 5 Sep 2016 08:45:36 +0000 (10:45 +0200)
Change-Id: Id7fabc3ff5d47bb0a2a793927d8dbfa12071a299
Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
renderers/faas/src/main/java/org/opendaylight/controller/config/yang/config/faas_provider/impl/FaasProviderInstance.java
renderers/faas/src/main/resources/org/opendaylight/blueprint/faas-renderer.xml

index f20b1aab027020dbe3c94c731f5f7eb66729c231..544ece9b460311d27535387187782e8d2fbbe60b 100644 (file)
@@ -8,21 +8,77 @@
 
 package org.opendaylight.controller.config.yang.config.faas_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.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
 import org.opendaylight.groupbasedpolicy.renderer.faas.FaasRenderer;
+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 FaasProviderInstance implements AutoCloseable{
+public class FaasProviderInstance implements ClusterSingletonService, AutoCloseable {
 
+    private static final Logger LOG = LoggerFactory.getLogger(FaasProviderInstance.class);
+
+    private static final ServiceGroupIdentifier IDENTIFIER =
+            ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
+    private final DataBroker dataBroker;
+    private final EpRendererAugmentationRegistry epRegistry;
+    private final ClusterSingletonServiceProvider clusterSingletonService;
+    private ClusterSingletonServiceRegistration singletonServiceRegistration;
     private FaasRenderer renderer;
 
-    public FaasProviderInstance (DataBroker dataBroker, EpRendererAugmentationRegistry epRegistry) {
+    public FaasProviderInstance(final DataBroker dataBroker,
+                                final EpRendererAugmentationRegistry epRegistry,
+                                final ClusterSingletonServiceProvider clusteringServiceProvider) {
+        this.dataBroker = Preconditions.checkNotNull(dataBroker);
+        this.epRegistry = Preconditions.checkNotNull(epRegistry);
+        this.clusterSingletonService = Preconditions.checkNotNull(clusteringServiceProvider);
+    }
+
+    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 FaasRenderer(dataBroker, epRegistry);
     }
 
+    @Override
+    public ListenableFuture<Void> closeServiceInstance() {
+        LOG.info("Instance {} closed", this.getClass().getSimpleName());
+        try {
+            renderer.close();
+        } catch (Exception e) {
+            LOG.warn("Exception while closing ... ", e);
+        }
+        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;
+    }
 }
index a72c7a8e04f9bae0273691432cc9b3fe0494ebec..0ee23f2f66333fab808d8584ba36dc4a0258b3fc 100644 (file)
@@ -5,10 +5,12 @@
 
     <reference id="dataBroker" interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"/>
     <reference id="epRegistry" interface="org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry"/>
+    <reference id="clusterSingletonService" interface="org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider"/>
 
     <bean id="faasRenderer" class="org.opendaylight.controller.config.yang.config.faas_provider.impl.FaasProviderInstance"
-        destroy-method="close">
+        init-method="initialize" destroy-method="close">
         <argument ref="dataBroker"/>
         <argument ref="epRegistry"/>
+        <argument ref="clusterSingletonService"/>
     </bean>
 </blueprint>
\ No newline at end of file