blob: a43e5d6422dcdcc9e6938ad59183fee7115a9cc5 [file] [log] [blame]
Rob Vaterlausfeee3712013-09-30 11:24:19 -07001package org.projectfloodlight.openflow.types;
2
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -07003import org.projectfloodlight.openflow.annotations.Immutable;
Rob Vaterlausfeee3712013-09-30 11:24:19 -07004import org.projectfloodlight.openflow.util.HexString;
5
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -07006import com.google.common.hash.PrimitiveSink;
Sovietacedbb0f0cc2015-07-20 22:40:02 -04007import com.google.common.primitives.Bytes;
Rob Vaterlaus57dde612013-10-18 10:02:28 -07008import com.google.common.primitives.Longs;
Andreas Wundsam72977ce2013-10-15 21:31:59 -07009import com.google.common.primitives.UnsignedLongs;
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070010
Rob Vaterlausfeee3712013-09-30 11:24:19 -070011/**
12 * Abstraction of a datapath ID that can be set and/or accessed as either a
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070013 * long value or a colon-separated string. Immutable
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070014 *
kjwon157bc85402015-02-12 15:07:42 +090015 * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >}
Rob Vaterlausfeee3712013-09-30 11:24:19 -070016 */
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070017@Immutable
Andreas Wundsamedb1f202013-10-17 10:59:11 -070018public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> {
Rob Vaterlausfeee3712013-09-30 11:24:19 -070019
20 public static final DatapathId NONE = new DatapathId(0);
21
22 private final long rawValue;
23
24 private DatapathId(long rawValue) {
25 this.rawValue = rawValue;
26 }
27
28 public static DatapathId of(long rawValue) {
29 return new DatapathId(rawValue);
30 }
31
32 public static DatapathId of(String s) {
33 return new DatapathId(HexString.toLong(s));
34 }
35
Rob Vaterlaus57dde612013-10-18 10:02:28 -070036 public static DatapathId of(byte[] bytes) {
37 return new DatapathId(Longs.fromByteArray(bytes));
38 }
Sovietacedfbb87082015-07-21 17:55:59 -040039
Sovietacedbb0f0cc2015-07-20 22:40:02 -040040 /**
Sovietacedfbb87082015-07-21 17:55:59 -040041 * Creates a {@link DatapathId} from an {@link MacAddress}. This factory
Sovietacedbb0f0cc2015-07-20 22:40:02 -040042 * method assumes that the {@link DatapathId} is composed of two zero bytes
43 * appended by the {@link MacAddress}'s 6 bytes.
44 * @param mac the {@link MacAddress} to create the {@link DatapathId} from
45 * @return a {@link DatapathId} derived from the supplied {@link MacAddress}
46 */
47 public static DatapathId of(MacAddress mac) {
48 byte[] zeroBytes = new byte[]{0,0};
49 byte[] fullBytes = Bytes.concat(zeroBytes, mac.getBytes());
50 return DatapathId.of(fullBytes);
51 }
Rob Vaterlaus57dde612013-10-18 10:02:28 -070052
Rob Vaterlausfeee3712013-09-30 11:24:19 -070053 public long getLong() {
54 return rawValue;
55 }
56
57 public U64 getUnsignedLong() {
58 return U64.of(rawValue);
59 }
60
Rob Vaterlaus57dde612013-10-18 10:02:28 -070061 public byte[] getBytes() {
62 return Longs.toByteArray(rawValue);
63 }
64
Rob Vaterlausfeee3712013-09-30 11:24:19 -070065 @Override
66 public String toString() {
67 return HexString.toHexString(rawValue);
68 }
69
70 @Override
71 public int hashCode() {
72 final int prime = 31;
73 int result = 1;
74 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
75 return result;
76 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj)
81 return true;
82 if (obj == null)
83 return false;
84 if (getClass() != obj.getClass())
85 return false;
86 DatapathId other = (DatapathId) obj;
87 if (rawValue != other.rawValue)
88 return false;
89 return true;
90 }
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070091
92 @Override
93 public void putTo(PrimitiveSink sink) {
94 sink.putLong(rawValue);
95 }
Andreas Wundsamedb1f202013-10-17 10:59:11 -070096
Andreas Wundsam73e2ec42013-10-18 17:52:18 -070097 @Override
Andreas Wundsam72977ce2013-10-15 21:31:59 -070098 public int compareTo(DatapathId o) {
99 return UnsignedLongs.compare(rawValue, o.rawValue);
100 }
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700101}