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 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 6 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 7 | import com.google.common.primitives.UnsignedInts; |
| 8 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | public class IPv6FlowLabel implements OFValueType<IPv6FlowLabel> { |
| 10 | |
| 11 | static final int LENGTH = 4; |
| 12 | |
| 13 | private final int label; |
| 14 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 15 | private final static int NONE_VAL = 0x0; |
| 16 | public static final IPv6FlowLabel NONE = new IPv6FlowLabel(NONE_VAL); |
| 17 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 18 | public static final IPv6FlowLabel NO_MASK = IPv6FlowLabel.of(0xFFFFFFFF); |
| 19 | public static final IPv6FlowLabel FULL_MASK = IPv6FlowLabel.of(0x0); |
| 20 | |
| 21 | private IPv6FlowLabel(int label) { |
| 22 | this.label = label; |
| 23 | } |
| 24 | |
| 25 | public static IPv6FlowLabel of(int label) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 26 | if(label == NONE_VAL) |
| 27 | return NONE; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | return new IPv6FlowLabel(label); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public int getLength() { |
| 33 | return LENGTH; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public boolean equals(Object obj) { |
| 38 | if (!(obj instanceof IPv6FlowLabel)) |
| 39 | return false; |
| 40 | IPv6FlowLabel other = (IPv6FlowLabel)obj; |
| 41 | if (other.label != this.label) |
| 42 | return false; |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public int hashCode() { |
| 48 | final int prime = 59; |
| 49 | int result = 1; |
| 50 | result = prime * result + label; |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String toString() { |
| 56 | return Integer.toHexString(label); |
| 57 | } |
| 58 | |
| 59 | public void write4Bytes(ChannelBuffer c) { |
| 60 | c.writeInt(this.label); |
| 61 | } |
| 62 | |
| 63 | public static IPv6FlowLabel read4Bytes(ChannelBuffer c) throws OFParseError { |
| 64 | return IPv6FlowLabel.of((int)(c.readUnsignedInt() & 0xFFFFFFFF)); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public IPv6FlowLabel applyMask(IPv6FlowLabel mask) { |
| 69 | return IPv6FlowLabel.of(this.label & mask.label); |
| 70 | } |
| 71 | |
| 72 | public int getIPv6FlowLabelValue() { |
| 73 | return label; |
| 74 | } |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public int compareTo(IPv6FlowLabel o) { |
| 78 | return UnsignedInts.compare(label, o.label); |
| 79 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | public void putTo(PrimitiveSink sink) { |
| 83 | sink.putInt(this.label); |
| 84 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 85 | } |