diff options
Diffstat (limited to 'v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read')
19 files changed, 2410 insertions, 0 deletions
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/AfPacketCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/AfPacketCustomizerTest.java new file mode 100644 index 000000000..1719f1c20 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/AfPacketCustomizerTest.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2018 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.hc2vpp.v3po.read; + +import static io.fd.hc2vpp.v3po.read.AfPacketCustomizer.getCfgId; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.InitializingReaderCustomizerTest; +import io.fd.hc2vpp.common.test.util.InterfaceDumpHelper; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.AfPacketDetails; +import io.fd.jvpp.core.dto.AfPacketDetailsReplyDump; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import java.util.Collections; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.AfPacket; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.AfPacketBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class AfPacketCustomizerTest extends InitializingReaderCustomizerTest<AfPacket, AfPacketBuilder> + implements InterfaceDumpHelper { + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "host-veth1"; + private static final int IF_INDEX = 1; + private static final InstanceIdentifier<AfPacket> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(AfPacket.class); + + private NamingContext interfaceContext; + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public AfPacketCustomizerTest() { + super(AfPacket.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + protected void setUp() throws Exception { + interfaceContext = new NamingContext("generatedInterfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<AfPacket, AfPacketBuilder> initCustomizer() { + return new AfPacketCustomizer(api, interfaceContext, dumpCacheManager); + } + + @Test + public void testRead() throws ReadFailedException { + final AfPacketBuilder builder = mock(AfPacketBuilder.class); + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IF_NAME)).thenReturn(ifaceDetails()); + when(api.afPacketDump(any())).thenReturn(future(afPacketReplyDump())); + + getCustomizer().readCurrentAttributes(IID, builder, ctx); + + verify(builder).setMac(new PhysAddress("01:02:03:04:05:06")); + } + + @Test + public void testReadFailed() throws ReadFailedException { + final AfPacketBuilder builder = mock(AfPacketBuilder.class); + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IF_NAME)).thenReturn(null); + + getCustomizer().readCurrentAttributes(IID, builder, ctx); + + verifyZeroInteractions(builder); + } + + @Test + public void testInit() { + final AfPacket operData = new AfPacketBuilder() + .setHostInterfaceName(IF_NAME) + .setMac(new PhysAddress("11:22:33:44:55:66")).build(); + final org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.AfPacket + cfgData = + new org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.AfPacketBuilder() + .setHostInterfaceName(IF_NAME) + .setMac(new PhysAddress("11:22:33:44:55:66")).build(); + invokeInitTest(IID, operData, getCfgId(IID), cfgData); + } + + private SwInterfaceDetails ifaceDetails() { + final SwInterfaceDetails details = new SwInterfaceDetails(); + details.swIfIndex = IF_INDEX; + details.interfaceName = IF_NAME.getBytes(); + details.l2Address = new byte[] {1, 2, 3, 4, 5, 6}; + return details; + } + + private AfPacketDetailsReplyDump afPacketReplyDump() { + final AfPacketDetailsReplyDump reply = new AfPacketDetailsReplyDump(); + final AfPacketDetails details0 = new AfPacketDetails(); + details0.swIfIndex = IF_INDEX; + details0.hostIfName = IF_NAME.getBytes(); + reply.afPacketDetails = Collections.singletonList(details0); + return reply; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/EthernetCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/EthernetCustomizerTest.java new file mode 100644 index 000000000..33246b63f --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/EthernetCustomizerTest.java @@ -0,0 +1,98 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.test.util.InterfaceDumpHelper; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.EthernetBaseAttributes; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.Ethernet; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.EthernetBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class EthernetCustomizerTest extends ReaderCustomizerTest<Ethernet, EthernetBuilder> implements + InterfaceDumpHelper { + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "local0"; + private static final int IF_INDEX = 1; + private static final InstanceIdentifier<Ethernet> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(Ethernet.class); + private NamingContext interfaceContext; + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public EthernetCustomizerTest() { + super(Ethernet.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + protected void setUp() throws Exception { + interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<Ethernet, EthernetBuilder> initCustomizer() { + return new EthernetCustomizer(dumpCacheManager); + } + + private void testRead(final int linkDuplex, final EthernetBaseAttributes.Duplex duplex) + throws ReadFailedException { + final EthernetBuilder builder = mock(EthernetBuilder.class); + final short mtu = 123; + when(dumpCacheManager.getInterfaceDetail(any(), any(), any())).thenReturn(ifaceDetails(mtu, linkDuplex)); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + verify(builder).setMtu((int) mtu); + verify(builder).setDuplex(duplex); + } + + private SwInterfaceDetails ifaceDetails(final short mtu, final int duplex) { + final SwInterfaceDetails details = new SwInterfaceDetails(); + details.swIfIndex = IF_INDEX; + details.linkMtu = mtu; + details.linkDuplex = (byte) duplex; + return details; + } + + @Test + public void testReadHalfDuplex() throws ReadFailedException { + testRead(1, EthernetBaseAttributes.Duplex.Half); + } + + @Test + public void testReadFullDuplex() throws ReadFailedException { + testRead(2, EthernetBaseAttributes.Duplex.Full); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/GreCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/GreCustomizerTest.java new file mode 100644 index 000000000..48decf256 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/GreCustomizerTest.java @@ -0,0 +1,128 @@ +/* + * 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.hc2vpp.v3po.read; + +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 static org.mockito.Mockito.when; + +import com.google.common.collect.Lists; +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.AddressTranslator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.VppInvocationException; +import io.fd.jvpp.core.dto.GreTunnelDetails; +import io.fd.jvpp.core.dto.GreTunnelDetailsReplyDump; +import io.fd.jvpp.core.dto.GreTunnelDump; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.types.GreTunnel; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.Gre; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.GreBuilder; +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.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class GreCustomizerTest extends ReaderCustomizerTest<Gre, GreBuilder> implements AddressTranslator { + + private static final String IFACE_NAME = "ifc1"; + private static final int IFACE_ID = 0; + private static final String IFC_CTX_NAME = "ifc-test-instance"; + + private NamingContext interfacesContext; + static final InstanceIdentifier<Gre> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(Gre.class); + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public GreCustomizerTest() { + super(Gre.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + public void setUp() throws VppInvocationException, ReadFailedException { + interfacesContext = new NamingContext("gre-tunnel", IFC_CTX_NAME); + defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME); + + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "gre-tunnel4".getBytes(); + + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IFACE_NAME)).thenReturn(v); + + final GreTunnelDetailsReplyDump value = new GreTunnelDetailsReplyDump(); + final GreTunnelDetails greTunnelDetails = new GreTunnelDetails(); + greTunnelDetails.tunnel = new GreTunnel(); + greTunnelDetails.tunnel.isIpv6 = 0; + greTunnelDetails.tunnel.dst = ipv4AddressToAddress(new Ipv4Address("1.2.3.4")); + greTunnelDetails.tunnel.src = ipv4AddressToAddress(new Ipv4Address("1.2.3.5")); + greTunnelDetails.tunnel.outerFibId = 55; + greTunnelDetails.tunnel.swIfIndex = 0; + value.greTunnelDetails = Lists.newArrayList(greTunnelDetails); + + doReturn(future(value)).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().getIpv6AddressNoZone()); + assertNotNull(builder.getSrc().getIpv4AddressNoZone()); + assertEquals("1.2.3.5", builder.getSrc().getIpv4AddressNoZone().getValue()); + + assertNull(builder.getDst().getIpv6AddressNoZone()); + assertNotNull(builder.getDst().getIpv4AddressNoZone()); + assertEquals("1.2.3.4", builder.getDst().getIpv4AddressNoZone().getValue()); + + verify(api).greTunnelDump(any(GreTunnelDump.class)); + } + + @Test + public void testReadCurrentAttributesWrongType() throws Exception { + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "tap-2".getBytes(); + + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IFACE_NAME)).thenReturn(v); + + final GreBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + verifyZeroInteractions(api); + } + + @Override + protected ReaderCustomizer<Gre, GreBuilder> initCustomizer() { + return new GreCustomizer(api, interfacesContext, dumpCacheManager); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceCustomizerTest.java new file mode 100644 index 000000000..05d4bc2f9 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceCustomizerTest.java @@ -0,0 +1,196 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.junit.Assert.assertEquals; +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.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.common.test.util.InterfaceDumpHelper; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.DisabledInterfacesManager; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.dto.SwInterfaceDump; +import io.fd.jvpp.core.types.InterfaceIndex; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.InterfacesBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class InterfaceCustomizerTest extends ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> + implements InterfaceDataTranslator, InterfaceDumpHelper { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IFACE0_NAME = "eth0"; + private static final String IFACE1_NAME = "eth1"; + private static final String SUB_IFACE_NAME = "eth1.1"; + private static final int IFACE0_ID = 0; + private static final int IFACE1_ID = 1; + private static final int SUB_IFACE_ID = 2; + + private NamingContext interfacesContext; + @Mock + private DisabledInterfacesManager interfaceDisableContext; + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public InterfaceCustomizerTest() { + super(Interface.class, InterfacesBuilder.class); + } + + @Override + public void setUp() { + interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IFACE0_NAME, IFACE0_ID, IFC_CTX_NAME); + defineMapping(mappingContext, IFACE1_NAME, IFACE1_ID, IFC_CTX_NAME); + defineMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() { + return new InterfaceCustomizer(interfacesContext, interfaceDisableContext, dumpCacheManager); + } + + private void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) { + assertEquals(iface.getName(), new String(details.interfaceName)); + assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex); + assertEquals(iface.getPhysAddress().getValue(), vppPhysAddrToYang(details.l2Address)); + } + + @Test + public void testReadCurrentAttributes() throws Exception { + final InstanceIdentifier<Interface> id = InstanceIdentifier.create(Interfaces.class) + .child(Interface.class, new InterfaceKey(IFACE0_NAME)); + final InterfaceBuilder builder = getCustomizer().getBuilder(id); + + final SwInterfaceDetails iface = new SwInterfaceDetails(); + iface.interfaceName = IFACE0_NAME.getBytes(); + iface.swIfIndex = 0; + iface.linkSpeed = 1; + iface.l2AddressLength = 6; + iface.l2Address = new byte[iface.l2AddressLength]; + when(dumpCacheManager.getInterfaceDetail(id, ctx, IFACE0_NAME)).thenReturn(iface); + + getCustomizer().readCurrentAttributes(id, builder, ctx); + + final SwInterfaceDump request = new SwInterfaceDump(); + request.swIfIndex = new InterfaceIndex(); + request.swIfIndex.interfaceindex =~0; + request.nameFilter = IFACE0_NAME.getBytes(); + request.nameFilterValid = 1; + + assertIfacesAreEqual(builder.build(), iface); + verify(dumpCacheManager, times(1)).getInterfaceDetail(id, ctx, IFACE0_NAME); + } + + @Test + public void testReadSubInterface() throws Exception { + final InstanceIdentifier<Interface> id = InstanceIdentifier.create(Interfaces.class) + .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME)); + final InterfaceBuilder builder = mock(InterfaceBuilder.class); + + final SwInterfaceDetails iface = new SwInterfaceDetails(); + iface.interfaceName = SUB_IFACE_NAME.getBytes(); + iface.swIfIndex = 2; + iface.supSwIfIndex = 1; + iface.subId = 1; + when(dumpCacheManager.getInterfaceDetail(id, ctx, SUB_IFACE_NAME)).thenReturn(iface); + + getCustomizer().readCurrentAttributes(id, builder, ctx); + + final SwInterfaceDump request = new SwInterfaceDump(); + request.swIfIndex = new InterfaceIndex(); + request.swIfIndex.interfaceindex =~0; + request.nameFilter = SUB_IFACE_NAME.getBytes(); + request.nameFilterValid = 1; + + verifyZeroInteractions(builder); + verify(dumpCacheManager, times(1)).getInterfaceDetail(id, ctx, SUB_IFACE_NAME); + } + + @Test + public void testGetAllIds() throws Exception { + final InstanceIdentifier<Interface> id = InstanceIdentifier.create(Interfaces.class) + .child(Interface.class); + + final SwInterfaceDetails swIf0 = new SwInterfaceDetails(); + swIf0.swIfIndex = 0; + swIf0.supSwIfIndex = 0; + swIf0.interfaceName = IFACE0_NAME.getBytes(); + final SwInterfaceDetails swIf1 = new SwInterfaceDetails(); + swIf1.swIfIndex = 1; + swIf1.supSwIfIndex = 1; + swIf1.interfaceName = IFACE1_NAME.getBytes(); + final SwInterfaceDetails swSubIf1 = new SwInterfaceDetails(); + swSubIf1.swIfIndex = 2; + swSubIf1.subId = 1; + swSubIf1.supSwIfIndex = 1; + swSubIf1.interfaceName = SUB_IFACE_NAME.getBytes(); + when(dumpCacheManager.getInterfaces(id, ctx)).thenReturn(Stream.of(swIf0, swIf1, swSubIf1)); + + final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey( + IFACE1_NAME)); + final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx); + + final SwInterfaceDump request = new SwInterfaceDump(); + request.swIfIndex = new InterfaceIndex(); + request.swIfIndex.interfaceindex = ~0; + request.nameFilter = "".getBytes(); + request.nameFilterValid = 0; + + // sub-interface should not be on the list + assertEquals(expectedIds, actualIds); + verify(dumpCacheManager, times(1)).getInterfaces(id, ctx); + } + + @Test + public void testGetAllIdsWithDisabled() throws Exception { + final InstanceIdentifier<Interface> id = InstanceIdentifier.create(Interfaces.class) + .child(Interface.class); + + doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext); + + final SwInterfaceDetails swIf0 = new SwInterfaceDetails(); + swIf0.swIfIndex = 0; + swIf0.interfaceName = IFACE0_NAME.getBytes(); + final SwInterfaceDetails swIf1 = new SwInterfaceDetails(); + swIf1.swIfIndex = 1; + swIf1.interfaceName = IFACE1_NAME.getBytes(); + when(dumpCacheManager.getInterfaces(id, ctx)).thenReturn(Stream.of(swIf0, swIf1)); + + final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME)); + final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx); + + // disabled interface should not be on the list + assertEquals(expectedIds, actualIds); + verify(dumpCacheManager, times(1)).getInterfaces(id, ctx); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceDataTranslatorTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceDataTranslatorTest.java new file mode 100644 index 000000000..6022f733c --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceDataTranslatorTest.java @@ -0,0 +1,104 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import java.nio.charset.StandardCharsets; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.AfPacket; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.TapV2; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VhostUser; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VxlanGpeTunnel; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VxlanTunnel; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev180703.EthernetCsmacd; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class InterfaceDataTranslatorTest implements InterfaceDataTranslator { + + @Test + public void testVppPhysAddrToYang() throws Exception { + assertEquals("01:02:03:04:05:06", vppPhysAddrToYang(new byte[] {1, 2, 3, 4, 5, 6})); + // Extended (64-bit) MAC addresses are currently not supported (it might require yang model update), + // so test if extended part is ignored + assertEquals("0a:0b:0c:0d:0e:0f", vppPhysAddrToYang(new byte[] {0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0})); + } + + @Test(expected = NullPointerException.class) + public void testVppPhysAddrToYangFailNullArgument() throws Exception { + vppPhysAddrToYang(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testVppPhysAddrToYangInvalidByteArrayLength() throws Exception { + vppPhysAddrToYang(new byte[] {1, 2, 3, 4, 5}); + } + + @Test + public void testGetInterfaceType() { + assertEquals(TapV2.class, getInterfaceType("tap0")); + assertEquals(VxlanTunnel.class, getInterfaceType("vxlan0")); + assertEquals(VxlanGpeTunnel.class, getInterfaceType("vxlan_gpe0")); + assertEquals(VhostUser.class, getInterfaceType("VirtualEthernet0/0/0")); + assertEquals(AfPacket.class, getInterfaceType("host-veth0")); + assertEquals(EthernetCsmacd.class, getInterfaceType("eth0.0")); + assertEquals(EthernetCsmacd.class, getInterfaceType("local0")); + } + + @Test + public void testIsInterfaceOfType() { + assertTrue(isInterfaceOfType(TapV2.class, interfaceDetails("tap0"))); + assertTrue(isInterfaceOfType(VxlanTunnel.class, interfaceDetails("vxlan0"))); + assertTrue(isInterfaceOfType(VxlanGpeTunnel.class, interfaceDetails("vxlan_gpe0"))); + assertTrue(isInterfaceOfType(VhostUser.class, interfaceDetails("VirtualEthernet0/0/0"))); + assertTrue(isInterfaceOfType(AfPacket.class, interfaceDetails("host-veth0"))); + assertTrue(isInterfaceOfType(EthernetCsmacd.class, interfaceDetails("eth0.0"))); + assertTrue(isInterfaceOfType(EthernetCsmacd.class, interfaceDetails("local0"))); + } + + @Test + public void testIsInterfaceOfTypeMissingIfc() throws ReadFailedException { + final InterfaceCacheDumpManager dumpManager = mock(InterfaceCacheDumpManager.class); + final ReadContext ctx = mock(ReadContext.class); + final String ifcName = "tapThatDoesNotExists"; + final InstanceIdentifier<Interface> id = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifcName)); + + when(dumpManager.getInterfaceDetail(id, ctx, ifcName)).thenReturn(null); + + assertFalse(isInterfaceOfType(dumpManager, id, ctx, TapV2.class)); + } + + private SwInterfaceDetails interfaceDetails(final String interfaceName) { + final SwInterfaceDetails details = new SwInterfaceDetails(); + details.interfaceName = interfaceName.getBytes(StandardCharsets.UTF_8); + return details; + } + + +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceRoutingCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceRoutingCustomizerTest.java new file mode 100644 index 000000000..290836bf3 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceRoutingCustomizerTest.java @@ -0,0 +1,94 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceGetTableReply; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.Routing; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.RoutingBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.fib.table.management.rev180521.VniReference; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class InterfaceRoutingCustomizerTest extends ReaderCustomizerTest<Routing, RoutingBuilder> { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "local0"; + private static final int IF_ID = 1; + private static final Long IP4_VRF_ID = 1L; + private static final Long IP6_VRF_ID = 2L; + + private NamingContext interfacesContext; + + public InterfaceRoutingCustomizerTest() { + super(Routing.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + public void setUp() { + interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_ID, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<Routing, RoutingBuilder> initCustomizer() { + return new InterfaceRoutingCustomizer(api, interfacesContext); + } + + private InstanceIdentifier<Routing> getRoutingId(final String name) { + return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)) + .augmentation(VppInterfaceAugmentation.class).child(Routing.class); + } + + @Test + public void testRead() throws Exception { + final RoutingBuilder builder = mock(RoutingBuilder.class); + when(api.swInterfaceGetTable(any())).thenReturn(future(tableReply(IP4_VRF_ID))).thenReturn(future(tableReply(IP6_VRF_ID))); + getCustomizer().readCurrentAttributes(getRoutingId(IF_NAME), builder, ctx); + + verify(builder).setIpv4VrfId(new VniReference(IP4_VRF_ID)); + verify(builder).setIpv6VrfId(new VniReference(IP6_VRF_ID)); + } + + @Test + public void testReadRoutingNotDefined() throws Exception { + final RoutingBuilder builder = mock(RoutingBuilder.class); + final Long vrfId = 0L; + when(api.swInterfaceGetTable(any())).thenReturn(future(tableReply(vrfId))); + getCustomizer().readCurrentAttributes(getRoutingId(IF_NAME), builder, ctx); + verifyZeroInteractions(builder); + } + + private SwInterfaceGetTableReply tableReply(final Long vrfId) { + final SwInterfaceGetTableReply reply = new SwInterfaceGetTableReply(); + reply.vrfId = vrfId.intValue(); + return reply; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceStatisticsCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceStatisticsCustomizerTest.java new file mode 100644 index 000000000..fc4e99d9c --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/InterfaceStatisticsCustomizerTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2019 PANTHEON.tech. + * + * 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.hc2vpp.v3po.read; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceStatisticsManager; +import io.fd.hc2vpp.v3po.read.cache.InterfaceStatisticsManagerImpl; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.stats.dto.InterfaceStatistics; +import io.fd.jvpp.stats.dto.InterfaceStatisticsDetails; +import io.fd.jvpp.stats.dto.InterfaceStatisticsDetailsReplyDump; +import io.fd.jvpp.stats.future.FutureJVppStatsFacade; +import java.util.Optional; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces._interface.Statistics; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces._interface.StatisticsBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class InterfaceStatisticsCustomizerTest extends ReaderCustomizerTest<Statistics, StatisticsBuilder> { + + private static final String IFC_CTX_NAME = "ifc-test-stats-instance"; + private static final String IF_NAME = "testIfc"; + private static final int SW_IF_INDEX = 1; + private static final int OUT_ERRORS = 2; + private static final int OUT_MULTI = 3; + private static final int OUT_UNI = 4; + private static final int OUT_BROAD = 5; + private static final int OUT_BYTES = 6; + private static final int IN_ERRORS = 22; + private static final int IN_MULTI = 33; + private static final int IN_UNI = 44; + private static final int IN_BROAD = 55; + private static final int IN_BYTES = 66; + private NamingContext interfaceContext; + private static final InstanceIdentifier<Statistics> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .child(Statistics.class); + @Mock + private FutureJVppStatsFacade jvppStats; + + private InterfaceStatisticsManager statisticsManager; + + public InterfaceStatisticsCustomizerTest() { + super(Statistics.class, InterfaceBuilder.class); + } + + @Override + public void setUp() { + interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + interfaceContext.addName(1, IF_NAME, ctx.getMappingContext()); + } + + @Override + protected ReaderCustomizer<Statistics, StatisticsBuilder> initCustomizer() { + statisticsManager = new InterfaceStatisticsManagerImpl(); + return new InterfaceStatisticsCustomizer(interfaceContext, jvppStats, statisticsManager); + } + + @Test + public void testReadStatistics() throws Exception { + statisticsManager.enableStatistics(); + when(ctx.getMappingContext().read(any())) + .thenReturn(Optional.of(new MappingBuilder().setName(IF_NAME).setIndex(SW_IF_INDEX).build())); + when(jvppStats.interfaceStatisticsDump(any())).thenReturn(future(getStatistics())); + StatisticsBuilder statBuilder = new StatisticsBuilder(); + getCustomizer().readCurrentAttributes(IID, statBuilder, ctx); + Statistics stat = statBuilder.build(); + int[] expected = new int[]{SW_IF_INDEX, OUT_ERRORS, OUT_MULTI, OUT_UNI, OUT_BROAD, + OUT_BYTES, IN_ERRORS, IN_MULTI, IN_UNI, IN_BROAD, IN_BYTES}; + int[] actual = new int[]{interfaceContext.getIndex(IF_NAME, ctx.getMappingContext()), + stat.getOutErrors().getValue().intValue(), stat.getOutMulticastPkts().getValue().intValue(), + stat.getOutUnicastPkts().getValue().intValue(), stat.getOutBroadcastPkts().getValue().intValue(), + stat.getOutOctets().getValue().intValue(), stat.getInErrors().getValue().intValue(), + stat.getInMulticastPkts().getValue().intValue(), stat.getInUnicastPkts().getValue().intValue(), + stat.getInBroadcastPkts().getValue().intValue(), stat.getInOctets().getValue().intValue()}; + + Assert.assertArrayEquals(expected, actual); + } + + @Test(expected = ReadFailedException.class) + public void testReadStatisticsFailed() throws Exception { + statisticsManager.enableStatistics(); + when(ctx.getMappingContext().read(any())) + .thenReturn(Optional.of(new MappingBuilder().setName(IF_NAME).setIndex(SW_IF_INDEX).build())); + when(jvppStats.interfaceStatisticsDump(any())).thenReturn(future(null)); + StatisticsBuilder statBuilder = new StatisticsBuilder(); + getCustomizer().readCurrentAttributes(IID, statBuilder, ctx); + } + + private InterfaceStatisticsDetailsReplyDump getStatistics() { + InterfaceStatisticsDetailsReplyDump dumpReply = new InterfaceStatisticsDetailsReplyDump(); + dumpReply.interfaceStatisticsDetails = new InterfaceStatisticsDetails(1, 1); + dumpReply.interfaceStatisticsDetails.interfaceStatistics[0] = + new InterfaceStatistics(SW_IF_INDEX, OUT_ERRORS, OUT_MULTI, OUT_UNI, OUT_BROAD, + OUT_BYTES, IN_ERRORS, IN_MULTI, IN_UNI, IN_BROAD, IN_BYTES); + return dumpReply; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/L2CustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/L2CustomizerTest.java new file mode 100644 index 000000000..7e1de173f --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/L2CustomizerTest.java @@ -0,0 +1,159 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.BridgeDomainDetails; +import io.fd.jvpp.core.dto.BridgeDomainDetailsReplyDump; +import io.fd.jvpp.core.dto.BridgeDomainDump; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.types.BridgeDomainSwIf; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.L2; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.L2Builder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.l2.config.attributes.Interconnection; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.l2.config.attributes.interconnection.BridgeBasedBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class L2CustomizerTest extends ReaderCustomizerTest<L2, L2Builder> { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String BD_CTX_NAME = "bd-test-instance"; + private NamingContext interfaceContext; + private NamingContext bridgeDomainContext; + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public L2CustomizerTest() { + super(L2.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + public void setUp() { + interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + bridgeDomainContext = new NamingContext("generatedBDName", BD_CTX_NAME); + } + + @Override + protected ReaderCustomizer<L2, L2Builder> initCustomizer() { + return new L2Customizer(api, interfaceContext, bridgeDomainContext, dumpCacheManager); + } + + private InstanceIdentifier<L2> getL2Id(final String name) { + return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)) + .augmentation(VppInterfaceAugmentation.class).child(L2.class); + } + + private void whenBridgeDomainDumpThenReturn(final List<BridgeDomainDetails> bridgeDomainDetails) { + final BridgeDomainDetailsReplyDump reply = new BridgeDomainDetailsReplyDump(); + reply.bridgeDomainDetails = bridgeDomainDetails; + when(api.bridgeDomainDump(any(BridgeDomainDump.class))).thenReturn(future(reply)); + } + + + private BridgeDomainSwIf generateBdSwIfDetails(final int ifId) { + final BridgeDomainSwIf bdSwIfDetails = new BridgeDomainSwIf(); + bdSwIfDetails.swIfIndex = ifId; + bdSwIfDetails.shg = 1; + return bdSwIfDetails; + } + + private Interconnection generateInterconnection(final String bdName, final Boolean bvi) { + final BridgeBasedBuilder bbBuilder = new BridgeBasedBuilder(); + bbBuilder.setBridgeDomain(bdName); + bbBuilder.setSplitHorizonGroup((short) 1); + if (bvi != null) { + bbBuilder.setBridgedVirtualInterface(bvi); + } else { + bbBuilder.setBridgedVirtualInterface(false); // false is default + } + return bbBuilder.build(); + } + + @Test + public void testReadBvi() throws Exception { + final int ifId = 1; + final int bdId = 1; + final String bdName = "bd001"; + final String ifName = "eth0.sub0"; + defineMapping(mappingContext, ifName, ifId, IFC_CTX_NAME); + defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME); + + final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails(); + ifaceDetails.subId = ifId; + + // BVIinterfaceContext + whenBridgeDomainDumpThenReturn(Collections.singletonList(generateBdDetails(ifId, ifId, bdId))); + + L2Builder builder = mock(L2Builder.class); + getCustomizer().readCurrentAttributes(getL2Id(ifName), builder, ctx); + + verify(builder).setInterconnection(generateInterconnection(bdName, true)); + } + + // split to separate test to avoid using cached value from previous run(cannot mock cache) + @Test + public void testReadNoBvi() throws Exception { + final Map<Integer, SwInterfaceDetails> cachedInterfaceDump = new HashMap<>(); + final int ifId = 1; + final int bdId = 1; + final String bdName = "bd001"; + final String ifName = "eth0.sub0"; + defineMapping(mappingContext, ifName, ifId, IFC_CTX_NAME); + defineMapping(mappingContext, bdName, bdId, BD_CTX_NAME); + + final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails(); + ifaceDetails.subId = ifId; + cachedInterfaceDump.put(ifId, ifaceDetails); + + // Not BVI + whenBridgeDomainDumpThenReturn(Collections + .singletonList(generateBdDetails(ifId, 99 /* Different ifc is marked as BVI in bd details */, bdId))); + + L2Builder builder = mock(L2Builder.class); + getCustomizer().readCurrentAttributes(getL2Id(ifName), builder, ctx); + + verify(builder).setInterconnection(generateInterconnection(bdName, null)); + } + + private BridgeDomainDetails generateBdDetails(final int ifId, final int bviSwIfIndex, int bdId) { + final BridgeDomainDetails bridgeDomainDetails = new BridgeDomainDetails(); + bridgeDomainDetails.bviSwIfIndex = bviSwIfIndex; + bridgeDomainDetails.bdId = bdId; + bridgeDomainDetails.swIfDetails = new BridgeDomainSwIf[]{generateBdSwIfDetails(ifId)}; + return bridgeDomainDetails; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/RewriteCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/RewriteCustomizerTest.java new file mode 100644 index 000000000..e27ef691b --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/RewriteCustomizerTest.java @@ -0,0 +1,118 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.common.translate.util.TagRewriteOperation; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import java.util.List; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.SubinterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319._802dot1q; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.rewrite.attributes.Rewrite; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.rewrite.attributes.RewriteBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.l2.config.attributes.L2Builder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.tag.rewrite.PushTags; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class RewriteCustomizerTest extends ReaderCustomizerTest<Rewrite, RewriteBuilder> { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "local0"; + private static final String VLAN_IF_NAME = "local0.1"; + private static final int VLAN_IF_INDEX = 11; + private static final int VLAN_ID = 1; + + private NamingContext interfacesContext; + + @Captor + private ArgumentCaptor<List<PushTags>> captor; + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public RewriteCustomizerTest() { + super(Rewrite.class, L2Builder.class); + } + + @Override + public void setUp() { + interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<Rewrite, RewriteBuilder> initCustomizer() { + return new RewriteCustomizer(dumpCacheManager); + } + + private InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) { + final Class<ChildOf<? super SubInterface>> child = (Class) Rewrite.class; + final InstanceIdentifier id = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)) + .augmentation( + SubinterfaceAugmentation.class).child(SubInterfaces.class) + .child(SubInterface.class, new SubInterfaceKey(index)) + .child(child); + return id; + } + + @Test + public void testRead() throws ReadFailedException { + final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails(); + ifaceDetails.subId = VLAN_ID; + ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes(); + ifaceDetails.vtrOp = TagRewriteOperation.translate_2_to_2.ordinal(); + ifaceDetails.subNumberOfTags = 2; + ifaceDetails.vtrTag1 = 123; + ifaceDetails.vtrTag2 = 321; + ifaceDetails.vtrPushDot1Q = 1; + ifaceDetails.swIfIndex = VLAN_IF_INDEX; + ifaceDetails.supSwIfIndex = 2; + + final RewriteBuilder builder = mock(RewriteBuilder.class); + final InstanceIdentifier<Rewrite> vlanTagRewriteId = getVlanTagRewriteId(IF_NAME, VLAN_ID); + when(dumpCacheManager.getInterfaceDetail(vlanTagRewriteId, ctx, VLAN_IF_NAME)).thenReturn(ifaceDetails); + getCustomizer().readCurrentAttributes(vlanTagRewriteId, builder, ctx); + + verify(builder).setVlanType(_802dot1q.class); + verify(builder).setPopTags((short) 2); + + verify(builder).setPushTags(captor.capture()); + final List<PushTags> tags = captor.getValue(); + assertEquals(ifaceDetails.subNumberOfTags, tags.size()); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceCustomizerTest.java new file mode 100644 index 000000000..2ce6cd712 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceCustomizerTest.java @@ -0,0 +1,138 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.common.test.util.InterfaceDumpHelper; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import java.util.List; +import java.util.stream.Stream; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.SubinterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.SubInterfacesBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.match.attributes.match.type.VlanTagged; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.Match; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.base.attributes.Tags; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class SubInterfaceCustomizerTest + extends ListReaderCustomizerTest<SubInterface, SubInterfaceKey, SubInterfaceBuilder> implements + InterfaceDumpHelper { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String SUPER_IF_NAME = "local0"; + private static final int SUPER_IF_INDEX = 1; + private static final String VLAN_IF_NAME = "local0.1"; + private static final int VLAN_IF_ID = 1; + private static final int VLAN_IF_INDEX = 11; + + private NamingContext interfacesContext; + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public SubInterfaceCustomizerTest() { + super(SubInterface.class, SubInterfacesBuilder.class); + } + + @Override + public void setUp() { + interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME); + defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<SubInterface, SubInterfaceBuilder> initCustomizer() { + return new SubInterfaceCustomizer(api, interfacesContext, dumpCacheManager); + } + + private InstanceIdentifier<SubInterface> getSubInterfaceId(final String name, final long id) { + return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)) + .augmentation( + SubinterfaceAugmentation.class).child( + SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(id)); + } + + @Test + public void testRead() throws ReadFailedException { + + final SwInterfaceDetails ifaceDetails = new SwInterfaceDetails(); + ifaceDetails.subId = VLAN_IF_ID; + ifaceDetails.interfaceName = VLAN_IF_NAME.getBytes(); + ifaceDetails.swIfIndex = 2; + ifaceDetails.supSwIfIndex = SUPER_IF_INDEX; + defineMapping(mappingContext, SUPER_IF_NAME, SUPER_IF_INDEX, IFC_CTX_NAME); + defineMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_CTX_NAME); + ifaceDetails.subNumberOfTags = 2; + ifaceDetails.subOuterVlanIdAny = 1; + ifaceDetails.subInnerVlanIdAny = 1; + ifaceDetails.subExactMatch = 1; + + final SubInterfaceBuilder builder = mock(SubInterfaceBuilder.class); + final InstanceIdentifier<SubInterface> subInterfaceId = getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID); + + when(dumpCacheManager.getInterfaceDetail(subInterfaceId, ctx, VLAN_IF_NAME)).thenReturn(ifaceDetails); + getCustomizer().readCurrentAttributes(subInterfaceId, builder, ctx); + + verify(builder).setIdentifier((long) VLAN_IF_ID); + + ArgumentCaptor<Tags> tagCaptor = ArgumentCaptor.forClass(Tags.class); + verify(builder).setTags(tagCaptor.capture()); + assertEquals(ifaceDetails.subNumberOfTags, tagCaptor.getValue().getTag().size()); + + ArgumentCaptor<Match> matchCaptor = ArgumentCaptor.forClass(Match.class); + verify(builder).setMatch(matchCaptor.capture()); + final VlanTagged matchType = (VlanTagged) matchCaptor.getValue().getMatchType(); + assertTrue(matchType.getVlanTagged().isMatchExactTags()); + } + + @Test + public void testGetAllIds() throws Exception { + final SwInterfaceDetails iface = new SwInterfaceDetails(); + iface.interfaceName = VLAN_IF_NAME.getBytes(); + iface.swIfIndex = VLAN_IF_INDEX; + iface.subId = VLAN_IF_ID; + iface.supSwIfIndex = SUPER_IF_INDEX; + + final InstanceIdentifier<SubInterface> subInterfaceId = getSubInterfaceId(SUPER_IF_NAME, VLAN_IF_ID); + when(dumpCacheManager.getInterfaces(subInterfaceId, ctx)).thenReturn(Stream.of(iface)); + final List<SubInterfaceKey> allIds = + getCustomizer().getAllIds(subInterfaceId, ctx); + + assertEquals(1, allIds.size()); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceL2CustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceL2CustomizerTest.java new file mode 100644 index 000000000..53be88c43 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceL2CustomizerTest.java @@ -0,0 +1,73 @@ +/* + * 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.hc2vpp.v3po.read; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.SubinterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.l2.config.attributes.L2; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.l2.config.attributes.L2Builder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class SubInterfaceL2CustomizerTest extends ReaderCustomizerTest<L2, L2Builder> { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String BD_CTX_NAME = "bd-test-instance"; + private NamingContext interfaceContext; + private NamingContext bridgeDomainContext; + + private static final String IF_NAME = "local0"; + private static final int IF_INDEX = 1; + private static final String SUB_IF_NAME = "local0.1"; + private static final long SUB_IF_ID = 1; + private static final int SUB_IF_INDEX = 11; + private InstanceIdentifier<L2> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(SubinterfaceAugmentation.class) + .child(SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID)) + .child(L2.class); + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public SubInterfaceL2CustomizerTest() { + super(L2.class, SubInterfaceBuilder.class); + } + + @Override + protected void setUp() { + interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + bridgeDomainContext = new NamingContext("generatedBDName", BD_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<L2, L2Builder> initCustomizer() { + return new SubInterfaceL2Customizer(api, interfaceContext, bridgeDomainContext, dumpCacheManager); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceRoutingCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceRoutingCustomizerTest.java new file mode 100644 index 000000000..57daac3c4 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/SubInterfaceRoutingCustomizerTest.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2017 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.hc2vpp.v3po.read; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceGetTable; +import io.fd.jvpp.core.dto.SwInterfaceGetTableReply; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.SubinterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.routing.attributes.Routing; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.routing.attributes.RoutingBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class SubInterfaceRoutingCustomizerTest extends ReaderCustomizerTest<Routing, RoutingBuilder> implements + ByteDataTranslator { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "local0"; + private static final int IF_ID = 1; + private static final String SUBIF_NAME = "local0.4"; + private static final int SUBIF_ID = 4; + private static final InstanceIdentifier<Routing> VALID_ID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(SubinterfaceAugmentation.class) + .child(SubInterfaces.class) + .child(SubInterface.class, new SubInterfaceKey((long) SUBIF_ID)) + .child(Routing.class); + private static final int IPV4_VRF = 4; + private static final int IPV6_VRF = 6; + private static final int NO_VRF = 0; + + private NamingContext interfacesContext; + + public SubInterfaceRoutingCustomizerTest() { + super(Routing.class, SubInterfaceBuilder.class); + } + + @Override + protected void setUp() throws Exception { + interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_ID, IFC_CTX_NAME); + defineMapping(mappingContext, SUBIF_NAME, SUBIF_ID, IFC_CTX_NAME); + } + + @Override + protected ReaderCustomizer<Routing, RoutingBuilder> initCustomizer() { + return new SubInterfaceRoutingCustomizer(api, interfacesContext); + } + + @Test + public void testReadAttributesAllDefined() throws ReadFailedException { + when(api.swInterfaceGetTable(request(false, SUBIF_ID))).thenReturn(future(reply(IPV4_VRF))); + when(api.swInterfaceGetTable(request(true, SUBIF_ID))).thenReturn(future(reply(IPV6_VRF))); + final RoutingBuilder routingBuilder = new RoutingBuilder(); + getCustomizer().readCurrentAttributes(VALID_ID, routingBuilder, ctx); + assertEquals(IPV4_VRF, routingBuilder.getIpv4VrfId().getValue().intValue()); + assertEquals(IPV6_VRF, routingBuilder.getIpv6VrfId().getValue().intValue()); + } + + @Test + public void testReadAttributesOneDefined() throws ReadFailedException { + when(api.swInterfaceGetTable(request(false, SUBIF_ID))).thenReturn(future(reply(IPV4_VRF))); + when(api.swInterfaceGetTable(request(true, SUBIF_ID))).thenReturn(future(reply(NO_VRF))); + final RoutingBuilder routingBuilder = new RoutingBuilder(); + getCustomizer().readCurrentAttributes(VALID_ID, routingBuilder, ctx); + assertEquals(IPV4_VRF, routingBuilder.getIpv4VrfId().getValue().intValue()); + assertNull(routingBuilder.getIpv6VrfId()); + } + + @Test + public void testReadAttributesNoDefined() throws ReadFailedException { + when(api.swInterfaceGetTable(any())).thenReturn(future(reply(NO_VRF))); + final RoutingBuilder routingBuilder = new RoutingBuilder(); + getCustomizer().readCurrentAttributes(VALID_ID, routingBuilder, ctx); + assertNull(routingBuilder.getIpv4VrfId()); + assertNull(routingBuilder.getIpv6VrfId()); + } + + private SwInterfaceGetTable request(final boolean ipv6, final int index) { + SwInterfaceGetTable request = new SwInterfaceGetTable(); + request.isIpv6 = booleanToByte(ipv6); + request.swIfIndex = index; + return request; + } + + private SwInterfaceGetTableReply reply(final int vrf) { + SwInterfaceGetTableReply reply = new SwInterfaceGetTableReply(); + reply.vrfId = vrf; + return reply; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/TapV2CustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/TapV2CustomizerTest.java new file mode 100644 index 000000000..66d7c3268 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/TapV2CustomizerTest.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2018 Pantheon Technologies 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.hc2vpp.v3po.read; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.test.util.InterfaceDumpHelper; +import io.fd.hc2vpp.common.translate.util.Ipv4Translator; +import io.fd.hc2vpp.common.translate.util.Ipv6Translator; +import io.fd.hc2vpp.common.translate.util.MacTranslator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.dto.SwInterfaceTapV2Details; +import io.fd.jvpp.core.dto.SwInterfaceTapV2DetailsReplyDump; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.TapV2; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.TapV2Builder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class TapV2CustomizerTest extends ReaderCustomizerTest<TapV2, TapV2Builder> implements InterfaceDumpHelper, + Ipv4Translator, Ipv6Translator, MacTranslator { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "tapV21"; + private static final String DEVICE_NAME = "testTapV2Device"; + private static final int IF_INDEX = 1; + private static final String HOST_BRIDGE = "TestBridge"; + private static final String HOST_IPV4_PREFIX = "192.168.255.100"; + private static final byte HOST_IPV4_PREFIX_LEN = 24; + private static final String HOST_IPV6_PREFIX = "a::100"; + private static final byte HOST_IPV6_PREFIX_LEN = -128; + private static final int HOST_IPV6_PREFIX_LEN_EXP = 128; + private static final int RX_TX_RING_SIZE = 512; + private static final String HOST_MAC = "00:ee:ee:ee:ee:ee"; + private static final String HOST_NAMESPACE = "testHostNS"; + + private static final InstanceIdentifier<TapV2> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(TapV2.class); + private NamingContext interfaceContext; + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public TapV2CustomizerTest() { + super(TapV2.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + protected void setUp() throws Exception { + interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IF_NAME)).thenReturn(ifaceDetails()); + } + + private SwInterfaceDetails ifaceDetails() { + final SwInterfaceDetails details = new SwInterfaceDetails(); + details.swIfIndex = IF_INDEX; + details.interfaceName = IF_NAME.getBytes(); + details.tag = new byte[64]; + return details; + } + + @Override + protected ReaderCustomizer<TapV2, TapV2Builder> initCustomizer() { + return new TapV2Customizer(api, interfaceContext, dumpCacheManager); + } + + @Test + public void testRead() throws ReadFailedException { + final TapV2Builder builder = mock(TapV2Builder.class); + when(api.swInterfaceTapV2Dump(any())).thenReturn(future(tapDump())); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + verify(builder).setHostInterfaceName(IF_NAME); + verify(builder).setDeviceName(DEVICE_NAME); + verify(builder).setHostBridge(HOST_BRIDGE); + verify(builder) + .setHostIpv4Address(new Ipv4Prefix(String.format("%s/%d", HOST_IPV4_PREFIX, HOST_IPV4_PREFIX_LEN))); + verify(builder) + .setHostIpv6Address(new Ipv6Prefix(String.format("%s/%d", HOST_IPV6_PREFIX, HOST_IPV6_PREFIX_LEN_EXP))); + verify(builder).setTxRingSize(RX_TX_RING_SIZE); + verify(builder).setRxRingSize(RX_TX_RING_SIZE); + verify(builder).setHostNamespace(HOST_NAMESPACE); + verify(builder).setHostMac(new PhysAddress(HOST_MAC)); + } + + @Test(expected = ReadFailedException.class) + public void testReadFailed() throws ReadFailedException { + when(api.swInterfaceTapV2Dump(any())).thenReturn(failedFuture()); + getCustomizer().readCurrentAttributes(IID, mock(TapV2Builder.class), ctx); + } + + private SwInterfaceTapV2DetailsReplyDump tapDump() { + final SwInterfaceTapV2DetailsReplyDump reply = new SwInterfaceTapV2DetailsReplyDump(); + final SwInterfaceTapV2Details details = new SwInterfaceTapV2Details(); + details.devName = DEVICE_NAME.getBytes(UTF_8); + details.swIfIndex = IF_INDEX; + details.hostBridge = HOST_BRIDGE.getBytes(UTF_8); + details.hostNamespace = HOST_NAMESPACE.getBytes(UTF_8); + details.hostIfName = IF_NAME.getBytes(UTF_8); + details.hostIp4PrefixLen = HOST_IPV4_PREFIX_LEN; + details.hostIp4Addr = ipv4AddressNoZoneToArray(HOST_IPV4_PREFIX); + details.hostIp6Addr = ipv6AddressNoZoneToArray(new Ipv6AddressNoZone(HOST_IPV6_PREFIX)); + details.hostIp6PrefixLen = HOST_IPV6_PREFIX_LEN; + details.hostMacAddr = parseMac(HOST_MAC); + details.txRingSz = details.rxRingSz = RX_TX_RING_SIZE; + reply.swInterfaceTapV2Details.add(details); + return reply; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VhostUserCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VhostUserCustomizerTest.java new file mode 100644 index 000000000..193676b92 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VhostUserCustomizerTest.java @@ -0,0 +1,121 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.test.util.InterfaceDumpHelper; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.dto.SwInterfaceVhostUserDetails; +import io.fd.jvpp.core.dto.SwInterfaceVhostUserDetailsReplyDump; +import java.math.BigInteger; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VhostUserRole; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.VhostUser; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.VhostUserBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class VhostUserCustomizerTest extends ReaderCustomizerTest<VhostUser, VhostUserBuilder> implements + InterfaceDumpHelper { + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "VirtualEthernet1"; + private static final int IF_INDEX = 1; + private static final InstanceIdentifier<VhostUser> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class); + + private NamingContext interfaceContext; + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public VhostUserCustomizerTest() { + super(VhostUser.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + protected void setUp() throws Exception { + interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IF_NAME)).thenReturn(ifaceDetails()); + } + + private SwInterfaceDetails ifaceDetails() { + final SwInterfaceDetails details = new SwInterfaceDetails(); + details.swIfIndex = IF_INDEX; + details.interfaceName = IF_NAME.getBytes(); + details.tag = new byte[64]; + return details; + } + + @Override + protected ReaderCustomizer<VhostUser, VhostUserBuilder> initCustomizer() { + return new VhostUserCustomizer(api, interfaceContext, dumpCacheManager); + } + + @Test + public void testRead() throws ReadFailedException { + final VhostUserBuilder builder = mock(VhostUserBuilder.class); + when(api.swInterfaceVhostUserDump(any())).thenReturn(future(vhostDump())); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + verifyVhostBuilder(builder); + } + + @Test(expected = ReadFailedException.class) + public void testReadFailed() throws ReadFailedException { + when(api.swInterfaceVhostUserDump(any())).thenReturn(failedFuture()); + getCustomizer().readCurrentAttributes(IID, mock(VhostUserBuilder.class), ctx); + } + + private SwInterfaceVhostUserDetailsReplyDump vhostDump() { + final SwInterfaceVhostUserDetailsReplyDump reply = new SwInterfaceVhostUserDetailsReplyDump(); + final SwInterfaceVhostUserDetails details = new SwInterfaceVhostUserDetails(); + details.swIfIndex = IF_INDEX; + details.interfaceName = IF_NAME.getBytes(); + details.isServer = 1; + details.features = 2; + details.numRegions = 3; + details.sockFilename = "socketName".getBytes(); + details.virtioNetHdrSz = 4; + details.sockErrno = 5; + reply.swInterfaceVhostUserDetails.add(details); + return reply; + } + + private void verifyVhostBuilder(final VhostUserBuilder builder) { + verify(builder).setRole(VhostUserRole.Server); + verify(builder).setFeatures(BigInteger.valueOf(2)); + verify(builder).setNumMemoryRegions(3L); + verify(builder).setSocket("socketName"); + verify(builder).setVirtioNetHdrSize(4L); + verify(builder).setConnectError("5"); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VxlanCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VxlanCustomizerTest.java new file mode 100644 index 000000000..482b7d4ac --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VxlanCustomizerTest.java @@ -0,0 +1,155 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import com.google.common.collect.Lists; +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.AddressTranslator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.VppInvocationException; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.dto.VxlanTunnelDetails; +import io.fd.jvpp.core.dto.VxlanTunnelDetailsReplyDump; +import io.fd.jvpp.core.dto.VxlanTunnelDump; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.L2Input; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.Vxlan; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.VxlanBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class VxlanCustomizerTest extends ReaderCustomizerTest<Vxlan, VxlanBuilder> { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "ifc1"; + private static final int IF_INDEX = 0; + + private NamingContext interfacesContext; + static final InstanceIdentifier<Vxlan> IID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(Vxlan.class); + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public VxlanCustomizerTest() { + super(Vxlan.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + public void setUp() throws VppInvocationException, ReadFailedException { + interfacesContext = new NamingContext("vxlan-tunnel", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "vxlan-tunnel4".getBytes(); + + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IF_NAME)).thenReturn(v); + doReturn(future(getVxlanTunnelDetailsReplyDump(55))).when(api).vxlanTunnelDump(any(VxlanTunnelDump.class)); + } + + @Test + public void testRead() throws Exception { + final VxlanBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + + assertEquals(9, builder.getVni().getValue().intValue()); + assertEquals(55, builder.getEncapVrfId().getValue().intValue()); + assertEquals(L2Input.class, builder.getDecapNext()); + + assertNull(builder.getSrc().getIpv6AddressNoZone()); + assertNotNull(builder.getSrc().getIpv4AddressNoZone()); + assertEquals("1.2.3.5", builder.getSrc().getIpv4AddressNoZone().getValue()); + + assertNull(builder.getDst().getIpv6AddressNoZone()); + assertNotNull(builder.getDst().getIpv4AddressNoZone()); + assertEquals("1.2.3.4", builder.getDst().getIpv4AddressNoZone().getValue()); + + verify(api).vxlanTunnelDump(any(VxlanTunnelDump.class)); + } + + @Test + public void testReadVniOverflow() throws Exception { + final long encapVrfId = 4294967295L; + doReturn(future(getVxlanTunnelDetailsReplyDump((int) encapVrfId))).when(api) + .vxlanTunnelDump(any(VxlanTunnelDump.class)); + final VxlanBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + + assertEquals(encapVrfId, builder.getEncapVrfId().getValue().longValue()); + verify(api).vxlanTunnelDump(any(VxlanTunnelDump.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void testReadVppNameNotCached() throws Exception { + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IF_NAME)) + .thenThrow(new IllegalArgumentException("Detail for interface not found")); + + final VxlanBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + } + + @Test + public void testReadWrongType() throws Exception { + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "tap-2".getBytes(); + + when(dumpCacheManager.getInterfaceDetail(IID, ctx, IF_NAME)).thenReturn(v); + + final VxlanBuilder builder = getCustomizer().getBuilder(IID); + getCustomizer().readCurrentAttributes(IID, builder, ctx); + + // Should be ignored + verifyZeroInteractions(api); + } + + @Override + protected ReaderCustomizer<Vxlan, VxlanBuilder> initCustomizer() { + return new VxlanCustomizer(api, interfacesContext, dumpCacheManager); + } + + private static VxlanTunnelDetailsReplyDump getVxlanTunnelDetailsReplyDump(final int encapVrfId) { + final VxlanTunnelDetailsReplyDump replyDump = new VxlanTunnelDetailsReplyDump(); + final VxlanTunnelDetails vxlanTunnelDetails = new VxlanTunnelDetails(); + vxlanTunnelDetails.isIpv6 = 0; + vxlanTunnelDetails.dstAddress = AddressTranslator.INSTANCE.ipv4AddressNoZoneToArray("1.2.3.4"); + vxlanTunnelDetails.srcAddress = AddressTranslator.INSTANCE.ipv4AddressNoZoneToArray("1.2.3.5"); + vxlanTunnelDetails.encapVrfId = encapVrfId; + vxlanTunnelDetails.swIfIndex = 0; + vxlanTunnelDetails.vni = 9; + vxlanTunnelDetails.decapNextIndex = 1; + replyDump.vxlanTunnelDetails = Lists.newArrayList(vxlanTunnelDetails); + return replyDump; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VxlanGpeCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VxlanGpeCustomizerTest.java new file mode 100644 index 000000000..829573877 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/VxlanGpeCustomizerTest.java @@ -0,0 +1,141 @@ +/* + * 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.hc2vpp.v3po.read; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.matches; +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 static org.mockito.Mockito.when; + +import com.google.common.collect.Lists; +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.AddressTranslator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManager; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.VppBaseCallException; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.dto.VxlanGpeTunnelDetails; +import io.fd.jvpp.core.dto.VxlanGpeTunnelDetailsReplyDump; +import io.fd.jvpp.core.dto.VxlanGpeTunnelDump; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.VxlanGpe; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.VxlanGpeBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class VxlanGpeCustomizerTest extends ReaderCustomizerTest<VxlanGpe, VxlanGpeBuilder> + implements AddressTranslator { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "ifc2"; + private static final int IF_INDEX = 0; + + private NamingContext interfacesContext; + private static final InstanceIdentifier<VxlanGpe> VXLAN_GPE_ID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class); + + @Mock + private InterfaceCacheDumpManager dumpCacheManager; + + public VxlanGpeCustomizerTest() { + super(VxlanGpe.class, VppInterfaceAugmentationBuilder.class); + } + + @Override + public void setUp() throws VppBaseCallException, ReadFailedException { + interfacesContext = new NamingContext("vxlan_gpe_inf", IFC_CTX_NAME); + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "vxlan_gpe_inf2".getBytes(); + + when(dumpCacheManager.getInterfaceDetail(any(), any(), matches(IF_NAME))).thenReturn(v); + + final VxlanGpeTunnelDetailsReplyDump value = new VxlanGpeTunnelDetailsReplyDump(); + final VxlanGpeTunnelDetails vxlanGpeTunnelDetails = new VxlanGpeTunnelDetails(); + vxlanGpeTunnelDetails.isIpv6 = 0; + vxlanGpeTunnelDetails.local = ipv4AddressNoZoneToArray("1.2.3.4"); + vxlanGpeTunnelDetails.remote = ipv4AddressNoZoneToArray("1.2.3.5"); + vxlanGpeTunnelDetails.vni = 9; + vxlanGpeTunnelDetails.protocol = 1; + vxlanGpeTunnelDetails.encapVrfId = 55; + vxlanGpeTunnelDetails.decapVrfId = 66; + vxlanGpeTunnelDetails.swIfIndex = 0; + value.vxlanGpeTunnelDetails = Lists.newArrayList(vxlanGpeTunnelDetails); + doReturn(future(value)).when(api).vxlanGpeTunnelDump(any(VxlanGpeTunnelDump.class)); + } + + @Test + public void testReadCurrentAttributes() throws Exception { + final VxlanGpeBuilder builder = getCustomizer().getBuilder(VXLAN_GPE_ID); + getCustomizer().readCurrentAttributes(VXLAN_GPE_ID, builder, ctx); + + assertNull(builder.getLocal().getIpv6AddressNoZone()); + assertNotNull(builder.getLocal().getIpv4AddressNoZone()); + assertEquals("1.2.3.4", builder.getLocal().getIpv4AddressNoZone().getValue()); + + assertNull(builder.getRemote().getIpv6AddressNoZone()); + assertNotNull(builder.getRemote().getIpv4AddressNoZone()); + assertEquals("1.2.3.5", builder.getRemote().getIpv4AddressNoZone().getValue()); + + assertEquals(9, builder.getVni().getValue().intValue()); + assertEquals(1, builder.getNextProtocol().getIntValue()); + assertEquals(55, builder.getEncapVrfId().intValue()); + assertEquals(66, builder.getDecapVrfId().intValue()); + + verify(api).vxlanGpeTunnelDump(any(VxlanGpeTunnelDump.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void testReadCurrentAttributesVppNameNotCached() throws Exception { + when(dumpCacheManager.getInterfaceDetail(VXLAN_GPE_ID, ctx, IF_NAME)) + .thenThrow(new IllegalArgumentException("Detail for interface not found")); + + final VxlanGpeBuilder builder = getCustomizer().getBuilder(VXLAN_GPE_ID); + getCustomizer().readCurrentAttributes(VXLAN_GPE_ID, builder, ctx); + } + + @Test + public void testReadCurrentAttributesWrongType() throws Exception { + final SwInterfaceDetails v = new SwInterfaceDetails(); + v.interfaceName = "tap-3".getBytes(); + + when(dumpCacheManager.getInterfaceDetail(VXLAN_GPE_ID, ctx, IF_NAME)).thenReturn(v); + + final VxlanGpeBuilder builder = getCustomizer().getBuilder(VXLAN_GPE_ID); + getCustomizer().readCurrentAttributes(VXLAN_GPE_ID, builder, ctx); + verifyZeroInteractions(api); + } + + @Override + protected ReaderCustomizer<VxlanGpe, VxlanGpeBuilder> initCustomizer() { + return new VxlanGpeCustomizer(api, interfacesContext, dumpCacheManager); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/cache/InterfaceCacheDumpManagerImplTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/cache/InterfaceCacheDumpManagerImplTest.java new file mode 100644 index 000000000..04b1d999e --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/cache/InterfaceCacheDumpManagerImplTest.java @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2017 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.hc2vpp.v3po.read.cache; + +import static io.fd.hc2vpp.v3po.read.cache.InterfaceCacheDumpManagerImpl.BY_NAME_INDEX_KEY; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import io.fd.hc2vpp.common.test.util.FutureProducer; +import io.fd.hc2vpp.common.test.util.NamingContextHelper; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.MappingContext; +import io.fd.honeycomb.translate.ModificationCache; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.jvpp.core.dto.SwInterfaceDetails; +import io.fd.jvpp.core.dto.SwInterfaceDetailsReplyDump; +import io.fd.jvpp.core.dto.SwInterfaceDump; +import io.fd.jvpp.core.future.FutureJVppCore; +import io.fd.jvpp.core.types.InterfaceIndex; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class InterfaceCacheDumpManagerImplTest implements NamingContextHelper, FutureProducer { + + private static final String IFACE_0 = "iface-0"; + private static final String IFACE_1 = "iface-1"; + private static final String IFACE_2 = "iface-2"; + private static final String IFACE_3 = "iface-3"; + + @Mock + private FutureJVppCore jvpp; + @Mock + private ReadContext ctx; + @Mock + private MappingContext mappingContext; + + private InstanceIdentifier<Interface> identifier; + private InstanceIdentifier<Interface> identifierThree; + private NamingContext namingContext; + private ModificationCache cache; + private InterfaceCacheDumpManagerImpl manager; + + @Before + public void setUp() throws Exception { + initMocks(this); + namingContext = new NamingContext("interface-", "interface-context"); + cache = new ModificationCache(); + manager = new InterfaceCacheDumpManagerImpl(jvpp, namingContext); + when(ctx.getModificationCache()).thenReturn(cache); + when(ctx.getMappingContext()).thenReturn(mappingContext); + identifier = InstanceIdentifier.create(Interfaces.class) + .child(Interface.class, new InterfaceKey(IFACE_0)); + + identifierThree = InstanceIdentifier.create(Interfaces.class) + .child(Interface.class, new InterfaceKey(IFACE_3)); + + when(jvpp.swInterfaceDump(fullRequest())).thenReturn(future(fullReply())); + + // this one is not in full dump + when(jvpp.swInterfaceDump(specificRequest(IFACE_3))).thenReturn(future(specificReplyThree())); + defineMapping(mappingContext, IFACE_0, 0, "interface-context"); + defineMapping(mappingContext, IFACE_1, 1, "interface-context"); + defineMapping(mappingContext, IFACE_2, 2, "interface-context"); + defineMapping(mappingContext, IFACE_3, 3, "interface-context"); + } + + @Test + public void getInterfaces() throws Exception { + assertFalse(cache.containsKey(BY_NAME_INDEX_KEY)); + final List<SwInterfaceDetails> interfaces = manager.getInterfaces(identifier, ctx).collect(Collectors.toList()); + assertEquals(3, interfaces.size()); + assertTrue(interfaces.contains(detailZero())); + assertTrue(interfaces.contains(detailOne())); + assertTrue(interfaces.contains(detailTwo())); + + // first request must call jvpp + verify(jvpp, times(1)).swInterfaceDump(fullRequest()); + assertTrue(cache.containsKey(BY_NAME_INDEX_KEY)); + + // then cached value should be returned + final List<SwInterfaceDetails> cachedInterfaces = + manager.getInterfaces(identifier, ctx).collect(Collectors.toList()); + assertEquals(3, cachedInterfaces.size()); + assertTrue(cachedInterfaces.contains(detailZero())); + assertTrue(cachedInterfaces.contains(detailOne())); + assertTrue(cachedInterfaces.contains(detailTwo())); + + //verify that dump wasn't invoked again + verifyNoMoreInteractions(jvpp); + } + + @Test + public void getInterfaceDetailFromCache() throws Exception { + final HashMap<Object, Object> cachedMap = new HashMap<>(); + final SwInterfaceDetails detailZero = detailZero(); + cachedMap.put(IFACE_0, detailZero); + cache.put(BY_NAME_INDEX_KEY, cachedMap); + when(jvpp.swInterfaceDump(specificRequest(IFACE_0))).thenReturn(future(specificReplyZero())); + final SwInterfaceDetails interfaceDetail = manager.getInterfaceDetail(identifier, ctx, IFACE_0); + assertEquals(detailZero, interfaceDetail); + + // must not call jvpp, just get it from cache + verifyZeroInteractions(jvpp); + } + + @Test + public void getInterfaceDetailNotInFullDump() throws Exception { + assertFalse(cache.containsKey(BY_NAME_INDEX_KEY)); + final SwInterfaceDetails specificDetail = manager.getInterfaceDetail(identifierThree, ctx, IFACE_3); + assertEquals(detailThree(), specificDetail); + + verify(jvpp, times(1)).swInterfaceDump(specificRequest(IFACE_3)); + } + + private SwInterfaceDetailsReplyDump fullReply() { + final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump(); + reply.swInterfaceDetails = Arrays.asList(detailZero(), detailOne(), detailTwo()); + return reply; + } + + private static SwInterfaceDetails detailTwo() { + SwInterfaceDetails detail3 = new SwInterfaceDetails(); + detail3.swIfIndex = 2; + detail3.interfaceName = IFACE_2.getBytes(); + return detail3; + } + + private static SwInterfaceDetails detailOne() { + SwInterfaceDetails detail2 = new SwInterfaceDetails(); + detail2.swIfIndex = 1; + detail2.interfaceName = IFACE_1.getBytes(); + return detail2; + } + + private static SwInterfaceDetails detailThree() { + SwInterfaceDetails detail2 = new SwInterfaceDetails(); + detail2.swIfIndex = 3; + detail2.interfaceName = IFACE_3.getBytes(); + return detail2; + } + + private static SwInterfaceDetails detailZero() { + SwInterfaceDetails detail1 = new SwInterfaceDetails(); + detail1.swIfIndex = 0; + detail1.interfaceName = IFACE_0.getBytes(); + return detail1; + } + + private SwInterfaceDetailsReplyDump specificReplyThree() { + final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump(); + reply.swInterfaceDetails = Arrays.asList(detailThree()); + return reply; + } + + private SwInterfaceDetailsReplyDump specificReplyZero() { + final SwInterfaceDetailsReplyDump reply = new SwInterfaceDetailsReplyDump(); + reply.swInterfaceDetails = Arrays.asList(detailZero()); + return reply; + } + + private static SwInterfaceDump specificRequest(final String ifaceName) { + final SwInterfaceDump specificRequest = new SwInterfaceDump(); + specificRequest.swIfIndex = new InterfaceIndex(); + specificRequest.swIfIndex.interfaceindex =~0; + specificRequest.nameFilterValid = 1; + specificRequest.nameFilter = ifaceName.getBytes(); + return specificRequest; + } + + private static SwInterfaceDump fullRequest() { + final SwInterfaceDump fullRequest = new SwInterfaceDump(); + fullRequest.swIfIndex = new InterfaceIndex(); + fullRequest.swIfIndex.interfaceindex = ~0; + fullRequest.nameFilterValid = 0; + fullRequest.nameFilter = "".getBytes(); + return fullRequest; + } +}
\ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/pbb/PbbRewriteCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/pbb/PbbRewriteCustomizerTest.java new file mode 100644 index 000000000..7a5213585 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/pbb/PbbRewriteCustomizerTest.java @@ -0,0 +1,35 @@ +/* + * 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.hc2vpp.v3po.read.pbb; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.pbb.rev161214.PbbRewriteInterfaceAugmentationBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.pbb.rev161214.interfaces._interface.PbbRewrite; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.pbb.rev161214.interfaces._interface.PbbRewriteBuilder; + +public class PbbRewriteCustomizerTest extends ReaderCustomizerTest<PbbRewrite, PbbRewriteBuilder> { + + public PbbRewriteCustomizerTest() { + super(PbbRewrite.class, PbbRewriteInterfaceAugmentationBuilder.class); + } + + @Override + protected ReaderCustomizer<PbbRewrite, PbbRewriteBuilder> initCustomizer() { + return new PbbRewriteCustomizer(api); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/span/MirroredInterfacesCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/span/MirroredInterfacesCustomizerTest.java new file mode 100644 index 000000000..eec044bc2 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/read/span/MirroredInterfacesCustomizerTest.java @@ -0,0 +1,128 @@ +/* + * 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.hc2vpp.v3po.read.span; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasSize; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.jvpp.core.dto.SwInterfaceSpanDetails; +import io.fd.jvpp.core.dto.SwInterfaceSpanDetailsReplyDump; +import java.util.Arrays; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.SpanState; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.Span; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.SpanBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.MirroredInterfaces; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.MirroredInterfacesBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.mirrored.interfaces.MirroredInterface; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.mirrored.interfaces.MirroredInterfaceBuilder; +import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.mirrored.interfaces.MirroredInterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + + +public class MirroredInterfacesCustomizerTest + extends ReaderCustomizerTest<MirroredInterfaces, MirroredInterfacesBuilder> { + + private static final String IFACE_NAME = "iface"; + + private static final String SRC_IFACE_NAME_1 = "src-one"; + private static final String SRC_IFACE_NAME_2 = "src-two"; + private static final String SRC_IFACE_NAME_3 = "src-three"; + + private static final int IFACE_INDEX = 3; + + private NamingContext interfaceContext; + private InstanceIdentifier<MirroredInterfaces> validId; + private MirroredInterface validData; + + public MirroredInterfacesCustomizerTest() { + super(MirroredInterfaces.class, SpanBuilder.class); + } + + @Override + protected void setUp() throws Exception { + interfaceContext = new NamingContext("iface", "iface-context"); + defineMapping(mappingContext, IFACE_NAME, IFACE_INDEX, "iface-context"); + defineMapping(mappingContext, SRC_IFACE_NAME_1, 1, "iface-context"); + defineMapping(mappingContext, SRC_IFACE_NAME_2, 2, "iface-context"); + defineMapping(mappingContext, SRC_IFACE_NAME_3, 3, "iface-context"); + + validId = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME)) + .augmentation(VppInterfaceAugmentation.class).child(Span.class) + .child(MirroredInterfaces.class); + + SwInterfaceSpanDetailsReplyDump dump = new SwInterfaceSpanDetailsReplyDump(); + SwInterfaceSpanDetails detail1 = new SwInterfaceSpanDetails(); + + detail1.swIfIndexTo = IFACE_INDEX; + detail1.swIfIndexFrom = 1; + detail1.state = 1; + + SwInterfaceSpanDetails detail2 = new SwInterfaceSpanDetails(); + + detail2.swIfIndexTo = IFACE_INDEX; + detail2.swIfIndexFrom = 2; + detail2.state = 3; + + SwInterfaceSpanDetails detail3 = new SwInterfaceSpanDetails(); + + detail3.swIfIndexTo = IFACE_INDEX; + detail3.swIfIndexFrom = 3; + detail3.state = 0; + + dump.swInterfaceSpanDetails = Arrays.asList(detail1, detail2, detail3); + + when(api.swInterfaceSpanDump(any())).thenReturn(future(dump)); + } + + @Test + public void readCurrentAttributes() throws Exception { + MirroredInterfacesBuilder builder = new MirroredInterfacesBuilder(); + getCustomizer().readCurrentAttributes(validId, builder, ctx); + + final MirroredInterfaces data = builder.build(); + // 1,2 should be returned,0 should be filtered out because of disabled state + + assertThat(data.getMirroredInterface(), hasSize(2)); + assertThat(data.getMirroredInterface(), containsInAnyOrder( + mirroredInterface(SRC_IFACE_NAME_1, SpanState.Receive), + mirroredInterface(SRC_IFACE_NAME_2, SpanState.Both))); + } + + private MirroredInterface mirroredInterface(final String ifaceName, final SpanState state) { + return new MirroredInterfaceBuilder() + .setIfaceRef(ifaceName) + .withKey(new MirroredInterfaceKey(ifaceName)) + .setState(state) + .build(); + } + + @Override + protected ReaderCustomizer<MirroredInterfaces, MirroredInterfacesBuilder> initCustomizer() { + return new InterfaceMirroredInterfacesCustomizer(api, interfaceContext); + } +} |