Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 17 | |
| 18 | import java.net.URI; |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 19 | import java.util.Objects; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 20 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 23 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 24 | * Immutable representation of a device identity. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 25 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 26 | public final class DeviceId extends ElementId { |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 27 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Represents either no device, or an unspecified device. |
| 30 | */ |
| 31 | public static final DeviceId NONE = deviceId("none:none"); |
| 32 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 33 | private static final int DEVICE_ID_MAX_LENGTH = 1024; |
| 34 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 35 | private final URI uri; |
| 36 | private final String str; |
| 37 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 38 | // Public construction is prohibited |
| 39 | private DeviceId(URI uri) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 40 | this.uri = uri; |
Thomas Vachuska | 2292567 | 2014-11-11 17:57:53 -0800 | [diff] [blame] | 41 | this.str = uri.toString().toLowerCase(); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
| 45 | // Default constructor for serialization |
| 46 | protected DeviceId() { |
| 47 | this.uri = null; |
| 48 | this.str = null; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 49 | } |
| 50 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 51 | /** |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 52 | * Creates a device id using the supplied URI. |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 53 | * |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 54 | * @param uri device URI |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 55 | * @return DeviceId |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 56 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 57 | 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 HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 65 | * @return DeviceId |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 66 | */ |
| 67 | public static DeviceId deviceId(String string) { |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 68 | checkArgument(string.length() <= DEVICE_ID_MAX_LENGTH, |
| 69 | "deviceId exceeds maximum length " + DEVICE_ID_MAX_LENGTH); |
tom | 568581d | 2014-09-08 20:13:36 -0700 | [diff] [blame] | 70 | return deviceId(URI.create(string)); |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 71 | } |
| 72 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 73 | /** |
| 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 Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 84 | return str.hashCode(); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 85 | } |
| 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 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 105 | } |