blob: 86ed53ec3051cb2d51da11b36991b8e66e2dde63 [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 }
34
35 volatile byte[] bytesCache;
36
37 @Override
38 public byte[] getBytes() {
39 if (bytesCache == null) {
40 synchronized (this) {
41 if (bytesCache == null) {
42 bytesCache = new byte[] { (byte)(port & 0xFF),
43 (byte)((port >>> 8) & 0xFF)};
44 }
45 }
46 }
47 return bytesCache;
48 }
49
50 @Override
51 public boolean equals(Object obj) {
52 if (!(obj instanceof TransportPort))
53 return false;
54 TransportPort other = (TransportPort)obj;
55 if (other.port != this.port)
56 return false;
57 return true;
58 }
59
60 @Override
61 public int hashCode() {
62 final int prime = 59;
63 int result = 1;
64 result = prime * result + port;
65 return result;
66 }
67
68 @Override
69 public String toString() {
70 return Integer.toString(port);
71 }
72
73 public static final Serializer<TransportPort> SERIALIZER_V10 = new SerializerV10();
74 public static final Serializer<TransportPort> SERIALIZER_V11 = SERIALIZER_V10;
75 public static final Serializer<TransportPort> SERIALIZER_V12 = SERIALIZER_V10;
76 public static final Serializer<TransportPort> SERIALIZER_V13 = SERIALIZER_V10;
77
78 private static class SerializerV10 implements OFValueType.Serializer<TransportPort> {
79
80 @Override
81 public void writeTo(TransportPort value, ChannelBuffer c) {
82 c.writeShort(value.port);
83 }
84
85 @Override
86 public TransportPort readFrom(ChannelBuffer c) throws OFParseError {
87 return TransportPort.of((c.readUnsignedShort() & 0x0FFFF));
88 }
89
90 }
91
92}