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=========================================================
21 package org.openecomp.sdc.be.ecomp;
23 import fj.data.Either;
24 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
25 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
26 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
27 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
28 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
29 import org.onap.portalsdk.core.restful.domain.EcompUser;
30 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
31 import org.openecomp.sdc.be.config.BeEcompErrorManager;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
34 import org.openecomp.sdc.be.model.User;
35 import org.openecomp.sdc.be.user.UserBusinessLogic;
36 import org.openecomp.sdc.be.user.UserBusinessLogicExt;
37 import org.openecomp.sdc.common.api.Constants;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.openecomp.sdc.exception.ResponseFormat;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.web.context.ContextLoader;
43 import javax.servlet.http.HttpServletRequest;
44 import java.util.HashMap;
47 public final class PortalRestAPICentralServiceImpl implements IPortalRestCentralService {
48 private static final String FAILED_TO_UPDATE_USER_CREDENTIALS = "Failed to update user credentials";
49 private static final String FAILED_TO_DEACTIVATE_USER = "Failed to deactivate user {}";
50 private static final String FAILED_TO_EDIT_USER = "Failed to edit user";
51 private static final String EDIT_USER = "EditUser";
52 private static final String CHECK_ROLES = "checkIfSingleRoleProvided";
53 private static final String RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID = "Received null for argument loginId";
54 private static final String RECEIVED_NULL_ROLES = "Received null roles for user";
55 private static final String RECEIVED_MULTIPLE_ROLES = "Received multiple roles for user {}";
56 private static final String RECEIVED_MULTIPLE_ROLES2 = "Received multiple roles for user";
57 private static final String FAILED_TO_CREATE_USER = "Failed to create user {}";
58 private static final String FAILED_TO_GET_USER_ID_HEADER = "Failed to get user_id header";
59 private static final String JH0003 = "jh0003";
60 private static final String PUSH_USER = "PushUser";
61 private static final String RECEIVED_NULL_FOR_ARGUMENT_USER = "Received null for argument user";
62 private static final Logger log = Logger.getLogger(PortalRestAPICentralServiceImpl.class);
63 private UserBusinessLogic userBusinessLogic;
64 private UserBusinessLogicExt userBusinessLogicExt;
66 public PortalRestAPICentralServiceImpl() throws PortalAPIException {
68 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
69 userBusinessLogic = (UserBusinessLogic) ctx.getBean("userBusinessLogic");
70 userBusinessLogicExt = (UserBusinessLogicExt) ctx.getBean("userBusinessLogicExt");
71 } catch (Exception e) {
72 log.debug("Failed to get user UserBusinessLogic", e);
73 BeEcompErrorManager.getInstance().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;
86 public Map<String, String> getAppCredentials() throws PortalAPIException {
87 Map<String, String> credMap = new HashMap<>();
88 String portal_user = PortalApiProperties.getProperty(PortalPropertiesEnum.USER.value());
89 String password = PortalApiProperties.getProperty(PortalPropertiesEnum.PASSWORD.value());
90 String appName = PortalApiProperties.getProperty(PortalPropertiesEnum.APP_NAME.value());
92 credMap.put("username", CipherUtil.decryptPKC(portal_user));
93 credMap.put("password", CipherUtil.decryptPKC(password));
94 credMap.put("appName", CipherUtil.decryptPKC(appName));
95 } catch (CipherUtilException e) {
96 log.debug("User authentication failed - Decryption failed", e);
97 throw new PortalAPIException("Failed to decrypt" + e.getMessage());
99 log.debug("the credentials map for portal is {}", credMap);
104 public void pushUser(EcompUser user) throws PortalAPIException {
105 log.debug("Start handle request of ECOMP pushUser");
108 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
109 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
110 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
112 checkIfSingleRoleProvided(user);
114 final String modifierAttId = JH0003;
115 log.debug("modifier id is {}", modifierAttId);
117 User convertedAsdcUser = EcompUserConverter.convertEcompUserToUser(user);
120 log.debug("Before creating ecomp user {} sdc user {}", user, convertedAsdcUser);
121 userBusinessLogic.createUser(modifierAttId, convertedAsdcUser);
122 }catch (Exception e) {
123 log.debug(FAILED_TO_CREATE_USER, user, e);
124 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, BeEcompErrorManager.ErrorSeverity.ERROR);
125 throw new PortalAPIException(FAILED_TO_CREATE_USER + e.getMessage());
128 log.debug("User created ecomp user {} sdc user {}", user, convertedAsdcUser);
132 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
134 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
135 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
136 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
137 } else if (loginId == null) {
138 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
139 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, BeEcompErrorManager.ErrorSeverity.INFO);
140 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
143 log.debug("Start handle request of ECOMP editUser {} with loginId {} with follopwing roles {}", user, loginId, user.getRoles());
145 final String modifierAttId = JH0003;
146 log.debug("modifier id is {}", modifierAttId);
148 if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
149 log.debug("loginId and user loginId not equal");
150 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, "loginId and user loginId not equal", BeEcompErrorManager.ErrorSeverity.INFO);
151 throw new PortalAPIException("loginId not equals to the user loginId field");
152 } else if (user.getLoginId() == null) {
153 user.setLoginId(loginId);
156 Either<User, ActionStatus> verifyNewUser;
158 verifyNewUser = userBusinessLogic.verifyNewUserForPortal(user.getLoginId());
159 } catch (ComponentException e){
160 log.debug("Failed to verify new user", e);
161 throw new PortalAPIException(e.getCause());
164 if(verifyNewUser.isRight() &&
165 (ActionStatus.USER_NOT_FOUND.equals(verifyNewUser.right().value()) ||
166 ActionStatus.USER_INACTIVE.equals(verifyNewUser.right().value()))){
167 log.debug("Edit user for user that not exist in DB, executing push user flow {}", user);
172 User asdcUser = EcompUserConverter.convertEcompUserToUser(user);
173 log.debug("Before editing ecomp user {} sdc user {}", user, asdcUser);
174 Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser);
176 if (updateUserCredentialsResponse.isRight()) {
177 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
178 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, BeEcompErrorManager.ErrorSeverity.ERROR);
179 throw new PortalAPIException(FAILED_TO_EDIT_USER + updateUserCredentialsResponse.right().value());
182 if(user.getRoles() == null || user.getRoles().isEmpty()){
184 log.debug("Before deactivating ecomp user {} sdc user {}", user, asdcUser);
185 userBusinessLogicExt.deActivateUser(modifierAttId, loginId);
187 catch (Exception e) {
188 log.debug("Error: Failed to deactivate user {}", loginId);
189 BeEcompErrorManager.getInstance().logInvalidInputError(FAILED_TO_DEACTIVATE_USER, "Failed to deactivate user", BeEcompErrorManager.ErrorSeverity.INFO);
190 throw new PortalAPIException("Error: Failed to deactivate user" + e);
193 checkIfSingleRoleProvided(user);
195 log.debug("Before updating ecomp user {} sdc user {}", user, asdcUser);
196 userBusinessLogic.updateUserRole(modifierAttId, loginId, asdcUser.getRole());
197 }catch (Exception e) {
198 log.debug("Error: Failed to update user role {}", loginId);
199 BeEcompErrorManager.getInstance().logInvalidInputError(FAILED_TO_EDIT_USER, "Failed to update user role", BeEcompErrorManager.ErrorSeverity.INFO);
200 throw new PortalAPIException("Error: Failed to update user role" + e);
203 log.debug("user updated ecomp user {} sdc user {}", user, asdcUser);
207 public String getUserId(HttpServletRequest request) throws PortalAPIException {
208 String header = request.getHeader(Constants.USER_ID_HEADER);
209 if (header == null) {
210 log.debug(FAILED_TO_GET_USER_ID_HEADER);
211 BeEcompErrorManager.getInstance().logInvalidInputError("getUserId", FAILED_TO_GET_USER_ID_HEADER, BeEcompErrorManager.ErrorSeverity.ERROR);
212 throw new PortalAPIException(FAILED_TO_GET_USER_ID_HEADER);
218 public static void checkIfSingleRoleProvided(EcompUser user) throws PortalAPIException {
219 if(user.getRoles() == null) {
220 log.debug(RECEIVED_NULL_ROLES, user);
221 BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_NULL_ROLES, BeEcompErrorManager.ErrorSeverity.ERROR);
222 throw new PortalAPIException(RECEIVED_NULL_ROLES + user);
223 }else if(user.getRoles().size() > 1) {
224 log.debug(RECEIVED_MULTIPLE_ROLES, user);
225 BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_MULTIPLE_ROLES2, BeEcompErrorManager.ErrorSeverity.ERROR);
226 throw new PortalAPIException(RECEIVED_MULTIPLE_ROLES2 + user);