blob: 5dcc076878b0815335ad1c04dcb04b7ae5e82f53 [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
tom0eb04ca2014-08-25 14:34:51 -070051 */
tomca20e0c2014-09-03 23:22:24 -070052 public static DeviceId deviceId(URI uri) {
53 return new DeviceId(uri);
54 }
55
56 /**
57 * Creates a device id using the supplied URI string.
58 *
59 * @param string device URI string
60 */
61 public static DeviceId deviceId(String string) {
tom568581d2014-09-08 20:13:36 -070062 return deviceId(URI.create(string));
tom64b7aac2014-08-26 00:18:21 -070063 }
64
tom545708e2014-10-09 17:10:02 -070065 /**
66 * Returns the backing URI.
67 *
68 * @return backing URI
69 */
70 public URI uri() {
71 return uri;
72 }
73
74 @Override
75 public int hashCode() {
76 return Objects.hash(str);
77 }
78
79 @Override
80 public boolean equals(Object obj) {
81 if (this == obj) {
82 return true;
83 }
84 if (obj instanceof DeviceId) {
85 final DeviceId that = (DeviceId) obj;
86 return this.getClass() == that.getClass() &&
87 Objects.equals(this.str, that.str);
88 }
89 return false;
90 }
91
92 @Override
93 public String toString() {
94 return str;
95 }
96
tom0eb04ca2014-08-25 14:34:51 -070097}