From ffc4005f85536f9d19379cab26a1d8dd9b3c4b10 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Thu, 2 Feb 2017 15:17:41 +0100 Subject: Lisp api use fix fixes use of Lisp api to new style with Object array instead of binary data Change-Id: Ib06f3d0b424cbb4288bb9b7ac8dc82c2fc0c69bf Signed-off-by: Jan Srnicek --- .../translate/write/RemoteMappingCustomizer.java | 91 ++++++------------ .../write/RemoteMappingCustomizerTest.java | 103 ++++++++++++++++++--- 2 files changed, 116 insertions(+), 78 deletions(-) (limited to 'lisp/lisp2vpp/src') diff --git a/lisp/lisp2vpp/src/main/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizer.java b/lisp/lisp2vpp/src/main/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizer.java index 9df5d49de..423625d97 100755 --- a/lisp/lisp2vpp/src/main/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizer.java +++ b/lisp/lisp2vpp/src/main/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizer.java @@ -17,31 +17,20 @@ package io.fd.hc2vpp.lisp.translate.write; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; -import static io.fd.hc2vpp.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.NEGATIVE; -import static io.fd.hc2vpp.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.POSITIVE; - import com.google.common.base.Preconditions; +import io.fd.hc2vpp.common.translate.util.AddressTranslator; +import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; +import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; import io.fd.hc2vpp.lisp.context.util.EidMappingContext; import io.fd.hc2vpp.lisp.translate.read.trait.MappingProducer; import io.fd.hc2vpp.lisp.translate.util.EidTranslator; import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer; -import io.fd.hc2vpp.common.translate.util.AddressTranslator; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; -import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.WriteFailedException; -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.TimeoutException; -import javax.annotation.Nonnull; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; +import io.fd.vpp.jvpp.VppBaseCallException; +import io.fd.vpp.jvpp.core.dto.LispAddDelRemoteMapping; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import io.fd.vpp.jvpp.core.types.RemoteLocator; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.MapReplyAction; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.MappingId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.RemoteMapping; @@ -50,12 +39,17 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.NegativeMapping; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.PositiveMapping; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.Rlocs; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.rlocs.Locator; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.eid.table.grouping.eid.table.VniTable; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import io.fd.vpp.jvpp.VppBaseCallException; -import io.fd.vpp.jvpp.core.dto.LispAddDelRemoteMapping; -import io.fd.vpp.jvpp.core.future.FutureJVppCore; + +import javax.annotation.Nonnull; +import java.io.IOException; +import java.util.Optional; +import java.util.concurrent.TimeoutException; + +import static com.google.common.base.Preconditions.*; +import static io.fd.hc2vpp.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.NEGATIVE; +import static io.fd.hc2vpp.lisp.translate.write.RemoteMappingCustomizer.LocatorListType.POSITIVE; /** @@ -143,7 +137,18 @@ public class RemoteMappingCustomizer extends FutureJVppCustomizer checkArgument(rlocs != null, "No remote locators set for Positive mapping"); - request.rlocs = locatorsToBinaryData(rlocs.getLocator()); + request.rlocs = rlocs.getLocator().stream() + .map(locator -> { + RemoteLocator remoteLocator = new RemoteLocator(); + remoteLocator.addr = ipAddressToArray(locator.getAddress()); + remoteLocator.isIp4 = booleanToByte(!isIpv6(locator.getAddress())); + Optional.ofNullable(locator.getPriority()) + .ifPresent(priority -> remoteLocator.priority = priority.byteValue()); + Optional.ofNullable(locator.getWeight()) + .ifPresent(weight -> remoteLocator.weight = weight.byteValue()); + + return remoteLocator; + }).toArray(RemoteLocator[]::new); request.rlocNum = Integer.valueOf(rlocs.getLocator().size()).byteValue(); } @@ -176,46 +181,6 @@ public class RemoteMappingCustomizer extends FutureJVppCustomizer return ((PositiveMapping) locatorList).getRlocs(); } - //cant be static because of use of default methods from traits - private byte[] locatorsToBinaryData(List locators) throws IOException { - checkNotNull(locators, "Cannot convert null list"); - - ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); - - DataOutputStream out = new DataOutputStream(byteArrayOut); - - - for (Locator locator : locators) { - boolean isIpv4; - byte[] address; - - //first byte says that its v4/v6 - isIpv4 = !isIpv6(locator.getAddress()); - out.writeByte(booleanToByte(isIpv4)); - - //then writes priority - out.write(locator.getPriority()); - - //and weight - out.write(locator.getWeight()); - - if (isIpv4) { - //vpp in this case needs address as 16 byte array,regardless if it is ivp4 or ipv6 - address = Arrays.copyOf( - - ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(locator.getAddress().getIpv4Address())), - 16); - - out.write(address); - } else { - out.write( - ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(locator.getAddress().getIpv6Address()))); - } - } - - return byteArrayOut.toByteArray(); - } - public enum LocatorListType { /** diff --git a/lisp/lisp2vpp/src/test/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizerTest.java b/lisp/lisp2vpp/src/test/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizerTest.java index fdd95e4f7..5130881ed 100755 --- a/lisp/lisp2vpp/src/test/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizerTest.java +++ b/lisp/lisp2vpp/src/test/java/io/fd/hc2vpp/lisp/translate/write/RemoteMappingCustomizerTest.java @@ -16,25 +16,18 @@ package io.fd.hc2vpp.lisp.translate.write; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import io.fd.hc2vpp.common.test.write.WriterCustomizerTest; import io.fd.hc2vpp.common.translate.util.Ipv4Translator; import io.fd.hc2vpp.lisp.context.util.EidMappingContext; import io.fd.honeycomb.translate.write.WriteFailedException; import io.fd.vpp.jvpp.core.dto.LispAddDelRemoteMapping; import io.fd.vpp.jvpp.core.dto.LispAddDelRemoteMappingReply; +import io.fd.vpp.jvpp.core.types.RemoteLocator; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder; @@ -48,7 +41,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.Eid; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.EidBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.NegativeMappingBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.PositiveMappingBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.negative.mapping.MapReplyBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.RlocsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.rlocs.LocatorBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.eid.table.grouping.EidTable; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.eid.table.grouping.eid.table.VniTable; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.eid.table.grouping.eid.table.VniTableKey; @@ -56,6 +52,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170315.lisp.feature.data.grouping.LispFeatureData; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; + public class RemoteMappingCustomizerTest extends WriterCustomizerTest implements Ipv4Translator { @Captor @@ -63,7 +69,9 @@ public class RemoteMappingCustomizerTest extends WriterCustomizerTest implements private MappingId mappingId; private RemoteMappingCustomizer customizer; - private RemoteMapping intf; + private RemoteMapping negativeMapping; + private RemoteMapping positiveMappingNoPrioNoWeight; + private RemoteMapping positiveMappingPrioWeight; private InstanceIdentifier id; @Mock @@ -82,13 +90,36 @@ public class RemoteMappingCustomizerTest extends WriterCustomizerTest implements mappingId = new MappingId("REMOTE"); final RemoteMappingKey key = new RemoteMappingKey(mappingId); - intf = new RemoteMappingBuilder() - .setEid( - eid) + negativeMapping = new RemoteMappingBuilder() + .setEid(eid) .setLocatorList(new NegativeMappingBuilder() .setMapReply(new MapReplyBuilder().setMapReplyAction(MapReplyAction.Drop).build()).build()) .build(); + positiveMappingNoPrioNoWeight = new RemoteMappingBuilder() + .setEid(eid) + .setLocatorList(new PositiveMappingBuilder() + .setRlocs(new RlocsBuilder() + .setLocator(Arrays.asList(new LocatorBuilder() + .setAddress(new IpAddress(new Ipv4Address("192.168.2.2"))) + .build())) + .build()) + .build()) + .build(); + + positiveMappingPrioWeight = new RemoteMappingBuilder() + .setEid(eid) + .setLocatorList(new PositiveMappingBuilder() + .setRlocs(new RlocsBuilder() + .setLocator(Arrays.asList(new LocatorBuilder() + .setAddress(new IpAddress(new Ipv4Address("192.168.2.3"))) + .setPriority((short) 2) + .setWeight((short) 5) + .build())) + .build()) + .build()) + .build(); + id = InstanceIdentifier.builder(Lisp.class) .child(LispFeatureData.class) .child(EidTable.class) @@ -102,6 +133,48 @@ public class RemoteMappingCustomizerTest extends WriterCustomizerTest implements when(api.lispAddDelRemoteMapping(any())).thenReturn(future(new LispAddDelRemoteMappingReply())); } + @Test + public void testWritePositiveMappingNoPrioNoWeight() throws WriteFailedException { + customizer.writeCurrentAttributes(id, positiveMappingNoPrioNoWeight, writeContext); + verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture()); + + final LispAddDelRemoteMapping request = mappingCaptor.getValue(); + assertNotNull(request); + assertEquals(1, request.isAdd); + assertEquals("192.168.2.1", arrayToIpv4AddressNoZone(request.eid).getValue()); + assertEquals(25, request.vni); + + final List remoteLocators = Arrays.stream(request.rlocs).collect(Collectors.toList()); + assertThat(remoteLocators, hasSize(1)); + + final RemoteLocator locator = remoteLocators.get(0); + assertArrayEquals(new byte[]{-64, -88, 2, 2}, locator.addr); + assertEquals(1, locator.isIp4); + assertEquals(0, locator.priority); + assertEquals(0, locator.weight); + } + + @Test + public void testWritePositiveMappingPrioWeight() throws WriteFailedException { + customizer.writeCurrentAttributes(id, positiveMappingPrioWeight, writeContext); + verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture()); + + final LispAddDelRemoteMapping request = mappingCaptor.getValue(); + assertNotNull(request); + assertEquals(1, request.isAdd); + assertEquals("192.168.2.1", arrayToIpv4AddressNoZone(request.eid).getValue()); + assertEquals(25, request.vni); + + final List remoteLocators = Arrays.stream(request.rlocs).collect(Collectors.toList()); + assertThat(remoteLocators, hasSize(1)); + + final RemoteLocator locator = remoteLocators.get(0); + assertArrayEquals(new byte[]{-64, -88, 2, 3}, locator.addr); + assertEquals(1, locator.isIp4); + assertEquals(2, locator.priority); + assertEquals(5, locator.weight); + } + @Test(expected = NullPointerException.class) public void testWriteCurrentAttributesNullData() throws WriteFailedException { customizer.writeCurrentAttributes(null, null, writeContext); @@ -114,7 +187,7 @@ public class RemoteMappingCustomizerTest extends WriterCustomizerTest implements @Test public void testWriteCurrentAttributes() throws WriteFailedException { - customizer.writeCurrentAttributes(id, intf, writeContext); + customizer.writeCurrentAttributes(id, negativeMapping, writeContext); verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture()); @@ -139,7 +212,7 @@ public class RemoteMappingCustomizerTest extends WriterCustomizerTest implements @Test public void testDeleteCurrentAttributes() throws WriteFailedException { when(remoteMappingContext.containsEid(any(), eq(mappingContext))).thenReturn(true); - customizer.deleteCurrentAttributes(id, intf, writeContext); + customizer.deleteCurrentAttributes(id, negativeMapping, writeContext); verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture()); -- cgit 1.2.3-korg