package org.opendaylight.controller.config.yang.config.neutron_ovsdb.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.controller.sal.binding.api.RpcProviderRegistry;
import org.opendaylight.groupbasedpolicy.neutron.ovsdb.NeutronOvsdb;
+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.neutron.ovsdb.params.rev160812.IntegrationBridgeSetting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-public class NeutronOvsdbInstance implements AutoCloseable{
+public class NeutronOvsdbInstance implements ClusterSingletonService, AutoCloseable {
+ private static final Logger LOG = LoggerFactory.getLogger(NeutronOvsdbInstance.class);
+
+ private static final ServiceGroupIdentifier IDENTIFIER =
+ ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
+ private final DataBroker dataBroker;
+ private final IntegrationBridgeSetting settings;
+ private final ClusterSingletonServiceProvider clusterSingletonService;
+ private final RpcProviderRegistry rpcBroker;
+ private ClusterSingletonServiceRegistration singletonServiceRegistration;
private NeutronOvsdb neutronOvsdb;
- public NeutronOvsdbInstance(DataBroker dataBroker, EndpointService epService,
- IntegrationBridgeSetting settings) {
+ public NeutronOvsdbInstance(final DataBroker dataBroker,
+ final RpcProviderRegistry rpcBroker,
+ final IntegrationBridgeSetting settings,
+ final ClusterSingletonServiceProvider clusterSingletonService) {
+ this.dataBroker = Preconditions.checkNotNull(dataBroker);
+ this.rpcBroker = Preconditions.checkNotNull(rpcBroker);
+ this.settings = Preconditions.checkNotNull(settings);
+ this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
+ }
+
+ public void initialize() {
+ LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
+ try {
+ singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
+ } catch (Exception e) {
+ LOG.warn("Exception thrown while registering cluster singleton service in {}", this.getClass(), e.getMessage());
+ }
+ }
+
+ @Override
+ public void instantiateServiceInstance() {
+ LOG.info("Instantiating {}", this.getClass().getSimpleName());
+ final EndpointService epService = rpcBroker.getRpcService(EndpointService.class);
neutronOvsdb = new NeutronOvsdb(dataBroker, epService, settings);
}
+ @Override
+ public ListenableFuture<Void> closeServiceInstance() {
+ LOG.info("Instance {} closed", this.getClass().getSimpleName());
+ try {
+ neutronOvsdb.close();
+ } catch (Exception e) {
+ LOG.warn("Instance close failed to ... ", e);
+ }
+ return Futures.immediateFuture(null);
+ }
+
@Override
public void close() throws Exception {
- neutronOvsdb.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;
+ }
}