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