blob: 82dab4114d85630a2989fef883c2161490cde293 [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
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 Harchol5c9d6f42013-08-01 11:09:20 -070034
35 public int getPort() {
36 return port;
37 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070038
39 @Override
40 public boolean equals(Object obj) {
41 if (!(obj instanceof TransportPort))
42 return false;
43 TransportPort other = (TransportPort)obj;
44 if (other.port != this.port)
45 return false;
46 return true;
47 }
48
49 @Override
50 public int hashCode() {
51 final int prime = 59;
52 int result = 1;
53 result = prime * result + port;
54 return result;
55 }
56
57 @Override
58 public String toString() {
59 return Integer.toString(port);
60 }
61
Yotam Harchold7b84202013-07-26 16:08:10 -070062 public void write2Bytes(ChannelBuffer c) {
63 c.writeShort(this.port);
64 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070065
Yotam Harchold7b84202013-07-26 16:08:10 -070066 public static TransportPort read2Bytes(ChannelBuffer c) throws OFParseError {
67 return TransportPort.of((c.readUnsignedShort() & 0x0FFFF));
Yotam Harchol161a5d52013-07-25 17:17:48 -070068 }
69
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070070 @Override
71 public TransportPort applyMask(TransportPort mask) {
72 return TransportPort.of(this.port & mask.port);
73 }
74
Yotam Harchol161a5d52013-07-25 17:17:48 -070075}