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; |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 9 | import com.google.common.primitives.Bytes; |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 10 | import com.google.common.primitives.Longs; |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 11 | import com.google.common.primitives.UnsignedLongs; |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 12 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 13 | /** |
| 14 | * 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] | 15 | * long value or a colon-separated string. Immutable |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 16 | * |
kjwon15 | 7bc8540 | 2015-02-12 15:07:42 +0900 | [diff] [blame] | 17 | * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >} |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 18 | */ |
Rob Vaterlaus | 4ae76f4 | 2013-10-18 19:40:48 -0700 | [diff] [blame] | 19 | @Immutable |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 20 | public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> { |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 21 | |
| 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 Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 38 | public static DatapathId of(byte[] bytes) { |
| 39 | return new DatapathId(Longs.fromByteArray(bytes)); |
| 40 | } |
Sovietaced | fbb8708 | 2015-07-21 17:55:59 -0400 | [diff] [blame] | 41 | |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 42 | /** |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame^] | 43 | * Creates a {@link DatapathId} from a {@link MacAddress}. This factory |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 44 | * 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 | */ |
Sovietaced | 5b75b7c | 2015-07-21 18:39:53 -0400 | [diff] [blame^] | 49 | public static DatapathId of(@Nonnull MacAddress mac) { |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 50 | byte[] zeroBytes = new byte[]{0,0}; |
| 51 | byte[] fullBytes = Bytes.concat(zeroBytes, mac.getBytes()); |
| 52 | return DatapathId.of(fullBytes); |
| 53 | } |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 54 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 55 | public long getLong() { |
| 56 | return rawValue; |
| 57 | } |
| 58 | |
| 59 | public U64 getUnsignedLong() { |
| 60 | return U64.of(rawValue); |
| 61 | } |
| 62 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 63 | public byte[] getBytes() { |
| 64 | return Longs.toByteArray(rawValue); |
| 65 | } |
| 66 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 67 | @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 Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | public void putTo(PrimitiveSink sink) { |
| 96 | sink.putLong(rawValue); |
| 97 | } |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 98 | |
Andreas Wundsam | 73e2ec4 | 2013-10-18 17:52:18 -0700 | [diff] [blame] | 99 | @Override |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 100 | public int compareTo(DatapathId o) { |
| 101 | return UnsignedLongs.compare(rawValue, o.rawValue); |
| 102 | } |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 103 | } |