2 * ============LICENSE_START==========================================
3 * ===================================================================
4 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 * ===================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END============================================
19 * ECOMP and OpenECOMP are trademarks
20 * and service marks of AT&T Intellectual Property.
24 package org.openecomp.mso.cloud.authentication;
26 import java.io.IOException;
27 import java.net.URISyntaxException;
29 import java.util.concurrent.ConcurrentHashMap;
31 import org.openecomp.mso.cloud.CloudIdentity;
33 import com.woorea.openstack.keystone.model.Authentication;
36 * This factory manages all the wrappers associated to authentication types.
39 public final class AuthenticationMethodFactory {
41 private static Map<String, AuthenticationWrapper> authWrappers = new ConcurrentHashMap<>();
46 private AuthenticationMethodFactory() {}
49 * Function to be called by classes implementing the abstract {@link AuthenticationWrapper#register(String, Class)}.
51 static final synchronized void register(String authenticationType, Class<? extends AuthenticationWrapper> wrapperClass) throws InstantiationException, IllegalAccessException {
52 if ((authenticationType == null) || ("".equals(authenticationType))) {
53 throw new IllegalArgumentException("Authentication Type to register cannot be null or an empty name string, provided value is " + authenticationType + ".");
55 if (wrapperClass == null) {
56 throw new IllegalArgumentException("Wrapper Class to register for Authentication cannot be null");
59 if (!authWrappers.containsKey(authenticationType)) {
60 authWrappers.put(authenticationType, wrapperClass.newInstance());
64 public static final synchronized Authentication getAuthenticationFor(CloudIdentity cloudIdentity) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, URISyntaxException {
65 if (cloudIdentity == null) {
66 throw new IllegalArgumentException("Cloud identity cannot be null");
68 if ((cloudIdentity.getIdentityAuthenticationType() == null) || ("".equals(cloudIdentity.getIdentityAuthenticationType().toString()))) {
69 throw new IllegalArgumentException("Cloud identity authentication type cannot be null or empty, provided value is " + cloudIdentity.getIdentityAuthenticationType() + ".");
71 String authenticationType = cloudIdentity.getIdentityAuthenticationType().toString();
73 if (authWrappers.containsKey(authenticationType)) {
74 return authWrappers.get(authenticationType).getAuthentication(cloudIdentity);
76 throw new IllegalArgumentException("Provided authentication type (" + authenticationType + ") is not implemented by any wrapper.");