blob: d4e0ce2a89d6e203240e10ad16e5c47bfe1d1825 [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 Hart472062d2014-04-03 10:56:48 -07008//import net.onrc.onos.core.topology.Port;
9//import net.onrc.onos.core.topology.Switch;
Jonathan Hart23701d12014-04-03 10:45:48 -070010import net.onrc.onos.core.util.FlowEntryMatch;
Komal Shah399a2922014-05-28 01:57:40 -070011import net.onrc.onos.core.util.IPv4;
12import net.onrc.onos.core.util.IPv4Net;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080013
14/**
Brian O'Connor7f8e3012014-02-15 23:59:29 -080015 * @author Brian O'Connor <bocon@onlab.us>
Brian O'Connor7f8e3012014-02-15 23:59:29 -080016 */
17
18public class Match {
Komal Shah399a2922014-05-28 01:57:40 -070019 private static final short IPV4_PREFIX_LEN = 32;
Ray Milkey269ffb92014-04-03 14:43:30 -070020 protected long sw;
21 protected MACAddress srcMac;
22 protected MACAddress dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070023 protected int srcIp;
24 protected int dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070025 protected long srcPort;
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080026
Ray Milkey269ffb92014-04-03 14:43:30 -070027 public Match(long sw, long srcPort,
28 MACAddress srcMac, MACAddress dstMac) {
Komal Shah399a2922014-05-28 01:57:40 -070029 this(sw, srcPort, srcMac, dstMac, ShortestPathIntent.EMPTYIPADDRESS, ShortestPathIntent.EMPTYIPADDRESS);
30 }
31
32 public Match(long sw, long srcPort, MACAddress srcMac, MACAddress dstMac,
33 int srcIp, int dstIp) {
34
Ray Milkey269ffb92014-04-03 14:43:30 -070035 this.sw = sw;
36 this.srcPort = srcPort;
37 this.srcMac = srcMac;
38 this.dstMac = dstMac;
Komal Shah399a2922014-05-28 01:57:40 -070039 this.srcIp = srcIp;
40 this.dstIp = dstIp;
Ray Milkey269ffb92014-04-03 14:43:30 -070041 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080042
Ray Milkey269ffb92014-04-03 14:43:30 -070043 @Override
44 public boolean equals(Object obj) {
45 if (obj instanceof Match) {
46 Match other = (Match) obj;
Komal Shah399a2922014-05-28 01:57:40 -070047 if (this.sw != other.sw) {
48 return false;
49 }
50 if (!Objects.equals(srcMac, other.srcMac)) {
51 return false;
52 }
53 if (!Objects.equals(dstMac, other.dstMac)) {
54 return false;
55 }
56 if (srcIp != other.srcIp) {
57 return false;
58 }
59
60 if (dstIp != other.dstIp) {
61 return false;
62 }
63 if (this.srcPort != other.srcPort) {
64 return false;
65 }
66 return true;
Ray Milkey269ffb92014-04-03 14:43:30 -070067 } else {
68 return false;
69 }
70 }
Yuta HIGUCHIe80664e2014-02-20 22:41:57 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 public FlowEntryMatch getFlowEntryMatch() {
73 FlowEntryMatch match = new FlowEntryMatch();
Komal Shah399a2922014-05-28 01:57:40 -070074 if (srcMac != null) {
75 match.enableSrcMac(srcMac);
76 }
77 if (dstMac != null) {
78 match.enableDstMac(dstMac);
79 }
80 if (srcIp != ShortestPathIntent.EMPTYIPADDRESS) {
81 match.enableEthernetFrameType(Ethernet.TYPE_IPV4);
82 IPv4 srcIPv4 = new IPv4(srcIp);
83 IPv4Net srcIP = new IPv4Net(srcIPv4, IPV4_PREFIX_LEN);
84 match.enableSrcIPv4Net(srcIP);
85 }
86 if (dstIp != ShortestPathIntent.EMPTYIPADDRESS) {
87 match.enableEthernetFrameType(Ethernet.TYPE_IPV4);
88 IPv4 dstIPv4 = new IPv4(dstIp);
89 IPv4Net dstIP = new IPv4Net(dstIPv4, IPV4_PREFIX_LEN);
90 match.enableDstIPv4Net(dstIP);
91 }
Ray Milkey269ffb92014-04-03 14:43:30 -070092 match.enableInPort(new net.onrc.onos.core.util.Port((short) srcPort));
93 return match;
94 }
95
96 @Override
97 public String toString() {
Komal Shah399a2922014-05-28 01:57:40 -070098 return "Sw:" + sw + " (" + srcPort + "," + srcMac + "," + dstMac + "," + srcIp + "," + dstIp + ")";
Ray Milkey269ffb92014-04-03 14:43:30 -070099 }
100
101 @Override
102 public int hashCode() {
Komal Shah399a2922014-05-28 01:57:40 -0700103 return Objects.hash(sw, srcPort, srcMac, dstMac, srcIp, dstIp);
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 }
Brian O'Connor7f8e3012014-02-15 23:59:29 -0800105}