Merge branch 'master' into 'master'

Fix app and app-release deleting issues

Closes product-iots#526

See merge request entgra/carbon-device-mgt!555
reporting
Saad Sahibjan 5 years ago
commit f1effbdde6

@ -24,7 +24,7 @@ import java.util.List;
/** /**
* This interface provides the list of operations that are performed in the database layer with respect to the * This interface provides the list of operations that are performed in the database layer with respect to the
* visibility. * visibility.
* *
*/ */
public interface VisibilityDAO { public interface VisibilityDAO {
@ -60,4 +60,14 @@ public interface VisibilityDAO {
*/ */
void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId) void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId)
throws VisibilityManagementDAOException; throws VisibilityManagementDAOException;
/**
* This method is responsible to delete all application unrestricted roles
*
* @param applicationId Application Id
* @param tenantId Tenant Id
* @throws VisibilityManagementDAOException if error occurred while deleting application unrestricted roles
*/
void deleteAppUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException;
} }

@ -146,4 +146,34 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
throw new VisibilityManagementDAOException(msg, e); throw new VisibilityManagementDAOException(msg, e);
} }
} }
@Override
public void deleteAppUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Application unrestricted roles of application which "
+ "has application Id " + applicationId);
}
String sql = "DELETE "
+ "FROM AP_UNRESTRICTED_ROLE "
+ "WHERE AP_APP_ID = ? AND "
+ "TENANT_ID = ?";
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId);
stmt.executeUpdate();
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to delete application unrestricted roles "
+ "which has application Id " + applicationId;
log.error(msg, e);
throw new VisibilityManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing query to delete application unrestricted roles which has"
+ " application Id " + applicationId + ". executed query: " + sql;
log.error(msg, e);
throw new VisibilityManagementDAOException(msg, e);
}
}
} }

