aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/main/java/org/onosproject/net/provider/ProviderId.java
blob: 2c959afd6d7cdfbdbb0d8ab79773818d36093363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright 2014 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.net.provider;

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * External identity of a {@link org.onosproject.net.provider.Provider} family.
 * It also carriers two designations of external characteristics, the URI
 * scheme and primary/ancillary indicator.
 * <p>
 * The device URI scheme is used to determine applicability of a provider to
 * operations on a specific device. The ancillary indicator serves to designate
 * a provider as a primary or ancillary.
 * </p>
 * <p>
 * A {@link org.onosproject.net.provider.ProviderRegistry} uses this designation
 * to permit only one primary provider per device URI scheme. Multiple
 * ancillary providers can register with the same device URI scheme however.
 * </p>
 */
public class ProviderId {

    /**
     * Represents no provider ID.
     */
    public static final ProviderId NONE = new ProviderId("none", "none");

    private final String scheme;
    private final String id;
    private final boolean ancillary;

    // For serialization
    private ProviderId() {
        scheme = null;
        id = null;
        ancillary = false;
    }

    /**
     * Creates a new primary provider identifier from the specified string.
     * The providers are expected to follow the reverse DNS convention, e.g.
     * {@code org.onosproject.provider.of.device}
     *
     * @param scheme device URI scheme to which this provider is bound, e.g. "of", "snmp"
     * @param id     string identifier
     */
    public ProviderId(String scheme, String id) {
        this(scheme, id, false);
    }

    /**
     * Creates a new provider identifier from the specified string.
     * The providers are expected to follow the reverse DNS convention, e.g.
     * {@code org.onosproject.provider.of.device}
     *
     * @param scheme    device URI scheme to which this provider is bound, e.g. "of", "snmp"
     * @param id        string identifier
     * @param ancillary ancillary provider indicator
     */
    public ProviderId(String scheme, String id, boolean ancillary) {
        this.scheme = checkNotNull(scheme, "Scheme cannot be null");
        this.id = checkNotNull(id, "ID cannot be null");
        this.ancillary = ancillary;
    }

    /**
     * Returns the device URI scheme to which this provider is bound.
     *
     * @return device URI scheme
     */
    public String scheme() {
        return scheme;
    }

    /**
     * Returns the device URI scheme specific id portion.
     *
     * @return id
     */
    public String id() {
        return id;
    }

    /**
     * Indicates whether this identifier designates an ancillary providers.
     *
     * @return true if the provider is ancillary; false if primary
     */
    public boolean isAncillary() {
        return ancillary;
    }

    @Override
    public int hashCode() {
        return Objects.hash(scheme, id);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof ProviderId) {
            final ProviderId other = (ProviderId) obj;
            return Objects.equals(this.scheme, other.scheme) &&
                    Objects.equals(this.id, other.id) &&
                    this.ancillary == other.ancillary;
        }
        return false;
    }

    @Override
    public String toString() {
        return toStringHelper(this).add("scheme", scheme).add("id", id)
                .add("ancillary", ancillary).toString();
    }

}