blob: cecb34906b29528e043553124e19988734f11a37 [file] [log] [blame]
Yotam Harchol161a5d52013-07-25 17:17:48 -07001package org.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import 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 */
11public 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 Harchol161a5d52013-07-25 17:17:48 -070034
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 Harchold7b84202013-07-26 16:08:10 -070058 public void write2Bytes(ChannelBuffer c) {
59 c.writeShort(this.port);
60 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070061
Yotam Harchold7b84202013-07-26 16:08:10 -070062 public static TransportPort read2Bytes(ChannelBuffer c) throws OFParseError {
63 return TransportPort.of((c.readUnsignedShort() & 0x0FFFF));
Yotam Harchol161a5d52013-07-25 17:17:48 -070064 }
65
66}