blob: 9eac361897490ef783334d153528f42b9f3115cd [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.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() {
Sho SHIMIZU87114512015-05-08 14:31:00 -070094 return Long.hashCode(value);
tom7ef8ff92014-09-17 13:08:06 -070095 }
96
97 /**
98 * Returns DPID created from the given device URI.
99 *
100 * @param uri device URI
101 * @return dpid
102 */
103 public static Dpid dpid(URI uri) {
104 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
105 return new Dpid(fromHex(uri.getSchemeSpecificPart()));
106 }
107
108 /**
109 * Produces device URI from the given DPID.
110 *
111 * @param dpid device dpid
112 * @return device URI
113 */
114 public static URI uri(Dpid dpid) {
115 return uri(dpid.value);
116 }
117
118 /**
119 * Produces device URI from the given DPID long.
120 *
121 * @param value device dpid as long
122 * @return device URI
123 */
124 public static URI uri(long value) {
125 try {
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700126 return new URI(SCHEME + ":" + toHex(value));
tom7ef8ff92014-09-17 13:08:06 -0700127 } catch (URISyntaxException e) {
128 return null;
129 }
130 }
131
132}