dhcpservice: drop nullToEmpty and reqNonNullOrElse 15/79915/4
authorStephen Kitt <skitt@redhat.com>
Fri, 25 Jan 2019 08:53:22 +0000 (09:53 +0100)
committerSam Hague <shague@redhat.com>
Sat, 26 Jan 2019 22:46:18 +0000 (22:46 +0000)
Change-Id: I5c303e30f697ffeff0759a93c943bb5264114cf9
Signed-off-by: Stephen Kitt <skitt@redhat.com>
dhcpservice/api/src/main/java/org/opendaylight/netvirt/dhcpservice/api/DHCPUtils.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolListener.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpExternalTunnelManager.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpNeutronPortListener.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpPktHandler.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpServiceUtils.java

index 157104093125ab634d9affd4675fa92a171bc559..b24fe90584071fb2a0216642930993ea7930f058 100644 (file)
@@ -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 <T> List<T> nullToEmpty(final @Nullable List<T> input) {
-        return input != null ? input : emptyList();
-    }
 }
index 7ebe1a2c0b4fc1ec3070e157c049b0128f666314..e5c9cdfc3beda679b679b4d26092b87e0cd89ae3 100644 (file)
@@ -52,7 +52,8 @@ public class DhcpAllocationPoolListener
     protected void add(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
         String networkId = key.firstKeyOf(Network.class).getNetworkId();
         dhcpAllocationPoolManager.createIdAllocationPool(networkId, dataObjectModification);
-        Map<BigInteger, List<String>> elanDpnInterfacesByName = getDpnInterfacesByNetwork(networkId);
+        Map<BigInteger, List<String>> elanDpnInterfacesByName =
+            dhcpAllocationPoolManager.getElanDpnInterfacesByName(dataBroker, networkId);
         for (Entry<BigInteger, List<String>> entry : elanDpnInterfacesByName.entrySet()) {
             BigInteger dpnId = entry.getKey();
             for (String interfaceName : entry.getValue()) {
@@ -79,7 +80,8 @@ public class DhcpAllocationPoolListener
     protected void remove(InstanceIdentifier<AllocationPool> key, AllocationPool dataObjectModification) {
         String networkId = key.firstKeyOf(Network.class).getNetworkId();
         dhcpAllocationPoolManager.releaseIdAllocationPool(networkId, dataObjectModification);
-        Map<BigInteger, List<String>> elanDpnInterfacesByName = getDpnInterfacesByNetwork(networkId);
+        Map<BigInteger, List<String>> 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<BigInteger, List<String>> getDpnInterfacesByNetwork(String networkId) {
-        Map<BigInteger, List<String>> elanDpnInterfacesByName = dhcpAllocationPoolManager
-                .getElanDpnInterfacesByName(dataBroker, networkId);
-        return elanDpnInterfacesByName;
-    }
 }
index ee18f14b1e7f2db6a9bb116b56db6260c7ad2025..750c038d9228deea04f6fcfb575a19c1a6aa7757 100644 (file)
@@ -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<BigInteger, List<String>> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) {
         InstanceIdentifier<ElanDpnInterfacesList> 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
index 22ddade12273820a618ab58d09efe2edc1ed254c..5adef7fcfc215716dbd45127978ca408facff1a4 100644 (file)
@@ -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<DesignatedSwitchesForExternalTunnels> designatedSwitchForTunnelOptional =
                 MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
         if (designatedSwitchForTunnelOptional.isPresent()) {
-            List<DesignatedSwitchForTunnel> list = nullToEmpty(
-                    designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel());
+            List<DesignatedSwitchForTunnel> list =
+                designatedSwitchForTunnelOptional.get().nonnullDesignatedSwitchForTunnel();
             for (DesignatedSwitchForTunnel designatedSwitchForTunnel : list) {
                 Set<Pair<IpAddress, String>> setOfTunnelIpElanNamePair =
                         designatedDpnsToTunnelIpElanNameCache
@@ -198,7 +197,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         InstanceIdentifier<Ports> inst = InstanceIdentifier.builder(Neutron.class).child(Ports.class).build();
         Optional<Ports> optionalPorts = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
         if (optionalPorts.isPresent()) {
-            List<Port> list = nullToEmpty(optionalPorts.get().getPort());
+            List<Port> 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<DesignatedSwitchForTunnel> 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<SubnetToDhcpPort> getSubnetDhcpPortData(String elanInstanceName) {
         java.util.Optional<SubnetToDhcpPort> optSubnetDhcp = java.util.Optional.empty();
         Uuid nwUuid = new Uuid(elanInstanceName);
-        List<Uuid> subnets = nullToEmpty(DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid));
+        List<Uuid> subnets = DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid);
         for (Uuid subnet : subnets) {
             if (DhcpServiceUtils.isIpv4Subnet(broker, subnet)) {
                 optSubnetDhcp = DhcpServiceUtils.getSubnetDhcpPortData(broker, subnet.getValue());
index 0f1e173f50eca9787b80b3f77e8f33f580baa631..cb75115636e820c2885b1e8dea2a4ab462663560 100644 (file)
@@ -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<Port> 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<FixedIps> updatedFixedIps = new ArrayList<>(nullToEmpty(update.getFixedIps()));
+            List<FixedIps> 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);
index 443d120160f9572dd2ed62e121681aa8023b9b16..47cbdac5d7f4e984d2cbe0b100b4185e7a5053a3 100644 (file)
@@ -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();
             }
index 43f3b0aa5963d3708b8d4586301c2c80444cb0ff..2d8defbaf2ce15d4e772d07bcb4ed55b87058996 100644 (file)
@@ -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<BigInteger> extractDpnsFromNodes(Optional<Nodes> 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<ElanDpnInterfacesList> elanDpnOptional =
                 MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInstanceIdentifier);
         if (elanDpnOptional.isPresent()) {
-            List<DpnInterfaces> dpns = nullToEmpty(elanDpnOptional.get().getDpnInterfaces());
+            List<DpnInterfaces> 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<Uuid> getSubnetIdsFromNetworkId(DataBroker broker, Uuid networkId) {
         InstanceIdentifier id = buildNetworkMapIdentifier(networkId);
         Optional<NetworkMap> optionalNetworkMap = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
         if (optionalNetworkMap.isPresent()) {
-            return optionalNetworkMap.get().getSubnetIdList();
+            @Nullable List<Uuid> subnetIdList = optionalNetworkMap.get().getSubnetIdList();
+            if (subnetIdList != null) {
+                return subnetIdList;
+            }
         }
-        return null;
+        return Collections.emptyList();
     }
 
     static InstanceIdentifier<NetworkMap> buildNetworkMapIdentifier(Uuid networkId) {