blob: 6eb07b1793e10d1ceda9e53f9d317720fd97add2 [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -07001package org.openflow.types;
2
Yotam Harchol161a5d52013-07-25 17:17:48 -07003import org.jboss.netty.buffer.ChannelBuffer;
4import org.openflow.exceptions.OFParseError;
Andreas Wundsamf22c2e22013-08-02 22:21:20 -07005import org.openflow.protocol.OFMessageReader;
6import org.openflow.protocol.Writeable;
Yotam Harchol161a5d52013-07-25 17:17:48 -07007
Andreas Wundsam40e14f72013-05-06 14:49:08 -07008/**
9 * A wrapper around the OpenFlow physical port description. The interfaces to
10 * this object are version agnostic.
11 *
12 * @author capveg
13 */
14
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070015public class OFPhysicalPort implements OFValueType<OFPhysicalPort>, Writeable {
Yotam Harchol161a5d52013-07-25 17:17:48 -070016
17 static final int LENGTH = 4;
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070018
Yotam Harchol161a5d52013-07-25 17:17:48 -070019 private final int port;
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070020
Yotam Harchol161a5d52013-07-25 17:17:48 -070021 private OFPhysicalPort(int port) {
22 this.port = port;
23 }
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070024
Yotam Harchol161a5d52013-07-25 17:17:48 -070025 public static OFPhysicalPort of(int port) {
26 return new OFPhysicalPort(port);
27 }
28
29 @Override
30 public int getLength() {
31 return LENGTH;
32 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070033
34 @Override
35 public boolean equals(Object obj) {
36 if (!(obj instanceof OFPhysicalPort))
37 return false;
38 OFPhysicalPort other = (OFPhysicalPort)obj;
39 if (other.port != this.port)
40 return false;
41 return true;
42 }
43
44 @Override
45 public int hashCode() {
46 final int prime = 59;
47 int result = 1;
48 result = prime * result + port;
49 return result;
50 }
51
52 @Override
53 public String toString() {
54 return Integer.toHexString(port);
55 }
56
Yotam Harchold7b84202013-07-26 16:08:10 -070057 public void write4Bytes(ChannelBuffer c) {
58 c.writeInt(this.port);
59 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070060
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070061 @Override
62 public void writeTo(ChannelBuffer bb) {
63 write4Bytes(bb);
64 }
65
Yotam Harchold7b84202013-07-26 16:08:10 -070066 public static OFPhysicalPort read4Bytes(ChannelBuffer c) throws OFParseError {
67 return OFPhysicalPort.of((int)(c.readUnsignedInt() & 0xFFFFFFFF));
Yotam Harchol161a5d52013-07-25 17:17:48 -070068 }
Andreas Wundsam40e14f72013-05-06 14:49:08 -070069
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070070 @Override
71 public OFPhysicalPort applyMask(OFPhysicalPort mask) {
72 return OFPhysicalPort.of(this.port & mask.port);
73 }
74
75 public int getPortNumber() {
76 return port;
77 }
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070078
79 public final static Reader READER = new Reader();
80 private static class Reader implements OFMessageReader<OFPhysicalPort> {
81 @Override
82 public OFPhysicalPort readFrom(ChannelBuffer bb) throws OFParseError {
83 return OFPhysicalPort.read4Bytes(bb);
84 }
85
86 }
Andreas Wundsam40e14f72013-05-06 14:49:08 -070087}