blob: 6397de84d7f034958263bb8f25def7ecda75c018 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Brian O'Connor7f8e3012014-02-15 23:59:29 -08002
Komal Shah399a2922014-05-28 01:57:40 -07003import java.util.Objects;
Brian O'Connor6dc44e92014-02-24 21:23:46 -08004
Brian O'Connor7f8e3012014-02-15 23:59:29 -08005import net.floodlightcontroller.util.MACAddress;
Brian O'Connor7f8e3012014-02-15 23:59:29 -08006
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07007import org.projectfloodlight.openflow.protocol.OFFactory;
8import org.projectfloodlight.openflow.protocol.match.Match.Builder;
9import org.projectfloodlight.openflow.protocol.match.MatchField;
10import org.projectfloodlight.openflow.types.EthType;
Matteo Gerolae95f4ea2014-08-06 16:15:01 +020011import org.projectfloodlight.openflow.types.IPv4Address;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070012import org.projectfloodlight.openflow.types.MacAddress;
13import org.projectfloodlight.openflow.types.OFPort;
14
Brian O'Connor7f8e3012014-02-15 23:59:29 -080015/**
Brian O'Connora84723c2014-06-13 00:26:49 -070016 * A class to represent the OpenFlow match.
17 * <p>
18 * At the moment, the supported match conditions are:
19 * <ul>
20 * <li>source port
21 * <li>source and destination MAC address
22 * <li>source and destination IP address
23 * </ul>
24 * <p>
25 * TODO: extend this object to allow more match conditions
Brian O'Connor7f8e3012014-02-15 23:59:29 -080026 */
27
28public class Match {
Komal Shah399a2922014-05-28 01:57:40 -070029 private static final short IPV4_PREFIX_LEN = 32;
Ray Milkey269ffb92014-04-03 14:43:30 -070030 protected long sw;
31 protected MACAddress srcMac;
32 protected MACAddress dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070033 protected int srcIp;
34 protected int dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070035 protected long srcPort;
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080036
Brian O'Connora84723c2014-06-13 00:26:49 -070037 /**
38 * Constructor for Ethernet-based matches.
39 *
40 * @param sw switch's DPID
41 * @param srcPort source port on switch
42 * @param srcMac source Ethernet MAC address
43 * @param dstMac destination Ethernet MAC address
44 */
Ray Milkey269ffb92014-04-03 14:43:30 -070045 public Match(long sw, long srcPort,
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070046 MACAddress srcMac, MACAddress dstMac) {
47 this(sw, srcPort, srcMac, dstMac,
48 ShortestPathIntent.EMPTYIPADDRESS,
49 ShortestPathIntent.EMPTYIPADDRESS);
Komal Shah399a2922014-05-28 01:57:40 -070050 }
51
Brian O'Connora84723c2014-06-13 00:26:49 -070052 /**
53 * Generic constructor.
54 *
55 * @param sw switch's DPID
56 * @param srcPort source port on switch
57 * @param srcMac source Ethernet MAC address
58 * @param dstMac destination Ethernet MAC address
59 * @param srcIp source IP address
60 * @param dstIp destination IP address
61 */
Komal Shah399a2922014-05-28 01:57:40 -070062 public Match(long sw, long srcPort, MACAddress srcMac, MACAddress dstMac,
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070063 int srcIp, int dstIp) {
Komal Shah399a2922014-05-28 01:57:40 -070064
Ray Milkey269ffb92014-04-03 14:43:30 -070065 this.sw = sw;
66 this.srcPort = srcPort;
67 this.srcMac = srcMac;
68 this.dstMac = dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070069 this.srcIp = srcIp;
70 this.dstIp = dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070071 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080072
Brian O'Connora84723c2014-06-13 00:26:49 -070073 /**
74 * Matches are equal if all object variables are equal.
75 *
76 * @return true if equal, false otherwise
77 */
Ray Milkey269ffb92014-04-03 14:43:30 -070078 @Override
79 public boolean equals(Object obj) {
80 if (obj instanceof Match) {
81 Match other = (Match) obj;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070082 // TODO: we might consider excluding sw from this comparison
Komal Shah399a2922014-05-28 01:57:40 -070083 if (this.sw != other.sw) {
84 return false;
85 }
86 if (!Objects.equals(srcMac, other.srcMac)) {
87 return false;
88 }
89 if (!Objects.equals(dstMac, other.dstMac)) {
90 return false;
91 }
92 if (srcIp != other.srcIp) {
93 return false;
94 }
95
96 if (dstIp != other.dstIp) {
97 return false;
98 }
99 if (this.srcPort != other.srcPort) {
100 return false;
101 }
102 return true;
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 } else {
104 return false;
105 }
106 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -0800107
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700108 public Builder getOFMatchBuilder(OFFactory factory) {
109 Builder matchBuilder = factory.buildMatch();
110
111 if (srcMac != null) {
112 matchBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(srcMac.toLong()));
113 }
114 if (dstMac != null) {
115 matchBuilder.setExact(MatchField.ETH_DST, MacAddress.of(dstMac.toLong()));
116 }
117 if (srcIp != ShortestPathIntent.EMPTYIPADDRESS) {
118 matchBuilder.setExact(MatchField.ETH_TYPE, EthType.IPv4)
Matteo Gerolae95f4ea2014-08-06 16:15:01 +0200119 .setMasked(MatchField.IPV4_SRC,
120 IPv4Address.of(srcIp).withMaskOfLength(IPV4_PREFIX_LEN));
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700121 }
122 if (dstIp != ShortestPathIntent.EMPTYIPADDRESS) {
123 matchBuilder.setExact(MatchField.ETH_TYPE, EthType.IPv4)
Matteo Gerolae95f4ea2014-08-06 16:15:01 +0200124 .setMasked(MatchField.IPV4_DST,
125 IPv4Address.of(dstIp).withMaskOfLength(IPV4_PREFIX_LEN));
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700126 }
127 matchBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) srcPort));
128
129 return matchBuilder;
130 }
131
Brian O'Connora84723c2014-06-13 00:26:49 -0700132 /**
133 * Returns a String representation of this Match.
134 *
135 * @return the match as a String
136 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 @Override
138 public String toString() {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700139 return "Sw:" + sw + " (" + srcPort + ","
140 + srcMac + "," + dstMac + ","
141 + srcIp + "," + dstIp + ")";
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 }
143
Brian O'Connora84723c2014-06-13 00:26:49 -0700144 /**
145 * Generates hash using Objects.hash() to hash all object variables.
146 *
147 * @return hashcode
148 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 @Override
150 public int hashCode() {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700151 // TODO: we might consider excluding sw from the hash function
152 // to make it easier to compare matches between switches
153 return Objects.hash(sw, srcPort, srcMac, dstMac, srcIp, dstIp);
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 }
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800155}