From 370f6de260f1e27699f609f9d014363afa066a4b Mon Sep 17 00:00:00 2001 From: harshanl Date: Tue, 18 Oct 2016 17:10:34 +0530 Subject: [PATCH] Removed applied policy upon removal of the last policy applicable for a device --- .../mgt/common/PolicyAdministratorPoint.java | 7 ++++ .../carbon/policy/mgt/core/dao/PolicyDAO.java | 2 + .../mgt/core/dao/impl/PolicyDAOImpl.java | 24 +++++++++++ .../impl/PolicyAdministratorPointImpl.java | 5 +++ .../policy/mgt/core/mgt/PolicyManager.java | 2 + .../mgt/core/mgt/impl/PolicyManagerImpl.java | 40 +++++++++++++------ .../services/SimplePolicyEvaluationTest.java | 6 +-- .../decision/point/SimpleEvaluationImpl.java | 4 +- 8 files changed, 72 insertions(+), 18 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 2774c3bf4f..358ee5ef66 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -131,6 +131,13 @@ public interface PolicyAdministratorPoint { */ void setPolicyUsed(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; + /** + * This method will remove the policy applied to the device. + * @param deviceIdentifier + * @throws PolicyManagementException + */ + void removePolicyUsed(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; + /** * This method will add the profile to database, * @param profile diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index db36c6ec99..609be0a533 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -137,6 +137,8 @@ public interface PolicyDAO { void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) throws PolicyManagerDAOException; + void deleteEffectivePolicyToDevice(int deviceId, int enrolmentId) throws PolicyManagerDAOException; + boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException; int getPolicyCount() throws PolicyManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index d622270e92..7b6da5a882 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -1163,6 +1163,30 @@ public class PolicyDAOImpl implements PolicyDAO { } } + @Override + public void deleteEffectivePolicyToDevice(int deviceId, int enrolmentId) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ? " + + "AND ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + stmt.setInt(3, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PolicyManagerDAOException("Error occurred while deleting the effective policy " + + "to device", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException { Connection conn; 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 e742a10ff4..871b490b73 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 @@ -274,6 +274,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { policyManager.addAppliedPolicyToDevice(deviceIdentifier, policy); } + @Override + public void removePolicyUsed(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + policyManager.removeAppliedPolicyToDevice(deviceIdentifier); + } + @Override public Profile addProfile(Profile profile) throws PolicyManagementException { try { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index 5e02d98859..b9cfe358ae 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -72,6 +72,8 @@ public interface PolicyManager { void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; + void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; + boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 7dc5fbfa64..2fbb9d84e1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -763,34 +763,23 @@ public class PolicyManagerImpl implements PolicyManager { List deviceList = new ArrayList<>(); List deviceIds; - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); List allDevices = service.getAllDevices(); - PolicyManagementDAOFactory.openConnection(); - - //int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); - - HashMap allDeviceMap = new HashMap<>(); - if (!allDevices.isEmpty()) { allDeviceMap = PolicyManagerUtil.covertDeviceListToMap(allDevices); } - for (int deviceId : deviceIds) { - if (allDeviceMap.containsKey(deviceId)) { if (log.isDebugEnabled()) { log.debug("Policy Applied device ids .............: " + deviceId + " - Policy Id " + policyId); } deviceList.add(allDeviceMap.get(deviceId)); } - //TODO FIX ME -- This is wrong, Device id is not device identifier, so converting is wrong. - //deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId)); } } catch (PolicyManagerDAOException e) { @@ -804,7 +793,6 @@ public class PolicyManagerImpl implements PolicyManager { } finally { PolicyManagementDAOFactory.closeConnection(); } - return deviceList; } @@ -912,6 +900,34 @@ public class PolicyManagerImpl implements PolicyManager { } } + @Override + public void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + + int deviceId = -1; + try { + DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); + Device device = service.getDevice(deviceIdentifier); + deviceId = device.getId(); + PolicyManagementDAOFactory.beginTransaction(); + + Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); + if (policySaved != null) { + policyDAO.deleteEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId()); + } + PolicyManagementDAOFactory.commitTransaction(); + } catch (PolicyManagerDAOException e) { + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while removing the applied policy to device (" + + deviceId + ")", e); + } catch (DeviceManagementException e) { + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while getting the device details (" + + deviceIdentifier.getId() + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); + } + } + @Override public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java index 4895155371..b4bed5da4d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java @@ -48,7 +48,7 @@ public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint { policyInformationPoint = policyManagerService.getPIP(); PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier); policyList = policyInformationPoint.getRelatedPolicies(pipDevice); - + policyAdministratorPoint = policyManagerService.getPAP(); for(Policy pol : policyList) { log.debug("Policy used in evaluation - Name : " + pol.getPolicyName() ); } @@ -57,12 +57,10 @@ public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint { if(!policyList.isEmpty()) { policy = policyList.get(0); } else { + policyAdministratorPoint.removePolicyUsed(deviceIdentifier); return null; } - - policyAdministratorPoint = policyManagerService.getPAP(); policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); - } } catch (PolicyManagementException e) { diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java index f7b7135eb5..13cde3e181 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java @@ -53,15 +53,15 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { policyInformationPoint = policyManagerService.getPIP(); PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier); policyList = policyInformationPoint.getRelatedPolicies(pipDevice); - + policyAdministratorPoint = policyManagerService.getPAP(); sortPolicies(); if(!policyList.isEmpty()) { policy = policyList.get(0); } else { + policyAdministratorPoint.removePolicyUsed(deviceIdentifier); return null; } //TODO : UNCOMMENT THE FOLLOWING CASE - policyAdministratorPoint = policyManagerService.getPAP(); policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); }