Handle nullable lists in ipv6service 54/77254/2
authorStephen Kitt <skitt@redhat.com>
Wed, 24 Oct 2018 14:08:46 +0000 (16:08 +0200)
committerSam Hague <shague@redhat.com>
Thu, 25 Oct 2018 22:46:09 +0000 (22:46 +0000)
Following YANGTOOLS-585, lists can be null (which is correctly
indicated with an @Nullable annotation). This patch deals with the
fallout.

Change-Id: Ibcc5c076574992e522dfa9f40947615c9a717618
Signed-off-by: Stephen Kitt <skitt@redhat.com>
ipv6service/impl/src/main/java/org/opendaylight/netvirt/ipv6service/IfMgr.java
ipv6service/impl/src/main/java/org/opendaylight/netvirt/ipv6service/Ipv6PktHandler.java
ipv6service/impl/src/main/java/org/opendaylight/netvirt/ipv6service/Ipv6RouterAdvt.java
ipv6service/impl/src/main/java/org/opendaylight/netvirt/ipv6service/NeutronPortChangeListener.java
ipv6service/impl/src/main/java/org/opendaylight/netvirt/ipv6service/VirtualNetwork.java
ipv6service/impl/src/main/java/org/opendaylight/netvirt/ipv6service/utils/Ipv6ServiceUtils.java
ipv6service/shell/src/main/java/org/opendaylight/netvirt/ipv6service/shell/ShowIpv6Command.java

index 42fc8b8ea2ee9805c2ce566ec587458e87fe21ba..e7a6e7db00c83566fb000d08d9bf86fca6503b34 100644 (file)
@@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
@@ -225,6 +226,7 @@ public class IfMgr implements ElementCache, AutoCloseable {
         }
     }
 
+    @Nullable
     private VirtualRouter getRouter(Uuid rtrId) {
         return rtrId != null ? vrouters.get(rtrId) : null;
     }
@@ -347,6 +349,7 @@ public class IfMgr implements ElementCache, AutoCloseable {
         }
     }
 
+    @Nullable
     private VirtualSubnet getSubnet(Uuid snetId) {
         return snetId != null ? vsubnets.get(snetId) : null;
     }
@@ -393,6 +396,7 @@ public class IfMgr implements ElementCache, AutoCloseable {
         }
     }
 
+    @Nullable
     private VirtualPort getPort(Uuid portId) {
         return portId != null ? vintfs.get(portId) : null;
     }
@@ -532,6 +536,7 @@ public class IfMgr implements ElementCache, AutoCloseable {
         }
     }
 
