blob: c2a3133d13619432c4f1a0291cce084acc9e3510 [file] [log] [blame]
tomc1a38d32014-08-25 23:01:32 -07001package org.onlab.onos.net.provider;
tom0eb04ca2014-08-25 14:34:51 -07002
tom64b7aac2014-08-26 00:18:21 -07003import java.util.Objects;
4
tomeadbb462014-09-07 16:10:19 -07005import static com.google.common.base.MoreObjects.toStringHelper;
toma1d16b62014-10-02 23:45:11 -07006import static com.google.common.base.Preconditions.checkNotNull;
tom64b7aac2014-08-26 00:18:21 -07007
tom0eb04ca2014-08-25 14:34:51 -07008/**
tomdb6c3f82014-10-01 21:30:17 -07009 * 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.
tom0eb04ca2014-08-25 14:34:51 -070020 */
21public class ProviderId {
22
toma1d16b62014-10-02 23:45:11 -070023 /**
24 * Represents no provider ID.
25 */
26 public static final ProviderId NONE = new ProviderId();
27
tom7e02cda2014-09-18 12:05:46 -070028 private final String scheme;
tom0eb04ca2014-08-25 14:34:51 -070029 private final String id;
toma5368862014-10-01 12:35:48 -070030 private final boolean ancillary;
tom0eb04ca2014-08-25 14:34:51 -070031
toma1d16b62014-10-02 23:45:11 -070032 // For serialization
33 private ProviderId() {
34 scheme = null;
35 id = null;
36 ancillary = false;
37 }
38
tom64b7aac2014-08-26 00:18:21 -070039 /**
tomdb6c3f82014-10-01 21:30:17 -070040 * Creates a new primary provider identifier from the specified string.
tom64b7aac2014-08-26 00:18:21 -070041 * The providers are expected to follow the reverse DNS convention, e.g.
42 * {@code org.onlab.onos.provider.of.device}
43 *
tomdb6c3f82014-10-01 21:30:17 -070044 * @param scheme device URI scheme to which this provider is bound, e.g. "of", "snmp"
45 * @param id string identifier
tom64b7aac2014-08-26 00:18:21 -070046 */
tom7e02cda2014-09-18 12:05:46 -070047 public ProviderId(String scheme, String id) {
toma5368862014-10-01 12:35:48 -070048 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) {
toma1d16b62014-10-02 23:45:11 -070061 this.scheme = checkNotNull(scheme, "Scheme cannot be null");
62 this.id = checkNotNull(id, "ID cannot be null");
toma5368862014-10-01 12:35:48 -070063 this.ancillary = ancillary;
tom0eb04ca2014-08-25 14:34:51 -070064 }
65
tom7e02cda2014-09-18 12:05:46 -070066 /**
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 HIGUCHIc7052012014-09-22 19:11:00 -070075 /**
76 * Returns the device URI scheme specific id portion.
77 *
78 * @return id
79 */
80 public String id() {
81 return id;
82 }
83
tomdb6c3f82014-10-01 21:30:17 -070084 /**
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
tom0eb04ca2014-08-25 14:34:51 -070093 @Override
tom64b7aac2014-08-26 00:18:21 -070094 public int hashCode() {
tom7e02cda2014-09-18 12:05:46 -070095 return Objects.hash(scheme, id);
tom0eb04ca2014-08-25 14:34:51 -070096 }
97
98 @Override
tom64b7aac2014-08-26 00:18:21 -070099 public boolean equals(Object obj) {
100 if (this == obj) {
101 return true;
102 }
tomfc9a4ff2014-09-22 18:22:47 -0700103 if (obj instanceof ProviderId) {
104 final ProviderId other = (ProviderId) obj;
105 return Objects.equals(this.scheme, other.scheme) &&
toma5368862014-10-01 12:35:48 -0700106 Objects.equals(this.id, other.id) &&
107 this.ancillary == other.ancillary;
tom64b7aac2014-08-26 00:18:21 -0700108 }
tomfc9a4ff2014-09-22 18:22:47 -0700109 return false;
tom0eb04ca2014-08-25 14:34:51 -0700110 }
111
112 @Override
113 public String toString() {
toma5368862014-10-01 12:35:48 -0700114 return toStringHelper(this).add("scheme", scheme).add("id", id)
115 .add("ancillary", ancillary).toString();
tom0eb04ca2014-08-25 14:34:51 -0700116 }
tom64b7aac2014-08-26 00:18:21 -0700117
tom0eb04ca2014-08-25 14:34:51 -0700118}