blob: c9b1f9d61d9a83455aa1e0865ccebbaea1f3f737 [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 Koide80db1842014-08-11 17:08:32 -070029 * Package private constructor.
30 * <p>
31 * This class should be instantiated by the builder.
Toshio Koidea03915e2014-07-01 18:39:52 -070032 *
Toshio Koide80db1842014-08-11 17:08:32 -070033 * @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 Koidea03915e2014-07-01 18:39:52 -070041 */
Toshio Koide80db1842014-08-11 17:08:32 -070042 PacketMatch(MACAddress srcMac, MACAddress dstMac,
43 Short etherType,
44 IPv4Net srcIp, IPv4Net dstIp, Byte ipProto,
45 Short srcTcpPort, Short dstTcpPort) {
Toshio Koide80db1842014-08-11 17:08:32 -070046 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 Koidea03915e2014-07-01 18:39:52 -070054 }
55
56 /**
57 * Gets the source host MAC address.
58 *
Toshio Koide80db1842014-08-11 17:08:32 -070059 * @return the source host MAC address
Toshio Koidea03915e2014-07-01 18:39:52 -070060 */
61 public MACAddress getSrcMacAddress() {
Toshio Koide80db1842014-08-11 17:08:32 -070062 return srcMac;
Toshio Koidea03915e2014-07-01 18:39:52 -070063 }
64
65 /**
66 * Gets the destination host MAC address.
67 *
Toshio Koide80db1842014-08-11 17:08:32 -070068 * @return the destination host MAC address
Toshio Koidea03915e2014-07-01 18:39:52 -070069 */
70 public MACAddress getDstMacAddress() {
Toshio Koide80db1842014-08-11 17:08:32 -070071 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 Koidea03915e2014-07-01 18:39:52 -070081 }
82
83 /**
84 * Gets the source host IP address.
85 *
Toshio Koide80db1842014-08-11 17:08:32 -070086 * @return the source host IP address
Toshio Koidea03915e2014-07-01 18:39:52 -070087 */
Toshio Koide80db1842014-08-11 17:08:32 -070088 public IPv4Net getSrcIpAddress() {
89 return srcIp;
Toshio Koidea03915e2014-07-01 18:39:52 -070090 }
91
92 /**
93 * Gets the destination host IP address.
94 *
Toshio Koide80db1842014-08-11 17:08:32 -070095 * @return the destination host IP address
Toshio Koidea03915e2014-07-01 18:39:52 -070096 */
Toshio Koide80db1842014-08-11 17:08:32 -070097 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 Koidea03915e2014-07-01 18:39:52 -0700126 }
Sho SHIMIZUcdc50132014-07-22 09:21:01 -0700127
128 @Override
129 public int hashCode() {
Toshio Koide80db1842014-08-11 17:08:32 -0700130 return Objects.hashCode(srcMac, dstMac, etherType,
131 srcIp, dstIp, ipProto,
132 srcTcpPort, dstTcpPort);
Sho SHIMIZUcdc50132014-07-22 09:21:01 -0700133 }
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 Koide80db1842014-08-11 17:08:32 -0700146 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 SHIMIZUcdc50132014-07-22 09:21:01 -0700162 }
163
164 @Override
165 public String toString() {
166 return Objects.toStringHelper(this)
Toshio Koide80db1842014-08-11 17:08:32 -0700167 .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 SHIMIZUcdc50132014-07-22 09:21:01 -0700175 .toString();
176 }
Toshio Koidea03915e2014-07-01 18:39:52 -0700177}