tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.openflow.controller; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 2 | |
| 3 | import org.projectfloodlight.openflow.util.HexString; |
| 4 | |
| 5 | import java.net.URI; |
| 6 | import java.net.URISyntaxException; |
| 7 | |
| 8 | import static com.google.common.base.Preconditions.checkArgument; |
| 9 | import static org.onlab.util.Tools.fromHex; |
| 10 | import static org.onlab.util.Tools.toHex; |
| 11 | |
| 12 | /** |
| 13 | * The class representing a network switch DPID. |
| 14 | * This class is immutable. |
| 15 | */ |
| 16 | public final class Dpid { |
| 17 | |
| 18 | private static final String SCHEME = "of"; |
| 19 | private static final long UNKNOWN = 0; |
| 20 | private final long value; |
| 21 | |
| 22 | /** |
| 23 | * Default constructor. |
| 24 | */ |
| 25 | public Dpid() { |
| 26 | this.value = Dpid.UNKNOWN; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Constructor from a long value. |
| 31 | * |
| 32 | * @param value the value to use. |
| 33 | */ |
| 34 | public Dpid(long value) { |
| 35 | this.value = value; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Constructor from a string. |
| 40 | * |
| 41 | * @param value the value to use. |
| 42 | */ |
| 43 | public Dpid(String value) { |
| 44 | this.value = HexString.toLong(value); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the value of the DPID. |
| 49 | * |
| 50 | * @return the value of the DPID. |
| 51 | */ |
| 52 | public long value() { |
| 53 | return value; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Convert the DPID value to a ':' separated hexadecimal string. |
| 58 | * |
| 59 | * @return the DPID value as a ':' separated hexadecimal string. |
| 60 | */ |
| 61 | @Override |
| 62 | public String toString() { |
| 63 | return HexString.toHexString(this.value); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean equals(Object other) { |
| 68 | if (!(other instanceof Dpid)) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | Dpid otherDpid = (Dpid) other; |
| 73 | |
| 74 | return value == otherDpid.value; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int hashCode() { |
| 79 | int hash = 17; |
| 80 | hash += 31 * hash + (int) (value ^ value >>> 32); |
| 81 | return hash; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns DPID created from the given device URI. |
| 86 | * |
| 87 | * @param uri device URI |
| 88 | * @return dpid |
| 89 | */ |
| 90 | public static Dpid dpid(URI uri) { |
| 91 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
| 92 | return new Dpid(fromHex(uri.getSchemeSpecificPart())); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Produces device URI from the given DPID. |
| 97 | * |
| 98 | * @param dpid device dpid |
| 99 | * @return device URI |
| 100 | */ |
| 101 | public static URI uri(Dpid dpid) { |
| 102 | return uri(dpid.value); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Produces device URI from the given DPID long. |
| 107 | * |
| 108 | * @param value device dpid as long |
| 109 | * @return device URI |
| 110 | */ |
| 111 | public static URI uri(long value) { |
| 112 | try { |
| 113 | return new URI(SCHEME, toHex(value), null); |
| 114 | } catch (URISyntaxException e) { |
| 115 | return null; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | } |