Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / ecomp / EcompIntImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.IPortalRestAPIService;
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.EcompRole;
30 import org.onap.portalsdk.core.restful.domain.EcompUser;
31 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
32 import org.openecomp.sdc.be.config.BeEcompErrorManager;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.ecomp.converters.EcompRoleConverter;
36 import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.be.user.Role;
39 import org.openecomp.sdc.be.user.UserBusinessLogic;
40 import org.openecomp.sdc.be.user.UserBusinessLogicExt;
41 import org.openecomp.sdc.common.api.Constants;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.openecomp.sdc.exception.ResponseFormat;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.web.context.ContextLoader;
46
47 import javax.servlet.http.HttpServletRequest;
48 import java.util.LinkedList;
49 import java.util.List;
50 import java.util.Map;
51
52
53 public class EcompIntImpl implements IPortalRestAPIService {
54         private static final String FAILED_TO_CONVERT_ROLES = "Failed to convert Roles";
55         public static final String FAILED_TO_GET_ROLES = "Failed to get Roles";
56         private static final String GET_USER_ROLES = "GetUserRoles";
57         public static final String ERROR_FAILED_TO_GET_ROLES = "Error: Failed to get Roles";
58         private static final String PUSH_USER_ROLE = "PushUserRole";
59         private static final String FAILED_TO_FETCH_ROLES = "Failed to fetch roles";
60         private static final String FAILED_TO_CONVERT_USER2 = "Failed to convert User {}";
61         private static final String GET_USERS = "GetUsers";
62         private static final String FAILED_TO_GET_USERS = "Failed to get Users";
63         private static final String GET_USER = "GetUser";
64         private static final String FAILED_TO_GET_USER = "Failed to get User";
65         private static final String FAILED_TO_UPDATE_USER_CREDENTIALS = "Failed to updateUserCredentials";
66         private static final String FAILED_TO_EDIT_USER = "Failed to edit user";
67         private static final String EDIT_USER = "EditUser";
68         private static final String RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID = "Received null for argument loginId";
69         private static final String NULL_POINTER_RETURNED_FROM_USER_CONVERTER = "NULL pointer returned from user converter";
70         private static final String FAILED_TO_CREATE_USER = "Failed to create user {}";
71         private static final String FAILED_TO_CONVERT_USER = "Failed to convert user";
72         private static final String JH0003 = "jh0003";
73         private static final String PUSH_USER = "PushUser";
74         private static final String RECEIVED_NULL_FOR_ARGUMENT_USER = "Received null for argument user";
75         private static final Logger log = Logger.getLogger(EcompIntImpl.class.getName());
76
77     public EcompIntImpl() {
78         log.debug("EcompIntImpl Class Instantiated");
79     }
80
81     @Override
82     public void pushUser(EcompUser user) throws PortalAPIException {
83         log.debug("Start handle request of ECOMP pushUser");
84         try {
85             if (user == null) {
86                 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, ErrorSeverity.INFO);
87                 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
88                 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
89             }
90
91             UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
92
93             final String modifierAttId = JH0003;
94             log.debug("modifier id is {}", modifierAttId);
95
96             User convertedAsdcUser = EcompUserConverter.convertEcompUserToUser(user);
97             if (convertedAsdcUser == null) {
98                 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, NULL_POINTER_RETURNED_FROM_USER_CONVERTER, ErrorSeverity.INFO);
99                 log.debug(FAILED_TO_CREATE_USER, user);
100                 throw new PortalAPIException("Failed to create user " + convertedAsdcUser);
101             }
102             userBusinessLogic.createUser(modifierAttId, convertedAsdcUser);
103             log.debug("User created {}", user);
104         } catch (ComponentException ce) {
105             if (ActionStatus.USER_ALREADY_EXIST.equals(ce.getActionStatus())) {
106                 log.debug("User already exist {}", user);
107             } else {
108                 log.debug(FAILED_TO_CREATE_USER, user);
109                 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, ErrorSeverity.ERROR);
110                 throw new PortalAPIException(FAILED_TO_CREATE_USER + ce.getActionStatus());
111             }
112         }
113         catch (Exception e) {
114             log.debug(FAILED_TO_CREATE_USER, user, e);
115             BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, ErrorSeverity.ERROR);
116             throw new PortalAPIException(FAILED_TO_CREATE_USER, e);
117         }
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      *
124      * loginId - equals to userId
125      *
126      */
127     @Override
128     public void editUser(String loginId, EcompUser user) throws PortalAPIException {
129         log.debug("Start handle request of ECOMP editUser");
130
131         try {
132             if (user == null) {
133                 log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
134                 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, ErrorSeverity.INFO);
135                 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
136             } else if (loginId == null) {
137                 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
138                 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, ErrorSeverity.INFO);
139                 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
140             }
141
142             UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
143
144             if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
145                 log.debug("loginId and user loginId not equal");
146                 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, "loginId and user loginId not equal", ErrorSeverity.INFO);
147                 throw new PortalAPIException("loginId not equals to the user loginId field");
148             } else if (user.getLoginId() == null) {
149                 user.setLoginId(loginId);
150             }
151
152             User asdcUser = EcompUserConverter.convertEcompUserToUser(user);
153             if (asdcUser == null) {
154                 log.debug(NULL_POINTER_RETURNED_FROM_USER_CONVERTER);
155                 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, NULL_POINTER_RETURNED_FROM_USER_CONVERTER, ErrorSeverity.INFO);
156                 throw new PortalAPIException(FAILED_TO_EDIT_USER);
157             }
158
159             Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser);
160
161             if (updateUserCredentialsResponse.isRight()) {
162                 log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
163                 BeEcompErrorManager.getInstance().logInvalidInputError(EDIT_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, ErrorSeverity.ERROR);
164                 throw new PortalAPIException(FAILED_TO_EDIT_USER + updateUserCredentialsResponse.right().value());
165             }
166         } catch (Exception e) {
167             log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
168             throw new PortalAPIException(FAILED_TO_EDIT_USER, e);
169         }
170
171     }
172
173     @Override
174     public EcompUser getUser(String loginId) throws PortalAPIException {
175         log.debug("Start handle request of ECOMP getUser");
176         try {
177             if (loginId == null) {
178                 log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
179                 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, ErrorSeverity.INFO);
180                 throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
181             }
182             UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
183             User user = userBusinessLogic.getUser(loginId, false);
184             Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
185             if (ecompUser.isLeft() && ecompUser.left().value() != null) {
186                 return ecompUser.left().value();
187             } else {
188                 log.debug(FAILED_TO_GET_USER);
189                 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, FAILED_TO_GET_USER, ErrorSeverity.INFO);
190                 throw new PortalAPIException(ecompUser.right().value());
191             }
192         } catch (ComponentException ce) {
193             log.debug(FAILED_TO_GET_USER);
194             BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER, FAILED_TO_GET_USER, ErrorSeverity.INFO);
195             throw new PortalAPIException(FAILED_TO_GET_USER + ce.getActionStatus());
196
197         }
198         catch (Exception e) {
199             log.debug(FAILED_TO_GET_USER);
200             throw new PortalAPIException(FAILED_TO_GET_USER, e);
201         }
202     }
203
204     @Override
205     public List<EcompUser> getUsers() throws PortalAPIException {
206         log.debug("Start handle request of ECOMP getUsers");
207
208         try {
209             UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
210             List<User> users = userBusinessLogic.getUsersList(JH0003, null, null);
211             List<EcompUser> ecompUserList = new LinkedList<>();
212             for (User user : users) {
213                 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
214                 if (ecompUser.isRight()) {
215                     log.debug(FAILED_TO_CONVERT_USER2, user);
216                     BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
217                     continue;
218                 } else if (ecompUser.left().value() == null) {
219                     log.debug(FAILED_TO_CONVERT_USER2, user);
220                     BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
221                     continue;
222                 }
223                 ecompUserList.add(ecompUser.left().value());
224             }
225             return ecompUserList;
226         } catch (Exception e) {
227             log.debug(FAILED_TO_GET_USERS);
228             BeEcompErrorManager.getInstance().logInvalidInputError(GET_USERS, FAILED_TO_GET_USERS, ErrorSeverity.INFO);
229             throw new PortalAPIException(FAILED_TO_GET_USERS, e);
230         }
231     }
232
233     @Override
234     public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
235         log.debug("Start handle request of ECOMP getAvailableRoles");
236         try {
237             List<EcompRole> ecompRolesList = new LinkedList<>();
238             for (Role role : Role.values()) {
239                 EcompRole ecompRole = new EcompRole();
240                 ecompRole.setId((long) role.ordinal());
241                 ecompRole.setName(role.name());
242                 ecompRolesList.add(ecompRole);
243             }
244
245             if (ecompRolesList.isEmpty()) {
246                 throw new PortalAPIException();
247             }
248
249             return ecompRolesList;
250         } catch (Exception e) {
251             log.debug(FAILED_TO_FETCH_ROLES);
252             BeEcompErrorManager.getInstance().logInvalidInputError("GetAvailableRoles", FAILED_TO_FETCH_ROLES, ErrorSeverity.INFO);
253             throw new PortalAPIException("Roles fetching failed", e);
254         }
255
256     }
257
258     /**
259      * The user role updated through this method only
260      */
261     @Override
262     public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
263         log.debug("Start handle request of ECOMP pushUserRole");
264
265         final String modifierAttId = JH0003;
266         log.debug("modifier id is {}", modifierAttId);
267
268         UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
269
270         String updatedRole;
271
272         if (roles == null) {
273             throw new PortalAPIException("Error: Received null for roles");
274         } else if (roles.iterator().hasNext()) {
275             EcompRole ecompRole = roles.iterator().next();
276             updatedRole = EcompRoleConverter.convertEcompRoleToRole(ecompRole);
277             log.debug("pushing role: {} to user: {}", updatedRole, loginId);
278             try {
279                 userBusinessLogic.updateUserRole(modifierAttId, loginId, updatedRole);
280             }
281             catch (Exception e) {
282                 log.debug("Error: Failed to update role");
283                 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, "Failed to update role", ErrorSeverity.INFO);
284                 throw new PortalAPIException("Failed to update role" + e);
285             }
286         } else {
287             log.debug("Error: No roles in List");
288             BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, FAILED_TO_FETCH_ROLES, ErrorSeverity.INFO);
289             //in this cases we want to deactivate the user
290             try {
291                 getUserBusinessLogicExt().deActivateUser(modifierAttId, loginId);
292             }
293             catch (Exception e) {
294                 log.debug("Error: Failed to deactivate user {}",loginId);
295                 BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER_ROLE, "Failed to deactivate user", ErrorSeverity.INFO);
296                 throw new PortalAPIException("Error: Failed to deactivate user" + e);
297             }
298         }
299     }
300
301     @Override
302     public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
303         try {
304             log.debug("Start handle request of ECOMP getUserRoles");
305             UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
306             User user = userBusinessLogic.getUser(loginId, false);
307             Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
308             if (ecompUser.isRight()) {
309                 log.debug("Error: Failed to convert Roles");
310                 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_CONVERT_ROLES, ErrorSeverity.ERROR);
311                 throw new PortalAPIException(ecompUser.right().value());
312             } else if (ecompUser.left().value() == null) {
313                 log.debug("Error: Failed to convert Roles");
314                 BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_CONVERT_ROLES, ErrorSeverity.ERROR);
315                 throw new PortalAPIException();
316             }
317             return new LinkedList<>(ecompUser.left().value().getRoles());
318         } catch (ComponentException ce) {
319             log.debug(ERROR_FAILED_TO_GET_ROLES);
320             BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_GET_ROLES, ErrorSeverity.INFO);
321             throw new PortalAPIException(FAILED_TO_GET_ROLES + ce.getActionStatus());
322         }
323         catch (Exception e) {
324             log.debug(ERROR_FAILED_TO_GET_ROLES);
325             BeEcompErrorManager.getInstance().logInvalidInputError(GET_USER_ROLES, FAILED_TO_GET_ROLES, ErrorSeverity.INFO);
326             throw new PortalAPIException(FAILED_TO_GET_ROLES, e);
327         }
328     }
329
330     @Override
331     public boolean isAppAuthenticated(HttpServletRequest request) {
332         final String portal_key = PortalApiProperties.getProperty("portal_pass");
333         final String portal_user = PortalApiProperties.getProperty("portal_user");
334         final String USERNAME = request.getHeader("username");
335         final String PASSWORD = request.getHeader("password");
336
337         if (USERNAME != null && PASSWORD != null) {
338             try {
339                 if (USERNAME.equals(CipherUtil.decryptPKC(portal_user)) &&
340                         PASSWORD.equals(CipherUtil.decryptPKC(portal_key))) {
341                     log.debug("User authenticated - Username: {}", USERNAME);
342                     return true;
343                 }
344             } catch (CipherUtilException e) {
345                 log.debug("User authentication failed - Decryption failed", e);
346                 return false;
347             }
348         }
349         log.debug("User authentication failed");
350         return false;
351     }
352
353     private UserBusinessLogic getUserBusinessLogic() {
354         ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
355         return (UserBusinessLogic) ctx.getBean("userBusinessLogic");
356     }
357
358     private UserBusinessLogicExt getUserBusinessLogicExt() {
359         ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
360         return (UserBusinessLogicExt) ctx.getBean("userBusinessLogicExt");
361     }
362
363     /**
364      * Gets and returns the userId for the logged-in user based on the request.
365      * If any error occurs, the method should throw PortalApiException with an
366      * appropriate message. The FW library will catch the exception and send an
367      * appropriate response to Portal.
368      * 
369       * As a guideline for AT&T specific implementation, see the sample apps
370      * repository
371      * https://codecloud.web.att.com/projects/EP_SDK/repos/ecomp_portal_sdk_third_party/
372      * for a sample implementation for on-boarded applications using EPSDK-FW.
373      * However, the app can always choose to have a custom implementation of
374      * this method. For Open-source implementation, for example, the app will
375      * have a totally different implementation for this method.
376      * 
377       * @param request
378      * @return true if the request contains appropriate credentials, else false.
379      * @throws PortalAPIException
380      *             If an unexpected error occurs while processing the request.
381      */
382     @Override
383     public String getUserId(HttpServletRequest request) throws PortalAPIException {
384         return request.getHeader(Constants.USER_ID_HEADER);
385     }
386
387     //TODO for what the following method stands for
388     @Override
389     public Map<String, String> getCredentials() throws PortalAPIException {
390         return null;
391     }
392 }