2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.ecomp;
22 import fj.data.Either;
23 import java.util.LinkedList;
24 import java.util.List;
26 import javax.servlet.http.HttpServletRequest;
27 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
28 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
29 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
30 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
31 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
32 import org.onap.portalsdk.core.restful.domain.EcompRole;
33 import org.onap.portalsdk.core.restful.domain.EcompUser;
34 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
35 import org.openecomp.sdc.be.config.BeEcompErrorManager;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.ecomp.converters.EcompRoleConverter;
39 import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.be.user.Role;
42 import org.openecomp.sdc.be.user.UserBusinessLogic;
43 import org.openecomp.sdc.be.user.UserBusinessLogicExt;
44 import org.openecomp.sdc.common.api.Constants;
45 import org.openecomp.sdc.common.log.wrappers.Logger;
46 import org.openecomp.sdc.exception.ResponseFormat;
47 import org.springframework.context.ApplicationContext;
48 import org.springframework.web.context.ContextLoader;
50 public class EcompIntImpl implements IPortalRestAPIService {
52 public static final String FAILED_TO_GET_ROLES = "Failed to get Roles";
53 public static final String ERROR_FAILED_TO_GET_ROLES = "Error: Failed to get Roles";
54 private static final String FAILED_TO_CONVERT_ROLES = "Failed to convert Roles";
55 private static final String GET_USER_ROLES = "GetUserRoles";
56 private static final String PUSH_USER_ROLE = "PushUserRole";
57 private static final String FAILED_TO_FETCH_ROLES = "Failed to fetch roles";
58 private static final String FAILED_TO_CONVERT_USER2 = "Failed to convert User {}";
59 private static final String GET_USERS = "GetUsers";
60 private static final String FAILED_TO_GET_USERS = "Failed to get Users";
61 private static final String GET_USER = "GetUser";
62 private static final String FAILED_TO_GET_USER = "Failed to get User";
63 private static final String FAILED_TO_UPDATE_USER_CREDENTIALS = "Failed to updateUserCredentials";
64 private static final String FAILED_TO_EDIT_USER = "Failed to edit user";
65 private static final String EDIT_USER = "EditUser";
66 private static final String RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID = "Received null for argument loginId";
67 private static final String FAILED_TO_CREATE_USER = "Failed to create user {}";
68 private static final String JH0003 = "jh0003";
69 private static final String PUSH_USER = "PushUser";
70 private static final String RECEIVED_NULL_FOR_ARGUMENT_USER = "Received null for argument user";
71 private static final Logger log = Logger.getLogger(EcompIntImpl.class.getName());
73 public EcompIntImpl() {
74 log.debug("EcompIntImpl Class Instantiated");
78 public void pushUser(EcompUser user) throws PortalAPIException {
79 log.debug("Start handle request of ECOMP pushUser");
82 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, ErrorSeverity.INFO);
83 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
84 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
86 UserBusinessLogic userBusinessLogic = getUserBusinessLogic(PUSH_USER);
87 final String modifierAttId = JH0003;
88 log.debug("modifier id is {}", modifierAttId);
89 User convertedAsdcUser = EcompUserConverter.convertEcompUserToUser(user);
90 userBusinessLogic.createUser(modifierAttId, convertedAsdcUser);
91 log.debug("User created {}", user);
92 } catch (ComponentException ce) {
93 if (ActionStatus.USER_ALREADY_EXIST.equals(ce.getActionStatus())) {
94 log.debug("User already exist {}", user);
96 log.debug(FAILED_TO_CREATE_USER, user);
97 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, ErrorSeverity.ERROR);
98 throw new PortalAPIException(FAILED_TO_CREATE_USER + ce.getActionStatus());
100 } catch (Exception e) {
101 log.debug(FAILED_TO_CREATE_USER, user, e);
102 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, ErrorSeverity.ERROR);
103 throw new PortalAPIException(FAILED_TO_CREATE_USER, e);
111 * loginId - equals to userId
115 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
116 log.debug("Start handle request of ECOMP editUser");
119 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
120 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, ErrorSeverity.INFO);
121 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
122 } else if (loginId == null) {
123 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
124 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, ErrorSeverity.INFO);
125 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
128 if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
129 log.debug("loginId and user loginId not equal");
130 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, "loginId and user loginId not equal", ErrorSeverity.INFO);
131 throw new PortalAPIException("loginId not equals to the user loginId field");
132 } else if (user.getLoginId() == null) {
133 user.setLoginId(loginId);
136 User asdcUser = EcompUserConverter.convertEcompUserToUser(user);
137 UserBusinessLogic userBusinessLogic = getUserBusinessLogic(EDIT_USER);
138 Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser);
139 if (updateUserCredentialsResponse.isRight()) {
140 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
141 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, ErrorSeverity.ERROR);
142 throw new PortalAPIException(FAILED_TO_EDIT_USER + updateUserCredentialsResponse.right().value());
144 } catch (Exception e) {
145 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
146 throw new PortalAPIException(FAILED_TO_EDIT_USER, e);
151 public EcompUser getUser(String loginId) throws PortalAPIException {
152 log.debug("Start handle request of ECOMP getUser");
154 if (loginId == null) {
155 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
156 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, ErrorSeverity.INFO);
157 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
159 UserBusinessLogic userBusinessLogic = getUserBusinessLogic(GET_USER);
160 User user = userBusinessLogic.getUser(loginId, false);
161 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
162 if (ecompUser.isLeft() && ecompUser.left().value() != null) {
163 return ecompUser.left().value();
165 log.debug(FAILED_TO_GET_USER);
166 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, FAILED_TO_GET_USER, ErrorSeverity.INFO);
167 throw new PortalAPIException(ecompUser.right().value());
169 } catch (ComponentException ce) {
170 log.debug(FAILED_TO_GET_USER);
171 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, FAILED_TO_GET_USER, ErrorSeverity.INFO);
172 throw new PortalAPIException(FAILED_TO_GET_USER + ce.getActionStatus());
173 } catch (Exception e) {
174 log.debug(FAILED_TO_GET_USER);
175 throw new PortalAPIException(FAILED_TO_GET_USER, e);
180 public List<EcompUser> getUsers() throws PortalAPIException {
181 log.debug("Start handle request of ECOMP getUsers");
183 UserBusinessLogic userBusinessLogic = getUserBusinessLogic(GET_USERS);
184 List<User> users = userBusinessLogic.getUsersList(JH0003, null, null);
185 List<EcompUser> ecompUserList = new LinkedList<>();
186 for (User user : users) {
187 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
188 if (ecompUser.isRight() || ecompUser.left().value() == null) {
189 log.debug(FAILED_TO_CONVERT_USER2, user);
190 BeEcompErrorManager.getInstance()
191 .logInvalidInputError(GET_USERS, "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
194 ecompUserList.add(ecompUser.left().value());
196 return ecompUserList;
197 } catch (Exception e) {
198 log.debug(FAILED_TO_GET_USERS);
199 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, FAILED_TO_GET_USERS, ErrorSeverity.INFO);
200 throw new PortalAPIException(FAILED_TO_GET_USERS, e);
205 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
206 log.debug("Start handle request of ECOMP getAvailableRoles");
208 List<EcompRole> ecompRolesList = new LinkedList<>();
209 for (Role role : Role.values()) {
210 EcompRole ecompRole = new EcompRole();
211 ecompRole.setId((long) role.ordinal());
212 ecompRole.setName(role.name());
213 ecompRolesList.add(ecompRole);
215 if (ecompRolesList.isEmpty()) {
216 throw new PortalAPIException();
218 return ecompRolesList;
219 } catch (Exception e) {
220 log.debug(FAILED_TO_FETCH_ROLES);
221 BeEcompErrorManager.getInstance().logInvalidInputError("GetAvailableRoles", FAILED_TO_FETCH_ROLES, ErrorSeverity.INFO);
222 throw new PortalAPIException("Roles fetching failed", e);
227 * The user role updated through this method only.
230 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
231 log.debug("Start handle request of ECOMP pushUserRole");
232 final String modifierAttId = JH0003;
233 log.debug("modifier id is {}", modifierAttId);
237 throw new PortalAPIException("Error: Received null for roles");
238 } else if (roles.iterator().hasNext()) {
239 EcompRole ecompRole = roles.iterator().next();
240 updatedRole = EcompRoleConverter.convertEcompRoleToRole(ecompRole);
241 log.debug("pushing role: {} to user: {}", updatedRole, loginId);
244 UserBusinessLogic userBusinessLogic = getUserBusinessLogic(PUSH_USER_ROLE);
245 userBusinessLogic.updateUserRole(modifierAttId, loginId, updatedRole);
246 } catch (Exception e) {
247 log.debug("Error: Failed to update role");
248 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, "Failed to update role", ErrorSeverity.INFO);
249 throw new PortalAPIException("Failed to update role" + e);
252 log.debug("Error: No roles in List");
253 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, FAILED_TO_FETCH_ROLES, ErrorSeverity.INFO);
255 //in these cases we want to deactivate the user
257 UserBusinessLogicExt userBusinessLogicExt = getUserBusinessLogicExt(PUSH_USER_ROLE);
258 userBusinessLogicExt.deActivateUser(modifierAttId, loginId);
259 } catch (Exception e) {
260 log.debug("Error: Failed to deactivate user {}", loginId);
261 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, "Failed to deactivate user", ErrorSeverity.INFO);
262 throw new PortalAPIException("Error: Failed to deactivate user" + e);
268 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
270 log.debug("Start handle request of ECOMP getUserRoles");
271 UserBusinessLogic userBusinessLogic = getUserBusinessLogic(GET_USER_ROLES);
272 User user = userBusinessLogic.getUser(loginId, false);
273 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
274 if (ecompUser.isRight()) {
275 log.debug("Error: Failed to convert Roles");
276 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_CONVERT_ROLES, ErrorSeverity.ERROR);
277 throw new PortalAPIException(ecompUser.right().value());
278 } else if (ecompUser.left().value() == null) {
279 log.debug("Error: Failed to convert Roles");
280 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_CONVERT_ROLES, ErrorSeverity.ERROR);
281 throw new PortalAPIException();
283 return new LinkedList<>(ecompUser.left().value().getRoles());
284 } catch (ComponentException ce) {
285 log.debug(ERROR_FAILED_TO_GET_ROLES);
286 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_GET_ROLES, ErrorSeverity.INFO);
287 throw new PortalAPIException(FAILED_TO_GET_ROLES + ce.getActionStatus());
288 } catch (Exception e) {
289 log.debug(ERROR_FAILED_TO_GET_ROLES);
290 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_GET_ROLES, ErrorSeverity.INFO);
291 throw new PortalAPIException(FAILED_TO_GET_ROLES, e);
296 public boolean isAppAuthenticated(HttpServletRequest request, Map<String, String> appCredentials) throws PortalAPIException {
297 final String portal_key = PortalApiProperties.getProperty("portal_pass");
298 final String portal_user = PortalApiProperties.getProperty("portal_user");
299 final String username = request.getHeader("username");
300 final String password = request.getHeader("password");
301 if (username != null && password != null) {
303 if (username.equals(CipherUtil.decryptPKC(portal_user)) && password.equals(CipherUtil.decryptPKC(portal_key))) {
304 log.debug("User authenticated - Username: {}", username);
307 } catch (CipherUtilException e) {
308 log.debug("User authentication failed - Decryption failed", e);
312 log.debug("User authentication failed");
316 private UserBusinessLogic getUserBusinessLogic(String context) throws PortalAPIException {
317 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
320 log.debug("Failed to get CurrentWebApplicationContext. Can't get UserBusinessLogic");
322 // Context used to aid in debugging
323 BeEcompErrorManager.getInstance().logInternalUnexpectedError(context,
324 "Failed to get CurrentWebApplicationContext. Can't get UserBusinessLogic", ErrorSeverity.ERROR);
325 throw new PortalAPIException("CurrentWebApplicationContext is null. Failed to get Bean userBusinessLogic");
328 return (UserBusinessLogic) ctx.getBean("userBusinessLogic");
331 private UserBusinessLogicExt getUserBusinessLogicExt(String context) throws PortalAPIException {
332 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
335 log.debug("Failed to get CurrentWebApplicationContext. Can't get UserBusinessLogicExt");
337 // Context used to aid in debugging
338 BeEcompErrorManager.getInstance().logInternalUnexpectedError(context,
339 "Failed to get CurrentWebApplicationContext. Can't get UserBusinessLogicExt", ErrorSeverity.ERROR);
340 throw new PortalAPIException("CurrentWebApplicationContext is null. Failed to get Bean userBusinessLogicExt");
343 return (UserBusinessLogicExt) ctx.getBean("userBusinessLogicExt");
347 * Gets and returns the userId for the logged-in user based on the request. If any error occurs, the method should throw PortalApiException with
348 * an appropriate message. The FW library will catch the exception and send an appropriate response to Portal. However, the app can always choose
349 * to have a custom implementation of this method. For Open-source implementation, for example, the app will have a totally different
350 * implementation for this method.
352 * @param request The HttpServletRequest
353 * @return true if the request contains appropriate credentials, else false.
354 * @throws PortalAPIException If an unexpected error occurs while processing the request.
357 public String getUserId(HttpServletRequest request) throws PortalAPIException {
358 return request.getHeader(Constants.USER_ID_HEADER);
362 public Map<String, String> getCredentials() throws PortalAPIException {