Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 1 | package org.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.openflow.exceptions.OFParseError; |
| 5 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 6 | public enum IpEcn implements OFValueType<IpEcn> { |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 7 | ECN_00((byte)0), |
| 8 | ECN_01((byte)1), |
| 9 | ECN_10((byte)2), |
Yotam Harchol | c281360 | 2013-08-20 12:14:22 -0700 | [diff] [blame] | 10 | ECN_11((byte)3), |
Yotam Harchol | ff8f85e | 2013-08-21 10:02:23 -0700 | [diff] [blame] | 11 | ECN_NO_MASK((byte)0xFF); |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 12 | |
Yotam Harchol | ff8f85e | 2013-08-21 10:02:23 -0700 | [diff] [blame] | 13 | public static final IpEcn NO_MASK = ECN_NO_MASK; |
| 14 | public static final IpEcn FULL_MASK = ECN_00; |
Yotam Harchol | c281360 | 2013-08-20 12:14:22 -0700 | [diff] [blame] | 15 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 16 | static final int LENGTH = 1; |
| 17 | |
| 18 | private final byte ecn; |
| 19 | |
| 20 | private IpEcn(byte ecn) { |
| 21 | this.ecn = ecn; |
| 22 | } |
| 23 | |
| 24 | public static IpEcn of(byte ecn) { |
| 25 | switch (ecn) { |
| 26 | case 0: |
| 27 | return ECN_00; |
| 28 | case 1: |
| 29 | return ECN_01; |
| 30 | case 2: |
| 31 | return ECN_10; |
| 32 | case 3: |
| 33 | return ECN_11; |
| 34 | default: |
| 35 | throw new IllegalArgumentException("Illegal IP ECN value: " + ecn); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public int getLength() { |
| 41 | return LENGTH; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public String toString() { |
| 46 | return (ecn < 3 ? "0" : "") + Integer.toBinaryString(ecn); |
| 47 | } |
| 48 | |
| 49 | public void writeByte(ChannelBuffer c) { |
| 50 | c.writeByte(this.ecn); |
| 51 | } |
| 52 | |
| 53 | public static IpEcn readByte(ChannelBuffer c) throws OFParseError { |
| 54 | return IpEcn.of((byte)(c.readUnsignedByte())); |
| 55 | } |
| 56 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 57 | @Override |
| 58 | public IpEcn applyMask(IpEcn mask) { |
| 59 | return IpEcn.of((byte)(this.ecn & mask.ecn)); |
| 60 | } |
| 61 | |
| 62 | public byte getEcnValue() { |
| 63 | return ecn; |
| 64 | } |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 65 | } |