blob: c49c3d8bb24022ba9207a39cb57bfbf27bf38011 [file] [log] [blame]
Toshio Koidea03915e2014-07-01 18:39:52 -07001package net.onrc.onos.core.matchaction.match;
2
3import net.floodlightcontroller.util.MACAddress;
Toshio Koide80db1842014-08-11 17:08:32 -07004import net.onrc.onos.core.util.IPv4Net;
5
6import com.google.common.base.Objects;
Toshio Koidea03915e2014-07-01 18:39:52 -07007
8/**
9 * A match object (traffic specifier) for packet nodes, flow-paths and intents.
10 * <p>
Toshio Koide80db1842014-08-11 17:08:32 -070011 * 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 Koidea03915e2014-07-01 18:39:52 -070015 */
Toshio Koided8b077a2014-08-13 10:47:21 -070016public class PacketMatch implements Match {
Toshio Koidea03915e2014-07-01 18:39:52 -070017
18 // Match fields
Toshio Koide80db1842014-08-11 17:08:32 -070019 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 Koidea03915e2014-07-01 18:39:52 -070027
28 /**
Toshio Koide2c67a2d2014-08-27 11:30:56 -070029 * 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 Koide80db1842014-08-11 17:08:32 -070044 * Package private constructor.
45 * <p>
46 * This class should be instantiated by the builder.
Toshio Koidea03915e2014-07-01 18:39:52 -070047 *
Toshio Koide80db1842014-08-11 17:08:32 -070048 * @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 Koidea03915e2014-07-01 18:39:52 -070056 */
Toshio Koide80db1842014-08-11 17:08:32 -070057 PacketMatch(MACAddress srcMac, MACAddress dstMac,
58 Short etherType,
59 IPv4Net srcIp, IPv4Net dstIp, Byte ipProto,
60 Short srcTcpPort, Short dstTcpPort) {
Toshio Koide80db1842014-08-11 17:08:32 -070061 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 Koidea03915e2014-07-01 18:39:52 -070069 }
70
71 /**
72 * Gets the source host MAC address.
73 *
Toshio Koide80db1842014-08-11 17:08:32 -070074 * @return the source host MAC address
Toshio Koidea03915e2014-07-01 18:39:52 -070075 */
76 public MACAddress getSrcMacAddress() {
Toshio Koide80db1842014-08-11 17:08:32 -070077 return srcMac;
Toshio Koidea03915e2014-07-01 18:39:52 -070078 }
79
80 /**
81 * Gets the destination host MAC address.
82 *
Toshio Koide80db1842014-08-11 17:08:32 -070083 * @return the destination host MAC address
Toshio Koidea03915e2014-07-01 18:39:52 -070084 */
85 public MACAddress getDstMacAddress() {
Toshio Koide80db1842014-08-11 17:08:32 -070086 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 Koidea03915e2014-07-01 18:39:52 -070096 }
97
98 /**
99 * Gets the source host IP address.
100 *
Toshio Koide80db1842014-08-11 17:08:32 -0700101 * @return the source host IP address
Toshio Koidea03915e2014-07-01 18:39:52 -0700102 */
Toshio Koide80db1842014-08-11 17:08:32 -0700103 public IPv4Net getSrcIpAddress() {
104 return srcIp;
Toshio Koidea03915e2014-07-01 18:39:52 -0700105 }
106
107 /**
108 * Gets the destination host IP address.
109 *
Toshio Koide80db1842014-08-11 17:08:32 -0700110 * @return the destination host IP address
Toshio Koidea03915e2014-07-01 18:39:52 -0700111 */
Toshio Koide80db1842014-08-11 17:08:32 -0700112 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 Koidea03915e2014-07-01 18:39:52 -0700141 }
Sho SHIMIZUcdc50132014-07-22 09:21:01 -0700142
143 @Override
144 public int hashCode() {
Toshio Koide80db1842014-08-11 17:08:32 -0700145 return Objects.hashCode(srcMac, dstMac, etherType,
146 srcIp, dstIp, ipProto,
147 srcTcpPort, dstTcpPort);
Sho SHIMIZUcdc50132014-07-22 09:21:01 -0700148 }
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 Koide80db1842014-08-11 17:08:32 -0700161 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 SHIMIZUcdc50132014-07-22 09:21:01 -0700177 }
178
179 @Override
180 public String toString() {
181 return Objects.toStringHelper(this)
Toshio Koide80db1842014-08-11 17:08:32 -0700182 .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 SHIMIZUcdc50132014-07-22 09:21:01 -0700190 .toString();
191 }
Toshio Koidea03915e2014-07-01 18:39:52 -0700192}