blob: 09b427bcffad4a188a84b32bcbc1198821f985e5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom0eb04ca2014-08-25 14:34:51 -070017
18import java.net.URI;
tom545708e2014-10-09 17:10:02 -070019import java.util.Objects;
tom0eb04ca2014-08-25 14:34:51 -070020
Jordan Halterman0d89ea32017-06-13 10:42:36 -070021import static com.google.common.base.Preconditions.checkArgument;
22
tom0eb04ca2014-08-25 14:34:51 -070023/**
tom64b7aac2014-08-26 00:18:21 -070024 * Immutable representation of a device identity.
tom0eb04ca2014-08-25 14:34:51 -070025 */
tomca20e0c2014-09-03 23:22:24 -070026public final class DeviceId extends ElementId {
tom0eb04ca2014-08-25 14:34:51 -070027
tom545708e2014-10-09 17:10:02 -070028 /**
29 * Represents either no device, or an unspecified device.
30 */
31 public static final DeviceId NONE = deviceId("none:none");
32
Jordan Halterman0d89ea32017-06-13 10:42:36 -070033 private static final int DEVICE_ID_MAX_LENGTH = 1024;
34
tom545708e2014-10-09 17:10:02 -070035 private final URI uri;
36 private final String str;
37
tomca20e0c2014-09-03 23:22:24 -070038 // Public construction is prohibited
39 private DeviceId(URI uri) {
tom545708e2014-10-09 17:10:02 -070040 this.uri = uri;
Thomas Vachuska22925672014-11-11 17:57:53 -080041 this.str = uri.toString().toLowerCase();
tom545708e2014-10-09 17:10:02 -070042 }
43
44
45 // Default constructor for serialization
46 protected DeviceId() {
47 this.uri = null;
48 this.str = null;
tomca20e0c2014-09-03 23:22:24 -070049 }
50
tom0eb04ca2014-08-25 14:34:51 -070051 /**
tomb36046e2014-08-27 00:22:24 -070052 * Creates a device id using the supplied URI.
tom0eb04ca2014-08-25 14:34:51 -070053 *
tomca20e0c2014-09-03 23:22:24 -070054 * @param uri device URI
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080055 * @return DeviceId
tom0eb04ca2014-08-25 14:34:51 -070056 */
tomca20e0c2014-09-03 23:22:24 -070057 public static DeviceId deviceId(URI uri) {
58 return new DeviceId(uri);
59 }
60
61 /**
62 * Creates a device id using the supplied URI string.
63 *
64 * @param string device URI string
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080065 * @return DeviceId
tomca20e0c2014-09-03 23:22:24 -070066 */
67 public static DeviceId deviceId(String string) {
Jordan Halterman0d89ea32017-06-13 10:42:36 -070068 checkArgument(string.length() <= DEVICE_ID_MAX_LENGTH,
69 "deviceId exceeds maximum length " + DEVICE_ID_MAX_LENGTH);
tom568581d2014-09-08 20:13:36 -070070 return deviceId(URI.create(string));
tom64b7aac2014-08-26 00:18:21 -070071 }
72
tom545708e2014-10-09 17:10:02 -070073 /**
74 * Returns the backing URI.
75 *
76 * @return backing URI
77 */
78 public URI uri() {
79 return uri;
80 }
81
82 @Override
83 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070084 return str.hashCode();
tom545708e2014-10-09 17:10:02 -070085 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92 if (obj instanceof DeviceId) {
93 final DeviceId that = (DeviceId) obj;
94 return this.getClass() == that.getClass() &&
95 Objects.equals(this.str, that.str);
96 }
97 return false;
98 }
99
100 @Override
101 public String toString() {
102 return str;
103 }
104
tom0eb04ca2014-08-25 14:34:51 -0700105}