blob: d3b1c9f87b143ec56149423ac0fc527c1ca1a461 [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 Harcholff8f85e2013-08-21 10:02:23 -070021 public static final OFPhysicalPort NO_MASK = OFPhysicalPort.of(0xFFFFFFFF);
22 public static final OFPhysicalPort FULL_MASK = OFPhysicalPort.of(0x0);
Yotam Harcholc2813602013-08-20 12:14:22 -070023
Yotam Harchol161a5d52013-07-25 17:17:48 -070024 private OFPhysicalPort(int port) {
25 this.port = port;
26 }
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070027
Yotam Harchol161a5d52013-07-25 17:17:48 -070028 public static OFPhysicalPort of(int port) {
29 return new OFPhysicalPort(port);
30 }
31
32 @Override
33 public int getLength() {
34 return LENGTH;
35 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070036
37 @Override
38 public boolean equals(Object obj) {
39 if (!(obj instanceof OFPhysicalPort))
40 return false;
41 OFPhysicalPort other = (OFPhysicalPort)obj;
42 if (other.port != this.port)
43 return false;
44 return true;
45 }
46
47 @Override
48 public int hashCode() {
49 final int prime = 59;
50 int result = 1;
51 result = prime * result + port;
52 return result;
53 }
54
55 @Override
56 public String toString() {
57 return Integer.toHexString(port);
58 }
59
Yotam Harchold7b84202013-07-26 16:08:10 -070060 public void write4Bytes(ChannelBuffer c) {
61 c.writeInt(this.port);
62 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070063
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070064 @Override
65 public void writeTo(ChannelBuffer bb) {
66 write4Bytes(bb);
67 }
68
Yotam Harchold7b84202013-07-26 16:08:10 -070069 public static OFPhysicalPort read4Bytes(ChannelBuffer c) throws OFParseError {
70 return OFPhysicalPort.of((int)(c.readUnsignedInt() & 0xFFFFFFFF));
Yotam Harchol161a5d52013-07-25 17:17:48 -070071 }
Andreas Wundsam40e14f72013-05-06 14:49:08 -070072
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070073 @Override
74 public OFPhysicalPort applyMask(OFPhysicalPort mask) {
75 return OFPhysicalPort.of(this.port & mask.port);
76 }
77
78 public int getPortNumber() {
79 return port;
80 }
Andreas Wundsamf22c2e22013-08-02 22:21:20 -070081
82 public final static Reader READER = new Reader();
83 private static class Reader implements OFMessageReader<OFPhysicalPort> {
84 @Override
85 public OFPhysicalPort readFrom(ChannelBuffer bb) throws OFParseError {
86 return OFPhysicalPort.read4Bytes(bb);
87 }
88
89 }
Andreas Wundsam40e14f72013-05-06 14:49:08 -070090}