blob: 45bbf4bf42edf951495fc1adb98b50bafed21f31 [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 Wundsam85c961f2013-09-29 21:22:12 -07006import com.google.common.primitives.UnsignedInts;
7
Yotam Harcholf3f11152013-09-05 16:47:16 -07008public class IPv6FlowLabel implements OFValueType<IPv6FlowLabel> {
9
10 static final int LENGTH = 4;
11
12 private final int label;
13
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070014 private final static int NONE_VAL = 0x0;
15 public static final IPv6FlowLabel NONE = new IPv6FlowLabel(NONE_VAL);
16
Yotam Harcholf3f11152013-09-05 16:47:16 -070017 public static final IPv6FlowLabel NO_MASK = IPv6FlowLabel.of(0xFFFFFFFF);
18 public static final IPv6FlowLabel FULL_MASK = IPv6FlowLabel.of(0x0);
19
20 private IPv6FlowLabel(int label) {
21 this.label = label;
22 }
23
24 public static IPv6FlowLabel of(int label) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070025 if(label == NONE_VAL)
26 return NONE;
Yotam Harcholf3f11152013-09-05 16:47:16 -070027 return new IPv6FlowLabel(label);
28 }
29
30 @Override
31 public int getLength() {
32 return LENGTH;
33 }
34
35 @Override
36 public boolean equals(Object obj) {
37 if (!(obj instanceof IPv6FlowLabel))
38 return false;
39 IPv6FlowLabel other = (IPv6FlowLabel)obj;
40 if (other.label != this.label)
41 return false;
42 return true;
43 }
44
45 @Override
46 public int hashCode() {
47 final int prime = 59;
48 int result = 1;
49 result = prime * result + label;
50 return result;
51 }
52
53 @Override
54 public String toString() {
55 return Integer.toHexString(label);
56 }
57
58 public void write4Bytes(ChannelBuffer c) {
59 c.writeInt(this.label);
60 }
61
62 public static IPv6FlowLabel read4Bytes(ChannelBuffer c) throws OFParseError {
63 return IPv6FlowLabel.of((int)(c.readUnsignedInt() & 0xFFFFFFFF));
64 }
65
66 @Override
67 public IPv6FlowLabel applyMask(IPv6FlowLabel mask) {
68 return IPv6FlowLabel.of(this.label & mask.label);
69 }
70
71 public int getIPv6FlowLabelValue() {
72 return label;
73 }
Andreas Wundsam85c961f2013-09-29 21:22:12 -070074
75 @Override
76 public int compareTo(IPv6FlowLabel o) {
77 return UnsignedInts.compare(label, o.label);
78 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070079}