From: Alessandro Boch Date: Tue, 24 Jun 2014 23:52:12 +0000 (-0700) Subject: AD-SAL: Filter packet-in based on container flow X-Git-Tag: release/helium~596^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=0fe04fdf4271be914d43d35231c1706ec34b05f6 AD-SAL: Filter packet-in based on container flow - In AD-SAL openflow plugin incoming packets are delivered to listeners in respective container only based on incoming port. - This patch enforces the container flow filtering logic as well Change-Id: I6a23b114712e63b8ffb18c173bbbaad5fb7634bd Signed-off-by: Alessandro Boch --- diff --git a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DataPacketMuxDemux.java b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DataPacketMuxDemux.java index 5c2af6d8b4..54d5fb888a 100644 --- a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DataPacketMuxDemux.java +++ b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DataPacketMuxDemux.java @@ -32,11 +32,15 @@ import org.opendaylight.controller.sal.core.Node; import org.opendaylight.controller.sal.core.NodeConnector; import org.opendaylight.controller.sal.core.Property; import org.opendaylight.controller.sal.core.UpdateType; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.packet.Ethernet; import org.opendaylight.controller.sal.packet.IPluginOutDataPacketService; +import org.opendaylight.controller.sal.packet.PacketException; import org.opendaylight.controller.sal.packet.PacketResult; import org.opendaylight.controller.sal.packet.RawPacket; import org.opendaylight.controller.sal.utils.GlobalConstants; import org.opendaylight.controller.sal.utils.HexEncode; +import org.opendaylight.controller.sal.utils.NetUtils; import org.openflow.protocol.OFMessage; import org.openflow.protocol.OFPacketIn; import org.openflow.protocol.OFPacketOut; @@ -169,12 +173,10 @@ public class DataPacketMuxDemux implements IContainerListener, @Override public void receive(ISwitch sw, OFMessage msg) { - if (sw == null || msg == null - || this.pluginOutDataPacketServices == null) { + if (sw == null || msg == null || this.pluginOutDataPacketServices == null) { // Something fishy, we cannot do anything - logger.debug( - "sw: {} and/or msg: {} and/or pluginOutDataPacketServices: {} is null!", - new Object[] { sw, msg, this.pluginOutDataPacketServices }); + logger.debug("sw: {} and/or msg: {} and/or pluginOutDataPacketServices: {} is null!", new Object[] { sw, + msg, this.pluginOutDataPacketServices }); return; } @@ -185,8 +187,7 @@ public class DataPacketMuxDemux implements IContainerListener, logger.debug("Connection service refused DataPacketMuxDemux receive {} {}", sw, msg); return; } - } - catch (Exception e) { + } catch (Exception e) { return; } @@ -204,30 +205,22 @@ public class DataPacketMuxDemux implements IContainerListener, // pass the parsed packet simply because once the // packet infra is settled all the packets will passed // around as parsed and read-only - for (int i = 0; i < this.iDataPacketListen.size(); i++) { - IDataPacketListen s = this.iDataPacketListen.get(i); - if (s.receiveDataPacket(dataPacket).equals( - PacketResult.CONSUME)) { + for (IDataPacketListen s : this.iDataPacketListen) { + if (s.receiveDataPacket(dataPacket).equals(PacketResult.CONSUME)) { logger.trace("Consumed locally data packet"); return; } } - // Now dispatch the packet toward SAL at least for - // default container, we need to revisit this in a general - // slicing architecture refresh + // Now dispatch the packet toward SAL for default container IPluginOutDataPacketService defaultOutService = this.pluginOutDataPacketServices .get(GlobalConstants.DEFAULT.toString()); if (defaultOutService != null) { defaultOutService.receiveDataPacket(dataPacket); if (logger.isTraceEnabled()) { - logger.trace( - "Dispatched to apps a frame of size: {} on " + - "container: {}: {}", new Object[] { - ofPacket.getPacketData().length, - GlobalConstants.DEFAULT.toString(), - HexEncode.bytesToHexString(dataPacket - .getPacketData()) }); + logger.trace("Dispatched to apps a frame of size: {} on " + "container: {}: {}", + new Object[] { ofPacket.getPacketData().length, GlobalConstants.DEFAULT.toString(), + HexEncode.bytesToHexString(dataPacket.getPacketData()) }); } } // Now check the mapping between nodeConnector and @@ -235,32 +228,43 @@ public class DataPacketMuxDemux implements IContainerListener, // flowSpec List containersRX = this.nc2Container.get(p); if (containersRX != null) { - for (int i = 0; i < containersRX.size(); i++) { - String container = containersRX.get(i); - IPluginOutDataPacketService s = this.pluginOutDataPacketServices - .get(container); - if (s != null) { - // TODO add filtering on a per-flowSpec base - s.receiveDataPacket(dataPacket); - if (logger.isTraceEnabled()) { - logger.trace( - "Dispatched to apps a frame of size: {}" + - " on container: {}: {}", new Object[] { - ofPacket.getPacketData().length, - container, - HexEncode.bytesToHexString(dataPacket - .getPacketData()) }); + Ethernet frame = new Ethernet(); + byte data[] = dataPacket.getPacketData(); + frame.deserialize(data, 0, data.length * NetUtils.NumBitsInAByte); + Match packetMatch = frame.getMatch(); + for (String container : containersRX) { + boolean notify = true; + List containerFlows = this.container2FlowSpecs.get(container); + if (containerFlows != null) { + notify = false; + for (ContainerFlow cFlow : containerFlows) { + if (cFlow.allowsMatch(packetMatch)) { + notify = true; + break; + } + } + if (notify) { + IPluginOutDataPacketService s = this.pluginOutDataPacketServices.get(container); + if (s != null) { + s.receiveDataPacket(dataPacket); + if (logger.isTraceEnabled()) { + logger.trace( + "Dispatched to apps a frame of size: {}" + " on container: {}: {}", + new Object[] { ofPacket.getPacketData().length, container, + HexEncode.bytesToHexString(dataPacket.getPacketData()) }); + } + } } } } } - // This is supposed to be the catch all for all the // DataPacket hence we will assume it has been handled return; } catch (ConstructionException cex) { + } catch (PacketException e) { + logger.debug("Failed to deserialize raw packet: ", e.getMessage()); } - // If we reach this point something went wrong. return; } else { diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/ARP.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/ARP.java index 8fc0d625eb..21c17b366c 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/ARP.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/ARP.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -258,18 +258,23 @@ public class ARP extends Packet { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (!super.equals(obj)) + } + if (!super.equals(obj)) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } ARP other = (ARP) obj; if (fieldValues == null) { - if (other.fieldValues != null) + if (other.fieldValues != null) { return false; - } else if (!fieldValues.equals(other.fieldValues)) + } + } else if (!fieldValues.equals(other.fieldValues)) { return false; + } return true; } } diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java index d0068564a9..235e71a760 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java @@ -15,6 +15,8 @@ import java.util.Map; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; import org.opendaylight.controller.sal.utils.EtherTypes; import org.opendaylight.controller.sal.utils.NetUtils; @@ -139,4 +141,10 @@ public class Ethernet extends Packet { return this; } + @Override + public void populateMatch(Match match) { + match.setField(MatchType.DL_SRC, this.getSourceMACAddress()); + match.setField(MatchType.DL_DST, this.getDestinationMACAddress()); + match.setField(MatchType.DL_TYPE, this.getEtherType()); + } } diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IEEE8021Q.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IEEE8021Q.java index 39c7d386bc..9825d0eefb 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IEEE8021Q.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IEEE8021Q.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -13,6 +13,8 @@ import java.util.LinkedHashMap; import java.util.Map; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; /** * Class that represents the IEEE 802.1Q objects @@ -148,4 +150,10 @@ public class IEEE8021Q extends Packet { return this; } + @Override + public void populateMatch(Match match) { + match.setField(MatchType.DL_VLAN, this.getVid()); + match.setField(MatchType.DL_VLAN_PR, this.getPcp()); + match.setField(MatchType.DL_TYPE, this.getEtherType()); + } } diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IPv4.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IPv4.java index 559acd633a..3363f423d6 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IPv4.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/IPv4.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -20,6 +20,8 @@ import java.util.Random; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; import org.opendaylight.controller.sal.utils.IPProtocols; import org.opendaylight.controller.sal.utils.NetUtils; import org.slf4j.Logger; @@ -577,4 +579,12 @@ public class IPv4 extends Packet { corrupted = true; } } + + @Override + public void populateMatch(Match match) { + match.setField(MatchType.NW_SRC, NetUtils.getInetAddress(this.getSourceAddress())); + match.setField(MatchType.NW_DST, NetUtils.getInetAddress(this.getDestinationAddress())); + match.setField(MatchType.NW_PROTO, this.getProtocol()); + match.setField(MatchType.NW_TOS, this.getDiffServ()); + } } diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDP.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDP.java index 6f5cf04a52..9b5dc082bb 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDP.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDP.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; + import org.opendaylight.controller.sal.utils.HexEncode; import org.opendaylight.controller.sal.utils.NetUtils; diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDPTLV.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDPTLV.java index efb938e37f..35244ea697 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDPTLV.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/LLDPTLV.java @@ -151,18 +151,23 @@ public class LLDPTLV extends Packet { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (!super.equals(obj)) + } + if (!super.equals(obj)) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } LLDPTLV other = (LLDPTLV) obj; if (fieldValues == null) { - if (other.fieldValues != null) + if (other.fieldValues != null) { return false; - } else if (!fieldValues.equals(other.fieldValues)) + } + } else if (!fieldValues.equals(other.fieldValues)) { return false; + } return true; } diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Packet.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Packet.java index 58b5d3914a..789aa12653 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Packet.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Packet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -13,6 +13,7 @@ import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.tuple.Pair; +import org.opendaylight.controller.sal.match.Match; import org.opendaylight.controller.sal.utils.HexEncode; import org.opendaylight.controller.sal.utils.NetUtils; import org.slf4j.Logger; @@ -364,4 +365,31 @@ public abstract class Packet { return true; } + /** + * Adds to the passed Match this packet's header fields + * + * @param match + * The Match object to populate + */ + public void populateMatch(Match match) { + // To be overridden by derived packet classes which have well known + // header fields so that Packet.getMatch would return desired result + } + + /** + * Returns the Match object containing this packet and its payload + * encapsulated packets' header fields + * + * @return The Match containing the header fields of this packet and of its + * payload encapsulated packets + */ + public Match getMatch() { + Match match = new Match(); + Packet packet = this; + while (packet != null) { + packet.populateMatch(match); + packet = packet.getPayload(); + } + return match; + } } diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/TCP.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/TCP.java index 8253ac46d3..d27494599c 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/TCP.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/TCP.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -15,6 +15,8 @@ import java.util.Map; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; /** * Class that represents the TCP segment objects @@ -238,4 +240,9 @@ public class TCP extends Packet { return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM))); } + @Override + public void populateMatch(Match match) { + match.setField(MatchType.TP_SRC, this.getSourcePort()); + match.setField(MatchType.TP_DST, this.getDestinationPort()); + } } diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/UDP.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/UDP.java index f82ed8fcae..069a277f89 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/UDP.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/UDP.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -15,6 +15,8 @@ import java.util.Map; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; /** * Class that represents the UDP datagram objects @@ -156,4 +158,10 @@ public class UDP extends Packet { fieldValues.put(CHECKSUM, checksum); return this; } + + @Override + public void populateMatch(Match match) { + match.setField(MatchType.TP_SRC, this.getSourcePort()); + match.setField(MatchType.TP_DST, this.getDestinationPort()); + } } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/ARPTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/ARPTest.java index a3ebd7ee0d..10bd535eb2 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/ARPTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/ARPTest.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/EthernetTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/EthernetTest.java index 2494eb6b2d..494dd2abf2 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/EthernetTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/EthernetTest.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -9,9 +9,14 @@ package org.opendaylight.controller.sal.packet; +import java.util.Arrays; + import junit.framework.Assert; import org.junit.Test; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; +import org.opendaylight.controller.sal.utils.EtherTypes; public class EthernetTest { @@ -95,4 +100,22 @@ public class EthernetTest { } + @Test + public void testGetMatch() throws Exception { + Ethernet eth = new Ethernet(); + byte smac[] = { (byte) 0xf0, (byte) 0xde, (byte) 0xf1, (byte) 0x71, (byte) 0x72, (byte) 0x8d }; + byte dmac[] = { (byte) 0xde, (byte) 0x28, (byte) 0xdb, (byte) 0xb3, (byte) 0x7c, (byte) 0xf8 }; + short ethType = EtherTypes.IPv4.shortValue(); + eth.setDestinationMACAddress(dmac); + eth.setSourceMACAddress(smac); + eth.setEtherType(ethType); + + Match match = eth.getMatch(); + + Assert.assertTrue(Arrays.equals(smac, (byte[]) match.getField(MatchType.DL_SRC).getValue())); + Assert.assertTrue(Arrays.equals(dmac, (byte[]) match.getField(MatchType.DL_DST).getValue())); + Assert.assertEquals(ethType, (short) match.getField(MatchType.DL_TYPE).getValue()); + + } + } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IEEE8021QTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IEEE8021QTest.java index a4c6c1fc4b..2443b491c7 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IEEE8021QTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IEEE8021QTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -9,7 +9,11 @@ package org.opendaylight.controller.sal.packet; import junit.framework.Assert; + import org.junit.Test; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; +import org.opendaylight.controller.sal.utils.EtherTypes; import org.opendaylight.controller.sal.utils.NetUtils; public class IEEE8021QTest { @@ -212,4 +216,21 @@ public class IEEE8021QTest { Assert.assertTrue(data[44] == (byte) 0x09); Assert.assertTrue(data[45] == (byte) 0xFE); } + + @Test + public void testGetMatchFullPacket() throws Exception { + IEEE8021Q dot1q = new IEEE8021Q(); + byte priority = 4; + short vlanId = 59; + short ethType = EtherTypes.IPv4.shortValue(); + dot1q.setPcp(priority); + dot1q.setVid(vlanId); + dot1q.setEtherType(ethType); + + Match match = dot1q.getMatch(); + + Assert.assertEquals(priority, (byte) match.getField(MatchType.DL_VLAN_PR).getValue()); + Assert.assertEquals(vlanId, (short) match.getField(MatchType.DL_VLAN).getValue()); + Assert.assertEquals(ethType, (short) match.getField(MatchType.DL_TYPE).getValue()); + } } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IPv4Test.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IPv4Test.java index 5afcd8be7d..f5298711b6 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IPv4Test.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/IPv4Test.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -15,6 +15,8 @@ import java.util.Arrays; import junit.framework.Assert; import org.junit.Test; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; import org.opendaylight.controller.sal.utils.EtherTypes; import org.opendaylight.controller.sal.utils.IPProtocols; import org.opendaylight.controller.sal.utils.NetUtils; @@ -452,4 +454,31 @@ public class IPv4Test { Assert.assertFalse(decIcmp.isCorrupted()); Assert.assertTrue(Arrays.equals(icmpRawPayload, decIcmp.getRawPayload())); } + + @Test + public void testGetMatch() throws Exception { + IPv4 ip = new IPv4(); + InetAddress sourceAddress = InetAddress.getByName("172.168.190.15"); + InetAddress destintationAddress = InetAddress.getByName("23.128.0.11"); + byte protocol = IPProtocols.TCP.byteValue(); + byte tos = 7; + ip.setVersion((byte) 4); + ip.setIdentification((short) 5); + ip.setDiffServ(tos); + ip.setECN((byte) 0); + ip.setTotalLength((short) 84); + ip.setFlags((byte) 2); + ip.setFragmentOffset((short) 0); + ip.setTtl((byte) 64); + ip.setProtocol(protocol); + ip.setDestinationAddress(destintationAddress); + ip.setSourceAddress(sourceAddress); + + Match match = ip.getMatch(); + + Assert.assertEquals(sourceAddress, match.getField(MatchType.NW_SRC).getValue()); + Assert.assertEquals(destintationAddress, match.getField(MatchType.NW_DST).getValue()); + Assert.assertEquals(protocol, (byte) match.getField(MatchType.NW_PROTO).getValue()); + Assert.assertEquals(tos, (byte) match.getField(MatchType.NW_TOS).getValue()); + } } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/PacketTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/PacketTest.java index 8f3b283b2e..95eff32ef1 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/PacketTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/PacketTest.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -9,11 +9,17 @@ package org.opendaylight.controller.sal.packet; +import java.net.InetAddress; +import java.util.Arrays; import java.util.Map; import junit.framework.Assert; import org.junit.Test; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; +import org.opendaylight.controller.sal.utils.EtherTypes; +import org.opendaylight.controller.sal.utils.IPProtocols; public class PacketTest { @@ -120,9 +126,9 @@ public class PacketTest { eth.setSourceMACAddress(sMAC); eth.setEtherType(etherType); - dMACdata = (byte[]) fCValues.get("DestinationMACAddress"); - sMACdata = (byte[]) fCValues.get("SourceMACAddress"); - etherTypedata = (byte[]) fCValues.get("EtherType"); + dMACdata = fCValues.get("DestinationMACAddress"); + sMACdata = fCValues.get("SourceMACAddress"); + etherTypedata = fCValues.get("EtherType"); Assert.assertTrue(dMACdata[0] == 10); Assert.assertTrue(dMACdata[1] == 12); @@ -160,4 +166,62 @@ public class PacketTest { Assert.assertTrue(data[13] == 6); } + + @Test + public void testGetMatch() throws Exception { + TCP tcp = new TCP(); + short sport = (short) 11093; + short dport = (short) 23; + tcp.setSourcePort(sport); + tcp.setDestinationPort(dport); + + IPv4 ip = new IPv4(); + InetAddress sourceAddress = InetAddress.getByName("192.168.100.100"); + InetAddress destintationAddress = InetAddress.getByName("192.168.100.101"); + byte protocol = IPProtocols.TCP.byteValue(); + byte tos = 5; + ip.setVersion((byte) 4); + ip.setIdentification((short) 5); + ip.setDiffServ(tos); + ip.setECN((byte) 0); + ip.setTotalLength((short) 84); + ip.setFlags((byte) 2); + ip.setFragmentOffset((short) 0); + ip.setTtl((byte) 64); + ip.setProtocol(protocol); + ip.setDestinationAddress(destintationAddress); + ip.setSourceAddress(sourceAddress); + ip.setPayload(tcp); + + IEEE8021Q dot1q = new IEEE8021Q(); + byte priority = 4; + short vlanId = 59; + short ethType = EtherTypes.IPv4.shortValue(); + dot1q.setPcp(priority); + dot1q.setVid(vlanId); + dot1q.setEtherType(ethType); + dot1q.setPayload(ip); + + Ethernet eth = new Ethernet(); + byte smac[] = { (byte) 0xf0, (byte) 0xde, (byte) 0xf1, (byte) 0x71, (byte) 0x72, (byte) 0x8d }; + byte dmac[] = { (byte) 0xde, (byte) 0x28, (byte) 0xdb, (byte) 0xb3, (byte) 0x7c, (byte) 0xf8 }; + eth.setDestinationMACAddress(dmac); + eth.setSourceMACAddress(smac); + eth.setEtherType(EtherTypes.VLANTAGGED.shortValue()); + eth.setPayload(dot1q); + + Match match = eth.getMatch(); + + Assert.assertTrue(Arrays.equals(smac, (byte[]) match.getField(MatchType.DL_SRC).getValue())); + Assert.assertTrue(Arrays.equals(dmac, (byte[]) match.getField(MatchType.DL_DST).getValue())); + Assert.assertEquals(priority, (byte) match.getField(MatchType.DL_VLAN_PR).getValue()); + Assert.assertEquals(vlanId, (short) match.getField(MatchType.DL_VLAN).getValue()); + Assert.assertEquals(ethType, (short) match.getField(MatchType.DL_TYPE).getValue()); + Assert.assertEquals(sourceAddress, match.getField(MatchType.NW_SRC).getValue()); + Assert.assertEquals(destintationAddress, match.getField(MatchType.NW_DST).getValue()); + Assert.assertEquals(protocol, (byte) match.getField(MatchType.NW_PROTO).getValue()); + Assert.assertEquals(tos, (byte) match.getField(MatchType.NW_TOS).getValue()); + Assert.assertEquals(sport, (short) match.getField(MatchType.TP_SRC).getValue()); + Assert.assertEquals(dport, (short) match.getField(MatchType.TP_DST).getValue()); + } } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/TCPTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/TCPTest.java index 3e18aedfdb..e56f41e5c1 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/TCPTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/TCPTest.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -12,6 +12,8 @@ package org.opendaylight.controller.sal.packet; import junit.framework.Assert; import org.junit.Test; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; public class TCPTest { @@ -113,4 +115,18 @@ public class TCPTest { short checksum = tcp.getChecksum(); Assert.assertTrue(checksum == 200); } + + @Test + public void testGetMatch() throws Exception { + TCP tcp = new TCP(); + short sport = (short) 52012; + short dport = (short) 40345; + tcp.setSourcePort(sport); + tcp.setDestinationPort(dport); + + Match match = tcp.getMatch(); + + Assert.assertEquals(sport, (short) match.getField(MatchType.TP_SRC).getValue()); + Assert.assertEquals(dport, (short) match.getField(MatchType.TP_DST).getValue()); + } } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/UDPTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/UDPTest.java index 8d5be849a0..a3a597a82d 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/UDPTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/packet/UDPTest.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 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, @@ -12,6 +12,8 @@ package org.opendaylight.controller.sal.packet; import junit.framework.Assert; import org.junit.Test; +import org.opendaylight.controller.sal.match.Match; +import org.opendaylight.controller.sal.match.MatchType; public class UDPTest { @@ -95,4 +97,19 @@ public class UDPTest { } + @Test + public void testGetMatch() throws Exception { + UDP udp = new UDP(); + short sport = (short) 33000; + short dport = (short) 843; + udp.setSourcePort(sport); + udp.setDestinationPort(dport); + + Match match = udp.getMatch(); + + Assert.assertEquals(sport, (short) match.getField(MatchType.TP_SRC).getValue()); + Assert.assertEquals(dport, (short) match.getField(MatchType.TP_DST).getValue()); + + } + }