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.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.be.user.UserBusinessLogic;
34 import org.openecomp.sdc.common.api.Constants;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.openecomp.sdc.exception.ResponseFormat;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.web.context.ContextLoader;
40 import javax.servlet.http.HttpServletRequest;
41 import java.util.HashMap;
44 public final class PortalRestAPICentralServiceImpl implements IPortalRestCentralService {
45 private static final String FAILED_TO_UPDATE_USER_CREDENTIALS = "Failed to update user credentials";
46 private static final String FAILED_TO_UPDATE_USER_ROLE = "Failed to update user role";
47 private static final String FAILED_TO_DEACTIVATE_USER = "Failed to deactivate user {}";
48 private static final String FAILED_TO_DEACTIVATE_USER2 = "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 NULL_POINTER_RETURNED_FROM_USER_CONVERTER = "NULL pointer returned from user converter";
57 private static final String FAILED_TO_CREATE_USER = "Failed to create user {}";
58 private static final String FAILED_TO_CONVERT_USER = "Failed to convert user";
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;
65 public PortalRestAPICentralServiceImpl() throws PortalAPIException {
67 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
68 userBusinessLogic = (UserBusinessLogic) ctx.getBean("userBusinessLogic");
69 } catch (Exception e) {
70 log.debug("Failed to get user UserBusinessLogic", e);
71 BeEcompErrorManager.getInstance().logInvalidInputError("constructor", "Exception thrown" + e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR);
72 throw new PortalAPIException("SDC Internal server error");
74 log.debug("PortalRestAPICentralServiceImpl Class Instantiated");
77 PortalRestAPICentralServiceImpl(UserBusinessLogic ubl) {
78 this.userBusinessLogic = ubl;
82 public Map<String, String> getAppCredentials() throws PortalAPIException {
83 Map<String, String> credMap = new HashMap<>();
84 String portal_user = PortalApiProperties.getProperty(PortalPropertiesEnum.PORTAL_USER.value);
85 String password = PortalApiProperties.getProperty(PortalPropertiesEnum.PORTAL_PASS.value);
86 String appName = PortalApiProperties.getProperty(PortalPropertiesEnum.PORTAL_APP_NAME.value);
88 credMap.put(PortalPropertiesEnum.PORTAL_USER.value, CipherUtil.decryptPKC(portal_user));
89 credMap.put(PortalPropertiesEnum.PORTAL_PASS.value, CipherUtil.decryptPKC(password));
90 credMap.put(PortalPropertiesEnum.PORTAL_APP_NAME.value, CipherUtil.decryptPKC(appName));
91 } catch (CipherUtilException e) {
92 log.debug("User authentication failed - Decryption failed", e);
93 throw new PortalAPIException("Failed to decrypt" + e.getMessage());
100 public void pushUser(EcompUser user) throws PortalAPIException {
101 log.debug("Start handle request of ECOMP pushUser");
104 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
105 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
106 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
108 checkIfSingleRoleProvided(user);
110 final String modifierAttId = JH0003;
111 User modifier = new User();
112 modifier.setUserId(modifierAttId);
113 log.debug("modifier id is {}", modifierAttId);
115 User convertedAsdcUser = EcompUserConverter.convertEcompUserToUser(user);
116 if (convertedAsdcUser == null) {
117 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CONVERT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
118 log.debug(FAILED_TO_CREATE_USER, user);
119 throw new PortalAPIException("Failed to convert user " + user);
122 Either<User, ResponseFormat> createUserResponse = userBusinessLogic.createUser(modifier, convertedAsdcUser);
124 // ALREADY EXIST ResponseFormat
125 final String ALREADY_EXISTS_RESPONSE_ID = "SVC4006";
127 if (createUserResponse.isRight()) {
128 if (!createUserResponse.right().value().getMessageId().equals(ALREADY_EXISTS_RESPONSE_ID)) {
129 log.debug(FAILED_TO_CREATE_USER, user);
130 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, BeEcompErrorManager.ErrorSeverity.ERROR);
131 throw new PortalAPIException(FAILED_TO_CREATE_USER + createUserResponse.right().value());
133 log.debug("User already exist and will be updated and reactivated {}", user);
134 Either<User, ResponseFormat> updateUserResp = userBusinessLogic.updateUserCredentials(convertedAsdcUser);
135 if(updateUserResp.isRight()){
136 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS, user);
137 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, BeEcompErrorManager.ErrorSeverity.ERROR);
138 throw new PortalAPIException(FAILED_TO_UPDATE_USER_CREDENTIALS + createUserResponse.right().value());
140 Either<User, ResponseFormat> updateUserRoleResp = userBusinessLogic.updateUserRole(modifier, convertedAsdcUser.getUserId(), convertedAsdcUser.getRole());
141 if(updateUserRoleResp.isRight()){
142 log.debug(FAILED_TO_UPDATE_USER_ROLE, user);
143 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_UPDATE_USER_ROLE, BeEcompErrorManager.ErrorSeverity.ERROR);
144 throw new PortalAPIException(FAILED_TO_UPDATE_USER_ROLE + createUserResponse.right().value());
149 log.debug("User created {}", user);
153 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
154 log.debug("Start handle request of ECOMP editUser");
157 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
158 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
159 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
160 } else if (loginId == null) {
161 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
162 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, BeEcompErrorManager.ErrorSeverity.INFO);
163 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
166 checkIfSingleRoleProvided(user);
168 final String modifierAttId = JH0003;
169 User modifier = new User();
170 modifier.setUserId(modifierAttId);
171 log.debug("modifier id is {}", modifierAttId);
173 if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
174 log.debug("loginId and user loginId not equal");
175 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, "loginId and user loginId not equal", BeEcompErrorManager.ErrorSeverity.INFO);
176 throw new PortalAPIException("loginId not equals to the user loginId field");
177 } else if (user.getLoginId() == null) {
178 user.setLoginId(loginId);
181 User asdcUser = EcompUserConverter.convertEcompUserToUser(user);
182 if (asdcUser == null) {
183 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CONVERT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
184 log.debug(FAILED_TO_CREATE_USER, user);
185 throw new PortalAPIException("Failed to convert user " + user);
188 Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser);
190 if (updateUserCredentialsResponse.isRight()) {
191 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
192 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, BeEcompErrorManager.ErrorSeverity.ERROR);
193 throw new PortalAPIException(FAILED_TO_EDIT_USER + updateUserCredentialsResponse.right().value());
196 Either<User, ResponseFormat> deActivateUser;
198 if(asdcUser.getRole() == null || asdcUser.getRole().isEmpty()){
199 deActivateUser = userBusinessLogic.deActivateUser(modifier, asdcUser.getUserId());
204 if (deActivateUser.isRight()) {
205 log.debug(FAILED_TO_DEACTIVATE_USER, asdcUser);
206 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, FAILED_TO_DEACTIVATE_USER2, BeEcompErrorManager.ErrorSeverity.ERROR);
207 throw new PortalAPIException(FAILED_TO_DEACTIVATE_USER2 + deActivateUser.right().value());
212 public String getUserId(HttpServletRequest request) throws PortalAPIException {
213 return request.getHeader(Constants.USER_ID_HEADER);
217 private void checkIfSingleRoleProvided(EcompUser user) throws PortalAPIException {
218 if(user.getRoles() == null) {
219 log.debug(RECEIVED_NULL_ROLES, user);
220 BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_NULL_ROLES, BeEcompErrorManager.ErrorSeverity.ERROR);
221 throw new PortalAPIException(RECEIVED_NULL_ROLES + user);
222 }else if(user.getRoles().size() > 1) {
223 log.debug(RECEIVED_MULTIPLE_ROLES, user);
224 BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_MULTIPLE_ROLES2, BeEcompErrorManager.ErrorSeverity.ERROR);
225 throw new PortalAPIException(FAILED_TO_DEACTIVATE_USER2 + user);
229 public enum PortalPropertiesEnum{
230 PORTAL_PASS ("portal_pass"),
231 PORTAL_USER("portal_user"),
232 PORTAL_APP_NAME("portal_app_name");
234 private final String value;
236 PortalPropertiesEnum(String value) {
240 public String value() {