catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / controller / config / yang / config / groupbasedpolicy / EpRendererAugmentationRegistryImplInstance.java
index 8cda5897ca7cd7cc434e131170ee9f09e99889cb..308bf4f88e73d6e4218a25f2133db0af539bfe89 100644 (file)
@@ -8,12 +8,20 @@
 
 package org.opendaylight.controller.config.yang.config.groupbasedpolicy;
 
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.concurrent.Future;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentation;
 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
 import org.opendaylight.groupbasedpolicy.endpoint.EndpointRpcRegistry;
+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.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput;
@@ -21,18 +29,28 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnsetEndpointGroupConditionsInput;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class EpRendererAugmentationRegistryImplInstance implements EpRendererAugmentationRegistry, EndpointService, AutoCloseable{
+public class EpRendererAugmentationRegistryImplInstance implements ClusterSingletonService, EpRendererAugmentationRegistry, EndpointService, AutoCloseable {
 
+    private static final Logger LOG = LoggerFactory.getLogger(EpRendererAugmentationRegistryImplInstance.class);
 
-    private final EndpointRpcRegistry endpointRpcRegistry;
+    private static final ServiceGroupIdentifier IDENTIFIER =
+            ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
+    private final DataBroker dataBroker;
+    private ClusterSingletonServiceProvider clusterSingletonService;
+    private final RpcProviderRegistry rpcProviderRegistry;
+    private ClusterSingletonServiceRegistration singletonServiceRegistration;
+    private EndpointRpcRegistry endpointRpcRegistry;
+    private BindingAwareBroker.RpcRegistration<EndpointService> serviceRpcRegistration;
 
-    public EpRendererAugmentationRegistryImplInstance(DataBroker dataProvider) {
-        endpointRpcRegistry = new EndpointRpcRegistry(dataProvider);
-    }
-    @Override
-    public void close() throws Exception {
-        endpointRpcRegistry.close();
+    public EpRendererAugmentationRegistryImplInstance(final DataBroker dataBroker,
+                                                      final ClusterSingletonServiceProvider clusterSingletonService,
+                                                      final RpcProviderRegistry rpcProviderRegistry) {
+        this.dataBroker = Preconditions.checkNotNull(dataBroker);
+        this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
+        this.rpcProviderRegistry = rpcProviderRegistry;
     }
 
     @Override
@@ -44,26 +62,73 @@ public class EpRendererAugmentationRegistryImplInstance implements EpRendererAug
     public void unregister(EpRendererAugmentation epRendererAugmentation) {
         endpointRpcRegistry.unregister(epRendererAugmentation);
     }
+
     @Override
     public Future<RpcResult<Void>> unsetEndpointGroupConditions(UnsetEndpointGroupConditionsInput input) {
         return endpointRpcRegistry.unsetEndpointGroupConditions(input);
     }
+
     @Override
     public Future<RpcResult<Void>> registerEndpoint(RegisterEndpointInput input) {
         return endpointRpcRegistry.registerEndpoint(input);
     }
+
     @Override
     public Future<RpcResult<Void>> setEndpointGroupConditions(SetEndpointGroupConditionsInput input) {
         return endpointRpcRegistry.setEndpointGroupConditions(input);
     }
+
     @Override
     public Future<RpcResult<Void>> registerL3PrefixEndpoint(RegisterL3PrefixEndpointInput input) {
         return endpointRpcRegistry.registerL3PrefixEndpoint(input);
     }
+
     @Override
     public Future<RpcResult<Void>> unregisterEndpoint(UnregisterEndpointInput input) {
         return endpointRpcRegistry.unregisterEndpoint(input);
     }
 
+    public void initialize() {
+        LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
+        try {
+            singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
+        }
+            catch (Exception e) {
+            LOG.warn("Exception while registering candidate ... ", e);
+        }
+    }
+
+    @Override
+    public void instantiateServiceInstance() {
+        LOG.info("Instantiating {}", this.getClass().getSimpleName());
+        endpointRpcRegistry = new EndpointRpcRegistry(dataBroker);
 
+        serviceRpcRegistration = rpcProviderRegistry.addRpcImplementation(EndpointService.class, this);
+    }
+
+    @Override
+    public ListenableFuture<Void> closeServiceInstance() {
+        LOG.info("Instance {} closed", this.getClass().getSimpleName());
+        endpointRpcRegistry.close();
+        serviceRpcRegistration.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);
+            }
+            singletonServiceRegistration = null;
+        }
+    }
+
+    @Override
+    public ServiceGroupIdentifier getIdentifier() {
+        return IDENTIFIER;
+    }
 }