Added oparent to sdc main
[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.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;
39
40 import javax.servlet.http.HttpServletRequest;
41 import java.util.HashMap;
42 import java.util.Map;
43
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;
64
65     public PortalRestAPICentralServiceImpl() throws PortalAPIException {
66         try {
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");
73         }
74         log.debug("PortalRestAPICentralServiceImpl Class Instantiated");
75     }
76
77     PortalRestAPICentralServiceImpl(UserBusinessLogic ubl) {
78         this.userBusinessLogic = ubl;
79     }
80
81     @Override
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);
87         try {
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());
94         }
95
96         return credMap;
97     }
98
99     @Override
100     public void pushUser(EcompUser user) throws PortalAPIException {
101         log.debug("Start handle request of ECOMP pushUser");
102
103         if (user == null) {
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);
107         }
108         checkIfSingleRoleProvided(user);
109
110         final String modifierAttId = JH0003;
111         User modifier = new User();
112         modifier.setUserId(modifierAttId);
113         log.debug("modifier id is {}", modifierAttId);
114
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);
120         }
121
122         Either<User, ResponseFormat> createUserResponse = userBusinessLogic.createUser(modifier, convertedAsdcUser);
123
124         // ALREADY EXIST ResponseFormat
125         final String ALREADY_EXISTS_RESPONSE_ID = "SVC4006";
126
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());
132             } else {
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());
139                 }
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());
145                 }
146             }
147
148         }
149         log.debug("User created {}", user);
150     }
151
152     @Override
153     public void editUser(String loginId, EcompUser user) throws PortalAPIException {
154         log.debug("Start handle request of ECOMP editUser");
155
156         if (user == null) {
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);
164         }
165
166         checkIfSingleRoleProvided(user);
167
168         final String modifierAttId = JH0003;
169         User modifier = new User();
170         modifier.setUserId(modifierAttId);
171         log.debug("modifier id is {}", modifierAttId);
172
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);
179         }
180
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);
186         }
187
188         Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser);
189
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());
194         }
195
196         Either<User, ResponseFormat> deActivateUser;
197
198         if(asdcUser.getRole() == null || asdcUser.getRole().isEmpty()){
199             deActivateUser = userBusinessLogic.deActivateUser(modifier, asdcUser.getUserId());
200         } else {
201             return;
202         }
203
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());
208         }
209     }
210
211     @Override
212     public String getUserId(HttpServletRequest request) throws PortalAPIException {
213         return request.getHeader(Constants.USER_ID_HEADER);
214     }
215
216
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);
226         }
227     }
228
229     public enum PortalPropertiesEnum{
230         PORTAL_PASS ("portal_pass"),
231         PORTAL_USER("portal_user"),
232         PORTAL_APP_NAME("portal_app_name");
233
234         private final String value;
235
236         PortalPropertiesEnum(String value) {
237             this.value = value;
238         }
239
240         public String value() {
241             return value;
242         }
243     }
244 }