Toshio Koide | 80db184 | 2014-08-11 17:08:32 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction.match; |
| 2 | |
| 3 | import net.floodlightcontroller.util.MACAddress; |
| 4 | import net.onrc.onos.core.util.IPv4; |
| 5 | import net.onrc.onos.core.util.IPv4Net; |
| 6 | |
| 7 | /** |
| 8 | * A builder to instantiate PacketMatch class. |
| 9 | */ |
| 10 | public class PacketMatchBuilder { |
| 11 | private MACAddress srcMac = null; |
| 12 | private MACAddress dstMac = null; |
| 13 | private Short etherType = null; |
| 14 | private IPv4Net srcIp = null; |
| 15 | private IPv4Net dstIp = null; |
| 16 | private Byte ipProto = null; |
| 17 | private Short srcTcpPort = null; |
| 18 | private Short dstTcpPort = null; |
| 19 | |
| 20 | /** |
| 21 | * Sets source MAC address. |
| 22 | * |
| 23 | * @param mac the source MAC address |
| 24 | * @return this builder |
| 25 | */ |
| 26 | public PacketMatchBuilder setSrcMac(MACAddress mac) { |
| 27 | srcMac = mac; |
| 28 | return this; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Sets destination MAC address. |
| 33 | * |
| 34 | * @param mac the destination MAC address |
| 35 | * @return this builder |
| 36 | */ |
| 37 | public PacketMatchBuilder setDstMac(MACAddress mac) { |
| 38 | dstMac = mac; |
| 39 | return this; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Sets Ethernet type. |
| 44 | * |
| 45 | * @param etherTypeNo the Ethernet type |
| 46 | * @return this builder |
| 47 | */ |
| 48 | public PacketMatchBuilder setEtherType(short etherTypeNo) { |
| 49 | etherType = etherTypeNo; |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Sets source IP address with prefix length. |
| 55 | * |
| 56 | * @param ip the source IP address |
| 57 | * @param prefixLen the prefix length |
| 58 | * @return this builder |
| 59 | */ |
| 60 | public PacketMatchBuilder setSrcIp(IPv4 ip, short prefixLen) { |
| 61 | srcIp = new IPv4Net(ip, prefixLen); |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets source IP address. |
| 67 | * |
| 68 | * @param ip the source IP address |
| 69 | * @return this builder |
| 70 | */ |
| 71 | public PacketMatchBuilder setSrcIp(IPv4 ip) { |
| 72 | return setSrcIp(ip, (short) 32); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets destination IP address with prefix length. |
| 77 | * |
| 78 | * @param ip the destination IP address |
| 79 | * @param prefixLen the prefix length |
| 80 | * @return this builder |
| 81 | */ |
| 82 | public PacketMatchBuilder setDstIp(IPv4 ip, short prefixLen) { |
| 83 | dstIp = new IPv4Net(ip, prefixLen); |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets destination IP address. |
| 89 | * |
| 90 | * @param ip the destination IP address |
| 91 | * @return this builder |
| 92 | */ |
| 93 | public PacketMatchBuilder setDstIp(IPv4 ip) { |
| 94 | return setDstIp(ip, (short) 32); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets IP protocol number. |
| 99 | * |
| 100 | * @param ipProtoNo the IP protocol |
| 101 | * @return this builder |
| 102 | */ |
| 103 | public PacketMatchBuilder setIpProto(byte ipProtoNo) { |
| 104 | ipProto = ipProtoNo; |
| 105 | return this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sets source TCP port number. |
| 110 | * |
| 111 | * @param tcpPort the source TCP port number |
| 112 | * @return this builder |
| 113 | */ |
| 114 | public PacketMatchBuilder setSrcTcpPort(short tcpPort) { |
| 115 | srcTcpPort = tcpPort; |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets destination TCP port number. |
| 121 | * |
| 122 | * @param tcpPort the destination TCP port number |
| 123 | * @return this builder |
| 124 | */ |
| 125 | public PacketMatchBuilder setDstTcpPort(short tcpPort) { |
| 126 | dstTcpPort = tcpPort; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Builds the PacketMatch instance. |
| 132 | * |
| 133 | * @return the PacketMatch instance |
| 134 | */ |
| 135 | public PacketMatch build() { |
| 136 | // TODO: check consistency among fields |
| 137 | |
| 138 | return new PacketMatch(srcMac, dstMac, |
| 139 | etherType, |
| 140 | srcIp, dstIp, ipProto, |
| 141 | srcTcpPort, dstTcpPort); |
| 142 | } |
| 143 | } |