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/api/DeviceManagementService.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/api/DeviceManagementService.java index eeb7caeeb3..357609b122 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/api/DeviceManagementService.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/api/DeviceManagementService.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api; +import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.DisenrollRequest; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -1279,6 +1280,48 @@ public interface DeviceManagementService { @Size(max = 45) String deviceId); + @PUT + @Produces(MediaType.APPLICATION_JSON) + @Path("/disenroll") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Remove Multiple Devices Specified by Device IDs and Device Type", + notes = "Deletes multiple devices of the specified device type specified by their device IDs and returns the status of the deletion operation.", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "dm:devices:delete") + }) + }, + nickname = "deleteMultipleDevicesByType" + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully deleted the devices.", + response = Device.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while deleting devices.", + response = ErrorResponse.class) + }) + Response disenrollMultipleDevices(@ApiParam( + name = "deviceTypeWithDeviceIds", + value = "The properties to advanced search devices.", + required = true) + DisenrollRequest deviceTypeWithDeviceIds); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/device-type/{type}/features") 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/DeviceManagementServiceImpl.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/DeviceManagementServiceImpl.java index 1960d0e41d..881a586f82 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/DeviceManagementServiceImpl.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/DeviceManagementServiceImpl.java @@ -26,6 +26,7 @@ import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionMa import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; +import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.DisenrollRequest; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -101,8 +102,9 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; -import java.util.concurrent.ExecutionException; +import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ExecutionException; @Path("/devices") public class DeviceManagementServiceImpl implements DeviceManagementService { @@ -474,6 +476,55 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } + @PUT + @Override + @Path("/disenroll") + public Response disenrollMultipleDevices(DisenrollRequest deviceTypeWithDeviceIds) { + + if (deviceTypeWithDeviceIds == null) { + String errorMsg = "Invalid request. The request body must not be null."; + return Response.status(Response.Status.BAD_REQUEST).entity(errorMsg).build(); + } + DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); + + List successfullyDisenrolledDevices = new ArrayList<>(); + List failedToDisenrollDevices = new ArrayList<>(); + + Map> list = deviceTypeWithDeviceIds.getDeviceTypeWithDeviceIds(); + + for (Map.Entry> entry : list.entrySet()) { + String deviceType = entry.getKey(); + List deviceIds = entry.getValue(); + + for (String deviceId : deviceIds) { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); + try { + Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true); + if (persistedDevice != null) { + boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier); + if (response) { + successfullyDisenrolledDevices.add(deviceIdentifier); + } else { + failedToDisenrollDevices.add(deviceIdentifier); + } + } else { + failedToDisenrollDevices.add(deviceIdentifier); + } + } catch (DeviceManagementException e) { + String msg = "Error encountered while dis-enrolling device of type: " + deviceType + " with " + deviceId; + log.error(msg, e); + failedToDisenrollDevices.add(deviceIdentifier); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + } + + Map> responseMap = new HashMap<>(); + responseMap.put("successfullyDisenrollDevices", successfullyDisenrolledDevices); + responseMap.put("failedToDisenrollDevices", failedToDisenrollDevices); + + return Response.status(Response.Status.OK).entity(responseMap).build(); + } @POST @Override @Path("/type/{deviceType}/id/{deviceId}/rename") 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/util/DisenrollRequest.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/util/DisenrollRequest.java new file mode 100644 index 0000000000..026866b5e1 --- /dev/null +++ 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/util/DisenrollRequest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.List; +import java.util.Map; + +@ApiModel(value = "DisenrollRequest", description = "Contains the multiple devices specified by device IDs") +public class DisenrollRequest { + @ApiModelProperty(name = "deviceTypeWithDeviceIds", value = "Contains the multiple devices specified by device IDs with type", + required = true) + private Map> deviceTypeWithDeviceIds; + + public Map> getDeviceTypeWithDeviceIds() { + return deviceTypeWithDeviceIds; + } + + public void setDeviceTypeWithDeviceIds(Map> deviceTypeWithDeviceIds) { + this.deviceTypeWithDeviceIds = deviceTypeWithDeviceIds; + } +}