blob: cfefd39ca555cfe85bc92b1088ddf92f55c4d199 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5
Andreas Wundsam22ba3af2013-10-04 16:00:30 -07006import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -07007import com.google.common.primitives.UnsignedInts;
8
Yotam Harcholf3f11152013-09-05 16:47:16 -07009public class IPv6FlowLabel implements OFValueType<IPv6FlowLabel> {
10
11 static final int LENGTH = 4;
12
13 private final int label;
14
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070015 private final static int NONE_VAL = 0x0;
16 public static final IPv6FlowLabel NONE = new IPv6FlowLabel(NONE_VAL);
17
Yotam Harcholf3f11152013-09-05 16:47:16 -070018 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 Wundsamb75c4ad2013-09-23 14:45:35 -070026 if(label == NONE_VAL)
27 return NONE;
Yotam Harcholf3f11152013-09-05 16:47:16 -070028 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() {
Ryan Izard4347d352015-02-05 18:15:45 -050056 return "0x" + Integer.toHexString(label);
Yotam Harcholf3f11152013-09-05 16:47:16 -070057 }
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 Wundsam85c961f2013-09-29 21:22:12 -070075
76 @Override
77 public int compareTo(IPv6FlowLabel o) {
78 return UnsignedInts.compare(label, o.label);
79 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070080
81 @Override
82 public void putTo(PrimitiveSink sink) {
83 sink.putInt(this.label);
84 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070085}