fed52a0de081229bd67e9d13824d790ec2b3121a
[sdnc/apps.git] /
1 /*\r
2  * ============LICENSE_START===================================================\r
3  * Copyright (c) 2018 Amdocs\r
4  * ============================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *        http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ============LICENSE_END=====================================================\r
17  */\r
18 package org.onap.sdnc.apps.pomba.networkdiscovery.service;\r
19 \r
20 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.UNAUTHORIZED;\r
21 \r
22 import java.io.File;\r
23 import java.io.FileInputStream;\r
24 import java.io.IOException;\r
25 \r
26 import javax.ws.rs.core.MediaType;\r
27 import javax.ws.rs.core.MultivaluedHashMap;\r
28 import javax.ws.rs.core.MultivaluedMap;\r
29 import javax.ws.rs.core.Response.Status;\r
30 \r
31 import org.apache.commons.io.IOUtils;\r
32 import org.onap.aai.restclient.client.OperationResult;\r
33 import org.onap.aai.restclient.client.RestClient;\r
34 import org.onap.logging.ref.slf4j.ONAPLogAdapter;\r
35 import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;\r
36 \r
37 public class OSAuthentication {\r
38 \r
39     private static final String CONFIG_AUTH_DIR = "config/auth";\r
40     private static final String X_SUBJECT_TOKEN = "X-Subject-Token";\r
41 \r
42     private static final String USER_PATTERN = "%USER%";    \r
43     private static final String PASSWORD_PATTERN = "%PASSWORD%";\r
44 \r
45     private OSAuthentication() {\r
46         throw new IllegalStateException("Utility class");\r
47     }\r
48 \r
49         public static String getToken(String openstackIdentityUrl, String userId, String password, RestClient openstackClient, ONAPLogAdapter adapter)\r
50                         throws IOException, ApplicationException {\r
51 \r
52                 MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();\r
53 \r
54                 String mappingConfigPath = CONFIG_AUTH_DIR + File.separator + "osauth.json";\r
55 \r
56                 File file = new File(mappingConfigPath);\r
57                 String payload = IOUtils.toString(new FileInputStream(file), "UTF-8");\r
58         payload = payload.replaceAll(USER_PATTERN, userId);\r
59                 payload = payload.replaceAll(PASSWORD_PATTERN, password);\r
60 \r
61                 OperationResult result = openstackClient.post(openstackIdentityUrl, payload, headers,\r
62                                 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);\r
63 \r
64                 adapter.unwrap().info("request at url = {} resulted in http response: {}", openstackIdentityUrl,\r
65                                 result.getResult());\r
66 \r
67                 String token = result.getHeaders().getFirst(X_SUBJECT_TOKEN);\r
68                 \r
69                 if (token == null) {\r
70                     throw new ApplicationException(UNAUTHORIZED, Status.UNAUTHORIZED);\r
71                 }\r
72 \r
73                 adapter.unwrap().debug("Got token: {}", token);\r
74 \r
75                 return token;\r
76         }\r
77 \r
78 }\r