Merge branch 'delete-device' into 'master'

Permanently Delete Device

Closes product-iots#98

See merge request entgra/carbon-device-mgt!118
revert-70aa11f8
Ruwan Yatawara 5 years ago
commit c15fbe9192

@ -899,7 +899,12 @@ public interface DeviceManagementService {
required = true)
@PathParam("device-id")
@Size(max = 45)
String deviceId);
String deviceId,
@ApiParam(
name = "permanentDelete",
value = "Boolean flag indicating whether to permanently delete the device.",
required = false)
@QueryParam("permanentDelete") boolean permanentDelete);
@GET
@Path("/{type}/{id}/features")

@ -323,7 +323,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@DELETE
@Override
@Path("/type/{device-type}/id/{device-id}")
public Response deleteDevice(@PathParam("device-type") String deviceType, @PathParam("device-id") String deviceId) {
public Response deleteDevice(@PathParam("device-type") String deviceType,
@PathParam("device-id") String deviceId,
@QueryParam("permanentDelete") boolean permanentDelete) {
DeviceManagementProviderService deviceManagementProviderService =
DeviceMgtAPIUtils.getDeviceManagementService();
try {
@ -333,13 +335,19 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.NOT_FOUND).build();
}
boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier);
boolean response;
if (permanentDelete) {
response = deviceManagementProviderService.deleteDevice(deviceIdentifier);
} else {
response = deviceManagementProviderService.disenrollDevice(deviceIdentifier);
}
return Response.status(Response.Status.OK).entity(response).build();
} catch (DeviceManagementException e) {
String msg = "Error encountered while deleting device of type : " + deviceType + " and " +
"ID : " + deviceId;
log.error(msg);
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()
).build();

@ -481,7 +481,8 @@ public class DeviceManagementServiceImplTest {
public void testDeleteDevice() {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService);
Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString());
Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString()
, false);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
}
@ -491,7 +492,8 @@ public class DeviceManagementServiceImplTest {
.toReturn(this.deviceManagementProviderService);
Mockito.when(this.deviceManagementProviderService
.getDevice(Mockito.any(DeviceIdentifier.class), Mockito.anyBoolean())).thenReturn(null);
Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString());
Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString()
, false);
Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
Mockito.reset(this.deviceManagementProviderService);
}
@ -502,7 +504,8 @@ public class DeviceManagementServiceImplTest {
.toReturn(this.deviceManagementProviderService);
Mockito.when(this.deviceManagementProviderService.disenrollDevice(Mockito.any(DeviceIdentifier.class)))
.thenThrow(new DeviceManagementException());
Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString());
Response response = this.deviceManagementService.deleteDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString()
, false);
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
Mockito.reset(this.deviceManagementProviderService);
}

