diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-05-06 09:41:51 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-05-11 09:21:01 +0200 |
commit | 44348596ff0c675bf3b799f88288111d92d771e6 (patch) | |
tree | 17edea7566a34efe7dedb5ee3d7833616e24f121 /v3po/v3po2vpp/src/test/java/io | |
parent | 557acf16589e03bf17d255af7438614e63c2d2d3 (diff) |
HONEYCOMG-47: Tap interface CRUD support
Tap interface specific configuration and state was added to V3po
yang model.
TapCustomizer added.
Fixed customizers for Interfaces state.
Fixed bug in Bridge domain customizers.
Change-Id: I9dd47b8ada5153df8732c02cb59d331ab1adc71e
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'v3po/v3po2vpp/src/test/java/io')
3 files changed, 199 insertions, 11 deletions
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/TapCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/TapCustomizerTest.java new file mode 100644 index 000000000..668eed4cf --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/TapCustomizerTest.java @@ -0,0 +1,145 @@ +/* + * 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.v3po.translate.v3po.interfaces; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import io.fd.honeycomb.v3po.translate.Context; +import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; +import java.util.concurrent.CompletableFuture; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +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.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress; +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.Tap; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.TapBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.dto.TapConnect; +import org.openvpp.jvpp.dto.TapConnectReply; +import org.openvpp.jvpp.dto.TapDelete; +import org.openvpp.jvpp.dto.TapDeleteReply; +import org.openvpp.jvpp.dto.TapModify; +import org.openvpp.jvpp.dto.TapModifyReply; +import org.openvpp.jvpp.future.FutureJVpp; + +public class TapCustomizerTest { + + @Mock + private FutureJVpp vppApi; + private NamingContext ctx; + private TapCustomizer tapCustomizer; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + ctx = new NamingContext("ifcintest"); + tapCustomizer = new TapCustomizer(vppApi, ctx); + } + + @Test + public void testCreate() throws Exception { + doAnswer(new Answer() { + + int idx = 0; + + @Override + public Object answer(final InvocationOnMock invocation) throws Throwable { + final CompletableFuture<Object> reply = new CompletableFuture<>(); + final TapConnectReply t = new TapConnectReply(); + t.swIfIndex = idx++; + t.retval = 0; + reply.complete(t); + return reply; + } + }).when(vppApi).tapConnect(any(TapConnect.class)); + + tapCustomizer.writeCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), new Context()); + tapCustomizer.writeCurrentAttributes(getTapId("tap2"), getTapData("tap2", "ff:ff:ff:ff:ff:ff"), new Context()); + + verify(vppApi, times(2)).tapConnect(any(TapConnect.class)); + assertTrue(ctx.containsIndex("tap")); + assertTrue(ctx.containsIndex("tap2")); + } + + @Test + public void testModify() throws Exception { + final CompletableFuture<TapConnectReply> reply = new CompletableFuture<>(); + final TapConnectReply t = new TapConnectReply(); + t.swIfIndex = 0; + reply.complete(t); + doReturn(reply).when(vppApi).tapConnect(any(TapConnect.class)); + + final CompletableFuture<TapModifyReply> replyModif = new CompletableFuture<>(); + final TapModifyReply tmodif = new TapModifyReply(); + tmodif.swIfIndex = 0; + tmodif.retval = 0; + replyModif.complete(tmodif); + doReturn(replyModif).when(vppApi).tapModify(any(TapModify.class)); + + tapCustomizer.writeCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), new Context()); + tapCustomizer.updateCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), getTapData("tap", "ff:ff:ff:ff:ff:f1"), new Context()); + + verify(vppApi).tapConnect(any(TapConnect.class)); + verify(vppApi).tapModify(any(TapModify.class)); + assertTrue(ctx.containsIndex("tap")); + assertFalse(ctx.containsIndex("tap2")); + } + + @Test + public void testDelete() throws Exception { + final CompletableFuture<TapConnectReply> reply = new CompletableFuture<>(); + final TapConnectReply t = new TapConnectReply(); + t.swIfIndex = 0; + reply.complete(t); + doReturn(reply).when(vppApi).tapConnect(any(TapConnect.class)); + + final CompletableFuture<TapDeleteReply> replyDelete = new CompletableFuture<>(); + final TapDeleteReply tmodif = new TapDeleteReply(); + tmodif.retval = 0; + replyDelete.complete(tmodif); + doReturn(replyDelete).when(vppApi).tapDelete(any(TapDelete.class)); + + tapCustomizer.writeCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), new Context()); + tapCustomizer.deleteCurrentAttributes(getTapId("tap"), getTapData("tap", "ff:ff:ff:ff:ff:ff"), new Context()); + + verify(vppApi).tapConnect(any(TapConnect.class)); + verify(vppApi).tapDelete(any(TapDelete.class)); + assertFalse(ctx.containsIndex("tap")); + } + + private InstanceIdentifier<Tap> getTapId(final String tap) { + return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(tap)).augmentation( + VppInterfaceAugmentation.class).child(Tap.class); + } + + private Tap getTapData(final String tap, final String mac) { + return new TapBuilder().setTapName(tap).setMac(new PhysAddress(mac)).build(); + } +}
\ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizerTest.java index 7c88c3198..3f33d141f 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceCustomizerTest.java @@ -16,16 +16,16 @@ package io.fd.honeycomb.v3po.translate.v3po.interfacesstate; -import static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceUtils.YangIfIndexToVpp; +import static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceUtils.yangIfIndexToVpp; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; 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.honeycomb.v3po.translate.read.ReadFailedException; import io.fd.honeycomb.v3po.translate.spi.read.RootReaderCustomizer; import io.fd.honeycomb.v3po.translate.v3po.test.ListReaderCustomizerTest; import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; @@ -63,6 +63,8 @@ public class InterfaceCustomizerTest extends @Override protected RootReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() { + interfacesContext.addName(0, "eth0"); + interfacesContext.addName(1, "eth1"); return new InterfaceCustomizer(api, interfacesContext); } @@ -75,10 +77,11 @@ public class InterfaceCustomizerTest extends verify(builder).setInterface(value); } - private void verifyBridgeDomainDumpUpdateWasInvoked(final int nameFilterValid, final String ifaceName) { + private void verifyBridgeDomainDumpUpdateWasInvoked(final int nameFilterValid, final String ifaceName, + final int dumpIfcsInvocationCount) { // TODO adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete ArgumentCaptor<SwInterfaceDump> argumentCaptor = ArgumentCaptor.forClass(SwInterfaceDump.class); - verify(api).swInterfaceDump(argumentCaptor.capture()); + verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(argumentCaptor.capture()); final SwInterfaceDump actual = argumentCaptor.getValue(); assertEquals(nameFilterValid, actual.nameFilterValid); assertArrayEquals(ifaceName.getBytes(), actual.nameFilter); @@ -86,7 +89,7 @@ public class InterfaceCustomizerTest extends private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) { assertEquals(iface.getName(), new String(details.interfaceName)); - assertEquals(YangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex); + assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex); assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address)); } @@ -119,7 +122,7 @@ public class InterfaceCustomizerTest extends getCustomizer().readCurrentAttributes(id, builder, ctx); - verifyBridgeDomainDumpUpdateWasInvoked(1, ifaceName); + verifyBridgeDomainDumpUpdateWasInvoked(1, ifaceName, 1); assertIfacesAreEqual(builder.build(), iface); } @@ -134,8 +137,8 @@ public class InterfaceCustomizerTest extends try { getCustomizer().readCurrentAttributes(id, builder, ctx); - } catch (ReadFailedException e) { - verifyBridgeDomainDumpUpdateWasInvoked(1, ifaceName); + } catch (IllegalArgumentException e) { + verifyBridgeDomainDumpUpdateWasInvoked(0, ifaceName, 2); return; } @@ -149,16 +152,18 @@ public class InterfaceCustomizerTest extends final String swIf0Name = "eth0"; final SwInterfaceDetails swIf0 = new SwInterfaceDetails(); + swIf0.swIfIndex = 0; swIf0.interfaceName = swIf0Name.getBytes(); - final String swIf1Name = "eth0"; + final String swIf1Name = "eth1"; final SwInterfaceDetails swIf1 = new SwInterfaceDetails(); + swIf1.swIfIndex = 1; swIf1.interfaceName = swIf1Name.getBytes(); whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1)); final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(swIf0Name), new InterfaceKey(swIf1Name)); final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx); - verifyBridgeDomainDumpUpdateWasInvoked(0, ""); + verifyBridgeDomainDumpUpdateWasInvoked(0, "", 1); assertEquals(expectedIds, actualIds); } diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java index 115eb2b9c..b28234db4 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/utils/V3poUtilsTest.java @@ -1,6 +1,7 @@ package io.fd.honeycomb.v3po.translate.v3po.utils; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -14,4 +15,41 @@ public class V3poUtilsTest { final String jString = V3poUtils.toString(cString); assertArrayEquals(expected, jString.getBytes()); } -}
\ No newline at end of file + + @Test + public void testParseMac() throws Exception { + byte[] bytes = V3poUtils.parseMac("00:fF:7f:15:5e:A9"); + assertMac(bytes); + } + + private void assertMac(final byte[] bytes) { + assertEquals(6, bytes.length); + assertEquals((byte)0, bytes[0]); + assertEquals((byte)255, bytes[1]); + assertEquals((byte)127, bytes[2]); + assertEquals((byte)21, bytes[3]); + assertEquals((byte)94, bytes[4]); + assertEquals((byte)169, bytes[5]); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseMacLonger() throws Exception { + byte[] bytes = V3poUtils.parseMac("00:fF:7f:15:5e:A9:88:77"); + assertMac(bytes); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseMacShorter() throws Exception { + V3poUtils.parseMac("00:fF:7f"); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseRandomString() throws Exception { + V3poUtils.parseMac("random{}}@$*&*!"); + } + + @Test(expected = NumberFormatException.class) + public void testParseMacNumberFormatEx() throws Exception { + V3poUtils.parseMac("00:XX:7f:15:5e:77\""); + } +} |