[MSO-8] Update the maven dependency
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / cloud / authentication / AuthenticationMethodFactory.java
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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============================================
18  *
19  * ECOMP and OpenECOMP are trademarks
20  * and service marks of AT&T Intellectual Property.
21  *
22  */
23
24 package org.openecomp.mso.cloud.authentication;
25
26 import java.io.IOException;
27 import java.net.URISyntaxException;
28 import java.util.Map;
29 import java.util.concurrent.ConcurrentHashMap;
30
31 import org.openecomp.mso.cloud.CloudIdentity;
32
33 import com.woorea.openstack.keystone.model.Authentication;
34
35 /**
36  * This factory manages all the wrappers associated to authentication types.
37  *
38  */
39 public final class AuthenticationMethodFactory {
40
41         private static Map<String, AuthenticationWrapper> authWrappers = new ConcurrentHashMap<>();
42         
43         /**
44          * 
45          */
46         private AuthenticationMethodFactory() {}
47         
48         /**
49          * Function to be called by classes implementing the abstract {@link AuthenticationWrapper#register(String, Class)}.
50          */
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 + ".");
54                 }
55                 if (wrapperClass == null) {
56                         throw new IllegalArgumentException("Wrapper Class to register for Authentication cannot be null");
57                 }
58
59                 if (!authWrappers.containsKey(authenticationType)) {
60                         authWrappers.put(authenticationType, wrapperClass.newInstance());
61                 }
62         }
63         
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");
67                 }
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() + ".");
70                 }
71                 String authenticationType = cloudIdentity.getIdentityAuthenticationType().toString();
72                 
73                 if (authWrappers.containsKey(authenticationType)) {
74                         return authWrappers.get(authenticationType).getAuthentication(cloudIdentity);
75                 } else {
76                         throw new IllegalArgumentException("Provided authentication type (" + authenticationType + ") is not implemented by any wrapper.");
77                 }
78         }
79 }