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