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