blob: fe52fc370555da4c07ebff7b578c573f3478dd3c [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;
Rob Vaterlaus57dde612013-10-18 10:02:28 -07007import com.google.common.primitives.Longs;
Andreas Wundsam72977ce2013-10-15 21:31:59 -07008import com.google.common.primitives.UnsignedLongs;
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -07009
Rob Vaterlausfeee3712013-09-30 11:24:19 -070010/**
11 * Abstraction of a datapath ID that can be set and/or accessed as either a
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070012 * long value or a colon-separated string. Immutable
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070013 *
kjwon157bc85402015-02-12 15:07:42 +090014 * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >}
Rob Vaterlausfeee3712013-09-30 11:24:19 -070015 */
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070016@Immutable
Andreas Wundsamedb1f202013-10-17 10:59:11 -070017public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> {
Rob Vaterlausfeee3712013-09-30 11:24:19 -070018
19 public static final DatapathId NONE = new DatapathId(0);
20
21 private final long rawValue;
22
23 private DatapathId(long rawValue) {
24 this.rawValue = rawValue;
25 }
26
27 public static DatapathId of(long rawValue) {
28 return new DatapathId(rawValue);
29 }
30
31 public static DatapathId of(String s) {
32 return new DatapathId(HexString.toLong(s));
33 }
34
Rob Vaterlaus57dde612013-10-18 10:02:28 -070035 public static DatapathId of(byte[] bytes) {
36 return new DatapathId(Longs.fromByteArray(bytes));
37 }
38
Rob Vaterlausfeee3712013-09-30 11:24:19 -070039 public long getLong() {
40 return rawValue;
41 }
42
43 public U64 getUnsignedLong() {
44 return U64.of(rawValue);
45 }
46
Rob Vaterlaus57dde612013-10-18 10:02:28 -070047 public byte[] getBytes() {
48 return Longs.toByteArray(rawValue);
49 }
50
Rob Vaterlausfeee3712013-09-30 11:24:19 -070051 @Override
52 public String toString() {
53 return HexString.toHexString(rawValue);
54 }
55
56 @Override
57 public int hashCode() {
58 final int prime = 31;
59 int result = 1;
60 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
61 return result;
62 }
63
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj)
67 return true;
68 if (obj == null)
69 return false;
70 if (getClass() != obj.getClass())
71 return false;
72 DatapathId other = (DatapathId) obj;
73 if (rawValue != other.rawValue)
74 return false;
75 return true;
76 }
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070077
78 @Override
79 public void putTo(PrimitiveSink sink) {
80 sink.putLong(rawValue);
81 }
Andreas Wundsamedb1f202013-10-17 10:59:11 -070082
Andreas Wundsam73e2ec42013-10-18 17:52:18 -070083 @Override
Andreas Wundsam72977ce2013-10-15 21:31:59 -070084 public int compareTo(DatapathId o) {
85 return UnsignedLongs.compare(rawValue, o.rawValue);
86 }
Rob Vaterlausfeee3712013-09-30 11:24:19 -070087}