blob: 6258f3ac1687e76b4721def1183d713062f4d39a [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
tom9c94c5b2014-09-17 13:14:42 -070016package org.onlab.onos.openflow.controller;
tom7ef8ff92014-09-17 13:08:06 -070017
18import org.projectfloodlight.openflow.util.HexString;
19
20import java.net.URI;
21import java.net.URISyntaxException;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static org.onlab.util.Tools.fromHex;
25import static org.onlab.util.Tools.toHex;
26
27/**
28 * The class representing a network switch DPID.
29 * This class is immutable.
30 */
31public 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}