Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 16 | package org.onlab.onos.openflow.controller; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
| 18 | import org.projectfloodlight.openflow.util.HexString; |
| 19 | |
| 20 | import java.net.URI; |
| 21 | import java.net.URISyntaxException; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | import static org.onlab.util.Tools.fromHex; |
| 25 | import static org.onlab.util.Tools.toHex; |
| 26 | |
| 27 | /** |
| 28 | * The class representing a network switch DPID. |
| 29 | * This class is immutable. |
| 30 | */ |
| 31 | public final class Dpid { |
| 32 | |
| 33 | private static final String SCHEME = "of"; |
| 34 | private static final long UNKNOWN = 0; |
| 35 | private final long value; |
| 36 | |
| 37 | /** |
| 38 | * Default constructor. |
| 39 | */ |
| 40 | public Dpid() { |
| 41 | this.value = Dpid.UNKNOWN; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Constructor from a long value. |
| 46 | * |
| 47 | * @param value the value to use. |
| 48 | */ |
| 49 | public Dpid(long value) { |
| 50 | this.value = value; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Constructor from a string. |
| 55 | * |
| 56 | * @param value the value to use. |
| 57 | */ |
| 58 | public Dpid(String value) { |
| 59 | this.value = HexString.toLong(value); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the value of the DPID. |
| 64 | * |
| 65 | * @return the value of the DPID. |
| 66 | */ |
| 67 | public long value() { |
| 68 | return value; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Convert the DPID value to a ':' separated hexadecimal string. |
| 73 | * |
| 74 | * @return the DPID value as a ':' separated hexadecimal string. |
| 75 | */ |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return HexString.toHexString(this.value); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean equals(Object other) { |
| 83 | if (!(other instanceof Dpid)) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | Dpid otherDpid = (Dpid) other; |
| 88 | |
| 89 | return value == otherDpid.value; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
| 94 | int hash = 17; |
| 95 | hash += 31 * hash + (int) (value ^ value >>> 32); |
| 96 | return hash; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns DPID created from the given device URI. |
| 101 | * |
| 102 | * @param uri device URI |
| 103 | * @return dpid |
| 104 | */ |
| 105 | public static Dpid dpid(URI uri) { |
| 106 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
| 107 | return new Dpid(fromHex(uri.getSchemeSpecificPart())); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Produces device URI from the given DPID. |
| 112 | * |
| 113 | * @param dpid device dpid |
| 114 | * @return device URI |
| 115 | */ |
| 116 | public static URI uri(Dpid dpid) { |
| 117 | return uri(dpid.value); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Produces device URI from the given DPID long. |
| 122 | * |
| 123 | * @param value device dpid as long |
| 124 | * @return device URI |
| 125 | */ |
| 126 | public static URI uri(long value) { |
| 127 | try { |
| 128 | return new URI(SCHEME, toHex(value), null); |
| 129 | } catch (URISyntaxException e) { |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |