blob: 708eaa9cef94166edba47e77477c6fc5925127a8 [file] [log] [blame]
Rob Vaterlausfeee3712013-09-30 11:24:19 -07001package org.projectfloodlight.openflow.types;
2
Sovietaced5b75b7c2015-07-21 18:39:53 -04003import javax.annotation.Nonnull;
4
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -07005import org.projectfloodlight.openflow.annotations.Immutable;
Rob Vaterlausfeee3712013-09-30 11:24:19 -07006import org.projectfloodlight.openflow.util.HexString;
7
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -07008import com.google.common.hash.PrimitiveSink;
Rob Vaterlaus57dde612013-10-18 10:02:28 -07009import com.google.common.primitives.Longs;
Andreas Wundsam72977ce2013-10-15 21:31:59 -070010import com.google.common.primitives.UnsignedLongs;
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070011
Rob Vaterlausfeee3712013-09-30 11:24:19 -070012/**
13 * Abstraction of a datapath ID that can be set and/or accessed as either a
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070014 * long value or a colon-separated string. Immutable
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070015 *
kjwon157bc85402015-02-12 15:07:42 +090016 * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >}
Rob Vaterlausfeee3712013-09-30 11:24:19 -070017 */
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070018@Immutable
Andreas Wundsamedb1f202013-10-17 10:59:11 -070019public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> {
Rob Vaterlausfeee3712013-09-30 11:24:19 -070020
21 public static final DatapathId NONE = new DatapathId(0);
22
23 private final long rawValue;
24
25 private DatapathId(long rawValue) {
26 this.rawValue = rawValue;
27 }
28
29 public static DatapathId of(long rawValue) {
30 return new DatapathId(rawValue);
31 }
32
33 public static DatapathId of(String s) {
34 return new DatapathId(HexString.toLong(s));
35 }
36
Rob Vaterlaus57dde612013-10-18 10:02:28 -070037 public static DatapathId of(byte[] bytes) {
38 return new DatapathId(Longs.fromByteArray(bytes));
39 }
Sovietacedfbb87082015-07-21 17:55:59 -040040
Sovietacedbb0f0cc2015-07-20 22:40:02 -040041 /**
Sovietaced8fe84f32015-07-22 22:17:31 -040042 * Creates a {@link DatapathId} from a {@link MacAddress}.
Sovietacedbb0f0cc2015-07-20 22:40:02 -040043 * @param mac the {@link MacAddress} to create the {@link DatapathId} from
44 * @return a {@link DatapathId} derived from the supplied {@link MacAddress}
45 */
Sovietaced5b75b7c2015-07-21 18:39:53 -040046 public static DatapathId of(@Nonnull MacAddress mac) {
Sovietaced8fe84f32015-07-22 22:17:31 -040047 return DatapathId.of(mac.getLong());
Sovietacedbb0f0cc2015-07-20 22:40:02 -040048 }
Rob Vaterlaus57dde612013-10-18 10:02:28 -070049
Rob Vaterlausfeee3712013-09-30 11:24:19 -070050 public long getLong() {
51 return rawValue;
52 }
53
54 public U64 getUnsignedLong() {
55 return U64.of(rawValue);
56 }
57
Rob Vaterlaus57dde612013-10-18 10:02:28 -070058 public byte[] getBytes() {
59 return Longs.toByteArray(rawValue);
60 }
61
Rob Vaterlausfeee3712013-09-30 11:24:19 -070062 @Override
63 public String toString() {
64 return HexString.toHexString(rawValue);
65 }
66
67 @Override
68 public int hashCode() {
69 final int prime = 31;
70 int result = 1;
71 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
72 return result;
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (this == obj)
78 return true;
79 if (obj == null)
80 return false;
81 if (getClass() != obj.getClass())
82 return false;
83 DatapathId other = (DatapathId) obj;
84 if (rawValue != other.rawValue)
85 return false;
86 return true;
87 }
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070088
89 @Override
90 public void putTo(PrimitiveSink sink) {
91 sink.putLong(rawValue);
92 }
Andreas Wundsamedb1f202013-10-17 10:59:11 -070093
Andreas Wundsam73e2ec42013-10-18 17:52:18 -070094 @Override
Andreas Wundsam72977ce2013-10-15 21:31:59 -070095 public int compareTo(DatapathId o) {
96 return UnsignedLongs.compare(rawValue, o.rawValue);
97 }
Rob Vaterlausfeee3712013-09-30 11:24:19 -070098}