blob: 072ba28f7ad6d528b9989774d70344b07bd8b8bd [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.onlab.onos.net;
2
3import java.net.URI;
tom545708e2014-10-09 17:10:02 -07004import java.util.Objects;
tom0eb04ca2014-08-25 14:34:51 -07005
6/**
tom64b7aac2014-08-26 00:18:21 -07007 * Immutable representation of a device identity.
tom0eb04ca2014-08-25 14:34:51 -07008 */
tomca20e0c2014-09-03 23:22:24 -07009public final class DeviceId extends ElementId {
tom0eb04ca2014-08-25 14:34:51 -070010
tom545708e2014-10-09 17:10:02 -070011 /**
12 * Represents either no device, or an unspecified device.
13 */
14 public static final DeviceId NONE = deviceId("none:none");
15
16 private final URI uri;
17 private final String str;
18
tomca20e0c2014-09-03 23:22:24 -070019 // Public construction is prohibited
20 private DeviceId(URI uri) {
tom545708e2014-10-09 17:10:02 -070021 this.uri = uri;
22 this.str = uri.toString();
23 }
24
25
26 // Default constructor for serialization
27 protected DeviceId() {
28 this.uri = null;
29 this.str = null;
tomca20e0c2014-09-03 23:22:24 -070030 }
31
tom0eb04ca2014-08-25 14:34:51 -070032 /**
tomb36046e2014-08-27 00:22:24 -070033 * Creates a device id using the supplied URI.
tom0eb04ca2014-08-25 14:34:51 -070034 *
tomca20e0c2014-09-03 23:22:24 -070035 * @param uri device URI
tom0eb04ca2014-08-25 14:34:51 -070036 */
tomca20e0c2014-09-03 23:22:24 -070037 public static DeviceId deviceId(URI uri) {
38 return new DeviceId(uri);
39 }
40
41 /**
42 * Creates a device id using the supplied URI string.
43 *
44 * @param string device URI string
45 */
46 public static DeviceId deviceId(String string) {
tom568581d2014-09-08 20:13:36 -070047 return deviceId(URI.create(string));
tom64b7aac2014-08-26 00:18:21 -070048 }
49
tom545708e2014-10-09 17:10:02 -070050 /**
51 * Returns the backing URI.
52 *
53 * @return backing URI
54 */
55 public URI uri() {
56 return uri;
57 }
58
59 @Override
60 public int hashCode() {
61 return Objects.hash(str);
62 }
63
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj) {
67 return true;
68 }
69 if (obj instanceof DeviceId) {
70 final DeviceId that = (DeviceId) obj;
71 return this.getClass() == that.getClass() &&
72 Objects.equals(this.str, that.str);
73 }
74 return false;
75 }
76
77 @Override
78 public String toString() {
79 return str;
80 }
81
tom0eb04ca2014-08-25 14:34:51 -070082}