blob: de49b51f8db82564913bcb8f3e4647fcecb82614 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5
6import com.google.common.hash.PrimitiveSink;
7import com.google.common.primitives.UnsignedInts;
8
9public class IPv6FlowLabel implements OFValueType<IPv6FlowLabel> {
10
11 static final int LENGTH = 4;
12
13 private final int label;
14
15 private final static int NONE_VAL = 0x0;
16 public static final IPv6FlowLabel NONE = new IPv6FlowLabel(NONE_VAL);
17
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) {
26 if(label == NONE_VAL)
27 return NONE;
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 }
75
76 @Override
77 public int compareTo(IPv6FlowLabel o) {
78 return UnsignedInts.compare(label, o.label);
79 }
80
81 @Override
82 public void putTo(PrimitiveSink sink) {
83 sink.putInt(this.label);
84 }
85}