From 940956e1513837842a780152fc1db837afea1864 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Thu, 21 Jun 2018 08:39:59 +0200 Subject: Fix NPE in InterfaceDataTranslator.isInterfaceOfType SwInterfaceDetails returned by dump manager are nullable. In that case isInterfaceOfType should return false. Change-Id: Iaf3d1f6c6923ed6863357fe2470716f135b6164f Signed-off-by: Marek Gradzki --- .../interfacesstate/InterfaceDataTranslator.java | 11 +++-- .../InterfaceDataTranslatorTest.java | 50 ++++++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) (limited to 'v3po/v3po2vpp') diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslator.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslator.java index 9f788afc6..75554d4b7 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslator.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslator.java @@ -29,6 +29,7 @@ import java.math.BigInteger; import java.util.Objects; import java.util.stream.Collector; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; @@ -177,9 +178,13 @@ public interface InterfaceDataTranslator extends ByteDataTranslator, JvppReplyCo return isInterfaceOfType(ifcType, vppInterfaceDetails); } - default boolean isInterfaceOfType(final Class ifcType, - final SwInterfaceDetails cachedDetails) { - return ifcType.equals(getInterfaceType(toString(cachedDetails.interfaceName))); + default boolean isInterfaceOfType(@Nonnull final Class ifcType, + @Nullable final SwInterfaceDetails cachedDetails) { + if (cachedDetails == null) { + return false; + } else { + return ifcType.equals(getInterfaceType(toString(cachedDetails.interfaceName))); + } } /** diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslatorTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslatorTest.java index 9a9697413..5595ebb5a 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslatorTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/InterfaceDataTranslatorTest.java @@ -17,22 +17,35 @@ package io.fd.hc2vpp.v3po.interfacesstate; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import io.fd.hc2vpp.v3po.interfacesstate.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails; +import java.nio.charset.StandardCharsets; import org.junit.Test; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.Tap; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VhostUser; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VxlanGpeTunnel; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170607.VxlanTunnel; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public class InterfaceDataTranslatorTest implements InterfaceDataTranslator { @Test public void testVppPhysAddrToYang() throws Exception { - assertEquals("01:02:03:04:05:06", vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5, 6})); + assertEquals("01:02:03:04:05:06", vppPhysAddrToYang(new byte[] {1, 2, 3, 4, 5, 6})); // Extended (64-bit) MAC addresses are currently not supported (it might require yang model update), // so test if extended part is ignored - assertEquals("0a:0b:0c:0d:0e:0f", vppPhysAddrToYang(new byte[]{0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0})); + assertEquals("0a:0b:0c:0d:0e:0f", vppPhysAddrToYang(new byte[] {0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0})); } @Test(expected = NullPointerException.class) @@ -42,7 +55,7 @@ public class InterfaceDataTranslatorTest implements InterfaceDataTranslator { @Test(expected = IllegalArgumentException.class) public void testVppPhysAddrToYangInvalidByteArrayLength() throws Exception { - vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5}); + vppPhysAddrToYang(new byte[] {1, 2, 3, 4, 5}); } @Test @@ -54,4 +67,35 @@ public class InterfaceDataTranslatorTest implements InterfaceDataTranslator { assertEquals(EthernetCsmacd.class, getInterfaceType("eth0.0")); assertEquals(EthernetCsmacd.class, getInterfaceType("local0")); } + + @Test + public void testIsInterfaceOfType() { + assertTrue(isInterfaceOfType(Tap.class, interfaceDetails("tap0"))); + assertTrue(isInterfaceOfType(VxlanTunnel.class, interfaceDetails("vxlan0"))); + assertTrue(isInterfaceOfType(VxlanGpeTunnel.class, interfaceDetails("vxlan_gpe0"))); + assertTrue(isInterfaceOfType(VhostUser.class, interfaceDetails("VirtualEthernet0/0/0"))); + assertTrue(isInterfaceOfType(EthernetCsmacd.class, interfaceDetails("eth0.0"))); + assertTrue(isInterfaceOfType(EthernetCsmacd.class, interfaceDetails("local0"))); + } + + @Test + public void testIsInterfaceOfTypeMissingIfc() throws ReadFailedException { + final InterfaceCacheDumpManager dumpManager = mock(InterfaceCacheDumpManager.class); + final ReadContext ctx = mock(ReadContext.class); + final String ifcName = "tapThatDoesNotExists"; + final InstanceIdentifier id = + InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(ifcName)); + + when(dumpManager.getInterfaceDetail(id, ctx, ifcName)).thenReturn(null); + + assertFalse(isInterfaceOfType(dumpManager, id, ctx, Tap.class)); + } + + private SwInterfaceDetails interfaceDetails(final String interfaceName) { + final SwInterfaceDetails details = new SwInterfaceDetails(); + details.interfaceName = interfaceName.getBytes(StandardCharsets.UTF_8); + return details; + } + + } \ No newline at end of file -- cgit 1.2.3-korg