2 * ============LICENSE_START=======================================================
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
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 java.util.LinkedList;
24 import java.util.List;
26 import javax.servlet.http.HttpServletRequest;
28 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
29 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
30 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
31 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
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.common.api.Constants;
41 import org.openecomp.sdc.exception.ResponseFormat;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.web.context.ContextLoader;
47 import fj.data.Either;
50 * PortalAPIException(String message, Throwable cause);
52 public class EcompIntImpl implements IPortalRestAPIService {
53 private static Logger log = LoggerFactory.getLogger(EcompIntImpl.class.getName());
55 public EcompIntImpl() {
56 log.debug("EcompIntImpl Class Instantiated");
60 public void pushUser(EcompUser user) throws PortalAPIException {
61 log.debug("Start handle request of ECOMP pushUser");
64 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Recieved null for argument user", ErrorSeverity.INFO);
65 log.debug("Recieved null for argument user");
66 throw new PortalAPIException("Recieved null for argument user");
69 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
71 final String modifierAttId = "jh0003";
72 User modifier = new User();
73 modifier.setUserId(modifierAttId);
74 log.debug("modifier id is {}", modifierAttId);
76 Either<User, String> newASDCUser = EcompUserConverter.convertEcompUserToUser(user);
77 if (newASDCUser.isRight()) {
78 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Failed to convert user", ErrorSeverity.INFO);
79 log.debug("Failed to create user {}", user.toString());
80 throw new PortalAPIException("Failed to create user " + newASDCUser.right().value());
81 } else if (newASDCUser.left().value() == null) {
82 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "NULL pointer returned from user converter", ErrorSeverity.INFO);
83 log.debug("Failed to create user {}", user.toString());
84 throw new PortalAPIException("Failed to create user " + newASDCUser.right().value());
87 User convertedAsdcUser = newASDCUser.left().value();
88 Either<User, ResponseFormat> createUserResponse = userBusinessLogic.createUser(modifier, convertedAsdcUser);
90 // ALREADY EXIST ResponseFormat
91 final String ALREADY_EXISTS_RESPONSE_ID = "SVC4006";
93 if (createUserResponse.isRight()) {
94 if (!createUserResponse.right().value().getMessageId().equals(ALREADY_EXISTS_RESPONSE_ID)) {
95 log.debug("Failed to create user {}", user.toString());
96 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Failed to create user", ErrorSeverity.ERROR);
97 throw new PortalAPIException("Failed to create user" + createUserResponse.right());
99 log.debug("User already exist {}", user.toString());
101 log.debug("User created {}", user.toString());
102 } catch (Exception e) {
103 log.debug("Failed to create user {}", user, e);
104 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Failed to create user", ErrorSeverity.ERROR);
105 throw new PortalAPIException("Failed to create user", e);
113 * loginId - equals to userId
117 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
118 log.debug("Start handle request of ECOMP editUser");
122 log.debug("Recieved null for argument user");
123 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Recieved null for argument user", ErrorSeverity.INFO);
124 throw new PortalAPIException("Recieved null for argument user");
125 } else if (loginId == null) {
126 log.debug("Recieved null for argument loginId");
127 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Recieved null for argument loginId", ErrorSeverity.INFO);
128 throw new PortalAPIException("Recieved null for argument loginId");
131 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
133 if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
134 log.debug("loginId and user loginId not equal");
135 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "loginId and user loginId not equal", ErrorSeverity.INFO);
136 throw new PortalAPIException("loginId not equals to the user loginId field");
137 } else if (user.getLoginId() == null) {
138 user.setLoginId(loginId);
141 Either<User, String> asdcUser = EcompUserConverter.convertEcompUserToUser(user);
142 if (asdcUser.isRight()) {
143 log.debug("Failed to convert user");
144 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Failed to convert user", ErrorSeverity.INFO);
145 throw new PortalAPIException(asdcUser.right().value());
146 } else if (asdcUser.left().value() == null) {
147 log.debug("NULL pointer returned from user converter");
148 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "NULL pointer returned from user converter", ErrorSeverity.INFO);
149 throw new PortalAPIException("Failed to edit user");
152 Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser.left().value());
154 if (updateUserCredentialsResponse.isRight()) {
155 log.debug("Failed to updateUserCredentials");
156 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Failed to updateUserCredentials", ErrorSeverity.ERROR);
157 throw new PortalAPIException("Failed to edit user" + updateUserCredentialsResponse.right().value());
159 } catch (Exception e) {
160 log.debug("Failed to updateUserCredentials");
161 throw new PortalAPIException("Failed to edit user", e);
167 public EcompUser getUser(String loginId) throws PortalAPIException {
168 log.debug("Start handle request of ECOMP getUser");
172 if (loginId == null) {
173 log.debug("Recieved null for argument loginId");
174 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Recieved null for argument loginId", ErrorSeverity.INFO);
175 throw new PortalAPIException("Recieved null for argument loginId");
178 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
180 Either<User, ActionStatus> getUserResponse = userBusinessLogic.getUser(loginId, false);
182 if (getUserResponse.isRight()) {
183 log.debug("Failed to get User");
184 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Failed to get User", ErrorSeverity.INFO);
185 throw new PortalAPIException("Failed to get User" + getUserResponse.right());
187 if (getUserResponse.left().value() != null) {
188 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(getUserResponse.left().value());
189 if (ecompUser.isLeft() && ecompUser.left().value() != null) {
190 return ecompUser.left().value();
192 log.debug("Failed to get User");
193 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Failed to get User", ErrorSeverity.INFO);
194 throw new PortalAPIException(ecompUser.right().value());
197 log.debug("Failed to get User");
198 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Failed to get User", ErrorSeverity.INFO);
199 throw new PortalAPIException("Failed to get User" + getUserResponse.right());
202 } catch (Exception e) {
203 log.debug("Failed to get User");
204 throw new PortalAPIException("Failed to get User", e);
209 public List<EcompUser> getUsers() throws PortalAPIException {
210 log.debug("Start handle request of ECOMP getUsers");
213 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
215 final String modifierAttId = "jh0003";
217 Either<List<User>, ResponseFormat> getUsersResponse = userBusinessLogic.getUsersList(modifierAttId, null, null);
219 if (getUsersResponse.isRight()) {
220 log.debug("Failed to get Users");
221 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to get users", ErrorSeverity.INFO);
222 throw new PortalAPIException("Failed to get Users" + getUsersResponse.right());
224 if (getUsersResponse.left().value() != null) {
225 List<EcompUser> ecompUserList = new LinkedList<>();
226 for (User user : getUsersResponse.left().value()) {
227 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
228 if (ecompUser.isRight()) {
229 log.debug("Failed to convert User {}", user);
230 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
232 } else if (ecompUser.left().value() == null) {
233 log.debug("Failed to convert User {}", user);
234 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
237 ecompUserList.add(ecompUser.left().value());
239 return ecompUserList;
241 log.debug("Failed to get users");
242 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to get users", ErrorSeverity.INFO);
243 throw new PortalAPIException("Failed to get Users" + getUsersResponse.right());
246 } catch (Exception e) {
247 log.debug("Failed to get users");
248 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to get users", ErrorSeverity.INFO);
249 throw new PortalAPIException("Failed to get Users", e);
254 public List<EcompRole> getAvailableRoles() throws PortalAPIException {
255 log.debug("Start handle request of ECOMP getAvailableRoles");
257 List<EcompRole> ecompRolesList = new LinkedList<>();
258 for (Role role : Role.values()) {
259 EcompRole ecompRole = new EcompRole();
260 ecompRole.setId(new Long(role.ordinal()));
261 ecompRole.setName(role.name());
262 ecompRolesList.add(ecompRole);
265 if (ecompRolesList.isEmpty()) {
266 throw new PortalAPIException();
269 return ecompRolesList;
270 } catch (Exception e) {
271 log.debug("Failed to fetch roles");
272 BeEcompErrorManager.getInstance().logInvalidInputError("GetAvailableRoles", "Failed to fetch roles", ErrorSeverity.INFO);
273 throw new PortalAPIException("Roles fetching failed", e);
279 * The user role updated through this method only
282 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
283 log.debug("Start handle request of ECOMP pushUserRole");
285 final String modifierAttId = "jh0003";
286 User modifier = new User();
287 modifier.setUserId(modifierAttId);
288 log.debug("modifier id is {}", modifierAttId);
290 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
292 String updatedRole = null;
295 throw new PortalAPIException("Error: Recieved null for roles");
296 } else if (roles.iterator().hasNext()) {
297 EcompRole ecompRole = roles.iterator().next();
298 updatedRole = EcompRoleConverter.convertEcompRoleToRole(ecompRole);
299 log.debug("pushing role: {} to user: {}", updatedRole, loginId);
300 Either<User, ResponseFormat> updateUserRoleResponse = userBusinessLogic.updateUserRole(modifier, loginId, updatedRole);
301 if (updateUserRoleResponse.isRight()) {
302 log.debug("Error: Failed to update role");
303 BeEcompErrorManager.getInstance().logInvalidInputError("PushUserRole", "Failed to update role", ErrorSeverity.INFO);
304 throw new PortalAPIException("Failed to update role" + updateUserRoleResponse.right().value().toString());
307 log.debug("Error: No roles in List");
308 BeEcompErrorManager.getInstance().logInvalidInputError("PushUserRole", "Failed to fetch roles", ErrorSeverity.INFO);
309 //throw new PortalAPIException("Error: No roles in List");
310 //in this cases we want to deactivate the user
311 Either<User, ResponseFormat> deActivateUserResponse = userBusinessLogic.deActivateUser(modifier, loginId);
312 if (deActivateUserResponse.isRight()) {
313 log.debug("Error: Failed to deactivate user {}",loginId);
314 BeEcompErrorManager.getInstance().logInvalidInputError("PushUserRole", "Failed to deactivate user", ErrorSeverity.INFO);
315 throw new PortalAPIException(deActivateUserResponse.right().value().getFormattedMessage());
321 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
323 log.debug("Start handle request of ECOMP getUserRoles");
325 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
327 Either<User, ActionStatus> getUserResponse = userBusinessLogic.getUser(loginId, false);
329 if (getUserResponse.isRight()) {
330 log.debug("Error: Failed to get Roles");
331 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to get Roles", ErrorSeverity.INFO);
332 throw new PortalAPIException("Failed to get Roles" + getUserResponse.right());
334 if (getUserResponse.left().value() != null) {
335 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(getUserResponse.left().value());
336 if (ecompUser.isRight()) {
337 log.debug("Error: Failed to convert Roles");
338 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to convert Roles", ErrorSeverity.ERROR);
339 throw new PortalAPIException(ecompUser.right().value());
340 } else if (ecompUser.left().value() == null) {
341 log.debug("Error: Failed to convert Roles");
342 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to convert Roles", ErrorSeverity.ERROR);
343 throw new PortalAPIException();
346 return new LinkedList<>(ecompUser.left().value().getRoles());
348 log.debug("Error: Failed to get Roles");
349 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to get Roles", ErrorSeverity.ERROR);
350 throw new PortalAPIException("Failed to get Roles" + getUserResponse.right());
353 } catch (Exception e) {
354 log.debug("Error: Failed to get Roles");
355 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to get Roles", ErrorSeverity.INFO);
356 throw new PortalAPIException("Failed to get Roles", e);
361 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
362 // TODO Validation should be changed completely
363 final String USERNAME = request.getHeader("username");
364 final String PASSWORD = request.getHeader("password");
366 if (USERNAME != null && PASSWORD != null) {
367 if (!USERNAME.equals("") && !PASSWORD.equals("")) {
368 log.debug("User authenticated - Username: ,Password: {}", USERNAME, PASSWORD);
373 log.debug("User authentication failed");
377 private UserBusinessLogic getUserBusinessLogic() {
378 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
379 UserBusinessLogic userBusinessLogic = (UserBusinessLogic) ctx.getBean("userBusinessLogic");
380 return userBusinessLogic;
384 * Gets and returns the userId for the logged-in user based on the request.
385 * If any error occurs, the method should throw PortalApiException with an
386 * appropriate message. The FW library will catch the exception and send an
387 * appropriate response to Portal.
389 * As a guideline for AT&T specific implementation, see the sample apps
391 * https://codecloud.web.att.com/projects/EP_SDK/repos/ecomp_portal_sdk_third_party/
392 * for a sample implementation for on-boarded applications using EPSDK-FW.
393 * However, the app can always choose to have a custom implementation of
394 * this method. For Open-source implementation, for example, the app will
395 * have a totally different implementation for this method.
398 * @return true if the request contains appropriate credentials, else false.
399 * @throws PortalAPIException
400 * If an unexpected error occurs while processing the request.
403 public String getUserId(HttpServletRequest request) throws PortalAPIException {
404 return request.getHeader(Constants.USER_ID_HEADER);