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 | */ |
| 11 | public class TransportPort implements OFValueType { |
| 12 | |
| 13 | static final int LENGTH = 2; |
| 14 | static final int MAX_PORT = 0xFFFF; |
| 15 | static final int MIN_PORT = 0; |
| 16 | |
| 17 | private final int port; |
| 18 | |
| 19 | private TransportPort(int port) { |
| 20 | this.port = port; |
| 21 | } |
| 22 | |
| 23 | public static TransportPort of(int port) { |
| 24 | if (port < MIN_PORT || port > MAX_PORT) { |
| 25 | throw new IllegalArgumentException("Illegal transport layer port number: " + port); |
| 26 | } |
| 27 | return new TransportPort(port); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public int getLength() { |
| 32 | return LENGTH; |
| 33 | } |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 34 | |
| 35 | @Override |
| 36 | public boolean equals(Object obj) { |
| 37 | if (!(obj instanceof TransportPort)) |
| 38 | return false; |
| 39 | TransportPort other = (TransportPort)obj; |
| 40 | if (other.port != this.port) |
| 41 | return false; |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public int hashCode() { |
| 47 | final int prime = 59; |
| 48 | int result = 1; |
| 49 | result = prime * result + port; |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public String toString() { |
| 55 | return Integer.toString(port); |
| 56 | } |
| 57 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame^] | 58 | public void write2Bytes(ChannelBuffer c) { |
| 59 | c.writeShort(this.port); |
| 60 | } |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 61 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame^] | 62 | public static TransportPort read2Bytes(ChannelBuffer c) throws OFParseError { |
| 63 | return TransportPort.of((c.readUnsignedShort() & 0x0FFFF)); |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } |