Handle nullable lists in dhcpservice 51/77151/2
authorStephen Kitt <skitt@redhat.com>
Fri, 19 Oct 2018 15:50:42 +0000 (17:50 +0200)
committerSam Hague <shague@redhat.com>
Fri, 19 Oct 2018 22:13:27 +0000 (22:13 +0000)
Following YANGTOOLS-585, lists can be null (which is correctly
indicated with an @Nullable annotation). This patch deals with the
fallout.

Change-Id: I8d41474f9340f91b61420c706e56963597192158
Signed-off-by: Stephen Kitt <skitt@redhat.com>
12 files changed:
dhcpservice/api/src/main/java/org/opendaylight/netvirt/dhcpservice/api/DHCPOptions.java
dhcpservice/api/src/main/java/org/opendaylight/netvirt/dhcpservice/api/DHCPUtils.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpAllocationPoolManager.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpConfigListener.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpExternalTunnelManager.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpL2GwUtil.java
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpManager.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
dhcpservice/impl/src/main/java/org/opendaylight/netvirt/dhcpservice/DhcpUCastMacListener.java
dhcpservice/shell/src/main/java/org/opendaylight/netvirt/dhcpservice/shell/DhcpCacheCli.java

index 5f2f3e101d2c24950465422a1f70257bc605cdce..5fa7146a6005bb25bab96ea9be7370e42cb8c698 100644 (file)
@@ -17,6 +17,7 @@ import java.net.UnknownHostException;
 import java.nio.charset.StandardCharsets;
 import java.util.LinkedHashMap;
 import java.util.List;
+import javax.annotation.Nullable;
 import org.apache.commons.lang3.ArrayUtils;
 import org.opendaylight.openflowplugin.libraries.liblldp.HexEncode;
 import org.opendaylight.openflowplugin.libraries.liblldp.NetUtils;
