diff options
author | 2016-07-22 19:20:18 +0800 | |
---|---|---|
committer | 2016-08-03 19:31:16 +0800 | |
commit | 3edacf6ea046b837907571911090aa1df4d60f69 (patch) | |
tree | 8d71c38693bf651b54c20894255f4d80cb2fa1b4 /v3po/v3po2vpp/src/test/java/io/fd/honeycomb | |
parent | 84eb5829ae847a08db1e6fd08de1be51762392a4 (diff) |
HONEYCOMB-83: Add Gre Feature within HC
PatchSet 7: refactor code
PatchSet 6: Refactor based on new code organization
PatchSet 5: consolidate review comments
PatchSet 4: refactor to support reader registry
PatchSet 3: rebuild
PatchSet 2: update outer-fib-id in v3po.yang.
PatchSet 1: Add GRE feature and support IPv6.
Change-Id: I1fc064b618f161eaef06395380949277e2f9f060
Signed-off-by: Hongjun Ni <hongjun.ni@intel.com>
Diffstat (limited to 'v3po/v3po2vpp/src/test/java/io/fd/honeycomb')
2 files changed, 386 insertions, 0 deletions
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfaces/GreCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfaces/GreCustomizerTest.java new file mode 100644 index 000000000..cc4854de6 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfaces/GreCustomizerTest.java @@ -0,0 +1,249 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.v3po.interfaces; + +import static io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMapping; +import static io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMappingIid; +import static java.util.Collections.singletonList; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; +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 static org.mockito.MockitoAnnotations.initMocks; + +import com.google.common.base.Optional; +import com.google.common.net.InetAddresses; +import io.fd.honeycomb.translate.MappingContext; +import io.fd.honeycomb.translate.ModificationCache; +import io.fd.honeycomb.translate.v3po.test.TestHelperUtils; +import io.fd.honeycomb.translate.v3po.util.NamingContext; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.WriteFailedException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping; +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.interfaces.rev140508.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Gre; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.GreBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.VppBaseCallException; +import org.openvpp.jvpp.VppInvocationException; +import org.openvpp.jvpp.dto.GreAddDelTunnel; +import org.openvpp.jvpp.dto.GreAddDelTunnelReply; +import org.openvpp.jvpp.future.FutureJVpp; + +public class GreCustomizerTest { + + private static final byte ADD_GRE = 1; + private static final byte DEL_GRE = 0; + + @Mock + private FutureJVpp api; + @Mock + private WriteContext writeContext; + @Mock + private MappingContext mappingContext; + + private GreCustomizer customizer; + private String ifaceName; + private InstanceIdentifier<Gre> id; + + @Before + public void setUp() throws Exception { + initMocks(this); + InterfaceTypeTestUtils.setupWriteContext(writeContext, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.GreTunnel.class); + // TODO create base class for tests using vppApi + NamingContext namingContext = new NamingContext("generateInterfaceNAme", "test-instance"); + final ModificationCache toBeReturned = new ModificationCache(); + doReturn(toBeReturned).when(writeContext).getModificationCache(); + doReturn(mappingContext).when(writeContext).getMappingContext(); + + customizer = new GreCustomizer(api, namingContext); + + ifaceName = "eth0"; + id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName)) + .augmentation(VppInterfaceAugmentation.class).child(Gre.class); + } + + private void whenGreAddDelTunnelThenSuccess() + throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException { + final CompletionStage<GreAddDelTunnelReply> replyCS = mock(CompletionStage.class); + final CompletableFuture<GreAddDelTunnelReply> replyFuture = mock(CompletableFuture.class); + when(replyCS.toCompletableFuture()).thenReturn(replyFuture); + final GreAddDelTunnelReply reply = new GreAddDelTunnelReply(); + when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply); + when(api.greAddDelTunnel(any(GreAddDelTunnel.class))).thenReturn(replyCS); + } + + /** + * Failure response send + */ + private void whenGreAddDelTunnelThenFailure() + throws ExecutionException, InterruptedException, VppInvocationException { + doReturn(TestHelperUtils.<GreAddDelTunnelReply>createFutureException()).when(api) + .greAddDelTunnel(any(GreAddDelTunnel.class)); + } + + private GreAddDelTunnel verifyGreAddDelTunnelWasInvoked(final Gre gre) throws VppInvocationException { + ArgumentCaptor<GreAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(GreAddDelTunnel.class); + verify(api).greAddDelTunnel(argumentCaptor.capture()); + final GreAddDelTunnel actual = argumentCaptor.getValue(); + assertEquals(0, actual.isIpv6); + assertArrayEquals(InetAddresses.forString(gre.getSrc().getIpv4Address().getValue()).getAddress(), + actual.srcAddress); + assertArrayEquals(InetAddresses.forString(gre.getDst().getIpv4Address().getValue()).getAddress(), + actual.dstAddress); + assertEquals(gre.getOuterFibId().intValue(), actual.outerFibId); + return actual; + } + + private void verifyGreAddWasInvoked(final Gre gre) throws VppInvocationException { + final GreAddDelTunnel actual = verifyGreAddDelTunnelWasInvoked(gre); + assertEquals(ADD_GRE, actual.isAdd); + } + + private void verifyGreDeleteWasInvoked(final Gre gre) throws VppInvocationException { + final GreAddDelTunnel actual = verifyGreAddDelTunnelWasInvoked(gre); + assertEquals(DEL_GRE, actual.isAdd); + } + + private static Gre generateGre(long vni) { + final GreBuilder builder = new GreBuilder(); + builder.setSrc(new IpAddress(new Ipv4Address("192.168.20.10"))); + builder.setDst(new IpAddress(new Ipv4Address("192.168.20.11"))); + builder.setOuterFibId(Long.valueOf(123)); + return builder.build(); + } + + private static Gre generateGre() { + return generateGre(Long.valueOf(11)); + } + + @Test + public void testWriteCurrentAttributes() throws Exception { + final Gre gre = generateGre(); + + whenGreAddDelTunnelThenSuccess(); + + doReturn(Optional.absent()) + .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class)); + + customizer.writeCurrentAttributes(id, gre, writeContext); + verifyGreAddWasInvoked(gre); + verify(mappingContext).put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get())); + } + + @Test + public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception { + final Gre gre = generateGre(); + + whenGreAddDelTunnelThenSuccess(); + final Optional<Mapping> ifcMapping = getMapping(ifaceName, 0); + + doReturn(Optional.of(new MappingsBuilder().setMapping(singletonList(ifcMapping.get())).build())) + .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class)); + + customizer.writeCurrentAttributes(id, gre, writeContext); + verifyGreAddWasInvoked(gre); + + // Remove the first mapping before putting in the new one + verify(mappingContext).delete(eq(getMappingIid(ifaceName, "test-instance"))); + verify(mappingContext).put(eq(getMappingIid(ifaceName, "test-instance")), eq(ifcMapping.get())); + } + + @Test + public void testWriteCurrentAttributesFailed() throws Exception { + final Gre gre = generateGre(); + + whenGreAddDelTunnelThenFailure(); + + try { + customizer.writeCurrentAttributes(id, gre, writeContext); + } catch (WriteFailedException.CreateFailedException e) { + assertTrue(e.getCause() instanceof VppBaseCallException); + verifyGreAddWasInvoked(gre); + // Mapping not stored due to failure + verify(mappingContext, times(0)).put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get())); + return; + } + fail("WriteFailedException.CreateFailedException was expected"); + } + + @Test + public void testUpdateCurrentAttributes() throws Exception { + try { + customizer.updateCurrentAttributes(id, generateGre(10), generateGre(11), writeContext); + } catch (WriteFailedException.UpdateFailedException e) { + assertEquals(UnsupportedOperationException.class, e.getCause().getClass()); + return; + } + fail("WriteFailedException.UpdateFailedException was expected"); + } + + @Test + public void testDeleteCurrentAttributes() throws Exception { + final Gre gre = generateGre(); + + whenGreAddDelTunnelThenSuccess(); + doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance")); + + customizer.deleteCurrentAttributes(id, gre, writeContext); + verifyGreDeleteWasInvoked(gre); + verify(mappingContext).delete(eq(getMappingIid(ifaceName, "test-instance"))); + } + + @Test + public void testDeleteCurrentAttributesaFailed() throws Exception { + final Gre gre = generateGre(); + + whenGreAddDelTunnelThenFailure(); + doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance")); + + try { + customizer.deleteCurrentAttributes(id, gre, writeContext); + } catch (WriteFailedException.DeleteFailedException e) { + assertTrue(e.getCause() instanceof VppBaseCallException); + verifyGreDeleteWasInvoked(gre); + verify(mappingContext, times(0)).delete(eq(getMappingIid(ifaceName, "test-instance"))); + return; + } + fail("WriteFailedException.DeleteFailedException was expected"); + } +}
\ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/GreCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/GreCustomizerTest.java new file mode 100644 index 000000000..8ac13e442 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/GreCustomizerTest.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.v3po.interfacesstate; + +import static io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMapping; +import static io.fd.honeycomb.translate.v3po.test.ContextTestUtils.getMappingIid; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +import com.google.common.collect.Lists; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.honeycomb.translate.v3po.test.ReaderCustomizerTest; +import io.fd.honeycomb.translate.v3po.util.NamingContext; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import org.junit.Test; +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.rev150105.VppInterfaceStateAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Gre; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.GreBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.VppInvocationException; +import org.openvpp.jvpp.dto.SwInterfaceDetails; +import org.openvpp.jvpp.dto.GreTunnelDetails; +import org.openvpp.jvpp.dto.GreTunnelDetailsReplyDump; +import org.openvpp.jvpp.dto.GreTunnelDump; + +public class GreCustomizerTest extends ReaderCustomizerTest<Gre, GreBuilder> { + + private NamingContext interfacesContext; + static final InstanceIdentifier<Gre> IID = + InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey("ifc1")) + .augmentation(VppInterfaceStateAugmentation.class).child(Gre.class); + + public GreCustomizerTest() { + super(Gre.class); + } + + @Override + public void setUpBefore() { + interfacesContext = new NamingContext("gre-tunnel", "test-instance"); + doReturn(getMapping("ifc1", 0)).when(mappingContext).read(getMappingIid("ifc1", "test-instance")); + + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "gre-tunnel4".getBytes(); + final Map<Integer, SwInterfaceDetails> map = new HashMap<>(); + map.put(0, v); + cache.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, map); + } + + @Override + protected void setUpAfter() throws UnknownHostException, VppInvocationException { + final CompletableFuture<GreTunnelDetailsReplyDump> greTunnelDetailsReplyDumpCompletionStage = + new CompletableFuture<>(); + + final GreTunnelDetailsReplyDump value = new GreTunnelDetailsReplyDump(); + final GreTunnelDetails greTunnelDetails = new GreTunnelDetails(); + greTunnelDetails.isIpv6 = 0; + greTunnelDetails.dstAddress = InetAddress.getByName("1.2.3.4").getAddress(); + greTunnelDetails.srcAddress = InetAddress.getByName("1.2.3.5").getAddress(); + greTunnelDetails.outerFibId = 55; + greTunnelDetails.swIfIndex = 0; + + value.greTunnelDetails = Lists.newArrayList(greTunnelDetails); + greTunnelDetailsReplyDumpCompletionStage.complete(value); + + doReturn(greTunnelDetailsReplyDumpCompletionStage).when(api).greTunnelDump(any(GreTunnelDump.class)); + } + + @Test + public void testReadCurrentAttributes() throws Exception { + final GreBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + + assertEquals(55, builder.getOuterFibId().intValue()); + + assertNull(builder.getSrc().getIpv6Address()); + assertNotNull(builder.getSrc().getIpv4Address()); + assertEquals("1.2.3.5", builder.getSrc().getIpv4Address().getValue()); + + assertNull(builder.getDst().getIpv6Address()); + assertNotNull(builder.getDst().getIpv4Address()); + assertEquals("1.2.3.4", builder.getDst().getIpv4Address().getValue()); + + verify(api).greTunnelDump(any(GreTunnelDump.class)); + } + + @Test(expected = NullPointerException.class) + public void testReadCurrentAttributesVppNameNotCached() throws Exception { + InterfaceCustomizer.getCachedInterfaceDump(cache).remove(0); + + final GreBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + } + + @Test + public void testReadCurrentAttributesWrongType() throws Exception { + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "tap-2".getBytes(); + InterfaceCustomizer.getCachedInterfaceDump(cache).put(0, v); + + final GreBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + + // Should be ignored + verifyZeroInteractions(api); + } + + @Override + protected ReaderCustomizer<Gre, GreBuilder> initCustomizer() { + return new GreCustomizer(api, interfacesContext); + } +}
\ No newline at end of file |