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 fj.data.Either;
24 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
25 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
26 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
27 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.ecomp.converters.EcompRoleConverter;
32 import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.user.Role;
35 import org.openecomp.sdc.be.user.UserBusinessLogic;
36 import org.openecomp.sdc.common.api.Constants;
37 import org.openecomp.sdc.exception.ResponseFormat;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.web.context.ContextLoader;
43 import javax.servlet.http.HttpServletRequest;
44 import java.util.LinkedList;
45 import java.util.List;
48 public class EcompIntImpl implements IPortalRestAPIService {
49 private static final Logger log = LoggerFactory.getLogger(EcompIntImpl.class);
51 public EcompIntImpl() {
52 log.debug("EcompIntImpl Class Instantiated");
56 public void pushUser(EcompUser user) throws PortalAPIException {
57 log.debug("Start handle request of ECOMP pushUser");
60 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Recieved null for argument user", ErrorSeverity.INFO);
61 log.debug("Recieved null for argument user");
62 throw new PortalAPIException("Recieved null for argument user");
65 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
67 final String modifierAttId = "jh0003";
68 User modifier = new User();
69 modifier.setUserId(modifierAttId);
70 log.debug("modifier id is {}", modifierAttId);
72 Either<User, String> newASDCUser = EcompUserConverter.convertEcompUserToUser(user);
73 if (newASDCUser.isRight()) {
74 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Failed to convert user", ErrorSeverity.INFO);
75 log.debug("Failed to create user {}", user);
76 throw new PortalAPIException("Failed to create user " + newASDCUser.right().value());
77 } else if (newASDCUser.left().value() == null) {
78 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "NULL pointer returned from user converter", ErrorSeverity.INFO);
79 log.debug("Failed to create user {}", user);
80 throw new PortalAPIException("Failed to create user " + newASDCUser.right().value());
83 User convertedAsdcUser = newASDCUser.left().value();
84 Either<User, ResponseFormat> createUserResponse = userBusinessLogic.createUser(modifier, convertedAsdcUser);
86 // ALREADY EXIST ResponseFormat
87 final String ALREADY_EXISTS_RESPONSE_ID = "SVC4006";
89 if (createUserResponse.isRight()) {
90 if (!createUserResponse.right().value().getMessageId().equals(ALREADY_EXISTS_RESPONSE_ID)) {
91 log.debug("Failed to create user {}", user);
92 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Failed to create user", ErrorSeverity.ERROR);
93 throw new PortalAPIException("Failed to create user" + createUserResponse.right());
95 log.debug("User already exist {}", user);
97 log.debug("User created {}", user);
98 } catch (Exception e) {
99 log.debug("Failed to create user {}", user, e);
100 BeEcompErrorManager.getInstance().logInvalidInputError("PushUser", "Failed to create user", ErrorSeverity.ERROR);
101 throw new PortalAPIException("Failed to create user", e);
109 * loginId - equals to userId
113 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
114 log.debug("Start handle request of ECOMP editUser");
118 log.debug("Recieved null for argument user");
119 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Recieved null for argument user", ErrorSeverity.INFO);
120 throw new PortalAPIException("Recieved null for argument user");
121 } else if (loginId == null) {
122 log.debug("Recieved null for argument loginId");
123 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Recieved null for argument loginId", ErrorSeverity.INFO);
124 throw new PortalAPIException("Recieved null for argument loginId");
127 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
129 if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
130 log.debug("loginId and user loginId not equal");
131 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "loginId and user loginId not equal", ErrorSeverity.INFO);
132 throw new PortalAPIException("loginId not equals to the user loginId field");
133 } else if (user.getLoginId() == null) {
134 user.setLoginId(loginId);
137 Either<User, String> asdcUser = EcompUserConverter.convertEcompUserToUser(user);
138 if (asdcUser.isRight()) {
139 log.debug("Failed to convert user");
140 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Failed to convert user", ErrorSeverity.INFO);
141 throw new PortalAPIException(asdcUser.right().value());
142 } else if (asdcUser.left().value() == null) {
143 log.debug("NULL pointer returned from user converter");
144 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "NULL pointer returned from user converter", ErrorSeverity.INFO);
145 throw new PortalAPIException("Failed to edit user");
148 Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser.left().value());
150 if (updateUserCredentialsResponse.isRight()) {
151 log.debug("Failed to updateUserCredentials");
152 BeEcompErrorManager.getInstance().logInvalidInputError("EditUser", "Failed to updateUserCredentials", ErrorSeverity.ERROR);
153 throw new PortalAPIException("Failed to edit user" + updateUserCredentialsResponse.right().value());
155 } catch (Exception e) {
156 log.debug("Failed to updateUserCredentials");
157 throw new PortalAPIException("Failed to edit user", e);
163 public EcompUser getUser(String loginId) throws PortalAPIException {
164 log.debug("Start handle request of ECOMP getUser");
168 if (loginId == null) {
169 log.debug("Recieved null for argument loginId");
170 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Recieved null for argument loginId", ErrorSeverity.INFO);
171 throw new PortalAPIException("Recieved null for argument loginId");
174 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
176 Either<User, ActionStatus> getUserResponse = userBusinessLogic.getUser(loginId, false);
178 if (getUserResponse.isRight()) {
179 log.debug("Failed to get User");
180 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Failed to get User", ErrorSeverity.INFO);
181 throw new PortalAPIException("Failed to get User" + getUserResponse.right());
183 if (getUserResponse.left().value() != null) {
184 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(getUserResponse.left().value());
185 if (ecompUser.isLeft() && ecompUser.left().value() != null) {
186 return ecompUser.left().value();
188 log.debug("Failed to get User");
189 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Failed to get User", ErrorSeverity.INFO);
190 throw new PortalAPIException(ecompUser.right().value());
193 log.debug("Failed to get User");
194 BeEcompErrorManager.getInstance().logInvalidInputError("GetUser", "Failed to get User", ErrorSeverity.INFO);
195 throw new PortalAPIException("Failed to get User" + getUserResponse.right());
198 } catch (Exception e) {
199 log.debug("Failed to get User");
200 throw new PortalAPIException("Failed to get User", e);
205 public List<EcompUser> getUsers() throws PortalAPIException {
206 log.debug("Start handle request of ECOMP getUsers");
209 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
211 final String modifierAttId = "jh0003";
213 Either<List<User>, ResponseFormat> getUsersResponse = userBusinessLogic.getUsersList(modifierAttId, null, null);
215 if (getUsersResponse.isRight()) {
216 log.debug("Failed to get Users");
217 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to get users", ErrorSeverity.INFO);
218 throw new PortalAPIException("Failed to get Users" + getUsersResponse.right());
220 if (getUsersResponse.left().value() != null) {
221 List<EcompUser> ecompUserList = new LinkedList<>();
222 for (User user : getUsersResponse.left().value()) {
223 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(user);
224 if (ecompUser.isRight()) {
225 log.debug("Failed to convert User {}", user);
226 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
228 } else if (ecompUser.left().value() == null) {
229 log.debug("Failed to convert User {}", user);
230 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to convert User" + user.toString(), ErrorSeverity.WARNING);
233 ecompUserList.add(ecompUser.left().value());
235 return ecompUserList;
237 log.debug("Failed to get users");
238 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to get users", ErrorSeverity.INFO);
239 throw new PortalAPIException("Failed to get Users" + getUsersResponse.right());
242 } catch (Exception e) {
243 log.debug("Failed to get users");
244 BeEcompErrorManager.getInstance().logInvalidInputError("GetUsers", "Failed to get users", ErrorSeverity.INFO);
245 throw new PortalAPIException("Failed to get Users", e);
250 public List<EcompRole> getAvailableRoles() throws PortalAPIException {
251 log.debug("Start handle request of ECOMP getAvailableRoles");
253 List<EcompRole> ecompRolesList = new LinkedList<>();
254 for (Role role : Role.values()) {
255 EcompRole ecompRole = new EcompRole();
256 ecompRole.setId(new Long(role.ordinal()));
257 ecompRole.setName(role.name());
258 ecompRolesList.add(ecompRole);
261 if (ecompRolesList.isEmpty()) {
262 throw new PortalAPIException();
265 return ecompRolesList;
266 } catch (Exception e) {
267 log.debug("Failed to fetch roles");
268 BeEcompErrorManager.getInstance().logInvalidInputError("GetAvailableRoles", "Failed to fetch roles", ErrorSeverity.INFO);
269 throw new PortalAPIException("Roles fetching failed", e);
275 * The user role updated through this method only
278 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
279 log.debug("Start handle request of ECOMP pushUserRole");
281 final String modifierAttId = "jh0003";
282 User modifier = new User();
283 modifier.setUserId(modifierAttId);
284 log.debug("modifier id is {}", modifierAttId);
286 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
288 String updatedRole = null;
291 throw new PortalAPIException("Error: Recieved null for roles");
292 } else if (roles.iterator().hasNext()) {
293 EcompRole ecompRole = roles.iterator().next();
294 updatedRole = EcompRoleConverter.convertEcompRoleToRole(ecompRole);
295 log.debug("pushing role: {} to user: {}", updatedRole, loginId);
296 Either<User, ResponseFormat> updateUserRoleResponse = userBusinessLogic.updateUserRole(modifier, loginId, updatedRole);
297 if (updateUserRoleResponse.isRight()) {
298 log.debug("Error: Failed to update role");
299 BeEcompErrorManager.getInstance().logInvalidInputError("PushUserRole", "Failed to update role", ErrorSeverity.INFO);
300 throw new PortalAPIException("Failed to update role" + updateUserRoleResponse.right().value().toString());
303 log.debug("Error: No roles in List");
304 BeEcompErrorManager.getInstance().logInvalidInputError("PushUserRole", "Failed to fetch roles", ErrorSeverity.INFO);
305 //in this cases we want to deactivate the user
306 Either<User, ResponseFormat> deActivateUserResponse = userBusinessLogic.deActivateUser(modifier, loginId);
307 if (deActivateUserResponse.isRight()) {
308 log.debug("Error: Failed to deactivate user {}",loginId);
309 BeEcompErrorManager.getInstance().logInvalidInputError("PushUserRole", "Failed to deactivate user", ErrorSeverity.INFO);
310 throw new PortalAPIException(deActivateUserResponse.right().value().getFormattedMessage());
316 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
318 log.debug("Start handle request of ECOMP getUserRoles");
320 UserBusinessLogic userBusinessLogic = getUserBusinessLogic();
322 Either<User, ActionStatus> getUserResponse = userBusinessLogic.getUser(loginId, false);
324 if (getUserResponse.isRight()) {
325 log.debug("Error: Failed to get Roles");
326 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to get Roles", ErrorSeverity.INFO);
327 throw new PortalAPIException("Failed to get Roles" + getUserResponse.right());
329 if (getUserResponse.left().value() != null) {
330 Either<EcompUser, String> ecompUser = EcompUserConverter.convertUserToEcompUser(getUserResponse.left().value());
331 if (ecompUser.isRight()) {
332 log.debug("Error: Failed to convert Roles");
333 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to convert Roles", ErrorSeverity.ERROR);
334 throw new PortalAPIException(ecompUser.right().value());
335 } else if (ecompUser.left().value() == null) {
336 log.debug("Error: Failed to convert Roles");
337 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to convert Roles", ErrorSeverity.ERROR);
338 throw new PortalAPIException();
341 return new LinkedList<>(ecompUser.left().value().getRoles());
343 log.debug("Error: Failed to get Roles");
344 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to get Roles", ErrorSeverity.ERROR);
345 throw new PortalAPIException("Failed to get Roles" + getUserResponse.right());
348 } catch (Exception e) {
349 log.debug("Error: Failed to get Roles");
350 BeEcompErrorManager.getInstance().logInvalidInputError("GetUserRoles", "Failed to get Roles", ErrorSeverity.INFO);
351 throw new PortalAPIException("Failed to get Roles", e);
356 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
357 // TODO Validation should be changed completely
358 final String USERNAME = request.getHeader("username");
359 final String PASSWORD = request.getHeader("password");
361 if (USERNAME != null && PASSWORD != null) {
362 if (!USERNAME.equals("") && !PASSWORD.equals("")) {
363 log.debug("User authenticated - Username: ,Password: {}", USERNAME, PASSWORD);
368 log.debug("User authentication failed");
372 private UserBusinessLogic getUserBusinessLogic() {
373 ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
374 UserBusinessLogic userBusinessLogic = (UserBusinessLogic) ctx.getBean("userBusinessLogic");
375 return userBusinessLogic;
379 * Gets and returns the userId for the logged-in user based on the request.
380 * If any error occurs, the method should throw PortalApiException with an
381 * appropriate message. The FW library will catch the exception and send an
382 * appropriate response to Portal.
384 * As a guideline for AT&T specific implementation, see the sample apps
386 * https://codecloud.web.att.com/projects/EP_SDK/repos/ecomp_portal_sdk_third_party/
387 * for a sample implementation for on-boarded applications using EPSDK-FW.
388 * However, the app can always choose to have a custom implementation of
389 * this method. For Open-source implementation, for example, the app will
390 * have a totally different implementation for this method.
393 * @return true if the request contains appropriate credentials, else false.
394 * @throws PortalAPIException
395 * If an unexpected error occurs while processing the request.
398 public String getUserId(HttpServletRequest request) throws PortalAPIException {
399 return request.getHeader(Constants.USER_ID_HEADER);