Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction.match; |
| 2 | |
| 3 | import net.floodlightcontroller.util.MACAddress; |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.util.IPv4Net; |
| 5 | |
| 6 | import com.google.common.base.Objects; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 7 | |
| 8 | /** |
| 9 | * A match object (traffic specifier) for packet nodes, flow-paths and intents. |
| 10 | * <p> |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 11 | * This class does not have a switch ID and a in-port number. They are handled |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 12 | * by MatchAction, Flow and Intent classes (for example MatchAction class has a |
| 13 | * separate SwitchPort field). Also, this class does not handle vlans or ttls. |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 14 | * <p> |
| 15 | * TODO: This class should be extensible. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 16 | */ |
Toshio Koide | d8b077a | 2014-08-13 10:47:21 -0700 | [diff] [blame] | 17 | public class PacketMatch implements Match { |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 18 | |
| 19 | // Match fields |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 20 | private final MACAddress srcMac; |
| 21 | private final MACAddress dstMac; |
| 22 | private final Short etherType; |
| 23 | private final IPv4Net srcIp; |
| 24 | private final IPv4Net dstIp; |
| 25 | private final Byte ipProto; |
| 26 | private final Short srcTcpPort; |
| 27 | private final Short dstTcpPort; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 30 | * Default constructor for Kryo deserialization. |
| 31 | */ |
| 32 | @Deprecated |
| 33 | protected PacketMatch() { |
| 34 | srcMac = null; |
| 35 | dstMac = null; |
| 36 | etherType = null; |
| 37 | srcIp = null; |
| 38 | dstIp = null; |
| 39 | ipProto = null; |
| 40 | srcTcpPort = null; |
| 41 | dstTcpPort = null; |
| 42 | } |
| 43 | |
| 44 | /** |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 45 | * Package private constructor. |
| 46 | * <p> |
| 47 | * This class should be instantiated by the builder. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 48 | * |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 49 | * @param srcMac the source host MAC address |
| 50 | * @param dstMac the destination host MAC address |
| 51 | * @param etherType the Ether type |
| 52 | * @param srcIp the source IP address with IP prefix |
| 53 | * @param dstIp the destination IP address with IP prefix |
| 54 | * @param ipProto |
| 55 | * @param srcTcpPort the source TCP port number |
| 56 | * @param dstTcpPort the destination TCP port number |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 57 | */ |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 58 | PacketMatch(MACAddress srcMac, MACAddress dstMac, |
| 59 | Short etherType, |
| 60 | IPv4Net srcIp, IPv4Net dstIp, Byte ipProto, |
| 61 | Short srcTcpPort, Short dstTcpPort) { |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 62 | this.srcMac = srcMac; |
| 63 | this.dstMac = dstMac; |
| 64 | this.etherType = etherType; |
| 65 | this.srcIp = srcIp; |
| 66 | this.dstIp = dstIp; |
| 67 | this.ipProto = ipProto; |
| 68 | this.srcTcpPort = srcTcpPort; |
| 69 | this.dstTcpPort = dstTcpPort; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Gets the source host MAC address. |
| 74 | * |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 75 | * @return the source host MAC address |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 76 | */ |
| 77 | public MACAddress getSrcMacAddress() { |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 78 | return srcMac; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets the destination host MAC address. |
| 83 | * |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 84 | * @return the destination host MAC address |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 85 | */ |
| 86 | public MACAddress getDstMacAddress() { |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 87 | return dstMac; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets the Ether type. |
| 92 | * |
| 93 | * @return the Ether type |
| 94 | */ |
| 95 | public Short getEtherType() { |
| 96 | return etherType; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the source host IP address. |
| 101 | * |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 102 | * @return the source host IP address |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 103 | */ |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 104 | public IPv4Net getSrcIpAddress() { |
| 105 | return srcIp; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Gets the destination host IP address. |
| 110 | * |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 111 | * @return the destination host IP address |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 112 | */ |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 113 | public IPv4Net getDstIpAddress() { |
| 114 | return dstIp; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Gets the IP protocol number. |
| 119 | * |
| 120 | * @return the IP protocol number |
| 121 | */ |
| 122 | public Byte getIpProtocolNumber() { |
| 123 | return ipProto; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Gets the source TCP port number. |
| 128 | * |
| 129 | * @return the source TCP port number |
| 130 | */ |
| 131 | public Short getSrcTcpPortNumber() { |
| 132 | return srcTcpPort; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Gets the destination TCP port number. |
| 137 | * |
| 138 | * @return the destination TCP port number |
| 139 | */ |
| 140 | public Short getDstTcpPortNumber() { |
| 141 | return dstTcpPort; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 142 | } |
Sho SHIMIZU | cdc5013 | 2014-07-22 09:21:01 -0700 | [diff] [blame] | 143 | |
| 144 | @Override |
| 145 | public int hashCode() { |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 146 | return Objects.hashCode(srcMac, dstMac, etherType, |
| 147 | srcIp, dstIp, ipProto, |
| 148 | srcTcpPort, dstTcpPort); |
Sho SHIMIZU | cdc5013 | 2014-07-22 09:21:01 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean equals(Object obj) { |
| 153 | if (obj == this) { |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | if (!(obj instanceof PacketMatch)) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | PacketMatch that = (PacketMatch) obj; |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 162 | return Objects.equal(this.srcMac, that.srcMac) |
| 163 | && Objects.equal(this.dstMac, that.dstMac) |
| 164 | && Objects.equal(this.etherType, that.etherType) |
| 165 | && Objects.equal(this.srcIp, that.srcIp) |
| 166 | && Objects.equal(this.dstIp, that.dstIp) |
| 167 | && Objects.equal(this.ipProto, that.ipProto) |
| 168 | && Objects.equal(this.srcTcpPort, that.srcTcpPort) |
| 169 | && Objects.equal(this.dstTcpPort, that.dstTcpPort); |
| 170 | } |
| 171 | |
| 172 | private Integer toUnsignedInt(Byte number) { |
| 173 | return number == null ? null : Integer.valueOf(number & 0xFF); |
| 174 | } |
| 175 | |
| 176 | private Integer toUnsignedInt(Short number) { |
| 177 | return number == null ? null : Integer.valueOf(number & 0xFFFF); |
Sho SHIMIZU | cdc5013 | 2014-07-22 09:21:01 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public String toString() { |
| 182 | return Objects.toStringHelper(this) |
Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 183 | .add("srcMac", srcMac) |
| 184 | .add("dstMac", dstMac) |
| 185 | .add("etherType", toUnsignedInt(etherType)) |
| 186 | .add("srcIp", srcIp) |
| 187 | .add("dstIp", dstIp) |
| 188 | .add("ipProto", toUnsignedInt(ipProto)) |
| 189 | .add("srcTcpPort", toUnsignedInt(srcTcpPort)) |
| 190 | .add("dstTcpPort", toUnsignedInt(dstTcpPort)) |
Sho SHIMIZU | cdc5013 | 2014-07-22 09:21:01 -0700 | [diff] [blame] | 191 | .toString(); |
| 192 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 193 | } |