Merge "Add CLI deployment as part of portal"
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / ExternalAccessRolesServiceImpl.java
index 17d9ceb..cddd1c2 100644 (file)
@@ -24,6 +24,7 @@ import org.openecomp.portalapp.portal.domain.EPUser;
 import org.openecomp.portalapp.portal.domain.EPUserApp;
 import org.openecomp.portalapp.portal.domain.ExternalRoleDetails;
 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
+import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
 import org.openecomp.portalapp.portal.transport.BulkUploadRoleFunction;
 import org.openecomp.portalapp.portal.transport.BulkUploadUserRoles;
 import org.openecomp.portalapp.portal.transport.CentralApp;
@@ -52,6 +53,7 @@ import org.springframework.http.HttpMethod;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.client.HttpClientErrorException;
 import org.springframework.web.client.RestTemplate;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -444,15 +446,15 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                        List <EPRole> getRoleCreated = null;
                                        if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                                                List<EPRole> roleCreated =  dataAccessService.getList(EPRole.class,
-                                                               " where role_name = '" + addRoleInDB.getName() +"'", null, null);       
+                                                               " where role_name = '" + addRoleInDB.getName() +"' and app_id = "+ app.getId(), null, null);    
                                                EPRole epUpdateRole = roleCreated.get(0);
                                                epUpdateRole.setAppRoleId(epUpdateRole.getId());
                                                dataAccessService.saveDomainObject(epUpdateRole, null);
                                                getRoleCreated =  dataAccessService.getList(EPRole.class,
-                                                               " where role_name = '" + addRoleInDB.getName() +"'", null, null);       
+                                                               " where role_name = '" + addRoleInDB.getName() +"' and app_id = "+ app.getId() , null, null);   
                                        } else{
                                                getRoleCreated =  dataAccessService.getList(EPRole.class,
-                                                               " where role_name = '" + addRoleInDB.getName() +"'", null, null);       
+                                                               " where role_name = '" + addRoleInDB.getName() +"' and app_id is null", null, null);    
                                        }
                                // Add role in External Access system
                                boolean response = addNewRoleInExternalSystem(getRoleCreated, app);
@@ -611,7 +613,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                        if (((epApp.getId().equals(app.getId()))
                                                        && (!userApp.getRole().getId().equals(PortalConstants.ACCOUNT_ADMIN_ROLE_ID)))
                                                        || ((epApp.getId().equals(PortalConstants.PORTAL_APP_ID))
-                                                                       && (globalRole.startsWith("global_")))) {
+                                                                       && (globalRole.toLowerCase().startsWith("global_")))) {
                                                CentralUserApp cua = new CentralUserApp();
                                                cua.setUserId(null);
                                                CentralApp cenApp = new CentralApp(1L, epApp.getCreated(), epApp.getModified(),
@@ -633,7 +635,13 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                                                        roleFunc.getCode(), roleFunc.getName(), null, null);
                                                        roleFunctionSet.add(cenRoleFunc);
                                                }
-                                               CentralRole cenRole = new CentralRole(userApp.getRole().getAppRoleId(),
+                                               Long userRoleId = null;
+                                               if(globalRole.toLowerCase().startsWith("global_") && epApp.getId().equals(PortalConstants.PORTAL_APP_ID)){
+                                                       userRoleId = userApp.getRole().getId();
+                                               } else{
+                                                       userRoleId = userApp.getRole().getAppRoleId();
+                                               }
+                                               CentralRole cenRole = new CentralRole(userRoleId,
                                                                userApp.getRole().getCreated(), userApp.getRole().getModified(),
                                                                userApp.getRole().getCreatedId(), userApp.getRole().getModifiedId(),
                                                                userApp.getRole().getRowNum(), userApp.getRole().getName(),
@@ -750,7 +758,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
 
                } catch (Exception e) {
                        logger.error(EELFLoggerDelegate.errorLogger, "getRoleFunction failed", e);
-                       throw new Exception("getRoleFunction failed");
+                       throw new Exception("getRoleFunction failed", e);
                }
                return getRoleFuncList.get(0);
        }
@@ -791,6 +799,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                                + app.getNameSpace() + "." + checkType + "/" + roleFuncName + "/*",
                                HttpMethod.GET, getSinglePermEntity, String.class);
                if (getResponse.getStatusCode().value() != 200) {
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, getResponse.getStatusCode());
                        throw new Exception(getResponse.getBody());
                }
                logger.debug(EELFLoggerDelegate.debugLogger, "Connected to External Access system");
@@ -808,8 +817,11 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                        SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "perm",
                                        HttpMethod.POST, entity, String.class);
                        logger.debug(EELFLoggerDelegate.debugLogger, "Connected to External Access system");
