package org.opendaylight.controller.config.yang.config.groupbasedpolicy.sxp_mapper;
+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.DomainSpecificRegistry;
import org.opendaylight.groupbasedpolicy.sxp.mapper.impl.SxpMapperProviderImpl;
+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.base_endpoint.rev160427.BaseEndpointService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-public class SxpMapperProviderInstance implements AutoCloseable {
+public class SxpMapperProviderInstance implements ClusterSingletonService, AutoCloseable {
+ private static final Logger LOG = LoggerFactory.getLogger(SxpMapperProviderInstance.class);
+
+ private static final ServiceGroupIdentifier IDENTIFIER =
+ ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
+ private final DataBroker dataBroker;
+ private final BaseEndpointService endpointService;
+ private final DomainSpecificRegistry registry;
+ private final ClusterSingletonServiceProvider clusterSingletonService;
+ private ClusterSingletonServiceRegistration singletonServiceRegistration;
private SxpMapperProviderImpl sxpMapperProviderImpl;
- public SxpMapperProviderInstance(DataBroker dataBroker, BaseEndpointService endpointService,
- DomainSpecificRegistry registry ) {
+ public SxpMapperProviderInstance(final DataBroker dataBroker,
+ final BaseEndpointService endpointService,
+ final DomainSpecificRegistry registry,
+ final ClusterSingletonServiceProvider clusterSingletonService) {
+ this.dataBroker = Preconditions.checkNotNull(dataBroker);
+ this.endpointService = Preconditions.checkNotNull(endpointService);
+ this.registry = Preconditions.checkNotNull(registry);
+ 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());
sxpMapperProviderImpl = new SxpMapperProviderImpl(dataBroker, endpointService, registry);
}
+ @Override
+ public ListenableFuture<Void> closeServiceInstance() {
+ LOG.info("Instance {} closed", this.getClass().getSimpleName());
+ try {
+ sxpMapperProviderImpl.close();
+ } catch (Exception e) {
+ LOG.warn("Exception while closing ... {}", e.getMessage());
+ }
+ return Futures.immediateFuture(null);
+ }
+
@Override
public void close() throws Exception {
- sxpMapperProviderImpl.close();
+ LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
+ if (singletonServiceRegistration != null) {
+ try {
+ singletonServiceRegistration.close();
+ } catch (Exception e) {
+ LOG.warn("{} closed unexpectedly. Cause: {}", e.getMessage());
+ }
+ singletonServiceRegistration = null;
+ }
}
+ @Override
+ public ServiceGroupIdentifier getIdentifier() {
+ return IDENTIFIER;
+ }
}
<reference id="dataBroker" interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"/>
<reference id="domainSpecificRegistry" interface="org.opendaylight.groupbasedpolicy.api.DomainSpecificRegistry"/>
+ <reference id="clusterSingletonService" interface="org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider"/>
<odl:rpc-service id="baseEndpointService" interface="org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService"/>
<bean id="sxpMapper" class="org.opendaylight.controller.config.yang.config.groupbasedpolicy.sxp_mapper.SxpMapperProviderInstance"
- destroy-method="close">
+ init-method="initialize" destroy-method="close">
<argument ref="dataBroker"/>
<argument ref="baseEndpointService"/>
<argument ref="domainSpecificRegistry"/>
+ <argument ref="clusterSingletonService"/>
</bean>
</blueprint>
\ No newline at end of file