Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1 | package org.openflow.types; |
| 2 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.openflow.exceptions.OFParseError; |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 5 | import org.openflow.protocol.OFMessageReader; |
| 6 | import org.openflow.protocol.Writeable; |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 7 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 8 | /** |
| 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 Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 15 | public class OFPhysicalPort implements OFValueType<OFPhysicalPort>, Writeable { |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 16 | |
| 17 | static final int LENGTH = 4; |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 18 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 19 | private final int port; |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 20 | |
Yotam Harchol | ff8f85e | 2013-08-21 10:02:23 -0700 | [diff] [blame] | 21 | public static final OFPhysicalPort NO_MASK = OFPhysicalPort.of(0xFFFFFFFF); |
| 22 | public static final OFPhysicalPort FULL_MASK = OFPhysicalPort.of(0x0); |
Yotam Harchol | c281360 | 2013-08-20 12:14:22 -0700 | [diff] [blame] | 23 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 24 | private OFPhysicalPort(int port) { |
| 25 | this.port = port; |
| 26 | } |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 27 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 28 | public static OFPhysicalPort of(int port) { |
| 29 | return new OFPhysicalPort(port); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public int getLength() { |
| 34 | return LENGTH; |
| 35 | } |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 36 | |
| 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 Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 60 | public void write4Bytes(ChannelBuffer c) { |
| 61 | c.writeInt(this.port); |
| 62 | } |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 63 | |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public void writeTo(ChannelBuffer bb) { |
| 66 | write4Bytes(bb); |
| 67 | } |
| 68 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 69 | public static OFPhysicalPort read4Bytes(ChannelBuffer c) throws OFParseError { |
| 70 | return OFPhysicalPort.of((int)(c.readUnsignedInt() & 0xFFFFFFFF)); |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 71 | } |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 72 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 73 | @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 Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 81 | |
| 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 Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 90 | } |