From 194e4d99b59ba2199ed95b4ced1bf23af503cf84 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Mon, 5 Jun 2017 00:12:59 +0530 Subject: [PATCH] added policy compliance support --- .../pom.xml | 8 ++ .../PullNotificationSubscriberImpl.java | 59 +++++++++++++- .../internal/PullNotificationDataHolder.java | 9 +++ .../PullNotificationServiceComponent.java | 17 ++++- .../service/impl/DeviceAgentServiceImpl.java | 76 ++++++++++++++----- 5 files changed, 147 insertions(+), 22 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml index 7f6e86b116..e6e557ada9 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/pom.xml @@ -54,6 +54,10 @@ org.eclipse.osgi org.eclipse.osgi.services + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.core + @@ -82,6 +86,10 @@ org.apache.commons.logging, org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.core.service + org.wso2.carbon.policy.mgt.core.*, + org.wso2.carbon.policy.mgt.core, + com.google.gson, + org.wso2.carbon.device.mgt.core.service.* diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java index e757b38fe2..3ee6cb5c4c 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/PullNotificationSubscriberImpl.java @@ -18,19 +18,35 @@ */ package org.wso2.carbon.device.mgt.extensions.pull.notification; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; +import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.extensions.pull.notification.internal.PullNotificationDataHolder; +import java.util.ArrayList; +import java.util.List; import java.util.Map; public class PullNotificationSubscriberImpl implements PullNotificationSubscriber { + public final class OperationCodes { + private OperationCodes() { + throw new AssertionError(); + } + public static final String POLICY_MONITOR = "POLICY_MONITOR"; + } + + private static final Log log = LogFactory.getLog(PullNotificationSubscriberImpl.class); public void init(Map properties) { @@ -40,14 +56,53 @@ public class PullNotificationSubscriberImpl implements PullNotificationSubscribe @Override public void execute(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException { try { - PullNotificationDataHolder.getInstance().getDeviceManagementProviderService().updateOperation( - deviceIdentifier, operation); + if (!Operation.Status.ERROR.equals(operation.getStatus()) && operation.getCode() != null && + OperationCodes.POLICY_MONITOR.equals(operation.getCode())) { + if (log.isDebugEnabled()) { + log.info("Received compliance status from POLICY_MONITOR operation ID: " + operation.getId()); + } + List features = getComplianceFeatures(operation.getPayLoad()); + PullNotificationDataHolder.getInstance().getPolicyManagerService() + .checkCompliance(deviceIdentifier, features); + + } else { + PullNotificationDataHolder.getInstance().getDeviceManagementProviderService().updateOperation( + deviceIdentifier, operation); + } } catch (OperationManagementException e) { throw new PullNotificationExecutionFailedException(e); + } catch (PolicyComplianceException e) { + throw new PullNotificationExecutionFailedException("Invalid payload format compliant feature", e); } } public void clean() { } + + private static List getComplianceFeatures(Object compliancePayload) throws + PolicyComplianceException { + String compliancePayloadString = new Gson().toJson(compliancePayload); + if (compliancePayload == null) { + return null; + } + // Parsing json string to get compliance features. + JsonElement jsonElement; + if (compliancePayloadString instanceof String) { + jsonElement = new JsonParser().parse(compliancePayloadString); + } else { + throw new PolicyComplianceException("Invalid policy compliance payload"); + } + + JsonArray jsonArray = jsonElement.getAsJsonArray(); + Gson gson = new Gson(); + ComplianceFeature complianceFeature; + List complianceFeatures = new ArrayList(jsonArray.size()); + + for (JsonElement element : jsonArray) { + complianceFeature = gson.fromJson(element, ComplianceFeature.class); + complianceFeatures.add(complianceFeature); + } + return complianceFeatures; + } } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java index af27888216..a9f7888c43 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationDataHolder.java @@ -19,10 +19,12 @@ package org.wso2.carbon.device.mgt.extensions.pull.notification.internal; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.policy.mgt.core.PolicyManagerService; public class PullNotificationDataHolder { private DeviceManagementProviderService deviceManagementProviderService; + private PolicyManagerService policyManagerService; private static PullNotificationDataHolder thisInstance = new PullNotificationDataHolder(); @@ -38,4 +40,11 @@ public class PullNotificationDataHolder { this.deviceManagementProviderService = deviceManagementProviderService; } + public PolicyManagerService getPolicyManagerService() { + return policyManagerService; + } + + public void setPolicyManagerService(PolicyManagerService policyManagerService) { + this.policyManagerService = policyManagerService; + } } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java index 2be614a772..351e514706 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.pull.notification/src/main/java/org/wso2/carbon/device/mgt/extensions/pull/notification/internal/PullNotificationServiceComponent.java @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.policy.mgt.core.PolicyManagerService; /** * @scr.component name="org.wso2.carbon.device.mgt.extensions.pull.notification.internal.PullNotificationServiceComponent" immediate="true" @@ -31,6 +32,12 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; * policy="dynamic" * bind="setDeviceManagementProviderService" * unbind="unsetDeviceManagementProviderService" + * @scr.reference name="org.wso2.carbon.policy.mgt.core" + * interface="org.wso2.carbon.policy.mgt.core.PolicyManagerService" + * cardinality="1..1" + * policy="dynamic" + * bind="setPolicyManagerService" + * unbind="unsetPolicyManagerService" */ public class PullNotificationServiceComponent { @@ -59,7 +66,15 @@ public class PullNotificationServiceComponent { } protected void unsetDeviceManagementProviderService(DeviceManagementProviderService deviceManagementProviderService) { - PullNotificationDataHolder.getInstance().setDeviceManagementProviderService(deviceManagementProviderService); + PullNotificationDataHolder.getInstance().setDeviceManagementProviderService(null); + } + + protected void setPolicyManagerService(PolicyManagerService policyManagerService) { + PullNotificationDataHolder.getInstance().setPolicyManagerService(policyManagerService); + } + + protected void unsetPolicyManagerService(PolicyManagerService policyManagerService) { + PullNotificationDataHolder.getInstance().setPolicyManagerService(null); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java index 3bd3f85329..0813ee50dc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java @@ -18,6 +18,10 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import org.apache.axis2.AxisFault; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -31,6 +35,9 @@ import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorization import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; +import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList; @@ -63,7 +70,7 @@ import java.util.Map; @Path("/device/agent") public class DeviceAgentServiceImpl implements DeviceAgentService { private static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class); - + private static final String POLICY_MONITOR = "POLICY_MONITOR"; @POST @Path("/enroll") @Override @@ -323,13 +330,11 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving deivce management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -354,13 +359,11 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving deivce management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -384,19 +387,30 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { log.error(msg); return Response.status(Response.Status.NO_CONTENT).entity(msg).build(); } - DeviceMgtAPIUtils.getDeviceManagementService().updateOperation - (deviceIdentifier, operation); + if (!Operation.Status.ERROR.equals(operation.getStatus()) && operation.getCode() != null && + POLICY_MONITOR.equals(operation.getCode())) { + if (log.isDebugEnabled()) { + log.info("Received compliance status from POLICY_MONITOR operation ID: " + operation.getId()); + } + List features = getComplianceFeatures(operation.getPayLoad()); + DeviceMgtAPIUtils.getPolicyManagementService().checkCompliance(deviceIdentifier, features); + + } else { + DeviceMgtAPIUtils.getDeviceManagementService().updateOperation(deviceIdentifier, operation); + } return Response.status(Response.Status.ACCEPTED).build(); } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving deivce management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + } catch (PolicyComplianceException e) { + String errorMessage = "Issue in retrieving deivce management service instance"; + log.error(errorMessage, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -425,13 +439,37 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } catch (DeviceManagementException e) { String errorMessage = "Issue in retrieving device management service"; log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + } + } + + private static List getComplianceFeatures(Object compliancePayload) throws + PolicyComplianceException { + String compliancePayloadString = new Gson().toJson(compliancePayload); + if (compliancePayload == null) { + return null; + } + // Parsing json string to get compliance features. + JsonElement jsonElement; + if (compliancePayloadString instanceof String) { + jsonElement = new JsonParser().parse(compliancePayloadString); + } else { + throw new PolicyComplianceException("Invalid policy compliance payload"); + } + + JsonArray jsonArray = jsonElement.getAsJsonArray(); + Gson gson = new Gson(); + ComplianceFeature complianceFeature; + List complianceFeatures = new ArrayList(jsonArray.size()); + + for (JsonElement element : jsonArray) { + complianceFeature = gson.fromJson(element, ComplianceFeature.class); + complianceFeatures.add(complianceFeature); } + return complianceFeatures; } }