Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Rob Vaterlaus | 4ae76f4 | 2013-10-18 19:40:48 -0700 | [diff] [blame] | 3 | import org.projectfloodlight.openflow.annotations.Immutable; |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 4 | import org.projectfloodlight.openflow.util.HexString; |
| 5 | |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 6 | import com.google.common.hash.PrimitiveSink; |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 7 | import com.google.common.primitives.Bytes; |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 8 | import com.google.common.primitives.Longs; |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 9 | import com.google.common.primitives.UnsignedLongs; |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 10 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 11 | /** |
| 12 | * 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] | 13 | * long value or a colon-separated string. Immutable |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 14 | * |
kjwon15 | 7bc8540 | 2015-02-12 15:07:42 +0900 | [diff] [blame] | 15 | * @author Rob Vaterlaus {@literal <}rob.vaterlaus@bigswitch.com{@literal >} |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 16 | */ |
Rob Vaterlaus | 4ae76f4 | 2013-10-18 19:40:48 -0700 | [diff] [blame] | 17 | @Immutable |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 18 | public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> { |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 19 | |
| 20 | public static final DatapathId NONE = new DatapathId(0); |
| 21 | |
| 22 | private final long rawValue; |
| 23 | |
| 24 | private DatapathId(long rawValue) { |
| 25 | this.rawValue = rawValue; |
| 26 | } |
| 27 | |
| 28 | public static DatapathId of(long rawValue) { |
| 29 | return new DatapathId(rawValue); |
| 30 | } |
| 31 | |
| 32 | public static DatapathId of(String s) { |
| 33 | return new DatapathId(HexString.toLong(s)); |
| 34 | } |
| 35 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 36 | public static DatapathId of(byte[] bytes) { |
| 37 | return new DatapathId(Longs.fromByteArray(bytes)); |
| 38 | } |
Sovietaced | fbb8708 | 2015-07-21 17:55:59 -0400 | [diff] [blame^] | 39 | |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 40 | /** |
Sovietaced | fbb8708 | 2015-07-21 17:55:59 -0400 | [diff] [blame^] | 41 | * Creates a {@link DatapathId} from an {@link MacAddress}. This factory |
Sovietaced | bb0f0cc | 2015-07-20 22:40:02 -0400 | [diff] [blame] | 42 | * method assumes that the {@link DatapathId} is composed of two zero bytes |
| 43 | * appended by the {@link MacAddress}'s 6 bytes. |
| 44 | * @param mac the {@link MacAddress} to create the {@link DatapathId} from |
| 45 | * @return a {@link DatapathId} derived from the supplied {@link MacAddress} |
| 46 | */ |
| 47 | public static DatapathId of(MacAddress mac) { |
| 48 | byte[] zeroBytes = new byte[]{0,0}; |
| 49 | byte[] fullBytes = Bytes.concat(zeroBytes, mac.getBytes()); |
| 50 | return DatapathId.of(fullBytes); |
| 51 | } |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 52 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 53 | public long getLong() { |
| 54 | return rawValue; |
| 55 | } |
| 56 | |
| 57 | public U64 getUnsignedLong() { |
| 58 | return U64.of(rawValue); |
| 59 | } |
| 60 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 61 | public byte[] getBytes() { |
| 62 | return Longs.toByteArray(rawValue); |
| 63 | } |
| 64 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 65 | @Override |
| 66 | public String toString() { |
| 67 | return HexString.toHexString(rawValue); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public int hashCode() { |
| 72 | final int prime = 31; |
| 73 | int result = 1; |
| 74 | result = prime * result + (int) (rawValue ^ (rawValue >>> 32)); |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean equals(Object obj) { |
| 80 | if (this == obj) |
| 81 | return true; |
| 82 | if (obj == null) |
| 83 | return false; |
| 84 | if (getClass() != obj.getClass()) |
| 85 | return false; |
| 86 | DatapathId other = (DatapathId) obj; |
| 87 | if (rawValue != other.rawValue) |
| 88 | return false; |
| 89 | return true; |
| 90 | } |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 91 | |
| 92 | @Override |
| 93 | public void putTo(PrimitiveSink sink) { |
| 94 | sink.putLong(rawValue); |
| 95 | } |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 96 | |
Andreas Wundsam | 73e2ec4 | 2013-10-18 17:52:18 -0700 | [diff] [blame] | 97 | @Override |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 98 | public int compareTo(DatapathId o) { |
| 99 | return UnsignedLongs.compare(rawValue, o.rawValue); |
| 100 | } |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 101 | } |