blob: 6974a823807667488a504f403cbd1b7115829714 [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
Saurav Dase972b3a2014-09-26 15:41:06 -070012 * 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 Koide80db1842014-08-11 17:08:32 -070014 * <p>
15 * TODO: This class should be extensible.
Toshio Koidea03915e2014-07-01 18:39:52 -070016 */
Toshio Koided8b077a2014-08-13 10:47:21 -070017public class PacketMatch implements Match {
Toshio Koidea03915e2014-07-01 18:39:52 -070018
19 // Match fields
Toshio Koide80db1842014-08-11 17:08:32 -070020 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 Koidea03915e2014-07-01 18:39:52 -070028
29 /**
Toshio Koide2c67a2d2014-08-27 11:30:56 -070030 * 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 Koide80db1842014-08-11 17:08:32 -070045 * Package private constructor.
46 * <p>
47 * This class should be instantiated by the builder.
Toshio Koidea03915e2014-07-01 18:39:52 -070048 *
Toshio Koide80db1842014-08-11 17:08:32 -070049 * @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 Koidea03915e2014-07-01 18:39:52 -070057 */
Toshio Koide80db1842014-08-11 17:08:32 -070058 PacketMatch(MACAddress srcMac, MACAddress dstMac,
59 Short etherType,
60 IPv4Net srcIp, IPv4Net dstIp, Byte ipProto,
61 Short srcTcpPort, Short dstTcpPort) {
Toshio Koide80db1842014-08-11 17:08:32 -070062 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 Koidea03915e2014-07-01 18:39:52 -070070 }
71
72 /**
73 * Gets the source host MAC address.
74 *
Toshio Koide80db1842014-08-11 17:08:32 -070075 * @return the source host MAC address
Toshio Koidea03915e2014-07-01 18:39:52 -070076 */
77 public MACAddress getSrcMacAddress() {
Toshio Koide80db1842014-08-11 17:08:32 -070078 return srcMac;
Toshio Koidea03915e2014-07-01 18:39:52 -070079 }
80
81 /**
82 * Gets the destination host MAC address.
83 *
Toshio Koide80db1842014-08-11 17:08:32 -070084 * @return the destination host MAC address
Toshio Koidea03915e2014-07-01 18:39:52 -070085 */
86 public MACAddress getDstMacAddress() {
Toshio Koide80db1842014-08-11 17:08:32 -070087 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 Koidea03915e2014-07-01 18:39:52 -070097 }
98
99 /**
100 * Gets the source host IP address.
101 *
Toshio Koide80db1842014-08-11 17:08:32 -0700102 * @return the source host IP address
Toshio Koidea03915e2014-07-01 18:39:52 -0700103 */
Toshio Koide80db1842014-08-11 17:08:32 -0700104 public IPv4Net getSrcIpAddress() {
105 return srcIp;
Toshio Koidea03915e2014-07-01 18:39:52 -0700106 }
107
108 /**
109 * Gets the destination host IP address.
110 *
Toshio Koide80db1842014-08-11 17:08:32 -0700111 * @return the destination host IP address
Toshio Koidea03915e2014-07-01 18:39:52 -0700112 */
Toshio Koide80db1842014-08-11 17:08:32 -0700113 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 Koidea03915e2014-07-01 18:39:52 -0700142 }
Sho SHIMIZUcdc50132014-07-22 09:21:01 -0700143
144 @Override
145 public int hashCode() {
Toshio Koide80db1842014-08-11 17:08:32 -0700146 return Objects.hashCode(srcMac, dstMac, etherType,
147 srcIp, dstIp, ipProto,
148 srcTcpPort, dstTcpPort);
Sho SHIMIZUcdc50132014-07-22 09:21:01 -0700149 }
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 Koide80db1842014-08-11 17:08:32 -0700162 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 SHIMIZUcdc50132014-07-22 09:21:01 -0700178 }
179
180 @Override
181 public String toString() {
182 return Objects.toStringHelper(this)
Toshio Koide80db1842014-08-11 17:08:32 -0700183 .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 SHIMIZUcdc50132014-07-22 09:21:01 -0700191 .toString();
192 }
Toshio Koidea03915e2014-07-01 18:39:52 -0700193}