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