@ -478,5 +478,13 @@ public interface DeviceDAO {
* fails.
*/
List<Device> getDevicesByIdentifiers(List<String> deviceIdentifiers, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to permanently delete the device and its related details
* @param deviceIdentifier device id
* @param tenantId tenant id
* @throws DeviceManagementDAOException
*/
void deleteDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException;
}

@ -36,6 +36,7 @@
package org.wso2.carbon.device.mgt.core.dao.impl;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
@ -62,6 +63,8 @@ import java.util.StringJoiner;
public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
private static final org.apache.commons.logging.Log log = LogFactory.getLog(AbstractDeviceDAOImpl.class);
@Override
public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException {
Connection conn;
@ -1405,4 +1408,378 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
public void deleteDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException {
Connection conn;
try {
conn = this.getConnection();
int deviceId = getDeviceId(conn, deviceIdentifier, tenantId);
if (deviceId == -1) {
String msg = "Device " + deviceIdentifier.getId() + " of type " + deviceIdentifier.getType() +
" is not found";
log.error(msg);
throw new DeviceManagementDAOException(msg);
} else {
int enrollmentId = getEnrollmentId(conn, deviceId, tenantId);
if (enrollmentId == -1) {
String msg = "Enrollment not found for the device " + deviceIdentifier.getId() + " of type " +
deviceIdentifier.getType();
log.error(msg);
throw new DeviceManagementDAOException(msg);
} else {
removeDeviceDetail(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device detail data");
}
removeDeviceLocation(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device location data");
}
removeDeviceInfo(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device info data");
}
removeDeviceNotification(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device notification data");
}
removeDeviceApplicationMapping(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device application mapping data");
}
removeDevicePolicyApplied(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device applied policy data");
}
removeDevicePolicy(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device policy data");
}
removeEnrollmentDeviceDetail(conn, enrollmentId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed enrollment device detail data");
}
removeEnrollmentDeviceLocation(conn, enrollmentId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed enrollment device location data");
}
removeEnrollmentDeviceInfo(conn, enrollmentId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed enrollment device info data");
}
removeEnrollmentDeviceApplicationMapping(conn, enrollmentId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed enrollment device application mapping data");
}
removeDeviceOperationResponse(conn, enrollmentId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device operation response data");
}
removeEnrollmentOperationMapping(conn, enrollmentId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed enrollment operation mapping data");
}
removeDeviceEnrollment(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device enrollment data");
}
removeDeviceGroupMapping(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully removed device group mapping data");
}
removeDevice(conn, deviceId);
if (log.isDebugEnabled()) {
log.debug("Successfully permanently deleted the device");
}
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while deleting the device", e);
}
}
private int getDeviceId(Connection conn, DeviceIdentifier deviceIdentifier, int tenantId)
throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
int deviceId = -1;
try {
String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceIdentifier.getId());
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
deviceId = rs.getInt("ID");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving device id of the device", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceId;
}
private int getEnrollmentId(Connection conn, int deviceId, int tenantId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
int enrollmentId = -1;
try {
String sql = "SELECT ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrollmentId = rs.getInt("ID");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving enrollment id of the device", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return enrollmentId;
}
private void removeDeviceDetail(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device detail", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDeviceLocation(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device location", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDeviceInfo(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device info", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDeviceNotification(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_NOTIFICATION WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device notification", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDeviceApplicationMapping(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device application mapping", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDevicePolicyApplied(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device policy applied", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDevicePolicy(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device policy", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeEnrollmentDeviceDetail(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE ENROLMENT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing enrollment device detail", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeEnrollmentDeviceLocation(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE ENROLMENT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing enrollment device location", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeEnrollmentDeviceInfo(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_INFO WHERE ENROLMENT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing enrollment device info", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeEnrollmentDeviceApplicationMapping(Connection conn, int enrollmentId)
throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE ENROLMENT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing enrollment device application " +
"mapping", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDeviceOperationResponse(Connection conn, int enrollmentId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device operation response", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeEnrollmentOperationMapping(Connection conn, int enrollmentId)
throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrollmentId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing enrollment operation mapping", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDeviceEnrollment(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device enrollment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDeviceGroupMapping(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device group mapping", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private void removeDevice(Connection conn, int deviceId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String sql = "DELETE FROM DM_DEVICE WHERE ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
}

@ -582,6 +582,8 @@ public interface DeviceManagementProviderService {
boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
boolean deleteDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException;
boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException;

@ -512,6 +512,67 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return deviceManager.disenrollDevice(deviceId);
}
@Override
public boolean deleteDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
if (deviceId == null) {
String msg = "Required values are not set to permanently delete device";
log.error(msg);
throw new DeviceManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Permanently deleting device: " + deviceId.getId() + " of type '" + deviceId.getType() + "'");
}
DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'deleteDevice'");
}
return false;
}
int tenantId = this.getTenantId();
Device device = this.getDevice(deviceId, false);
if (device == null) {
if (log.isDebugEnabled()) {
log.debug("Device not found for id '" + deviceId.getId() + "'");
}
return false;
}
if (!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
String msg = "Device " + deviceId.getId() + " of type " + deviceId.getType() + " is not dis-enrolled to " +
"permanently delete the device";
log.error(msg);
throw new DeviceManagementException(msg);
} else {
try {
DeviceManagementDAOFactory.beginTransaction();
deviceDAO.deleteDevice(deviceId, tenantId);
DeviceManagementDAOFactory.commitTransaction();
this.removeDeviceFromCache(deviceId);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while permanently deleting '" + deviceId.getType() +
"' device with the identifier '" + deviceId.getId() + "'";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred while permanently deleting device: " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
return true;
}
@Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
Device device = this.getDevice(deviceId, false);

Loading…
Cancel
Save