From 159f603605bfc70174419d7453d6eb40fb1352e8 Mon Sep 17 00:00:00 2001 From: ashvini Date: Mon, 8 Apr 2024 18:07:31 +0530 Subject: [PATCH] Delete tenant data Refactor code Add begin transaction Refactor code --- .../common/services/ApplicationManager.java | 1 + .../pom.xml | 5 + .../mgt/core/impl/ApplicationManagerImpl.java | 81 ++++++++++++++++ .../admin/UserManagementAdminServiceImpl.java | 15 ++- .../DeviceManagementProviderService.java | 1 + .../DeviceManagementProviderServiceImpl.java | 94 ++++++++++++++++++- .../mgt/core/impl/TenantManagerImpl.java | 9 +- 7 files changed, 199 insertions(+), 7 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java index be048514023..4a1be2ea1e7 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java @@ -546,4 +546,5 @@ public interface ApplicationManager { * @throws ApplicationManagementException thrown if an error occurs when deleting data */ void deleteApplicationDataOfTenant(int tenantId) throws ApplicationManagementException; + void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml index dc417eb2553..87e6688f13a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml @@ -384,6 +384,11 @@ com.squareup.okio okio + + org.wso2.carbon.multitenancy + org.wso2.carbon.tenant.mgt + compile + diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java index cfa8404375e..ef852f5ff42 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java @@ -96,6 +96,8 @@ import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementEx import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; +import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; @@ -4427,15 +4429,18 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Database access error is occurred when getting applications for tenant with ID: " + tenantId; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (LifeCycleManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while deleting life-cycle state data of application releases of the tenant" + " of ID: " + tenantId ; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (ReviewManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while deleting reviews of application releases of the applications" + " of tenant ID: " + tenantId ; log.error(msg, e); @@ -4450,4 +4455,80 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.closeDBConnection(); } } + + @Override + public void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException { + int tenantId; + try{ + TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); + TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); + tenantId = tenantInfoBean.getTenantId(); + + } catch (Exception e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + + try { + ConnectionManagerUtil.beginDBTransaction(); + + vppApplicationDAO.deleteAssociationByTenant(tenantId); + vppApplicationDAO.deleteVppUserByTenant(tenantId); + vppApplicationDAO.deleteAssetsByTenant(tenantId); + reviewDAO.deleteReviewsByTenant(tenantId); + subscriptionDAO.deleteOperationMappingByTenant(tenantId); + subscriptionDAO.deleteDeviceSubscriptionByTenant(tenantId); + subscriptionDAO.deleteGroupSubscriptionByTenant(tenantId); + subscriptionDAO.deleteRoleSubscriptionByTenant(tenantId); + subscriptionDAO.deleteUserSubscriptionByTenant(tenantId); + applicationDAO.deleteAppFavouritesByTenant(tenantId); + applicationDAO.deleteApplicationTagsMappingByTenant(tenantId); + applicationDAO.deleteApplicationTagsByTenant(tenantId); + applicationDAO.deleteApplicationCategoryMappingByTenant(tenantId); + applicationDAO.deleteApplicationCategoriesByTenant(tenantId); + subscriptionDAO.deleteScheduledSubscriptionByTenant(tenantId); + lifecycleStateDAO.deleteAppLifecycleStatesByTenant(tenantId); + applicationReleaseDAO.deleteReleasesByTenant(tenantId); + visibilityDAO.deleteAppUnrestrictedRolesByTenant(tenantId); + spApplicationDAO.deleteSPApplicationMappingByTenant(tenantId); + spApplicationDAO.deleteIdentityServerByTenant(tenantId); + applicationDAO.deleteApplicationsByTenant(tenantId); + APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); + + ConnectionManagerUtil.commitDBTransaction(); + } catch (DBConnectionException e) { + String msg = "Error occurred while observing the database connection to delete applications for tenant with ID: " + + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Database access error is occurred when getting applications for tenant with ID: " + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (LifeCycleManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while deleting life-cycle state data of application releases of the tenant" + + " of ID: " + tenantId ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ReviewManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while deleting reviews of application releases of the applications" + + " of tenant ID: " + tenantId ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationStorageManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while deleting App folder of tenant" + + " of tenant ID: " + tenantId ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index 296bdf5060c..d4f34d8e4ab 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.admin; +import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -93,11 +94,13 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic public Response deleteTenantByDomain(@PathParam("tenantDomain") String tenantDomain) { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - if(tenantId != MultitenantConstants.SUPER_TENANT_ID){ + if (tenantId != MultitenantConstants.SUPER_TENANT_ID){ String msg = "Only super tenants are allowed to delete tenants"; log.error(msg); return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); - }else{ + } else { + DeviceMgtAPIUtils.getApplicationManager().deleteApplicationDataByTenantDomain(tenantDomain); + DeviceMgtAPIUtils.getDeviceManagementService().deleteDeviceDataByTenantDomain(tenantDomain); TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); tenantMgtAdminService.deleteTenant(tenantDomain); String msg = "Tenant Deletion process has been initiated for tenant:" + tenantDomain; @@ -108,6 +111,14 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic String msg = "Error deleting tenant: " + tenantDomain; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error deleting application data of tenant: " + tenantDomain; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (DeviceManagementException e) { + String msg = "Error deleting device data of tenant: " + tenantDomain; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java index 44f6fe15dd2..683971472bd 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderService.java @@ -1070,4 +1070,5 @@ public interface DeviceManagementProviderService { List getInstalledApplicationsOnDevice(Device device) throws DeviceManagementException; List getEnrolledDevicesSince(Date since) throws DeviceManagementException; List getEnrolledDevicesPriorTo(Date before) throws DeviceManagementException; + void deleteDeviceDataByTenantDomain(String tenantDomain) throws DeviceManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 5c8a8eb28e1..3477fc25896 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.core.service; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; import io.entgra.device.mgt.core.notification.logger.DeviceEnrolmentLogContext; import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceEnrolmentLoggerImpl; @@ -160,7 +161,6 @@ import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, @@ -178,6 +178,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private final ApplicationDAO applicationDAO; private MetadataDAO metadataDAO; private final DeviceStatusDAO deviceStatusDAO; + private final TenantDAO tenantDao; int count = 0; public DeviceManagementProviderServiceImpl() { @@ -188,6 +189,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv this.enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); this.deviceStatusDAO = DeviceManagementDAOFactory.getDeviceStatusDAO(); + this.tenantDao = DeviceManagementDAOFactory.getTenantDAO(); /* Registering a listener to retrieve events when some device management service plugin is installed after * the component is done getting initialized */ @@ -5187,4 +5189,94 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return devices; } + + @Override + public void deleteDeviceDataByTenantDomain(String tenantDomain) throws DeviceManagementException { + int tenantId; + try{ + TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); + TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); + tenantId = tenantInfoBean.getTenantId(); + + } catch (Exception e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain; + log.error(msg); + throw new DeviceManagementException(msg, e); + } + + try { + DeviceManagementDAOFactory.beginTransaction(); + + tenantDao.deleteExternalPermissionMapping(tenantId); + tenantDao.deleteExternalDeviceMappingByTenantId(tenantId); + tenantDao.deleteExternalGroupMappingByTenantId(tenantId); + // TODO: Check whether deleting DM_DEVICE_ORGANIZATION table data is necessary +// tenantDao.deleteDeviceOrganizationByTenantId(tenantId); + tenantDao.deleteDeviceHistoryLastSevenDaysByTenantId(tenantId); + tenantDao.deleteDeviceDetailByTenantId(tenantId); + tenantDao.deleteDeviceLocationByTenantId(tenantId); + tenantDao.deleteDeviceInfoByTenantId(tenantId); + tenantDao.deleteNotificationByTenantId(tenantId); + tenantDao.deleteAppIconsByTenantId(tenantId); + tenantDao.deleteTraccarUnsyncedDevicesByTenantId(tenantId); + tenantDao.deleteDeviceEventGroupMappingByTenantId(tenantId); + tenantDao.deleteDeviceEventByTenantId(tenantId); + tenantDao.deleteGeofenceEventMappingByTenantId(tenantId); + tenantDao.deleteGeofenceGroupMappingByTenantId(tenantId); + tenantDao.deleteGeofenceByTenantId(tenantId); + tenantDao.deleteDeviceGroupPolicyByTenantId(tenantId); + tenantDao.deleteDynamicTaskPropertiesByTenantId(tenantId); + tenantDao.deleteDynamicTaskByTenantId(tenantId); + tenantDao.deleteMetadataByTenantId(tenantId); + tenantDao.deleteOTPDataByTenantId(tenantId); + tenantDao.deleteSubOperationTemplate(tenantId); + tenantDao.deleteDeviceSubTypeByTenantId(tenantId); + tenantDao.deleteCEAPoliciesByTenantId(tenantId); + + tenantDao.deleteApplicationByTenantId(tenantId); + tenantDao.deletePolicyCriteriaPropertiesByTenantId(tenantId); + tenantDao.deletePolicyCriteriaByTenantId(tenantId); + tenantDao.deleteCriteriaByTenantId(tenantId); + tenantDao.deletePolicyChangeManagementByTenantId(tenantId); + tenantDao.deletePolicyComplianceFeaturesByTenantId(tenantId); + tenantDao.deletePolicyComplianceStatusByTenantId(tenantId); + tenantDao.deleteRolePolicyByTenantId(tenantId); + tenantDao.deleteUserPolicyByTenantId(tenantId); + tenantDao.deleteDeviceTypePolicyByTenantId(tenantId); + tenantDao.deleteDevicePolicyAppliedByTenantId(tenantId); + tenantDao.deleteDevicePolicyByTenantId(tenantId); + tenantDao.deletePolicyCorrectiveActionByTenantId(tenantId); + tenantDao.deletePolicyByTenantId(tenantId); + tenantDao.deleteProfileFeaturesByTenantId(tenantId); + tenantDao.deleteProfileByTenantId(tenantId); + + tenantDao.deleteDeviceOperationResponseLargeByTenantId(tenantId); + tenantDao.deleteDeviceOperationResponseByTenantId(tenantId); + tenantDao.deleteEnrolmentOpMappingByTenantId(tenantId); + tenantDao.deleteDeviceStatusByTenantId(tenantId); + tenantDao.deleteEnrolmentByTenantId(tenantId); + tenantDao.deleteOperationByTenantId(tenantId); + tenantDao.deleteDeviceGroupMapByTenantId(tenantId); + tenantDao.deleteGroupPropertiesByTenantId(tenantId); + tenantDao.deleteDevicePropertiesByTenantId(tenantId); + tenantDao.deleteDeviceByTenantId(tenantId); + tenantDao.deleteRoleGroupMapByTenantId(tenantId); + tenantDao.deleteGroupByTenantId(tenantId); + tenantDao.deleteDeviceCertificateByTenantId(tenantId); + + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error deleting data of tenant of ID: '" + tenantId + "'"; + log.error(msg); + throw new DeviceManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error while initiating transaction when trying to delete tenant info of '" + tenantId + "'"; + log.error(msg); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java index 791723ebe31..bd7351d5f89 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java @@ -142,12 +142,13 @@ public class TenantManagerImpl implements TenantManager { log.debug("Request is received to delete Device related data of tenant with ID: " + tenantId); } try { - DeviceManagementDAOFactory.openConnection(); + DeviceManagementDAOFactory.beginTransaction(); tenantDao.deleteExternalPermissionMapping(tenantId); tenantDao.deleteExternalDeviceMappingByTenantId(tenantId); tenantDao.deleteExternalGroupMappingByTenantId(tenantId); - tenantDao.deleteDeviceOrganizationByTenantId(tenantId); + // TODO: Check whether deleting DM_DEVICE_ORGANIZATION table data is necessary +// tenantDao.deleteDeviceOrganizationByTenantId(tenantId); tenantDao.deleteDeviceHistoryLastSevenDaysByTenantId(tenantId); tenantDao.deleteDeviceDetailByTenantId(tenantId); tenantDao.deleteDeviceLocationByTenantId(tenantId); @@ -206,8 +207,8 @@ public class TenantManagerImpl implements TenantManager { String msg = "Error deleting data of tenant of ID: '" + tenantId + "'"; log.error(msg); throw new TenantMgtException(msg, e); - } catch (SQLException e) { - String msg = "Error accessing the database when trying to delete tenant info of '" + tenantId + "'"; + } catch (TransactionManagementException e) { + String msg = "Error initializing transaction when trying to delete tenant info of '" + tenantId + "'"; log.error(msg); throw new TenantMgtException(msg, e); } finally {