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;
+ }
}