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; |
| 5 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 6 | /** |
| 7 | * A wrapper around the OpenFlow physical port description. The interfaces to |
| 8 | * this object are version agnostic. |
| 9 | * |
| 10 | * @author capveg |
| 11 | */ |
| 12 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame^] | 13 | public class OFPhysicalPort implements OFValueType { |
| 14 | |
| 15 | static final int LENGTH = 4; |
| 16 | |
| 17 | private final int port; |
| 18 | |
| 19 | private OFPhysicalPort(int port) { |
| 20 | this.port = port; |
| 21 | } |
| 22 | |
| 23 | public static OFPhysicalPort of(int port) { |
| 24 | return new OFPhysicalPort(port); |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public int getLength() { |
| 29 | return LENGTH; |
| 30 | } |
| 31 | |
| 32 | volatile byte[] bytesCache; |
| 33 | |
| 34 | @Override |
| 35 | public byte[] getBytes() { |
| 36 | if (bytesCache == null) { |
| 37 | synchronized (this) { |
| 38 | if (bytesCache == null) { |
| 39 | bytesCache = new byte[] { (byte)(port & 0xFF), |
| 40 | (byte)((port >>> 8) & 0xFF), |
| 41 | (byte)((port >>> 16) & 0xFF), |
| 42 | (byte)((port >>> 24) & 0xFF)}; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | return bytesCache; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public boolean equals(Object obj) { |
| 51 | if (!(obj instanceof OFPhysicalPort)) |
| 52 | return false; |
| 53 | OFPhysicalPort other = (OFPhysicalPort)obj; |
| 54 | if (other.port != this.port) |
| 55 | return false; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int hashCode() { |
| 61 | final int prime = 59; |
| 62 | int result = 1; |
| 63 | result = prime * result + port; |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String toString() { |
| 69 | return Integer.toHexString(port); |
| 70 | } |
| 71 | |
| 72 | public static final Serializer<OFPhysicalPort> SERIALIZER_V11 = new SerializerV11(); |
| 73 | public static final Serializer<OFPhysicalPort> SERIALIZER_V12 = SERIALIZER_V11; |
| 74 | public static final Serializer<OFPhysicalPort> SERIALIZER_V13 = SERIALIZER_V11; |
| 75 | |
| 76 | private static class SerializerV11 implements OFValueType.Serializer<OFPhysicalPort> { |
| 77 | |
| 78 | @Override |
| 79 | public void writeTo(OFPhysicalPort value, ChannelBuffer c) { |
| 80 | c.writeInt(value.port); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public OFPhysicalPort readFrom(ChannelBuffer c) throws OFParseError { |
| 85 | return OFPhysicalPort.of((int)(c.readUnsignedInt() & 0xFFFFFFFF)); |
| 86 | } |
| 87 | |
| 88 | } |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 89 | |
| 90 | } |