From 33b79dcbba104c1424c846f3d1c2dea09e845330 Mon Sep 17 00:00:00 2001 From: Supun94 Date: Mon, 7 Nov 2016 17:25:28 +0530 Subject: [PATCH] Adding effective policy service --- .../service/api/PolicyManagementService.java | 4 +++ .../impl/PolicyManagementServiceImpl.java | 25 +++++++++++++++++ .../decision/point/SimpleEvaluationPoint.java | 28 +++++++++++++------ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java index 6a77938b0fe..9226fd61dca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java @@ -562,5 +562,9 @@ public interface PolicyManagementService { required = true) List priorityUpdatedPolicies); + @GET + @Path("/effective-policy/{deviceType}/{deviceId}") + @Permission(name = "Get Effective Policy of Devices", permission = "/device-mgt/policies/view") + Response getEffectivePolicy(@PathParam("deviceId") String deviceId, @PathParam("deviceType") String deviceType); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index 16e7a8eb062..be9d318e3ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -373,4 +373,29 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } } + @GET + @Path("/effective-policy/{deviceType}/{deviceId}") + @Override + public Response getEffectivePolicy(@PathParam("deviceId") String deviceId, @PathParam("deviceType") String deviceType) { + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + final org.wso2.carbon.policy.mgt.common.Policy policy; + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(deviceType); + policy = policyManagementService.getEffectivePolicy(deviceIdentifier); + if (policy == null) { + return Response.status(Response.Status.NOT_FOUND).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage( + "No policy found for device ID '" + deviceId + "'"+ deviceId).build()).build(); + } + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving policy corresponding to the id '" + deviceType + "'"+ deviceId; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + return Response.status(Response.Status.OK).entity(policy).build(); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java index 1519fea1c51..137d95c3aaf 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationPoint.java @@ -33,6 +33,7 @@ public class SimpleEvaluationPoint implements PolicyEvaluationPoint { //TODO : to revove the stale reference private PolicyManagerService policyManagerService; private List policyList; + PIPDevice pipDevice; // public SimpleEvaluationPoint() { // policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService(); // } @@ -73,15 +74,26 @@ public class SimpleEvaluationPoint implements PolicyEvaluationPoint { @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { - if (policyManagerService == null || policyList.size() == 0) { - return null; + try { + policyManagerService = getPolicyManagerService(); + PolicyInformationPoint policyInformationPoint = policyManagerService.getPIP(); + pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier); + policyList = policyInformationPoint.getRelatedPolicies(pipDevice); + + if (policyManagerService == null || policyList.size() == 0) { + return null; + } + + Policy policy = new Policy(); + Profile profile = new Profile(); + profile.setProfileFeaturesList(getEffectiveFeatures(deviceIdentifier)); + policy.setProfile(profile); + return policy; + } catch (PolicyManagementException e) { + String msg = "Error occurred when retrieving the policy related data from policy management service."; + log.error(msg, e); + throw new PolicyEvaluationException(msg, e); } - - Policy policy = new Policy(); - Profile profile = new Profile(); - profile.setProfileFeaturesList(getEffectiveFeatures(deviceIdentifier)); - policy.setProfile(profile); - return policy; } private Policy policyResolve(List policyList) throws PolicyEvaluationException, PolicyManagementException {