+    @Nullable
     public Set<VirtualPort> removeUnprocessed(Map<Uuid, Set<VirtualPort>> unprocessed, Uuid id) {
         if (id != null) {
             return unprocessed.remove(id);
@@ -583,11 +588,13 @@ public class IfMgr implements ElementCache, AutoCloseable {
         return removedSubnets != null ? removedSubnets : Collections.emptySet();
     }
 
+    @Nullable
     public VirtualPort getRouterV6InterfaceForNetwork(Uuid networkId) {
         LOG.debug("obtaining the virtual interface for {}", networkId);
         return networkId != null ? vrouterv6IntfMap.get(networkId) : null;
     }
 
+    @Nullable
     public VirtualPort obtainV6Interface(Uuid id) {
         VirtualPort intf = getPort(id);
         if (intf == null) {
@@ -821,6 +828,7 @@ public class IfMgr implements ElementCache, AutoCloseable {
         return interfaceName;
     }
 
+    @Nullable
     public Long updateNetworkElanTag(Uuid networkId) {
         Long elanTag = null;
         if (null != this.elanProvider) {
@@ -836,10 +844,12 @@ public class IfMgr implements ElementCache, AutoCloseable {
         return elanTag;
     }
 
+    @Nullable
     private VirtualNetwork getNetwork(Uuid networkId) {
         return networkId != null ? vnetworks.get(networkId) : null;
     }
 
+    @Nullable
     public Long getNetworkElanTag(Uuid networkId) {
         Long elanTag = null;
         IVirtualNetwork net = getNetwork(networkId);
index 7b6baa102335d77451167de1ac97aecc5f8ea9d9..4f6b8cae58f306337ca82a9db2d7220c147ce4ea 100644 (file)
@@ -247,7 +247,7 @@ public class Ipv6PktHandler implements AutoCloseable, PacketProcessingListener {
         private void updateNAResponse(NeighborSolicitationPacket pdu,
                                       VirtualPort port, NeighborAdvertisePacketBuilder naPacket) {
             long flag = 0;
-            if (!pdu.getSourceIpv6().equals(Ipv6ServiceUtils.UNSPECIFIED_ADDR)) {
+            if (!Ipv6ServiceUtils.UNSPECIFIED_ADDR.equals(pdu.getSourceIpv6())) {
                 naPacket.setDestinationIpv6(pdu.getSourceIpv6());
                 flag = 0xE0; // Set Router, Solicited and Override Flag.
             } else {
index ad30f84de65e2eaff8362752ffae4cba4be7a625..3bac01f69d8c909aa62f18786aa537b79fcb19eb 100644 (file)
@@ -9,12 +9,14 @@
 package org.opendaylight.netvirt.ipv6service;
 
 import static java.util.Objects.requireNonNull;
+import static org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceUtils.nullToEmpty;
 
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import javax.annotation.Nullable;
 import org.opendaylight.genius.ipv6util.api.Icmpv6Type;
 import org.opendaylight.genius.ipv6util.api.Ipv6Constants;
 import org.opendaylight.genius.ipv6util.api.Ipv6Constants.Ipv6RouterAdvertisementType;
@@ -55,7 +57,7 @@ public class Ipv6RouterAdvt {
     }
 
     public boolean transmitRtrAdvertisement(Ipv6RouterAdvertisementType raType, VirtualPort routerPort,
-                                            long elanTag, RouterSolicitationPacket rsPdu,
+                                            long elanTag, @Nullable RouterSolicitationPacket rsPdu,
                                             BigInteger dpnId, Uuid port) {
         RouterAdvertisementPacketBuilder raPacket = new RouterAdvertisementPacketBuilder();
         updateRAResponse(raType, rsPdu, raPacket, routerPort);
@@ -86,7 +88,7 @@ public class Ipv6RouterAdvt {
         return true;
     }
 
-    private static void updateRAResponse(Ipv6RouterAdvertisementType raType, RouterSolicitationPacket pdu,
+    private static void updateRAResponse(Ipv6RouterAdvertisementType raType, @Nullable RouterSolicitationPacket pdu,
                                          RouterAdvertisementPacketBuilder raPacket,
                                          VirtualPort routerPort) {
         short icmpv6RaFlags = 0;
@@ -124,6 +126,9 @@ public class Ipv6RouterAdvt {
         MacAddress sourceMac = MacAddress.getDefaultInstance(gatewayMac);
         raPacket.setSourceMac(sourceMac);
         if (raType == Ipv6RouterAdvertisementType.SOLICITED_ADVERTISEMENT) {
+            if (pdu == null) {
+                throw new IllegalArgumentException("Null pdu for solicited advertisement");
+            }
             raPacket.setDestinationMac(pdu.getSourceMac());
             raPacket.setDestinationIpv6(pdu.getSourceIpv6());
             raPacket.setFlowLabel(pdu.getFlowLabel());
@@ -191,8 +196,6 @@ public class Ipv6RouterAdvt {
         }
 
         raPacket.setPrefixList(prefixList);
-
-        return;
     }
 
     private byte[] fillRouterAdvertisementPacket(RouterAdvertisementPacket pdu) {
@@ -223,7 +226,7 @@ public class Ipv6RouterAdvt {
         buf.put((byte)pdu.getSourceAddrLength().shortValue());
         buf.put(Ipv6Util.bytesFromHexString(pdu.getSourceLlAddress().getValue()));
 
-        for (PrefixList prefix : pdu.getPrefixList()) {
+        for (PrefixList prefix : nullToEmpty(pdu.getPrefixList())) {
             buf.put((byte)prefix.getOptionType().shortValue());
             buf.put((byte)prefix.getOptionLength().shortValue());
             buf.put((byte)prefix.getPrefixLength().shortValue());
index ea2786164cfae2ed659d120369e1b01119a96a28..5ba8456c387aa6e0f1c7d877a78febc94b477465 100644 (file)
@@ -7,10 +7,13 @@
  */
 package org.opendaylight.netvirt.ipv6service;
 
+import static org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceUtils.nullToEmpty;
+
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Singleton;
@@ -53,19 +56,18 @@ public class NeutronPortChangeListener extends AsyncClusteredDataTreeChangeListe
 
     @Override
     protected void add(InstanceIdentifier<Port> identifier, Port port) {
-        if (port.getDeviceOwner().equalsIgnoreCase(Ipv6ServiceConstants.NETWORK_ROUTER_GATEWAY)) {
+        if (Ipv6ServiceConstants.NETWORK_ROUTER_GATEWAY.equalsIgnoreCase(port.getDeviceOwner())) {
             // Todo: revisit when IPv6 north-south support is implemented.
             LOG.info("IPv6Service (TODO): Skipping router_gateway port {} for add event", port);
             return;
         }
-        if (port.getDeviceOwner().equalsIgnoreCase(Ipv6ServiceConstants.DEVICE_OWNER_DHCP)) {
+        if (Ipv6ServiceConstants.DEVICE_OWNER_DHCP.equalsIgnoreCase(port.getDeviceOwner())) {
             LOG.info("IPv6Service: Skipping network_dhcp port {} for add event", port);
             return;
         }
 
         LOG.debug("Add port notification handler is invoked for port {} ", port);
-        List<FixedIps> ipList = port.getFixedIps();
-        for (FixedIps fixedip : ipList) {
+        for (FixedIps fixedip : nullToEmpty(port.getFixedIps())) {
             if (fixedip.getIpAddress().getIpv4Address() != null) {
                 continue;
             }
@@ -75,12 +77,12 @@ public class NeutronPortChangeListener extends AsyncClusteredDataTreeChangeListe
 
     @Override
     protected void remove(InstanceIdentifier<Port> identifier, Port port) {
-        if (port.getDeviceOwner().equalsIgnoreCase(Ipv6ServiceConstants.NETWORK_ROUTER_GATEWAY)) {
+        if (Ipv6ServiceConstants.NETWORK_ROUTER_GATEWAY.equalsIgnoreCase(port.getDeviceOwner())) {
             // Todo: revisit when IPv6 north-south support is implemented.
             LOG.info("IPv6Service (TODO): Skipping router_gateway port {} for remove event", port);
             return;
         }
-        if (port.getDeviceOwner().equalsIgnoreCase(Ipv6ServiceConstants.DEVICE_OWNER_DHCP)) {
+        if (Ipv6ServiceConstants.DEVICE_OWNER_DHCP.equalsIgnoreCase(port.getDeviceOwner())) {
             LOG.info("IPv6Service: Skipping network_dhcp port {} for remove event", port);
             return;
         }
@@ -91,12 +93,12 @@ public class NeutronPortChangeListener extends AsyncClusteredDataTreeChangeListe
 
     @Override
     protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
-        if (update.getDeviceOwner().equalsIgnoreCase(Ipv6ServiceConstants.NETWORK_ROUTER_GATEWAY)) {
+        if (Ipv6ServiceConstants.NETWORK_ROUTER_GATEWAY.equalsIgnoreCase(update.getDeviceOwner())) {
             // Todo: revisit when IPv6 north-south support is implemented.
             LOG.info("IPv6Service (TODO): Skipping router_gateway port {} for update event", update);
             return;
         }
-        if (update.getDeviceOwner().equalsIgnoreCase(Ipv6ServiceConstants.DEVICE_OWNER_DHCP)) {
+        if (Ipv6ServiceConstants.DEVICE_OWNER_DHCP.equalsIgnoreCase(update.getDeviceOwner())) {
             LOG.info("IPv6Service: Skipping network_dhcp port {} for update event", update);
             return;
         }
@@ -133,7 +135,7 @@ public class NeutronPortChangeListener extends AsyncClusteredDataTreeChangeListe
     }
 
     protected void addInterfaceInfo(Port port, FixedIps fixedip) {
-        if (port.getDeviceOwner().equalsIgnoreCase(Ipv6ServiceConstants.NETWORK_ROUTER_INTERFACE)) {
+        if (Ipv6ServiceConstants.NETWORK_ROUTER_INTERFACE.equalsIgnoreCase(port.getDeviceOwner())) {
             LOG.info("IPv6: addInterfaceInfo is invoked for a router interface {}, fixedIp: {}", port, fixedip);
             // Add router interface
             ifMgr.addRouterIntf(port.getUuid(),
@@ -155,7 +157,7 @@ public class NeutronPortChangeListener extends AsyncClusteredDataTreeChangeListe
         }
     }
 
-    private Set<FixedIps> getFixedIpSet(List<FixedIps> fixedIps) {
+    private Set<FixedIps> getFixedIpSet(@Nullable List<FixedIps> fixedIps) {
         return fixedIps != null ? new HashSet<>(fixedIps) : Collections.emptySet();
     }
 
index 10b136a92394f7058e26e58c3310f8bf574eb3e9..3f7b600a46599bfc46a8a4cafa8f3bb94b719ad3 100644 (file)
@@ -15,6 +15,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
+import javax.annotation.Nullable;
 import org.opendaylight.genius.ipv6util.api.Ipv6Util;
 import org.opendaylight.netvirt.ipv6service.api.IVirtualNetwork;
 import org.opendaylight.netvirt.ipv6service.utils.Ipv6ServiceConstants;
@@ -68,6 +69,7 @@ public class VirtualNetwork implements IVirtualNetwork {
         return dpnIfaceList.values();
     }
 
+    @Nullable
     public DpnInterfaceInfo getDpnIfaceInfo(BigInteger dpId) {
         return dpId != null ? dpnIfaceList.get(dpId) : null;
     }
index b782ddd0e2ee16db45eac2f0da46ce0782123795..0d4e125cce392d449d89c9dc05842d4ad9fbfe28 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.netvirt.ipv6service.utils;
 
+import static java.util.Collections.emptyList;
+
 import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.net.InetAddress;
@@ -16,6 +18,8 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.apache.commons.lang3.StringUtils;
@@ -82,6 +86,7 @@ public class Ipv6ServiceUtils {
     public static final Ipv6Address ALL_NODES_MCAST_ADDR = newIpv6Address(Ipv6Constants.ALL_NODES_MCAST_ADDRESS);
     public static final Ipv6Address UNSPECIFIED_ADDR = newIpv6Address("0:0:0:0:0:0:0:0");
 
+    @Nullable
     private static Ipv6Address newIpv6Address(String ip) {
         try {
             return Ipv6Address.getDefaultInstance(InetAddress.getByName(ip).getHostAddress());
@@ -123,15 +128,10 @@ public class Ipv6ServiceUtils {
      * @param interfaceName the interface name
      * @return the interface.
      */
+    @Nullable
     public org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
         .Interface getInterface(String interfaceName) {
-        Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
-            .Interface> optInterface =
-                read(LogicalDatastoreType.CONFIGURATION, getInterfaceIdentifier(interfaceName));
-        if (optInterface.isPresent()) {
-            return optInterface.get();
-        }
-        return null;
+        return read(LogicalDatastoreType.CONFIGURATION, getInterfaceIdentifier(interfaceName)).orNull();
     }
 
     /**
@@ -168,11 +168,10 @@ public class Ipv6ServiceUtils {
      * @param interfaceName the interface name.
      * @return the interface state.
      */
+    @Nullable
     public org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
             .Interface getInterfaceStateFromOperDS(String interfaceName) {
-        InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
-                .interfaces.state.Interface> ifStateId = buildStateInterfaceId(interfaceName);
-        return MDSALUtil.read(LogicalDatastoreType.OPERATIONAL, ifStateId, broker).orNull();
+        return MDSALUtil.read(LogicalDatastoreType.OPERATIONAL, buildStateInterfaceId(interfaceName), broker).orNull();
     }
 
     private static List<MatchInfo> getIcmpv6RSMatch(Long elanTag) {
@@ -379,6 +378,7 @@ public class Ipv6ServiceUtils {
                         NwConstants.IPV6_SERVICE_INDEX)));
     }
 
+    @Nullable
     public BigInteger getDpIdFromInterfaceState(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
             .interfaces.rev140508.interfaces.state.Interface interfaceState) {
         BigInteger dpId = null;
@@ -407,4 +407,10 @@ public class Ipv6ServiceUtils {
         }
         return subnet.getIpVersion().equals(Ipv6ServiceConstants.IP_VERSION_V6) ? true : false;
     }
+
+    // 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 7838841a133ddafa44e96b92ab758a74c130ea92..27c5c6547d8a9b6039451a8c094c746cdeeaf44e 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.netvirt.ipv6service.shell;
 
 import java.util.List;
 import java.util.stream.Collectors;
+import javax.annotation.Nullable;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -38,6 +39,7 @@ public class ShowIpv6Command extends OsgiCommandSupport {
     }
 
     @Override
+    @Nullable
     protected Object doExecute() {
         TablePrinter tp = new TablePrinter();