Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 3 | import javax.annotation.Nonnull; |
| 4 | |
Rob Vaterlaus | 4ae76f4 | 2013-10-18 19:40:48 -0700 | [diff] [blame] | 5 | import org.projectfloodlight.openflow.annotations.Immutable; |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 6 | import org.projectfloodlight.openflow.util.HexString; |
| 7 | |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 8 | import com.google.common.hash.PrimitiveSink; |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 9 | import com.google.common.primitives.Longs; |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 10 | import com.google.common.primitives.UnsignedLongs; |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 11 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 12 | /** |
| 13 | * Abstraction of a datapath ID that can be set and/or accessed as either a |
Rob Vaterlaus | 4ae76f4 | 2013-10-18 19:40:48 -0700 | [diff] [blame] | 14 | * long value or a colon-separated string. Immutable |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 15 | * |
kjwon15 | 7bc8540 | 2015-02-12 15:07:42 +0900 | [diff] [blame] | 16 | * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >} |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 17 | */ |
Rob Vaterlaus | 4ae76f4 | 2013-10-18 19:40:48 -0700 | [diff] [blame] | 18 | @Immutable |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 19 | public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> { |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 20 | |
| 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 Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 37 | public static DatapathId of(byte[] bytes) { |
| 38 | return new DatapathId(Longs.fromByteArray(bytes)); |
| 39 | } |
Sovietaced | fbb8708 | 2015-07-21 17:55:59 -0400 | [diff] [blame] | 40 | |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 41 | /** |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 42 | * Creates a {@link DatapathId} from a {@link MacAddress}. |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 43 | * @param mac the {@link MacAddress} to create the {@link DatapathId} from |
| 44 | * @return a {@link DatapathId} derived from the supplied {@link MacAddress} |
| 45 | */ |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame] | 46 | public static DatapathId of(@Nonnull MacAddress mac) { |
Sovietaced | 8fe84f3 | 2015-07-22 22:17:31 -0400 | [diff] [blame] | 47 | return DatapathId.of(mac.getLong()); |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 48 | } |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 49 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 50 | public long getLong() { |
| 51 | return rawValue; |
| 52 | } |
| 53 | |
| 54 | public U64 getUnsignedLong() { |
| 55 | return U64.of(rawValue); |
| 56 | } |
| 57 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 58 | public byte[] getBytes() { |
| 59 | return Longs.toByteArray(rawValue); |
| 60 | } |
| 61 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 62 | @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 Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 88 | |
| 89 | @Override |
| 90 | public void putTo(PrimitiveSink sink) { |
| 91 | sink.putLong(rawValue); |
| 92 | } |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 93 | |
Andreas Wundsam | 73e2ec4 | 2013-10-18 17:52:18 -0700 | [diff] [blame] | 94 | @Override |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 95 | public int compareTo(DatapathId o) { |
| 96 | return UnsignedLongs.compare(rawValue, o.rawValue); |
| 97 | } |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 98 | } |