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; |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 6 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 7 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 8 | import org.projectfloodlight.openflow.protocol.match.Match.Builder; |
| 9 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 10 | import org.projectfloodlight.openflow.types.EthType; |
Matteo Gerola | e95f4ea | 2014-08-06 16:15:01 +0200 | [diff] [blame] | 11 | import org.projectfloodlight.openflow.types.IPv4Address; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 12 | import org.projectfloodlight.openflow.types.MacAddress; |
| 13 | import org.projectfloodlight.openflow.types.OFPort; |
| 14 | |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 15 | /** |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 16 | * 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'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | public class Match { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 29 | private static final short IPV4_PREFIX_LEN = 32; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | protected long sw; |
| 31 | protected MACAddress srcMac; |
| 32 | protected MACAddress dstMac; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 33 | protected int srcIp; |
| 34 | protected int dstIp; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | protected long srcPort; |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 36 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 37 | /** |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | public Match(long sw, long srcPort, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 46 | MACAddress srcMac, MACAddress dstMac) { |
| 47 | this(sw, srcPort, srcMac, dstMac, |
| 48 | ShortestPathIntent.EMPTYIPADDRESS, |
| 49 | ShortestPathIntent.EMPTYIPADDRESS); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 52 | /** |
| 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 Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 62 | public Match(long sw, long srcPort, MACAddress srcMac, MACAddress dstMac, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 63 | int srcIp, int dstIp) { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 64 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | this.sw = sw; |
| 66 | this.srcPort = srcPort; |
| 67 | this.srcMac = srcMac; |
| 68 | this.dstMac = dstMac; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 69 | this.srcIp = srcIp; |
| 70 | this.dstIp = dstIp; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | } |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 72 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Matches are equal if all object variables are equal. |
| 75 | * |
| 76 | * @return true if equal, false otherwise |
| 77 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | @Override |
| 79 | public boolean equals(Object obj) { |
| 80 | if (obj instanceof Match) { |
| 81 | Match other = (Match) obj; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 82 | // TODO: we might consider excluding sw from this comparison |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 83 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | } else { |
| 104 | return false; |
| 105 | } |
| 106 | } |
Yuta HIGUCHI | e80664e | 2014-02-20 22:41:57 -0800 | [diff] [blame] | 107 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 108 | 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 Gerola | e95f4ea | 2014-08-06 16:15:01 +0200 | [diff] [blame] | 119 | .setMasked(MatchField.IPV4_SRC, |
| 120 | IPv4Address.of(srcIp).withMaskOfLength(IPV4_PREFIX_LEN)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 121 | } |
| 122 | if (dstIp != ShortestPathIntent.EMPTYIPADDRESS) { |
| 123 | matchBuilder.setExact(MatchField.ETH_TYPE, EthType.IPv4) |
Matteo Gerola | e95f4ea | 2014-08-06 16:15:01 +0200 | [diff] [blame] | 124 | .setMasked(MatchField.IPV4_DST, |
| 125 | IPv4Address.of(dstIp).withMaskOfLength(IPV4_PREFIX_LEN)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 126 | } |
| 127 | matchBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) srcPort)); |
| 128 | |
| 129 | return matchBuilder; |
| 130 | } |
| 131 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Returns a String representation of this Match. |
| 134 | * |
| 135 | * @return the match as a String |
| 136 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public String toString() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 139 | return "Sw:" + sw + " (" + srcPort + "," |
| 140 | + srcMac + "," + dstMac + "," |
| 141 | + srcIp + "," + dstIp + ")"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian O'Connor | a84723c | 2014-06-13 00:26:49 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Generates hash using Objects.hash() to hash all object variables. |
| 146 | * |
| 147 | * @return hashcode |
| 148 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public int hashCode() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 151 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | } |
Brian O'Connor | 7f8e301 | 2014-02-15 23:59:29 -0800 | [diff] [blame] | 155 | } |