blob: ae0af8f28c7f39124fa61df1028b85a4b012b58f [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 */
Daniel Park2ff66b42018-08-01 11:52:45 +090016package org.onosproject.openstacknode.api;
Jian Li27841662018-04-14 01:59:47 +090017
18import com.google.common.base.MoreObjects;
Jian Li27841662018-04-14 01:59:47 +090019
20import java.util.Objects;
21
22import static com.google.common.base.Preconditions.checkArgument;
23
Jian Li789fadb2018-07-10 13:59:47 +090024/**
25 * Implementation class of openstack authentication.
26 */
Daniel Parkdeefa702018-07-17 17:55:51 +090027public final class DefaultOpenstackAuth implements OpenstackAuth {
Jian Li27841662018-04-14 01:59:47 +090028
29 private final String version;
Jian Li27841662018-04-14 01:59:47 +090030 private final Protocol protocol;
31 private final String username;
32 private final String password;
33 private final String project;
34 private final Perspective perspective;
35
36 private static final String NOT_NULL_MSG = "% cannot be null";
37
38 /**
39 * A default constructor of keystone authentication instance.
40 *
41 * @param version version number
Jian Li27841662018-04-14 01:59:47 +090042 * @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 */
Jian Li6d410362018-08-17 09:41:08 +090048 private DefaultOpenstackAuth(String version, Protocol protocol,
Jian Li27841662018-04-14 01:59:47 +090049 String username, String password, String project,
50 Perspective perspective) {
51
52 this.version = version;
Jian Li27841662018-04-14 01:59:47 +090053 this.protocol = protocol;
54 this.username = username;
55 this.password = password;
56 this.project = project;
57 this.perspective = perspective;
58 }
59
60 @Override
61 public String version() {
62 return version;
63 }
64
65 @Override
Jian Li27841662018-04-14 01:59:47 +090066 public Protocol protocol() {
67 return protocol;
68 }
69
70 @Override
71 public String username() {
72 return username;
73 }
74
75 @Override
76 public String password() {
77 return password;
78 }
79
80 @Override
81 public String project() {
82 return project;
83 }
84
85 @Override
86 public Perspective perspective() {
87 return perspective;
88 }
89
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj) {
93 return true;
94 }
95
96 if (obj instanceof DefaultOpenstackAuth) {
97 DefaultOpenstackAuth that = (DefaultOpenstackAuth) obj;
98 return Objects.equals(version, that.version) &&
Jian Li27841662018-04-14 01:59:47 +090099 Objects.equals(protocol, that.protocol) &&
100 Objects.equals(username, that.username) &&
101 Objects.equals(password, that.password) &&
102 Objects.equals(project, that.project) &&
103 Objects.equals(perspective, that.perspective);
104 }
105 return false;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash(version,
Jian Li27841662018-04-14 01:59:47 +0900111 protocol,
112 username,
113 password,
114 project,
115 perspective);
116 }
117
118 @Override
119 public String toString() {
120 return MoreObjects.toStringHelper(getClass())
121 .add("version", version)
Jian Li27841662018-04-14 01:59:47 +0900122 .add("protocol", protocol)
123 .add("username", username)
124 .add("password", password)
125 .add("project", project)
126 .add("perspective", perspective)
127 .toString();
128 }
129
130 /**
131 * Returns new builder instance.
132 *
133 * @return keystone authentication instance builder
134 */
135 public static Builder builder() {
136 return new Builder();
137 }
138
139 /**
140 * A builder class for openstack authentication.
141 */
142 public static final class Builder implements OpenstackAuth.Builder {
143
144 private String version;
Jian Li27841662018-04-14 01:59:47 +0900145 private Protocol protocol;
146 private String username;
147 private String password;
148 private String project;
149 private Perspective perspective;
150
151 // private constructor not intended to use from external
152 private Builder() {
153 }
154
155 @Override
156 public OpenstackAuth build() {
157 checkArgument(version != null, NOT_NULL_MSG, "version");
Jian Li27841662018-04-14 01:59:47 +0900158 checkArgument(protocol != null, NOT_NULL_MSG, "protocol");
159 checkArgument(username != null, NOT_NULL_MSG, "username");
160 checkArgument(password != null, NOT_NULL_MSG, "password");
161 checkArgument(project != null, NOT_NULL_MSG, "project");
162
163 return new DefaultOpenstackAuth(version,
Jian Li27841662018-04-14 01:59:47 +0900164 protocol,
165 username,
166 password,
167 project,
168 perspective);
169 }
170
171 @Override
172 public Builder version(String version) {
173 this.version = version;
174 return this;
175 }
176
177 @Override
Jian Li27841662018-04-14 01:59:47 +0900178 public Builder protocol(Protocol protocol) {
179 this.protocol = protocol;
180 return this;
181 }
182
183 @Override
184 public Builder username(String username) {
185 this.username = username;
186 return this;
187 }
188
189 @Override
190 public Builder password(String password) {
191 this.password = password;
192 return this;
193 }
194
195 @Override
196 public Builder project(String project) {
197 this.project = project;
198 return this;
199 }
200
201 @Override
202 public Builder perspective(Perspective perspective) {
203 this.perspective = perspective;
204 return this;
205 }
206 }
207}