blob: 5a3b30dac76b0111618b1cb6e447942d522300be [file] [log] [blame]
Jian Li27841662018-04-14 01:59:47 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacknode.impl;
17
18import com.google.common.base.MoreObjects;
19import org.onosproject.openstacknode.api.OpenstackAuth;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkArgument;
24
Jian Li789fadb2018-07-10 13:59:47 +090025/**
26 * Implementation class of openstack authentication.
27 */
Jian Li27841662018-04-14 01:59:47 +090028public class DefaultOpenstackAuth implements OpenstackAuth {
29
30 private final String version;
31 private final Integer port;
32 private final Protocol protocol;
33 private final String username;
34 private final String password;
35 private final String project;
36 private final Perspective perspective;
37
38 private static final String NOT_NULL_MSG = "% cannot be null";
39
40 /**
41 * A default constructor of keystone authentication instance.
42 *
43 * @param version version number
44 * @param port endpoint port number
45 * @param protocol endpoint protocol type
46 * @param username keystone username
47 * @param password keystone password
48 * @param project project name
49 * @param perspective user perspective
50 */
51 protected DefaultOpenstackAuth(String version, Integer port, Protocol protocol,
52 String username, String password, String project,
53 Perspective perspective) {
54
55 this.version = version;
56 this.port = port;
57 this.protocol = protocol;
58 this.username = username;
59 this.password = password;
60 this.project = project;
61 this.perspective = perspective;
62 }
63
64 @Override
65 public String version() {
66 return version;
67 }
68
69 @Override
70 public Integer port() {
71 return port;
72 }
73
74 @Override
75 public Protocol protocol() {
76 return protocol;
77 }
78
79 @Override
80 public String username() {
81 return username;
82 }
83
84 @Override
85 public String password() {
86 return password;
87 }
88
89 @Override
90 public String project() {
91 return project;
92 }
93
94 @Override
95 public Perspective perspective() {
96 return perspective;
97 }
98
99 @Override
100 public boolean equals(Object obj) {
101 if (this == obj) {
102 return true;
103 }
104
105 if (obj instanceof DefaultOpenstackAuth) {
106 DefaultOpenstackAuth that = (DefaultOpenstackAuth) obj;
107 return Objects.equals(version, that.version) &&
108 Objects.equals(port, that.port) &&
109 Objects.equals(protocol, that.protocol) &&
110 Objects.equals(username, that.username) &&
111 Objects.equals(password, that.password) &&
112 Objects.equals(project, that.project) &&
113 Objects.equals(perspective, that.perspective);
114 }
115 return false;
116 }
117
118 @Override
119 public int hashCode() {
120 return Objects.hash(version,
121 port,
122 protocol,
123 username,
124 password,
125 project,
126 perspective);
127 }
128
129 @Override
130 public String toString() {
131 return MoreObjects.toStringHelper(getClass())
132 .add("version", version)
133 .add("port", port)
134 .add("protocol", protocol)
135 .add("username", username)
136 .add("password", password)
137 .add("project", project)
138 .add("perspective", perspective)
139 .toString();
140 }
141
142 /**
143 * Returns new builder instance.
144 *
145 * @return keystone authentication instance builder
146 */
147 public static Builder builder() {
148 return new Builder();
149 }
150
151 /**
152 * A builder class for openstack authentication.
153 */
154 public static final class Builder implements OpenstackAuth.Builder {
155
156 private String version;
157 private Integer port;
158 private Protocol protocol;
159 private String username;
160 private String password;
161 private String project;
162 private Perspective perspective;
163
164 // private constructor not intended to use from external
165 private Builder() {
166 }
167
168 @Override
169 public OpenstackAuth build() {
170 checkArgument(version != null, NOT_NULL_MSG, "version");
171 checkArgument(port != null, NOT_NULL_MSG, "port");
172 checkArgument(protocol != null, NOT_NULL_MSG, "protocol");
173 checkArgument(username != null, NOT_NULL_MSG, "username");
174 checkArgument(password != null, NOT_NULL_MSG, "password");
175 checkArgument(project != null, NOT_NULL_MSG, "project");
176
177 return new DefaultOpenstackAuth(version,
178 port,
179 protocol,
180 username,
181 password,
182 project,
183 perspective);
184 }
185
186 @Override
187 public Builder version(String version) {
188 this.version = version;
189 return this;
190 }
191
192 @Override
193 public Builder port(Integer port) {
194 this.port = port;
195 return this;
196 }
197
198 @Override
199 public Builder protocol(Protocol protocol) {
200 this.protocol = protocol;
201 return this;
202 }
203
204 @Override
205 public Builder username(String username) {
206 this.username = username;
207 return this;
208 }
209
210 @Override
211 public Builder password(String password) {
212 this.password = password;
213 return this;
214 }
215
216 @Override
217 public Builder project(String project) {
218 this.project = project;
219 return this;
220 }
221
222 @Override
223 public Builder perspective(Perspective perspective) {
224 this.perspective = perspective;
225 return this;
226 }
227 }
228}