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=========================================================
21 package org.openecomp.sdc.be.ecomp;
23 import fj.data.Either;
24 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
25 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
26 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
27 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.ecomp.converters.EcompRoleConverter;
32 import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.user.Role;
35 import org.openecomp.sdc.be.user.UserBusinessLogic;
36 import org.openecomp.sdc.common.api.Constants;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.openecomp.sdc.exception.ResponseFormat;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.web.context.ContextLoader;
42 import javax.servlet.http.HttpServletRequest;
43 import java.util.LinkedList;
44 import java.util.List;
47 public class EcompIntImpl implements IPortalRestAPIService {
48 private static final String FAILED_TO_CONVERT_ROLES = "Failed to convert Roles";
49 private static final String FAILED_TO_GET_ROLES = "Failed to get Roles";
50 private static final String GET_USER_ROLES = "GetUserRoles";
51 private static final String ERROR_FAILED_TO_GET_ROLES = "Error: Failed to get Roles";
52 private static final String PUSH_USER_ROLE = "PushUserRole";
53 private static final String FAILED_TO_FETCH_ROLES = "Failed to fetch roles";
54 private static final String FAILED_TO_CONVERT_USER2 = "Failed to convert User {}";
55 private static final String GET_USERS = "GetUsers";
56 private static final String FAILED_TO_GET_USERS = "Failed to get Users";
57 private static final String GET_USER = "GetUser";
58 private static final String FAILED_TO_GET_USER = "Failed to get User";
59 private static final String FAILED_TO_UPDATE_USER_CREDENTIALS = "Failed to updateUserCredentials";
60 private static final String FAILED_TO_EDIT_USER = "Failed to edit user";
61 private static final String EDIT_USER = "EditUser";
62 private static final String RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID = "Received null for argument loginId";
63 private static final String NULL_POINTER_RETURNED_FROM_USER_CONVERTER = "NULL pointer returned from user converter";
64 private static final String FAILED_TO_CREATE_USER = "Failed to create user {}";
65 private static final String FAILED_TO_CONVERT_USER = "Failed to convert user";
66 private static final String JH0003 = "jh0003";
67 private static final String PUSH_USER = "PushUser";
68 private static final String RECEIVED_NULL_FOR_ARGUMENT_USER = "Received null for argument user";
69 private static final Logger log = Logger.getLogger(EcompIntImpl.class.getName());
71 public EcompIntImpl() {
72 log.debug("EcompIntImpl Class Instantiated");
76 public void pushUser(EcompUser user) throws PortalAPIException {
77 log.debug("Start handle request of ECOMP pushUser");
80 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, ErrorSeverity.INFO);
81 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
82 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
85 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
87 final String modifierAttId = JH0003;
88 User modifier = new User();
89 modifier.setUserId(modifierAttId);
90 log.debug("modifier id is {}", modifierAttId);
92 Either<User, String> newASDCUser = EcompUserConverter.convertEcompUserToUser(user);
93 if (newASDCUser.isRight()) {
94 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CONVERT_USER, ErrorSeverity.INFO);
95 log.debug(FAILED_TO_CREATE_USER, user);
96 throw new PortalAPIException("Failed to create user " + newASDCUser.right().value());
97 } else if (newASDCUser.left().value() == null) {
98 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, NULL_POINTER_RETURNED_FROM_USER_CONVERTER, ErrorSeverity.INFO);
99 log.debug(FAILED_TO_CREATE_USER, user);
100 throw new PortalAPIException("Failed to create user " + newASDCUser.right().value());
103 User convertedAsdcUser = newASDCUser.left().value();
104 Either<User, ResponseFormat> createUserResponse = userBusinessLogic.createUser(modifier, convertedAsdcUser);
106 // ALREADY EXIST ResponseFormat
107 final String ALREADY_EXISTS_RESPONSE_ID = "SVC4006";
109 if (createUserResponse.isRight()) {
110 if (!createUserResponse.right().value().getMessageId().equals(ALREADY_EXISTS_RESPONSE_ID)) {
111 log.debug(FAILED_TO_CREATE_USER, user);
112 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, ErrorSeverity.ERROR);
113 throw new PortalAPIException(FAILED_TO_CREATE_USER + createUserResponse.right());
115 log.debug("User already exist {}", user);
117 log.debug("User created {}", user);
118 } catch (Exception e) {
119 log.debug(FAILED_TO_CREATE_USER, user, e);
120 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, ErrorSeverity.ERROR);
121 throw new PortalAPIException(FAILED_TO_CREATE_USER, e);
129 * loginId - equals to userId
133 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
134 log.debug("Start handle request of ECOMP editUser");
138 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
139 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, ErrorSeverity.INFO);
140 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
141 } else if (loginId == null) {
142 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
143 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, ErrorSeverity.INFO);
144 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
147 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
149 if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
150 log.debug("loginId and user loginId not equal");
151 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, "loginId and user loginId not equal", ErrorSeverity.INFO);
152 throw new PortalAPIException("loginId not equals to the user loginId field");
153 } else if (user.getLoginId() == null) {
154 user.setLoginId(loginId);
157 Either<User, String> asdcUser = EcompUserConverter.convertEcompUserToUser(user);
158 if (asdcUser.isRight()) {
159 log.debug(FAILED_TO_CONVERT_USER);
160 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, FAILED_TO_CONVERT_USER, ErrorSeverity.INFO);
161 throw new PortalAPIException(asdcUser.right().value());
162 } else if (asdcUser.left().value() == null) {
163 log.debug(NULL_POINTER_RETURNED_FROM_USER_CONVERTER);
164 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, NULL_POINTER_RETURNED_FROM_USER_CONVERTER, ErrorSeverity.INFO);
165 throw new PortalAPIException(FAILED_TO_EDIT_USER);
168 Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser.left().value());
170 if (updateUserCredentialsResponse.isRight()) {
171 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
172 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, ErrorSeverity.ERROR);
173 throw new PortalAPIException(FAILED_TO_EDIT_USER + updateUserCredentialsResponse.right().value());
175 } catch (Exception e) {
176 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
177 throw new PortalAPIException(FAILED_TO_EDIT_USER, e);
183 public EcompUser getUser(String loginId) throws PortalAPIException {
184 log.debug("Start handle request of ECOMP getUser");
188 if (loginId == null) {
189 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
190 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, ErrorSeverity.INFO);
191 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
194 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
196 Either<User, ActionStatus> getUserResponse = userBusinessLogic.getUser(loginId, false);
198 if (getUserResponse.isRight()) {
199 log.debug(FAILED_TO_GET_USER);
200 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, FAILED_TO_GET_USER, ErrorSeverity.INFO);
201 throw new PortalAPIException(FAILED_TO_GET_USER + getUserResponse.right());
203 if (getUserResponse.left().value() != null) {
204 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(getUserResponse.left().value());
205 if (ecompUser.isLeft() && ecompUser.left().value() != null) {
206 return ecompUser.left().value();
208 log.debug(FAILED_TO_GET_USER);
209 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, FAILED_TO_GET_USER, ErrorSeverity.INFO);
210 throw new PortalAPIException(ecompUser.right().value());
213 log.debug(FAILED_TO_GET_USER);
214 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, FAILED_TO_GET_USER, ErrorSeverity.INFO);
215 throw new PortalAPIException(FAILED_TO_GET_USER + getUserResponse.right());
218 } catch (Exception e) {
219 log.debug(FAILED_TO_GET_USER);
220 throw new PortalAPIException(FAILED_TO_GET_USER, e);
225 public List<EcompUser> getUsers() throws PortalAPIException {
226 log.debug("Start handle request of ECOMP getUsers");
229 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
231 Either<List<User>, ResponseFormat> getUsersResponse = userBusinessLogic.getUsersList(JH0003, null, null);
233 if (getUsersResponse.isRight()) {
234 log.debug(FAILED_TO_GET_USERS);
235 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, FAILED_TO_GET_USERS, ErrorSeverity.INFO);
236 throw new PortalAPIException(FAILED_TO_GET_USERS + getUsersResponse.right());
238 if (getUsersResponse.left().value() != null) {
239 List<EcompUser> ecompUserList = new LinkedList<>();
240 for (User user : getUsersResponse.left().value()) {
241 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
242 if (ecompUser.isRight()) {
243 log.debug(FAILED_TO_CONVERT_USER2, user);
244 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
246 } else if (ecompUser.left().value() == null) {
247 log.debug(FAILED_TO_CONVERT_USER2, user);
248 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
251 ecompUserList.add(ecompUser.left().value());
253 return ecompUserList;
255 log.debug(FAILED_TO_GET_USERS);
256 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, FAILED_TO_GET_USERS, ErrorSeverity.INFO);
257 throw new PortalAPIException(FAILED_TO_GET_USERS + getUsersResponse.right());
260 } catch (Exception e) {
261 log.debug(FAILED_TO_GET_USERS);
262 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, FAILED_TO_GET_USERS, ErrorSeverity.INFO);
263 throw new PortalAPIException(FAILED_TO_GET_USERS, e);
268 public List<EcompRole> getAvailableRoles() throws PortalAPIException {
269 log.debug("Start handle request of ECOMP getAvailableRoles");
271 List<EcompRole> ecompRolesList = new LinkedList<>();
272 for (Role role : Role.values()) {
273 EcompRole ecompRole = new EcompRole();
274 ecompRole.setId(new Long(role.ordinal()));
275 ecompRole.setName(role.name());
276 ecompRolesList.add(ecompRole);
279 if (ecompRolesList.isEmpty()) {
280 throw new PortalAPIException();
283 return ecompRolesList;
284 } catch (Exception e) {
285 log.debug(FAILED_TO_FETCH_ROLES);
286 BeEcompErrorManager.getInstance().logInvalidInputError("GetAvailableRoles", FAILED_TO_FETCH_ROLES, ErrorSeverity.INFO);
287 throw new PortalAPIException("Roles fetching failed", e);
293 * The user role updated through this method only
296 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
297 log.debug("Start handle request of ECOMP pushUserRole");
299 final String modifierAttId = JH0003;
300 User modifier = new User();
301 modifier.setUserId(modifierAttId);
302 log.debug("modifier id is {}", modifierAttId);
304 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
306 String updatedRole = null;
309 throw new PortalAPIException("Error: Recieved null for roles");
310 } else if (roles.iterator().hasNext()) {
311 EcompRole ecompRole = roles.iterator().next();
312 updatedRole = EcompRoleConverter.convertEcompRoleToRole(ecompRole);
313 log.debug("pushing role: {} to user: {}", updatedRole, loginId);
314 Either<User, ResponseFormat> updateUserRoleResponse = userBusinessLogic.updateUserRole(modifier, loginId, updatedRole);
315 if (updateUserRoleResponse.isRight()) {
316 log.debug("Error: Failed to update role");
317 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, "Failed to update role", ErrorSeverity.INFO);
318 throw new PortalAPIException("Failed to update role" + updateUserRoleResponse.right().value().toString());
321 log.debug("Error: No roles in List");
322 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, FAILED_TO_FETCH_ROLES, ErrorSeverity.INFO);
323 //in this cases we want to deactivate the user
324 Either<User, ResponseFormat> deActivateUserResponse = userBusinessLogic.deActivateUser(modifier, loginId);
325 if (deActivateUserResponse.isRight()) {
326 log.debug("Error: Failed to deactivate user {}",loginId);
327 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, "Failed to deactivate user", ErrorSeverity.INFO);
328 throw new PortalAPIException(deActivateUserResponse.right().value().getFormattedMessage());
334 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
336 log.debug("Start handle request of ECOMP getUserRoles");
338 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
340 Either<User, ActionStatus> getUserResponse = userBusinessLogic.getUser(loginId, false);
342 if (getUserResponse.isRight()) {
343 log.debug(ERROR_FAILED_TO_GET_ROLES);
344 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_GET_ROLES, ErrorSeverity.INFO);
345 throw new PortalAPIException(FAILED_TO_GET_ROLES + getUserResponse.right());
347 if (getUserResponse.left().value() != null) {
348 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(getUserResponse.left().value());
349 if (ecompUser.isRight()) {
350 log.debug("Error: Failed to convert Roles");
351 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_CONVERT_ROLES, ErrorSeverity.ERROR);
352 throw new PortalAPIException(ecompUser.right().value());
353 } else if (ecompUser.left().value() == null) {
354 log.debug("Error: Failed to convert Roles");
355 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_CONVERT_ROLES, ErrorSeverity.ERROR);
356 throw new PortalAPIException();
359 return new LinkedList<>(ecompUser.left().value().getRoles());
361 log.debug(ERROR_FAILED_TO_GET_ROLES);
362 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_GET_ROLES, ErrorSeverity.ERROR);
363 throw new PortalAPIException(FAILED_TO_GET_ROLES + getUserResponse.right());
366 } catch (Exception e) {
367 log.debug(ERROR_FAILED_TO_GET_ROLES);
368 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_GET_ROLES, ErrorSeverity.INFO);
369 throw new PortalAPIException(FAILED_TO_GET_ROLES, e);
374 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
375 // TODO Validation should be changed completely
376 final String USERNAME = request.getHeader("username");
377 final String PASSWORD = request.getHeader("password");
379 if (USERNAME != null && PASSWORD != null) {
380 if (!USERNAME.equals("") && !PASSWORD.equals("")) {
381 log.debug("User authenticated - Username: ,Password: {}", USERNAME, PASSWORD);
386 log.debug("User authentication failed");
390 private UserBusinessLogic getUserBusinessLogic() {
391 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
392 return (UserBusinessLogic) ctx.getBean("userBusinessLogic");
396 * Gets and returns the userId for the logged-in user based on the request.
397 * If any error occurs, the method should throw PortalApiException with an
398 * appropriate message. The FW library will catch the exception and send an
399 * appropriate response to Portal.
401 * As a guideline for AT&T specific implementation, see the sample apps
403 * https://codecloud.web.att.com/projects/EP_SDK/repos/ecomp_portal_sdk_third_party/
404 * for a sample implementation for on-boarded applications using EPSDK-FW.
405 * However, the app can always choose to have a custom implementation of
406 * this method. For Open-source implementation, for example, the app will
407 * have a totally different implementation for this method.
410 * @return true if the request contains appropriate credentials, else false.
411 * @throws PortalAPIException
412 * If an unexpected error occurs while processing the request.
415 public String getUserId(HttpServletRequest request) throws PortalAPIException {
416 return request.getHeader(Constants.USER_ID_HEADER);