import static org.opendaylight.netvirt.neutronvpn.NeutronvpnUtils.buildfloatingIpIdToPortMappingIdentifier;
-import com.google.common.base.Optional;
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
-import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
import org.opendaylight.infrautils.utils.concurrent.NamedLocks;
import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock.AcquireResult;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.FloatingIpInfo;
import org.slf4j.LoggerFactory;
@Singleton
-public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTreeChangeListenerBase<Floatingip,
- NeutronFloatingToFixedIpMappingChangeListener> {
+public class NeutronFloatingToFixedIpMappingChangeListener extends AbstractAsyncDataTreeChangeListener<Floatingip> {
private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingToFixedIpMappingChangeListener.class);
private static final long LOCK_WAIT_TIME = 10L;
@Inject
public NeutronFloatingToFixedIpMappingChangeListener(final DataBroker dataBroker) {
- super(Floatingip.class, NeutronFloatingToFixedIpMappingChangeListener.class);
+ super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
+ .child(Floatingips.class).child(Floatingip.class),
+ Executors.newSingleThreadExecutor("NeutronFloatingToFixedIpMappingChangeListener", LOG));
this.dataBroker = dataBroker;
}
- @Override
- @PostConstruct
public void init() {
LOG.info("{} init", getClass().getSimpleName());
- registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
- }
-
- @Override
- protected InstanceIdentifier<Floatingip> getWildCardPath() {
- return InstanceIdentifier.create(Neutron.class).child(Floatingips.class).child(Floatingip.class);
}
@Override
- protected NeutronFloatingToFixedIpMappingChangeListener getDataTreeChangeListener() {
- return NeutronFloatingToFixedIpMappingChangeListener.this;
+ @PreDestroy
+ public void close() {
+ super.close();
+ Executors.shutdownAndAwaitTermination(getExecutorService());
}
@Override
- protected void add(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
+ public void add(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
LOG.trace("Neutron Floating IP created: key: {}, value={}", identifier, input);
IpAddress fixedIp = input.getFixedIpAddress();
String floatingIp = input.getFloatingIpAddress().getIpv4Address().getValue();
}
@Override
- protected void remove(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
+ public void remove(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
LOG.trace("Neutron Floating IP deleted : key: {}, value={}", identifier, input);
IpAddress fixedIp = input.getFixedIpAddress();
if (fixedIp != null) {
// populate the floating to fixed ip map upon association/dissociation from fixed ip
@Override
- protected void update(InstanceIdentifier<Floatingip> identifier, Floatingip original, Floatingip update) {
+ public void update(InstanceIdentifier<Floatingip> identifier, Floatingip original, Floatingip update) {
LOG.trace("Handling FloatingIptoFixedIp mapping : key: {}, original value={}, update value={}", identifier,
original, update);
IpAddress oldFixedIp = original.getFixedIpAddress();
routerPortsBuilder.setExternalNetworkId(extNetworkId);
}
if (fixedNeutronPortName != null) {
- List<Ports> portsList = routerPortsBuilder.getPorts();
- if (portsList == null) {
- portsList = new ArrayList<>();
- }
+ List<Ports> portsList = routerPortsBuilder.getPorts() != null
+ ? new ArrayList<Ports>(routerPortsBuilder.getPorts().values()) : new ArrayList<Ports>();
PortsBuilder fixedNeutronPortBuilder = null;
for (Ports neutronPort : portsList) {
if (neutronPort.getPortName().equals(fixedNeutronPortName)) {
.setPortName(fixedNeutronPortName);
}
if (fixedIpAddress != null) {
- List<InternalToExternalPortMap> intExtPortMapList = fixedNeutronPortBuilder
- .getInternalToExternalPortMap();
- if (intExtPortMapList == null) {
- intExtPortMapList = new ArrayList<>();
- }
+ Collection<InternalToExternalPortMap> intExtPortMapCollection = fixedNeutronPortBuilder
+ .getInternalToExternalPortMap() != null ? fixedNeutronPortBuilder
+ .getInternalToExternalPortMap().values() : null;
+ List<InternalToExternalPortMap> intExtPortMapList =
+ new ArrayList<InternalToExternalPortMap>(intExtPortMapCollection != null
+ ? intExtPortMapCollection : Collections.emptyList());
InternalToExternalPortMap intExtPortMap = new InternalToExternalPortMapBuilder().withKey(new
InternalToExternalPortMapKey(fixedIpAddress)).setInternalIp(fixedIpAddress)
- .setExternalIp(floatingIpAddress).setExternalId(floatingIpId).setLabel(0L).build();
+ .setExternalIp(floatingIpAddress).setExternalId(floatingIpId).build();
intExtPortMapList.add(intExtPortMap);
fixedNeutronPortBuilder.setInternalToExternalPortMap(intExtPortMapList);
}
routerPortsBuilder.build());
LOG.debug("FloatingIpInfo DS updated for floating IP {} ", floatingIpAddress);
}
- } catch (ReadFailedException | RuntimeException e) {
+ } catch (RuntimeException | ExecutionException | InterruptedException e) {
LOG.error("addToFloatingIpInfo failed for floating IP: {} ", floatingIpAddress, e);
}
}
routerPortsIdentifierBuilder.build());
if (optionalRouterPorts.isPresent()) {
RouterPorts routerPorts = optionalRouterPorts.get();
- List<Ports> portsList = routerPorts.nonnullPorts();
- List<InternalToExternalPortMap> intExtPortMap = new ArrayList<>();
- for (Ports ports : portsList) {
+ Map<PortsKey, Ports> keyPortsMap = routerPorts.nonnullPorts();
+ Map<InternalToExternalPortMapKey, InternalToExternalPortMap> keyInternalToExternalPortMapMap
+ = new HashMap<InternalToExternalPortMapKey, InternalToExternalPortMap>();
+ for (Ports ports : keyPortsMap.values()) {
if (Objects.equals(ports.getPortName(), fixedNeutronPortName)) {
- intExtPortMap = ports.nonnullInternalToExternalPortMap();
+ keyInternalToExternalPortMapMap = ports.nonnullInternalToExternalPortMap();
break;
}
}
- if (intExtPortMap.size() == 1) {
- removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder, portsList,
- fixedNeutronPortName);
+ if (keyInternalToExternalPortMapMap.size() == 1) {
+ removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder,
+ new ArrayList<Ports>(keyPortsMap.values()), fixedNeutronPortName);
} else {
- for (InternalToExternalPortMap intToExtMap : intExtPortMap) {
+ for (InternalToExternalPortMap intToExtMap : keyInternalToExternalPortMapMap.values()) {
if (Objects.equals(intToExtMap.getInternalIp(), fixedIpAddress)) {
InstanceIdentifier<InternalToExternalPortMap> intExtPortMapIdentifier =
routerPortsIdentifierBuilder.child(Ports
}
// remove particular internal-to-external-port-map
- LOG.debug("removing particular internal-to-external-port-map {}", intExtPortMap);
+ LOG.debug("removing particular internal-to-external-port-map {}",
+ keyInternalToExternalPortMapMap);
try {
MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
intExtPortMapIdentifier);
} catch (Exception e) {
- LOG.error("Failure in deletion of internal-to-external-port-map {}", intExtPortMap,
- e);
+ LOG.error("Failure in deletion of internal-to-external-port-map {}",
+ keyInternalToExternalPortMapMap, e);
}
}
}
} else {
LOG.warn("routerPorts for router {} - fixedIp {} not found", routerName, fixedIpAddress);
}
- } catch (RuntimeException | ReadFailedException e) {
+ } catch (RuntimeException | ExecutionException | InterruptedException e) {
LOG.error("Failed to delete internal-to-external-port-map from FloatingIpInfo DS for fixed Ip {}",
fixedIpAddress, e);
}
SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.CONFIGURATION,
floatingIpInfoIdentifierBuilder.build());
if (optionalFloatingIPInfo.isPresent()) {
- List<RouterPorts> routerPortsList = optionalFloatingIPInfo.get().getRouterPorts();
- if (routerPortsList != null && !routerPortsList.isEmpty()) {
- for (RouterPorts routerPorts : routerPortsList) {
- List<Ports> portsList = routerPorts.getPorts();
- if (portsList != null && !portsList.isEmpty()) {
- for (Ports ports : portsList) {
+ Map<RouterPortsKey, RouterPorts> keyRouterPortsMap = optionalFloatingIPInfo.get().getRouterPorts();
+ if (keyRouterPortsMap != null && !keyRouterPortsMap.isEmpty()) {
+ for (RouterPorts routerPorts : keyRouterPortsMap.values()) {
+ Map<PortsKey, Ports> keyPortsMap = routerPorts.getPorts();
+ if (keyPortsMap != null && !keyPortsMap.isEmpty()) {
+ for (Ports ports : keyPortsMap.values()) {
if (Objects.equals(ports.getPortName(), fixedNeutronPortName)) {
String routerName = routerPorts.getRouterId();
InstanceIdentifier.InstanceIdentifierBuilder<RouterPorts>
routerPortsIdentifierBuilder = floatingIpInfoIdentifierBuilder
.child(RouterPorts.class, new RouterPortsKey(routerName));
- removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder, portsList,
- fixedNeutronPortName);
+ removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder,
+ new ArrayList<Ports>(keyPortsMap.values()), fixedNeutronPortName);
LOG.debug("Deletion from FloatingIpInfo DS successful for fixedIP neutron port {} ",
fixedNeutronPortName);
break;
LOG.debug("FloatingIPInfo DS empty. Hence, no router present containing fixed to floating IP "
+ "association(s)");
}
- } catch (ReadFailedException e) {
+ } catch (ExecutionException | InterruptedException e) {
LOG.error("Failed to dissociate fixedIP from FloatingIpInfo DS for neutron port {}",
fixedNeutronPortName, e);
}