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

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

@ -775,11 +775,12 @@ public interface DeviceManagementProviderService {
/** /**
* Permanently delete a device type with all it's devices * 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 * @return True if device type successfully removed
* @throws DeviceManagementException Will be thrown if any service level or DAO level error occurs * @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 * Retrieves a list of configurations of a specific device

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

Loading…
Cancel
Save