From 51910ee67a2be01ba2b793bf5591649210112a54 Mon Sep 17 00:00:00 2001 From: harshanl Date: Thu, 13 Oct 2016 17:11:36 +0530 Subject: [PATCH] EMM-1675:Added policy revoke operation --- .../operation/mgt/OperationMgtConstants.java | 7 +++ .../policy/mgt/core/PolicyManagerService.java | 2 +- .../mgt/core/PolicyManagerServiceImpl.java | 4 +- .../mgt/core/enforcement/DelegationTask.java | 35 +++++++------- .../PolicyEnforcementDelegator.java | 2 + .../PolicyEnforcementDelegatorImpl.java | 48 ++++++++++++++++--- .../impl/PolicyAdministratorPointImpl.java | 8 +++- .../mgt/core/mgt/MonitoringManager.java | 2 +- .../core/mgt/impl/MonitoringManagerImpl.java | 2 +- 9 files changed, 80 insertions(+), 30 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java index 24bb47c0906..cd39b730600 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationMgtConstants.java @@ -29,4 +29,11 @@ public class OperationMgtConstants { public static final String DEVICE_ID_SERVICE_NOT_FOUND = "Issue in retrieving device management service instance for device found at %s"; } + + public final class OperationCodes { + private OperationCodes() { + throw new AssertionError(); + } + public static final String POLICY_REVOKE = "POLICY_REVOKE"; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java index 81ccf0eca41..96d7aac4d2f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java @@ -77,5 +77,5 @@ public interface PolicyManagerService { ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; - boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + boolean isCompliant(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index a3079fcb4c9..da127b70a97 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -201,7 +201,7 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { } @Override - public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { - return monitoringManager.isCompliance(deviceIdentifier); + public boolean isCompliant(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + return monitoringManager.isCompliant(deviceIdentifier); } } \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java index 9a7eab1c6d5..dea10f5fac2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -64,32 +64,33 @@ public class DelegationTask implements Task { if (!deviceTypes.isEmpty()) { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance() .getDeviceManagementService(); - List devices = new ArrayList<>(); + List devices; + List toBeNotified; for (String deviceType : deviceTypes) { try { + devices = new ArrayList<>(); + toBeNotified = new ArrayList<>(); devices.addAll(service.getAllDevices(deviceType)); + //HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); + for (Device device : devices) { + // if (deviceIdPolicy.containsKey(device.getId())) { + toBeNotified.add(device); + // } + } + if (!toBeNotified.isEmpty()) { + PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); + enforcementDelegator.delegate(); + } } catch (DeviceManagementException e) { - throw new PolicyManagementException("Error occurred while taking the devices", e); + throw new PolicyManagementException("Error occurred while fetching the devices", e); + } catch (PolicyDelegationException e) { + throw new PolicyManagementException("Error occurred while running the delegation task on " + + "device-type : " + deviceType, e); } } -// HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); - List toBeNotified = new ArrayList<>(); - - for (Device device : devices) { -// if (deviceIdPolicy.containsKey(device.getId())) { - toBeNotified.add(device); -// } - } - if (!toBeNotified.isEmpty()) { - PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); - enforcementDelegator.delegate(); - } } - } catch (PolicyManagementException e) { log.error("Error occurred while getting the policies applied to devices.", e); - } catch (PolicyDelegationException e) { - log.error("Error occurred while running the delegation task.", e); } } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java index 218fe0398ed..e782308c069 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java @@ -31,4 +31,6 @@ public interface PolicyEnforcementDelegator { void addPolicyOperation(List deviceIdentifiers, Policy policy) throws PolicyDelegationException; + void addPolicyRevokeOperation(List deviceIdentifiers) throws PolicyDelegationException; + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java index 4e2051e8b1a..90f7acd8756 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java @@ -23,14 +23,18 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; +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.OperationManager; +import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.util.ArrayList; @@ -58,7 +62,6 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato @Override public void delegate() throws PolicyDelegationException { - for (Device device : devices) { DeviceIdentifier identifier = new DeviceIdentifier(); identifier.setId(device.getDeviceIdentifier()); @@ -70,14 +73,17 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato List deviceIdentifiers = new ArrayList<>(); deviceIdentifiers.add(identifier); this.addPolicyOperation(deviceIdentifiers, policy); + } else { + //This means all the applicable policies have been removed. Hence sending policy-revoke operation. + List deviceIdentifiers = new ArrayList<>(); + deviceIdentifiers.add(identifier); + this.addPolicyRevokeOperation(deviceIdentifiers); } - } } @Override public Policy getEffectivePolicy(DeviceIdentifier identifier) throws PolicyDelegationException { - try { PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); return policyManagerService.getPEP().getEffectivePolicy(identifier); @@ -96,11 +102,13 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato @Override public void addPolicyOperation(List deviceIdentifiers, Policy policy) throws PolicyDelegationException { - try { - //ToDo Need to fix this to fetch OSGi service - OperationManager operationManager = new OperationManagerImpl(); - operationManager.addOperation(PolicyManagerUtil.transformPolicy(policy), deviceIdentifiers); + String type = null; + if (deviceIdentifiers.size() > 0) { + type = deviceIdentifiers.get(0).getType(); + } + PolicyManagementDataHolder.getInstance().getDeviceManagementService().addOperation(type, + PolicyManagerUtil.transformPolicy(policy), deviceIdentifiers); } catch (InvalidDeviceException e) { String msg = "Invalid DeviceIdentifiers found."; log.error(msg, e); @@ -110,7 +118,33 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato log.error(msg, e); throw new PolicyDelegationException(msg, e); } + } + @Override + public void addPolicyRevokeOperation(List deviceIdentifiers) throws PolicyDelegationException { + try { + String type = null; + if (deviceIdentifiers.size() > 0) { + type = deviceIdentifiers.get(0).getType(); + } + PolicyManagementDataHolder.getInstance().getDeviceManagementService().addOperation(type, + this.getPolicyRevokeOperation(), deviceIdentifiers); + } catch (InvalidDeviceException e) { + String msg = "Invalid DeviceIdentifiers found."; + log.error(msg, e); + throw new PolicyDelegationException(msg, e); + } catch (OperationManagementException e) { + String msg = "Error occurred while adding the operation to device."; + log.error(msg, e); + throw new PolicyDelegationException(msg, e); + } } + private Operation getPolicyRevokeOperation() { + CommandOperation policyRevokeOperation = new CommandOperation(); + policyRevokeOperation.setEnabled(true); + policyRevokeOperation.setCode(OperationMgtConstants.OperationCodes.POLICY_REVOKE); + policyRevokeOperation.setType(Operation.Type.COMMAND); + return policyRevokeOperation; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index b96e8f1cd70..1f82cb780b5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -114,7 +114,13 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public boolean deletePolicy(int policyId) throws PolicyManagementException { boolean bool =policyManager.deletePolicy(policyId); - PolicyCacheManagerImpl.getInstance().rePopulateCache(); + PolicyCacheManager policyCacheManager = PolicyCacheManagerImpl.getInstance(); + policyCacheManager.rePopulateCache(); + List appliedPolicies = policyCacheManager.getAllPolicies(); + //This means all the policies have been deleted. Hence triggering publishChanges to take immediate effect. + if (appliedPolicies.isEmpty()) { + this.publishChanges(); + } return bool; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java index 2b6e7d5eae7..3eb809d2ae0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java @@ -33,7 +33,7 @@ public interface MonitoringManager { throws PolicyComplianceException; - boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + boolean isCompliant(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 91d16cb01a9..8643fd87342 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -171,7 +171,7 @@ public class MonitoringManagerImpl implements MonitoringManager { } @Override - public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + public boolean isCompliant(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService();