Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 2 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 3 | |
| 4 | import java.util.Objects; |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 5 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 6 | import net.floodlightcontroller.util.MACAddress; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.packet.Ethernet; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.util.FlowEntryMatch; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.IPv4; |
| 10 | import net.onrc.onos.core.util.IPv4Net; |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.util.PortNumber; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 12 | |
| 13 | /** |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 14 | * 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'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | public class Match { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 27 | private static final short IPV4_PREFIX_LEN = 32; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | protected long sw; |
| 29 | protected MACAddress srcMac; |
| 30 | protected MACAddress dstMac; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 31 | protected int srcIp; |
| 32 | protected int dstIp; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | protected long srcPort; |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 34 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 35 | /** |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | public Match(long sw, long srcPort, |
| 44 | MACAddress srcMac, MACAddress dstMac) { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 45 | this(sw, srcPort, srcMac, dstMac, ShortestPathIntent.EMPTYIPADDRESS, ShortestPathIntent.EMPTYIPADDRESS); |
| 46 | } |
| 47 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 48 | /** |
| 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 Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 58 | public Match(long sw, long srcPort, MACAddress srcMac, MACAddress dstMac, |
| 59 | int srcIp, int dstIp) { |
| 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | this.sw = sw; |
| 62 | this.srcPort = srcPort; |
| 63 | this.srcMac = srcMac; |
| 64 | this.dstMac = dstMac; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 65 | this.srcIp = srcIp; |
| 66 | this.dstIp = dstIp; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | } |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 68 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Matches are equal if all object variables are equal. |
| 71 | * |
| 72 | * @return true if equal, false otherwise |
| 73 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public boolean equals(Object obj) { |
| 76 | if (obj instanceof Match) { |
| 77 | Match other = (Match) obj; |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 78 | //TODO: we might consider excluding sw from this comparison |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 79 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | } else { |
| 100 | return false; |
| 101 | } |
| 102 | } |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 103 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Converts the Match into a legacy FlowEntryMatch object. |
| 106 | * |
| 107 | * @return an equivalent FlowEntryMatch object |
| 108 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | public FlowEntryMatch getFlowEntryMatch() { |
| 110 | FlowEntryMatch match = new FlowEntryMatch(); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 111 | 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 HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 129 | match.enableInPort(new PortNumber((short) srcPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | return match; |
| 131 | } |
| 132 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Returns a String representation of this Match. |
| 135 | * |
| 136 | * @return the match as a String |
| 137 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | @Override |
| 139 | public String toString() { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 140 | return "Sw:" + sw + " (" + srcPort + "," + srcMac + "," + dstMac + "," + srcIp + "," + dstIp + ")"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 143 | /** |
| 144 | * Generates hash using Objects.hash() to hash all object variables. |
| 145 | * |
| 146 | * @return hashcode |
| 147 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public int hashCode() { |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 150 | //TODO: we might consider excluding sw from the hash function |
| 151 | // to make it easier to compare matches between switches |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 152 | return Objects.hash(sw, srcPort, srcMac, dstMac, srcIp, dstIp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 153 | } |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 154 | } |