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 | import java.util.Objects; |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 4 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 5 | import net.floodlightcontroller.util.MACAddress; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.packet.Ethernet; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.util.FlowEntryMatch; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.util.IPv4; |
| 9 | import net.onrc.onos.core.util.IPv4Net; |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.util.PortNumber; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 11 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 12 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 13 | import org.projectfloodlight.openflow.protocol.match.Match.Builder; |
| 14 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 15 | import org.projectfloodlight.openflow.types.EthType; |
| 16 | import org.projectfloodlight.openflow.types.IPv4AddressWithMask; |
| 17 | import org.projectfloodlight.openflow.types.MacAddress; |
| 18 | import org.projectfloodlight.openflow.types.OFPort; |
| 19 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 20 | /** |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 21 | * A class to represent the OpenFlow match. |
| 22 | * <p> |
| 23 | * At the moment, the supported match conditions are: |
| 24 | * <ul> |
| 25 | * <li>source port |
| 26 | * <li>source and destination MAC address |
| 27 | * <li>source and destination IP address |
| 28 | * </ul> |
| 29 | * <p> |
| 30 | * TODO: extend this object to allow more match conditions |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | public class Match { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 34 | private static final short IPV4_PREFIX_LEN = 32; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | protected long sw; |
| 36 | protected MACAddress srcMac; |
| 37 | protected MACAddress dstMac; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 38 | protected int srcIp; |
| 39 | protected int dstIp; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | protected long srcPort; |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 41 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Constructor for Ethernet-based matches. |
| 44 | * |
| 45 | * @param sw switch's DPID |
| 46 | * @param srcPort source port on switch |
| 47 | * @param srcMac source Ethernet MAC address |
| 48 | * @param dstMac destination Ethernet MAC address |
| 49 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | public Match(long sw, long srcPort, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 51 | MACAddress srcMac, MACAddress dstMac) { |
| 52 | this(sw, srcPort, srcMac, dstMac, |
| 53 | ShortestPathIntent.EMPTYIPADDRESS, |
| 54 | ShortestPathIntent.EMPTYIPADDRESS); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Generic constructor. |
| 59 | * |
| 60 | * @param sw switch's DPID |
| 61 | * @param srcPort source port on switch |
| 62 | * @param srcMac source Ethernet MAC address |
| 63 | * @param dstMac destination Ethernet MAC address |
| 64 | * @param srcIp source IP address |
| 65 | * @param dstIp destination IP address |
| 66 | */ |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 67 | public Match(long sw, long srcPort, MACAddress srcMac, MACAddress dstMac, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 68 | int srcIp, int dstIp) { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 69 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | this.sw = sw; |
| 71 | this.srcPort = srcPort; |
| 72 | this.srcMac = srcMac; |
| 73 | this.dstMac = dstMac; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 74 | this.srcIp = srcIp; |
| 75 | this.dstIp = dstIp; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | } |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 77 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Matches are equal if all object variables are equal. |
| 80 | * |
| 81 | * @return true if equal, false otherwise |
| 82 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public boolean equals(Object obj) { |
| 85 | if (obj instanceof Match) { |
| 86 | Match other = (Match) obj; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 87 | // TODO: we might consider excluding sw from this comparison |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 88 | if (this.sw != other.sw) { |
| 89 | return false; |
| 90 | } |
| 91 | if (!Objects.equals(srcMac, other.srcMac)) { |
| 92 | return false; |
| 93 | } |
| 94 | if (!Objects.equals(dstMac, other.dstMac)) { |
| 95 | return false; |
| 96 | } |
| 97 | if (srcIp != other.srcIp) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | if (dstIp != other.dstIp) { |
| 102 | return false; |
| 103 | } |
| 104 | if (this.srcPort != other.srcPort) { |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | } else { |
| 109 | return false; |
| 110 | } |
| 111 | } |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 112 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Converts the Match into a legacy FlowEntryMatch object. |
| 115 | * |
| 116 | * @return an equivalent FlowEntryMatch object |
| 117 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | public FlowEntryMatch getFlowEntryMatch() { |
| 119 | FlowEntryMatch match = new FlowEntryMatch(); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 120 | if (srcMac != null) { |
| 121 | match.enableSrcMac(srcMac); |
| 122 | } |
| 123 | if (dstMac != null) { |
| 124 | match.enableDstMac(dstMac); |
| 125 | } |
| 126 | if (srcIp != ShortestPathIntent.EMPTYIPADDRESS) { |
| 127 | match.enableEthernetFrameType(Ethernet.TYPE_IPV4); |
| 128 | IPv4 srcIPv4 = new IPv4(srcIp); |
| 129 | IPv4Net srcIP = new IPv4Net(srcIPv4, IPV4_PREFIX_LEN); |
| 130 | match.enableSrcIPv4Net(srcIP); |
| 131 | } |
| 132 | if (dstIp != ShortestPathIntent.EMPTYIPADDRESS) { |
| 133 | match.enableEthernetFrameType(Ethernet.TYPE_IPV4); |
| 134 | IPv4 dstIPv4 = new IPv4(dstIp); |
| 135 | IPv4Net dstIP = new IPv4Net(dstIPv4, IPV4_PREFIX_LEN); |
| 136 | match.enableDstIPv4Net(dstIP); |
| 137 | } |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 138 | match.enableInPort(new PortNumber((short) srcPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | return match; |
| 140 | } |
| 141 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 142 | public Builder getOFMatchBuilder(OFFactory factory) { |
| 143 | Builder matchBuilder = factory.buildMatch(); |
| 144 | |
| 145 | if (srcMac != null) { |
| 146 | matchBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(srcMac.toLong())); |
| 147 | } |
| 148 | if (dstMac != null) { |
| 149 | matchBuilder.setExact(MatchField.ETH_DST, MacAddress.of(dstMac.toLong())); |
| 150 | } |
| 151 | if (srcIp != ShortestPathIntent.EMPTYIPADDRESS) { |
| 152 | matchBuilder.setExact(MatchField.ETH_TYPE, EthType.IPv4) |
| 153 | .setMasked(MatchField.IPV4_SRC, |
| 154 | IPv4AddressWithMask.of(srcIp, IPV4_PREFIX_LEN)); |
| 155 | } |
| 156 | if (dstIp != ShortestPathIntent.EMPTYIPADDRESS) { |
| 157 | matchBuilder.setExact(MatchField.ETH_TYPE, EthType.IPv4) |
| 158 | .setMasked(MatchField.IPV4_DST, |
| 159 | IPv4AddressWithMask.of(dstIp, IPV4_PREFIX_LEN)); |
| 160 | } |
| 161 | matchBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) srcPort)); |
| 162 | |
| 163 | return matchBuilder; |
| 164 | } |
| 165 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Returns a String representation of this Match. |
| 168 | * |
| 169 | * @return the match as a String |
| 170 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 171 | @Override |
| 172 | public String toString() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 173 | return "Sw:" + sw + " (" + srcPort + "," |
| 174 | + srcMac + "," + dstMac + "," |
| 175 | + srcIp + "," + dstIp + ")"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Generates hash using Objects.hash() to hash all object variables. |
| 180 | * |
| 181 | * @return hashcode |
| 182 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public int hashCode() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 185 | // TODO: we might consider excluding sw from the hash function |
| 186 | // to make it easier to compare matches between switches |
| 187 | return Objects.hash(sw, srcPort, srcMac, dstMac, srcIp, dstIp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 188 | } |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 189 | } |