From 05311d66093430664eea9d51c26fb830b5e39714 Mon Sep 17 00:00:00 2001 From: Matej Perina Date: Fri, 29 Jan 2016 16:06:53 +0100 Subject: [PATCH] UT: ofoverlay-arp Change-Id: I7982604cb94ffa25218fb8db20e1eb123c08b5d0 Signed-off-by: Matej Perina --- groupbasedpolicy/pom.xml | 11 + renderers/ofoverlay/pom.xml | 7 + .../ofoverlay/arp/ArpFlowFactoryTest.java | 63 +++++ .../ofoverlay/arp/ArpResolverUtilsTest.java | 67 +++++ .../renderer/ofoverlay/arp/ArpSenderTest.java | 70 +++++ .../renderer/ofoverlay/arp/ArpTaskerTest.java | 267 ++++++++++++++++++ .../renderer/ofoverlay/arp/ArpTest.java | 107 +++++++ .../renderer/ofoverlay/arp/ArpUtilsTest.java | 70 +++++ .../test/OfOverlayDataBrokerTest.java | 32 +++ 9 files changed, 694 insertions(+) create mode 100644 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpFlowFactoryTest.java create mode 100644 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpResolverUtilsTest.java create mode 100644 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpSenderTest.java create mode 100644 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTaskerTest.java create mode 100644 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTest.java create mode 100644 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpUtilsTest.java create mode 100644 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/test/OfOverlayDataBrokerTest.java diff --git a/groupbasedpolicy/pom.xml b/groupbasedpolicy/pom.xml index 7a4a186bd..075ac46e3 100755 --- a/groupbasedpolicy/pom.xml +++ b/groupbasedpolicy/pom.xml @@ -69,6 +69,17 @@ + + org.apache.maven.plugins + maven-jar-plugin + + + + test-jar + + + + diff --git a/renderers/ofoverlay/pom.xml b/renderers/ofoverlay/pom.xml index 473f63283..2ad01bc47 100755 --- a/renderers/ofoverlay/pom.xml +++ b/renderers/ofoverlay/pom.xml @@ -69,6 +69,13 @@ test-jar test + + org.opendaylight.groupbasedpolicy + groupbasedpolicy + ${project.version} + test-jar + test + diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpFlowFactoryTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpFlowFactoryTest.java new file mode 100644 index 000000000..ab2200b9e --- /dev/null +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpFlowFactoryTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.controller.liblldp.EtherTypes; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues; +import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch; +import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch; + +public class ArpFlowFactoryTest { + + @Test + public void createEthernetMatchWithAddressTest() { + MacAddress mac = new MacAddress("00:00:00:00:00:01"); + EthernetMatch match = ArpFlowFactory.createEthernetMatch(mac); + Assert.assertEquals(mac, match.getEthernetDestination().getAddress()); + Assert.assertNull(match.getEthernetSource()); + Assert.assertEquals(Long.valueOf(EtherTypes.ARP.intValue()), match.getEthernetType().getType().getValue()); + } + + @Test + public void createEthernetMatchWithoutAddressTest() { + EthernetMatch match = ArpFlowFactory.createEthernetMatch(); + Assert.assertEquals(Long.valueOf(EtherTypes.ARP.intValue()), match.getEthernetType().getType().getValue()); + Assert.assertNull(match.getEthernetDestination()); + } + + @Test + public void createArpMatchTest() { + Ipv4Address senderAddress = new Ipv4Address("192.168.0.1"); + Ipv4Address targetAddress = new Ipv4Address("192.168.0.2"); + MacAddress targetMac = new MacAddress("00:00:00:00:00:01"); + ArpMessageAddress target = new ArpMessageAddress(targetMac, targetAddress); + ArpMatch match = ArpFlowFactory.createArpMatch(target, senderAddress); + Assert.assertTrue(match.getArpTargetTransportAddress().getValue().contains(targetAddress.getValue())); + Assert.assertTrue(match.getArpSourceTransportAddress().getValue().contains(senderAddress.getValue())); + Assert.assertEquals(ArpOperation.REPLY.intValue(), match.getArpOp().intValue()); + } + + @Test + public void createSendToControllerActionTest() { + int order = 25; + Action action = ArpFlowFactory.createSendToControllerAction(order); + Assert.assertEquals(order, action.getOrder().intValue()); + Assert.assertEquals(order, action.getKey().getOrder().intValue()); + Assert.assertTrue(action.getAction() instanceof OutputActionCase); + OutputActionCase output = ((OutputActionCase) action.getAction()); + Assert.assertEquals(OutputPortValues.CONTROLLER.toString(), + output.getOutputAction().getOutputNodeConnector().getValue()); + } +} diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpResolverUtilsTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpResolverUtilsTest.java new file mode 100644 index 000000000..0d40724cb --- /dev/null +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpResolverUtilsTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp; + +import java.net.InetAddress; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.opendaylight.controller.liblldp.EtherTypes; +import org.opendaylight.controller.liblldp.Ethernet; +import org.opendaylight.controller.liblldp.HexEncode; +import org.opendaylight.controller.liblldp.PacketException; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder; + +public class ArpResolverUtilsTest { + + @Test + public void getArpFromTest() throws Exception { + + Arp arp = new Arp(); + byte[] sha = HexEncode.bytesFromHexString("00:00:00:00:00:01"); + byte[] spa = InetAddress.getByName("192.168.0.1").getAddress(); + byte[] tha = HexEncode.bytesFromHexString("00:00:00:00:00:02"); + byte[] tpa = InetAddress.getByName("192.168.0.2").getAddress(); + int htype = 1; + int ptype = EtherTypes.IPv4.intValue(); + short hlen = 6; + short plen = 4; + int operation = 1; + + arp.setSenderHardwareAddress(sha); + arp.setSenderProtocolAddress(spa); + arp.setTargetHardwareAddress(tha); + arp.setTargetProtocolAddress(tpa); + arp.setOperation(operation); + arp.setHardwareLength(hlen); + arp.setProtocolLength(plen); + arp.setHardwareType(htype); + arp.setProtocolType(ptype); + + Ethernet arpFrame = new Ethernet().setSourceMACAddress(sha) + .setDestinationMACAddress(tha) + .setEtherType(EtherTypes.ARP.shortValue()); + arpFrame.setPayload(arp); + PacketReceived packet = new PacketReceivedBuilder().setPayload(arpFrame.serialize()).build(); + Arp arpOut = ArpResolverUtils.getArpFrom(packet); + Assert.assertEquals(arp, arpOut); + } + + @Rule public ExpectedException e = ExpectedException.none(); + @Test + public void getArpFromTest_notArpPacket() throws PacketException { + byte[] payload = {0xb, 0xe, 0xe, 0xf}; + PacketReceived packet = new PacketReceivedBuilder().setPayload(payload).build(); + e.expect(PacketException.class); + ArpResolverUtils.getArpFrom(packet); + } +} diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpSenderTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpSenderTest.java new file mode 100644 index 000000000..05c434f4f --- /dev/null +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpSenderTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp; + +import java.util.concurrent.Future; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.opendaylight.controller.liblldp.Ethernet; +import org.opendaylight.controller.liblldp.Packet; +import org.opendaylight.controller.liblldp.PacketException; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.common.RpcResult; + +import com.google.common.util.concurrent.Futures; + +public class ArpSenderTest { + + private PacketProcessingService packetService; + + @Before + public void init() { + packetService = Mockito.mock(PacketProcessingService.class); + Future> future = Futures.immediateCheckedFuture(null); + Mockito.when(packetService.transmitPacket(Matchers.any(TransmitPacketInput.class))).thenReturn(future); + } + + @Test + public void floodArpTest() throws PacketException { + ArpSender arpSender = new ArpSender(packetService); + MacAddress senderMac = new MacAddress("00:00:00:00:00:01"); + Ipv4Address senderAddress = new Ipv4Address("192.168.0.1"); + Ipv4Address targetAddress = new Ipv4Address("192.168.0.2"); + InstanceIdentifier nodeIid = + InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("node1"))).build(); + ArpMessageAddress arpAddress = new ArpMessageAddress(senderMac, senderAddress); + + arpSender.floodArp(arpAddress, targetAddress, nodeIid); + ArgumentCaptor argument = ArgumentCaptor.forClass(TransmitPacketInput.class); + Mockito.verify(packetService).transmitPacket(argument.capture()); + + Assert.assertEquals(nodeIid, argument.getValue().getNode().getValue()); + Packet ethernet = new Ethernet().deserialize(argument.getValue().getPayload(), 0, + argument.getValue().getPayload().length); + Packet potentialArp = ethernet.getPayload(); + Assert.assertTrue(potentialArp instanceof Arp); + Arp arp = (Arp) potentialArp; + Assert.assertArrayEquals(ArpUtils.ipToBytes(senderAddress), arp.getSenderProtocolAddress()); + Assert.assertArrayEquals(ArpUtils.ipToBytes(targetAddress), arp.getTargetProtocolAddress()); + Assert.assertArrayEquals(ArpUtils.macToBytes(senderMac), arp.getSenderHardwareAddress()); + } +} diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTaskerTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTaskerTest.java new file mode 100644 index 000000000..6d6b1eb7e --- /dev/null +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTaskerTest.java @@ -0,0 +1,267 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp; + +import java.net.InetAddress; +import java.util.Collections; +import java.util.concurrent.Future; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.opendaylight.controller.liblldp.EtherTypes; +import org.opendaylight.controller.liblldp.Ethernet; +import org.opendaylight.controller.liblldp.HexEncode; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.test.OfOverlayDataBrokerTest; +import org.opendaylight.groupbasedpolicy.util.IidFactory; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2ContextId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Key; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayL3Context; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayL3ContextBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfig; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.ExternalInterfacesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.TenantsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ForwardingContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ForwardingContextBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomainBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomainKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.common.RpcResult; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; + +public class ArpTaskerTest extends OfOverlayDataBrokerTest { + + private ArpTasker arpTasker; + private DataBroker broker; + private RpcProviderRegistry rpcRegistry; + private SalFlowService flowService; + + @Before + public void init() { + + PacketProcessingService packetService = Mockito.mock(PacketProcessingService.class); + flowService = Mockito.mock(SalFlowService.class); + rpcRegistry = Mockito.mock(RpcProviderRegistry.class); + Mockito.when(rpcRegistry.getRpcService(PacketProcessingService.class)).thenReturn(packetService); + Mockito.when(rpcRegistry.getRpcService(SalFlowService.class)).thenReturn(flowService); + } + + @SuppressWarnings("unchecked") + @Test + public void addMacForL3EpAndCreateEp_onPacketReceivedTest() throws Exception { + + IpAddress ipAddr = new IpAddress(new Ipv4Address("192.168.0.1")); + L3ContextId l3conId = new L3ContextId("l3context"); + EndpointL3Key key = new EndpointL3Key(ipAddr, l3conId); + EndpointL3Builder epL3 = new EndpointL3Builder(); + // node conector + NodeConnectorId connectorId = new NodeConnectorId("nodeConnector"); + MacAddress macAddr = new MacAddress("00:00:00:00:00:01"); + FlowCapableNodeConnectorBuilder fcnConnector = + new FlowCapableNodeConnectorBuilder().setHardwareAddress(macAddr); + NodeConnectorBuilder connector = new NodeConnectorBuilder().setKey(new NodeConnectorKey(connectorId)) + .addAugmentation(FlowCapableNodeConnector.class, fcnConnector.build()); + // node + NodeId nodeId = new NodeId("node"); + ExternalInterfacesBuilder extIface = new ExternalInterfacesBuilder().setNodeConnectorId(connectorId); + OfOverlayNodeConfigBuilder ofOverNodeCfg = + new OfOverlayNodeConfigBuilder().setExternalInterfaces(Collections.singletonList(extIface.build())); + NodeBuilder node = new NodeBuilder().addAugmentation(OfOverlayNodeConfig.class, ofOverNodeCfg.build()) + .setKey(new NodeKey(nodeId)) + .setId(nodeId) + .setNodeConnector(Collections.singletonList(connector.build())); + // subnet + NetworkDomainId domainId = new NetworkDomainId("domainId"); + TenantId tenantId = new TenantId("tenant"); + L2ContextId l2conId = new L2ContextId("l2context"); + SubnetBuilder subnet = new SubnetBuilder().setId(new SubnetId(domainId)) + .setIpPrefix(new IpPrefix(new Ipv4Prefix(ipAddr.getIpv4Address().getValue() + "/24"))) + .setParent(l2conId); + TenantsBuilder tenants = + new TenantsBuilder().setTenant(Collections.singletonList(new TenantBuilder().setId(tenantId) + .setForwardingContext( + new ForwardingContextBuilder().setSubnet(Collections.singletonList(subnet.build())).build()) + .build())); + + // test without key + ReadOnlyTransaction rtx = Mockito.mock(ReadOnlyTransaction.class); + broker = Mockito.mock(DataBroker.class); + arpTasker = new ArpTasker(rpcRegistry, broker); + + epL3.setKey(new EndpointL3Key(Mockito.mock(IpAddress.class), null)); + arpTasker.addMacForL3EpAndCreateEp(epL3.build()); + Mockito.verify(broker, Mockito.never()).newReadOnlyTransaction(); + + // test without node with external interface + epL3.setKey(key); + Mockito.when(broker.newReadOnlyTransaction()).thenReturn(rtx); + CheckedFuture, ReadFailedException> future = + Futures.immediateCheckedFuture(Optional.absent()); + Mockito.when(rtx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION), Matchers.any(InstanceIdentifier.class))) + .thenReturn(future); + arpTasker.addMacForL3EpAndCreateEp(epL3.build()); + Mockito.verify(broker).newReadOnlyTransaction(); + Mockito.verify(rtx).close(); + + // test correct + broker = getDataBroker(); + arpTasker = new ArpTasker(rpcRegistry, broker); + WriteTransaction wtx = broker.newWriteOnlyTransaction(); + wtx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Nodes.class).build(), + new NodesBuilder().setNode(Collections.singletonList(node.build())).build(), true); + wtx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(Nodes.class) + .child(Node.class, node.getKey()) + .child(NodeConnector.class, new NodeConnectorKey(connectorId)) + .build(), + connector.build(), true); + wtx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Tenants.class).build(), tenants.build(), + true); + wtx.submit().get(); + + Future> flowFuture = Mockito.mock(Future.class); + Mockito.when(flowService.addFlow(Mockito.any(AddFlowInput.class))).thenReturn(flowFuture); + + epL3.setNetworkContainment(domainId).setTenant(tenantId); + arpTasker.addMacForL3EpAndCreateEp(epL3.build()); + ArgumentCaptor argument = ArgumentCaptor.forClass(AddFlowInput.class); + Mockito.verify(flowService).addFlow(argument.capture()); + AddFlowInput result = argument.getValue(); + Assert.assertEquals(EtherTypes.ARP.intValue(), + result.getMatch().getEthernetMatch().getEthernetType().getType().getValue().intValue()); + ArpMatch match = (ArpMatch)result.getMatch().getLayer3Match(); + Assert.assertEquals(ArpOperation.REPLY.intValue(),match.getArpOp().intValue()); + Assert.assertEquals("192.168.0.254/32",match.getArpTargetTransportAddress().getValue()); + Assert.assertEquals("192.168.0.1/32", match.getArpSourceTransportAddress().getValue()); + Assert.assertEquals(connectorId, result.getMatch().getInPort()); + Assert.assertEquals(new NodeRef(InstanceIdentifier.builder(Nodes.class) + .child(Node.class, node.getKey()).build()), result.getNode()); + + // onPacketReceived + Arp arp = new Arp(); + byte[] sha = HexEncode.bytesFromHexString("00:00:00:00:00:01"); + byte[] spa = InetAddress.getByName("192.168.0.1").getAddress(); + byte[] tha = HexEncode.bytesFromHexString("00:00:00:00:00:02"); + byte[] tpa = InetAddress.getByName("192.168.0.2").getAddress(); + int htype = 1; + int ptype = EtherTypes.IPv4.intValue(); + short hlen = 6; + short plen = 4; + int operation = ArpOperation.REPLY.intValue(); + + arp.setSenderHardwareAddress(sha); + arp.setSenderProtocolAddress(spa); + arp.setTargetHardwareAddress(tha); + arp.setTargetProtocolAddress(tpa); + arp.setOperation(operation); + arp.setHardwareLength(hlen); + arp.setProtocolLength(plen); + arp.setHardwareType(htype); + arp.setProtocolType(ptype); + + Ethernet arpFrame = new Ethernet().setSourceMACAddress(sha) + .setDestinationMACAddress(tha) + .setEtherType(EtherTypes.ARP.shortValue()); + arpFrame.setPayload(arp); + + L2BridgeDomainId l2domainId = new L2BridgeDomainId(l2conId); + L2BridgeDomainBuilder l2domain = new L2BridgeDomainBuilder().setId(l2domainId); + + InstanceIdentifier ncIid = InstanceIdentifier.builder(Nodes.class) + .child(Node.class, new NodeKey(new NodeId("node"))) + .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("connector"))) + .build(); + PacketReceived packet = new PacketReceivedBuilder().setPayload(arpFrame.serialize()) + .setIngress(new NodeConnectorRef(ncIid)) + .build(); + + wtx = broker.newWriteOnlyTransaction(); + OfOverlayL3Context augment = new OfOverlayL3ContextBuilder().build(); + epL3.addAugmentation(OfOverlayL3Context.class, augment); + InstanceIdentifier epL3Iid = + InstanceIdentifier.builder(Endpoints.class).child(EndpointL3.class, key).build(); + wtx.put(LogicalDatastoreType.OPERATIONAL, epL3Iid, epL3.build(), true); + + InstanceIdentifier l2domainIid = InstanceIdentifier.builder(Tenants.class) + .child(Tenant.class, new TenantKey(tenantId)) + .child(ForwardingContext.class) + .child(L2BridgeDomain.class, new L2BridgeDomainKey(l2domainId)) + .build(); + wtx.put(LogicalDatastoreType.OPERATIONAL, l2domainIid, l2domain.build() ,true); + wtx.submit(); + + arpTasker.onPacketReceived(packet); + rtx = broker.newReadOnlyTransaction(); + Optional optional = rtx.read(LogicalDatastoreType.OPERATIONAL, epL3Iid).get(); + Assert.assertTrue(optional.isPresent()); + EndpointL3 epl3 = optional.get(); + Assert.assertArrayEquals(sha, HexEncode.bytesFromHexString(epl3.getMacAddress().getValue())); + Assert.assertEquals(l2domain.getId(), epl3.getL2Context()); + Optional optionalEp = rtx.read(LogicalDatastoreType.OPERATIONAL, + IidFactory.endpointIid(l2domainId, new MacAddress("00:00:00:00:00:01"))).get(); + Assert.assertTrue(optionalEp.isPresent()); + Assert.assertEquals(new OfOverlayContextBuilder(augment).build(), optionalEp.get().getAugmentation(OfOverlayContext.class)); + } +} diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTest.java new file mode 100644 index 000000000..cbcdaf352 --- /dev/null +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpTest.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.liblldp.EtherTypes; +import org.opendaylight.controller.liblldp.HexEncode; +import org.opendaylight.controller.liblldp.Packet; +import org.opendaylight.controller.liblldp.PacketException; + +import com.google.common.primitives.Ints; + +public class ArpTest { + + private byte[] sha; + private byte[] spa; + private byte[] tha; + private byte[] tpa; + private int htype; + private int ptype; + private short hlen; + private short plen; + private int operation; + private Arp arp; + + @Before + public void init() throws UnknownHostException { + sha = HexEncode.bytesFromHexString("00:00:00:00:00:01"); + spa = InetAddress.getByName("192.168.0.1").getAddress(); + tha = HexEncode.bytesFromHexString("00:00:00:00:00:02"); + tpa = InetAddress.getByName("192.168.0.2").getAddress(); + htype = 2; + ptype = EtherTypes.IPv6.intValue(); + hlen = 6; + plen = 4; + operation = 32; + + arp = new Arp(); + + arp.setSenderHardwareAddress(sha); + arp.setSenderProtocolAddress(spa); + arp.setTargetHardwareAddress(tha); + arp.setTargetProtocolAddress(tpa); + arp.setOperation(operation); + arp.setHardwareLength(hlen); + arp.setProtocolLength(plen); + arp.setHardwareType(htype); + arp.setProtocolType(ptype); + } + + @Test + public void ArpConstructionTest() throws Exception { + + Assert.assertArrayEquals(sha, arp.getSenderHardwareAddress()); + Assert.assertArrayEquals(tha, arp.getTargetHardwareAddress()); + Assert.assertArrayEquals(spa, arp.getSenderProtocolAddress()); + Assert.assertArrayEquals(tpa, arp.getTargetProtocolAddress()); + Assert.assertEquals(operation, arp.getOperation()); + Assert.assertEquals(hlen, arp.getHardwareLength()); + Assert.assertEquals(plen, arp.getProtocolLength()); + Assert.assertEquals(htype, arp.getHardwareType()); + Assert.assertEquals(ptype, arp.getProtocolType()); + } + + @Test + public void serializeTest() throws PacketException { + byte[] output = arp.serialize(); + Assert.assertEquals(htype, Ints.fromBytes((byte) 0, (byte) 0, output[0], output[1])); + Assert.assertEquals(ptype, Ints.fromBytes((byte) 0, (byte) 0, output[2], output[3])); + Assert.assertEquals(hlen, output[4]); + Assert.assertEquals(plen, output[5]); + Assert.assertEquals(operation, Ints.fromBytes((byte) 0, (byte) 0, output[6], output[7])); + Assert.assertArrayEquals(sha, Arrays.copyOfRange(output, 8, 14)); + Assert.assertArrayEquals(spa, Arrays.copyOfRange(output, 14, 18)); + Assert.assertArrayEquals(tha, Arrays.copyOfRange(output, 18, 24)); + Assert.assertArrayEquals(tpa, Arrays.copyOfRange(output, 24, 28)); + } + + @Test + public void deserializeTest() throws PacketException { + byte[] output = arp.serialize(); + Packet packet = arp.deserialize(output, 0, 28); + Assert.assertTrue(packet instanceof Arp); + Arp newArp = (Arp) packet; + Assert.assertArrayEquals(sha, newArp.getSenderHardwareAddress()); + Assert.assertArrayEquals(tha, newArp.getTargetHardwareAddress()); + Assert.assertArrayEquals(spa, newArp.getSenderProtocolAddress()); + Assert.assertArrayEquals(tpa, newArp.getTargetProtocolAddress()); + Assert.assertEquals(operation, newArp.getOperation()); + Assert.assertEquals(hlen, newArp.getHardwareLength()); + Assert.assertEquals(plen, newArp.getProtocolLength()); + Assert.assertEquals(htype, newArp.getHardwareType()); + Assert.assertEquals(ptype, newArp.getProtocolType()); + } +} diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpUtilsTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpUtilsTest.java new file mode 100644 index 000000000..7e46d9621 --- /dev/null +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/arp/ArpUtilsTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp; + +import java.net.InetAddress; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.controller.liblldp.EtherTypes; +import org.opendaylight.controller.liblldp.Ethernet; +import org.opendaylight.controller.liblldp.HexEncode; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; + +public class ArpUtilsTest { + + @Test + public void getArpFrameToStringFormatTest() throws Exception { + MacAddress destMac = new MacAddress("00:00:00:00:00:01"); + MacAddress srcMac = new MacAddress("00:00:00:00:00:02"); + Arp arp = new Arp(); + byte[] sha = HexEncode.bytesFromHexString(srcMac.getValue()); + byte[] spa = InetAddress.getByName("192.168.0.1").getAddress(); + byte[] tha = HexEncode.bytesFromHexString(destMac.getValue()); + byte[] tpa = InetAddress.getByName("192.168.0.2").getAddress(); + int htype = 2; + int ptype = EtherTypes.IPv6.intValue(); + short hlen = 8; + short plen = 8; + int operation = 32; + + arp.setSenderHardwareAddress(sha); + arp.setSenderProtocolAddress(spa); + arp.setTargetHardwareAddress(tha); + arp.setTargetProtocolAddress(tpa); + arp.setOperation(operation); + arp.setHardwareLength(hlen); + arp.setProtocolLength(plen); + arp.setHardwareType(htype); + arp.setProtocolType(ptype); + + Ethernet eth = new Ethernet().setEtherType(EtherTypes.IPv4.shortValue()) + .setDestinationMACAddress(ArpUtils.macToBytes(destMac)) + .setSourceMACAddress(ArpUtils.macToBytes(srcMac)); + eth.setPayload(arp); + + String daco = InetAddress.getByAddress(spa).getHostAddress(); + String result = ArpUtils.getArpFrameToStringFormat(eth); + Assert.assertTrue(result.contains("getSourceMACAddress()=" + srcMac.getValue())); + Assert.assertTrue(result.contains("getDestinationMACAddress()=" + destMac.getValue())); + Assert.assertTrue( + result.contains("getEtherType()=" + EtherTypes.loadFromString(String.valueOf(eth.getEtherType())))); + Assert.assertTrue(result.contains("getHardwareType()=" + htype)); + Assert.assertTrue(result.contains("getProtocolType()=" + ptype)); + Assert.assertTrue(result.contains("getHardwareLength()=" + hlen)); + Assert.assertTrue(result.contains("getProtocolLength()=" + plen)); + Assert.assertTrue(result.contains("getOperation()=" + ArpOperation.loadFromInt(arp.getOperation()))); + Assert.assertTrue(result.contains("getSenderHardwareAddress()=" + HexEncode.bytesToHexStringFormat(sha))); + Assert.assertTrue(result.contains("getTargetHardwareAddress()=" + HexEncode.bytesToHexStringFormat(tha))); + Assert.assertTrue( + result.contains("getSenderProtocolAddress()=" + InetAddress.getByAddress(spa).getHostAddress())); + Assert.assertTrue( + result.contains("getTargetProtocolAddress()=" + InetAddress.getByAddress(tpa).getHostAddress())); + } +} diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/test/OfOverlayDataBrokerTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/test/OfOverlayDataBrokerTest.java new file mode 100644 index 000000000..174e78008 --- /dev/null +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/test/OfOverlayDataBrokerTest.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.test; + +import java.util.Collection; + +import org.opendaylight.groupbasedpolicy.test.CustomDataBrokerTest; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfig; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; + +import com.google.common.collect.ImmutableList; + +public class OfOverlayDataBrokerTest extends CustomDataBrokerTest{ + + @Override + public Collection> getClassesFromModules() { + return ImmutableList.>of(Nodes.class, OfOverlayNodeConfig.class, FlowCapableNodeConnector.class, + MacAddress.class, Tenants.class, SubnetId.class, Endpoints.class); + } + +} -- 2.36.6