From 458618191d323fb5ac1255313a71c2e6fbd6a378 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 31 Aug 2015 19:31:13 +0530 Subject: [PATCH] Fixing datasource exaust issue, Fixing the enrolmentinfo getId method, adding policy cache manager. Changing the test cases --- .../device/mgt/common/EnrolmentInfo.java | 9 ++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 7 +- .../org.wso2.carbon.policy.mgt.core/pom.xml | 2 - .../mgt/core/cache/PolicyCacheManager.java | 11 +- .../cache/impl/PolicyCacheManagerImpl.java | 120 ++++++++++++++++-- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 14 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 41 ++++-- .../mgt/core/enforcement/DelegationTask.java | 3 + .../impl/PolicyAdministratorPointImpl.java | 47 +++++-- .../core/mgt/impl/MonitoringManagerImpl.java | 2 + .../mgt/core/mgt/impl/PolicyManagerImpl.java | 81 +++++++----- .../policy/mgt/core/task/MonitoringTask.java | 11 +- .../core/task/TaskScheduleServiceImpl.java | 56 +++++--- .../policy/mgt/core/PolicyDAOTestCase.java | 63 +++++---- .../src/test/resources/sql/CreateH2TestDB.sql | 15 +-- .../src/main/resources/dbscripts/cdm/h2.sql | 11 +- 16 files changed, 342 insertions(+), 151 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index 0a3f32ba62..d6c399d130 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -32,6 +32,7 @@ public class EnrolmentInfo implements Serializable{ BYOD, COPE } + private int id; private Device device; private Long dateOfEnrolment; private Long dateOfLastUpdate; @@ -48,6 +49,14 @@ public class EnrolmentInfo implements Serializable{ this.status = status; } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public Long getDateOfEnrolment() { return dateOfEnrolment; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 69c297d7f5..746b7d4bdb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -113,7 +113,7 @@ public class DeviceDAOImpl implements DeviceDAO { conn = this.getConnection(); String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, " + - "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT " + + "e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID " + "FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " + @@ -210,7 +210,7 @@ public class DeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + + "e.DATE_OF_ENROLMENT , e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT t.NAME AS DEVICE_TYPE, d.ID, d.DESCRIPTION, " + "d.NAME, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?"; @@ -475,6 +475,7 @@ public class DeviceDAOImpl implements DeviceDAO { private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setId(rs.getInt("ENROLMENT_ID")); enrolmentInfo.setOwner(rs.getString("OWNER")); enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); @@ -493,7 +494,7 @@ public class DeviceDAOImpl implements DeviceDAO { String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " + "AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID " + "AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index 953650fd8e..a2333b475d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -55,8 +55,6 @@ Policy Management Core Bundle org.wso2.carbon.policy.mgt.core.internal - org.apache.axis2.*; - version="${axis2.osgi.version.range}", org.osgi.framework, org.osgi.service.component, org.apache.commons.logging, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java index 573df93e38..63b3a666c7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java @@ -19,7 +19,8 @@ package org.wso2.carbon.policy.mgt.core.cache; -import org.wso2.carbon.device.mgt.core.policy.mgt.policy.Policy; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import java.util.List; @@ -29,7 +30,9 @@ public interface PolicyCacheManager { void updateAllPolicies(List policies); - List getAllPolicies(); + List getAllPolicies() throws PolicyManagementException; + + void rePopulateCache() throws PolicyManagementException; void removeAllPolicies(); @@ -37,9 +40,11 @@ public interface PolicyCacheManager { void updatePolicy(Policy policy); + void updatePolicy(int policyId) throws PolicyManagementException; + void removePolicy(int policyId); - Policy getPolicy(int policyId); + Policy getPolicy(int policyId) throws PolicyManagementException; void addPolicyToDevice(int deviceId, int policyId); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java index d7c8046755..677b30920d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java @@ -19,51 +19,145 @@ package org.wso2.carbon.policy.mgt.core.cache.impl; -import org.wso2.carbon.device.mgt.core.policy.mgt.policy.Policy; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; +import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; +import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Set; public class PolicyCacheManagerImpl implements PolicyCacheManager { + private static final Log log = LogFactory.getLog(PolicyCacheManagerImpl.class); + + private static HashMap> tenantedPolicyMap = new HashMap<>(); + + private static PolicyCacheManagerImpl policyCacheManager; + + private PolicyCacheManagerImpl() { + } + + public static PolicyCacheManager getInstance() { + if (policyCacheManager == null) { + synchronized (PolicyCacheManagerImpl.class) { + if (policyCacheManager == null) { + policyCacheManager = new PolicyCacheManagerImpl(); + } + } + } + return policyCacheManager; + } + @Override public void addAllPolicies(List policies) { + HashMap map = this.getTenantRelatedMap(); + if (map.isEmpty()) { + for (Policy policy : policies) { + map.put(policy.getId(), policy); + } + } } @Override public void updateAllPolicies(List policies) { + HashMap map = this.getTenantRelatedMap(); + map.clear(); + if (map.isEmpty()) { + for (Policy policy : policies) { + map.put(policy.getId(), policy); + } + } + } + @Override + public List getAllPolicies() throws PolicyManagementException { + HashMap map = this.getTenantRelatedMap(); + if (map.isEmpty()) { + PolicyManager policyManager = new PolicyManagerImpl(); + this.addAllPolicies(policyManager.getPolicies()); + } + if (log.isDebugEnabled()) { + log.debug("No of policies stored in the cache .. : " + map.size()); + + Set keySet = map.keySet(); + for (Integer x : keySet) { + log.debug("Policy id in maps .. : " + map.get(x).getId() + " policy name : " + map.get(x). + getPolicyName() + " Activated : " + map.get(x).isActive()); + } + } + return new ArrayList<>(map.values()); } @Override - public List getAllPolicies() { - return null; + public void rePopulateCache() throws PolicyManagementException { + + this.removeAllPolicies(); + this.getAllPolicies(); } @Override public void removeAllPolicies() { - + HashMap map = this.getTenantRelatedMap(); + map.clear(); } @Override public void addPolicy(Policy policy) { - + HashMap map = this.getTenantRelatedMap(); + if (!map.containsKey(policy.getId())) { + map.put(policy.getId(), policy); + } else { + log.warn("Policy id (" + policy.getId() + ") already exist in the map. hence not attempted to store."); + } } @Override public void updatePolicy(Policy policy) { + HashMap map = this.getTenantRelatedMap(); + if (map.containsKey(policy.getId())) { + map.remove(policy.getId()); + map.put(policy.getId(), policy); + } + } + @Override + public void updatePolicy(int policyId) throws PolicyManagementException { + HashMap map = this.getTenantRelatedMap(); + if (map.containsKey(policyId)) { + this.removePolicy(policyId); + } + PolicyManager policyManager = new PolicyManagerImpl(); + Policy policy = policyManager.getPolicy(policyId); + map.put(policyId, policy); } @Override public void removePolicy(int policyId) { - + HashMap map = this.getTenantRelatedMap(); + if (map.containsKey(policyId)) { + map.remove(policyId); + } else { + log.warn("Policy id (" + policyId + ") does not exist in the cache. Hence not removed."); + } } @Override - public Policy getPolicy(int policyId) { - return null; + public Policy getPolicy(int policyId) throws PolicyManagementException { + HashMap map = this.getTenantRelatedMap(); + if (!map.containsKey(policyId)) { + this.removeAllPolicies(); + this.getAllPolicies(); + } + return map.get(policyId); + } @Override @@ -80,4 +174,14 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager { public int getPolicyIdOfDevice(int deviceId) { return 0; } + + private HashMap getTenantRelatedMap(){ + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if(!tenantedPolicyMap.containsKey(tenantId)){ + HashMap policyMap = new HashMap<>(); + tenantedPolicyMap.put(tenantId, policyMap); + } + return tenantedPolicyMap.get(tenantId); + } } 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 d0c04add50..9f5ba5b255 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 @@ -106,23 +106,23 @@ public interface PolicyDAO { List getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException; - void addEffectivePolicyToDevice(int deviceId, Policy policy) + void addEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) throws PolicyManagerDAOException; - void setPolicyApplied(int deviceId) throws PolicyManagerDAOException; + void setPolicyApplied(int deviceId, int enrollmentId) throws PolicyManagerDAOException; - void updateEffectivePolicyToDevice(int deviceId, Policy policy) + void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) throws PolicyManagerDAOException; - boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; + boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException; int getPolicyCount() throws PolicyManagerDAOException; - int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException; + int getAppliedPolicyId(int deviceId, int enrollmentId) throws PolicyManagerDAOException; - Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException; + Policy getAppliedPolicy(int deviceId, int enrollmentId) throws PolicyManagerDAOException; HashMap getAppliedPolicyIds(List deviceIds) throws PolicyManagerDAOException; HashMap getAppliedPolicyIdsDeviceIds() 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 8f7d8d5e3b..05406bb0ca 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 @@ -119,17 +119,22 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)"; + String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID, ENROLMENT_ID, DEVICE) VALUES (?, ?, " + + "?, ?)"; stmt = conn.prepareStatement(query); for (Device device : devices) { stmt.setInt(1, device.getId()); stmt.setInt(2, policy.getId()); + stmt.setInt(3, device.getEnrolmentInfo().getId()); + stmt.setBytes(4, PolicyManagerUtil.getBytes(device)); stmt.addBatch(); } stmt.executeBatch(); } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the device ids with policy to " + "database", e); + } catch (IOException e) { + throw new PolicyManagerDAOException("Error occurred while getting the byte array from device.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -881,7 +886,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { + public void addEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) throws + PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); @@ -889,7 +895,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED (DEVICE_ID, POLICY_ID, POLICY_CONTENT, " + - "CREATED_TIME, UPDATED_TIME, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?)"; + "CREATED_TIME, UPDATED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); stmt.setInt(2, policy.getId()); @@ -897,6 +903,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(5, currentTimestamp); stmt.setInt(6, tenantId); + stmt.setInt(7, enrolmentId); stmt.executeUpdate(); } catch (SQLException | IOException e) { throw new PolicyManagerDAOException("Error occurred while adding the evaluated feature list to device", e); @@ -907,7 +914,7 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public void setPolicyApplied(int deviceId) throws PolicyManagerDAOException { + public void setPolicyApplied(int deviceId, int enrollmentId) throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); @@ -915,12 +922,13 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? AND" + - " TENANT_ID = ?"; + " TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setBoolean(2, true); stmt.setInt(3, deviceId); stmt.setInt(4, tenantId); + stmt.setInt(5, enrollmentId); stmt.executeUpdate(); } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while updating applied policy to device (" + @@ -932,7 +940,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { + public void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) throws + PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); @@ -940,7 +949,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + - "APPLIED = ? WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + "APPLIED = ? WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); stmt.setBytes(2, PolicyManagerUtil.getBytes(policy)); @@ -948,6 +957,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setBoolean(4, false); stmt.setInt(5, deviceId); stmt.setInt(6, tenantId); + stmt.setInt(7, enrolmentId); stmt.executeUpdate(); } catch (SQLException | IOException e) { @@ -959,7 +969,7 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException { + public boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -967,10 +977,12 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + String query = "SELECT * 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, enrollmentId); resultSet = stmt.executeQuery(); exist = resultSet.next(); } catch (SQLException e) { @@ -1282,17 +1294,18 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException { + public int getAppliedPolicyId(int deviceId, int enrollmentId) throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + String query = "SELECT * 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, enrollmentId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -1307,7 +1320,7 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException { + public Policy getAppliedPolicy(int deviceId, int enrollmentId) throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -1315,10 +1328,12 @@ public class PolicyDAOImpl implements PolicyDAO { Policy policy = null; try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + String query = "SELECT * 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, enrollmentId); resultSet = stmt.executeQuery(); while (resultSet.next()) { 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 33ce6bd606..5e6ca873cc 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 @@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; @@ -57,6 +58,8 @@ public class DelegationTask implements Task { PolicyManager policyManager = new PolicyManagerImpl(); List deviceTypes = policyManager.applyChangesMadeToPolicies(); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); + if (log.isDebugEnabled()) { log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size()); } 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 393c869b82..b17fd2bd6e 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 @@ -27,6 +27,8 @@ import org.wso2.carbon.ntask.core.TaskInfo; import org.wso2.carbon.ntask.core.TaskManager; import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; +import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; @@ -47,12 +49,14 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { private PolicyManager policyManager; private ProfileManager profileManager; private FeatureManager featureManager; + private PolicyCacheManager cacheManager; // private PolicyEnforcementDelegator delegator; public PolicyAdministratorPointImpl() { this.policyManager = new PolicyManagerImpl(); this.profileManager = new ProfileManagerImpl(); this.featureManager = new FeatureManagerImpl(); + this.cacheManager = PolicyCacheManagerImpl.getInstance(); // this.delegator = new PolicyEnforcementDelegatorImpl(); } @@ -64,6 +68,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { // } catch (PolicyDelegationException e) { // throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); // } + PolicyCacheManagerImpl.getInstance().addPolicy(resultantPolicy); return resultantPolicy; } @@ -75,32 +80,41 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { // } catch (PolicyDelegationException e) { // throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); // } + PolicyCacheManagerImpl.getInstance().updatePolicy(resultantPolicy); return resultantPolicy; } @Override public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { - return policyManager.updatePolicyPriorities(policies); + boolean bool = policyManager.updatePolicyPriorities(policies); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); + return bool; } @Override public void activatePolicy(int policyId) throws PolicyManagementException { policyManager.activatePolicy(policyId); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); } @Override public void inactivatePolicy(int policyId) throws PolicyManagementException { policyManager.inactivatePolicy(policyId); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); } @Override public boolean deletePolicy(Policy policy) throws PolicyManagementException { - return policyManager.deletePolicy(policy); + boolean bool = policyManager.deletePolicy(policy); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); + return bool; } @Override public boolean deletePolicy(int policyId) throws PolicyManagementException { - return policyManager.deletePolicy(policyId); + boolean bool =policyManager.deletePolicy(policyId); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); + return bool; } @Override @@ -127,20 +141,23 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId); - if(!taskManager.isTaskScheduled(taskName)) { + if (!taskManager.isTaskScheduled(taskName)) { - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, + properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); } else { - throw new PolicyManagementException("There is a task already running for policy changes. Please try to apply " + + throw new PolicyManagementException("There is a task already running for policy changes. Please try " + + "to apply " + "changes after few minutes."); } } catch (TaskException e) { - String msg = "Error occurred while creating the policy delegation task for tenant " + PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(); + String msg = "Error occurred while creating the policy delegation task for tenant " + + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(); log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -185,22 +202,26 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws PolicyManagementException { - return policyManager.addPolicyToDevice(deviceIdentifierList, policy); + policy = policyManager.addPolicyToDevice(deviceIdentifierList, policy); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); + return policy; } @Override public Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagementException { - return policyManager.addPolicyToRole(roleNames, policy); + policy = policyManager.addPolicyToRole(roleNames, policy); + PolicyCacheManagerImpl.getInstance().rePopulateCache(); + return policy; } @Override public List getPolicies() throws PolicyManagementException { - return policyManager.getPolicies(); + return PolicyCacheManagerImpl.getInstance().getAllPolicies(); } @Override public Policy getPolicy(int policyId) throws PolicyManagementException { - return policyManager.getPolicy(policyId); + return PolicyCacheManagerImpl.getInstance().getPolicy(policyId); } @Override @@ -311,7 +332,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public int getPolicyCount() throws PolicyManagementException { - return policyManager.getPolicyCount(); + return PolicyCacheManagerImpl.getInstance().getAllPolicies().size(); } } 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 89f96dea2c..6b52b04e7a 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 @@ -248,6 +248,8 @@ public class MonitoringManagerImpl implements MonitoringManager { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); } catch (PolicyManagerDAOException e) { throw new PolicyComplianceException("SQL error occurred while getting policy details.", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } Map deviceIdsToAddOperation = new HashMap<>(); 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 89589200e1..53c322236f 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 @@ -29,6 +29,8 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; +import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; @@ -44,6 +46,7 @@ public class PolicyManagerImpl implements PolicyManager { private ProfileDAO profileDAO; private FeatureDAO featureDAO; private ProfileManager profileManager; + private PolicyCacheManager policyCacheManager; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); public PolicyManagerImpl() { @@ -188,15 +191,16 @@ public class PolicyManagerImpl implements PolicyManager { public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { boolean bool; try { - List existingPolicies = this.getPolicies(); +// List existingPolicies = this.getPolicies(); + List existingPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); PolicyManagementDAOFactory.beginTransaction(); bool = policyDAO.updatePolicyPriorities(policies); // This logic is added because ui sends only policy id and priority to update priorities. for (Policy policy : policies) { - for(Policy exPolicy: existingPolicies) { - if(policy.getId() == exPolicy.getId()) { + for (Policy exPolicy : existingPolicies) { + if (policy.getId() == exPolicy.getId()) { policy.setProfile(exPolicy.getProfile()); } } @@ -553,7 +557,8 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.closeConnection(); } - List tempPolicyList = this.getPolicies(); +// List tempPolicyList = this.getPolicies(); + List tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); for (Policy policy : tempPolicyList) { for (Integer i : policyIdList) { @@ -570,22 +575,29 @@ public class PolicyManagerImpl implements PolicyManager { @Override public List getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException { List policies = new ArrayList<>(); - try { - List profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); - List allPolicies = this.getPolicies(); - - for (Profile profile : profileList) { - for (Policy policy : allPolicies) { - if (policy.getProfileId() == profile.getProfileId()) { - policy.setProfile(profile); - policies.add(policy); - } - } +// try { + // List profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); +// List allPolicies = this.getPolicies(); + List allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); + + for (Policy policy : allPolicies) { + if (policy.getProfile().getDeviceType().getName().equalsIgnoreCase(deviceTypeName)) { + policies.add(policy); } - Collections.sort(policies); - } catch (ProfileManagementException e) { - throw new PolicyManagementException("Error occurred while getting all the profile features.", e); } + +// for (Profile profile : profileList) { +// for (Policy policy : allPolicies) { +// if (policy.getProfileId() == profile.getProfileId()) { +// policy.setProfile(profile); +// policies.add(policy); +// } +// } +// } + Collections.sort(policies); +// } catch (ProfileManagementException e) { +// throw new PolicyManagementException("Error occurred while getting all the profile features.", e); +// } return policies; } @@ -607,7 +619,8 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.closeConnection(); } - List tempPolicyList = this.getPolicies(); +// List tempPolicyList = this.getPolicies(); + List tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); for (Policy policy : tempPolicyList) { for (Integer i : policyIdList) { @@ -636,7 +649,8 @@ public class PolicyManagerImpl implements PolicyManager { } finally { PolicyManagementDAOFactory.closeConnection(); } - List tempPolicyList = this.getPolicies(); +// List tempPolicyList = this.getPolicies(); + List tempPolicyList = PolicyCacheManagerImpl.getInstance().getAllPolicies(); for (Policy policy : tempPolicyList) { for (Integer i : policyIdList) { @@ -665,14 +679,14 @@ public class PolicyManagerImpl implements PolicyManager { HashMap allDeviceMap = new HashMap<>(); - if(!allDevices.isEmpty()) { + if (!allDevices.isEmpty()) { allDeviceMap = PolicyManagerUtil.covertDeviceListToMap(allDevices); } for (int deviceId : deviceIds) { - if(allDeviceMap.containsKey(deviceId)){ - if(log.isDebugEnabled()) { + if (allDeviceMap.containsKey(deviceId)) { + if (log.isDebugEnabled()) { log.debug("Policy Applied device ids .............: " + deviceId + " - Policy Id " + policyId); } deviceList.add(allDeviceMap.get(deviceId)); @@ -706,11 +720,11 @@ public class PolicyManagerImpl implements PolicyManager { deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); - boolean exist = policyDAO.checkPolicyAvailable(deviceId); + boolean exist = policyDAO.checkPolicyAvailable(deviceId, device.getEnrolmentInfo().getId()); if (exist) { - policyDAO.updateEffectivePolicyToDevice(deviceId, policy); + policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } else { - policyDAO.addEffectivePolicyToDevice(deviceId, policy); + policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { @@ -736,7 +750,8 @@ public class PolicyManagerImpl implements PolicyManager { // List inactivePolicies = new ArrayList<>(); List updatedPolicyIds = new ArrayList<>(); - List allPolicies = this.getPolicies(); +// List allPolicies = this.getPolicies(); + List allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); for (Policy policy : allPolicies) { if (policy.isUpdated()) { @@ -777,13 +792,13 @@ public class PolicyManagerImpl implements PolicyManager { deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); - Policy policySaved = policyDAO.getAppliedPolicy(deviceId); + Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); if (policySaved != null && policySaved.getId() != 0) { if (policy.getId() != policySaved.getId()) { - policyDAO.updateEffectivePolicyToDevice(deviceId, policy); + policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } } else { - policyDAO.addEffectivePolicyToDevice(deviceId, policy); + policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { @@ -807,7 +822,7 @@ public class PolicyManagerImpl implements PolicyManager { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); PolicyManagementDAOFactory.openConnection(); - exist = policyDAO.checkPolicyAvailable(device.getId()); + exist = policyDAO.checkPolicyAvailable(device.getId(), device.getEnrolmentInfo().getId()); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while checking whether device has a policy " + "to apply.", e); @@ -829,7 +844,7 @@ public class PolicyManagerImpl implements PolicyManager { Device device = service.getDevice(deviceIdentifier); PolicyManagementDAOFactory.openConnection(); - policyDAO.setPolicyApplied(device.getId()); + policyDAO.setPolicyApplied(device.getId(), device.getEnrolmentInfo().getId()); return true; } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while setting the policy has applied to device (" + @@ -869,7 +884,7 @@ public class PolicyManagerImpl implements PolicyManager { Device device = service.getDevice(deviceIdentifier); //int policyId = policyDAO.getAppliedPolicyId(device.getId()); PolicyManagementDAOFactory.openConnection(); - policy = policyDAO.getAppliedPolicy(device.getId()); + policy = policyDAO.getAppliedPolicy(device.getId(), device.getEnrolmentInfo().getId()); } catch (DeviceManagementException e) { throw new PolicyManagementException("Error occurred while getting device id.", e); } catch (PolicyManagerDAOException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index b8146e26e2..cff114bf40 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -76,10 +76,17 @@ public class MonitoringTask implements Task { if (!deviceTypes.isEmpty()) { try { + + DeviceManagementProviderService deviceManagementProviderService = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); for (DeviceType deviceType : deviceTypes) { + + if(log.isDebugEnabled()){ + log.debug("Running task for device type : " + deviceType.getName() ); + } + PolicyMonitoringService monitoringService = PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName()); List devices = deviceManagementProviderService.getAllDevices(deviceType.getName()); @@ -90,7 +97,7 @@ public class MonitoringTask implements Task { if (log.isDebugEnabled()) { log.debug("Removing inactive and blocked devices from the list for the device type : " + - deviceType); + deviceType.getName()); } for (Device device : devices) { if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.INACTIVE) || @@ -101,7 +108,7 @@ public class MonitoringTask implements Task { } } if (log.isDebugEnabled()) { - log.debug("Following devices selected to send the notification for " + deviceType); + log.debug("Following devices selected to send the notification for " + deviceType.getName()); for (Device device : notifiableDevices) { log.debug(device.getDeviceIdentifier()); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java index d12ce10d03..75550744ca 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java @@ -76,22 +76,28 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, - properties, triggerInfo); + if (!taskManager.isTaskScheduled(taskName)) { - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, + properties, triggerInfo); + + taskManager.registerTask(taskInfo); + taskManager.rescheduleTask(taskInfo.getName()); + } else { + throw new PolicyMonitoringTaskException("Monitoring task is already started for this tenant " + + tenantId); + } } catch (TaskException e) { - throw new PolicyMonitoringTaskException("Error occurred while creating the task for tenant " + PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(), e); + throw new PolicyMonitoringTaskException("Error occurred while creating the task for tenant " + + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(), e); } } else { throw new PolicyMonitoringTaskException("Policy monitoring is not enabled in the cdm-config.xml."); } - } @Override @@ -103,8 +109,9 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); taskManager.deleteTask(taskName); } catch (TaskException e) { - throw new PolicyMonitoringTaskException("Error occurred while deleting the task for tenant " + PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(), e); + throw new PolicyMonitoringTaskException("Error occurred while deleting the task for tenant " + + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(), e); } } @@ -116,23 +123,30 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); - taskManager.deleteTask(taskName); - TriggerInfo triggerInfo = new TriggerInfo(); - triggerInfo.setIntervalMillis(monitoringFrequency); - triggerInfo.setRepeatCount(-1); + if (taskManager.isTaskScheduled(taskName)) { + + taskManager.deleteTask(taskName); + TriggerInfo triggerInfo = new TriggerInfo(); + triggerInfo.setIntervalMillis(monitoringFrequency); + triggerInfo.setRepeatCount(-1); - Map properties = new HashMap<>(); - properties.put("tenantId", String.valueOf(tenantId)); + Map properties = new HashMap<>(); + properties.put("tenantId", String.valueOf(tenantId)); - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, - triggerInfo); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, + triggerInfo); - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); + taskManager.registerTask(taskInfo); + taskManager.rescheduleTask(taskInfo.getName()); + } else { + throw new PolicyMonitoringTaskException("Monitoring task has not been started for this tenant " + + tenantId + ". Please start the task first."); + } } catch (TaskException e) { - throw new PolicyMonitoringTaskException("Error occurred while updating the task for tenant " + PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(), e); + throw new PolicyMonitoringTaskException("Error occurred while updating the task for tenant " + + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(), e); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index b836aaac4b..e8ab64cf01 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -32,12 +32,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceIm import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; -import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; -import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; -import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; -import org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImpl; -import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; -import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl; import org.wso2.carbon.policy.mgt.core.util.*; import java.util.ArrayList; @@ -87,7 +81,6 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } - DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); PolicyManagementDataHolder.getInstance().setDeviceManagementService(service); @@ -119,38 +112,39 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { @Test(dependsOnMethods = ("addFeatures")) public void addProfileFeatures() throws ProfileManagementException { - ProfileManager profileManager = new ProfileManagerImpl(); - Profile profile = ProfileCreator.getProfile2(FeatureCreator.getFeatureList2()); +// ProfileManager profileManager = new ProfileManagerImpl(); +// Profile profile = ProfileCreator.getProfile2(FeatureCreator.getFeatureList2()); // profileManager.addProfile(profile); // profileFeatureList = profile.getProfileFeaturesList(); } @Test(dependsOnMethods = ("addProfileFeatures")) public void addPolicy() throws PolicyManagementException, ProfileManagementException { - ProfileManager profileManager = new ProfileManagerImpl(); +// ProfileManager profileManager = new ProfileManagerImpl(); Profile profile = ProfileCreator.getProfile5(FeatureCreator.getFeatureList5()); - profileManager.addProfile(profile); - PolicyManager policyManager = new PolicyManagerImpl(); +// profileManager.addProfile(profile); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); policy = PolicyCreator.createPolicy(profile); - policyManager.addPolicy(policy); + policy = pap.addPolicy(policy); + pap.activatePolicy(policy.getId()); } @Test(dependsOnMethods = ("addPolicy")) public void addPolicyToRole() throws PolicyManagementException { - PolicyManager policyManager = new PolicyManagerImpl(); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); List roles = new ArrayList(); roles.add("Test_ROLE_01"); roles.add("Test_ROLE_02"); roles.add("Test_ROLE_03"); - policyManager.addPolicyToRole(roles, policy); + pap.addPolicyToRole(roles, policy); } @Test(dependsOnMethods = ("addPolicyToRole")) public void addPolicyToDevice() throws PolicyManagementException { - PolicyManager policyManager = new PolicyManagerImpl(); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); Device device = DeviceCreator.getSingleDevice(); List deviceIdentifierList = new ArrayList(); @@ -159,29 +153,31 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { deviceIdentifier.setType("android"); deviceIdentifierList.add(deviceIdentifier); - policyManager.addPolicyToDevice(deviceIdentifierList, policy); + pap.addPolicyToDevice(deviceIdentifierList, policy); } @Test(dependsOnMethods = ("addPolicyToDevice")) public void addNewPolicy() throws PolicyManagementException, ProfileManagementException { - ProfileManager profileManager = new ProfileManagerImpl(); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); Profile profile = ProfileCreator.getProfile3(FeatureCreator.getFeatureList3()); - profileManager.addProfile(profile); - PolicyManager policyManager = new PolicyManagerImpl(); + //pap.addProfile(profile); + // PolicyManager policyManager = new PolicyManagerImpl(); Policy policy = PolicyCreator.createPolicy2(profile); - policyManager.addPolicy(policy); + policy = pap.addPolicy(policy); + pap.activatePolicy(policy.getId()); } @Test(dependsOnMethods = ("addPolicyToDevice")) public void addThirdPolicy() throws PolicyManagementException, ProfileManagementException { - ProfileManager profileManager = new ProfileManagerImpl(); + //ProfileManager profileManager = new ProfileManagerImpl(); Profile profile = ProfileCreator.getProfile4(FeatureCreator.getFeatureList4()); - profileManager.addProfile(profile); - PolicyManager policyManager = new PolicyManagerImpl(); + //profileManager.addProfile(profile); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); Policy policy = PolicyCreator.createPolicy4(profile); - policyManager.addPolicy(policy); + policy = pap.addPolicy(policy); + pap.activatePolicy(policy.getId()); } @Test(dependsOnMethods = ("addNewPolicy")) @@ -273,26 +269,29 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { @Test(dependsOnMethods = ("getRoleRelatedPolicy")) public void addSecondPolicy() throws PolicyManagementException, ProfileManagementException { - ProfileManager profileManager = new ProfileManagerImpl(); + // ProfileManager profileManager = new ProfileManagerImpl(); Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList()); - profileManager.addProfile(profile); - PolicyManager policyManager = new PolicyManagerImpl(); + //profileManager.addProfile(profile); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); Policy policy = PolicyCreator.createPolicy3(profile); - policyManager.addPolicy(policy); + policy = pap.addPolicy(policy); + pap.activatePolicy(policy.getId()); } @Test(dependsOnMethods = ("addSecondPolicy")) public void updatedPolicy() throws PolicyManagementException { - PolicyManager policyManager = new PolicyManagerImpl(); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); Profile profile = ProfileCreator.getProfile3(FeatureCreator.getFeatureList3()); Policy policy = PolicyCreator.createPolicy3(profile); policy.setPolicyName("Policy_05"); - policy = policyManager.addPolicy(policy); + policy = pap.addPolicy(policy); + pap.activatePolicy(policy.getId()); List users = new ArrayList<>(); users.add("Udara"); users.add("Dileesha"); policy.setUsers(users); - policyManager.updatePolicy(policy); + pap.updatePolicy(policy); + pap.activatePolicy(policy.getId()); } @Test(dependsOnMethods = ("updatedPolicy")) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 4a32bc8db7..f221ba0e78 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -160,6 +160,8 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , DEVICE_ID INT(11) NOT NULL , + ENROLMENT_ID INT(11) NOT NULL, + DEVICE BLOB NOT NULL, POLICY_ID INT(11) NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_POLICY_DEVICE_POLICY @@ -245,8 +247,9 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED ( - ID INT NOT NULL AUTO_INCREMENT , - DEVICE_ID INT NOT NULL , + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT NOT NULL, + ENROLMENT_ID INT(11) NOT NULL, POLICY_ID INT NOT NULL , POLICY_CONTENT BLOB NULL , TENANT_ID INT NOT NULL, @@ -316,6 +319,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( ID INT NOT NULL AUTO_INCREMENT, DEVICE_ID INT NOT NULL, + ENROLMENT_ID INT(11) NOT NULL, POLICY_ID INT NOT NULL, TENANT_ID INT NOT NULL, STATUS INT NULL, @@ -352,12 +356,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT ( POLICY_ID INT NOT NULL, DEVICE_TYPE_ID INT NOT NULL, TENANT_ID INT(11) NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_POLICY_CHANGE_MGT_POLICY - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION + PRIMARY KEY (ID) ); -- POLICY RELATED TABLES FINISHED -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 1e645d2554..781ea7640b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -145,6 +145,8 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , DEVICE_ID INT(11) NOT NULL , + ENROLMENT_ID INT(11) NOT NULL, + DEVICE BLOB NOT NULL, POLICY_ID INT(11) NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_POLICY_DEVICE_POLICY @@ -232,6 +234,7 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED ( ID INT NOT NULL AUTO_INCREMENT , DEVICE_ID INT NOT NULL , + ENROLMENT_ID INT(11) NOT NULL, POLICY_ID INT NOT NULL , POLICY_CONTENT BLOB NULL , TENANT_ID INT NOT NULL, @@ -297,6 +300,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( ID INT NOT NULL AUTO_INCREMENT, DEVICE_ID INT NOT NULL, + ENROLMENT_ID INT(11) NOT NULL, POLICY_ID INT NOT NULL, TENANT_ID INT NOT NULL, STATUS INT NULL, @@ -319,12 +323,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT ( POLICY_ID INT NOT NULL, DEVICE_TYPE_ID INT NOT NULL, TENANT_ID INT(11) NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_POLICY_CHANGE_MGT_POLICY - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION + PRIMARY KEY (ID) );