+                       }catch(HttpClientErrorException e){
+                               logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to add function in external central auth system", e);
+                               EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
                        }catch(Exception e){
-                               logger.error(EELFLoggerDelegate.errorLogger, "Failed to add fucntion in external central auth system", e);
+                               logger.error(EELFLoggerDelegate.errorLogger, "Failed to add function in external central auth system", e);
                        }
                } else {
                        try{
@@ -824,8 +836,11 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                        SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "perm",
                                        HttpMethod.PUT, entity, String.class);
                        logger.debug(EELFLoggerDelegate.debugLogger, "Connected to External Access system");
-                       } catch(Exception e){
-                               logger.error(EELFLoggerDelegate.errorLogger, "Failed to add fucntion in external central auth system", e);
+                       }catch(HttpClientErrorException e){
+                               logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to add function in external central auth system", e);
+                               EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
+                       }catch(Exception e){
+                               logger.error(EELFLoggerDelegate.errorLogger, "Failed to add function in external central auth system", e);
 
                        }
                }
@@ -869,6 +884,9 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                template.exchange(
                                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "perm?force=true",
                                HttpMethod.DELETE, entity, String.class);
+               } catch(HttpClientErrorException e){
+                       logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to delete functions in External System", e);
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
                } catch(Exception e){
                        if(e.getMessage().equalsIgnoreCase("404 Not Found")){
                        logger.debug(EELFLoggerDelegate.debugLogger, " It seems like function is already deleted in external central auth system  but exists in local DB", e.getMessage());
@@ -926,6 +944,7 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                        + epRoleList.get(0).getName().replaceAll(" ", "_") + "\"}";
                        deleteResponse = deleteRoleInExternalSystem(deleteRoleKey);
                        if (deleteResponse.getStatusCode().value() != 200) {
+                               EPLogUtil.logExternalAuthAccessAlarm(logger, deleteResponse.getStatusCode());
                                throw new Exception("Failed to delete role in external access system!");
                        }
                        logger.debug(EELFLoggerDelegate.debugLogger, "about to commit the transaction");
@@ -1032,7 +1051,10 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                        transaction.commit();
                        logger.debug(EELFLoggerDelegate.debugLogger, "committed the transaction");
                        result = true;
-               } catch (Exception e) {
+               }catch(HttpClientErrorException e){
+                       logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to deleteRoleDependeciesRecord", e);
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
+               }catch (Exception e) {
                        EcompPortalUtils.rollbackTransaction(transaction,
                                        "deleteDependcyRoleRecord rollback, exception = " + e);
                        logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
@@ -1168,7 +1190,10 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                addRoleFunctionInExternalSystem(cenRoleFunc, app);
                                functionsAdded++;
                        }
-               } catch (Exception e) {
+               }catch(HttpClientErrorException e){
+                       logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - bulkUploadFunctions failed", e);
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
+               }catch (Exception e) {
                        logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadFunctions failed", e.getMessage(), e);
                }
                return functionsAdded;
@@ -1206,7 +1231,10 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                template.exchange(
                                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role",
                                HttpMethod.POST, entity, String.class);
-               } catch(Exception e){
+               }catch(HttpClientErrorException e){
+                       logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to addRoleInExternalSystem", e);
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
+               }catch(Exception e){
                        if (e.getMessage().equalsIgnoreCase("409 Conflict")) {
                                logger.error(EELFLoggerDelegate.errorLogger, "Role already exits but does not break functionality");
                        } else {
@@ -1232,6 +1260,9 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                                        }
                                }
                        }
+               } catch(HttpClientErrorException e){
+                       logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to bulkUploadRolesFunctions", e);
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
                } catch (Exception e) {
                        logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRolesFunctions failed", e);
                }
@@ -1542,6 +1573,9 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                        }
                
                logger.debug(EELFLoggerDelegate.debugLogger, "Finished SyncApplicationRolesWithEcompDB");
+               }catch(HttpClientErrorException e){
+                       logger.error(EELFLoggerDelegate.errorLogger, "Failed to SyncApplicationRolesWithEcompDB", e);
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
                }catch(Exception e){
                        logger.error(EELFLoggerDelegate.errorLogger, "Failed to SyncApplicationRolesWithEcompDB", e);
                }
@@ -1591,7 +1625,10 @@ public class ExternalAccessRolesServiceImpl implements ExternalAccessRolesServic
                template.exchange(
                                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "userRole",
                                HttpMethod.POST, entity, String.class);
-               } catch (Exception e) {
+               }catch(HttpClientErrorException e){
+                       logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to addUserRoleInExternalSystem", e);
+                       EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
+               }catch (Exception e) {
                        if (e.getMessage().equalsIgnoreCase("409 Conflict")) {
                                logger.error(EELFLoggerDelegate.errorLogger, "UserRole already exits but does not break functionality");
                        } else {