ab9c608ac8970980ec4fbae964e545a5975010fa
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalsdk.core.onboarding.crossapi;
39
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.stream.Collectors;
47
48 import javax.servlet.ServletException;
49 import javax.servlet.http.HttpServletRequest;
50
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
53 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
54 import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient;
55 import org.onap.portalsdk.core.onboarding.util.AuthUtil;
56 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
57 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
58 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
59 import org.onap.portalsdk.core.restful.domain.EcompRole;
60 import org.onap.portalsdk.core.restful.domain.EcompUser;
61 import org.apache.commons.logging.Log;
62 import org.apache.commons.logging.LogFactory;
63
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import com.fasterxml.jackson.databind.type.TypeFactory;
66
67 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
68
69         private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class);
70         private String username;
71         private String password;
72         private String appName;
73         IPortalRestCentralService portalRestCentralService;
74         public static final String API_VERSION = "/v4";
75         private static String portalApiVersion = "/v3";
76         private static final String nameSpace = PortalApiProperties
77                         .getProperty(PortalApiConstants.AUTH_NAMESPACE);
78
79         public PortalRestAPICentralServiceImpl() throws ServletException {
80                 String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
81                 if (centralClassName == null)
82                         throw new ServletException(
83                                         "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
84                 try {
85                         Class<?> centralImplClass = Class.forName(centralClassName);
86                         portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance());
87                         username = portalRestCentralService.getAppCredentials().get("username");
88                         password = portalRestCentralService.getAppCredentials().get("password");
89                         appName = portalRestCentralService.getAppCredentials().get("appName");
90                 } catch (Exception e) {
91                         throw new ClassCastException("Failed to find or instantiate class ");
92                 }
93         }
94
95         private final ObjectMapper mapper = new ObjectMapper();
96
97         @Override
98         public void pushUser(EcompUser user) throws PortalAPIException {
99                 portalRestCentralService.pushUser(user);
100         }
101
102         @Override
103         public void editUser(String loginId, EcompUser user) throws PortalAPIException {
104                 portalRestCentralService.editUser(loginId, user);
105         }
106
107         @Override
108         public EcompUser getUser(String loginId) throws PortalAPIException {
109                 EcompUser user = new EcompUser();
110                 String responseString = null;
111                 try {
112                         responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
113                                         appName, null, username, password, true);
114                         logger.debug("responseString is: " + responseString);
115                         user = mapper.readValue(responseString, EcompUser.class);
116
117                 } catch (IOException e) {
118                         String response = "Failed to get user from portal";
119                         logger.error(response, e);
120                         throw new PortalAPIException(response, e);
121                 }
122                 return user;
123         }
124
125         @Override
126         public List<EcompUser> getUsers() throws PortalAPIException {
127                 List<EcompUser> usersList = new ArrayList<>();
128                 String responseString = null;
129                 try {
130                         responseString = RestWebServiceClient.getInstance().getPortalContent(portalApiVersion + "/users", null, appName, null,
131                                         username, password, true);
132                         logger.debug("responseString is: " + responseString);
133                         usersList = mapper.readValue(responseString,
134                                         TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
135
136                 } catch (IOException e) {
137                         String response = "Failed to get the users from portal";
138                         logger.error(response, e);
139                         throw new PortalAPIException(response, e);
140                 }
141                 return usersList;
142         }
143
144         @Override
145         public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
146                 List<EcompRole> rolesList = new ArrayList<>();
147                 String responseString = null;
148                 try {
149                         responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/roles", requestedLoginId,
150                                         appName, null, username, password, true);
151                         logger.debug("responseString is: " + responseString);
152                         rolesList = mapper.readValue(responseString,
153                                         TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
154
155                 } catch (IOException e) {
156                         String response = "Failed to get Roles from portal";
157                         logger.error(response, e);
158                         throw new PortalAPIException(response, e);
159                 }
160                 return rolesList;
161         }
162
163         @Override
164         public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
165                 throw new PortalAPIException("Please use Portal for Role Management");
166
167         }
168
169         @SuppressWarnings("unchecked")
170         @Override
171         public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
172                 List<EcompRole> userRoles = new ArrayList<>();
173                 EcompUser user = new EcompUser();
174                 String responseString = null;
175                 try {
176                         responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
177                                         appName, null, username, password, true);
178                         logger.debug("responseString is: " + responseString);
179                         user = mapper.readValue(responseString, EcompUser.class);
180                         Set roles = user.getRoles();
181                         userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
182
183                 } catch (IOException e) {
184                         String response = "Failed to get user roles from portal";
185                         logger.error(response, e);
186                         throw new PortalAPIException(response, e);
187                 }
188                 return userRoles;
189         }
190
191         @Override
192         public boolean isAppAuthenticated(HttpServletRequest request, Map<String,String> appCredentials) throws PortalAPIException {
193                 boolean accessAllowed = false;
194                 try {
195                         accessAllowed = AuthUtil.isAccessAllowed(request, nameSpace, appCredentials);
196                 } catch (Exception e) {
197                         logger.error(e);
198                 }
199                 return accessAllowed;
200         }
201
202         @Override
203         public String getUserId(HttpServletRequest request) throws PortalAPIException {
204                 return portalRestCentralService.getUserId(request);
205         }
206         
207         @Override
208         public Map<String, String> getCredentials() throws PortalAPIException{
209                 Map<String, String> credentialsMap = new HashMap<>();
210
211                 credentialsMap.put("username", username);
212                 credentialsMap.put("password", password);
213                 credentialsMap.put("appName", appName);
214                 return credentialsMap;
215         }
216
217 }