From 2d6e512fdf3a2f5189dfd22fb09175de249a4d2e Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Thu, 2 Nov 2023 11:42:58 +0530 Subject: [PATCH 1/2] Add multiple device dis-enrollment support --- .../service/api/DeviceManagementService.java | 43 +++++++++++++++ .../impl/DeviceManagementServiceImpl.java | 53 ++++++++++++++++++- .../service/impl/util/DisenrollRequest.java | 40 ++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 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 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; + } +} From 46090437f013ab9f3f4c79353ac2ff9fd69918a0 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Fri, 10 Nov 2023 18:06:58 +0530 Subject: [PATCH 2/2] Optimize multiple dis-enrollment --- .../service/api/DeviceManagementService.java | 5 +++-- .../impl/DeviceManagementServiceImpl.java | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) 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 357609b122..acb161d77e 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 @@ -1287,7 +1287,8 @@ public interface DeviceManagementService { 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.", + notes = "Deletes multiple devices of the specified device type specified by their device IDs" + + " and returns the status of the dis-enrollment operation.", tags = "Device Management", extensions = { @Extension(properties = { @@ -1319,7 +1320,7 @@ public interface DeviceManagementService { }) Response disenrollMultipleDevices(@ApiParam( name = "deviceTypeWithDeviceIds", - value = "The properties to advanced search devices.", + value = "Device type and corresponding device IDs for disenrollment", required = true) DisenrollRequest deviceTypeWithDeviceIds); @GET 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 881a586f82..9e82728d3a 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 @@ -491,17 +491,22 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { List failedToDisenrollDevices = new ArrayList<>(); Map> list = deviceTypeWithDeviceIds.getDeviceTypeWithDeviceIds(); + String deviceType; + List deviceIds; + DeviceIdentifier deviceIdentifier; + Device persistedDevice; + boolean response; for (Map.Entry> entry : list.entrySet()) { - String deviceType = entry.getKey(); - List deviceIds = entry.getValue(); + deviceType = entry.getKey(); + deviceIds = entry.getValue(); for (String deviceId : deviceIds) { - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); + deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); try { - Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true); + persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true); if (persistedDevice != null) { - boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier); + response = deviceManagementProviderService.disenrollDevice(deviceIdentifier); if (response) { successfullyDisenrolledDevices.add(deviceIdentifier); } else { @@ -509,6 +514,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } else { failedToDisenrollDevices.add(deviceIdentifier); + if(log.isDebugEnabled()){ + String msg = "Error encountered while dis-enrolling device of type: " + deviceType + " with " + deviceId; + log.error(msg); + } } } catch (DeviceManagementException e) { String msg = "Error encountered while dis-enrolling device of type: " + deviceType + " with " + deviceId;