blob: 2e7a59b35adc48736c5303faa645ae5cf168d192 [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;
Sovietacedbb0f0cc2015-07-20 22:40:02 -04009import com.google.common.primitives.Bytes;
Rob Vaterlaus57dde612013-10-18 10:02:28 -070010import com.google.common.primitives.Longs;
Andreas Wundsam72977ce2013-10-15 21:31:59 -070011import com.google.common.primitives.UnsignedLongs;
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070012
Rob Vaterlausfeee3712013-09-30 11:24:19 -070013/**
14 * Abstraction of a datapath ID that can be set and/or accessed as either a
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070015 * long value or a colon-separated string. Immutable
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070016 *
kjwon157bc85402015-02-12 15:07:42 +090017 * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >}
Rob Vaterlausfeee3712013-09-30 11:24:19 -070018 */
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070019@Immutable
Andreas Wundsamedb1f202013-10-17 10:59:11 -070020public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> {
Rob Vaterlausfeee3712013-09-30 11:24:19 -070021
22 public static final DatapathId NONE = new DatapathId(0);
23
24 private final long rawValue;
25
26 private DatapathId(long rawValue) {
27 this.rawValue = rawValue;
28 }
29
30 public static DatapathId of(long rawValue) {
31 return new DatapathId(rawValue);
32 }
33
34 public static DatapathId of(String s) {
35 return new DatapathId(HexString.toLong(s));
36 }
37
Rob Vaterlaus57dde612013-10-18 10:02:28 -070038 public static DatapathId of(byte[] bytes) {
39 return new DatapathId(Longs.fromByteArray(bytes));
40 }
Sovietacedfbb87082015-07-21 17:55:59 -040041
Sovietacedbb0f0cc2015-07-20 22:40:02 -040042 /**
Sovietaced5b75b7c2015-07-21 18:39:53 -040043 * Creates a {@link DatapathId} from a {@link MacAddress}. This factory
Sovietacedbb0f0cc2015-07-20 22:40:02 -040044 * method assumes that the {@link DatapathId} is composed of two zero bytes
45 * appended by the {@link MacAddress}'s 6 bytes.
46 * @param mac the {@link MacAddress} to create the {@link DatapathId} from
47 * @return a {@link DatapathId} derived from the supplied {@link MacAddress}
48 */
Sovietaced5b75b7c2015-07-21 18:39:53 -040049 public static DatapathId of(@Nonnull MacAddress mac) {
Sovietacedbb0f0cc2015-07-20 22:40:02 -040050 byte[] zeroBytes = new byte[]{0,0};
51 byte[] fullBytes = Bytes.concat(zeroBytes, mac.getBytes());
52 return DatapathId.of(fullBytes);
53 }
Rob Vaterlaus57dde612013-10-18 10:02:28 -070054
Rob Vaterlausfeee3712013-09-30 11:24:19 -070055 public long getLong() {
56 return rawValue;
57 }
58
59 public U64 getUnsignedLong() {
60 return U64.of(rawValue);
61 }
62
Rob Vaterlaus57dde612013-10-18 10:02:28 -070063 public byte[] getBytes() {
64 return Longs.toByteArray(rawValue);
65 }
66
Rob Vaterlausfeee3712013-09-30 11:24:19 -070067 @Override
68 public String toString() {
69 return HexString.toHexString(rawValue);
70 }
71
72 @Override
73 public int hashCode() {
74 final int prime = 31;
75 int result = 1;
76 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
77 return result;
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (this == obj)
83 return true;
84 if (obj == null)
85 return false;
86 if (getClass() != obj.getClass())
87 return false;
88 DatapathId other = (DatapathId) obj;
89 if (rawValue != other.rawValue)
90 return false;
91 return true;
92 }
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070093
94 @Override
95 public void putTo(PrimitiveSink sink) {
96 sink.putLong(rawValue);
97 }
Andreas Wundsamedb1f202013-10-17 10:59:11 -070098
Andreas Wundsam73e2ec42013-10-18 17:52:18 -070099 @Override
Andreas Wundsam72977ce2013-10-15 21:31:59 -0700100 public int compareTo(DatapathId o) {
101 return UnsignedLongs.compare(rawValue, o.rawValue);
102 }
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700103}