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