/* * Copyright (c) 2016 Cisco Systems, Inc. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.config.yang.config.neutron_mapper.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.neutron.mapper.NeutronMapper; 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.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class NeutronMapperInstance implements ClusterSingletonService, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(NeutronMapperInstance.class); private static final ServiceGroupIdentifier IDENTIFIER = ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER); private final DataBroker dataBroker; private final EndpointService epService; private final BaseEndpointService baseEndpointService; private final ClusterSingletonServiceProvider clusterSingletonService; private ClusterSingletonServiceRegistration singletonServiceRegistration; private NeutronMapper mapper; public NeutronMapperInstance(final DataBroker dataBroker, final EndpointService epService, final BaseEndpointService baseEndpointService, final ClusterSingletonServiceProvider clusterSingletonService) { this.dataBroker = Preconditions.checkNotNull(dataBroker); this.epService = Preconditions.checkNotNull(epService); this.baseEndpointService = Preconditions.checkNotNull(baseEndpointService); this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService); } public void instantiate() { LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName()); singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this); } @Override public void instantiateServiceInstance() { LOG.info("Instantiating {}", this.getClass().getSimpleName()); mapper = new NeutronMapper(dataBroker, epService, baseEndpointService); } @Override public ListenableFuture closeServiceInstance() { LOG.info("Instance {} closed", this.getClass().getSimpleName()); mapper.close(); return Futures.immediateFuture(null); } @Override public void close() throws Exception { LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName()); if (singletonServiceRegistration != null) { try { singletonServiceRegistration.close(); } catch (Exception e) { LOG.warn("{} closed unexpectedly", e.getMessage()); } singletonServiceRegistration = null; } } @Override public ServiceGroupIdentifier getIdentifier() { return IDENTIFIER; } }