@@ -83,6 +84,7 @@ public class DHCPOptions {
 
     // It's unclear from all the callers if returning an empty array would be safe.
     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
+    @Nullable
     public byte[] getOptionBytes(byte code) {
         DhcpOption option = this.getOption(code);
         return option != null ? option.getValue() : null;
@@ -118,6 +120,7 @@ public class DHCPOptions {
         this.setOption(new DhcpOption(code, DHCPUtils.inetAddrToByteArray(opt)));
     }
 
+    @Nullable
     public InetAddress getOptionInetAddr(byte code) {
         byte[] opt = this.getOptionBytes(code);
         try {
@@ -131,6 +134,7 @@ public class DHCPOptions {
         this.setOption(new DhcpOption(code, DHCPUtils.strAddrToByteArray(opt)));
     }
 
+    @Nullable
     public String getOptionStrAddr(byte code) {
         byte[] opt = this.getOptionBytes(code);
         try {
index d33af1bf75649bd36098dc4c8d07b3121d11b8e8..157104093125ab634d9affd4675fa92a171bc559 100644 (file)
@@ -8,11 +8,15 @@
 
 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 {
 
@@ -46,13 +50,14 @@ public abstract class DHCPUtils {
         return result;
     }
 
-    public static short byteArrayToShort(byte[] ba) {
+    public static short byteArrayToShort(@Nullable byte[] ba) {
         if (ba == null || ba.length != 2) {
             return 0;
         }
         return (short) ((0xff & ba[0]) << 8 | 0xff & ba[1]);
     }
 
+    @Nullable
     public static InetAddress byteArrayToInetAddr(byte[] ba) {
         try {
             return InetAddress.getByAddress(ba);
@@ -64,6 +69,7 @@ public abstract class DHCPUtils {
     // An empty byte[] would technically be an invalid MAC address and it's unclear if we can force callers to pass
     // a non-null String.
     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
+    @Nullable
     public static byte[] strMacAddrtoByteArray(String macAddress) {
         if (macAddress == null) {
             return null;
@@ -88,4 +94,10 @@ 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 1fd6ca5cb80e198770f4989463c1654b5390febf..ee18f14b1e7f2db6a9bb116b56db6260c7ad2025 100644 (file)
@@ -7,6 +7,8 @@
  */
 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.EventListener;
@@ -15,6 +17,7 @@ import java.util.Map;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.stream.Collectors;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
@@ -93,13 +96,12 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener {
         }
     }
 
+    @Nullable
     public IpAddress getIpAllocation(String networkId, AllocationPool pool, String macAddress) {
         String poolIdKey = getPoolKeyIdByAllocationPool(networkId, pool);
         long allocatedIpLong = createIdAllocation(poolIdKey, macAddress);
         LOG.debug("allocated id {} for mac {}, from pool {}", allocatedIpLong, macAddress, poolIdKey);
-        IpAddress allocatedIpAddress = allocatedIpLong != 0 ? DhcpServiceUtils.convertLongToIp(allocatedIpLong)
-                : null;
-        return allocatedIpAddress;
+        return allocatedIpLong != 0 ? DhcpServiceUtils.convertLongToIp(allocatedIpLong) : null;
     }
 
     public void releaseIpAllocation(String networkId, AllocationPool pool, String macAddress) {
@@ -108,6 +110,7 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener {
         releaseIdAllocation(poolIdKey, macAddress);
     }
 
+    @Nullable
     public AllocationPool getAllocationPoolByNetwork(String networkId) throws ReadFailedException {
         InstanceIdentifier<Network> network = InstanceIdentifier.builder(DhcpAllocationPool.class)
                 .child(Network.class, new NetworkKey(networkId)).build();
@@ -129,6 +132,7 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener {
         }
     }
 
+    @Nullable
     public Map<BigInteger, List<String>> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) {
         InstanceIdentifier<ElanDpnInterfacesList> elanDpnIfacesIid = InstanceIdentifier.builder(ElanDpnInterfaces.class)
                 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
@@ -139,10 +143,11 @@ public class DhcpAllocationPoolManager implements AutoCloseable, EventListener {
             return null;
         }
 
-        return elanDpnIfacesOpc.get().getDpnInterfaces().stream()
-                .collect(Collectors.toMap(DpnInterfaces::getDpId, DpnInterfaces::getInterfaces));
+        return nullToEmpty(elanDpnIfacesOpc.get().getDpnInterfaces()).stream()
+                .collect(Collectors.toMap(DpnInterfaces::getDpId, value -> nullToEmpty(value.getInterfaces())));
     }
 
+    @Nullable
     public String getNetworkByPort(String portUuid) throws ReadFailedException {
         InstanceIdentifier<ElanInterface> elanInterfaceName = InstanceIdentifier.builder(ElanInterfaces.class)
                 .child(ElanInterface.class, new ElanInterfaceKey(portUuid)).build();
index b70968b176d91a5d73285b437b023f8666a071ee..a2cb5d304c6d2534186eabf764ee74a9dfe29b5d 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.netvirt.dhcpservice;
 
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
@@ -71,7 +72,7 @@ public class DhcpConfigListener extends AsyncClusteredDataTreeChangeListenerBase
         updateConfig(add);
     }
 
-    private void updateConfig(DhcpConfig update) {
+    private void updateConfig(@Nullable DhcpConfig update) {
         //TODO: Update operational with actual values
         if (update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
             dhcpManager.setLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
index 05c5e774791a83afd1c462785d869d262ffee873..22ddade12273820a618ab58d09efe2edc1ed254c 100644 (file)
@@ -8,6 +8,7 @@
 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;
@@ -20,12 +21,14 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map.Entry;
+import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Named;
@@ -174,8 +177,8 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         Optional<DesignatedSwitchesForExternalTunnels> designatedSwitchForTunnelOptional =
                 MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
         if (designatedSwitchForTunnelOptional.isPresent()) {
-            List<DesignatedSwitchForTunnel> list =
-                    designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel();
+            List<DesignatedSwitchForTunnel> list = nullToEmpty(
+                    designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel());
             for (DesignatedSwitchForTunnel designatedSwitchForTunnel : list) {
                 Set<Pair<IpAddress, String>> setOfTunnelIpElanNamePair =
                         designatedDpnsToTunnelIpElanNameCache
@@ -195,7 +198,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 = optionalPorts.get().getPort();
+            List<Port> list = nullToEmpty(optionalPorts.get().getPort());
             for (Port port : list) {
                 if (NeutronUtils.isPortVnicTypeNormal(port)) {
                     continue;
@@ -274,6 +277,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         tunnelIpElanNameToVmMacCache.remove(tunnelIpElanNamePair);
     }
 
+    @Nullable
     public BigInteger readDesignatedSwitchesForExternalTunnel(IpAddress tunnelIp, String elanInstanceName) {
         if (tunnelIp == null || elanInstanceName == null || elanInstanceName.isEmpty()) {
             return null;
@@ -349,7 +353,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
                 MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
         if (designatedSwitchForTunnelOptional.isPresent()) {
             List<DesignatedSwitchForTunnel> list =
-                    designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel();
+                    nullToEmpty(designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel());
             for (DesignatedSwitchForTunnel designatedSwitchForTunnel : list) {
                 if (dpId.equals(BigInteger.valueOf(designatedSwitchForTunnel.getDpId()))) {
                     return true;
@@ -599,7 +603,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 = DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid);
+        List<Uuid> subnets = nullToEmpty(DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid));
         for (Uuid subnet : subnets) {
             if (DhcpServiceUtils.isIpv4Subnet(broker, subnet)) {
                 optSubnetDhcp = DhcpServiceUtils.getSubnetDhcpPortData(broker, subnet.getValue());
@@ -707,7 +711,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         ConcurrentMap<String, L2GatewayDevice> l2GwDevices =
                 ElanL2GwCacheUtils.getInvolvedL2GwDevices(tunnelElanPair.getRight());
         for (L2GatewayDevice device : l2GwDevices.values()) {
-            if (device.getTunnelIp().equals(tunnelElanPair.getLeft())) {
+            if (Objects.equals(device.getTunnelIp(), tunnelElanPair.getLeft())) {
                 return true;
             }
         }
@@ -775,6 +779,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         vniMacAddressToPortCache.remove(vniMacAddressPair);
     }
 
+    @Nullable
     public Port readVniMacToPortCache(BigInteger vni, String macAddress) {
         if (macAddress == null) {
             return null;
@@ -815,6 +820,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, psNodeId, dataBroker);
     }
 
+    @Nullable
     public RemoteMcastMacs createRemoteMcastMac(Node dstDevice, String logicalSwitchName, IpAddress internalTunnelIp) {
         Set<LocatorSet> locators = new HashSet<>();
         TerminationPointKey terminationPointKey = HwvtepSouthboundUtils.getTerminationPointKey(
@@ -847,11 +853,11 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
     private void putRemoteMcastMac(TypedWriteTransaction<Configuration> transaction, String elanName,
                                    L2GatewayDevice device, IpAddress internalTunnelIp) {
         Optional<Node> optionalNode = getNode(broker, device.getHwvtepNodeId());
-        Node dstNode = optionalNode.get();
-        if (dstNode == null) {
+        if (!optionalNode.isPresent()) {
             LOG.trace("could not get device node {} ", device.getHwvtepNodeId());
             return;
         }
+        Node dstNode = optionalNode.get();
         RemoteMcastMacs macs = createRemoteMcastMac(dstNode, elanName, internalTunnelIp);
         HwvtepUtils.addRemoteMcastMac(transaction, dstNode.getNodeId(), macs);
     }
@@ -893,6 +899,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         });
     }
 
+    @Nullable
     private L2GatewayDevice getDeviceFromTunnelIp(IpAddress tunnelIp) {
         Collection<L2GatewayDevice> devices = l2GatewayCache.getAll();
         LOG.trace("In getDeviceFromTunnelIp devices {}", devices);
@@ -990,6 +997,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         });
     }
 
+    @Nullable
     public IpAddress getTunnelIpBasedOnElan(String elanInstanceName, String vmMacAddress) {
         LOG.trace("DhcpExternalTunnelManager getTunnelIpBasedOnElan elanInstanceName {}", elanInstanceName);
         IpAddress tunnelIp = null;
index bdef37d70fb457e0555a10ab38ad237ed098fceb..13a41a14eae6040601daa20d1b11e74ead0e4cad 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.netvirt.dhcpservice;
 import com.google.common.base.Optional;
 import java.util.List;
 import java.util.function.Predicate;
+import javax.annotation.Nullable;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -47,6 +48,7 @@ public class DhcpL2GwUtil {
         this.l2GatewayCache = l2GatewayCache;
     }
 
+    @Nullable
     public IpAddress getHwvtepNodeTunnelIp(InstanceIdentifier<Node> nodeIid) {
         String nodeId = nodeIid.firstKeyOf(Node.class).getNodeId().getValue();
         L2GatewayDevice targetDevice = null;
@@ -60,6 +62,7 @@ public class DhcpL2GwUtil {
     }
 
 
+    @Nullable
     private IpAddress getTunnelIp(InstanceIdentifier<Node> nodeIid) {
         Optional<Node> nodeOptional =
                 MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, nodeIid);
index eaaad90fd89afdd22cb17caabca9cb4e14116fbc..0d9606fb450a99da9c06466c180e8d7b30b787d7 100644 (file)
@@ -146,6 +146,7 @@ public class DhcpManager {
         this.dhcpOptLeaseTime = leaseTime;
     }
 
+    @Nullable
     public Subnet getNeutronSubnet(Port port) {
         if (port != null) {
             // DHCP Service is only interested in IPv4 IPs/Subnets
@@ -154,6 +155,7 @@ public class DhcpManager {
         return null;
     }
 
+    @Nullable
     public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
         for (FixedIps fixedIp: fixedIps) {
             if (fixedIp.getIpAddress().getIpv4Address() != null) {
@@ -163,6 +165,7 @@ public class DhcpManager {
         return null;
     }
 
+    @Nullable
     private Subnet getNeutronSubnet(Uuid subnetId) {
         Subnet subnet = null;
         InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet
@@ -175,6 +178,7 @@ public class DhcpManager {
         return subnet;
     }
 
+    @Nullable
     public Port getNeutronPort(String name) {
         Port prt = null;
         InstanceIdentifier<Port> inst = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class,
index 3559117886706560f555f35d71148edbe879a2c1..0f1e173f50eca9787b80b3f77e8f33f580baa631 100644 (file)
@@ -9,8 +9,10 @@ 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;
 import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
@@ -133,11 +135,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 (original.getFixedIps().size() < update.getFixedIps().size()) {
+        if (nullToEmpty(original.getFixedIps()).size() < nullToEmpty(update.getFixedIps()).size()) {
             final String interfaceName = update.getUuid().getValue();
-            List<FixedIps> updatedFixedIps = update.getFixedIps();
+            List<FixedIps> updatedFixedIps = new ArrayList<>(nullToEmpty(update.getFixedIps()));
             // Need to check only the newly added fixed ip.
-            updatedFixedIps.removeAll(original.getFixedIps());
+            updatedFixedIps.removeAll(nullToEmpty(original.getFixedIps()));
             Subnet subnet = dhcpManager.getNeutronSubnet(updatedFixedIps);
             if (null == subnet || !subnet.isEnableDhcp()) {
                 LOG.trace("Subnet is null/not ipv4 or not enabled {}", subnet);
index 1c4c2b007f212bc14fcf2c0cd1a18e42a24d611b..fc15f6c938efd0070f8a297e2cc0e2a1755b9e20 100644 (file)
@@ -7,6 +7,8 @@
  */
 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;
@@ -20,6 +22,7 @@ import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.apache.commons.net.util.SubnetUtils;
@@ -256,6 +259,7 @@ public class DhcpPktHandler implements PacketProcessingListener {
         JdkFutures.addErrorLogging(pktService.transmitPacket(output), LOG, "Transmit packet");
     }
 
+    @Nullable
     private DHCP handleDhcpPacket(DHCP dhcpPkt, String interfaceName, String macAddress, Port interfacePort,
                                   Subnet subnet, String serverIp) {
         LOG.trace("DHCP pkt rcvd {}", dhcpPkt);
@@ -278,6 +282,7 @@ public class DhcpPktHandler implements PacketProcessingListener {
         return reply;
     }
 
+    @Nullable
     private DhcpInfo handleDhcpNeutronPacket(byte msgType, Port port, Subnet subnet, String serverIp) {
         if (msgType == DHCPConstants.MSG_DECLINE) {
             LOG.trace("DHCPDECLINE received");
@@ -290,6 +295,7 @@ public class DhcpPktHandler implements PacketProcessingListener {
     }
 
 
+    @Nullable
     private DhcpInfo handleDhcpAllocationPoolPacket(byte msgType, String interfaceName, String macAddress) {
         try {
             String networkId = dhcpAllocationPoolMgr.getNetworkByPort(interfaceName);
@@ -329,6 +335,7 @@ public class DhcpPktHandler implements PacketProcessingListener {
         return dhcpInfo;
     }
 
+    @Nullable
     private DhcpInfo getDhcpInfo(Port port, Subnet subnet, String serverIp) {
         DhcpInfo dhcpInfo = null;
         if (port != null && subnet != null) {
@@ -358,13 +365,12 @@ public class DhcpPktHandler implements PacketProcessingListener {
         return dhcpInfo;
     }
 
+    @Nonnull
     private static DhcpInfo getApDhcpInfo(AllocationPool ap, IpAddress allocatedIp) {
-        DhcpInfo dhcpInfo = null;
-
         String clientIp = allocatedIp.stringValue();
         String serverIp = ap.getGateway().stringValue();
         List<IpAddress> dnsServers = ap.getDnsServers();
-        dhcpInfo = new DhcpInfo();
+        DhcpInfo dhcpInfo = new DhcpInfo();
         dhcpInfo.setClientIp(clientIp).setServerIp(serverIp).setCidr(ap.getSubnet().stringValue())
             .setHostRoutes(Collections.emptyList()).setDnsServersIpAddrs(dnsServers).setGatewayIp(serverIp);
 
@@ -376,9 +382,10 @@ public class DhcpPktHandler implements PacketProcessingListener {
      * Many other modules use/need similar methods. Should
      * be refactored to a common NeutronUtils module.     *
      */
+    @Nullable
     private static String getIpv4Address(Port port) {
 
-        for (FixedIps fixedIp : port.getFixedIps()) {
+        for (FixedIps fixedIp : nullToEmpty(port.getFixedIps())) {
             if (isIpv4Address(fixedIp.getIpAddress())) {
                 return fixedIp.getIpAddress().getIpv4Address().getValue();
             }
@@ -387,18 +394,21 @@ public class DhcpPktHandler implements PacketProcessingListener {
         return null;
     }
 
-    private static boolean isIpv4Address(IpAddress ip) {
+    private static boolean isIpv4Address(@Nullable IpAddress ip) {
         return ip != null && ip.getIpv4Address() != null;
     }
 
+    @Nullable
     private Subnet getNeutronSubnet(Port port) {
         return dhcpMgr.getNeutronSubnet(port);
     }
 
+    @Nullable
     private Port getNeutronPort(String interfaceName) {
         return dhcpMgr.getNeutronPort(interfaceName);
     }
 
+    @Nullable
     private static DHCP getDhcpPktIn(Ethernet actualEthernetPacket) {
         Ethernet ethPkt = actualEthernetPacket;
         if (ethPkt.getEtherType() == (short)NwConstants.ETHTYPE_802_1Q) {
@@ -500,6 +510,7 @@ public class DhcpPktHandler implements PacketProcessingListener {
     // "Consider returning a zero length array rather than null" - the eventual user of the returned byte[] likely
     // expects null and it's unclear what the behavior would be if empty array was returned.
     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
+    @Nullable
     protected byte[] getDhcpPacketOut(DHCP reply, Ethernet etherPkt, String phyAddrees) {
         if (reply == null) {
             /*
@@ -776,6 +787,7 @@ public class DhcpPktHandler implements PacketProcessingListener {
         return pktInReason == SendToController.class;
     }
 
+    @Nullable
     private String getInterfaceNameFromTag(long portTag) {
         String interfaceName = null;
         GetInterfaceFromIfIndexInput input =
index bedc427beebaa0724457b4a0eb2f98f7d9666c9a..43f3b0aa5963d3708b8d4586301c2c80444cb0ff 100644 (file)
@@ -9,6 +9,7 @@
 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;
@@ -194,8 +195,8 @@ public final class DhcpServiceUtils {
     }
 
     public static void setupDhcpArpRequest(BigInteger dpId, short tableId, BigInteger vni, String dhcpIpAddress,
-                                           int lportTag, Long elanTag, boolean add, IMdsalApiManager mdsalUtil,
-                                           TypedReadWriteTransaction<Configuration> tx)
+                                           int lportTag, @Nullable Long elanTag, boolean add,
+                                           IMdsalApiManager mdsalUtil, TypedReadWriteTransaction<Configuration> tx)
             throws ExecutionException, InterruptedException {
         List<MatchInfo> matches = getDhcpArpMatch(vni, dhcpIpAddress);
         if (add) {
@@ -275,7 +276,7 @@ public final class DhcpServiceUtils {
     @Nonnull
     private static List<BigInteger> extractDpnsFromNodes(Optional<Nodes> optionalNodes) {
         return optionalNodes.toJavaUtil().map(
-            nodes -> nodes.getNode().stream().map(Node::getId).filter(Objects::nonNull).map(
+            nodes -> nullToEmpty(nodes.getNode()).stream().map(Node::getId).filter(Objects::nonNull).map(
                     MDSALUtil::getDpnIdFromNodeName).collect(
                     Collectors.toList())).orElse(Collections.emptyList());
     }
@@ -289,7 +290,7 @@ public final class DhcpServiceUtils {
         Optional<ElanDpnInterfacesList> elanDpnOptional =
                 MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInstanceIdentifier);
         if (elanDpnOptional.isPresent()) {
-            List<DpnInterfaces> dpns = elanDpnOptional.get().getDpnInterfaces();
+            List<DpnInterfaces> dpns = nullToEmpty(elanDpnOptional.get().getDpnInterfaces());
             for (DpnInterfaces dpnInterfaces : dpns) {
                 elanDpns.add(dpnInterfaces.getDpId());
             }
@@ -297,6 +298,7 @@ public final class DhcpServiceUtils {
         return elanDpns;
     }
 
+    @Nullable
     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
             .state.Interface getInterfaceFromOperationalDS(String interfaceName, DataBroker dataBroker) {
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
@@ -311,6 +313,7 @@ public final class DhcpServiceUtils {
     }
 
 
+    @Nullable
     public static String getSegmentationId(Uuid networkId, DataBroker broker) {
         InstanceIdentifier<Network> inst = InstanceIdentifier.create(Neutron.class)
                 .child(Networks.class).child(Network.class, new NetworkKey(networkId));
@@ -327,6 +330,7 @@ public final class DhcpServiceUtils {
         return MDSALUtil.NODE_PREFIX + MDSALUtil.SEPARATOR + dpnId.toString();
     }
 
+    @Nullable
     public static String getTrunkPortMacAddress(String parentRefName,
             DataBroker broker) {
         InstanceIdentifier<Port> portInstanceIdentifier =
@@ -514,6 +518,7 @@ public final class DhcpServiceUtils {
         return existingEntry.get().getMacAddress();
     }
 
+    @Nullable
     private static String getNeutronMacAddress(String interfaceName, DhcpManager dhcpManager) {
         Port port = dhcpManager.getNeutronPort(interfaceName);
         if (port != null) {
@@ -523,6 +528,7 @@ public final class DhcpServiceUtils {
         return null;
     }
 
+    @Nullable
     public static List<Uuid> getSubnetIdsFromNetworkId(DataBroker broker, Uuid networkId) {
         InstanceIdentifier id = buildNetworkMapIdentifier(networkId);
         Optional<NetworkMap> optionalNetworkMap = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
@@ -533,9 +539,8 @@ public final class DhcpServiceUtils {
     }
 
     static InstanceIdentifier<NetworkMap> buildNetworkMapIdentifier(Uuid networkId) {
-        InstanceIdentifier<NetworkMap> id = InstanceIdentifier.builder(NetworkMaps.class).child(NetworkMap.class, new
-                NetworkMapKey(networkId)).build();
-        return id;
+        return InstanceIdentifier.builder(NetworkMaps.class).child(NetworkMap.class,
+            new NetworkMapKey(networkId)).build();
     }
 
     public static boolean isIpv4Subnet(DataBroker broker, Uuid subnetUuid) {
@@ -545,9 +550,7 @@ public final class DhcpServiceUtils {
         final Optional<Subnet> subnet = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, subnetidentifier);
         if (subnet.isPresent()) {
             Class<? extends IpVersionBase> ipVersionBase = subnet.get().getIpVersion();
-            if (ipVersionBase.equals(IpVersionV4.class)) {
-                return true;
-            }
+            return IpVersionV4.class.equals(ipVersionBase);
         }
         return false;
     }
index bcc61cb7faab160bad36976529c76b5b0b32b57f..77f1e39a8ad65367b8413cc6483df054c5206a57 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.netvirt.dhcpservice;
 
 import com.google.common.base.Optional;
 import java.math.BigInteger;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
@@ -152,6 +153,7 @@ public class DhcpUCastMacListener
                 DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, macAddress);
     }
 
+    @Nullable
     private LogicalSwitches getLogicalSwitches(LocalUcastMacs ucastMacs) {
         InstanceIdentifier<LogicalSwitches> logicalSwitchRef =
                 (InstanceIdentifier<LogicalSwitches>)ucastMacs.getLogicalSwitchRef().getValue();
index edf3d05c91808797c065b97e312583de9754b7ed..05838d2b641e5a5ce0af77838a4f19a9d825518f 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.netvirt.dhcpservice.shell;
 
+import javax.annotation.Nullable;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
 import org.opendaylight.netvirt.dhcpservice.api.IDhcpExternalTunnelManager;
@@ -24,6 +25,7 @@ public class DhcpCacheCli extends OsgiCommandSupport {
     }
 
     @Override
+    @Nullable
     protected Object doExecute() {
         session.getConsole().println("Printing Designated Dpns To TunnelIp and ElanName cache "
                 + "(DesignatedDpnsToTunnelIpElanNameCache)");