blob: e1080f269f06c5174f41bfae65222914234419df [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
7 *
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.
15 */
tom0eb04ca2014-08-25 14:34:51 -070016package org.onlab.onos.net;
17
18import java.net.URI;
tom545708e2014-10-09 17:10:02 -070019import java.util.Objects;
tom0eb04ca2014-08-25 14:34:51 -070020
21/**
tom64b7aac2014-08-26 00:18:21 -070022 * Immutable representation of a device identity.
tom0eb04ca2014-08-25 14:34:51 -070023 */
tomca20e0c2014-09-03 23:22:24 -070024public final class DeviceId extends ElementId {
tom0eb04ca2014-08-25 14:34:51 -070025
tom545708e2014-10-09 17:10:02 -070026 /**
27 * Represents either no device, or an unspecified device.
28 */
29 public static final DeviceId NONE = deviceId("none:none");
30
31 private final URI uri;
32 private final String str;
33
tomca20e0c2014-09-03 23:22:24 -070034 // Public construction is prohibited
35 private DeviceId(URI uri) {
tom545708e2014-10-09 17:10:02 -070036 this.uri = uri;
37 this.str = uri.toString();
38 }
39
40
41 // Default constructor for serialization
42 protected DeviceId() {
43 this.uri = null;
44 this.str = null;
tomca20e0c2014-09-03 23:22:24 -070045 }
46
tom0eb04ca2014-08-25 14:34:51 -070047 /**
tomb36046e2014-08-27 00:22:24 -070048 * Creates a device id using the supplied URI.
tom0eb04ca2014-08-25 14:34:51 -070049 *
tomca20e0c2014-09-03 23:22:24 -070050 * @param uri device URI
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080051 * @return DeviceId
tom0eb04ca2014-08-25 14:34:51 -070052 */
tomca20e0c2014-09-03 23:22:24 -070053 public static DeviceId deviceId(URI uri) {
54 return new DeviceId(uri);
55 }
56
57 /**
58 * Creates a device id using the supplied URI string.
59 *
60 * @param string device URI string
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080061 * @return DeviceId
tomca20e0c2014-09-03 23:22:24 -070062 */
63 public static DeviceId deviceId(String string) {
tom568581d2014-09-08 20:13:36 -070064 return deviceId(URI.create(string));
tom64b7aac2014-08-26 00:18:21 -070065 }
66
tom545708e2014-10-09 17:10:02 -070067 /**
68 * Returns the backing URI.
69 *
70 * @return backing URI
71 */
72 public URI uri() {
73 return uri;
74 }
75
76 @Override
77 public int hashCode() {
78 return Objects.hash(str);
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (this == obj) {
84 return true;
85 }
86 if (obj instanceof DeviceId) {
87 final DeviceId that = (DeviceId) obj;
88 return this.getClass() == that.getClass() &&
89 Objects.equals(this.str, that.str);
90 }
91 return false;
92 }
93
94 @Override
95 public String toString() {
96 return str;
97 }
98
tom0eb04ca2014-08-25 14:34:51 -070099}