From c2a9b6a0738293d87b355082680070af0aa849a4 Mon Sep 17 00:00:00 2001 From: Pramila Niroshan Date: Tue, 19 Dec 2023 18:56:49 +0000 Subject: [PATCH] Add a config to decide which device statuses to persist in the database (#250) Co-authored-by: pramilaniroshan Reviewed-on: https://repository.entgra.net/community/device-mgt-core/pulls/250 Co-authored-by: Pramila Niroshan Co-committed-by: Pramila Niroshan --- .../api/DeviceStatusFilterService.java | 252 +++++++++++ .../impl/DeviceStatusFilterServiceImpl.java | 120 +++++ .../mgt/api/jaxrs/util/DeviceMgtAPIUtils.java | 25 ++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 + .../metadata/mgt/AllowedDeviceStatus.java | 43 ++ .../mgt/DeviceStatusManagementService.java | 85 ++++ .../config/ui/DeviceStatusConfigurations.java | 46 ++ .../mgt/core/config/ui/DeviceStatusItem.java | 48 ++ .../mgt/core/config/ui/UIConfiguration.java | 16 +- .../device/mgt/core/dao/EnrollmentDAO.java | 31 ++ .../dao/impl/AbstractEnrollmentDAOImpl.java | 14 +- .../enrolment/GenericEnrollmentDAOImpl.java | 12 +- .../enrolment/SQLServerEnrollmentDAOImpl.java | 12 +- .../internal/DeviceManagementDataHolder.java | 11 + .../DeviceManagementServiceComponent.java | 13 + .../DeviceStatusManagementServiceImpl.java | 322 ++++++++++++++ .../mgt/dao/util/MetadataConstants.java | 3 + .../DeviceManagementProviderServiceImpl.java | 52 ++- .../task/impl/DeviceStatusMonitoringTask.java | 21 +- .../device/mgt/core/search/DeviceDetails.java | 8 +- .../DeviceManagementProviderServiceTest.java | 12 +- .../mgt/core/task/DeviceTaskManagerTest.java | 3 +- .../config/operation/mdm-ui-config.xml | 410 ++++++++++++++++++ .../src/test/resources/sql/h2.sql | 12 + .../mgt/common/spi/TenantManagerService.java | 2 + .../core/tenant/mgt/core/TenantManager.java | 7 + .../mgt/core/impl/TenantManagerImpl.java | 15 + .../core/impl/TenantManagerServiceImpl.java | 5 + .../core/internal/TenantMgtDataHolder.java | 11 + .../internal/TenantMgtServiceComponent.java | 6 + .../listener/DeviceMgtTenantListener.java | 1 + .../src/main/resources/conf/mdm-ui-config.xml | 50 +++ 32 files changed, 1630 insertions(+), 40 deletions(-) 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/api/DeviceStatusFilterService.java 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/DeviceStatusFilterServiceImpl.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/AllowedDeviceStatus.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusConfigurations.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusItem.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/config/operation/mdm-ui-config.xml 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/DeviceStatusFilterService.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/DeviceStatusFilterService.java new file mode 100644 index 0000000000..7c0d84de1d --- /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/api/DeviceStatusFilterService.java @@ -0,0 +1,252 @@ +/* + * 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.api; + +import io.entgra.device.mgt.core.apimgt.annotations.Scope; +import io.entgra.device.mgt.core.apimgt.annotations.Scopes; +import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ErrorResponse; +import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants; +import io.swagger.annotations.*; +import org.apache.axis2.transport.http.HTTPConstants; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; + +/** + * Metadata related REST-API implementation. + */ +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "Device Status Filter Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "DeviceStatusManagement"), + @ExtensionProperty(name = "context", value = "/api/device-mgt/v1.0/device-status-filters"), + }) + } + ), + tags = { + @Tag(name = "device_management") + } +) +@Scopes( + scopes = { + @Scope( + name = "View Device Status Filter", + description = "View device status details", + key = "dm:devicestatusfilter:view", + roles = {"Internal/devicemgt-user"}, + permissions = {"/device-mgt/device-status-filters/view"} + ), + @Scope( + name = "Update Device status filter", + description = "Updating Device status filter", + key = "dm:devicestatusfilter:update", + roles = {"Internal/devicemgt-user"}, + permissions = {"/device-mgt/device-status-filters/update"} + ), + } +) +@Api(value = "Device Status Management") +@Path("/device-status-filters") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface DeviceStatusFilterService { + + @GET + @Path("/{device-type}") + @ApiOperation( + httpMethod = HTTPConstants.HEADER_GET, + value = "Get device status filters", + notes = "Get device status filters for the tenant of the logged in user", + tags = "Tenant Metadata Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "dm:devicestatusfilter:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved device status filters.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 500, + message = "Internal Server Error. " + + "\n Server error occurred while getting device status filters.", + response = ErrorResponse.class) + }) + Response getDeviceStatusFilters( @ApiParam( + name = "deviceType", + value = "The device type.", + required = true) @PathParam ("device-type") String deviceType); + + @GET + @Path("/is-enabled") + @ApiOperation( + httpMethod = HTTPConstants.HEADER_GET, + value = "Get device status filter", + notes = "Get device status filter enable or not for the tenant of the logged in user", + tags = "Tenant Metadata Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "dm:devicestatusfilter:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved device status filter.", + response = Response.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 500, + message = "Internal Server Error. " + + "\n Server error occurred while getting device status check.", + response = ErrorResponse.class) + }) + Response getDeviceStatusCheck(); + + @PUT + @Path("/toggle-device-status") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Update Device status check for tenant", + notes = "Update Device status check for the tenant of the logged in user", + tags = "Tenant Metadata Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "dm:devicestatusfilter:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully updated device status check.", + response = Response.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 500, + message = "Internal Server Error. " + + "\n Server error occurred while updating device status check.", + response = ErrorResponse.class) + }) + Response updateDeviceStatusCheck( + @ApiParam( + name = "Device status check", + value = "The device status filtering is enable or not.", + required = true) + @QueryParam("isEnabled") boolean isEnabled); + + @PUT + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Update Device status filters for given device type for tenant", + notes = "Update Device status filters for given device type for the tenant of the logged in user", + tags = "Tenant Metadata Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "dm:devicestatusfilter:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully updated device status filters for given device type..", + response = Response.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 500, + message = "Internal Server Error. " + + "\n Server error occurred while updating device status filters for given device type.", + response = ErrorResponse.class) + }) + Response updateDeviceStatusFilters( + @ApiParam( + name = "deviceType", + value = "The device type for which you want to update device status filters.", + required = true) + @QueryParam("deviceType") String deviceType, + @ApiParam( + name = "deviceStatus", + value = "A list of device status values to update for the given device type.", + required = true) + @QueryParam("deviceStatus") List deviceStatus); +} 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/DeviceStatusFilterServiceImpl.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/DeviceStatusFilterServiceImpl.java new file mode 100644 index 0000000000..7e5fd01437 --- /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/DeviceStatusFilterServiceImpl.java @@ -0,0 +1,120 @@ +/* + * 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; + +import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.DeviceStatusFilterService; +import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; + +@Path("/device-status-filters") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class DeviceStatusFilterServiceImpl implements DeviceStatusFilterService { + + private static final Log log = LogFactory.getLog(DeviceStatusFilterServiceImpl.class); + + @Override + @GET + @Path("/{deviceType}") + public Response getDeviceStatusFilters(@PathParam("deviceType") String deviceType) { + List result; + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService(); + result = deviceManagementProviderService.getDeviceStatusFilters(deviceType, tenantId); + if (result != null) { + return Response.status(Response.Status.OK).entity(result).build(); + } + return Response.status(Response.Status.NO_CONTENT).entity(false).build(); + } catch (MetadataManagementException e) { + String msg = "Error occurred while getting device status filter of the tenant."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @GET + @Path("/is-enabled") + @Override + public Response getDeviceStatusCheck() { + boolean result; + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService(); + result = deviceManagementProviderService.getDeviceStatusCheck(tenantId); + return Response.status(Response.Status.OK).entity(result).build(); + } catch (MetadataManagementException e) { + String msg = "Error occurred while getting device status filter of the tenant."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + @PUT + @Path("/toggle-device-status") + public Response updateDeviceStatusCheck( + @QueryParam("isEnabled") + boolean isEnabled) { + boolean result; + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService(); + result = deviceManagementProviderService.updateDefaultDeviceStatusCheck(tenantId, isEnabled); + if (result) { + return Response.status(Response.Status.OK).entity("Successfully updated device status check.").build(); + } else { + return Response.status(Response.Status.NO_CONTENT).entity(false).build(); + } + } catch (MetadataManagementException e) { + String msg = "Error occurred while updating device status check."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + @PUT + public Response updateDeviceStatusFilters( + @QueryParam("deviceType") + String deviceType, + @QueryParam("deviceStatus") + List deviceStatus + ) { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService(); + deviceManagementProviderService.updateDefaultDeviceStatusFilters(tenantId, deviceType, deviceStatus); + return Response.status(Response.Status.OK).entity("Successfully updated device status filters for " + deviceType).build(); + } catch (MetadataManagementException e) { + String msg = "Error occurred while updating device status for " + deviceType; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } +} 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/util/DeviceMgtAPIUtils.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/util/DeviceMgtAPIUtils.java index 1c3be6a797..a39cdba7b2 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/util/DeviceMgtAPIUtils.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/util/DeviceMgtAPIUtils.java @@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.util; import io.entgra.device.mgt.core.apimgt.webapp.publisher.APIPublisherService; 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.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; import org.apache.axis2.java.security.SSLProtocolSocketFactory; @@ -153,6 +154,8 @@ public class DeviceMgtAPIUtils { // private static IntegrationClientService integrationClientService; private static MetadataManagementService metadataManagementService; private static WhiteLabelManagementService whiteLabelManagementService; + + private static DeviceStatusManagementService deviceStatusManagementService; private static OTPManagementService otpManagementService; private static volatile SubscriptionManager subscriptionManager; @@ -532,6 +535,28 @@ public class DeviceMgtAPIUtils { return whiteLabelManagementService; } + /** + * Initializing and accessing method for DeviceStatusManagementService. + * + * @return WhiteLabelManagementService instance + * @throws IllegalStateException if DeviceStatusManagementService cannot be initialized + */ + public static DeviceStatusManagementService getDeviceStatusManagmentService() { + if (deviceStatusManagementService == null) { + synchronized (DeviceMgtAPIUtils.class) { + if (deviceStatusManagementService == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + deviceStatusManagementService = (DeviceStatusManagementService) ctx.getOSGiService( + DeviceStatusManagementService.class, null); + if (deviceStatusManagementService == null) { + throw new IllegalStateException("DeviceStatusManagementService Management service not initialized."); + } + } + } + } + return deviceStatusManagementService; + } + /** * Initializing and accessing method for MetadataManagementService. * diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index bb05b0b2e3..7cdb4ffa0e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -50,6 +50,7 @@ + @@ -101,6 +102,7 @@ + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/AllowedDeviceStatus.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/AllowedDeviceStatus.java new file mode 100644 index 0000000000..6824315b0d --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/AllowedDeviceStatus.java @@ -0,0 +1,43 @@ +/* + * 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.common.metadata.mgt; + +import java.util.List; + +public class AllowedDeviceStatus { + private String type; + + private List status; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public List getStatus() { + return status; + } + + public void setStatus(List status) { + this.status = status; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java new file mode 100644 index 0000000000..5ee7c8fce0 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java @@ -0,0 +1,85 @@ +/* + * 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.common.metadata.mgt; + +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; + +import java.util.List; + + +public interface DeviceStatusManagementService { + + /** + * This method is useful to create & persist default device status filter for provided tenant if + * it doesn't exist already + * + * @throws MetadataManagementException if error while adding default device status + */ + void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException; + + /** + * This method is useful to reset existing device status to default values in xml + * + * @throws MetadataManagementException if error while resetting default device status + */ + void resetToDefaultDeviceStausFilter() throws MetadataManagementException; + + /** + * This method is useful to update existing allowed device status + * + * @throws MetadataManagementException if error while updating existing device status + */ + void updateDefaultDeviceStatusFilters(int tenantId, String deviceType, List deviceStatus) + throws MetadataManagementException; + + /** + * This method is useful to update existing device status check + * + * @throws MetadataManagementException if error while updating existing device status + */ + boolean updateDefaultDeviceStatusCheck(int tenantId, boolean isChecked) + throws MetadataManagementException; + /** + * This method is useful to get existing device status filters + * + * @throws MetadataManagementException if error while getting existing device status + */ + List getDeviceStatusFilters(int tenantId) throws MetadataManagementException; + + /** + * This method is useful to get existing device status filters by device type and tenant id + * + * @throws MetadataManagementException if error while getting existing device status + */ + List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException; + + /** + * This method is useful to get existing device status filters + * + * @throws MetadataManagementException if error while getting existing device status check + */ + boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException; + + /** + * This method is useful to check status is valid for device type + * + * @throws MetadataManagementException if error while getting existing device status check + */ + boolean isDeviceStatusValid(String deviceType, String deviceStatus, int tenantId) throws MetadataManagementException; +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusConfigurations.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusConfigurations.java new file mode 100644 index 0000000000..9cdbacf399 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusConfigurations.java @@ -0,0 +1,46 @@ +/* + * 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.core.config.ui; + +import javax.xml.bind.annotation.XmlElement; +import java.util.List; + +public class DeviceStatusConfigurations { + + private List deviceStatusItems; + private boolean deviceStatusCheck; + + @XmlElement(name = "DeviceStatusItem") + public List getDeviceStatusItems() { + return deviceStatusItems; + } + + @XmlElement(name = "EnableDeviceStatusCheck") + public boolean isDeviceStatusCheck() { + return deviceStatusCheck; + } + + public void setDeviceStatusCheck(boolean deviceStatusCheck) { + this.deviceStatusCheck = deviceStatusCheck; + } + + public void setDeviceStatusItems(List deviceStatusItems) { + this.deviceStatusItems = deviceStatusItems; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusItem.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusItem.java new file mode 100644 index 0000000000..43a5cc018a --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/DeviceStatusItem.java @@ -0,0 +1,48 @@ +/* + * 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.core.config.ui; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import java.util.List; + +public class DeviceStatusItem { + + private List status; + private String type; + + @XmlElement(name = "Type") + public String getType() { + return type; + } + + @XmlElementWrapper(name = "AllowedStatus") + @XmlElement(name = "Status") + public List getStatus() { + return status; + } + + public void setType(String type) { + this.type = type; + } + + public void setStatus(List status) { + this.status = status; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/UIConfiguration.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/UIConfiguration.java index 6e71d17072..4dd9cbfe4a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/UIConfiguration.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/ui/UIConfiguration.java @@ -38,7 +38,9 @@ public class UIConfiguration { private DeviceInfoConfigurations deviceInfoConfigurations; - @XmlElement(name = "AppRegistration", required=true) + private DeviceStatusConfigurations deviceStatusConfigurations; + + @XmlElement(name = "AppRegistration", required = true) public AppRegistration getAppRegistration() { return appRegistration; } @@ -74,7 +76,8 @@ public class UIConfiguration { public void setHubspotChat(HubspotChat hubspotChat) { this.hubspotChat = hubspotChat; } - @XmlElement(name = "Billing", required=true) + + @XmlElement(name = "Billing", required = true) public Billing getBilling() { return billing; } @@ -109,4 +112,13 @@ public class UIConfiguration { public void setDeviceInfoConfigurations(DeviceInfoConfigurations deviceInfoConfigurations) { this.deviceInfoConfigurations = deviceInfoConfigurations; } + + @XmlElement(name = "DeviceStatusConfigurations", required = true) + public DeviceStatusConfigurations getDeviceStatusConfigurations() { + return deviceStatusConfigurations; + } + + public void setDeviceStatusConfigurations(DeviceStatusConfigurations deviceStatusConfigurations) { + this.deviceStatusConfigurations = deviceStatusConfigurations; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java index c6fd1828bc..1bce2627d9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/EnrollmentDAO.java @@ -62,4 +62,35 @@ public interface EnrollmentDAO { boolean updateOwnerOfEnrollment(List devices, String owner, int tenantId) throws DeviceManagementDAOException; + /*** + *This method is used to add the device status of the enrollment for given set of devices to given user. + * + * @param currentOwner of device. + * @param status going to add + * @param tenantId tenant id. + * @return either (1) true, if device status is succeeded or false. + * @throws DeviceManagementDAOException if an error occurs when updating device owner. + */ + boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException; + + /*** + *This method is used to add the device status of the enrollment for given set of devices to given user. + * + * @param config of Enrollment. + * @return either (1) true, if device status is succeeded or false. + * @throws DeviceManagementDAOException if an error occurs when updating device owner. + */ + boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException; + + /*** + *This method is used to add the device status of the enrollment for given set of devices to given user. + * + * @param enrolmentId of device. + * @param status going to add + * @return either (1) true, if device status is succeeded or false. + * @throws DeviceManagementDAOException if an error occurs when updating device owner. + */ + boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException; + + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java index 456dca9c8a..98050fe318 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java @@ -93,9 +93,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { stmt.setInt(4, enrolmentInfo.getId()); stmt.setInt(5, tenantId); int updatedCount = stmt.executeUpdate(); - if (updatedCount == 1){ - addDeviceStatus(enrolmentInfo.getId(), enrolmentInfo.getStatus()); - } return updatedCount; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); @@ -131,9 +128,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { } if (updateStatus > 0) { status = true; - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - addDeviceStatus(enrolmentInfo); - } } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); @@ -256,11 +250,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { return true; } - private boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { + public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { return addDeviceStatus(config.getId(), config.getStatus()); } - private boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { + public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (changedBy == null){ @@ -323,7 +317,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { return true; } - private boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { + public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (changedBy == null){ @@ -347,7 +341,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { DeviceManagementDAOUtil.cleanupResources(stmt, null); // if there was no record for the enrolment or the previous status is not the same as the current status // we'll add a record - if (previousStatus == null || previousStatus != status){ + if (previousStatus == null || previousStatus != status) { if (deviceId == -1) { // we need the device id in order to add a new record, therefore we get it from the enrolment table sql = "SELECT DEVICE_ID FROM DM_ENROLMENT WHERE ID = ?"; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java index 452e1a1b30..188af00f88 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/GenericEnrollmentDAOImpl.java @@ -88,9 +88,6 @@ public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { stmt.setInt(4, enrolmentInfo.getId()); stmt.setInt(5, tenantId); int updatedCount = stmt.executeUpdate(); - if (updatedCount == 1){ - addDeviceStatus(enrolmentInfo.getId(), enrolmentInfo.getStatus()); - } return updatedCount; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); @@ -126,9 +123,6 @@ public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { } if (updateStatus > 0) { status = true; - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - addDeviceStatus(enrolmentInfo); - } } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); @@ -251,11 +245,11 @@ public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { return true; } - private boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { + public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { return addDeviceStatus(config.getId(), config.getStatus()); } - private boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { + public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (changedBy == null){ @@ -318,7 +312,7 @@ public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { return true; } - private boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { + public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (changedBy == null){ diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java index cd5d846df9..18074e0841 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/enrolment/SQLServerEnrollmentDAOImpl.java @@ -87,9 +87,6 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { stmt.setInt(4, enrolmentInfo.getId()); stmt.setInt(5, tenantId); int updatedCount = stmt.executeUpdate(); - if (updatedCount == 1){ - addDeviceStatus(enrolmentInfo.getId(), enrolmentInfo.getStatus()); - } return updatedCount; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); @@ -125,9 +122,6 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { } if (updateStatus > 0) { status = true; - for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { - addDeviceStatus(enrolmentInfo); - } } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e); @@ -250,11 +244,11 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { return true; } - private boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { + public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException { return addDeviceStatus(config.getId(), config.getStatus()); } - private boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { + public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (changedBy == null){ @@ -317,7 +311,7 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl { return true; } - private boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { + public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException { Connection conn; String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (changedBy == null){ diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java index a1dbe98c2a..5cdc29da25 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.internal; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.DeviceStatusTaskPluginConfig; @@ -93,6 +94,8 @@ public class DeviceManagementDataHolder { private WhiteLabelManagementService whiteLabelManagementService; private TraccarManagementService traccarManagementService; + private DeviceStatusManagementService deviceStatusManagementService; + private final Map deviceStatusTaskPluginConfigs = Collections.synchronizedMap( new HashMap<>()); @@ -384,6 +387,14 @@ public class DeviceManagementDataHolder { this.whiteLabelManagementService = whiteLabelManagementService; } + public DeviceStatusManagementService getDeviceStatusManagementService() { + return deviceStatusManagementService; + } + + public void setDeviceStatusManagementService(DeviceStatusManagementService deviceStatusManagementService) { + this.deviceStatusManagementService = deviceStatusManagementService; + } + public TraccarManagementService getTraccarManagementService() { TraccarManagementService traccarManagementService; PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java index d7b459d4b7..b3bc1f2f0d 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -17,6 +17,8 @@ */ package io.entgra.device.mgt.core.device.mgt.core.internal; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -372,6 +374,17 @@ public class DeviceManagementServiceComponent { } bundleContext.registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null); + /* Registering DeviceState Filter Service */ + DeviceStatusManagementService deviceStatusManagemntService = new DeviceStatusManagementServiceImpl(); + DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagemntService); + try { + deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist(tenantId); + } catch (Throwable e) { + log.error("Error occurred while adding default tenant device status", e); + + } + bundleContext.registerService(DeviceStatusManagementService.class.getName(), deviceStatusManagemntService, null); + /* Registering Event Configuration Service */ EventConfigurationProviderService eventConfigurationService = new EventConfigurationProviderServiceImpl(); DeviceManagementDataHolder.getInstance().setEventConfigurationProviderService(eventConfigurationService); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java new file mode 100644 index 0000000000..1daea9fd24 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -0,0 +1,322 @@ +/* + * 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.core.metadata.mgt; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.AllowedDeviceStatus; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; +import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusConfigurations; +import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusItem; +import io.entgra.device.mgt.core.device.mgt.core.config.ui.UIConfigurationManager; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataDAO; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util.MetadataConstants; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.lang.reflect.Type; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + + +public class DeviceStatusManagementServiceImpl implements DeviceStatusManagementService { + + private static final Log log = LogFactory.getLog(DeviceStatusManagementServiceImpl.class); + + private final MetadataDAO metadataDAO; + + public DeviceStatusManagementServiceImpl() { + this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); + } + + @Override + public void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.beginTransaction(); + if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY) && !metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { + Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus()); + Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck()); + // Add default device status and device status check metadata entries + addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + } + MetadataManagementDAOFactory.commitTransaction(); + } catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while inserting default device status metadata entry."; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + + @Override + public void resetToDefaultDeviceStausFilter() throws MetadataManagementException { + + } + + @Override + public void updateDefaultDeviceStatusFilters(int tenantId, String deviceType, List deviceStatus) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.beginTransaction(); + // Retrieve the current device status metadata + Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + if (metadata != null) { + Gson gson = new Gson(); + Type listType = new TypeToken>() { + }.getType(); + List currentStatusList = gson.fromJson(metadata.getMetaValue(), listType); + + // Find the status for the specified deviceType + for (AllowedDeviceStatus status : currentStatusList) { + if (status.getType().equalsIgnoreCase(deviceType)) { + // Update the status list for the specified deviceType + status.setStatus(deviceStatus); + break; + } + } + metadata.setMetaValue(gson.toJson(currentStatusList)); + updateMetadataEntry(tenantId, metadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + } + MetadataManagementDAOFactory.commitTransaction(); + } catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while updating device status metadata entry."; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + + @Override + public boolean updateDefaultDeviceStatusCheck(int tenantId, boolean isChecked) throws MetadataManagementException { + boolean success = false; + try { + MetadataManagementDAOFactory.beginTransaction(); + if (metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { + Metadata isDeviceStatusChecked = constructDeviceStatusCheckMetadata(isChecked); + // Add default device status check metadata entries + updateMetadataEntry(tenantId, isDeviceStatusChecked, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + success = true; + } + MetadataManagementDAOFactory.commitTransaction(); + } catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while updating device status check metadata entry."; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + return success; + } + + @Override + public List getDeviceStatusFilters(int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.openConnection(); + Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + Gson gson = new Gson(); + Type listType = new TypeToken>() {}.getType(); + List statusList = gson.fromJson(metadata.getMetaValue(), listType); + + return statusList; + } catch (MetadataManagementDAOException e) { + String msg = "Error occurred while retrieving device status meta data for tenant:" + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + + } + + public List getDeviceStatusFilters(String deviceType, int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.openConnection(); + Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + Gson gson = new Gson(); + Type listType = new TypeToken>() {}.getType(); + List statusList = gson.fromJson(metadata.getMetaValue(), listType); + + for (AllowedDeviceStatus status : statusList) { + if (status.getType().equalsIgnoreCase(deviceType)) { + return status.getStatus(); + } + } + // Device type not found in metadata + return Collections.emptyList(); + } catch (MetadataManagementDAOException e) { + String msg = "Error occurred while retrieving device status meta data for tenant: " + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + + + @Override + public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.openConnection(); + Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + String metaValue = metadata.getMetaValue(); + return Boolean.parseBoolean(metaValue); + } catch (MetadataManagementDAOException e) { + String msg = "Error occurred while retrieving device status check meta data for tenant:" + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + + @Override + public boolean isDeviceStatusValid(String deviceType, String deviceStatus, int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.openConnection(); + Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + + Gson gson = new Gson(); + Type listType = new TypeToken>() { + }.getType(); + List statusList = gson.fromJson(metadata.getMetaValue(), listType); + + for (AllowedDeviceStatus status : statusList) { + if (status.getType().equalsIgnoreCase(deviceType)) { + List allowedStatus = status.getStatus(); + return allowedStatus.contains(deviceStatus); + } + } + + return false; // Device type not found in metadata + } catch (MetadataManagementDAOException e) { + String msg = "Error occurred while retrieving device status meta data for tenant: " + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + + private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException { + metadataDAO.addMetadata(tenantId, metadata); + if (log.isDebugEnabled()) { + log.debug(key + " metadata entry has been inserted successfully"); + } + } + + private void updateMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException { + metadataDAO.updateMetadata(tenantId, metadata); + if (log.isDebugEnabled()) { + log.debug(key + " metadata entry has been updated successfully"); + } + } + + private Metadata constructDeviceStatusMetadata(List deviceStatusItems) { + Gson gson = new Gson(); + String deviceStatusItemsJsonString = gson.toJson(deviceStatusItems); + + Metadata metadata = new Metadata(); + metadata.setMetaKey(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + metadata.setMetaValue(deviceStatusItemsJsonString); + + return metadata; + } + + private Metadata constructDeviceStatusCheckMetadata(boolean deviceStatusCheck) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("deviceStatusCheck", String.valueOf(deviceStatusCheck)); + Metadata metadata = new Metadata(); + metadata.setMetaKey(MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + metadata.setMetaValue(String.valueOf(deviceStatusCheck)); + + return metadata; + } + + private List getDefaultDeviceStatus() { + DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations(); + List deviceStatusItems = new ArrayList<>(); + + if (deviceStatusConfigurations != null) { + // Access the list of DeviceStatusItem objects + deviceStatusItems = deviceStatusConfigurations.getDeviceStatusItems(); + } else { + if (log.isDebugEnabled()) { + log.debug("DeviceStatusConfigurations is null."); + } + } + + return deviceStatusItems; + } + + private boolean getDefaultDeviceStatusCheck() { + DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations(); + boolean deviceStatusCheck = false; + + if (deviceStatusConfigurations != null) { + // Access the deviceStatusCheck + deviceStatusCheck = deviceStatusConfigurations.isDeviceStatusCheck(); + } else { + if (log.isDebugEnabled()) { + log.debug("DeviceStatusConfigurations is null."); + } + } + return deviceStatusCheck; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java index 644875efdb..5d2c44af42 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/dao/util/MetadataConstants.java @@ -20,4 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util; public class MetadataConstants { public static final String WHITELABEL_META_KEY = "whitelabel"; + public static final String ALLOWED_DEVICE_STATUS_META_KEY = "allowed_device_status"; + + public static final String IS_DEVICE_STATUS_CHECK_META_KEY = "device-status-check"; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 60750b454f..278912c25a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.core.service; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; import io.entgra.device.mgt.core.notification.logger.DeviceEnrolmentLogContext; import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceEnrolmentLoggerImpl; @@ -485,6 +486,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv Device currentDevice = this.getDevice(deviceIdentifier, false); DeviceManagementDAOFactory.beginTransaction(); device.setId(currentDevice.getId()); + DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder + .getInstance().getDeviceStatusManagementService(); if (device.getEnrolmentInfo().getId() == 0) { device.getEnrolmentInfo().setId(currentDevice.getEnrolmentInfo().getId()); } @@ -495,7 +498,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.setName(currentDevice.getName()); } deviceDAO.updateDevice(device, tenantId); - enrollmentDAO.updateEnrollment(device.getEnrolmentInfo(), tenantId); + int updatedRows = enrollmentDAO.updateEnrollment(device.getEnrolmentInfo(), tenantId); + boolean isEnableDeviceStatusCheck = deviceStatusManagementService.getDeviceStatusCheck(tenantId); + boolean isValidState = deviceStatusManagementService.isDeviceStatusValid(device.getType(),device.getEnrolmentInfo().getStatus().name(),tenantId); + if (updatedRows == 1 && !deviceStatusManagementService.getDeviceStatusCheck(tenantId)){ + enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); + } else if (updatedRows ==1 && isEnableDeviceStatusCheck && isValidState ) { + enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); + } DeviceManagementDAOFactory.commitTransaction(); log.info("Device enrolled successfully", deviceEnrolmentLogContextBuilder.setDeviceId(String.valueOf(currentDevice.getId())).setDeviceType(String.valueOf(currentDevice.getType())).setOwner(currentDevice.getEnrolmentInfo().getOwner()).setOwnership(String.valueOf(currentDevice.getEnrolmentInfo().getOwnership())).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).setUserName(userName).build()); @@ -603,7 +613,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); DeviceManagementDAOFactory.beginTransaction(); - enrollmentDAO.updateEnrollment(device.getEnrolmentInfo(), tenantId); + DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder + .getInstance().getDeviceStatusManagementService(); + int updatedRows = enrollmentDAO.updateEnrollment(device.getEnrolmentInfo(), tenantId); + boolean isEnableDeviceStatusCheck = deviceStatusManagementService.getDeviceStatusCheck(tenantId); + boolean isValidState = deviceStatusManagementService.isDeviceStatusValid(device.getType(),device.getEnrolmentInfo().getStatus().name(),tenantId); + if (updatedRows == 1 && !deviceStatusManagementService.getDeviceStatusCheck(tenantId)){ + enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); + } else if (updatedRows ==1 && isEnableDeviceStatusCheck && isValidState ) { + enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); + } deviceDAO.updateDevice(device, tenantId); DeviceManagementDAOFactory.commitTransaction(); this.removeDeviceFromCache(deviceId); @@ -3339,7 +3358,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv isDeviceUpdated = disenrollDevice(deviceIdentifier); } else { enrolmentInfo.setStatus(newStatus); - isDeviceUpdated = updateEnrollment(deviceId, enrolmentInfo, tenantId); + isDeviceUpdated = updateEnrollment(deviceId, enrolmentInfo, tenantId, deviceIdentifier); } this.removeDeviceFromCache(deviceIdentifier); return isDeviceUpdated; @@ -3370,7 +3389,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } - private boolean updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) + private boolean updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId, DeviceIdentifier deviceIdentifier) throws DeviceManagementException { if (log.isDebugEnabled()) { log.debug("Update enrollment of device: " + deviceId); @@ -3379,10 +3398,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.beginTransaction(); int updatedRows = enrollmentDAO.updateEnrollment(enrolmentInfo, tenantId); + String type = deviceIdentifier.getType(); + DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder + .getInstance().getDeviceStatusManagementService(); DeviceManagementDAOFactory.commitTransaction(); if (updatedRows > 0) { isUpdatedEnrollment = true; } + boolean isEnableDeviceStatusCheck = deviceStatusManagementService.getDeviceStatusCheck(tenantId); + boolean isValidState = deviceStatusManagementService.isDeviceStatusValid(type, enrolmentInfo.getStatus().name(), tenantId); + if (updatedRows == 1 && !deviceStatusManagementService.getDeviceStatusCheck(tenantId)) { + enrollmentDAO.addDeviceStatus(enrolmentInfo.getId(), enrolmentInfo.getStatus()); + } else if (updatedRows == 1 && isEnableDeviceStatusCheck && isValidState) { + enrollmentDAO.addDeviceStatus(enrolmentInfo.getId(), enrolmentInfo.getStatus()); + } + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while updating the enrollment information device for" + @@ -4411,13 +4441,25 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); // different try blocks are used to isolate transactions try { - enrollmentDAO.updateEnrollment(device.getEnrolmentInfo(), tenantId); + String type = device.getType(); + DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder + .getInstance().getDeviceStatusManagementService(); + int updatedRows = enrollmentDAO.updateEnrollment(device.getEnrolmentInfo(), tenantId); + boolean isEnableDeviceStatusCheck = deviceStatusManagementService.getDeviceStatusCheck(tenantId); + boolean isValidState = deviceStatusManagementService.isDeviceStatusValid(type, String.valueOf(EnrolmentInfo.Status.REMOVED),tenantId); + if (updatedRows == 1 && !deviceStatusManagementService.getDeviceStatusCheck(tenantId)){ + enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); + } else if (updatedRows ==1 && isEnableDeviceStatusCheck && isValidState ) { + enrollmentDAO.addDeviceStatus(device.getEnrolmentInfo().getId(), device.getEnrolmentInfo().getStatus()); + } } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while dis-enrolling device: " + device.getName(); log.error(msg, e); throw new DeviceManagementException(msg, e); + } catch (MetadataManagementException e) { + throw new RuntimeException(e); } try { deviceDAO.updateDevice(device, tenantId); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java index 6f078619ee..fd9958782b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java @@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.device.mgt.core.status.task.impl; import com.google.gson.Gson; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; @@ -33,6 +36,7 @@ import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOExceptio import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.status.task.DeviceStatusTaskException; import io.entgra.device.mgt.core.device.mgt.core.task.impl.DynamicPartitionedScheduleTask; +import org.wso2.carbon.context.CarbonContext; import java.sql.SQLException; import java.util.ArrayList; @@ -135,11 +139,26 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask { private boolean updateDeviceStatus(List enrolmentInfos) throws DeviceStatusTaskException { boolean updateStatus; + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder + .getInstance().getDeviceStatusManagementService(); try { DeviceManagementDAOFactory.beginTransaction(); updateStatus = DeviceManagementDAOFactory.getEnrollmentDAO().updateEnrollmentStatus(enrolmentInfos); + boolean isEnableDeviceStatusCheck = deviceStatusManagementService.getDeviceStatusCheck(tenantId); + if (updateStatus) { + for (EnrolmentInfo enrolmentInfo : enrolmentInfos) { + if (isEnableDeviceStatusCheck) { + if (deviceStatusManagementService.isDeviceStatusValid(this.deviceType, enrolmentInfo.getStatus().name(), tenantId)) { + DeviceManagementDAOFactory.getEnrollmentDAO().addDeviceStatus(enrolmentInfo); + } + } else { + DeviceManagementDAOFactory.getEnrollmentDAO().addDeviceStatus(enrolmentInfo); + } + } + } DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | MetadataManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceStatusTaskException("Error occurred while updating enrollment status of devices of type '" + deviceType + "'", e); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java index 3c7365936e..8d6a4a5673 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/search/DeviceDetails.java @@ -19,6 +19,8 @@ package io.entgra.device.mgt.core.device.mgt.core.search; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.annotations.BeforeClass; @@ -38,9 +40,11 @@ public class DeviceDetails extends BaseDeviceManagementTest { @Override public void init() throws Exception { - DeviceManagementProviderService deviceManagementProviderService = new DeviceManagementProviderServiceImpl(); - DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService); + DeviceManagementProviderService deviceManagementProviderService = new DeviceManagementProviderServiceImpl(); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService); + DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); + DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); } @Test diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java index e1bbe3ee76..942cb2c8b0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -18,6 +18,9 @@ package io.entgra.device.mgt.core.device.mgt.core.service; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.core.config.ui.UIConfigurationManager; import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -61,6 +64,7 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import java.io.File; import java.io.InputStream; import java.lang.reflect.Field; import java.sql.Timestamp; @@ -78,6 +82,8 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes private static final String ALTERNATE_DEVICE_ID = "1128"; private static final String DEVICE_TYPE = "RANDOM_DEVICE_TYPE"; private final DeviceDetailsDAO deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO(); + private static final String MDM_CONFIG_LOCATION = "src" + File.separator + "test" + File.separator + "resources" + + File.separator + "config" + File.separator + "operation" + File.separator + "mdm-ui-config.xml"; DeviceManagementProviderService deviceMgtService; @@ -96,6 +102,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); + UIConfigurationManager.getInstance().initConfig(MDM_CONFIG_LOCATION); } private RegistryService getRegistryService() throws RegistryException { @@ -204,8 +211,11 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) - public void testReEnrollmentofSameDeviceUnderSameUser() throws DeviceManagementException { + public void testReEnrollmentofSameDeviceUnderSameUser() throws DeviceManagementException, MetadataManagementException { if (!isMock()) { + DeviceStatusManagementService deviceStatusManagementService = DeviceManagementDataHolder + .getInstance().getDeviceStatusManagementService(); + deviceStatusManagementService.addDefaultDeviceStatusFilterIfNotExist(MultitenantConstants.SUPER_TENANT_ID); Device device = TestDataHolder.generateDummyDeviceData(new DeviceIdentifier(DEVICE_ID, DEVICE_TYPE)); boolean enrollment = deviceMgtService.enrollDevice(device); Assert.assertTrue(enrollment); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java index 335ba0dc25..e232bffe44 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerTest.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.core.task; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -68,7 +69,7 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { private OperationManager operationManager; @BeforeClass - public void init() throws DeviceManagementException, RegistryException { + public void init() throws DeviceManagementException, RegistryException, MetadataManagementException { log.info("Initializing Device Task Manager Test Suite"); this.deviceIds = new ArrayList<>(); for (int i = 0; i < 5; i++) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/config/operation/mdm-ui-config.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/config/operation/mdm-ui-config.xml new file mode 100644 index 0000000000..8b5eba6efa --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/config/operation/mdm-ui-config.xml @@ -0,0 +1,410 @@ + + + + + true + true + + 3600 + + 10000 + + + false + tracking_url + + access_token + sender_actorId + + + false + true + false + true + false + true + + + + name + label_device + default + + + type + label_type + default + + + owner + label_owner + default + + + serial + label_serialNumber + default + + + ownership + label_ownership + default + + + status + label_status + default + + + dateOfLastUpdate + label_last_updated + default + + + actions + label_actions + default + + + batteryLevel + label_battery_leve + deviceInfo + + + deviceModel + label_deviceModel + deviceInfo + + + osVersion + label_os_version + deviceInfo + + + vendor + label_vendor + deviceInfo + + + availableRAMMemory + label_available_ram_memory + deviceInfo + + + connectionType + label_connection_type + deviceInfo + + + cpuUsage + label_cpu_usage + deviceInfo + + + externalAvailableMemory + label_external_available_memory + deviceInfo + + + externalTotalMemory + label_external_tot_memory + deviceInfo + + + internalAvailableMemory + label_internal_available_memory + deviceInfo + + + internalTotalMemory + label_internal_tot_memory + deviceInfo + + + osBuildDate + label_os_build_date + deviceInfo + + + pluggedIn + label_plugged_in + deviceInfo + + + ssid + label_ssid + deviceInfo + + + totalRAMMemory + label_tot_ram_memory + deviceInfo + + + updatedTime + label_updated_time + deviceInfo + + + + true + + android + + ACTIVE + CREATED + INACTIVE + UNREACHABLE + UNCLAIMED + SUSPENDED + BLOCKED + REMOVED + SUSPENDED + DISENROLLMENT_REQUESTED + + + + ios + + ACTIVE + CREATED + INACTIVE + UNREACHABLE + UNCLAIMED + SUSPENDED + BLOCKED + REMOVED + SUSPENDED + DISENROLLMENT_REQUESTED + + + + windows + + ACTIVE + CREATED + INACTIVE + UNREACHABLE + UNCLAIMED + SUSPENDED + BLOCKED + REMOVED + SUSPENDED + DISENROLLMENT_REQUESTED + + + + + + analytics_management + application_management + device_management + subscription_management + review_management + + true + + + grafana:api:view + am:store:app:review:view + am:store:app:review:update + am:pub:sp:app:view + am:pub:sp:create + am:pub:sp:attach + am:pub:sp:detach + am:pub:sp:connect + am:pub:app:view + am:pub:app:update + am:store:app:view + am:store:app:modify + am:store:app:sub:install + am:store:app:sub:uninstall + am:admin:pub:app:review:view + am:admin:pub:app:update + am:admin:store:app:review:update + am:admin:store:app:sub:view + am:admin:store:app:sub:modify + dm:device-type:view + and:enterprise:modify + and:enterprise:view + dm:sign-csr + dm:admin:devices:view + dm:devices:status:change + rm:roles:add + rm:users:add + rm:roles:update + rm:roles:permissions:view + rm:roles:details:view + rm:roles:view + rm:roles:combined:add + rm:roles:delete + dm:activity:get + dm:devices:delete + dm:devices:app:view + dm:devices:policy:view + dm:devices:compliance:view + dm:devices:features:view + dm:devices:ops:view + dm:devices:search + dm:devices:details + dm:devices:update + dm:devices:view + dm:devices:enrollment-guide:view + dm:conf:view + dm:conf:manage + pm:policies:remove + pm:policies:priorities:update + pm:policies:deactivate + pm:policies:details:view + pm:policies:add + pm:policies:activate + pm:policies:update + pm:policies:change + dm:policies:view + um:users:add + um:users:details:view + um:users:count + um:users:delete + um:roles:view + um:users:user-details:view + um:users:cred:change + um:users:search + um:users:is-exist + um:users:update + um:users:invite + um:admin:users:view + dm:admin:enrollment:update + gm:devices:view + gm:groups:update + gm:groups:add + gm:groups:device:view + gm:devices:count + gm:devices-types:view + gm:groups:remove + gm:groups:view + gm:groups:groups-view + gm:roles:share + gm:groups:count + gm:roles:view + gm:devices:remove + gm:devices:add + gm:devices:assign + dm:device-type:conf:view + dm:device-type:features:view + dm:device-type:view + am:admin:app:install + am:admin:app:uninstall + gm:admin:groups:count + gm:admin:groups:view + gm:admin:groups:add + dm:notif:mark-checked + dm:notifications:view + cm:cert:delete + cm:cert:details:get + cm:cert:view + cm:cert:add + cm:cert:verify + dm:admin + dm:device-type:deploy + dm:device-type:event:modify + dm:device-type:event:view + dm:admin:device-type:modify + dm:admin:device-type:view + dm:admin:device-type:conf:add + dm:device:enroll + dm:geo:an:view + dm:geo:alerts:manage + dm:admin:devices:permanent-delete + and:conf:manage + and:conf:view + um:users:permission:view + ios:conf:view + ios:conf:manage + ios:dep:view + ios:dep:add + win:conf:view + win:conf:manage + and:ops:lock-devices + and:ops:unlock-devices + and:ops:location + and:ops:clear-password + and:ops:control-camera + and:ops:enterprise-wipe + and:ops:wipe + and:ops:ring + and:ops:app-list + and:ops:reboot + and:ops:change-LockTask + and:ops:mute + and:ops:conf-display-msg + and:ops:send-app-restrictions + and:ops:file-transfer + and:ops:set-webclip + and:ops:password-policy + and:ops:change-lock-code + and:ops:upgrade-firmware + and:ops:send-notif + dm:geo:geo-fence:manage + dm:whitelable:view + dm:whitelable:update + dm:metadata:view + dm:metadata:create + dm:metadata:update + and:ops:add-google-acc + and:ops:authenticate-acc + and:ops:update-default-sim + and:ops:add-google-acc + and:ops:device-info + win:ops:lock-devices + win:devices:enroll + win:ops:disenroll + win:ops:wipe + win:ops:ring + win:ops:lock-reset + win:ops:reboot + win:ops:location + admin:tenant:view + dm:admin:devices:usage:view + and:ops:clear-app + and:ops:suspend-package + and:ops:alternate-install + ios:ops:lock + ios:ops:location + ios:ops:ring + ios:ops:clear-passcode + ios:ops:enterprise-wipe + ios:ops:notif + ios:ops:wipe + ios:ops:boolean-setting + ios:ops:wallpaper + ios:ops:app-attributes + ios:ops:app-conf + mac:ops:restart + mac:ops:shutdown + am:store:vpp:user:modify + am:store:vpp:user:view + am:store:vpp:assets:modify + am:store:vpp:assets:view + and:devices:enroll + ios:devices:enroll + + + device-mgt + + \ No newline at end of file diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql index b4d2bde228..94c94106b5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql @@ -670,3 +670,15 @@ CREATE TABLE IF NOT EXISTS DM_EXT_PERMISSION_MAPPING ( TRACCAR_USER_ID INT DEFAULT 0 ); -- END OF DM_EXT_PERMISSION_MAPPING TABLE-- + +-- METADATA TABLE -- +CREATE TABLE IF NOT EXISTS DM_METADATA ( + METADATA_ID INT AUTO_INCREMENT NOT NULL, + DATA_TYPE VARCHAR(16) NOT NULL, + METADATA_KEY VARCHAR(128) NOT NULL, + METADATA_VALUE TEXT NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (METADATA_ID), + CONSTRAINT METADATA_KEY_TENANT_ID UNIQUE (METADATA_KEY, TENANT_ID) + ); +-- END OF METADATA TABLE -- diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java index c8af337243..a43f20f24c 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java @@ -24,4 +24,6 @@ public interface TenantManagerService { void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException; void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; + + void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException; } \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java index 1a6eeff68a..97eb706be7 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java @@ -36,4 +36,11 @@ public interface TenantManager { * @throws TenantMgtException Throws when error occurred while adding default application categories */ void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; + + /** + * Add default device status filters to a tenant described by the tenant info bean + * @param tenantInfoBean The info bean that provides tenant info + * @throws TenantMgtException Throws when error occurred while adding default application categories + */ + void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException; } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java index edfb95360b..5dc2eb7f08 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java @@ -98,6 +98,21 @@ public class TenantManagerImpl implements TenantManager { } } + @Override + public void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException { + initTenantFlow(tenantInfoBean); + try { + TenantMgtDataHolder.getInstance().getDeviceStatusManagementService(). + addDefaultDeviceStatusFilterIfNotExist(tenantInfoBean.getTenantId()); + } catch (MetadataManagementException e) { + String msg = "Error occurred while adding default device status filter"; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } finally { + endTenantFlow(); + } + } + private void initTenantFlow(TenantInfoBean tenantInfoBean) { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java index 79de1b0000..da21a52100 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java @@ -33,4 +33,9 @@ public class TenantManagerServiceImpl implements TenantManagerService { public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException { TenantMgtDataHolder.getInstance().getTenantManager().addDefaultAppCategories(tenantInfoBean); } + + @Override + public void addDefaultDeviceStatusFilters(TenantInfoBean tenantInfoBean) throws TenantMgtException { + TenantMgtDataHolder.getInstance().getTenantManager().addDefaultDeviceStatusFilters(tenantInfoBean); + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java index 7c34b3c177..9658d68848 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.WhiteLabelManagementService; import org.wso2.carbon.user.core.service.RealmService; @@ -32,6 +33,8 @@ public class TenantMgtDataHolder { private RealmService realmService; + private DeviceStatusManagementService deviceStatusManagementService; + public RealmService getRealmService() { return realmService; } @@ -67,4 +70,12 @@ public class TenantMgtDataHolder { public static TenantMgtDataHolder getInstance() { return instance; } + + public DeviceStatusManagementService getDeviceStatusManagementService() { + return deviceStatusManagementService; + } + + public void setDeviceStatusManagementService(DeviceStatusManagementService deviceStatusManagementService) { + this.deviceStatusManagementService = deviceStatusManagementService; + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java index 7c7210213c..fa2f26c972 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; +import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerService; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerImpl; @@ -64,6 +66,10 @@ public class TenantMgtServiceComponent { componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(), whiteLabelManagementService, null); TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); + DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); + componentContext.getBundleContext().registerService(DeviceStatusManagementService.class.getName(), + deviceStatusManagementService, null); + TenantMgtDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener(); if(log.isDebugEnabled()) { log.info("Tenant management listener is registering"); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java index 40b58efe7e..978e69b0e4 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -38,6 +38,7 @@ public class DeviceMgtTenantListener implements TenantMgtListener { try { tenantManager.addDefaultRoles(tenantInfoBean); tenantManager.addDefaultAppCategories(tenantInfoBean); + tenantManager.addDefaultDeviceStatusFilters(tenantInfoBean); } catch (TenantMgtException e) { String msg = "Error occurred while executing tenant creation flow"; log.error(msg, e); diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml index 416c0711f3..50e75857bd 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml @@ -162,6 +162,54 @@ deviceInfo + + false + + android + + ACTIVE + CREATED + INACTIVE + UNREACHABLE + UNCLAIMED + SUSPENDED + BLOCKED + REMOVED + SUSPENDED + DISENROLLMENT_REQUESTED + + + + ios + + ACTIVE + CREATED + INACTIVE + UNREACHABLE + UNCLAIMED + SUSPENDED + BLOCKED + REMOVED + SUSPENDED + DISENROLLMENT_REQUESTED + + + + windows + + ACTIVE + CREATED + INACTIVE + UNREACHABLE + UNCLAIMED + SUSPENDED + BLOCKED + REMOVED + SUSPENDED + DISENROLLMENT_REQUESTED + + + analytics_management @@ -315,6 +363,8 @@ dm:geo:geo-fence:manage dm:whitelable:view dm:whitelable:update + dm:devicestatusfilter:view + dm:devicestatusfilter:update dm:metadata:view dm:metadata:create dm:metadata:update