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