2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.HashMap;
25 import javax.servlet.http.HttpServletRequest;
26 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
27 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
28 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
29 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
30 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
31 import org.onap.portalsdk.core.restful.domain.EcompUser;
32 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.user.UserBusinessLogic;
38 import org.openecomp.sdc.be.user.UserBusinessLogicExt;
39 import org.openecomp.sdc.common.api.Constants;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.openecomp.sdc.exception.ResponseFormat;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.web.context.ContextLoader;
45 public final class PortalRestApiCentralServiceImpl implements IPortalRestCentralService {
47 private static final String FAILED_TO_UPDATE_USER_CREDENTIALS = "Failed to update user credentials";
48 private static final String FAILED_TO_DEACTIVATE_USER = "Failed to deactivate user {}";
49 private static final String FAILED_TO_EDIT_USER = "Failed to edit user";
50 private static final String EDIT_USER = "EditUser";
51 private static final String CHECK_ROLES = "checkIfSingleRoleProvided";
52 private static final String RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID = "Received null for argument loginId";
53 private static final String RECEIVED_NULL_ROLES = "Received null roles for user";
54 private static final String RECEIVED_MULTIPLE_ROLES = "Received multiple roles for user {}";
55 private static final String RECEIVED_MULTIPLE_ROLES2 = "Received multiple roles for user";
56 private static final String FAILED_TO_CREATE_USER = "Failed to create user {}";
57 private static final String FAILED_TO_GET_USER_ID_HEADER = "Failed to get user_id header";
58 private static final String JH0003 = "jh0003";
59 private static final String PUSH_USER = "PushUser";
60 private static final String RECEIVED_NULL_FOR_ARGUMENT_USER = "Received null for argument user";
61 private static final Logger log = Logger.getLogger(PortalRestApiCentralServiceImpl.class);
62 private UserBusinessLogic userBusinessLogic;
63 private UserBusinessLogicExt userBusinessLogicExt;
65 public PortalRestApiCentralServiceImpl() throws PortalAPIException {
67 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
68 userBusinessLogic = (UserBusinessLogic) ctx.getBean("userBusinessLogic");
69 userBusinessLogicExt = (UserBusinessLogicExt) ctx.getBean("userBusinessLogicExt");
70 } catch (Exception e) {
71 log.debug("Failed to get user UserBusinessLogic", e);
72 BeEcompErrorManager.getInstance()
73 .logInvalidInputError("constructor", "Exception thrown" + e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR);
74 throw new PortalAPIException("SDC Internal server error");
76 log.debug("PortalRestApiCentralServiceImpl Class Instantiated");
79 //For testing purposes
80 PortalRestApiCentralServiceImpl(UserBusinessLogic ubl, UserBusinessLogicExt uble) {
81 this.userBusinessLogic = ubl;
82 this.userBusinessLogicExt = uble;
85 public static void checkIfSingleRoleProvided(EcompUser user) throws PortalAPIException {
86 if (user.getRoles() == null) {
87 log.debug(RECEIVED_NULL_ROLES, user);
88 BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_NULL_ROLES, BeEcompErrorManager.ErrorSeverity.ERROR);
89 throw new PortalAPIException(RECEIVED_NULL_ROLES + user);
90 } else if (user.getRoles().size() > 1) {
91 log.debug(RECEIVED_MULTIPLE_ROLES, user);
92 BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_MULTIPLE_ROLES2, BeEcompErrorManager.ErrorSeverity.ERROR);
93 throw new PortalAPIException(RECEIVED_MULTIPLE_ROLES2 + user);
98 public Map<String, String> getAppCredentials() throws PortalAPIException {
99 Map<String, String> credMap = new HashMap<>();
100 String portalUser = PortalApiProperties.getProperty(PortalPropertiesEnum.USER.value());
101 String password = PortalApiProperties.getProperty(PortalPropertiesEnum.PASSWORD.value());
102 String appName = PortalApiProperties.getProperty(PortalPropertiesEnum.APP_NAME.value());
104 credMap.put("username", CipherUtil.decryptPKC(portalUser));
105 credMap.put("password", CipherUtil.decryptPKC(password));
106 credMap.put("appName", CipherUtil.decryptPKC(appName));
107 } catch (CipherUtilException e) {
108 log.debug("User authentication failed - Decryption failed", e);
109 throw new PortalAPIException("Failed to decrypt" + e.getMessage());
111 log.debug("the credentials map for portal is {}", credMap);
116 public void pushUser(EcompUser user) throws PortalAPIException {
117 log.debug("Start handle request of ECOMP pushUser");
119 BeEcompErrorManager.getInstance()
120 .logInvalidInputError(PUSH_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
121 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
122 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
124 checkIfSingleRoleProvided(user);
125 final String modifierAttId = JH0003;
126 log.debug("modifier id is {}", modifierAttId);
127 User convertedAsdcUser = EcompUserConverter.convertEcompUserToUser(user);
129 log.debug("Before creating ecomp user {} sdc user {}", user, convertedAsdcUser);
130 userBusinessLogic.createUser(modifierAttId, convertedAsdcUser);
131 } catch (Exception e) {
132 log.debug(FAILED_TO_CREATE_USER, user, e);
133 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, BeEcompErrorManager.ErrorSeverity.ERROR);
134 throw new PortalAPIException(FAILED_TO_CREATE_USER + e.getMessage());
136 log.debug("User created ecomp user {} sdc user {}", user, convertedAsdcUser);
140 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
142 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
143 BeEcompErrorManager.getInstance()
144 .logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
145 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
146 } else if (loginId == null) {
147 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
148 BeEcompErrorManager.getInstance()
149 .logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, BeEcompErrorManager.ErrorSeverity.INFO);
150 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
152 log.debug("Start handle request of ECOMP editUser {} with loginId {} with follopwing roles {}", user, loginId, user.getRoles());
153 final String modifierAttId = JH0003;
154 log.debug("modifier id is {}", modifierAttId);
155 if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
156 log.debug("loginId and user loginId not equal");
157 BeEcompErrorManager.getInstance()
158 .logInvalidInputError(EDIT_USER, "loginId and user loginId not equal", BeEcompErrorManager.ErrorSeverity.INFO);
159 throw new PortalAPIException("loginId not equals to the user loginId field");
160 } else if (user.getLoginId() == null) {
161 user.setLoginId(loginId);
163 Either<User, ActionStatus> verifyNewUser;
165 verifyNewUser = userBusinessLogic.verifyNewUserForPortal(user.getLoginId());
166 } catch (ComponentException e) {
167 log.debug("Failed to verify new user", e);
168 throw new PortalAPIException(e.getCause());
170 if (verifyNewUser.isRight() && (ActionStatus.USER_NOT_FOUND.equals(verifyNewUser.right().value()) || ActionStatus.USER_INACTIVE
171 .equals(verifyNewUser.right().value()))) {
172 log.debug("Edit user for user that not exist in DB, executing push user flow {}", user);
176 User asdcUser = EcompUserConverter.convertEcompUserToUser(user);
177 log.debug("Before editing ecomp user {} sdc user {}", user, asdcUser);
178 Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser);
179 if (updateUserCredentialsResponse.isRight()) {
180 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
181 BeEcompErrorManager.getInstance()
182 .logInvalidInputError(EDIT_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, BeEcompErrorManager.ErrorSeverity.ERROR);
183 throw new PortalAPIException(FAILED_TO_EDIT_USER + updateUserCredentialsResponse.right().value());
185 if (user.getRoles() == null || user.getRoles().isEmpty()) {
187 log.debug("Before deactivating ecomp user {} sdc user {}", user, asdcUser);
188 userBusinessLogicExt.deActivateUser(modifierAttId, loginId);
189 } catch (Exception e) {
190 log.debug("Error: Failed to deactivate user {}", loginId);
191 BeEcompErrorManager.getInstance()
192 .logInvalidInputError(FAILED_TO_DEACTIVATE_USER, "Failed to deactivate user", BeEcompErrorManager.ErrorSeverity.INFO);
193 throw new PortalAPIException("Error: Failed to deactivate user" + e);
196 checkIfSingleRoleProvided(user);
198 log.debug("Before updating ecomp user {} sdc user {}", user, asdcUser);
199 userBusinessLogic.updateUserRole(modifierAttId, loginId, asdcUser.getRole());
200 } catch (Exception e) {
201 log.debug("Error: Failed to update user role {}", loginId);
202 BeEcompErrorManager.getInstance()
203 .logInvalidInputError(FAILED_TO_EDIT_USER, "Failed to update user role", BeEcompErrorManager.ErrorSeverity.INFO);
204 throw new PortalAPIException("Error: Failed to update user role" + e);
207 log.debug("user updated ecomp user {} sdc user {}", user, asdcUser);
211 public String getUserId(HttpServletRequest request) throws PortalAPIException {
212 String header = request.getHeader(Constants.USER_ID_HEADER);
213 if (header == null) {
214 log.debug(FAILED_TO_GET_USER_ID_HEADER);
215 BeEcompErrorManager.getInstance()
216 .logInvalidInputError("getUserId", FAILED_TO_GET_USER_ID_HEADER, BeEcompErrorManager.ErrorSeverity.ERROR);
217 throw new PortalAPIException(FAILED_TO_GET_USER_ID_HEADER);