blob: 85b0b3745d4ef3d9d5ff314c7143d000694e05dd [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;
Sovietacedbb0f0cc2015-07-20 22:40:02 -04004import org.projectfloodlight.openflow.types.DatapathId;
5import org.projectfloodlight.openflow.types.MacAddress;
Rob Vaterlausfeee3712013-09-30 11:24:19 -07006import org.projectfloodlight.openflow.util.HexString;
7
Sovietacedbb0f0cc2015-07-20 22:40:02 -04008import com.google.common.base.Preconditions;
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -07009import com.google.common.hash.PrimitiveSink;
Sovietacedbb0f0cc2015-07-20 22:40:02 -040010import com.google.common.primitives.Bytes;
Rob Vaterlaus57dde612013-10-18 10:02:28 -070011import com.google.common.primitives.Longs;
Andreas Wundsam72977ce2013-10-15 21:31:59 -070012import com.google.common.primitives.UnsignedLongs;
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070013
Rob Vaterlausfeee3712013-09-30 11:24:19 -070014/**
15 * Abstraction of a datapath ID that can be set and/or accessed as either a
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070016 * long value or a colon-separated string. Immutable
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070017 *
kjwon157bc85402015-02-12 15:07:42 +090018 * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >}
Rob Vaterlausfeee3712013-09-30 11:24:19 -070019 */
Rob Vaterlaus4ae76f42013-10-18 19:40:48 -070020@Immutable
Andreas Wundsamedb1f202013-10-17 10:59:11 -070021public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> {
Rob Vaterlausfeee3712013-09-30 11:24:19 -070022
23 public static final DatapathId NONE = new DatapathId(0);
24
25 private final long rawValue;
26
27 private DatapathId(long rawValue) {
28 this.rawValue = rawValue;
29 }
30
31 public static DatapathId of(long rawValue) {
32 return new DatapathId(rawValue);
33 }
34
35 public static DatapathId of(String s) {
36 return new DatapathId(HexString.toLong(s));
37 }
38
Rob Vaterlaus57dde612013-10-18 10:02:28 -070039 public static DatapathId of(byte[] bytes) {
40 return new DatapathId(Longs.fromByteArray(bytes));
41 }
Sovietacedbb0f0cc2015-07-20 22:40:02 -040042
43 /**
44 * Creates a {@link DatapathId} from an {@link MacAddress}. This factory
45 * method assumes that the {@link DatapathId} is composed of two zero bytes
46 * appended by the {@link MacAddress}'s 6 bytes.
47 * @param mac the {@link MacAddress} to create the {@link DatapathId} from
48 * @return a {@link DatapathId} derived from the supplied {@link MacAddress}
49 */
50 public static DatapathId of(MacAddress mac) {
51 byte[] zeroBytes = new byte[]{0,0};
52 byte[] fullBytes = Bytes.concat(zeroBytes, mac.getBytes());
53 return DatapathId.of(fullBytes);
54 }
Rob Vaterlaus57dde612013-10-18 10:02:28 -070055
Rob Vaterlausfeee3712013-09-30 11:24:19 -070056 public long getLong() {
57 return rawValue;
58 }
59
60 public U64 getUnsignedLong() {
61 return U64.of(rawValue);
62 }
63
Rob Vaterlaus57dde612013-10-18 10:02:28 -070064 public byte[] getBytes() {
65 return Longs.toByteArray(rawValue);
66 }
67
Rob Vaterlausfeee3712013-09-30 11:24:19 -070068 @Override
69 public String toString() {
70 return HexString.toHexString(rawValue);
71 }
72
73 @Override
74 public int hashCode() {
75 final int prime = 31;
76 int result = 1;
77 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
78 return result;
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (this == obj)
84 return true;
85 if (obj == null)
86 return false;
87 if (getClass() != obj.getClass())
88 return false;
89 DatapathId other = (DatapathId) obj;
90 if (rawValue != other.rawValue)
91 return false;
92 return true;
93 }
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -070094
95 @Override
96 public void putTo(PrimitiveSink sink) {
97 sink.putLong(rawValue);
98 }
Andreas Wundsamedb1f202013-10-17 10:59:11 -070099
Andreas Wundsam73e2ec42013-10-18 17:52:18 -0700100 @Override
Andreas Wundsam72977ce2013-10-15 21:31:59 -0700101 public int compareTo(DatapathId o) {
102 return UnsignedLongs.compare(rawValue, o.rawValue);
103 }
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700104}