From fb462a4622ce1ba07fb6fce7f2d2aac846391ab2 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Mon, 16 May 2016 15:02:42 +0200 Subject: HONEYCOMB-48: sub-interface CRUD support Change-Id: Id6ca6776ca988aef060ecc33c508b7b699c53d10 Signed-off-by: Marek Gradzki --- .../interfaces/SubInterfaceCustomizerTest.java | 189 +++++++++++++++++++++ .../v3po/interfacesstate/InterfaceUtilsTest.java | 2 + .../SubInterfaceCustomizerTest.java | 102 +++++++++++ 3 files changed, 293 insertions(+) create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/SubInterfaceCustomizerTest.java (limited to 'v3po/v3po2vpp/src/test/java/io/fd') diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizerTest.java new file mode 100644 index 000000000..d70654655 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/SubInterfaceCustomizerTest.java @@ -0,0 +1,189 @@ +/* + * 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.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.MockitoAnnotations.initMocks; + +import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; +import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; +import io.fd.honeycomb.v3po.translate.write.WriteContext; +import io.fd.honeycomb.v3po.translate.write.WriteFailedException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +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.VlanTag; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VlanType; +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.SubInterface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.SubInterfaceBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.dto.CreateSubif; +import org.openvpp.jvpp.dto.CreateSubifReply; +import org.openvpp.jvpp.future.FutureJVpp; + +public class SubInterfaceCustomizerTest { + + @Mock + private FutureJVpp api; + @Mock + private WriteContext writeContext; + + private NamingContext namingContext; + private SubInterfaceCustomizer customizer; + public static final String SUPER_IF_NAME = "local0"; + public static final int SUPER_IF_ID = 1; + + @Before + public void setUp() throws Exception { + initMocks(this); + InterfaceTypeTestUtils.setupWriteContext(writeContext, + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface.class); + namingContext = new NamingContext("generatedSubInterfaceName"); + // TODO create base class for tests using vppApi + customizer = new SubInterfaceCustomizer(api, namingContext); + namingContext.addName(SUPER_IF_ID, SUPER_IF_NAME); + } + + private SubInterface generateSubInterface(final String superIfName) { + SubInterfaceBuilder builder = new SubInterfaceBuilder(); + builder.setVlanType(VlanType._802dot1q); + builder.setIdentifier(11L); + builder.setNumberOfTags((short)1); + builder.setOuterId(new VlanTag(100)); + builder.setInnerId(new VlanTag(200)); + builder.setSuperInterface(superIfName); + return builder.build(); + } + + private CreateSubif generateSubInterfaceRequest(final int superIfId) { + CreateSubif request = new CreateSubif(); + request.subId = 11; + request.swIfIndex = superIfId; + request.oneTag = 1; + request.dot1Ad = 1; + request.outerVlanId = 100; + request.innerVlanId = 200; + return request; + } + + private InstanceIdentifier getSubInterfaceId(final String name) { + return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation( + VppInterfaceAugmentation.class).child(SubInterface.class); + } + + private void whenCreateSubifThen(final int retval) throws ExecutionException, InterruptedException { + final CompletableFuture replyFuture = new CompletableFuture<>(); + final CreateSubifReply reply = new CreateSubifReply(); + reply.retval = retval; + replyFuture.complete(reply); + doReturn(replyFuture).when(api).createSubif(any(CreateSubif.class)); + } + + private void whenCreateSubifThenSuccess() throws ExecutionException, InterruptedException { + whenCreateSubifThen(0); + } + + private void whenCreateSubifThenFailure() throws ExecutionException, InterruptedException { + whenCreateSubifThen(-1); + } + + private CreateSubif verifyCreateSubifWasInvoked(final CreateSubif expected) { + ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(CreateSubif.class); + verify(api).createSubif(argumentCaptor.capture()); + final CreateSubif actual = argumentCaptor.getValue(); + + assertEquals(expected.swIfIndex, actual.swIfIndex); + assertEquals(expected.subId, actual.subId); + assertEquals(expected.noTags, actual.noTags); + assertEquals(expected.oneTag, actual.oneTag); + assertEquals(expected.twoTags, actual.twoTags); + assertEquals(expected.dot1Ad, actual.dot1Ad); + assertEquals(expected.exactMatch, actual.exactMatch); + assertEquals(expected.defaultSub, actual.defaultSub); + assertEquals(expected.outerVlanIdAny, actual.outerVlanIdAny); + assertEquals(expected.innerVlanIdAny, actual.innerVlanIdAny); + assertEquals(expected.outerVlanId, actual.outerVlanId); + assertEquals(expected.innerVlanId, actual.innerVlanId); + return actual; + } + + @Test + public void testCreate() throws Exception { + final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME); + final String subIfaceName = "local0.sub1"; + final InstanceIdentifier id = getSubInterfaceId(subIfaceName); + + whenCreateSubifThenSuccess(); + + customizer.writeCurrentAttributes(id, subInterface, writeContext); + + verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID)); + assertTrue(namingContext.containsIndex(subIfaceName)); + } + + @Test + public void testCreateFailed() throws Exception { + final SubInterface subInterface = generateSubInterface(SUPER_IF_NAME); + final String subIfaceName = "local0.sub1"; + final InstanceIdentifier id = getSubInterfaceId(subIfaceName); + + whenCreateSubifThenFailure(); + + try { + customizer.writeCurrentAttributes(id, subInterface, writeContext); + } catch (WriteFailedException.CreateFailedException e) { + assertEquals(VppApiInvocationException.class, e.getCause().getClass()); + verifyCreateSubifWasInvoked(generateSubInterfaceRequest(SUPER_IF_ID)); + assertFalse(namingContext.containsIndex(subIfaceName)); + return; + } + fail("WriteFailedException.CreateFailedException was expected"); + } + + @Test + public void testUpdateNoChange() throws Exception { + final SubInterface before = generateSubInterface(SUPER_IF_NAME); + final SubInterface after = generateSubInterface(SUPER_IF_NAME); + customizer.updateCurrentAttributes(null, before, after, writeContext); + } + + @Test(expected = UnsupportedOperationException.class) + public void testUpdate() throws Exception { + final SubInterface before = generateSubInterface("eth0"); + final SubInterface after = generateSubInterface("eth1"); + customizer.updateCurrentAttributes(null, before, after, writeContext); + } + + @Test(expected = UnsupportedOperationException.class) + public void testDelete() throws Exception { + final SubInterface subInterface = generateSubInterface("eth0"); + customizer.deleteCurrentAttributes(null, subInterface, writeContext); + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java index 0a0eff03b..6832fe018 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; 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.opendaylight.params.xml.ns.yang.v3po.rev150105.SubInterface; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Tap; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanTunnel; @@ -47,6 +48,7 @@ public class InterfaceUtilsTest { assertEquals(Tap.class, InterfaceUtils.getInterfaceType("tap0")); assertEquals(VxlanTunnel.class, InterfaceUtils.getInterfaceType("vxlan0")); assertEquals(VhostUser.class, InterfaceUtils.getInterfaceType("VirtualEthernet0/0/0")); + assertEquals(SubInterface.class, InterfaceUtils.getInterfaceType("eth0.0")); assertEquals(EthernetCsmacd.class, InterfaceUtils.getInterfaceType("local0")); } } \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/SubInterfaceCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/SubInterfaceCustomizerTest.java new file mode 100644 index 000000000..211f818ea --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/SubInterfaceCustomizerTest.java @@ -0,0 +1,102 @@ +/* + * 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.interfacesstate; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import io.fd.honeycomb.v3po.translate.Context; +import io.fd.honeycomb.v3po.translate.read.ReadFailedException; +import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer; +import io.fd.honeycomb.v3po.translate.v3po.test.ChildReaderCustomizerTest; +import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; +import java.util.HashMap; +import java.util.Map; +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.VlanType; +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.VppInterfaceStateAugmentationBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.SubInterface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.SubInterfaceBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.dto.SwInterfaceDetails; + +public class SubInterfaceCustomizerTest extends ChildReaderCustomizerTest { + + private NamingContext interfacesContext; + + public SubInterfaceCustomizerTest() { + super(SubInterface.class); + } + + @Override + protected ChildReaderCustomizer initCustomizer() { + return new SubInterfaceCustomizer(api, interfacesContext); + } + + @Override + public void setUpBefore() { + interfacesContext = new NamingContext("generatedIfaceName"); + } + + private InstanceIdentifier getSubInterfaceId(final String name) { + return InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(name)).augmentation( + VppInterfaceStateAugmentation.class).child( + SubInterface.class); + } + + @Test + public void testMerge() { + final VppInterfaceStateAugmentationBuilder builder = mock(VppInterfaceStateAugmentationBuilder.class); + final SubInterface value = mock(SubInterface.class); + getCustomizer().merge(builder, value); + verify(builder).setSubInterface(value); + } + + @Test + public void testRead() throws ReadFailedException { + final Context ctx = new Context(); + final Map cachedInterfaceDump = new HashMap<>(); + final int ifId = 1; + final String ifName = "eth0.sub0"; + interfacesContext.addName(ifId, ifName); + final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails(); + ifaceDetails.subId = ifId; + ifaceDetails.interfaceName = ifName.getBytes(); + cachedInterfaceDump.put(ifId, ifaceDetails); + ctx.put(InterfaceCustomizer.DUMPED_IFCS_CONTEXT_KEY, cachedInterfaceDump); + + final SubInterfaceBuilder builder = mock(SubInterfaceBuilder.class); + getCustomizer().readCurrentAttributes(getSubInterfaceId(ifName), builder, ctx); + + verify(builder).setIdentifier((long)ifId); + verify(builder).setSuperInterface(interfacesContext.getArtificialName(0)); + verify(builder).setNumberOfTags((short)0); + verify(builder).setVlanType(VlanType._802dot1ad); + verify(builder, never()).setExactMatch(any()); + verify(builder, never()).setDefaultSubif(any()); + verify(builder, never()).setMatchAnyOuterId(any()); + verify(builder, never()).setMatchAnyInnerId(any()); + verify(builder, never()).setInnerId(any()); + verify(builder, never()).setOuterId(any()); + } +} \ No newline at end of file -- cgit 1.2.3-korg