Update service to check device type validation at service level and return a Not Found request, if device type doesn't exist

feature/appm-store/pbac
Yohan Avishke 5 years ago
parent 709af6795b
commit 426287fdc1

@ -594,7 +594,7 @@ public interface DeviceTypeManagementAdminService {
@DELETE
@Path("{deviceType}/delete")
@Path("{deviceTypeName}/delete")
@ApiOperation(
httpMethod = "DELETE",
value = "Delete device type.",
@ -628,8 +628,8 @@ public interface DeviceTypeManagementAdminService {
})
Response deleteDeviceType(
@ApiParam(
name = "deviceType",
name = "deviceTypeName",
value = "Device type name.",
required = true)
@PathParam("deviceType") String deviceType);
@PathParam("deviceTypeName") String deviceTypeName);
}

@ -320,20 +320,27 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen
@Override
@DELETE
@Path("{deviceType}/delete")
public Response deleteDeviceType(@PathParam("deviceType") String deviceType) {
@Path("{deviceTypeName}/delete")
public Response deleteDeviceType(@PathParam("deviceTypeName") String deviceTypeName) {
try {
DeviceManagementProviderService deviceManagementProviderService =
DeviceMgtAPIUtils.getDeviceManagementService();
if (!deviceManagementProviderService.deleteDeviceType(deviceType)){
String msg = "Error occurred while deleting device of type: " + deviceType;
DeviceType deviceType = deviceManagementProviderService.getDeviceType(deviceTypeName);
if (deviceType == null) {
String msg = "Error, device of type: " + deviceTypeName + " does not exist";
log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
}
if (!deviceManagementProviderService.deleteDeviceType(deviceTypeName, deviceType)){
String msg = "Error occurred while deleting device of type: " + deviceTypeName;
log.error(msg);
return Response.serverError().entity(msg).build();
}
return Response.status(Response.Status.ACCEPTED).entity(
"Device of type: " + deviceType + " permanently deleted.").build();
return Response.status(Response.Status.ACCEPTED)
.entity("Device of type: " + deviceTypeName + " permanently deleted.")
.build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while deleting device of type: " + deviceType;
String msg = "Error occurred while deleting device of type: " + deviceTypeName;
log.error(msg, e);
return Response.serverError().entity(msg).build();
}

@ -775,11 +775,12 @@ public interface DeviceManagementProviderService {
/**
* Permanently delete a device type with all it's devices
*
* @param deviceTypeName device type name
* @param deviceTypeName Device type name
* @param deviceType Device type object
* @return True if device type successfully removed
* @throws DeviceManagementException Will be thrown if any service level or DAO level error occurs
*/
boolean deleteDeviceType(String deviceTypeName) throws DeviceManagementException;
boolean deleteDeviceType(String deviceTypeName, DeviceType deviceType) throws DeviceManagementException;
/**
* Retrieves a list of configurations of a specific device

@ -3589,25 +3589,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public boolean deleteDeviceType(String deviceTypeName) throws DeviceManagementException {
int tenantId = getTenantId();
public boolean deleteDeviceType(String deviceTypeName, DeviceType deviceType)
throws DeviceManagementException {
List<String> deviceIdentifiers;
List<DeviceTypeVersion> deviceTypeVersions;
boolean result;
int tenantId = getTenantId();
try {
if (deviceTypeName.isEmpty()) {
if (deviceTypeName.isEmpty() || deviceType == null) {
String msg = "Error, device type cannot be null or empty";
log.error(msg);
return false;
}
DeviceManagementDAOFactory.beginTransaction();
DeviceType deviceTypeObj = deviceTypeDAO.getDeviceType(deviceTypeName, tenantId);
if (deviceTypeObj == null) {
String msg = "Error, device of type: " + deviceTypeName + " does not exist";
log.error(msg);
return false;
}
List<Device> devices = deviceDAO.getDevices(deviceTypeName, this.getTenantId());
if (devices.isEmpty()) {
if (log.isDebugEnabled()) {
@ -3637,14 +3632,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.beginTransaction();
}
// remove device type versions
deviceTypeVersions = deviceTypeDAO.getDeviceTypeVersions(deviceTypeObj.getId(), deviceTypeName);
deviceTypeVersions = deviceTypeDAO.getDeviceTypeVersions(deviceType.getId(), deviceTypeName);
if (deviceTypeVersions.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("Device of type: " + deviceTypeName + "doesn't have any type versions");
}
} else {
for (DeviceTypeVersion deviceTypeVersion : deviceTypeVersions) {
result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceTypeObj.getId()
result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceType.getId()
, deviceTypeVersion.getVersionName(), tenantId);
if (!result) {
String msg = "Device type of: " + deviceTypeName + "is unauthorized to modify " +
@ -3662,7 +3657,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
}
// delete device type
deviceTypeDAO.deleteDeviceType(tenantId, deviceTypeObj.getId());
deviceTypeDAO.deleteDeviceType(tenantId, deviceType.getId());
DeviceManagementDAOFactory.commitTransaction();
} catch (InvalidDeviceException e) {
String msg = "Error occurred while deleting devices of type: " + deviceTypeName;

Loading…
Cancel
Save