blob: 62207d94d01fe231d8ad36857bb2a6ec9ec3d060 [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.
Thomas Vachuska4b420772014-10-30 16:46:17 -070027 * <p>
tomdb6c3f82014-10-01 21:30:17 -070028 * 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 Vachuska4b420772014-10-30 16:46:17 -070031 * </p>
32 * <p>
tomdb6c3f82014-10-01 21:30:17 -070033 * A {@link org.onlab.onos.net.provider.ProviderRegistry} uses this designation
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 Vachuska4b420772014-10-30 16:46:17 -070036 * </p>
tom0eb04ca2014-08-25 14:34:51 -070037 */
38public class ProviderId {
39
toma1d16b62014-10-02 23:45:11 -070040 /**
41 * Represents no provider ID.
42 */
Yuta HIGUCHI6a529232014-10-29 11:57:34 -070043 public static final ProviderId NONE = new ProviderId("none", "none");
toma1d16b62014-10-02 23:45:11 -070044
tom7e02cda2014-09-18 12:05:46 -070045 private final String scheme;
tom0eb04ca2014-08-25 14:34:51 -070046 private final String id;
toma5368862014-10-01 12:35:48 -070047 private final boolean ancillary;
tom0eb04ca2014-08-25 14:34:51 -070048
toma1d16b62014-10-02 23:45:11 -070049 // For serialization
50 private ProviderId() {
51 scheme = null;
52 id = null;
53 ancillary = false;
54 }
55
tom64b7aac2014-08-26 00:18:21 -070056 /**
tomdb6c3f82014-10-01 21:30:17 -070057 * Creates a new primary provider identifier from the specified string.
tom64b7aac2014-08-26 00:18:21 -070058 * The providers are expected to follow the reverse DNS convention, e.g.
59 * {@code org.onlab.onos.provider.of.device}
60 *
Thomas Vachuska4b420772014-10-30 16:46:17 -070061 * @param scheme device URI scheme to which this provider is bound, e.g. "of", "snmp"
62 * @param id string identifier
tom64b7aac2014-08-26 00:18:21 -070063 */
tom7e02cda2014-09-18 12:05:46 -070064 public ProviderId(String scheme, String id) {
toma5368862014-10-01 12:35:48 -070065 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.
71 * {@code org.onlab.onos.provider.of.device}
72 *
Thomas Vachuska4b420772014-10-30 16:46:17 -070073 * @param scheme device URI scheme to which this provider is bound, e.g. "of", "snmp"
74 * @param id string identifier
toma5368862014-10-01 12:35:48 -070075 * @param ancillary ancillary provider indicator
76 */
77 public ProviderId(String scheme, String id, boolean ancillary) {
toma1d16b62014-10-02 23:45:11 -070078 this.scheme = checkNotNull(scheme, "Scheme cannot be null");
79 this.id = checkNotNull(id, "ID cannot be null");
toma5368862014-10-01 12:35:48 -070080 this.ancillary = ancillary;
tom0eb04ca2014-08-25 14:34:51 -070081 }
82
tom7e02cda2014-09-18 12:05:46 -070083 /**
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 HIGUCHIc7052012014-09-22 19:11:00 -070092 /**
93 * Returns the device URI scheme specific id portion.
94 *
95 * @return id
96 */
97 public String id() {
98 return id;
99 }
100
tomdb6c3f82014-10-01 21:30:17 -0700101 /**
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
tom0eb04ca2014-08-25 14:34:51 -0700110 @Override
tom64b7aac2014-08-26 00:18:21 -0700111 public int hashCode() {
tom7e02cda2014-09-18 12:05:46 -0700112 return Objects.hash(scheme, id);
tom0eb04ca2014-08-25 14:34:51 -0700113 }
114
115 @Override
tom64b7aac2014-08-26 00:18:21 -0700116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
tomfc9a4ff2014-09-22 18:22:47 -0700120 if (obj instanceof ProviderId) {
121 final ProviderId other = (ProviderId) obj;
122 return Objects.equals(this.scheme, other.scheme) &&
toma5368862014-10-01 12:35:48 -0700123 Objects.equals(this.id, other.id) &&
124 this.ancillary == other.ancillary;
tom64b7aac2014-08-26 00:18:21 -0700125 }
tomfc9a4ff2014-09-22 18:22:47 -0700126 return false;
tom0eb04ca2014-08-25 14:34:51 -0700127 }
128
129 @Override
130 public String toString() {
toma5368862014-10-01 12:35:48 -0700131 return toStringHelper(this).add("scheme", scheme).add("id", id)
132 .add("ancillary", ancillary).toString();
tom0eb04ca2014-08-25 14:34:51 -0700133 }
tom64b7aac2014-08-26 00:18:21 -0700134
tom0eb04ca2014-08-25 14:34:51 -0700135}