Add geofence policy transform

corrective-policy
Pahansith 4 years ago
parent de4f3f64fe
commit d06fc0e5f3

@ -68,6 +68,7 @@ public final class PolicyManagementConstants {
public static final String POLICY_FEATURE_CODE = "POLICY_ACTION"; public static final String POLICY_FEATURE_CODE = "POLICY_ACTION";
public static final String POLICY_ACTIONS = "POLICY_ACTIONS"; public static final String POLICY_ACTIONS = "POLICY_ACTIONS";
public static final String CORRECTIVE_POLICY_FEATURE_CODE = "CORRECTIVE_POLICY"; public static final String CORRECTIVE_POLICY_FEATURE_CODE = "CORRECTIVE_POLICY";
public static final String GEOFENCE_POLICY = "GEOFENCE_POLICY";
/** /**
Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME, Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME,

@ -37,6 +37,10 @@ package org.wso2.carbon.policy.mgt.core.util;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang.StringUtils;
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.w3c.dom.Document; import org.w3c.dom.Document;
@ -45,12 +49,15 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService;
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException;
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
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.policy.mgt.CorrectiveAction; import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.geo.service.GeoLocationProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
@ -143,7 +150,7 @@ public class PolicyManagerUtil {
List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList(); List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList();
PolicyOperation policyOperation = new PolicyOperation(); PolicyOperation policyOperation = new PolicyOperation();
policyOperation.setEnabled(true); policyOperation.setEnabled(true);
policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY); policyOperation.setType(Operation.Type.POLICY);
policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE); policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE);
policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures)); policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures));
if (policy.getPolicyType() != null && if (policy.getPolicyType() != null &&
@ -155,6 +162,7 @@ public class PolicyManagerUtil {
} }
if (payloadVersion >= 2.0f) { if (payloadVersion >= 2.0f) {
setMultipleCorrectiveActions(effectiveFeatures, policyOperation, policy); setMultipleCorrectiveActions(effectiveFeatures, policyOperation, policy);
transformGeoFencePolicy(effectiveFeatures, policyOperation, policy);
} else { } else {
setSingleCorrectiveAction(policy, effectiveFeatures); setSingleCorrectiveAction(policy, effectiveFeatures);
} }
@ -163,6 +171,61 @@ public class PolicyManagerUtil {
return policyOperation; return policyOperation;
} }
/**
* Transform geofence policy payload
* @param effectiveFeatures feature list of the policy
* @param policyOperation operation object
* @param policy related policy object
* @throws PolicyTransformException if any error occurs while transforming geo fence policy
*/
private static void transformGeoFencePolicy(List<ProfileFeature> effectiveFeatures,
PolicyOperation policyOperation, Policy policy) throws PolicyTransformException {
String payload = null;
for (ProfileFeature effectiveFeature : effectiveFeatures) {
if (effectiveFeature.getFeatureCode().equals(PolicyManagementConstants.GEOFENCE_POLICY)) {
payload = effectiveFeature.getContent().toString();
break;
}
}
for (ProfileOperation profileOperation : policyOperation.getProfileOperations()) {
int fenceId = -1;
try {
if (profileOperation.getCode().equals(PolicyManagementConstants.GEOFENCE_POLICY)) {
JsonParser jsonParser = new JsonParser();
if (payload != null) {
JsonElement parsedPayload = jsonParser.parse(payload);
JsonObject jsonPayload = parsedPayload.getAsJsonObject();
GeoLocationProviderServiceImpl geoLocationProviderService = new GeoLocationProviderServiceImpl();
if (jsonPayload.get("fenceId") == null) {
String msg = "No valid fence Id found in operation payload";
log.error(msg);
throw new PolicyTransformException(msg);
}
fenceId = jsonPayload.get("fenceId").getAsInt();
if (log.isDebugEnabled()) {
log.debug("Retrieving geofence with ID " + fenceId);
}
GeofenceData geofence = geoLocationProviderService.getGeofence(fenceId);
if (geofence != null) {
JsonObject operationPayload = new JsonObject();
operationPayload.addProperty("fenceId", geofence.getId());
operationPayload.addProperty("latitude", geofence.getLatitude());
operationPayload.addProperty("longitude", geofence.getLongitude());
operationPayload.addProperty("radius", geofence.getRadius());
operationPayload.addProperty("name", geofence.getFenceName());
profileOperation.setPayLoad(operationPayload.toString());
}
}
}
} catch (GeoLocationBasedServiceException e) {
String msg = "Error occurred while retrieving geofence with fence Id " + fenceId
+ " for the policy with Id "+policy.getId();
log.error(msg);
throw new PolicyTransformException(msg);
}
}
}
/** /**
* This method is used for generate single corrective action set for a single policy which is * This method is used for generate single corrective action set for a single policy which is
* bind to the policy payload * bind to the policy payload
@ -247,11 +310,11 @@ public class PolicyManagerUtil {
for (Policy correctivePolicy : allCorrectivePolicies) { for (Policy correctivePolicy : allCorrectivePolicies) {
if (policyId == correctivePolicy.getId()) { if (policyId == correctivePolicy.getId()) {
createCorrectiveProfileOperations(correctivePolicy, correctiveProfileOperation); createCorrectiveProfileOperations(correctivePolicy, correctiveProfileOperation);
policyOperation.getProfileOperations().add(correctiveProfileOperation);
break; break;
} }
} }
} }
policyOperation.getProfileOperations().add(correctiveProfileOperation);
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving corrective policy for policy " + String msg = "Error occurred while retrieving corrective policy for policy " +
policy.getPolicyName() + " and policy ID " + policy.getId(); policy.getPolicyName() + " and policy ID " + policy.getId();
@ -272,8 +335,8 @@ public class PolicyManagerUtil {
profileOperation.setId(correctivePolicy.getId()); profileOperation.setId(correctivePolicy.getId());
profileOperation.setCode(PolicyManagementConstants.POLICY_FEATURE_CODE); profileOperation.setCode(PolicyManagementConstants.POLICY_FEATURE_CODE);
profileOperation.setEnabled(true); profileOperation.setEnabled(true);
profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); profileOperation.setStatus(Operation.Status.PENDING);
profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); profileOperation.setType(Operation.Type.PROFILE);
List<ProfileOperation> profileOperations = createProfileOperations(correctivePolicy List<ProfileOperation> profileOperations = createProfileOperations(correctivePolicy
.getProfile().getProfileFeaturesList()); .getProfile().getProfileFeaturesList());
profileOperation.setPayLoad(profileOperations); profileOperation.setPayLoad(profileOperations);
@ -299,8 +362,8 @@ public class PolicyManagerUtil {
profileOperation.setCode(feature.getFeatureCode()); profileOperation.setCode(feature.getFeatureCode());
profileOperation.setEnabled(true); profileOperation.setEnabled(true);
profileOperation.setId(feature.getId()); profileOperation.setId(feature.getId());
profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); profileOperation.setStatus(Operation.Status.PENDING);
profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); profileOperation.setType(Operation.Type.PROFILE);
profileOperation.setPayLoad(feature.getContent()); profileOperation.setPayLoad(feature.getContent());
if (feature.getCorrectiveActions() != null) { if (feature.getCorrectiveActions() != null) {
for (CorrectiveAction correctiveAction : feature.getCorrectiveActions()) { for (CorrectiveAction correctiveAction : feature.getCorrectiveActions()) {

Loading…
Cancel
Save