blob: e35d044a4ea229a09969e8f26ff65ee56eca15d5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
tomc1a38d32014-08-25 23:01:32 -070016package org.onlab.onos.net.provider;
tom0eb04ca2014-08-25 14:34:51 -070017
tom64b7aac2014-08-26 00:18:21 -070018import java.util.Objects;
19
tomeadbb462014-09-07 16:10:19 -070020import static com.google.common.base.MoreObjects.toStringHelper;
toma1d16b62014-10-02 23:45:11 -070021import static com.google.common.base.Preconditions.checkNotNull;
tom64b7aac2014-08-26 00:18:21 -070022
tom0eb04ca2014-08-25 14:34:51 -070023/**
tomdb6c3f82014-10-01 21:30:17 -070024 * 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.
tom0eb04ca2014-08-25 14:34:51 -070035 */
36public class ProviderId {
37
toma1d16b62014-10-02 23:45:11 -070038 /**
39 * Represents no provider ID.
40 */
Yuta HIGUCHI6a529232014-10-29 11:57:34 -070041 public static final ProviderId NONE = new ProviderId("none", "none");
toma1d16b62014-10-02 23:45:11 -070042
tom7e02cda2014-09-18 12:05:46 -070043 private final String scheme;
tom0eb04ca2014-08-25 14:34:51 -070044 private final String id;
toma5368862014-10-01 12:35:48 -070045 private final boolean ancillary;
tom0eb04ca2014-08-25 14:34:51 -070046
toma1d16b62014-10-02 23:45:11 -070047 // For serialization
48 private ProviderId() {
49 scheme = null;
50 id = null;
51 ancillary = false;
52 }
53
tom64b7aac2014-08-26 00:18:21 -070054 /**
tomdb6c3f82014-10-01 21:30:17 -070055 * Creates a new primary provider identifier from the specified string.
tom64b7aac2014-08-26 00:18:21 -070056 * The providers are expected to follow the reverse DNS convention, e.g.
57 * {@code org.onlab.onos.provider.of.device}
58 *
tomdb6c3f82014-10-01 21:30:17 -070059 * @param scheme device URI scheme to which this provider is bound, e.g. "of", "snmp"
60 * @param id string identifier
tom64b7aac2014-08-26 00:18:21 -070061 */
tom7e02cda2014-09-18 12:05:46 -070062 public ProviderId(String scheme, String id) {
toma5368862014-10-01 12:35:48 -070063 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) {
toma1d16b62014-10-02 23:45:11 -070076 this.scheme = checkNotNull(scheme, "Scheme cannot be null");
77 this.id = checkNotNull(id, "ID cannot be null");
toma5368862014-10-01 12:35:48 -070078 this.ancillary = ancillary;
tom0eb04ca2014-08-25 14:34:51 -070079 }
80
tom7e02cda2014-09-18 12:05:46 -070081 /**
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 HIGUCHIc7052012014-09-22 19:11:00 -070090 /**
91 * Returns the device URI scheme specific id portion.
92 *
93 * @return id
94 */
95 public String id() {
96 return id;
97 }
98
tomdb6c3f82014-10-01 21:30:17 -070099 /**
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
tom0eb04ca2014-08-25 14:34:51 -0700108 @Override
tom64b7aac2014-08-26 00:18:21 -0700109 public int hashCode() {
tom7e02cda2014-09-18 12:05:46 -0700110 return Objects.hash(scheme, id);
tom0eb04ca2014-08-25 14:34:51 -0700111 }
112
113 @Override
tom64b7aac2014-08-26 00:18:21 -0700114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
tomfc9a4ff2014-09-22 18:22:47 -0700118 if (obj instanceof ProviderId) {
119 final ProviderId other = (ProviderId) obj;
120 return Objects.equals(this.scheme, other.scheme) &&
toma5368862014-10-01 12:35:48 -0700121 Objects.equals(this.id, other.id) &&
122 this.ancillary == other.ancillary;
tom64b7aac2014-08-26 00:18:21 -0700123 }
tomfc9a4ff2014-09-22 18:22:47 -0700124 return false;
tom0eb04ca2014-08-25 14:34:51 -0700125 }
126
127 @Override
128 public String toString() {
toma5368862014-10-01 12:35:48 -0700129 return toStringHelper(this).add("scheme", scheme).add("id", id)
130 .add("ancillary", ancillary).toString();
tom0eb04ca2014-08-25 14:34:51 -0700131 }
tom64b7aac2014-08-26 00:18:21 -0700132
tom0eb04ca2014-08-25 14:34:51 -0700133}