@ -1405,30 +1405,34 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ applicationId); + applicationId);
} }
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
ApplicationDTO applicationDTO = getApplication(applicationId); ApplicationDTO applicationDTO = getApplication(applicationId);
List<ApplicationReleaseDTO> applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs(); deleteApplication(applicationDTO, tenantId);
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { }
if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())){
String msg = "Application release which has application release UUID: " +
applicationReleaseDTO.getUuid() + " is not in a deletable state. Therefore Application "
+ "deletion is not permitted. In order to delete the application, all application releases "
+ "of the application has to be in a deletable state.";
log.error(msg);
throw new ForbiddenException(msg);
}
}
/**
* Delete the entire app data and the each app release data and artifacts.
*
* @param applicationDTO ApplicationDTO object
* @param tenantId Tenant Id
* @throws ApplicationManagementException if error occurred while deleting application data or app relase data.
*/
private void deleteApplication(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementException {
List<Integer> deletingAppReleaseIds = new ArrayList<>();
List<String> deletingAppHashVals = new ArrayList<>();
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
List<Integer> deletingAppReleaseIds = new ArrayList<>(); for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) {
List<String> deletingAppHashVals = new ArrayList<>(); if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())){
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { String msg = "Application release which has application release UUID: " +
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO applicationReleaseDTO.getUuid() + " is not in a deletable state. Therefore Application "
.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); + "deletion is not permitted. In order to delete the application, all application releases "
if (!deviceSubscriptionDTOS.isEmpty()){ + "of the application has to be in a deletable state.";
String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + log.error(msg);
" either subscribed to device/s or it had subscribed to device/s. Therefore you are not " throw new ForbiddenException(msg);
}
if (!subscriptionDAO.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId).isEmpty()) {
String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid()
+ " either subscribed to device/s or it had subscribed to device/s. Therefore you are not "
+ "permitted to delete the application release."; + "permitted to delete the application release.";
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
@ -1436,37 +1440,38 @@ public class ApplicationManagerImpl implements ApplicationManager {
deletingAppHashVals.add(applicationReleaseDTO.getAppHashValue()); deletingAppHashVals.add(applicationReleaseDTO.getAppHashValue());
deletingAppReleaseIds.add(applicationReleaseDTO.getId()); deletingAppReleaseIds.add(applicationReleaseDTO.getId());
} }
applicationStorageManager.deleteAllApplicationReleaseArtifacts(deletingAppHashVals, tenantId);
this.lifecycleStateDAO.deleteLifecycleStates(deletingAppReleaseIds); this.lifecycleStateDAO.deleteLifecycleStates(deletingAppReleaseIds);
this.applicationReleaseDAO.deleteReleases(deletingAppReleaseIds); this.applicationReleaseDAO.deleteReleases(deletingAppReleaseIds);
this.applicationDAO.deleteApplicationTags(applicationId, tenantId); this.applicationDAO.deleteApplicationTags(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteAppCategories(applicationId, tenantId); this.applicationDAO.deleteAppCategories(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteApplication(applicationId, tenantId); this.visibilityDAO.deleteAppUnrestrictedRoles(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteApplication(applicationDTO.getId(), tenantId);
APIUtil.getApplicationStorageManager().deleteAllApplicationReleaseArtifacts(deletingAppHashVals, tenantId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while observing the database connection to delete application which has " String msg = "Error occurred while observing the database connection to delete application which has "
+ "application ID: " + applicationId; + "application ID: " + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Database access error is occurred when deleting application which has application ID: " String msg = "Database access error is occurred when deleting application which has application ID: "
+ applicationId; + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when getting application data for application id: " + applicationId; String msg = "Error occurred when getting application data for application id: " + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationStorageManagementException e) { } catch (ApplicationStorageManagementException e) {
String msg = "Error occurred when deleting application artifacts in the file system. Application id: " String msg = "Error occurred when deleting application artifacts in the file system. Application id: "
+ applicationId; + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occured while deleting life-cycle state data of application releases of the application" String msg = "Error occured while deleting life-cycle state data of application releases of the application"
+ " which has application ID: " + applicationId; + " which has application ID: " + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} finally { } finally {
@ -1521,22 +1526,34 @@ public class ApplicationManagerImpl implements ApplicationManager {
public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException { public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
AtomicBoolean isDeletingApp = new AtomicBoolean(false); ApplicationDTO applicationDTO;
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.openDBConnection();
ApplicationDTO applicationDTO = this.applicationDAO.getApplication(releaseUuid, tenantId); applicationDTO = this.applicationDAO.getApplication(releaseUuid, tenantId);
if (applicationDTO == null) { if (applicationDTO == null) {
String msg = "Couldn't find an application which has application release UUID: " + releaseUuid; String msg = "Couldn't find an application which has application release UUID: " + releaseUuid;
log.error(msg); log.error(msg);
throw new NotFoundException(msg); throw new NotFoundException(msg);
} }
} catch (DBConnectionException e) {
String msg = "Error occurred while observing the database connection to get application which has "
+ "application release of UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg =
"Error occurred when getting application data which has application release UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
List<ApplicationReleaseDTO> applicationReleaseDTOS = applicationDTO.getApplicationReleaseDTOs(); if (applicationDTO.getApplicationReleaseDTOs().size() == 1) {
if (applicationReleaseDTOS.size() == 1) { deleteApplication(applicationDTO, tenantId);
isDeletingApp.set(true); } else {
} for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) {
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOS) {
if (releaseUuid.equals(applicationReleaseDTO.getUuid())) { if (releaseUuid.equals(applicationReleaseDTO.getUuid())) {
if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) { if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) {
String msg = String msg =
@ -1545,55 +1562,55 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
} }
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO try {
.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); ConnectionManagerUtil.beginDBTransaction();
if (!deviceSubscriptionDTOS.isEmpty()) { List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId);
+ " either subscribed to device/s or it had subscribed to device/s. Therefore you are not " if (!deviceSubscriptionDTOS.isEmpty()) {
+ "permitted to delete the application release."; String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid()
log.error(msg); + " either subscribed to device/s or it had subscribed to device/s. Therefore you "
throw new ForbiddenException(msg); + "are not permitted to delete the application release.";
log.error(msg);
throw new ForbiddenException(msg);
}
lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId());
applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId());
applicationStorageManager.deleteAllApplicationReleaseArtifacts(
Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId);
ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException e) {
String msg = "Error occurred while observing the database connection to delete application "
+ "release which has the UUID:" + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (TransactionManagementException e) {
String msg = "Database access error is occurred when deleting application release which has "
+ "the UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred while verifying whether application relase has an subscription or "
+ "not. Application release UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationStorageManagementException e) {
String msg = "Error occurred when deleting the application release artifact from the file "
+ "system. Application release UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when deleting lifecycle data for application release UUID: "
+ releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
applicationStorageManager.deleteAllApplicationReleaseArtifacts(
Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId);
lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId());
applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId());
break; break;
} }
} }
if (isDeletingApp.get()) {
this.applicationDAO.deleteApplicationTags(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteAppCategories(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteApplication(applicationDTO.getId(), tenantId);
}
ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException e) {
String msg = "Error occurred while observing the database connection to delete application release which "
+ "has UUID:" + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (TransactionManagementException e) {
String msg = "Database access error is occurred when deleting application release which has UUID: "
+ releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when application release data for application release UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationStorageManagementException e) {
String msg = "Error occurred when deleting the application release artifact from the file system. "
+ "Application release UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when dleting lifecycle data for application release UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }

Loading…
Cancel
Save