Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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.provider; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 17 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 18 | import java.util.Objects; |
| 19 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 22 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 23 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | * External identity of a {@link org.onosproject.net.provider.Provider} family. |
tom | db6c3f8 | 2014-10-01 21:30:17 -0700 | [diff] [blame] | 25 | * It also carriers two designations of external characteristics, the URI |
| 26 | * scheme and primary/ancillary indicator. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 27 | * <p> |
tom | db6c3f8 | 2014-10-01 21:30:17 -0700 | [diff] [blame] | 28 | * The device URI scheme is used to determine applicability of a provider to |
| 29 | * operations on a specific device. The ancillary indicator serves to designate |
| 30 | * a provider as a primary or ancillary. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 31 | * </p> |
| 32 | * <p> |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | * A {@link org.onosproject.net.provider.ProviderRegistry} uses this designation |
tom | db6c3f8 | 2014-10-01 21:30:17 -0700 | [diff] [blame] | 34 | * to permit only one primary provider per device URI scheme. Multiple |
| 35 | * ancillary providers can register with the same device URI scheme however. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 36 | * </p> |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 37 | */ |
| 38 | public class ProviderId { |
| 39 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Represents no provider ID. |
| 42 | */ |
Yuta HIGUCHI | 6a52923 | 2014-10-29 11:57:34 -0700 | [diff] [blame] | 43 | public static final ProviderId NONE = new ProviderId("none", "none"); |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 44 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 45 | private final String scheme; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 46 | private final String id; |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 47 | private final boolean ancillary; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 48 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 49 | // For serialization |
| 50 | private ProviderId() { |
| 51 | scheme = null; |
| 52 | id = null; |
| 53 | ancillary = false; |
| 54 | } |
| 55 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 56 | /** |
tom | db6c3f8 | 2014-10-01 21:30:17 -0700 | [diff] [blame] | 57 | * Creates a new primary provider identifier from the specified string. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 58 | * The providers are expected to follow the reverse DNS convention, e.g. |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | * {@code org.onosproject.provider.of.device} |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 60 | * |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 61 | * @param scheme device URI scheme to which this provider is bound, e.g. "of", "snmp" |
| 62 | * @param id string identifier |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 63 | */ |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 64 | public ProviderId(String scheme, String id) { |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 65 | this(scheme, id, false); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a new provider identifier from the specified string. |
| 70 | * The providers are expected to follow the reverse DNS convention, e.g. |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 71 | * {@code org.onosproject.provider.of.device} |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 72 | * |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 73 | * @param scheme device URI scheme to which this provider is bound, e.g. "of", "snmp" |
| 74 | * @param id string identifier |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 75 | * @param ancillary ancillary provider indicator |
| 76 | */ |
| 77 | public ProviderId(String scheme, String id, boolean ancillary) { |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 78 | this.scheme = checkNotNull(scheme, "Scheme cannot be null"); |
| 79 | this.id = checkNotNull(id, "ID cannot be null"); |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 80 | this.ancillary = ancillary; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 81 | } |
| 82 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Returns the device URI scheme to which this provider is bound. |
| 85 | * |
| 86 | * @return device URI scheme |
| 87 | */ |
| 88 | public String scheme() { |
| 89 | return scheme; |
| 90 | } |
| 91 | |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Returns the device URI scheme specific id portion. |
| 94 | * |
| 95 | * @return id |
| 96 | */ |
| 97 | public String id() { |
| 98 | return id; |
| 99 | } |
| 100 | |
tom | db6c3f8 | 2014-10-01 21:30:17 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Indicates whether this identifier designates an ancillary providers. |
| 103 | * |
| 104 | * @return true if the provider is ancillary; false if primary |
| 105 | */ |
| 106 | public boolean isAncillary() { |
| 107 | return ancillary; |
| 108 | } |
| 109 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 110 | @Override |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 111 | public int hashCode() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 112 | return Objects.hash(scheme, id); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 116 | public boolean equals(Object obj) { |
| 117 | if (this == obj) { |
| 118 | return true; |
| 119 | } |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 120 | if (obj instanceof ProviderId) { |
| 121 | final ProviderId other = (ProviderId) obj; |
| 122 | return Objects.equals(this.scheme, other.scheme) && |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 123 | Objects.equals(this.id, other.id) && |
| 124 | this.ancillary == other.ancillary; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 125 | } |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 126 | return false; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
tom | a536886 | 2014-10-01 12:35:48 -0700 | [diff] [blame] | 131 | return toStringHelper(this).add("scheme", scheme).add("id", id) |
| 132 | .add("ancillary", ancillary).toString(); |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 133 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 134 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 135 | } |