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