From d06fc0e5f398b563f7b64326e33149c70014a1ad Mon Sep 17 00:00:00 2001 From: Pahansith Date: Tue, 29 Sep 2020 19:31:30 +0530 Subject: [PATCH 1/6] Add geofence policy transform --- .../core/util/PolicyManagementConstants.java | 1 + .../mgt/core/util/PolicyManagerUtil.java | 75 +++++++++++++++++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index fb97d097f9..3cf993c25c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -68,6 +68,7 @@ public final class PolicyManagementConstants { public static final String POLICY_FEATURE_CODE = "POLICY_ACTION"; public static final String POLICY_ACTIONS = "POLICY_ACTIONS"; 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, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index a40334f57e..c2daceb2aa 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -37,6 +37,10 @@ package org.wso2.carbon.policy.mgt.core.util; import com.google.gson.Gson; 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.LogFactory; 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.PlatformConfiguration; 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.operation.mgt.Operation; 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.policy.PolicyConfiguration; 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.ProfileOperation; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; @@ -143,7 +150,7 @@ public class PolicyManagerUtil { List effectiveFeatures = policy.getProfile().getProfileFeaturesList(); PolicyOperation policyOperation = new PolicyOperation(); 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.setProfileOperations(createProfileOperations(effectiveFeatures)); if (policy.getPolicyType() != null && @@ -155,6 +162,7 @@ public class PolicyManagerUtil { } if (payloadVersion >= 2.0f) { setMultipleCorrectiveActions(effectiveFeatures, policyOperation, policy); + transformGeoFencePolicy(effectiveFeatures, policyOperation, policy); } else { setSingleCorrectiveAction(policy, effectiveFeatures); } @@ -163,6 +171,61 @@ public class PolicyManagerUtil { 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 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 * bind to the policy payload @@ -247,11 +310,11 @@ public class PolicyManagerUtil { for (Policy correctivePolicy : allCorrectivePolicies) { if (policyId == correctivePolicy.getId()) { createCorrectiveProfileOperations(correctivePolicy, correctiveProfileOperation); - policyOperation.getProfileOperations().add(correctiveProfileOperation); break; } } } + policyOperation.getProfileOperations().add(correctiveProfileOperation); } catch (PolicyManagementException e) { String msg = "Error occurred while retrieving corrective policy for policy " + policy.getPolicyName() + " and policy ID " + policy.getId(); @@ -272,8 +335,8 @@ public class PolicyManagerUtil { profileOperation.setId(correctivePolicy.getId()); profileOperation.setCode(PolicyManagementConstants.POLICY_FEATURE_CODE); profileOperation.setEnabled(true); - profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); - profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); + profileOperation.setStatus(Operation.Status.PENDING); + profileOperation.setType(Operation.Type.PROFILE); List profileOperations = createProfileOperations(correctivePolicy .getProfile().getProfileFeaturesList()); profileOperation.setPayLoad(profileOperations); @@ -299,8 +362,8 @@ public class PolicyManagerUtil { profileOperation.setCode(feature.getFeatureCode()); profileOperation.setEnabled(true); profileOperation.setId(feature.getId()); - profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); - profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); + profileOperation.setStatus(Operation.Status.PENDING); + profileOperation.setType(Operation.Type.PROFILE); profileOperation.setPayLoad(feature.getContent()); if (feature.getCorrectiveActions() != null) { for (CorrectiveAction correctiveAction : feature.getCorrectiveActions()) { From 0a9fbe36547050ed2654f35f79f17ef66ef36b4f Mon Sep 17 00:00:00 2001 From: Pahansith Date: Wed, 30 Sep 2020 00:23:30 +0530 Subject: [PATCH 2/6] Improve code --- .../mgt/core/util/PolicyManagerUtil.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index c2daceb2aa..a71e247ff8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -181,23 +181,23 @@ public class PolicyManagerUtil { private static void transformGeoFencePolicy(List effectiveFeatures, PolicyOperation policyOperation, Policy policy) throws PolicyTransformException { String payload = null; + int fenceId = -1; 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) { + if (payload != null) { + for (ProfileOperation profileOperation : policyOperation.getProfileOperations()) { + try { + if (profileOperation.getCode().equals(PolicyManagementConstants.GEOFENCE_POLICY)) { + JsonParser jsonParser = new JsonParser(); 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"; + String msg = "No valid fence Id found in saved policy payload"; log.error(msg); throw new PolicyTransformException(msg); } @@ -216,12 +216,17 @@ public class PolicyManagerUtil { 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); } - } 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); + } + } else { + if (log.isDebugEnabled()) { + String msg = "No Geofence feature attached with the policy " + policy.getId(); + log.debug(msg); } } } From 9c362eabf09bd66c43dd709c9a9428115666af23 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Tue, 29 Sep 2020 19:31:30 +0530 Subject: [PATCH 3/6] Add geofence policy transform --- .../core/util/PolicyManagementConstants.java | 1 + .../mgt/core/util/PolicyManagerUtil.java | 75 +++++++++++++++++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index fb97d097f9..3cf993c25c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -68,6 +68,7 @@ public final class PolicyManagementConstants { public static final String POLICY_FEATURE_CODE = "POLICY_ACTION"; public static final String POLICY_ACTIONS = "POLICY_ACTIONS"; 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, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index a40334f57e..c2daceb2aa 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -37,6 +37,10 @@ package org.wso2.carbon.policy.mgt.core.util; import com.google.gson.Gson; 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.LogFactory; 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.PlatformConfiguration; 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.operation.mgt.Operation; 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.policy.PolicyConfiguration; 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.ProfileOperation; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; @@ -143,7 +150,7 @@ public class PolicyManagerUtil { List effectiveFeatures = policy.getProfile().getProfileFeaturesList(); PolicyOperation policyOperation = new PolicyOperation(); 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.setProfileOperations(createProfileOperations(effectiveFeatures)); if (policy.getPolicyType() != null && @@ -155,6 +162,7 @@ public class PolicyManagerUtil { } if (payloadVersion >= 2.0f) { setMultipleCorrectiveActions(effectiveFeatures, policyOperation, policy); + transformGeoFencePolicy(effectiveFeatures, policyOperation, policy); } else { setSingleCorrectiveAction(policy, effectiveFeatures); } @@ -163,6 +171,61 @@ public class PolicyManagerUtil { 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 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 * bind to the policy payload @@ -247,11 +310,11 @@ public class PolicyManagerUtil { for (Policy correctivePolicy : allCorrectivePolicies) { if (policyId == correctivePolicy.getId()) { createCorrectiveProfileOperations(correctivePolicy, correctiveProfileOperation); - policyOperation.getProfileOperations().add(correctiveProfileOperation); break; } } } + policyOperation.getProfileOperations().add(correctiveProfileOperation); } catch (PolicyManagementException e) { String msg = "Error occurred while retrieving corrective policy for policy " + policy.getPolicyName() + " and policy ID " + policy.getId(); @@ -272,8 +335,8 @@ public class PolicyManagerUtil { profileOperation.setId(correctivePolicy.getId()); profileOperation.setCode(PolicyManagementConstants.POLICY_FEATURE_CODE); profileOperation.setEnabled(true); - profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); - profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); + profileOperation.setStatus(Operation.Status.PENDING); + profileOperation.setType(Operation.Type.PROFILE); List profileOperations = createProfileOperations(correctivePolicy .getProfile().getProfileFeaturesList()); profileOperation.setPayLoad(profileOperations); @@ -299,8 +362,8 @@ public class PolicyManagerUtil { profileOperation.setCode(feature.getFeatureCode()); profileOperation.setEnabled(true); profileOperation.setId(feature.getId()); - profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); - profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); + profileOperation.setStatus(Operation.Status.PENDING); + profileOperation.setType(Operation.Type.PROFILE); profileOperation.setPayLoad(feature.getContent()); if (feature.getCorrectiveActions() != null) { for (CorrectiveAction correctiveAction : feature.getCorrectiveActions()) { From 206f433a9fcb855e6612e3da52407d08e3f68aeb Mon Sep 17 00:00:00 2001 From: Pahansith Date: Wed, 30 Sep 2020 00:23:30 +0530 Subject: [PATCH 4/6] Improve code --- .../mgt/core/util/PolicyManagerUtil.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index c2daceb2aa..a71e247ff8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -181,23 +181,23 @@ public class PolicyManagerUtil { private static void transformGeoFencePolicy(List effectiveFeatures, PolicyOperation policyOperation, Policy policy) throws PolicyTransformException { String payload = null; + int fenceId = -1; 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) { + if (payload != null) { + for (ProfileOperation profileOperation : policyOperation.getProfileOperations()) { + try { + if (profileOperation.getCode().equals(PolicyManagementConstants.GEOFENCE_POLICY)) { + JsonParser jsonParser = new JsonParser(); 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"; + String msg = "No valid fence Id found in saved policy payload"; log.error(msg); throw new PolicyTransformException(msg); } @@ -216,12 +216,17 @@ public class PolicyManagerUtil { 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); } - } 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); + } + } else { + if (log.isDebugEnabled()) { + String msg = "No Geofence feature attached with the policy " + policy.getId(); + log.debug(msg); } } } From 48e1241de64f09f6f897603f6deacb3ffee465a7 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Wed, 30 Sep 2020 21:44:03 +0530 Subject: [PATCH 5/6] Add email type corrective action transform --- .../core/util/PolicyManagementConstants.java | 3 ++ .../mgt/core/util/PolicyManagerUtil.java | 29 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index 3cf993c25c..2af19a9694 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -69,6 +69,9 @@ public final class PolicyManagementConstants { public static final String POLICY_ACTIONS = "POLICY_ACTIONS"; public static final String CORRECTIVE_POLICY_FEATURE_CODE = "CORRECTIVE_POLICY"; public static final String GEOFENCE_POLICY = "GEOFENCE_POLICY"; + public static final String EMAIL_CORRECTIVE_ACTION_TYPE = "EMAIL"; + public static final String EMAIL_FEATURE_CODE = "EMAIL_ACTION"; + public static final Integer EMAIL_ACTION_ID = 450; /** Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index a71e247ff8..d42a9d5c86 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -304,6 +304,9 @@ public class PolicyManagerUtil { if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE .equals(correctiveAction.getActionType())) { correctivePolicyIdSet.add(correctiveAction.getPolicyId()); + } else if (PolicyManagementConstants.EMAIL_CORRECTIVE_ACTION_TYPE + .equals(correctiveAction.getActionType())) { + createEmailCorrectiveActions(correctiveProfileOperation); } //Add check for another action type in future implementation } @@ -328,6 +331,20 @@ public class PolicyManagerUtil { } } + private static void createEmailCorrectiveActions(ProfileOperation correctiveProfileOperation) { + ProfileOperation profileOperation = new ProfileOperation(); + profileOperation.setId(PolicyManagementConstants.EMAIL_ACTION_ID); + profileOperation.setCode(PolicyManagementConstants.EMAIL_FEATURE_CODE); + profileOperation.setEnabled(true); + profileOperation.setStatus(Operation.Status.PENDING); + profileOperation.setType(Operation.Type.PROFILE); + List profileOperations = new ArrayList<>(); + profileOperation.setPayLoad(profileOperations); + List payLoad = new ArrayList<>(); + payLoad.add(profileOperation); + correctiveProfileOperation.setPayLoad(payLoad); + } + /** * This method is using for generate profile operations list which to be sent to the device. * this method is only using multiple corrective actions @@ -376,12 +393,20 @@ public class PolicyManagerUtil { if (profileOperation.getReactiveActionIds() == null) { profileOperation.setReactiveActionIds(new ArrayList<>()); } - profileOperation.getReactiveActionIds().add(correctiveAction.getPolicyId()); + if (correctiveAction.getActionType().equals(PolicyManagementConstants.EMAIL_CORRECTIVE_ACTION_TYPE)) { + profileOperation.getReactiveActionIds().add(PolicyManagementConstants.EMAIL_ACTION_ID); + } else if (correctiveAction.getActionType().equals(PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE)){ + profileOperation.getReactiveActionIds().add(correctiveAction.getPolicyId()); + } } else { if (profileOperation.getCorrectiveActionIds() == null) { profileOperation.setCorrectiveActionIds(new ArrayList<>()); } - profileOperation.getCorrectiveActionIds().add(correctiveAction.getPolicyId()); + if (correctiveAction.getActionType().equals(PolicyManagementConstants.EMAIL_CORRECTIVE_ACTION_TYPE)) { + profileOperation.getCorrectiveActionIds().add(PolicyManagementConstants.EMAIL_ACTION_ID); + } else if (correctiveAction.getActionType().equals(PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE)){ + profileOperation.getCorrectiveActionIds().add(correctiveAction.getPolicyId()); + } } } } From 618b8962b3c5f54b64c1d4954c14719e27a8958a Mon Sep 17 00:00:00 2001 From: Pahansith Date: Wed, 30 Sep 2020 22:08:31 +0530 Subject: [PATCH 6/6] Add doc comments --- .../wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index d42a9d5c86..850ecdcf9d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -331,6 +331,10 @@ public class PolicyManagerUtil { } } + /** + * Transform email type corrective actions + * @param correctiveProfileOperation Email type corrective operation + */ private static void createEmailCorrectiveActions(ProfileOperation correctiveProfileOperation) { ProfileOperation profileOperation = new ProfileOperation(); profileOperation.setId(PolicyManagementConstants.EMAIL_ACTION_ID);