added policy compliance support

revert-70aa11f8
ayyoob 7 years ago
parent 5b46dc03a5
commit 194e4d99b5

@ -54,6 +54,10 @@
<groupId>org.eclipse.osgi</groupId> <groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi.services</artifactId> <artifactId>org.eclipse.osgi.services</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.core</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -82,6 +86,10 @@
org.apache.commons.logging, org.apache.commons.logging,
org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.device.mgt.core.service 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.*
</Import-Package> </Import-Package>
</instructions> </instructions>
</configuration> </configuration>

@ -18,19 +18,35 @@
*/ */
package org.wso2.carbon.device.mgt.extensions.pull.notification; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; 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.PullNotificationExecutionFailedException;
import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber;
import org.wso2.carbon.device.mgt.extensions.pull.notification.internal.PullNotificationDataHolder; import org.wso2.carbon.device.mgt.extensions.pull.notification.internal.PullNotificationDataHolder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
public class PullNotificationSubscriberImpl implements PullNotificationSubscriber { 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); private static final Log log = LogFactory.getLog(PullNotificationSubscriberImpl.class);
public void init(Map<String, String> properties) { public void init(Map<String, String> properties) {
@ -40,14 +56,53 @@ public class PullNotificationSubscriberImpl implements PullNotificationSubscribe
@Override @Override
public void execute(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException { public void execute(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException {
try { try {
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<ComplianceFeature> features = getComplianceFeatures(operation.getPayLoad());
PullNotificationDataHolder.getInstance().getPolicyManagerService()
.checkCompliance(deviceIdentifier, features);
} else {
PullNotificationDataHolder.getInstance().getDeviceManagementProviderService().updateOperation( PullNotificationDataHolder.getInstance().getDeviceManagementProviderService().updateOperation(
deviceIdentifier, operation); deviceIdentifier, operation);
}
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
throw new PullNotificationExecutionFailedException(e); throw new PullNotificationExecutionFailedException(e);
} catch (PolicyComplianceException e) {
throw new PullNotificationExecutionFailedException("Invalid payload format compliant feature", e);
} }
} }
public void clean() { public void clean() {
} }
private static List<ComplianceFeature> 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<ComplianceFeature> complianceFeatures = new ArrayList<ComplianceFeature>(jsonArray.size());
for (JsonElement element : jsonArray) {
complianceFeature = gson.fromJson(element, ComplianceFeature.class);
complianceFeatures.add(complianceFeature);
}
return complianceFeatures;
}
} }

@ -19,10 +19,12 @@
package org.wso2.carbon.device.mgt.extensions.pull.notification.internal; package org.wso2.carbon.device.mgt.extensions.pull.notification.internal;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
public class PullNotificationDataHolder { public class PullNotificationDataHolder {
private DeviceManagementProviderService deviceManagementProviderService; private DeviceManagementProviderService deviceManagementProviderService;
private PolicyManagerService policyManagerService;
private static PullNotificationDataHolder thisInstance = new PullNotificationDataHolder(); private static PullNotificationDataHolder thisInstance = new PullNotificationDataHolder();
@ -38,4 +40,11 @@ public class PullNotificationDataHolder {
this.deviceManagementProviderService = deviceManagementProviderService; this.deviceManagementProviderService = deviceManagementProviderService;
} }
public PolicyManagerService getPolicyManagerService() {
return policyManagerService;
}
public void setPolicyManagerService(PolicyManagerService policyManagerService) {
this.policyManagerService = policyManagerService;
}
} }

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext; import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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" * @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" * policy="dynamic"
* bind="setDeviceManagementProviderService" * bind="setDeviceManagementProviderService"
* unbind="unsetDeviceManagementProviderService" * 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 { public class PullNotificationServiceComponent {
@ -59,7 +66,15 @@ public class PullNotificationServiceComponent {
} }
protected void unsetDeviceManagementProviderService(DeviceManagementProviderService deviceManagementProviderService) { 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);
} }
} }

@ -18,6 +18,10 @@
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl; 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.axis2.AxisFault;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.authorization.DeviceAccessAuthorizationService;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; 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.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.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationList;
@ -63,7 +70,7 @@ import java.util.Map;
@Path("/device/agent") @Path("/device/agent")
public class DeviceAgentServiceImpl implements DeviceAgentService { public class DeviceAgentServiceImpl implements DeviceAgentService {
private static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class); private static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class);
private static final String POLICY_MONITOR = "POLICY_MONITOR";
@POST @POST
@Path("/enroll") @Path("/enroll")
@Override @Override
@ -323,13 +330,11 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String errorMessage = "Issue in retrieving operation management service instance"; String errorMessage = "Issue in retrieving operation management service instance";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String errorMessage = "Issue in retrieving deivce management service instance"; String errorMessage = "Issue in retrieving deivce management service instance";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
} }
} }
@ -354,13 +359,11 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String errorMessage = "Issue in retrieving operation management service instance"; String errorMessage = "Issue in retrieving operation management service instance";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String errorMessage = "Issue in retrieving deivce management service instance"; String errorMessage = "Issue in retrieving deivce management service instance";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
} }
} }
@ -384,19 +387,30 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
log.error(msg); log.error(msg);
return Response.status(Response.Status.NO_CONTENT).entity(msg).build(); return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
} }
DeviceMgtAPIUtils.getDeviceManagementService().updateOperation if (!Operation.Status.ERROR.equals(operation.getStatus()) && operation.getCode() != null &&
(deviceIdentifier, operation); POLICY_MONITOR.equals(operation.getCode())) {
if (log.isDebugEnabled()) {
log.info("Received compliance status from POLICY_MONITOR operation ID: " + operation.getId());
}
List<ComplianceFeature> features = getComplianceFeatures(operation.getPayLoad());
DeviceMgtAPIUtils.getPolicyManagementService().checkCompliance(deviceIdentifier, features);
} else {
DeviceMgtAPIUtils.getDeviceManagementService().updateOperation(deviceIdentifier, operation);
}
return Response.status(Response.Status.ACCEPTED).build(); return Response.status(Response.Status.ACCEPTED).build();
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
String errorMessage = "Issue in retrieving operation management service instance"; String errorMessage = "Issue in retrieving operation management service instance";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String errorMessage = "Issue in retrieving deivce management service instance"; String errorMessage = "Issue in retrieving deivce management service instance";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).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) { } catch (OperationManagementException e) {
String errorMessage = "Issue in retrieving operation management service instance"; String errorMessage = "Issue in retrieving operation management service instance";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String errorMessage = "Issue in retrieving device management service"; String errorMessage = "Issue in retrieving device management service";
log.error(errorMessage, e); log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build(); }
}
private static List<ComplianceFeature> 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<ComplianceFeature> complianceFeatures = new ArrayList<ComplianceFeature>(jsonArray.size());
for (JsonElement element : jsonArray) {
complianceFeature = gson.fromJson(element, ComplianceFeature.class);
complianceFeatures.add(complianceFeature);
} }
return complianceFeatures;
} }
} }

Loading…
Cancel
Save