blob: 0731395f9b1743bbec202c24d2846b1d69a6958d [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;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080011
12/**
Brian O'Connora84723c2014-06-13 00:26:49 -070013 * A class to represent the OpenFlow match.
14 * <p>
15 * At the moment, the supported match conditions are:
16 * <ul>
17 * <li>source port
18 * <li>source and destination MAC address
19 * <li>source and destination IP address
20 * </ul>
21 * <p>
22 * TODO: extend this object to allow more match conditions
Brian O'Connor7f8e3012014-02-15 23:59:29 -080023 */
24
25public class Match {
Komal Shah399a2922014-05-28 01:57:40 -070026 private static final short IPV4_PREFIX_LEN = 32;
Ray Milkey269ffb92014-04-03 14:43:30 -070027 protected long sw;
28 protected MACAddress srcMac;
29 protected MACAddress dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070030 protected int srcIp;
31 protected int dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070032 protected long srcPort;
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080033
Brian O'Connora84723c2014-06-13 00:26:49 -070034 /**
35 * Constructor for Ethernet-based matches.
36 *
37 * @param sw switch's DPID
38 * @param srcPort source port on switch
39 * @param srcMac source Ethernet MAC address
40 * @param dstMac destination Ethernet MAC address
41 */
Ray Milkey269ffb92014-04-03 14:43:30 -070042 public Match(long sw, long srcPort,
43 MACAddress srcMac, MACAddress dstMac) {
Komal Shah399a2922014-05-28 01:57:40 -070044 this(sw, srcPort, srcMac, dstMac, ShortestPathIntent.EMPTYIPADDRESS, ShortestPathIntent.EMPTYIPADDRESS);
45 }
46
Brian O'Connora84723c2014-06-13 00:26:49 -070047 /**
48 * Generic constructor.
49 *
50 * @param sw switch's DPID
51 * @param srcPort source port on switch
52 * @param srcMac source Ethernet MAC address
53 * @param dstMac destination Ethernet MAC address
54 * @param srcIp source IP address
55 * @param dstIp destination IP address
56 */
Komal Shah399a2922014-05-28 01:57:40 -070057 public Match(long sw, long srcPort, MACAddress srcMac, MACAddress dstMac,
58 int srcIp, int dstIp) {
59
Ray Milkey269ffb92014-04-03 14:43:30 -070060 this.sw = sw;
61 this.srcPort = srcPort;
62 this.srcMac = srcMac;
63 this.dstMac = dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070064 this.srcIp = srcIp;
65 this.dstIp = dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070066 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080067
Brian O'Connora84723c2014-06-13 00:26:49 -070068 /**
69 * Matches are equal if all object variables are equal.
70 *
71 * @return true if equal, false otherwise
72 */
Ray Milkey269ffb92014-04-03 14:43:30 -070073 @Override
74 public boolean equals(Object obj) {
75 if (obj instanceof Match) {
76 Match other = (Match) obj;
Brian O'Connora84723c2014-06-13 00:26:49 -070077 //TODO: we might consider excluding sw from this comparison
Komal Shah399a2922014-05-28 01:57:40 -070078 if (this.sw != other.sw) {
79 return false;
80 }
81 if (!Objects.equals(srcMac, other.srcMac)) {
82 return false;
83 }
84 if (!Objects.equals(dstMac, other.dstMac)) {
85 return false;
86 }
87 if (srcIp != other.srcIp) {
88 return false;
89 }
90
91 if (dstIp != other.dstIp) {
92 return false;
93 }
94 if (this.srcPort != other.srcPort) {
95 return false;
96 }
97 return true;
Ray Milkey269ffb92014-04-03 14:43:30 -070098 } else {
99 return false;
100 }
101 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -0800102
Brian O'Connora84723c2014-06-13 00:26:49 -0700103 /**
104 * Converts the Match into a legacy FlowEntryMatch object.
105 *
106 * @return an equivalent FlowEntryMatch object
107 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 public FlowEntryMatch getFlowEntryMatch() {
109 FlowEntryMatch match = new FlowEntryMatch();
Komal Shah399a2922014-05-28 01:57:40 -0700110 if (srcMac != null) {
111 match.enableSrcMac(srcMac);
112 }
113 if (dstMac != null) {
114 match.enableDstMac(dstMac);
115 }
116 if (srcIp != ShortestPathIntent.EMPTYIPADDRESS) {
117 match.enableEthernetFrameType(Ethernet.TYPE_IPV4);
118 IPv4 srcIPv4 = new IPv4(srcIp);
119 IPv4Net srcIP = new IPv4Net(srcIPv4, IPV4_PREFIX_LEN);
120 match.enableSrcIPv4Net(srcIP);
121 }
122 if (dstIp != ShortestPathIntent.EMPTYIPADDRESS) {
123 match.enableEthernetFrameType(Ethernet.TYPE_IPV4);
124 IPv4 dstIPv4 = new IPv4(dstIp);
125 IPv4Net dstIP = new IPv4Net(dstIPv4, IPV4_PREFIX_LEN);
126 match.enableDstIPv4Net(dstIP);
127 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 match.enableInPort(new net.onrc.onos.core.util.Port((short) srcPort));
129 return match;
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() {
Komal Shah399a2922014-05-28 01:57:40 -0700139 return "Sw:" + sw + " (" + srcPort + "," + srcMac + "," + dstMac + "," + srcIp + "," + dstIp + ")";
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 }
141
Brian O'Connora84723c2014-06-13 00:26:49 -0700142 /**
143 * Generates hash using Objects.hash() to hash all object variables.
144 *
145 * @return hashcode
146 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 @Override
148 public int hashCode() {
Brian O'Connora84723c2014-06-13 00:26:49 -0700149 //TODO: we might consider excluding sw from the hash function
150 // to make it easier to compare matches between switches
Komal Shah399a2922014-05-28 01:57:40 -0700151 return Objects.hash(sw, srcPort, srcMac, dstMac, srcIp, dstIp);
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 }
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800153}