From fe7e2e940445b1f8da99138142c3110849e5c1c2 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Fri, 25 Jan 2019 09:53:22 +0100 Subject: [PATCH] dhcpservice: drop nullToEmpty and reqNonNullOrElse Change-Id: I5c303e30f697ffeff0759a93c943bb5264114cf9 Signed-off-by: Stephen Kitt --- .../netvirt/dhcpservice/api/DHCPUtils.java | 9 --------- .../dhcpservice/DhcpAllocationPoolListener.java | 12 ++++-------- .../dhcpservice/DhcpAllocationPoolManager.java | 13 +++++++------ .../dhcpservice/DhcpExternalTunnelManager.java | 11 +++++------ .../dhcpservice/DhcpNeutronPortListener.java | 7 +++---- .../netvirt/dhcpservice/DhcpPktHandler.java | 4 +--- .../netvirt/dhcpservice/DhcpServiceUtils.java | 14 ++++++++------ 7 files changed, 28 insertions(+), 42 deletions(-) diff --git a/dhcpservice/api/src/main/java/org/opendaylight/netvirt/dhcpservice/api/DHCPUtils.java b/dhcpservice/api/src/main/java/org/opendaylight/netvirt/dhcpservice/api/DHCPUtils.java index 1571040931..b24fe90584 100644 --- a/dhcpservice/api/src/main/java/org/opendaylight/netvirt/dhcpservice/api/DHCPUtils.java +++ b/dhcpservice/api/src/main/java/org/opendaylight/netvirt/dhcpservice/api/DHCPUtils.java @@ -8,14 +8,11 @@ package org.opendaylight.netvirt.dhcpservice.api; -import static java.util.Collections.emptyList; - import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; -import javax.annotation.Nonnull; import javax.annotation.Nullable; public abstract class DHCPUtils { @@ -94,10 +91,4 @@ public abstract class DHCPUtils { str.deleteCharAt(str.lastIndexOf(":")); return str.toString(); } - - // TODO Replace this with mdsal's DataObjectUtils.nullToEmpty when upgrading to mdsal 3 - @Nonnull - public static List nullToEmpty(final @Nullable List input) { - return input != null ? input : emptyList(); - } } diff --git a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolListener.java b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolListener.java index 7ebe1a2c0b..e5c9cdfc3b 100644 --- a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolListener.java +++ b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolListener.java @@ -52,7 +52,8 @@ public class DhcpAllocationPoolListener protected void add(InstanceIdentifier key, AllocationPool dataObjectModification) { String networkId = key.firstKeyOf(Network.class).getNetworkId(); dhcpAllocationPoolManager.createIdAllocationPool(networkId, dataObjectModification); - Map> elanDpnInterfacesByName = getDpnInterfacesByNetwork(networkId); + Map> elanDpnInterfacesByName = + dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId); for (Entry> entry : elanDpnInterfacesByName.entrySet()) { BigInteger dpnId = entry.getKey(); for (String interfaceName : entry.getValue()) { @@ -79,7 +80,8 @@ public class DhcpAllocationPoolListener protected void remove(InstanceIdentifier key, AllocationPool dataObjectModification) { String networkId = key.firstKeyOf(Network.class).getNetworkId(); dhcpAllocationPoolManager.releaseIdAllocationPool(networkId, dataObjectModification); - Map> elanDpnInterfacesByName = getDpnInterfacesByNetwork(networkId); + Map> elanDpnInterfacesByName = + dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId); elanDpnInterfacesByName.values().forEach(interfaceNames -> interfaceNames.forEach(interfaceName -> { DhcpAllocationPoolRemoveJob job = new DhcpAllocationPoolRemoveJob(txRunner, interfaceName); jobCoordinator.enqueueJob(DhcpServiceUtils.getJobKey(interfaceName), job, @@ -93,10 +95,4 @@ public class DhcpAllocationPoolListener // TODO Auto-generated method stub } - - private Map> getDpnInterfacesByNetwork(String networkId) { - Map> elanDpnInterfacesByName = dhcpAllocationPoolManager - .getElanDpnInterfacesByName(dataBroker, networkId); - return elanDpnInterfacesByName; - } } diff --git a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java index ee18f14b1e..750c038d92 100644 --- a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java +++ b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java @@ -7,16 +7,16 @@ */ package org.opendaylight.netvirt.dhcpservice; -import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty; - import com.google.common.base.Optional; import java.math.BigInteger; +import java.util.Collections; import java.util.EventListener; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.stream.Collectors; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @@ -132,7 +132,7 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener { } } - @Nullable + @Nonnull public Map> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) { InstanceIdentifier elanDpnIfacesIid = InstanceIdentifier.builder(ElanDpnInterfaces.class) .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build(); @@ -140,11 +140,12 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener { elanDpnIfacesIid); if (!elanDpnIfacesOpc.isPresent()) { LOG.warn("Could not find DpnInterfaces for elan {}", elanInstanceName); - return null; + return Collections.emptyMap(); } - return nullToEmpty(elanDpnIfacesOpc.get().getDpnInterfaces()).stream() - .collect(Collectors.toMap(DpnInterfaces::getDpId, value -> nullToEmpty(value.getInterfaces()))); + return elanDpnIfacesOpc.get().nonnullDpnInterfaces().stream() + .collect(Collectors.toMap(DpnInterfaces::getDpId, + value -> value.getInterfaces() != null ? value.getInterfaces() : Collections.emptyList())); } @Nullable diff --git a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpExternalTunnelManager.java b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpExternalTunnelManager.java index 22ddade122..5adef7fcfc 100644 --- a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpExternalTunnelManager.java +++ b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpExternalTunnelManager.java @@ -8,7 +8,6 @@ package org.opendaylight.netvirt.dhcpservice; import static org.opendaylight.genius.infra.Datastore.CONFIGURATION; -import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty; import com.google.common.base.Optional; import com.google.common.util.concurrent.ListenableFuture; @@ -177,8 +176,8 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager { Optional designatedSwitchForTunnelOptional = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier); if (designatedSwitchForTunnelOptional.isPresent()) { - List list = nullToEmpty( - designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel()); + List list = + designatedSwitchForTunnelOptional.get().nonnullDesignatedSwitchForTunnel(); for (DesignatedSwitchForTunnel designatedSwitchForTunnel : list) { Set> setOfTunnelIpElanNamePair = designatedDpnsToTunnelIpElanNameCache @@ -198,7 +197,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager { InstanceIdentifier inst = InstanceIdentifier.builder(Neutron.class).child(Ports.class).build(); Optional optionalPorts = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst); if (optionalPorts.isPresent()) { - List list = nullToEmpty(optionalPorts.get().getPort()); + List list = optionalPorts.get().nonnullPort(); for (Port port : list) { if (NeutronUtils.isPortVnicTypeNormal(port)) { continue; @@ -353,7 +352,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager { MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier); if (designatedSwitchForTunnelOptional.isPresent()) { List list = - nullToEmpty(designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel()); + designatedSwitchForTunnelOptional.get().nonnullDesignatedSwitchForTunnel(); for (DesignatedSwitchForTunnel designatedSwitchForTunnel : list) { if (dpId.equals(BigInteger.valueOf(designatedSwitchForTunnel.getDpId()))) { return true; @@ -603,7 +602,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager { public java.util.Optional getSubnetDhcpPortData(String elanInstanceName) { java.util.Optional optSubnetDhcp = java.util.Optional.empty(); Uuid nwUuid = new Uuid(elanInstanceName); - List subnets = nullToEmpty(DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid)); + List subnets = DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid); for (Uuid subnet : subnets) { if (DhcpServiceUtils.isIpv4Subnet(broker, subnet)) { optSubnetDhcp = DhcpServiceUtils.getSubnetDhcpPortData(broker, subnet.getValue()); diff --git a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpNeutronPortListener.java b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpNeutronPortListener.java index 0f1e173f50..cb75115636 100644 --- a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpNeutronPortListener.java +++ b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpNeutronPortListener.java @@ -9,7 +9,6 @@ package org.opendaylight.netvirt.dhcpservice; import static org.opendaylight.genius.infra.Datastore.CONFIGURATION; import static org.opendaylight.genius.infra.Datastore.OPERATIONAL; -import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty; import java.math.BigInteger; import java.util.ArrayList; @@ -135,11 +134,11 @@ public class DhcpNeutronPortListener protected void update(InstanceIdentifier identifier, Port original, Port update) { LOG.trace("Port changed to {}", update); //With Ipv6 changes we can get ipv4 subnets later. The below check is to support such scenario. - if (nullToEmpty(original.getFixedIps()).size() < nullToEmpty(update.getFixedIps()).size()) { + if (original.nonnullFixedIps().size() < update.nonnullFixedIps().size()) { final String interfaceName = update.getUuid().getValue(); - List updatedFixedIps = new ArrayList<>(nullToEmpty(update.getFixedIps())); + List updatedFixedIps = new ArrayList<>(update.nonnullFixedIps()); // Need to check only the newly added fixed ip. - updatedFixedIps.removeAll(nullToEmpty(original.getFixedIps())); + updatedFixedIps.removeAll(original.nonnullFixedIps()); Subnet subnet = dhcpManager.getNeutronSubnet(updatedFixedIps); if (null == subnet || !subnet.isEnableDhcp()) { LOG.trace("Subnet is null/not ipv4 or not enabled {}", subnet); diff --git a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpPktHandler.java b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpPktHandler.java index 443d120160..47cbdac5d7 100644 --- a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpPktHandler.java +++ b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpPktHandler.java @@ -7,8 +7,6 @@ */ package org.opendaylight.netvirt.dhcpservice; -import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty; - import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -385,7 +383,7 @@ public class DhcpPktHandler implements PacketProcessingListener { @Nullable private static String getIpv4Address(Port port) { - for (FixedIps fixedIp : nullToEmpty(port.getFixedIps())) { + for (FixedIps fixedIp : port.nonnullFixedIps()) { if (isIpv4Address(fixedIp.getIpAddress())) { return fixedIp.getIpAddress().getIpv4Address().getValue(); } diff --git a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpServiceUtils.java b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpServiceUtils.java index 43f3b0aa59..2d8defbaf2 100644 --- a/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpServiceUtils.java +++ b/dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpServiceUtils.java @@ -9,7 +9,6 @@ package org.opendaylight.netvirt.dhcpservice; import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS; -import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty; import com.google.common.base.Optional; import java.math.BigInteger; @@ -276,7 +275,7 @@ public final class DhcpServiceUtils { @Nonnull private static List extractDpnsFromNodes(Optional optionalNodes) { return optionalNodes.toJavaUtil().map( - nodes -> nullToEmpty(nodes.getNode()).stream().map(Node::getId).filter(Objects::nonNull).map( + nodes -> nodes.nonnullNode().stream().map(Node::getId).filter(Objects::nonNull).map( MDSALUtil::getDpnIdFromNodeName).collect( Collectors.toList())).orElse(Collections.emptyList()); } @@ -290,7 +289,7 @@ public final class DhcpServiceUtils { Optional elanDpnOptional = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInstanceIdentifier); if (elanDpnOptional.isPresent()) { - List dpns = nullToEmpty(elanDpnOptional.get().getDpnInterfaces()); + List dpns = elanDpnOptional.get().nonnullDpnInterfaces(); for (DpnInterfaces dpnInterfaces : dpns) { elanDpns.add(dpnInterfaces.getDpId()); } @@ -528,14 +527,17 @@ public final class DhcpServiceUtils { return null; } - @Nullable + @Nonnull public static List getSubnetIdsFromNetworkId(DataBroker broker, Uuid networkId) { InstanceIdentifier id = buildNetworkMapIdentifier(networkId); Optional optionalNetworkMap = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id); if (optionalNetworkMap.isPresent()) { - return optionalNetworkMap.get().getSubnetIdList(); + @Nullable List subnetIdList = optionalNetworkMap.get().getSubnetIdList(); + if (subnetIdList != null) { + return subnetIdList; + } } - return null; + return Collections.emptyList(); } static InstanceIdentifier buildNetworkMapIdentifier(Uuid networkId) { -- 2.36.6