Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / ecomp / PortalRestAPICentralServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.ecomp;
22
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;
42
43 import javax.servlet.http.HttpServletRequest;
44 import java.util.HashMap;
45 import java.util.Map;
46
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;
65
66     public PortalRestAPICentralServiceImpl() throws PortalAPIException {
67         try {
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");
75         }
76         log.debug("PortalRestAPICentralServiceImpl Class Instantiated");
77     }
78
79     //For testing purposes
80     PortalRestAPICentralServiceImpl(UserBusinessLogic ubl, UserBusinessLogicExt uble) {
81         this.userBusinessLogic = ubl;
82         this.userBusinessLogicExt = uble;
83     }
84
85     @Override
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());
91         try {
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());
98         }
99         log.debug("the credentials map for portal is {}", credMap);
100         return credMap;
101     }
102
103     @Override
104     public void pushUser(EcompUser user) throws PortalAPIException {
105         log.debug("Start handle request of ECOMP pushUser");
106
107         if (user == null) {
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);
111         }
112         checkIfSingleRoleProvided(user);
113
114         final String modifierAttId = JH0003;
115         log.debug("modifier id is {}", modifierAttId);
116
117         User convertedAsdcUser = EcompUserConverter.convertEcompUserToUser(user);
118
119         try{
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());
126         }
127
128         log.debug("User created ecomp user {} sdc user {}", user, convertedAsdcUser);
129     }
130
131     @Override
132     public void editUser(String loginId, EcompUser user) throws PortalAPIException {
133         if (user == null) {
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);
141         }
142
143         log.debug("Start handle request of ECOMP editUser {} with loginId {} with follopwing roles {}", user, loginId, user.getRoles());
144
145         final String modifierAttId = JH0003;
146         log.debug("modifier id is {}", modifierAttId);
147
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);
154         }
155
156         Either<User, ActionStatus> verifyNewUser;
157         try{
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());
162         }
163
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);
168             pushUser(user);
169             return;
170         }
171
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);
175
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());
180         }
181
182         if(user.getRoles() == null || user.getRoles().isEmpty()){
183             try {
184                 log.debug("Before deactivating ecomp user {} sdc user {}", user, asdcUser);
185                 userBusinessLogicExt.deActivateUser(modifierAttId, loginId);
186             }
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);
191             }
192         } else {
193             checkIfSingleRoleProvided(user);
194             try {
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);
201             }
202         }
203         log.debug("user updated ecomp user {} sdc user {}", user, asdcUser);
204     }
205
206     @Override
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);
213         }
214         return header;
215     }
216
217
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);
227         }
228     }
229
230
231 }