blob: ed6a4fe8a2c85822853174b13b28a992e3ba8e42 [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 */
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070011public class TransportPort implements OFValueType<TransportPort> {
Yotam Harchol161a5d52013-07-25 17:17:48 -070012
13 static final int LENGTH = 2;
14 static final int MAX_PORT = 0xFFFF;
15 static final int MIN_PORT = 0;
16
Yotam Harcholc2813602013-08-20 12:14:22 -070017 public static final TransportPort FULL_MASK = TransportPort.of(0xFFFFFFFF);
18 public static final TransportPort NO_MASK = TransportPort.of(0x0);
19
Yotam Harchol161a5d52013-07-25 17:17:48 -070020 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 Harchol5c9d6f42013-08-01 11:09:20 -070037
38 public int getPort() {
39 return port;
40 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070041
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 Harchold7b84202013-07-26 16:08:10 -070065 public void write2Bytes(ChannelBuffer c) {
66 c.writeShort(this.port);
67 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070068
Yotam Harchold7b84202013-07-26 16:08:10 -070069 public static TransportPort read2Bytes(ChannelBuffer c) throws OFParseError {
70 return TransportPort.of((c.readUnsignedShort() & 0x0FFFF));
Yotam Harchol161a5d52013-07-25 17:17:48 -070071 }
72
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070073 @Override
74 public TransportPort applyMask(TransportPort mask) {
75 return TransportPort.of(this.port & mask.port);
76 }
77
Yotam Harchol161a5d52013-07-25 17:17:48 -070078}