Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame^] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 5 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 6 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 7 | |
| 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 | |
| 15 | public class OFPhysicalPort implements OFValueType<OFPhysicalPort>, Writeable { |
| 16 | |
| 17 | static final int LENGTH = 4; |
| 18 | |
| 19 | private final int port; |
| 20 | |
| 21 | public static final OFPhysicalPort NO_MASK = OFPhysicalPort.of(0xFFFFFFFF); |
| 22 | public static final OFPhysicalPort FULL_MASK = OFPhysicalPort.of(0x0); |
| 23 | |
| 24 | private OFPhysicalPort(int port) { |
| 25 | this.port = port; |
| 26 | } |
| 27 | |
| 28 | public static OFPhysicalPort of(int port) { |
| 29 | return new OFPhysicalPort(port); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public int getLength() { |
| 34 | return LENGTH; |
| 35 | } |
| 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 | |
| 60 | public void write4Bytes(ChannelBuffer c) { |
| 61 | c.writeInt(this.port); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void writeTo(ChannelBuffer bb) { |
| 66 | write4Bytes(bb); |
| 67 | } |
| 68 | |
| 69 | public static OFPhysicalPort read4Bytes(ChannelBuffer c) throws OFParseError { |
| 70 | return OFPhysicalPort.of((int)(c.readUnsignedInt() & 0xFFFFFFFF)); |
| 71 | } |
| 72 | |
| 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 | } |
| 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 | } |
| 90 | } |