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; |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 7 | import com.google.common.primitives.Longs; |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 8 | import com.google.common.primitives.UnsignedLongs; |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 9 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 10 | /** |
| 11 | * 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] | 12 | * long value or a colon-separated string. Immutable |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 13 | * |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 14 | * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com> |
| 15 | */ |
Rob Vaterlaus | 4ae76f4 | 2013-10-18 19:40:48 -0700 | [diff] [blame] | 16 | @Immutable |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 17 | public class DatapathId implements PrimitiveSinkable, Comparable<DatapathId> { |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 18 | |
| 19 | public static final DatapathId NONE = new DatapathId(0); |
| 20 | |
| 21 | private final long rawValue; |
| 22 | |
| 23 | private DatapathId(long rawValue) { |
| 24 | this.rawValue = rawValue; |
| 25 | } |
| 26 | |
| 27 | public static DatapathId of(long rawValue) { |
| 28 | return new DatapathId(rawValue); |
| 29 | } |
| 30 | |
| 31 | public static DatapathId of(String s) { |
| 32 | return new DatapathId(HexString.toLong(s)); |
| 33 | } |
| 34 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 35 | public static DatapathId of(byte[] bytes) { |
| 36 | return new DatapathId(Longs.fromByteArray(bytes)); |
| 37 | } |
| 38 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 39 | public long getLong() { |
| 40 | return rawValue; |
| 41 | } |
| 42 | |
| 43 | public U64 getUnsignedLong() { |
| 44 | return U64.of(rawValue); |
| 45 | } |
| 46 | |
Rob Vaterlaus | 57dde61 | 2013-10-18 10:02:28 -0700 | [diff] [blame] | 47 | public byte[] getBytes() { |
| 48 | return Longs.toByteArray(rawValue); |
| 49 | } |
| 50 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 51 | @Override |
| 52 | public String toString() { |
| 53 | return HexString.toHexString(rawValue); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public int hashCode() { |
| 58 | final int prime = 31; |
| 59 | int result = 1; |
| 60 | result = prime * result + (int) (rawValue ^ (rawValue >>> 32)); |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean equals(Object obj) { |
| 66 | if (this == obj) |
| 67 | return true; |
| 68 | if (obj == null) |
| 69 | return false; |
| 70 | if (getClass() != obj.getClass()) |
| 71 | return false; |
| 72 | DatapathId other = (DatapathId) obj; |
| 73 | if (rawValue != other.rawValue) |
| 74 | return false; |
| 75 | return true; |
| 76 | } |
Andreas Wundsam | 5ea1aca | 2013-10-07 17:00:24 -0700 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public void putTo(PrimitiveSink sink) { |
| 80 | sink.putLong(rawValue); |
| 81 | } |
Andreas Wundsam | edb1f20 | 2013-10-17 10:59:11 -0700 | [diff] [blame] | 82 | |
Andreas Wundsam | 73e2ec4 | 2013-10-18 17:52:18 -0700 | [diff] [blame] | 83 | @Override |
Andreas Wundsam | 72977ce | 2013-10-15 21:31:59 -0700 | [diff] [blame] | 84 | public int compareTo(DatapathId o) { |
| 85 | return UnsignedLongs.compare(rawValue, o.rawValue); |
| 86 | } |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 87 | } |