From e187f2bd1301a3f20d5316c5a14a99b733f07550 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Mon, 27 Jun 2016 11:49:12 +0200 Subject: Fix IPv4 read caching Single cache key was used for each interface during a single read returing same IPs for all the interfaces Change-Id: I8cc05591b257d44a253cc23c9d79d9096459dcdd Signed-off-by: Maros Marsalek --- .../ip/Ipv4AddressCustomizerTest.java | 77 ++++++++++++++++++---- 1 file changed, 64 insertions(+), 13 deletions(-) (limited to 'v3po/v3po2vpp/src/test') diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java index 85f528e1e..584464645 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java @@ -19,7 +19,9 @@ package io.fd.honeycomb.v3po.translate.v3po.interfacesstate.ip; import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping; import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid; import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.reverseBytes; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -38,6 +40,7 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; +import org.hamcrest.CoreMatchers; import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings; @@ -64,7 +67,9 @@ import org.openvpp.jvpp.dto.IpAddressDump; public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest { private static final String IFACE_NAME = "eth0"; + private static final String IFACE_2_NAME = "eth1"; private static final int IFACE_ID = 1; + private static final int IFACE_2_ID = 2; private NamingContext interfacesContext; @@ -80,24 +85,27 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest initCustomizer() { final KeyedInstanceIdentifier eth0Id = getMappingIid(IFACE_NAME, "test-instance"); + final KeyedInstanceIdentifier eth1Id = getMappingIid(IFACE_2_NAME, "test-instance"); final Optional eth0 = getMapping(IFACE_NAME, IFACE_ID); + final Optional eth1 = getMapping(IFACE_2_NAME, IFACE_2_ID); - final List allMappings = Lists.newArrayList(getMapping(IFACE_NAME, IFACE_ID).get()); + final List allMappings = Lists.newArrayList(eth0.get(), eth1.get()); final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build(); doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class)); doReturn(eth0).when(mappingContext).read(eth0Id); + doReturn(eth1).when(mappingContext).read(eth1Id); return new Ipv4AddressCustomizer(api, interfacesContext); } - private static InstanceIdentifier
getId(final String address) { + private static InstanceIdentifier
getId(final String address, final String ifaceName) { return InstanceIdentifier.builder(InterfacesState.class) - .child(Interface.class, new InterfaceKey(IFACE_NAME)) - .augmentation(Interface2.class) - .child(Ipv4.class) - .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address)))) - .build(); + .child(Interface.class, new InterfaceKey(ifaceName)) + .augmentation(Interface2.class) + .child(Ipv4.class) + .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address)))) + .build(); } @Test @@ -118,17 +126,60 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest id = getId("192.168.2.1"); + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); getCustomizer().readCurrentAttributes(id, builder, ctx); assertEquals("192.168.2.1", builder.getIp().getValue()); } + @Test + public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException { + ModificationCache cache = new ModificationCache(); + + IpAddressDetails detail1 = new IpAddressDetails(); + IpAddressDetails detail2 = new IpAddressDetails(); + + detail1.ip = reverseBytes( + TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); + detail2.ip = reverseBytes( + TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); + + IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); + reply.ipAddressDetails = ImmutableList.of(detail1); + IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump(); + reply2.ipAddressDetails = ImmutableList.of(detail2); + + CompletableFuture future = new CompletableFuture<>(); + future.complete(reply); + CompletableFuture future2 = new CompletableFuture<>(); + future2.complete(reply2); + + when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2); + when(ctx.getModificationCache()).thenReturn(cache); + + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); + final InstanceIdentifier
id2 = getId("192.168.2.2", IFACE_2_NAME); + + final List ifc1Ids = getCustomizer().getAllIds(id, ctx); + assertThat(ifc1Ids.size(), is(1)); + assertThat(ifc1Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1")))); + final List ifc2Ids = getCustomizer().getAllIds(id2, ctx); + assertThat(ifc2Ids.size(), is(1)); + assertThat(ifc2Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2")))); + + AddressBuilder builder = new AddressBuilder(); + getCustomizer().readCurrentAttributes(id, builder, ctx); + assertEquals(builder.getIp().getValue(), "192.168.2.1"); + builder = new AddressBuilder(); + getCustomizer().readCurrentAttributes(id2, builder, ctx); + assertEquals(builder.getIp().getValue(), "192.168.2.2"); + } + @Test public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException { ModificationCache cache = new ModificationCache(); @@ -155,7 +206,7 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest id = getId("192.168.2.1"); + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); getCustomizer().readCurrentAttributes(id, builder, ctx); @@ -180,10 +231,10 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest id = getId("192.168.2.1"); + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); List ids = getCustomizer().getAllIds(id, ctx).stream() .map(key -> key.getIp()) @@ -220,7 +271,7 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest id = getId("192.168.2.1"); + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); List ids = getCustomizer().getAllIds(id, ctx).stream() .map(key -> key.getIp()) -- cgit 1.2.3-korg