blob: cddd6dead3bf11d35812c3d38e8506422b7bc7e6 [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 -07003
4import java.util.Objects;
Brian O'Connor6dc44e92014-02-24 21:23:46 -08005
Brian O'Connor7f8e3012014-02-15 23:59:29 -08006import net.floodlightcontroller.util.MACAddress;
Komal Shah399a2922014-05-28 01:57:40 -07007import net.onrc.onos.core.packet.Ethernet;
Jonathan Hart23701d12014-04-03 10:45:48 -07008import net.onrc.onos.core.util.FlowEntryMatch;
Komal Shah399a2922014-05-28 01:57:40 -07009import net.onrc.onos.core.util.IPv4;
10import net.onrc.onos.core.util.IPv4Net;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070011import net.onrc.onos.core.util.PortNumber;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080012
13/**
Brian O'Connora84723c2014-06-13 00:26:49 -070014 * A class to represent the OpenFlow match.
15 * <p>
16 * At the moment, the supported match conditions are:
17 * <ul>
18 * <li>source port
19 * <li>source and destination MAC address
20 * <li>source and destination IP address
21 * </ul>
22 * <p>
23 * TODO: extend this object to allow more match conditions
Brian O'Connor7f8e3012014-02-15 23:59:29 -080024 */
25
26public class Match {
Komal Shah399a2922014-05-28 01:57:40 -070027 private static final short IPV4_PREFIX_LEN = 32;
Ray Milkey269ffb92014-04-03 14:43:30 -070028 protected long sw;
29 protected MACAddress srcMac;
30 protected MACAddress dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070031 protected int srcIp;
32 protected int dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070033 protected long srcPort;
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080034
Brian O'Connora84723c2014-06-13 00:26:49 -070035 /**
36 * Constructor for Ethernet-based matches.
37 *
38 * @param sw switch's DPID
39 * @param srcPort source port on switch
40 * @param srcMac source Ethernet MAC address
41 * @param dstMac destination Ethernet MAC address
42 */
Ray Milkey269ffb92014-04-03 14:43:30 -070043 public Match(long sw, long srcPort,
44 MACAddress srcMac, MACAddress dstMac) {
Komal Shah399a2922014-05-28 01:57:40 -070045 this(sw, srcPort, srcMac, dstMac, ShortestPathIntent.EMPTYIPADDRESS, ShortestPathIntent.EMPTYIPADDRESS);
46 }
47
Brian O'Connora84723c2014-06-13 00:26:49 -070048 /**
49 * Generic constructor.
50 *
51 * @param sw switch's DPID
52 * @param srcPort source port on switch
53 * @param srcMac source Ethernet MAC address
54 * @param dstMac destination Ethernet MAC address
55 * @param srcIp source IP address
56 * @param dstIp destination IP address
57 */
Komal Shah399a2922014-05-28 01:57:40 -070058 public Match(long sw, long srcPort, MACAddress srcMac, MACAddress dstMac,
59 int srcIp, int dstIp) {
60
Ray Milkey269ffb92014-04-03 14:43:30 -070061 this.sw = sw;
62 this.srcPort = srcPort;
63 this.srcMac = srcMac;
64 this.dstMac = dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070065 this.srcIp = srcIp;
66 this.dstIp = dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070067 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080068
Brian O'Connora84723c2014-06-13 00:26:49 -070069 /**
70 * Matches are equal if all object variables are equal.
71 *
72 * @return true if equal, false otherwise
73 */
Ray Milkey269ffb92014-04-03 14:43:30 -070074 @Override
75 public boolean equals(Object obj) {
76 if (obj instanceof Match) {
77 Match other = (Match) obj;
Brian O'Connora84723c2014-06-13 00:26:49 -070078 //TODO: we might consider excluding sw from this comparison
Komal Shah399a2922014-05-28 01:57:40 -070079 if (this.sw != other.sw) {
80 return false;
81 }
82 if (!Objects.equals(srcMac, other.srcMac)) {
83 return false;
84 }
85 if (!Objects.equals(dstMac, other.dstMac)) {
86 return false;
87 }
88 if (srcIp != other.srcIp) {
89 return false;
90 }
91
92 if (dstIp != other.dstIp) {
93 return false;
94 }
95 if (this.srcPort != other.srcPort) {
96 return false;
97 }
98 return true;
Ray Milkey269ffb92014-04-03 14:43:30 -070099 } else {
100 return false;
101 }
102 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -0800103
Brian O'Connora84723c2014-06-13 00:26:49 -0700104 /**
105 * Converts the Match into a legacy FlowEntryMatch object.
106 *
107 * @return an equivalent FlowEntryMatch object
108 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 public FlowEntryMatch getFlowEntryMatch() {
110 FlowEntryMatch match = new FlowEntryMatch();
Komal Shah399a2922014-05-28 01:57:40 -0700111 if (srcMac != null) {
112 match.enableSrcMac(srcMac);
113 }
114 if (dstMac != null) {
115 match.enableDstMac(dstMac);
116 }
117 if (srcIp != ShortestPathIntent.EMPTYIPADDRESS) {
118 match.enableEthernetFrameType(Ethernet.TYPE_IPV4);
119 IPv4 srcIPv4 = new IPv4(srcIp);
120 IPv4Net srcIP = new IPv4Net(srcIPv4, IPV4_PREFIX_LEN);
121 match.enableSrcIPv4Net(srcIP);
122 }
123 if (dstIp != ShortestPathIntent.EMPTYIPADDRESS) {
124 match.enableEthernetFrameType(Ethernet.TYPE_IPV4);
125 IPv4 dstIPv4 = new IPv4(dstIp);
126 IPv4Net dstIP = new IPv4Net(dstIPv4, IPV4_PREFIX_LEN);
127 match.enableDstIPv4Net(dstIP);
128 }
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700129 match.enableInPort(new PortNumber((short) srcPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 return match;
131 }
132
Brian O'Connora84723c2014-06-13 00:26:49 -0700133 /**
134 * Returns a String representation of this Match.
135 *
136 * @return the match as a String
137 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 @Override
139 public String toString() {
Komal Shah399a2922014-05-28 01:57:40 -0700140 return "Sw:" + sw + " (" + srcPort + "," + srcMac + "," + dstMac + "," + srcIp + "," + dstIp + ")";
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 }
142
Brian O'Connora84723c2014-06-13 00:26:49 -0700143 /**
144 * Generates hash using Objects.hash() to hash all object variables.
145 *
146 * @return hashcode
147 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 @Override
149 public int hashCode() {
Brian O'Connora84723c2014-06-13 00:26:49 -0700150 //TODO: we might consider excluding sw from the hash function
151 // to make it easier to compare matches between switches
Komal Shah399a2922014-05-28 01:57:40 -0700152 return Objects.hash(sw, srcPort, srcMac, dstMac, srcIp, dstIp);
Ray Milkey269ffb92014-04-03 14:43:30 -0700153 }
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800154}