blob: 1ebb807700723a9071e28cdc3d75b6b199cf27dc [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.)
Yotam Harchol991b3492013-08-22 15:40:13 -07008 *
Yotam Harchol161a5d52013-07-25 17:17:48 -07009 * @author Yotam Harchol (yotam.harchol@bigswitch.com)
10 */
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070011public class TransportPort implements OFValueType<TransportPort> {
Yotam Harchol991b3492013-08-22 15:40:13 -070012
Yotam Harchol161a5d52013-07-25 17:17:48 -070013 static final int LENGTH = 2;
14 static final int MAX_PORT = 0xFFFF;
15 static final int MIN_PORT = 0;
Yotam Harchol991b3492013-08-22 15:40:13 -070016
17 public static final TransportPort NO_MASK = new TransportPort(0xFFFFFFFF);
Yotam Harcholff8f85e2013-08-21 10:02:23 -070018 public static final TransportPort FULL_MASK = TransportPort.of(0x0);
Yotam Harcholc2813602013-08-20 12:14:22 -070019
Yotam Harchol161a5d52013-07-25 17:17:48 -070020 private final int port;
Yotam Harchol991b3492013-08-22 15:40:13 -070021
Yotam Harchol161a5d52013-07-25 17:17:48 -070022 private TransportPort(int port) {
23 this.port = port;
24 }
Yotam Harchol991b3492013-08-22 15:40:13 -070025
Yotam Harchol161a5d52013-07-25 17:17:48 -070026 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 Harchol991b3492013-08-22 15:40:13 -070037
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070038 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 }
Yotam Harchol991b3492013-08-22 15:40:13 -070077
Yotam Harchol161a5d52013-07-25 17:17:48 -070078}