Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 1 | package org.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.openflow.exceptions.OFParseError; |
| 5 | |
| 6 | /** |
| 7 | * Represents L4 (Transport Layer) port (TCP, UDP, etc.) |
| 8 | * |
| 9 | * @author Yotam Harchol (yotam.harchol@bigswitch.com) |
| 10 | */ |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 11 | public class TransportPort implements OFValueType<TransportPort> { |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 12 | |
| 13 | static final int LENGTH = 2; |
| 14 | static final int MAX_PORT = 0xFFFF; |
| 15 | static final int MIN_PORT = 0; |
| 16 | |
Yotam Harchol | ff8f85e | 2013-08-21 10:02:23 -0700 | [diff] [blame] | 17 | public static final TransportPort NO_MASK = TransportPort.of(0xFFFFFFFF); |
| 18 | public static final TransportPort FULL_MASK = TransportPort.of(0x0); |
Yotam Harchol | c281360 | 2013-08-20 12:14:22 -0700 | [diff] [blame] | 19 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 20 | private final int port; |
| 21 | |
| 22 | private TransportPort(int port) { |
| 23 | this.port = port; |
| 24 | } |
| 25 | |
| 26 | public static TransportPort of(int port) { |
| 27 | if (port < MIN_PORT || port > MAX_PORT) { |
| 28 | throw new IllegalArgumentException("Illegal transport layer port number: " + port); |
| 29 | } |
| 30 | return new TransportPort(port); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public int getLength() { |
| 35 | return LENGTH; |
| 36 | } |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 37 | |
| 38 | public int getPort() { |
| 39 | return port; |
| 40 | } |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 41 | |
| 42 | @Override |
| 43 | public boolean equals(Object obj) { |
| 44 | if (!(obj instanceof TransportPort)) |
| 45 | return false; |
| 46 | TransportPort other = (TransportPort)obj; |
| 47 | if (other.port != this.port) |
| 48 | return false; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public int hashCode() { |
| 54 | final int prime = 59; |
| 55 | int result = 1; |
| 56 | result = prime * result + port; |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public String toString() { |
| 62 | return Integer.toString(port); |
| 63 | } |
| 64 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 65 | public void write2Bytes(ChannelBuffer c) { |
| 66 | c.writeShort(this.port); |
| 67 | } |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 68 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 69 | public static TransportPort read2Bytes(ChannelBuffer c) throws OFParseError { |
| 70 | return TransportPort.of((c.readUnsignedShort() & 0x0FFFF)); |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 73 | @Override |
| 74 | public TransportPort applyMask(TransportPort mask) { |
| 75 | return TransportPort.of(this.port & mask.port); |
| 76 | } |
| 77 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 78 | } |