From 68b7d304a03e9c4da194c1155c6d1e9dab49ede2 Mon Sep 17 00:00:00 2001 From: madhawap Date: Wed, 6 Jul 2016 18:40:49 +0530 Subject: [PATCH] Added end point to get Compliance Data Of a Device --- .../mgt/jaxrs/beans/DeviceCompliance.java | 55 +++++++++++++++++++ .../service/api/DeviceManagementService.java | 42 ++++++++++++++ .../impl/DeviceManagementServiceImpl.java | 47 ++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceCompliance.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceCompliance.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceCompliance.java new file mode 100644 index 0000000000..ce76a63c25 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceCompliance.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.device.mgt.jaxrs.beans; + +import io.swagger.annotations.ApiModel; +import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; + +@ApiModel(value = "DeviceCompliance", description = "Device's policy compliance status") +public class DeviceCompliance { + + private int deviceID; + private ComplianceData complianceData; + private Long code; + + public ComplianceData getComplianceData() { + return complianceData; + } + + public void setComplianceData(ComplianceData complianceData) { + this.complianceData = complianceData; + } + + public Long getCode() { + return code; + } + + public void setCode(Long code) { + this.code = code; + } + + public int getDeviceID() { + return deviceID; + } + + public void setDeviceID(int deviceID) { + this.deviceID = deviceID; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 7f90c9b3c0..f10a95e3ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -667,4 +668,45 @@ public interface DeviceManagementService { @HeaderParam("If-Modified-Since") String ifModifiedSince); + + + @GET + @Path("{type}/{id}/compliance-data") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get the effective policy calculated for a device.", + notes = "When a device registers with WSO2 EMM a policy is enforced on the device. Initially the " + + "EMM filters the policies based on the Platform (device type), filters based on the " + + "device ownership type , filters based on the user role or name and finally the policy" + + " is enforced on the device.", + tags = "Device Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK", + response = ComplianceData.class), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Error occurred while getting the compliance data.", + response = ErrorResponse.class) + } + ) + Response getComplianceDataOfDevice( + @ApiParam( + name = "type", + value = "The device type, such as ios, android or windows.", + required = true) + @PathParam("type") + String type, + @ApiParam( + name = "id", + value = "Device Identifier", + required = true) + @PathParam("id") String id); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index edac09a140..1f14e590a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServ import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceCompliance; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList; @@ -38,6 +39,8 @@ import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import javax.ws.rs.*; @@ -301,4 +304,48 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } + @GET + @Path("{type}/{id}/compliance-data") + public Response getComplianceDataOfDevice(@PathParam("type") String type, + @PathParam("id") String id) { + + RequestValidationUtil.validateDeviceIdentifier(type, id); + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + Policy policy; + ComplianceData complianceData = null; + DeviceCompliance deviceCompliance = new DeviceCompliance(); + + try { + policy = policyManagementService.getAppliedPolicyToDevice(new DeviceIdentifier(id, type)); + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving the current policy associated with the '" + type + + "' device, which carries the id '" + id + "'"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + + if (policy == null) { + deviceCompliance.setDeviceID(Integer.valueOf(id)); + deviceCompliance.setComplianceData(null); + deviceCompliance.setCode(0001l); + return Response.status(Response.Status.OK).entity(deviceCompliance).build(); + } else { + try { + policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + complianceData = policyManagementService.getDeviceCompliance( + new DeviceIdentifier(id, type)); + deviceCompliance.setDeviceID(Integer.valueOf(id)); + deviceCompliance.setComplianceData(complianceData); + deviceCompliance.setCode(0002l); + return Response.status(Response.Status.OK).entity(deviceCompliance).build(); + } catch (PolicyComplianceException e) { + String error = "Error occurred while getting the compliance data."; + log.error(error, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(error).build()).build(); + } + } + } + }